C

apc220setup.ino
Go to the documentation of this file.
1 #include <SoftwareSerial.h>
2
3
4 /* APC220 reprogramming
5
6 * AUTO VERSION WHICH USE hw serial and pin 8 for set and if seelcted pin7 for enable radio
7 *
8 * (C)CJens Dalsgaard Nielsen (AAU)
9 * no warranty whatsoever
10 * you are on your own
11 * find m on github.com/jdn-aau/div
12 * * SEE readme.ino for more info (look up in tabs)
13 */
14
15 /**** IMPORTANT ***/
16
17
18 int shieldID = 1; ;
19
20 // NB NB NB NB NB
21 // orange radioes V4 i 435000 freq range (431-455)
22 // green radioer i 475000 freq range (455-485 ca)
23
24 unsigned long startF = 435000;
25 unsigned long stepF = 500; // only relevant for fastprogramming in manuel mode with command "p"
26 unsigned long nrStep = 0; // ...
27
28 // programming by p command p 2 == f = 439000 + 2 * 500 = 440000 kHz for radio
29
30 // and rest of parameters as below - baud rate etc setup+
31 char rfBaud = '3' ; // 1.. 4 equals 2400(1km)/4800/9600/19200bps
32 char rfPower = '9'; // 0 ..9 9(max power) equals 13dBm(20mW).
33 char uartBaud = '3'; // 0.. 4 equals 1200/2400/4800/9600(3)/19200/
34 char uartParChk = '0'; // 0/1/2 equals NoCheck(8N1)/EvenParity(8E1)/OddParity
35
36 // ex: 3 9 3 0 : 9600 in air , full power, 9600 on rs232, no parity
37
38
39
40
41 /* READ HERE READ HERE
42 *
43 * Jumpers
44 * 1 on - connect setup pin to D8
45 * J2 on - connect radio tx to arduino rx (pin 0)
46 * J3 on - connect radio rx to arduino tx (pin 1)
47 * J4 and J5 off - no connection to openlog
48 *
49 * Setting parameters prior to auto programming
50 * See fields below like rfBaud, rfPower etc
51
52 * remove jumper 4 and 5
53 * remove USB
54 * attach battery and start Arduino on battery
55 * wait 10 seconds or if AUTOMODELIGHT is defined it is finished when blinking LED13
56 * remove battery
57 * remove jumper 1
58 * and program your normal progrtam :-)
59 * happy hacking
60 * ALSO FOR OLD SHIELD !!!
61 * automode can also be used for old shield
62 * In addition to the setup above setup pin has to be manually connected to D8 by wire
63 * Then just go for mode 4 (shieldID = 4;)
64 */
65
66
67
68
69
70 /**----------------------------------------------------------------------------------*/
71 void (*resetF)(void) = 0x00;
72
73 const int vrs = 3141;
74
75
76 char radioRcvStr[25]; // for receving from radio
77 /**----------------------------------------------------------------------------------*/
78
79 // enable pin turn radio on(HIGH). off(LOW)
80 // by software serial
81 #define ENPIN12 12
82 #define ENPIN7 7
83
84 // setpin radio on(high) off(low)
85 #define SETPIN 8
86
87 #define RXPIN 10
88 #define TXPIN 11
89
90 // D13 supply radio with power
91 #define FIVEV 13
92
93 // auto program for boardID + AUTOOFFSET
94 #define AUTOOFFSET 100
95
96 int enPin;
97
98 /**----------------------------------------------------------------------------------*/
99
100 #define PARM_LGT 10
101 #define CMD_BUF_LGT 50
102
103 char var1[PARM_LGT], var2[PARM_LGT], var3[PARM_LGT], var4[PARM_LGT], var5[PARM_LGT], var6[PARM_LGT];
104
105 SoftwareSerial apc220(RXPIN, TXPIN); // Crt softserial port and bind tx/rx to appropriate PINS
106
107 void emptySerial()
108 {
109 int c;
110 while (Serial.available())
111 c = Serial.read();
112 }
113
114
115 /**----------------------------------------------------------------------------------*/
116 char st[200];
117 void radioFastProg(int index)
118 {
119 int i = 0;
120
121 st[i++] = ' W';
122 st[i++] = ' R';
123 st[i++] = ' ' ;
124
125 ltoa(startF + index * stepF, st + i, 10);
126 i = 0;
127 while ( st[i] != 0x00)
128 i++;
129
130 st[i++] = ' ';
131 st[i++] = rfBaud;
132 st[i++] = ' ';
133 st[i++] = rfPower;
134 st[i++] = ' ';
135 st[i++] = uartBaud;
136 st[i++] = ' ';
137 st[i++] = uartParChk;
138 st[i] = 0x00;
139
140 delay(10);
141
142 radioPrintln(st);
143
144 delay(20); // critical bq we are using softserial - so you have to be present in read for not loosing chars
145
146 while (radioAvailable()) {
147 i = radioRead();
148 if (!shieldID) {
149 Serial.print((char)(i));
150 }
151 delay(10);
152 }
153 delay(10);
154
155 }
156
157 /**----------------------------------------------------------------------------------*/
158 void set_para(char hz[], char rf_rate[], char pwr[], char uart_rate[], char sc[])
159 {
160 // sanity chk
161 if (strlen(hz) != 6) {
162 Serial.println("Freq parm not 6 digits... - legal is 418000 - 455000");
163 return;
164 }
165 if (strlen(rf_rate) != 1 ) {
166 Serial.println("RF parm is not 1 digit: legal values is 1/2/3/4");
167 return;
168 }
169 if (strlen(pwr) != 1 ) {
170 Serial.println("Power parm is not 1 digit: legal values is 1..9");
171 return;
172 }
173 if (strlen(uart_rate) != 1 ) {
174 Serial.println("Uart baudrate parm is not 1 digit: legal values is 0..6");
175 return;
176 }
177 if (strlen(sc) != 1 ) {
178 Serial.println("Parity parm is not 1 digit: legal values is 0/1/2");
179 return;
180 }
181
182 Serial.println("programming");
183
184 delay(10);
185 radioPrint("WR");
186 radioPrint(" ");
187
188 radioPrint(hz);
189 radioPrint(" ");
190
191 radioPrint(rf_rate);
192 radioPrint(" ");
193
194 radioPrint(pwr);
195 radioPrint(" ");
196
197 radioPrint(uart_rate);
198 radioPrint(" ");
199
200 radioPrint(sc);
201
202 radioPrintln("");
203
204
205 //0x0D);
206 //radioWriteCh(0x0A);
207 delay(10);
208
209 // read feedback from radio
210
211 while (radioAvailable()) {
212 Serial.print((char)(radioRead()));
213 }
214
215 }
216
217 /**----------------------------------------------------------------------------------*/
218 void get_para(char s[])
219 {
220 int i = 0;
221
222 delay(200); // wait a second
223
224
225 radioPrintln("RD");
226 delay(10); // critical !
227
228 while (radioAvailable()) {
229 s[i] = radioRead();
230 i++;
231 // Serial.print((char)(radioRead()));
232 delay(5);
233 }
234
235 i = 0;
236 while ((s[i] != 0x0a) && (s[i] != 0x0d))
237 i++;
238 s[i] = 0x00; //termination of string
239 }
240
241
242 boolean isRadioPresent()
243 {
244 radioRcvStr[0] = 0x00;
245 get_para(radioRcvStr);
246 if ( (radioRcvStr[0] == 'P') && (radioRcvStr[1] == 'A') && (radioRcvStr[2] == 'R'))
247 return true;
248 else
249 return false;
250 }
251
252
253 /**----------------------------------------------------------------------------------*/
254 void setupShield(int id)
255 {
256 shieldID = id;
257 radioConfig(shieldID);
258 delay(20);
259 configPinInit();
260 configModeOff();
261 delay(500);
262 }
263
264 /**----------------------------------------------------------------------------------*/
265 void do_cmd(void) {
266 int i = 0; char c;
267 char buff[CMD_BUF_LGT];
268
269 emptySerial();
270
271 menu();
272
273 buff[0] = 0;
274 c = 0x00;
275
276 while (c != 0x0a && c != 0x0d && i < CMD_BUF_LGT) {
277 if (Serial.available()) {
278 buff[i++] = c = Serial.read();
279 }
280 delay(5);
281 }
282
283 buff[i] = 0x00;
284
285 var1[0] = 0x00; // reset
286 Serial.println(buff);
287 if (0 == sscanf(buff, "%s %s %s %s %s %s", var1, var2, var3, var4, var5, var6)) {
288 return; //err
289 }
290 configModeOn();
291 switch (var1[0]) { // one letter commands :-)
292 case 'r':
293 {
294 Serial.print(">>> Read radio:");
295 if (isRadioPresent())
296 Serial.println(radioRcvStr);
297 else
298 Serial.println(F("no radio found on pin 8-13"));
299 delay(1000);
300 break;
301 }
302 case 'w':
303 {
304 Serial.print("write(config) radio ");
305 set_para(var2, var3, var4, var5, var6);
306 delay(1000);
307 break;
308 }
309 case 'p':
310 {
311 long l;
312 l = 0;
313 l = atol(var2);
314 Serial.println(l);
315 radioFastProg(l);
316 break;
317 }
318 case 'P': {
319 drawRadio();
320 break;
321 }
322 case 'R': resetF();
323 break;
324 case 'b':
325 beacons();
326 break;
327 case 'c':
328 {
329 int i;
330 for (i = 0 ; i < 60; i++)
331 Serial.println("");
332 break;
333 }
334 default:
335 {
336 Serial.println("BAD COMMAND...");
337 delay(500);
338 }
339 }
340 configModeOff();
341 }
342
343 void beacons()
344 {
345 delay(1000);
346 configModeOn();
347 delay(20);
348 isRadioPresent();
349 configModeOff();
350 delay(200);
351 radioWrite("BEACONMODE\n");
352 while (1) {
353 radioWrite("ping \n");
354 delay(1000);
355 }
356 }
357
358 boolean radioOn(int p)
359 {
360 setupShield(p); // test for radio on p8-13
361
362 delay(200);
363
364 for (int i = 0 ; i < 3 ; i++) {
365 configModeOn();
366 if (isRadioPresent())
367 return true;
368 configModeOff();
369 delay(500); // one more trial
370 }
371
372 delay(500);
373 return false;
374 }
375
376 /**----------------------------------------------------------------------------------*/
377 void setup() {
378 int jj;
379 delay(500);
380 pinMode(13, OUTPUT);
381 digitalWrite(13, LOW);
382
383 Serial.begin(9600);
384
385 delay(100);
386
387 dmpProgID(); // version of program
388
389 Serial.println(F("Test starts for presence on apc220 radios in 1 second"));
390
391 delay(500);
392
393 Serial.print(F("APC220 on dig pin 8-13 ?.. "));
394
395 delay(500);
396
397 if (radioOn(0)) {
398 goto xx;
399 }
400
401 delay(200);
402
403 Serial.println(" nope");
404
405 delay(200);
406 digitalWrite(13, LOW); // radio power off
407 delay(200);
408 // let us test if the radio is on a shield and connected to HW serial port
409
410 Serial.print(F("APC220 on AAU shield (and setup pin connected to dig pin 8) ? "));
411 delay(1000);
412 if (radioOn(1))
413 goto xx;
414
415 delay(500);
416
417 Serial.println(" nope - beacon mode now");
418
419 while (1) {
420 radioWrite("ping\n ");
421 delay(1000);
422 }
423
424 xx:
425 configModeOff();
426 delay(400);
427 Serial.print(" YEP - configurable radio found - configured as ");
428 Serial.println(radioRcvStr);
429
430 if (shieldID) {
431 Serial.println("Auto programming on HW tx/rx in 3 seconds");
432 delay(3000);
433 }
434 }
435
436 /**----------------------------------------------------------------------------------*/
437 void loop()
438 {
439
440 if (!shieldID) {
441
442 do_cmd();
443 }
444 else { /* autoprogramming */
445 delay(1000);
446 configModeOn();
447 Serial.println("Reprogramming on Serial port now");
448 delay(500);
449 radioFastProg(0);
450 configModeOff();
451
452 digitalWrite(13, HIGH); // Finish
453 beacons();
454 }
455 }
drawRadio
void drawRadio()
Definition: drawi.ino:2
radioRead
int radioRead()
Definition: rdwrapc220.ino:115
vrs
const int vrs
Definition: apc220setup.ino:73
radioPrintln
void radioPrintln(char x[])
Definition: rdwrapc220.ino:88
PARM_LGT
#define PARM_LGT
Definition: apc220setup.ino:100
radioConfig
void radioConfig(int s)
Definition: rdwrapc220.ino:36
TXPIN
#define TXPIN
Definition: apc220setup.ino:88
enPin
int enPin
Definition: apc220setup.ino:96
beacons
void beacons()
Definition: apc220setup.ino:343
radioOn
boolean radioOn(int p)
Definition: apc220setup.ino:358
apc220
SoftwareSerial apc220(RXPIN, TXPIN)
var5
char var5[PARM_LGT]
Definition: apc220setup.ino:103
radioFastProg
void radioFastProg(int index)
Definition: apc220setup.ino:117
configModeOn
void configModeOn()
Definition: rdwrapc220.ino:2
uartParChk
char uartParChk
Definition: apc220setup.ino:34
configPinInit
void configPinInit()
Definition: rdwrapc220.ino:15
var3
char var3[PARM_LGT]
Definition: apc220setup.ino:103
var1
char var1[PARM_LGT]
Definition: apc220setup.ino:103
var2
char var2[PARM_LGT]
Definition: apc220setup.ino:103
CMD_BUF_LGT
#define CMD_BUF_LGT
Definition: apc220setup.ino:101
resetF
void(* resetF)(void)=0x00
Definition: apc220setup.ino:71
radioRcvStr
char radioRcvStr[25]
Definition: apc220setup.ino:76
setup
void setup()
Definition: apc220setup.ino:377
set_para
void set_para(char hz[], char rf_rate[], char pwr[], char uart_rate[], char sc[])
Definition: apc220setup.ino:158
rfBaud
char rfBaud
Definition: apc220setup.ino:31
menu
void menu(void)
Definition: cmd.ino:37
emptySerial
void emptySerial()
Definition: apc220setup.ino:107
startF
unsigned long startF
Definition: apc220setup.ino:24
stepF
unsigned long stepF
Definition: apc220setup.ino:25
setupShield
void setupShield(int id)
Definition: apc220setup.ino:254
radioPrint
void radioPrint(char x[])
Definition: rdwrapc220.ino:96
loop
void loop()
Definition: apc220setup.ino:437
nrStep
unsigned long nrStep
Definition: apc220setup.ino:26
dmpProgID
void dmpProgID()
Definition: cmd.ino:11
configModeOff
void configModeOff()
Definition: rdwrapc220.ino:9
shieldID
int shieldID
Definition: apc220setup.ino:18
radioWrite
void radioWrite(char x[])
Definition: rdwrapc220.ino:79
isRadioPresent
boolean isRadioPresent()
Definition: apc220setup.ino:242
radioAvailable
int radioAvailable()
Definition: rdwrapc220.ino:106
do_cmd
void do_cmd(void)
Definition: apc220setup.ino:265
uartBaud
char uartBaud
Definition: apc220setup.ino:33
st
char st[200]
Definition: apc220setup.ino:116
var4
char var4[PARM_LGT]
Definition: apc220setup.ino:103
RXPIN
#define RXPIN
Definition: apc220setup.ino:87
var6
char var6[PARM_LGT]
Definition: apc220setup.ino:103
rfPower
char rfPower
Definition: apc220setup.ino:32
get_para
void get_para(char s[])
Definition: apc220setup.ino:218