-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathFreqTest.ino
296 lines (260 loc) · 8.27 KB
/
FreqTest.ino
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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// 2019-11-16 stan23 Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// ci-test=yes board=328p aes=no
//- -----------------------------------------------------------------------------------------------------------------------
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER
#if defined ARDUINO_ARCH_STM32F1
#define STORAGEDRIVER at24cX<0x50,128,32>
#define TICKS_PER_SECOND 500UL
#define USE_HW_SERIAL
#include <SPI.h> // when we include SPI.h - we can use LibSPI class
#include <Wire.h> // for I2C access to EEPROM
#include <EEPROM.h> // the EEPROM library contains Flash Access Methods
#endif
#include <AskSinPP.h>
#include <Device.h>
#include <Register.h>
#if defined ARDUINO_ARCH_STM32F1
// we use a BluePill
#define CC1101_GDO0_PIN PB0
#define CC1101_CS_PIN PA4
// CC1101 communication uses HW-SPI
#define LED_PIN LED_BUILTIN
#else
// we use a Pro Mini
#define CC1101_GDO0_PIN 2 // PD2
#define CC1101_CS_PIN 10 // PB2
#define CC1101_MOSI_PIN 11 // PB3
#define CC1101_MISO_PIN 12 // PB4
#define CC1101_SCK_PIN 13 // PB5
// Pro Mini LED
#define LED_PIN 4 // PD4
#endif
// all library classes are placed in the namespace 'as'
using namespace as;
// define all device properties
const struct DeviceInfo PROGMEM devinfo = {
{0x01,0x01,0x01}, // Device ID
"FreqTest00", // Device Serial
{0x00,0x00}, // Device Model
0x10, // Firmware Version
as::DeviceType::Sensor, // Device Type
{0x00,0x00} // Info Bytes
};
/**
* Configure the used hardware
*/
#if defined ARDUINO_ARCH_STM32F1
typedef LibSPI<CC1101_CS_PIN> RadioSPI;
#else
typedef AvrSPI<CC1101_CS_PIN, CC1101_MOSI_PIN, CC1101_MISO_PIN, CC1101_SCK_PIN> RadioSPI;
#endif
typedef Radio<RadioSPI, CC1101_GDO0_PIN> RadioType;
typedef StatusLed<LED_PIN> LedType;
typedef AskSin<LedType,NoBattery,RadioType> HalType;
#define STARTFREQ 0x656A // frequency we start scanning
#define MINFREQ (STARTFREQ - 0x300) // frequency we abort scanning
#define SEARCHSTEP 0x50 // step with during search
#define BOUNDSTEP 0x10 // step width during upper/lower bound analysis
// see https://github.com/AskSinPP/asksinpp.de/blob/master/Grundlagen/FAQ/Fehlerhafte_CC1101.md
// #define ACTIVE_PING
HMID PING_FROM(0x12,0x34,0x56); // from address for status message e.g. switch
HMID PING_TO(0x99,0x66,0x99); // to address for status message / central / CCU
#ifdef ACTIVE_PING
#define SCANTIME seconds2ticks(5) // maximal time to wait for a valid message
#else
#define SCANTIME seconds2ticks(60) // maximal time to wait for a valid message
#endif
class TestDevice : public Device<HalType,DefList0>, Alarm {
DefList0 l0;
uint16_t freq, start, end;
uint8_t received, rssi;
public:
enum SearchMode { Search, Up, Down, Done };
SearchMode mode;
HMID id;
typedef Device<HalType,DefList0> BaseDevice;
TestDevice (const DeviceInfo& i,uint16_t addr) : BaseDevice(i,addr,l0,0), Alarm(0), l0(addr),
freq(STARTFREQ), start(STARTFREQ), end(STARTFREQ),
received(0), rssi(0), mode(Search) {}
virtual ~TestDevice () {}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
DPRINT(" ");DDEC(received);
if (!received) {
DPRINTLN("");
} else {
DPRINT(" / -");DDEC(rssi);DPRINTLN("dBm");
}
if( mode == Search ) {
if( received > 0 ) {
start = end = freq;
mode = Up; // start find upper bound
DPRINTLN("Search for upper bound");
setFreq(freq + BOUNDSTEP);
}
else {
if( freq < MINFREQ ) mode = Done;
if( freq <= STARTFREQ ) setFreq(STARTFREQ + (STARTFREQ-freq) + SEARCHSTEP);
else setFreq(STARTFREQ - (freq-STARTFREQ));
}
}
else if(mode == Up) {
if( received > 0 ) {
end = freq;
setFreq(end + BOUNDSTEP);
}
else {
mode = Down; // start find lower bound
DPRINTLN("Search for lower bound");
setFreq(start - BOUNDSTEP);
}
}
else if(mode == Down) {
if( received > 0 ) {
start = freq;
setFreq(start - BOUNDSTEP);
}
else {
mode = Done;
}
}
if( mode == Done ) {
DPRINT("\nDone: 0x21");DHEX(start);DPRINT(" - 0x21");DHEXLN(end);
if( start == end && start == STARTFREQ ) {
DPRINT("Could not receive any message");
}
else {
freq = start+((end - start)/2);
DPRINT("Calculated Freq: 0x21");DHEX((uint8_t)(freq>>8));DHEX((uint8_t)(freq&0xff));
printFreq(0x210000 + freq);DPRINTLN("");
// store frequency
DPRINT("Store into config area: ");DHEX((uint8_t)(freq>>8));DHEX((uint8_t)(freq&0xff));
StorageConfig sc = getConfigArea();
#if defined ARDUINO_ARCH_STM32F1
Wire.begin();
DPRINT(".");
#endif
// read old frequency
uint8_t oldF1 = sc.getByte(CONFIG_FREQ1);
uint8_t oldF2 = sc.getByte(CONFIG_FREQ2);
sc.clear();
DPRINT(".");
sc.setByte(CONFIG_FREQ1, freq>>8);
DPRINT(".");
sc.setByte(CONFIG_FREQ2, freq&0xff);
DPRINT(".");
sc.validate();
DPRINTLN("stored!");
if ((oldF1 >= 0x60) && (oldF1 <= 0x6A)) {
DPRINT("\nOld Config Freq was: 0x21");DHEX(oldF1);DHEX(oldF2);
printFreq(0x210000 + oldF1*256 + oldF2);
}
#if defined ARDUINO_ARCH_STM32F1
// measurement is done, loop here forever
while(1);
#else
DPRINTLN("Going to sleep...");
Serial.flush();
activity().savePower<Sleep<> >(this->getHal());
#endif
}
}
}
virtual bool process(Message& msg) {
msg.from().dump(); DPRINT(".");
rssi = max(rssi,radio().rssi());
#ifdef ACTIVE_PING
if (msg.from() == PING_TO)
#endif
{
received++;
if( received > 0 ) {
trigger(sysclock);
}
}
return true;
}
bool init (HalType& hal) {
this->setHal(hal);
this->getDeviceID(id);
hal.init(id);
#ifdef ACTIVE_PING
DPRINT("Active ping is enabled, looking for telegrams only from ");
PING_TO.dump(); DPRINTLN("!");
#else
DPRINTLN("Active ping is NOT enabled, looking for any telegram");
#endif
DPRINTLN("Start searching ...");
setFreq(STARTFREQ);
return false;
}
void setFreq (uint16_t current) {
sysclock.cancel(*this);
freq = current;
rssi=0;
received=0;
DPRINT("Freq 0x21");DHEX(freq);
printFreq(0x210000 + freq);
DPRINT(": ");
this->radio().initReg(CC1101_FREQ2, 0x21);
this->radio().initReg(CC1101_FREQ1, freq >> 8);
this->radio().initReg(CC1101_FREQ0, freq & 0xff);
set(SCANTIME);
sysclock.add(*this);
}
void printFreq(uint32_t freq) {
char buffer[16];
float val = (float)freq * 26.0 / 65536.0;
dtostrf(val, 8, 3, buffer);
DPRINT(buffer);DPRINT(" MHz");
}
};
HalType hal;
TestDevice sdev(devinfo,0x20);
class InfoSender : public Alarm {
class channel {
public:
uint8_t number() const {return 1; }
uint8_t status() const {return 0; }
uint8_t flags() const {return 0; }
void patchStatus(__attribute__ ((unused)) Message& msg) {}
void changed(__attribute__ ((unused)) bool b) {}
};
uint8_t cnt;
channel ch;
public:
InfoSender () : Alarm(0), cnt(0) {}
virtual ~InfoSender () {}
virtual void trigger (AlarmClock& clock) {
#ifdef ACTIVE_PING
InfoActuatorStatusMsg msg;
msg.init(cnt++, ch, hal.radio.rssi());
msg.to(PING_TO);
msg.from(PING_FROM);
msg.ackRequired();
msg.setRpten();
sdev.radio().write(msg,msg.burstRequired());
#endif
sdev.led().ledOn(millis2ticks(100), 0);
if( sdev.mode != TestDevice::SearchMode::Done ) {
set(seconds2ticks(1));
clock.add(*this);
}
}
} info;
void setup () {
#if defined ARDUINO_ARCH_STM32F1
delay(5000);
#endif
DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
sdev.init(hal);
// start sender
info.trigger(sysclock);
}
void loop() {
sdev.pollRadio();
hal.runready();
}