запустил измерения температуры модуля пч на py32f002b
This commit is contained in:
552
Core/Dallas/dallas_tools.c
Normal file
552
Core/Dallas/dallas_tools.c
Normal file
@@ -0,0 +1,552 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file dallas_tools.c
|
||||
* @brief Äðàéâåð äëÿ ðàáîòû ñ äàò÷èêàìè òåìïåðàòóðû DS18B20
|
||||
* @author MicroTechnics (microtechnics.ru)
|
||||
******************************************************************************
|
||||
@details
|
||||
Ýòîò ôàéë ñîäåðæèò ðåàëèçàöèþ ôóíêöèé äëÿ ðàáîòû ñ äàò÷èêîì DS18B20
|
||||
÷åðåç èíòåðôåéñ 1-Wire. Îí ïðåäîñòàâëÿåò ôóíêöèè äëÿ ÷òåíèÿ è çàïèñè
|
||||
êîíôèãóðàöèè, âûïîëíåíèÿ èçìåðåíèé è îáðàáîòêè ïîëó÷åííûõ äàííûõ.
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
/* Includes ----------------------------------------------------------------*/
|
||||
|
||||
#include "dallas_tools.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
/* Declarations and definitions --------------------------------------------*/
|
||||
|
||||
/* Functions ---------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Ôóíêöèÿ äëÿ íàõîæäåíèÿ íîâîãî äàò÷èêà íà ìåñòî ïîòåðÿííîãî
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor)
|
||||
{
|
||||
HAL_StatusTypeDef result;
|
||||
|
||||
|
||||
if(sensor == NULL)
|
||||
return HAL_ERROR;
|
||||
|
||||
result = Dallas_IsConnected(sensor);
|
||||
|
||||
if(sensor->isLost)
|
||||
{
|
||||
if(DS18B20_Search(&DS, &OW) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
if(sensor->Init.init_func(sensor->onewire, sensor) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return HAL_BUSY; // äàò÷èê íå ïîòåðÿí
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 >> (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++)
|
||||
{
|
||||
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_SensorInit(onewire, sensor, &DS.DevAddr[ROM_ind]);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ïîëüçîâàòåëüñêèì áàéòàì
|
||||
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @retval HAL Status
|
||||
*/
|
||||
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 < onewire->RomCnt; i++)
|
||||
{
|
||||
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
sensor->sensROM = 0;
|
||||
memset(&sensor->scratchpad, 0, sizeof(DALLAS_ScratchpadTypeDef));
|
||||
/* Âîçâðàùàåì îøèáêó åñëè íå íàøëè */
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ïîðÿäêîâîìó íîìåðó
|
||||
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @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 ROM ROM äàò÷èêà, êîòîðûé íàäî èíèöèàëèçèðîâàòü
|
||||
* @retval HAL Status
|
||||
*/
|
||||
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;
|
||||
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);
|
||||
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;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sensor->isInitialized = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Äåèíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà
|
||||
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà
|
||||
* @param sens_ind Ïîðÿäêîâûé íîìåð äàò÷èêà â ñòðóêòóðå
|
||||
* @retval HAL Status
|
||||
*/
|
||||
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 onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
|
||||
* @param waitCondition Óñëîâèå îæèäàíèÿ çàâåðøåíèÿ ïðåîáðàçîâàíèÿ
|
||||
* @param dallas_delay_ms Âðåìÿ îæèäàíèÿ îêîí÷àíèÿ êîíâåðñèè
|
||||
* @retval HAL Status
|
||||
*/
|
||||
HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitConvertionTypeDef 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(OneWire_ReadBit(onewire) == 1)
|
||||
// return HAL_ERROR;
|
||||
|
||||
// Îæèäàíèå çàâåðøåíèÿ ïðåîáðàçîâàíèÿ, ïóòåì ïðîâåðêè øèíû
|
||||
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_WaitConvertionTypeDef 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 ×èòàåò èçìåðåííóþ äàò÷èêîì òåìïåðàòóðó
|
||||
* @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 Ïðîâåðÿåò ïîäêëþ÷åí ëè äàò÷èê (÷òåíèå scratchpad)
|
||||
* @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;
|
||||
|
||||
// Dallas_ReplaceLostedSensor(sensor);
|
||||
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.other_err_cnt++;
|
||||
return result;
|
||||
}
|
||||
result = DS18B20_ReadScratchpad(sensor->onewire, (uint8_t *)&sensor->sensROM, (uint8_t *)&sensor->scratchpad);
|
||||
if (result != HAL_OK)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
140
Core/Dallas/dallas_tools.h
Normal file
140
Core/Dallas/dallas_tools.h
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file dallas_tools.h
|
||||
* @brief Äðàéâåð äàò÷èêîâ òåìïåðàòóðû DALLAS
|
||||
******************************************************************************
|
||||
* Ýòîò ôàéë ïðåäîñòàâëÿåò îáúÿâëåíèÿ è îïðåäåëåíèÿ äëÿ ðàáîòû ñ äàò÷èêàìè
|
||||
* òåìïåðàòóðû DS18B20. Îí âêëþ÷àåò ñòðóêòóðû äàííûõ, ìàêðîñû è ïðîòîòèïû
|
||||
* ôóíêöèé äëÿ èíèöèàëèçàöèè, ÷òåíèÿ òåìïåðàòóðû
|
||||
* è óïðàâëåíèÿ äàò÷èêàìè.
|
||||
*
|
||||
* Ðàáîòà ñ äàò÷èêàìè âåä¸òñÿ ÷åðåç ïðîòîêîë OneWire.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef DALLAS_TOOLS_H
|
||||
#define DALLAS_TOOLS_H
|
||||
|
||||
|
||||
|
||||
/* Includes -----------------------------------------------------------------*/
|
||||
#include "ds18b20_driver.h"
|
||||
#include "onewire.h"
|
||||
|
||||
/* Îïðåäåëåíèÿ ïîëüçîâàòåëüñêèõ áàéòîâ äëÿ çàïèñè ÷òåíèÿ */
|
||||
#define DALLAS_USER_BYTE_1 (1<<0) ///< Ïåðâûé ïîëüçîâàòåëüñêèé áàéò
|
||||
#define DALLAS_USER_BYTE_2 (1<<1) ///< Âòîðîé ïîëüçîâàòåëüñêèé áàéò
|
||||
#define DALLAS_USER_BYTE_3 (1<<2) ///< Òðåòèé ïîëüçîâàòåëüñêèé áàéò
|
||||
#define DALLAS_USER_BYTE_4 (1<<3) ///< ×åòâ¸ðòûé ïîëüçîâàòåëüñêèé áàéò
|
||||
|
||||
#define DALLAS_USER_BYTE_12 (DALLAS_USER_BYTE_1|DALLAS_USER_BYTE_2) ///< Ïåðâûå äâà áàéòà
|
||||
#define DALLAS_USER_BYTE_34 (DALLAS_USER_BYTE_3|DALLAS_USER_BYTE_4) ///< Âòîðûå äâà áàéòà
|
||||
#define DALLAS_USER_BYTE_ALL (DALLAS_USER_BYTE_12|DALLAS_USER_BYTE_34) ///< Âñå ïîëüçîâàòåëüñêèå áàéòû
|
||||
|
||||
/* Declarations and definitions ---------------------------------------------*/
|
||||
#define DALLAS_ROM_SIZE 8
|
||||
|
||||
#define DALLAS_CONFIG_9_BITS 0x1F
|
||||
#define DALLAS_CONFIG_10_BITS 0x3F
|
||||
#define DALLAS_CONFIG_11_BITS 0x5F
|
||||
#define DALLAS_CONFIG_12_BITS 0x7F
|
||||
|
||||
#define DALLAS_DELAY_MS_9_BITS 94
|
||||
#define DALLAS_DELAY_MS_10_BITS 188
|
||||
#define DALLAS_DELAY_MS_11_BITS 375
|
||||
#define DALLAS_DELAY_MS_12_BITS 750
|
||||
#define DALLAS_DELAY_MS_MAX DALLAS_DELAY_MS_12_BITS
|
||||
|
||||
/** @brief Ñòðóêòóðà Scratchpad äàò÷èêà DALLAS */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t TemperatureLSB; ///< Ìëàäøèé áàéò òåìïåðàòóðû
|
||||
uint8_t TemperatureMSB; ///< Ñòàðøèé áàéò òåìïåðàòóðû
|
||||
uint8_t tHighRegister; ///< Âåðõíèé òåìïåðàòóðíûé ïîðîã
|
||||
uint8_t tLowRegister; ///< Íèæíèé òåìïåðàòóðíûé ïîðîã
|
||||
uint8_t ConfigRegister; ///< Êîíôèãóðàöèîííûé ðåãèñòð
|
||||
uint8_t reserved; ///< Çàðåçåðâèðîâàíî
|
||||
uint8_t UserByte3; ///< Ïîëüçîâàòåëüñêèé áàéò 3
|
||||
uint8_t UserByte4; ///< Ïîëüçîâàòåëüñêèé áàéò 4
|
||||
uint8_t ScratchpadCRC; ///< Êîíòðîëüíàÿ ñóììà
|
||||
}DALLAS_ScratchpadTypeDef;
|
||||
|
||||
/** @brief Ñòðóêòóðà ôëàãîâ îøèáîê äàò÷èêîâ DALLAS */
|
||||
typedef struct
|
||||
{
|
||||
unsigned disconnect_cnt; ///< Ñ÷åò÷èê îòêëþ÷åíèé äàò÷èêà
|
||||
unsigned read_temperature_err_cnt; ///< Ñ÷åò÷èê îøèáîê ÷òåíèÿ òåìïåðàòóðû
|
||||
unsigned timeout_convertion_cnt; ///< Ñ÷åò÷èê îøèáîê òàéìàóòà êîíâåðòàöèè
|
||||
unsigned other_err_cnt; ///< Ñ÷åò÷èê äðóãèõ îøèáîê
|
||||
}DALLAS_FlagsTypeDef;
|
||||
|
||||
|
||||
/** @brief Ñòðóêòóðà èíèöèàëèçàöèè äàò÷èêà DALLAS */
|
||||
typedef struct
|
||||
{
|
||||
uint64_t ROM; ///< Óíèêàëüíûé ROM-êîä äàò÷èêà
|
||||
uint16_t UserBytes12; ///< Ïîëüçîâàòåëüñêèå áàéòû 1 è 2
|
||||
uint16_t UserBytes34; ///< Ïîëüçîâàòåëüñêèå áàéòû 3 è 4
|
||||
uint8_t SensInd; ///< Èíäåêñ ñåíñîðà
|
||||
uint8_t Resolution; ///< Ðàçðåøåíèå äàò÷èêà
|
||||
HAL_StatusTypeDef (*init_func)(); ///< Ôóíêöèÿ èíèöèàëèçàöèè
|
||||
} DALLAS_InitStructTypeDef;
|
||||
|
||||
/** @brief Îñíîâíàÿ ñòðóêòóðà îáðàáîò÷èêà äàò÷èêà DALLAS */
|
||||
typedef struct
|
||||
{
|
||||
unsigned isConnected:1; ///< Ôëàã ñîåäèíåíèÿ
|
||||
unsigned isInitialized:1; ///< Ôëàã èíèöèàëèçàöèè
|
||||
unsigned isLost:1; ///< Ôëàã ïîòåðè ñâÿçè
|
||||
|
||||
OneWire_t *onewire; ///< Èíòåðôåéñ OneWire
|
||||
uint64_t sensROM; ///< ROM-êîä äàò÷èêà
|
||||
|
||||
DALLAS_ScratchpadTypeDef scratchpad; ///< Scratchpad äàò÷èêà
|
||||
float temperature; ///< Òåêóùàÿ òåìïåðàòóðà
|
||||
|
||||
DALLAS_InitStructTypeDef Init; ///< Ñòðóêòóðà èíèöèàëèçàöèè
|
||||
DALLAS_FlagsTypeDef f; ///< Ôëàãè
|
||||
|
||||
} DALLAS_HandleTypeDef;
|
||||
|
||||
|
||||
|
||||
/** @brief Âàðèàíòû îæèäàíèÿ îêîí÷àíèÿ êîíâåðñèè */
|
||||
typedef enum
|
||||
{
|
||||
DALLAS_WAIT_NONE = 0x00, ///< Áåç îæèäàíèÿ îêîí÷àíèÿ êîíâåðñèè
|
||||
DALLAS_WAIT_BUS = 0x01, ///< Îæèäàíèå îêîí÷àíèÿ êîíâåðñèè ïî øèíå (îïðîñ äàò÷èêîâ - ÷òåíèå áèòà)
|
||||
DALLAS_WAIT_DELAY = 0x02, ///< Áåç îæèäàíèÿ îêîí÷àíèÿ ÷åðåç çàäåðæêó (ìàêñèìàëüíàÿ çàäåðæêà äëÿ çàäàííîé ðàçðÿäíîñòè)
|
||||
} DALLAS_WaitConvertionTypeDef;
|
||||
|
||||
|
||||
|
||||
/* Functions ---------------------------------------------------------------*/
|
||||
|
||||
/* Ôóíêöèÿ äëÿ íàõîæäåíèÿ íîâîãî äàò÷èêà íà ìåñòî ïîòåðÿííîãî */
|
||||
HAL_StatusTypeDef Dallas_ReplaceLostedSensor(DALLAS_HandleTypeDef *sensor);
|
||||
/* Ôóíêöèÿ äëÿ èíèèöàëèçàöèè íîâîãî äàò÷èêà â ñòðóêòóðå */
|
||||
HAL_StatusTypeDef Dallas_AddNewSensors(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor);
|
||||
/* Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ROM */
|
||||
HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor);
|
||||
/* Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ïîëüçîâàòåëüñêèì áàéòàì */
|
||||
HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor);
|
||||
/* Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî ïîðÿäêîâîìó íîìåðó */
|
||||
HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor);
|
||||
/* Èíèöèàëèçèðóåò äàò÷èê äëÿ ðàáîòû */
|
||||
HAL_StatusTypeDef Dallas_SensorInit(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t (*ROM)[DALLAS_ROM_SIZE]);
|
||||
/* Äåèíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà */
|
||||
HAL_StatusTypeDef Dallas_SensorDeInit(DALLAS_HandleTypeDef *sensor);
|
||||
/* Çàïóñêàåò èçìåðåíèå òåìïåðàòóðû íà âñåõ äàò÷èêàõ */
|
||||
HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitConvertionTypeDef waitCondition, uint8_t dallas_delay_ms);
|
||||
/* Èçìåðÿåò òåìïåðàòóðó íà äàò÷èêå */
|
||||
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitConvertionTypeDef waitCondition);
|
||||
/* ×èòàåò èçìåðåííóþ äàò÷èêîì òåìïåðàòóðó */
|
||||
HAL_StatusTypeDef Dallas_ReadTemperature(DALLAS_HandleTypeDef *sensor);
|
||||
/* Ïðîâåðÿåò ïîäêëþ÷åí ëè äàò÷èê (÷òåíèå scratchpad) */
|
||||
HAL_StatusTypeDef Dallas_IsConnected(DALLAS_HandleTypeDef *sensor);
|
||||
/* Çàïèñûâàåò ïîëüçîâàòåëüñêèå áàéòû */
|
||||
HAL_StatusTypeDef Dallas_WriteUserBytes(DALLAS_HandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34, uint8_t UserBytesMask);
|
||||
|
||||
|
||||
#endif // #ifndef DALLAS_TOOLS_H
|
||||
534
Core/Dallas/ds18b20_driver.c
Normal file
534
Core/Dallas/ds18b20_driver.c
Normal file
@@ -0,0 +1,534 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ds18b20_driver.c
|
||||
* @brief This file includes the HAL/LL driver for DS18B20 1-Wire Digital
|
||||
* Thermometer
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "ds18b20_driver.h"
|
||||
|
||||
DS18B20_Drv_t DS;
|
||||
OneWire_t OW;
|
||||
|
||||
/**
|
||||
* @brief The function is used to check valid DS18B20 ROM
|
||||
* @retval Return in OK = 1, Failed = 0
|
||||
* @param ROM Pointer to ROM number
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM)
|
||||
{
|
||||
uint8_t check_family = (*ROM == DS18B20_FAMILY_CODE);
|
||||
/* Calculate CRC */
|
||||
uint8_t crc = OneWire_CRC8(ROM, 7);
|
||||
uint8_t check_crc = (crc == ROM[7]);
|
||||
/* Checks if first byte is equal to DS18B20's family code */
|
||||
if(check_family && check_crc)
|
||||
return HAL_OK;
|
||||
else
|
||||
return HAL_ERROR;
|
||||
}
|
||||
/**
|
||||
* @brief The function is used to check valid DS18B20 ROM
|
||||
* @retval Return in OK = 1, Failed = 0
|
||||
* @param ROM Pointer to ROM number
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_IsValid(uint8_t *ROM)
|
||||
{
|
||||
if(*ROM == DS18B20_FAMILY_CODE)
|
||||
return HAL_OK;
|
||||
else
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to get resolution
|
||||
* @retval Return value in 9 - 12
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
*/
|
||||
uint8_t DS18B20_GetResolution(OneWire_t* OW, uint8_t *ROM) {
|
||||
uint8_t conf;
|
||||
|
||||
/* Check valid ROM */
|
||||
if (DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return 0;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Read scratchpad command by onewire protocol */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_READSCRATCHPAD);
|
||||
|
||||
/* Ignore first 4 bytes */
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
|
||||
/* 5th byte of scratchpad is configuration register */
|
||||
conf = OneWire_ReadByte(OW);
|
||||
|
||||
/* Return 9 - 12 value according to number of bits */
|
||||
return ((conf & 0x60) >> 5) + 9;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used as set resolution
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
* @param Resolution Resolution in 9 - 12
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM,
|
||||
DS18B20_Res_t Resolution)
|
||||
{
|
||||
uint8_t th, tl, conf;
|
||||
|
||||
/* Check valid ROM */
|
||||
if (DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Read scratchpad command by onewire protocol */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_READSCRATCHPAD);
|
||||
|
||||
/* Ignore first 2 bytes */
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
|
||||
th = OneWire_ReadByte(OW);
|
||||
tl = OneWire_ReadByte(OW);
|
||||
conf = OneWire_ReadByte(OW);
|
||||
|
||||
/* Set choosed resolution */
|
||||
conf = Resolution;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Write scratchpad command by onewire protocol, only th, tl and conf
|
||||
* register can be written */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_WRITESCRATCHPAD);
|
||||
|
||||
/* Write bytes */
|
||||
OneWire_WriteByte(OW, th);
|
||||
OneWire_WriteByte(OW, tl);
|
||||
OneWire_WriteByte(OW, conf);
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Copy scratchpad to EEPROM of DS18B20 */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_COPYSCRATCHPAD);
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used as start selected ROM device
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM)
|
||||
{
|
||||
/* Check if device is DS18B20 */
|
||||
if(DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Start temperature conversion */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_CONVERT);
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
/**
|
||||
* @brief The function is used as start all ROM device
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_StartConvTAll(OneWire_t* OW)
|
||||
{
|
||||
/* Reset pulse */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Skip rom */
|
||||
OneWire_WriteByte(OW, ONEWIRE_CMD_SKIPROM);
|
||||
|
||||
/* Start conversion on all connected devices */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_CONVERT);
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used as read temreature from device and store in selected
|
||||
* destination
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
* @param Destination Pointer to return value
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_CalcTemperature(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad, float *Destination)
|
||||
{
|
||||
uint16_t temperature;
|
||||
uint8_t resolution;
|
||||
int8_t digit, minus = 0;
|
||||
float decimal;
|
||||
|
||||
/* Check if device is DS18B20 */
|
||||
if (DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
/* First two bytes of scratchpad are temperature values */
|
||||
temperature = Scratchpad[0] | (Scratchpad[1] << 8);
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Check if temperature is negative */
|
||||
if (temperature & 0x8000) {
|
||||
/* Two's complement, temperature is negative */
|
||||
temperature = ~temperature + 1;
|
||||
minus = 1;
|
||||
}
|
||||
|
||||
/* Get sensor resolution */
|
||||
resolution = Scratchpad[4];
|
||||
|
||||
/* Store temperature integer digits and decimal digits */
|
||||
digit = temperature >> 4;
|
||||
digit |= ((temperature >> 8) & 0x7) << 4;
|
||||
|
||||
/* Store decimal digits */
|
||||
switch (resolution) {
|
||||
case DS18B20_RESOLUTION_9BITS: {
|
||||
decimal = (temperature >> 3) & 0x01;
|
||||
decimal *= (float)DS18B20_DECIMAL_STEP_9BIT;
|
||||
} break;
|
||||
case DS18B20_RESOLUTION_10BITS: {
|
||||
decimal = (temperature >> 2) & 0x03;
|
||||
decimal *= (float)DS18B20_DECIMAL_STEP_10BIT;
|
||||
} break;
|
||||
case DS18B20_RESOLUTION_11BITS: {
|
||||
decimal = (temperature >> 1) & 0x07;
|
||||
decimal *= (float)DS18B20_DECIMAL_STEP_11BIT;
|
||||
} break;
|
||||
case DS18B20_RESOLUTION_12BITS: {
|
||||
decimal = temperature & 0x0F;
|
||||
decimal *= (float)DS18B20_DECIMAL_STEP_12BIT;
|
||||
} break;
|
||||
default: {
|
||||
*Destination = 0;
|
||||
return HAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for negative part */
|
||||
decimal = digit + decimal;
|
||||
if (minus) {
|
||||
decimal = 0 - decimal;
|
||||
}
|
||||
|
||||
/* Set to pointer */
|
||||
*Destination = decimal;
|
||||
|
||||
/* Return HAL_OK, temperature valid */
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t scratchpad_buff[8];
|
||||
/**
|
||||
* @brief The function is used as read scratchpad from device
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
* @param Destination Pointer to Scratchpad array
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad)
|
||||
{
|
||||
if(Scratchpad == NULL)
|
||||
Scratchpad = scratchpad_buff;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Read scratchpad command by onewire protocol */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_READSCRATCHPAD);
|
||||
|
||||
/* Get data */
|
||||
for (int i = 0; i < 9; i++) {
|
||||
/* Read byte by byte */
|
||||
Scratchpad[i] = OneWire_ReadByte(OW);
|
||||
}
|
||||
|
||||
/* Calculate CRC */
|
||||
uint8_t crc = OneWire_CRC8(Scratchpad, 8);
|
||||
|
||||
/* Check if CRC is ok */
|
||||
if (crc != Scratchpad[8]) {
|
||||
/* CRC invalid */
|
||||
return HAL_ERROR;
|
||||
}
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief The function is used to wait for end of convertion
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW)
|
||||
{
|
||||
uint32_t tickstart = HAL_GetTick();
|
||||
|
||||
/* Wait until line is released, then coversion is completed */
|
||||
while(OneWire_ReadBit(OW) == 0)
|
||||
{
|
||||
if(HAL_GetTick() - tickstart > DS18B20_DELAY_MS_MAX)
|
||||
return HAL_TIMEOUT; // end of convertion has not come
|
||||
}
|
||||
return HAL_OK; // convertion done
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief The function is used as set temperature alarm range on
|
||||
* selected device
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
* @param Low Low temperature alarm, value > -55, 0 = reset
|
||||
* @param High High temperature alarm,, value < 125, 0 = reset
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low,
|
||||
int8_t High)
|
||||
{
|
||||
uint8_t tl, th, conf;
|
||||
|
||||
/* Check if device is DS18B20 */
|
||||
if (DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
Low = ((Low < -55) || (Low == 0)) ? -55 : Low;
|
||||
High = ((High > 125) || (High == 0)) ? 125 : High;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Read scratchpad command by onewire protocol */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_READSCRATCHPAD);
|
||||
|
||||
/* Ignore first 2 bytes */
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
|
||||
th = OneWire_ReadByte(OW);
|
||||
tl = OneWire_ReadByte(OW);
|
||||
conf = OneWire_ReadByte(OW);
|
||||
|
||||
th = (uint8_t)High;
|
||||
tl = (uint8_t)Low;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Write scratchpad command by onewire protocol, only th, tl and conf
|
||||
* register can be written */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_WRITESCRATCHPAD);
|
||||
|
||||
/* Write bytes */
|
||||
OneWire_WriteByte(OW, th);
|
||||
OneWire_WriteByte(OW, tl);
|
||||
OneWire_WriteByte(OW, conf);
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Copy scratchpad to EEPROM of DS18B20 */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_COPYSCRATCHPAD);
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used as set user bytes with mask
|
||||
* @retval status in OK = 1, Failed = 0
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to ROM number
|
||||
* @param UserBytes12 First 2 User Bytes (tHigh and tLow)
|
||||
* @param UserBytes34 Second 2 User Bytes
|
||||
* @param UserBytesMask Which User Bytes write, and which ignore
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t UserBytes12,
|
||||
int16_t UserBytes34, uint8_t UserBytesMask)
|
||||
{
|
||||
uint8_t ub1, ub2, conf, ub3, ub4;
|
||||
uint8_t UserByte1 = UserBytes12 & 0xFF;
|
||||
uint8_t UserByte2 = UserBytes12 >> 8;
|
||||
uint8_t UserByte3 = UserBytes34 & 0xFF;
|
||||
uint8_t UserByte4 = UserBytes34 >> 8;
|
||||
|
||||
/* Check if device is DS18B20 */
|
||||
if (DS18B20_IsValid(ROM) != HAL_OK)
|
||||
return HAL_ERROR;
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Read scratchpad command by onewire protocol */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_READSCRATCHPAD);
|
||||
|
||||
/* Ignore first 2 bytes */
|
||||
OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
|
||||
ub1 = OneWire_ReadByte(OW);
|
||||
ub2 = OneWire_ReadByte(OW);
|
||||
conf = OneWire_ReadByte(OW);
|
||||
OneWire_ReadByte(OW);
|
||||
ub3 = OneWire_ReadByte(OW);
|
||||
ub4 = OneWire_ReadByte(OW);
|
||||
|
||||
/* If user bytes in mask */
|
||||
if(UserBytesMask & (1<<0))
|
||||
{
|
||||
ub1 = UserByte1;
|
||||
}
|
||||
if(UserBytesMask & (1<<1))
|
||||
{
|
||||
ub2 = UserByte2;
|
||||
}
|
||||
if(UserBytesMask & (1<<2))
|
||||
{
|
||||
ub3 = UserByte3;
|
||||
}
|
||||
if(UserBytesMask & (1<<3))
|
||||
{
|
||||
ub4 = UserByte4;
|
||||
}
|
||||
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Write scratchpad command by onewire protocol, only th, tl and conf
|
||||
* register can be written */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_WRITESCRATCHPAD);
|
||||
|
||||
/* Write bytes */
|
||||
OneWire_WriteByte(OW, ub1);
|
||||
OneWire_WriteByte(OW, ub2);
|
||||
OneWire_WriteByte(OW, conf);
|
||||
OneWire_WriteByte(OW, ub3);
|
||||
OneWire_WriteByte(OW, ub4);
|
||||
|
||||
/* Reset line */
|
||||
OneWire_Reset(OW);
|
||||
|
||||
/* Select ROM number */
|
||||
OneWire_MatchROM(OW, ROM);
|
||||
|
||||
/* Copy scratchpad to EEPROM of DS18B20 */
|
||||
OneWire_WriteByte(OW, DS18B20_CMD_COPYSCRATCHPAD);
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief The function is used as search device that had temperature alarm
|
||||
* triggered and store it in DS18B20 alarm data structure
|
||||
* @retval status of search, OK = 1, Failed = 0
|
||||
* @param DS DS18B20 HandleTypedef
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t DS18B20_AlarmSearch(DS18B20_Drv_t *DS, OneWire_t* OW)
|
||||
{
|
||||
uint8_t t = 0;
|
||||
|
||||
/* Reset Alarm in DS */
|
||||
for(uint8_t i = 0; i < OW->RomCnt; i++)
|
||||
{
|
||||
for(uint8_t j = 0; j < 8; j++)
|
||||
{
|
||||
DS->AlmAddr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start alarm search */
|
||||
while (OneWire_Search(OW, DS18B20_CMD_ALARM_SEARCH))
|
||||
{
|
||||
/* Store ROM of device which has alarm flag set */
|
||||
OneWire_GetDevRom(OW, DS->AlmAddr[t]);
|
||||
t++;
|
||||
}
|
||||
return (t > 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to initialize the DS18B20 sensor, and search
|
||||
* for all ROM along the line. Store in DS18B20 data structure
|
||||
* @retval Rom detect status, OK = 1, No Rom detected = 0
|
||||
* @param DS DS18B20 HandleTypedef
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
HAL_StatusTypeDef DS18B20_Search(DS18B20_Drv_t *DS, OneWire_t *OW)
|
||||
{
|
||||
OW->RomCnt = 0;
|
||||
/* Search all OneWire devices ROM */
|
||||
while(1)
|
||||
{
|
||||
/* Start searching for OneWire devices along the line */
|
||||
if(OneWire_Search(OW, ONEWIRE_CMD_SEARCHROM) != 1) break;
|
||||
|
||||
/* Get device ROM */
|
||||
OneWire_GetDevRom(OW, DS->DevAddr[OW->RomCnt]);
|
||||
|
||||
OW->RomCnt++;
|
||||
}
|
||||
|
||||
if(OW->RomCnt > 0)
|
||||
return HAL_OK;
|
||||
else
|
||||
return HAL_BUSY;
|
||||
}
|
||||
107
Core/Dallas/ds18b20_driver.h
Normal file
107
Core/Dallas/ds18b20_driver.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ds18b20_driver.h
|
||||
* @brief This file contains all the constants parameters for the DS18B20
|
||||
* 1-Wire Digital Thermometer
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* Usage:
|
||||
* Uncomment LL Driver for HAL driver
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef DS18B20_H
|
||||
#define DS18B20_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "onewire.h"
|
||||
|
||||
/* I/O Port ------------------------------------------------------------------*/
|
||||
#define DS_Pin GPIO_PIN_0
|
||||
#define DS_GPIO_Port GPIOB
|
||||
|
||||
/* Data Structure ------------------------------------------------------------*/
|
||||
#define DS18B20_DEVICE_AMOUNT 8
|
||||
|
||||
/* Register ------------------------------------------------------------------*/
|
||||
#define DS18B20_CMD_CONVERT 0x44
|
||||
#define DS18B20_CMD_ALARM_SEARCH 0xEC
|
||||
#define DS18B20_CMD_READSCRATCHPAD 0xBE
|
||||
#define DS18B20_CMD_WRITESCRATCHPAD 0x4E
|
||||
#define DS18B20_CMD_COPYSCRATCHPAD 0x48
|
||||
/* Data Structure ------------------------------------------------------------*/
|
||||
#define DS18B20_FAMILY_CODE 0x28
|
||||
|
||||
|
||||
#define DS18B20_SERIAL_NUMBER_LEN_BYTES 6
|
||||
#define DS18B20_SERIAL_NUMBER_OFFSET_BYTES 1
|
||||
|
||||
#define DS18B20_SCRATCHPAD_T_LSB_BYTE_IDX 0
|
||||
#define DS18B20_SCRATCHPAD_T_MSB_BYTE_IDX 1
|
||||
#define DS18B20_SCRATCHPAD_T_LIMIT_H_BYTE_IDX 2
|
||||
#define DS18B20_SCRATCHPAD_T_LIMIT_L_BYTE_IDX 3
|
||||
#define DS18B20_SCRATCHPAD_CONFIG_BYTE_IDX 4
|
||||
#define DS18B20_SCRATCHPAD_USER_BYTE_3_IDX 6
|
||||
#define DS18B20_SCRATCHPAD_USER_BYTE_4_IDX 7
|
||||
#define DS18B20_SCRATCHPAD_CRC_IDX 8
|
||||
|
||||
/* Bits locations for resolution */
|
||||
#define DS18B20_RESOLUTION_R1 6
|
||||
#define DS18B20_RESOLUTION_R0 5
|
||||
|
||||
#define DS18B20_DECIMAL_STEP_12BIT 0.0625
|
||||
#define DS18B20_DECIMAL_STEP_11BIT 0.125
|
||||
#define DS18B20_DECIMAL_STEP_10BIT 0.25
|
||||
#define DS18B20_DECIMAL_STEP_9BIT 0.5
|
||||
|
||||
#define DS18B20_DELAY_MS_9_BITS 94
|
||||
#define DS18B20_DELAY_MS_10_BITS 188
|
||||
#define DS18B20_DELAY_MS_11_BITS 375
|
||||
#define DS18B20_DELAY_MS_12_BITS 750
|
||||
#define DS18B20_DELAY_MS_MAX DS18B20_DELAY_MS_12_BITS
|
||||
|
||||
|
||||
/* DS18B20 Resolutions */
|
||||
typedef enum {
|
||||
DS18B20_RESOLUTION_9BITS = 0x1F,
|
||||
DS18B20_RESOLUTION_10BITS = 0x3F,
|
||||
DS18B20_RESOLUTION_11BITS = 0x5F,
|
||||
DS18B20_RESOLUTION_12BITS = 0x7F
|
||||
} DS18B20_Res_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t DevAddr[DS18B20_DEVICE_AMOUNT][8];
|
||||
uint8_t AlmAddr[DS18B20_DEVICE_AMOUNT][8];
|
||||
float Temperature[DS18B20_DEVICE_AMOUNT];
|
||||
DS18B20_Res_t Resolution;
|
||||
} DS18B20_Drv_t;
|
||||
extern DS18B20_Drv_t DS;
|
||||
extern OneWire_t OW;
|
||||
|
||||
/* External Function ---------------------------------------------------------*/
|
||||
HAL_StatusTypeDef DS18B20_Search(DS18B20_Drv_t *DS, OneWire_t *OW);
|
||||
HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM);
|
||||
HAL_StatusTypeDef DS18B20_StartConvTAll(OneWire_t* OW);
|
||||
HAL_StatusTypeDef DS18B20_CalcTemperature(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad, float *destination);
|
||||
HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad);
|
||||
HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW);
|
||||
HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low,
|
||||
int8_t High);
|
||||
HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t UserBytes12,
|
||||
int16_t UserBytes34, uint8_t UserBytesMask);
|
||||
uint8_t DS18B20_AlarmSearch(DS18B20_Drv_t *DS, OneWire_t* OW);
|
||||
|
||||
HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM,
|
||||
DS18B20_Res_t Resolution);
|
||||
HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DS18B20_H */
|
||||
58
Core/Dallas/dwt.c
Normal file
58
Core/Dallas/dwt.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file dwt.c
|
||||
* @brief This file includes the utilities for DWT
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "dwt.h"
|
||||
|
||||
static uint32_t SysCClk, start;
|
||||
|
||||
/**
|
||||
* @brief Initialize DWT
|
||||
*/
|
||||
void DwtInit(void)
|
||||
{
|
||||
SysCClk = (SystemCoreClock / 1000000); // Calculate in us
|
||||
DWT_LAR |= DWT_LAR_UNLOCK;
|
||||
DEM_CR |= (uint32_t)DEM_CR_TRCENA;
|
||||
DWT_CYCCNT = (uint32_t)0u; // Reset the clock counter
|
||||
DWT_CR |= (uint32_t)DWT_CR_CYCCNTENA;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start DWT Counter
|
||||
*/
|
||||
void DwtStart(void)
|
||||
{
|
||||
start = DWT_CYCCNT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculate Interval Base On Previous Start Time
|
||||
* @retval Interval in us
|
||||
*/
|
||||
float DwtInterval(void)
|
||||
{
|
||||
return (float)(DWT_CYCCNT - start) / SysCClk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function to delay in microsecond
|
||||
* @param usec Period in microsecond
|
||||
*/
|
||||
inline void DwtDelay_us(uint32_t usec)
|
||||
{
|
||||
start = DWT_CYCCNT;
|
||||
while(((DWT_CYCCNT - start) / SysCClk) < usec) {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function to delay in millisecond
|
||||
* @param msec Period in millisecond
|
||||
*/
|
||||
inline void DwtDelay_ms(uint32_t msec)
|
||||
{
|
||||
start = DWT_CYCCNT;
|
||||
while(((DWT_CYCCNT - start) / SysCClk) < (msec * 1000)) {};
|
||||
}
|
||||
39
Core/Dallas/dwt.h
Normal file
39
Core/Dallas/dwt.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file dwt.h
|
||||
* @brief This file contains all the constants parameters for the dwt delay
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef DWT_H
|
||||
#define DWT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "py32f0xx_hal.h"
|
||||
|
||||
/* Custom Define -------------------------------------------------------------*/
|
||||
#define DWT_LAR_UNLOCK (uint32_t)0xC5ACCE55
|
||||
#define DEM_CR_TRCENA (1 << 24)
|
||||
#define DWT_CR_CYCCNTENA (1 << 0)
|
||||
#define DWT_CR *(volatile uint32_t *)0xE0001000
|
||||
#define DWT_LAR *(volatile uint32_t *)0xE0001FB0
|
||||
#define DWT_CYCCNT *(volatile uint32_t *)0xE0001004
|
||||
#define DEM_CR *(volatile uint32_t *)0xE000EDFC
|
||||
|
||||
|
||||
/* External Function ---------------------------------------------------------*/
|
||||
void DwtInit(void);
|
||||
void DwtStart(void);
|
||||
float DwtInterval(void);
|
||||
void DwtDelay_us(uint32_t usec);
|
||||
void DwtDelay_ms(uint32_t msec);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DWT_H */
|
||||
375
Core/Dallas/onewire.c
Normal file
375
Core/Dallas/onewire.c
Normal file
@@ -0,0 +1,375 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file onewire.c
|
||||
* @brief This file includes the HAL/LL driver for OneWire devices
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "onewire.h"
|
||||
|
||||
/**
|
||||
* @brief The internal function is used to write bit
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param bit bit in 0 or 1
|
||||
*/
|
||||
void OneWire_WriteBit(OneWire_t* OW, uint8_t bit)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return;
|
||||
#ifndef ONEWIRE_UART_H
|
||||
if(bit)
|
||||
{
|
||||
/* Set line low */
|
||||
OneWire_Pin_Level(OW, 0);
|
||||
OneWire_Pin_Mode(OW, Output);
|
||||
|
||||
/* Forming pulse */
|
||||
OneWire_Delay_uw(ONEWIRE_WRITE_1_US);
|
||||
|
||||
/* Release line (pull up line) */
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
|
||||
/* Wait for 55 us and release the line */
|
||||
OneWire_Delay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_1_US);
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
}else{
|
||||
/* Set line low */
|
||||
OneWire_Pin_Level(OW, 0);
|
||||
OneWire_Pin_Mode(OW, Output);
|
||||
|
||||
/* Forming pulse */
|
||||
OneWire_Delay_uw(ONEWIRE_WRITE_0_US);
|
||||
|
||||
/* Release line (pull up line) */
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
|
||||
/* Wait for 5 us and release the line */
|
||||
OneWire_Delay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_0_US);
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
}
|
||||
#else
|
||||
OneWireUART_ProcessBit(onewire_uart, bit);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to read bit
|
||||
* @retval bit
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t OneWire_ReadBit(OneWire_t* OW)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return 0;
|
||||
|
||||
uint8_t bit = 0;
|
||||
#ifndef ONEWIRE_UART_H
|
||||
/* Line low */
|
||||
OneWire_Pin_Level(OW, 0);
|
||||
OneWire_Pin_Mode(OW, Output);
|
||||
OneWire_Delay_uw(ONEWIRE_READ_CMD_US);
|
||||
|
||||
/* Release line */
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
OneWire_Delay_uw(ONEWIRE_READ_DELAY_US);
|
||||
|
||||
/* Read line value */
|
||||
bit = OneWire_Pin_Read(OW);
|
||||
|
||||
/* Wait 50us to complete 60us period */
|
||||
OneWire_Delay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_READ_CMD_US - ONEWIRE_READ_DELAY_US);
|
||||
#else
|
||||
bit = OneWireUART_ProcessBit(onewire_uart, 1);
|
||||
#endif
|
||||
/* Return bit value */
|
||||
return bit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to write byte
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param byte byte to write
|
||||
*/
|
||||
void OneWire_WriteByte(OneWire_t* OW, uint8_t byte)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return;
|
||||
|
||||
#ifndef ONEWIRE_UART_H
|
||||
uint8_t bit = 8;
|
||||
/* Write 8 bits */
|
||||
while (bit--) {
|
||||
/* LSB bit is first */
|
||||
OneWire_WriteBit(OW, byte & 0x01);
|
||||
byte >>= 1;
|
||||
}
|
||||
#else
|
||||
OneWireUART_ProcessByte(onewire_uart, byte);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to read byte
|
||||
* @retval byte from device
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t OneWire_ReadByte(OneWire_t* OW)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return 0;
|
||||
|
||||
uint8_t byte = 0;
|
||||
#ifndef ONEWIRE_UART_H
|
||||
uint8_t bit = 8;
|
||||
while (bit--) {
|
||||
byte >>= 1;
|
||||
byte |= (OneWire_ReadBit(OW) << 7);
|
||||
}
|
||||
#else
|
||||
byte = OneWireUART_ProcessByte(onewire_uart, 0xFF);
|
||||
#endif
|
||||
|
||||
return byte;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to reset device
|
||||
* @retval respond from device
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t OneWire_Reset(OneWire_t* OW)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return 1;
|
||||
|
||||
#ifndef ONEWIRE_UART_H
|
||||
/* Line low, and wait 480us */
|
||||
OneWire_Pin_Level(OW, 0);
|
||||
OneWire_Pin_Mode(OW, Output);
|
||||
OneWire_Delay_uw(ONEWIRE_RESET_PULSE_US);
|
||||
|
||||
/* Release line and wait for 70us */
|
||||
OneWire_Pin_Mode(OW, Input);
|
||||
OneWire_Delay_uw(ONEWIRE_PRESENCE_WAIT_US);
|
||||
|
||||
/* Check bit value */
|
||||
uint8_t rslt = OneWire_Pin_Read(OW);
|
||||
|
||||
/* Delay for 410 us */
|
||||
OneWire_Delay_uw(ONEWIRE_PRESENCE_DURATION_US);
|
||||
#else
|
||||
|
||||
uint8_t rslt = 0;
|
||||
if(OneWireUART_Reset(onewire_uart) == HAL_OK)
|
||||
rslt = 0;
|
||||
else
|
||||
rslt = 1;
|
||||
#endif
|
||||
|
||||
return rslt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to search device
|
||||
* @retval Search result
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t OneWire_Search(OneWire_t* OW, uint8_t Cmd)
|
||||
{
|
||||
if(OW == NULL)
|
||||
return 0;
|
||||
|
||||
uint8_t id_bit_number = 1;
|
||||
uint8_t last_zero = 0;
|
||||
uint8_t rom_byte_number = 0;
|
||||
uint8_t search_result = 0;
|
||||
uint8_t rom_byte_mask = 1;
|
||||
uint8_t id_bit, cmp_id_bit, search_direction;
|
||||
|
||||
/* if the last call was not the last one */
|
||||
if (!OW->LastDeviceFlag)
|
||||
{
|
||||
if (OneWire_Reset(OW))
|
||||
{
|
||||
OW->LastDiscrepancy = 0;
|
||||
OW->LastDeviceFlag = 0;
|
||||
OW->LastFamilyDiscrepancy = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// issue the search command
|
||||
OneWire_WriteByte(OW, Cmd);
|
||||
|
||||
// loop to do the search
|
||||
do {
|
||||
// read a bit and its complement
|
||||
id_bit = OneWire_ReadBit(OW);
|
||||
cmp_id_bit = OneWire_ReadBit(OW);
|
||||
|
||||
// check for no devices on 1-wire
|
||||
if ((id_bit == 1) && (cmp_id_bit == 1))
|
||||
{
|
||||
break;
|
||||
} else {
|
||||
// all devices coupled have 0 or 1
|
||||
if (id_bit != cmp_id_bit)
|
||||
{
|
||||
search_direction = id_bit; // bit write value for search
|
||||
} else {
|
||||
/* if this discrepancy if before the Last Discrepancy
|
||||
* on a previous next then pick the same as last time */
|
||||
if (id_bit_number < OW->LastDiscrepancy)
|
||||
{
|
||||
search_direction = ((OW->RomByte[rom_byte_number] & rom_byte_mask) > 0);
|
||||
} else {
|
||||
// if equal to last pick 1, if not then pick 0
|
||||
search_direction = (id_bit_number == OW->LastDiscrepancy);
|
||||
}
|
||||
|
||||
// if 0 was picked then record its position in LastZero
|
||||
if (search_direction == 0)
|
||||
{
|
||||
last_zero = id_bit_number;
|
||||
|
||||
// check for Last discrepancy in family
|
||||
if (last_zero < 9)
|
||||
{
|
||||
OW->LastFamilyDiscrepancy = last_zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* set or clear the bit in the ROM byte rom_byte_number
|
||||
* with mask rom_byte_mask */
|
||||
if (search_direction == 1)
|
||||
{
|
||||
OW->RomByte[rom_byte_number] |= rom_byte_mask;
|
||||
} else {
|
||||
OW->RomByte[rom_byte_number] &= ~rom_byte_mask;
|
||||
}
|
||||
|
||||
// serial number search direction write bit
|
||||
OneWire_WriteBit(OW, search_direction);
|
||||
|
||||
/* increment the byte counter id_bit_number and shift the
|
||||
* mask rom_byte_mask */
|
||||
id_bit_number++;
|
||||
rom_byte_mask <<= 1;
|
||||
|
||||
/* if the mask is 0 then go to new SerialNum byte
|
||||
* rom_byte_number and reset mask */
|
||||
if (rom_byte_mask == 0)
|
||||
{
|
||||
rom_byte_number++;
|
||||
rom_byte_mask = 1;
|
||||
}
|
||||
}
|
||||
} while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7
|
||||
if the search was successful then */
|
||||
|
||||
if (!(id_bit_number < 65))
|
||||
{
|
||||
/* search successful so set LastDiscrepancy, LastDeviceFlag,
|
||||
* search_result */
|
||||
OW->LastDiscrepancy = last_zero;
|
||||
// check for last device
|
||||
if (OW->LastDiscrepancy == 0) {
|
||||
OW->LastDeviceFlag = 1;
|
||||
}
|
||||
search_result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* if no device found then reset counters so next 'search' will be like a
|
||||
* first */
|
||||
if (!search_result || !OW->RomByte[0])
|
||||
{
|
||||
OW->LastDiscrepancy = 0;
|
||||
OW->LastDeviceFlag = 0;
|
||||
OW->LastFamilyDiscrepancy = 0;
|
||||
search_result = 0;
|
||||
}
|
||||
|
||||
return search_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used get ROM full address
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to device ROM
|
||||
*/
|
||||
void OneWire_GetDevRom(OneWire_t* OW, uint8_t *ROM)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
*(ROM + i) = OW->RomByte[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to initialize OneWire Communication
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
void OneWire_Init(OneWire_t* OW)
|
||||
{
|
||||
OneWire_Pin_Mode(OW, Output);
|
||||
OneWire_Pin_Level(OW, 1);
|
||||
OneWire_Delay_uw(1000);
|
||||
OneWire_Pin_Level(OW, 0);
|
||||
OneWire_Delay_uw(1000);
|
||||
OneWire_Pin_Level(OW, 1);
|
||||
OneWire_Delay_uw(2000);
|
||||
|
||||
/* Reset the search state */
|
||||
OW->LastDiscrepancy = 0;
|
||||
OW->LastDeviceFlag = 0;
|
||||
OW->LastFamilyDiscrepancy = 0;
|
||||
OW->RomCnt = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used selected specific device ROM
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param ROM Pointer to device ROM
|
||||
*/
|
||||
void OneWire_MatchROM(OneWire_t* OW, uint8_t *ROM)
|
||||
{
|
||||
OneWire_WriteByte(OW, ONEWIRE_CMD_MATCHROM);
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
OneWire_WriteByte(OW, *(ROM + i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used to access to all ROM
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
void OneWire_Skip(OneWire_t* OW)
|
||||
{
|
||||
OneWire_WriteByte(OW, ONEWIRE_CMD_SKIPROM);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The function is used check CRC
|
||||
* @param Addr Pointer to address
|
||||
* @param ROM Number of byte
|
||||
*/
|
||||
uint8_t OneWire_CRC8(uint8_t *Addr, uint8_t Len)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
uint8_t inbyte, i, mix;
|
||||
|
||||
while (Len--)
|
||||
{
|
||||
inbyte = *Addr++;
|
||||
|
||||
for (i = 8; i; i--)
|
||||
{
|
||||
mix = (crc ^ inbyte) & 0x01;
|
||||
crc >>= 1;
|
||||
crc ^= (mix) ? 0x8C : 0;
|
||||
inbyte >>= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
77
Core/Dallas/onewire.h
Normal file
77
Core/Dallas/onewire.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file onewire.h
|
||||
* @brief This file contains all the constants parameters for the OneWire
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef ONEWIRE_H
|
||||
#define ONEWIRE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "ow_port.h"
|
||||
|
||||
/* Driver Selection ----------------------------------------------------------*/
|
||||
//#define LL_Driver
|
||||
#define CMSIS_Driver
|
||||
/* OneWire Timings -----------------------------------------------------------*/
|
||||
#define ONEWIRE_RESET_PULSE_US 480 // Äëèòåëüíîñòü èìïóëüñà ñáðîñà
|
||||
#define ONEWIRE_PRESENCE_WAIT_US 70 // Îæèäàíèå îòâåòà îò äàò÷èêà
|
||||
#define ONEWIRE_PRESENCE_DURATION_US 410 // Äëèòåëüíîñòü ñèãíàëà ïðèñóòñòâèÿ
|
||||
|
||||
#define ONEWIRE_WRITE_1_US 8 // Äëèòåëüíîñòü çàïèñè "1"
|
||||
#define ONEWIRE_WRITE_0_US 57 // Äëèòåëüíîñòü çàïèñè "0"
|
||||
#define ONEWIRE_READ_CMD_US 2 // Âðåìÿ êîììàíäû ÷òåíèÿ áèòà
|
||||
#define ONEWIRE_READ_DELAY_US 6 // Çàäåðæêà ïåðåä ñ÷èòûâàíèåì áèòà
|
||||
#define ONEWIRE_COMMAND_SLOT_US 58 // Îáùåå âðåìÿ êîììàíäû OneWire
|
||||
#define ONEWIRE_RECOVERY_TIME_US 1 // Âîññòàíîâëåíèå ïåðåä ñëåäóþùèì ñëîòîì
|
||||
/* Common Register -----------------------------------------------------------*/
|
||||
#define ONEWIRE_CMD_SEARCHROM 0xF0
|
||||
#define ONEWIRE_CMD_READROM 0x33
|
||||
#define ONEWIRE_CMD_MATCHROM 0x55
|
||||
#define ONEWIRE_CMD_SKIPROM 0xCC
|
||||
|
||||
/* Data Structure ------------------------------------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
Input,
|
||||
Output
|
||||
} PinMode;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t LastDiscrepancy;
|
||||
uint8_t LastFamilyDiscrepancy;
|
||||
uint8_t LastDeviceFlag;
|
||||
uint8_t RomByte[8];
|
||||
uint8_t RomCnt;
|
||||
uint16_t DataPin;
|
||||
GPIO_TypeDef *DataPort;
|
||||
} OneWire_t;
|
||||
|
||||
/* External Function ---------------------------------------------------------*/
|
||||
void OneWire_Init(OneWire_t* OW);
|
||||
uint8_t OneWire_Search(OneWire_t* OW, uint8_t Cmd);
|
||||
void OneWire_GetDevRom(OneWire_t* OW, uint8_t *dev);
|
||||
uint8_t OneWire_Reset(OneWire_t* OW);
|
||||
uint8_t OneWire_ReadBit(OneWire_t* OW);
|
||||
uint8_t OneWire_ReadByte(OneWire_t* OW);
|
||||
void OneWire_WriteByte(OneWire_t* OW, uint8_t byte);
|
||||
void OneWire_MatchROM(OneWire_t* OW, uint8_t *Rom);
|
||||
void OneWire_Skip(OneWire_t* OW);
|
||||
uint8_t OneWire_CRC8(uint8_t *addr, uint8_t len);
|
||||
|
||||
void OneWire_Pin_Mode(OneWire_t* OW, PinMode Mode);
|
||||
void OneWire_Pin_Level(OneWire_t* OW, uint8_t Level);
|
||||
uint8_t OneWire_Pin_Read(OneWire_t* OW);
|
||||
void OneWire_WriteBit(OneWire_t* OW, uint8_t bit);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ONEWIRE_H */
|
||||
126
Core/Dallas/ow_port.c
Normal file
126
Core/Dallas/ow_port.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ow_port.c
|
||||
* @brief This file includes the driver for port for OneWire purposes
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "ow_port.h"
|
||||
#include "onewire.h"
|
||||
#include "tim.h"
|
||||
|
||||
/**
|
||||
* @brief The internal function is used as gpio pin mode
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param Mode Input or Output
|
||||
*/
|
||||
void OneWire_Pin_Mode(OneWire_t* OW, PinMode Mode)
|
||||
{
|
||||
#ifdef CMSIS_Driver
|
||||
static uint32_t pin_cr_numb = 0;
|
||||
static int get_pin_numb = 1;
|
||||
|
||||
if(get_pin_numb)
|
||||
{
|
||||
get_pin_numb = 0;
|
||||
for(int i = 0; i < 8; i++)
|
||||
{
|
||||
if((OW->DataPin >> i) == 0x1)
|
||||
pin_cr_numb = i*4;
|
||||
}
|
||||
for(int i = 8; i < 16; i++)
|
||||
{
|
||||
if((OW->DataPin >> i) == 0x1)
|
||||
pin_cr_numb = (i-8)*4;
|
||||
}
|
||||
}
|
||||
if(Mode == Input)
|
||||
{
|
||||
OW->DataPort->MODER &= ~((GPIO_MODER_MODE0_Msk) << pin_cr_numb);
|
||||
OW->DataPort->MODER |= (GPIO_MODE_INPUT << (pin_cr_numb+2));
|
||||
}else{
|
||||
OW->DataPort->MODER &= ~((GPIO_MODER_MODE0_Msk) << pin_cr_numb);
|
||||
OW->DataPort->MODER |= (GPIO_MODE_OUTPUT_PP << pin_cr_numb);
|
||||
}
|
||||
#else
|
||||
#ifdef LL_Driver
|
||||
if(Mode == Input)
|
||||
{
|
||||
LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_INPUT);
|
||||
}else{
|
||||
LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_OUTPUT);
|
||||
}
|
||||
#else
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = OW->DataPin;
|
||||
if(Mode == Input)
|
||||
{
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
}else{
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
}
|
||||
HAL_GPIO_Init(OW->DataPort, &GPIO_InitStruct);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The internal function is used as gpio pin level
|
||||
* @param OW OneWire HandleTypedef
|
||||
* @param Mode Level: Set/High = 1, Reset/Low = 0
|
||||
*/
|
||||
void OneWire_Pin_Level(OneWire_t* OW, uint8_t Level)
|
||||
{
|
||||
#ifdef CMSIS_Driver
|
||||
if (Level != GPIO_PIN_RESET)
|
||||
{
|
||||
OW->DataPort->BSRR = OW->DataPin;
|
||||
}
|
||||
else
|
||||
{
|
||||
OW->DataPort->BSRR = (uint32_t)OW->DataPin << 16u;
|
||||
}
|
||||
#else
|
||||
#ifdef LL_Driver
|
||||
if(Level == 1)
|
||||
{
|
||||
LL_GPIO_SetOutputPin(OW->DataPort, OW->DataPin);
|
||||
}else{
|
||||
LL_GPIO_ResetOutputPin(OW->DataPort, OW->DataPin);
|
||||
}
|
||||
#else
|
||||
HAL_GPIO_WritePin(OW->DataPort, OW->DataPin, Level);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The internal function is used to read data pin
|
||||
* @retval Pin level status
|
||||
* @param OW OneWire HandleTypedef
|
||||
*/
|
||||
uint8_t OneWire_Pin_Read(OneWire_t* OW)
|
||||
{
|
||||
#ifdef CMSIS_Driver
|
||||
return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0;
|
||||
#else
|
||||
#ifdef LL_Driver
|
||||
return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0;
|
||||
#else
|
||||
return HAL_GPIO_ReadPin(OW->DataPort, OW->DataPin);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t start;
|
||||
uint16_t end;
|
||||
uint32_t tim_1us_period = 24;
|
||||
void OneWire_Delay_uw(uint32_t us)
|
||||
{
|
||||
// start = htim1.Instance->CNT;
|
||||
// end = start + us*tim_1us_period;
|
||||
htim1.Instance->CNT = 0;
|
||||
end = us*tim_1us_period;
|
||||
|
||||
while(htim1.Instance->CNT < end) {};
|
||||
}
|
||||
17
Core/Dallas/ow_port.h
Normal file
17
Core/Dallas/ow_port.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ow_port.h
|
||||
* @brief This file includes the driver for port for OneWire purposes
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef ONEWIRE_PORT_H
|
||||
#define ONEWIRE_PORT_H
|
||||
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "py32f0xx_hal.h"
|
||||
/* OneWire Timings -----------------------------------------------------------*/
|
||||
void OneWire_Delay_uw(uint32_t us);
|
||||
/* Common Register -----------------------------------------------------------*/
|
||||
#endif /* ONEWIRE_PORT_H */
|
||||
304
Core/Dallas/pch_sensors.c
Normal file
304
Core/Dallas/pch_sensors.c
Normal file
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @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;
|
||||
module->sens1.action.read = 1;
|
||||
PCHSens_AddSensor(onewire, &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);
|
||||
|
||||
module->sens3.Location.all = module->refLocation.all;
|
||||
module->sens3.Location.param.Location = 2;
|
||||
module->sens3.action.read = 1;
|
||||
PCHSens_AddSensor(onewire, &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);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
173
Core/Dallas/pch_sensors.h
Normal file
173
Core/Dallas/pch_sensors.h
Normal file
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file pch_sensors.h
|
||||
* @brief Ðàáîòà ñ äàò÷èêàìè òåìïåðàòóðû DS18B20 â Ï×
|
||||
******************************************************************************
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef PCH_SENSORS_H
|
||||
#define PCH_SENSORS_H
|
||||
|
||||
|
||||
|
||||
/* Includes -----------------------------------------------------------------*/
|
||||
#include "dallas_tools.h"
|
||||
|
||||
/* Declarations and definitions ---------------------------------------------*/
|
||||
#define USED_USER_BYTES DALLAS_USER_BYTE_12
|
||||
|
||||
/* Ïîçèöèè ïàðàìåòðîâ â UserBytes */
|
||||
#define REG_SENS_LOCATION_Pos (0) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Ëîêàöèÿ âíóòðè ìîäóëÿ" */
|
||||
|
||||
#define REG_PCH_LOCATION_Pos (8) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Ðàñïîëîæåíèå â Ï×" */
|
||||
#define REG_PCH_DIOD_PHASE_Pos (10) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Äèîäíûé èëè ôàçíûé ìîäóëü" @ref REG_PCH_LOCATION_Pos */
|
||||
#define REG_PCH_MODULE_NUMB_Pos (8) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Ïîðÿäêîâûé íîìåð äèîäíîãî/ôàçíîãî ìîäóëÿ" @ref REG_PCH_LOCATION_Pos */
|
||||
|
||||
#define REG_PCH_NUMB_Pos (11) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Ïðåîáðàçîâàòåëü ÷àñòîòû" */
|
||||
#define REG_PCH_NUMB_DIGIT_1_Pos (13) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Ïåðâàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" @ref REG_PCH_NUMB_Pos */
|
||||
#define REG_PCH_NUMB_DIGIT_2_Pos (11) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "Âòîðàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" @ref REG_PCH_NUMB_Pos */
|
||||
|
||||
#define REG_ZIP_Pos (15) /*!< @brief Ïîçèöèÿ ïàðàìåòðà "ÇÈÏ/íå ÇÈÏ" */
|
||||
|
||||
/* Ìàñêè ïàðàìåòðîâ â UserBytes */
|
||||
#define REG_SENS_LOCATION_Mask ((uint16_t)0x3 << REG_SENS_LOCATION_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Ëîêàöèÿ âíóòðè ìîäóëÿ" */
|
||||
|
||||
#define REG_PCH_LOCATION_Mask ((uint16_t)0x7 << REG_PCH_LOCATION_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Ðàñïîëîæåíèå â Ï×" */
|
||||
#define REG_PCH_DIOD_PHASE_Mask ((uint16_t)0x1 << REG_PCH_DIOD_PHASE_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Äèîäíûé èëè ôàçíûé ìîäóëü" */
|
||||
#define REG_PCH_MODULE_NUMB_Mask ((uint16_t)0x3 << REG_PCH_MODULE_NUMB_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Ïîðÿäêîâûé íîìåð äèîäíîãî/ôàçíîãî ìîäóëÿ" */
|
||||
|
||||
#define REG_PCH_NUMB_Mask ((uint16_t)0xF << REG_PCH_NUMB_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Ïðåîáðàçîâàòåëü ÷àñòîòû" */
|
||||
#define REG_PCH_NUMB_DIGIT_1_Mask ((uint16_t)0x3 << REG_PCH_NUMB_DIGIT_1_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Ïåðâàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" */
|
||||
#define REG_PCH_NUMB_DIGIT_2_Mask ((uint16_t)0x3 << REG_PCH_NUMB_DIGIT_2_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "Âòîðàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" */
|
||||
|
||||
#define REG_ZIP_Mask ((uint16_t)0x1 << REG_ZIP_Pos) /*!< @brief Ìàñêà ïàðàìåòðà "ÇÈÏ/íå ÇÈÏ" */
|
||||
|
||||
/* Âàðèàíòû ïàðàìåòðîâ â UserBytes */
|
||||
#define REG_PCH_NUMB_11 ((1 << REG_PCH_NUMB_DIGIT_1_Pos) | (1 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
#define REG_PCH_NUMB_12 ((1 << REG_PCH_NUMB_DIGIT_1_Pos) | (2 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
#define REG_PCH_NUMB_13 ((1 << REG_PCH_NUMB_DIGIT_1_Pos) | (3 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
#define REG_PCH_NUMB_21 ((2 << REG_PCH_NUMB_DIGIT_1_Pos) | (1 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
#define REG_PCH_NUMB_22 ((2 << REG_PCH_NUMB_DIGIT_1_Pos) | (2 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
#define REG_PCH_NUMB_23 ((2 << REG_PCH_NUMB_DIGIT_1_Pos) | (3 << REG_PCH_NUMB_DIGIT_2_Pos))
|
||||
|
||||
#define REG_PCH_DIODE_NUMB_1 ((0 << REG_PCH_DIOD_PHASE_Pos) | (1 << REG_PCH_MODULE_NUMB_Pos))
|
||||
#define REG_PCH_DIODE_NUMB_2 ((0 << REG_PCH_DIOD_PHASE_Pos) | (2 << REG_PCH_MODULE_NUMB_Pos))
|
||||
#define REG_PCH_DIODE_NUMB_3 ((0 << REG_PCH_DIOD_PHASE_Pos) | (3 << REG_PCH_MODULE_NUMB_Pos))
|
||||
#define REG_PCH_PHASE_NUMB_1 ((1 << REG_PCH_DIOD_PHASE_Pos) | (1 << REG_PCH_MODULE_NUMB_Pos))
|
||||
#define REG_PCH_PHASE_NUMB_2 ((1 << REG_PCH_DIOD_PHASE_Pos) | (2 << REG_PCH_MODULE_NUMB_Pos))
|
||||
#define REG_PCH_PHASE_NUMB_3 ((1 << REG_PCH_DIOD_PHASE_Pos) | (3 << REG_PCH_MODULE_NUMB_Pos))
|
||||
|
||||
/* Ïîëó÷èòü ïàðàìåòð èç UserBytes */
|
||||
#define GET_REG_SENS_LOCATION(_REG_) ((_REG_) & REG_SENS_LOCATION_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Ëîêàöèÿ âíóòðè ìîäóëÿ" */
|
||||
|
||||
#define GET_REG_PCH_LOCATION(_REG_) ((_REG_) & REG_PCH_LOCATION_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Ðàñïîëîæåíèå â Ï×" */
|
||||
#define GET_REG_PCH_DIOD_PHASE(_REG_) ((_REG_) & REG_PCH_DIOD_PHASE_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Äèîäíûé èëè ôàçíûé ìîäóëü" */
|
||||
#define GET_REG_PCH_MODULE_NUMB(_REG_) ((_REG_) & REG_PCH_MODULE_NUMB_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïîðÿäêîâûé íîìåð äèîäíîãî/ôàçíîãî ìîäóëÿ" */
|
||||
|
||||
#define GET_REG_PCH_NUMB(_REG_) ((_REG_) & REG_PCH_NUMB_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïðåîáðàçîâàòåëü ÷àñòîòû" */
|
||||
#define GET_REG_PCH_NUMB_DIGIT_1(_REG_) ((_REG_) & REG_PCH_NUMB_DIGIT_1_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïåðâàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" */
|
||||
#define GET_REG_PCH_NUMB_DIGIT_2(_REG_) ((_REG_) & REG_PCH_NUMB_DIGIT_2_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "Âòîðàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" */
|
||||
|
||||
#define GET_REG_ZIP(_REG_) ((_REG_) & REG_ZIP_Mask) /*!< @brief Ïîëó÷èòü ïàðàìåòð "ÇÈÏ/íå ÇÈÏ" */
|
||||
|
||||
/* Äèàïàçîíû ïàðàìåòðîâ èç UserBytes */
|
||||
#define REG_SENS_LOCATION_MAX 3
|
||||
#define REG_SENS_LOCATION_MIN 0
|
||||
#define REG_PCH_DIOD_PHASE_MAX 1
|
||||
#define REG_PCH_DIOD_PHASE_MIN 0
|
||||
#define REG_PCH_MODULE_NUMB_MAX 3
|
||||
#define REG_PCH_MODULE_NUMB_MIN 0
|
||||
#define REG_PCH_NUMB_DIGIT_1_MAX 3
|
||||
#define REG_PCH_NUMB_DIGIT_1_MIN 1
|
||||
#define REG_PCH_NUMB_DIGIT_2_MAX 2
|
||||
#define REG_PCH_NUMB_DIGIT_2_MIN 1
|
||||
|
||||
|
||||
/** @brief Ïîëó÷èòü ïàðàìåòð "Ëîêàöèÿ âíóòðè ìîäóëÿ" */
|
||||
#define IS_REG_SENS_LOCATION(_REG_) (((_REG_)->param.Location <= REG_SENS_LOCATION_MAX) && ((_REG_)->param.Location >= REG_SENS_LOCATION_MIN))
|
||||
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Ðàñïîëîæåíèå â Ï×" */
|
||||
#define IS_REG_PCH_LOCATION(_REG_) (IS_REG_PCH_DIOD_PHASE(_REG_) && IS_REG_PCH_MODULE_NUMB(_REG_))
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Äèîäíûé èëè ôàçíûé ìîäóëü" */
|
||||
#define IS_REG_PCH_DIOD_PHASE(_REG_) (((_REG_)->param.DiodeOrPhase <= REG_PCH_DIOD_PHASE_MAX) && ((_REG_)->param.DiodeOrPhase >= REG_PCH_DIOD_PHASE_MIN))
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïîðÿäêîâûé íîìåð äèîäíîãî/ôàçíîãî ìîäóëÿ" */
|
||||
#define IS_REG_PCH_MODULE_NUMB(_REG_) (((_REG_)->param.ModuleNumb <= REG_PCH_MODULE_NUMB_MAX) && ((_REG_)->param.ModuleNumb >= REG_PCH_MODULE_NUMB_MIN))
|
||||
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïðåîáðàçîâàòåëü ÷àñòîòû" */
|
||||
#define IS_REG_PCH_NUMB(_REG_) (IS_REG_PCH_NUMB_DIGIT_1(_REG_) && IS_REG_PCH_NUMB_DIGIT_2(_REG_))
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Ïåðâàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" */
|
||||
#define IS_REG_PCH_NUMB_DIGIT_1(_REG_) (((_REG_)->param.PCHdig1 <= REG_PCH_NUMB_DIGIT_1_MAX) && ((_REG_)->param.PCHdig1 >= REG_PCH_NUMB_DIGIT_1_MIN))
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "Âòîðàÿ öèôðà íîìåðà ïðåîáðàçîâàòåëÿ ÷àñòîòû" s*/
|
||||
#define IS_REG_PCH_NUMB_DIGIT_2(_REG_) (((_REG_)->param.PCHdig2 <= REG_PCH_NUMB_DIGIT_2_MAX) && ((_REG_)->param.PCHdig2 >= REG_PCH_NUMB_DIGIT_2_MIN))
|
||||
/*!< @brief Ïîëó÷èòü ïàðàìåòð "ÇÈÏ/íå ÇÈÏ" */
|
||||
#define IS_REG_ZIP(_REG_) (GET_REG_ZIP(_REG_))
|
||||
|
||||
typedef union
|
||||
{
|
||||
uint16_t all;
|
||||
struct
|
||||
{
|
||||
unsigned Location:2;
|
||||
unsigned reserved:6;
|
||||
unsigned ModuleNumb:2;
|
||||
unsigned DiodeOrPhase:1;
|
||||
unsigned PCHdig2:2;
|
||||
unsigned PCHdig1:2;
|
||||
unsigned ZIP:1;
|
||||
}param;
|
||||
}PCHSens_LocationTypeDef;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t connectROM;
|
||||
unsigned read;
|
||||
unsigned deinit;
|
||||
}PCHSens_SensorActionsTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
DALLAS_HandleTypeDef sens;
|
||||
// PCHSens_LocationTypeDef *UserBytes;
|
||||
PCHSens_LocationTypeDef Location;
|
||||
PCHSens_SensorActionsTypeDef action;
|
||||
unsigned not_found:1;
|
||||
}PCHSens_SensorTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
OneWire_t *onewire;
|
||||
|
||||
PCHSens_SensorTypeDef sens1;
|
||||
PCHSens_SensorTypeDef sens2;
|
||||
PCHSens_SensorTypeDef sens3;
|
||||
PCHSens_SensorTypeDef sens4;
|
||||
|
||||
PCHSens_LocationTypeDef refLocation;
|
||||
|
||||
}PCHSens_ModuleTypeDef;
|
||||
extern PCHSens_ModuleTypeDef module1;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
OneWire_t *onewire;
|
||||
|
||||
DALLAS_HandleTypeDef unknown_sensors[DS18B20_DEVICE_AMOUNT];
|
||||
uint8_t UnknownCnt;
|
||||
uint64_t ROMtoDefine;
|
||||
PCHSens_LocationTypeDef LocationtoDefine;
|
||||
PCHSens_SensorTypeDef *senstoDefine;
|
||||
}PCHSens_UnknownSensorsTypeDef;
|
||||
|
||||
|
||||
/* Functions ---------------------------------------------------------------*/
|
||||
HAL_StatusTypeDef PCHSens_FindUnknownSensors(OneWire_t *onewire, PCHSens_UnknownSensorsTypeDef *unknowns);
|
||||
HAL_StatusTypeDef PCHSens_InitNewSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor, uint64_t ROM);
|
||||
HAL_StatusTypeDef PCHSens_AddSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor);
|
||||
HAL_StatusTypeDef PCHSens_InitModule(OneWire_t *onewire, PCHSens_ModuleTypeDef* module, uint16_t param);
|
||||
HAL_StatusTypeDef PCHSens_ReadTemperature(PCHSens_ModuleTypeDef *module);
|
||||
HAL_StatusTypeDef PCHSens_CheckSensor(OneWire_t *onewire, PCHSens_SensorTypeDef* sensor);
|
||||
HAL_StatusTypeDef PCHSens_DefineUnknownSensor(PCHSens_UnknownSensorsTypeDef *unknowns, PCHSens_SensorTypeDef *sensor);
|
||||
HAL_StatusTypeDef PCHSens_SensorHandleActions(OneWire_t *onewire, PCHSens_SensorTypeDef *sensor);
|
||||
void PCHSens_FirstInit(void);
|
||||
#endif // #ifndef PCH_SENSORS_H
|
||||
Reference in New Issue
Block a user