Put the modbus and rs485 codes together. Briefly tested.
This commit is contained in:
@@ -99,11 +99,13 @@ void Error_Handler(void);
|
||||
/* USER CODE BEGIN Private defines */
|
||||
#define MEASUREMENT_PERIOD_MS 600000
|
||||
|
||||
extern uint8_t lpuart1_rx_message_index;
|
||||
extern uint8_t lpuart1_rx_message_len;
|
||||
extern uint16_t lpuart1_rx_message_index;
|
||||
extern uint16_t lpuart1_rx_message_len;
|
||||
extern uint8_t lpuart1_rx_done;
|
||||
extern uint8_t lpuart1_rx_message_too_long;
|
||||
|
||||
extern uint8_t tim21_elapsed_period;
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -111,7 +111,7 @@ static void MX_I2C1_Init(void);
|
||||
static void MX_LPUART1_UART_Init(void);
|
||||
static void MX_TIM21_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
void LPUART1_TX_Buffer(uint8_t* buffer_tx, uint8_t buffer_tx_len);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
@@ -131,7 +131,9 @@ int8_t modbus_slave_callback(modbus_transaction_t *transaction)
|
||||
|
||||
int8_t modbus_transmit_function(uint8_t *buffer, int data_len)
|
||||
{
|
||||
return MODBUS_OK; /* TODO */
|
||||
/* TODO */
|
||||
LPUART1_TX_Buffer(buffer, data_len);
|
||||
return MODBUS_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -189,23 +191,28 @@ int main(void)
|
||||
i2c_context.i2c = I2C1;
|
||||
i2c_init(&i2c_context);
|
||||
|
||||
modbus_slave_set_address(0x11);
|
||||
|
||||
scd4x_start_periodic_measurement();
|
||||
uint8_t scd4x_is_connected = 1;
|
||||
uint8_t sps30_is_connected = 0;
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
uint8_t scd4x_is_connected = 0;
|
||||
/*uint8_t scd4x_is_connected = 0;
|
||||
if (scd4x_start_periodic_measurement() == SCD4X_OK)
|
||||
{
|
||||
scd4x_is_connected = 1;
|
||||
}
|
||||
}*/
|
||||
|
||||
/* Attempt to start SPS30 measurement and check if it's connected */
|
||||
sps30_reset();
|
||||
/*sps30_reset();
|
||||
uint8_t sps30_is_connected = 0;
|
||||
if (sps30_start_measurement() == SPS30_OK)
|
||||
{
|
||||
sps30_is_connected = 1;
|
||||
}
|
||||
}*/
|
||||
|
||||
/* Wait 1000ms for sensors initialization */
|
||||
/* SHT4x Init Time: max 1 ms (datasheet pg. 8) */
|
||||
@@ -222,15 +229,28 @@ int main(void)
|
||||
/* UART RX is done */
|
||||
if (lpuart1_rx_done == 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);
|
||||
/* 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;
|
||||
LL_LPUART_EnableIT_RXNE(LPUART1);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* It is time for measurement */
|
||||
@@ -410,65 +430,78 @@ static void MX_I2C1_Init(void)
|
||||
static void MX_LPUART1_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN LPUART1_Init 0 */
|
||||
/* USER CODE BEGIN LPUART1_Init 0 */
|
||||
|
||||
/* USER CODE END LPUART1_Init 0 */
|
||||
/* USER CODE END LPUART1_Init 0 */
|
||||
|
||||
LL_LPUART_InitTypeDef LPUART_InitStruct = {0};
|
||||
LL_LPUART_InitTypeDef LPUART_InitStruct = {0};
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1);
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1);
|
||||
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
/**LPUART1 GPIO Configuration
|
||||
PA0-CK_IN ------> LPUART1_RX
|
||||
PA1 ------> LPUART1_TX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
/**LPUART1 GPIO Configuration
|
||||
PA0-CK_IN ------> LPUART1_RX
|
||||
PA1 ------> LPUART1_TX
|
||||
PB1 ------> LPUART1_DE
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* LPUART1 interrupt Init */
|
||||
NVIC_SetPriority(LPUART1_IRQn, 0);
|
||||
NVIC_EnableIRQ(LPUART1_IRQn);
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_4;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN LPUART1_Init 1 */
|
||||
/* LPUART1 interrupt Init */
|
||||
NVIC_SetPriority(LPUART1_IRQn, 0);
|
||||
NVIC_EnableIRQ(LPUART1_IRQn);
|
||||
|
||||
/* USER CODE END LPUART1_Init 1 */
|
||||
LPUART_InitStruct.BaudRate = 115200;
|
||||
LPUART_InitStruct.DataWidth = LL_LPUART_DATAWIDTH_9B;
|
||||
LPUART_InitStruct.StopBits = LL_LPUART_STOPBITS_1;
|
||||
LPUART_InitStruct.Parity = LL_LPUART_PARITY_EVEN;
|
||||
LPUART_InitStruct.TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
||||
LPUART_InitStruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
||||
LL_LPUART_Init(LPUART1, &LPUART_InitStruct);
|
||||
LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_SWAPPED);
|
||||
/* USER CODE BEGIN LPUART1_Init 2 */
|
||||
/* USER CODE BEGIN LPUART1_Init 1 */
|
||||
|
||||
/* Enable IDLE Interrupt */
|
||||
LL_LPUART_EnableIT_IDLE(LPUART1);
|
||||
/* USER CODE END LPUART1_Init 1 */
|
||||
LPUART_InitStruct.BaudRate = 115200;
|
||||
LPUART_InitStruct.DataWidth = LL_LPUART_DATAWIDTH_9B;
|
||||
LPUART_InitStruct.StopBits = LL_LPUART_STOPBITS_1;
|
||||
LPUART_InitStruct.Parity = LL_LPUART_PARITY_EVEN;
|
||||
LPUART_InitStruct.TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
||||
LPUART_InitStruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
||||
LL_LPUART_Init(LPUART1, &LPUART_InitStruct);
|
||||
LL_LPUART_EnableDEMode(LPUART1);
|
||||
LL_LPUART_SetDESignalPolarity(LPUART1, LL_LPUART_DE_POLARITY_HIGH);
|
||||
LL_LPUART_SetDEAssertionTime(LPUART1, 0);
|
||||
LL_LPUART_SetDEDeassertionTime(LPUART1, 0);
|
||||
/* USER CODE BEGIN LPUART1_Init 2 */
|
||||
|
||||
/* Enable RX Not Empty Interrupt */
|
||||
LL_LPUART_EnableIT_RXNE(LPUART1);
|
||||
/* Enable IDLE Interrupt */
|
||||
LL_LPUART_EnableIT_IDLE(LPUART1);
|
||||
|
||||
LL_LPUART_EnableDirectionRx(LPUART1);
|
||||
LL_LPUART_EnableDirectionTx(LPUART1);
|
||||
LL_LPUART_Enable(LPUART1);
|
||||
/* USER CODE END LPUART1_Init 2 */
|
||||
/* Enable RX Not Empty Interrupt */
|
||||
LL_LPUART_EnableIT_RXNE(LPUART1);
|
||||
|
||||
LL_LPUART_EnableDirectionRx(LPUART1);
|
||||
LL_LPUART_EnableDirectionTx(LPUART1);
|
||||
LL_LPUART_Enable(LPUART1);
|
||||
/* USER CODE END LPUART1_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
@@ -559,6 +592,16 @@ static void MX_GPIO_Init(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
void LPUART1_TX_Buffer(uint8_t* buffer_tx, uint8_t buffer_tx_len)
|
||||
{
|
||||
__disable_irq();
|
||||
for (uint8_t i = 0; i < buffer_tx_len; i++)
|
||||
{
|
||||
LL_LPUART_TransmitData9(LPUART1, buffer_tx[i]);
|
||||
while (!LL_LPUART_IsActiveFlag_TXE(LPUART1));
|
||||
}
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "stm32l0xx_it.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "modbus.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -42,9 +43,10 @@
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
uint8_t lpuart1_rx_message_index = 0;
|
||||
uint8_t lpuart1_rx_message_len = 0;
|
||||
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 */
|
||||
@@ -187,6 +189,10 @@ void LPUART1_IRQHandler(void)
|
||||
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)
|
||||
{
|
||||
lpuart1_rx_message_too_long = 1;
|
||||
}
|
||||
}
|
||||
/* USER CODE END LPUART1_IRQn 1 */
|
||||
}
|
||||
@@ -195,10 +201,11 @@ void LPUART1_IRQHandler(void)
|
||||
void LPUART1_CharReception_Callback( void )
|
||||
{
|
||||
uint16_t lpuart1_rx_bit = LL_LPUART_ReceiveData9(LPUART1);
|
||||
if (lpuart1_rx_message_index < (MODBUS_BUFFER_SIZE - 1)) {
|
||||
/* buffer (defined in modbus.c) is shared for TX and RX */
|
||||
modbus_buffer[lpuart1_rx_message_index++] = (uint8_t)lpuart1_rx_bit;
|
||||
if (lpuart1_rx_message_index < MODBUS_MAX_RTU_FRAME_SIZE)
|
||||
{
|
||||
modbus_buffer[lpuart1_rx_message_index] = (uint8_t)lpuart1_rx_bit;
|
||||
}
|
||||
lpuart1_rx_message_index++;
|
||||
}
|
||||
/* USER CODE END 1 */
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
||||
Reference in New Issue
Block a user