Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miscellaneous OneWire low level improvements #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 87 additions & 69 deletions ds18b20.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,68 +102,77 @@ bool Ds18b20_ManualConvert(void)
#if (_DS18B20_USE_FREERTOS==1)
void Task_Ds18b20(void const * argument)
{
uint8_t Ds18b20TryToFind=5;
do
uint8_t Ds18b20TryToFind=5;
do
{
OneWire_Init(&OneWire,_DS18B20_GPIO ,_DS18B20_PIN);
TempSensorCount = 0;
while(HAL_GetTick() < 3000)
Ds18b20Delay(100);
OneWireDevices = OneWire_First(&OneWire);
while (OneWireDevices)
{
OneWire_Init(&OneWire,_DS18B20_GPIO ,_DS18B20_PIN);
TempSensorCount = 0;
while(HAL_GetTick() < 3000)
Ds18b20Delay(100);
OneWireDevices = OneWire_First(&OneWire);
while (OneWireDevices)
{
Ds18b20Delay(100);
TempSensorCount++;
OneWire_GetFullROM(&OneWire, ds18b20[TempSensorCount-1].Address);
OneWireDevices = OneWire_Next(&OneWire);
}
if(TempSensorCount>0)
break;
Ds18b20TryToFind--;
}while(Ds18b20TryToFind>0);
if(Ds18b20TryToFind==0)
vTaskDelete(Ds18b20Handle);
for (uint8_t i = 0; i < TempSensorCount; i++)
Ds18b20Delay(100);
TempSensorCount++;
OneWire_GetFullROM(&OneWire, ds18b20[TempSensorCount-1].Address);
OneWireDevices = OneWire_Next(&OneWire);
}
if(TempSensorCount>0)
break;
Ds18b20TryToFind--;
} while(Ds18b20TryToFind>0);
if(Ds18b20TryToFind==0)
vTaskDelete(Ds18b20Handle);
for (uint8_t i = 0; i < TempSensorCount; i++)
{
Ds18b20Delay(50);
DS18B20_SetResolution(&OneWire, ds18b20[i].Address, DS18B20_Resolution_12bits);
Ds18b20Delay(50);
DS18B20_DisableAlarmTemperature(&OneWire, ds18b20[i].Address);
}
for(;;)
{
while(_DS18B20_UPDATE_INTERVAL_MS==0)
{
Ds18b20Delay(50);
DS18B20_SetResolution(&OneWire, ds18b20[i].Address, DS18B20_Resolution_12bits);
Ds18b20Delay(50);
DS18B20_DisableAlarmTemperature(&OneWire, ds18b20[i].Address);
}
for(;;)
if(Ds18b20StartConvert==1)
break;
Ds18b20Delay(10);
}
DS18B20_StartAll(&OneWire);
Ds18b20Timeout=_DS18B20_CONVERT_TIMEOUT_MS/10;
#if _DS18B20_USE_PULLPWR
ONEWIRE_HIGH(&OneWire); /// set high level
ONEWIRE_OUTPUT_PP(&OneWire); /// and set push-pull for powering the conversion
osDelay(750);
#else
osDelay(100);
while (!DS18B20_AllDone(&OneWire))
{
while(_DS18B20_UPDATE_INTERVAL_MS==0)
{
if(Ds18b20StartConvert==1)
break;
Ds18b20Delay(10);
}
Ds18b20Timeout=_DS18B20_CONVERT_TIMEOUT_MS/10;
DS18B20_StartAll(&OneWire);
osDelay(100);
while (!DS18B20_AllDone(&OneWire))
{
osDelay(10);
Ds18b20Timeout-=1;
if(Ds18b20Timeout==0)
break;
}
if(Ds18b20Timeout>0)
{
for (uint8_t i = 0; i < TempSensorCount; i++)
{
Ds18b20Delay(1000);
ds18b20[i].DataIsValid = DS18B20_Read(&OneWire, ds18b20[i].Address, &ds18b20[i].Temperature);
}
}
else
osDelay(10);
Ds18b20Timeout-=1;
if(Ds18b20Timeout==0)
break;
}
#endif
if(Ds18b20Timeout>0)
{
#if _DS18B20_USE_PULLPWR
ONEWIRE_OUTPUT(&OneWire); //// return pull-up for reading data instead of push-pull for powering conversion
#endif
for (uint8_t i = 0; i < TempSensorCount; i++)
{
for (uint8_t i = 0; i < TempSensorCount; i++)
ds18b20[i].DataIsValid = false;
//Ds18b20Delay(1000);
ds18b20[i].DataIsValid = DS18B20_Read(&OneWire, ds18b20[i].Address, &ds18b20[i].Temperature);
}
Ds18b20StartConvert=0;
osDelay(_DS18B20_UPDATE_INTERVAL_MS);
}
else
{
for (uint8_t i = 0; i < TempSensorCount; i++)
ds18b20[i].DataIsValid = false;
}
Ds18b20StartConvert=0;
osDelay(_DS18B20_UPDATE_INTERVAL_MS);
}
}
#endif
//###########################################################################################
Expand All @@ -184,17 +193,19 @@ uint8_t DS18B20_Start(OneWire_t* OneWire, uint8_t *ROM)
return 1;
}

void DS18B20_StartAll(OneWire_t* OneWire)
bool DS18B20_StartAll(OneWire_t* OneWire)
{
bool ret_val = 0;
/* Reset pulse */
OneWire_Reset(OneWire);
ret_val = !OneWire_Reset(OneWire);
/* Skip rom */
OneWire_WriteByte(OneWire, ONEWIRE_CMD_SKIPROM);
/* Start conversion on all connected devices */
OneWire_WriteByte(OneWire, DS18B20_CMD_CONVERTTEMP);
return ret_val;
}

bool DS18B20_Read(OneWire_t* OneWire, uint8_t *ROM, float *destination)
bool DS18B20_Read(OneWire_t* OneWire, uint8_t *ROM, float *destination)
{
uint16_t temperature;
uint8_t resolution;
Expand All @@ -204,11 +215,13 @@ bool DS18B20_Read(OneWire_t* OneWire, uint8_t *ROM, float *destination)
uint8_t data[9];
uint8_t crc;

/* Check if device is DS18B20 */
if (!DS18B20_Is(ROM)) {
return false;
if (ROM) /// if reading with pointer used then check if it is the address of a DS18B20
{
/* Check if device is DS18B20 */
if (!DS18B20_Is(ROM)) {
return false;
}
}

/* Check if line is released, if it is, then conversion is complete */
if (!OneWire_ReadBit(OneWire))
{
Expand All @@ -218,8 +231,13 @@ bool DS18B20_Read(OneWire_t* OneWire, uint8_t *ROM, float *destination)

/* Reset line */
OneWire_Reset(OneWire);
/* Select ROM number */
OneWire_SelectWithPointer(OneWire, ROM);
if (ROM) { /// if reading with pointer used then select device
/* Select ROM number */
OneWire_SelectWithPointer(OneWire, ROM);
}
if (!ROM) { /// if reading without selecting device used then send Skip ROM command
OneWire_WriteByte(OneWire, ONEWIRE_CMD_SKIPROM);
}
/* Read scratchpad command by onewire protocol */
OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);

Expand Down Expand Up @@ -326,7 +344,7 @@ uint8_t DS18B20_GetResolution(OneWire_t* OneWire, uint8_t *ROM)
return ((conf & 0x60) >> 5) + 9;
}

uint8_t DS18B20_SetResolution(OneWire_t* OneWire, uint8_t *ROM, DS18B20_Resolution_t resolution)
uint8_t DS18B20_SetResolution(OneWire_t* OneWire, uint8_t *ROM, DS18B20_Resolution_t resolution)
{
uint8_t th, tl, conf;
if (!DS18B20_Is(ROM))
Expand Down Expand Up @@ -400,7 +418,7 @@ uint8_t DS18B20_Is(uint8_t *ROM)
return 0;
}

uint8_t DS18B20_SetAlarmLowTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t temp)
uint8_t DS18B20_SetAlarmLowTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t temp)
{
uint8_t tl, th, conf;
if (!DS18B20_Is(ROM))
Expand Down Expand Up @@ -451,7 +469,7 @@ uint8_t DS18B20_SetAlarmLowTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t
return 1;
}

uint8_t DS18B20_SetAlarmHighTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t temp)
uint8_t DS18B20_SetAlarmHighTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t temp)
{
uint8_t tl, th, conf;
if (!DS18B20_Is(ROM))
Expand Down Expand Up @@ -502,7 +520,7 @@ uint8_t DS18B20_SetAlarmHighTemperature(OneWire_t* OneWire, uint8_t *ROM, int8_t
return 1;
}

uint8_t DS18B20_DisableAlarmTemperature(OneWire_t* OneWire, uint8_t *ROM)
uint8_t DS18B20_DisableAlarmTemperature(OneWire_t* OneWire, uint8_t *ROM)
{
uint8_t tl, th, conf;
if (!DS18B20_Is(ROM))
Expand Down
18 changes: 15 additions & 3 deletions ds18b20.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// 2018/09/08


#include "onewire.h"
#include "ds18b20Config.h"
#include "onewire.h"
#include <stdbool.h>

#if (_DS18B20_USE_FREERTOS==1)
Expand Down Expand Up @@ -51,6 +51,8 @@ extern Ds18b20Sensor_t ds18b20[_DS18B20_MAX_SENSORS];
#define DS18B20_DATA_LEN 2
#endif

//extern OneWire_t;

//###################################################################################
typedef enum {
DS18B20_Resolution_9bits = 9, /*!< DS18B20 9 bits resolution */
Expand All @@ -66,10 +68,20 @@ void Ds18b20_Init(osPriority Priority);
bool Ds18b20_Init(void);
#endif
bool Ds18b20_ManualConvert(void);
//struct OneWire_t;
//###################################################################################
uint8_t DS18B20_Start(OneWire_t* OneWireStruct, uint8_t* ROM);
void DS18B20_StartAll(OneWire_t* OneWireStruct);
bool DS18B20_Read(OneWire_t* OneWireStruct, uint8_t* ROM, float* destination);
/**
* \brief send start temperature conversion command to all devices on the bus
* \return false if no devices on the bus, otherwise true
*/
bool DS18B20_StartAll(OneWire_t* OneWireStruct);
/**
* \brief read temperature from device with address ROM or the single device
* \param [in] ROM address of the selected device or 0 if the only one device on the bus
* \return true if reading OK
*/
bool DS18B20_Read(OneWire_t* OneWireStruct, uint8_t* ROM, float* destination);
uint8_t DS18B20_GetResolution(OneWire_t* OneWireStruct, uint8_t* ROM);
uint8_t DS18B20_SetResolution(OneWire_t* OneWireStruct, uint8_t* ROM, DS18B20_Resolution_t resolution);
uint8_t DS18B20_Is(uint8_t* ROM);
Expand Down
13 changes: 10 additions & 3 deletions ds18b20Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
#define _DS18B20_GPIO DS18B20_GPIO_Port
#define _DS18B20_PIN DS18B20_Pin

#define _DS18B20_CONVERT_TIMEOUT_MS 5000
#define _DS18B20_CONVERT_TIMEOUT_MS 5000
#if (_DS18B20_USE_FREERTOS==1)
#define _DS18B20_UPDATE_INTERVAL_MS 10000 // ((( if==0 >> Ds18b20_ManualConvert() ))) ((( if>0 >>>> Auto refresh )))
#define _DS18B20_UPDATE_INTERVAL_MS 10000 // ((( if==0 >> Ds18b20_ManualConvert() ))) ((( if>0 >>>> Auto refresh )))
#endif

#define _DS18B20_TIMER htim13
#define _DS18B20_NO_NOT_USE_TIMER 1 /// use NOPs instead of hardware timer

#ifdef DEBUG
#define _DS18B20_CYCS_PER_NS 250 /// for(){NOP} cycles per nanosecond for 72 MHz core clock
#else
#define _DS18B20_CYCS_PER_NS 150 /// for(){NOP} cycles per nanosecond for 72 MHz core clock
#endif

#define _DS18B20_TIMER htim13
//###################################################################################

#endif
Expand Down
Loading