Переструктурирован проект, перенесена ветка тестирования modbus и чуть скомпрессированная библиотека dallas

This commit is contained in:
2025-03-03 13:05:37 +03:00
parent 0f2736b8a5
commit b6978c385a
14 changed files with 387 additions and 4321 deletions

View File

@@ -8,30 +8,33 @@
/* Includes ----------------------------------------------------------------*/
#include "pch_sensors.h"
PCHSens_UnknownSensorsTypeDef UnknownSensors;
PCHSens_DallasBusHandle DallasBus;
PCHSens_SensorActionsTypeDef action;
/* Declarations and definitions --------------------------------------------*/
PCHSens_ModuleTypeDef module1;
PCHSens_ModuleTypeDef module2;
PCHSens_ModuleTypeDef module3;
PCHSens_ModuleTypeDef module4;
PCHSens_ModuleTypeDef module5;
PCHSens_ModuleTypeDef module6;
/* Functions ---------------------------------------------------------------*/
HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor, uint64_t ROM)
HAL_StatusTypeDef PCHSens_InitNewSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_SensorTypeDef* sensor, uint64_t ROM)
{
DALLAS_HandleTypeDef tempsens;
DALLAS_SensorHandleTypeDef tempsens;
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == 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.InitParam = ROM;
sensor->sens.Init.init_func = &Dallas_SensorInitByROM;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
result = Dallas_AddNewSensors(hdallas, &sensor->sens);
if(result != HAL_OK)
{
sensor->not_found = 1;
@@ -42,9 +45,10 @@ HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDe
if(result != HAL_OK)
return result;
sensor->sens.Init.InitParam = sensor->Location.all;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
result = Dallas_AddNewSensors(hdallas, &sensor->sens);
if(result == HAL_OK)
{
sensor->not_found = 0;
@@ -60,21 +64,21 @@ HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDe
HAL_StatusTypeDef PCHSens_AddSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor)
HAL_StatusTypeDef PCHSens_AddSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_SensorTypeDef* sensor)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == 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.InitParam = sensor->Location.all;
sensor->sens.Init.init_func = &Dallas_SensorInitByUserBytes;
result = Dallas_AddNewSensors(onewire, &sensor->sens);
result = Dallas_AddNewSensors(hdallas, &sensor->sens);
if(result == HAL_OK)
{
@@ -88,38 +92,36 @@ HAL_StatusTypeDef PCHSens_AddSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* s
return result;
}
HAL_StatusTypeDef PCHSens_InitModule(OneWire_t *onewire, PCHSens_ModuleTypeDef* module, uint16_t param)
HAL_StatusTypeDef PCHSens_InitModule(DALLAS_HandleTypeDef *hdallas, PCHSens_ModuleTypeDef* module, uint16_t param)
{
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
if(module == NULL)
return HAL_ERROR;
PCHSens_LocationTypeDef initlocation;
initlocation.all = param;
action.read = 1;
module->onewire = onewire;
module->hdallas = hdallas;
module->refLocation = initlocation;
module->sens1.Location.all = module->refLocation.all;
module->sens1.Location.param.Location = 0;
module->sens1.action.read = 1;
PCHSens_AddSensor(onewire, &module->sens1);
PCHSens_AddSensor(hdallas, &module->sens1);
module->sens2.Location.all = module->refLocation.all;
module->sens2.Location.param.Location = 1;
module->sens2.action.read = 1;
PCHSens_AddSensor(onewire, &module->sens2);
PCHSens_AddSensor(hdallas, &module->sens2);
module->sens3.Location.all = module->refLocation.all;
module->sens3.Location.param.Location = 2;
module->sens3.action.read = 1;
PCHSens_AddSensor(onewire, &module->sens3);
PCHSens_AddSensor(hdallas, &module->sens3);
module->sens4.Location.all = module->refLocation.all;
module->sens4.Location.param.Location = 3;
module->sens4.action.read = 1;
PCHSens_AddSensor(onewire, &module->sens4);
PCHSens_AddSensor(hdallas, &module->sens4);
return HAL_OK;
}
@@ -131,20 +133,18 @@ HAL_StatusTypeDef PCHSens_ReadTemperature(PCHSens_ModuleTypeDef *module)
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);
result = Dallas_StartConvertTAll(module->hdallas, DALLAS_WAIT_BUS, 0);
result = PCHSens_SensorHandleActions(module->hdallas, &module->sens1);
result = PCHSens_SensorHandleActions(module->hdallas, &module->sens2);
result = PCHSens_SensorHandleActions(module->hdallas, &module->sens3);
result = PCHSens_SensorHandleActions(module->hdallas, &module->sens4);
return result;
}
HAL_StatusTypeDef PCHSens_CheckSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor)
HAL_StatusTypeDef PCHSens_CheckSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_SensorTypeDef* sensor)
{
HAL_StatusTypeDef result;
PCHSens_LocationTypeDef initlocation;
@@ -171,23 +171,19 @@ HAL_StatusTypeDef PCHSens_CheckSensor(OneWire_t *onewire, PCHSens_SensorTypeDef*
}
HAL_StatusTypeDef PCHSens_FindUnknownSensors(OneWire_t *onewire, PCHSens_UnknownSensorsTypeDef *unknowns)
HAL_StatusTypeDef PCHSens_FindUnknownSensors(PCHSens_DallasBusHandle *hbus)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
return HAL_ERROR;
if(unknowns == NULL)
if(hbus == NULL)
return HAL_ERROR;
unknowns->onewire = onewire;
unknowns->UnknownCnt = 0;
DALLAS_ScratchpadTypeDef scratchpad;
hbus->UnknownCnt = 0;
PCHSens_LocationTypeDef *param = (PCHSens_LocationTypeDef *)&scratchpad.tHighRegister;
for(int i = 0; i < onewire->RomCnt; i++)
PCHSens_LocationTypeDef *param = (PCHSens_LocationTypeDef *)&hbus->hdallas->scratchpad.tHighRegister;
for(int i = 0; i < hbus->hdallas->onewire->RomCnt; i++)
{
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = DS18B20_ReadScratchpad(onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&scratchpad);
result = DS18B20_ReadScratchpad(hbus->hdallas->onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&hbus->hdallas->scratchpad);
if(result != HAL_OK)
__NOP();
@@ -195,31 +191,32 @@ HAL_StatusTypeDef PCHSens_FindUnknownSensors(OneWire_t *onewire, PCHSens_Unknown
(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++]);
// unknowns->unknown_sensors[unknowns->UnknownCnt].Init.InitParam = i;
// unknowns->unknown_sensors[unknowns->UnknownCnt].Init.init_func = &Dallas_SensorInitByInd;
// result = Dallas_AddNewSensors(hbus, &unknowns->unknown_sensors[unknowns->UnknownCnt]);
hbus->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_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)
{
@@ -237,28 +234,28 @@ HAL_StatusTypeDef PCHSens_UndefineSensor(PCHSens_SensorTypeDef *sensor)
return result;
}
HAL_StatusTypeDef PCHSens_SensorHandleActions(OneWire_t *onewire, PCHSens_SensorTypeDef *sensor)
HAL_StatusTypeDef PCHSens_SensorHandleActions(DALLAS_HandleTypeDef *hdallas, PCHSens_SensorTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(sensor == NULL)
return HAL_ERROR;
if(sensor->action.connectROM != NULL)
if(action.connectROM != NULL)
{
result = PCHSens_InitNewSensor(onewire, sensor, sensor->action.connectROM);
sensor->action.connectROM = 0;
result = PCHSens_InitNewSensor(hdallas, sensor, action.connectROM);
action.connectROM = 0;
}
if(sensor->action.read != NULL)
if(action.read != NULL)
{
result = Dallas_ReadTemperature(&sensor->sens);
if(result != HAL_OK)
PCHSens_CheckSensor(onewire, sensor);
PCHSens_CheckSensor(hdallas, sensor);
}
if(sensor->action.deinit != NULL)
if(action.deinit != NULL)
{
sensor->action.deinit = 0;
action.deinit = 0;
result = PCHSens_UndefineSensor(sensor);
}
@@ -275,10 +272,11 @@ void PCHSens_FirstInit(void)
OneWire_Init(&OW);
DS18B20_Search(&DS, &OW);
PCHSens_InitModule(&OW, &module1, REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1);
hdallas1.onewire = &OW;
PCHSens_InitModule(&hdallas1, &module1, REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1);
PCHSens_FindUnknownSensors(&OW, &UnknownSensors);
PCHSens_DefineUnknownSensor(&UnknownSensors, NULL);
PCHSens_FindUnknownSensors(&DallasBus);
// PCHSens_DefineUnknownSensor(&UnknownSensors, NULL);
// Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
// Dallas_SensorInitByInd(&OW, &AllSens.indoor, 2);
// Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 1);