From 28749c63e8006dcf35c3a78a4b3d39dd0cb0e320 Mon Sep 17 00:00:00 2001 From: Razvalyaev Date: Wed, 5 Mar 2025 10:07:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=D0=BD=D0=B0=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D1=81=D0=BE=D1=80=D0=BE=D0=B2=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD=D0=B8=D0=B8=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D0=B1=D0=B0=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Заполнение респонса в модбас выведено в отдельную функцию --- Core/Dallas/dallas_tools.c | 10 +- Core/Dallas/dallas_tools.h | 1 + Core/Dallas/ds18b20_driver.h | 1 - Core/Dallas/onewire.c | 4 + Core/Modbus/modbus.c | 3 + Core/Modbus/modbus_data.h | 5 +- Core/PY32Module/PY32module_main.c | 253 +++++++++++++++++++----------- Core/PY32Module/PY32module_main.h | 10 ++ Core/PY32Module/pch_sensors.c | 1 - Core/PY32Module/pch_sensors.h | 1 - MDK-ARM/PY32Dallas.uvoptx | 52 +++++- 11 files changed, 241 insertions(+), 100 deletions(-) diff --git a/Core/Dallas/dallas_tools.c b/Core/Dallas/dallas_tools.c index 0ed6a6f..a791dff 100644 --- a/Core/Dallas/dallas_tools.c +++ b/Core/Dallas/dallas_tools.c @@ -112,7 +112,7 @@ HAL_StatusTypeDef Dallas_SensorInitByROM(DALLAS_HandleTypeDef *hdallas, DALLAS_S comparebytes = DALLAS_ROM_SIZE; for(int rom_byte = 0; rom_byte < DALLAS_ROM_SIZE; rom_byte++) { - if(DS.DevAddr[i][rom_byte] == ROM[rom_byte]) + if(hdallas->ds_devices->DevAddr[i][rom_byte] == ROM[rom_byte]) comparebytes--; } if(comparebytes == 0) @@ -126,7 +126,7 @@ HAL_StatusTypeDef Dallas_SensorInitByROM(DALLAS_HandleTypeDef *hdallas, DALLAS_S if(comparebytes == 0) { - result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[ROM_ind]); + result = Dallas_SensorInit(hdallas, sensor, &hdallas->ds_devices->DevAddr[ROM_ind]); return result; } else @@ -164,7 +164,7 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(DALLAS_HandleTypeDef *hdallas, DA for(int i = 0; i < hdallas->onewire->RomCnt; i++) { /* Проверка присутствует ли выбранный датчик на линии */ - result = DS18B20_ReadScratchpad(hdallas->onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&hdallas->scratchpad); + result = DS18B20_ReadScratchpad(hdallas->onewire, (uint8_t *)&hdallas->ds_devices->DevAddr[i], (uint8_t *)&hdallas->scratchpad); if (result != HAL_OK) return result; @@ -199,7 +199,7 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(DALLAS_HandleTypeDef *hdallas, DA { // sensor->isInitialized = 1; // sensor->Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes; - result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[i]); + result = Dallas_SensorInit(hdallas, sensor, &hdallas->ds_devices->DevAddr[i]); return result; } } @@ -226,7 +226,7 @@ HAL_StatusTypeDef Dallas_SensorInitByInd(DALLAS_HandleTypeDef *hdallas, DALLAS_S if(sensor == NULL) return HAL_ERROR; - result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[sensor->Init.InitParam]); + result = Dallas_SensorInit(hdallas, sensor, &hdallas->ds_devices->DevAddr[sensor->Init.InitParam]); return result; } diff --git a/Core/Dallas/dallas_tools.h b/Core/Dallas/dallas_tools.h index 345c8ff..45a8b26 100644 --- a/Core/Dallas/dallas_tools.h +++ b/Core/Dallas/dallas_tools.h @@ -82,6 +82,7 @@ typedef struct __packed typedef struct { OneWire_t *onewire; + DS18B20_Drv_t *ds_devices; DALLAS_ScratchpadTypeDef scratchpad; }DALLAS_HandleTypeDef; extern DALLAS_HandleTypeDef hdallas1; diff --git a/Core/Dallas/ds18b20_driver.h b/Core/Dallas/ds18b20_driver.h index 4751312..baf22f6 100644 --- a/Core/Dallas/ds18b20_driver.h +++ b/Core/Dallas/ds18b20_driver.h @@ -77,7 +77,6 @@ typedef enum { typedef struct { uint8_t DevAddr[DS18B20_DEVICE_AMOUNT][8]; - DS18B20_Res_t Resolution; } DS18B20_Drv_t; extern DS18B20_Drv_t DS; extern OneWire_t OW; diff --git a/Core/Dallas/onewire.c b/Core/Dallas/onewire.c index ff5e7d0..b7433a3 100644 --- a/Core/Dallas/onewire.c +++ b/Core/Dallas/onewire.c @@ -16,6 +16,7 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) if(OW == NULL) return; #ifndef ONEWIRE_UART_H + __disable_irq(); if(bit) { /* Set line low */ @@ -46,6 +47,7 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) OneWire_Delay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_0_US); OneWire_Pin_Mode(OW, Input); } + __enable_irq(); #else OneWireUART_ProcessBit(onewire_uart, bit); #endif @@ -61,6 +63,7 @@ uint8_t OneWire_ReadBit(OneWire_t* OW) if(OW == NULL) return 0; + __disable_irq(); uint8_t bit = 0; #ifndef ONEWIRE_UART_H /* Line low */ @@ -77,6 +80,7 @@ uint8_t OneWire_ReadBit(OneWire_t* OW) /* Wait 50us to complete 60us period */ OneWire_Delay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_READ_CMD_US - ONEWIRE_READ_DELAY_US); + __enable_irq(); #else bit = OneWireUART_ProcessBit(onewire_uart, 1); #endif diff --git a/Core/Modbus/modbus.c b/Core/Modbus/modbus.c index e5e0626..5b29082 100644 --- a/Core/Modbus/modbus.c +++ b/Core/Modbus/modbus.c @@ -287,6 +287,9 @@ MB_ExceptionTypeDef MB_DefineRegistersAddress(uint16_t **pRegs, uint16_t Addr, u else if(MB_Check_Address_For_Arr(Addr, Qnt, R_SENS_PARAMS_ADDR, R_SENS_PARAMS_QNT) == NO_ERRORS) { *pRegs = MB_Set_Register_Ptr(&MB_DATA.InRegs, Addr); // начало регистров хранения/входных + // икрементирвоание счетчика скана, для вывода всех датчиков на линии в модбас структуру + extern void PYModule_IncrementScanSensor(void); + PYModule_IncrementScanSensor(); } // if address doesnt match any array - return illegal data address response else diff --git a/Core/Modbus/modbus_data.h b/Core/Modbus/modbus_data.h index fdc05d0..2168fbd 100644 --- a/Core/Modbus/modbus_data.h +++ b/Core/Modbus/modbus_data.h @@ -69,7 +69,7 @@ typedef struct //MB_DataInRegsTypeDef // DEFINES FOR INPUT REGISTERS ARRAYS #define R_TEMPERATURE_ADDR (0) #define R_TEMPERATURE_QNT (DS18B20_DEVICE_AMOUNT) -#define R_SENS_PARAMS_ADDR (DS18B20_DEVICE_AMOUNT+1) +#define R_SENS_PARAMS_ADDR (DS18B20_DEVICE_AMOUNT) #define R_SENS_PARAMS_QNT (sizeof(MB_SensorParamsTypeDef)/sizeof(uint16_t)) // DEFINES FOR HOLDING REGISTERS ARRAYS @@ -112,8 +112,9 @@ typedef struct //MB_DataCoilsTypeDef unsigned ReadSensor:1; unsigned InitSensor:1; unsigned DeInitSensor:1; + unsigned ScanSensors:1; - unsigned reserved:13; + unsigned reserved:11; /* reg 2 - settings */ unsigned ConvertionDone:1; diff --git a/Core/PY32Module/PY32module_main.c b/Core/PY32Module/PY32module_main.c index a0fdfcf..778be9c 100644 --- a/Core/PY32Module/PY32module_main.c +++ b/Core/PY32Module/PY32module_main.c @@ -12,8 +12,13 @@ /* Declarations and definitions --------------------------------------------*/ PCHSens_TypeDef pchsens; +PCHSens_DallasBusHandle DallasBus; +static uint8_t scan_cnt; /* Functions ---------------------------------------------------------------*/ +void PYModule_FillResponse(PCHSens_SensorTypeDef* sensor, Sensor_ResponseStatusTypeDef status); +void PYModule_CheckModuleLosted(PCHSens_ModuleTypeDef *module, uint8_t *losted_flag); +void PYModule_StoreModuleToModbus(PCHSens_ModuleTypeDef *module); void PYModule_main(void) { @@ -27,10 +32,13 @@ void PYModule_main(void) } } - if(MB_DATA.Coils.ReadSensor) + if(MB_DATA.Coils.ScanSensors) { - PYModule_ReadSensor(&hdallas1, &pchsens); - MB_DATA.Coils.ReadSensor = 0; + PYModule_ScanSensor(&DallasBus); + } + else + { + scan_cnt = 0; } if(MB_DATA.Coils.InitSensor) @@ -46,7 +54,6 @@ void PYModule_main(void) } PYModule_CheckLosted(&pchsens); - if(MB_DATA.Coils.RunConvertions) { @@ -70,7 +77,6 @@ void PYModule_FirstInit(void) { OW.DataPin = DS_Pin; OW.DataPort = DS_GPIO_Port; - DS.Resolution = DS18B20_RESOLUTION_9BITS; /* Инициализация onewire и поиск датчиков*/ OneWire_Init(&OW); @@ -84,6 +90,7 @@ void PYModule_FirstInit(void) /* Инициализация структур датчиков ПЧ */ DallasBus.hdallas = &hdallas1; DallasBus.hdallas->onewire = &OW; + DallasBus.hdallas->ds_devices = &DS; PCHSens_InitModule(&hdallas1, &pchsens.module1, REG_PCH_NUMB_11|REG_PCH_DIODE_NUMB_1); PCHSens_InitModule(&hdallas1, &pchsens.module2, REG_PCH_NUMB_12|REG_PCH_DIODE_NUMB_1); PCHSens_InitModule(&hdallas1, &pchsens.module3, REG_PCH_NUMB_13|REG_PCH_DIODE_NUMB_1); @@ -98,7 +105,33 @@ void PYModule_FirstInit(void) } - +void PYModule_ScanSensor(PCHSens_DallasBusHandle *hbus) +{ + PCHSens_SensorTypeDef sensor; + sensor.sens.hdallas = hbus->hdallas; + sensor.sens.sensROM = *(uint64_t *)(hbus->hdallas->ds_devices->DevAddr[scan_cnt]); + if (scan_cnt >= hbus->hdallas->onewire->RomCnt) + { + scan_cnt = hbus->hdallas->onewire->RomCnt; + PYModule_FillResponse(&sensor, 0); + return; + } + if(Dallas_ReadScratchpad(&sensor.sens) == HAL_OK) + { + PYModule_FillResponse(&sensor, STATUS_OK); + } + else + { + PYModule_FillResponse(&sensor, STATUS_ERR_SCAN); + } +} +void PYModule_IncrementScanSensor(void) +{ + if(MB_DATA.Coils.ScanSensors) + { + scan_cnt++; + } +} void PYModule_ReadSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_TypeDef *pchsens) { // чтение по локации @@ -111,48 +144,28 @@ void PYModule_ReadSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_TypeDef *pchsens if(Dallas_ReadScratchpad(&sensor->sens) == HAL_OK) { - MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; - MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; - MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); - MB_DATA.InRegs.Response.Status = 0x1; + PYModule_FillResponse(sensor, STATUS_OK); } else { - MB_DATA.InRegs.Response.Location = MB_DATA.HoldRegs.InitStruct.Location; - MB_DATA.InRegs.Response.Config = 0; - MB_DATA.InRegs.Response.ROM[0] = 0; - MB_DATA.InRegs.Response.ROM[1] = 0; - MB_DATA.InRegs.Response.ROM[2] = 0; - MB_DATA.InRegs.Response.ROM[3] = 0; - MB_DATA.InRegs.Response.Status = 0xFF; + PYModule_FillResponse(sensor, STATUS_ERR_READ_LOCATION); } } // чтение по ROM else if(MB_DATA.HoldRegs.InitStruct.ROM[0] | MB_DATA.HoldRegs.InitStruct.ROM[1] | // если РОМ не равен нулю MB_DATA.HoldRegs.InitStruct.ROM[2] | MB_DATA.HoldRegs.InitStruct.ROM[3] != 0) { - if(DS18B20_ReadScratchpad(hdallas->onewire, (uint8_t *)MB_DATA.HoldRegs.InitStruct.ROM, (uint8_t *)&hdallas->scratchpad) == HAL_OK) + PCHSens_SensorTypeDef sensor; + sensor.sens.hdallas = hdallas; + sensor.sens.sensROM = *(uint64_t *)MB_DATA.HoldRegs.InitStruct.ROM; + + if(Dallas_ReadScratchpad(&sensor.sens) == HAL_OK) { - MB_DATA.InRegs.Response.Location = *(uint16_t *)&hdallas->scratchpad.tHighRegister; - MB_DATA.InRegs.Response.Config = hdallas->scratchpad.ConfigRegister; - MB_DATA.InRegs.Response.ROM[0] = MB_DATA.HoldRegs.InitStruct.ROM[0]; - MB_DATA.InRegs.Response.ROM[1] = MB_DATA.HoldRegs.InitStruct.ROM[1]; - MB_DATA.InRegs.Response.ROM[2] = MB_DATA.HoldRegs.InitStruct.ROM[2]; - MB_DATA.InRegs.Response.ROM[3] = MB_DATA.HoldRegs.InitStruct.ROM[3]; - MB_DATA.InRegs.Response.Status = 0x1; + PYModule_FillResponse(&sensor, STATUS_OK); } else { - MB_DATA.InRegs.Response.Location = 0; - MB_DATA.InRegs.Response.Config = 0; - MB_DATA.InRegs.Response.ROM[0] = MB_DATA.HoldRegs.InitStruct.ROM[0]; - MB_DATA.InRegs.Response.ROM[1] = MB_DATA.HoldRegs.InitStruct.ROM[1]; - MB_DATA.InRegs.Response.ROM[2] = MB_DATA.HoldRegs.InitStruct.ROM[2]; - MB_DATA.InRegs.Response.ROM[3] = MB_DATA.HoldRegs.InitStruct.ROM[3]; - MB_DATA.InRegs.Response.Status = 0xFF; + PYModule_FillResponse(&sensor, STATUS_ERR_READ_ROM); } } @@ -176,23 +189,11 @@ void PYModule_InitSensor(PCHSens_TypeDef *pchsens) connectROM |= ((uint64_t)(MB_DATA.HoldRegs.InitStruct.ROM[3])); if(PCHSens_InitNewSensor(&hdallas1, sensor, connectROM) == HAL_OK) { - MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; - MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; - MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); - MB_DATA.InRegs.Response.Status = 0x1; + PYModule_FillResponse(sensor, STATUS_OK); } else { - MB_DATA.InRegs.Response.Location = MB_DATA.HoldRegs.InitStruct.Location; - MB_DATA.InRegs.Response.Config = 0; - MB_DATA.InRegs.Response.ROM[0] = MB_DATA.HoldRegs.InitStruct.ROM[0]; - MB_DATA.InRegs.Response.ROM[1] = MB_DATA.HoldRegs.InitStruct.ROM[1]; - MB_DATA.InRegs.Response.ROM[2] = MB_DATA.HoldRegs.InitStruct.ROM[2]; - MB_DATA.InRegs.Response.ROM[3] = MB_DATA.HoldRegs.InitStruct.ROM[3]; - MB_DATA.InRegs.Response.Status = 0xFF; + PYModule_FillResponse(sensor, STATUS_ERR_INIT); } } } @@ -208,38 +209,15 @@ void PYModule_DeInitSensor(PCHSens_TypeDef *pchsens) if(PCHSens_UndefineSensor(sensor) != HAL_OK) { - MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; - MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; - MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); - MB_DATA.InRegs.Response.Status = 0xFF; + PYModule_FillResponse(sensor, STATUS_OK); } else { - MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; - MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; - MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); - MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); - MB_DATA.InRegs.Response.Status = 1; + PYModule_FillResponse(sensor, STATUS_ERR_DEINIT); } } } -void PYModule_CheckModuleLosted(PCHSens_ModuleTypeDef *module, uint8_t *losted_flag) -{ - if(module->sens1.not_found) - *losted_flag |= 1; - if(module->sens2.not_found) - *losted_flag |= 1; - if(module->sens3.not_found) - *losted_flag |= 1; - if(module->sens4.not_found) - *losted_flag |= 1; -} void PYModule_CheckLosted(PCHSens_TypeDef *pchsens) { uint8_t losted = 0; @@ -258,6 +236,119 @@ void PYModule_CheckLosted(PCHSens_TypeDef *pchsens) MB_DATA.Coils.LostedSensors = 0; } +void PYModule_StoreModbus(PCHSens_TypeDef *pchsens) +{ + uint8_t losted = 0; + + // module1 + PYModule_StoreModuleToModbus(&pchsens->module1); + PYModule_StoreModuleToModbus(&pchsens->module2); + PYModule_StoreModuleToModbus(&pchsens->module3); + PYModule_StoreModuleToModbus(&pchsens->module4); + PYModule_StoreModuleToModbus(&pchsens->module5); + PYModule_StoreModuleToModbus(&pchsens->module6); + + MB_DATA.Coils.ConvertionDone |= 1; +} + +void PYModule_FillResponse(PCHSens_SensorTypeDef* sensor, Sensor_ResponseStatusTypeDef status) +{ + switch(status) + { + // successfull response + case STATUS_OK: + // fil with sensor data + MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; + MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; + MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); + MB_DATA.InRegs.Response.Status = status; + break; + + // error read by location + case STATUS_ERR_READ_LOCATION: + // fill only location from holdreg + MB_DATA.InRegs.Response.Location = MB_DATA.HoldRegs.InitStruct.Location; + MB_DATA.InRegs.Response.Config = 0; + MB_DATA.InRegs.Response.ROM[0] = 0; + MB_DATA.InRegs.Response.ROM[1] = 0; + MB_DATA.InRegs.Response.ROM[2] = 0; + MB_DATA.InRegs.Response.ROM[3] = 0; + MB_DATA.InRegs.Response.Status = status; + break; + + // error read by ROM + case STATUS_ERR_READ_ROM: + // fill only ROM from holdreg + MB_DATA.InRegs.Response.Location = 0; + MB_DATA.InRegs.Response.Config = 0; + MB_DATA.InRegs.Response.ROM[0] = MB_DATA.HoldRegs.InitStruct.ROM[0]; + MB_DATA.InRegs.Response.ROM[1] = MB_DATA.HoldRegs.InitStruct.ROM[1]; + MB_DATA.InRegs.Response.ROM[2] = MB_DATA.HoldRegs.InitStruct.ROM[2]; + MB_DATA.InRegs.Response.ROM[3] = MB_DATA.HoldRegs.InitStruct.ROM[3]; + MB_DATA.InRegs.Response.Status = status; + break; + + // error init + case STATUS_ERR_INIT: + // fill ROM and location from holdreg + MB_DATA.InRegs.Response.Location = MB_DATA.HoldRegs.InitStruct.Location; + MB_DATA.InRegs.Response.Config = 0; + MB_DATA.InRegs.Response.ROM[0] = MB_DATA.HoldRegs.InitStruct.ROM[0]; + MB_DATA.InRegs.Response.ROM[1] = MB_DATA.HoldRegs.InitStruct.ROM[1]; + MB_DATA.InRegs.Response.ROM[2] = MB_DATA.HoldRegs.InitStruct.ROM[2]; + MB_DATA.InRegs.Response.ROM[3] = MB_DATA.HoldRegs.InitStruct.ROM[3]; + MB_DATA.InRegs.Response.Status = status; + break; + + // error deinit + case STATUS_ERR_DEINIT: + // fill with sensor data + MB_DATA.InRegs.Response.Location = *(uint16_t *)&sensor->sens.hdallas->scratchpad.tHighRegister; + MB_DATA.InRegs.Response.Config = sensor->sens.hdallas->scratchpad.ConfigRegister; + MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); + MB_DATA.InRegs.Response.Status = status; + break; + + // error deinit + case STATUS_ERR_SCAN: + // fill with sensor data + MB_DATA.InRegs.Response.Location = 0; + MB_DATA.InRegs.Response.Config = 0; + MB_DATA.InRegs.Response.ROM[0] = __REV16((sensor->sens.sensROM) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[1] = __REV16((sensor->sens.sensROM >> 16) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[2] = __REV16((sensor->sens.sensROM >> 32) & 0xFFFF); + MB_DATA.InRegs.Response.ROM[3] = __REV16((sensor->sens.sensROM >> 48) & 0xFFFF); + MB_DATA.InRegs.Response.Status = status; + break; + + default: + MB_DATA.InRegs.Response.Location = 0; + MB_DATA.InRegs.Response.Config = 0; + MB_DATA.InRegs.Response.ROM[0] = 0; + MB_DATA.InRegs.Response.ROM[1] = 0; + MB_DATA.InRegs.Response.ROM[2] = 0; + MB_DATA.InRegs.Response.ROM[3] = 0; + MB_DATA.InRegs.Response.Status = 0; + break; + } +} +void PYModule_CheckModuleLosted(PCHSens_ModuleTypeDef *module, uint8_t *losted_flag) +{ + if(module->sens1.not_found) + *losted_flag |= 1; + if(module->sens2.not_found) + *losted_flag |= 1; + if(module->sens3.not_found) + *losted_flag |= 1; + if(module->sens4.not_found) + *losted_flag |= 1; +} void PYModule_StoreModuleToModbus(PCHSens_ModuleTypeDef *module) { uint8_t mb_location; @@ -281,17 +372,3 @@ void PYModule_StoreModuleToModbus(PCHSens_ModuleTypeDef *module) MB_DATA.InRegs.SensTemperature[mb_location] = module->sens4.sens.temperature*100; } -void PYModule_StoreModbus(PCHSens_TypeDef *pchsens) -{ - uint8_t losted = 0; - - // module1 - PYModule_StoreModuleToModbus(&pchsens->module1); - PYModule_StoreModuleToModbus(&pchsens->module2); - PYModule_StoreModuleToModbus(&pchsens->module3); - PYModule_StoreModuleToModbus(&pchsens->module4); - PYModule_StoreModuleToModbus(&pchsens->module5); - PYModule_StoreModuleToModbus(&pchsens->module6); - - MB_DATA.Coils.ConvertionDone |= 1; -} \ No newline at end of file diff --git a/Core/PY32Module/PY32module_main.h b/Core/PY32Module/PY32module_main.h index 00e9259..efdd4c3 100644 --- a/Core/PY32Module/PY32module_main.h +++ b/Core/PY32Module/PY32module_main.h @@ -14,10 +14,20 @@ #include "modbus_data.h" /* Declarations and definitions ---------------------------------------------*/ +typedef enum +{ + STATUS_OK = 0x01, + STATUS_ERR_READ_LOCATION = 0xF0, + STATUS_ERR_READ_ROM = 0x0F, + STATUS_ERR_INIT = 0xAA, + STATUS_ERR_DEINIT = 0x55, + STATUS_ERR_SCAN = 0xBB, +}Sensor_ResponseStatusTypeDef; /* Functions ---------------------------------------------------------------*/ void PYModule_main(void); void PYModule_FirstInit(void); +void PYModule_ScanSensor(PCHSens_DallasBusHandle *hbus); void PYModule_ReadSensor(DALLAS_HandleTypeDef *hdallas, PCHSens_TypeDef *pchsens); void PYModule_InitSensor(PCHSens_TypeDef *pchsens); void PYModule_DeInitSensor(PCHSens_TypeDef *pchsens); diff --git a/Core/PY32Module/pch_sensors.c b/Core/PY32Module/pch_sensors.c index 2300ef6..5cdafe6 100644 --- a/Core/PY32Module/pch_sensors.c +++ b/Core/PY32Module/pch_sensors.c @@ -9,7 +9,6 @@ #include "pch_sensors.h" #include "modbus_data.h" -PCHSens_DallasBusHandle DallasBus; /* Declarations and definitions --------------------------------------------*/ diff --git a/Core/PY32Module/pch_sensors.h b/Core/PY32Module/pch_sensors.h index c9bc7d2..992be13 100644 --- a/Core/PY32Module/pch_sensors.h +++ b/Core/PY32Module/pch_sensors.h @@ -152,7 +152,6 @@ typedef struct DALLAS_HandleTypeDef *hdallas; uint8_t UnknownCnt; }PCHSens_DallasBusHandle; -extern PCHSens_DallasBusHandle DallasBus; typedef struct diff --git a/MDK-ARM/PY32Dallas.uvoptx b/MDK-ARM/PY32Dallas.uvoptx index 40c61dc..8d49872 100644 --- a/MDK-ARM/PY32Dallas.uvoptx +++ b/MDK-ARM/PY32Dallas.uvoptx @@ -153,7 +153,40 @@ (105=-1,-1,-1,-1,0) - + + + 0 + 0 + 37 + 1 +
134235612
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\Core\PY32Module\PY32module_main.c + + \\Project\../Core/PY32Module/PY32module_main.c\37 +
+ + 1 + 0 + 48 + 1 +
134235662
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\Core\PY32Module\PY32module_main.c + + \\Project\../Core/PY32Module/PY32module_main.c\48 +
+
0 @@ -240,6 +273,21 @@ 1 DS + + 17 + 1 + \\Project\../Core/PY32Module/PY32module_main.c\pchsens.module3 + + + 18 + 1 + \\Project\../Core/PY32Module/PY32module_main.c\pchsens.module4 + + + 19 + 1 + scan_cnt + @@ -250,7 +298,7 @@ 1 2 - DS + DS,0x10 2