command.c 6.9 KB

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