From a290f52d4d318ea2295fd225e01d71422c3907bb Mon Sep 17 00:00:00 2001 From: Duke NUCem Date: Wed, 20 Oct 2021 18:14:13 +0200 Subject: [PATCH] Disable UART IRQs during sensor readout --- fw/Core/Inc/main.h | 2 ++ fw/Core/Src/main.c | 32 ++++++++++++++++++++++++++------ fw/Core/Src/scd4x.c | 4 +++- fw/Core/Src/sht4x.c | 3 +++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/fw/Core/Inc/main.h b/fw/Core/Inc/main.h index 238f0be..a144a49 100644 --- a/fw/Core/Inc/main.h +++ b/fw/Core/Inc/main.h @@ -75,6 +75,8 @@ extern "C" { void Error_Handler(void); /* USER CODE BEGIN EFP */ +int8_t uart_disable_interrupts(void); +int8_t uart_enable_interrupts(void); /* USER CODE END EFP */ diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index cb9ac02..23f6270 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -269,15 +269,17 @@ int main(void) if (baudrate_changed) { while (!LL_LPUART_IsActiveFlag_TXE(LPUART1)); - LL_LPUART_Disable(LPUART1); - LL_LPUART_DisableIT_IDLE(LPUART1); - LL_LPUART_EnableIT_RXNE(LPUART1); + uart_disable_interrupts(); +// LL_LPUART_Disable(LPUART1); +// LL_LPUART_DisableIT_IDLE(LPUART1); +// LL_LPUART_EnableIT_RXNE(LPUART1); LL_LPUART_SetBaudRate(LPUART1, SYSTICK_FREQ_HZ, config_baudrates[sensor_config.baudrate_index]); - LL_LPUART_Enable(LPUART1); - LL_LPUART_EnableIT_IDLE(LPUART1); - LL_LPUART_EnableIT_RXNE(LPUART1); +// LL_LPUART_Enable(LPUART1); +// LL_LPUART_EnableIT_IDLE(LPUART1); +// LL_LPUART_EnableIT_RXNE(LPUART1); + uart_enable_interrupts(); LL_LPUART_EnableDirectionRx(LPUART1); LL_LPUART_EnableDirectionTx(LPUART1); baudrate_changed = 0; @@ -645,6 +647,24 @@ void LPUART1_TX_Buffer(uint8_t* buffer_tx, uint16_t buffer_tx_len) __enable_irq(); } +int8_t uart_disable_interrupts(void) +{ + LL_LPUART_Disable(LPUART1); + LL_LPUART_DisableIT_IDLE(LPUART1); + LL_LPUART_EnableIT_RXNE(LPUART1); + + return 0; +} + +int8_t uart_enable_interrupts(void) +{ + LL_LPUART_Enable(LPUART1); + LL_LPUART_EnableIT_IDLE(LPUART1); + LL_LPUART_EnableIT_RXNE(LPUART1); + + return 0; +} + int8_t modbus_slave_callback(modbus_transaction_t *transaction) { uint16_t register_number = transaction->register_number; diff --git a/fw/Core/Src/scd4x.c b/fw/Core/Src/scd4x.c index 450cea0..1fc96ec 100644 --- a/fw/Core/Src/scd4x.c +++ b/fw/Core/Src/scd4x.c @@ -6,6 +6,7 @@ */ #include "scd4x.h" +#include "main.h" /* for uart_disable_interrupts() */ int8_t scd4x_send_cmd(scd4x_cmd_t cmd) { @@ -74,8 +75,9 @@ int8_t scd4x_read_measurement(uint16_t * co2, int16_t *temperature, uint16_t *re /*if (result != I2C_OK) { return SCD4X_ERROR; }*/ - + uart_disable_interrupts(); LL_mDelay(1); // 10 ms should be enough + uart_enable_interrupts(); // read out result = i2c_receive(SCD4X_I2C_ADDRESS<<1, buffer, 9); if (result != I2C_OK) diff --git a/fw/Core/Src/sht4x.c b/fw/Core/Src/sht4x.c index 554da83..af6c23b 100644 --- a/fw/Core/Src/sht4x.c +++ b/fw/Core/Src/sht4x.c @@ -6,6 +6,7 @@ */ #include "sht4x.h" +#include "main.h" /* for uart_disable_interrupts() */ int8_t sht4x_send_cmd(sht4x_cmd_t cmd) { @@ -30,7 +31,9 @@ int8_t sht4x_measure(int16_t *temperature, uint16_t *relative_humidity) if (result != I2C_OK) { return SHT4X_ERROR; }*/ + uart_disable_interrupts(); LL_mDelay(10); // 10 ms should be enough + uart_enable_interrupts(); // read out result = i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6); if (result != I2C_OK) {