Moved MODBUS callback back to main
This commit is contained in:
parent
e7f7077b8a
commit
bed177785c
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include "stm32l0xx_ll_lpuart.h"
|
||||||
|
|
||||||
i2c_context_t *i2c_context;
|
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)
|
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_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
|
||||||
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
|
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
|
||||||
int i = 0;
|
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
|
// is raised everytime len != number of TXed bytes
|
||||||
return I2C_ERROR_TX_INCOMPLETE;
|
return I2C_ERROR_TX_INCOMPLETE;
|
||||||
}
|
}
|
||||||
|
// __enable_irq();
|
||||||
|
// LL_LPUART_Enable(LPUART1);
|
||||||
|
// LL_LPUART_EnableIT_RXNE(LPUART1);
|
||||||
return I2C_OK;
|
return I2C_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_receive(uint8_t address, uint8_t *buffer, int len)
|
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_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
|
||||||
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
|
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -60,5 +71,8 @@ int i2c_receive(uint8_t address, uint8_t *buffer, int len)
|
|||||||
if (len != i) {
|
if (len != i) {
|
||||||
return I2C_ERROR_RX_INCOMPLETE;
|
return I2C_ERROR_RX_INCOMPLETE;
|
||||||
}
|
}
|
||||||
|
// __enable_irq();
|
||||||
|
// LL_LPUART_Enable(LPUART1);
|
||||||
|
// LL_LPUART_EnableIT_RXNE(LPUART1);
|
||||||
return I2C_OK; // TODO error detection
|
return I2C_OK; // TODO error detection
|
||||||
}
|
}
|
||||||
|
@ -231,6 +231,29 @@ int main(void)
|
|||||||
/* Enter the main loop */
|
/* Enter the main loop */
|
||||||
while (1)
|
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 */
|
/* It is time for measurement */
|
||||||
if (tim21_elapsed_period == 1)
|
if (tim21_elapsed_period == 1)
|
||||||
{
|
{
|
||||||
|
@ -45,6 +45,8 @@
|
|||||||
/* USER CODE BEGIN PV */
|
/* USER CODE BEGIN PV */
|
||||||
uint16_t lpuart1_rx_message_index = 0;
|
uint16_t lpuart1_rx_message_index = 0;
|
||||||
uint16_t lpuart1_rx_message_len = 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;
|
uint8_t tim21_elapsed_period = 0;
|
||||||
/* USER CODE END PV */
|
/* USER CODE END PV */
|
||||||
@ -183,18 +185,11 @@ void LPUART1_IRQHandler(void)
|
|||||||
/* Reset the buffer index */
|
/* Reset the buffer index */
|
||||||
lpuart1_rx_message_len = lpuart1_rx_message_index;
|
lpuart1_rx_message_len = lpuart1_rx_message_index;
|
||||||
lpuart1_rx_message_index = 0;
|
lpuart1_rx_message_index = 0;
|
||||||
|
lpuart1_rx_done = 1;
|
||||||
if (lpuart1_rx_message_len > MODBUS_MAX_RTU_FRAME_SIZE)
|
if (lpuart1_rx_message_len > MODBUS_MAX_RTU_FRAME_SIZE)
|
||||||
{
|
{
|
||||||
/* message too long, ignore */
|
lpuart1_rx_message_too_long = 1;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
/* 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 */
|
/* USER CODE END LPUART1_IRQn 1 */
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ instrument.serial.baudrate = 115200
|
|||||||
instrument.serial.bytesize = 8
|
instrument.serial.bytesize = 8
|
||||||
instrument.serial.parity = serial.PARITY_EVEN
|
instrument.serial.parity = serial.PARITY_EVEN
|
||||||
instrument.serial.stopbits = 1
|
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.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
|
||||||
instrument.clear_buffers_before_each_transaction = True
|
instrument.clear_buffers_before_each_transaction = True
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user