Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
LoRa/lora_driver.c@5:53302861bfea, 2018-07-16 (annotated)
- Committer:
- fahadmirza
- Date:
- Mon Jul 16 20:12:42 2018 +0000
- Revision:
- 5:53302861bfea
- Parent:
- lora_driver.c@2:1ef859bc5cd2
- Child:
- 10:19da323c2bc0
Updated directories;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fahadmirza | 0:a0c5877bd360 | 1 | /* |
fahadmirza | 0:a0c5877bd360 | 2 | _ _ _____ _______ |
fahadmirza | 0:a0c5877bd360 | 3 | | | | | |_ _| |__ __| |
fahadmirza | 0:a0c5877bd360 | 4 | | |__| | __ ___ __ | | ___ | | |
fahadmirza | 0:a0c5877bd360 | 5 | | __ |/ _` \ \/ / | | / _ \| | |
fahadmirza | 0:a0c5877bd360 | 6 | | | | | (_| |> < _| || (_) | | |
fahadmirza | 0:a0c5877bd360 | 7 | |_| |_|\__,_/_/\_\_____\___/|_| |
fahadmirza | 0:a0c5877bd360 | 8 | (C)2017 HaxIoT |
fahadmirza | 0:a0c5877bd360 | 9 | */ |
fahadmirza | 0:a0c5877bd360 | 10 | /******************************************************************************* |
fahadmirza | 0:a0c5877bd360 | 11 | * @File : lora_driver.c |
fahadmirza | 0:a0c5877bd360 | 12 | * @Author : Fahad Mirza (Haxiot) |
fahadmirza | 0:a0c5877bd360 | 13 | * @Version : V1.0.0 |
fahadmirza | 0:a0c5877bd360 | 14 | * @Modified: 12-Apr-2018 |
fahadmirza | 0:a0c5877bd360 | 15 | * @Brief : LoRa Driver |
fahadmirza | 0:a0c5877bd360 | 16 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 17 | * @attention |
fahadmirza | 0:a0c5877bd360 | 18 | * |
fahadmirza | 0:a0c5877bd360 | 19 | * <h2><center>© COPYRIGHT(c) 2017 Haxiot</center></h2> |
fahadmirza | 0:a0c5877bd360 | 20 | * |
fahadmirza | 0:a0c5877bd360 | 21 | * Redistribution and use in source and binary forms, with or without modification, |
fahadmirza | 0:a0c5877bd360 | 22 | * are permitted provided that the following conditions are met: |
fahadmirza | 0:a0c5877bd360 | 23 | * 1. Redistributions of source code must retain the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 24 | * this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 25 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 26 | * this list of conditions and the following disclaimer in the documentation |
fahadmirza | 0:a0c5877bd360 | 27 | * and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 28 | * 3. Neither the name of Haxiot nor the names of its contributors |
fahadmirza | 0:a0c5877bd360 | 29 | * may be used to endorse or promote products derived from this software |
fahadmirza | 0:a0c5877bd360 | 30 | * without specific prior written permission. |
fahadmirza | 0:a0c5877bd360 | 31 | * |
fahadmirza | 0:a0c5877bd360 | 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
fahadmirza | 0:a0c5877bd360 | 33 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fahadmirza | 0:a0c5877bd360 | 34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
fahadmirza | 0:a0c5877bd360 | 35 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
fahadmirza | 0:a0c5877bd360 | 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fahadmirza | 0:a0c5877bd360 | 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
fahadmirza | 0:a0c5877bd360 | 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
fahadmirza | 0:a0c5877bd360 | 39 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
fahadmirza | 0:a0c5877bd360 | 40 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
fahadmirza | 0:a0c5877bd360 | 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 42 | * |
fahadmirza | 0:a0c5877bd360 | 43 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 44 | */ |
fahadmirza | 0:a0c5877bd360 | 45 | /* Includes ------------------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 46 | #include <stdio.h> |
fahadmirza | 0:a0c5877bd360 | 47 | #include "hxc_client.h" |
fahadmirza | 0:a0c5877bd360 | 48 | #include "debug.h" |
fahadmirza | 0:a0c5877bd360 | 49 | #include "stm32l0xx_nucleo.h" |
fahadmirza | 0:a0c5877bd360 | 50 | #include "time_server.h" |
fahadmirza | 0:a0c5877bd360 | 51 | #include "lora_driver.h" |
fahadmirza | 0:a0c5877bd360 | 52 | #include "tiny_sscanf.h" |
fahadmirza | 0:a0c5877bd360 | 53 | #include "utilities.h" |
fahadmirza | 0:a0c5877bd360 | 54 | |
fahadmirza | 0:a0c5877bd360 | 55 | /* Private Macros ------------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 56 | #define JOIN_SEND_DELAY_MAX (10000U) // Randomization range - 10s |
fahadmirza | 0:a0c5877bd360 | 57 | #define JOIN_STATUS_REQ_DELAY (7000U) // milliseconds. Join req takes 6s. |
fahadmirza | 0:a0c5877bd360 | 58 | |
fahadmirza | 0:a0c5877bd360 | 59 | /* Private type definitions --------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 60 | // LoRaWAN State Machine states |
fahadmirza | 0:a0c5877bd360 | 61 | typedef enum eDevicState |
fahadmirza | 0:a0c5877bd360 | 62 | { |
fahadmirza | 0:a0c5877bd360 | 63 | DEVICE_INIT, |
fahadmirza | 0:a0c5877bd360 | 64 | DEVICE_CONFIG, |
fahadmirza | 0:a0c5877bd360 | 65 | DEVICE_JOIN, |
fahadmirza | 0:a0c5877bd360 | 66 | DEVICE_SEND, |
fahadmirza | 0:a0c5877bd360 | 67 | SLEEP_DEVICE, |
fahadmirza | 0:a0c5877bd360 | 68 | DEVICE_DATA_RECEIVED, |
fahadmirza | 0:a0c5877bd360 | 69 | DEVICE_JOIN_STATUS_CHECK |
fahadmirza | 0:a0c5877bd360 | 70 | } eDeviceState_t; |
fahadmirza | 0:a0c5877bd360 | 71 | |
fahadmirza | 0:a0c5877bd360 | 72 | /* Private global variables --------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 73 | static sLoraConfig_t *LoraConfigParam; |
fahadmirza | 0:a0c5877bd360 | 74 | static sLoraDriverParam_t *LoraDriverParam; |
fahadmirza | 0:a0c5877bd360 | 75 | static volatile eDeviceState_t DeviceState = DEVICE_INIT; |
fahadmirza | 0:a0c5877bd360 | 76 | |
fahadmirza | 0:a0c5877bd360 | 77 | static TimerEvent_t JoinRequestTimer; |
fahadmirza | 0:a0c5877bd360 | 78 | static TimerEvent_t SensorMeasureTimer; |
fahadmirza | 0:a0c5877bd360 | 79 | static TimerEvent_t NucleoLedTimer; |
fahadmirza | 0:a0c5877bd360 | 80 | static TimerEvent_t JoinStatusDelayTimer; |
fahadmirza | 0:a0c5877bd360 | 81 | |
fahadmirza | 0:a0c5877bd360 | 82 | // Object definition for data to be sent to loRa application server |
fahadmirza | 0:a0c5877bd360 | 83 | static uint8_t DataBinaryBuff[64]; |
fahadmirza | 0:a0c5877bd360 | 84 | static sSendDataBinary_t SendDataBinary = {DataBinaryBuff, 0 , 0, 0}; |
fahadmirza | 0:a0c5877bd360 | 85 | |
fahadmirza | 0:a0c5877bd360 | 86 | // RNG handler declaration |
fahadmirza | 0:a0c5877bd360 | 87 | RNG_HandleTypeDef RngHandle = {.Instance = RNG}; |
fahadmirza | 0:a0c5877bd360 | 88 | |
fahadmirza | 0:a0c5877bd360 | 89 | /* Private functions ---------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 90 | static void Lora_ReadData(sRecvDataBinary_t *receivedData); |
fahadmirza | 0:a0c5877bd360 | 91 | static void Lora_OnLedTimerEvent(void); |
fahadmirza | 0:a0c5877bd360 | 92 | static void Lora_OnJoinRequestTimerEvt(void); |
fahadmirza | 0:a0c5877bd360 | 93 | static void Lora_OnJoinStatusDelayTimerEvt(void); |
fahadmirza | 0:a0c5877bd360 | 94 | static void Lora_OnSensorMeasureTimerEvt(void); |
fahadmirza | 0:a0c5877bd360 | 95 | static void setJoinRequestTimer(void); |
fahadmirza | 0:a0c5877bd360 | 96 | |
fahadmirza | 0:a0c5877bd360 | 97 | |
fahadmirza | 0:a0c5877bd360 | 98 | /* Function definitions ------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 99 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 100 | * @Brief : Initialize LoRa Modem |
fahadmirza | 0:a0c5877bd360 | 101 | * @Param : sLoraConfig_t |
fahadmirza | 0:a0c5877bd360 | 102 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 103 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 104 | void Lora_init(sLoraConfig_t *loraConfig, sLoraDriverParam_t *loraDriverParam) |
fahadmirza | 0:a0c5877bd360 | 105 | { |
fahadmirza | 0:a0c5877bd360 | 106 | |
fahadmirza | 0:a0c5877bd360 | 107 | LoraConfigParam = loraConfig; |
fahadmirza | 0:a0c5877bd360 | 108 | LoraDriverParam = loraDriverParam; |
fahadmirza | 0:a0c5877bd360 | 109 | |
fahadmirza | 0:a0c5877bd360 | 110 | if(Modem_Init() != AT_OK) |
fahadmirza | 0:a0c5877bd360 | 111 | { |
fahadmirza | 0:a0c5877bd360 | 112 | DBG_PRINTF("Modem_Init failed\r\n"); |
fahadmirza | 0:a0c5877bd360 | 113 | } |
fahadmirza | 0:a0c5877bd360 | 114 | |
fahadmirza | 0:a0c5877bd360 | 115 | // Initialize RNG for join request send randomization |
fahadmirza | 0:a0c5877bd360 | 116 | HAL_RNG_Init(&RngHandle); |
fahadmirza | 0:a0c5877bd360 | 117 | |
fahadmirza | 0:a0c5877bd360 | 118 | |
fahadmirza | 0:a0c5877bd360 | 119 | // Timer for join request send |
fahadmirza | 0:a0c5877bd360 | 120 | TimerInit(&JoinRequestTimer, Lora_OnJoinRequestTimerEvt); |
fahadmirza | 0:a0c5877bd360 | 121 | // Timer for join status check |
fahadmirza | 0:a0c5877bd360 | 122 | TimerInit(&JoinStatusDelayTimer, Lora_OnJoinStatusDelayTimerEvt); |
fahadmirza | 0:a0c5877bd360 | 123 | // Timer for sensor occurrence measure |
fahadmirza | 0:a0c5877bd360 | 124 | TimerInit(&SensorMeasureTimer, Lora_OnSensorMeasureTimerEvt); |
fahadmirza | 0:a0c5877bd360 | 125 | // Timer for Nucleo LED |
fahadmirza | 0:a0c5877bd360 | 126 | TimerInit(&NucleoLedTimer, Lora_OnLedTimerEvent); |
fahadmirza | 0:a0c5877bd360 | 127 | } |
fahadmirza | 0:a0c5877bd360 | 128 | |
fahadmirza | 0:a0c5877bd360 | 129 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 130 | * @Brief : Check if the modem responds |
fahadmirza | 0:a0c5877bd360 | 131 | * @Param : void |
fahadmirza | 0:a0c5877bd360 | 132 | * @Return : AT_OK or other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 133 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 134 | static eAtStatus_t is_modem_working(void) |
fahadmirza | 0:a0c5877bd360 | 135 | { |
fahadmirza | 0:a0c5877bd360 | 136 | return Modem_AT_Cmd(AT_CTRL, AT, NULL); |
fahadmirza | 0:a0c5877bd360 | 137 | } |
fahadmirza | 0:a0c5877bd360 | 138 | |
fahadmirza | 0:a0c5877bd360 | 139 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 140 | * @Brief : Set Device EUI |
fahadmirza | 0:a0c5877bd360 | 141 | * @Param : Pointer to Device EUI |
fahadmirza | 0:a0c5877bd360 | 142 | * @Return : AT_OK or other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 143 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 144 | eAtStatus_t Lora_setDevEui(char *devEui) |
fahadmirza | 0:a0c5877bd360 | 145 | { |
fahadmirza | 0:a0c5877bd360 | 146 | return Modem_AT_Cmd(AT_SET, AT_DEVEUI, devEui); |
fahadmirza | 0:a0c5877bd360 | 147 | } |
fahadmirza | 0:a0c5877bd360 | 148 | |
fahadmirza | 0:a0c5877bd360 | 149 | /****************************************************************************** |
fahadmirza | 2:1ef859bc5cd2 | 150 | * @Brief : Turn on or off ADR |
fahadmirza | 2:1ef859bc5cd2 | 151 | * @Param : ADR_ON or ADR_OFF |
fahadmirza | 2:1ef859bc5cd2 | 152 | * @Return : AT_OK or other eAtStatus_t |
fahadmirza | 2:1ef859bc5cd2 | 153 | ******************************************************************************/ |
fahadmirza | 2:1ef859bc5cd2 | 154 | eAtStatus_t Lora_setAdr(eAdrStatus_t adrStatus) |
fahadmirza | 2:1ef859bc5cd2 | 155 | { |
fahadmirza | 2:1ef859bc5cd2 | 156 | //char adr = (adrStatus == ADR_ON ? '1' : '0'); |
fahadmirza | 2:1ef859bc5cd2 | 157 | return Modem_AT_Cmd(AT_SET, AT_ADR, (uint8_t *)(&adrStatus)); |
fahadmirza | 2:1ef859bc5cd2 | 158 | } |
fahadmirza | 2:1ef859bc5cd2 | 159 | |
fahadmirza | 2:1ef859bc5cd2 | 160 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 161 | * @Brief : Set Application EUI |
fahadmirza | 0:a0c5877bd360 | 162 | * @Param : Pointer to Application EUI |
fahadmirza | 0:a0c5877bd360 | 163 | * @Return : AT_OK or other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 164 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 165 | eAtStatus_t Lora_setAppEui(char *appEui) |
fahadmirza | 0:a0c5877bd360 | 166 | { |
fahadmirza | 0:a0c5877bd360 | 167 | return Modem_AT_Cmd(AT_SET, AT_APPEUI, appEui); |
fahadmirza | 0:a0c5877bd360 | 168 | } |
fahadmirza | 0:a0c5877bd360 | 169 | |
fahadmirza | 0:a0c5877bd360 | 170 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 171 | * @Brief : Set Application Key |
fahadmirza | 0:a0c5877bd360 | 172 | * @Param : Pointer to Application Key |
fahadmirza | 0:a0c5877bd360 | 173 | * @Return : AT_OK or other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 174 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 175 | eAtStatus_t Lora_setAppKey(char *appKey) |
fahadmirza | 0:a0c5877bd360 | 176 | { |
fahadmirza | 0:a0c5877bd360 | 177 | return Modem_AT_Cmd(AT_SET, AT_APPKEY, appKey); |
fahadmirza | 0:a0c5877bd360 | 178 | } |
fahadmirza | 0:a0c5877bd360 | 179 | |
fahadmirza | 0:a0c5877bd360 | 180 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 181 | * @Brief : Set join mode |
fahadmirza | 0:a0c5877bd360 | 182 | * @Param : OTAA or ABP |
fahadmirza | 0:a0c5877bd360 | 183 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 184 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 185 | eAtStatus_t Lora_setJoinMode(eJoinMode_t joinMode) |
fahadmirza | 0:a0c5877bd360 | 186 | { |
fahadmirza | 0:a0c5877bd360 | 187 | if(joinMode == OTAA) |
fahadmirza | 0:a0c5877bd360 | 188 | { |
fahadmirza | 0:a0c5877bd360 | 189 | return Modem_AT_Cmd(AT_SET, AT_NJM, "OTAA"); |
fahadmirza | 0:a0c5877bd360 | 190 | } |
fahadmirza | 0:a0c5877bd360 | 191 | |
fahadmirza | 0:a0c5877bd360 | 192 | return Modem_AT_Cmd(AT_SET, AT_NJM, "ABP"); |
fahadmirza | 0:a0c5877bd360 | 193 | } |
fahadmirza | 0:a0c5877bd360 | 194 | |
fahadmirza | 0:a0c5877bd360 | 195 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 196 | * @Brief : Set Class |
fahadmirza | 0:a0c5877bd360 | 197 | * @Param : CLASS_A or CLASS_C |
fahadmirza | 0:a0c5877bd360 | 198 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 199 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 200 | eAtStatus_t Lora_setClass(char class) |
fahadmirza | 0:a0c5877bd360 | 201 | { |
fahadmirza | 0:a0c5877bd360 | 202 | return Modem_AT_Cmd(AT_SET, AT_CLASS, &class); |
fahadmirza | 0:a0c5877bd360 | 203 | } |
fahadmirza | 0:a0c5877bd360 | 204 | |
fahadmirza | 0:a0c5877bd360 | 205 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 206 | * @Brief : Join network and initiate join sleep transition timer |
fahadmirza | 0:a0c5877bd360 | 207 | * @Param : None |
fahadmirza | 0:a0c5877bd360 | 208 | * @Retval : AT_OK or other eAtStatus_t |
fahadmirza | 0:a0c5877bd360 | 209 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 210 | static eAtStatus_t Lora_Join(void) |
fahadmirza | 0:a0c5877bd360 | 211 | { |
fahadmirza | 0:a0c5877bd360 | 212 | return Modem_AT_Cmd(AT_CTRL, AT_JOIN, NULL); |
fahadmirza | 0:a0c5877bd360 | 213 | } |
fahadmirza | 0:a0c5877bd360 | 214 | |
fahadmirza | 0:a0c5877bd360 | 215 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 216 | * @Brief : Check JOIN status |
fahadmirza | 0:a0c5877bd360 | 217 | * @Param : None |
fahadmirza | 0:a0c5877bd360 | 218 | * @Retval : JOINED or NOT_JOINED |
fahadmirza | 0:a0c5877bd360 | 219 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 220 | static eJoinStatus_t Lora_getJoinStatus(void) |
fahadmirza | 0:a0c5877bd360 | 221 | { |
fahadmirza | 0:a0c5877bd360 | 222 | char joinStatus[12]; // "NOT JOINED" is 10 characters + 1 Null char |
fahadmirza | 0:a0c5877bd360 | 223 | |
fahadmirza | 0:a0c5877bd360 | 224 | Modem_AT_Cmd(AT_GET, AT_NJS, joinStatus); |
fahadmirza | 0:a0c5877bd360 | 225 | |
fahadmirza | 0:a0c5877bd360 | 226 | if(strncmp("JOINED", joinStatus, 6) == 0) |
fahadmirza | 0:a0c5877bd360 | 227 | { |
fahadmirza | 0:a0c5877bd360 | 228 | return JOINED; |
fahadmirza | 0:a0c5877bd360 | 229 | } |
fahadmirza | 0:a0c5877bd360 | 230 | |
fahadmirza | 0:a0c5877bd360 | 231 | return NOT_JOINED; |
fahadmirza | 0:a0c5877bd360 | 232 | } |
fahadmirza | 0:a0c5877bd360 | 233 | |
fahadmirza | 0:a0c5877bd360 | 234 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 235 | * @Brief : Send uplink packet using binary payload |
fahadmirza | 0:a0c5877bd360 | 236 | * @Param : Pointer of sSendDataBinary_t variable |
fahadmirza | 0:a0c5877bd360 | 237 | * @Retval : AT_OK or other eAtStatus_t statuses |
fahadmirza | 0:a0c5877bd360 | 238 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 239 | static eAtStatus_t Lora_SendDataBinary(sSendDataBinary_t *binaryData) |
fahadmirza | 0:a0c5877bd360 | 240 | { |
fahadmirza | 0:a0c5877bd360 | 241 | return Modem_AT_Cmd(AT_SET, AT_SENDB, binaryData); |
fahadmirza | 0:a0c5877bd360 | 242 | } |
fahadmirza | 0:a0c5877bd360 | 243 | |
fahadmirza | 0:a0c5877bd360 | 244 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 245 | * @Brief : LoRa Modem state machine |
fahadmirza | 0:a0c5877bd360 | 246 | * @Param : Void |
fahadmirza | 0:a0c5877bd360 | 247 | * @Return : None |
fahadmirza | 0:a0c5877bd360 | 248 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 249 | void Lora_fsm(void) |
fahadmirza | 0:a0c5877bd360 | 250 | { |
fahadmirza | 0:a0c5877bd360 | 251 | switch(DeviceState) |
fahadmirza | 0:a0c5877bd360 | 252 | { |
fahadmirza | 0:a0c5877bd360 | 253 | case DEVICE_INIT: |
fahadmirza | 0:a0c5877bd360 | 254 | { |
fahadmirza | 0:a0c5877bd360 | 255 | if(is_modem_working() != AT_OK) |
fahadmirza | 0:a0c5877bd360 | 256 | { |
fahadmirza | 1:168a6afffbff | 257 | DBG_PRINTF("AT failed. Resetting HW...\r\n"); |
fahadmirza | 0:a0c5877bd360 | 258 | // Modem isn't responding. Execute hard reset. |
fahadmirza | 0:a0c5877bd360 | 259 | Modem_HardReset(); |
fahadmirza | 0:a0c5877bd360 | 260 | // We stay in DEVICE_INIT state and try again. |
fahadmirza | 0:a0c5877bd360 | 261 | } |
fahadmirza | 0:a0c5877bd360 | 262 | else |
fahadmirza | 0:a0c5877bd360 | 263 | { |
fahadmirza | 0:a0c5877bd360 | 264 | DeviceState = DEVICE_CONFIG; |
fahadmirza | 0:a0c5877bd360 | 265 | } |
fahadmirza | 0:a0c5877bd360 | 266 | |
fahadmirza | 0:a0c5877bd360 | 267 | break; |
fahadmirza | 0:a0c5877bd360 | 268 | } |
fahadmirza | 0:a0c5877bd360 | 269 | case DEVICE_CONFIG: |
fahadmirza | 0:a0c5877bd360 | 270 | { |
fahadmirza | 0:a0c5877bd360 | 271 | eAtStatus_t loraModemRetCode = Lora_setDevEui(LoraConfigParam->devEui); |
fahadmirza | 0:a0c5877bd360 | 272 | loraModemRetCode |= Lora_setAppEui(LoraConfigParam->appEui); |
fahadmirza | 0:a0c5877bd360 | 273 | loraModemRetCode |= Lora_setAppKey(LoraConfigParam->appKey); |
fahadmirza | 0:a0c5877bd360 | 274 | loraModemRetCode |= Lora_setJoinMode(LoraConfigParam->joinMode); |
fahadmirza | 0:a0c5877bd360 | 275 | loraModemRetCode |= Lora_setClass(LoraConfigParam->class_); |
fahadmirza | 2:1ef859bc5cd2 | 276 | loraModemRetCode |= Lora_setAdr(ADR_OFF); |
fahadmirza | 0:a0c5877bd360 | 277 | // ToDo: Maybe add FW version checking |
fahadmirza | 0:a0c5877bd360 | 278 | |
fahadmirza | 0:a0c5877bd360 | 279 | if(loraModemRetCode == AT_OK) |
fahadmirza | 0:a0c5877bd360 | 280 | { |
fahadmirza | 0:a0c5877bd360 | 281 | setJoinRequestTimer(); |
fahadmirza | 0:a0c5877bd360 | 282 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 283 | } |
fahadmirza | 0:a0c5877bd360 | 284 | else if(loraModemRetCode == AT_TIMEOUT) |
fahadmirza | 0:a0c5877bd360 | 285 | { |
fahadmirza | 0:a0c5877bd360 | 286 | DeviceState = DEVICE_INIT; |
fahadmirza | 0:a0c5877bd360 | 287 | } |
fahadmirza | 0:a0c5877bd360 | 288 | else |
fahadmirza | 0:a0c5877bd360 | 289 | { |
fahadmirza | 0:a0c5877bd360 | 290 | DBG_PRINTF("Check your keys\r\n"); |
fahadmirza | 0:a0c5877bd360 | 291 | // We stay in DEVICE_CONFIG and try again |
fahadmirza | 0:a0c5877bd360 | 292 | } |
fahadmirza | 0:a0c5877bd360 | 293 | |
fahadmirza | 0:a0c5877bd360 | 294 | break; |
fahadmirza | 0:a0c5877bd360 | 295 | } |
fahadmirza | 0:a0c5877bd360 | 296 | case DEVICE_JOIN: |
fahadmirza | 0:a0c5877bd360 | 297 | { |
fahadmirza | 1:168a6afffbff | 298 | DBG_PRINTF("Joining...\r\n"); |
fahadmirza | 0:a0c5877bd360 | 299 | // Indicate a Join request using Nucleo LED |
fahadmirza | 0:a0c5877bd360 | 300 | BSP_LED_On(LED_GREEN); |
fahadmirza | 0:a0c5877bd360 | 301 | TimerStart(&NucleoLedTimer, 200); |
fahadmirza | 0:a0c5877bd360 | 302 | |
fahadmirza | 0:a0c5877bd360 | 303 | switch(Lora_Join()) |
fahadmirza | 0:a0c5877bd360 | 304 | { |
fahadmirza | 0:a0c5877bd360 | 305 | case AT_OK: |
fahadmirza | 0:a0c5877bd360 | 306 | { |
fahadmirza | 0:a0c5877bd360 | 307 | // Start the Join status request timer and go to sleep |
fahadmirza | 0:a0c5877bd360 | 308 | TimerStart(&JoinStatusDelayTimer, JOIN_STATUS_REQ_DELAY); |
fahadmirza | 0:a0c5877bd360 | 309 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 310 | break; |
fahadmirza | 0:a0c5877bd360 | 311 | } |
fahadmirza | 0:a0c5877bd360 | 312 | case AT_TIMEOUT: |
fahadmirza | 0:a0c5877bd360 | 313 | { |
fahadmirza | 0:a0c5877bd360 | 314 | // The modem isn't responding. Check if it is working |
fahadmirza | 0:a0c5877bd360 | 315 | DeviceState = DEVICE_INIT; |
fahadmirza | 0:a0c5877bd360 | 316 | break; |
fahadmirza | 0:a0c5877bd360 | 317 | } |
fahadmirza | 0:a0c5877bd360 | 318 | default: |
fahadmirza | 0:a0c5877bd360 | 319 | { |
fahadmirza | 0:a0c5877bd360 | 320 | DBG_PRINTF("Join cmd failed\n"); |
fahadmirza | 0:a0c5877bd360 | 321 | // We stay in DEVICE_JOIN state and redo Lora_Join() |
fahadmirza | 0:a0c5877bd360 | 322 | break; |
fahadmirza | 0:a0c5877bd360 | 323 | } |
fahadmirza | 0:a0c5877bd360 | 324 | } |
fahadmirza | 0:a0c5877bd360 | 325 | |
fahadmirza | 0:a0c5877bd360 | 326 | break; |
fahadmirza | 0:a0c5877bd360 | 327 | } |
fahadmirza | 0:a0c5877bd360 | 328 | case DEVICE_JOIN_STATUS_CHECK: |
fahadmirza | 0:a0c5877bd360 | 329 | { |
fahadmirza | 0:a0c5877bd360 | 330 | if(Lora_getJoinStatus() == JOINED) |
fahadmirza | 0:a0c5877bd360 | 331 | { |
fahadmirza | 0:a0c5877bd360 | 332 | DBG_PRINTF("Nwk Joined\n"); |
fahadmirza | 0:a0c5877bd360 | 333 | // Start timer for uplink transmission for sensor-data |
fahadmirza | 0:a0c5877bd360 | 334 | TimerStart( &SensorMeasureTimer, LoraDriverParam->SensorMeasureTime); |
fahadmirza | 0:a0c5877bd360 | 335 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 336 | } |
fahadmirza | 0:a0c5877bd360 | 337 | else |
fahadmirza | 0:a0c5877bd360 | 338 | { |
fahadmirza | 0:a0c5877bd360 | 339 | // Try joining again |
fahadmirza | 0:a0c5877bd360 | 340 | setJoinRequestTimer(); |
fahadmirza | 0:a0c5877bd360 | 341 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 342 | } |
fahadmirza | 0:a0c5877bd360 | 343 | break; |
fahadmirza | 0:a0c5877bd360 | 344 | } |
fahadmirza | 0:a0c5877bd360 | 345 | case SLEEP_DEVICE: |
fahadmirza | 0:a0c5877bd360 | 346 | { |
fahadmirza | 0:a0c5877bd360 | 347 | /* Wake up through RTC events or asynchronous event coming from HXC modem*/ |
fahadmirza | 0:a0c5877bd360 | 348 | if(Modem_IsNewDataReceived() == true) |
fahadmirza | 0:a0c5877bd360 | 349 | { |
fahadmirza | 0:a0c5877bd360 | 350 | DeviceState = DEVICE_DATA_RECEIVED; |
fahadmirza | 0:a0c5877bd360 | 351 | } |
fahadmirza | 0:a0c5877bd360 | 352 | break; |
fahadmirza | 0:a0c5877bd360 | 353 | } |
fahadmirza | 0:a0c5877bd360 | 354 | case DEVICE_SEND: |
fahadmirza | 0:a0c5877bd360 | 355 | { |
fahadmirza | 0:a0c5877bd360 | 356 | // Read sensor data and populate payload |
fahadmirza | 0:a0c5877bd360 | 357 | LoraDriverParam->SendDataHandler(SendDataBinary.Buffer, &SendDataBinary.DataSize, &SendDataBinary.Ack, &SendDataBinary.Port); |
fahadmirza | 0:a0c5877bd360 | 358 | |
fahadmirza | 0:a0c5877bd360 | 359 | // Initiate uplink transmission |
fahadmirza | 0:a0c5877bd360 | 360 | eAtStatus_t status = Lora_SendDataBinary(&SendDataBinary); |
fahadmirza | 0:a0c5877bd360 | 361 | if (status == AT_OK) |
fahadmirza | 0:a0c5877bd360 | 362 | { |
fahadmirza | 0:a0c5877bd360 | 363 | // Schedule the next packet |
fahadmirza | 0:a0c5877bd360 | 364 | TimerStart( &SensorMeasureTimer, LoraDriverParam->SensorMeasureTime ); |
fahadmirza | 0:a0c5877bd360 | 365 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 366 | } |
fahadmirza | 0:a0c5877bd360 | 367 | else if(status == AT_TIMEOUT) |
fahadmirza | 0:a0c5877bd360 | 368 | { |
fahadmirza | 0:a0c5877bd360 | 369 | // Device isn't responding. Go to init. |
fahadmirza | 0:a0c5877bd360 | 370 | DeviceState = DEVICE_INIT; |
fahadmirza | 0:a0c5877bd360 | 371 | } |
fahadmirza | 0:a0c5877bd360 | 372 | |
fahadmirza | 0:a0c5877bd360 | 373 | break; |
fahadmirza | 0:a0c5877bd360 | 374 | } |
fahadmirza | 0:a0c5877bd360 | 375 | case DEVICE_DATA_RECEIVED: |
fahadmirza | 0:a0c5877bd360 | 376 | { |
fahadmirza | 0:a0c5877bd360 | 377 | uint8_t rBuffer[64]; |
fahadmirza | 0:a0c5877bd360 | 378 | sRecvDataBinary_t rxPacket = {.Buffer = rBuffer}; |
fahadmirza | 0:a0c5877bd360 | 379 | |
fahadmirza | 0:a0c5877bd360 | 380 | Lora_ReadData(&rxPacket); |
fahadmirza | 0:a0c5877bd360 | 381 | |
fahadmirza | 0:a0c5877bd360 | 382 | // Execute users ReceivedPacketHandler function |
fahadmirza | 0:a0c5877bd360 | 383 | LoraDriverParam->ReceiveDataHandler(rxPacket.Buffer, rxPacket.DataSize, rxPacket.Ack, rxPacket.Port); |
fahadmirza | 0:a0c5877bd360 | 384 | |
fahadmirza | 0:a0c5877bd360 | 385 | DeviceState = SLEEP_DEVICE; |
fahadmirza | 0:a0c5877bd360 | 386 | break; |
fahadmirza | 0:a0c5877bd360 | 387 | } |
fahadmirza | 0:a0c5877bd360 | 388 | default: |
fahadmirza | 0:a0c5877bd360 | 389 | { |
fahadmirza | 0:a0c5877bd360 | 390 | DeviceState = DEVICE_INIT; |
fahadmirza | 0:a0c5877bd360 | 391 | break; |
fahadmirza | 0:a0c5877bd360 | 392 | } |
fahadmirza | 0:a0c5877bd360 | 393 | } |
fahadmirza | 0:a0c5877bd360 | 394 | } |
fahadmirza | 0:a0c5877bd360 | 395 | |
fahadmirza | 0:a0c5877bd360 | 396 | static void setJoinRequestTimer(void) |
fahadmirza | 0:a0c5877bd360 | 397 | { |
fahadmirza | 0:a0c5877bd360 | 398 | // Use a random delay to avoid synchronized join request from all LoRa node after power up |
fahadmirza | 0:a0c5877bd360 | 399 | uint32_t joinDelay = (HAL_RNG_GetRandomNumber(&RngHandle) % JOIN_SEND_DELAY_MAX) + 1; |
fahadmirza | 0:a0c5877bd360 | 400 | TimerStart( &JoinRequestTimer, joinDelay); |
fahadmirza | 0:a0c5877bd360 | 401 | } |
fahadmirza | 0:a0c5877bd360 | 402 | |
fahadmirza | 0:a0c5877bd360 | 403 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 404 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
fahadmirza | 0:a0c5877bd360 | 405 | * @Param : none |
fahadmirza | 0:a0c5877bd360 | 406 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 407 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 408 | static void Lora_OnJoinRequestTimerEvt(void) |
fahadmirza | 0:a0c5877bd360 | 409 | { |
fahadmirza | 0:a0c5877bd360 | 410 | TimerStop(&JoinRequestTimer); |
fahadmirza | 0:a0c5877bd360 | 411 | DeviceState = DEVICE_JOIN; |
fahadmirza | 0:a0c5877bd360 | 412 | } |
fahadmirza | 0:a0c5877bd360 | 413 | |
fahadmirza | 0:a0c5877bd360 | 414 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 415 | * @Brief : Function executed on NucleoLedTimer Timeout event |
fahadmirza | 0:a0c5877bd360 | 416 | * @Param : none |
fahadmirza | 0:a0c5877bd360 | 417 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 418 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 419 | static void Lora_OnLedTimerEvent(void) |
fahadmirza | 0:a0c5877bd360 | 420 | { |
fahadmirza | 0:a0c5877bd360 | 421 | TimerStop(&NucleoLedTimer); |
fahadmirza | 0:a0c5877bd360 | 422 | BSP_LED_Off(LED_GREEN); |
fahadmirza | 0:a0c5877bd360 | 423 | } |
fahadmirza | 0:a0c5877bd360 | 424 | |
fahadmirza | 0:a0c5877bd360 | 425 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 426 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
fahadmirza | 0:a0c5877bd360 | 427 | * @Param : none |
fahadmirza | 0:a0c5877bd360 | 428 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 429 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 430 | static void Lora_OnJoinStatusDelayTimerEvt(void) |
fahadmirza | 0:a0c5877bd360 | 431 | { |
fahadmirza | 0:a0c5877bd360 | 432 | TimerStop( &JoinStatusDelayTimer ); |
fahadmirza | 0:a0c5877bd360 | 433 | DeviceState = DEVICE_JOIN_STATUS_CHECK; |
fahadmirza | 0:a0c5877bd360 | 434 | } |
fahadmirza | 0:a0c5877bd360 | 435 | |
fahadmirza | 0:a0c5877bd360 | 436 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 437 | * @Brief : Function executed on SensorMeasureTimer Timeout event |
fahadmirza | 0:a0c5877bd360 | 438 | * @Param : none |
fahadmirza | 0:a0c5877bd360 | 439 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 440 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 441 | static void Lora_OnSensorMeasureTimerEvt(void) |
fahadmirza | 0:a0c5877bd360 | 442 | { |
fahadmirza | 0:a0c5877bd360 | 443 | TimerStop( &SensorMeasureTimer ); |
fahadmirza | 0:a0c5877bd360 | 444 | DeviceState = DEVICE_SEND; |
fahadmirza | 0:a0c5877bd360 | 445 | } |
fahadmirza | 0:a0c5877bd360 | 446 | |
fahadmirza | 0:a0c5877bd360 | 447 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 448 | * @Brief : Read the received downlink packet |
fahadmirza | 0:a0c5877bd360 | 449 | * @Param : Pointer to sRecvDataBinary_t variable |
fahadmirza | 0:a0c5877bd360 | 450 | * @Return : none |
fahadmirza | 0:a0c5877bd360 | 451 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 452 | static void Lora_ReadData(sRecvDataBinary_t *receivedData) |
fahadmirza | 0:a0c5877bd360 | 453 | { |
fahadmirza | 0:a0c5877bd360 | 454 | char receiveString[64]; |
fahadmirza | 0:a0c5877bd360 | 455 | |
fahadmirza | 0:a0c5877bd360 | 456 | Modem_AT_Cmd(AT_GET, AT_RECVB, receiveString); |
fahadmirza | 0:a0c5877bd360 | 457 | |
fahadmirza | 0:a0c5877bd360 | 458 | tiny_sscanf(receiveString, "%hhu,%hhu", &(receivedData->Ack), &(receivedData->Port)); |
fahadmirza | 0:a0c5877bd360 | 459 | |
fahadmirza | 0:a0c5877bd360 | 460 | // Find the position after the second comma |
fahadmirza | 0:a0c5877bd360 | 461 | char *hexString = strchr((strchr(receiveString,',') + 1),',') + 1; |
fahadmirza | 0:a0c5877bd360 | 462 | |
fahadmirza | 0:a0c5877bd360 | 463 | receivedData->DataSize = stringHexToByteArray(hexString, receivedData->Buffer, 64); |
fahadmirza | 0:a0c5877bd360 | 464 | } |
fahadmirza | 0:a0c5877bd360 | 465 | |
fahadmirza | 0:a0c5877bd360 | 466 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 467 | * @brief RNG MSP Initialization |
fahadmirza | 0:a0c5877bd360 | 468 | * This function configures the hardware resources used in this example: |
fahadmirza | 0:a0c5877bd360 | 469 | * - Peripheral's clock enable |
fahadmirza | 0:a0c5877bd360 | 470 | * @param hrng: RNG handle pointer |
fahadmirza | 0:a0c5877bd360 | 471 | * @retval None |
fahadmirza | 0:a0c5877bd360 | 472 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 473 | void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng) |
fahadmirza | 0:a0c5877bd360 | 474 | { |
fahadmirza | 0:a0c5877bd360 | 475 | /* RNG Peripheral clock enable */ |
fahadmirza | 0:a0c5877bd360 | 476 | __HAL_RCC_RNG_CLK_ENABLE(); |
fahadmirza | 0:a0c5877bd360 | 477 | } |
fahadmirza | 0:a0c5877bd360 | 478 | |
fahadmirza | 0:a0c5877bd360 | 479 |