Skip to content

Commit 27e2ace

Browse files
committed
Added support for building for non-AVR boards. Building was tested for Arduino Due, Arduino Zero and ESP8266 boards.
1 parent fa073c3 commit 27e2ace

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

src/dsmr/crc16.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This file was taken from Paul Stoffregen's Teensy 3 core at:
3+
*
4+
* https://github.com/PaulStoffregen/cores/tree/master/teensy3
5+
*/
6+
7+
#ifndef _UTIL_CRC16_H_
8+
#define _UTIL_CRC16_H_
9+
10+
#include <stdint.h>
11+
12+
static inline uint16_t _crc16_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
13+
static inline uint16_t _crc16_update(uint16_t crc, uint8_t data)
14+
{
15+
unsigned int i;
16+
17+
crc ^= data;
18+
for (i = 0; i < 8; ++i) {
19+
if (crc & 1) {
20+
crc = (crc >> 1) ^ 0xA001;
21+
} else {
22+
crc = (crc >> 1);
23+
}
24+
}
25+
return crc;
26+
}
27+
28+
static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
29+
static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data)
30+
{
31+
unsigned int i;
32+
33+
crc = crc ^ ((uint16_t)data << 8);
34+
for (i=0; i<8; i++) {
35+
if (crc & 0x8000) {
36+
crc = (crc << 1) ^ 0x1021;
37+
} else {
38+
crc <<= 1;
39+
}
40+
}
41+
return crc;
42+
}
43+
44+
static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
45+
static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
46+
{
47+
data ^= (crc & 255);
48+
data ^= data << 4;
49+
50+
return ((((uint16_t)data << 8) | (crc >> 8)) ^ (uint8_t)(data >> 4)
51+
^ ((uint16_t)data << 3));
52+
}
53+
54+
static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data) __attribute__((always_inline, unused));
55+
static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data)
56+
{
57+
unsigned int i;
58+
59+
crc = crc ^ data;
60+
for (i = 0; i < 8; i++) {
61+
if (crc & 0x01) {
62+
crc = (crc >> 1) ^ 0x8C;
63+
} else {
64+
crc >>= 1;
65+
}
66+
}
67+
return crc;
68+
}
69+
70+
#endif
71+

src/dsmr/parser.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
#ifndef DSMR_INCLUDE_PARSER_H
3232
#define DSMR_INCLUDE_PARSER_H
3333

34+
#ifdef ARDUINO_ARCH_AVR
3435
#include <util/crc16.h>
36+
#else
37+
#include "crc16.h"
38+
#endif
3539
#include "util.h"
3640

3741
namespace dsmr {
@@ -83,7 +87,11 @@ struct ParsedData<> {
8387
// Do not use F() for multiply-used strings (including strings used from
8488
// multiple template instantiations), that would result in multiple
8589
// instances of the string in the binary
90+
#ifndef ARDUINO_ARCH_ESP8266
8691
static constexpr char DUPLICATE_FIELD[] PROGMEM = "Duplicate field";
92+
#else
93+
static constexpr char DUPLICATE_FIELD[] = "Duplicate field";
94+
#endif
8795

8896
/**
8997
* General case: At least one typename is passed.
@@ -156,8 +164,13 @@ struct StringParser {
156164
// Do not use F() for multiply-used strings (including strings used from
157165
// multiple template instantiations), that would result in multiple
158166
// instances of the string in the binary
167+
#ifndef ARDUINO_ARCH_ESP8266
159168
static constexpr char INVALID_NUMBER[] PROGMEM = "Invalid number";
160169
static constexpr char INVALID_UNIT[] PROGMEM = "Invalid unit";
170+
#else
171+
static constexpr char INVALID_NUMBER[] = "Invalid number";
172+
static constexpr char INVALID_UNIT[] = "Invalid unit";
173+
#endif
161174

162175
struct NumParser {
163176
static ParseResult<uint32_t> parse(size_t max_decimals, const char* unit, const char *str, const char *end) {

src/dsmr/reader.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
#define DSMR_INCLUDE_READER_H
3434

3535
#include <Arduino.h>
36+
#ifdef ARDUINO_ARCH_AVR
3637
#include <util/crc16.h>
37-
38+
#else
39+
#include "crc16.h"
40+
#endif
3841
#include "parser.h"
3942

4043
namespace dsmr {
@@ -70,7 +73,7 @@ class P1Reader {
7073
* rate configured).
7174
*/
7275
P1Reader(Stream *stream, uint8_t req_pin)
73-
: stream(stream), req_pin(req_pin), once(false), state(State::DISABLED) {
76+
: stream(stream), req_pin(req_pin), once(false), state(State::DISABLED_STATE) {
7477
pinMode(req_pin, OUTPUT);
7578
digitalWrite(req_pin, LOW);
7679
}
@@ -85,7 +88,7 @@ class P1Reader {
8588
*/
8689
void enable(bool once) {
8790
digitalWrite(this->req_pin, HIGH);
88-
this->state = State::WAITING;
91+
this->state = State::WAITING_STATE;
8992
this->once = once;
9093
}
9194

@@ -96,7 +99,7 @@ class P1Reader {
9699
*/
97100
void disable() {
98101
digitalWrite(this->req_pin, LOW);
99-
this->state = State::DISABLED;
102+
this->state = State::DISABLED_STATE;
100103
if (!this->_available)
101104
this->buffer = "";
102105
// Clear any pending bytes
@@ -118,7 +121,7 @@ class P1Reader {
118121
*/
119122
bool loop() {
120123
while(true) {
121-
if (state == State::CHECKSUM) {
124+
if (state == State::CHECKSUM_STATE) {
122125
// Let the Stream buffer the CRC bytes
123126
if (this->stream->available() < CrcParser::CRC_LEN)
124127
return false;
@@ -130,7 +133,7 @@ class P1Reader {
130133
ParseResult<uint16_t> crc = CrcParser::parse(buf, buf + lengthof(buf));
131134

132135
// Prepare for next message
133-
state = State::WAITING;
136+
state = State::WAITING_STATE;
134137

135138
if (!crc.err && crc.result == this->crc) {
136139
// Message complete, checksum correct
@@ -148,22 +151,22 @@ class P1Reader {
148151
return false;
149152

150153
switch (this->state) {
151-
case State::DISABLED:
154+
case State::DISABLED_STATE:
152155
// Where did this byte come from? Just toss it
153156
break;
154-
case State::WAITING:
157+
case State::WAITING_STATE:
155158
if (c == '/') {
156-
this->state = State::READING;
159+
this->state = State::READING_STATE;
157160
// Include the / in the CRC
158161
this->crc = _crc16_update(0, c);
159162
this->clear();
160163
}
161164
break;
162-
case State::READING:
165+
case State::READING_STATE:
163166
// Include the ! in the CRC
164167
this->crc = _crc16_update(this->crc, c);
165168
if (c == '!')
166-
this->state = State::CHECKSUM;
169+
this->state = State::CHECKSUM_STATE;
167170
else
168171
buffer.concat((char)c);
169172

@@ -218,10 +221,10 @@ class P1Reader {
218221
Stream *stream;
219222
uint8_t req_pin;
220223
enum class State : uint8_t {
221-
DISABLED,
222-
WAITING,
223-
READING,
224-
CHECKSUM,
224+
DISABLED_STATE,
225+
WAITING_STATE,
226+
READING_STATE,
227+
CHECKSUM_STATE,
225228
};
226229
bool _available;
227230
bool once;

0 commit comments

Comments
 (0)