добавил проверки на пустой указатель в драйвере ds18b20

This commit is contained in:
2025-03-03 13:45:03 +03:00
parent 884f1fc3f1
commit e4b2830fcc

View File

@@ -17,6 +17,9 @@ OneWire_t OW;
*/ */
HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM) HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM)
{ {
if(ROM == NULL)
return HAL_ERROR;
uint8_t check_family = (*ROM == DS18B20_FAMILY_CODE); uint8_t check_family = (*ROM == DS18B20_FAMILY_CODE);
/* Calculate CRC */ /* Calculate CRC */
uint8_t crc = OneWire_CRC8(ROM, 7); uint8_t crc = OneWire_CRC8(ROM, 7);
@@ -34,6 +37,9 @@ HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM)
*/ */
HAL_StatusTypeDef DS18B20_IsValid(uint8_t *ROM) HAL_StatusTypeDef DS18B20_IsValid(uint8_t *ROM)
{ {
if(ROM == NULL)
return HAL_ERROR;
if(*ROM == DS18B20_FAMILY_CODE) if(*ROM == DS18B20_FAMILY_CODE)
return HAL_OK; return HAL_OK;
else else
@@ -49,6 +55,11 @@ HAL_StatusTypeDef DS18B20_IsValid(uint8_t *ROM)
uint8_t DS18B20_GetResolution(OneWire_t* OW, uint8_t *ROM) { uint8_t DS18B20_GetResolution(OneWire_t* OW, uint8_t *ROM) {
uint8_t conf; uint8_t conf;
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
/* Check valid ROM */ /* Check valid ROM */
if (DS18B20_IsValid(ROM) != HAL_OK) if (DS18B20_IsValid(ROM) != HAL_OK)
return 0; return 0;
@@ -85,6 +96,11 @@ uint8_t DS18B20_GetResolution(OneWire_t* OW, uint8_t *ROM) {
HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM, HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM,
DS18B20_Res_t Resolution) DS18B20_Res_t Resolution)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
uint8_t th, tl, conf; uint8_t th, tl, conf;
/* Check valid ROM */ /* Check valid ROM */
@@ -146,6 +162,11 @@ HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM,
*/ */
HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM) HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
/* Check if device is DS18B20 */ /* Check if device is DS18B20 */
if(DS18B20_IsValid(ROM) != HAL_OK) if(DS18B20_IsValid(ROM) != HAL_OK)
return HAL_ERROR; return HAL_ERROR;
@@ -167,6 +188,9 @@ HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM)
*/ */
HAL_StatusTypeDef DS18B20_StartConvTAll(OneWire_t* OW) HAL_StatusTypeDef DS18B20_StartConvTAll(OneWire_t* OW)
{ {
if(OW == NULL)
return HAL_ERROR;
/* Reset pulse */ /* Reset pulse */
OneWire_Reset(OW); OneWire_Reset(OW);
@@ -189,6 +213,15 @@ 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_CalcTemperature(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad, float *Destination)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
if(Scratchpad == NULL)
return HAL_ERROR;
if(Destination == NULL)
return HAL_ERROR;
uint16_t temperature; uint16_t temperature;
uint8_t resolution; uint8_t resolution;
int8_t digit, minus = 0; int8_t digit, minus = 0;
@@ -265,6 +298,10 @@ HAL_StatusTypeDef DS18B20_CalcTemperature(OneWire_t* OW, uint8_t *ROM, uint8_t *
*/ */
HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad) HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
if(Scratchpad == NULL) if(Scratchpad == NULL)
return HAL_ERROR; return HAL_ERROR;
@@ -301,6 +338,8 @@ HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *S
*/ */
HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW) HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW)
{ {
if(OW == NULL)
return HAL_ERROR;
uint32_t tickstart = HAL_GetTick(); uint32_t tickstart = HAL_GetTick();
/* Wait until line is released, then coversion is completed */ /* Wait until line is released, then coversion is completed */
@@ -326,6 +365,11 @@ HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW)
HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low, HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low,
int8_t High) int8_t High)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
uint8_t tl, th, conf; uint8_t tl, th, conf;
/* Check if device is DS18B20 */ /* Check if device is DS18B20 */
@@ -394,6 +438,11 @@ HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low,
HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t UserBytes12, HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t UserBytes12,
int16_t UserBytes34, uint8_t UserBytesMask) int16_t UserBytes34, uint8_t UserBytesMask)
{ {
if(OW == NULL)
return HAL_ERROR;
if(ROM == NULL)
return HAL_ERROR;
uint8_t ub1, ub2, conf, ub3, ub4; uint8_t ub1, ub2, conf, ub3, ub4;
uint8_t UserByte1 = UserBytes12 & 0xFF; uint8_t UserByte1 = UserBytes12 & 0xFF;
uint8_t UserByte2 = UserBytes12 >> 8; uint8_t UserByte2 = UserBytes12 >> 8;
@@ -512,6 +561,10 @@ HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t Us
*/ */
HAL_StatusTypeDef DS18B20_Search(DS18B20_Drv_t *DS, OneWire_t *OW) HAL_StatusTypeDef DS18B20_Search(DS18B20_Drv_t *DS, OneWire_t *OW)
{ {
if(OW == NULL)
return HAL_ERROR;
OW->RomCnt = 0; OW->RomCnt = 0;
/* Search all OneWire devices ROM */ /* Search all OneWire devices ROM */
while(1) while(1)