From 572caf437c5a94978fc9c604cc3e92085abf2bf0 Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Sat, 7 Dec 2024 15:24:37 +0000 Subject: [PATCH 1/6] Set general layout of needed files and functions --- Sts1CobcSw/Hal/IoNames.hpp | 2 ++ Sts1CobcSw/Periphery/TemperatureSensor.cpp | 28 ++++++++++++++++++++++ Sts1CobcSw/Periphery/TemperatureSensor.hpp | 1 + 3 files changed, 31 insertions(+) create mode 100644 Sts1CobcSw/Periphery/TemperatureSensor.cpp create mode 100644 Sts1CobcSw/Periphery/TemperatureSensor.hpp diff --git a/Sts1CobcSw/Hal/IoNames.hpp b/Sts1CobcSw/Hal/IoNames.hpp index 62829f5c..03aa20b5 100644 --- a/Sts1CobcSw/Hal/IoNames.hpp +++ b/Sts1CobcSw/Hal/IoNames.hpp @@ -53,6 +53,8 @@ inline constexpr auto epsAdc4CsPin = pd2; inline constexpr auto epsAdc5CsPin = pb4; inline constexpr auto epsAdc6CsPin = pb5; +inline constexpr auto rfTmpPin = pc0; + inline constexpr auto rfSpiIndex = RODOS::SPI_IDX2; inline constexpr auto rfSpiSckPin = pc7; inline constexpr auto rfSpiMisoPin = pc2; diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.cpp b/Sts1CobcSw/Periphery/TemperatureSensor.cpp new file mode 100644 index 00000000..02467a5f --- /dev/null +++ b/Sts1CobcSw/Periphery/TemperatureSensor.cpp @@ -0,0 +1,28 @@ +//! @file +//! @brief temperature sensors +//! + + +#include + +#include +#include + +//from ionames inline constexpr auto rfTmpPin = pc0; + +// https://github.com/SpaceTeam/rodos/blob/st_develop/api/hal/hal_adc.h +// https://github.com/SpaceTeam/rodos/blob/st_develop/src/bare-metal/stm32f4/hal/hal_adc.cpp + +// STM32F411 +// RF_TMP is read on pin PC0 on internal ADC + + + +auto ReadRfTemperature() -> std::uint16_t +{ + std::uint16_t rfTemp = 0; + + //code + + return rfTemp; +} diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.hpp b/Sts1CobcSw/Periphery/TemperatureSensor.hpp new file mode 100644 index 00000000..6f70f09b --- /dev/null +++ b/Sts1CobcSw/Periphery/TemperatureSensor.hpp @@ -0,0 +1 @@ +#pragma once From 4431e9167e952fe56cb4e8a8619fc83742ede11b Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Sat, 7 Dec 2024 17:43:42 +0000 Subject: [PATCH 2/6] Add prototype code for temperature sensor --- Sts1CobcSw/Periphery/TemperatureSensor.cpp | 39 ++++++++++++++++++++-- Sts1CobcSw/Periphery/TemperatureSensor.hpp | 4 +++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.cpp b/Sts1CobcSw/Periphery/TemperatureSensor.cpp index 02467a5f..a7ba2e13 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.cpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.cpp @@ -7,8 +7,11 @@ #include #include +#include -//from ionames inline constexpr auto rfTmpPin = pc0; +// from ionames inline constexpr auto rfTmpPin = pc0; //probably not used + +// temperature Sensor: TMP36xS // https://github.com/SpaceTeam/rodos/blob/st_develop/api/hal/hal_adc.h // https://github.com/SpaceTeam/rodos/blob/st_develop/src/bare-metal/stm32f4/hal/hal_adc.cpp @@ -20,9 +23,41 @@ auto ReadRfTemperature() -> std::uint16_t { + + //RODOS::ADC_ERROR errorValue; //there seems to be not an option with 0 = no error witch is dumb. + + + + const RODOS::ADC_IDX adcChannelIndex = RODOS::ADC_IDX1; // index 1, 2, 3are ok + RODOS::HAL_ADC temperatureAdcObject(adcChannelIndex); + + // set channel: + int32_t error = 0; + const RODOS::ADC_CHANNEL adcChannelForRfTemp = RODOS::ADC_CH_010; // ADC_CH_010, // PC0/PC0/PC0 + error = temperatureAdcObject.init(adcChannelForRfTemp); + if ( error != 0 ) + { + //do something with error + } + + // set resolution: + + // 6-bit 2.578°C + // 8-bit 0.644°C + // 10-bit 0.161°C + // 12-bit 0.0403°C + const int32_t bitResolution = 10; + const RODOS::ADC_PARAMETER_TYPE configParameter = RODOS::ADC_PARAMETER_RESOLUTION; + error = temperatureAdcObject.config(configParameter, bitResolution); + if ( error != 0 ) + { + //do something with error + } + std::uint16_t rfTemp = 0; + rfTemp = temperatureAdcObject.read(adcChannelForRfTemp); //errors are returned as negative values. type is uint16_t errors are ~65535 - //code + temperatureAdcObject.reset(); // unsure if this is needed. return rfTemp; } diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.hpp b/Sts1CobcSw/Periphery/TemperatureSensor.hpp index 6f70f09b..cdf957a0 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.hpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.hpp @@ -1 +1,5 @@ #pragma once + +#include + +auto ReadRfTemperature() -> std::uint16_t; From 6a584f0e483ce08ea755e1bf638a1934a9ccd9ad Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Sat, 7 Dec 2024 19:38:54 +0000 Subject: [PATCH 3/6] Split code into init and read function --- Sts1CobcSw/Periphery/TemperatureSensor.cpp | 53 ++++++++-------------- Sts1CobcSw/Periphery/TemperatureSensor.hpp | 1 + 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.cpp b/Sts1CobcSw/Periphery/TemperatureSensor.cpp index a7ba2e13..10e3be6c 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.cpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.cpp @@ -1,12 +1,12 @@ //! @file -//! @brief temperature sensors +//! @brief temperature sensors readout //! #include -#include -#include +#include // Todo: MR:07.12.2024: I think this is not needed, check if file can be reset to original state +#include // Todo: MR:07.12.2024: I think this is not needed, #include // from ionames inline constexpr auto rfTmpPin = pc0; //probably not used @@ -21,43 +21,30 @@ -auto ReadRfTemperature() -> std::uint16_t -{ - - //RODOS::ADC_ERROR errorValue; //there seems to be not an option with 0 = no error witch is dumb. - +RODOS::HAL_ADC temperatureAdc(RODOS::ADC_IDX1); //this blocks adc index 1 !!! +constexpr RODOS::ADC_CHANNEL adcChannel = RODOS::ADC_CH_010; // ADC_CH_010, // PC0/PC0/PC0 - const RODOS::ADC_IDX adcChannelIndex = RODOS::ADC_IDX1; // index 1, 2, 3are ok - RODOS::HAL_ADC temperatureAdcObject(adcChannelIndex); +auto InitRfTemperature() -> void +{ + temperatureAdc.init(adcChannel); - // set channel: - int32_t error = 0; - const RODOS::ADC_CHANNEL adcChannelForRfTemp = RODOS::ADC_CH_010; // ADC_CH_010, // PC0/PC0/PC0 - error = temperatureAdcObject.init(adcChannelForRfTemp); - if ( error != 0 ) - { - //do something with error - } + const int32_t bitResolution = 12; // 12-bit: 0.0403°C // 10-bit: 0.161°C // 8-bit: 0.644°C // 6-bit: 2.578°C + temperatureAdc.config(RODOS::ADC_PARAMETER_RESOLUTION, bitResolution); +} - // set resolution: - // 6-bit 2.578°C - // 8-bit 0.644°C - // 10-bit 0.161°C - // 12-bit 0.0403°C - const int32_t bitResolution = 10; - const RODOS::ADC_PARAMETER_TYPE configParameter = RODOS::ADC_PARAMETER_RESOLUTION; - error = temperatureAdcObject.config(configParameter, bitResolution); - if ( error != 0 ) +auto ReadRfTemperature() -> std::uint16_t +{ + std::uint16_t rfTemp = 0; + const std::uint16_t adcConvFail = 65533; // ADC failed conversion + rfTemp = temperatureAdc.read(adcChannel); //errors are returned as negative values. type is uint16_t errors are ~65535 + if (rfTemp == adcConvFail) { - //do something with error + temperatureAdc.reset(); // Todo: MR:07.12.2024: check what function actually does. It might reset too much. + InitRfTemperature(); + rfTemp = temperatureAdc.read(adcChannel); } - std::uint16_t rfTemp = 0; - rfTemp = temperatureAdcObject.read(adcChannelForRfTemp); //errors are returned as negative values. type is uint16_t errors are ~65535 - - temperatureAdcObject.reset(); // unsure if this is needed. - return rfTemp; } diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.hpp b/Sts1CobcSw/Periphery/TemperatureSensor.hpp index cdf957a0..ab26afd3 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.hpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.hpp @@ -2,4 +2,5 @@ #include +auto InitRfTemperature() -> void; auto ReadRfTemperature() -> std::uint16_t; From 6be8bff4261abf853b9674fd4a1ab0cd6c78a67a Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Fri, 13 Dec 2024 17:04:13 +0100 Subject: [PATCH 4/6] Refactor code for temperature sensor Refactor code for temperature sensor Refactor code for temperature sensor --- Sts1CobcSw/Hal/IoNames.hpp | 3 -- Sts1CobcSw/Periphery/TemperatureSensor.cpp | 54 ++++++++-------------- Sts1CobcSw/Periphery/TemperatureSensor.hpp | 9 +++- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/Sts1CobcSw/Hal/IoNames.hpp b/Sts1CobcSw/Hal/IoNames.hpp index 03aa20b5..9e9e3800 100644 --- a/Sts1CobcSw/Hal/IoNames.hpp +++ b/Sts1CobcSw/Hal/IoNames.hpp @@ -53,8 +53,6 @@ inline constexpr auto epsAdc4CsPin = pd2; inline constexpr auto epsAdc5CsPin = pb4; inline constexpr auto epsAdc6CsPin = pb5; -inline constexpr auto rfTmpPin = pc0; - inline constexpr auto rfSpiIndex = RODOS::SPI_IDX2; inline constexpr auto rfSpiSckPin = pc7; inline constexpr auto rfSpiMisoPin = pc2; @@ -66,7 +64,6 @@ inline constexpr auto rfGpio0Pin = pc6; inline constexpr auto rfGpio1Pin = pc8; inline constexpr auto rfPaEnablePin = pc9; #if HW_VERSION >= 27 -inline constexpr auto rfTmpPin = pc0; #if HW_VERSION >= 30 inline constexpr auto rfLatchupDisablePin1 = pa0; inline constexpr auto rfLatchupDisablePin2 = pa1; diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.cpp b/Sts1CobcSw/Periphery/TemperatureSensor.cpp index 10e3be6c..1bb1f4e8 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.cpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.cpp @@ -1,50 +1,36 @@ //! @file -//! @brief temperature sensors readout -//! +//! @brief "Driver" for the temperature sensor TMP36xS -#include - -#include // Todo: MR:07.12.2024: I think this is not needed, check if file can be reset to original state -#include // Todo: MR:07.12.2024: I think this is not needed, -#include - -// from ionames inline constexpr auto rfTmpPin = pc0; //probably not used - -// temperature Sensor: TMP36xS - -// https://github.com/SpaceTeam/rodos/blob/st_develop/api/hal/hal_adc.h -// https://github.com/SpaceTeam/rodos/blob/st_develop/src/bare-metal/stm32f4/hal/hal_adc.cpp - -// STM32F411 -// RF_TMP is read on pin PC0 on internal ADC +#include +#include -RODOS::HAL_ADC temperatureAdc(RODOS::ADC_IDX1); //this blocks adc index 1 !!! -constexpr RODOS::ADC_CHANNEL adcChannel = RODOS::ADC_CH_010; // ADC_CH_010, // PC0/PC0/PC0 +namespace sts1cobcsw::rftemperaturesensor +{ +// RF_TMP is read on pin PC0 on internal ADC1 +auto adc = RODOS::HAL_ADC(RODOS::ADC_IDX1); +constexpr auto channel = RODOS::ADC_CH_010; -auto InitRfTemperature() -> void +auto Initialize() -> void { - temperatureAdc.init(adcChannel); - - const int32_t bitResolution = 12; // 12-bit: 0.0403°C // 10-bit: 0.161°C // 8-bit: 0.644°C // 6-bit: 2.578°C - temperatureAdc.config(RODOS::ADC_PARAMETER_RESOLUTION, bitResolution); + adc.init(channel); + const int32_t bitResolution = 12; // = 0.0403°C + adc.config(RODOS::ADC_PARAMETER_RESOLUTION, bitResolution); } -auto ReadRfTemperature() -> std::uint16_t +auto Read() -> std::uint16_t { - std::uint16_t rfTemp = 0; - const std::uint16_t adcConvFail = 65533; // ADC failed conversion - rfTemp = temperatureAdc.read(adcChannel); //errors are returned as negative values. type is uint16_t errors are ~65535 - if (rfTemp == adcConvFail) + auto rfTemperature = adc.read(channel); + if(rfTemperature == static_cast(RODOS::ADC_ERR_CONV_FAIL)) { - temperatureAdc.reset(); // Todo: MR:07.12.2024: check what function actually does. It might reset too much. - InitRfTemperature(); - rfTemp = temperatureAdc.read(adcChannel); + adc.reset(); + Initialize(); + rfTemperature = adc.read(channel); } - - return rfTemp; + return rfTemperature; +} } diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.hpp b/Sts1CobcSw/Periphery/TemperatureSensor.hpp index ab26afd3..4a893c4a 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.hpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.hpp @@ -1,6 +1,11 @@ #pragma once + #include -auto InitRfTemperature() -> void; -auto ReadRfTemperature() -> std::uint16_t; + +namespace sts1cobcsw::rftemperaturesensor +{ +auto Initialize() -> void; +auto Read() -> std::uint16_t; +} From 2f2dcb3a3a65482013ef81e5707050ea7e73652c Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Fri, 13 Dec 2024 16:19:25 +0000 Subject: [PATCH 5/6] Add `TemperatureSensor.cpp` as target source --- Sts1CobcSw/Periphery/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sts1CobcSw/Periphery/CMakeLists.txt b/Sts1CobcSw/Periphery/CMakeLists.txt index 310b17f8..7b4a7ce2 100644 --- a/Sts1CobcSw/Periphery/CMakeLists.txt +++ b/Sts1CobcSw/Periphery/CMakeLists.txt @@ -5,7 +5,10 @@ target_link_libraries( ) if(CMAKE_SYSTEM_NAME STREQUAL Generic) - target_sources(Sts1CobcSw_Periphery PRIVATE Eps.cpp Flash.cpp Fram.cpp Rf.cpp Spis.cpp) + target_sources( + Sts1CobcSw_Periphery PRIVATE Eps.cpp Flash.cpp Fram.cpp Rf.cpp Spis.cpp + TemperatureSensor.cpp + ) else() target_sources(Sts1CobcSw_Periphery PRIVATE FlashMock.cpp FramMock.cpp SpiMocks.cpp) endif() From 71c80cd384e945430434f3a1325de5ca87232896 Mon Sep 17 00:00:00 2001 From: Marcus Roth Date: Fri, 13 Dec 2024 17:21:44 +0000 Subject: [PATCH 6/6] Add HW test for temperature sensor --- Sts1CobcSw/Periphery/TemperatureSensor.cpp | 2 +- Tests/HardwareTests/CMakeLists.txt | 7 +++ .../HardwareTests/TemperatureSensor.test.cpp | 44 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Tests/HardwareTests/TemperatureSensor.test.cpp diff --git a/Sts1CobcSw/Periphery/TemperatureSensor.cpp b/Sts1CobcSw/Periphery/TemperatureSensor.cpp index 1bb1f4e8..f13438d3 100644 --- a/Sts1CobcSw/Periphery/TemperatureSensor.cpp +++ b/Sts1CobcSw/Periphery/TemperatureSensor.cpp @@ -17,7 +17,7 @@ constexpr auto channel = RODOS::ADC_CH_010; auto Initialize() -> void { adc.init(channel); - const int32_t bitResolution = 12; // = 0.0403°C + const int32_t bitResolution = 12; // 3.3 V / 2^12 bits / (10 mV/°C) = 0.0806 °C/bit adc.config(RODOS::ADC_PARAMETER_RESOLUTION, bitResolution); } diff --git a/Tests/HardwareTests/CMakeLists.txt b/Tests/HardwareTests/CMakeLists.txt index 38229786..758d6aa2 100644 --- a/Tests/HardwareTests/CMakeLists.txt +++ b/Tests/HardwareTests/CMakeLists.txt @@ -103,6 +103,13 @@ target_link_libraries( ) add_watchdog_version_of(SpiSupervisor) +add_program(TemperatureSensor TemperatureSensor.test.cpp) +target_link_libraries( + Sts1CobcSwTests_TemperatureSensor PRIVATE rodos::rodos Sts1CobcSw_Periphery + Sts1CobcSwTests_RfLatchupDisablePin +) +add_watchdog_version_of(TemperatureSensor) + add_program(Gpio Gpio.test.cpp) target_link_libraries( Sts1CobcSwTests_Gpio PRIVATE rodos::rodos Sts1CobcSw_Hal Sts1CobcSwTests_RfLatchupDisablePin diff --git a/Tests/HardwareTests/TemperatureSensor.test.cpp b/Tests/HardwareTests/TemperatureSensor.test.cpp new file mode 100644 index 00000000..6ed961c5 --- /dev/null +++ b/Tests/HardwareTests/TemperatureSensor.test.cpp @@ -0,0 +1,44 @@ +#include + +#include + +#include + + +namespace sts1cobcsw +{ +class TermperatureSensorTest : public RODOS::StaticThread<> +{ +public: + TermperatureSensorTest() : StaticThread("TermperatureSensorTest") + { + } + + +private: + void init() override + { + InitializeRfLatchupDisablePins(); + rftemperaturesensor::Initialize(); + } + + + void run() override + { + using RODOS::PRINTF; + + EnableRfLatchupProtection(); + + PRINTF("\nRF temperature sensor test\n\n"); + + auto const conversionFactor = 0.0806; // °C/bit + auto const offset = -50; // °C at 0 V + TIME_LOOP(0, 1000 * RODOS::MILLISECONDS) + { + auto temperature = rftemperaturesensor::Read(); + PRINTF("raw value = %5d\n", temperature); + PRINTF("temperature = %5.1f deg C\n", temperature * conversionFactor + offset); + } + } +} termperatureSensorTest; +}