updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / arduino-rfm12 / arduino-rfm12-01-config.patch
blobd96bec05eb5fa64e19459de95fa30a3304da81ff
1 From 898079b684d892b3b1df790f204c363635fa8361 Mon Sep 17 00:00:00 2001
2 From: Peter Hatina <phatina@gmail.com>
3 Date: Tue, 5 Jul 2011 14:59:14 +0200
4 Subject: [PATCH] store config into eeprom
6 ---
7 RF12.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++----------------
8 RF12.h | 16 ++++++++++--
9 2 files changed, 69 insertions(+), 23 deletions(-)
11 diff --git a/RF12.cpp b/RF12.cpp
12 index 87658c6..bcaa1ae 100644
13 --- a/RF12.cpp
14 +++ b/RF12.cpp
15 @@ -1,12 +1,13 @@
16 // RFM12B driver implementation
17 // 2009-02-09 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
18 // $Id: RF12.cpp 7733 2011-06-22 01:37:05Z jcw $
19 +// Copyright (C) 2011 Peter Hatina <phatina@gmail.com>
21 #include "RF12.h"
22 #include <avr/io.h>
23 -#include <util/crc16.h>
24 #include <avr/eeprom.h>
25 #include <avr/sleep.h>
26 +#include <util/crc16.h>
27 #include <WProgram.h>
29 // maximum transmit / receive buffer: 3 header + data + 2 crc bytes
30 @@ -352,30 +353,19 @@ void rf12_onOff (uint8_t value) {
31 rf12_xfer(value ? RF_XMITTER_ON : RF_IDLE_MODE);
34 -uint8_t rf12_config (uint8_t show) {
35 - uint16_t crc = ~0;
36 +uint8_t rf12_config () {
37 + uint8_t crc = ~0;
38 for (uint8_t i = 0; i < RF12_EEPROM_SIZE; ++i)
39 - crc = _crc16_update(crc, eeprom_read_byte(RF12_EEPROM_ADDR + i));
40 + crc = rf12_crc8_update(crc, eeprom_read_byte(RF12_EEPROM_ADDR + i));
41 if (crc != 0)
42 return 0;
44 - uint8_t nodeId = 0, group = 0;
45 - for (uint8_t i = 0; i < RF12_EEPROM_SIZE - 2; ++i) {
46 - uint8_t b = eeprom_read_byte(RF12_EEPROM_ADDR + i);
47 - if (i == 0)
48 - nodeId = b;
49 - else if (i == 1)
50 - group = b;
51 - else if (b == 0)
52 - break;
53 - else if (show)
54 - Serial.print(b);
55 - }
56 - if (show)
57 - Serial.println();
58 + uint8_t node = eeprom_read_byte(RF12_EEPROM_ADDR);
59 + uint8_t group = eeprom_read_byte(RF12_EEPROM_ADDR + 1);
60 + uint8_t band = eeprom_read_byte(RF12_EEPROM_ADDR + 2);
62 - rf12_initialize(nodeId, nodeId >> 6, group);
63 - return nodeId & RF12_HDR_MASK;
64 + rf12_initialize(node, band, group);
65 + return 1;
68 void rf12_sleep (char n) {
69 @@ -508,3 +498,49 @@ void rf12_encrypt (const uint8_t* key) {
70 } else
71 crypter = 0;
74 +uint8_t rf12_crc8_update(uint8_t crc, uint8_t data)
75 +{
76 + uint8_t i;
78 + crc ^= data;
79 + for(i = 0; i < 8; i++) {
80 + if(crc & 0x80)
81 + crc = (crc << 1) ^ 0xE5;
82 + else
83 + crc <<= 1;
84 + }
86 + return crc;
89 +void rf12_store_config(uint8_t node, uint8_t group, uint8_t band)
91 + uint8_t crc = ~0;
92 + eeprom_write_byte(RF12_EEPROM_ADDR, node);
93 + eeprom_write_byte(RF12_EEPROM_ADDR + 1, group);
94 + eeprom_write_byte(RF12_EEPROM_ADDR + 2, band);
96 + crc = rf12_crc8_update(crc, node);
97 + crc = rf12_crc8_update(crc, group);
98 + crc = rf12_crc8_update(crc, band);
100 + for (int i = 0; i < 16; ++i)
101 + crc = rf12_crc8_update(crc, eeprom_read_byte(RF12_EEPROM_EKEY + i));
103 + eeprom_write_byte(RF12_EEPROM_ADDR + 19, crc);
106 +void rf12_store_key(const uint32_t key[4])
108 + uint8_t crc = ~0;
109 + for (uint8_t i = 0; i < 3; ++i)
110 + crc = rf12_crc8_update(crc, eeprom_read_byte(RF12_EEPROM_ADDR + i));
112 + for (int i = 0; i < 16; ++i) {
113 + eeprom_write_byte(RF12_EEPROM_EKEY + i, ((uint8_t*) key)[i]);
114 + crc = rf12_crc8_update(crc, ((uint8_t*) key)[i]);
117 + eeprom_write_byte(RF12_EEPROM_ADDR + 19, crc);
119 diff --git a/RF12.h b/RF12.h
120 index 653d397..52bc3dc 100644
121 --- a/RF12.h
122 +++ b/RF12.h
123 @@ -1,6 +1,7 @@
124 // RFM12B driver definitions
125 // 2009-02-09 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
126 // $Id: RF12.h 7501 2011-04-07 10:33:43Z jcw $
127 +// Copyright (C) 2011 Peter Hatina <phatina@gmail.com>
129 #ifndef RF12_h
130 #define RF12_h
131 @@ -29,8 +30,8 @@
133 // EEPROM address range used by the rf12_config() code
134 #define RF12_EEPROM_ADDR ((uint8_t*) 0x20)
135 -#define RF12_EEPROM_SIZE 32
136 -#define RF12_EEPROM_EKEY (RF12_EEPROM_ADDR + RF12_EEPROM_SIZE)
137 +#define RF12_EEPROM_SIZE 20
138 +#define RF12_EEPROM_EKEY (RF12_EEPROM_ADDR + 3)
139 #define RF12_EEPROM_ELEN 16
141 // shorthand to simplify sending out the proper ACK when requested
142 @@ -52,7 +53,7 @@ void rf12_initialize(uint8_t id, uint8_t band, uint8_t group=0xD4);
143 // initialize the RF12 module from settings stored in EEPROM by "RF12demo"
144 // don't call rf12_initialize() if you init the hardware with rf12_config()
145 // returns the node ID as 1..31 value (1..26 correspond to nodes 'A'..'Z')
146 -uint8_t rf12_config(uint8_t show =1);
147 +uint8_t rf12_config();
149 // call this frequently, returns true if a packet has been received
150 uint8_t rf12_recvDone(void);
151 @@ -96,4 +97,13 @@ void rf12_encrypt(const uint8_t*);
152 // low-level control of the RFM12B via direct register access
153 uint16_t rf12_control(uint16_t cmd);
155 +// 8bit checksum
156 +uint8_t rf12_crc8_update(uint8_t crc, uint8_t data);
158 +// store config into eeprom
159 +void rf12_store_config(uint8_t node, uint8_t group, uint8_t band);
161 +// store crypt key into eeprom
162 +void rf12_store_key(const uint32_t key[4]);
164 #endif
166 1.7.6