2020-08-25 01:06:50 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "stm32g431xx.h"
|
|
|
|
#include "stm32g4xx_hal.h"
|
|
|
|
|
2020-09-21 20:29:31 +08:00
|
|
|
extern ADC_HandleTypeDef hadc1;
|
|
|
|
|
2020-08-25 01:06:50 +08:00
|
|
|
namespace STM {
|
|
|
|
|
2020-09-27 05:34:31 +08:00
|
|
|
void Init();
|
|
|
|
// No FreeRTOS function calls are allowed from interrupts with higher priorities than 5.
|
|
|
|
// Certain parts of the data acquisition need higher priorities (so they don't get interrupted by FreeRTOS)
|
|
|
|
// but they also need to trigger FreeRTOS functions. This can be achieved by dispatching a function-pointer
|
|
|
|
// to a lower priority interrupt. The passed function can then handle the FreeRTOS function call
|
|
|
|
bool DispatchToInterrupt(void (*cb)(void));
|
|
|
|
|
2020-08-25 01:06:50 +08:00
|
|
|
static inline bool InInterrupt() {
|
|
|
|
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;
|
|
|
|
}
|
|
|
|
|
2020-09-21 20:29:31 +08:00
|
|
|
static inline int8_t getTemperature() {
|
|
|
|
HAL_ADC_Start(&hadc1);
|
|
|
|
HAL_ADC_PollForConversion(&hadc1, 100);
|
|
|
|
int16_t adc = HAL_ADC_GetValue(&hadc1);
|
2022-06-26 01:36:06 +08:00
|
|
|
// convert to used reference during calibration
|
|
|
|
adc = (int32_t) adc * 3300 / TEMPSENSOR_CAL_VREFANALOG;
|
2020-09-21 20:29:31 +08:00
|
|
|
int16_t rangeFrom = *TEMPSENSOR_CAL2_ADDR - *TEMPSENSOR_CAL1_ADDR;
|
|
|
|
int16_t rangeTo = TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP;
|
|
|
|
adc -= *TEMPSENSOR_CAL1_ADDR;
|
|
|
|
int16_t result = ((int32_t) adc * rangeTo) / rangeFrom + TEMPSENSOR_CAL1_TEMP;
|
|
|
|
if(result < -128) {
|
|
|
|
result = -128;
|
|
|
|
} else if(result > 127) {
|
|
|
|
result = 127;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:06:50 +08:00
|
|
|
}
|