|
@@ -103,7 +103,7 @@ void vMBPortSerialEnable(BOOL bRxEnable, BOOL bTxEnable)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void vMBPortSerialRxPoll(size_t xEventSize)
|
|
|
|
|
|
|
+static USHORT usMBPortSerialRxPoll(size_t xEventSize)
|
|
|
{
|
|
{
|
|
|
BOOL xReadStatus = TRUE;
|
|
BOOL xReadStatus = TRUE;
|
|
|
USHORT usCnt = 0;
|
|
USHORT usCnt = 0;
|
|
@@ -114,17 +114,18 @@ static void vMBPortSerialRxPoll(size_t xEventSize)
|
|
|
// Get received packet into Rx buffer
|
|
// Get received packet into Rx buffer
|
|
|
for(usCnt = 0; xReadStatus && (usCnt < xEventSize); usCnt++ ) {
|
|
for(usCnt = 0; xReadStatus && (usCnt < xEventSize); usCnt++ ) {
|
|
|
// Call the Modbus stack callback function and let it fill the buffers.
|
|
// Call the Modbus stack callback function and let it fill the buffers.
|
|
|
- xReadStatus = pxMBFrameCBByteReceived(); // callback to execute receive FSM state machine
|
|
|
|
|
|
|
+ xReadStatus = pxMBFrameCBByteReceived(); // callback to execute receive FSM
|
|
|
}
|
|
}
|
|
|
uart_flush_input(ucUartNumber);
|
|
uart_flush_input(ucUartNumber);
|
|
|
// Send event EV_FRAME_RECEIVED to allow stack process packet
|
|
// Send event EV_FRAME_RECEIVED to allow stack process packet
|
|
|
-#ifndef MB_TIMER_PORT_ENABLED
|
|
|
|
|
|
|
+#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
|
|
// Let the stack know that T3.5 time is expired and data is received
|
|
// Let the stack know that T3.5 time is expired and data is received
|
|
|
(void)pxMBPortCBTimerExpired(); // calls callback xMBRTUTimerT35Expired();
|
|
(void)pxMBPortCBTimerExpired(); // calls callback xMBRTUTimerT35Expired();
|
|
|
#endif
|
|
#endif
|
|
|
ESP_LOGD(TAG, "RX: %d bytes\n", usCnt);
|
|
ESP_LOGD(TAG, "RX: %d bytes\n", usCnt);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ return usCnt;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
BOOL xMBPortSerialTxPoll(void)
|
|
BOOL xMBPortSerialTxPoll(void)
|
|
@@ -136,7 +137,7 @@ BOOL xMBPortSerialTxPoll(void)
|
|
|
// Continue while all response bytes put in buffer or out of buffer
|
|
// Continue while all response bytes put in buffer or out of buffer
|
|
|
while((bNeedPoll) && (usCount++ < MB_SERIAL_BUF_SIZE)) {
|
|
while((bNeedPoll) && (usCount++ < MB_SERIAL_BUF_SIZE)) {
|
|
|
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
|
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
|
|
- bNeedPoll = pxMBFrameCBTransmitterEmpty( ); // callback to transmit FSM state machine
|
|
|
|
|
|
|
+ bNeedPoll = pxMBFrameCBTransmitterEmpty( ); // callback to transmit FSM
|
|
|
}
|
|
}
|
|
|
ESP_LOGD(TAG, "MB_TX_buffer send: (%d) bytes\n", (uint16_t)usCount);
|
|
ESP_LOGD(TAG, "MB_TX_buffer send: (%d) bytes\n", (uint16_t)usCount);
|
|
|
// Waits while UART sending the packet
|
|
// Waits while UART sending the packet
|
|
@@ -151,15 +152,23 @@ BOOL xMBPortSerialTxPoll(void)
|
|
|
static void vUartTask(void *pvParameters)
|
|
static void vUartTask(void *pvParameters)
|
|
|
{
|
|
{
|
|
|
uart_event_t xEvent;
|
|
uart_event_t xEvent;
|
|
|
|
|
+ USHORT usResult = 0;
|
|
|
for(;;) {
|
|
for(;;) {
|
|
|
if (xQueueReceive(xMbUartQueue, (void*)&xEvent, portMAX_DELAY) == pdTRUE) {
|
|
if (xQueueReceive(xMbUartQueue, (void*)&xEvent, portMAX_DELAY) == pdTRUE) {
|
|
|
ESP_LOGD(TAG, "MB_uart[%d] event:", ucUartNumber);
|
|
ESP_LOGD(TAG, "MB_uart[%d] event:", ucUartNumber);
|
|
|
switch(xEvent.type) {
|
|
switch(xEvent.type) {
|
|
|
//Event of UART receving data
|
|
//Event of UART receving data
|
|
|
case UART_DATA:
|
|
case UART_DATA:
|
|
|
- ESP_LOGD(TAG,"Receive data, len: %d", xEvent.size);
|
|
|
|
|
- // Read received data and send it to modbus stack
|
|
|
|
|
- vMBPortSerialRxPoll(xEvent.size);
|
|
|
|
|
|
|
+ ESP_LOGD(TAG,"Data event, length: %d", xEvent.size);
|
|
|
|
|
+ // This flag set in the event means that no more
|
|
|
|
|
+ // data received during configured timeout and UART TOUT feature is triggered
|
|
|
|
|
+ if (xEvent.timeout_flag) {
|
|
|
|
|
+ // Get buffered data length
|
|
|
|
|
+ ESP_ERROR_CHECK(uart_get_buffered_data_len(ucUartNumber, &xEvent.size));
|
|
|
|
|
+ // Read received data and send it to modbus stack
|
|
|
|
|
+ usResult = usMBPortSerialRxPoll(xEvent.size);
|
|
|
|
|
+ ESP_LOGD(TAG,"Timeout occured, processed: %d bytes", usResult);
|
|
|
|
|
+ }
|
|
|
break;
|
|
break;
|
|
|
//Event of HW FIFO overflow detected
|
|
//Event of HW FIFO overflow detected
|
|
|
case UART_FIFO_OVF:
|
|
case UART_FIFO_OVF:
|
|
@@ -251,12 +260,14 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
|
|
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
|
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
|
|
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
|
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
|
|
"mb serial driver failure, uart_driver_install() returned (0x%x).", xErr);
|
|
"mb serial driver failure, uart_driver_install() returned (0x%x).", xErr);
|
|
|
-#ifndef MB_TIMER_PORT_ENABLED
|
|
|
|
|
|
|
+#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
|
|
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
|
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
|
|
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
|
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
|
|
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
|
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
|
|
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", xErr);
|
|
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", xErr);
|
|
|
#endif
|
|
#endif
|
|
|
|
|
+ // Set always timeout flag to trigger timeout interrupt even after rx fifo full
|
|
|
|
|
+ uart_set_always_rx_timeout(ucUartNumber, true);
|
|
|
// Create a task to handle UART events
|
|
// Create a task to handle UART events
|
|
|
BaseType_t xStatus = xTaskCreate(vUartTask, "uart_queue_task", MB_SERIAL_TASK_STACK_SIZE,
|
|
BaseType_t xStatus = xTaskCreate(vUartTask, "uart_queue_task", MB_SERIAL_TASK_STACK_SIZE,
|
|
|
NULL, MB_SERIAL_TASK_PRIO, &xMbTaskHandle);
|
|
NULL, MB_SERIAL_TASK_PRIO, &xMbTaskHandle);
|