PicoWAN SDK Documentation
radio.h
Go to the documentation of this file.
1 
4 /*******************************************************************************
5  * Copyright (c) 2014-2015 IBM Corporation.
6  * Copyright (c) 2018 Archos S.A.
7  * All rights reserved. This program and the accompanying materials
8  * are made available under the terms of the Eclipse Public License v1.0
9  * which accompanies this distribution, and is available at
10  * http://www.eclipse.org/legal/epl-v10.html
11  *
12  * Contributors:
13  * IBM Zurich Research Lab - initial API, implementation and documentation
14  * Archos S.A.
15  *******************************************************************************/
16 
17 #ifndef _radio_h_
18 #define _radio_h_
19 
23 #define RADIO_OSC_STARTUP 1 // [ms]
24 
28 #define RADIO_SLEEP_TO_RX 2 // [ms]
29 
33 #define RADIO_WAKEUP_TIME (RADIO_OSC_STARTUP + RADIO_SLEEP_TO_RX)
34 
35 // purpose of receive window - lmic_t.rxState
36 enum { RADIO_RST=0, RADIO_TX=1, RADIO_RX=2, RADIO_RXON=3 };
37 
38 enum _cr_t { CR_4_5 = 0, CR_4_6, CR_4_7, CR_4_8 };
39 enum _sf_t { FSK = 0, SF6, SF7, SF8, SF9, SF10, SF11, SF12 };
40 enum _bw_t { BW7_8 = 0, BW10_4, BW15_6, BW20_8, BW31_25, BW41_7, BW62_5, BW125, BW250, BW500 };
41 typedef uint8_t cr_t;
42 typedef uint8_t sf_t;
43 typedef uint8_t bw_t;
44 typedef uint8_t dr_t;
45 
46 // Radio parameter set (encodes SF/BW/CR/IH/NOCRC)
47 typedef uint32_t rps_t;
48 
49 // Global maximum frame length
50 enum { STD_PREAMBLE_LEN = 8 };
51 enum { MAX_LEN_FRAME = 64 };
52 
53 struct radio_t {
54  // Radio settings TX/RX
55  os_time_t txend;
56  os_time_t rxtime;
57  uint32_t freq;
58  int16_t rssi;
59  int8_t snr;
60  rps_t rps;
61  uint16_t rxsyms;
62  uint8_t dndr;
63  int8_t txpow; // dBm
64  uint16_t preamble_len;
65  uint8_t payload_length;
66  uint8_t sync_word;
67  uint8_t invert_iq; // '0' -> normal mode, '1' -> inverted
68  uint32_t freq_delta;
69  int8_t ppm_offset;
70  uint8_t crc_ok;
71 
72  os_job_t osjob;
73 
74  // Public part of RADIO state
75  uint8_t dataLen; // 0 no data or zero length data, >0 byte count of data
76  uint8_t frame[MAX_LEN_FRAME];
77 };
78 
79 extern struct radio_t RADIO;
80 
81 
82 #define MAKERPS(sf, bw, cr, ih, nocrc) ((rps_t) ((sf) | ((bw) << 3) | ((cr) << 7) | ((nocrc) ? (1 << 9) : 0) | ((ih & 0xFF) << 10)))
83 
84 static inline sf_t getSf(rps_t params)
85 {
86  return (sf_t) (params & 0x7);
87 }
88 
89 static inline rps_t setSf(rps_t params, sf_t sf)
90 {
91  return (rps_t) ((params & ~0x7) | sf);
92 }
93 
94 static inline bw_t getBw(rps_t params)
95 {
96  return (bw_t) ((params >> 3) & 0xf);
97 }
98 
99 static inline rps_t setBw(rps_t params, bw_t bw)
100 {
101  return (rps_t) ((params & ~(0xf << 3)) | (bw << 3));
102 }
103 
104 static inline cr_t getCr(rps_t params)
105 {
106  return (cr_t) ((params >> 7) & 0x3);
107 }
108 
109 static inline rps_t setCr(rps_t params, cr_t cr)
110 {
111  return (rps_t) ((params & ~(0x3 << 7)) | (cr << 7));
112 }
113 
114 static inline int getNocrc(rps_t params)
115 {
116  return ((params >> 9) & 0x1);
117 }
118 
119 static inline rps_t setNocrc(rps_t params, int nocrc)
120 {
121  return (rps_t) ((params & ~(0x1 << 9)) | (nocrc << 9));
122 }
123 
124 static inline int getIh(rps_t params)
125 {
126  return ((params >> 10) & 0xFF);
127 }
128 
129 static inline rps_t setIh(rps_t params, int ih)
130 {
131  return (rps_t) ((params & ~(0xff << 10)) | ((ih & 0xff) << 10));
132 }
133 
134 static inline rps_t makeRps(sf_t sf, bw_t bw, cr_t cr, int ih, int nocrc)
135 {
136  return sf | (bw << 3) | (cr << 7) | (nocrc ? (1 << 9) : 0) | ((ih & 0xFF) << 10);
137 }
138 
139 
145 uint8_t radio_rand1(void);
146 
150 void radio_init(void);
151 
157 void radio_reset(uint8_t val);
158 
164 void radio_irq_handler(uint8_t dio);
165 
173 uint8_t radio_rssi(void);
174 
180 void os_radio(uint8_t mode);
181 
191 uint8_t radio_read(uint8_t reg);
200 void radio_write(uint8_t addr, uint8_t data);
201 
202 #endif
uint8_t radio_rssi(void)
Returns the current RSSI.
Definition: radio.c:1061
void os_radio(uint8_t mode)
Changes the radio mode.
Definition: radio.c:1188
void radio_init(void)
Initializes the radio driver.
Definition: radio.c:954
void radio_irq_handler(uint8_t dio)
The radio driver IRQ handler.
Definition: radio.c:1081
void radio_write(uint8_t addr, uint8_t data)
Writes a radio register.
Definition: radio.c:1183
Definition: radio.h:53
uint8_t radio_read(uint8_t reg)
Reads a radio register.
Definition: radio.c:1178
uint8_t radio_rand1(void)
Returns a random byte.
Definition: radio.c:1048
void radio_reset(uint8_t val)
Resets the SX127x module.
Definition: radio.c:1033