Переструктурирован проект, перенесена ветка тестирования 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

@@ -18,6 +18,7 @@
/* Declarations and definitions --------------------------------------------*/
DALLAS_HandleTypeDef hdallas1;
/* Functions ---------------------------------------------------------------*/
/**
@@ -25,7 +26,7 @@
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
@@ -40,7 +41,7 @@ HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor)
if(DS18B20_Search(&DS, &OW) != HAL_OK)
return HAL_ERROR;
if(sensor->Init.init_func(sensor->onewire, sensor) != HAL_OK)
if(sensor->Init.init_func(sensor->hdallas->onewire, sensor) != HAL_OK)
return HAL_ERROR;
return HAL_OK;
@@ -58,18 +59,18 @@ HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor)
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_AddNewSensors(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_AddNewSensors(DALLAS_HandleTypeDef *hdallas, DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
sensor->onewire = onewire;
sensor->hdallas = hdallas;
result = sensor->Init.init_func(onewire, sensor);
result = sensor->Init.init_func(hdallas, sensor);
return result;
}
@@ -82,31 +83,31 @@ HAL_StatusTypeDef Dallas_AddNewSensors(OneWire_t *onewire, DALLAS_HandleTypeDef
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_SensorInitByROM(DALLAS_HandleTypeDef *hdallas, DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
uint8_t ROM[8] = {0};
ROM[0] = (sensor->Init.ROM >> (7*8)) & 0xFF;
ROM[1] = (sensor->Init.ROM >> (6*8)) & 0xFF;
ROM[2] = (sensor->Init.ROM >> (5*8)) & 0xFF;
ROM[3] = (sensor->Init.ROM >> (4*8)) & 0xFF;
ROM[4] = (sensor->Init.ROM >> (3*8)) & 0xFF;
ROM[5] = (sensor->Init.ROM >> (2*8)) & 0xFF;
ROM[6] = (sensor->Init.ROM >> (1*8)) & 0xFF;
ROM[7] = (sensor->Init.ROM >> (0*8)) & 0xFF;
ROM[0] = (sensor->Init.InitParam >> (7*8)) & 0xFF;
ROM[1] = (sensor->Init.InitParam >> (6*8)) & 0xFF;
ROM[2] = (sensor->Init.InitParam >> (5*8)) & 0xFF;
ROM[3] = (sensor->Init.InitParam >> (4*8)) & 0xFF;
ROM[4] = (sensor->Init.InitParam >> (3*8)) & 0xFF;
ROM[5] = (sensor->Init.InitParam >> (2*8)) & 0xFF;
ROM[6] = (sensor->Init.InitParam >> (1*8)) & 0xFF;
ROM[7] = (sensor->Init.InitParam >> (0*8)) & 0xFF;
if(DS18B20_IsValidAddress(ROM) != HAL_OK)
return HAL_ERROR;
uint8_t comparebytes = DALLAS_ROM_SIZE;
int ROM_ind = 0;
for(int i = 0; i < onewire->RomCnt; i++)
for(int i = 0; i < hdallas->onewire->RomCnt; i++)
{
comparebytes = DALLAS_ROM_SIZE;
for(int rom_byte = 0; rom_byte < DALLAS_ROM_SIZE; rom_byte++)
@@ -125,7 +126,7 @@ HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDe
if(comparebytes == 0)
{
result = Dallas_SensorInit(onewire, sensor, &DS.DevAddr[ROM_ind]);
result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[ROM_ind]);
return result;
}
else
@@ -144,35 +145,34 @@ HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDe
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_SensorInitByUserBytes(DALLAS_HandleTypeDef *hdallas, DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
uint8_t UserByte1 = sensor->Init.UserBytes12 & 0xFF;
uint8_t UserByte2 = sensor->Init.UserBytes12 >> 8;
uint8_t UserByte3 = sensor->Init.UserBytes34 & 0xFF;
uint8_t UserByte4 = sensor->Init.UserBytes34 >> 8;
uint8_t UserByte1 = sensor->Init.InitParam & 0xFF;
uint8_t UserByte2 = sensor->Init.InitParam >> 8;
uint8_t UserByte3 = (sensor->Init.InitParam >> 16) & 0xFF;
uint8_t UserByte4 = (sensor->Init.InitParam >> 16) >> 8;
uint8_t UserByte12Cmp = 0;
uint8_t UserByte34Cmp = 0;
DALLAS_ScratchpadTypeDef scratchpad;
for(int i = 0; i < onewire->RomCnt; i++)
for(int i = 0; i < hdallas->onewire->RomCnt; i++)
{
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = DS18B20_ReadScratchpad(onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&scratchpad);
result = DS18B20_ReadScratchpad(hdallas->onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&hdallas->scratchpad);
if (result != HAL_OK)
return result;
/* Ñðàâíåíèå UserByte1 è UserByte2, åñëè îíè íå ðàâíû íóëþ */
if(sensor->Init.UserBytes12 != NULL)
if((sensor->Init.InitParam & 0xFFFF) != NULL)
{
if( (scratchpad.tHighRegister == UserByte1) &&
(scratchpad.tLowRegister == UserByte2))
if( (hdallas->scratchpad.tHighRegister == UserByte1) &&
(hdallas->scratchpad.tLowRegister == UserByte2))
{
UserByte12Cmp = 1;
}
@@ -182,10 +182,10 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle
UserByte12Cmp = 1;
}
/* Ñðàâíåíèå UserByte3 è UserByte4, åñëè îíè íå ðàâíû íóëþ */
if(sensor->Init.UserBytes34 != NULL)
if((sensor->Init.InitParam & 0xFFFF0000) != NULL)
{
if( (scratchpad.UserByte3 == UserByte3) &&
(scratchpad.UserByte4 == UserByte4))
if( (hdallas->scratchpad.UserByte3 == UserByte3) &&
(hdallas->scratchpad.UserByte4 == UserByte4))
{
UserByte34Cmp = 1;
}
@@ -199,13 +199,12 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle
{
// sensor->isInitialized = 1;
// sensor->Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
result = Dallas_SensorInit(onewire, sensor, &DS.DevAddr[i]);
result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[i]);
return result;
}
}
sensor->sensROM = 0;
memset(&sensor->scratchpad, 0, sizeof(DALLAS_ScratchpadTypeDef));
/* Âîçâðàùàåì îøèáêó åñëè íå íàøëè */
return HAL_ERROR;
}
@@ -218,20 +217,16 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle
* @details Ïîðÿäêîâûé íîìåð äàò÷èêà â ñïèñêå íàéäåííûõ.
* Ò.å. êàêèì ïî ñ÷åòó ýòîò äàò÷èê áûë íàéäåí
*/
HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_SensorInitByInd(DALLAS_HandleTypeDef *hdallas, DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
if(sensor == NULL)
return HAL_ERROR;
// sensor->onewire = onewire;
// sensor->sensROM = &DS.DevAddr[sensor->Init.SensInd];
// sensor->Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByInd;
result = Dallas_SensorInit(onewire, sensor, &DS.DevAddr[sensor->Init.SensInd]);
result = Dallas_SensorInit(hdallas, sensor, &DS.DevAddr[sensor->Init.InitParam]);
return result;
}
@@ -243,27 +238,27 @@ HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDe
* @param ROM ROM äàò÷èêà, êîòîðûé íàäî èíèöèàëèçèðîâàòü
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_SensorInit(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t (*ROM)[DALLAS_ROM_SIZE])
HAL_StatusTypeDef Dallas_SensorInit(DALLAS_HandleTypeDef *hdallas, DALLAS_SensorHandleTypeDef *sensor, uint8_t (*ROM)[DALLAS_ROM_SIZE])
{
HAL_StatusTypeDef result;
if(sensor == NULL)
return HAL_ERROR;
if(onewire == 0)
if(hdallas == 0)
return HAL_ERROR;
sensor->onewire = onewire;
sensor->hdallas = hdallas;
sensor->sensROM = 0;
sensor->sensROM = *(uint64_t *)(ROM);
// for(int i = 0; i < DALLAS_ROM_SIZE; i++)
// sensor->sensROM |= ((uint64_t)(*ROM)[i] << (56 - 8*i));
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->scratchpad);
result = DS18B20_ReadScratchpad(hdallas->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&hdallas->scratchpad);
if (result == HAL_OK)
{
/* Óñòàíîâêà ðàçðåøåíèÿ */
result = DS18B20_SetResolution(onewire, (uint8_t *)ROM, sensor->Init.Resolution);
result = DS18B20_SetResolution(hdallas->onewire, (uint8_t *)ROM, sensor->Init.Resolution);
if (result == HAL_OK)
{
sensor->isInitialized = 1;
@@ -289,14 +284,14 @@ HAL_StatusTypeDef Dallas_SensorInit(OneWire_t *onewire, DALLAS_HandleTypeDef *se
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_SensorDeInit(DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_SensorDeInit(DALLAS_SensorHandleTypeDef *sensor)
{
if(sensor == NULL)
return HAL_ERROR;
DALLAS_InitStructTypeDef initbuff = sensor->Init;
memset(sensor, 0, sizeof(DALLAS_HandleTypeDef));
memset(sensor, 0, sizeof(DALLAS_SensorHandleTypeDef));
sensor->Init = initbuff;
@@ -310,16 +305,16 @@ HAL_StatusTypeDef Dallas_SensorDeInit(DALLAS_HandleTypeDef *sensor)
* @param dallas_delay_ms Âðåìÿ îæèäàíèÿ îêîí÷àíèÿ êîíâåðñèè
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitConvertionTypeDef waitCondition, uint8_t dallas_delay_ms)
HAL_StatusTypeDef Dallas_StartConvertTAll(DALLAS_HandleTypeDef *hdallas, DALLAS_WaitConvertionTypeDef waitCondition, uint8_t dallas_delay_ms)
{
HAL_StatusTypeDef result;
uint8_t rxDummyData;
if(onewire == NULL)
if(hdallas == NULL)
return HAL_ERROR;
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
result = DS18B20_StartConvTAll(onewire);
result = DS18B20_StartConvTAll(hdallas->onewire);
if(result != HAL_OK)
{
return result;
@@ -331,7 +326,7 @@ HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitConvert
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
if (waitCondition == DALLAS_WAIT_BUS)
{
result = DS18B20_WaitForEndConvertion(onewire);
result = DS18B20_WaitForEndConvertion(hdallas->onewire);
return result;
}
@@ -374,7 +369,7 @@ HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitConvert
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConvertionTypeDef waitCondition)
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_SensorHandleTypeDef *sensor, DALLAS_WaitConvertionTypeDef waitCondition)
{
HAL_StatusTypeDef result;
uint8_t rxDummyData;
@@ -390,7 +385,7 @@ HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConve
return result;
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
result = DS18B20_StartConvT(sensor->onewire, (uint8_t *)&sensor->sensROM);
result = DS18B20_StartConvT(sensor->hdallas->onewire, (uint8_t *)&sensor->sensROM);
if(result != HAL_OK)
{
return result;
@@ -399,7 +394,7 @@ HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConve
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
if (waitCondition == DALLAS_WAIT_BUS)
{
result = DS18B20_WaitForEndConvertion(sensor->onewire);
result = DS18B20_WaitForEndConvertion(sensor->hdallas->onewire);
if(result == HAL_TIMEOUT)
{
sensor->f.timeout_convertion_cnt++;
@@ -412,7 +407,7 @@ HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConve
{
uint32_t delayValueMs = 0;
switch (sensor->scratchpad.ConfigRegister)
switch (sensor->hdallas->scratchpad.ConfigRegister)
{
case DALLAS_CONFIG_9_BITS:
delayValueMs = DALLAS_DELAY_MS_9_BITS;
@@ -452,7 +447,7 @@ HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConve
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
@@ -467,7 +462,7 @@ HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor)
return result;
result = DS18B20_CalcTemperature(sensor->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->scratchpad, &sensor->temperature);
result = DS18B20_CalcTemperature(sensor->hdallas->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->hdallas->scratchpad, &sensor->temperature);
if (result != HAL_OK)
{
@@ -483,14 +478,14 @@ HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor)
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
* @retval HAL Status
*/
HAL_StatusTypeDef Dallas_IsConnected(DALLAS_HandleTypeDef *sensor)
HAL_StatusTypeDef Dallas_IsConnected(DALLAS_SensorHandleTypeDef *sensor)
{
HAL_StatusTypeDef result;
if(sensor->isInitialized == 0)
return HAL_ERROR;
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->scratchpad);
result = DS18B20_ReadScratchpad(sensor->hdallas->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->hdallas->scratchpad);
if (result == HAL_OK)
{
@@ -522,7 +517,7 @@ HAL_StatusTypeDef Dallas_IsConnected(DALLAS_HandleTypeDef *sensor)
* @retval HAL Status
* @details ñòàðøèé áàéò - UserByte4/UserByte2, ìëàäøèé - UserByte3/UserByte1.
*/
HAL_StatusTypeDef Dallas_WriteUserBytes(DALLAS_HandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34, uint8_t UserBytesMask)
HAL_StatusTypeDef Dallas_WriteUserBytes(DALLAS_SensorHandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34, uint8_t UserBytesMask)
{
HAL_StatusTypeDef result;
@@ -534,13 +529,13 @@ HAL_StatusTypeDef Dallas_WriteUserBytes(DALLAS_HandleTypeDef *sensor, uint16_t U
if (result != HAL_OK)
return result;
result = DS18B20_WriteUserBytes(sensor->onewire, (uint8_t *)&sensor->sensROM, UserBytes12, UserBytes34, UserBytesMask);
result = DS18B20_WriteUserBytes(sensor->hdallas->onewire, (uint8_t *)&sensor->sensROM, UserBytes12, UserBytes34, UserBytesMask);
if (result != HAL_OK)
{
sensor->f.other_err_cnt++;
sensor->f.write_err_cnt++;
return result;
}
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->scratchpad);
result = DS18B20_ReadScratchpad(sensor->hdallas->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->hdallas->scratchpad);
if (result != HAL_OK)
{
return result;