A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Sep 13 15:29:47 2018 +0000
Revision:
19:0c90e38fc9c5
Parent:
17:e682a18c3914
Child:
21:f1d561ed31a1
Removed STRING uplink;

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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 17:e682a18c3914 59 // Re Authenticate every REJOIN_TIME
fahadmirza 17:e682a18c3914 60 #define REJOIN_TIME (1 * 60 * 60 * 1000) // 1 hour
fahadmirza 17:e682a18c3914 61
fahadmirza 0:a0c5877bd360 62 /* Private global variables --------------------------------------------------*/
fahadmirza 0:a0c5877bd360 63 static sLoraConfig_t *LoraConfigParam;
fahadmirza 0:a0c5877bd360 64 static sLoraDriverParam_t *LoraDriverParam;
fahadmirza 0:a0c5877bd360 65 static volatile eDeviceState_t DeviceState = DEVICE_INIT;
fahadmirza 0:a0c5877bd360 66
fahadmirza 0:a0c5877bd360 67 static TimerEvent_t JoinRequestTimer;
fahadmirza 17:e682a18c3914 68 static TimerEvent_t RejoinTimer;
fahadmirza 0:a0c5877bd360 69 static TimerEvent_t SensorMeasureTimer;
fahadmirza 0:a0c5877bd360 70 static TimerEvent_t NucleoLedTimer;
fahadmirza 0:a0c5877bd360 71 static TimerEvent_t JoinStatusDelayTimer;
fahadmirza 0:a0c5877bd360 72
fahadmirza 0:a0c5877bd360 73 // Object definition for data to be sent to loRa application server
fahadmirza 0:a0c5877bd360 74 static uint8_t DataBinaryBuff[64];
fahadmirza 0:a0c5877bd360 75 static sSendDataBinary_t SendDataBinary = {DataBinaryBuff, 0 , 0, 0};
fahadmirza 0:a0c5877bd360 76
fahadmirza 0:a0c5877bd360 77 // RNG handler declaration
fahadmirza 0:a0c5877bd360 78 RNG_HandleTypeDef RngHandle = {.Instance = RNG};
fahadmirza 0:a0c5877bd360 79
fahadmirza 0:a0c5877bd360 80 /* Private functions ---------------------------------------------------------*/
fahadmirza 17:e682a18c3914 81 static void OnRejoinTimerEvent(void);
fahadmirza 10:19da323c2bc0 82 static void OnLedTimerEvent(void);
fahadmirza 10:19da323c2bc0 83 static void OnJoinRequestTimerEvt(void);
fahadmirza 10:19da323c2bc0 84 static void OnJoinStatusDelayTimerEvt(void);
fahadmirza 10:19da323c2bc0 85 static void OnSensorMeasureTimerEvt(void);
fahadmirza 0:a0c5877bd360 86 static void setJoinRequestTimer(void);
fahadmirza 0:a0c5877bd360 87
fahadmirza 0:a0c5877bd360 88
fahadmirza 0:a0c5877bd360 89 /* Function definitions ------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 90 /******************************************************************************
fahadmirza 0:a0c5877bd360 91 * @Brief : Initialize LoRa Modem
fahadmirza 0:a0c5877bd360 92 * @Param : sLoraConfig_t
fahadmirza 16:2179ec24dff8 93 * @Return : None
fahadmirza 0:a0c5877bd360 94 ******************************************************************************/
fahadmirza 0:a0c5877bd360 95 void Lora_init(sLoraConfig_t *loraConfig, sLoraDriverParam_t *loraDriverParam)
fahadmirza 0:a0c5877bd360 96 {
fahadmirza 0:a0c5877bd360 97
fahadmirza 0:a0c5877bd360 98 LoraConfigParam = loraConfig;
fahadmirza 0:a0c5877bd360 99 LoraDriverParam = loraDriverParam;
fahadmirza 0:a0c5877bd360 100
fahadmirza 0:a0c5877bd360 101 if(Modem_Init() != AT_OK)
fahadmirza 0:a0c5877bd360 102 {
fahadmirza 0:a0c5877bd360 103 DBG_PRINTF("Modem_Init failed\r\n");
fahadmirza 0:a0c5877bd360 104 }
fahadmirza 0:a0c5877bd360 105
fahadmirza 0:a0c5877bd360 106 // Initialize RNG for join request send randomization
fahadmirza 0:a0c5877bd360 107 HAL_RNG_Init(&RngHandle);
fahadmirza 0:a0c5877bd360 108
fahadmirza 0:a0c5877bd360 109
fahadmirza 0:a0c5877bd360 110 // Timer for join request send
fahadmirza 10:19da323c2bc0 111 TimerInit(&JoinRequestTimer, OnJoinRequestTimerEvt);
fahadmirza 0:a0c5877bd360 112 // Timer for join status check
fahadmirza 10:19da323c2bc0 113 TimerInit(&JoinStatusDelayTimer, OnJoinStatusDelayTimerEvt);
fahadmirza 0:a0c5877bd360 114 // Timer for sensor occurrence measure
fahadmirza 10:19da323c2bc0 115 TimerInit(&SensorMeasureTimer, OnSensorMeasureTimerEvt);
fahadmirza 0:a0c5877bd360 116 // Timer for Nucleo LED
fahadmirza 10:19da323c2bc0 117 TimerInit(&NucleoLedTimer, OnLedTimerEvent);
fahadmirza 17:e682a18c3914 118 // Timer for Re Authenticate / Join
fahadmirza 17:e682a18c3914 119 TimerInit(&RejoinTimer, OnRejoinTimerEvent);
fahadmirza 0:a0c5877bd360 120 }
fahadmirza 0:a0c5877bd360 121
fahadmirza 0:a0c5877bd360 122 /******************************************************************************
fahadmirza 0:a0c5877bd360 123 * @Brief : Check if the modem responds
fahadmirza 0:a0c5877bd360 124 * @Param : void
fahadmirza 0:a0c5877bd360 125 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 126 ******************************************************************************/
fahadmirza 0:a0c5877bd360 127 static eAtStatus_t is_modem_working(void)
fahadmirza 0:a0c5877bd360 128 {
fahadmirza 0:a0c5877bd360 129 return Modem_AT_Cmd(AT_CTRL, AT, NULL);
fahadmirza 0:a0c5877bd360 130 }
fahadmirza 0:a0c5877bd360 131
fahadmirza 0:a0c5877bd360 132 /******************************************************************************
fahadmirza 0:a0c5877bd360 133 * @Brief : Set Device EUI
fahadmirza 0:a0c5877bd360 134 * @Param : Pointer to Device EUI
fahadmirza 0:a0c5877bd360 135 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 136 ******************************************************************************/
fahadmirza 10:19da323c2bc0 137 static eAtStatus_t Lora_setDevEui(char *devEui)
fahadmirza 0:a0c5877bd360 138 {
fahadmirza 0:a0c5877bd360 139 return Modem_AT_Cmd(AT_SET, AT_DEVEUI, devEui);
fahadmirza 0:a0c5877bd360 140 }
fahadmirza 0:a0c5877bd360 141
fahadmirza 0:a0c5877bd360 142 /******************************************************************************
fahadmirza 2:1ef859bc5cd2 143 * @Brief : Turn on or off ADR
fahadmirza 2:1ef859bc5cd2 144 * @Param : ADR_ON or ADR_OFF
fahadmirza 2:1ef859bc5cd2 145 * @Return : AT_OK or other eAtStatus_t
fahadmirza 2:1ef859bc5cd2 146 ******************************************************************************/
fahadmirza 10:19da323c2bc0 147 static eAtStatus_t Lora_setAdr(eAdrStatus_t adrStatus)
fahadmirza 2:1ef859bc5cd2 148 {
fahadmirza 2:1ef859bc5cd2 149 //char adr = (adrStatus == ADR_ON ? '1' : '0');
fahadmirza 2:1ef859bc5cd2 150 return Modem_AT_Cmd(AT_SET, AT_ADR, (uint8_t *)(&adrStatus));
fahadmirza 2:1ef859bc5cd2 151 }
fahadmirza 2:1ef859bc5cd2 152
fahadmirza 2:1ef859bc5cd2 153 /******************************************************************************
fahadmirza 0:a0c5877bd360 154 * @Brief : Set Application EUI
fahadmirza 0:a0c5877bd360 155 * @Param : Pointer to Application EUI
fahadmirza 0:a0c5877bd360 156 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 157 ******************************************************************************/
fahadmirza 10:19da323c2bc0 158 static eAtStatus_t Lora_setAppEui(char *appEui)
fahadmirza 0:a0c5877bd360 159 {
fahadmirza 0:a0c5877bd360 160 return Modem_AT_Cmd(AT_SET, AT_APPEUI, appEui);
fahadmirza 0:a0c5877bd360 161 }
fahadmirza 0:a0c5877bd360 162
fahadmirza 0:a0c5877bd360 163 /******************************************************************************
fahadmirza 0:a0c5877bd360 164 * @Brief : Set Application Key
fahadmirza 0:a0c5877bd360 165 * @Param : Pointer to Application Key
fahadmirza 0:a0c5877bd360 166 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 167 ******************************************************************************/
fahadmirza 10:19da323c2bc0 168 static eAtStatus_t Lora_setAppKey(char *appKey)
fahadmirza 0:a0c5877bd360 169 {
fahadmirza 0:a0c5877bd360 170 return Modem_AT_Cmd(AT_SET, AT_APPKEY, appKey);
fahadmirza 0:a0c5877bd360 171 }
fahadmirza 0:a0c5877bd360 172
fahadmirza 0:a0c5877bd360 173 /******************************************************************************
fahadmirza 0:a0c5877bd360 174 * @Brief : Set join mode
fahadmirza 0:a0c5877bd360 175 * @Param : OTAA or ABP
fahadmirza 0:a0c5877bd360 176 * @Retval : AT_OK if successful, otherwise other eAtStatus_t
fahadmirza 0:a0c5877bd360 177 ******************************************************************************/
fahadmirza 10:19da323c2bc0 178 static eAtStatus_t Lora_setJoinMode(eJoinMode_t joinMode)
fahadmirza 0:a0c5877bd360 179 {
fahadmirza 0:a0c5877bd360 180 if(joinMode == OTAA)
fahadmirza 0:a0c5877bd360 181 {
fahadmirza 0:a0c5877bd360 182 return Modem_AT_Cmd(AT_SET, AT_NJM, "OTAA");
fahadmirza 0:a0c5877bd360 183 }
fahadmirza 0:a0c5877bd360 184
fahadmirza 0:a0c5877bd360 185 return Modem_AT_Cmd(AT_SET, AT_NJM, "ABP");
fahadmirza 0:a0c5877bd360 186 }
fahadmirza 0:a0c5877bd360 187
fahadmirza 0:a0c5877bd360 188 /******************************************************************************
fahadmirza 0:a0c5877bd360 189 * @Brief : Set Class
fahadmirza 0:a0c5877bd360 190 * @Param : CLASS_A or CLASS_C
fahadmirza 0:a0c5877bd360 191 * @Retval : AT_OK if successful, otherwise other eAtStatus_t
fahadmirza 0:a0c5877bd360 192 ******************************************************************************/
fahadmirza 10:19da323c2bc0 193 static eAtStatus_t Lora_setClass(char class)
fahadmirza 0:a0c5877bd360 194 {
fahadmirza 0:a0c5877bd360 195 return Modem_AT_Cmd(AT_SET, AT_CLASS, &class);
fahadmirza 0:a0c5877bd360 196 }
fahadmirza 0:a0c5877bd360 197
fahadmirza 0:a0c5877bd360 198 /******************************************************************************
fahadmirza 0:a0c5877bd360 199 * @Brief : Join network and initiate join sleep transition timer
fahadmirza 0:a0c5877bd360 200 * @Param : None
fahadmirza 0:a0c5877bd360 201 * @Retval : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 202 ******************************************************************************/
fahadmirza 0:a0c5877bd360 203 static eAtStatus_t Lora_Join(void)
fahadmirza 0:a0c5877bd360 204 {
fahadmirza 0:a0c5877bd360 205 return Modem_AT_Cmd(AT_CTRL, AT_JOIN, NULL);
fahadmirza 0:a0c5877bd360 206 }
fahadmirza 0:a0c5877bd360 207
fahadmirza 0:a0c5877bd360 208 /******************************************************************************
fahadmirza 0:a0c5877bd360 209 * @Brief : Check JOIN status
fahadmirza 0:a0c5877bd360 210 * @Param : None
fahadmirza 0:a0c5877bd360 211 * @Retval : JOINED or NOT_JOINED
fahadmirza 0:a0c5877bd360 212 ******************************************************************************/
fahadmirza 0:a0c5877bd360 213 static eJoinStatus_t Lora_getJoinStatus(void)
fahadmirza 0:a0c5877bd360 214 {
fahadmirza 0:a0c5877bd360 215 char joinStatus[12]; // "NOT JOINED" is 10 characters + 1 Null char
fahadmirza 0:a0c5877bd360 216
fahadmirza 0:a0c5877bd360 217 Modem_AT_Cmd(AT_GET, AT_NJS, joinStatus);
fahadmirza 0:a0c5877bd360 218
fahadmirza 0:a0c5877bd360 219 if(strncmp("JOINED", joinStatus, 6) == 0)
fahadmirza 0:a0c5877bd360 220 {
fahadmirza 0:a0c5877bd360 221 return JOINED;
fahadmirza 0:a0c5877bd360 222 }
fahadmirza 0:a0c5877bd360 223
fahadmirza 0:a0c5877bd360 224 return NOT_JOINED;
fahadmirza 0:a0c5877bd360 225 }
fahadmirza 0:a0c5877bd360 226
fahadmirza 0:a0c5877bd360 227 /******************************************************************************
fahadmirza 0:a0c5877bd360 228 * @Brief : Send uplink packet using binary payload
fahadmirza 0:a0c5877bd360 229 * @Param : Pointer of sSendDataBinary_t variable
fahadmirza 0:a0c5877bd360 230 * @Retval : AT_OK or other eAtStatus_t statuses
fahadmirza 0:a0c5877bd360 231 ******************************************************************************/
fahadmirza 0:a0c5877bd360 232 static eAtStatus_t Lora_SendDataBinary(sSendDataBinary_t *binaryData)
fahadmirza 0:a0c5877bd360 233 {
fahadmirza 0:a0c5877bd360 234 return Modem_AT_Cmd(AT_SET, AT_SENDB, binaryData);
fahadmirza 0:a0c5877bd360 235 }
fahadmirza 0:a0c5877bd360 236
fahadmirza 0:a0c5877bd360 237 /******************************************************************************
fahadmirza 10:19da323c2bc0 238 * @Brief : Read the received downlink packet
fahadmirza 10:19da323c2bc0 239 * @Param : Pointer to sRecvDataBinary_t variable
fahadmirza 16:2179ec24dff8 240 * @Return : None
fahadmirza 10:19da323c2bc0 241 ******************************************************************************/
fahadmirza 10:19da323c2bc0 242 static void Lora_ReadData(sRecvDataBinary_t *receivedData)
fahadmirza 10:19da323c2bc0 243 {
fahadmirza 10:19da323c2bc0 244 char receiveString[64];
fahadmirza 10:19da323c2bc0 245
fahadmirza 10:19da323c2bc0 246 Modem_AT_Cmd(AT_GET, AT_RECVB, receiveString);
fahadmirza 10:19da323c2bc0 247
fahadmirza 10:19da323c2bc0 248 tiny_sscanf(receiveString, "%hhu,%hhu", &(receivedData->Ack), &(receivedData->Port));
fahadmirza 10:19da323c2bc0 249
fahadmirza 10:19da323c2bc0 250 // Find the position after the second comma
fahadmirza 10:19da323c2bc0 251 char *hexString = strchr((strchr(receiveString,',') + 1),',') + 1;
fahadmirza 10:19da323c2bc0 252
fahadmirza 10:19da323c2bc0 253 receivedData->DataSize = stringHexToByteArray(hexString, receivedData->Buffer, 64);
fahadmirza 10:19da323c2bc0 254 }
fahadmirza 10:19da323c2bc0 255
fahadmirza 10:19da323c2bc0 256 /******************************************************************************
fahadmirza 10:19da323c2bc0 257 * @Brief : Change Lora_fsm() DeviceState
fahadmirza 10:19da323c2bc0 258 * @Param : Any of eDeviceState_t type
fahadmirza 10:19da323c2bc0 259 * @Return : None
fahadmirza 10:19da323c2bc0 260 ******************************************************************************/
fahadmirza 10:19da323c2bc0 261 void Lora_ChangeDeviceState(eDeviceState_t newDeviceState)
fahadmirza 10:19da323c2bc0 262 {
fahadmirza 10:19da323c2bc0 263 //ToDo: Check for valid eDeviceState_t states
fahadmirza 10:19da323c2bc0 264 DeviceState = newDeviceState;
fahadmirza 10:19da323c2bc0 265 }
fahadmirza 10:19da323c2bc0 266
fahadmirza 10:19da323c2bc0 267 /******************************************************************************
fahadmirza 0:a0c5877bd360 268 * @Brief : LoRa Modem state machine
fahadmirza 0:a0c5877bd360 269 * @Param : Void
fahadmirza 0:a0c5877bd360 270 * @Return : None
fahadmirza 0:a0c5877bd360 271 ******************************************************************************/
fahadmirza 0:a0c5877bd360 272 void Lora_fsm(void)
fahadmirza 0:a0c5877bd360 273 {
fahadmirza 0:a0c5877bd360 274 switch(DeviceState)
fahadmirza 0:a0c5877bd360 275 {
fahadmirza 0:a0c5877bd360 276 case DEVICE_INIT:
fahadmirza 0:a0c5877bd360 277 {
fahadmirza 0:a0c5877bd360 278 if(is_modem_working() != AT_OK)
fahadmirza 0:a0c5877bd360 279 {
fahadmirza 1:168a6afffbff 280 DBG_PRINTF("AT failed. Resetting HW...\r\n");
fahadmirza 0:a0c5877bd360 281 // Modem isn't responding. Execute hard reset.
fahadmirza 0:a0c5877bd360 282 Modem_HardReset();
fahadmirza 0:a0c5877bd360 283 // We stay in DEVICE_INIT state and try again.
fahadmirza 0:a0c5877bd360 284 }
fahadmirza 0:a0c5877bd360 285 else
fahadmirza 0:a0c5877bd360 286 {
fahadmirza 0:a0c5877bd360 287 DeviceState = DEVICE_CONFIG;
fahadmirza 0:a0c5877bd360 288 }
fahadmirza 0:a0c5877bd360 289
fahadmirza 0:a0c5877bd360 290 break;
fahadmirza 0:a0c5877bd360 291 }
fahadmirza 0:a0c5877bd360 292 case DEVICE_CONFIG:
fahadmirza 0:a0c5877bd360 293 {
fahadmirza 0:a0c5877bd360 294 eAtStatus_t loraModemRetCode = Lora_setDevEui(LoraConfigParam->devEui);
fahadmirza 0:a0c5877bd360 295 loraModemRetCode |= Lora_setAppEui(LoraConfigParam->appEui);
fahadmirza 0:a0c5877bd360 296 loraModemRetCode |= Lora_setAppKey(LoraConfigParam->appKey);
fahadmirza 0:a0c5877bd360 297 loraModemRetCode |= Lora_setJoinMode(LoraConfigParam->joinMode);
fahadmirza 0:a0c5877bd360 298 loraModemRetCode |= Lora_setClass(LoraConfigParam->class_);
fahadmirza 10:19da323c2bc0 299 loraModemRetCode |= Lora_setAdr(LoraConfigParam->adrStatus);
fahadmirza 0:a0c5877bd360 300 // ToDo: Maybe add FW version checking
fahadmirza 0:a0c5877bd360 301
fahadmirza 0:a0c5877bd360 302 if(loraModemRetCode == AT_OK)
fahadmirza 0:a0c5877bd360 303 {
fahadmirza 0:a0c5877bd360 304 setJoinRequestTimer();
fahadmirza 0:a0c5877bd360 305 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 306 }
fahadmirza 0:a0c5877bd360 307 else if(loraModemRetCode == AT_TIMEOUT)
fahadmirza 0:a0c5877bd360 308 {
fahadmirza 0:a0c5877bd360 309 DeviceState = DEVICE_INIT;
fahadmirza 0:a0c5877bd360 310 }
fahadmirza 0:a0c5877bd360 311 else
fahadmirza 0:a0c5877bd360 312 {
fahadmirza 0:a0c5877bd360 313 DBG_PRINTF("Check your keys\r\n");
fahadmirza 0:a0c5877bd360 314 // We stay in DEVICE_CONFIG and try again
fahadmirza 0:a0c5877bd360 315 }
fahadmirza 0:a0c5877bd360 316
fahadmirza 0:a0c5877bd360 317 break;
fahadmirza 0:a0c5877bd360 318 }
fahadmirza 0:a0c5877bd360 319 case DEVICE_JOIN:
fahadmirza 0:a0c5877bd360 320 {
fahadmirza 1:168a6afffbff 321 DBG_PRINTF("Joining...\r\n");
fahadmirza 0:a0c5877bd360 322 // Indicate a Join request using Nucleo LED
fahadmirza 0:a0c5877bd360 323 BSP_LED_On(LED_GREEN);
fahadmirza 0:a0c5877bd360 324 TimerStart(&NucleoLedTimer, 200);
fahadmirza 0:a0c5877bd360 325
fahadmirza 0:a0c5877bd360 326 switch(Lora_Join())
fahadmirza 0:a0c5877bd360 327 {
fahadmirza 0:a0c5877bd360 328 case AT_OK:
fahadmirza 0:a0c5877bd360 329 {
fahadmirza 0:a0c5877bd360 330 // Start the Join status request timer and go to sleep
fahadmirza 0:a0c5877bd360 331 TimerStart(&JoinStatusDelayTimer, JOIN_STATUS_REQ_DELAY);
fahadmirza 0:a0c5877bd360 332 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 333 break;
fahadmirza 0:a0c5877bd360 334 }
fahadmirza 0:a0c5877bd360 335 case AT_TIMEOUT:
fahadmirza 0:a0c5877bd360 336 {
fahadmirza 0:a0c5877bd360 337 // The modem isn't responding. Check if it is working
fahadmirza 0:a0c5877bd360 338 DeviceState = DEVICE_INIT;
fahadmirza 0:a0c5877bd360 339 break;
fahadmirza 0:a0c5877bd360 340 }
fahadmirza 0:a0c5877bd360 341 default:
fahadmirza 0:a0c5877bd360 342 {
fahadmirza 0:a0c5877bd360 343 DBG_PRINTF("Join cmd failed\n");
fahadmirza 0:a0c5877bd360 344 // We stay in DEVICE_JOIN state and redo Lora_Join()
fahadmirza 0:a0c5877bd360 345 break;
fahadmirza 0:a0c5877bd360 346 }
fahadmirza 0:a0c5877bd360 347 }
fahadmirza 0:a0c5877bd360 348
fahadmirza 0:a0c5877bd360 349 break;
fahadmirza 0:a0c5877bd360 350 }
fahadmirza 0:a0c5877bd360 351 case DEVICE_JOIN_STATUS_CHECK:
fahadmirza 0:a0c5877bd360 352 {
fahadmirza 0:a0c5877bd360 353 if(Lora_getJoinStatus() == JOINED)
fahadmirza 0:a0c5877bd360 354 {
fahadmirza 12:f1fd61aa85e0 355 // Inidicate Join status using LED
fahadmirza 12:f1fd61aa85e0 356 GPIO_InitTypeDef GPIO_InitStruct;
fahadmirza 12:f1fd61aa85e0 357
fahadmirza 12:f1fd61aa85e0 358 GPIO_InitStruct.Pin = GPIO_PIN_7;
fahadmirza 12:f1fd61aa85e0 359 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 12:f1fd61aa85e0 360 GPIO_InitStruct.Pull = GPIO_NOPULL;
fahadmirza 12:f1fd61aa85e0 361 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
fahadmirza 12:f1fd61aa85e0 362
fahadmirza 12:f1fd61aa85e0 363 GPIO_InitStruct.Pin = GPIO_PIN_6;
fahadmirza 12:f1fd61aa85e0 364 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 12:f1fd61aa85e0 365 GPIO_InitStruct.Pull = GPIO_NOPULL;
fahadmirza 12:f1fd61aa85e0 366 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
fahadmirza 12:f1fd61aa85e0 367
fahadmirza 12:f1fd61aa85e0 368 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
fahadmirza 12:f1fd61aa85e0 369 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
fahadmirza 12:f1fd61aa85e0 370
fahadmirza 0:a0c5877bd360 371 DBG_PRINTF("Nwk Joined\n");
fahadmirza 17:e682a18c3914 372
fahadmirza 17:e682a18c3914 373 // Start timer for Re-Authentication
fahadmirza 17:e682a18c3914 374 TimerStart(&RejoinTimer, REJOIN_TIME);
fahadmirza 17:e682a18c3914 375
fahadmirza 0:a0c5877bd360 376 // Start timer for uplink transmission for sensor-data
fahadmirza 17:e682a18c3914 377 TimerStart(&SensorMeasureTimer, LoraDriverParam->SensorMeasureTime);
fahadmirza 0:a0c5877bd360 378 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 379 }
fahadmirza 0:a0c5877bd360 380 else
fahadmirza 0:a0c5877bd360 381 {
fahadmirza 0:a0c5877bd360 382 // Try joining again
fahadmirza 0:a0c5877bd360 383 setJoinRequestTimer();
fahadmirza 0:a0c5877bd360 384 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 385 }
fahadmirza 0:a0c5877bd360 386 break;
fahadmirza 0:a0c5877bd360 387 }
fahadmirza 0:a0c5877bd360 388 case SLEEP_DEVICE:
fahadmirza 0:a0c5877bd360 389 {
fahadmirza 0:a0c5877bd360 390 /* Wake up through RTC events or asynchronous event coming from HXC modem*/
fahadmirza 0:a0c5877bd360 391 if(Modem_IsNewDataReceived() == true)
fahadmirza 0:a0c5877bd360 392 {
fahadmirza 0:a0c5877bd360 393 DeviceState = DEVICE_DATA_RECEIVED;
fahadmirza 0:a0c5877bd360 394 }
fahadmirza 0:a0c5877bd360 395 break;
fahadmirza 0:a0c5877bd360 396 }
fahadmirza 0:a0c5877bd360 397 case DEVICE_SEND:
fahadmirza 0:a0c5877bd360 398 {
fahadmirza 0:a0c5877bd360 399 // Read sensor data and populate payload
fahadmirza 0:a0c5877bd360 400 LoraDriverParam->SendDataHandler(SendDataBinary.Buffer, &SendDataBinary.DataSize, &SendDataBinary.Ack, &SendDataBinary.Port);
fahadmirza 0:a0c5877bd360 401
fahadmirza 0:a0c5877bd360 402 // Initiate uplink transmission
fahadmirza 0:a0c5877bd360 403 eAtStatus_t status = Lora_SendDataBinary(&SendDataBinary);
fahadmirza 0:a0c5877bd360 404 if (status == AT_OK)
fahadmirza 0:a0c5877bd360 405 {
fahadmirza 0:a0c5877bd360 406 // Schedule the next packet
fahadmirza 0:a0c5877bd360 407 TimerStart( &SensorMeasureTimer, LoraDriverParam->SensorMeasureTime );
fahadmirza 0:a0c5877bd360 408 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 409 }
fahadmirza 0:a0c5877bd360 410 else if(status == AT_TIMEOUT)
fahadmirza 0:a0c5877bd360 411 {
fahadmirza 0:a0c5877bd360 412 // Device isn't responding. Go to init.
fahadmirza 0:a0c5877bd360 413 DeviceState = DEVICE_INIT;
fahadmirza 0:a0c5877bd360 414 }
fahadmirza 0:a0c5877bd360 415
fahadmirza 0:a0c5877bd360 416 break;
fahadmirza 0:a0c5877bd360 417 }
fahadmirza 0:a0c5877bd360 418 case DEVICE_DATA_RECEIVED:
fahadmirza 0:a0c5877bd360 419 {
fahadmirza 0:a0c5877bd360 420 uint8_t rBuffer[64];
fahadmirza 0:a0c5877bd360 421 sRecvDataBinary_t rxPacket = {.Buffer = rBuffer};
fahadmirza 0:a0c5877bd360 422
fahadmirza 0:a0c5877bd360 423 Lora_ReadData(&rxPacket);
fahadmirza 0:a0c5877bd360 424
fahadmirza 0:a0c5877bd360 425 // Execute users ReceivedPacketHandler function
fahadmirza 0:a0c5877bd360 426 LoraDriverParam->ReceiveDataHandler(rxPacket.Buffer, rxPacket.DataSize, rxPacket.Ack, rxPacket.Port);
fahadmirza 0:a0c5877bd360 427
fahadmirza 0:a0c5877bd360 428 DeviceState = SLEEP_DEVICE;
fahadmirza 0:a0c5877bd360 429 break;
fahadmirza 0:a0c5877bd360 430 }
fahadmirza 0:a0c5877bd360 431 default:
fahadmirza 0:a0c5877bd360 432 {
fahadmirza 0:a0c5877bd360 433 DeviceState = DEVICE_INIT;
fahadmirza 0:a0c5877bd360 434 break;
fahadmirza 0:a0c5877bd360 435 }
fahadmirza 0:a0c5877bd360 436 }
fahadmirza 0:a0c5877bd360 437 }
fahadmirza 0:a0c5877bd360 438
fahadmirza 16:2179ec24dff8 439 /******************************************************************************
fahadmirza 16:2179ec24dff8 440 * @Brief : Set Join request timer
fahadmirza 16:2179ec24dff8 441 * @Param : none
fahadmirza 16:2179ec24dff8 442 * @Return : none
fahadmirza 16:2179ec24dff8 443 ******************************************************************************/
fahadmirza 0:a0c5877bd360 444 static void setJoinRequestTimer(void)
fahadmirza 0:a0c5877bd360 445 {
fahadmirza 0:a0c5877bd360 446 // Use a random delay to avoid synchronized join request from all LoRa node after power up
fahadmirza 0:a0c5877bd360 447 uint32_t joinDelay = (HAL_RNG_GetRandomNumber(&RngHandle) % JOIN_SEND_DELAY_MAX) + 1;
fahadmirza 19:0c90e38fc9c5 448
fahadmirza 0:a0c5877bd360 449 TimerStart( &JoinRequestTimer, joinDelay);
fahadmirza 0:a0c5877bd360 450 }
fahadmirza 0:a0c5877bd360 451
fahadmirza 0:a0c5877bd360 452 /******************************************************************************
fahadmirza 0:a0c5877bd360 453 * @Brief : Function executed on JoinStatusDelayTimer Timeout event
fahadmirza 0:a0c5877bd360 454 * @Param : none
fahadmirza 0:a0c5877bd360 455 * @Return : none
fahadmirza 0:a0c5877bd360 456 ******************************************************************************/
fahadmirza 10:19da323c2bc0 457 static void OnJoinRequestTimerEvt(void)
fahadmirza 0:a0c5877bd360 458 {
fahadmirza 0:a0c5877bd360 459 TimerStop(&JoinRequestTimer);
fahadmirza 0:a0c5877bd360 460 DeviceState = DEVICE_JOIN;
fahadmirza 0:a0c5877bd360 461 }
fahadmirza 0:a0c5877bd360 462
fahadmirza 0:a0c5877bd360 463 /******************************************************************************
fahadmirza 0:a0c5877bd360 464 * @Brief : Function executed on NucleoLedTimer Timeout event
fahadmirza 0:a0c5877bd360 465 * @Param : none
fahadmirza 0:a0c5877bd360 466 * @Return : none
fahadmirza 0:a0c5877bd360 467 ******************************************************************************/
fahadmirza 10:19da323c2bc0 468 static void OnLedTimerEvent(void)
fahadmirza 0:a0c5877bd360 469 {
fahadmirza 0:a0c5877bd360 470 TimerStop(&NucleoLedTimer);
fahadmirza 0:a0c5877bd360 471 BSP_LED_Off(LED_GREEN);
fahadmirza 0:a0c5877bd360 472 }
fahadmirza 0:a0c5877bd360 473
fahadmirza 0:a0c5877bd360 474 /******************************************************************************
fahadmirza 0:a0c5877bd360 475 * @Brief : Function executed on JoinStatusDelayTimer Timeout event
fahadmirza 0:a0c5877bd360 476 * @Param : none
fahadmirza 0:a0c5877bd360 477 * @Return : none
fahadmirza 0:a0c5877bd360 478 ******************************************************************************/
fahadmirza 10:19da323c2bc0 479 static void OnJoinStatusDelayTimerEvt(void)
fahadmirza 0:a0c5877bd360 480 {
fahadmirza 0:a0c5877bd360 481 TimerStop( &JoinStatusDelayTimer );
fahadmirza 0:a0c5877bd360 482 DeviceState = DEVICE_JOIN_STATUS_CHECK;
fahadmirza 0:a0c5877bd360 483 }
fahadmirza 0:a0c5877bd360 484
fahadmirza 0:a0c5877bd360 485 /******************************************************************************
fahadmirza 0:a0c5877bd360 486 * @Brief : Function executed on SensorMeasureTimer Timeout event
fahadmirza 0:a0c5877bd360 487 * @Param : none
fahadmirza 0:a0c5877bd360 488 * @Return : none
fahadmirza 0:a0c5877bd360 489 ******************************************************************************/
fahadmirza 10:19da323c2bc0 490 static void OnSensorMeasureTimerEvt(void)
fahadmirza 0:a0c5877bd360 491 {
fahadmirza 0:a0c5877bd360 492 TimerStop( &SensorMeasureTimer );
fahadmirza 0:a0c5877bd360 493 DeviceState = DEVICE_SEND;
fahadmirza 0:a0c5877bd360 494 }
fahadmirza 0:a0c5877bd360 495
fahadmirza 0:a0c5877bd360 496 /******************************************************************************
fahadmirza 17:e682a18c3914 497 * @Brief : Function executed on RejoinTimer Timeout event
fahadmirza 17:e682a18c3914 498 * @Param : none
fahadmirza 17:e682a18c3914 499 * @Return : none
fahadmirza 17:e682a18c3914 500 ******************************************************************************/
fahadmirza 17:e682a18c3914 501 static void OnRejoinTimerEvent(void)
fahadmirza 17:e682a18c3914 502 {
fahadmirza 17:e682a18c3914 503 setJoinRequestTimer();
fahadmirza 17:e682a18c3914 504 }
fahadmirza 17:e682a18c3914 505
fahadmirza 17:e682a18c3914 506 /******************************************************************************
fahadmirza 0:a0c5877bd360 507 * @brief RNG MSP Initialization
fahadmirza 0:a0c5877bd360 508 * This function configures the hardware resources used in this example:
fahadmirza 0:a0c5877bd360 509 * - Peripheral's clock enable
fahadmirza 0:a0c5877bd360 510 * @param hrng: RNG handle pointer
fahadmirza 0:a0c5877bd360 511 * @retval None
fahadmirza 0:a0c5877bd360 512 ******************************************************************************/
fahadmirza 0:a0c5877bd360 513 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
fahadmirza 0:a0c5877bd360 514 {
fahadmirza 0:a0c5877bd360 515 /* RNG Peripheral clock enable */
fahadmirza 0:a0c5877bd360 516 __HAL_RCC_RNG_CLK_ENABLE();
fahadmirza 0:a0c5877bd360 517 }
fahadmirza 0:a0c5877bd360 518
fahadmirza 0:a0c5877bd360 519