Куча всего для работы с датчиками температуры в ПЧ (не работает пока)

Планируется сделать:
- инициализацию (запись локации в ROM) найденных неизвестных датчиков
- переинициализацию уже найденных датчиков
This commit is contained in:
2025-02-13 13:40:33 +03:00
parent ed1ff35913
commit 971817fa2d
5 changed files with 216 additions and 100 deletions

View File

@@ -8,14 +8,47 @@
/* Includes ----------------------------------------------------------------*/
#include "pch_sensors.h"
PCHSens_UnknownSensorsTypeDef UnknownSensors;
/* Declarations and definitions --------------------------------------------*/
PCHSens_ModuleTypeDef module1;
/* Functions ---------------------------------------------------------------*/
HAL_StatusTypeDef PCHSens_FindUnknownSensors(OneWire_t *onewire, PCHSens_UnknownSensorsTypeDef *unknowns)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(unknowns == NULL)
return HAL_ERROR;
unknowns->onewire = onewire;
unknowns->UnknownCnt = 0;
DALLAS_ScratchpadTypeDef scratchpad;
PCHSens_LocationTypeDef *location = (PCHSens_LocationTypeDef *)&scratchpad.tHighRegister;
for(int i = 0; i < onewire->RomCnt; i++)
{
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = DS18B20_ReadScratchpad(onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&scratchpad);
if(result != HAL_OK)
__NOP();
if((IS_REG_PCH_LOCATION(location) == 0) ||
(IS_REG_PCH_LOCATION(location) == 0) ||
(IS_REG_PCH_NUMB(location) == 0) )
{
unknowns->unknown_sensors[unknowns->UnknownCnt].Init.SensInd = i;
unknowns->unknown_sensors[unknowns->UnknownCnt].Init.init_func = &Dallas_SensorInitByInd;
result = Dallas_AddNewSensors(onewire, &unknowns->unknown_sensors[unknowns->UnknownCnt++]);
if(result != HAL_OK)
__NOP();
}
}
return HAL_OK;
}
HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCH_SensorTypeDef* sensor, uint64_t ROM, PCHSens_LocationTypeDef *location)
HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor, uint64_t ROM, uint16_t location)
{
DALLAS_HandleTypeDef tempsens;
HAL_StatusTypeDef result;
@@ -25,59 +58,75 @@ HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCH_SensorTypeDef* s
return HAL_ERROR;
if(location == NULL)
return HAL_ERROR;
PCHSens_LocationTypeDef initlocation = *location;
sensor->Location = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.ROM = ROM;
sensor->sens.Init.UserBytes12 = initlocation.all;
sensor->sens.Init.UserBytes12 = location;
sensor->sens.Init.init_func = &Dallas_SensorInitByROM;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
if(result != HAL_OK)
{
sensor->not_found = 1;
return result;
}
result = Dallas_WriteUserBytes(&sensor->sens, initlocation.all, 0, DALLAS_USER_BYTE_12);
result = Dallas_WriteUserBytes(&sensor->sens, location, 0, DALLAS_USER_BYTE_12);
if(result != HAL_OK)
return result;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
return Dallas_AddNewSensors(onewire, &sensor->sens);
result = Dallas_AddNewSensors(onewire, &sensor->sens);
if(result == HAL_OK)
sensor->not_found = 0;
else
sensor->not_found = 1;
return result;
}
HAL_StatusTypeDef PCHSens_FindSensor(OneWire_t *onewire, PCH_SensorTypeDef* sensor, PCHSens_LocationTypeDef *location)
{
HAL_StatusTypeDef PCHSens_AddSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor, uint16_t location)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
if(location == NULL)
return HAL_ERROR;
PCHSens_LocationTypeDef initlocation = *location;
sensor->Location = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.UserBytes12 = initlocation.all;
sensor->sens.Init.UserBytes12 = location;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
return Dallas_AddNewSensors(onewire, &sensor->sens);
result = Dallas_AddNewSensors(onewire, &sensor->sens);
if(result == HAL_OK)
sensor->not_found = 0;
else
sensor->not_found = 1;
return result;
}
HAL_StatusTypeDef PCHSens_FindModule(OneWire_t *onewire, PCHSens_ModuleTypeDef* module, PCHSens_LocationTypeDef *location, uint8_t init)
HAL_StatusTypeDef PCHSens_InitModule(OneWire_t *onewire, PCHSens_ModuleTypeDef* module, uint16_t location, uint8_t init)
{
if(onewire == NULL)
return HAL_ERROR;
if(module == NULL)
return HAL_ERROR;
PCHSens_LocationTypeDef initlocation = *location;
PCHSens_LocationTypeDef initlocation;
initlocation.all = location;
module->onewire = onewire;
module->refLocation = initlocation;
@@ -85,54 +134,109 @@ HAL_StatusTypeDef PCHSens_FindModule(OneWire_t *onewire, PCHSens_ModuleTypeDef*
if(init == 0)
{
initlocation.location.Location = 0;
PCHSens_FindSensor(onewire, &module->sens1, &initlocation);
PCHSens_AddSensor(onewire, &module->sens1, initlocation.all);
initlocation.location.Location = 1;
PCHSens_FindSensor(onewire, &module->sens2, &initlocation);
PCHSens_AddSensor(onewire, &module->sens2, initlocation.all);
initlocation.location.Location = 2;
PCHSens_FindSensor(onewire, &module->sens3, &initlocation);
PCHSens_AddSensor(onewire, &module->sens3, initlocation.all);
initlocation.location.Location = 3;
PCHSens_FindSensor(onewire, &module->sens4, &initlocation);
PCHSens_AddSensor(onewire, &module->sens4, initlocation.all);
}
else
{
uint64_t ROM = 0x28366a48f6563c8d;
initlocation.location.Location = 0;
PCHSens_InitNewSensor(onewire, &module->sens1, ROM, &initlocation);
PCHSens_InitNewSensor(onewire, &module->sens1, ROM, initlocation.all);
ROM = 0x28CF5248F6BB3C2F;
initlocation.location.Location = 1;
PCHSens_InitNewSensor(onewire, &module->sens2, ROM, &initlocation);
PCHSens_InitNewSensor(onewire, &module->sens2, ROM, initlocation.all);
ROM = 0x28876D60060000CD;
initlocation.location.Location = 2;
PCHSens_InitNewSensor(onewire, &module->sens3, ROM, &initlocation);
PCHSens_InitNewSensor(onewire, &module->sens3, ROM, initlocation.all);
ROM = 0;
initlocation.location.Location = 3;
PCHSens_InitNewSensor(onewire, &module->sens4, ROM, &initlocation);
PCHSens_InitNewSensor(onewire, &module->sens4, ROM, initlocation.all);
}
return HAL_OK;
}
HAL_StatusTypeDef PCHSens_ReadModuleTemperature(PCHSens_ModuleTypeDef *module)
HAL_StatusTypeDef PCHSens_ReadTemperature(PCHSens_ModuleTypeDef *module)
{
HAL_StatusTypeDef result;
if(module == NULL)
return HAL_ERROR;
result = Dallas_StartConvertTAll(module->onewire, DALLAS_WAIT_BUS, 0);
result = Dallas_ReadTemperature(&module->sens1.sens);
if(result != HAL_OK)
PCHSens_CheckSensor(module->onewire, &module->sens1);
result = Dallas_ReadTemperature(&module->sens2.sens);
if(result != HAL_OK)
PCHSens_CheckSensor(module->onewire, &module->sens1);
result = Dallas_ReadTemperature(&module->sens3.sens);
if(result != HAL_OK)
PCHSens_CheckSensor(module->onewire, &module->sens1);
result = Dallas_ReadTemperature(&module->sens4.sens);
if(result != HAL_OK)
PCHSens_CheckSensor(module->onewire, &module->sens1);
return result;
}
void Dallas_FirstInit(void)
HAL_StatusTypeDef PCHSens_CheckSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor)
{
HAL_StatusTypeDef result;
PCHSens_LocationTypeDef initlocation;
unsigned unknow_sensors_flag = 0;
if(sensor == NULL)
return HAL_ERROR;
if(sensor->sens.isInitialized == 0)
return HAL_ERROR;
if((sensor->sens.isLost == 1))
{
initlocation.location.Location = 0;
if(Dallas_ReplaceLostedSensor(&sensor->sens) != HAL_OK)
{
sensor->not_found = 1;
}
else
{
sensor->not_found = 0;
}
}
return HAL_OK;
}
HAL_StatusTypeDef PCHSens_DefineUnknownSensor(PCHSens_UnknownSensorsTypeDef *unknowns, PCHSens_SensorTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(unknowns->UnknownCnt == 0)
return HAL_OK;
if((unknowns->ROMtoDefine != NULL) && (unknowns->LocationtoDefine.all != NULL))
{
result = PCHSens_InitNewSensor(unknowns->onewire, sensor, unknowns->ROMtoDefine, unknowns->LocationtoDefine.all);
}
return HAL_OK;
}
void PCHSens_FirstInit(void)
{
int init_find = 0;
OW.DataPin = DS_Pin;
@@ -142,12 +246,9 @@ void Dallas_FirstInit(void)
OneWire_Init(&OW);
DS18B20_Search(&DS, &OW);
PCHSens_InitModule(&OW, &module1, REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1, init_find);
PCHSens_LocationTypeDef location;
location.all = REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1;
PCHSens_FindModule(&OW, &module1, &location, init_find);
PCHSens_FindUnknownSensors(&OW, &UnknownSensors);
// Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
// Dallas_SensorInitByInd(&OW, &AllSens.indoor, 2);
// Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 1);