From bed177785ce7672378abad90fd68e32e8c095bd9 Mon Sep 17 00:00:00 2001 From: Duke NUCem Date: Thu, 30 Sep 2021 17:46:39 +0200 Subject: [PATCH] Moved MODBUS callback back to main --- fw/Core/Src/i2c.c | 14 ++++++++++++++ fw/Core/Src/main.c | 23 +++++++++++++++++++++++ fw/Core/Src/stm32l0xx_it.c | 13 ++++--------- tests/minimalmodbus_test.py | 2 +- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/fw/Core/Src/i2c.c b/fw/Core/Src/i2c.c index 8720c72..07aeaaf 100644 --- a/fw/Core/Src/i2c.c +++ b/fw/Core/Src/i2c.c @@ -6,6 +6,7 @@ */ #include "i2c.h" +#include "stm32l0xx_ll_lpuart.h" i2c_context_t *i2c_context; @@ -20,6 +21,10 @@ int i2c_init(i2c_context_t *context) int i2c_transmit(uint8_t address, uint8_t *buffer, int len) { + /* prevent interrupts during I2C communication (e.g. collision with MODBUS) */ +// LL_LPUART_Disable(LPUART1); +// LL_LPUART_DisableIT_RXNE(LPUART1); +// __disable_irq(); LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE); int i = 0; @@ -41,11 +46,17 @@ int i2c_transmit(uint8_t address, uint8_t *buffer, int len) // is raised everytime len != number of TXed bytes return I2C_ERROR_TX_INCOMPLETE; } +// __enable_irq(); +// LL_LPUART_Enable(LPUART1); +// LL_LPUART_EnableIT_RXNE(LPUART1); return I2C_OK; } int i2c_receive(uint8_t address, uint8_t *buffer, int len) { +// __disable_irq(); +// LL_LPUART_Disable(LPUART1); +// LL_LPUART_DisableIT_RXNE(LPUART1); LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ); int i = 0; @@ -60,5 +71,8 @@ int i2c_receive(uint8_t address, uint8_t *buffer, int len) if (len != i) { return I2C_ERROR_RX_INCOMPLETE; } +// __enable_irq(); +// LL_LPUART_Enable(LPUART1); +// LL_LPUART_EnableIT_RXNE(LPUART1); return I2C_OK; // TODO error detection } diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index 9993b8e..14ad827 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -231,6 +231,29 @@ int main(void) /* Enter the main loop */ while (1) { + if (lpuart1_rx_done == 1) + { + /* Process the message */ + if (lpuart1_rx_message_too_long) + { + /* Do nothing, just delete the buffer and set the flag back to zero*/ + lpuart1_rx_message_too_long = 0; + } else + { + /* Process the message: + * message is stored in modbus_buffer[], no copying necessary; + * but we need to make sure that modbus_buffer[] will not be used while + * processing the message: this can be done by disabling RX interrupt */ + LL_LPUART_DisableIT_RXNE(LPUART1); + modbus_slave_process_msg(modbus_buffer, lpuart1_rx_message_len); + /* Reset the RX DONE flag */ + lpuart1_rx_done = 0; + LL_LPUART_EnableIT_RXNE(LPUART1); + } + /* Reset the RX DONE flag */ + lpuart1_rx_done = 0; + } + /* It is time for measurement */ if (tim21_elapsed_period == 1) { diff --git a/fw/Core/Src/stm32l0xx_it.c b/fw/Core/Src/stm32l0xx_it.c index 9ee73af..504a5a5 100644 --- a/fw/Core/Src/stm32l0xx_it.c +++ b/fw/Core/Src/stm32l0xx_it.c @@ -45,6 +45,8 @@ /* USER CODE BEGIN PV */ uint16_t lpuart1_rx_message_index = 0; uint16_t lpuart1_rx_message_len = 0; +uint8_t lpuart1_rx_done = 0; +uint8_t lpuart1_rx_message_too_long = 0; uint8_t tim21_elapsed_period = 0; /* USER CODE END PV */ @@ -183,18 +185,11 @@ void LPUART1_IRQHandler(void) /* Reset the buffer index */ lpuart1_rx_message_len = lpuart1_rx_message_index; lpuart1_rx_message_index = 0; + lpuart1_rx_done = 1; if (lpuart1_rx_message_len > MODBUS_MAX_RTU_FRAME_SIZE) { - /* message too long, ignore */ - return; + lpuart1_rx_message_too_long = 1; } - /* Process the message: - * message is stored in modbus_buffer[], no copying necessary; - * but we need to make sure that modbus_buffer[] will not be used while - * processing the message: this can be done by disabling RX interrupt */ - LL_LPUART_DisableIT_RXNE(LPUART1); - modbus_slave_process_msg(modbus_buffer, lpuart1_rx_message_len); - LL_LPUART_EnableIT_RXNE(LPUART1); } /* USER CODE END LPUART1_IRQn 1 */ } diff --git a/tests/minimalmodbus_test.py b/tests/minimalmodbus_test.py index de43177..ca5a1ae 100755 --- a/tests/minimalmodbus_test.py +++ b/tests/minimalmodbus_test.py @@ -10,7 +10,7 @@ instrument.serial.baudrate = 115200 instrument.serial.bytesize = 8 instrument.serial.parity = serial.PARITY_EVEN instrument.serial.stopbits = 1 -instrument.serial.timeout = 0.05 # seconds +instrument.serial.timeout = 0.1 # seconds instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode instrument.clear_buffers_before_each_transaction = True