Arduino LMIC 6.0.1
Arduino LoRaWAN(r) MAC in C
Loading...
Searching...
No Matches
oslmic.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2016 IBM Corporation.
3 * Copyright (c) 2018-2026 MCCI Corporation
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the <organization> nor the
14 * names of its contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
30#ifndef _oslmic_h_
31#define _oslmic_h_
32
33// Dependencies required for the LMIC to run.
34// These settings can be adapted to the underlying system.
35// You should not, however, change the lmic merely for porting purposes.[hc]
36
37#include "config.h"
38
39#ifndef _lmic_env_h_
40# include "lmic_env.h"
41#endif
42
43#ifndef _oslmic_types_h_
44# include "oslmic_types.h"
45#endif
46
47LMIC_BEGIN_DECLS
48
49
50#include <string.h>
51#include "hal.h"
52#define EV(a,b,c) /**/
53#define DO_DEVDB(field1,field2) /**/
54#if !defined(CFG_noassert)
55#define ASSERT(cond) if(!(cond)) lmic_hal_failed(__FILE__, __LINE__)
56#else
57#define ASSERT(cond) /**/
58#endif
59
62#define os_clearMem(pDest,nDest) memset(pDest,0,nDest)
63
70#define os_copyMem(pDest,pSrc,nSrc) memcpy(pDest,pSrc,nSrc)
71
72typedef struct osjob_t osjob_t;
73typedef struct band_t band_t;
74typedef struct chnldef_t chnldef_t;
75typedef struct rxsched_t rxsched_t;
76typedef struct bcninfo_t bcninfo_t;
77
78// int32_t == s4_t is long on some platforms; and someday
79// we will want 64-bit ostime_t. So, we will use a macro for the
80// print formatting of ostime_t.
81#ifndef LMIC_PRId_ostime_t
82# include <inttypes.h>
83# define LMIC_PRId_ostime_t PRId32
84#endif
85
86#define TYPEDEF_xref2rps_t typedef rps_t* xref2rps_t
87#define TYPEDEF_xref2rxsched_t typedef rxsched_t* xref2rxsched_t
88#define TYPEDEF_xref2chnldef_t typedef chnldef_t* xref2chnldef_t
89#define TYPEDEF_xref2band_t typedef band_t* xref2band_t
90#define TYPEDEF_xref2osjob_t typedef osjob_t* xref2osjob_t
91
92#define SIZEOFEXPR(x) sizeof(x)
93
94#define DECL_ON_LMIC_EVENT LMIC_DECLARE_FUNCTION_WEAK(void, onEvent, (ev_t e))
95
96#define FUNC_ADDR(func) (&(func))
97
98u1_t radio_rand1 (void);
99#define os_getRndU1() radio_rand1()
100
101#define DEFINE_LMIC struct lmic_t LMIC
102#define DECLARE_LMIC extern struct lmic_t LMIC
103
104typedef struct oslmic_radio_rssi_s oslmic_radio_rssi_t;
105
107 s2_t min_rssi;
108 s2_t max_rssi;
109 s2_t mean_rssi;
110 u2_t n_rssi;
111};
112
113int radio_init (void);
114void radio_irq_handler (u1_t dio);
115void radio_irq_handler_v2 (u1_t dio, ostime_t tref);
116void os_init (void);
117int os_init_ex (const void *pPinMap);
118void os_runloop (void);
119void os_runloop_once (void);
120u1_t radio_rssi (void);
121void radio_monitor_rssi(ostime_t n, oslmic_radio_rssi_t *pRssi);
122
123//================================================================================
124
125#ifndef RX_RAMPUP_DEFAULT
131#define RX_RAMPUP_DEFAULT (us2osticks(10000))
132#endif
133
134#ifndef TX_RAMPUP
135// TX_RAMPUP specifies the extra time we must allow to set up a TX event) due
136// to platform issues. It's specified in units of ostime_t. It must reflect
137// platform jitter and latency, as well as the speed of the LMIC when running
138// on this plaform.
139#define TX_RAMPUP (us2osticks(10000))
140#endif
141
142#ifndef OSTICKS_PER_SEC
143#define OSTICKS_PER_SEC 32768
144#elif OSTICKS_PER_SEC < 10000 || OSTICKS_PER_SEC > 64516
145#error Illegal OSTICKS_PER_SEC - must be in range [10000:64516]. One tick must be 15.5us .. 100us long.
146#endif
147
148#if !HAS_ostick_conv
149#define us2osticks(us) ((ostime_t)( ((int64_t)(us) * OSTICKS_PER_SEC) / 1000000))
150#define ms2osticks(ms) ((ostime_t)( ((int64_t)(ms) * OSTICKS_PER_SEC) / 1000))
151#define sec2osticks(sec) ((ostime_t)( (int64_t)(sec) * OSTICKS_PER_SEC))
152#define osticks2ms(os) ((s4_t)(((os)*(int64_t)1000 ) / OSTICKS_PER_SEC))
153#define osticks2us(os) ((s4_t)(((os)*(int64_t)1000000 ) / OSTICKS_PER_SEC))
154// Special versions
155#define us2osticksCeil(us) ((ostime_t)( ((int64_t)(us) * OSTICKS_PER_SEC + 999999) / 1000000))
156#define us2osticksRound(us) ((ostime_t)( ((int64_t)(us) * OSTICKS_PER_SEC + 500000) / 1000000))
157#define ms2osticksCeil(ms) ((ostime_t)( ((int64_t)(ms) * OSTICKS_PER_SEC + 999) / 1000))
158#define ms2osticksRound(ms) ((ostime_t)( ((int64_t)(ms) * OSTICKS_PER_SEC + 500) / 1000))
159#endif
160
161
162struct osjob_t; // fwd decl.
163
165typedef void (osjobcbfn_t)(struct osjob_t*);
166
169
170struct osjob_t {
171 struct osjob_t* next;
172 ostime_t deadline;
173 osjobcb_t func;
174};
175TYPEDEF_xref2osjob_t;
176
178// must treat incoming == 0 as being 1 instead.
179static inline int os_jobIsTimed(xref2osjob_t job) {
180 return (job->deadline != 0);
181}
182
183#ifndef HAS_os_calls
184
185#ifndef os_getDevKey
186void os_getDevKey (xref2u1_t buf);
187#endif
188#ifndef os_getArtEui
189void os_getArtEui (xref2u1_t buf);
190#endif
191#ifndef os_getDevEui
192void os_getDevEui (xref2u1_t buf);
193#endif
194#ifndef os_setCallback
195void os_setCallback (xref2osjob_t job, osjobcb_t cb);
196#endif
197#ifndef os_setTimedCallback
198void os_setTimedCallback (xref2osjob_t job, ostime_t time, osjobcb_t cb);
199#endif
200#ifndef os_setIdleJobFunction
202#endif
203#ifndef os_clearCallback
204void os_clearCallback (xref2osjob_t job);
205#endif
206#ifndef os_getRadioRxRampup
207ostime_t os_getRadioRxRampup (void);
208#endif
209#ifndef os_getTime
210ostime_t os_getTime (void);
211#endif
212#ifndef os_getTimeSecs
213uint os_getTimeSecs (void);
214#endif
215#ifndef os_radio
216void os_radio (u1_t mode);
217#endif
218#ifndef os_radio_v2
219void os_radio_v2 (u1_t mode, xref2osjob_t job);
220#endif
221#ifndef os_getBattLevel
222u1_t os_getBattLevel (void);
223#endif
224#ifndef os_queryTimeCriticalJobs
226bit_t os_queryTimeCriticalJobs(ostime_t time);
227#endif
228
229#ifndef os_rlsbf4
231u4_t os_rlsbf4 (xref2cu1_t buf);
232#endif
233#ifndef os_wlsbf4
235void os_wlsbf4 (xref2u1_t buf, u4_t value);
236#endif
237#ifndef os_rmsbf4
239u4_t os_rmsbf4 (xref2cu1_t buf);
240#endif
241#ifndef os_wmsbf4
243void os_wmsbf4 (xref2u1_t buf, u4_t value);
244#endif
245#ifndef os_rlsbf2
247u2_t os_rlsbf2 (xref2cu1_t buf);
248#endif
249#ifndef os_wlsbf2
251void os_wlsbf2 (xref2u1_t buf, u2_t value);
252#endif
253
255#ifndef os_getRndU2
256#define os_getRndU2() ((u2_t)((os_getRndU1()<<8)|os_getRndU1()))
257#endif
258#ifndef os_crc16
259u2_t os_crc16 (xref2cu1_t d, uint len);
260#endif
261
262#endif // !HAS_os_calls
263
264// ======================================================================
265// Table support
266// These macros for defining a table of constants and retrieving values
267// from it makes it easier for other platforms (like AVR) to optimize
268// table accesses.
269// Use CONST_TABLE() whenever declaring or defining a table, and
270// TABLE_GET_xx whenever accessing its values. The actual name of the
271// declared variable will be modified to prevent accidental direct
272// access. The accessor macros forward to an inline function to allow
273// proper type checking of the array element type.
274
275// Helper to add a prefix to the table name
276#define RESOLVE_TABLE(table) constant_table_ ## table
277
278// get number of entries in table
279#define LENOF_TABLE(table) (sizeof(RESOLVE_TABLE(table)) / sizeof(RESOLVE_TABLE(table)[0]))
280
281// Accessors for table elements
282#define TABLE_GET_U1(table, index) table_get_u1(RESOLVE_TABLE(table), index)
283#define TABLE_GET_S1(table, index) table_get_s1(RESOLVE_TABLE(table), index)
284#define TABLE_GET_U2(table, index) table_get_u2(RESOLVE_TABLE(table), index)
285#define TABLE_GET_S2(table, index) table_get_s2(RESOLVE_TABLE(table), index)
286#define TABLE_GET_U4(table, index) table_get_u4(RESOLVE_TABLE(table), index)
287#define TABLE_GET_S4(table, index) table_get_s4(RESOLVE_TABLE(table), index)
288#define TABLE_GET_OSTIME(table, index) table_get_ostime(RESOLVE_TABLE(table), index)
289#define TABLE_GET_U1_TWODIM(table, index1, index2) table_get_u1(RESOLVE_TABLE(table)[index1], index2)
290
291#if defined(__AVR__)
292 #include <avr/pgmspace.h>
293 // Macro to define the getter functions. This loads data from
294 // progmem using pgm_read_xx, or accesses memory directly when the
295 // index is a constant so gcc can optimize it away;
296 #define TABLE_GETTER(postfix, type, pgm_type) \
297 static inline type table_get ## postfix(const type *table, size_t index) { \
298 if (__builtin_constant_p(table[index])) \
299 return table[index]; \
300 return pgm_read_ ## pgm_type(&table[index]); \
301 }
302
303 TABLE_GETTER(_u1, u1_t, byte);
304 TABLE_GETTER(_s1, s1_t, byte);
305 TABLE_GETTER(_u2, u2_t, word);
306 TABLE_GETTER(_s2, s2_t, word);
307 TABLE_GETTER(_u4, u4_t, dword);
308 TABLE_GETTER(_s4, s4_t, dword);
309
310 // This assumes ostime_t is 4 bytes, so error out if it is not
311 typedef int check_sizeof_ostime_t[(sizeof(ostime_t) == 4) ? 0 : -1];
312 TABLE_GETTER(_ostime, ostime_t, dword);
313
314 // For AVR, store constants in PROGMEM, saving on RAM usage
315 #define CONST_TABLE(type, name) const type PROGMEM RESOLVE_TABLE(name)
316#else
317 static inline u1_t table_get_u1(const u1_t *table, size_t index) { return table[index]; }
318 static inline s1_t table_get_s1(const s1_t *table, size_t index) { return table[index]; }
319 static inline u2_t table_get_u2(const u2_t *table, size_t index) { return table[index]; }
320 static inline s2_t table_get_s2(const s2_t *table, size_t index) { return table[index]; }
321 static inline u4_t table_get_u4(const u4_t *table, size_t index) { return table[index]; }
322 static inline s4_t table_get_s4(const s4_t *table, size_t index) { return table[index]; }
323 static inline ostime_t table_get_ostime(const ostime_t *table, size_t index) { return table[index]; }
324
325 // Declare a table
326 #define CONST_TABLE(type, name) const type RESOLVE_TABLE(name)
327#endif
328
329// ======================================================================
330// Simple logging support. Vanishes unless enabled.
331
332#if LMIC_ENABLE_event_logging
333extern void LMICOS_logEvent(const char *pMessage);
334extern void LMICOS_logEventUint32(const char *pMessage, uint32_t datum);
335#else // ! LMIC_ENABLE_event_logging
336# define LMICOS_logEvent(m) do { ; } while (0)
337# define LMICOS_logEventUint32(m, d) do { ; } while (0)
338#endif // ! LMIC_ENABLE_event_logging
339
340
341LMIC_END_DECLS
342
343#endif // _oslmic_h_
osjobcbfn_t * osjobcb_t
the pointer-to-function for osjob_t callbacks
Definition oslmic.h:168
u4_t os_rlsbf4(xref2cu1_t buf)
Read 32-bit quantity from given pointer in little endian byte order.
Definition lmic.c:86
void osjobcbfn_t(struct osjob_t *)
the function type for osjob_t callbacks
Definition oslmic.h:165
void os_wmsbf4(xref2u1_t buf, u4_t value)
Write 32-bit quantity into buffer in big endian byte order.
Definition lmic.c:116
u1_t radio_rand1(void)
Generate an 8-bit uniformly-distributed integer.
Definition radio_sx127x.c:1236
void os_wlsbf2(xref2u1_t buf, u2_t value)
Write 16-bit quantity into buffer in little endian byte order.
Definition lmic.c:100
void radio_irq_handler(u1_t dio)
legacy radio IRQ handler
Definition radio_sx127x.c:1361
bit_t os_queryTimeCriticalJobs(ostime_t time)
Return non-zero if any jobs are scheduled between now and now+time.
Definition oslmic.c:182
int radio_init(void)
Initialize radio at system startup.
Definition radio_sx127x.c:1148
void radio_irq_handler_v2(u1_t dio, ostime_t tref)
Radio IRQ handler.
Definition radio_sx127x.c:1386
void radio_monitor_rssi(ostime_t n, oslmic_radio_rssi_t *pRssi)
Measure the current broadband RSSI for the current channel.
Definition radio_sx127x.c:1270
u4_t os_rmsbf4(xref2cu1_t buf)
Read 32-bit quantity from given pointer in big endian byte order.
Definition lmic.c:93
void os_wlsbf4(xref2u1_t buf, u4_t value)
Write 32-bit quantity into buffer in little endian byte order.
Definition lmic.c:107
bit_t os_setIdleJobFunction(osjob_t *job, osjobcb_t cb)
set function in idle job (for future use)
Definition oslmic.c:112
u2_t os_rlsbf2(xref2cu1_t buf)
Read 16-bit quantity from given pointer in little endian byte order.
Definition lmic.c:80
Information about the last and previous beacons.
Definition lmic.h:239
Definition oslmic.h:170
Definition oslmic.h:106
Definition lmic.h:218