LMiC adapted to work with SX1272MB2xAS LoRa shield.
Fork of LMiC by
oslmic.cpp@8:5879e83f632a, 2018-04-02 (annotated)
- Committer:
- GTsapparellas
- Date:
- Mon Apr 02 12:04:59 2018 +0000
- Revision:
- 8:5879e83f632a
- Parent:
- 5:464c1f2d6cbb
LoRa Node consisting of FRDM-K64F along with SX1272MB2xAS LoRa shield employed through IBM's LMiC library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:62d1edcc13d1 | 1 | /******************************************************************************* |
mluis | 1:d3b7bde3995c | 2 | * Copyright (c) 2014-2015 IBM Corporation. |
mluis | 0:62d1edcc13d1 | 3 | * All rights reserved. This program and the accompanying materials |
mluis | 0:62d1edcc13d1 | 4 | * are made available under the terms of the Eclipse Public License v1.0 |
mluis | 0:62d1edcc13d1 | 5 | * which accompanies this distribution, and is available at |
mluis | 0:62d1edcc13d1 | 6 | * http://www.eclipse.org/legal/epl-v10.html |
mluis | 0:62d1edcc13d1 | 7 | * |
mluis | 0:62d1edcc13d1 | 8 | * Contributors: |
mluis | 0:62d1edcc13d1 | 9 | * IBM Zurich Research Lab - initial API, implementation and documentation |
GTsapparellas | 8:5879e83f632a | 10 | * |
GTsapparellas | 8:5879e83f632a | 11 | * ///////////////////////////////////////////////////////////////////////////// |
GTsapparellas | 8:5879e83f632a | 12 | * |
GTsapparellas | 8:5879e83f632a | 13 | * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring |
GTsapparellas | 8:5879e83f632a | 14 | * device for agriculture using LoRaWAN technology. |
GTsapparellas | 8:5879e83f632a | 15 | * |
GTsapparellas | 8:5879e83f632a | 16 | * Date of issued copy: 25 January 2018 |
GTsapparellas | 8:5879e83f632a | 17 | * |
GTsapparellas | 8:5879e83f632a | 18 | * Modifications: |
GTsapparellas | 8:5879e83f632a | 19 | * - No external modifications of the existing "AS IT IS" software. |
GTsapparellas | 8:5879e83f632a | 20 | * - Comment out debug messages of each function for more clear |
GTsapparellas | 8:5879e83f632a | 21 | * debugging procedure. |
GTsapparellas | 8:5879e83f632a | 22 | ******************************************************************************/ |
mluis | 0:62d1edcc13d1 | 23 | |
mluis | 0:62d1edcc13d1 | 24 | #include "lmic.h" |
tmulrooney | 5:464c1f2d6cbb | 25 | #include "debug.h" |
mluis | 0:62d1edcc13d1 | 26 | |
mluis | 0:62d1edcc13d1 | 27 | // RUNTIME STATE |
tmulrooney | 5:464c1f2d6cbb | 28 | static struct |
tmulrooney | 5:464c1f2d6cbb | 29 | { |
mluis | 0:62d1edcc13d1 | 30 | osjob_t* scheduledjobs; |
mluis | 0:62d1edcc13d1 | 31 | osjob_t* runnablejobs; |
mluis | 0:62d1edcc13d1 | 32 | } OS; |
mluis | 0:62d1edcc13d1 | 33 | |
tmulrooney | 5:464c1f2d6cbb | 34 | void os_init () |
tmulrooney | 5:464c1f2d6cbb | 35 | { |
GTsapparellas | 8:5879e83f632a | 36 | //debug("os_init enter\r\n"); |
mluis | 0:62d1edcc13d1 | 37 | memset(&OS, 0x00, sizeof(OS)); |
mluis | 0:62d1edcc13d1 | 38 | hal_init(); |
mluis | 0:62d1edcc13d1 | 39 | radio_init(); |
mluis | 0:62d1edcc13d1 | 40 | LMIC_init(); |
GTsapparellas | 8:5879e83f632a | 41 | //debug("os_init exit\r\n"); |
mluis | 0:62d1edcc13d1 | 42 | } |
mluis | 0:62d1edcc13d1 | 43 | |
tmulrooney | 5:464c1f2d6cbb | 44 | ostime_t os_getTime () |
tmulrooney | 5:464c1f2d6cbb | 45 | { |
GTsapparellas | 8:5879e83f632a | 46 | //debug("os_getTime enter %d\r\n",hal_ticks()); |
mluis | 0:62d1edcc13d1 | 47 | return hal_ticks(); |
mluis | 0:62d1edcc13d1 | 48 | } |
mluis | 0:62d1edcc13d1 | 49 | |
tmulrooney | 5:464c1f2d6cbb | 50 | static u1_t unlinkjob (osjob_t** pnext, osjob_t* job) |
tmulrooney | 5:464c1f2d6cbb | 51 | { |
GTsapparellas | 8:5879e83f632a | 52 | //debug("unlinkjob enter\r\n"); |
mluis | 0:62d1edcc13d1 | 53 | for( ; *pnext; pnext = &((*pnext)->next)) { |
mluis | 0:62d1edcc13d1 | 54 | if(*pnext == job) { // unlink |
mluis | 0:62d1edcc13d1 | 55 | *pnext = job->next; |
mluis | 0:62d1edcc13d1 | 56 | return 1; |
mluis | 0:62d1edcc13d1 | 57 | } |
mluis | 0:62d1edcc13d1 | 58 | } |
mluis | 0:62d1edcc13d1 | 59 | return 0; |
mluis | 0:62d1edcc13d1 | 60 | } |
mluis | 0:62d1edcc13d1 | 61 | |
mluis | 0:62d1edcc13d1 | 62 | // clear scheduled job |
tmulrooney | 5:464c1f2d6cbb | 63 | void os_clearCallback (osjob_t* job) |
tmulrooney | 5:464c1f2d6cbb | 64 | { |
GTsapparellas | 8:5879e83f632a | 65 | //debug("os_clearCallback enter\r\n"); |
mluis | 0:62d1edcc13d1 | 66 | hal_disableIRQs(); |
mluis | 0:62d1edcc13d1 | 67 | unlinkjob(&OS.scheduledjobs, job) || unlinkjob(&OS.runnablejobs, job); |
mluis | 0:62d1edcc13d1 | 68 | hal_enableIRQs(); |
mluis | 0:62d1edcc13d1 | 69 | } |
mluis | 0:62d1edcc13d1 | 70 | |
mluis | 0:62d1edcc13d1 | 71 | // schedule immediately runnable job |
tmulrooney | 5:464c1f2d6cbb | 72 | void os_setCallback (osjob_t* job, osjobcb_t cb) |
tmulrooney | 5:464c1f2d6cbb | 73 | { |
GTsapparellas | 8:5879e83f632a | 74 | //debug("os_setCallback enter\r\n"); |
mluis | 0:62d1edcc13d1 | 75 | osjob_t** pnext; |
mluis | 0:62d1edcc13d1 | 76 | hal_disableIRQs(); |
mluis | 0:62d1edcc13d1 | 77 | // remove if job was already queued |
mluis | 0:62d1edcc13d1 | 78 | os_clearCallback(job); |
mluis | 0:62d1edcc13d1 | 79 | // fill-in job |
mluis | 0:62d1edcc13d1 | 80 | job->func = cb; |
mluis | 0:62d1edcc13d1 | 81 | job->next = NULL; |
mluis | 0:62d1edcc13d1 | 82 | // add to end of run queue |
mluis | 0:62d1edcc13d1 | 83 | for(pnext=&OS.runnablejobs; *pnext; pnext=&((*pnext)->next)); |
mluis | 0:62d1edcc13d1 | 84 | *pnext = job; |
mluis | 0:62d1edcc13d1 | 85 | hal_enableIRQs(); |
mluis | 0:62d1edcc13d1 | 86 | } |
mluis | 0:62d1edcc13d1 | 87 | |
mluis | 0:62d1edcc13d1 | 88 | // schedule timed job |
tmulrooney | 5:464c1f2d6cbb | 89 | void os_setTimedCallback (osjob_t* job, ostime_t time, osjobcb_t cb) |
tmulrooney | 5:464c1f2d6cbb | 90 | { |
GTsapparellas | 8:5879e83f632a | 91 | //debug("os_setTimedCallback enter %d\r\n",time); |
mluis | 0:62d1edcc13d1 | 92 | osjob_t** pnext; |
mluis | 0:62d1edcc13d1 | 93 | hal_disableIRQs(); |
mluis | 0:62d1edcc13d1 | 94 | // remove if job was already queued |
mluis | 0:62d1edcc13d1 | 95 | os_clearCallback(job); |
mluis | 0:62d1edcc13d1 | 96 | // fill-in job |
mluis | 0:62d1edcc13d1 | 97 | job->deadline = time; |
mluis | 0:62d1edcc13d1 | 98 | job->func = cb; |
mluis | 0:62d1edcc13d1 | 99 | job->next = NULL; |
mluis | 0:62d1edcc13d1 | 100 | // insert into schedule |
mluis | 0:62d1edcc13d1 | 101 | for(pnext=&OS.scheduledjobs; *pnext; pnext=&((*pnext)->next)) { |
mluis | 1:d3b7bde3995c | 102 | if((*pnext)->deadline - time > 0) { // (cmp diff, not abs!) |
mluis | 0:62d1edcc13d1 | 103 | // enqueue before next element and stop |
mluis | 0:62d1edcc13d1 | 104 | job->next = *pnext; |
mluis | 0:62d1edcc13d1 | 105 | break; |
mluis | 0:62d1edcc13d1 | 106 | } |
mluis | 0:62d1edcc13d1 | 107 | } |
mluis | 0:62d1edcc13d1 | 108 | *pnext = job; |
mluis | 0:62d1edcc13d1 | 109 | hal_enableIRQs(); |
mluis | 0:62d1edcc13d1 | 110 | } |
mluis | 0:62d1edcc13d1 | 111 | |
mluis | 0:62d1edcc13d1 | 112 | // execute jobs from timer and from run queue |
tmulrooney | 5:464c1f2d6cbb | 113 | void os_runloop () |
tmulrooney | 5:464c1f2d6cbb | 114 | { |
GTsapparellas | 8:5879e83f632a | 115 | debug_str("os_runloop enter\r\n"); |
mluis | 0:62d1edcc13d1 | 116 | while(1) { |
mluis | 0:62d1edcc13d1 | 117 | osjob_t* j = NULL; |
mluis | 0:62d1edcc13d1 | 118 | hal_disableIRQs(); |
mluis | 1:d3b7bde3995c | 119 | // check for runnable jobs |
mluis | 1:d3b7bde3995c | 120 | if(OS.runnablejobs) { |
mluis | 0:62d1edcc13d1 | 121 | j = OS.runnablejobs; |
mluis | 0:62d1edcc13d1 | 122 | OS.runnablejobs = j->next; |
mluis | 1:d3b7bde3995c | 123 | } else if(OS.scheduledjobs && hal_checkTimer(OS.scheduledjobs->deadline)) { // check for expired timed jobs |
mluis | 0:62d1edcc13d1 | 124 | j = OS.scheduledjobs; |
mluis | 0:62d1edcc13d1 | 125 | OS.scheduledjobs = j->next; |
mluis | 0:62d1edcc13d1 | 126 | } else { // nothing pending |
mluis | 0:62d1edcc13d1 | 127 | hal_sleep(); // wake by irq (timer already restarted) |
mluis | 0:62d1edcc13d1 | 128 | } |
mluis | 0:62d1edcc13d1 | 129 | hal_enableIRQs(); |
mluis | 0:62d1edcc13d1 | 130 | if(j) { // run job callback |
mluis | 1:d3b7bde3995c | 131 | j->func(j); |
mluis | 0:62d1edcc13d1 | 132 | } |
mluis | 0:62d1edcc13d1 | 133 | } |
mluis | 0:62d1edcc13d1 | 134 | } |
GTsapparellas | 8:5879e83f632a | 135 | |
GTsapparellas | 8:5879e83f632a | 136 | void os_runloop_once() { |
GTsapparellas | 8:5879e83f632a | 137 | |
GTsapparellas | 8:5879e83f632a | 138 | bool has_deadline = false; |
GTsapparellas | 8:5879e83f632a | 139 | osjob_t* j = NULL; |
GTsapparellas | 8:5879e83f632a | 140 | hal_disableIRQs(); |
GTsapparellas | 8:5879e83f632a | 141 | |
GTsapparellas | 8:5879e83f632a | 142 | // check for runnable jobs |
GTsapparellas | 8:5879e83f632a | 143 | if(OS.runnablejobs) { |
GTsapparellas | 8:5879e83f632a | 144 | j = OS.runnablejobs; |
GTsapparellas | 8:5879e83f632a | 145 | OS.runnablejobs = j->next; |
GTsapparellas | 8:5879e83f632a | 146 | } else if(OS.scheduledjobs && hal_checkTimer(OS.scheduledjobs->deadline)) { // check for expired timed jobs |
GTsapparellas | 8:5879e83f632a | 147 | j = OS.scheduledjobs; |
GTsapparellas | 8:5879e83f632a | 148 | OS.scheduledjobs = j->next; |
GTsapparellas | 8:5879e83f632a | 149 | has_deadline = true; |
GTsapparellas | 8:5879e83f632a | 150 | } else { // nothing pending |
GTsapparellas | 8:5879e83f632a | 151 | hal_sleep(); // wake by irq (timer already restarted) |
GTsapparellas | 8:5879e83f632a | 152 | } |
GTsapparellas | 8:5879e83f632a | 153 | hal_enableIRQs(); |
GTsapparellas | 8:5879e83f632a | 154 | if(j) { // run job callback |
GTsapparellas | 8:5879e83f632a | 155 | // printf("%lu: Running job %p, cb %p, deadline %lu\r\n", os_getTime(), j, j->func, has_deadline ? j->deadline : 0); |
GTsapparellas | 8:5879e83f632a | 156 | j->func(j); |
GTsapparellas | 8:5879e83f632a | 157 | } |
GTsapparellas | 8:5879e83f632a | 158 | } |