command.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "command.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <inttypes.h>
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <sys/time.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. #include "Devices/lmx2594.h"
  13. #include "Devices/dac8811.h"
  14. #include "Devices/ad9912.h"
  15. #include "Devices/potentiometer.h"
  16. double f_pd = 200e6;
  17. uint16_t armCode[1] = {0};
  18. uint16_t attCode[1] = {0};
  19. //Массив структур Command, который связывает строки команд с соответствующими функциями.
  20. Command commands[] = {
  21. {"TMSG44:FREQ ", handleFreqCmd},
  22. {"TMSG44:LD?", handleLdCmd},
  23. {"TMSG44:POW ", handlePowCmd},
  24. {"TMSG44:ARM ", handleArmCmd},
  25. {"TMSG44:ATT ", handleAttCmd},
  26. {"*IDN?", handleIdnCmd},
  27. {"TMSG44:OFFSET ", handleOffsetCmd},
  28. {"TMSG44:SLOPE ", handleSlopeCmd},
  29. {NULL, NULL} // Завершающий элемент для обозначения конца массива
  30. };
  31. //handleXXXXCmd - обработчики команд
  32. void handleFreqCmd(const char* recvBuff)
  33. {
  34. printf("\nHandle command \"TMSG44:FREQ\"\n");
  35. double freq[1] = {0};
  36. double lmx_freq = 0;
  37. splitLexeme(recvBuff, freq, sizeof(freq[0]), convertToDouble);
  38. // usleep(1);
  39. uint32_t cfg_reg = get_cfg_reg();
  40. SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_MODE_BITM, CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
  41. uint32_t *spi_mode = bar1 +CFG_REG_ADDR;
  42. *spi_mode = cfg_reg;
  43. set_cfg_reg(cfg_reg);
  44. lmx_freq = lmx_get_freq(freq[0]);
  45. f_pd = ad9912_set(bar1, lmx_freq, f_pd);
  46. printf("f_pd frequency is set to %.6f MHz\n", f_pd/1e6);
  47. lmx_freq_set(bar1, lmx_freq, f_pd);
  48. // Switch the keys
  49. key_switch(bar1, freq[0],lmx_freq);
  50. printf("The frequency is set to %.2f MHz\n", freq[0]/1e6);
  51. // Send the data
  52. send_data_qspi(bar1);
  53. // Return the 1 MOSI mode
  54. usleep(1);
  55. cfg_reg = get_cfg_reg();
  56. SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_1MOSI);
  57. *spi_mode = cfg_reg;
  58. set_cfg_reg(cfg_reg);
  59. }
  60. void send_data_qspi(void *bar1) {
  61. // get the gpio reg and shift reg data
  62. uint32_t gpio_reg = get_tmsg_gpio_reg();
  63. uint32_t shift_reg = get_tmsg_shift_reg();
  64. // Create a header 4 Mosi mode
  65. uint32_t qspi_header = ((ENUM_SPIMODE_4MOSI) |(0x1 << BITP_GPIO_4MOSI_HEADER) |(0x1 << BITP_SHIFT_REG_4MOSI_HEADER )| ((sizeof(ad9912_ftw_regs_qspi) / 4) << BITP_DDS_4MOSI_HEADER) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | (0x1 << BITP_DAC_4MOSI_HEADER) | (0x1 << BITP_ATT_4MOSI_HEADER) |(0x2 << BITP_POT_4MOSI_HEADER)| TERM_BIT_1);
  66. uint32_t *data = bar1 + LMX_BASE_ADDR;
  67. *data = qspi_header;
  68. // Initialize the registers
  69. // Send the data for AD9912
  70. for (int i = 0; i < sizeof(ad9912_ftw_regs_qspi) / 4; i++) {
  71. *data = ad9912_ftw_regs_qspi[i];
  72. }
  73. // Send the data for the GPIO
  74. *data = gpio_reg;
  75. // Send the data for LMX2594
  76. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  77. *data = lmx_change_freq_regs[i];
  78. }
  79. // Send the data for the shift register
  80. *data = shift_reg;
  81. // Send the data for the potentiometer
  82. for (int i = 0; i < sizeof(pot_array) / 4; i++) {
  83. *data = pot_array[i];
  84. }
  85. // Send the data for the DAC
  86. *data = armCode[0];
  87. // Send the data for the ATT
  88. *data = attCode[0];
  89. }
  90. void handleLdCmd(const char* recvBuff)
  91. {
  92. char messageLd[] = "1\n";
  93. printf("\nHandle command \"TMSG44:LD?\"\n");
  94. uint32_t ld_status = lmx_ld_status(bar1);
  95. clock_t before = clock();
  96. clock_t difference;
  97. int difference_msec = 0;
  98. int trigger = 10000; //10ms
  99. while(!ld_status)
  100. {
  101. difference = clock() - before;
  102. difference_msec = difference * 1000 / CLOCKS_PER_SEC;
  103. if(difference_msec >= trigger)
  104. {
  105. strcpy(messageLd, "0\n");
  106. printf("LD timeout\n");
  107. break;
  108. }
  109. ld_status = lmx_ld_status(bar1);
  110. // printf("WHILE LD status: %d\n", ld_status);
  111. }
  112. send(conn_fd, messageLd, sizeof(messageLd), 0);
  113. printf("\nSend msg LD: %d!\n", ld_status);
  114. }
  115. void handlePowCmd(const char* recvBuff)
  116. {
  117. printf("\nHandle command \"TMSG44:POW\"\n");
  118. double pow[1] = {0};
  119. splitLexeme(recvBuff, pow, sizeof(pow[0]), convertToDouble);
  120. printf("%f\n", pow[0]);
  121. }
  122. void handleArmCmd(const char* recvBuff)
  123. {
  124. printf("\nHandle command \"TMSG44:ARM\"\n");
  125. splitLexeme(recvBuff, armCode, sizeof(armCode[0]), convertToUInt16);
  126. printf("\n%u\n", armCode[0]);
  127. // dac8811_set(bar1,armCode[0]);
  128. }
  129. void handleAttCmd(const char* recvBuff)
  130. {
  131. printf("\nHandle command \"TMSG44:ATT\"\n");
  132. splitLexeme(recvBuff, attCode, sizeof(attCode[0]), convertToUInt16);
  133. printf("\n%u\n", attCode[0]);
  134. // dac8811_att_set(bar1,attCode[0]);
  135. }
  136. void handleIdnCmd(const char* recvBuff)
  137. {
  138. printf("\nHandle command \"*IDN?\"\n");
  139. char messageIdn[] = "TMSG44_CoolPi\n";
  140. send(conn_fd, messageIdn, sizeof(messageIdn), 0);
  141. }
  142. void handleOffsetCmd(const char* recvBuff)
  143. {
  144. printf("\nHandle command \"TMSG44:OFFSET\"\n");
  145. uint16_t offsetCode[1] = {0};
  146. splitLexeme(recvBuff, offsetCode, sizeof(offsetCode[0]), convertToUInt16);
  147. printf("\n%u\n", offsetCode[0]);
  148. }
  149. void handleSlopeCmd(const char* recvBuff)
  150. {
  151. printf("\nHandle command \"TMSG44:SLOPE\"\n");
  152. uint16_t slopeCode[1] = {0};
  153. splitLexeme(recvBuff, slopeCode, sizeof(slopeCode[0]), convertToUInt16);
  154. printf("\n%u\n", slopeCode[0]);
  155. }
  156. //Проходим по массиву команд и ищем команду, которая совпадает с началом строки recvBuff.
  157. //Если команда найдена, вызывается соответствующая функция-обработчик
  158. void processCommand(const char* recvBuff)
  159. {
  160. for (int i = 0; commands[i].command != NULL; i++)
  161. {
  162. if (!strncasecmp(recvBuff, commands[i].command, strlen(commands[i].command)))
  163. {
  164. commands[i].handler(recvBuff);
  165. return;
  166. }
  167. }
  168. printf("\nUnknown command: %s\n", recvBuff);
  169. }
  170. // Преобразование строки в uint16_t
  171. void convertToUInt16(const char *str, void *output)
  172. {
  173. *(uint16_t *)output = (uint16_t)strtoul(str, NULL, 10);
  174. }
  175. // Преобразование строки в unsigned long long int
  176. void convertToUint64(const char *str, void *output)
  177. {
  178. *(uint64_t *)output = (uint64_t)strtoull(str, NULL, 10);
  179. }
  180. // Преобразование строки в double
  181. void convertToDouble(const char *str, void *output)
  182. {
  183. *(double *)output = strtod(str, NULL);
  184. }
  185. // Универсальная функция для разделения строки на лексемы
  186. void splitLexeme(const char *ptrSCPI, void *numOutAndValue, size_t elementSize, ConvertFunc convertFunc)
  187. {
  188. uint8_t counter = 0;
  189. // Разделители лексем
  190. const char charSeparator[] = {" "};
  191. char *ptrLexeme = NULL;
  192. // Указатель для хранения контекста токенизации
  193. char *savePtr;
  194. // Инициализируем функцию
  195. ptrLexeme = strtok_r((char *)ptrSCPI, charSeparator, &savePtr);
  196. // Ищем лексемы разделенные разделителем
  197. ptrLexeme = strtok_r(NULL, charSeparator, &savePtr);
  198. // Ищем лексемы строки
  199. while (ptrLexeme) {
  200. // Проверяем, является ли первый символ лексемы числом
  201. if(('0' <= ptrLexeme[0]) && (ptrLexeme[0] <= '9')) {
  202. // Преобразуем строку с числом в число
  203. convertFunc(ptrLexeme, (uint8_t *)numOutAndValue + counter * elementSize);
  204. counter++;
  205. }
  206. // Ищем лексемы разделенные разделителем
  207. ptrLexeme = strtok_r(NULL, charSeparator, &savePtr);
  208. }
  209. }