Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #define BUF_SIZE  (512)
  2.  
  3. static QueueHandle_t uart_queue;
  4. void uart_init(void)
  5. {
  6.     uart_config_t uart_config = {
  7.         .baud_rate = 115200,
  8.         .data_bits = UART_DATA_8_BITS,
  9.         .parity    = UART_PARITY_DISABLE,
  10.         .stop_bits = UART_STOP_BITS_1,
  11.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  12.     };
  13.     uart_param_config(MY_UART, &uart_config);
  14.     uart_set_pin(MY_UART, USART_TXD, USART_RXD, USART_RTS, USART_CTS);
  15.     //uart_driver_install(RS485_UART, BUF_SIZE*2, BUF_SIZE, 0, NULL, 0);
  16.     uart_driver_install(MY_UART, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart_queue, 0);
  17. }
  18.  
  19. // sending buffers to UART
  20. void UART_send_data(DataBuffer_t buffToSend)
  21. {
  22.     switch(buffToSend)
  23.     {
  24.     case E_DATA_1:
  25.         uart_write_bytes(RS485_UART, (char*)MyData.Buf_1, MyData.Buf_1_len);
  26.         break;
  27.  
  28.     case E_DATA_2:
  29.         uart_write_bytes(RS485_UART, (char*)MyData.Buf_2, MyData.Buf_2_len);
  30.         break;
  31.  
  32.     default:
  33.         break;
  34.     }
  35. }
  36.  
  37. static void tx_task(void *pvParameters)
  38. {
  39.     while(1)
  40.     {
  41.         //send data1 to UART
  42.         if(MyData.got_packet_1 == true)
  43.         {
  44.             UART_send_data(E_DATA_1);
  45.             MyData.got_packet_1 = false;
  46.         }
  47.  
  48.         //send data2 to UART
  49.         if(MyData.got_packet_2 == true)
  50.         {
  51.             UART_send_data(E_DATA_2);
  52.             MyData.got_packet_2= false;
  53.         }
  54.     }
  55. }
  56.  
  57. static void RS485_rx_task(void *pvParameters)
  58. {
  59.     uart_event_t event;
  60.     int len = 0;
  61.  
  62.     while (1)
  63.     {
  64.         if(xQueueReceive(uart_queue, (void * )&event, (portTickType)portMAX_DELAY))
  65.         {
  66.             ESP_LOGI(UART_TAG, "uart[%d] event:", MY_UART);
  67.  
  68.             if(event.type == UART_DATA)
  69.             {
  70.                 ESP_LOGI(UART_TAG, "(UART_DATA), event type: %d", event.type);
  71.                 len = uart_read_bytes(MY_UART, MyData.UART_Buf, event.size, portMAX_DELAY);
  72.             }
  73.             else
  74.             {
  75.                 ESP_LOGI(UART_TAG, "uart event type: %d", event.type);
  76.             }
  77.         }
  78.  
  79.         if(len > 0)
  80.         {
  81.             MyData.UART_len = len;
  82.             MyData.UART_got_packet = true;
  83.  
  84.             else if (MyData.TCP_conn == true) // send UART data to TCP
  85.             {
  86.                 printf("FL_UART(len:%d):%s\n",MyData.UART_len, MyData.UART_Buf);
  87.                 send(connect_socket, MyData.UART_Buf, MyData.UART_len, 0);
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. void app_main()
  94. {
  95.     uart_init();
  96.     xTaskCreate(rx_task, "rx_task", 8*1024, NULL, 12, NULL);
  97.     xTaskCreate(tx_task, "tx_task", 8*1024, NULL, 5, NULL);
  98.     xTaskCreate(&tcp_server_task,"tcp_server_task", 4*1024, NULL, 5, NULL);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement