Kashish Haresh Shah
/
HXCClientShield
HXC Client Shield Repository.
LoRa/lora_driver.c@3:5e1a54378107, 2021-04-19 (annotated)
- Committer:
- kashish_mbed
- Date:
- Mon Apr 19 17:43:09 2021 +0000
- Revision:
- 3:5e1a54378107
- Parent:
- 0:bacc6e701fb4
- Child:
- 2:52bb5ee0a72d
Successful Build file with CmdProcess Functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kashish_mbed | 0:bacc6e701fb4 | 1 | /* |
kashish_mbed | 0:bacc6e701fb4 | 2 | _ _ _____ _______ |
kashish_mbed | 0:bacc6e701fb4 | 3 | | | | | |_ _| |__ __| |
kashish_mbed | 0:bacc6e701fb4 | 4 | | |__| | __ ___ __ | | ___ | | |
kashish_mbed | 0:bacc6e701fb4 | 5 | | __ |/ _` \ \/ / | | / _ \| | |
kashish_mbed | 0:bacc6e701fb4 | 6 | | | | | (_| |> < _| || (_) | | |
kashish_mbed | 0:bacc6e701fb4 | 7 | |_| |_|\__,_/_/\_\_____\___/|_| |
kashish_mbed | 0:bacc6e701fb4 | 8 | (C)2017 HaxIoT |
kashish_mbed | 0:bacc6e701fb4 | 9 | */ |
kashish_mbed | 0:bacc6e701fb4 | 10 | /******************************************************************************* |
kashish_mbed | 0:bacc6e701fb4 | 11 | * @File : lora_driver.c |
kashish_mbed | 0:bacc6e701fb4 | 12 | * @Author : Fahad Mirza (Haxiot) |
kashish_mbed | 0:bacc6e701fb4 | 13 | * @Version : V1.0.0 |
kashish_mbed | 0:bacc6e701fb4 | 14 | * @Modified: 18 October, 2018 |
kashish_mbed | 0:bacc6e701fb4 | 15 | * @Brief : LoRa Driver |
kashish_mbed | 0:bacc6e701fb4 | 16 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 17 | * @attention |
kashish_mbed | 0:bacc6e701fb4 | 18 | * |
kashish_mbed | 0:bacc6e701fb4 | 19 | * <h2><center>© COPYRIGHT(c) 2017 Haxiot</center></h2> |
kashish_mbed | 0:bacc6e701fb4 | 20 | * |
kashish_mbed | 0:bacc6e701fb4 | 21 | * Redistribution and use in source and binary forms, with or without modification, |
kashish_mbed | 0:bacc6e701fb4 | 22 | * are permitted provided that the following conditions are met: |
kashish_mbed | 0:bacc6e701fb4 | 23 | * 1. Redistributions of source code must retain the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 24 | * this list of conditions and the following disclaimer. |
kashish_mbed | 0:bacc6e701fb4 | 25 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 26 | * this list of conditions and the following disclaimer in the documentation |
kashish_mbed | 0:bacc6e701fb4 | 27 | * and/or other materials provided with the distribution. |
kashish_mbed | 0:bacc6e701fb4 | 28 | * 3. Neither the name of Haxiot nor the names of its contributors |
kashish_mbed | 0:bacc6e701fb4 | 29 | * may be used to endorse or promote products derived from this software |
kashish_mbed | 0:bacc6e701fb4 | 30 | * without specific prior written permission. |
kashish_mbed | 0:bacc6e701fb4 | 31 | * |
kashish_mbed | 0:bacc6e701fb4 | 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
kashish_mbed | 0:bacc6e701fb4 | 33 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
kashish_mbed | 0:bacc6e701fb4 | 34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kashish_mbed | 0:bacc6e701fb4 | 35 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
kashish_mbed | 0:bacc6e701fb4 | 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
kashish_mbed | 0:bacc6e701fb4 | 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
kashish_mbed | 0:bacc6e701fb4 | 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
kashish_mbed | 0:bacc6e701fb4 | 39 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
kashish_mbed | 0:bacc6e701fb4 | 40 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
kashish_mbed | 0:bacc6e701fb4 | 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kashish_mbed | 0:bacc6e701fb4 | 42 | * |
kashish_mbed | 0:bacc6e701fb4 | 43 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 44 | */ |
kashish_mbed | 0:bacc6e701fb4 | 45 | /* Includes ------------------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 46 | #include <stdio.h> |
kashish_mbed | 0:bacc6e701fb4 | 47 | #include "hxc_client.h" |
kashish_mbed | 0:bacc6e701fb4 | 48 | #include "debug.h" |
kashish_mbed | 0:bacc6e701fb4 | 49 | #include "stm32l0xx_nucleo.h" |
kashish_mbed | 0:bacc6e701fb4 | 50 | #include "time_server.h" |
kashish_mbed | 0:bacc6e701fb4 | 51 | #include "lora_driver.h" |
kashish_mbed | 0:bacc6e701fb4 | 52 | #include "tiny_sscanf.h" |
kashish_mbed | 0:bacc6e701fb4 | 53 | #include "utilities.h" |
kashish_mbed | 0:bacc6e701fb4 | 54 | #include "hxcclient_bsp.h" |
kashish_mbed | 0:bacc6e701fb4 | 55 | |
kashish_mbed | 0:bacc6e701fb4 | 56 | /* Private Macros ------------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 57 | #define JOIN_SEND_DELAY_MAX (10000U) // Randomization range - 10s |
kashish_mbed | 0:bacc6e701fb4 | 58 | #define JOIN_STATUS_REQ_DELAY (7000U) // milliseconds. Join req takes 6s. |
kashish_mbed | 0:bacc6e701fb4 | 59 | |
kashish_mbed | 0:bacc6e701fb4 | 60 | // Re Authenticate every REJOIN_TIME |
kashish_mbed | 0:bacc6e701fb4 | 61 | #define REJOIN_TIME (1 * 60 * 60 * 1000U) // 1 hour |
kashish_mbed | 0:bacc6e701fb4 | 62 | |
kashish_mbed | 0:bacc6e701fb4 | 63 | /* Private global variables --------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 64 | static sLoraConfig_t *LoraConfigParam; |
kashish_mbed | 0:bacc6e701fb4 | 65 | static sLoraDriverParam_t *LoraDriverParam; |
kashish_mbed | 0:bacc6e701fb4 | 66 | static volatile eDeviceState_t DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 67 | |
kashish_mbed | 0:bacc6e701fb4 | 68 | static TimerEvent_t JoinRequestTimer; |
kashish_mbed | 0:bacc6e701fb4 | 69 | static TimerEvent_t RejoinTimer; |
kashish_mbed | 0:bacc6e701fb4 | 70 | static TimerEvent_t SensorMeasureTimer; |
kashish_mbed | 0:bacc6e701fb4 | 71 | static TimerEvent_t JoinStatusReqTimer; |
kashish_mbed | 0:bacc6e701fb4 | 72 | |
kashish_mbed | 0:bacc6e701fb4 | 73 | // Object definition for data to be sent to loRa application server |
kashish_mbed | 0:bacc6e701fb4 | 74 | static uint8_t DataBinaryBuff[MAX_PAYLOAD_SIZE]; |
kashish_mbed | 0:bacc6e701fb4 | 75 | static sSendDataBinary_t SendDataBinary = {DataBinaryBuff, 0 , 0, 0}; |
kashish_mbed | 0:bacc6e701fb4 | 76 | |
kashish_mbed | 0:bacc6e701fb4 | 77 | // RNG handler declaration |
kashish_mbed | 0:bacc6e701fb4 | 78 | RNG_HandleTypeDef RngHandle = {.Instance = RNG}; |
kashish_mbed | 0:bacc6e701fb4 | 79 | |
kashish_mbed | 0:bacc6e701fb4 | 80 | /* Private functions ---------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 81 | static void OnRejoinTimerEvent(void); |
kashish_mbed | 0:bacc6e701fb4 | 82 | static void OnJoinRequestTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 83 | static void OnJoinStatusReqTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 84 | static void OnSensorMeasureTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 85 | static void setJoinRequestTimer(void); |
kashish_mbed | 0:bacc6e701fb4 | 86 | |
kashish_mbed | 0:bacc6e701fb4 | 87 | |
kashish_mbed | 0:bacc6e701fb4 | 88 | /* Function definitions ------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 89 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 90 | * @Brief : Initialize LoRa Modem |
kashish_mbed | 0:bacc6e701fb4 | 91 | * @Param : sLoraConfig_t |
kashish_mbed | 0:bacc6e701fb4 | 92 | * @Return : None |
kashish_mbed | 0:bacc6e701fb4 | 93 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 94 | void Lora_init(sLoraConfig_t *loraConfig, sLoraDriverParam_t *loraDriverParam) |
kashish_mbed | 0:bacc6e701fb4 | 95 | { |
kashish_mbed | 0:bacc6e701fb4 | 96 | |
kashish_mbed | 0:bacc6e701fb4 | 97 | LoraConfigParam = loraConfig; |
kashish_mbed | 0:bacc6e701fb4 | 98 | LoraDriverParam = loraDriverParam; |
kashish_mbed | 0:bacc6e701fb4 | 99 | |
kashish_mbed | 0:bacc6e701fb4 | 100 | if(Modem_Init() != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 101 | { |
kashish_mbed | 0:bacc6e701fb4 | 102 | DBG_PRINTF("Modem_Init failed\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 103 | } |
kashish_mbed | 0:bacc6e701fb4 | 104 | |
kashish_mbed | 0:bacc6e701fb4 | 105 | // Initialize RNG for join request send randomization |
kashish_mbed | 0:bacc6e701fb4 | 106 | HAL_RNG_Init(&RngHandle); |
kashish_mbed | 0:bacc6e701fb4 | 107 | |
kashish_mbed | 0:bacc6e701fb4 | 108 | |
kashish_mbed | 0:bacc6e701fb4 | 109 | // Timer for join request send |
kashish_mbed | 0:bacc6e701fb4 | 110 | TimerInit(&JoinRequestTimer, OnJoinRequestTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 111 | // Timer for join status check |
kashish_mbed | 0:bacc6e701fb4 | 112 | TimerInit(&JoinStatusReqTimer, OnJoinStatusReqTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 113 | TimerSetValue(&JoinStatusReqTimer, JOIN_STATUS_REQ_DELAY); |
kashish_mbed | 0:bacc6e701fb4 | 114 | // Timer for sensor occurrence measure |
kashish_mbed | 0:bacc6e701fb4 | 115 | TimerInit(&SensorMeasureTimer, OnSensorMeasureTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 116 | TimerSetValue(&SensorMeasureTimer, LoraDriverParam->UplinkCycle); |
kashish_mbed | 0:bacc6e701fb4 | 117 | // Timer for Re-Authenticate / Re-Join |
kashish_mbed | 0:bacc6e701fb4 | 118 | TimerInit(&RejoinTimer, OnRejoinTimerEvent); |
kashish_mbed | 0:bacc6e701fb4 | 119 | TimerSetValue(&RejoinTimer, REJOIN_TIME); |
kashish_mbed | 0:bacc6e701fb4 | 120 | } |
kashish_mbed | 0:bacc6e701fb4 | 121 | |
kashish_mbed | 0:bacc6e701fb4 | 122 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 123 | * @Brief : Check if the modem responds |
kashish_mbed | 0:bacc6e701fb4 | 124 | * @Param : void |
kashish_mbed | 0:bacc6e701fb4 | 125 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 126 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 127 | static eAtStatus_t is_modem_working(void) |
kashish_mbed | 0:bacc6e701fb4 | 128 | { |
kashish_mbed | 0:bacc6e701fb4 | 129 | return Modem_AT_Cmd(AT_CTRL, AT, NULL); |
kashish_mbed | 0:bacc6e701fb4 | 130 | } |
kashish_mbed | 0:bacc6e701fb4 | 131 | |
kashish_mbed | 0:bacc6e701fb4 | 132 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 133 | * @Brief : Set Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 134 | * @Param : Pointer to Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 135 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 136 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 137 | static eAtStatus_t Lora_setDevEui(char *devEui) |
kashish_mbed | 0:bacc6e701fb4 | 138 | { |
kashish_mbed | 0:bacc6e701fb4 | 139 | return Modem_AT_Cmd(AT_SET, AT_DEVEUI, devEui); |
kashish_mbed | 0:bacc6e701fb4 | 140 | } |
kashish_mbed | 0:bacc6e701fb4 | 141 | |
kashish_mbed | 0:bacc6e701fb4 | 142 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 143 | * @Brief : Print Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 144 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 145 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 146 | ******************************************************************************/ |
kashish_mbed | 3:5e1a54378107 | 147 | void Lora_printDevEui(void) |
kashish_mbed | 0:bacc6e701fb4 | 148 | { |
kashish_mbed | 0:bacc6e701fb4 | 149 | char deveui_str[25]; |
kashish_mbed | 0:bacc6e701fb4 | 150 | |
kashish_mbed | 0:bacc6e701fb4 | 151 | Modem_AT_Cmd(AT_GET, AT_DEVEUI, deveui_str); |
kashish_mbed | 0:bacc6e701fb4 | 152 | |
kashish_mbed | 0:bacc6e701fb4 | 153 | DBG_PRINTF("DevEui: %s\n", deveui_str); |
kashish_mbed | 0:bacc6e701fb4 | 154 | } |
kashish_mbed | 0:bacc6e701fb4 | 155 | |
kashish_mbed | 0:bacc6e701fb4 | 156 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 157 | * @Brief : Print Device Address |
kashish_mbed | 0:bacc6e701fb4 | 158 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 159 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 160 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 161 | static void Lora_printDevAdr(void) |
kashish_mbed | 0:bacc6e701fb4 | 162 | { |
kashish_mbed | 0:bacc6e701fb4 | 163 | char devadr_str[15]; |
kashish_mbed | 0:bacc6e701fb4 | 164 | |
kashish_mbed | 0:bacc6e701fb4 | 165 | Modem_AT_Cmd(AT_GET, AT_DEVADR, devadr_str); |
kashish_mbed | 0:bacc6e701fb4 | 166 | |
kashish_mbed | 0:bacc6e701fb4 | 167 | DBG_PRINTF("DevAdr: %s\n", devadr_str); |
kashish_mbed | 0:bacc6e701fb4 | 168 | } |
kashish_mbed | 0:bacc6e701fb4 | 169 | |
kashish_mbed | 0:bacc6e701fb4 | 170 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 171 | * @Brief : Turn on or off ADR |
kashish_mbed | 0:bacc6e701fb4 | 172 | * @Param : ADR_ON or ADR_OFF |
kashish_mbed | 0:bacc6e701fb4 | 173 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 174 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 175 | static eAtStatus_t Lora_setAdr(eAdrStatus_t adrStatus) |
kashish_mbed | 0:bacc6e701fb4 | 176 | { |
kashish_mbed | 0:bacc6e701fb4 | 177 | //char adr = (adrStatus == ADR_ON ? '1' : '0'); |
kashish_mbed | 0:bacc6e701fb4 | 178 | return Modem_AT_Cmd(AT_SET, AT_ADR, (uint8_t *)(&adrStatus)); |
kashish_mbed | 0:bacc6e701fb4 | 179 | } |
kashish_mbed | 0:bacc6e701fb4 | 180 | |
kashish_mbed | 0:bacc6e701fb4 | 181 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 182 | * @Brief : Set Application EUI |
kashish_mbed | 0:bacc6e701fb4 | 183 | * @Param : Pointer to Application EUI |
kashish_mbed | 0:bacc6e701fb4 | 184 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 185 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 186 | static eAtStatus_t Lora_setAppEui(char *appEui) |
kashish_mbed | 0:bacc6e701fb4 | 187 | { |
kashish_mbed | 0:bacc6e701fb4 | 188 | return Modem_AT_Cmd(AT_SET, AT_APPEUI, appEui); |
kashish_mbed | 0:bacc6e701fb4 | 189 | } |
kashish_mbed | 0:bacc6e701fb4 | 190 | |
kashish_mbed | 0:bacc6e701fb4 | 191 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 192 | * @Brief : Set Application Key |
kashish_mbed | 0:bacc6e701fb4 | 193 | * @Param : Pointer to Application Key |
kashish_mbed | 0:bacc6e701fb4 | 194 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 195 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 196 | static eAtStatus_t Lora_setAppKey(char *appKey) |
kashish_mbed | 0:bacc6e701fb4 | 197 | { |
kashish_mbed | 0:bacc6e701fb4 | 198 | return Modem_AT_Cmd(AT_SET, AT_APPKEY, appKey); |
kashish_mbed | 0:bacc6e701fb4 | 199 | } |
kashish_mbed | 0:bacc6e701fb4 | 200 | |
kashish_mbed | 0:bacc6e701fb4 | 201 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 202 | * @Brief : Set join mode |
kashish_mbed | 0:bacc6e701fb4 | 203 | * @Param : OTAA or ABP |
kashish_mbed | 0:bacc6e701fb4 | 204 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 205 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 206 | static eAtStatus_t Lora_setJoinMode(eJoinMode_t joinMode) |
kashish_mbed | 0:bacc6e701fb4 | 207 | { |
kashish_mbed | 0:bacc6e701fb4 | 208 | if(joinMode == OTAA) |
kashish_mbed | 0:bacc6e701fb4 | 209 | { |
kashish_mbed | 0:bacc6e701fb4 | 210 | return Modem_AT_Cmd(AT_SET, AT_NJM, "OTAA"); |
kashish_mbed | 0:bacc6e701fb4 | 211 | } |
kashish_mbed | 0:bacc6e701fb4 | 212 | |
kashish_mbed | 0:bacc6e701fb4 | 213 | return Modem_AT_Cmd(AT_SET, AT_NJM, "ABP"); |
kashish_mbed | 0:bacc6e701fb4 | 214 | } |
kashish_mbed | 0:bacc6e701fb4 | 215 | |
kashish_mbed | 0:bacc6e701fb4 | 216 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 217 | * @Brief : Set Class |
kashish_mbed | 0:bacc6e701fb4 | 218 | * @Param : CLASS_A or CLASS_C |
kashish_mbed | 0:bacc6e701fb4 | 219 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 220 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 221 | static eAtStatus_t Lora_setClass(char class) |
kashish_mbed | 0:bacc6e701fb4 | 222 | { |
kashish_mbed | 0:bacc6e701fb4 | 223 | return Modem_AT_Cmd(AT_SET, AT_CLASS, &class); |
kashish_mbed | 0:bacc6e701fb4 | 224 | } |
kashish_mbed | 0:bacc6e701fb4 | 225 | |
kashish_mbed | 0:bacc6e701fb4 | 226 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 227 | * @Brief : Join network and initiate join sleep transition timer |
kashish_mbed | 0:bacc6e701fb4 | 228 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 229 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 230 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 231 | static eAtStatus_t Lora_Join(void) |
kashish_mbed | 0:bacc6e701fb4 | 232 | { |
kashish_mbed | 0:bacc6e701fb4 | 233 | return Modem_AT_Cmd(AT_CTRL, AT_JOIN, NULL); |
kashish_mbed | 0:bacc6e701fb4 | 234 | } |
kashish_mbed | 0:bacc6e701fb4 | 235 | |
kashish_mbed | 0:bacc6e701fb4 | 236 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 237 | * @Brief : Check JOIN status |
kashish_mbed | 0:bacc6e701fb4 | 238 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 239 | * @Retval : JOINED or NOT_JOINED |
kashish_mbed | 0:bacc6e701fb4 | 240 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 241 | static eJoinStatus_t Lora_getJoinStatus(void) |
kashish_mbed | 0:bacc6e701fb4 | 242 | { |
kashish_mbed | 0:bacc6e701fb4 | 243 | char joinStatus[12]; // "NOT JOINED" is 10 characters + 1 Null char |
kashish_mbed | 0:bacc6e701fb4 | 244 | |
kashish_mbed | 0:bacc6e701fb4 | 245 | Modem_AT_Cmd(AT_GET, AT_NJS, joinStatus); |
kashish_mbed | 0:bacc6e701fb4 | 246 | |
kashish_mbed | 0:bacc6e701fb4 | 247 | if(strncmp("JOINED", joinStatus, 6) == 0) |
kashish_mbed | 0:bacc6e701fb4 | 248 | { |
kashish_mbed | 0:bacc6e701fb4 | 249 | return JOINED; |
kashish_mbed | 0:bacc6e701fb4 | 250 | } |
kashish_mbed | 0:bacc6e701fb4 | 251 | |
kashish_mbed | 0:bacc6e701fb4 | 252 | return NOT_JOINED; |
kashish_mbed | 0:bacc6e701fb4 | 253 | } |
kashish_mbed | 0:bacc6e701fb4 | 254 | |
kashish_mbed | 0:bacc6e701fb4 | 255 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 256 | * @Brief : Get firmware version |
kashish_mbed | 0:bacc6e701fb4 | 257 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 258 | * @Retval : Integer firmware version |
kashish_mbed | 0:bacc6e701fb4 | 259 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 260 | uint16_t Lora_getFwVersion(void) |
kashish_mbed | 0:bacc6e701fb4 | 261 | { |
kashish_mbed | 0:bacc6e701fb4 | 262 | char fwVersion_str[5]; |
kashish_mbed | 0:bacc6e701fb4 | 263 | uint16_t fwVer_int; |
kashish_mbed | 0:bacc6e701fb4 | 264 | |
kashish_mbed | 0:bacc6e701fb4 | 265 | eAtStatus_t status = Modem_AT_Cmd(AT_GET, AT_VER, fwVersion_str); |
kashish_mbed | 0:bacc6e701fb4 | 266 | |
kashish_mbed | 0:bacc6e701fb4 | 267 | if(status != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 268 | { |
kashish_mbed | 0:bacc6e701fb4 | 269 | return 0; |
kashish_mbed | 0:bacc6e701fb4 | 270 | } |
kashish_mbed | 0:bacc6e701fb4 | 271 | |
kashish_mbed | 0:bacc6e701fb4 | 272 | // FW version is a decimal number e.g. 1.18 |
kashish_mbed | 0:bacc6e701fb4 | 273 | // Convert the float number into integer. |
kashish_mbed | 0:bacc6e701fb4 | 274 | uint8_t i = 0; |
kashish_mbed | 0:bacc6e701fb4 | 275 | while(fwVersion_str[++i] != '.'); |
kashish_mbed | 0:bacc6e701fb4 | 276 | |
kashish_mbed | 0:bacc6e701fb4 | 277 | while(fwVersion_str[i] != '\0') |
kashish_mbed | 0:bacc6e701fb4 | 278 | { |
kashish_mbed | 0:bacc6e701fb4 | 279 | fwVersion_str[i] = fwVersion_str[i + 1]; |
kashish_mbed | 0:bacc6e701fb4 | 280 | i++; |
kashish_mbed | 0:bacc6e701fb4 | 281 | } |
kashish_mbed | 0:bacc6e701fb4 | 282 | |
kashish_mbed | 0:bacc6e701fb4 | 283 | tiny_sscanf(fwVersion_str, "%hu", &fwVer_int); |
kashish_mbed | 0:bacc6e701fb4 | 284 | |
kashish_mbed | 0:bacc6e701fb4 | 285 | return fwVer_int; |
kashish_mbed | 0:bacc6e701fb4 | 286 | } |
kashish_mbed | 0:bacc6e701fb4 | 287 | |
kashish_mbed | 0:bacc6e701fb4 | 288 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 289 | * @Brief : Get battery level |
kashish_mbed | 0:bacc6e701fb4 | 290 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 291 | * @Retval : Battery level |
kashish_mbed | 0:bacc6e701fb4 | 292 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 293 | uint8_t Lora_getBatteryLevel(void) |
kashish_mbed | 0:bacc6e701fb4 | 294 | { |
kashish_mbed | 0:bacc6e701fb4 | 295 | char batteryLevel_str[5]; |
kashish_mbed | 0:bacc6e701fb4 | 296 | uint8_t batteryLevel = 0; |
kashish_mbed | 0:bacc6e701fb4 | 297 | |
kashish_mbed | 0:bacc6e701fb4 | 298 | eAtStatus_t status = Modem_AT_Cmd(AT_GET, AT_BAT, batteryLevel_str); |
kashish_mbed | 0:bacc6e701fb4 | 299 | |
kashish_mbed | 0:bacc6e701fb4 | 300 | if(status == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 301 | { |
kashish_mbed | 0:bacc6e701fb4 | 302 | tiny_sscanf(batteryLevel_str, "%hhu", &batteryLevel); |
kashish_mbed | 0:bacc6e701fb4 | 303 | } |
kashish_mbed | 0:bacc6e701fb4 | 304 | |
kashish_mbed | 0:bacc6e701fb4 | 305 | return batteryLevel; |
kashish_mbed | 0:bacc6e701fb4 | 306 | } |
kashish_mbed | 0:bacc6e701fb4 | 307 | |
kashish_mbed | 0:bacc6e701fb4 | 308 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 309 | * @Brief : Update the uplink rate |
kashish_mbed | 0:bacc6e701fb4 | 310 | * @Param : time_s seconds |
kashish_mbed | 0:bacc6e701fb4 | 311 | * @Retval : None |
kashish_mbed | 0:bacc6e701fb4 | 312 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 313 | void Lora_updateUplinkRate(uint32_t time_s) |
kashish_mbed | 0:bacc6e701fb4 | 314 | { |
kashish_mbed | 0:bacc6e701fb4 | 315 | TimerSetValue(&SensorMeasureTimer, (time_s * 1000)); |
kashish_mbed | 0:bacc6e701fb4 | 316 | TimerReset(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 317 | } |
kashish_mbed | 0:bacc6e701fb4 | 318 | |
kashish_mbed | 0:bacc6e701fb4 | 319 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 320 | * @Brief : Send uplink packet using binary payload |
kashish_mbed | 0:bacc6e701fb4 | 321 | * @Param : Pointer of sSendDataBinary_t variable |
kashish_mbed | 0:bacc6e701fb4 | 322 | * @Retval : AT_OK or other eAtStatus_t statuses |
kashish_mbed | 0:bacc6e701fb4 | 323 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 324 | static eAtStatus_t Lora_SendDataBinary(sSendDataBinary_t *binaryData) |
kashish_mbed | 0:bacc6e701fb4 | 325 | { |
kashish_mbed | 0:bacc6e701fb4 | 326 | return Modem_AT_Cmd(AT_SET, AT_SENDB, binaryData); |
kashish_mbed | 0:bacc6e701fb4 | 327 | } |
kashish_mbed | 0:bacc6e701fb4 | 328 | |
kashish_mbed | 0:bacc6e701fb4 | 329 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 330 | * @Brief : Read the received downlink packet |
kashish_mbed | 0:bacc6e701fb4 | 331 | * @Param : Pointer to sRecvDataBinary_t variable |
kashish_mbed | 0:bacc6e701fb4 | 332 | * @Return : AT_OK or other eAtStatus_t statuses |
kashish_mbed | 0:bacc6e701fb4 | 333 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 334 | static eAtStatus_t Lora_ReadData(sRecvDataBinary_t *rxData) |
kashish_mbed | 0:bacc6e701fb4 | 335 | { |
kashish_mbed | 0:bacc6e701fb4 | 336 | char *rxString = Modem_GetResponseBuffer(); |
kashish_mbed | 0:bacc6e701fb4 | 337 | |
kashish_mbed | 0:bacc6e701fb4 | 338 | // Find the position after : |
kashish_mbed | 0:bacc6e701fb4 | 339 | // e.g. rxdata:0,1,86GF25 |
kashish_mbed | 0:bacc6e701fb4 | 340 | rxString = (strchr(rxString,':') + 1); |
kashish_mbed | 0:bacc6e701fb4 | 341 | |
kashish_mbed | 0:bacc6e701fb4 | 342 | if(rxString == NULL) |
kashish_mbed | 0:bacc6e701fb4 | 343 | { |
kashish_mbed | 0:bacc6e701fb4 | 344 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 345 | } |
kashish_mbed | 0:bacc6e701fb4 | 346 | |
kashish_mbed | 0:bacc6e701fb4 | 347 | if(tiny_sscanf(rxString, "%hhu,%hhu", &(rxData->Ack), &(rxData->Port)) != 2) |
kashish_mbed | 0:bacc6e701fb4 | 348 | { |
kashish_mbed | 0:bacc6e701fb4 | 349 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 350 | } |
kashish_mbed | 0:bacc6e701fb4 | 351 | |
kashish_mbed | 0:bacc6e701fb4 | 352 | // Find the position after the second comma |
kashish_mbed | 0:bacc6e701fb4 | 353 | rxString = strchr((strchr(rxString,',') + 1),',') + 1; |
kashish_mbed | 0:bacc6e701fb4 | 354 | if(rxString == NULL) |
kashish_mbed | 0:bacc6e701fb4 | 355 | { |
kashish_mbed | 0:bacc6e701fb4 | 356 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 357 | } |
kashish_mbed | 0:bacc6e701fb4 | 358 | |
kashish_mbed | 0:bacc6e701fb4 | 359 | rxData->DataSize = stringHexToByteArray(rxString, rxData->Buffer, MAX_PAYLOAD_SIZE); |
kashish_mbed | 0:bacc6e701fb4 | 360 | |
kashish_mbed | 0:bacc6e701fb4 | 361 | return AT_OK; |
kashish_mbed | 0:bacc6e701fb4 | 362 | } |
kashish_mbed | 0:bacc6e701fb4 | 363 | |
kashish_mbed | 0:bacc6e701fb4 | 364 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 365 | * @Brief : LoRa Modem state machine |
kashish_mbed | 0:bacc6e701fb4 | 366 | * @Param : Void |
kashish_mbed | 0:bacc6e701fb4 | 367 | * @Return : None |
kashish_mbed | 0:bacc6e701fb4 | 368 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 369 | void Lora_fsm(void) |
kashish_mbed | 0:bacc6e701fb4 | 370 | { |
kashish_mbed | 0:bacc6e701fb4 | 371 | switch(DeviceState) |
kashish_mbed | 0:bacc6e701fb4 | 372 | { |
kashish_mbed | 0:bacc6e701fb4 | 373 | case CLIENT_INIT: |
kashish_mbed | 0:bacc6e701fb4 | 374 | { |
kashish_mbed | 0:bacc6e701fb4 | 375 | if(is_modem_working() != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 376 | { |
kashish_mbed | 0:bacc6e701fb4 | 377 | DBG_PRINTF("AT failed. Resetting HW...\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 378 | // Modem isn't responding. Execute hard reset. |
kashish_mbed | 0:bacc6e701fb4 | 379 | Modem_HardReset(); |
kashish_mbed | 0:bacc6e701fb4 | 380 | // We stay in CLIENT_INIT state and try again. |
kashish_mbed | 0:bacc6e701fb4 | 381 | } |
kashish_mbed | 0:bacc6e701fb4 | 382 | else |
kashish_mbed | 0:bacc6e701fb4 | 383 | { |
kashish_mbed | 0:bacc6e701fb4 | 384 | DeviceState = CLIENT_CONFIG; |
kashish_mbed | 0:bacc6e701fb4 | 385 | } |
kashish_mbed | 0:bacc6e701fb4 | 386 | |
kashish_mbed | 0:bacc6e701fb4 | 387 | break; |
kashish_mbed | 0:bacc6e701fb4 | 388 | } |
kashish_mbed | 0:bacc6e701fb4 | 389 | case CLIENT_CONFIG: |
kashish_mbed | 0:bacc6e701fb4 | 390 | { |
kashish_mbed | 0:bacc6e701fb4 | 391 | eAtStatus_t loraModemRetCode = Lora_setDevEui(LoraConfigParam->DevEui); |
kashish_mbed | 0:bacc6e701fb4 | 392 | loraModemRetCode |= Lora_setAppEui(LoraConfigParam->AppEui); |
kashish_mbed | 0:bacc6e701fb4 | 393 | loraModemRetCode |= Lora_setAppKey(LoraConfigParam->AppKey); |
kashish_mbed | 0:bacc6e701fb4 | 394 | loraModemRetCode |= Lora_setJoinMode(LoraConfigParam->JoinMode); |
kashish_mbed | 0:bacc6e701fb4 | 395 | loraModemRetCode |= Lora_setClass(LoraConfigParam->Class); |
kashish_mbed | 0:bacc6e701fb4 | 396 | loraModemRetCode |= Lora_setAdr(LoraConfigParam->AdrStatus); |
kashish_mbed | 0:bacc6e701fb4 | 397 | |
kashish_mbed | 0:bacc6e701fb4 | 398 | if(loraModemRetCode == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 399 | { |
kashish_mbed | 0:bacc6e701fb4 | 400 | // If users use AUTO as Device EUI, print the DevEUI |
kashish_mbed | 0:bacc6e701fb4 | 401 | // so that they can add that on X-ON |
kashish_mbed | 0:bacc6e701fb4 | 402 | Lora_printDevEui(); |
kashish_mbed | 0:bacc6e701fb4 | 403 | |
kashish_mbed | 0:bacc6e701fb4 | 404 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 405 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 406 | } |
kashish_mbed | 0:bacc6e701fb4 | 407 | else if(loraModemRetCode == AT_TIMEOUT) |
kashish_mbed | 0:bacc6e701fb4 | 408 | { |
kashish_mbed | 0:bacc6e701fb4 | 409 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 410 | } |
kashish_mbed | 0:bacc6e701fb4 | 411 | else |
kashish_mbed | 0:bacc6e701fb4 | 412 | { |
kashish_mbed | 0:bacc6e701fb4 | 413 | DBG_PRINTF("Check your keys\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 414 | while(1); |
kashish_mbed | 0:bacc6e701fb4 | 415 | } |
kashish_mbed | 0:bacc6e701fb4 | 416 | |
kashish_mbed | 0:bacc6e701fb4 | 417 | break; |
kashish_mbed | 0:bacc6e701fb4 | 418 | } |
kashish_mbed | 0:bacc6e701fb4 | 419 | case CLIENT_JOIN: |
kashish_mbed | 0:bacc6e701fb4 | 420 | { |
kashish_mbed | 0:bacc6e701fb4 | 421 | DBG_PRINTF("Joining...\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 422 | HXC_BSP_RGB_Off(); |
kashish_mbed | 0:bacc6e701fb4 | 423 | |
kashish_mbed | 0:bacc6e701fb4 | 424 | switch(Lora_Join()) |
kashish_mbed | 0:bacc6e701fb4 | 425 | { |
kashish_mbed | 0:bacc6e701fb4 | 426 | case AT_OK: |
kashish_mbed | 0:bacc6e701fb4 | 427 | { |
kashish_mbed | 0:bacc6e701fb4 | 428 | // Start the Join status request timer and go to sleep |
kashish_mbed | 0:bacc6e701fb4 | 429 | TimerStart(&JoinStatusReqTimer); |
kashish_mbed | 0:bacc6e701fb4 | 430 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 431 | break; |
kashish_mbed | 0:bacc6e701fb4 | 432 | } |
kashish_mbed | 0:bacc6e701fb4 | 433 | case AT_TIMEOUT: |
kashish_mbed | 0:bacc6e701fb4 | 434 | { |
kashish_mbed | 0:bacc6e701fb4 | 435 | // The modem isn't responding. Execute hard reset |
kashish_mbed | 0:bacc6e701fb4 | 436 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 437 | break; |
kashish_mbed | 0:bacc6e701fb4 | 438 | } |
kashish_mbed | 0:bacc6e701fb4 | 439 | default: |
kashish_mbed | 0:bacc6e701fb4 | 440 | { |
kashish_mbed | 0:bacc6e701fb4 | 441 | DBG_PRINTF("Join cmd failed\n"); |
kashish_mbed | 0:bacc6e701fb4 | 442 | // We stay in CLIENT_JOIN state and redo Lora_Join() |
kashish_mbed | 0:bacc6e701fb4 | 443 | break; |
kashish_mbed | 0:bacc6e701fb4 | 444 | } |
kashish_mbed | 0:bacc6e701fb4 | 445 | } |
kashish_mbed | 0:bacc6e701fb4 | 446 | |
kashish_mbed | 0:bacc6e701fb4 | 447 | break; |
kashish_mbed | 0:bacc6e701fb4 | 448 | } |
kashish_mbed | 0:bacc6e701fb4 | 449 | case CLIENT_JOIN_STATUS_CHECK: |
kashish_mbed | 0:bacc6e701fb4 | 450 | { |
kashish_mbed | 0:bacc6e701fb4 | 451 | if(Lora_getJoinStatus() == JOINED) |
kashish_mbed | 0:bacc6e701fb4 | 452 | { |
kashish_mbed | 0:bacc6e701fb4 | 453 | // Indicate Join status using LED |
kashish_mbed | 0:bacc6e701fb4 | 454 | HXC_BSP_RGB_On(PINK); |
kashish_mbed | 0:bacc6e701fb4 | 455 | DBG_PRINTF("Nwk Joined\n"); |
kashish_mbed | 0:bacc6e701fb4 | 456 | Lora_printDevAdr(); |
kashish_mbed | 0:bacc6e701fb4 | 457 | |
kashish_mbed | 0:bacc6e701fb4 | 458 | // Start timer for Re-Authentication |
kashish_mbed | 0:bacc6e701fb4 | 459 | TimerStart(&RejoinTimer); |
kashish_mbed | 0:bacc6e701fb4 | 460 | |
kashish_mbed | 0:bacc6e701fb4 | 461 | // Start timer for uplink transmission for sensor-data |
kashish_mbed | 0:bacc6e701fb4 | 462 | TimerStart(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 463 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 464 | } |
kashish_mbed | 0:bacc6e701fb4 | 465 | else |
kashish_mbed | 0:bacc6e701fb4 | 466 | { |
kashish_mbed | 0:bacc6e701fb4 | 467 | // Try joining again |
kashish_mbed | 0:bacc6e701fb4 | 468 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 469 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 470 | } |
kashish_mbed | 0:bacc6e701fb4 | 471 | break; |
kashish_mbed | 0:bacc6e701fb4 | 472 | } |
kashish_mbed | 0:bacc6e701fb4 | 473 | case CLIENT_SLEEP: |
kashish_mbed | 0:bacc6e701fb4 | 474 | { |
kashish_mbed | 0:bacc6e701fb4 | 475 | /* Wake up through RTC events or asynchronous event coming from HXC modem*/ |
kashish_mbed | 0:bacc6e701fb4 | 476 | if(Modem_IsNewDataReceived() == true) |
kashish_mbed | 0:bacc6e701fb4 | 477 | { |
kashish_mbed | 0:bacc6e701fb4 | 478 | DeviceState = CLIENT_DATA_RECEIVED; |
kashish_mbed | 0:bacc6e701fb4 | 479 | } |
kashish_mbed | 0:bacc6e701fb4 | 480 | break; |
kashish_mbed | 0:bacc6e701fb4 | 481 | } |
kashish_mbed | 0:bacc6e701fb4 | 482 | case CLIENT_SEND: |
kashish_mbed | 0:bacc6e701fb4 | 483 | { |
kashish_mbed | 0:bacc6e701fb4 | 484 | // Read sensor data and populate payload |
kashish_mbed | 0:bacc6e701fb4 | 485 | SendDataBinary.DataSize = LoraDriverParam->UplinkHandler(\ |
kashish_mbed | 0:bacc6e701fb4 | 486 | SendDataBinary.Buffer ,\ |
kashish_mbed | 0:bacc6e701fb4 | 487 | MAX_PAYLOAD_SIZE ,\ |
kashish_mbed | 0:bacc6e701fb4 | 488 | &SendDataBinary.Ack ,\ |
kashish_mbed | 0:bacc6e701fb4 | 489 | &SendDataBinary.Port ); |
kashish_mbed | 0:bacc6e701fb4 | 490 | |
kashish_mbed | 0:bacc6e701fb4 | 491 | // Initiate uplink transmission |
kashish_mbed | 0:bacc6e701fb4 | 492 | eAtStatus_t status = Lora_SendDataBinary(&SendDataBinary); |
kashish_mbed | 0:bacc6e701fb4 | 493 | |
kashish_mbed | 0:bacc6e701fb4 | 494 | if (status == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 495 | { |
kashish_mbed | 0:bacc6e701fb4 | 496 | DBG_PRINTF("Uplink sent\n"); |
kashish_mbed | 0:bacc6e701fb4 | 497 | // Schedule the next packet |
kashish_mbed | 0:bacc6e701fb4 | 498 | TimerStart(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 499 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 500 | } |
kashish_mbed | 0:bacc6e701fb4 | 501 | else if(status == AT_TIMEOUT) |
kashish_mbed | 0:bacc6e701fb4 | 502 | { |
kashish_mbed | 0:bacc6e701fb4 | 503 | // Device isn't responding. Go to init. |
kashish_mbed | 0:bacc6e701fb4 | 504 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 505 | } |
kashish_mbed | 0:bacc6e701fb4 | 506 | else if(status == AT_NO_NET_JOINED) |
kashish_mbed | 0:bacc6e701fb4 | 507 | { |
kashish_mbed | 0:bacc6e701fb4 | 508 | DeviceState = CLIENT_JOIN; |
kashish_mbed | 0:bacc6e701fb4 | 509 | } |
kashish_mbed | 0:bacc6e701fb4 | 510 | else |
kashish_mbed | 0:bacc6e701fb4 | 511 | { |
kashish_mbed | 0:bacc6e701fb4 | 512 | DBG_PRINTF("Uplink Failed (Error: %d)\n", (uint8_t)status); |
kashish_mbed | 0:bacc6e701fb4 | 513 | } |
kashish_mbed | 0:bacc6e701fb4 | 514 | break; |
kashish_mbed | 0:bacc6e701fb4 | 515 | } |
kashish_mbed | 0:bacc6e701fb4 | 516 | case CLIENT_DATA_RECEIVED: |
kashish_mbed | 0:bacc6e701fb4 | 517 | { |
kashish_mbed | 0:bacc6e701fb4 | 518 | uint8_t rBuffer[64]; |
kashish_mbed | 0:bacc6e701fb4 | 519 | sRecvDataBinary_t rxPacket = {.Buffer = rBuffer}; |
kashish_mbed | 0:bacc6e701fb4 | 520 | |
kashish_mbed | 0:bacc6e701fb4 | 521 | if(Lora_ReadData(&rxPacket) == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 522 | { |
kashish_mbed | 0:bacc6e701fb4 | 523 | // Execute users ReceivedPacketHandler function |
kashish_mbed | 0:bacc6e701fb4 | 524 | LoraDriverParam->DownlinkHandler(rxPacket.Buffer, rxPacket.DataSize, rxPacket.Ack, rxPacket.Port); |
kashish_mbed | 0:bacc6e701fb4 | 525 | } |
kashish_mbed | 0:bacc6e701fb4 | 526 | else |
kashish_mbed | 0:bacc6e701fb4 | 527 | { |
kashish_mbed | 0:bacc6e701fb4 | 528 | DBG_PRINTF("Dwlink read failed\n"); |
kashish_mbed | 0:bacc6e701fb4 | 529 | } |
kashish_mbed | 0:bacc6e701fb4 | 530 | |
kashish_mbed | 0:bacc6e701fb4 | 531 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 532 | break; |
kashish_mbed | 0:bacc6e701fb4 | 533 | } |
kashish_mbed | 0:bacc6e701fb4 | 534 | default: |
kashish_mbed | 0:bacc6e701fb4 | 535 | { |
kashish_mbed | 0:bacc6e701fb4 | 536 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 537 | break; |
kashish_mbed | 0:bacc6e701fb4 | 538 | } |
kashish_mbed | 0:bacc6e701fb4 | 539 | } |
kashish_mbed | 0:bacc6e701fb4 | 540 | } |
kashish_mbed | 0:bacc6e701fb4 | 541 | |
kashish_mbed | 0:bacc6e701fb4 | 542 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 543 | * @Brief : Set Join request timer |
kashish_mbed | 0:bacc6e701fb4 | 544 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 545 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 546 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 547 | static void setJoinRequestTimer(void) |
kashish_mbed | 0:bacc6e701fb4 | 548 | { |
kashish_mbed | 0:bacc6e701fb4 | 549 | // Use a random delay to avoid synchronized join request from all LoRa node after power up |
kashish_mbed | 0:bacc6e701fb4 | 550 | uint32_t joinDelay = (HAL_RNG_GetRandomNumber(&RngHandle) % JOIN_SEND_DELAY_MAX) + 1; |
kashish_mbed | 0:bacc6e701fb4 | 551 | |
kashish_mbed | 0:bacc6e701fb4 | 552 | TimerSetValue(&JoinRequestTimer, joinDelay); |
kashish_mbed | 0:bacc6e701fb4 | 553 | TimerStart(&JoinRequestTimer); |
kashish_mbed | 0:bacc6e701fb4 | 554 | } |
kashish_mbed | 0:bacc6e701fb4 | 555 | |
kashish_mbed | 0:bacc6e701fb4 | 556 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 557 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 558 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 559 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 560 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 561 | static void OnJoinRequestTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 562 | { |
kashish_mbed | 0:bacc6e701fb4 | 563 | TimerStop(&JoinRequestTimer); |
kashish_mbed | 0:bacc6e701fb4 | 564 | // If this is a re-join SensorMeasureTimer is active |
kashish_mbed | 0:bacc6e701fb4 | 565 | TimerStop(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 566 | |
kashish_mbed | 0:bacc6e701fb4 | 567 | DeviceState = CLIENT_JOIN; |
kashish_mbed | 0:bacc6e701fb4 | 568 | } |
kashish_mbed | 0:bacc6e701fb4 | 569 | |
kashish_mbed | 0:bacc6e701fb4 | 570 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 571 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 572 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 573 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 574 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 575 | static void OnJoinStatusReqTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 576 | { |
kashish_mbed | 0:bacc6e701fb4 | 577 | TimerStop(&JoinStatusReqTimer); |
kashish_mbed | 0:bacc6e701fb4 | 578 | DeviceState = CLIENT_JOIN_STATUS_CHECK; |
kashish_mbed | 0:bacc6e701fb4 | 579 | } |
kashish_mbed | 0:bacc6e701fb4 | 580 | |
kashish_mbed | 0:bacc6e701fb4 | 581 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 582 | * @Brief : Function executed on SensorMeasureTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 583 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 584 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 585 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 586 | static void OnSensorMeasureTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 587 | { |
kashish_mbed | 0:bacc6e701fb4 | 588 | TimerStop(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 589 | DeviceState = CLIENT_SEND; |
kashish_mbed | 0:bacc6e701fb4 | 590 | } |
kashish_mbed | 0:bacc6e701fb4 | 591 | |
kashish_mbed | 0:bacc6e701fb4 | 592 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 593 | * @Brief : Function executed on RejoinTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 594 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 595 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 596 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 597 | static void OnRejoinTimerEvent(void) |
kashish_mbed | 0:bacc6e701fb4 | 598 | { |
kashish_mbed | 0:bacc6e701fb4 | 599 | TimerStop(&RejoinTimer); |
kashish_mbed | 0:bacc6e701fb4 | 600 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 601 | } |
kashish_mbed | 0:bacc6e701fb4 | 602 | |
kashish_mbed | 0:bacc6e701fb4 | 603 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 604 | * @brief RNG MSP Initialization |
kashish_mbed | 0:bacc6e701fb4 | 605 | * This function configures the hardware resources used in this example: |
kashish_mbed | 0:bacc6e701fb4 | 606 | * - Peripheral's clock enable |
kashish_mbed | 0:bacc6e701fb4 | 607 | * @param hrng: RNG handle pointer |
kashish_mbed | 0:bacc6e701fb4 | 608 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 609 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 610 | void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng) |
kashish_mbed | 0:bacc6e701fb4 | 611 | { |
kashish_mbed | 0:bacc6e701fb4 | 612 | /* RNG Peripheral clock enable */ |
kashish_mbed | 0:bacc6e701fb4 | 613 | __HAL_RCC_RNG_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 614 | } |
kashish_mbed | 0:bacc6e701fb4 | 615 | |
kashish_mbed | 0:bacc6e701fb4 | 616 |