Files
DS18B20_Library/DS18B20/pch_sensors.c
Razvalyaev 971817fa2d Куча всего для работы с датчиками температуры в ПЧ (не работает пока)
Планируется сделать:
- инициализацию (запись локации в ROM) найденных неизвестных датчиков
- переинициализацию уже найденных датчиков
2025-02-13 13:40:33 +03:00

275 lines
7.9 KiB
C

/**
******************************************************************************
* @file pch_sensors.c
* @brief Ðàáîòà ñ äàò÷èêàìè òåìïåðàòóðû DS18B20 â Ï×
*****************************************************************************/
/* 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, PCHSens_SensorTypeDef* sensor, uint64_t ROM, uint16_t location)
{
DALLAS_HandleTypeDef tempsens;
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
if(location == NULL)
return HAL_ERROR;
sensor->Location = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.ROM = ROM;
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, location, 0, DALLAS_USER_BYTE_12);
if(result != HAL_OK)
return result;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
if(result == HAL_OK)
sensor->not_found = 0;
else
sensor->not_found = 1;
return result;
}
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;
sensor->Location = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.UserBytes12 = location;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
if(result == HAL_OK)
sensor->not_found = 0;
else
sensor->not_found = 1;
return result;
}
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;
initlocation.all = location;
module->onewire = onewire;
module->refLocation = initlocation;
if(init == 0)
{
initlocation.location.Location = 0;
PCHSens_AddSensor(onewire, &module->sens1, initlocation.all);
initlocation.location.Location = 1;
PCHSens_AddSensor(onewire, &module->sens2, initlocation.all);
initlocation.location.Location = 2;
PCHSens_AddSensor(onewire, &module->sens3, initlocation.all);
initlocation.location.Location = 3;
PCHSens_AddSensor(onewire, &module->sens4, initlocation.all);
}
else
{
uint64_t ROM = 0x28366a48f6563c8d;
initlocation.location.Location = 0;
PCHSens_InitNewSensor(onewire, &module->sens1, ROM, initlocation.all);
ROM = 0x28CF5248F6BB3C2F;
initlocation.location.Location = 1;
PCHSens_InitNewSensor(onewire, &module->sens2, ROM, initlocation.all);
ROM = 0x28876D60060000CD;
initlocation.location.Location = 2;
PCHSens_InitNewSensor(onewire, &module->sens3, ROM, initlocation.all);
ROM = 0;
initlocation.location.Location = 3;
PCHSens_InitNewSensor(onewire, &module->sens4, ROM, initlocation.all);
}
return HAL_OK;
}
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;
}
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;
OW.DataPort = DS_GPIO_Port;
DS.Resolution = DS18B20_RESOLUTION_9BITS;
OneWire_Init(&OW);
DS18B20_Search(&DS, &OW);
PCHSens_InitModule(&OW, &module1, REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1, init_find);
PCHSens_FindUnknownSensors(&OW, &UnknownSensors);
// Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
// Dallas_SensorInitByInd(&OW, &AllSens.indoor, 2);
// Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 1);
// Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3);
// Dallas_SensorInitByInd(&OW, &AllSens.big_room, 4);
// Dallas_SensorInitByInd(&OW, &AllSens.small_room, 5);
// Dallas_SensorInitByInd(&OW, &AllSens.living_room, 6);
// Dallas_SensorInitByInd(&OW, &AllSens.basement, 7);
//
// uint8_t mask = DALLAS_USER_BYTE_ALL;
// Dallas_WriteUserBytes(&AllSens.outdoor, 1, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.indoor, 2, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.bathroom, 3, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.kitchen, 4, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.big_room, 5, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.small_room, 6, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.living_room, 7, NULL, mask);
// Dallas_WriteUserBytes(&AllSens.basement, 8, NULL, mask);
}