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.

665 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
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 on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; }
  105. void Xsync::registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb) { m_on_camera_sync_msg_cb = on_camera_sync_msg_cb; }
  106. void Xsync::registerOnWorkstateChangeMsgCallback(xsync_on_workstate_change_msg_t on_workstate_change_msg_cb) { m_on_workstate_change_msg_cb = on_workstate_change_msg_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. if (m_on_workstate_change_msg_cb) m_on_workstate_change_msg_cb(packet->data[0]);
  268. }
  269. }
  270. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  271. uint32_t count = 0;
  272. uint32_t data0 = data[7];
  273. uint32_t data1 = data[6];
  274. uint32_t data2 = data[5];
  275. uint32_t data3 = data[4];
  276. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  277. xysnc_camera_sync_data_t camera_sync_data;
  278. camera_sync_data.frameIndex = count;
  279. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  280. }
  281. xs_error_code_t Xsync::generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); }
  282. xs_error_code_t Xsync::factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); }
  283. xs_error_code_t Xsync::reboot() { return doaction(xsync_stm32_action_reboot, 0, nullptr); }
  284. xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); }
  285. xs_error_code_t Xsync::changeNetworkConfig(string ip, string mask, string gateway) {
  286. uint32_t ip32 = 0;
  287. uint32_t mask32 = 0;
  288. uint32_t gateway32 = 0;
  289. xs_error_code_t ecode;
  290. bool suc = false;
  291. ip32 = (uint32_t)ipToUint32(ip.c_str(), suc);
  292. if (!suc) return kxs_ec_param_error;
  293. mask32 = (uint32_t)ipToUint32(mask.c_str(), suc);
  294. if (!suc) return kxs_ec_param_error;
  295. gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc);
  296. if (!suc) return kxs_ec_param_error;
  297. uint32_t readbak = 0;
  298. ecode = reg_write(reg::kstm32_ip, ip32, readbak);
  299. if (ecode != kxs_ec_success) return ecode;
  300. ecode = reg_write(reg::kstm32_netmask, mask32, readbak);
  301. if (ecode != kxs_ec_success) return ecode;
  302. ecode = reg_write(reg::kstm32_gw, gateway32, readbak);
  303. if (ecode != kxs_ec_success) return ecode;
  304. ecode = storageConfig();
  305. if (ecode != kxs_ec_success) return ecode;
  306. return kxs_ec_success;
  307. }
  308. xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) {
  309. //
  310. uint32_t readbak = 0;
  311. xs_error_code_t ecode;
  312. ecode = reg_write(reg::kstm32_action_val0, actionval, readbak);
  313. if (ecode != kxs_ec_success) return ecode;
  314. ecode = reg_write(reg::kstm32_action0, action, readbak, overtime_ms);
  315. if (ecode != kxs_ec_success) return ecode;
  316. if (ackreturn) *ackreturn = readbak;
  317. return ecode;
  318. }
  319. /*******************************************************************************
  320. * TTLInputModule *
  321. *******************************************************************************/
  322. #define FREQ_CNT_TO_FREQ(cnt) ((cnt != 0) ? (uint32_t)(1.0 / (cnt * 1.0 / (10 * 1000 * 1000)) + 0.5) : 0) //+0.5是因为c++ 小数强转成整数时是取整,而非四舍五入
  323. xs_error_code_t Xsync::TTLInputModule1_detectFreq(uint32_t &freq) {
  324. uint32_t freq_cnt = 0;
  325. DO_XSYNC(reg_read(reg::k_ttlin1_freq_detector_reg, freq_cnt, 10));
  326. if (freq_cnt == 0) {
  327. freq = 0;
  328. }
  329. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  330. return kxs_ec_success;
  331. }
  332. xs_error_code_t Xsync::TTLInputModule2_detectFreq(uint32_t &freq) {
  333. uint32_t freq_cnt = 0;
  334. DO_XSYNC(reg_read(reg::k_ttlin2_freq_detector_reg, freq_cnt, 10));
  335. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  336. return kxs_ec_success;
  337. }
  338. xs_error_code_t Xsync::TTLInputModule3_detectFreq(uint32_t &freq) {
  339. uint32_t freq_cnt = 0;
  340. DO_XSYNC(reg_read(reg::k_ttlin3_freq_detector_reg, freq_cnt, 10));
  341. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  342. return kxs_ec_success;
  343. }
  344. xs_error_code_t Xsync::TTLInputModule4_detectFreq(uint32_t &freq) {
  345. uint32_t freq_cnt = 0;
  346. DO_XSYNC(reg_read(reg::k_ttlin4_freq_detector_reg, freq_cnt, 10));
  347. freq = FREQ_CNT_TO_FREQ(freq_cnt);
  348. return kxs_ec_success;
  349. }
  350. /*******************************************************************************
  351. * TTLOutputModule *
  352. *******************************************************************************/
  353. // 0:固定输出低电平,1:固定输出高电平,2:分频倍频模式,3:转发模式,4:测试模式
  354. xs_error_code_t Xsync::TTLOutputModule1_setSrcSigType(SignalType_t source) {
  355. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  356. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 2, 10)); // 分频倍频模式
  357. } else {
  358. DO_XSYNC(reg_write(reg::kreg_ttlout1_signal_process_mode, 3, 10)); // 转发模式
  359. }
  360. REG_WRITE(reg::kreg_ttlout1_input_signal_select, source);
  361. }
  362. xs_error_code_t Xsync::TTLOutputModule2_setSrcSigType(SignalType_t source) {
  363. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  364. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 2, 10)); // 分频倍频模式
  365. } else {
  366. DO_XSYNC(reg_write(reg::kreg_ttlout2_signal_process_mode, 3, 10)); // 转发模式
  367. }
  368. REG_WRITE(reg::kreg_ttlout2_input_signal_select, source);
  369. }
  370. xs_error_code_t Xsync::TTLOutputModule3_setSrcSigType(SignalType_t source) {
  371. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  372. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 2, 10)); // 分频倍频模式
  373. } else {
  374. DO_XSYNC(reg_write(reg::kreg_ttlout3_signal_process_mode, 3, 10)); // 转发模式
  375. }
  376. REG_WRITE(reg::kreg_ttlout3_input_signal_select, source);
  377. }
  378. xs_error_code_t Xsync::TTLOutputModule4_setSrcSigType(SignalType_t source) {
  379. if (source == SIGNAL_TTLIN1 || source == SIGNAL_TTLIN2 || source == SIGNAL_TTLIN3 || source == SIGNAL_TTLIN4) {
  380. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 2, 10)); // 分频倍频模式
  381. } else {
  382. DO_XSYNC(reg_write(reg::kreg_ttlout4_signal_process_mode, 3, 10)); // 转发模式
  383. }
  384. REG_WRITE(reg::kreg_ttlout4_input_signal_select, source);
  385. }
  386. xs_error_code_t Xsync::TTLOutputModule1_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout1_input_signal_select, source); }
  387. xs_error_code_t Xsync::TTLOutputModule2_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout2_input_signal_select, source); }
  388. xs_error_code_t Xsync::TTLOutputModule3_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout3_input_signal_select, source); }
  389. xs_error_code_t Xsync::TTLOutputModule4_getSrcSigType(SignalType_t &source) { REG_READ(reg::kreg_ttlout4_input_signal_select, source); }
  390. xs_error_code_t Xsync::TTLOutputModule1_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  391. xs_error_code_t Xsync::TTLOutputModule2_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  392. xs_error_code_t Xsync::TTLOutputModule3_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  393. xs_error_code_t Xsync::TTLOutputModule4_setFreqDivision(uint32_t div) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  394. xs_error_code_t Xsync::TTLOutputModule1_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout1_pllout_freq_division_ctrl, div); }
  395. xs_error_code_t Xsync::TTLOutputModule2_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout2_pllout_freq_division_ctrl, div); }
  396. xs_error_code_t Xsync::TTLOutputModule3_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout3_pllout_freq_division_ctrl, div); }
  397. xs_error_code_t Xsync::TTLOutputModule4_getFreqDivision(uint32_t &div) { REG_READ(reg::kreg_ttlout4_pllout_freq_division_ctrl, div); }
  398. xs_error_code_t Xsync::TTLOutputModule1_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  399. xs_error_code_t Xsync::TTLOutputModule2_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  400. xs_error_code_t Xsync::TTLOutputModule3_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  401. xs_error_code_t Xsync::TTLOutputModule4_setFreqMultiplication(uint32_t multi) { REG_WRITE(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  402. xs_error_code_t Xsync::TTLOutputModule1_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout1_pllout_freq_multiplication_ctrl, multi); }
  403. xs_error_code_t Xsync::TTLOutputModule2_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout2_pllout_freq_multiplication_ctrl, multi); }
  404. xs_error_code_t Xsync::TTLOutputModule3_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout3_pllout_freq_multiplication_ctrl, multi); }
  405. xs_error_code_t Xsync::TTLOutputModule4_getFreqMultiplication(uint32_t &multi) { REG_READ(reg::kreg_ttlout4_pllout_freq_multiplication_ctrl, multi); }
  406. xs_error_code_t Xsync::TTLOutputModule1_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_in_freq_detect, freq); }
  407. xs_error_code_t Xsync::TTLOutputModule2_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_in_freq_detect, freq); }
  408. xs_error_code_t Xsync::TTLOutputModule3_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_in_freq_detect, freq); }
  409. xs_error_code_t Xsync::TTLOutputModule4_readInFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_in_freq_detect, freq); }
  410. xs_error_code_t Xsync::TTLOutputModule1_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout1_sig_out_freq_detect, freq); }
  411. xs_error_code_t Xsync::TTLOutputModule2_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout2_sig_out_freq_detect, freq); }
  412. xs_error_code_t Xsync::TTLOutputModule3_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout3_sig_out_freq_detect, freq); }
  413. xs_error_code_t Xsync::TTLOutputModule4_readOutFreq(float &freq) { return readfreq(reg::kreg_ttlout4_sig_out_freq_detect, freq); }
  414. /*******************************************************************************
  415. * TimecodeInputModule *
  416. *******************************************************************************/
  417. xs_error_code_t Xsync::ExternalTimecode_setSource(InputInterface_t src) {
  418. if (src == INPUT_IF_TIMECODE_BNC) {
  419. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 1, 10)); // 0:off,1:bnc,2:headphone
  420. } else if (src == INPUT_IF_TIMECODE_HEADPHONE) {
  421. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 2, 10)); // 0:off,1:bnc,2:headphone
  422. } else if (src == INPUT_IF_OFF) {
  423. DO_XSYNC(reg_write(reg::external_timecode_sig_selt, 0, 10)); // 0:off,1:bnc,2:headphone
  424. } else {
  425. return kxs_ec_param_error;
  426. }
  427. return kxs_ec_success;
  428. }
  429. xs_error_code_t Xsync::ExternalTimecode_getSource(InputInterface_t &timecode_select) {
  430. uint32_t readbak = 0;
  431. DO_XSYNC(reg_read(reg::external_timecode_sig_selt, readbak, 10));
  432. if (readbak == 1) {
  433. timecode_select = INPUT_IF_TIMECODE_BNC;
  434. } else if (readbak == 2) {
  435. timecode_select = INPUT_IF_TIMECODE_HEADPHONE;
  436. } else if (readbak == 0) {
  437. timecode_select = INPUT_IF_OFF;
  438. } else {
  439. timecode_select = INPUT_IF_OFF;
  440. }
  441. return kxs_ec_success;
  442. }
  443. xs_error_code_t Xsync::ExternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::external_timecode_format, format); }
  444. xs_error_code_t Xsync::ExternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::external_timecode_format, format); }
  445. xs_error_code_t Xsync::ExternalTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::external_timecode_code0, reg::external_timecode_code1, timecode); }
  446. /*******************************************************************************
  447. * InternalTimecode *
  448. *******************************************************************************/
  449. xs_error_code_t Xsync::InternalTimecode_setFormat(TimecodeFormat_t format) { REG_WRITE(reg::internal_timecode_format, format); }
  450. xs_error_code_t Xsync::InternalTimecode_getFormat(TimecodeFormat_t &format) { REG_READ(reg::internal_timecode_format, format); }
  451. xs_error_code_t Xsync::InternalTimecode_setCode(XsyncTimecode_t timecode) {
  452. DO_XSYNC(reg_write(reg::internal_timecode_en, 0));
  453. DO_XSYNC(writetimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode));
  454. DO_XSYNC(reg_write(reg::internal_timecode_en, 1));
  455. return kxs_ec_success;
  456. }
  457. xs_error_code_t Xsync::InternalTimecode_getCode(XsyncTimecode_t &timecode) { return readtimecode(reg::internal_timecode_data0, reg::internal_timecode_data1, timecode); }
  458. /*******************************************************************************
  459. * SysTimecode *
  460. *******************************************************************************/
  461. xs_error_code_t Xsync::SysTimecode_setSource(uint32_t sig) { REG_WRITE(reg::sys_timecode_select, sig); }
  462. xs_error_code_t Xsync::SysTimecode_getSource(uint32_t &sig) { REG_READ(reg::sys_timecode_select, sig); }
  463. xs_error_code_t Xsync::SysTimecode_readFormat(TimecodeFormat_t &format) { REG_READ(reg::sys_timecode_format, format); }
  464. xs_error_code_t Xsync::SysTimecode_readCode(XsyncTimecode_t &timecode) { return readtimecode(reg::sys_timecode_data0, reg::sys_timecode_data1, timecode); }
  465. /*******************************************************************************
  466. * TimecodeOutputModule *
  467. *******************************************************************************/
  468. xs_error_code_t Xsync::TimecodeOutputModule_setBncOutputLevel(int level) { REG_WRITE(reg::timecode_output_bnc_outut_level_select, level); }
  469. xs_error_code_t Xsync::TimecodeOutputModule_getBncOutputLevel(int &level) { REG_READ(reg::timecode_output_bnc_outut_level_select, level); }
  470. xs_error_code_t Xsync::TimecodeOutputModule_setHeadphoneOutputLevel(int level) { REG_WRITE(reg::timecode_output_headphone_outut_level_select, level); }
  471. xs_error_code_t Xsync::TimecodeOutputModule_getHeadphoneOutputLevel(int &level) { REG_READ(reg::timecode_output_headphone_outut_level_select, level); }
  472. /*******************************************************************************
  473. * GENLOCK *
  474. *******************************************************************************/
  475. xs_error_code_t Xsync::ExternalGenlock_detectFreq(float &freq) { return readfreq(reg::external_genlock_freq, freq); }
  476. xs_error_code_t Xsync::InternalGenlock_setFormat(GenlockFormat_t format) { return reg_write(reg::internal_genlock_format, format); }
  477. xs_error_code_t Xsync::InternalGenlock_getFormat(GenlockFormat_t &format) { REG_READ(reg::internal_genlock_format, format); }
  478. xs_error_code_t Xsync::SysGenlock_setSrc(uint32_t source) { return reg_write(reg::sys_genlock_source, source); }
  479. xs_error_code_t Xsync::SysGenlock_getSrc(uint32_t &extern_or_internal) {
  480. REG_READ(reg::sys_genlock_source, extern_or_internal);
  481. return kxs_ec_success;
  482. }
  483. xs_error_code_t Xsync::SysGenlock_readFreq(float &freq) { return readfreq(reg::sys_genlock_freq, freq); }
  484. /*******************************************************************************
  485. * INTERNAL_CLOCK *
  486. *******************************************************************************/
  487. xs_error_code_t Xsync::InternalClock_setFreq(float freq) {
  488. double T = 1.0 / freq;
  489. double T_ns = T * 1000 * 1000 * 1000;
  490. double cnt = T_ns / 100 + 0.5; // 10MHZ <=> 100ns
  491. uint32_t cnt_u32 = uint32_t(cnt);
  492. return reg_write(reg::internal_clock_freq, cnt_u32);
  493. return kxs_ec_success;
  494. }
  495. xs_error_code_t Xsync::InternalClock_getFreq(float &freq) { return readfreq(reg::internal_clock_freq, freq); }
  496. /*******************************************************************************
  497. * SysClock *
  498. *******************************************************************************/
  499. xs_error_code_t Xsync::SysClock_setSrc(SignalType_t sig) { return reg_write(reg::sys_clock_source, sig); }
  500. xs_error_code_t Xsync::SysClock_getSrc(SignalType_t &sig) { REG_READ(reg::sys_clock_source, sig); }
  501. xs_error_code_t Xsync::SysClock_setTriggerEdge(TriggerEdge_t edge) { return reg_write(reg::sys_clock_trigger_edge_select, edge); }
  502. xs_error_code_t Xsync::SysClock_getTriggerEdge(TriggerEdge_t &edge) { return _reg_read(reg::sys_clock_trigger_edge_select, edge); }
  503. xs_error_code_t Xsync::SysClock_setFreqDivision(uint32_t div) { return reg_write(reg::sys_clock_freq_division_ctrl, div); }
  504. xs_error_code_t Xsync::SysClock_geFreqtDivision(uint32_t &div) { return _reg_read(reg::sys_clock_freq_division_ctrl, div); }
  505. xs_error_code_t Xsync::SysClock_setFreqMultiplication(uint32_t muti) { return reg_write(reg::sys_clock_freq_multiplication_ctrl, muti); }
  506. xs_error_code_t Xsync::SysClock_getFreqMultiplication(uint32_t &muti) { return _reg_read(reg::sys_clock_freq_multiplication_ctrl, muti); }
  507. xs_error_code_t Xsync::SysClock_readOutSigFreq(float &freq) { return readfreq(reg::sys_clock_outfreq_detect, freq); }
  508. xs_error_code_t Xsync::SysClock_readInSigFreq(float &freq) { return readfreq(reg::sys_clock_infreq_detect, freq); }
  509. xs_error_code_t Xsync::RecordSigGenerator_setContrlMode(ControlMode_t mode) {
  510. if (mode != CONTROLMODE_MANUAL_TRIGGER && mode != CONTROLMODE_TIMECODE_TRIGGER && mode != CONTROLMODE_EXTERNALTTL_TRIGGER) {
  511. return kxs_ec_param_error;
  512. }
  513. return reg_write(reg::record_sig_gen_ctrl_control_mode, mode);
  514. }
  515. xs_error_code_t Xsync::RecordSigGenerator_getContrlMode(ControlMode_t &mode) { //
  516. return _reg_read(reg::record_sig_gen_ctrl_control_mode, mode);
  517. }
  518. xs_error_code_t Xsync::RecordSigGenerator_manualStart() { return reg_write(reg::record_sig_gen_manual_ctrl, 1, 10); }
  519. xs_error_code_t Xsync::RecordSigGenerator_manualStop() { return reg_write(reg::record_sig_gen_manual_ctrl, 0, 10); }
  520. xs_error_code_t Xsync::RecordSigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) { //
  521. return writetimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  522. }
  523. xs_error_code_t Xsync::RecordSigGenerator_setAutoStopTimecode(XsyncTimecode_t timecode) { //
  524. return writetimecode(reg::record_sig_gen_timecode_stop0, reg::record_sig_gen_timecode_stop1, timecode);
  525. }
  526. xs_error_code_t Xsync::RecordSigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) { //
  527. return readtimecode(reg::record_sig_gen_timecode_start0, reg::record_sig_gen_timecode_start1, timecode);
  528. }
  529. 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); }
  530. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerSrc(InputInterface_t ttlPortNum) {
  531. if (ttlPortNum != INPUT_IF_TTL1 && ttlPortNum != INPUT_IF_TTL2 && ttlPortNum != INPUT_IF_TTL3 && ttlPortNum != INPUT_IF_TTL4) {
  532. return kxs_ec_param_error;
  533. }
  534. return reg_write(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  535. }
  536. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerSrc(InputInterface_t &ttlPortNum) { //
  537. return _reg_read(reg::record_sig_gen_ttlin_trigger_sig_source, ttlPortNum);
  538. }
  539. xs_error_code_t Xsync::RecordSigGenerator_setExternalTTLTriggerPolarity(uint32_t polarity) { //
  540. return reg_write(reg::record_sig_gen_ttlin_trigger_level, polarity);
  541. }
  542. xs_error_code_t Xsync::RecordSigGenerator_getExternalTTLTriggerPolarity(uint32_t &polarity) { //
  543. return _reg_read(reg::record_sig_gen_ttlin_trigger_level, polarity);
  544. }
  545. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureTime(uint32_t us) { //
  546. return reg_write(reg::record_sig_gen_exposure_time, us);
  547. }
  548. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureTime(uint32_t &us) {
  549. auto ret = reg_read(reg::record_sig_gen_exposure_time, us);
  550. // us = us / 10;1
  551. return ret;
  552. }
  553. xs_error_code_t Xsync::RecordSigGenerator_setRecordExposureOffsetTime(uint32_t us) { return reg_write(reg::record_sig_gen_exposure_offset_time, us); }
  554. xs_error_code_t Xsync::RecordSigGenerator_getRecordExposureOffsetTime(uint32_t &us) {
  555. auto ret = reg_read(reg::record_sig_gen_exposure_offset_time, us);
  556. // us = us / 10;
  557. return ret;
  558. }
  559. xs_error_code_t Xsync::RecordSigGenerator_setTimecodeCtrlFlag(uint32_t autoStart, uint32_t autoStop) { //
  560. uint32_t flag = (autoStart & 0x01) | ((autoStop & 0x01) << 1);
  561. return reg_write(reg::record_sig_gen_timecode_control_flag, flag);
  562. }
  563. xs_error_code_t Xsync::RecordSigGenerator_getTimecodeCtrlFlag(uint32_t &autoStart, uint32_t &autoStop) {
  564. uint32_t flag = 0;
  565. auto ret = reg_read(reg::record_sig_gen_timecode_control_flag, flag);
  566. autoStart = flag & 0x01;
  567. autoStop = (flag >> 1) & 0x01;
  568. return ret;
  569. }
  570. xs_error_code_t Xsync::RecordSigGenerator_getRecordState(uint32_t &state) { return _reg_read(reg::record_sig_gen_record_state, state); }
  571. xs_error_code_t Xsync::RecordSigGenerator_readTimecodeSnapshot(XsyncTimecode_t &timecode) { //
  572. return readtimecode(reg::record_sig_gen_timecode_snapshot0, reg::record_sig_gen_timecode_snapshot1, timecode);
  573. }