-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS18B20.c
315 lines (264 loc) · 12.5 KB
/
DS18B20.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "DS18B20.h"
#define INITIAL_SENSOR_COUNT 1
#define DS18B20_SCRATCHPAD_SIZE 9
#define DS18B20_UNIQUE_CODE_LENGTH 64
#define DS18B20_MASTER_TX_RESET_PULSE_US 480
#define DS18B20_TX_PRESENCE_PULSE_US 80
#define DS18B20_MASTER_RX_PULSE_US 400
#define DS18B20_12BIT_CONVERSION_DELAY 750
#define MATCH_ROM_COMMAND 0x55
#define SEARCH_ROM_COMMAND 0xF0
#define READ_SCRATCH_PAD_COMMAND 0xBE
#define WRITE_SCRATCH_PAD_COMMAND 0x4E
#define CONVERT_TEMPERATURE_COMMAND 0x44
#define RESOLUTION_12BIT_COMMAND 0x7F
#define BITS_IN_BYTE 8
#define TEMPERATURE_LSB_INDEX 0
#define TEMPERATURE_MSB_INDEX 1
#define MAX_TEMPERATURE_THRESHOLD 125
#define MIN_TEMPERATURE_THRESHOLD 55
#define TEMPERATURE_CONVERSION_COEFFICIENT 0.0625f
#define SIGN_BIT_POSITION 11
#define BIT_READ(value, bit) (((value) >> (bit)) & 0x01)
#define BIT_SET(value, bit) ((value) |= (1ULL << (bit)))
#define BIT_CLEAR(value, bit) ((value) &= ~(1ULL << (bit)))
#define BIT_WRITE(value, bit, bitValue) ((bitValue) ? BIT_SET((value), (bit)) : BIT_CLEAR((value), (bit)))
typedef struct AddressNavigationData {
uint8_t lastDiscrepancy;
uint64_t address;
uint8_t lastZero;
} AddressNavigationData;
typedef struct AddressItem {
GPIO_TypeDef *GPIOx;
uint32_t pin;
uint64_t address;
} AddressItem;
static Vector addressItemVector = NULL;
static char scratchPadDataArray[DS18B20_SCRATCHPAD_SIZE] = {[0 ... DS18B20_SCRATCHPAD_SIZE - 1] = 0};
static bool isSensorsForLineAlreadyDiscovered(GPIO_TypeDef *GPIOx, uint32_t pin);
static void searchDS18B20Sensors(GPIO_TypeDef *GPIOx, uint32_t pin);
static DS18B20Status searchForDeviceAddress(GPIO_TypeDef *GPIOx, uint32_t pin, AddressNavigationData *navigationData);
static AddressItem *createAddressItem(GPIO_TypeDef *GPIOx, uint32_t pin, uint64_t address);
static void assignSensorAddress(DS18B20Sensor *sensor);
static void selectDS18B20Sensor(DS18B20Sensor *sensor);
static DS18B20Status startDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin);
static void writeByteDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t byte);
static void writeBitDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t bit);
static void readScratchpadDS18B20(DS18B20Sensor *sensor);
static uint8_t readByteDS18B20(DS18B20Sensor *sensor);
static uint8_t readBitDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin);
static DS18B20Status validateCheckSum();
DS18B20Sensor *initDS18B20Sensor(GPIO_TypeDef *GPIOx, uint32_t pin) {
DS18B20Sensor *sensor = malloc(sizeof(DS18B20Sensor));
if (sensor == NULL) return NULL;
initSingletonVector(&addressItemVector, INITIAL_SENSOR_COUNT);
sensor->GPIOx = GPIOx;
sensor->pin = pin;
sensor->address = 0;
sensor->status = DS18B20_OK;
sensor->temperature = 0;
if (!isSensorsForLineAlreadyDiscovered(GPIOx, pin)) {
searchDS18B20Sensors(GPIOx, pin);
}
bool isSensorFound = isVectorNotEmpty(addressItemVector);
if (!isSensorFound) {
sensor->status = DS18B20_NOT_FOUND_ERROR;
return sensor;
}
assignSensorAddress(sensor);
selectDS18B20Sensor(sensor);
if (sensor->status == DS18B20_OK) {
writeByteDS18B20(sensor->GPIOx, sensor->pin, WRITE_SCRATCH_PAD_COMMAND);
writeByteDS18B20(sensor->GPIOx, sensor->pin, MAX_TEMPERATURE_THRESHOLD);
writeByteDS18B20(sensor->GPIOx, sensor->pin, MIN_TEMPERATURE_THRESHOLD);
writeByteDS18B20(sensor->GPIOx, sensor->pin, RESOLUTION_12BIT_COMMAND);
}
return sensor;
}
void readTemperatureDS18B20(DS18B20Sensor *sensor) {
selectDS18B20Sensor(sensor);
if (sensor->status == DS18B20_OK) {
writeByteDS18B20(sensor->GPIOx, sensor->pin, CONVERT_TEMPERATURE_COMMAND);
delay_ms(DS18B20_12BIT_CONVERSION_DELAY); // wait for conversion
selectDS18B20Sensor(sensor);
if (sensor->status == DS18B20_OK) {
writeByteDS18B20(sensor->GPIOx, sensor->pin, READ_SCRATCH_PAD_COMMAND);
readScratchpadDS18B20(sensor);
if (sensor->status == DS18B20_OK) {
uint8_t temperatureLSB = scratchPadDataArray[TEMPERATURE_LSB_INDEX];
uint8_t temperatureMSB = scratchPadDataArray[TEMPERATURE_MSB_INDEX];
uint16_t temperature = (temperatureMSB << 8) | temperatureLSB;
sensor->temperature = (float) temperature * TEMPERATURE_CONVERSION_COEFFICIENT;
bool isNegativeTemperature = (temperatureMSB & (1 << SIGN_BIT_POSITION));
if (isNegativeTemperature) {
sensor->temperature *= -1;
}
}
}
}
}
void checkConnectionStatusDS18B20(DS18B20Sensor *sensor) {
sensor->status = startDS18B20(sensor->GPIOx, sensor->pin);
if (sensor->status != DS18B20_OK) return;
writeByteDS18B20(sensor->GPIOx, sensor->pin, SEARCH_ROM_COMMAND);
for (uint8_t bitPosition = 0; bitPosition < DS18B20_UNIQUE_CODE_LENGTH; bitPosition++) {
uint8_t actualBit = readBitDS18B20(sensor->GPIOx, sensor->pin);
uint8_t complementBit = readBitDS18B20(sensor->GPIOx, sensor->pin);
bool isNoDevicesOnLine = actualBit == 1 && complementBit == 1;
if (isNoDevicesOnLine) {
sensor->status = DS18B20_NOT_FOUND_ERROR;
return;
}
writeBitDS18B20(sensor->GPIOx, sensor->pin, BIT_READ(sensor->address, bitPosition));
}
sensor->status = DS18B20_OK;
}
void deleteDS18B20(DS18B20Sensor *sensor) {
vectorDelete(addressItemVector);
free(sensor);
addressItemVector = NULL;
}
static bool isSensorsForLineAlreadyDiscovered(GPIO_TypeDef *GPIOx, uint32_t pin) {
for (uint32_t i = 0; i < getVectorSize(addressItemVector); i++) {
AddressItem *addressItem = vectorGet(addressItemVector, i);
if (addressItem->GPIOx == GPIOx && addressItem->pin == pin) {
return true;
}
}
return false;
}
static void searchDS18B20Sensors(GPIO_TypeDef *GPIOx, uint32_t pin) {
bool isLastDevice = false;
uint8_t lastDiscrepancy = 0;
while (!isLastDevice) {
DS18B20Status status = startDS18B20(GPIOx, pin);
if (status != DS18B20_OK) return;
writeByteDS18B20(GPIOx, pin, SEARCH_ROM_COMMAND);
AddressNavigationData navigationData = { .lastDiscrepancy = lastDiscrepancy, .lastZero = 0, .address = 0};
status = searchForDeviceAddress(GPIOx, pin, &navigationData);
if (status == DS18B20_NOT_FOUND_ERROR) return;
lastDiscrepancy = navigationData.lastZero;
if (!lastDiscrepancy) {
isLastDevice = true;
}
AddressItem *addressItem = createAddressItem(GPIOx, pin, navigationData.address);
vectorAdd(addressItemVector, addressItem);
}
}
static DS18B20Status searchForDeviceAddress(GPIO_TypeDef *GPIOx, uint32_t pin, AddressNavigationData *navigationData) {
for (uint8_t bitNumber = 0, idBitNumber = 1; bitNumber < DS18B20_UNIQUE_CODE_LENGTH; bitNumber++) {
uint8_t actualBit = readBitDS18B20(GPIOx, pin);
uint8_t complementBit = readBitDS18B20(GPIOx, pin);
bool isNoDevicesOnLine = actualBit == 1 && complementBit == 1;
if (isNoDevicesOnLine) {
return DS18B20_NOT_FOUND_ERROR;
}
bool isMultipleDevicesOnLine = actualBit == 0 && complementBit == 0;
if (isMultipleDevicesOnLine) {
if (idBitNumber > navigationData->lastDiscrepancy) { // First device search
BIT_WRITE(navigationData->address, bitNumber, 0);
navigationData->lastZero = idBitNumber;
writeBitDS18B20(GPIOx, pin, 0);
} else if (idBitNumber == navigationData->lastDiscrepancy) {
BIT_WRITE(navigationData->address, bitNumber, 1);
writeBitDS18B20(GPIOx, pin, 1);
} else if (idBitNumber < navigationData->lastDiscrepancy) {// if this discrepancy if before the Last Discrepancy
uint8_t directionBit = BIT_READ(navigationData->address, idBitNumber); //on a previous next then pick the same as last time
BIT_WRITE(navigationData->address, bitNumber, BIT_READ(navigationData->address, idBitNumber));
navigationData->lastZero = (directionBit == 0) ? idBitNumber : navigationData->lastZero; // if 0 was picked then record its position in lastZero
writeBitDS18B20(GPIOx, pin, directionBit);
}
} else { // only one device left
BIT_WRITE(navigationData->address, bitNumber, actualBit); // save to address
writeBitDS18B20(GPIOx, pin, actualBit); // write received bit to the line
}
idBitNumber++;
}
return DS18B20_OK;
}
static AddressItem *createAddressItem(GPIO_TypeDef *GPIOx, uint32_t pin, uint64_t address) {
AddressItem *addressItem = malloc(sizeof(AddressItem));
addressItem->GPIOx = GPIOx;
addressItem->pin = pin;
addressItem->address = address;
return addressItem;
}
static void assignSensorAddress(DS18B20Sensor *sensor) {
for (uint32_t i = 0; i < getVectorSize(addressItemVector); i++) {
AddressItem *addressItem = vectorGet(addressItemVector, i);
if (addressItem->GPIOx == sensor->GPIOx && addressItem->pin == sensor->pin) {
sensor->address = addressItem->address;
free(vectorRemoveAt(addressItemVector, i)); // remove from vector and then delete item, because one address per one device only
break;
}
}
}
static void selectDS18B20Sensor(DS18B20Sensor *sensor) {
sensor->status = startDS18B20(sensor->GPIOx, sensor->pin);
if (sensor->status != DS18B20_OK) return;
writeByteDS18B20(sensor->GPIOx, sensor->pin, MATCH_ROM_COMMAND);
for (uint8_t i = 0; i < DS18B20_UNIQUE_CODE_LENGTH; i++) {
uint8_t bit = BIT_READ(sensor->address, i);
writeBitDS18B20(sensor->GPIOx, sensor->pin, bit);
}
}
static DS18B20Status startDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin) {
DS18B20Status responseStatus;
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_OUTPUT); // set the pin as output
LL_GPIO_ResetOutputPin(GPIOx, pin); // pull the pin low
delay_us(DS18B20_MASTER_TX_RESET_PULSE_US); // delay according to datasheet
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_INPUT); // set the pin as input
delay_us(DS18B20_TX_PRESENCE_PULSE_US);
responseStatus = !LL_GPIO_IsInputPinSet(GPIOx, pin) ? DS18B20_OK : DS18B20_RESPONSE_ERROR;// if the pin is LOW i.e the presence pulse is detected
delay_us(DS18B20_MASTER_RX_PULSE_US); // 480 us delay totally.
return responseStatus;
}
static void writeByteDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t byte) {
for (uint8_t i = 0; i < BITS_IN_BYTE; i++) {
writeBitDS18B20(GPIOx, pin, BIT_READ(byte, i));
}
}
static void writeBitDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t bit) {
if (bit) { // write 1
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_OUTPUT); // set the pin as output
LL_GPIO_ResetOutputPin(GPIOx, pin); // pull the pin LOW
delay_us(1);
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_INPUT);
delay_us(60);
} else { // write 0
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_OUTPUT); // set the pin as output
LL_GPIO_ResetOutputPin(GPIOx, pin); // pull the pin LOW
delay_us(60);
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_INPUT);
delay_us(15); //wait for pull up
}
}
static void readScratchpadDS18B20(DS18B20Sensor *sensor) {
for (int i = 0; i < DS18B20_SCRATCHPAD_SIZE; i++) {
scratchPadDataArray[i] = readByteDS18B20(sensor);
}
sensor->status = validateCheckSum();
}
static uint8_t readByteDS18B20(DS18B20Sensor *sensor) {
uint8_t result = 0;
for (uint8_t i = 0; i < BITS_IN_BYTE; i++) {
uint8_t dataBit = readBitDS18B20(sensor->GPIOx, sensor->pin);
result += (dataBit << i);
}
return result;
}
static uint8_t readBitDS18B20(GPIO_TypeDef *GPIOx, uint32_t pin) {
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_OUTPUT); // set the pin as output
LL_GPIO_ResetOutputPin(GPIOx, pin); // pull the pin LOW
delay_us(2);
LL_GPIO_SetPinMode(GPIOx, pin, LL_GPIO_MODE_INPUT);
delay_us(10); // wait for pull up if the sensor do not write 0
uint8_t bit = LL_GPIO_IsInputPinSet(GPIOx, pin); // if set 1, else 0
delay_us(50); // wait for the remaining 50 us (50 + 10 = 60)
return bit;
}
static DS18B20Status validateCheckSum() {
uint8_t generatedCRC = generateCRC8(scratchPadDataArray, DS18B20_SCRATCHPAD_SIZE - 1);
uint8_t receivedCRC = scratchPadDataArray[DS18B20_SCRATCHPAD_SIZE - 1];
return (generatedCRC == receivedCRC) ? DS18B20_OK : DS18B20_CHECKSUM_ERROR;
}