Files
DS18B20_Library/DS18B20/pch_sensors.c

294 lines
8.3 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_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor, uint64_t ROM)
{
DALLAS_HandleTypeDef tempsens;
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
// sensor->UserBytes = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.ROM = ROM;
sensor->sens.Init.UserBytes12 = sensor->Location.all;
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, sensor->Location.all, sensor->Location.all, USED_USER_BYTES);
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)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
// sensor->UserBytes = (PCHSens_LocationTypeDef *)&sensor->sens.scratchpad.tHighRegister;
sensor->sens.Init.UserBytes12 = sensor->Location.all;
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 param)
{
if(onewire == NULL)
return HAL_ERROR;
if(module == NULL)
return HAL_ERROR;
PCHSens_LocationTypeDef initlocation;
initlocation.all = param;
module->onewire = onewire;
module->refLocation = initlocation;
module->sens1.Location.all = module->refLocation.all;
module->sens1.Location.param.Location = 0;
PCHSens_AddSensor(onewire, &module->sens1);
module->sens2.Location.all = module->refLocation.all;
module->sens2.Location.param.Location = 1;
PCHSens_AddSensor(onewire, &module->sens2);
module->sens3.Location.all = module->refLocation.all;
module->sens3.Location.param.Location = 2;
PCHSens_AddSensor(onewire, &module->sens3);
module->sens4.Location.all = module->refLocation.all;
module->sens4.Location.param.Location = 3;
PCHSens_AddSensor(onewire, &module->sens4);
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 = PCHSens_SensorHandleActions(module->onewire, &module->sens1);
result = PCHSens_SensorHandleActions(module->onewire, &module->sens2);
result = PCHSens_SensorHandleActions(module->onewire, &module->sens3);
result = PCHSens_SensorHandleActions(module->onewire, &module->sens4);
PCHSens_DefineUnknownSensor(&UnknownSensors, NULL);
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.param.Location = 0;
if(Dallas_ReplaceLostedSensor(&sensor->sens) != HAL_OK)
{
sensor->not_found = 1;
}
else
{
sensor->not_found = 0;
}
}
return HAL_OK;
}
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 *param = (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_SENS_LOCATION(param) == 0) ||
(IS_REG_PCH_LOCATION(param) == 0) ||
(IS_REG_PCH_NUMB(param) == 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_DefineUnknownSensor(PCHSens_UnknownSensorsTypeDef *unknowns, PCHSens_SensorTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(sensor == NULL)
return HAL_ERROR;
if((unknowns->ROMtoDefine != NULL) && (unknowns->LocationtoDefine.all != NULL) && (unknowns->senstoDefine != NULL))
{
result = PCHSens_InitNewSensor(unknowns->onewire, unknowns->senstoDefine, unknowns->ROMtoDefine);
unknowns->ROMtoDefine = 0;
unknowns->LocationtoDefine.all = 0;
unknowns->senstoDefine = 0;
return result;
}
return HAL_OK;
}
HAL_StatusTypeDef PCHSens_UndefineSensor(PCHSens_SensorTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(sensor == NULL)
return HAL_ERROR;
result = Dallas_WriteUserBytes(&sensor->sens, 0, 0, USED_USER_BYTES);
if(result != HAL_OK)
{
return result;
}
result = Dallas_SensorDeInit(&sensor->sens);
return result;
}
HAL_StatusTypeDef PCHSens_SensorHandleActions(OneWire_t *onewire, PCHSens_SensorTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(sensor == NULL)
return HAL_ERROR;
if(sensor->action.connectROM != NULL)
{
result = PCHSens_InitNewSensor(onewire, sensor, sensor->action.connectROM);
sensor->action.connectROM = 0;
}
if(sensor->action.read != NULL)
{
// sensor->action.read = 0;
result = Dallas_ReadTemperature(&sensor->sens);
if(result != HAL_OK)
PCHSens_CheckSensor(onewire, sensor);
}
if(sensor->action.deinit != NULL)
{
sensor->action.deinit = 0;
result = PCHSens_UndefineSensor(sensor);
}
return result;
}
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);
PCHSens_FindUnknownSensors(&OW, &UnknownSensors);
PCHSens_DefineUnknownSensor(&UnknownSensors, NULL);
// 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);
}