From ea151c42850182ae615090a47a8629a5d3b46bdb Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Mon, 5 Jul 2021 16:23:27 +0200 Subject: [PATCH] I2c tx/rx non-blocking --- Core/Src/main.c | 25 ++++++++++++++++--------- Core/Src/stm32g0xx_it.c | 19 ++++++++++++++----- Makefile | 16 ++++++++-------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 2bda898..79daaa9 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -8,6 +8,8 @@ UART_HandleTypeDef huart2; I2C_HandleTypeDef hi2c1; +uint8_t data[2] = {0}; + void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART2_UART_Init(void); @@ -35,21 +37,26 @@ int main(void) MX_USART2_UART_Init(); MX_I2C1_Init(); - uint8_t data[2] = {0}; data[0] = 1; // Config register - if(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x48 << 1), data,1, 1000) != HAL_OK) { + if(HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)(0x48 << 1), data, 1) != HAL_OK) { printf("I2c tx failed.\r\n"); - } else { - if(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(0x48 << 1), data, 2, 1000) != HAL_OK) { - printf("I2c rx failed.\r\n"); - } else { - uint16_t config = (uint16_t)(data[0] << 8) | data[1]; - printf("ADS1115 config register: 0x%04x\r\n", config); - } } while(1) {}; } +void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) +{ + if(HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)(0x48 << 1), data, 2) != HAL_OK) { + printf("I2c rx failed.\r\n"); + } +} + +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) +{ + uint16_t config = (uint16_t)(data[0] << 8) | data[1]; + printf("ADS1115 config register: 0x%04x\r\n", config); +} + void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; diff --git a/Core/Src/stm32g0xx_it.c b/Core/Src/stm32g0xx_it.c index 6fdeefb..6b8449b 100644 --- a/Core/Src/stm32g0xx_it.c +++ b/Core/Src/stm32g0xx_it.c @@ -1,16 +1,16 @@ #include "main.h" #include "stm32g0xx_it.h" +extern I2C_HandleTypeDef hi2c1; + void NMI_Handler(void) { - while (1) { - } + while (1) {} } void HardFault_Handler(void) { - while (1) { - } + while (1) {} } void SVC_Handler(void) @@ -23,5 +23,14 @@ void PendSV_Handler(void) void SysTick_Handler(void) { - HAL_IncTick(); + HAL_IncTick(); +} + +void I2C1_IRQHandler(void) +{ + if(hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) { + HAL_I2C_ER_IRQHandler(&hi2c1); + } else { + HAL_I2C_EV_IRQHandler(&hi2c1); + } } diff --git a/Makefile b/Makefile index a54e531..e54bca9 100644 --- a/Makefile +++ b/Makefile @@ -12,18 +12,18 @@ Core/Src/stm32g0xx_hal_msp.c \ Core/Src/system_stm32g0xx.c \ Core/Src/syscalls.c \ Core/Src/sysmem.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c \ Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c \ Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c \ Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c \ Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_iwdg.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c \ +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c \ Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c.c \ -Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_i2c_ex.c +Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c ASM_SOURCES = \ Core/Startup/startup_stm32g031y8yx.s @@ -64,7 +64,7 @@ LIBS = -lc -lm -lnosys LIBDIR = LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin +all: $(BUILD_DIR)/$(TARGET).elf OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) vpath %.c $(sort $(dir $(C_SOURCES)))