diff --git a/Src/peripheral/adc/adc.hpp b/Src/peripheral/adc/adc.hpp index 74d54d1..5dcb472 100644 --- a/Src/peripheral/adc/adc.hpp +++ b/Src/peripheral/adc/adc.hpp @@ -5,7 +5,6 @@ #define SRC_PERIPHERY_ADC_ADC_HPP_ #include -#include "peripheral/temperature_sensor/temperature_sensor.hpp" #ifdef __cplusplus extern "C" { #endif diff --git a/Src/peripheral/adc/circuit_periphery.cpp b/Src/peripheral/adc/circuit_periphery.cpp index 3b0f637..4822298 100644 --- a/Src/peripheral/adc/circuit_periphery.cpp +++ b/Src/peripheral/adc/circuit_periphery.cpp @@ -11,6 +11,9 @@ #include "params.hpp" #include "peripheral/led/led.hpp" #include "peripheral/iwdg/iwdg.hpp" +#ifdef STM32G0B1xx +#include "adc.h" +#endif static const std::array, (int)BoardType::BOARDS_AMOUNT> hw_info = {{ {59, 69}, @@ -32,6 +35,20 @@ static const std::array, (int)BoardType::BOARDS {"co.rl.mini.v3", 14}, }}; +uint16_t CircuitPeriphery::temperature() { + auto adc_12b = AdcPeriphery::get(AdcChannel::ADC_TEMPERATURE); + uint16_t temperature_kelvin; +#ifdef STM32G0B1xx + temperature_kelvin = __HAL_ADC_CALC_TEMPERATURE(3300, adc_12b, ADC_RESOLUTION_12B) + 273; +#else // STM32F103xB + static const uint16_t TEMP_REF = 25; + static const uint16_t ADC_REF = 1750; ///< v_ref / 3.3 * 4095 + static const uint16_t AVG_SLOPE = 5; ///< avg_slope/(3.3/4096) + temperature_kelvin = (ADC_REF - adc_12b) / AVG_SLOPE + TEMP_REF + 273; +#endif + return temperature_kelvin; +} + BoardType CircuitPeriphery::detect_board_type() { auto hardware_version = CircuitPeriphery::hardware_version(); diff --git a/Src/peripheral/adc/circuit_periphery.hpp b/Src/peripheral/adc/circuit_periphery.hpp index 7cf7f6e..60aa2fa 100644 --- a/Src/peripheral/adc/circuit_periphery.hpp +++ b/Src/peripheral/adc/circuit_periphery.hpp @@ -7,7 +7,6 @@ #include #include #include -#include "peripheral/temperature_sensor/temperature_sensor.hpp" #include "peripheral/adc/adc.hpp" #ifdef __cplusplus @@ -32,10 +31,10 @@ class CircuitPeriphery{ return AdcPeriphery::init(); } - static uint16_t temperature() { - uint16_t temp = AdcPeriphery::get(AdcChannel::ADC_TEMPERATURE); - return stm32TemperatureParse(temp); - } + /** + * @return Temperature, Kelvin + */ + static uint16_t temperature(); /** * @return The current in Amperes if the hardware supports it, otherwise NaN. diff --git a/Src/peripheral/temperature_sensor/temperature_sensor.hpp b/Src/peripheral/temperature_sensor/temperature_sensor.hpp deleted file mode 100644 index 0a1ed2c..0000000 --- a/Src/peripheral/temperature_sensor/temperature_sensor.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2018-2023 Dmitry Ponomarev - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -/** - * @file stm32_temperature.h - * @author d.ponomarev - * @date Nov 18, 2018 - */ - -#ifndef SRC_PERIPHERY_TEMPERATURE_SENSOR_HPP_ -#define SRC_PERIPHERY_TEMPERATURE_SENSOR_HPP_ - -#include "stdint.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** -* @brief Get temperature value -* @return temperature value in degree celsius -*/ -uint16_t stm32TemperatureParse(uint16_t raw_adc_value); - -#ifdef __cplusplus -} -#endif - -#endif // SRC_PERIPHERY_TEMPERATURE_SENSOR_HPP_ diff --git a/Src/platform/stm32f103/CMakeLists.txt b/Src/platform/stm32f103/CMakeLists.txt index 8c9b2bf..bf41baf 100644 --- a/Src/platform/stm32f103/CMakeLists.txt +++ b/Src/platform/stm32f103/CMakeLists.txt @@ -16,7 +16,6 @@ add_executable(${EXECUTABLE} ${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp ${CMAKE_CURRENT_LIST_DIR}/pwm.cpp ${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp - ${CMAKE_CURRENT_LIST_DIR}/temperature_sensor.cpp ${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp ${coreSources} diff --git a/Src/platform/stm32f103/temperature_sensor.cpp b/Src/platform/stm32f103/temperature_sensor.cpp deleted file mode 100644 index 83cb910..0000000 --- a/Src/platform/stm32f103/temperature_sensor.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2018-2023 Dmitry Ponomarev - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -/** - * @file stm32_temperature.c - * @author d.ponomarev - * @brief Implementation of TemperatureSensor - * @date Nov 18, 2018 - * @note From the datasheets: - * mcu temp_ref, C v_ref,V avg_slope,mV/C - * stm32f103 25 1.41 4.3 - * stm32g0 30 0.76 2.5 - */ - -#include "peripheral/temperature_sensor/temperature_sensor.hpp" - -#ifdef STM32G0B1xx - static const uint16_t TEMP_REF = 30; - static const uint16_t ADC_REF = 943; ///< v_ref / 3.3 * 4095 - static const uint16_t AVG_SLOPE = 3.1; ///< avg_slope/(3.3/4096) -#else // STM32F103xB - static const uint16_t TEMP_REF = 25; - static const uint16_t ADC_REF = 1750; ///< v_ref / 3.3 * 4095 - static const uint16_t AVG_SLOPE = 5; ///< avg_slope/(3.3/4096) -#endif - -uint16_t stm32TemperatureParse(uint16_t adc_measurement) { - uint16_t temperature = (ADC_REF - adc_measurement) / AVG_SLOPE + TEMP_REF + 273; - return temperature; -} diff --git a/Src/platform/stm32g0b1/CMakeLists.txt b/Src/platform/stm32g0b1/CMakeLists.txt index 0a2b64f..0d1d3a9 100644 --- a/Src/platform/stm32g0b1/CMakeLists.txt +++ b/Src/platform/stm32g0b1/CMakeLists.txt @@ -24,7 +24,6 @@ add_executable(${EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp ${PLATFORM_DIR}/stm32g0b1/pwm.cpp ${ROOT_DIR}/Src/platform/stm32/iwdg/iwdg.cpp - ${PLATFORM_DIR}/stm32f103/temperature_sensor.cpp ${ROOT_DIR}/Src/platform/stm32/platform_specific.cpp ${coreSources} diff --git a/Src/platform/ubuntu/CMakeLists.txt b/Src/platform/ubuntu/CMakeLists.txt index 72a0b68..b0cf45b 100644 --- a/Src/platform/ubuntu/CMakeLists.txt +++ b/Src/platform/ubuntu/CMakeLists.txt @@ -16,7 +16,6 @@ add_executable(${EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp ${CMAKE_CURRENT_LIST_DIR}/pwm.cpp ${CMAKE_CURRENT_LIST_DIR}/iwdg.cpp - ${CMAKE_CURRENT_LIST_DIR}/temperature_sensor.cpp ${CMAKE_CURRENT_LIST_DIR}/platform_specific.cpp ) diff --git a/Src/platform/ubuntu/temperature_sensor.cpp b/Src/platform/ubuntu/temperature_sensor.cpp deleted file mode 100644 index 83cb910..0000000 --- a/Src/platform/ubuntu/temperature_sensor.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2018-2023 Dmitry Ponomarev - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -/** - * @file stm32_temperature.c - * @author d.ponomarev - * @brief Implementation of TemperatureSensor - * @date Nov 18, 2018 - * @note From the datasheets: - * mcu temp_ref, C v_ref,V avg_slope,mV/C - * stm32f103 25 1.41 4.3 - * stm32g0 30 0.76 2.5 - */ - -#include "peripheral/temperature_sensor/temperature_sensor.hpp" - -#ifdef STM32G0B1xx - static const uint16_t TEMP_REF = 30; - static const uint16_t ADC_REF = 943; ///< v_ref / 3.3 * 4095 - static const uint16_t AVG_SLOPE = 3.1; ///< avg_slope/(3.3/4096) -#else // STM32F103xB - static const uint16_t TEMP_REF = 25; - static const uint16_t ADC_REF = 1750; ///< v_ref / 3.3 * 4095 - static const uint16_t AVG_SLOPE = 5; ///< avg_slope/(3.3/4096) -#endif - -uint16_t stm32TemperatureParse(uint16_t adc_measurement) { - uint16_t temperature = (ADC_REF - adc_measurement) / AVG_SLOPE + TEMP_REF + 273; - return temperature; -}