main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <arpa/inet.h>
  9. #include <signal.h>
  10. #include <sys/mman.h>
  11. #include <fcntl.h>
  12. #include <sys/time.h>
  13. #include "Devices//pci.h"
  14. #include "Devices//tmsgheaders.h"
  15. #include "Devices//lmx2594.h"
  16. #include "Devices//max2870.h"
  17. #include "Devices//lmk04821.h"
  18. #include "Devices//ad9912.h"
  19. #include "Devices/pe43711.h"
  20. #include "Devices//potentiometer.h"
  21. #include "command.h"
  22. #define REQUESTED_MEMORY_SIZE 0x1000
  23. #define SERVER_PORT 5025
  24. #define BACKLOG 10
  25. volatile int conn_fd = 0;
  26. volatile int pci_fd = 0;
  27. int listen_fd = 0;
  28. void *bar1;
  29. reg_addr_pci* pci_bar_1;
  30. //Обработчик ошибок
  31. void error(const char *msg)
  32. {
  33. perror(msg);
  34. if (listen_fd != 0)
  35. {
  36. close(listen_fd);
  37. }
  38. if (pci_fd != 0)
  39. {
  40. close(pci_fd);
  41. }
  42. munmap(bar1, REQUESTED_MEMORY_SIZE);
  43. exit(1);
  44. }
  45. //Обработчик сигнала SIGINT завершения программы при нажатии Ctrl+C
  46. void handleCloseSignal(int signal)
  47. {
  48. if (signal == SIGINT)
  49. {
  50. printf("\nCaught signal %d, closing socket and exiting...\n", signal);
  51. if (conn_fd != 0)
  52. {
  53. close(conn_fd);
  54. }
  55. if (listen_fd != 0)
  56. {
  57. close(listen_fd);
  58. }
  59. if (pci_fd != 0)
  60. {
  61. close(pci_fd);
  62. }
  63. munmap(bar1, REQUESTED_MEMORY_SIZE);
  64. exit(0);
  65. }
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69. char *filename = "/dev/MyDmaModule";
  70. struct timeval tv;
  71. // Длина структуры адреса
  72. socklen_t client_len;
  73. // Переменная для количества байт принятых из сокета и для хранения ошибок
  74. ssize_t n;
  75. struct sockaddr_in serv_addr, client_addr;
  76. // Приёмный буффер для сокета
  77. char recvBuff[1024];
  78. // Разделители команд
  79. const char charSeparator[] = {"\n"};
  80. // Указатель команды
  81. char *ptrLexeme = NULL;
  82. // Указатель для хранения контекста токенизации
  83. char *savePtr = NULL;
  84. pci_fd = open(filename, O_RDWR | O_SYNC);
  85. if (pci_fd == -1) {
  86. int error = errno;
  87. fprintf(stderr, "Cannot open PCIe device file: %s\n", strerror(error));
  88. return 1;
  89. }
  90. bar1 = mmap(NULL, REQUESTED_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, pci_fd, 0);
  91. if (bar1 == MAP_FAILED) {
  92. perror("mmap");
  93. close(pci_fd);
  94. return 1;
  95. }
  96. pci_bar_1 = (reg_addr_pci*)bar1;
  97. lmk04821_a_init(pci_bar_1);
  98. usleep(500);
  99. lmk04821_b_init(pci_bar_1);
  100. pe43711_att_1_init(pci_bar_1);
  101. usleep(1);
  102. pe43711_att_2_init(pci_bar_1);
  103. rst_for_fpga(bar1);
  104. shift_reg(bar1);
  105. potentiometer_set(pci_bar_1, 0, 0);
  106. max2870_init(bar1);
  107. ad9912_init(pci_bar_1);
  108. lmx2594_init(bar1);
  109. usleep(1000);
  110. // Установка режима SPI
  111. uint32_t cfg_reg = get_cfg_reg();
  112. SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_MODE_BITM, CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
  113. SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_CLK_BITM, CFG_REG_SPI_CLK_BITP, CFG_REG_SPI_CLK_50MHZ);
  114. uint32_t *spi_mode = bar1 +CFG_REG_ADDR;
  115. *spi_mode = cfg_reg;
  116. set_cfg_reg(cfg_reg);
  117. // Установка обработчика сигналов
  118. signal(SIGINT, handleCloseSignal);
  119. listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  120. if (listen_fd < 0)
  121. {
  122. error("Error : Could not create socket!");
  123. }
  124. memset(&serv_addr, 0, sizeof(serv_addr));
  125. serv_addr.sin_family = AF_INET;
  126. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  127. serv_addr.sin_port = htons(SERVER_PORT);
  128. if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
  129. {
  130. error("setsockopt(SO_REUSEADDR) failed");
  131. }
  132. if (bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
  133. {
  134. error("Error : Bind failed");
  135. }
  136. if (listen(listen_fd, BACKLOG) < 0)
  137. {
  138. error("Error : Listen failed");
  139. }
  140. printf("Server is listening on port %d...\n", SERVER_PORT);
  141. while(1) {
  142. client_len = sizeof(client_addr);
  143. conn_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &client_len);
  144. if (conn_fd < 0)
  145. {
  146. perror("Error : Accept failed");
  147. continue;
  148. }
  149. printf("Connection established with client\n");
  150. //Очистка буфера
  151. memset(recvBuff, 0, sizeof(recvBuff));
  152. double time_begin =0;
  153. while ((n = recv(conn_fd, recvBuff, sizeof(recvBuff) - 1, 0)) > 0)
  154. {
  155. gettimeofday(&tv, NULL);
  156. double time_end = ((double)tv.tv_sec) * 1000 +
  157. ((double)tv.tv_usec) / 1000 ;
  158. double total_time_ms = time_end - time_begin;
  159. printf("TOTAL TIME (ms) = %f\n", total_time_ms);
  160. recvBuff[n] = 0;
  161. if(fputs(recvBuff, stdout) == EOF)
  162. {
  163. printf("\n Error : Fputs error\n");
  164. break;
  165. }
  166. // Указатель команды
  167. ptrLexeme = NULL;
  168. // Указатель для хранения контекста токенизации
  169. savePtr = NULL;
  170. // Инициализируем функцию и ищем команду в строке
  171. ptrLexeme = strtok_r(recvBuff, charSeparator, &savePtr);
  172. // Выполняем команды, пока не дойдём до конца приёмного буффера
  173. while (ptrLexeme) {
  174. // Запуск парсера команд
  175. processCommand(ptrLexeme);
  176. // Ищем команды разделенные разделителем
  177. ptrLexeme = strtok_r(NULL, charSeparator, &savePtr);
  178. }
  179. gettimeofday(&tv, NULL);
  180. time_begin = ((double)tv.tv_sec) * 1000 +
  181. ((double)tv.tv_usec) / 1000;
  182. }
  183. if (n == 0)
  184. {
  185. printf("\n Client closed the connection\n");
  186. }
  187. else if (n < 0)
  188. {
  189. perror("Read error");
  190. }
  191. close(conn_fd);
  192. printf("Waiting for new client connection...\n");
  193. }
  194. return 0;
  195. }