forked from achamaikin/CCSModuleSW30Web
if it breaks, I’m in the mountains
This commit is contained in:
@@ -2,16 +2,19 @@
|
||||
#include "main.h"
|
||||
#include "board.h"
|
||||
#include "tim.h"
|
||||
#include "sma_filter.h"
|
||||
|
||||
extern ADC_HandleTypeDef hadc1;
|
||||
|
||||
//TODO:
|
||||
//TEMP READ
|
||||
//GBT_TEMP_SENSORS
|
||||
// Connector temperature sensors
|
||||
|
||||
InfoBlock_t *InfoBlock = (InfoBlock_t *)(VERSION_OFFSET);
|
||||
|
||||
uint8_t RELAY_State[RELAY_COUNT];
|
||||
static volatile uint8_t adc_lock = 0;
|
||||
static SMAFilter_t conn_temp_adc_filter[2];
|
||||
|
||||
void RELAY_Write(relay_t num, uint8_t state){
|
||||
switch (num) {
|
||||
@@ -30,6 +33,9 @@ void RELAY_Write(relay_t num, uint8_t state){
|
||||
case RELAY_AC:
|
||||
HAL_GPIO_WritePin(RELAY5_GPIO_Port, RELAY5_Pin, state);
|
||||
break;
|
||||
case RELAY_CP:
|
||||
HAL_GPIO_WritePin(RELAY_CP_GPIO_Port, RELAY_CP_Pin, state);
|
||||
break;
|
||||
case RELAY_CC:
|
||||
HAL_GPIO_WritePin(RELAY_CC_GPIO_Port, RELAY_CC_Pin, state);
|
||||
break;
|
||||
@@ -89,8 +95,12 @@ void Init_Peripheral(){
|
||||
RELAY_Write(RELAY3, 0);
|
||||
RELAY_Write(RELAY_DC, 0);
|
||||
RELAY_Write(RELAY_AC, 0);
|
||||
RELAY_Write(RELAY_CP, 1);
|
||||
RELAY_Write(RELAY_CC, 1);
|
||||
RELAY_Write(RELAY_DC1, 0);
|
||||
|
||||
SMAFilter_Init(&conn_temp_adc_filter[0]);
|
||||
SMAFilter_Init(&conn_temp_adc_filter[1]);
|
||||
}
|
||||
|
||||
float pt1000_to_temperature(float resistance) {
|
||||
@@ -119,7 +129,9 @@ float calculate_NTC_resistance(int adc_value, float Vref, float Vin, float R) {
|
||||
return R_NTC;
|
||||
}
|
||||
|
||||
int16_t GBT_ReadTemp(uint8_t ch){
|
||||
int16_t CONN_ReadTemp(uint8_t ch){
|
||||
ADC_LockBlocking();
|
||||
|
||||
//TODO
|
||||
if(ch)ADC_Select_Channel(ADC_CHANNEL_8);
|
||||
else ADC_Select_Channel(ADC_CHANNEL_9);
|
||||
@@ -136,19 +148,28 @@ int16_t GBT_ReadTemp(uint8_t ch){
|
||||
// Остановка АЦП (по желанию)
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
|
||||
if(adcValue>4000) return 20; //Термодатчик не подключен
|
||||
int32_t adc_filtered = SMAFilter_Update(&conn_temp_adc_filter[ch ? 1u : 0u], (int32_t)adcValue);
|
||||
if((uint32_t)adc_filtered > 4000u) {
|
||||
ADC_Unlock();
|
||||
return 20; //Термодатчик не подключен
|
||||
}
|
||||
|
||||
// int adc_value = 2048; // Пример значения АЦП
|
||||
float Vref = 3.3; // Напряжение опорное
|
||||
float Vin = 5.0; // Входное напряжение
|
||||
float R = 1000; // Сопротивление резистора в Омах
|
||||
|
||||
float temp = pt1000_to_temperature(calculate_NTC_resistance(adcValue, Vref, Vin, R));
|
||||
float temp = pt1000_to_temperature(calculate_NTC_resistance((int)adc_filtered, Vref, Vin, R));
|
||||
|
||||
ADC_Unlock();
|
||||
return (int16_t)temp;
|
||||
|
||||
}
|
||||
|
||||
int16_t GBT_ReadTemp(uint8_t ch){
|
||||
return CONN_ReadTemp(ch);
|
||||
}
|
||||
|
||||
void ADC_Select_Channel(uint32_t ch) {
|
||||
ADC_ChannelConfTypeDef conf = {
|
||||
.Channel = ch,
|
||||
@@ -159,3 +180,34 @@ void ADC_Select_Channel(uint32_t ch) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ADC_TryLock(void) {
|
||||
uint32_t primask = __get_PRIMASK();
|
||||
__disable_irq();
|
||||
if (adc_lock != 0u) {
|
||||
if (primask == 0u) {
|
||||
__enable_irq();
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
adc_lock = 1u;
|
||||
if (primask == 0u) {
|
||||
__enable_irq();
|
||||
}
|
||||
return 1u;
|
||||
}
|
||||
|
||||
void ADC_LockBlocking(void) {
|
||||
while (ADC_TryLock() == 0u) {
|
||||
/* wait in main context until ADC is free */
|
||||
}
|
||||
}
|
||||
|
||||
void ADC_Unlock(void) {
|
||||
uint32_t primask = __get_PRIMASK();
|
||||
__disable_irq();
|
||||
adc_lock = 0u;
|
||||
if (primask == 0u) {
|
||||
__enable_irq();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user