commit 45ade5c29e8e4c98e7b42b24204ecdab6c6c5070
parent 02a4892190aafe00c15880d98a2ad42b4be00dae
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Sun, 15 Mar 2026 19:03:34 -0300
move `DS18B20_*` sensor methods `task_sensor`
Diffstat:
7 files changed, 656 insertions(+), 573 deletions(-)
diff --git a/Core/Inc/main.h b/Core/Inc/main.h
@@ -1,84 +1,85 @@
-/* USER CODE BEGIN Header */
-/**
- ******************************************************************************
- * @file : main.h
- * @brief : Header for main.c file.
- * This file contains the common defines of the application.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
-/* USER CODE END Header */
-
-/* Define to prevent recursive inclusion -------------------------------------*/
-#ifndef __MAIN_H
-#define __MAIN_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Includes ------------------------------------------------------------------*/
-#include "stm32f1xx_hal.h"
-
-/* Private includes ----------------------------------------------------------*/
-/* USER CODE BEGIN Includes */
-
-/* USER CODE END Includes */
-
-/* Exported types ------------------------------------------------------------*/
-/* USER CODE BEGIN ET */
-
-/* USER CODE END ET */
-
-/* Exported constants --------------------------------------------------------*/
-/* USER CODE BEGIN EC */
-
-/* USER CODE END EC */
-
-/* Exported macro ------------------------------------------------------------*/
-/* USER CODE BEGIN EM */
-
-/* USER CODE END EM */
-
-/* Exported functions prototypes ---------------------------------------------*/
-void Error_Handler(void);
-
-/* USER CODE BEGIN EFP */
-
-/* USER CODE END EFP */
-
-/* Private defines -----------------------------------------------------------*/
-#define B1_Pin GPIO_PIN_13
-#define B1_GPIO_Port GPIOC
-#define B1_EXTI_IRQn EXTI15_10_IRQn
-#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
-#define SWO_Pin GPIO_PIN_3
-#define SWO_GPIO_Port GPIOB
-
-/* USER CODE BEGIN Private defines */
-
-/* USER CODE END Private defines */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __MAIN_H */
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * @file : main.h
+ * @brief : Header for main.c file.
+ * This file contains the common defines of the application.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2025 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __MAIN_H
+#define __MAIN_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f1xx_hal.h"
+
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+extern UART_HandleTypeDef huart1;
+
+/* USER CODE END Includes */
+
+/* Exported types ------------------------------------------------------------*/
+/* USER CODE BEGIN ET */
+
+/* USER CODE END ET */
+
+/* Exported constants --------------------------------------------------------*/
+/* USER CODE BEGIN EC */
+
+/* USER CODE END EC */
+
+/* Exported macro ------------------------------------------------------------*/
+/* USER CODE BEGIN EM */
+
+/* USER CODE END EM */
+
+/* Exported functions prototypes ---------------------------------------------*/
+void Error_Handler(void);
+
+/* USER CODE BEGIN EFP */
+
+/* USER CODE END EFP */
+
+/* Private defines -----------------------------------------------------------*/
+#define B1_Pin GPIO_PIN_13
+#define B1_GPIO_Port GPIOC
+#define B1_EXTI_IRQn EXTI15_10_IRQn
+#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
+#define SWO_Pin GPIO_PIN_3
+#define SWO_GPIO_Port GPIOB
+
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MAIN_H */
diff --git a/Core/Src/main.c b/Core/Src/main.c
@@ -1,457 +1,338 @@
-/* USER CODE BEGIN Header */
-/**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
-/* USER CODE END Header */
-/* Includes ------------------------------------------------------------------*/
-#include "main.h"
-
-/* Private includes ----------------------------------------------------------*/
-/* USER CODE BEGIN Includes */
-
-/* Application includes. */
-#include "logger.h"
-#include "app.h"
-
-/* USER CODE END Includes */
-
-/* Private typedef -----------------------------------------------------------*/
-/* USER CODE BEGIN PTD */
-
-/* USER CODE END PTD */
-
-/* Private define ------------------------------------------------------------*/
-/* USER CODE BEGIN PD */
-
-/* USER CODE END PD */
-
-/* Private macro -------------------------------------------------------------*/
-/* USER CODE BEGIN PM */
-
-/* USER CODE END PM */
-
-/* Private variables ---------------------------------------------------------*/
-UART_HandleTypeDef huart1;
-UART_HandleTypeDef huart2;
-DMA_HandleTypeDef hdma_usart1_rx;
-DMA_HandleTypeDef hdma_usart1_tx;
-
-/* USER CODE BEGIN PV */
-
-/* USER CODE END PV */
-
-/* Private function prototypes -----------------------------------------------*/
-void SystemClock_Config(void);
-static void MX_GPIO_Init(void);
-static void MX_DMA_Init(void);
-static void MX_USART2_UART_Init(void);
-static void MX_USART1_UART_Init(void);
-/* USER CODE BEGIN PFP */
-
-/* USER CODE END PFP */
-
-/* Private user code ---------------------------------------------------------*/
-/* USER CODE BEGIN 0 */
-#if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
-extern void initialise_monitor_handles(void);
-#endif
-
-uint8_t b_DS18B20_Received_data;
-uint8_t g_DS18B20_Received_data_buffer[8];
-float g_DS18B20_temp;
-
-uint8_t Temp_LSB, Temp_MSB;
-uint16_t temp;
-
-void UART1_SetBaud(uint32_t baud)
-{
- huart1.Instance = USART1;
- huart1.Init.BaudRate = baud;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- };
-}
-
-uint8_t DS18B20_Start(void)
-{
- uint8_t data = 0xF0;
- UART1_SetBaud(9600);
- HAL_UART_Transmit(&huart1, &data, 1, 100);
-
- if ((HAL_UART_Receive(&huart1, &data, 1, 1000) != HAL_OK) || (data == 0xF0))
- {
- // initialization failed
- LOGGER_ERROR("Couldn't read temperature sensor");
- return 1;
- }
-
- UART1_SetBaud(115200);
- return 0;
-}
-
-void DS18B20_Init(void)
-{
- if (DS18B20_Start() == 0)
- {
- LOGGER_INFO("Temperature sensor initialized successfully");
- }
-}
-
-void DS18B20_Write(uint8_t data)
-{
- uint8_t buffer[8];
-
- for (uint8_t i = 0; i < 8; ++i)
- {
- if (data & (1 << i))
- {
- buffer[i] = 0xFF;
- }
- else
- {
- buffer[i] = 0x00;
- }
- }
-
- HAL_UART_Transmit(&huart1, buffer, 8, 100);
-}
-
-uint8_t DS18B20_Read(void)
-{
- uint8_t buffer[8];
- uint8_t received_value = 0;
-
- for (uint8_t i = 0; i < 8; ++i)
- {
- buffer[i] = 0xFF;
- }
-
- HAL_UART_Transmit_DMA(&huart1, buffer, 8);
- HAL_UART_Receive_DMA(&huart1, g_DS18B20_Received_data_buffer, 8);
-
- while (b_DS18B20_Received_data == false)
- ;
-
- for (uint8_t i = 0; i < 8; ++i)
- {
- if (g_DS18B20_Received_data_buffer[i] == 0xFF)
- {
- received_value |= (1 << i);
- }
- }
-
- b_DS18B20_Received_data = false;
- return received_value;
-}
-
-void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
-{
- b_DS18B20_Received_data = true;
-}
-
-void DS18B20_Read_temp(void)
-{
- DS18B20_Start();
- DS18B20_Write(0xCC); // send 'Skip ROM' command
- DS18B20_Write(0x44); // send 'Convert T' command
-
- DS18B20_Start();
- DS18B20_Write(0xCC); // send 'Skip ROM' command
- DS18B20_Write(0xBE); // send 'read scratchpad' command
-
- Temp_LSB = DS18B20_Read();
- Temp_MSB = DS18B20_Read();
-
- temp = (Temp_MSB << 8) | Temp_LSB;
- g_DS18B20_temp = (float) temp / 16.0;
-}
-
-/* USER CODE END 0 */
-
-/**
- * @brief The application entry point.
- * @retval int
- */
-int main(void)
-{
-
- /* USER CODE BEGIN 1 */
-#if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
- initialise_monitor_handles();
-#endif
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_USART2_UART_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- DS18B20_Init();
-
- /* Application Init */
- // app_init();
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
-
- LOGGER_INFO("Running main loop...");
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
-
- /* Application Update */
- // app_update();
- // send reset signal to sensor, a low pulse for no less than 480 us
- // uint8_t data = 0xF0;
- // HAL_UART_Transmit(&huart1, &data, 1, 100);
- DS18B20_Read_temp();
- LOGGER_INFO("%d°C", (int)g_DS18B20_temp);
- HAL_Delay(2000);
- }
- /* USER CODE END 3 */
-}
-
-/**
- * @brief System Clock Configuration
- * @retval None
- */
-void SystemClock_Config(void)
-{
- RCC_OscInitTypeDef RCC_OscInitStruct =
- { 0 };
- RCC_ClkInitTypeDef RCC_ClkInitStruct =
- { 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_DIV2;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
- | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
-}
-
-/**
- * @brief USART1 Initialization Function
- * @param None
- * @retval None
- */
-static void MX_USART1_UART_Init(void)
-{
-
- /* USER CODE BEGIN USART1_Init 0 */
-
- /* USER CODE END USART1_Init 0 */
-
- /* USER CODE BEGIN USART1_Init 1 */
-
- /* USER CODE END USART1_Init 1 */
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART1_Init 2 */
-
- /* USER CODE END USART1_Init 2 */
-
-}
-
-/**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
-static void MX_USART2_UART_Init(void)
-{
-
- /* USER CODE BEGIN USART2_Init 0 */
-
- /* USER CODE END USART2_Init 0 */
-
- /* USER CODE BEGIN USART2_Init 1 */
-
- /* USER CODE END USART2_Init 1 */
- 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;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
-
- /* USER CODE END USART2_Init 2 */
-
-}
-
-/**
- * Enable DMA controller clock
- */
-static void MX_DMA_Init(void)
-{
-
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
-
- /* DMA interrupt init */
- /* DMA1_Channel4_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
- /* DMA1_Channel5_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
-
-}
-
-/**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
-static void MX_GPIO_Init(void)
-{
- GPIO_InitTypeDef GPIO_InitStruct =
- { 0 };
- /* USER CODE BEGIN MX_GPIO_Init_1 */
- /* USER CODE END MX_GPIO_Init_1 */
-
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pin : LD2_Pin */
- 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);
-
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
-
- /* USER CODE BEGIN MX_GPIO_Init_2 */
- /* USER CODE END MX_GPIO_Init_2 */
-}
-
-/* USER CODE BEGIN 4 */
-
-/* USER CODE END 4 */
-
-/**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
-void Error_Handler(void)
-{
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- ;
- }
- /* USER CODE END Error_Handler_Debug */
-}
-#ifdef USE_FULL_ASSERT
-/**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
-void assert_failed(uint8_t *file, uint32_t line)
-{
- /* USER CODE BEGIN 6 */
- /*
- * User can add his own implementation to report the file name and line
- * number, i.e. `printf("Error: file %s on line %d\r\n", file, line)
- */
- /* USER CODE END 6 */
-}
-#endif /* USE_FULL_ASSERT */
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * @file : main.c
+ * @brief : Main program body
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2025 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* Application includes. */
+#include "logger.h"
+#include "app.h"
+
+/* USER CODE END Includes */
+
+/* Private typedef -----------------------------------------------------------*/
+/* USER CODE BEGIN PTD */
+
+/* USER CODE END PTD */
+
+/* Private define ------------------------------------------------------------*/
+/* USER CODE BEGIN PD */
+
+/* USER CODE END PD */
+
+/* Private macro -------------------------------------------------------------*/
+/* USER CODE BEGIN PM */
+
+/* USER CODE END PM */
+
+/* Private variables ---------------------------------------------------------*/
+UART_HandleTypeDef huart1;
+UART_HandleTypeDef huart2;
+DMA_HandleTypeDef hdma_usart1_rx;
+DMA_HandleTypeDef hdma_usart1_tx;
+
+/* USER CODE BEGIN PV */
+
+/* USER CODE END PV */
+
+/* Private function prototypes -----------------------------------------------*/
+void SystemClock_Config(void);
+static void MX_GPIO_Init(void);
+static void MX_DMA_Init(void);
+static void MX_USART2_UART_Init(void);
+static void MX_USART1_UART_Init(void);
+/* USER CODE BEGIN PFP */
+
+/* USER CODE END PFP */
+
+/* Private user code ---------------------------------------------------------*/
+/* USER CODE BEGIN 0 */
+#if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
+extern void initialise_monitor_handles(void);
+#endif
+
+/* USER CODE END 0 */
+
+/**
+ * @brief The application entry point.
+ * @retval int
+ */
+int main(void)
+{
+
+ /* USER CODE BEGIN 1 */
+#if (1 == LOGGER_CONFIG_USE_SEMIHOSTING)
+ initialise_monitor_handles();
+#endif
+ /* USER CODE END 1 */
+
+ /* MCU Configuration--------------------------------------------------------*/
+
+ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
+ HAL_Init();
+
+ /* USER CODE BEGIN Init */
+
+ /* USER CODE END Init */
+
+ /* Configure the system clock */
+ SystemClock_Config();
+
+ /* USER CODE BEGIN SysInit */
+
+ /* USER CODE END SysInit */
+
+ /* Initialize all configured peripherals */
+ MX_GPIO_Init();
+ MX_DMA_Init();
+ MX_USART2_UART_Init();
+ MX_USART1_UART_Init();
+ /* USER CODE BEGIN 2 */
+
+ /* Application Init */
+ app_init();
+ /* USER CODE END 2 */
+
+ /* Infinite loop */
+ /* USER CODE BEGIN WHILE */
+
+ LOGGER_INFO("Running main loop...");
+ while (1)
+ {
+ /* USER CODE END WHILE */
+
+ /* USER CODE BEGIN 3 */
+
+ /* Application Update */
+ app_update();
+ // send reset signal to sensor, a low pulse for no less than 480 us
+ // uint8_t data = 0xF0;
+ // HAL_UART_Transmit(&huart1, &data, 1, 100);
+ // DS18B20_Read_temp();
+ // LOGGER_INFO("%d°C", (int)g_DS18B20_temp);
+ }
+ /* USER CODE END 3 */
+}
+
+/**
+ * @brief System Clock Configuration
+ * @retval None
+ */
+void SystemClock_Config(void)
+{
+ RCC_OscInitTypeDef RCC_OscInitStruct =
+ { 0 };
+ RCC_ClkInitTypeDef RCC_ClkInitStruct =
+ { 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_DIV2;
+ RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
+ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
+ {
+ Error_Handler();
+ }
+
+ /** Initializes the CPU, AHB and APB buses clocks
+ */
+ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
+ | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
+
+ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
+ {
+ Error_Handler();
+ }
+}
+
+/**
+ * @brief USART1 Initialization Function
+ * @param None
+ * @retval None
+ */
+static void MX_USART1_UART_Init(void)
+{
+
+ /* USER CODE BEGIN USART1_Init 0 */
+
+ /* USER CODE END USART1_Init 0 */
+
+ /* USER CODE BEGIN USART1_Init 1 */
+
+ /* USER CODE END USART1_Init 1 */
+ huart1.Instance = USART1;
+ huart1.Init.BaudRate = 115200;
+ huart1.Init.WordLength = UART_WORDLENGTH_8B;
+ huart1.Init.StopBits = UART_STOPBITS_1;
+ huart1.Init.Parity = UART_PARITY_NONE;
+ huart1.Init.Mode = UART_MODE_TX_RX;
+ huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ huart1.Init.OverSampling = UART_OVERSAMPLING_16;
+ if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
+ {
+ Error_Handler();
+ }
+ /* USER CODE BEGIN USART1_Init 2 */
+
+ /* USER CODE END USART1_Init 2 */
+
+}
+
+/**
+ * @brief USART2 Initialization Function
+ * @param None
+ * @retval None
+ */
+static void MX_USART2_UART_Init(void)
+{
+
+ /* USER CODE BEGIN USART2_Init 0 */
+
+ /* USER CODE END USART2_Init 0 */
+
+ /* USER CODE BEGIN USART2_Init 1 */
+
+ /* USER CODE END USART2_Init 1 */
+ 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;
+ if (HAL_UART_Init(&huart2) != HAL_OK)
+ {
+ Error_Handler();
+ }
+ /* USER CODE BEGIN USART2_Init 2 */
+
+ /* USER CODE END USART2_Init 2 */
+
+}
+
+/**
+ * Enable DMA controller clock
+ */
+static void MX_DMA_Init(void)
+{
+
+ /* DMA controller clock enable */
+ __HAL_RCC_DMA1_CLK_ENABLE();
+
+ /* DMA interrupt init */
+ /* DMA1_Channel4_IRQn interrupt configuration */
+ HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
+ HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
+ /* DMA1_Channel5_IRQn interrupt configuration */
+ HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
+ HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
+
+}
+
+/**
+ * @brief GPIO Initialization Function
+ * @param None
+ * @retval None
+ */
+static void MX_GPIO_Init(void)
+{
+ GPIO_InitTypeDef GPIO_InitStruct =
+ { 0 };
+ /* USER CODE BEGIN MX_GPIO_Init_1 */
+ /* USER CODE END MX_GPIO_Init_1 */
+
+ /* GPIO Ports Clock Enable */
+ __HAL_RCC_GPIOC_CLK_ENABLE();
+ __HAL_RCC_GPIOD_CLK_ENABLE();
+ __HAL_RCC_GPIOA_CLK_ENABLE();
+ __HAL_RCC_GPIOB_CLK_ENABLE();
+
+ /*Configure GPIO pin Output Level */
+ HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
+
+ /*Configure GPIO pin : B1_Pin */
+ GPIO_InitStruct.Pin = B1_Pin;
+ GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
+
+ /*Configure GPIO pin : LD2_Pin */
+ 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);
+
+ /* EXTI interrupt init*/
+ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
+ HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
+
+ /* USER CODE BEGIN MX_GPIO_Init_2 */
+ /* USER CODE END MX_GPIO_Init_2 */
+}
+
+/* USER CODE BEGIN 4 */
+
+/* USER CODE END 4 */
+
+/**
+ * @brief This function is executed in case of error occurrence.
+ * @retval None
+ */
+void Error_Handler(void)
+{
+ /* USER CODE BEGIN Error_Handler_Debug */
+ /* User can add his own implementation to report the HAL error return state */
+ __disable_irq();
+ while (1)
+ {
+ ;
+ }
+ /* USER CODE END Error_Handler_Debug */
+}
+#ifdef USE_FULL_ASSERT
+/**
+ * @brief Reports the name of the source file and the source line number
+ * where the assert_param error has occurred.
+ * @param file: pointer to the source file name
+ * @param line: assert_param error line source number
+ * @retval None
+ */
+void assert_failed(uint8_t *file, uint32_t line)
+{
+ /* USER CODE BEGIN 6 */
+ /*
+ * User can add his own implementation to report the file name and line
+ * number, i.e. `printf("Error: file %s on line %d\r\n", file, line)
+ */
+ /* USER CODE END 6 */
+}
+#endif /* USE_FULL_ASSERT */
diff --git a/app/inc/ds18b20.h b/app/inc/ds18b20.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2026 Martin Javier Klöckner <mklockner@fi.uba.ar>
+ *
+ * See file `LICENSE` for full details
+ */
+
+#ifndef DS18B20_H_
+#define DS18B20_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void UART1_SetBaud(uint32_t baud);
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
+
+void DS18B20_Init(void);
+void DS18B20_Write(uint8_t data);
+void DS18B20_Read_temp(void);
+uint8_t DS18B20_Read(void);
+uint8_t DS18B20_Start(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* DS18B20_H_ */
diff --git a/app/inc/logger.h b/app/inc/logger.h
@@ -19,7 +19,7 @@ extern "C" {
#define LOGGER_CONFIG_ENABLE (1)
#define LOGGER_CONFIG_MAXLEN (64)
-#define LOGGER_CONFIG_USE_SEMIHOSTING (0)
+#define LOGGER_CONFIG_USE_SEMIHOSTING (1)
#if 1 == LOGGER_CONFIG_ENABLE
#define LOGGER_LOG(...)\
diff --git a/app/inc/task_sensor_attribute.h b/app/inc/task_sensor_attribute.h
@@ -12,37 +12,45 @@
extern "C" {
#endif
-/* Sensor Statechart - State Transition Table */
-/* +-------------------+----------------+-------------+-------------------+-----------------------+
- * | Current | Event | | Next | |
- * | State | (Parameters) | [Guard] | State | Actions |
- * |===================+================+=============+===================+=======================|
- * | | | | | |
- * +-------------------+----------------+-------------+-------------------+-----------------------+
+/* Sensor Statechart - State Transition Table
+ *
+ * +---------------+----------------------+-------------+--------------+--------------------------+
+ * | Current state | Event | [Guard] | Next state | Actions |
+ * +===============+======================+=============+==============+==========================+
+ * | | | [tick == 0] | ST_SEN_BUSY | DS18B20_Read_temp() |
+ * + ST_SEN_READY +----------------------+-------------+--------------+--------------------------+
+ * | | | | ST_SEN_READY | tick-- |
+ * +---------------+----------------------+-------------+--------------+--------------------------+
+ * | ST_SEN_BUSY | EV_DS18B20_CONV_DONE | | ST_SEN_READY | tick = TEMP_READ_PERIOD |
+ * +---------------+----------------------+-------------+--------------+--------------------------+
+ *
*/
/* Events to excite Task Sensor */
typedef enum {
- EV_BLANK
+ EV_DS18B20_CONV_DONE
} task_sensor_ev_t;
/* States of Task Sensor */
typedef enum {
- ST_BLANK
+ ST_SEN_READY,
+ ST_SEN_BUSY
} task_sensor_st_t;
/* Identifier of Task Sensor */
typedef enum {
- ID_BLANK
+ ID_DS18B20
} task_sensor_id_t;
typedef struct {
- void *empty;
+ task_sensor_id_t identifier;
+ UART_HandleTypeDef huart;
} task_sensor_cfg_t;
typedef struct {
- task_sensor_id_t identifier;
- task_sensor_ev_t sig;
+ uint32_t tick;
+ task_sensor_st_t state;
+ task_sensor_ev_t event;
} task_sensor_dta_t;
extern task_sensor_dta_t task_sensor_dta_list[];
diff --git a/app/src/ds18b20.c b/app/src/ds18b20.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2026 Martin Javier Klöckner <mklockner@fi.uba.ar>
+ *
+ * See file `LICENSE` for full details
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "main.h"
+
+#include "ds18b20.h"
+#include "logger.h"
+
+uint8_t b_DS18B20_Received_data;
+uint8_t g_DS18B20_Received_data_buffer[8];
+float g_DS18B20_temp;
+
+uint8_t Temp_LSB, Temp_MSB;
+uint16_t temp;
+// extern UART_HandleTypeDef huart1;
+
+void UART1_SetBaud(uint32_t baud)
+{
+ huart1.Instance = USART1;
+ huart1.Init.BaudRate = baud;
+ huart1.Init.WordLength = UART_WORDLENGTH_8B;
+ huart1.Init.StopBits = UART_STOPBITS_1;
+ huart1.Init.Parity = UART_PARITY_NONE;
+ huart1.Init.Mode = UART_MODE_TX_RX;
+ huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ huart1.Init.OverSampling = UART_OVERSAMPLING_16;
+ if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
+ {
+ Error_Handler();
+ };
+}
+
+uint8_t DS18B20_Start(void)
+{
+ uint8_t data = 0xF0;
+ UART1_SetBaud(9600);
+ HAL_UART_Transmit(&huart1, &data, 1, 100);
+
+ if ((HAL_UART_Receive(&huart1, &data, 1, 1000) != HAL_OK) || (data == 0xF0))
+ {
+ // initialization failed
+ LOGGER_ERROR("Couldn't read temperature sensor");
+ return 1;
+ }
+
+ UART1_SetBaud(115200);
+ return 0;
+}
+
+void DS18B20_Init(void)
+{
+ if (DS18B20_Start() == 0)
+ {
+ LOGGER_INFO("Temperature sensor initialized successfully");
+ }
+}
+
+void DS18B20_Write(uint8_t data)
+{
+ uint8_t buffer[8];
+
+ for (uint8_t i = 0; i < 8; ++i)
+ {
+ if (data & (1 << i))
+ {
+ buffer[i] = 0xFF;
+ }
+ else
+ {
+ buffer[i] = 0x00;
+ }
+ }
+
+ HAL_UART_Transmit(&huart1, buffer, 8, 100);
+}
+
+uint8_t DS18B20_Read(void)
+{
+ uint8_t buffer[8];
+ uint8_t received_value = 0;
+
+ for (uint8_t i = 0; i < 8; ++i)
+ {
+ buffer[i] = 0xFF;
+ }
+
+ HAL_UART_Transmit_DMA(&huart1, buffer, 8);
+ HAL_UART_Receive_DMA(&huart1, g_DS18B20_Received_data_buffer, 8);
+
+ while (b_DS18B20_Received_data == false)
+ ;
+
+ for (uint8_t i = 0; i < 8; ++i)
+ {
+ if (g_DS18B20_Received_data_buffer[i] == 0xFF)
+ {
+ received_value |= (1 << i);
+ }
+ }
+
+ b_DS18B20_Received_data = false;
+ return received_value;
+}
+
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
+{
+ b_DS18B20_Received_data = true;
+}
+
+void DS18B20_Read_temp(void)
+{
+ DS18B20_Start();
+ DS18B20_Write(0xCC); // send 'Skip ROM' command
+ DS18B20_Write(0x44); // send 'Convert T' command
+
+ DS18B20_Start();
+ DS18B20_Write(0xCC); // send 'Skip ROM' command
+ DS18B20_Write(0xBE); // send 'read scratchpad' command
+
+ Temp_LSB = DS18B20_Read();
+ Temp_MSB = DS18B20_Read();
+
+ temp = (Temp_MSB << 8) | Temp_LSB;
+ g_DS18B20_temp = (float) temp / 16.0;
+}
diff --git a/app/src/task_sensor.c b/app/src/task_sensor.c
@@ -15,46 +15,52 @@
#include "task_sensor_attribute.h"
#include "task_system_attribute.h"
#include "task_system_interface.h"
+#include "ds18b20.h"
-#define G_TASK_SEN_CNT_INIT 0ul
-#define G_TASK_SEN_TICK_CNT_INI 0ul
-
-#define DEL_BTN_XX_MIN 0ul
-#define DEL_BTN_XX_MED 25ul
-#define DEL_BTN_XX_MAX 50ul
+#define G_TASK_SEN_TICK_CNT_INI 0ul
+#define G_TASK_SEN_TEMP_PERIOD_MS 2000ul
const task_sensor_cfg_t task_sensor_cfg_list[] = {
+ {ID_DS18B20, 0, EV_DS18B20_CONV_DONE}
};
#define SENSOR_CFG_QTY (sizeof(task_sensor_cfg_list)/sizeof(task_sensor_cfg_t))
-task_sensor_dta_t task_sensor_dta_list[] = {0};
+task_sensor_dta_t task_sensor_dta_list[] = {
+ {G_TASK_SEN_TICK_CNT_INI, ST_SEN_READY}
+};
#define SENSOR_DTA_QTY (sizeof(task_sensor_dta_list)/sizeof(task_sensor_dta_t))
void task_sensor_statechart(void);
-uint32_t g_task_sensor_cnt;
+uint32_t g_task_sensor_tick;
// this variable is incremented by 1 on every ms by HAL_SysTick
volatile uint32_t g_task_sensor_tick_cnt;
void task_sensor_init(void *parameters)
{
+ const task_sensor_cfg_t *p_task_sensor_cfg;
// task_sensor_dta_t *p_task_sensor_dta;
- // task_sensor_st_t state;
- // task_sensor_ev_t event;
LOGGER_INFO("Initializing `task_sensor`...");
- g_task_sensor_cnt = G_TASK_SEN_CNT_INIT;
+ g_task_sensor_tick = G_TASK_SEN_TEMP_PERIOD_MS;
for (uint32_t index = 0; SENSOR_DTA_QTY > index; index++)
{
- /* Init every associated sensor variables */
+ p_task_sensor_cfg = &task_sensor_cfg_list[index];
// p_task_sensor_dta = &task_sensor_dta_list[index];
- // ...
+ switch (p_task_sensor_cfg->identifier) {
+ case ID_DS18B20:
+ DS18B20_Init();
+ break;
+
+ default: break;
+ }
+
}
LOGGER_INFO("Done initializing `task_sensor`");
@@ -74,8 +80,6 @@ void task_sensor_update(void *parameters)
while (b_time_update_required)
{
- g_task_sensor_cnt++;
-
task_sensor_statechart();
__asm("CPSID i");
@@ -103,8 +107,37 @@ void task_sensor_statechart(void)
p_task_sensor_cfg = &task_sensor_cfg_list[index];
p_task_sensor_dta = &task_sensor_dta_list[index];
- // unused
- (void) p_task_sensor_cfg;
- (void) p_task_sensor_dta;
+ switch (p_task_sensor_cfg->identifier)
+ {
+ case ID_DS18B20:
+ switch (p_task_sensor_dta->state)
+ {
+ case ST_SEN_READY:
+ if (0 == g_task_sensor_tick)
+ {
+ DS18B20_Read_temp();
+ p_task_sensor_dta->event = EV_DS18B20_CONV_DONE;
+ p_task_sensor_dta->state = ST_SEN_BUSY;
+ }
+ else
+ {
+ g_task_sensor_tick--;
+ }
+ break;
+
+ case ST_SEN_BUSY:
+ if (EV_DS18B20_CONV_DONE == p_task_sensor_dta->event)
+ {
+ p_task_sensor_dta->state = ST_SEN_READY;
+ g_task_sensor_tick = G_TASK_SEN_TEMP_PERIOD_MS;
+ }
+ break;
+
+ default: break;
+ }
+ break;
+
+ default: break;
+ }
}
}