| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <arpa/inet.h>
- #include <signal.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include "Devices//pci.h"
- #include "Devices//tmsgheaders.h"
- #include "Devices//lmx2594.h"
- #include "Devices//max2870.h"
- #include "Devices//lmk04821.h"
- #include "Devices//ad9912.h"
- #include "Devices/pe43711.h"
- #include "Devices//potentiometer.h"
- #include "command.h"
- #define REQUESTED_MEMORY_SIZE 0x1000
- #define SERVER_PORT 5025
- #define BACKLOG 10
- volatile int conn_fd = 0;
- volatile int pci_fd = 0;
- int listen_fd = 0;
- void *bar1;
- reg_addr_pci* pci_bar_1;
- //Обработчик ошибок
- void error(const char *msg)
- {
- perror(msg);
- if (listen_fd != 0)
- {
- close(listen_fd);
- }
- if (pci_fd != 0)
- {
- close(pci_fd);
- }
- munmap(bar1, REQUESTED_MEMORY_SIZE);
- exit(1);
- }
- //Обработчик сигнала SIGINT завершения программы при нажатии Ctrl+C
- void handle_close_signal(int signal)
- {
- if (signal == SIGINT)
- {
- printf("\nCaught signal %d, closing socket and exiting...\n", signal);
- if (conn_fd != 0)
- {
- close(conn_fd);
- }
- if (listen_fd != 0)
- {
- close(listen_fd);
- }
- if (pci_fd != 0)
- {
- close(pci_fd);
- }
- munmap(bar1, REQUESTED_MEMORY_SIZE);
- exit(0);
- }
- }
- int main(int argc, char *argv[])
- {
- char *filename = "/dev/MyDmaModule";
- struct timeval tv;
- // Длина структуры адреса
- socklen_t client_len;
- // Переменная для количества байт принятых из сокета и для хранения ошибок
- ssize_t n;
- struct sockaddr_in serv_addr, client_addr;
-
- // Приёмный буффер для сокета
- char recv_buff[1024];
- // Разделители команд
- const char charSeparator[] = {"\n"};
- // Указатель команды
- char *ptr_lexeme = NULL;
- // Указатель для хранения контекста токенизации
- char *savePtr = NULL;
- pci_fd = open(filename, O_RDWR | O_SYNC);
- if (pci_fd == -1) {
- int error = errno;
- fprintf(stderr, "Cannot open PCIe device file: %s\n", strerror(error));
- return 1;
- }
- bar1 = mmap(NULL, REQUESTED_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, pci_fd, 0);
- if (bar1 == MAP_FAILED) {
- perror("mmap");
- close(pci_fd);
- return 1;
- }
- pci_bar_1 = (reg_addr_pci*)bar1;
- lmk04821_a_init(pci_bar_1);
- usleep(500);
- lmk04821_b_init(pci_bar_1);
- // pe43711_att_2_init(pci_bar_1);
- pe43711_att_1_init(pci_bar_1);
- // usleep(1);
- rst_for_fpga(pci_bar_1);
- shift_reg(pci_bar_1);
- potentiometer_set(pci_bar_1, 0, 0);
- max2870_init(pci_bar_1);
- ad9912_init(pci_bar_1);
- lmx2594_init(pci_bar_1);
- usleep(1000);
-
- // Установка режима SPI
- uint32_t cfg_reg = get_cfg_reg();
- SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_MODE_BITM, CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_4MOSI);
- SET_REGISTER_PARAM(cfg_reg, CFG_REG_SPI_CLK_BITM, CFG_REG_SPI_CLK_BITP, CFG_REG_SPI_CLK_50MHZ);
- pci_bar_1->cfg_reg_addr = cfg_reg;
- set_cfg_reg(cfg_reg);
- // Установка обработчика сигналов
- signal(SIGINT, handle_close_signal);
- listen_fd = socket(AF_INET, SOCK_STREAM, 0);
- if (listen_fd < 0)
- {
- error("Error : Could not create socket!");
- }
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- serv_addr.sin_port = htons(SERVER_PORT);
- if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
- {
- error("setsockopt(SO_REUSEADDR) failed");
- }
- if (bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
- {
- error("Error : Bind failed");
- }
- if (listen(listen_fd, BACKLOG) < 0)
- {
- error("Error : Listen failed");
- }
- printf("Server is listening on port %d...\n", SERVER_PORT);
-
- while(1) {
- client_len = sizeof(client_addr);
- conn_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &client_len);
- if (conn_fd < 0)
- {
- perror("Error : Accept failed");
- continue;
- }
- printf("Connection established with client\n");
- //Очистка буфера
- memset(recv_buff, 0, sizeof(recv_buff));
- double time_begin =0;
- while ((n = recv(conn_fd, recv_buff, sizeof(recv_buff) - 1, 0)) > 0)
- {
- gettimeofday(&tv, NULL);
- double time_end = ((double)tv.tv_sec) * 1000 +
- ((double)tv.tv_usec) / 1000 ;
- double total_time_ms = time_end - time_begin;
- printf("TOTAL TIME (ms) = %f\n", total_time_ms);
- recv_buff[n] = 0;
-
- if(fputs(recv_buff, stdout) == EOF)
- {
- printf("\n Error : Fputs error\n");
- break;
- }
-
- // Указатель команды
- ptr_lexeme = NULL;
- // Указатель для хранения контекста токенизации
- savePtr = NULL;
- // Инициализируем функцию и ищем команду в строке
- ptr_lexeme = strtok_r(recv_buff, charSeparator, &savePtr);
-
- // Выполняем команды, пока не дойдём до конца приёмного буффера
- while (ptr_lexeme) {
- // Запуск парсера команд
- process_command(ptr_lexeme);
- // Ищем команды разделенные разделителем
- ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr);
- }
- gettimeofday(&tv, NULL);
- time_begin = ((double)tv.tv_sec) * 1000 +
- ((double)tv.tv_usec) / 1000;
- }
- if (n == 0)
- {
- printf("\n Client closed the connection\n");
- }
- else if (n < 0)
- {
- perror("Read error");
- }
- close(conn_fd);
- printf("Waiting for new client connection...\n");
- }
- return 0;
- }
|