aboutsummaryrefslogtreecommitdiff
path: root/stm32f091
diff options
context:
space:
mode:
Diffstat (limited to 'stm32f091')
-rw-r--r--stm32f091/FreeRTOSConfig.h4
-rw-r--r--stm32f091/main.c25
-rw-r--r--stm32f091/main.h3
-rw-r--r--stm32f091/sensor.c50
-rw-r--r--stm32f091/sensor.h20
-rw-r--r--stm32f091/setup.c136
-rw-r--r--stm32f091/setup.h26
-rw-r--r--stm32f091/stm32f0xx_hal_msp.c32
-rw-r--r--stm32f091/stm32f0xx_hal_msp.h9
9 files changed, 246 insertions, 59 deletions
diff --git a/stm32f091/FreeRTOSConfig.h b/stm32f091/FreeRTOSConfig.h
index b1a692a..223cf8b 100644
--- a/stm32f091/FreeRTOSConfig.h
+++ b/stm32f091/FreeRTOSConfig.h
@@ -50,7 +50,7 @@
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
- // void xPortSysTickHandler(void);
+ void xPortSysTickHandler(void);
#endif
#define configUSE_PREEMPTION 1
#define configSUPPORT_STATIC_ALLOCATION 1
@@ -119,7 +119,7 @@ standard names. */
/* IMPORTANT: This define is commented when used with STM32Cube firmware, when the timebase source is SysTick,
to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
-#define xPortSysTickHandler SysTick_Handler
+/* #define xPortSysTickHandler SysTick_Handler */
/* USER CODE BEGIN Defines */
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
diff --git a/stm32f091/main.c b/stm32f091/main.c
index 6d1fb38..9235f1b 100644
--- a/stm32f091/main.c
+++ b/stm32f091/main.c
@@ -1,30 +1,23 @@
#include <FreeRTOS.h>
#include <task.h>
-#include <stm32f0xx.h>
-#include <stdint.h>
#include <stm32f0xx_hal.h>
-void task_1() {
- uint8_t led = 1;
-
- while (1) {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, led);
- led ^= 1;
-
- vTaskDelay(1000 / portTICK_RATE_MS);
- }
-}
+#include "main.h"
+#include "setup.h"
+#include "sensor.h"
+#include "backlog.h"
int main() {
- HAL_Init();
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
+ ws_io_setup();
HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) {
.Pin = GPIO_PIN_5,
.Mode = GPIO_MODE_OUTPUT_PP,
.Pull = GPIO_NOPULL
});
- xTaskCreate(task_1, "task1", 128, NULL, 1, NULL);
+ ws_backlog_alloc(24 * 60);
+ ws_sensor_read();
+
+ 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..9595166
--- /dev/null
+++ b/stm32f091/main.h
@@ -0,0 +1,3 @@
+#pragma once
+
+int main();
diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c
new file mode 100644
index 0000000..77ade64
--- /dev/null
+++ b/stm32f091/sensor.c
@@ -0,0 +1,50 @@
+#include <FreeRTOS.h>
+#include <task.h>
+#include <stm32f0xx_hal.h>
+
+#include "setup.h"
+#include "sensor.h"
+#include "backlog.h"
+
+#define SI7021_ADDRESS ((uint16_t)(0x40) << 1)
+#define REG_TEMP ((uint8_t)(0xE3))
+#define REG_HUM ((uint8_t)(0xF5))
+
+uint8_t ws_sensor_temperature() {
+ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
+ uint8_t buf[12];
+ int16_t val;
+ float temp_c;
+ buf[0] = REG_TEMP;
+ HAL_I2C_Master_Transmit(&hi2c1, SI7021_ADDRESS, buf, 1, HAL_MAX_DELAY);
+ HAL_I2C_Master_Receive(&hi2c1, SI7021_ADDRESS, buf, 2, HAL_MAX_DELAY);
+ val = ((int16_t)buf[0]<< 8 ) | (buf[1]);
+ temp_c= ((175.72*val)/65536) - 46.85;
+
+ return (uint8_t) temp_c; //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 * 1);
+ }
+}
+
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..ed4ee0b
--- /dev/null
+++ b/stm32f091/setup.c
@@ -0,0 +1,136 @@
+#include <stm32f0xx_hal.h>
+#include <stm32f0xx_hal_rcc.h>
+#include <stm32f0xx_hal_i2c.h>
+#include <stm32f0xx_hal_uart.h>
+#include <FreeRTOS.h>
+#include <task.h>
+
+#include "setup.h"
+
+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_setup_error_handler();
+
+void ws_io_setup() {
+ HAL_Init();
+
+ ws_io_clock_setup();
+ ws_io_i2c_setup();
+ ws_io_usart2_setup();
+}
+
+static void ws_io_clock_setup() {
+ 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 ws_setup_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 ws_setup_error_handler();
+ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
+ PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
+ PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) return ws_setup_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();
+ if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) return ws_setup_error_handler();
+ if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) return ws_setup_error_handler();
+}
+
+static void ws_io_usart2_setup() {
+ 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 ws_setup_error_handler();
+}
+
+void HAL_MspInit() {
+ __HAL_RCC_SYSCFG_CLK_ENABLE();
+ __HAL_RCC_PWR_CLK_ENABLE();
+ HAL_NVIC_SetPriority(PendSV_IRQn, 3, 0);
+}
+
+void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) {
+ if(hi2c->Instance != I2C1) return;
+
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
+ __HAL_RCC_GPIOB_CLK_ENABLE();
+ 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;
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
+ GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
+ HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+ __HAL_RCC_I2C1_CLK_ENABLE();
+}
+
+void HAL_UART_MspInit(UART_HandleTypeDef *huart) {
+ if(huart->Instance != USART2) return;
+
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
+ __HAL_RCC_USART2_CLK_ENABLE();
+ __HAL_RCC_GPIOA_CLK_ENABLE();
+ 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;
+ GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+}
+
+void SysTick_Handler() {
+ HAL_IncTick();
+ if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) xPortSysTickHandler();
+}
+
+static void ws_setup_error_handler() {
+ __disable_irq();
+ for(;;);
+}
+
+void NMI_Handler() {
+ for(;;);
+}
+
+void HardFault_Handler() {
+ for(;;);
+}
diff --git a/stm32f091/setup.h b/stm32f091/setup.h
new file mode 100644
index 0000000..713ed58
--- /dev/null
+++ b/stm32f091/setup.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <stm32f0xx_hal.h>
+#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();
+
+void NMI_Handler();
+void HardFault_Handler();
+void SysTick_Handler();
+void HAL_MspInit();
+void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c);
+void HAL_UART_MspInit(UART_HandleTypeDef *huart);
diff --git a/stm32f091/stm32f0xx_hal_msp.c b/stm32f091/stm32f0xx_hal_msp.c
deleted file mode 100644
index 8fd7330..0000000
--- a/stm32f091/stm32f0xx_hal_msp.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include "stm32f0xx_hal_msp.h"
-
-void HAL_MspInit(void) {
- return;
- __HAL_RCC_SYSCFG_CLK_ENABLE();
- __HAL_RCC_PWR_CLK_ENABLE();
- HAL_NVIC_SetPriority(PendSV_IRQn, 3, 0);
-}
-
-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);
- }
-}
-
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);
-