запустил измерения температуры модуля пч на py32f002b

This commit is contained in:
2025-02-28 17:56:20 +03:00
commit c2e5f70f36
489 changed files with 318323 additions and 0 deletions

552
Core/Dallas/dallas_tools.c Normal file
View 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
View 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

View 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;
}

View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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

47
Core/Inc/gpio.h Normal file
View File

@@ -0,0 +1,47 @@
/**
******************************************************************************
* @file gpio.h
* @brief This file contains all the function prototypes for
* the gpio.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
/* Exported variables prototypes ---------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/
void MX_GPIO_Init(void);
#ifdef __cplusplus
}
#endif
#endif /* __GPIO_H__ */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

51
Core/Inc/main.h Normal file
View File

@@ -0,0 +1,51 @@
/**
******************************************************************************
* @file main.h
* @author MCU Application Team
* @brief Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "py32f0xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
#define GPIO_LED_2 GPIO_PIN_1
#define GPIO_LED_3 GPIO_PIN_5
#define GPIO_LED_4 GPIO_PIN_4
/* Exported variables prototypes ---------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/
void APP_ErrorHandler(void);
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

View File

@@ -0,0 +1,214 @@
/**
******************************************************************************
* @file py32f002b_hal_conf.h
* @author MCU Application Team
* @brief HAL configuration file.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __PY32F002B_HAL_CONF_H
#define __PY32F002B_HAL_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*/
#define HAL_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
//#define HAL_ADC_MODULE_ENABLED
//#define HAL_CRC_MODULE_ENABLED
//#define HAL_COMP_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
//#define HAL_IWDG_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
//#define HAL_LPTIM_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
//#define HAL_I2C_MODULE_ENABLED
//#define HAL_UART_MODULE_ENABLED
#define HAL_USART_MODULE_ENABLED
//#define HAL_SPI_MODULE_ENABLED
//#define HAL_EXTI_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
/* ########################## Oscillator Values adaptation ####################*/
#if !defined (HSI_VALUE)
#define HSI_VALUE ((uint32_t)24000000) /*!< Value of the Internal oscillator in Hz */
#endif /* HSI_VALUE */
/**
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
* This value is used by the RCC HAL module to compute the system frequency
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)24000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (HSE_STARTUP_TIMEOUT)
#define HSE_STARTUP_TIMEOUT ((uint32_t)200) /*!< Time out for HSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */
/**
* @brief Internal Low Speed Internal oscillator (LSI) value.
*/
#if !defined (LSI_VALUE)
#define LSI_VALUE ((uint32_t)32768) /*!< LSI Typical Value in Hz */
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations
in voltage and temperature. */
/**
* @brief Adjust the value of External Low Speed oscillator (LSE) used in your application.
* This value is used by the RCC HAL module to compute the system frequency
*/
#if !defined (LSE_VALUE)
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External oscillator in Hz*/
#endif /* LSE_VALUE */
#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
#endif /* LSE_STARTUP_TIMEOUT */
/* Tip: To avoid modifying this file each time you need to use different HSE,
=== you can define the HSE value in your toolchain compiler preprocessor. */
/* ########################### System Configuration ######################### */
/**
* @brief This is the HAL system configuration section
*/
#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */
#define PRIORITY_HIGHEST 0
#define PRIORITY_HIGH 1
#define PRIORITY_LOW 2
#define PRIORITY_LOWEST 3
#define TICK_INT_PRIORITY ((uint32_t)PRIORITY_LOWEST) /*!< tick interrupt priority (lowest by default) */
#define USE_RTOS 0
#define PREFETCH_ENABLE 0
/* ########################## Assert Selection ############################## */
/**
* @brief Uncomment the line below to expanse the "assert_param" macro in the
* HAL drivers code
*/
/* #define USE_FULL_ASSERT 1U */
/* Includes ------------------------------------------------------------------*/
/**
* @brief Include module's header file
*/
#ifdef HAL_MODULE_ENABLED
#include "py32f0xx_hal.h"
#endif /* HAL_MODULE_ENABLED */
#ifdef HAL_RCC_MODULE_ENABLED
#include "py32f002b_hal_rcc.h"
#endif /* HAL_RCC_MODULE_ENABLED */
#ifdef HAL_EXTI_MODULE_ENABLED
#include "py32f002b_hal_exti.h"
#endif /* HAL_EXTI_MODULE_ENABLED */
#ifdef HAL_GPIO_MODULE_ENABLED
#include "py32f002b_hal_gpio.h"
#endif /* HAL_GPIO_MODULE_ENABLED */
#ifdef HAL_CORTEX_MODULE_ENABLED
#include "py32f002b_hal_cortex.h"
#endif /* HAL_CORTEX_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
#include "py32f002b_hal_adc.h"
#endif /* HAL_ADC_MODULE_ENABLED */
#ifdef HAL_CRC_MODULE_ENABLED
#include "py32f002b_hal_crc.h"
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_COMP_MODULE_ENABLED
#include "py32f002b_hal_comp.h"
#endif /* HAL_COMP_MODULE_ENABLED */
#ifdef HAL_FLASH_MODULE_ENABLED
#include "py32f002b_hal_flash.h"
#endif /* HAL_FLASH_MODULE_ENABLED */
#ifdef HAL_I2C_MODULE_ENABLED
#include "py32f002b_hal_i2c.h"
#endif /* HAL_I2C_MODULE_ENABLED */
#ifdef HAL_IWDG_MODULE_ENABLED
#include "py32f002b_hal_iwdg.h"
#endif /* HAL_IWDG_MODULE_ENABLED */
#ifdef HAL_PWR_MODULE_ENABLED
#include "py32f002b_hal_pwr.h"
#endif /* HAL_PWR_MODULE_ENABLED */
#ifdef HAL_SPI_MODULE_ENABLED
#include "py32f002b_hal_spi.h"
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_TIM_MODULE_ENABLED
#include "py32f002b_hal_tim.h"
#endif /* HAL_TIM_MODULE_ENABLED */
#ifdef HAL_LPTIM_MODULE_ENABLED
#include "py32f002b_hal_lptim.h"
#endif /* HAL_LPTIM_MODULE_ENABLED */
#ifdef HAL_UART_MODULE_ENABLED
#include "py32f002b_hal_uart.h"
#endif /* HAL_UART_MODULE_ENABLED */
#ifdef HAL_USART_MODULE_ENABLED
#include "py32f002b_hal_usart.h"
#endif /* HAL_USART_MODULE_ENABLED */
/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus
}
#endif
#endif /* __PY32F002B_HAL_CONF_H */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

48
Core/Inc/py32f002b_it.h Normal file
View File

@@ -0,0 +1,48 @@
/**
******************************************************************************
* @file py32f002b_it.h
* @author MCU Application Team
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __PY32F002B_IT_H
#define __PY32F002B_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Private includes ----------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/
void NMI_Handler(void);
void HardFault_Handler(void);
void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __PY32F002B_IT_H */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

52
Core/Inc/tim.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.h
* @brief This file contains all the function prototypes for
* the tim.c file
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TIM_H__
#define __TIM_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern TIM_HandleTypeDef htim1;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_TIM1_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __TIM_H__ */

64
Core/Src/gpio.c Normal file
View File

@@ -0,0 +1,64 @@
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
#include "ds18b20_driver.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure LED GPIO pin : PB0 (OneWire) */
GPIO_InitStruct.Pin = DS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(DS_GPIO_Port, &GPIO_InitStruct);
/*Configure LED GPIO pin : PA1 PA4 PA5 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

133
Core/Src/main.c Normal file
View File

@@ -0,0 +1,133 @@
/**
******************************************************************************
* @file main.c
* @author MCU Application Team
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "tim.h"
#include "pch_sensors.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint32_t predelay = 10000;
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void APP_SystemClockConfig(void);
/**
* @brief Application Entry Function.
* @retval int
*/
int main(void)
{
__HAL_DBGMCU_FREEZE_TIM1();
/* Reset of all peripherals, Initializes the Systick. */
HAL_Init();
/* System clock configuration */
APP_SystemClockConfig();
MX_GPIO_Init();
MX_TIM1_Init();
HAL_TIM_Base_Start(&htim1);
PCHSens_FirstInit();
/* infinite loop */
while (1)
{
GPIOA->ODR ^= GPIO_LED_2;
PCHSens_ReadTemperature(&module1);
}
}
/**
* @brief System clock configuration function
* @param None
* @retval None
*/
static void APP_SystemClockConfig(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Oscillator configuration */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* Select oscillator HSE, HSI, LSI, LSE */
RCC_OscInitStruct.HSIState = RCC_HSI_ON; /* Enable HSI */
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; /* HSI 1 frequency division */
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz; /* Configure HSI clock 24MHz */
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS_DISABLE; /* Close HSE bypass */
RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* Close LSI */
/*RCC_OscInitStruct.LSICalibrationValue = RCC_LSICALIBRATION_32768Hz;*/
RCC_OscInitStruct.LSEState = RCC_LSE_OFF; /* Close LSE */
/*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/
/* Configure oscillator */
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
APP_ErrorHandler();
}
/* Clock source configuration */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* Choose to configure clock HCLK, SYSCLK, PCLK1 */
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSISYS; /* Select HSISYS as the system clock */
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* AHB clock 1 division */
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* APB clock 1 division */
/* Configure clock source */
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
APP_ErrorHandler();
}
}
/**
* @brief Error executing function.
* @param None
* @retval None
*/
void APP_ErrorHandler(void)
{
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* Users can add their own printing information as needed,
for example: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

View File

@@ -0,0 +1,43 @@
/**
******************************************************************************
* @file py32f002b_hal_msp.c
* @author MCU Application Team
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* External functions --------------------------------------------------------*/
/**
* @brief Initialize global MSP.
*/
void HAL_MspInit(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
}
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

85
Core/Src/py32f002b_it.c Normal file
View File

@@ -0,0 +1,85 @@
/**
******************************************************************************
* @file py32f002b_it.c
* @author MCU Application Team
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "py32f002b_it.h"
/* Private includes ----------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* External variables --------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M0+ Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
while (1)
{
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
HAL_IncTick();
}
/******************************************************************************/
/* PY32F002B Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file. */
/******************************************************************************/
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

162
Core/Src/system_py32f002b.c Normal file
View File

@@ -0,0 +1,162 @@
/**
******************************************************************************
* @file system_py32f002b.c
* @author MCU Application Team
* @Version V1.0.0
* @Date 2020-10-19
* @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "py32f0xx.h"
#if !defined (HSE_VALUE)
#define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 24000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
#if !defined (LSI_VALUE)
#define LSI_VALUE 32768U /*!< Value of LSI in Hz*/
#endif /* LSI_VALUE */
#if !defined (LSE_VALUE)
#define LSE_VALUE 32768U /*!< Value of LSE in Hz*/
#endif /* LSE_VALUE */
/************************* Miscellaneous Configuration ************************/
/*!< Uncomment the following line if you need to relocate your vector Table in
Internal SRAM. */
/* #define FORBID_VECT_TAB_MIGRATION */
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
This value must be a multiple of 0x100. */
/******************************************************************************/
/*----------------------------------------------------------------------------
Clock Variable definitions
*----------------------------------------------------------------------------*/
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = HSI_VALUE;
const uint32_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint32_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
#if defined(RCC_HSI48M_SUPPORT)
const uint32_t HSIFreqTable[8] = {0U, 0U, 0U, 0U, 24000000U, 48000000U, 0U, 0U};
#else
const uint32_t HSIFreqTable[8] = {0U, 0U, 0U, 0U, 24000000U, 0U, 0U, 0U};
#endif
/* Private function prototypes -----------------------------------------------*/
#ifndef SWD_DELAY
static void DelayTime(uint32_t mdelay);
#endif /* SWD_DELAY */
/**
* @brief Clock functions.
* @param none
* @return none
*/
void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
{
uint32_t tmp;
uint32_t hsidiv;
uint32_t hsifs;
/* Get SYSCLK source -------------------------------------------------------*/
switch (RCC->CFGR & RCC_CFGR_SWS)
{
case RCC_CFGR_SWS_0: /* HSE used as system clock */
SystemCoreClock = HSE_VALUE;
break;
case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
SystemCoreClock = LSI_VALUE;
break;
#if defined(RCC_LSE_SUPPORT)
case RCC_CFGR_SWS_2: /* LSE used as system clock */
SystemCoreClock = LSE_VALUE;
break;
#endif /* RCC_LSE_SUPPORT */
case 0x00000000U: /* HSI used as system clock */
default: /* HSI used as system clock */
hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
break;
}
/* Compute HCLK clock frequency --------------------------------------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
/**
* @brief Setup the microcontroller system.
* Initialize the System.
* @param none
* @return none
*/
void SystemInit(void)
{
/*Set the HSI clock to 24MHz by default*/
RCC->ICSCR = (RCC->ICSCR & 0xFFFF0000) | (*(uint32_t *)(0x1FFF0100));
/*Set the LSI clock to 32.768KHz by default*/
RCC->ICSCR = (RCC->ICSCR & 0xFE00FFFF) | ((*(uint32_t *)(0x1FFF0144)) << RCC_ICSCR_LSI_TRIM_Pos);
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif /* VECT_TAB_SRAM */
#ifndef SWD_DELAY
/* When the SWD pin is reused for other functions, this function is used to solve the
problem of not being able to update the code. */
DelayTime(100);
#endif /* SWD_DELAY */
}
#ifndef SWD_DELAY
/**
* @brief This function provides delay (in milliseconds) based on CPU cycles method.
* @param mdelay: specifies the delay time length, in milliseconds.
* @retval None
*/
static void DelayTime(uint32_t mdelay)
{
__IO uint32_t Delay = mdelay * (24000000U / 8U / 1000U);
do
{
__NOP();
}
while (Delay --);
}
#endif /* SWD_DELAY */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/

107
Core/Src/tim.c Normal file
View File

@@ -0,0 +1,107 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim1;
/* TIM3 init function */
void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
HAL_RCC_GetPCLK1Freq();
/* USER CODE END TIM3_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xFFFFFFFF;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
while(1);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
while(1);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
while(1);
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* TIM3 clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM3_MspDeInit 0 */
/* USER CODE END TIM3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* TIM3 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
/* USER CODE BEGIN TIM3_MspDeInit 1 */
/* USER CODE END TIM3_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

111
Core/Src/usart.c Normal file
View File

@@ -0,0 +1,111 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */