-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dstemp.cpp
619 lines (535 loc) · 19.3 KB
/
dstemp.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* Bill Siever
* 2018-10-10
* 2021-02-27 CODAL and V2 updates
*
* Development environment specifics:
* Written in Microsoft PXT
* Based on SparkFun weather:bit for micro:bit (https://github.com/sparkfun/pxt-weather-bit)
*
* This code is released under the [MIT License](http://opensource.org/licenses/MIT).
* Please review the LICENSE.md file included with this example. If you have any questions
* or concerns with licensing, please contact techsupport@sparkfun.com.
* Distributed as-is; no warranty is given.
*/
#include "pxt.h"
#include <cstdint>
#include <math.h>
#include "app_error.h"
#include "nrf.h"
#include "MicroBitSystemTimer.h"
// Enable debugging: Debugging uses #ifdefs, so uncomment or comment out
//#define DEBUG 1
// DEBUG uses ioPin P1 to indicate sampling of read (for timing calibration)
using namespace pxt;
#if MICROBIT_CODAL
// ********************* V2/CODAL Specific Functions ***********************
// Example of port mapping from V1 codebase
// From: codal-nrf52/source/NRF52Pin.cpp
#ifdef NRF_P1
#define PORT (pin < 32 ? NRF_P0 : NRF_P1)
#define PIN ((pin) & 31)
#define NUM_PINS 48
#else
#define PORT (NRF_P0)
#define PIN (pin)
#define NUM_PINS 32
#endif
#define _wait_us(us) system_timer_wait_cycles((us)==0? 1: (((10*500*(us))/470))) // Adjusted to 11
#define _GPIO int
static void setToInput(_GPIO pin) { PORT->PIN_CNF[PIN] &= 0xfffffffc; }
static void setToOutput(_GPIO pin) { PORT->PIN_CNF[PIN] |= 3; }
static void setPinValue(_GPIO pin, int val) { if (val) PORT->OUTSET = 1 << PIN; else PORT->OUTCLR = 1 << PIN;}
static bool getPinValue(_GPIO pin) { return (PORT->IN & (1 << PIN)) ? 1 : 0; }
static void configTimer() {
// Ensure that the external crystal is being used (higher precision)
// and that the system timer is in 32-bit mode
static NRF_TIMER_Type *timer = NULL;
// If we haven't gotten the timer yet, do startup tasks, including getting the timer.
if(timer == NULL) {
NVIC_DisableIRQ(TIMER1_IRQn);
// Ensure the HFCLOCK is running
NRF_CLOCK_Type *clock = NRF_CLOCK;
clock->TASKS_HFCLKSTART = 1;
// Get the timer (ensures this is only done once)
timer = NRF_TIMER1;
// Disable timer
timer->TASKS_STOP = 1;
// Set bit mode
timer->BITMODE = 3; // Ensure 32-bit mode (error in CODAL was resulting in 24-bit mode)
// Restart it
timer->TASKS_START = 1;
NVIC_EnableIRQ(TIMER1_IRQn);
// Call timer calibration
//system_timer_calibrate_cycles();
}
}
#else
// ********************* V1/AL Specific Functions ***********************
// Map to NRF library
#define _wait_us(us) wait_us(((us)>5)?(us)-5:0)
#define _GPIO gpio_t*
#define setToInput(pin) gpio_dir((pin), PIN_INPUT)
#define setToOutput(pin) gpio_dir((pin), PIN_OUTPUT)
#define setPinValue(pin, val) gpio_write((pin), (val))
#define getPinValue(pin) gpio_read((pin))
#endif
#ifdef DEBUG
// If debugging is enabled create an "indicatePin" to assist timing tests
#if MICROBIT_CODAL
_GPIO indicatePin = uBit.io.P1.name;
#else
gpio_t indicateObj;
_GPIO indicatePin = &indicateObj;
#endif
#endif
namespace dstemp {
// ************* Forward Decalarations
void loopUntilSent(ManagedString str);
void loopUntilSent(int str);
void writeBit(_GPIO ioPin, bool one, bool finalYield = true);
void writeByte(_GPIO ioPin, uint8_t b, bool finalYield = true);
bool readBit(_GPIO ioPin);
bool readScratchpad(_GPIO ioPin, float& temp);
bool resetAndCheckPresence(_GPIO ioPin);
bool configure(_GPIO pin);
bool startConversion(_GPIO ioPin);
// ************* Timing related constants
// Times related to time slots and read/write operations
const int TIME_SLOT = 90; // Time slot length = 60-120uS T_SLOT; Rounded up from min
const int TIME_RECOV = 15; // Recovery time between bits = 1uS T_REC; Rounded up for pull-up time.
const int TIME_ZERO_LOW = TIME_SLOT; // Zero low time = 60-120uS; T_LOW0; Assume 100% of time slot
const int TIME_ONE_LOW = 0; // One Low Time = 1-15uS; T_LOW1; Hand calibrated via scope to be ~11.5uS
const int TIME_READ_START = 1; // Not used / Read start (>uS); Not used; Hand calibrated via extra call
const int TIME_READ_OFFSET = 0; // Hand Calibrated via scope; 1uS+overhead ==> 15uS from beginning of cycle (no more than 15uS)
const int TIME_SLAVE_WRITE_END = 61; // Time from start of cycle to the end of the slave impacting the bus: 15uS+45uS (rounded up)
// Times related to reset / presence detection (all in uS)
const int TIME_RESET_LOW = 500; // Reset low = 480uS T_RSTL (rounded up)
const int TIME_RESET_HIGH = 500; // Reset High/Presence detection Time = 480 uS T_RSTH (rounded up)
const int TIME_POWER_UP = 1000; // Time for DS18B20 to power up before reset (not mentioned in Data Sheet; 1ms assumed sufficient)
const int TIME_POST_RESET_TO_DETECT = 10; // Time after reset to wait before checking for presence = 15uS T_PDHIGH (rounded down)
const int TIME_PRESENCE_DETECT = 300; // Max time to wait after releasing reset for potential detect = 60uS+240uS T_PDHIGH+T_PDLOW
const int TIME_CONVERSION = 760; // Conversion Time = 750mS T_CONV (rounded up)
const float ERROR_SENTINEL = -INFINITY; // Sentinel value to return for errors.
const int HIGH_ALARM = 0xFF; // High Alarm Value (both set and confirmed; Should be non-zero)
const int LOW_ALARM = 0x80; // Low Alarm Value (both set and confirmed; Should be non-zero)
const int MAX_TRIES = 3; // Max tries to attempt conversion before fail
// ************* State variables
int errorObjectIdx = 0;
int errorPort = 0;
Action errorHandler = NULL;
// ************* Blocks
//%
void setErrorHandler(Action a) {
// Release any prior error handler
if(errorHandler)
pxt::decr(errorHandler);
errorHandler = a;
if(errorHandler)
pxt::incr(errorHandler);
}
/*
* Helper method to send an actual error code to the registered handler.
* It will set error values and immediately call the handler (i.e., no race condition should occur)
*/
void error(int objIndex, int port) {
errorObjectIdx = objIndex;
errorPort = port - MICROBIT_ID_IO_P0;
if(errorHandler) {
pxt::runAction0(errorHandler);
}
}
/* Configure the device for 12-bit conversion and set High/Low Alarm Values (Magic numbers too.)
* @param ioPin the IO pin to use
* @returns true on (assumed) success; false on known failure
*/
bool configure(_GPIO ioPin) {
if(resetAndCheckPresence(ioPin) == false) {
return false;
}
// Write ROM command: Skip ROM Command CCh: To address all devices
writeByte(ioPin, 0xCC);
// Write: Function Command Write Scratchpad 4Eh
writeByte(ioPin, 0x4E);
// Write: Data for Function command - Alarm High Byte
writeByte(ioPin, HIGH_ALARM);
// Write: Data for Function command - Alarm Low Byte
writeByte(ioPin, LOW_ALARM);
// Write: Data for Function command - Conversion Configuration Byte
writeByte(ioPin, 0x7F); // Write bits for 12-bit conversion.
return true;
}
/* Start a temperature conversion
* @param ioPin the IO pin to use
* @returns true on success; false on failure
*/
bool startConversion(_GPIO ioPin) {
if(resetAndCheckPresence(ioPin) == false) {
return false;
}
// Write ROM command: Skip ROM Command CCh: To address all devices
writeByte(ioPin, 0xCC);
// Write: Function command - Convert 44h
writeByte(ioPin, 0x44, false);
// Read Time Slot
return readBit(ioPin)==0;
}
// Calibration code that can be used to debug timing issues and
// identify values for constants
#ifdef DEBUG
void calibrate(_GPIO gpio) {
// Misc tests.
#if MICROBIT_CODAL
loopUntilSent("CODAL");
#else
loopUntilSent("DAL");
#endif
// Setup
setPinValue(gpio, 1);
setToOutput(gpio);
setPinValue(indicatePin, 1);
setToOutput(indicatePin);
setPinValue(gpio, 1);
_wait_us(100);
setPinValue(gpio, 0);
_wait_us(100);
setPinValue(gpio, 1);
_wait_us(200);
setPinValue(gpio, 0);
_wait_us(1000);
setPinValue(gpio, 1);
// Calibrate input loop
// v1: 1147 uS for 2000 iterations; 0.5735/iteration
// v2: 127 uS for 2000 iterations ; 0.0635uS/ iteration
setPinValue(indicatePin, 0);
setToInput(gpio);
uint32_t maxCounts = 2000;
bool b = true;
do {
// If the bus goes low, its a 0
b = b && getPinValue(gpio);
} while(maxCounts-- > 0);
setPinValue(indicatePin, 1);
_wait_us(100);
setPinValue(indicatePin, 0);
// v1: 3551 uS for 2000 iterations; 1.775 uS/Iteration
// v2: 346 for 2000 iterations ; 0.173uS/iter
maxCounts = 2000;
setToInput(gpio);
// Check for presence pulse
bool presence = false;
do {
presence = presence || (getPinValue(gpio) == 0);
} while (maxCounts-- > 0);
setPinValue(indicatePin, 1);
_wait_us(200);
// v1: Aim for exactly 200uS
//maxCounts = (int)(200/1.775);
// v2: Aim for exactly 200uS
maxCounts = (int)(200/0.173);
setPinValue(indicatePin, 0);
setToInput(gpio);
// Check for presence pulse
presence = false;
do {
presence = presence || (getPinValue(gpio) == 0);
} while (maxCounts-- > 0);
setPinValue(indicatePin, 1);
setToOutput(gpio);
}
#endif
//%
float celsius(int pin) {
// Only needs to be done once, but done every call...
#if MICROBIT_CODAL
// CODAL may not be using external crystal by default; Update it
// May also be using 24-bit timer
#ifdef SOFTDEVICE_PRESENT
if (!ble_running()) // Only configTimer if either no soft-dev or no ble
#endif
configTimer();
#endif
// Get corresponding I/O ioPin Object
MicroBitPin *mbp = getPin(pin); // This returns a "uBit.io.P0" type thing
#if MICROBIT_CODAL
_GPIO gpio = mbp->name;
#else
gpio_t gpioObj;
_GPIO gpio = &gpioObj;
gpio_init(gpio, mbp->name);
#endif
// If debugging, configure the indicate pin
#ifdef DEBUG
#if MICROBIT_CODAL
#else
MicroBitPin* indicate = &uBit.io.P1;
gpio_init(indicatePin, indicate->name);
#endif
setToOutput(indicatePin);
setPinValue(indicatePin, 0);
#endif
#ifdef DEBUG
// Optional calibration (rather than actually running)
// calibrate(gpio);
// return 0;
#endif
#ifdef DEBUG
{
char buffer[24];
sprintf(buffer, "pin: %d\n", mbp->name);
loopUntilSent(buffer);
loopUntilSent("Celsius Block\n");
}
#endif
bool success = false;
// 1. Check for valid device, configure it for conversion, and start conversion
for(int tries=0;tries<MAX_TRIES;tries++) {
// A. Configure Device
if(configure(gpio)==false) {
error(1, pin);
goto return_error;
}
// B. Start conversion
if(startConversion(gpio)) {
success = true;
break; // Leave the loop
}
}
// If unable to start conversion, return error
if(success==false) {
error(2, pin);
goto return_error;
}
// 2. Wait for conversion to complete
success = false;
for(int maxIterations = 20; maxIterations>0; maxIterations--) {
if(readBit(gpio) == 0) {
success = true;
break;
} else {
// Wait for conversion to complete.
uBit.sleep(0);
}
}
// If not successful, error
if(success==false) {
error(4, pin);
goto return_error;
}
// 3. Retrieve Data
for(int tries=0;tries<MAX_TRIES;tries++) {
// If reset is successful, request and read data
if(resetAndCheckPresence(gpio)) {
// Write ROM command: Skip ROM Command CCh: To address all devices
writeByte(gpio, 0xCC);
// Write Function command - Read scratchpad
writeByte(gpio, 0xBE);
// Read 8 bytes of scratch pad
float temp;
success = readScratchpad(gpio, temp);
if(success) {
errorObjectIdx = 0;
errorPort = pin;
// Return to input
setToInput(gpio);
return temp;
}
}
}
// ERROR: Max Read Tries
error(3, pin);
return_error:
// Return to input
setToInput(gpio);
// Return special sentinel value
return ERROR_SENTINEL;
}
//%
int getErrorObjectIdx() {
return errorObjectIdx;
}
//%
int getErrorPort() {
return errorPort;
}
// ************* Helper Functions
#ifdef DEBUG
/**
*
*/
// https://www.forward.com.au/pfod/microbit/gettingStarted.html
void loopUntilSent(ManagedString str) {
int rtn = uBit.serial.send(str);
while(rtn == MICROBIT_SERIAL_IN_USE) {
uBit.sleep(0); // let other tasks run
rtn = uBit.serial.send(str);
}
}
void loopUntilSent(int str) {
int rtn = uBit.serial.send(str);
while(rtn == MICROBIT_SERIAL_IN_USE) {
uBit.sleep(0); // let other tasks run
rtn = uBit.serial.send(str);
}
}
#endif
/*
* Write a bit
* @param ioPin The MicroBitPin pin to use.
* @param one A boolean: true indicates send a 1; false indicates send a 0
*/
void writeBit(_GPIO ioPin, bool one, bool finalYield) {
// Ensure recovery time
setPinValue(ioPin, 1);
setToOutput(ioPin);
_wait_us(TIME_RECOV);
// Start bus transaction
setPinValue(ioPin, 0);
// Time sensitive delay
_wait_us(one ? TIME_ONE_LOW : TIME_ZERO_LOW);
// Restore the bus
setToInput(ioPin);
setPinValue(ioPin, 1);
// Wait out rest of slot
_wait_us(one ? TIME_SLOT : 1);
}
/*
* Write a full byte
* @param ioPin the MicroBitPin pin to use.
* @param b the byte to send
*/
void writeByte(_GPIO ioPin, uint8_t b, bool finalYield) {
for(int i=0;i<8;i++,b>>=1) {
writeBit(ioPin, (b & 0x01), i!=7);
}
}
/*
* Read a single bit
* @param ioPin the MicroBitPin pin to read from
* @return true if the bit is a 1; false otherwise
*/
bool readBit(_GPIO ioPin) {
// Ensure recovery time
setToOutput(ioPin);
setPinValue(ioPin,1);
_wait_us(TIME_RECOV);
// Start the transaction
setPinValue(ioPin, 0);
_wait_us(1); // Updated to 1 for minimum wait
setPinValue(ioPin, 1);
setToInput(ioPin);
// Start high (default)
bool b = true;
#ifdef DEBUG
setPinValue(indicatePin, 1);
#endif
// Sample for ~70uS after releasing
#if MICROBIT_CODAL
// v2: 156 uS for 2000 iterations ; 0.077uS/ iteration
uint32_t maxCounts = (int)(TIME_SLOT/0.0635);
#else
// v1: 115 uS for 200 iterations; 0.575uS/iteration
_wait_us(0); // Wait for ~6uS
uint32_t maxCounts = (int)(TIME_SLOT/0.57);
#endif
do {
// If the bus goes low, its a 0
b = b && getPinValue(ioPin);
} while(maxCounts-->0);
// Switch back to output
setPinValue(ioPin,1);
#ifdef DEBUG
setPinValue(indicatePin, 0);
#endif
return b;
}
/*
* Read the DS18B20 Temperature from Scratch pad and confirm success (via High/Low and CRC).
* @param ioPin the MicroBitPin pin to use
* @param temp the temperature (on success) or
*/
// Assumes configuration and HIGH/LOW set already.
bool readScratchpad(_GPIO ioPin, float& temp) {
uint8_t data[9];
int16_t value;
uint8_t crc=0;
// Read each byte
for(int j = 0; j<9; j++) {
// read each bit (LSB to MSB)
uint8_t b = 0;
for(int i=0; i<8; i++) {
bool bit = readBit(ioPin);
b |= (bit<<i);
bool lsb = crc & 0x1;
crc >>= 1; // Shift CRC to left
if(bit != lsb) // bit xor lsb
crc ^= 0x8C;
}
data[j] = b;
}
value = data[1];
value <<= 8;
value |= data[0];
temp = value;
temp /= 16.0;
#ifdef DEBUG
{
char buffer[40];
sprintf(buffer, "data: %X %X %X %X %X %X %X %X %X\n", data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8]);
loopUntilSent(buffer);
sprintf(buffer, "crc: %X\n", crc);
loopUntilSent(buffer);
}
#endif
return crc==0 && data[2]==HIGH_ALARM && data[3]==LOW_ALARM;
}
/*
* Reset the DS18B20 on the designated pin.
* @param ioPin the pin to use for the bus
* @returns true if a device is detected on the bus following the reset; false otherwise
*/
bool resetAndCheckPresence(_GPIO ioPin) {
// Set pin to High (and get I/O object)
setToOutput(ioPin);
setPinValue(ioPin, 1);
_wait_us(TIME_POWER_UP); // Possible power-up time
// Set ioPin to output / apply reset signal
setPinValue(ioPin, 0);
_wait_us(TIME_RESET_LOW); // Wait for duration of reset pulse
// Return pin to input for presence detection
setPinValue(ioPin, 1);
setToInput(ioPin);
_wait_us(TIME_POST_RESET_TO_DETECT);
#if MICROBIT_CODAL
// v2: 462.5 for 2000 iterations ; 0.231uS/iter
// Padded down (the "release" needs to be complete)
int maxCounts = (int)(TIME_PRESENCE_DETECT/0.1); // Hand tuned values...Full presence period sample
#else
// v1: 1.705 uS/Iteration
int maxCounts = (int)(TIME_PRESENCE_DETECT/1);
#endif
// Check for presence pulse (pulling line low)
#ifdef DEBUG
setPinValue(indicatePin,1);
#endif
bool presence = false;
do {
presence = presence || (getPinValue(ioPin) == 0);
} while (maxCounts-- > 0);
// Confirm that it's released
#ifdef DEBUG
setPinValue(indicatePin,0);
#endif
bool release = getPinValue(ioPin)==1;
// Success if the pin was pulled low and went high again
bool success = presence && release;
#ifdef DEBUG
loopUntilSent("\npres= ");
loopUntilSent(presence);
loopUntilSent(" relese= ");
loopUntilSent(release);
loopUntilSent("\n");
#endif
return success; // Return success or failure
}
}