diff --git a/.cproject b/.cproject
index f4cce13..484ea0a 100644
--- a/.cproject
+++ b/.cproject
@@ -14,7 +14,7 @@
-
+
@@ -30,7 +30,7 @@
-
+
@@ -43,6 +43,7 @@
+
@@ -56,7 +57,6 @@
+
@@ -219,6 +218,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -233,7 +246,7 @@
-
+
@@ -248,7 +261,7 @@
-
+
@@ -261,18 +274,20 @@
+
-
-
-
-
-
-
-
-
+
-
-
+
diff --git a/system/include/drivers/serial.h b/system/include/drivers/serial.h
index 0cb0dad..d7e6575 100644
--- a/system/include/drivers/serial.h
+++ b/system/include/drivers/serial.h
@@ -55,6 +55,9 @@ typedef enum srl_error_reason {
typedef struct srl_context_t {
USART_TypeDef *port;
+ uint32_t port_baurate;
+ uint8_t port_stopbits;
+
GPIO_TypeDef* te_port;
uint16_t te_pin;
@@ -148,6 +151,7 @@ extern "C" {
void srl_init(srl_context_t *ctx, USART_TypeDef *port, uint8_t *rx_buffer, uint16_t rx_buffer_size, uint8_t *tx_buffer, uint16_t tx_buffer_size, uint32_t baudrate, uint8_t stop_bits);
+void srl_close(srl_context_t *ctx);
uint8_t srl_send_data(srl_context_t *ctx, uint8_t* data, uint8_t mode, uint16_t leng, uint8_t internal_external);
uint8_t srl_start_tx(srl_context_t *ctx, short leng);
void srl_wait_for_tx_completion(srl_context_t *ctx);
diff --git a/system/src/drivers/serial.c b/system/src/drivers/serial.c
index 311a4bf..8be1952 100644
--- a/system/src/drivers/serial.c
+++ b/system/src/drivers/serial.c
@@ -50,9 +50,15 @@ void srl_init(
ctx->srl_tx_buf_ln = rx_buffer_size;
#endif
+ memset(ctx->srl_rx_buf_pointer, 0x00, ctx->srl_rx_buf_ln);
+ memset(ctx->srl_tx_buf_pointer, 0x00, ctx->srl_tx_buf_ln);
+
ctx->port = port;
- ctx->te_port = 0;
- ctx->te_pin = 0;
+ ctx->port_baurate = baudrate;
+ ctx->port_stopbits = stop_bits;
+
+ //ctx->te_port = 0;
+ //ctx->te_pin = 0;
ctx->srl_tx_start_time = 0xFFFFFFFFu;
@@ -93,6 +99,23 @@ void srl_init(
ctx->srl_rx_timeout_calc_started = 0;
ctx->srl_rx_idle_counter = 0;
+
+ ctx->srl_rx_start_time = 0;
+ ctx->srl_rx_waiting_start_time = 0;
+
+ ctx->srl_rx_timeout_enable = 0;
+ ctx->srl_rx_timeout_waiting_enable = 0;
+}
+
+void srl_close(srl_context_t *ctx) {
+ USART_DeInit(ctx->port);
+
+ if (ctx->te_port != NULL && ctx->te_pin != 0) {
+ GPIO_ResetBits(ctx->te_port, ctx->te_pin);
+ }
+
+ ctx->srl_rx_state = SRL_RX_NOT_CONFIG;
+ ctx->srl_tx_state = SRL_TX_NOT_CONFIG;
}
// this function shall be called in 10ms periods by some timer to check the timeout
@@ -446,6 +469,7 @@ uint8_t srl_receive_data_with_callback(srl_context_t *ctx, srl_rx_termination_ca
ctx->srl_rx_state = SRL_RXING;
ctx->port->CR1 |= USART_CR1_RE; // uruchamianie odbiornika
ctx->port->CR1 |= USART_CR1_RXNEIE;
+ ctx->port->CR1 |= USART_CR1_IDLEIE;
ctx->srl_rx_waiting_start_time = master_time;
ctx->srl_rx_start_time = master_time;
@@ -467,7 +491,9 @@ void srl_irq_handler(srl_context_t *ctx) {
// local variable to store
uint8_t value = 0;
- if ((ctx->port->SR & USART_SR_ORE) == USART_SR_IDLE) {
+ if ((ctx->port->SR & USART_SR_IDLE) == USART_SR_IDLE) {
+ ctx->srl_garbage_storage = (uint8_t)ctx->port->DR;
+
ctx->srl_rx_idle_counter++;
}
@@ -572,6 +598,7 @@ void srl_irq_handler(srl_context_t *ctx) {
// disabling UART receiver and its interrupt
ctx->port->CR1 &= (0xFFFFFFFF ^ USART_CR1_RE);
ctx->port->CR1 &= (0xFFFFFFFF ^ USART_CR1_RXNEIE);
+ ctx->port->CR1 &= (0xFFFFFFFF ^ USART_CR1_IDLEIE);
}
break;
diff --git a/system/src/modbus_rtu/rtu_serial_io.c b/system/src/modbus_rtu/rtu_serial_io.c
index b4024d8..b99c945 100644
--- a/system/src/modbus_rtu/rtu_serial_io.c
+++ b/system/src/modbus_rtu/rtu_serial_io.c
@@ -182,6 +182,15 @@ int32_t rtu_serial_pool(rtu_pool_queue_t* queue, srl_context_t* serial_context)
switch (rtu_pool) {
case RTU_POOL_IDLE: {
+ srl_init( serial_context,
+ serial_context->port,
+ serial_context->srl_rx_buf_pointer,
+ serial_context->srl_rx_buf_ln,
+ serial_context->srl_tx_buf_pointer,
+ serial_context->srl_tx_buf_ln,
+ serial_context->port_baurate,
+ serial_context->port_stopbits);
+
// Enabling the timeout for Modbus-RTU.
// This timeout starts after first received byte and triggers if
// the slave will hang up and stop the transmission before the end of the frame
@@ -320,6 +329,8 @@ int32_t rtu_serial_pool(rtu_pool_queue_t* queue, srl_context_t* serial_context)
// get current time to start the inter-frame delay
rtu_time_of_last_receive = main_get_master_time();
+ srl_close(serial_context);
+
}
// in case of any error during data reception or the serial driver have fallen into unknown & unexpected
@@ -328,7 +339,10 @@ int32_t rtu_serial_pool(rtu_pool_queue_t* queue, srl_context_t* serial_context)
rte_wx_last_modbus_rx_error_timestamp = main_get_master_time();
rtu_pool = RTU_POOL_RECEIVE_ERROR;
+
+ srl_close(serial_context);
}
+
break;
}
case RTU_POOL_WAIT_AFTER_RECEIVE: {