altos: Clean up warnings for LPC products
[fw/altos] / src / drivers / ao_cc115l.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_cc115l.h>
20 #include <ao_exti.h>
21 #include <ao_telemetry.h>
22 #include <ao_fec.h>
23
24 #define AO_RADIO_MAX_SEND       sizeof (struct ao_telemetry_generic)
25
26 uint8_t ao_radio_mutex;
27
28 static uint8_t ao_radio_fifo;           /* fifo drained interrupt received */
29 static uint8_t ao_radio_done;           /* tx done interrupt received */
30 static uint8_t ao_radio_wake;           /* sleep address for radio interrupts */
31 static uint8_t ao_radio_abort;          /* radio operation should abort */
32
33 /* Debugging commands */
34 #define CC115L_DEBUG    0
35
36 /* Runtime tracing */
37 #define CC115L_TRACE    0
38
39 #define FOSC    26000000
40
41 #define ao_radio_select()       ao_spi_get_mask(AO_CC115L_SPI_CS_PORT,(1 << AO_CC115L_SPI_CS_PIN),AO_CC115L_SPI_BUS,AO_SPI_SPEED_1MHz)
42 #define ao_radio_deselect()     ao_spi_put_mask(AO_CC115L_SPI_CS_PORT,(1 << AO_CC115L_SPI_CS_PIN),AO_CC115L_SPI_BUS)
43 #define ao_radio_spi_send(d,l)  ao_spi_send((d), (l), AO_CC115L_SPI_BUS)
44 #define ao_radio_spi_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_CC115L_SPI_BUS)
45 #define ao_radio_spi_recv(d,l)  ao_spi_recv((d), (l), AO_CC115L_SPI_BUS)
46 #define ao_radio_duplex(o,i,l)  ao_spi_duplex((o), (i), (l), AO_CC115L_SPI_BUS)
47
48 struct ao_cc115l_reg {
49         uint16_t        addr;
50         char            *name;
51 };
52
53 #if CC115L_TRACE
54
55 const static struct ao_cc115l_reg ao_cc115l_reg[];
56 const static char *cc115l_state_name[];
57
58 enum ao_cc115l_trace_type {
59         trace_strobe,
60         trace_read,
61         trace_write,
62         trace_dma,
63         trace_line,
64 };
65
66 struct ao_cc115l_trace {
67         enum ao_cc115l_trace_type       type;
68         int16_t                         addr;
69         int16_t                         value;
70         const char                      *comment;
71 };
72
73 #define NUM_TRACE       256
74
75 static struct ao_cc115l_trace   trace[NUM_TRACE];
76 static int                      trace_i;
77 static int                      trace_disable;
78
79 static void trace_add(enum ao_cc115l_trace_type type, int16_t addr, int16_t value, const char *comment)
80 {
81         if (trace_disable)
82                 return;
83         switch (type) {
84         case trace_read:
85         case trace_write:
86                 comment = ao_cc115l_reg[addr].name;
87                 break;
88         case trace_strobe:
89                 comment = cc115l_state_name[(value >> 4) & 0x7];
90                 break;
91         }
92         trace[trace_i].type = type;
93         trace[trace_i].addr = addr;
94         trace[trace_i].value = value;
95         trace[trace_i].comment = comment;
96         if (++trace_i == NUM_TRACE)
97                 trace_i = 0;
98 }
99 #else
100 #define trace_add(t,a,v,c)
101 #endif
102
103 static uint8_t
104 ao_radio_reg_read(uint8_t addr)
105 {
106         uint8_t data[1];
107
108         data[0] = ((1 << CC115L_READ)  |
109                    (0 << CC115L_BURST) |
110                    addr);
111         ao_radio_select();
112         ao_radio_spi_send(data, 1);
113         ao_radio_spi_recv(data, 1);
114         ao_radio_deselect();
115         trace_add(trace_read, addr, data[0], NULL);
116         return data[0];
117 }
118
119 static void
120 ao_radio_reg_write(uint8_t addr, uint8_t value)
121 {
122         uint8_t data[2];
123
124         trace_add(trace_write, addr, value, NULL);
125         data[0] = ((0 << CC115L_READ)  |
126                    (0 << CC115L_BURST) |
127                    addr);
128         data[1] = value;
129         ao_radio_select();
130         ao_radio_spi_send(data, 2);
131         ao_radio_deselect();
132 }
133
134 #if UNUSED
135 static void
136 ao_radio_burst_read_start (uint16_t addr)
137 {
138         uint8_t data[1];
139
140         data[0] = ((1 << CC115L_READ)  |
141                    (1 << CC115L_BURST) |
142                    addr);
143         ao_radio_select();
144         ao_radio_spi_send(data, 1);
145 }
146
147 static void
148 ao_radio_burst_read_stop (void)
149 {
150         ao_radio_deselect();
151 }
152 #endif
153
154
155 static uint8_t
156 ao_radio_strobe(uint8_t addr)
157 {
158         uint8_t in;
159
160         ao_radio_select();
161         ao_radio_duplex(&addr, &in, 1);
162         ao_radio_deselect();
163         trace_add(trace_strobe, addr, in, NULL);
164         return in;
165 }
166
167 static uint8_t
168 ao_radio_fifo_write_start(void)
169 {
170         uint8_t addr = ((0 << CC115L_READ)  |
171                         (1 << CC115L_BURST) |
172                         CC115L_FIFO);
173         uint8_t status;
174
175         ao_radio_select();
176         ao_radio_duplex(&addr, &status, 1);
177         return status;
178 }
179
180 static inline uint8_t ao_radio_fifo_write_stop(uint8_t status) {
181         ao_radio_deselect();
182         return status;
183 }
184
185 static uint8_t
186 ao_radio_fifo_write(uint8_t *data, uint8_t len)
187 {
188         uint8_t status = ao_radio_fifo_write_start();
189         trace_add(trace_dma, CC115L_FIFO, len, NULL);
190         ao_radio_spi_send(data, len);
191         return ao_radio_fifo_write_stop(status);
192 }
193
194 static uint8_t
195 ao_radio_tx_fifo_space(void)
196 {
197         return CC115L_FIFO_SIZE - (ao_radio_reg_read(CC115L_TXBYTES) & CC115L_TXBYTES_NUM_TX_BYTES_MASK);
198 }
199
200 #if UNUSED
201 static uint8_t
202 ao_radio_status(void)
203 {
204         return ao_radio_strobe (CC115L_SNOP);
205 }
206 #endif
207
208 #define ao_radio_rdf_value 0x55
209
210 #if UNUSED
211 static uint8_t
212 ao_radio_get_marcstate(void)
213 {
214         return ao_radio_reg_read(CC115L_MARCSTATE) & CC115L_MARCSTATE_MASK;
215 }
216 #endif
217           
218 static void
219 ao_radio_done_isr(void)
220 {
221         ao_exti_disable(AO_CC115L_DONE_INT_PORT, AO_CC115L_DONE_INT_PIN);
222         trace_add(trace_line, __LINE__, 0, "done_isr");
223         ao_radio_done = 1;
224         ao_wakeup(&ao_radio_wake);
225 }
226
227 static void
228 ao_radio_fifo_isr(void)
229 {
230         ao_exti_disable(AO_CC115L_FIFO_INT_PORT, AO_CC115L_FIFO_INT_PIN);
231         trace_add(trace_line, __LINE__, 0, "fifo_isr");
232         ao_radio_fifo = 1;
233         ao_wakeup(&ao_radio_wake);
234 }
235
236 static void
237 ao_radio_idle(void)
238 {
239         ao_radio_pa_off();
240         for (;;) {
241                 uint8_t state = ao_radio_strobe(CC115L_SIDLE);
242                 if ((state >> CC115L_STATUS_STATE) == CC115L_STATUS_STATE_IDLE)
243                         break;
244                 if ((state >> CC115L_STATUS_STATE) == CC115L_STATUS_STATE_TX_FIFO_UNDERFLOW)
245                         ao_radio_strobe(CC115L_SFTX);
246         }
247         /* Flush any pending TX bytes */
248         ao_radio_strobe(CC115L_SFTX);
249 }
250
251 /*
252  * Packet deviation is 20.5kHz
253  *
254  *      fdev = fosc >> 17 * (8 + dev_m) << dev_e
255  *
256  *      26e6 / (2 ** 17) * (8 + 5) * (2 ** 3) = 20630Hz
257  */
258
259 #define PACKET_DEV_E    3
260 #define PACKET_DEV_M    5
261
262 /*
263  * For our packet data, set the symbol rate to 38400 Baud
264  *
265  *              (256 + DATARATE_M) * 2 ** DATARATE_E
266  *      Rdata = -------------------------------------- * fosc
267  *                           2 ** 28
268  *
269  *              (256 + 131) * (2 ** 10) / (2**28) * 26e6 = 38383
270  *
271  *      DATARATE_M = 131
272  *      DATARATE_E = 10
273  */
274 #define PACKET_DRATE_E  10
275 #define PACKET_DRATE_M  131
276
277 static const uint16_t packet_setup[] = {
278         CC115L_DEVIATN,         ((PACKET_DEV_E << CC115L_DEVIATN_DEVIATION_E) |
279                                  (PACKET_DEV_M << CC115L_DEVIATN_DEVIATION_M)),
280         CC115L_MDMCFG4,         ((0xf << 4) |
281                                  (PACKET_DRATE_E << CC115L_MDMCFG4_DRATE_E)),
282         CC115L_MDMCFG3,         (PACKET_DRATE_M),
283         CC115L_MDMCFG2,         (0x00 |
284                                  (CC115L_MDMCFG2_MOD_FORMAT_GFSK << CC115L_MDMCFG2_MOD_FORMAT) |
285                                  (0 << CC115L_MDMCFG2_MANCHESTER_EN) |
286                                  (CC115L_MDMCFG2_SYNC_MODE_16BITS << CC115L_MDMCFG2_SYNC_MODE)),
287 };
288
289
290 /*
291  * RDF deviation is 5kHz
292  *
293  *      fdev = fosc >> 17 * (8 + dev_m) << dev_e
294  *
295  *      26e6 / (2 ** 17) * (8 + 4) * (2 ** 1) = 4761Hz
296  */
297
298 #define RDF_DEV_E       1
299 #define RDF_DEV_M       4
300
301 /*
302  * For our RDF beacon, set the symbol rate to 2kBaud (for a 1kHz tone)
303  *
304  *              (256 + DATARATE_M) * 2 ** DATARATE_E
305  *      Rdata = -------------------------------------- * fosc
306  *                           2 ** 28
307  *
308  *              (256 + 67) * (2 ** 6) / (2**28) * 26e6 = 2002
309  *
310  *      DATARATE_M = 67
311  *      DATARATE_E = 6
312  */
313 #define RDF_DRATE_E     6
314 #define RDF_DRATE_M     67
315
316 static const uint16_t rdf_setup[] = {
317         CC115L_DEVIATN,         ((RDF_DEV_E << CC115L_DEVIATN_DEVIATION_E) |
318                                  (RDF_DEV_M << CC115L_DEVIATN_DEVIATION_M)),
319         CC115L_MDMCFG4,         ((0xf << 4) |
320                                  (RDF_DRATE_E << CC115L_MDMCFG4_DRATE_E)),
321         CC115L_MDMCFG3,         (RDF_DRATE_M),
322         CC115L_MDMCFG2,         (0x00 |
323                                  (CC115L_MDMCFG2_MOD_FORMAT_GFSK << CC115L_MDMCFG2_MOD_FORMAT) |
324                                  (0 << CC115L_MDMCFG2_MANCHESTER_EN) |
325                                  (CC115L_MDMCFG2_SYNC_MODE_NONE << CC115L_MDMCFG2_SYNC_MODE)),
326 };
327
328 /*
329  * APRS deviation is 3kHz
330  *
331  * 26e6 / (2 ** 17) * (8 + 7) * (2 ** 0) = 2975
332  */
333
334 #define APRS_DEV_E      0
335 #define APRS_DEV_M      7
336
337 /*
338  * For our APRS beacon, set the symbol rate to 9.6kBaud (8x oversampling for 1200 baud data rate)
339  *
340  *              (256 + DATARATE_M) * 2 ** DATARATE_E
341  *      Rdata = -------------------------------------- * fosc
342  *                           2 ** 28
343  *
344  *              (256 + 131) * (2 ** 8) / (2**28) * 26e6 = 9596
345  *
346  *      DATARATE_M = 131
347  *      DATARATE_E = 8
348  *
349  */
350 #define APRS_DRATE_E    8
351 #define APRS_DRATE_M    131
352
353 static const uint16_t aprs_setup[] = {
354         CC115L_DEVIATN,         ((APRS_DEV_E << CC115L_DEVIATN_DEVIATION_E) |
355                                  (APRS_DEV_M << CC115L_DEVIATN_DEVIATION_M)),
356         CC115L_MDMCFG4,         ((0xf << 4) |
357                                  (APRS_DRATE_E << CC115L_MDMCFG4_DRATE_E)),
358         CC115L_MDMCFG3,         (APRS_DRATE_M),
359         CC115L_MDMCFG2,         (0x00 |
360                                  (CC115L_MDMCFG2_MOD_FORMAT_GFSK << CC115L_MDMCFG2_MOD_FORMAT) |
361                                  (0 << CC115L_MDMCFG2_MANCHESTER_EN) |
362                                  (CC115L_MDMCFG2_SYNC_MODE_NONE << CC115L_MDMCFG2_SYNC_MODE)),
363 };
364
365 #define AO_PKTCTRL0_INFINITE    ((CC115L_PKTCTRL0_PKT_FORMAT_NORMAL << CC115L_PKTCTRL0_PKT_FORMAT) | \
366                                  (0 << CC115L_PKTCTRL0_PKT_CRC_EN) |                                    \
367                                  (CC115L_PKTCTRL0_PKT_LENGTH_CONFIG_INFINITE << CC115L_PKTCTRL0_PKT_LENGTH_CONFIG))
368 #define AO_PKTCTRL0_FIXED       ((CC115L_PKTCTRL0_PKT_FORMAT_NORMAL << CC115L_PKTCTRL0_PKT_FORMAT) | \
369                                  (0 << CC115L_PKTCTRL0_PKT_CRC_EN) |                                    \
370                                  (CC115L_PKTCTRL0_PKT_LENGTH_CONFIG_FIXED << CC115L_PKTCTRL0_PKT_LENGTH_CONFIG))
371
372 static uint16_t ao_radio_mode;
373
374
375 /*
376  * These set the data rate and modulation parameters
377  */
378 #define AO_RADIO_MODE_BITS_PACKET_TX    1
379 #define AO_RADIO_MODE_BITS_RDF          2
380 #define AO_RADIO_MODE_BITS_APRS         4
381
382 /*
383  * Flips between infinite packet mode and fixed packet mode;
384  * we use infinite mode until the sender gives us the
385  * last chunk of data
386  */
387 #define AO_RADIO_MODE_BITS_INFINITE     40
388 #define AO_RADIO_MODE_BITS_FIXED        80
389
390 #define AO_RADIO_MODE_NONE              0
391
392 #define AO_RADIO_MODE_RDF               AO_RADIO_MODE_BITS_RDF
393 #define AO_RADIO_MODE_PACKET_TX         AO_RADIO_MODE_BITS_PACKET_TX
394 #define AO_RADIO_MODE_APRS              AO_RADIO_MODE_BITS_APRS
395
396 static void
397 ao_radio_set_mode(uint16_t new_mode)
398 {
399         uint16_t changes;
400         unsigned int i;
401
402         if (new_mode == ao_radio_mode)
403                 return;
404
405         changes = new_mode & (~ao_radio_mode);
406         if (changes & AO_RADIO_MODE_BITS_PACKET_TX)
407                 for (i = 0; i < sizeof (packet_setup) / sizeof (packet_setup[0]); i += 2)
408                         ao_radio_reg_write(packet_setup[i], packet_setup[i+1]);
409
410         if (changes & AO_RADIO_MODE_BITS_RDF)
411                 for (i = 0; i < sizeof (rdf_setup) / sizeof (rdf_setup[0]); i += 2)
412                         ao_radio_reg_write(rdf_setup[i], rdf_setup[i+1]);
413
414         if (changes & AO_RADIO_MODE_BITS_APRS)
415                 for (i = 0; i < sizeof (aprs_setup) / sizeof (aprs_setup[0]); i += 2)
416                         ao_radio_reg_write(aprs_setup[i], aprs_setup[i+1]);
417
418         if (changes & AO_RADIO_MODE_BITS_INFINITE)
419                 ao_radio_reg_write(CC115L_PKTCTRL0, AO_PKTCTRL0_INFINITE);
420
421         if (changes & AO_RADIO_MODE_BITS_FIXED)
422                 ao_radio_reg_write(CC115L_PKTCTRL0, AO_PKTCTRL0_FIXED);
423
424         ao_radio_mode = new_mode;
425 }
426
427 /***************************************************************
428  *  SmartRF Studio(tm) Export
429  *
430  *  Radio register settings specifed with address, value
431  *
432  *  RF device: CC115L
433  *
434  ***************************************************************/
435
436 static const uint16_t radio_setup[] = {
437
438         /* High when FIFO is above threshold, low when fifo is below threshold */
439         AO_CC115L_FIFO_INT_GPIO_IOCFG,      CC115L_IOCFG_GPIO_CFG_TXFIFO_THR,
440
441         /* High when transmitter is running, low when off */
442         AO_CC115L_DONE_INT_GPIO_IOCFG,      CC115L_IOCFG_GPIO_CFG_PA_PD | (1 << CC115L_IOCFG_GPIO_INV),
443
444         CC115L_FIFOTHR,                     0x47,       /* TX FIFO Thresholds */
445         CC115L_MDMCFG1,                     (0x00 |
446                                              (CC115L_MDMCFG1_NUM_PREAMBLE_4 << CC115L_MDMCFG1_NUM_PREAMBLE) |
447                                              (1 << CC115L_MDMCFG1_CHANSPC_E)),
448         CC115L_MDMCFG0,                     248,        /* Channel spacing M value (100kHz channels) */
449         CC115L_MCSM0,                       0x38,       /* Main Radio Control State Machine Configuration */
450         CC115L_RESERVED_0X20,               0xfb,       /* Use setting from SmartRF Studio */
451         CC115L_FSCAL3,                      0xe9,       /* Frequency Synthesizer Calibration */
452         CC115L_FSCAL2,                      0x2a,       /* Frequency Synthesizer Calibration */
453         CC115L_FSCAL1,                      0x00,       /* Frequency Synthesizer Calibration */
454         CC115L_FSCAL0,                      0x1f,       /* Frequency Synthesizer Calibration */
455         CC115L_TEST2,                       0x81,       /* Various Test Settings */
456         CC115L_TEST1,                       0x35,       /* Various Test Settings */
457         CC115L_TEST0,                       0x09,       /* Various Test Settings */
458 };
459
460 static uint8_t  ao_radio_configured = 0;
461
462 static void
463 ao_radio_setup(void)
464 {
465         unsigned int    i;
466
467         ao_radio_strobe(CC115L_SRES);
468         ao_delay(AO_MS_TO_TICKS(10));
469
470         for (i = 0; i < sizeof (radio_setup) / sizeof (radio_setup[0]); i += 2)
471                 ao_radio_reg_write(radio_setup[i], radio_setup[i+1]);
472
473         ao_radio_mode = 0;
474
475         ao_config_get();
476
477         ao_radio_strobe(CC115L_SCAL);
478
479         ao_radio_configured = 1;
480 }
481
482 static void
483 ao_radio_set_len(uint8_t len)
484 {
485         static uint8_t  last_len;
486
487         if (len != last_len) {
488                 ao_radio_reg_write(CC115L_PKTLEN, len);
489                 last_len = len;
490         }
491 }
492
493 static void
494 ao_radio_get(void)
495 {
496         static uint32_t last_radio_setting;
497
498         ao_mutex_get(&ao_radio_mutex);
499         if (!ao_radio_configured)
500                 ao_radio_setup();
501         if (ao_config.radio_setting != last_radio_setting) {
502                 ao_radio_reg_write(CC115L_FREQ2, ao_config.radio_setting >> 16);
503                 ao_radio_reg_write(CC115L_FREQ1, ao_config.radio_setting >> 8);
504                 ao_radio_reg_write(CC115L_FREQ0, ao_config.radio_setting);
505                 last_radio_setting = ao_config.radio_setting;
506         }
507 }
508
509 static void
510 _ao_radio_send_lots(ao_radio_fill_func fill, uint8_t mode);
511
512 #define ao_radio_put()  ao_mutex_put(&ao_radio_mutex)
513
514 struct ao_radio_tone {
515         uint8_t value;
516         uint8_t len;
517 };
518
519 struct ao_radio_tone *ao_radio_tone;
520 uint8_t ao_radio_tone_count;
521 uint8_t ao_radio_tone_current;
522 uint8_t ao_radio_tone_offset;
523
524 int16_t
525 ao_radio_tone_fill(uint8_t *buf, int16_t len)
526 {
527         int16_t ret = 0;
528
529         while (len) {
530                 int16_t                 this_time;
531                 struct ao_radio_tone    *t;
532
533                 /* Figure out how many to send of the current value */
534                 t = &ao_radio_tone[ao_radio_tone_current];
535                 this_time = t->len - ao_radio_tone_offset;
536                 if (this_time > len)
537                         this_time = len;
538
539                 /* queue the data */
540                 memset(buf, t->value, this_time);
541
542                 /* mark as sent */
543                 len -= this_time;
544                 ao_radio_tone_offset += this_time;
545                 ret += this_time;
546
547                 if (ao_radio_tone_offset >= t->len) {
548                         ao_radio_tone_offset = 0;
549                         ao_radio_tone_current++;
550                         if (ao_radio_tone_current >= ao_radio_tone_count) {
551                                 trace_add(trace_line, __LINE__, ret, "done with tone");
552                                 return -ret;
553                         }
554                 }
555         }
556         trace_add(trace_line, __LINE__, ret, "got some tone");
557         return ret;
558 }
559
560 static void
561 ao_radio_tone_run(struct ao_radio_tone *tones, int ntones)
562 {
563         ao_radio_get();
564         ao_radio_tone = tones;
565         ao_radio_tone_current = 0;
566         ao_radio_tone_offset = 0;
567         _ao_radio_send_lots(ao_radio_tone_fill, AO_RADIO_MODE_RDF);
568         ao_radio_put();
569 }
570
571 void
572 ao_radio_rdf(void)
573 {
574         struct ao_radio_tone    tone;
575
576         tone.value = ao_radio_rdf_value;
577         tone.len = AO_RADIO_RDF_LEN;
578         ao_radio_tone_run(&tone, 1);
579 }
580
581 void
582 ao_radio_continuity(uint8_t c)
583 {
584         struct ao_radio_tone    tones[7];
585         uint8_t count = 0;
586         uint8_t i;
587
588         for (i = 0; i < 3; i++) {
589                 tones[count].value = 0x00;
590                 tones[count].len = AO_RADIO_CONT_PAUSE_LEN;
591                 count++;
592                 if (i < c)
593                         tones[count].value = ao_radio_rdf_value;
594                 else
595                         tones[count].value = 0x00;
596                 tones[count].len = AO_RADIO_CONT_TONE_LEN;
597                 count++;
598         }
599         tones[count].value = 0x00;
600         tones[count].len = AO_RADIO_CONT_PAUSE_LEN;
601         count++;
602         ao_radio_tone_run(tones, count);
603 }
604
605 void
606 ao_radio_rdf_abort(void)
607 {
608         ao_radio_abort = 1;
609         ao_wakeup(&ao_radio_wake);
610 }
611
612 #define POWER_STEP      0x08
613
614 static void
615 ao_radio_stx(void)
616 {
617         uint8_t power;
618         ao_radio_pa_on();
619         ao_radio_reg_write(CC115L_PA, 0);
620         ao_radio_strobe(CC115L_STX);
621         for (power = POWER_STEP; power < ao_config.radio_power; power += POWER_STEP)
622                 ao_radio_reg_write(CC115L_PA, power);
623         ao_radio_reg_write(CC115L_PA, ao_config.radio_power);
624 }
625
626 static void
627 ao_radio_test_cmd(void)
628 {
629         uint8_t mode = 2;
630         static uint8_t radio_on;
631         ao_cmd_white();
632         if (ao_cmd_lex_c != '\n') {
633                 ao_cmd_decimal();
634                 mode = (uint8_t) ao_cmd_lex_u32;
635         }
636         mode++;
637         if ((mode & 2) && !radio_on) {
638 #if HAS_MONITOR
639                 ao_monitor_disable();
640 #endif
641 #if PACKET_HAS_SLAVE
642                 ao_packet_slave_stop();
643 #endif
644                 ao_radio_get();
645                 ao_radio_strobe(CC115L_SFTX);
646                 ao_radio_set_len(0xff);
647                 ao_radio_set_mode(AO_RADIO_MODE_RDF);
648                 ao_radio_stx();
649                 radio_on = 1;
650         }
651         if (mode == 3) {
652                 printf ("Hit a character to stop..."); flush();
653                 getchar();
654                 putchar('\n');
655         }
656         if ((mode & 1) && radio_on) {
657                 ao_radio_idle();
658                 ao_radio_put();
659                 radio_on = 0;
660 #if HAS_MONITOR
661                 ao_monitor_enable();
662 #endif
663         }
664 }
665
666 #if CC115L_TRACE
667 static inline int16_t
668 ao_radio_gpio_bits(void)
669 {
670         return AO_CC115L_DONE_INT_PORT->idr & ((1 << AO_CC115L_FIFO_INT_PIN) |
671                                                (1 << AO_CC115L_DONE_INT_PIN));
672 }
673 #endif
674
675 static void
676 ao_radio_wait_fifo(void)
677 {
678         ao_arch_block_interrupts();
679         while (!ao_radio_fifo && !ao_radio_done && !ao_radio_abort) {
680                 trace_add(trace_line, __LINE__, ao_radio_gpio_bits(), "wait_fifo");
681                 ao_sleep(&ao_radio_wake);
682         }
683         ao_arch_release_interrupts();
684         trace_add(trace_line, __LINE__, ao_radio_gpio_bits(), "wake bits");
685         trace_add(trace_line, __LINE__, ao_radio_fifo, "wake fifo");
686         trace_add(trace_line, __LINE__, ao_radio_done, "wake done");
687         trace_add(trace_line, __LINE__, ao_radio_abort, "wake abort");
688 }
689
690 static void
691 ao_radio_wait_done(void)
692 {
693         ao_arch_block_interrupts();
694         while (!ao_radio_done && !ao_radio_abort) {
695                 trace_add(trace_line, __LINE__, ao_radio_gpio_bits(), "wait_done");
696                 ao_sleep(&ao_radio_wake);
697         }
698         ao_arch_release_interrupts();
699         trace_add(trace_line, __LINE__, ao_radio_gpio_bits(), "wake bits");
700         trace_add(trace_line, __LINE__, ao_radio_fifo, "wake fifo");
701         trace_add(trace_line, __LINE__, ao_radio_done, "wake done");
702         trace_add(trace_line, __LINE__, ao_radio_abort, "wake abort");
703 }
704
705 static uint8_t  tx_data[(AO_RADIO_MAX_SEND + 4) * 2];
706
707 static uint8_t  *ao_radio_send_buf;
708 static int16_t  ao_radio_send_len;
709
710 static int16_t
711 ao_radio_send_fill(uint8_t *buf, int16_t len)
712 {
713         int16_t this_time;
714
715         this_time = ao_radio_send_len;
716         if (this_time > len)
717                 this_time = len;
718         memcpy(buf, ao_radio_send_buf, this_time);
719         ao_radio_send_buf += this_time;
720         ao_radio_send_len -= this_time;
721         if (ao_radio_send_len == 0)
722                 return -this_time;
723         return this_time;
724 }
725
726 void
727 ao_radio_send(const void *d, uint8_t size)
728 {
729         ao_radio_get();
730         ao_radio_send_len = ao_fec_encode(d, size, tx_data);
731         ao_radio_send_buf = tx_data;
732         _ao_radio_send_lots(ao_radio_send_fill, AO_RADIO_MODE_PACKET_TX);
733         ao_radio_put();
734 }
735
736 #define AO_RADIO_LOTS   64
737
738 static void
739 _ao_radio_send_lots(ao_radio_fill_func fill, uint8_t mode)
740 {
741         uint8_t buf[AO_RADIO_LOTS], *b;
742         int     cnt;
743         int     total = 0;
744         uint8_t done = 0;
745         uint8_t started = 0;
746         uint8_t fifo_space;
747
748         fifo_space = CC115L_FIFO_SIZE;
749         ao_radio_abort = 0;
750
751         ao_radio_strobe(CC115L_SFTX);
752
753         ao_radio_done = 0;
754         ao_radio_fifo = 0;
755         while (!done) {
756                 cnt = (*fill)(buf, sizeof(buf));
757                 trace_add(trace_line, __LINE__, cnt, "send data count");
758                 if (cnt < 0) {
759                         done = 1;
760                         cnt = -cnt;
761                 }
762                 total += cnt;
763
764                 /* At the last buffer, set the total length */
765                 if (done) {
766                         ao_radio_set_len(total & 0xff);
767                         ao_radio_set_mode(mode | AO_RADIO_MODE_BITS_FIXED);
768                 } else {
769                         ao_radio_set_len(0xff);
770                         ao_radio_set_mode(mode | AO_RADIO_MODE_BITS_INFINITE);
771                 }
772
773                 b = buf;
774                 while (cnt) {
775                         uint8_t this_len = cnt;
776
777                         /* Wait for some space in the fifo */
778                         while (!ao_radio_abort && (fifo_space = ao_radio_tx_fifo_space()) == 0) {
779                                 trace_add(trace_line, __LINE__, this_len, "wait for space");
780                                 ao_radio_wait_fifo();
781                         }
782                         if (ao_radio_abort || ao_radio_done)
783                                 break;
784                         trace_add(trace_line, __LINE__, fifo_space, "got space");
785                         if (this_len > fifo_space)
786                                 this_len = fifo_space;
787
788                         cnt -= this_len;
789
790                         ao_radio_done = 0;
791                         ao_radio_fifo = 0;
792                         ao_radio_fifo_write(b, this_len);
793                         b += this_len;
794
795                         ao_exti_enable(AO_CC115L_FIFO_INT_PORT, AO_CC115L_FIFO_INT_PIN);
796                         ao_exti_enable(AO_CC115L_DONE_INT_PORT, AO_CC115L_DONE_INT_PIN);
797
798                         if (!started) {
799                                 ao_radio_stx();
800                                 started = 1;
801                         }
802                 }
803                 if (ao_radio_abort || ao_radio_done)
804                         break;
805         }
806         if (ao_radio_abort)
807                 ao_radio_idle();
808         ao_radio_wait_done();
809         ao_radio_pa_off();
810 }
811
812 void
813 ao_radio_send_aprs(ao_radio_fill_func fill)
814 {
815         ao_radio_get();
816         _ao_radio_send_lots(fill, AO_RADIO_MODE_APRS);
817         ao_radio_put();
818 }
819
820 #if CC115L_DEBUG
821 const static char *cc115l_state_name[] = {
822         [CC115L_STATUS_STATE_IDLE] = "IDLE",
823         [CC115L_STATUS_STATE_TX] = "TX",
824         [CC115L_STATUS_STATE_FSTXON] = "FSTXON",
825         [CC115L_STATUS_STATE_CALIBRATE] = "CALIBRATE",
826         [CC115L_STATUS_STATE_SETTLING] = "SETTLING",
827         [CC115L_STATUS_STATE_TX_FIFO_UNDERFLOW] = "TX_FIFO_UNDERFLOW",
828 };
829
830 const static struct ao_cc115l_reg ao_cc115l_reg[] = {
831         { .addr = CC115L_IOCFG2, .name = "IOCFG2" },
832         { .addr = CC115L_IOCFG1, .name = "IOCFG1" },
833         { .addr = CC115L_IOCFG0, .name = "IOCFG0" },
834         { .addr = CC115L_FIFOTHR, .name = "FIFOTHR" },
835         { .addr = CC115L_SYNC1, .name = "SYNC1" },
836         { .addr = CC115L_SYNC0, .name = "SYNC0" },
837         { .addr = CC115L_PKTLEN, .name = "PKTLEN" },
838         { .addr = CC115L_PKTCTRL0, .name = "PKTCTRL0" },
839         { .addr = CC115L_CHANNR, .name = "CHANNR" },
840         { .addr = CC115L_FSCTRL0, .name = "FSCTRL0" },
841         { .addr = CC115L_FREQ2, .name = "FREQ2" },
842         { .addr = CC115L_FREQ1, .name = "FREQ1" },
843         { .addr = CC115L_FREQ0, .name = "FREQ0" },
844         { .addr = CC115L_MDMCFG4, .name = "MDMCFG4" },
845         { .addr = CC115L_MDMCFG3, .name = "MDMCFG3" },
846         { .addr = CC115L_MDMCFG2, .name = "MDMCFG2" },
847         { .addr = CC115L_MDMCFG1, .name = "MDMCFG1" },
848         { .addr = CC115L_MDMCFG0, .name = "MDMCFG0" },
849         { .addr = CC115L_DEVIATN, .name = "DEVIATN" },
850         { .addr = CC115L_MCSM1, .name = "MCSM1" },
851         { .addr = CC115L_MCSM0, .name = "MCSM0" },
852         { .addr = CC115L_RESERVED_0X20, .name = "RESERVED_0X20" },
853         { .addr = CC115L_FREND0, .name = "FREND0" },
854         { .addr = CC115L_FSCAL3, .name = "FSCAL3" },
855         { .addr = CC115L_FSCAL2, .name = "FSCAL2" },
856         { .addr = CC115L_FSCAL1, .name = "FSCAL1" },
857         { .addr = CC115L_FSCAL0, .name = "FSCAL0" },
858         { .addr = CC115L_RESERVED_0X29, .name = "RESERVED_0X29" },
859         { .addr = CC115L_RESERVED_0X2A, .name = "RESERVED_0X2A" },
860         { .addr = CC115L_RESERVED_0X2B, .name = "RESERVED_0X2B" },
861         { .addr = CC115L_TEST2, .name = "TEST2" },
862         { .addr = CC115L_TEST1, .name = "TEST1" },
863         { .addr = CC115L_TEST0, .name = "TEST0" },
864         { .addr = CC115L_PARTNUM, .name = "PARTNUM" },
865         { .addr = CC115L_VERSION, .name = "VERSION" },
866         { .addr = CC115L_MARCSTATE, .name = "MARCSTATE" },
867         { .addr = CC115L_PKTSTATUS, .name = "PKTSTATUS" },
868         { .addr = CC115L_TXBYTES, .name = "TXBYTES" },
869         { .addr = CC115L_PA, .name = "PA" },
870 };
871
872 #define AO_NUM_CC115L_REG       (sizeof ao_cc115l_reg / sizeof ao_cc115l_reg[0])
873
874 static void ao_radio_show(void) {
875         uint8_t status = ao_radio_status();
876         int     i;
877
878         ao_radio_get();
879         status = ao_radio_status();
880         printf ("Status:   %02x\n", status);
881         printf ("CHIP_RDY: %d\n", (status >> CC115L_STATUS_CHIP_RDY) & 1);
882         printf ("STATE:    %s\n", cc115l_state_name[(status >> CC115L_STATUS_STATE) & CC115L_STATUS_STATE_MASK]);
883         printf ("MARC:     %02x\n", ao_radio_get_marcstate());
884
885         for (i = 0; i < AO_NUM_CC115L_REG; i++)
886                 printf ("\t%02x %-20.20s\n", ao_radio_reg_read(ao_cc115l_reg[i].addr), ao_cc115l_reg[i].name);
887         ao_radio_put();
888 }
889
890 static void ao_radio_beep(void) {
891         ao_radio_rdf();
892 }
893
894 static void ao_radio_packet(void) {
895         static const uint8_t packet[] = {
896 #if 1
897                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
898                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
899                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
900                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
901 #else
902                 3, 1, 2, 3
903 #endif
904         };
905
906         ao_radio_send(packet, sizeof (packet));
907 }
908
909
910 #if HAS_APRS
911 #include <ao_aprs.h>
912
913 static void
914 ao_radio_aprs()
915 {
916 #if PACKET_HAS_SLAVE
917         ao_packet_slave_stop();
918 #endif
919         ao_aprs_send();
920 }
921 #endif
922 #endif /* CC115L_DEBUG */
923
924 static const struct ao_cmds ao_radio_cmds[] = {
925         { ao_radio_test_cmd,    "C <1 start, 0 stop, none both>\0Radio carrier test" },
926 #if CC115L_DEBUG
927 #if HAS_APRS
928         { ao_radio_aprs,        "G\0Send APRS packet" },
929 #endif
930         { ao_radio_show,        "R\0Show CC115L status" },
931         { ao_radio_beep,        "b\0Emit an RDF beacon" },
932         { ao_radio_packet,      "p\0Send a test packet" },
933 #endif
934         { 0, NULL }
935 };
936
937 void
938 ao_radio_init(void)
939 {
940 #if 0
941         int     i;
942 #endif
943
944         ao_radio_configured = 0;
945         ao_spi_init_cs (AO_CC115L_SPI_CS_PORT, (1 << AO_CC115L_SPI_CS_PIN));
946
947 #if 0
948         AO_CC115L_SPI_CS_PORT->bsrr = ((uint32_t) (1 << AO_CC115L_SPI_CS_PIN));
949         for (i = 0; i < 10000; i++) {
950                 if ((SPI_2_PORT->idr & (1 << SPI_2_MISO_PIN)) == 0)
951                         break;
952         }
953         AO_CC115L_SPI_CS_PORT->bsrr = (1 << AO_CC115L_SPI_CS_PIN);
954         if (i == 10000)
955                 ao_panic(AO_PANIC_SELF_TEST_CC115L);
956 #endif
957
958         /* Enable the fifo threhold interrupt pin */
959         ao_enable_port(AO_CC115L_FIFO_INT_PORT);
960         ao_exti_setup(AO_CC115L_FIFO_INT_PORT, AO_CC115L_FIFO_INT_PIN,
961                       AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_HIGH,
962                       ao_radio_fifo_isr);
963
964         /* Enable the tx done interrupt pin */
965         ao_enable_port(AO_CC115L_DONE_INT_PORT);
966         ao_exti_setup(AO_CC115L_DONE_INT_PORT, AO_CC115L_DONE_INT_PIN,
967                       AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_MED,
968                       ao_radio_done_isr);
969
970         ao_radio_pa_init();
971
972         ao_cmd_register(&ao_radio_cmds[0]);
973 }