LMiC adapted to work with SX1272MB2xAS LoRa shield.

Fork of LMiC by Timothy Mulrooney

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oslmic.h Source File

oslmic.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * Copyright (c) 2014-2015 IBM Corporation.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the Eclipse Public License v1.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.eclipse.org/legal/epl-v10.html
00007  *
00008  * Contributors:
00009  *    IBM Zurich Research Lab - initial API, implementation and documentation
00010  *
00011  * /////////////////////////////////////////////////////////////////////////////
00012  *
00013  * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring
00014  * device for agriculture using LoRaWAN technology.
00015  * 
00016  * Date of issued copy: 25 January 2018
00017  *
00018  * Modifications: 
00019  * - No external modifications of the existing "AS IT IS" software.
00020  ******************************************************************************/
00021 
00022 //! \file
00023 #ifndef _oslmic_h_
00024 #define _oslmic_h_
00025 
00026 // Dependencies required for the LoRa MAC in C to run.
00027 // These settings can be adapted to the underlying system.
00028 // You should not, however, change the lmic.[hc]
00029 
00030 
00031 
00032 //================================================================================
00033 //================================================================================
00034 // Target platform as C library
00035 typedef unsigned char      bit_t;
00036 typedef unsigned char      u1_t;
00037 typedef   signed char      s1_t;
00038 typedef unsigned short     u2_t;
00039 typedef          short     s2_t;
00040 typedef unsigned int       u4_t;
00041 typedef          int       s4_t;
00042 typedef unsigned long long u8_t;
00043 typedef          long long s8_t;
00044 typedef unsigned int       uint;
00045 typedef const char* str_t;
00046 
00047 #include <string.h>
00048 #include "hal.h"
00049 #define EV(a,b,c) /**/
00050 #define DO_DEVDB(field1,field2) /**/
00051 #if !defined(CFG_noassert)
00052 #define ASSERT(cond) if(!(cond)) hal_failed()
00053 #else
00054 #define ASSERT(cond) /**/
00055 #endif
00056 
00057 #define os_clearMem(a,b)   memset(a,0,b)
00058 #define os_copyMem(a,b,c)  memcpy(a,b,c)
00059 
00060 typedef     struct osjob_t osjob_t;
00061 typedef      struct band_t band_t;
00062 typedef   struct chnldef_t chnldef_t;
00063 typedef   struct rxsched_t rxsched_t;
00064 typedef   struct bcninfo_t bcninfo_t;
00065 typedef        const u1_t* xref2cu1_t;
00066 typedef              u1_t* xref2u1_t;
00067 #define TYPEDEF_xref2rps_t     typedef         rps_t* xref2rps_t
00068 #define TYPEDEF_xref2rxsched_t typedef     rxsched_t* xref2rxsched_t
00069 #define TYPEDEF_xref2chnldef_t typedef     chnldef_t* xref2chnldef_t
00070 #define TYPEDEF_xref2band_t    typedef        band_t* xref2band_t
00071 #define TYPEDEF_xref2osjob_t   typedef       osjob_t* xref2osjob_t
00072 
00073 #define SIZEOFEXPR(x) sizeof(x)
00074 
00075 #define ON_LMIC_EVENT(ev)  onEvent(ev)
00076 #define DECL_ON_LMIC_EVENT void onEvent(ev_t e)
00077 
00078 extern u4_t AESAUX[];
00079 extern u4_t AESKEY[];
00080 #define AESkey ((u1_t*)AESKEY)
00081 #define AESaux ((u1_t*)AESAUX)
00082 #define FUNC_ADDR(func) (&(func))
00083 
00084 u1_t radio_rand1 (void);
00085 #define os_getRndU1() radio_rand1()
00086 
00087 #define DEFINE_LMIC  struct lmic_t LMIC
00088 #define DECLARE_LMIC extern struct lmic_t LMIC
00089 
00090 void radio_init (void);
00091 void radio_irq_handler (u1_t dio);
00092 void os_init (void);
00093 void os_runloop (void);
00094 void os_runloop_once (void);
00095 
00096 //================================================================================
00097 
00098 
00099 #ifndef RX_RAMPUP
00100 #define RX_RAMPUP  (us2osticks(2000))
00101 #endif
00102 #ifndef TX_RAMPUP
00103 #define TX_RAMPUP  (us2osticks(2000))
00104 #endif
00105 
00106 #ifndef OSTICKS_PER_SEC
00107 #define OSTICKS_PER_SEC 15625
00108 #elif OSTICKS_PER_SEC < 10000 || OSTICKS_PER_SEC > 64516
00109 #error Illegal OSTICKS_PER_SEC - must be in range [10000:64516]. One tick must be 15.5us .. 100us long.
00110 #endif
00111 
00112 typedef s4_t  ostime_t;
00113 
00114 #if !HAS_ostick_conv
00115 #define us2osticks(us)   ((ostime_t)( ((s8_t)(us) * OSTICKS_PER_SEC) / 1000000))
00116 #define ms2osticks(ms)   ((ostime_t)( ((s8_t)(ms) * OSTICKS_PER_SEC)    / 1000))
00117 #define sec2osticks(sec) ((ostime_t)( (s8_t)(sec) * OSTICKS_PER_SEC))
00118 #define osticks2ms(os)   ((s4_t)(((os)*(s8_t)1000    ) / OSTICKS_PER_SEC))
00119 #define osticks2us(os)   ((s4_t)(((os)*(s8_t)1000000 ) / OSTICKS_PER_SEC))
00120 // Special versions
00121 #define us2osticksCeil(us)  ((ostime_t)( ((s8_t)(us) * OSTICKS_PER_SEC + 999999) / 1000000))
00122 #define us2osticksRound(us) ((ostime_t)( ((s8_t)(us) * OSTICKS_PER_SEC + 500000) / 1000000))
00123 #define ms2osticksCeil(ms)  ((ostime_t)( ((s8_t)(ms) * OSTICKS_PER_SEC + 999) / 1000))
00124 #define ms2osticksRound(ms) ((ostime_t)( ((s8_t)(ms) * OSTICKS_PER_SEC + 500) / 1000))
00125 #endif
00126 
00127 
00128 struct osjob_t;  // fwd decl.
00129 typedef void (*osjobcb_t) (struct osjob_t*);
00130 struct osjob_t {
00131     struct osjob_t* next;
00132     ostime_t deadline;
00133     osjobcb_t  func;
00134 };
00135 TYPEDEF_xref2osjob_t;
00136 
00137 
00138 #ifndef HAS_os_calls
00139 
00140 #ifndef os_getDevKey
00141 void os_getDevKey (xref2u1_t buf);
00142 #endif
00143 #ifndef os_getArtEui
00144 void os_getArtEui (xref2u1_t buf);
00145 #endif
00146 #ifndef os_getDevEui
00147 void os_getDevEui (xref2u1_t buf);
00148 #endif
00149 #ifndef os_setCallback
00150 void os_setCallback (xref2osjob_t job, osjobcb_t cb);
00151 #endif
00152 #ifndef os_setTimedCallback
00153 void os_setTimedCallback (xref2osjob_t job, ostime_t time, osjobcb_t cb);
00154 #endif
00155 #ifndef os_clearCallback
00156 void os_clearCallback (xref2osjob_t job);
00157 #endif
00158 #ifndef os_getTime
00159 ostime_t os_getTime (void);
00160 #endif
00161 #ifndef os_getTimeSecs
00162 uint os_getTimeSecs (void);
00163 #endif
00164 #ifndef os_radio
00165 void os_radio (u1_t mode);
00166 #endif
00167 #ifndef os_getBattLevel
00168 u1_t os_getBattLevel (void);
00169 #endif
00170 
00171 #ifndef os_rlsbf4
00172 //! Read 32-bit quantity from given pointer in little endian byte order.
00173 u4_t os_rlsbf4 (xref2cu1_t buf);
00174 #endif
00175 #ifndef os_wlsbf4
00176 //! Write 32-bit quntity into buffer in little endian byte order.
00177 void os_wlsbf4 (xref2u1_t buf, u4_t value);
00178 #endif
00179 #ifndef os_rmsbf4
00180 //! Read 32-bit quantity from given pointer in big endian byte order.
00181 u4_t os_rmsbf4 (xref2cu1_t buf);
00182 #endif
00183 #ifndef os_wmsbf4
00184 //! Write 32-bit quntity into buffer in big endian byte order.
00185 void os_wmsbf4 (xref2u1_t buf, u4_t value);
00186 #endif
00187 #ifndef os_rlsbf2
00188 //! Read 16-bit quantity from given pointer in little endian byte order.
00189 u2_t os_rlsbf2 (xref2cu1_t buf);
00190 #endif
00191 #ifndef os_wlsbf2
00192 //! Write 16-bit quntity into buffer in little endian byte order.
00193 void os_wlsbf2 (xref2u1_t buf, u2_t value);
00194 #endif
00195 
00196 //! Get random number (default impl for u2_t).
00197 #ifndef os_getRndU2
00198 #define os_getRndU2() ((u2_t)((os_getRndU1()<<8)|os_getRndU1()))
00199 #endif
00200 #ifndef os_crc16
00201 u2_t os_crc16 (xref2u1_t d, uint len);
00202 #endif
00203 
00204 #endif // !HAS_os_calls
00205 
00206 // ======================================================================
00207 // AES support 
00208 // !!Keep in sync with lorabase.hpp!!
00209 
00210 #ifndef AES_ENC  // if AES_ENC is defined as macro all other values must be too
00211 #define AES_ENC       0x00 
00212 #define AES_DEC       0x01
00213 #define AES_MIC       0x02
00214 #define AES_CTR       0x04
00215 #define AES_MICNOAUX  0x08
00216 #endif
00217 #ifndef AESkey  // if AESkey is defined as macro all other values must be too
00218 extern xref2u1_t AESkey;
00219 extern xref2u1_t AESaux;
00220 #endif
00221 #ifndef os_aes
00222 u4_t os_aes (u1_t mode, xref2u1_t buf, u2_t len);
00223 #endif
00224 
00225 
00226 
00227 #endif // _oslmic_h_