Сделан поиск потерянных датчиков по функции в структуре (по тому же алгоритму, которому они были найдены)
696 lines
20 KiB
C
696 lines
20 KiB
C
/**
|
|
******************************************************************************
|
|
* @file dallas_tools.c
|
|
* @brief Äðàéâåð äëÿ ðàáîòû ñ äàò÷èêàìè òåìïåðàòóðû DS18B20
|
|
* @author MicroTechnics (microtechnics.ru)
|
|
******************************************************************************
|
|
@details
|
|
Ýòîò ôàéë ñîäåðæèò ðåàëèçàöèþ ôóíêöèé äëÿ ðàáîòû ñ äàò÷èêîì DALLAS_HandleTypeDef
|
|
÷åðåç èíòåðôåéñ 1-Wire. Îí ïðåäîñòàâëÿåò ôóíêöèè äëÿ ÷òåíèÿ è çàïèñè
|
|
êîíôèãóðàöèè, âûïîëíåíèÿ èçìåðåíèé è îáðàáîòêè ïîëó÷åííûõ äàííûõ.
|
|
*****************************************************************************/
|
|
|
|
|
|
/* Includes ----------------------------------------------------------------*/
|
|
|
|
#include "dallas_tools.h"
|
|
#include "string.h"
|
|
|
|
|
|
/* Declarations and definitions --------------------------------------------*/
|
|
|
|
struct
|
|
{
|
|
DALLAS_HandleTypeDef outdoor;
|
|
DALLAS_HandleTypeDef indoor;
|
|
DALLAS_HandleTypeDef bathroom;
|
|
DALLAS_HandleTypeDef kitchen;
|
|
DALLAS_HandleTypeDef big_room;
|
|
DALLAS_HandleTypeDef small_room;
|
|
DALLAS_HandleTypeDef living_room;
|
|
DALLAS_HandleTypeDef basement;
|
|
}AllSens;
|
|
|
|
/* Functions ---------------------------------------------------------------*/
|
|
|
|
void Dallas_ReadAll(void)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
result = Dallas_StartConvertTAll(&OW, DALLAS_WAIT_BUS, 0);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.outdoor) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.outdoor);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.indoor) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.indoor);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.bathroom) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.bathroom);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.kitchen) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.kitchen);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.big_room) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.big_room);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.small_room) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.small_room);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.living_room) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.living_room);
|
|
|
|
if(Dallas_ReadTemperature(&AllSens.basement) == HAL_BUSY)
|
|
Dallas_ReplaceLostedSensor(&AllSens.basement);
|
|
|
|
}
|
|
|
|
void Dallas_FirstInit(void)
|
|
{
|
|
OW.DataPin = DS_Pin;
|
|
OW.DataPort = DS_GPIO_Port;
|
|
DS.Resolution = DS18B20_RESOLUTION_9BITS;
|
|
|
|
OneWire_Init(&OW);
|
|
DS18B20_Search(&DS, &OW);
|
|
AllSens.outdoor.Init.ROM = 0x28C60C61060000DC;
|
|
AllSens.indoor.Init.ROM = 0x283E4861060000F9;
|
|
AllSens.bathroom.Init.ROM = 0x28876D60060000CD;
|
|
|
|
AllSens.outdoor.Init.SensInd = 0;
|
|
AllSens.indoor.Init.SensInd = 1;
|
|
AllSens.bathroom.Init.SensInd = 2;
|
|
AllSens.kitchen.Init.SensInd = 3;
|
|
AllSens.big_room.Init.SensInd = 4;
|
|
AllSens.small_room.Init.SensInd = 5;
|
|
AllSens.living_room.Init.SensInd = 6;
|
|
AllSens.basement.Init.SensInd = 7;
|
|
|
|
AllSens.outdoor.Init.UserBytes12 = 1;
|
|
AllSens.indoor.Init.UserBytes12 = 2;
|
|
AllSens.bathroom.Init.UserBytes12 = 3;
|
|
AllSens.kitchen.Init.UserBytes12 = 4;
|
|
AllSens.big_room.Init.UserBytes12 = 5;
|
|
AllSens.small_room.Init.UserBytes12 = 6;
|
|
AllSens.living_room.Init.UserBytes12 = 7;
|
|
AllSens.basement.Init.UserBytes12 = 8;
|
|
|
|
|
|
|
|
AllSens.outdoor.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.indoor.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.bathroom.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.kitchen.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.big_room.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.small_room.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.living_room.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
AllSens.basement.Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
|
|
Dallas_AddNewSensors(&OW, &AllSens.outdoor);
|
|
Dallas_AddNewSensors(&OW, &AllSens.indoor);
|
|
Dallas_AddNewSensors(&OW, &AllSens.bathroom);
|
|
|
|
//
|
|
// 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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @brief Ôóíêöèÿ äëÿ íàõîæäåíèÿ íîâîãî äàò÷èêà íà ìåñòî ïîòåðÿííîãî
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
|
|
if(AllSens.outdoor.isLost)
|
|
{
|
|
if(DS18B20_Search(&DS, &OW) != HAL_OK)
|
|
return HAL_ERROR;
|
|
|
|
if(sensor->Init.init_func(sensor->onewire, sensor) != HAL_OK)
|
|
return HAL_ERROR;
|
|
}
|
|
else
|
|
{
|
|
/*unknown sensor*/
|
|
return HAL_ERROR;
|
|
}
|
|
return HAL_OK;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Ôóíêöèÿ äëÿ äîáàâëåíèÿ íîâîãî äàò÷èêà â ñòðóêòóðó
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_AddNewSensors(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(onewire == NULL)
|
|
return HAL_ERROR;
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
|
|
sensor->onewire = onewire;
|
|
|
|
result = sensor->Init.init_func(onewire, sensor);
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ROM
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(onewire == 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) & 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 < DS18B20_DEVICE_AMOUNT; i++)
|
|
{
|
|
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])
|
|
comparebytes--;
|
|
}
|
|
if(comparebytes == 0)
|
|
{
|
|
ROM_ind = i;
|
|
break;
|
|
}
|
|
}
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
|
|
if(comparebytes == 0)
|
|
{
|
|
// result = Dallas_IsConnected(sensor);
|
|
// if(result != HAL_OK)
|
|
// return result;
|
|
//
|
|
// sensor->isInitialized = 1;
|
|
// sensor->Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByROM;
|
|
result = Dallas_SensorInit(onewire, sensor, &DS.DevAddr[ROM_ind]);
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ïîëüçîâàòåëüñêèì áàéòàì
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param UserBytes34 Ïîëüçîâàòåëüñêèå áàéòû 3 è 4, NULL äëÿ èãíîðà
|
|
* @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2, NULL äëÿ èãíîðà
|
|
* @retval HAL Status
|
|
* @details ñòàðøèé áàéò - UserByte4/UserByte2, ìëàäøèé - UserByte3/UserByte1.
|
|
*/
|
|
HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(onewire == 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 UserByte12Cmp = 0;
|
|
uint8_t UserByte34Cmp = 0;
|
|
|
|
DALLAS_ScratchpadTypeDef scratchpad;
|
|
for(int i = 0; i < DS18B20_DEVICE_AMOUNT; i++)
|
|
{
|
|
|
|
for(int rom_ind = 0; rom_ind < DALLAS_ROM_SIZE; rom_ind++)
|
|
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
result = DS18B20_ReadScratchpad(onewire, (uint8_t *)&DS.DevAddr[i], (uint8_t *)&scratchpad);
|
|
if (result != HAL_OK)
|
|
return result;
|
|
|
|
|
|
/* Ñðàâíåíèå UserByte1 è UserByte2, åñëè âûáðàíî */
|
|
if(sensor->Init.UserBytes12 != NULL)
|
|
{
|
|
if( (scratchpad.tHighRegister == UserByte1) &&
|
|
(scratchpad.tLowRegister == UserByte2))
|
|
{
|
|
UserByte12Cmp = 1;
|
|
}
|
|
}/* Åñëè ðàâíåíèå UserByte1 è UserByte2 íå âûáðàíî, òî ñ÷èòàåì ÷òî îíè îäèíàêîâûå */
|
|
else
|
|
{
|
|
UserByte12Cmp = 1;
|
|
}
|
|
/* Ñðàâíåíèå UserByte3 è UserByte4, åñëè âûáðàíî */
|
|
if(sensor->Init.UserBytes34 != NULL)
|
|
{
|
|
if( (scratchpad.UserByte3 == UserByte3) &&
|
|
(scratchpad.UserByte4 == UserByte4))
|
|
{
|
|
UserByte34Cmp = 1;
|
|
}
|
|
}/* Åñëè ðàâíåíèå UserByte3 è UserByte4 íå âûáðàíî, òî ñ÷èòàåì ÷òî îíè îäèíàêîâûå */
|
|
else
|
|
{
|
|
UserByte34Cmp = 1;
|
|
}
|
|
/* Åñëè íàøëè íóæíûé äàò÷èê - çàâåðøàåì ïîèñê */
|
|
if(UserByte12Cmp && UserByte34Cmp)
|
|
{
|
|
// sensor->isInitialized = 1;
|
|
// sensor->Init.init_func = (HAL_StatusTypeDef (*)())Dallas_SensorInitByUserBytes;
|
|
result = Dallas_SensorInit(onewire, sensor, &DS.DevAddr[i]);
|
|
return result;
|
|
}
|
|
}
|
|
Dallas_SensorDeInit(sensor);
|
|
/* Âîçâðàùàåì îøèáêó åñëè íå íàøëè */
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/**
|
|
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî èíäåêó
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
|
|
* @retval HAL Status
|
|
* @details Èíäåêñ - ýòî ïîðÿäêîâûé íîìåð äàò÷èêà â ñïèñêå íàéäåííûõ.
|
|
* Ò.å. êàêèì ïî ñ÷åòó ýòîò äàò÷èê áûë íàéäåí
|
|
*/
|
|
HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(onewire == 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]);
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief Èíèöèàëèçèðóåò äàò÷èê äëÿ ðàáîòû
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
|
|
* @retval HAL Status
|
|
* @details Èíäåêñ - ýòî ïîðÿäêîâûé íîìåð äàò÷èêà â ñïèñêå íàéäåííûõ.
|
|
* Ò.å. êàêèì ïî ñ÷åòó ýòîò äàò÷èê áûë íàéäåí
|
|
*/
|
|
HAL_StatusTypeDef Dallas_SensorInit(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t (*ROM)[DALLAS_ROM_SIZE])
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
if(onewire == 0)
|
|
return HAL_ERROR;
|
|
|
|
sensor->onewire = onewire;
|
|
for(int i = 0; i < DALLAS_ROM_SIZE; i++)
|
|
sensor->sensROM[i] = (*ROM)[i];
|
|
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
|
if (result == HAL_OK)
|
|
{
|
|
/* Óñòàíîâêà ðàçðåøåíèÿ */
|
|
result = DS18B20_SetResolution(onewire, (uint8_t *)ROM, sensor->Init.Resolution);
|
|
if (result == HAL_OK)
|
|
{
|
|
sensor->isInitialized = 1;
|
|
return HAL_OK;
|
|
}
|
|
else
|
|
{
|
|
sensor->isInitialized = 0;
|
|
Dallas_SensorDeInit(sensor);
|
|
return result;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sensor->isInitialized = 0;
|
|
Dallas_SensorDeInit(sensor);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Äåèíèöèàëèçèðóåò ïîëíîñòüþ ñòðóêòóðó äàò÷èêà
|
|
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
|
|
* @retval HAL Status
|
|
* @details Èíäåêñ - ýòî ïîðÿäêîâûé íîìåð äàò÷èêà â ñïèñêå íàéäåííûõ.
|
|
* Ò.å. êàêèì ïî ñ÷åòó ýòîò äàò÷èê áûë íàéäåí
|
|
*/
|
|
HAL_StatusTypeDef Dallas_SensorDeInit(DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
|
|
DALLAS_InitStructTypeDef initbuff = sensor->Init;
|
|
|
|
memset(sensor, 0, sizeof(DALLAS_HandleTypeDef));
|
|
|
|
sensor->Init = initbuff;
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Çàïóñêàåò èçìåðåíèå òåìïåðàòóðû íà âñåõ äàò÷èêàõ
|
|
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitCondition waitCondition, uint8_t dallas_delay_ms)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
uint8_t rxDummyData;
|
|
|
|
if(onewire == NULL)
|
|
return HAL_ERROR;
|
|
|
|
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
|
|
result = DS18B20_StartConvTAll(onewire);
|
|
if(result != HAL_OK)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
|
|
if (waitCondition == DALLAS_WAIT_BUS)
|
|
{
|
|
result = DS18B20_WaitForEndConvertion(onewire);
|
|
return result;
|
|
}
|
|
|
|
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì çàäåðæêè
|
|
if (waitCondition == DALLAS_WAIT_DELAY)
|
|
{
|
|
uint32_t delayValueMs = 0;
|
|
|
|
switch (dallas_delay_ms)
|
|
{
|
|
case DALLAS_CONFIG_9_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_9_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_10_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_10_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_11_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_11_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_12_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_12_BITS;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
HAL_Delay(delayValueMs);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief Èçìåðÿåò òåìïåðàòóðó íà äàò÷èêå
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitCondition waitCondition)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
uint8_t rxDummyData;
|
|
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
if(sensor->isInitialized == 0)
|
|
return HAL_ERROR;
|
|
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
result = Dallas_IsConnected(sensor);
|
|
if (result != HAL_OK)
|
|
return result;
|
|
|
|
// Îòïðàâêà êîìàíäû íà÷àëà ïðåîáðàçîâàíèÿ òåìïåðàòóðû
|
|
result = DS18B20_StartConvT(sensor->onewire, (uint8_t *)sensor->sensROM);
|
|
if(result != HAL_OK)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
|
|
if (waitCondition == DALLAS_WAIT_BUS)
|
|
{
|
|
result = DS18B20_WaitForEndConvertion(sensor->onewire);
|
|
if(result == HAL_TIMEOUT)
|
|
{
|
|
sensor->f.timeout_convertion_cnt++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì çàäåðæêè
|
|
if (waitCondition == DALLAS_WAIT_DELAY)
|
|
{
|
|
uint32_t delayValueMs = 0;
|
|
|
|
switch (sensor->scratchpad.ConfigRegister)
|
|
{
|
|
case DALLAS_CONFIG_9_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_9_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_10_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_10_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_11_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_11_BITS;
|
|
break;
|
|
|
|
case DALLAS_CONFIG_12_BITS:
|
|
delayValueMs = DALLAS_DELAY_MS_12_BITS;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
HAL_Delay(delayValueMs);
|
|
}
|
|
|
|
/* Íå ñ÷èòûâàåì òåìïåðàòóðó, åñëè íå âûáðàíî îæèäàíèå îêîí÷àíèÿ ïðåîáðàçîâàíèÿ */
|
|
if(waitCondition != DALLAS_WAIT_NONE)
|
|
{
|
|
result = Dallas_ReadTemperature(sensor);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief ×èòàåò ñîäåðæèìîå ïàìÿòè (scratchpad) äàò÷èêà DALLAS_HandleTypeDef
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(sensor == NULL)
|
|
return HAL_ERROR;
|
|
if(sensor->isInitialized == 0)
|
|
return HAL_ERROR;
|
|
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
result = Dallas_IsConnected(sensor);
|
|
if (result != HAL_OK)
|
|
return result;
|
|
|
|
|
|
result = DS18B20_CalcTemperature(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad, &sensor->temperature);
|
|
|
|
if (result != HAL_OK)
|
|
{
|
|
sensor->f.read_temperature_err_cnt++;
|
|
return result;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Âûïîëíÿåò êîìàíäó èíèöèàëèçàöèè DALLAS_HandleTypeDef
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @retval HAL Status
|
|
*/
|
|
HAL_StatusTypeDef Dallas_IsConnected(DALLAS_HandleTypeDef *sensor)
|
|
{
|
|
HAL_StatusTypeDef result;
|
|
|
|
if(sensor->isInitialized == 0)
|
|
return HAL_ERROR;
|
|
|
|
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
|
|
|
if (result == HAL_OK)
|
|
{
|
|
sensor->isConnected = 1;
|
|
sensor->isLost = 0;
|
|
return HAL_OK;
|
|
}
|
|
else
|
|
{
|
|
if(sensor->isConnected == 1)
|
|
{
|
|
sensor->f.disconnect_cnt++;
|
|
sensor->isLost = 1;
|
|
}
|
|
sensor->isConnected = 0;
|
|
return HAL_BUSY; // èñïîëüçóþ busy, ÷òîáû îòëè÷àòü ñèòóàöèþ îò HAL_ERROR
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Çàïèñûâàåò ïîëüçîâàòåëüñêèå áàéòû
|
|
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
* @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2
|
|
* @param UserBytes34 Ïîëüçîâàòåëüñêèå áàéòû 3 è 4
|
|
* @param UserBytesMask Ìàñêà, êàêèå áàéòû çàïèñûâàòü, à êàêèå íåò
|
|
* @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 result;
|
|
|
|
if(sensor->isInitialized == 0)
|
|
return HAL_ERROR;
|
|
|
|
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
|
result = Dallas_IsConnected(sensor);
|
|
if (result != HAL_OK)
|
|
return result;
|
|
|
|
result = DS18B20_WriteUserBytes(sensor->onewire, (uint8_t *)sensor->sensROM, UserBytes12, UserBytes34, UserBytesMask);
|
|
if (result != HAL_OK)
|
|
{
|
|
sensor->f.read_temperature_err_cnt++;
|
|
return result;
|
|
}
|
|
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
|
if (result != HAL_OK)
|
|
{
|
|
sensor->f.read_temperature_err_cnt++;
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
///**
|
|
// * @brief ×èòàåò óíèêàëüíûé ROM-êîä äàò÷èêà DALLAS_HandleTypeDef
|
|
// * @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
|
// * @retval HAL Status
|
|
// */
|
|
//HAL_StatusTypeDef Dallas_ReadRom(DALLAS_HandleTypeDef *sensor)
|
|
//{
|
|
// HAL_StatusTypeDef result = HAL_OK;
|
|
// uint8_t rxData[DALLAS_READ_ROM_RX_BYTES_NUM];
|
|
//
|
|
// DS18B20_ReadScratchpad(sensor->onewire, sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
|
//
|
|
//
|
|
// if (result == HAL_OK)
|
|
// {
|
|
// for (uint8_t i = 0; i < DALLAS_SERIAL_NUMBER_LEN_BYTES; i++)
|
|
// {
|
|
// sensor->sensROM[i] = rxData[DALLAS_SERIAL_NUMBER_OFFSET_BYTES + i];
|
|
// }
|
|
// }
|
|
|
|
// return result;
|
|
//}
|