Skip to content

Commit ba2fb14

Browse files
committed
Refactoring assert and benchmarks
1 parent d710ca5 commit ba2fb14

File tree

9 files changed

+127
-62
lines changed

9 files changed

+127
-62
lines changed

examples/Alarm/Alarm.ino

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "GPIO.h"
22
#include "OWI.h"
33
#include "Driver/DS18B20.h"
4+
#include "assert.h"
45

56
// Configure: Software/Hardware OWI Bus Manager
67
#define USE_SOFTWARE_OWI
@@ -29,16 +30,6 @@ Hardware::OWI owi(twi);
2930

3031
DS18B20 sensor(owi);
3132

32-
#define ASSERT(expr) \
33-
do { \
34-
if (!(expr)) { \
35-
Serial.print(__LINE__); \
36-
Serial.println(F(":assert:" #expr)); \
37-
Serial.flush(); \
38-
exit(0); \
39-
} \
40-
} while (0)
41-
4233
void setup()
4334
{
4435
Serial.begin(57600);

examples/Benchmark/Benchmark.ino

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "GPIO.h"
2+
#include "OWI.h"
3+
#include "benchmark.h"
4+
5+
// Configure: Software/Hardware OWI Bus Manager
6+
#define USE_SOFTWARE_OWI
7+
#if defined(USE_SOFTWARE_OWI)
8+
#include "Software/OWI.h"
9+
Software::OWI<BOARD::D7> owi;
10+
11+
#else
12+
#include "TWI.h"
13+
#include "Hardware/OWI.h"
14+
// Configure: Software/Hardware TWI Bus Manager
15+
// #define USE_SOFTWARE_TWI
16+
#if defined(USE_SOFTWARE_TWI)
17+
#include "Software/TWI.h"
18+
#if defined(SAM)
19+
Software::TWI<BOARD::D8,BOARD::D9> twi;
20+
#else
21+
Software::TWI<BOARD::D18,BOARD::D19> twi;
22+
#endif
23+
#else
24+
#include "Hardware/TWI.h"
25+
Hardware::TWI twi;
26+
#endif
27+
Hardware::OWI owi(twi);
28+
#endif
29+
30+
void setup()
31+
{
32+
Serial.begin(57600);
33+
while (!Serial);
34+
BENCHMARK_BASELINE(1);
35+
}
36+
37+
void loop()
38+
{
39+
// Measure the standard 1-wire bus manager functions
40+
// All measurements are in micro-seconds
41+
42+
uint8_t rom[owi.ROM_MAX];
43+
uint8_t dir = 0;
44+
45+
// Cyclic Redundancy Check per byte
46+
MEASURE(owi.crc_update(0,0));
47+
48+
// Reset pulse and presence check
49+
MEASURE(owi.reset());
50+
51+
// Write bit and byte
52+
MEASURE(owi.write(0,1));
53+
MEASURE(owi.write(1,1));
54+
MEASURE(owi.write(0x00));
55+
MEASURE(owi.write(0xff));
56+
57+
// Read bit and byte
58+
MEASURE(owi.read(1));
59+
MEASURE(owi.read());
60+
61+
// Triplet; read two bits and write one
62+
MEASURE(owi.triplet(dir));
63+
64+
// Standard rom functions
65+
MEASURE(owi.read_rom(rom));
66+
MEASURE(owi.skip_rom());
67+
MEASURE(owi.match_rom(rom));
68+
MEASURE(owi.search_rom(0, rom));
69+
MEASURE(owi.alarm_search(rom));
70+
71+
Serial.println();
72+
delay(2000);
73+
}

examples/DS18B20/DS18B20.ino

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#include "GPIO.h"
22
#include "OWI.h"
33
#include "Driver/DS18B20.h"
4+
#include "assert.h"
45

56
// Configure: Software/Hardware OWI Bus Manager
67
#define USE_SOFTWARE_OWI
78
#if defined(USE_SOFTWARE_OWI)
89
#include "Software/OWI.h"
10+
11+
#if defined(ARDUINO_attiny)
12+
#include "Software/Serial.h"
13+
Software::Serial<BOARD::D0> Serial;
14+
Software::OWI<BOARD::D1> owi;
15+
#else
916
Software::OWI<BOARD::D7> owi;
17+
#endif
1018

1119
#else
1220
// Configure: Software/Hardware TWI Bus Manager
@@ -29,16 +37,6 @@ Hardware::OWI owi(twi);
2937

3038
DS18B20 sensor(owi);
3139

32-
#define ASSERT(expr) \
33-
do { \
34-
if (!(expr)) { \
35-
Serial.print(__LINE__); \
36-
Serial.println(F(":assert:" #expr)); \
37-
Serial.flush(); \
38-
exit(0); \
39-
} \
40-
} while (0)
41-
4240
void setup()
4341
{
4442
Serial.begin(57600);

examples/DS1990A/DS1990A.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "OWI.h"
21
#include "GPIO.h"
2+
#include "OWI.h"
33
#include "Software/OWI.h"
44

55
#if defined(ARDUINO_attiny)

examples/Scanner/Scanner.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "OWI.h"
21
#include "GPIO.h"
2+
#include "OWI.h"
33

44
// Configure: Software/Hardware OWI Bus Manager
55
#define USE_SOFTWARE_OWI

examples/Search/Search.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "OWI.h"
21
#include "GPIO.h"
2+
#include "OWI.h"
33

44
// Configure: Software/Hardware OWI Bus Manager
55
#define USE_SOFTWARE_OWI

src/Hardware/OWI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class OWI : public ::OWI {
6969
while (bits--) {
7070
res >>= 1;
7171
m_bridge.one_wire_read_bit(value);
72-
if (value) res |= 0x80;
72+
res |= (value ? 0x80 : 0x00);
7373
}
7474
res >>= adjust;
7575
}

src/OWI.h

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ class OWI {
4545
/**
4646
* @override{OWI}
4747
* Read the given number of bits from the one wire bus. Default
48-
* number of bits is CHARBITS (8). Calculates partial check-sum.
48+
* number of bits is CHARBITS (8).
4949
* @param[in] bits to be read (default CHARBITS).
5050
* @return value read.
5151
*/
5252
virtual uint8_t read(uint8_t bits = CHARBITS) = 0;
5353

5454
/**
5555
* Read given number of bytes from one wire bus (device) to given
56-
* buffer.
56+
* buffer. Calculates 8-bit Cyclic Redundancy Check sum and return
57+
* result of check.
5758
* @param[in] buf buffer pointer.
5859
* @param[in] count number of bytes to read.
59-
* @return true(1) if correctly read otherwise false(0).
60+
* @return true(1) if check sum is correct otherwise false(0).
6061
*/
6162
bool read(void* buf, size_t count)
6263
{
@@ -80,15 +81,15 @@ class OWI {
8081
virtual void write(uint8_t value, uint8_t bits = CHARBITS) = 0;
8182

8283
/**
83-
* Write the given value and given number of bytes from buffer to
84+
* Write the given command and given number of bytes from buffer to
8485
* the one wire bus (device).
85-
* @param[in] value to write.
86+
* @param[in] cmd command to write.
8687
* @param[in] buf buffer pointer.
8788
* @param[in] count number of bytes to write.
8889
*/
89-
void write(uint8_t value, const void* buf, size_t count)
90+
void write(uint8_t cmd, const void* buf, size_t count)
9091
{
91-
write(value);
92+
write(cmd);
9293
const uint8_t* bp = (const uint8_t*) buf;
9394
while (count--) write(*bp++);
9495
}
@@ -121,6 +122,34 @@ class OWI {
121122
}
122123
}
123124

125+
/**
126+
* Standard 1-Wire ROM Commands.
127+
*/
128+
enum {
129+
SEARCH_ROM = 0xF0,//!< Initiate device search.
130+
READ_ROM = 0x33,//!< Read device family code and serial number.
131+
MATCH_ROM = 0x55,//!< Select device with 64-bit rom code.
132+
SKIP_ROM = 0xCC,//!< Broadcast or single device.
133+
ALARM_SEARCH = 0xEC//!< Initiate device alarm search.
134+
} __attribute__((packed));
135+
136+
/**
137+
* Optimized Dallas/Maxim iButton 8-bit Cyclic Redundancy Check
138+
* calculation. Polynomial: x^8 + x^5 + x^4 + 1 (0x8C).
139+
* See http://www.maxim-ic.com/appnotes.cfm/appnote_number/27
140+
*/
141+
static uint8_t crc_update(uint8_t crc, uint8_t data)
142+
{
143+
crc = crc ^ data;
144+
for (uint8_t i = 0; i < 8; i++) {
145+
if (crc & 0x01)
146+
crc = (crc >> 1) ^ 0x8C;
147+
else
148+
crc >>= 1;
149+
}
150+
return (crc);
151+
}
152+
124153
/** Search position and return values. */
125154
enum {
126155
FIRST = -1,//!< Start position of search.
@@ -250,34 +279,6 @@ class OWI {
250279
uint8_t m_rom[ROM_MAX];
251280
};
252281

253-
/**
254-
* Standard ROM Commands.
255-
*/
256-
enum {
257-
SEARCH_ROM = 0xF0,//!< Initiate device search.
258-
READ_ROM = 0x33,//!< Read device family code and serial number.
259-
MATCH_ROM = 0x55,//!< Select device with 64-bit rom code.
260-
SKIP_ROM = 0xCC,//!< Broadcast or single device.
261-
ALARM_SEARCH = 0xEC//!< Initiate device alarm search.
262-
} __attribute__((packed));
263-
264-
/**
265-
* Optimized Dallas (now Maxim) iButton 8-bit CRC calculation.
266-
* Polynomial: x^8 + x^5 + x^4 + 1 (0x8C) Initial value: 0x0
267-
* See http://www.maxim-ic.com/appnotes.cfm/appnote_number/27
268-
*/
269-
static uint8_t crc_update(uint8_t crc, uint8_t data)
270-
{
271-
crc = crc ^ data;
272-
for (uint8_t i = 0; i < 8; i++) {
273-
if (crc & 0x01)
274-
crc = (crc >> 1) ^ 0x8C;
275-
else
276-
crc >>= 1;
277-
}
278-
return (crc);
279-
}
280-
281282
protected:
282283
/** Maximum number of reset retries. */
283284
static const uint8_t RESET_RETRY_MAX = 4;
@@ -299,8 +300,10 @@ class OWI {
299300
uint8_t dir = (pos == last) || ((pos < last) && (code[i] & (1 << j)));
300301
switch (triplet(dir)) {
301302
case 0b00:
302-
if (pos == last) last = FIRST;
303-
else if (pos > last || (code[i] & (1 << j)) == 0) next = pos;
303+
if (pos == last)
304+
last = FIRST;
305+
else if (pos > last || (code[i] & (1 << j)) == 0)
306+
next = pos;
304307
break;
305308
case 0b11:
306309
return (ERROR);

src/Software/OWI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class OWI : public ::OWI {
6565
/**
6666
* @override{OWI}
6767
* Read the given number of bits from the one wire bus. Default
68-
* number of bits is 8. Calculate partial check-sum.
68+
* number of bits is 8.
6969
* @param[in] bits to be read.
7070
* @return value read.
7171
*/

0 commit comments

Comments
 (0)