Skip to content

Commit c29ecea

Browse files
committed
Improving 1-wire slave device support
1 parent 7c19f0e commit c29ecea

File tree

11 files changed

+670
-247
lines changed

11 files changed

+670
-247
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Version: 1.8
1717
* [Hardware One-Wire Bus Manager, DS2482, Hardware::OWI](./src/Hardware/OWI.h)
1818
* [Software One-Wire Slave Device, Slave::OWI](./src/Slave/OWI.h)
1919
* [Programmable Resolution 1-Wire Digital Thermometer, DS18B20](./src/Driver/DS18B20.h)
20+
* [One-Wire Interface Remove Arduino](./src/Driver/Arduino.h)
2021

2122
## Example Sketches
2223

examples/Arduino/Arduino.ino

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "GPIO.h"
2+
#include "OWI.h"
3+
#include "Software/OWI.h"
4+
#include "Driver/Arduino.h"
5+
#include "assert.h"
6+
#include "benchmark.h"
7+
8+
// One-Wire Interface Remove Arduino (Master)
9+
Software::OWI<BOARD::D7> owi;
10+
Arduino arduino(owi);
11+
12+
void setup()
13+
{
14+
Serial.begin(57600);
15+
while (!Serial);
16+
17+
uint8_t rom[owi.ROM_MAX] = { 0 };
18+
int8_t last = owi.FIRST;
19+
int id = 0;
20+
int pins;
21+
22+
// Search for device and set short address (label)
23+
last = owi.search_rom(arduino.FAMILY, rom, last);
24+
ASSERT(last != owi.ERROR);
25+
arduino.rom(rom);
26+
arduino.label_rom(id);
27+
Serial.print(id);
28+
Serial.print(':');
29+
arduino.print_rom();
30+
Serial.println();
31+
32+
// Get number of digital pins and read pin state
33+
ASSERT((pins = arduino.num_digital_pins()) > 0);
34+
Serial.print(F("arduino.num_digital_pins="));
35+
Serial.println(pins);
36+
Serial.println(F("arduino.digitalRead:"));
37+
for (int i = 0; i < pins; i++) {
38+
Serial.print('D');
39+
Serial.print(i);
40+
Serial.print(':');
41+
Serial.println(arduino.digitalRead(i));
42+
}
43+
44+
// Get number of analog inputs and read analog values
45+
ASSERT((pins = arduino.num_analog_inputs()) > 0);
46+
Serial.print(F("arduino.num_analog_inputs="));
47+
Serial.println(pins);
48+
Serial.println(F("arduino.analogRead:"));
49+
for (int i = 0; i < pins; i++) {
50+
Serial.print('A');
51+
Serial.print(i);
52+
Serial.print(':');
53+
Serial.println(arduino.analogRead(i));
54+
}
55+
56+
// Set builtin led pin to output
57+
ASSERT(arduino.pinMode(13, OUTPUT) == 0);
58+
59+
// Set pulse width modulated pin(3) to 50% duty(127)
60+
ASSERT(arduino.analogWrite(3, 127) == 0);
61+
}
62+
63+
void loop()
64+
{
65+
// Classical blink sketch loop; measure micro-seconds to perform
66+
// one-wire interface remote arduino functions
67+
MEASURE(arduino.digitalWrite(13, HIGH));
68+
delay(1000);
69+
MEASURE(arduino.digitalWrite(13, LOW));
70+
delay(1000);
71+
}

examples/ROM/ROM.ino

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "GPIO.h"
2+
#include "OWI.h"
3+
#include "Software/OWI.h"
4+
#include "assert.h"
5+
6+
Software::OWI<BOARD::D7> owi;
7+
8+
void setup()
9+
{
10+
Serial.begin(57600);
11+
while (!Serial);
12+
}
13+
14+
void loop()
15+
{
16+
// The Standard ROM functions:
17+
// 1. Read ROM from single device on the bus.
18+
// 2. Skip ROM to address all devices
19+
// 3. Match ROM to address a specific device.
20+
// 4. Search ROM to iterate devices.
21+
// 5. Alarm Search to iterate devices with alarm set.
22+
// 6. Match LABEL to use short addressing (Extended function).
23+
24+
uint8_t ROM[OWI::ROM_MAX];
25+
uint8_t label;
26+
int8_t last;
27+
uint8_t id;
28+
bool res;
29+
30+
// Read ROM from slave, and print
31+
ASSERT(owi.read_rom(ROM));
32+
Serial.print(F("read_rom:"));
33+
print(ROM);
34+
Serial.println();
35+
36+
// Address slave with skip rom
37+
ASSERT(res = owi.skip_rom());
38+
Serial.print(F("skip_rom:res="));
39+
Serial.println(res);
40+
owi.write(0);
41+
42+
// Address slave with match rom
43+
ASSERT(res = owi.match_rom(ROM));
44+
Serial.print(F("match_rom:res="));
45+
Serial.println(res);
46+
owi.write(0);
47+
48+
// Address slave with search rom
49+
id = 0;
50+
last = owi.FIRST;
51+
memset(ROM, 0, sizeof(ROM));
52+
do {
53+
last = owi.search_rom(1, ROM, last);
54+
if (last == owi.ERROR) break;
55+
label = 10;
56+
owi.write(0x15);
57+
owi.write(label);
58+
Serial.print(F("search_rom["));
59+
Serial.print(id);
60+
Serial.print(F("]:"));
61+
print(ROM);
62+
Serial.print(F(":label_rom:label="));
63+
Serial.println(label);
64+
id += 1;
65+
} while (last != owi.LAST);
66+
67+
// Address slave with alarm search
68+
last = owi.FIRST;
69+
id = 0;
70+
memset(ROM, 0, sizeof(ROM));
71+
do {
72+
last = owi.alarm_search(ROM, last);
73+
if (last == owi.ERROR) break;
74+
owi.write(0x16);
75+
label = owi.read();
76+
Serial.print(F("alarm_search["));
77+
Serial.print(id++);
78+
Serial.print(F("]:"));
79+
print(ROM);
80+
Serial.print(F(":read_label:label="));
81+
Serial.println(label);
82+
} while (last != owi.LAST);
83+
84+
// Address slave with match label
85+
ASSERT(res = owi.match_label(10));
86+
owi.write(0);
87+
Serial.print(F("match_label:res="));
88+
Serial.println(res);
89+
Serial.println();
90+
91+
delay(2000);
92+
}
93+
94+
void print(uint8_t* rom)
95+
{
96+
// Print 1-Wire ROM identity code; family, serial number and crc
97+
Serial.print(F("famly="));
98+
if (rom[0] < 0x10) Serial.print('0');
99+
Serial.print(rom[0], HEX);
100+
Serial.print(F(",sn="));
101+
for (size_t i = 1; i < OWI::ROM_MAX - 1; i++) {
102+
if (rom[i] < 0x10) Serial.print('0');
103+
Serial.print(rom[i], HEX);
104+
if (i < OWI::ROM_MAX - 2) Serial.print(',');
105+
}
106+
Serial.print(F(",crc="));
107+
uint8_t crc = rom[OWI::ROM_MAX - 1];
108+
if (crc < 0x10) Serial.print('0');
109+
Serial.print(crc, HEX);
110+
}

examples/Slave/Arduino/Arduino.ino

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "GPIO.h"
2+
#include "OWI.h"
3+
#include "Slave/OWI.h"
4+
#include "Driver/Arduino.h"
5+
6+
/** ROM identity code (in program memory). */
7+
const uint8_t ROM[OWI::ROM_MAX] PROGMEM = {
8+
Arduino::FAMILY, 0x21, 0x32, 0x43, 0x54, 0x65, 0x76, 0x80
9+
};
10+
11+
/** One-Wire Slave Device Bus Manager. */
12+
Slave::OWI<BOARD::D7> owi(ROM);
13+
14+
/**
15+
* Setup One-Wire Remote Arduino Slave Device. Set alarm
16+
* state to allow alarm search before reading analog input.
17+
*/
18+
void setup()
19+
{
20+
owi.alarm(true);
21+
}
22+
23+
/**
24+
* Service incoming one-wire rom and remote arduino functions.
25+
*/
26+
void loop()
27+
{
28+
// Additional sketch code could be placed here before
29+
// polling one-wire bus for commands
30+
if (!owi.rom_command()) return;
31+
32+
const size_t BUF_MAX = 32;
33+
uint8_t buf[BUF_MAX];
34+
uint16_t value;
35+
uint8_t pin;
36+
uint8_t mode;
37+
uint8_t* bp;
38+
uint8_t count;
39+
uint8_t res;
40+
uint8_t duty;
41+
42+
// Read and dispatch remote arduino commands
43+
switch (owi.read_command()) {
44+
case Arduino::PIN_MODE:
45+
pin = owi.read(6);
46+
mode = owi.read(2);
47+
pinMode(pin, mode);
48+
break;
49+
case Arduino::DIGITAL_READ:
50+
pin = owi.read(6);
51+
value = digitalRead(pin);
52+
owi.write(value, 1);
53+
break;
54+
case Arduino::DIGITAL_WRITE:
55+
pin = owi.read(6);
56+
value = owi.read(1);
57+
digitalWrite(pin, value);
58+
break;
59+
case Arduino::ANALOG_READ:
60+
pin = owi.read(6);
61+
value = analogRead(pin);
62+
owi.alarm(value > 512);
63+
owi.write(&value, sizeof(value));
64+
break;
65+
case Arduino::ANALOG_WRITE:
66+
pin = owi.read(6);
67+
duty = owi.read();
68+
analogWrite(pin, duty);
69+
break;
70+
case Arduino::SRAM_READ:
71+
owi.read(&bp, sizeof(bp));
72+
count = owi.read();
73+
owi.write(bp, count);
74+
break;
75+
case Arduino::SRAM_WRITE:
76+
owi.read(&bp, sizeof(bp));
77+
count = owi.read();
78+
owi.read(bp, count + 1);
79+
break;
80+
case Arduino::EEPROM_READ:
81+
owi.read(&bp, sizeof(bp));
82+
count = owi.read();
83+
owi.crc(0);
84+
while (count--) owi.write(eeprom_read_byte(bp++));
85+
owi.write(owi.crc());
86+
break;
87+
case Arduino::EEPROM_WRITE:
88+
owi.read(&bp, sizeof(bp));
89+
count = owi.read();
90+
res = 0b00;
91+
if (count > BUF_MAX) {
92+
while (count--) owi.read();
93+
owi.read();
94+
}
95+
else if (owi.read(buf, count)) {
96+
eeprom_write_block(buf, bp, count);
97+
res = 0b10;
98+
}
99+
owi.write(res, 2);
100+
break;
101+
case Arduino::DIGITAL_PINS:
102+
owi.write(NUM_DIGITAL_PINS, 6);
103+
break;
104+
case Arduino::ANALOG_PINS:
105+
owi.write(NUM_ANALOG_INPUTS, 6);
106+
break;
107+
}
108+
}

examples/Slave/DS18B20/DS18B20.ino

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum {
1818
} __attribute__((packed));
1919

2020
/**
21-
* DS18B20 Memory Map.
21+
* DS18B20 Scratchpad structure.
2222
*/
2323
struct scratchpad_t {
2424
int16_t temperature; //!< Temperature reading (9-12 bits).
@@ -29,7 +29,9 @@ struct scratchpad_t {
2929
} __attribute__((packed));
3030

3131
// ROM identity for slave device
32-
uint8_t ROM[OWI::ROM_MAX];
32+
const uint8_t ROM[OWI::ROM_MAX] PROGMEM = {
33+
FAMILY_CODE, 0x12, 0x34, 0x56, 0x78, 0x89, 0x9a, 0xab
34+
};
3335

3436
// Slave device one wire access
3537
Slave::OWI<BOARD::D7> owi(ROM);
@@ -46,16 +48,16 @@ scratchpad_t scratchpad = {
4648
// Analog pin used for emulated temperature reading
4749
const int pin = A0;
4850

51+
// Use builtin led; active during conversion delay
52+
GPIO<BOARD::D13> led;
53+
4954
void setup()
5055
{
51-
// Random ROM identity code
52-
ROM[0] = FAMILY_CODE;
53-
uint8_t* p = (uint8_t*) 0;
54-
for (size_t i = 1; i < OWI::ROM_MAX; i++) ROM[i] = *p++;
56+
led.output();
5557
}
5658

57-
// This sketch uses approx. 1900 bytes (Uno) of program storage space,
58-
// and 38 bytes for global variables (random access memory)
59+
// This sketch uses approx. 1500 bytes (Uno) of program storage space,
60+
// and less than 40 bytes for global variables (random access memory)
5961
void loop()
6062
{
6163
// Application could do something in the background before
@@ -73,9 +75,11 @@ void loop()
7375
value >>= 2;
7476
owi.alarm(value >= scratchpad.high_trigger ||
7577
value <= scratchpad.low_trigger);
78+
led.high();
7679
break;
7780
case READ_SCRATCHPAD:
7881
owi.write(&scratchpad, sizeof(scratchpad));
82+
led.low();
7983
break;
8084
case WRITE_SCRATCHPAD:
8185
owi.read(&scratchpad.high_trigger, 3);

0 commit comments

Comments
 (0)