aboutsummaryrefslogtreecommitdiff
path: root/stm32f091
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-05 20:23:58 +0200
committerlonkaars <loek@pipeframe.xyz>2022-10-05 20:23:58 +0200
commit6e63028aec15f93bf82a0452b45a6b4dfec81f05 (patch)
tree3416de57d96b7bcbcf6504fd209bd38f14cf04eb /stm32f091
parent1000c8fd861b046a72bb3957dcc04cd6b28ceede (diff)
merge bjorn's code
Diffstat (limited to 'stm32f091')
-rw-r--r--stm32f091/Si7021_driver_STM32.patch25
m---------stm32f091/lib/Si7021_driver_STM320
-rw-r--r--stm32f091/main.c28
-rw-r--r--stm32f091/main.h12
-rw-r--r--stm32f091/makefile2
-rw-r--r--stm32f091/sensor.c37
-rw-r--r--stm32f091/sensor.h20
-rw-r--r--stm32f091/setup.c106
-rw-r--r--stm32f091/setup.h7
9 files changed, 219 insertions, 18 deletions
diff --git a/stm32f091/Si7021_driver_STM32.patch b/stm32f091/Si7021_driver_STM32.patch
new file mode 100644
index 0000000..eff7623
--- /dev/null
+++ b/stm32f091/Si7021_driver_STM32.patch
@@ -0,0 +1,25 @@
+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/lib/Si7021_driver_STM32 b/stm32f091/lib/Si7021_driver_STM32
new file mode 160000
+Subproject cae65b26c416b7ebe870d0b16886e74b3a652af
diff --git a/stm32f091/main.c b/stm32f091/main.c
index 6d1fb38..68e8dd4 100644
--- a/stm32f091/main.c
+++ b/stm32f091/main.c
@@ -1,30 +1,22 @@
#include <FreeRTOS.h>
#include <task.h>
-#include <stm32f0xx.h>
-#include <stdint.h>
#include <stm32f0xx_hal.h>
-void task_1() {
- uint8_t led = 1;
+#include "main.h"
+#include "setup.h"
+#include "sensor.h"
- while (1) {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, led);
- led ^= 1;
-
- vTaskDelay(1000 / portTICK_RATE_MS);
- }
-}
+I2C_HandleTypeDef hi2c2;
+UART_HandleTypeDef huart2;
int main() {
HAL_Init();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) {
- .Pin = GPIO_PIN_5,
- .Mode = GPIO_MODE_OUTPUT_PP,
- .Pull = GPIO_NOPULL
- });
+ SystemClock_Config();
+ MX_GPIO_Init();
+ MX_USART2_UART_Init();
+ MX_I2C2_Init();
- xTaskCreate(task_1, "task1", 128, NULL, 1, NULL);
+ xTaskCreate(ws_sensor_read_task, "sensor", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
diff --git a/stm32f091/main.h b/stm32f091/main.h
new file mode 100644
index 0000000..7c13733
--- /dev/null
+++ b/stm32f091/main.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <stm32f0xx_hal_i2c.h>
+#include <stm32f0xx_hal_uart.h>
+
+#define hi2c1 hi2c2
+
+extern I2C_HandleTypeDef hi2c2;
+extern UART_HandleTypeDef huart2;
+
+void task_1();
+int main();
diff --git a/stm32f091/makefile b/stm32f091/makefile
index 5a185de..ffcb109 100644
--- a/stm32f091/makefile
+++ b/stm32f091/makefile
@@ -19,6 +19,7 @@ SHARED_FLAGS += -I./lib/STM32-base-STM32Cube/CMSIS/STM32F0xx/inc
SHARED_FLAGS += -I./lib/STM32-base/startup
SHARED_FLAGS += -I./lib/FreeRTOS-Kernel/include
SHARED_FLAGS += -I./lib/FreeRTOS-Kernel/portable/GCC/ARM_CM0/
+SHARED_FLAGS += -I./lib/Si7021_driver_STM32/driver/inc/
SHARED_FLAGS += -I.
SHARED_FLAGS += -O1
SHARED_FLAGS += -ffunction-sections
@@ -65,6 +66,7 @@ OBJS += lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_rcc.o \
lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_tim_ex.o \
lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_uart.o \
lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_uart_ex.o
+OBJS += lib/Si7021_driver_STM32/driver/src/Si7021_driver.o
.PHONY: flash clean
diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c
new file mode 100644
index 0000000..f2e9e29
--- /dev/null
+++ b/stm32f091/sensor.c
@@ -0,0 +1,37 @@
+#include <FreeRTOS.h>
+#include <task.h>
+#include <Si7021_driver.h>
+
+#include "sensor.h"
+#include "backlog.h"
+
+uint8_t ws_sensor_temperature() {
+ float temp = 0.f;
+ r_single_Si7021(&temp, Temperature);
+ return (uint8_t) temp; //TODO: convert with range -> util.h
+}
+
+uint8_t ws_sensor_humidity() {
+ return 0x00;
+}
+
+uint8_t ws_sensor_atmospheric_pressure() {
+ return 0x00;
+}
+
+void ws_sensor_read() {
+ ws_s_backlog_record record = {
+ .sens_temperature = ws_sensor_temperature(),
+ .sens_atm_pressure = ws_sensor_atmospheric_pressure(),
+ .sens_humidity = ws_sensor_humidity()
+ };
+ ws_backlog_add_record(record);
+}
+
+void ws_sensor_read_task() {
+ while (1) {
+ ws_sensor_read();
+ vTaskDelay(portTICK_PERIOD_MS * 1000 * 60);
+ }
+}
+
diff --git a/stm32f091/sensor.h b/stm32f091/sensor.h
new file mode 100644
index 0000000..802656b
--- /dev/null
+++ b/stm32f091/sensor.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <stdint.h>
+
+/** Get current temperature measurement */
+uint8_t ws_sensor_temperature();
+/** Get current humidity measurement */
+uint8_t ws_sensor_humidity();
+/** Get current atmospheric pressure measurement */
+uint8_t ws_sensor_atmospheric_pressure();
+
+/**
+ * Put measurements from every sensor into a `ws_s_backlog_record`, and commit
+ * that struct to the database
+ */
+void ws_sensor_read();
+
+/** FreeRTOS task that runs `ws_sensor_read` every minute */
+void ws_sensor_read_task();
+
diff --git a/stm32f091/setup.c b/stm32f091/setup.c
new file mode 100644
index 0000000..42f54b3
--- /dev/null
+++ b/stm32f091/setup.c
@@ -0,0 +1,106 @@
+#include <stm32f0xx_hal.h>
+#include <stm32f0xx_hal_rcc.h>
+#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() {
+ 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.
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+ RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+ RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+ 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();
+
+ // Initializes the CPU, AHB and APB buses clocks
+ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
+ |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;
+ PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) return Error_Handler();
+}
+
+void MX_I2C2_Init() {
+ hi2c2.Instance = I2C2;
+ hi2c2.Init.Timing = 0x20303E5D;
+ hi2c2.Init.OwnAddress1 = 0;
+ hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
+ hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
+ hi2c2.Init.OwnAddress2 = 0;
+ hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
+ hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
+ hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
+ if (HAL_I2C_Init(&hi2c2) != HAL_OK) return Error_Handler();
+ if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) return Error_Handler();
+ if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) return Error_Handler();
+}
+
+void MX_USART2_UART_Init() {
+ huart2.Instance = USART2;
+ huart2.Init.BaudRate = 115200;
+ huart2.Init.WordLength = UART_WORDLENGTH_8B;
+ huart2.Init.StopBits = UART_STOPBITS_1;
+ huart2.Init.Parity = UART_PARITY_NONE;
+ huart2.Init.Mode = UART_MODE_TX_RX;
+ huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ 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();
+}
+
+void MX_GPIO_Init() {
+ 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;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
+
+ GPIO_InitStruct.Pin = LD2_Pin;
+ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
+ HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
+}
+
+void Error_Handler() {
+ __disable_irq();
+ for(;;);
+}
+
diff --git a/stm32f091/setup.h b/stm32f091/setup.h
new file mode 100644
index 0000000..36eb9af
--- /dev/null
+++ b/stm32f091/setup.h
@@ -0,0 +1,7 @@
+#pragma once
+
+void SystemClock_Config();
+void MX_I2C2_Init();
+void MX_USART2_UART_Init();
+void MX_GPIO_Init();
+void Error_Handler();