diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-11 14:27:53 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-11 14:27:53 +0200 |
commit | 5b1f94522624e18fe9fd94478ada1c8d8997b747 (patch) | |
tree | d9a34d21a620a700b9fb0dbf5ba4cef59fb44683 | |
parent | 687767404ede06182a5d1f09ab3b7c66ba5f4d13 (diff) |
more WIP, untested i2c but usart2 works
-rw-r--r-- | stm32f091/Si7021_driver_STM32.patch | 25 | ||||
-rw-r--r-- | stm32f091/main.c | 13 | ||||
-rw-r--r-- | stm32f091/main.h | 7 | ||||
-rw-r--r-- | stm32f091/sensor.c | 4 | ||||
-rw-r--r-- | stm32f091/setup.c | 132 | ||||
-rw-r--r-- | stm32f091/setup.h | 22 | ||||
-rw-r--r-- | stm32f091/stm32f0xx_hal_msp.c | 52 | ||||
-rw-r--r-- | stm32f091/stm32f0xx_hal_msp.h | 9 |
8 files changed, 99 insertions, 165 deletions
diff --git a/stm32f091/Si7021_driver_STM32.patch b/stm32f091/Si7021_driver_STM32.patch deleted file mode 100644 index eff7623..0000000 --- a/stm32f091/Si7021_driver_STM32.patch +++ /dev/null @@ -1,25 +0,0 @@ -diff --git a/driver/inc/Si7021_driver.h b/driver/inc/Si7021_driver.h -index 7ed1f4a..50803ef 100644 ---- a/driver/inc/Si7021_driver.h -+++ b/driver/inc/Si7021_driver.h -@@ -1,7 +1,7 @@ - #ifndef SI7021_H_ - #define SI7021_H_ - --#include "stm32f4xx_hal.h" -+#include "stm32f0xx_hal.h" - - #define RES0 0 - #define RES1 7 -diff --git a/driver/src/Si7021_driver.c b/driver/src/Si7021_driver.c -index 47df31c..d11ee38 100644 ---- a/driver/src/Si7021_driver.c -+++ b/driver/src/Si7021_driver.c -@@ -1,5 +1,6 @@ - #include <Si7021_driver.h> --#include <i2c.h> -+#include "stm32f0xx_hal_i2c.h" -+#include "main.h" - - static const uint16_t I2C_ADDR = (0x40<<1); // Si7021 I2C address - static const uint8_t HEATER_CURRENT_OFFSET = 3; // current value in mA for register value 0 diff --git a/stm32f091/main.c b/stm32f091/main.c index 61608a8..aec5869 100644 --- a/stm32f091/main.c +++ b/stm32f091/main.c @@ -1,25 +1,14 @@ #include <FreeRTOS.h> #include <task.h> #include <stm32f0xx_hal.h> -#include <stm32f0xx_hal_msp.h> #include "main.h" #include "setup.h" #include "sensor.h" #include "backlog.h" -I2C_HandleTypeDef hi2c1; -UART_HandleTypeDef huart2; - int main() { - HAL_Init(); - - SystemClock_Config(); - MX_GPIO_Init(); - MX_USART2_UART_Init(); - MX_I2C2_Init(); - - HAL_I2C_MspInit(&hi2c1); + ws_io_setup(); ws_backlog_alloc(24 * 60); ws_sensor_read(); diff --git a/stm32f091/main.h b/stm32f091/main.h index b8a1db5..9595166 100644 --- a/stm32f091/main.h +++ b/stm32f091/main.h @@ -1,10 +1,3 @@ #pragma once -#include <stm32f0xx_hal_i2c.h> -#include <stm32f0xx_hal_uart.h> - -extern I2C_HandleTypeDef hi2c1; -extern UART_HandleTypeDef huart2; - -void task_1(); int main(); diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c index fc2fe2a..04181cf 100644 --- a/stm32f091/sensor.c +++ b/stm32f091/sensor.c @@ -2,7 +2,7 @@ #include <task.h> #include <stm32f0xx_hal.h> -#include "main.h" +#include "setup.h" #include "sensor.h" #include "backlog.h" @@ -11,6 +11,8 @@ #define REG_HUM ((uint8_t)(0xF5)) uint8_t ws_sensor_temperature() { + HAL_UART_Transmit(&huart2, (uint8_t * const) "hello world!", 13, 100); + return 0; uint8_t buf[12]; int16_t val; float temp_c; diff --git a/stm32f091/setup.c b/stm32f091/setup.c index d10393c..5f2db16 100644 --- a/stm32f091/setup.c +++ b/stm32f091/setup.c @@ -3,29 +3,43 @@ #include <stm32f0xx_hal_i2c.h> #include <stm32f0xx_hal_uart.h> -#include "main.h" #include "setup.h" -#define B1_Pin GPIO_PIN_13 -#define B1_GPIO_Port GPIOC -#define USART_TX_Pin GPIO_PIN_2 -#define USART_TX_GPIO_Port GPIOA -#define USART_RX_Pin GPIO_PIN_3 -#define USART_RX_GPIO_Port GPIOA -#define LD2_Pin GPIO_PIN_5 -#define LD2_GPIO_Port GPIOA -#define TMS_Pin GPIO_PIN_13 -#define TMS_GPIO_Port GPIOA -#define TCK_Pin GPIO_PIN_14 -#define TCK_GPIO_Port GPIOA - -void SystemClock_Config() { +I2C_HandleTypeDef hi2c1; +UART_HandleTypeDef huart2; + +static void ws_io_clock_setup(); +static void ws_io_i2c_setup(); +static void ws_io_usart2_setup(); +static void ws_io_gpio_setup(); +static void ws_setup_error_handler(); + +void ws_io_setup() { + HAL_Init(); + + ws_io_clock_setup(); + ws_io_gpio_setup(); + ws_io_i2c_setup(); + ws_io_usart2_setup(); +} + +static void ws_io_clock_setup() { + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_I2C1_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_USART2_CLK_ENABLE(); + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; - // Initializes the RCC Oscillators according to the specified parameters in - // the RCC_OscInitTypeDef structure. + /** Initializes the RCC Oscillators according to the specified parameters in + * the RCC_OscInitTypeDef structure. + */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; @@ -33,37 +47,44 @@ void SystemClock_Config() { RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) return Error_Handler(); + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) return ws_setup_error_handler(); - // Initializes the CPU, AHB and APB buses clocks + /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1; + |RCC_CLOCKTYPE_PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) return Error_Handler(); - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2; + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) return ws_setup_error_handler(); + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1; PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) return Error_Handler(); + PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) return ws_setup_error_handler(); } -void MX_I2C2_Init() { - hi2c1.Instance = I2C1; - hi2c1.Init.Timing = 0x2000090E; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) return Error_Handler(); - if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) return Error_Handler(); - if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) return Error_Handler(); +static void ws_io_i2c_setup() { + hi2c1.Instance = I2C1; + hi2c1.Init.Timing = 0x2000090E; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) return ws_setup_error_handler(); + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) return ws_setup_error_handler(); + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) return ws_setup_error_handler(); } -void MX_USART2_UART_Init() { +static void ws_io_usart2_setup() { huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; @@ -74,32 +95,35 @@ void MX_USART2_UART_Init() { huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; - if (HAL_UART_Init(&huart2) != HAL_OK) return Error_Handler(); + if (HAL_UART_Init(&huart2) != HAL_OK) return ws_setup_error_handler(); } -void MX_GPIO_Init() { +static void ws_io_gpio_setup() { GPIO_InitTypeDef GPIO_InitStruct = {0}; - - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOF_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - - HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); - - GPIO_InitStruct.Pin = B1_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + /**I2C1 GPIO Configuration + PB8 ------> I2C1_SCL + PB9 ------> I2C1_SDA + */ + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; //TODO: use #defines in setup.h + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF1_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - GPIO_InitStruct.Pin = LD2_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + /**USART2 GPIO Configuration + PA2 ------> USART2_TX + PA3 ------> USART2_RX + */ + GPIO_InitStruct.Pin = WS_PINOUT_USART_TX_PIN | WS_PINOUT_USART_RX_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); + GPIO_InitStruct.Alternate = GPIO_AF1_USART2; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } -void Error_Handler() { +static void ws_setup_error_handler() { __disable_irq(); for(;;); } diff --git a/stm32f091/setup.h b/stm32f091/setup.h index 36eb9af..bca0e82 100644 --- a/stm32f091/setup.h +++ b/stm32f091/setup.h @@ -1,7 +1,19 @@ #pragma once -void SystemClock_Config(); -void MX_I2C2_Init(); -void MX_USART2_UART_Init(); -void MX_GPIO_Init(); -void Error_Handler(); +#include <stm32f0xx_hal_i2c.h> +#include <stm32f0xx_hal_uart.h> + +#define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9 +#define WS_PINOUT_I2C_SDA_PORT GPIOB +#define WS_PINOUT_I2C_SCL_PIN GPIO_PIN_8 +#define WS_PINOUT_I2C_SCL_PORT GPIOB +#define WS_PINOUT_USART_RX_PIN GPIO_PIN_3 +#define WS_PINOUT_USART_RX_PORT GPIOA +#define WS_PINOUT_USART_TX_PIN GPIO_PIN_2 +#define WS_PINOUT_USART_TX_PORT GPIOA + +extern I2C_HandleTypeDef hi2c1; +extern UART_HandleTypeDef huart2; + +void ws_io_setup(); + diff --git a/stm32f091/stm32f0xx_hal_msp.c b/stm32f091/stm32f0xx_hal_msp.c deleted file mode 100644 index d99b334..0000000 --- a/stm32f091/stm32f0xx_hal_msp.c +++ /dev/null @@ -1,52 +0,0 @@ -#include <stm32f0xx_hal.h> - -#include "main.h" -#include "stm32f0xx_hal_msp.h" - -void HAL_MspInit(void) { - __HAL_RCC_SYSCFG_CLK_ENABLE(); - __HAL_RCC_PWR_CLK_ENABLE(); -} - -void HAL_UART_MspInit(UART_HandleTypeDef* huart) { - return; - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(huart->Instance==USART2) { - __HAL_RCC_USART2_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF1_USART2; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } -} - -void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) { - return; - if(huart->Instance==USART2) { - __HAL_RCC_USART2_CLK_DISABLE(); - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); - } -} - -void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if (hi2c->Instance==I2C1) { - __HAL_RCC_GPIOB_CLK_ENABLE(); - /** - * I2C1 GPIO Configuration - * PB8 ------> I2C1_SCL - * PB9 ------> I2C1_SDA - */ - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF1_I2C1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - __HAL_RCC_I2C1_CLK_ENABLE(); - } -} - diff --git a/stm32f091/stm32f0xx_hal_msp.h b/stm32f091/stm32f0xx_hal_msp.h deleted file mode 100644 index 3ffdf48..0000000 --- a/stm32f091/stm32f0xx_hal_msp.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include <stm32f0xx_hal.h> -#include <stm32f0xx_hal_uart.h> - -void HAL_MspInit(void); -void HAL_UART_MspInit(UART_HandleTypeDef* huart); -void HAL_UART_MspDeInit(UART_HandleTypeDef* huart); - |