You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
31 KiB

2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "xsync.hpp"
  2. #include <string.h>
  3. #include <map>
  4. #include "xsync_utils.hpp"
  5. #define TAG "XSYNC"
  6. using namespace xsync;
  7. using namespace std;
  8. /**
  9. * @brief XSYNC协议端口
  10. */
  11. #define IFLYTOP_XSYNC_SERVICE_XSYNC_PORT 19900 // xsync端端口
  12. #define IFLYTOP_XSYNC_SERVICE_PC_PORT 19901 // pc 端端口
  13. #define IFLYTOP_XSYNC_TIMECODE_REPORT_XSYNC_PORT 19902 // xsync端端口
  14. #define IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT 19903 // pc端端口
  15. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_XSYNC_PORT 13013 // xsync端端口
  16. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT 13014 // pc端端口
  17. #define DO_XSYNC(exptr) \
  18. { \
  19. xs_error_code_t ecode = exptr; \
  20. if (ecode != kxs_ec_success) return ecode; \
  21. }
  22. #define REG_WRITE(reg, value) \
  23. { \
  24. DO_XSYNC(reg_write(reg, value, 10)); \
  25. return kxs_ec_success; \
  26. }
  27. #define REG_READ(reg, value) \
  28. { \
  29. uint32_t readbak = 0; \
  30. DO_XSYNC(reg_read(reg, readbak, 10)); \
  31. value = (decltype(value))readbak; \
  32. return kxs_ec_success; \
  33. }
  34. /*******************************************************************************
  35. * Xsync *
  36. *******************************************************************************/
  37. Xsync::Xsync(/* args */) {}
  38. Xsync &Xsync::Ins() {
  39. static Xsync xsync;
  40. return xsync;
  41. }
  42. void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
  43. xs_error_code_t Xsync::connect(string xsync_ip) {
  44. lock_guard<recursive_mutex> lock(lock_);
  45. m_xsync_ip = xsync_ip;
  46. disConnect();
  47. /**
  48. * @brief m_xsync_reg_udp
  49. */
  50. xs_error_code_t ecode = kxs_ec_success;
  51. auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
  52. ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
  53. if (ecode != kxs_ec_success) {
  54. return ecode;
  55. }
  56. /**
  57. * @brief m_xsync_timecode_udp_listener
  58. */
  59. auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
  60. ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
  61. if (ecode != kxs_ec_success) {
  62. return ecode;
  63. }
  64. ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
  65. if (ecode != kxs_ec_success) {
  66. return ecode;
  67. }
  68. /**
  69. * @brief m_xsync_camera_sync_udp_listener
  70. */
  71. auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
  72. ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
  73. if (ecode != kxs_ec_success) {
  74. return ecode;
  75. }
  76. ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
  77. if (ecode != kxs_ec_success) {
  78. return ecode;
  79. }
  80. m_xsync_reg_udp = xsync_reg_udp;
  81. m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
  82. m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
  83. m_net_state = kxsync_net_state_connected;
  84. return ecode;
  85. }
  86. xs_error_code_t Xsync::disConnect() {
  87. lock_guard<recursive_mutex> lock(lock_);
  88. if (m_xsync_reg_udp != nullptr) {
  89. m_xsync_reg_udp->stopReceive();
  90. m_xsync_reg_udp = nullptr;
  91. }
  92. if (m_xsync_timecode_udp_listener != nullptr) {
  93. m_xsync_timecode_udp_listener->stopReceive();
  94. m_xsync_timecode_udp_listener = nullptr;
  95. }
  96. if (m_xsync_camera_sync_udp_listener != nullptr) {
  97. m_xsync_camera_sync_udp_listener->stopReceive();
  98. m_xsync_camera_sync_udp_listener = nullptr;
  99. }
  100. m_net_state = kxsync_net_state_disconnect;
  101. return kxs_ec_success;
  102. }
  103. xsync_net_state_t Xsync::getNetState() { return m_net_state; }
  104. void Xsync::registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t cb) { m_on_timecode_msg_cb = cb; }
  105. void Xsync::registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t cb) { m_on_camera_sync_msg_cb = cb; }
  106. void Xsync::registerOnRecordSigChangeMsgCallback(xsync_on_record_sig_change_msg_t cb) { m_on_record_sig_change_msg_cb = cb; }
  107. xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize, int32_t overtime_ms) {
  108. lock_guard<recursive_mutex> lock(lock_);
  109. if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
  110. m_xsync_reg_udp->clearRxBuffer();
  111. cmd->index = txpacket_index++;
  112. XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
  113. xs_error_code_t ecode = //
  114. m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
  115. if (ecode != kxs_ec_success) {
  116. return ecode;
  117. }
  118. XsyncNetAdd fromadd;
  119. while (true) {
  120. ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, overtime_ms);
  121. if (ecode != kxs_ec_success) {
  122. return ecode;
  123. }
  124. if (rx_data->index != cmd->index) {
  125. continue;
  126. }
  127. break;
  128. }
  129. return (xs_error_code_t)rx_data->data[0];
  130. }
  131. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, int32_t overtime_ms) { //
  132. uint32_t readbak = 0;
  133. return reg_write(regadd, regvalue, readbak, overtime_ms);
  134. }
  135. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms) {
  136. /**
  137. * @brief
  138. *
  139. *
  140. * kxsync_packet_type_reg_write
  141. * tx: regadd,regdata
  142. * rx: ecode,regdata
  143. */
  144. uint8_t txdata[128] = {0};
  145. uint8_t rxdata[128] = {0};
  146. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  147. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  148. txpacket->type = kxsync_packet_type_cmd;
  149. txpacket->index = txpacket_index++;
  150. txpacket->cmd = kxsync_packet_type_reg_write;
  151. txpacket->ndata = 2;
  152. txpacket->data[0] = regadd;
  153. txpacket->data[1] = regvalue;
  154. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  155. if (ecode != kxs_ec_success) {
  156. return ecode;
  157. }
  158. regbackvalue = rxpacket->data[1];
  159. return ecode;
  160. }
  161. xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms) {
  162. /**
  163. * @brief
  164. *
  165. *
  166. * kxsync_packet_type_reg_write
  167. * tx: regadd,regdata
  168. * rx: ecode,regdata
  169. */
  170. uint8_t txdata[128] = {0};
  171. uint8_t rxdata[128] = {0};
  172. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  173. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  174. txpacket->type = kxsync_packet_type_cmd;
  175. txpacket->index = txpacket_index++;
  176. txpacket->cmd = kxsync_packet_type_reg_read;
  177. txpacket->ndata = 2;
  178. txpacket->data[0] = regadd;
  179. txpacket->data[1] = regvalue;
  180. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  181. if (ecode != kxs_ec_success) {
  182. return ecode;
  183. }
  184. regvalue = rxpacket->data[1];
  185. return ecode;
  186. }
  187. xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms) {
  188. /**
  189. * @brief
  190. *
  191. *
  192. * kxsync_packet_type_reg_read_regs
  193. * tx: regstartadd,nreg
  194. * rx: ecode,regdatas
  195. */
  196. uint8_t txdata[128] = {0};
  197. uint8_t rxdata[1280] = {0};
  198. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  199. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  200. txpacket->type = kxsync_packet_type_cmd;
  201. txpacket->index = txpacket_index++;
  202. txpacket->cmd = kxsync_packet_type_reg_read_regs;
  203. txpacket->ndata = 2;
  204. txpacket->data[0] = regadd;
  205. txpacket->data[1] = nreg;
  206. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  207. if (ecode != kxs_ec_success) {
  208. return ecode;
  209. }
  210. if (rxpacket->ndata > 0) {
  211. for (int i = 0; i < rxpacket->ndata - 1; i++) {
  212. regvalues.push_back(rxpacket->data[i + 1]);
  213. }
  214. }
  215. return ecode;
  216. }
  217. xs_error_code_t Xsync::readtimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t &timecode) {
  218. uint32_t readbak = 0;
  219. xs_error_code_t ecode = kxs_ec_success;
  220. uint32_t tc0 = 0;
  221. uint32_t tc1 = 0;
  222. ecode = reg_read(reg0, tc0, 10);
  223. if (ecode != kxs_ec_success) return ecode;
  224. ecode = reg_read(reg1, tc1, 10);
  225. if (ecode != kxs_ec_success) return ecode;
  226. Timecode64_t tc64;
  227. tc64.tc0 = tc0;
  228. tc64.tc1 = tc1;
  229. timecode = timecode64ToXsyncTimeCode(tc64);
  230. return ecode;
  231. }
  232. xs_error_code_t Xsync::writetimecode(uint32_t reg0, uint32_t reg1, XsyncTimecode_t timecode) {
  233. uint32_t readbak = 0;
  234. xs_error_code_t ecode = kxs_ec_success;
  235. Timecode64_t tc64 = timecodeTo64(timecode);
  236. ecode = reg_write(reg0, tc64.tc0, readbak, 10);
  237. if (ecode != kxs_ec_success) return ecode;
  238. ecode = reg_write(reg1, tc64.tc1, readbak, 10);
  239. if (ecode != kxs_ec_success) return ecode;
  240. return ecode;
  241. }
  242. xs_error_code_t Xsync::readfreq(uint32_t reg, float &freqfloat) {
  243. uint32_t freq_cnt = 0;
  244. DO_XSYNC(reg_read(reg, freq_cnt));
  245. if (freq_cnt == 0) {
  246. freqfloat = 0;
  247. }
  248. if (freq_cnt != 0) {
  249. uint32_t freq_1000x = ((1.0 / (freq_cnt * 1.0 / (10 * 1000 * 1000))) * 1000 + 0.5); //+0.5是因为c++ 小数强转成整数时是取整,而非四舍五入
  250. // ZLOGI(TAG, "freq_10x %f", freq_10x);
  251. freqfloat = freq_1000x / 1000.0;
  252. } else {
  253. freqfloat = 0;
  254. }
  255. return kxs_ec_success;
  256. }
  257. void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  258. iflytop_xsync_event_report_packet_t *packet = (iflytop_xsync_event_report_packet_t *)data;
  259. if (packet->eventid == ktimecode_report_event) {
  260. Timecode64_t tc64;
  261. tc64.tc0 = packet->data[0];
  262. tc64.tc1 = packet->data[1];
  263. XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
  264. if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
  265. } else if (packet->eventid == kxsync_work_state_report_event) {
  266. // 信号发生器状态改变
  267. Timecode64_t tc64;
  268. tc64.tc0 = packet->data[1];
  269. tc64.tc1 = packet->data[2];
  270. XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
  271. if (m_on_record_sig_change_msg_cb) m_on_record_sig_change_msg_cb(packet->data[0], &timecode);
  272. }
  273. }
  274. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  275. uint32_t count = 0;
  276. uint32_t data0 = data[7];
  277. uint32_t data1 = data[6];
  278. uint32_t data2 = data[5];
  279. uint32_t data3 = data[4];
  280. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  281. xysnc_camera_sync_data_t camera_sync_data;
  282. camera_sync_data.frameIndex = count;
  283. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  284. }
  285. xs_error_code_t Xsync::generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); }
  286. xs_error_code_t Xsync::factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); }
  287. xs_error_code_t Xsync::reboot() { return doaction(xsync_stm32_action_reboot, 0, nullptr); }
  288. xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); }
  289. xs_error_code_t Xsync::changeNetworkConfig(string ip, string mask, string gateway) {
  290. uint32_t ip32 = 0;
  291. uint32_t mask32 = 0;
  292. uint32_t gateway32 = 0;
  293. xs_error_code_t ecode;
  294. bool suc = false;
  295. ip32 = (uint32_t)ipToUint32(ip.c_str(), suc);
  296. if (!suc) return kxs_ec_param_error;
  297. mask32 = (uint32_t)ipToUint32(mask.c_str(), suc);
  298. if (!suc) return kxs_ec_param_error;
  299. gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc);
  300. if (!suc) return kxs_ec_param_error;
  301. uint32_t readbak = 0;
  302. ecode = reg_write(reg::kstm32_ip, ip32, readbak);
  303. if (ecode != kxs_ec_success) return ecode;
  304. ecode = reg_write(reg::kstm32_netmask, mask32, readbak);
  305. if (ecode != kxs_ec_success) return ecode;
  306. ecode = reg_write(reg::kstm32_gw, gateway32, readbak);
  307. if (ecode != kxs_ec_success) return ecode;
  308. ecode = storageConfig();
  309. if (ecode != kxs_ec_success) return ecode;
  310. return kxs_ec_success;
  311. }
  312. xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) {
  313. //
  314. uint32_t readbak = 0;
  315. xs_error_code_t ecode;
  316. ecode = reg_write(reg::kstm32_action_val0, actionval, readbak);
  317. if (ecode != kxs_ec_success) return ecode;
  318. ecode = reg_write(reg::kstm32_action0, action, readbak, overtime_ms);
  319. if (ecode != kxs_ec_success) return ecode;
  320. if (ackreturn) *ackreturn = readbak;
  321. return ecode;
  322. }
  323. /*******************************************************************************
  324. * TTLInputModule *
  325. *******************************************************************************/
  326. #define FREQ_CNT_TO_FREQ(cnt) ((cnt != 0) ? (uint32_t)(1.0 / (cnt * 1.0 / (10 * 1000 * 1000)) + 0.5) : 0) //+0.5是因为c++ 小数强转成整数时是取整,而非四舍五入
  327. xs_error_code_t Xsync::TTLInputModule1_detectFreq(uint32_t &freq) {
  328. uint32_t freq_cnt = 0;
  329. DO_XSYNC(reg_read(reg::k_ttlin1_freq_detector_reg, freq_cnt, 10));
  330. if (freq_cnt == 0) {
  331. freq = 0;
  332. }
  333. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  334. return kxs_ec_success;
  335. }
  336. xs_error_code_t Xsync::TTLInputModule2_detectFreq(uint32_t &freq) {
  337. uint32_t freq_cnt = 0;
  338. DO_XSYNC(reg_read(reg::k_ttlin2_freq_detector_reg, freq_cnt, 10));
  339. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  340. return kxs_ec_success;
  341. }
  342. xs_error_code_t Xsync::TTLInputModule3_detectFreq(uint32_t &freq) {
  343. uint32_t freq_cnt = 0;
  344. DO_XSYNC(reg_read(reg::k_ttlin3_freq_detector_reg, freq_cnt, 10));
  345. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  346. return kxs_ec_success;
  347. }
  348. xs_error_code_t Xsync::TTLInputModule4_detectFreq(uint32_t &freq) {
  349. uint32_t freq_cnt = 0;
  350. DO_XSYNC(reg_read(reg::k_ttlin4_freq_detector_reg, freq_cnt, 10));
  351. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  352. return kxs_ec_success;
  353. }
  354. /*******************************************************************************
  355. * TTLOutputModule *
  356. *******************************************************************************/
  357. // 0:固定输出低电平,1:固定输出高电平,2:分频倍频模式,3:转发模式,4:测试模式
  358. xs_error_code_t Xsync::TTLOutputModule1_setSrcSigType(SignalType_t source) {
  359. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  360. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 2, 10)); // 分频倍频模式
  361. } else {
  362. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 3, 10)); // 转发模式
  363. }
  364. REG_WRITE(reg::kreg_ttlout1_input_signal_select, source);
  365. }
  366. xs_error_code_t Xsync::TTLOutputModule2_setSrcSigType(SignalType_t source) {
  367. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  368. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 2, 10)); // 分频倍频模式
  369. } else {
  370. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 3, 10)); // 转发模式
  371. }
  372. REG_WRITE(reg::kreg_ttlout2_input_signal_select, source);
  373. }
  374. xs_error_code_t Xsync::TTLOutputModule3_setSrcSigType(SignalType_t source) {
  375. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  376. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 2, 10)); // 分频倍频模式
  377. } else {
  378. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 3, 10)); // 转发模式
  379. }
  380. REG_WRITE(reg::kreg_ttlout3_input_signal_select, source);
  381. }
  382. xs_error_code_t Xsync::TTLOutputModule4_setSrcSigType(SignalType_t source) {
  383. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  384. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 2, 10)); // 分频倍频模式
  385. } else {
  386. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 3, 10)); // 转发模式
  387. }
  388. REG_WRITE(reg::kreg_ttlout4_input_signal_select, source);
  389. }
  390. xs_error_code_t Xsync::TTLOutputModule1_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout1_input_signal_select, source); }
  391. xs_error_code_t Xsync::TTLOutputModule2_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout2_input_signal_select, source); }
  392. xs_error_code_t Xsync::TTLOutputModule3_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout3_input_signal_select, source); }
  393. xs_error_code_t Xsync::TTLOutputModule4_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout4_input_signal_select, source); }
  394. xs_error_code_t Xsync::TTLOutputModule1_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  395. xs_error_code_t Xsync::TTLOutputModule2_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  396. xs_error_code_t Xsync::TTLOutputModule3_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  397. xs_error_code_t Xsync::TTLOutputModule4_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  398. xs_error_code_t Xsync::TTLOutputModule1_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  399. xs_error_code_t Xsync::TTLOutputModule2_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  400. xs_error_code_t Xsync::TTLOutputModule3_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  401. xs_error_code_t Xsync::TTLOutputModule4_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  402. xs_error_code_t Xsync::TTLOutputModule1_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  403. xs_error_code_t Xsync::TTLOutputModule2_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  404. xs_error_code_t Xsync::TTLOutputModule3_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  405. xs_error_code_t Xsync::TTLOutputModule4_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  406. xs_error_code_t Xsync::TTLOutputModule1_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  407. xs_error_code_t Xsync::TTLOutputModule2_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  408. xs_error_code_t Xsync::TTLOutputModule3_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  409. xs_error_code_t Xsync::TTLOutputModule4_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  410. xs_error_code_t Xsync::TTLOutputModule1_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_in_freq_detect, freq); }
  411. xs_error_code_t Xsync::TTLOutputModule2_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_in_freq_detect, freq); }
  412. xs_error_code_t Xsync::TTLOutputModule3_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_in_freq_detect, freq); }
  413. xs_error_code_t Xsync::TTLOutputModule4_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_in_freq_detect, freq); }
  414. xs_error_code_t Xsync::TTLOutputModule1_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_out_freq_detect, freq); }
  415. xs_error_code_t Xsync::TTLOutputModule2_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_out_freq_detect, freq); }
  416. xs_error_code_t Xsync::TTLOutputModule3_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_out_freq_detect, freq); }
  417. xs_error_code_t Xsync::TTLOutputModule4_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_out_freq_detect, freq); }
  418. /*******************************************************************************
  419. * TimecodeInputModule *
  420. *******************************************************************************/
  421. xs_error_code_t Xsync::ExternalTimecode_setSource(InputInterface_t src) {
  422. if (src == INPUT_IF_TIMECODE_BNC) {
  423. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 1, 10)); // 0:off,1:bnc,2:headphone
  424. } else if (src == INPUT_IF_TIMECODE_HEADPHONE) {
  425. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 2, 10)); // 0:off,1:bnc,2:headphone
  426. } else if (src == INPUT_IF_OFF) {
  427. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 0, 10)); // 0:off,1:bnc,2:headphone
  428. } else {
  429. return kxs_ec_param_error;
  430. }
  431. return kxs_ec_success;
  432. }
  433. xs_error_code_t Xsync::ExternalTimecode_getSource(InputInterface_t &timecode_select) {
  434. uint32_t readbak = 0;
  435. DO_XSYNC(reg_read(reg::external_timecode_sig_selt, readbak, 10));
  436. if (readbak == 1) {
  437. timecode_select = INPUT_IF_TIMECODE_BNC;
  438. } else if (readbak == 2) {
  439. timecode_select = INPUT_IF_TIMECODE_HEADPHONE;
  440. } else if (readbak == 0) {
  441. timecode_select = INPUT_IF_OFF;
  442. } else {
  443. timecode_select = INPUT_IF_OFF;
  444. }
  445. return kxs_ec_success;
  446. }
  447. xs_error_code_t Xsync::ExternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::external_timecode_format, format); }
  448. xs_error_code_t Xsync::ExternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::external_timecode_format, format); }
  449. xs_error_code_t Xsync::ExternalTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::external_timecode_code0, reg::external_timecode_code1, timecode); }
  450. /*******************************************************************************
  451. * InternalTimecode *
  452. *******************************************************************************/
  453. xs_error_code_t Xsync::InternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::internal_timecode_format, format); }
  454. xs_error_code_t Xsync::InternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::internal_timecode_format, format); }
  455. xs_error_code_t Xsync::InternalTimecode_setCode(XsyncTimecode_t timecode) {
  456. DO_XSYNC(reg_write(reg::internal_timecode_en, 0));
  457. DO_XSYNC(writetimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode));
  458. DO_XSYNC(reg_write(reg::internal_timecode_en, 1));
  459. return kxs_ec_success;
  460. }
  461. xs_error_code_t Xsync::InternalTimecode_getCode(XsyncTimecode_t &timecode) { return readtimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode); }
  462. /*******************************************************************************
  463. * SysTimecode *
  464. *******************************************************************************/
  465. xs_error_code_t Xsync::SysTimecode_setSource(uint32_t sig) { REG_WRITE(reg::sys_timecode_select, sig); }
  466. xs_error_code_t Xsync::SysTimecode_getSource(uint32_t &sig) { REG_READ(reg::sys_timecode_select, sig); }
  467. xs_error_code_t Xsync::SysTimecode_readFormat(TimecodeFormat_t &format) { REG_READ(reg::sys_timecode_format, format); }
  468. xs_error_code_t Xsync::SysTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::sys_timecode_data0, reg::sys_timecode_data1, timecode); }
  469. /*******************************************************************************
  470. * TimecodeOutputModule *
  471. *******************************************************************************/
  472. xs_error_code_t Xsync::TimecodeOutputModule_setBncOutputLevel(int level) { REG_WRITE(reg::timecode_output_bnc_outut_level_select, level); }
  473. xs_error_code_t Xsync::TimecodeOutputModule_getBncOutputLevel(int &level) { REG_READ(reg::timecode_output_bnc_outut_level_select, level); }
  474. xs_error_code_t Xsync::TimecodeOutputModule_setHeadphoneOutputLevel(int level) { REG_WRITE(reg::timecode_output_headphone_outut_level_select, level); }
  475. xs_error_code_t Xsync::TimecodeOutputModule_getHeadphoneOutputLevel(int &level) { REG_READ(reg::timecode_output_headphone_outut_level_select, level); }
  476. /*******************************************************************************
  477. * GENLOCK *
  478. *******************************************************************************/
  479. xs_error_code_t Xsync::ExternalGenlock_detectFreq(float &freq) { return readfreq(reg::external_genlock_freq, freq); }
  480. xs_error_code_t Xsync::InternalGenlock_setFormat(GenlockFormat_t format) { return reg_write(reg::internal_genlock_format, format); }
  481. xs_error_code_t Xsync::InternalGenlock_getFormat(GenlockFormat_t &format) { REG_READ(reg::internal_genlock_format, format); }
  482. xs_error_code_t Xsync::SysGenlock_setSrc(uint32_t source) { return reg_write(reg::sys_genlock_source, source); }
  483. xs_error_code_t Xsync::SysGenlock_getSrc(uint32_t &extern_or_internal) {
  484. REG_READ(reg::sys_genlock_source, extern_or_internal);
  485. return kxs_ec_success;
  486. }
  487. xs_error_code_t Xsync::SysGenlock_readFreq(float &freq) { return readfreq(reg::sys_genlock_freq, freq); }
  488. /*******************************************************************************
  489. * INTERNAL_CLOCK *
  490. *******************************************************************************/
  491. xs_error_code_t Xsync::InternalClock_setFreq(float freq) {
  492. double T = 1.0 / freq;
  493. double T_ns = T * 1000 * 1000 * 1000;
  494. double cnt = T_ns / 100 + 0.5; // 10MHZ <=> 100ns
  495. uint32_t cnt_u32 = uint32_t(cnt);
  496. return reg_write(reg::internal_clock_freq, cnt_u32);
  497. return kxs_ec_success;
  498. }
  499. xs_error_code_t Xsync::InternalClock_getFreq(float &freq) { return readfreq(reg::internal_clock_freq, freq); }
  500. /*******************************************************************************
  501. * SysClock *
  502. *******************************************************************************/
  503. xs_error_code_t Xsync::SysClock_setSrc(SignalType_t sig) { return reg_write(reg::sys_clock_source, sig); }
  504. xs_error_code_t Xsync::SysClock_getSrc(SignalType_t &sig) { REG_READ(reg::sys_clock_source, sig); }
  505. xs_error_code_t Xsync::SysClock_setTriggerEdge(TriggerEdge_t edge) { return reg_write(reg::sys_clock_trigger_edge_select, edge); }
  506. xs_error_code_t Xsync::SysClock_getTriggerEdge(TriggerEdge_t &edge) { return _reg_read(reg::sys_clock_trigger_edge_select, edge); }
  507. xs_error_code_t Xsync::SysClock_setFreqDivision(uint32_t div) { return reg_write(reg::sys_clock_freq_division_ctrl, div); }
  508. xs_error_code_t Xsync::SysClock_geFreqtDivision(uint32_t &div) { return _reg_read(reg::sys_clock_freq_division_ctrl, div); }
  509. xs_error_code_t Xsync::SysClock_setFreqMultiplication(uint32_t muti) { return reg_write(reg::sys_clock_freq_multiplication_ctrl, muti); }
  510. xs_error_code_t Xsync::SysClock_getFreqMultiplication(uint32_t &muti) { return _reg_read(reg::sys_clock_freq_multiplication_ctrl, muti); }
  511. xs_error_code_t Xsync::SysClock_readOutSigFreq(float &freq) { return readfreq(reg::sys_clock_outfreq_detect, freq); }
  512. xs_error_code_t Xsync::SysClock_readInSigFreq(float &freq) { return readfreq(reg::sys_clock_infreq_detect, freq); }
  513. xs_error_code_t Xsync::RecordSigGenerator_setContrlMode(ControlMode_t mode) {
  514. if (mode != CONTROLMODE_MANUAL_TRIGGER && mode != CONTROLMODE_TIMECODE_TRIGGER && mode != CONTROLMODE_EXTERNALTTL_TRIGGER) {
  515. return kxs_ec_param_error;
  516. }
  517. return reg_write(reg::record_sig_gen_ctrl_control_mode, mode);
  518. }
  519. xs_error_code_t Xsync::RecordSigGenerator_getContrlMode(ControlMode_t &mode) { //
  520. return _reg_read(reg::record_sig_gen_ctrl_control_mode, mode);
  521. }
  522. xs_error_code_t Xsync::RecordSigGenerator_manualStart() { return reg_write(reg::record_sig_gen_manual_ctrl, 1, 10); }
  523. xs_error_code_t Xsync::RecordSigGenerator_manualStop() { return reg_write(reg::record_sig_gen_manual_ctrl, 0, 10); }
  524. xs_error_code_t Xsync::RecordSigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) { //
  525. return writetimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  526. }
  527. xs_error_code_t Xsync::RecordSigGenerator_setAutoStopTimecode(XsyncTimecode_t timecode) { //
  528. return writetimecode(reg::record_sig_gen_timecode_stop0, reg::record_sig_gen_timecode_stop1, timecode);
  529. }
  530. xs_error_code_t Xsync::RecordSigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) { //
  531. return readtimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  532. }
  533. xs_error_code_t Xsync::RecordSigGenerator_getAutoStopTimecode(XsyncTimecode_t &timecode) { return readtimecode(reg::record_sig_gen_timecode_stop0, reg::record_sig_gen_timecode_stop1, timecode); }
  534. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerSrc(InputInterface_t ttlPortNum) {
  535. if (ttlPortNum != INPUT_IF_TTL1 && ttlPortNum != INPUT_IF_TTL2 && ttlPortNum != INPUT_IF_TTL3 && ttlPortNum != INPUT_IF_TTL4) {
  536. return kxs_ec_param_error;
  537. }
  538. return reg_write(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  539. }
  540. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerSrc(InputInterface_t &ttlPortNum) { //
  541. return _reg_read(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  542. }
  543. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerPolarity(uint32_t polarity) { //
  544. return reg_write(reg::record_sig_gen_ttlin_trigger_level, polarity);
  545. }
  546. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerPolarity(uint32_t &polarity) { //
  547. return _reg_read(reg::record_sig_gen_ttlin_trigger_level, polarity);
  548. }
  549. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureTime(uint32_t us) { //
  550. return reg_write(reg::record_sig_gen_exposure_time, us);
  551. }
  552. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureTime(uint32_t &us) {
  553. auto ret = reg_read(reg::record_sig_gen_exposure_time, us);
  554. // us = us / 10;1
  555. return ret;
  556. }
  557. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureOffsetTime(uint32_t us) { return reg_write(reg::record_sig_gen_exposure_offset_time, us); }
  558. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureOffsetTime(uint32_t &us) {
  559. auto ret = reg_read(reg::record_sig_gen_exposure_offset_time, us);
  560. // us = us / 10;
  561. return ret;
  562. }
  563. xs_error_code_t Xsync::RecordSigGenerator_setTimecodeCtrlFlag(uint32_t autoStart, uint32_t autoStop) { //
  564. uint32_t flag = (autoStart & 0x01) | ((autoStop & 0x01) << 1);
  565. return reg_write(reg::record_sig_gen_timecode_control_flag, flag);
  566. }
  567. xs_error_code_t Xsync::RecordSigGenerator_getTimecodeCtrlFlag(uint32_t &autoStart, uint32_t &autoStop) {
  568. uint32_t flag = 0;
  569. auto ret = reg_read(reg::record_sig_gen_timecode_control_flag, flag);
  570. autoStart = flag & 0x01;
  571. autoStop = (flag >> 1) & 0x01;
  572. return ret;
  573. }
  574. xs_error_code_t Xsync::RecordSigGenerator_getRecordState(uint32_t &state) { return _reg_read(reg::record_sig_gen_record_state, state); }
  575. xs_error_code_t Xsync::RecordSigGenerator_readTimecodeSnapshot(XsyncTimecode_t &timecode) { //
  576. return readtimecode(reg::record_sig_gen_timecode_snapshot0, reg::record_sig_gen_timecode_snapshot1, timecode);
  577. }