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

Dependencies:   mbed

Committer:
fahadmirza
Date:
Tue Sep 04 17:27:02 2018 +0000
Revision:
18:24c232951ff8
Parent:
15:2860c960d2ff
Child:
21:f1d561ed31a1
Changed device address format in the hxc_client driver from uint32_t to char*;

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 : hxc_client.c
fahadmirza 0:a0c5877bd360 12 * Author : Fahad (Haxiot)
fahadmirza 0:a0c5877bd360 13 * Version : V1.0.0
fahadmirza 15:2860c960d2ff 14 * Modified: 15-July-2018
fahadmirza 0:a0c5877bd360 15 * Brief : HXC AT command API
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
fahadmirza 0:a0c5877bd360 46 /* Includes ------------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 47 #include <stdbool.h>
fahadmirza 0:a0c5877bd360 48 #include <stdint.h>
fahadmirza 0:a0c5877bd360 49 #include <stdarg.h>
fahadmirza 0:a0c5877bd360 50 #include <string.h>
fahadmirza 0:a0c5877bd360 51 #include "hw_usart.h"
fahadmirza 0:a0c5877bd360 52 #include "utilities.h"
fahadmirza 0:a0c5877bd360 53 #include "tiny_vsnprintf.h"
fahadmirza 0:a0c5877bd360 54 #include "hxc_client.h"
fahadmirza 0:a0c5877bd360 55 #include "hw_conf.h"
fahadmirza 0:a0c5877bd360 56 #include "delay.h"
fahadmirza 0:a0c5877bd360 57 #include "time_server.h"
fahadmirza 0:a0c5877bd360 58 #include "hw_gpio.h"
fahadmirza 0:a0c5877bd360 59
fahadmirza 0:a0c5877bd360 60 /* Private typedef -----------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 61 // Type definition for return code analysis
fahadmirza 0:a0c5877bd360 62 typedef char* AtErrorStr_t;
fahadmirza 0:a0c5877bd360 63
fahadmirza 0:a0c5877bd360 64 typedef struct sAtRetCode
fahadmirza 0:a0c5877bd360 65 {
fahadmirza 0:a0c5877bd360 66 AtErrorStr_t RetCodeStr;
fahadmirza 0:a0c5877bd360 67 uint8_t SizeRetCodeStr;
fahadmirza 0:a0c5877bd360 68 eAtStatus_t RetCode;
fahadmirza 0:a0c5877bd360 69 }sAtRetCode_t;
fahadmirza 0:a0c5877bd360 70
fahadmirza 15:2860c960d2ff 71
fahadmirza 0:a0c5877bd360 72 /* Private functions ---------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 73 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr);
fahadmirza 0:a0c5877bd360 74 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr);
fahadmirza 0:a0c5877bd360 75 static eAtStatus_t at_cmd_send(uint16_t len);
fahadmirza 0:a0c5877bd360 76 static eAtStatus_t at_cmd_receive(void *pdata);
fahadmirza 0:a0c5877bd360 77 static eAtStatus_t at_cmd_responseAnalysing(const char *ReturnResp);
fahadmirza 0:a0c5877bd360 78 static uint16_t at_cmd_vprintf(const char *format, ...);
fahadmirza 0:a0c5877bd360 79
fahadmirza 15:2860c960d2ff 80
fahadmirza 0:a0c5877bd360 81 /* Private macro -------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 82 #define AT_VPRINTF(...) at_cmd_vprintf(__VA_ARGS__)
fahadmirza 0:a0c5877bd360 83 #define AT_VSSCANF(...) tiny_sscanf(__VA_ARGS__)
fahadmirza 0:a0c5877bd360 84
fahadmirza 0:a0c5877bd360 85 #define BAUD_RATE 9600
fahadmirza 0:a0c5877bd360 86 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
fahadmirza 0:a0c5877bd360 87
fahadmirza 0:a0c5877bd360 88 #define DATA_RX_MAX_BUFF_SIZE 64 /* Max size of the received buffer.
fahadmirza 0:a0c5877bd360 89 * To optimize we can match with device key sizeof */
fahadmirza 0:a0c5877bd360 90
fahadmirza 0:a0c5877bd360 91 #define DATA_TX_MAX_BUFF_SIZE 78 // Max size of the transmit buffer
fahadmirza 0:a0c5877bd360 92 #define HXC_TIMEOUT 2000 // 2 seconds
fahadmirza 0:a0c5877bd360 93
fahadmirza 0:a0c5877bd360 94 /*
fahadmirza 0:a0c5877bd360 95 * RetCode used to compare the return code from HXC modem.
fahadmirza 0:a0c5877bd360 96 * In direct relation with sAtRetCode_t
fahadmirza 0:a0c5877bd360 97 */
fahadmirza 0:a0c5877bd360 98 #define RET_OK "OK"
fahadmirza 0:a0c5877bd360 99 #define RET_ERROR "AT_ERROR"
fahadmirza 0:a0c5877bd360 100 #define RET_PARAM_ERROR "AT_PARAM_ERROR"
fahadmirza 0:a0c5877bd360 101 #define RET_BUSY_ERROR "AT_BUSY_ERROR"
fahadmirza 0:a0c5877bd360 102 #define RET_PARAM_OVERFLOW "AT_PARAM_OVERFLOW"
fahadmirza 0:a0c5877bd360 103 #define RET_INVALID_MODE "AT_INVALID_MODE"
fahadmirza 0:a0c5877bd360 104 #define RET_NO_NETWORK_JOINED "AT_NO_NETWORK_JOINED"
fahadmirza 0:a0c5877bd360 105 #define RET_PAYLOAD_SIZE_ERROR "AT_PAYLOAD_SIZE_ERROR"
fahadmirza 0:a0c5877bd360 106
fahadmirza 0:a0c5877bd360 107
fahadmirza 0:a0c5877bd360 108 /* Private Variables --------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 109 static const sAtRetCode_t AT_RetCode[] = {
fahadmirza 0:a0c5877bd360 110 {RET_OK, sizeof(RET_OK), AT_OK},
fahadmirza 0:a0c5877bd360 111 {RET_ERROR, sizeof(RET_ERROR), AT_ERROR},
fahadmirza 0:a0c5877bd360 112 {RET_PARAM_ERROR, sizeof(RET_PARAM_ERROR), AT_PARAM_ERROR},
fahadmirza 0:a0c5877bd360 113 {RET_NO_NETWORK_JOINED, sizeof(RET_NO_NETWORK_JOINED), AT_NO_NET_JOINED},
fahadmirza 0:a0c5877bd360 114 {RET_BUSY_ERROR, sizeof(RET_BUSY_ERROR), AT_BUSY_ERROR},
fahadmirza 0:a0c5877bd360 115 {RET_PARAM_OVERFLOW, sizeof(RET_PARAM_OVERFLOW), AT_PARAM_OVERFLOW},
fahadmirza 0:a0c5877bd360 116 {RET_INVALID_MODE, sizeof(RET_INVALID_MODE), AT_INVALID_MODE},
fahadmirza 0:a0c5877bd360 117 {RET_PAYLOAD_SIZE_ERROR, sizeof(RET_PAYLOAD_SIZE_ERROR), AT_PAYLOAD_SIZE_ERROR}};
fahadmirza 0:a0c5877bd360 118
fahadmirza 0:a0c5877bd360 119
fahadmirza 0:a0c5877bd360 120 /*
fahadmirza 0:a0c5877bd360 121 * List of AT cmd supported by the HXC Client Module:
fahadmirza 0:a0c5877bd360 122 * HXC900 and HXC400
fahadmirza 0:a0c5877bd360 123 */
fahadmirza 0:a0c5877bd360 124 static const char *CmdTab[] =
fahadmirza 0:a0c5877bd360 125 {
fahadmirza 0:a0c5877bd360 126 "", // AT
fahadmirza 0:a0c5877bd360 127 "+RESET", // Reset modem
fahadmirza 0:a0c5877bd360 128 "+FD", // Factory default
fahadmirza 0:a0c5877bd360 129 "+DEVEUI", // Device identifier
fahadmirza 0:a0c5877bd360 130 "+DEVADR", // Device address
fahadmirza 0:a0c5877bd360 131 "+APPKEY", // Application key
fahadmirza 0:a0c5877bd360 132 "+NWKSKEY", // Network session key
fahadmirza 0:a0c5877bd360 133 "+APPSKEY", // Application session key
fahadmirza 0:a0c5877bd360 134 "+APPEUI", // Application identifier
fahadmirza 0:a0c5877bd360 135 "+ADR", // Adaptive data rate
fahadmirza 0:a0c5877bd360 136 "+TXP", // Transmit power
fahadmirza 0:a0c5877bd360 137 "+DR", // Data rate
fahadmirza 0:a0c5877bd360 138 "+DCS", // DCS duty cycle settings
fahadmirza 0:a0c5877bd360 139 "+PNM", // Public/Private network
fahadmirza 0:a0c5877bd360 140 "+RX2WND", // Rx2 window frequency and datarate
fahadmirza 0:a0c5877bd360 141 "+RX1DL", // Delay of the Rx1 window
fahadmirza 0:a0c5877bd360 142 "+RX2DL", // Delay of the Rx2 window
fahadmirza 0:a0c5877bd360 143 "+JN1DL", // Join delay on Rx1 window
fahadmirza 0:a0c5877bd360 144 "+JN2DL", // Join delay on Rx2 window
fahadmirza 0:a0c5877bd360 145 "+NJM", // Network join mode
fahadmirza 0:a0c5877bd360 146 "+NWKID", // Network ID
fahadmirza 0:a0c5877bd360 147 "+FCU", // Uplink frame counter
fahadmirza 0:a0c5877bd360 148 "+FCD", // Downlink frame counter
fahadmirza 0:a0c5877bd360 149 "+CLASS", // LoRa class
fahadmirza 0:a0c5877bd360 150 "+CH", // Channel configuration
fahadmirza 0:a0c5877bd360 151 "+JOIN", // Join network server
fahadmirza 0:a0c5877bd360 152 "+NJS", // Network join mode
fahadmirza 0:a0c5877bd360 153 "+SENDB", // Send binary formatted data
fahadmirza 0:a0c5877bd360 154 "+SEND", // Send data in ASCII format
fahadmirza 0:a0c5877bd360 155 "+RECVB", // Get the last received data
fahadmirza 0:a0c5877bd360 156 "+CFS", // Confirm status
fahadmirza 0:a0c5877bd360 157 "+SNR", // Signal to noise ratio
fahadmirza 0:a0c5877bd360 158 "+RSSI", // Signal strength indicator on received radio signal
fahadmirza 0:a0c5877bd360 159 "+MODE", // Modem mode
fahadmirza 0:a0c5877bd360 160 "+RFCFG", // LoRa only configuration
fahadmirza 0:a0c5877bd360 161 "+TXCW", // Continuous Tx
fahadmirza 0:a0c5877bd360 162 "+TX", // Send LoRa only data in raw format
fahadmirza 0:a0c5877bd360 163 "+RX", // Continuous Rx
fahadmirza 0:a0c5877bd360 164 "+BAT", /// Battery level
fahadmirza 0:a0c5877bd360 165 "+VER", // Firmware version of the HXC Client
fahadmirza 0:a0c5877bd360 166 };
fahadmirza 0:a0c5877bd360 167
fahadmirza 0:a0c5877bd360 168
fahadmirza 0:a0c5877bd360 169 char LoRa_AT_Cmd_Buff[DATA_TX_MAX_BUFF_SIZE];// Buffer used for AT cmd transmission
fahadmirza 0:a0c5877bd360 170
fahadmirza 0:a0c5877bd360 171 static uint16_t Offset = 0; // Write position needed for LoRa_AT_Cmd_Buff[] during AT_SET
fahadmirza 0:a0c5877bd360 172
fahadmirza 15:2860c960d2ff 173 /* Has to be the largest of the response.
fahadmirza 15:2860c960d2ff 174 * Not only for return code but also for
fahadmirza 15:2860c960d2ff 175 * return value e.g. APPKEY.
fahadmirza 0:a0c5877bd360 176 */
fahadmirza 0:a0c5877bd360 177 static char response[DATA_RX_MAX_BUFF_SIZE];
fahadmirza 0:a0c5877bd360 178
fahadmirza 0:a0c5877bd360 179
fahadmirza 0:a0c5877bd360 180 /* Exported functions ------------------------------------------------------- */
fahadmirza 0:a0c5877bd360 181
fahadmirza 15:2860c960d2ff 182 /*******************************************************************************
fahadmirza 15:2860c960d2ff 183 * @Brief : Configures HXC UART interface, Reset Pin
fahadmirza 15:2860c960d2ff 184 * @Param : None
fahadmirza 15:2860c960d2ff 185 * @Return : AT_OK in case of success
fahadmirza 15:2860c960d2ff 186 * AT_UART_LINK_ERROR in case of UART init failure
fahadmirza 15:2860c960d2ff 187 *******************************************************************************/
fahadmirza 0:a0c5877bd360 188 eAtStatus_t Modem_Init( void )
fahadmirza 0:a0c5877bd360 189 {
fahadmirza 0:a0c5877bd360 190 if (HW_UART_Modem_Init(BAUD_RATE) == false)
fahadmirza 0:a0c5877bd360 191 {
fahadmirza 0:a0c5877bd360 192 return AT_UART_LINK_ERROR;
fahadmirza 0:a0c5877bd360 193 }
fahadmirza 0:a0c5877bd360 194
fahadmirza 0:a0c5877bd360 195 // Reset pin initialization
fahadmirza 0:a0c5877bd360 196 HXC_RESET_GPIO_CLK_ENABLE();
fahadmirza 0:a0c5877bd360 197 GPIO_InitTypeDef GPIO_InitStruct;
fahadmirza 0:a0c5877bd360 198 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 0:a0c5877bd360 199 GPIO_InitStruct.Pull = GPIO_PULLUP;
fahadmirza 0:a0c5877bd360 200 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
fahadmirza 0:a0c5877bd360 201 HW_GPIO_Init(HXC_RESET_PORT, HXC_RESET_PIN, &GPIO_InitStruct);
fahadmirza 0:a0c5877bd360 202
fahadmirza 0:a0c5877bd360 203 return AT_OK;
fahadmirza 0:a0c5877bd360 204 }
fahadmirza 0:a0c5877bd360 205
fahadmirza 0:a0c5877bd360 206
fahadmirza 15:2860c960d2ff 207 /*******************************************************************************
fahadmirza 15:2860c960d2ff 208 * @Brief : Deinitialize modem UART interface.
fahadmirza 15:2860c960d2ff 209 * @Param : None
fahadmirza 15:2860c960d2ff 210 * @Return : None
fahadmirza 15:2860c960d2ff 211 *******************************************************************************/
fahadmirza 0:a0c5877bd360 212 void Modem_IO_DeInit( void )
fahadmirza 0:a0c5877bd360 213 {
fahadmirza 0:a0c5877bd360 214 HW_UART_Modem_DeInit();
fahadmirza 0:a0c5877bd360 215 }
fahadmirza 0:a0c5877bd360 216
fahadmirza 0:a0c5877bd360 217
fahadmirza 15:2860c960d2ff 218 /*******************************************************************************
fahadmirza 15:2860c960d2ff 219 * @Brief : Handle the AT cmd following their Group type
fahadmirza 15:2860c960d2ff 220 * @Param : at_group AT group (control, set , get)
fahadmirza 15:2860c960d2ff 221 * Cmd AT command
fahadmirza 15:2860c960d2ff 222 * pdata pointer to the IN/OUT buffer
fahadmirza 15:2860c960d2ff 223 * @Return : Module status
fahadmirza 15:2860c960d2ff 224 ******************************************************************************/
fahadmirza 0:a0c5877bd360 225 eAtStatus_t Modem_AT_Cmd(ATGroup_t at_group, ATCmd_t atCmd, void *pdata )
fahadmirza 0:a0c5877bd360 226 {
fahadmirza 0:a0c5877bd360 227 eAtStatus_t atStatus = AT_END_ERROR;
fahadmirza 0:a0c5877bd360 228 uint16_t atCmdLen;
fahadmirza 0:a0c5877bd360 229
fahadmirza 0:a0c5877bd360 230 // Reset At_cmd buffer for each transmission
fahadmirza 0:a0c5877bd360 231 memset1((uint8_t *)LoRa_AT_Cmd_Buff, 0x00, sizeof(LoRa_AT_Cmd_Buff));
fahadmirza 15:2860c960d2ff 232
fahadmirza 0:a0c5877bd360 233 /* Reset the UART circular buffer for each transmission to make sure
fahadmirza 0:a0c5877bd360 234 * the responses we will get are for the current AT cmd. */
fahadmirza 0:a0c5877bd360 235 HW_UART_ResetBuffer();
fahadmirza 0:a0c5877bd360 236
fahadmirza 0:a0c5877bd360 237 // Format AT cmd
fahadmirza 0:a0c5877bd360 238 atCmdLen = at_cmd_format(at_group, atCmd, pdata);
fahadmirza 0:a0c5877bd360 239 if(atCmdLen == 0)
fahadmirza 0:a0c5877bd360 240 {
fahadmirza 0:a0c5877bd360 241 /*
fahadmirza 15:2860c960d2ff 242 * You are trying to use a command behavior that HXC Client doesn't
fahadmirza 15:2860c960d2ff 243 * support. Check manual for possible AT command behaviors.
fahadmirza 0:a0c5877bd360 244 */
fahadmirza 0:a0c5877bd360 245 return AT_CMD_ERROR;
fahadmirza 0:a0c5877bd360 246 }
fahadmirza 0:a0c5877bd360 247
fahadmirza 0:a0c5877bd360 248 // Send AT cmd string
fahadmirza 0:a0c5877bd360 249 if(at_cmd_send(atCmdLen) != AT_OK)
fahadmirza 0:a0c5877bd360 250 {
fahadmirza 0:a0c5877bd360 251 // Problem on UART transmission
fahadmirza 0:a0c5877bd360 252 return AT_UART_LINK_ERROR;
fahadmirza 0:a0c5877bd360 253 }
fahadmirza 0:a0c5877bd360 254
fahadmirza 0:a0c5877bd360 255 // Read response from HXC client for the AT cmd
fahadmirza 0:a0c5877bd360 256 if (at_group == AT_GET)
fahadmirza 0:a0c5877bd360 257 {
fahadmirza 0:a0c5877bd360 258 // Get the value
fahadmirza 0:a0c5877bd360 259 atStatus = at_cmd_receive(pdata);
fahadmirza 0:a0c5877bd360 260 }
fahadmirza 0:a0c5877bd360 261 else
fahadmirza 0:a0c5877bd360 262 {
fahadmirza 0:a0c5877bd360 263 // Check for the return status
fahadmirza 0:a0c5877bd360 264 atStatus = at_cmd_receive(NULL);
fahadmirza 0:a0c5877bd360 265 }
fahadmirza 0:a0c5877bd360 266
fahadmirza 0:a0c5877bd360 267 return atStatus;
fahadmirza 0:a0c5877bd360 268 }
fahadmirza 0:a0c5877bd360 269
fahadmirza 0:a0c5877bd360 270
fahadmirza 15:2860c960d2ff 271 /*******************************************************************************
fahadmirza 15:2860c960d2ff 272 * @Brief : Format the cmd in order to be send
fahadmirza 15:2860c960d2ff 273 * @Param : at_group - the behavior of AT cmd
fahadmirza 15:2860c960d2ff 274 * Cmd - AT command
fahadmirza 15:2860c960d2ff 275 * ptr - generic pointer to the IN/OUT buffer
fahadmirza 15:2860c960d2ff 276 * @Return: Length of the formated frame to be send
fahadmirza 15:2860c960d2ff 277 ******************************************************************************/
fahadmirza 0:a0c5877bd360 278 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr)
fahadmirza 0:a0c5877bd360 279 {
fahadmirza 0:a0c5877bd360 280 uint16_t len = 0; /*length of the formated command*/
fahadmirza 0:a0c5877bd360 281
fahadmirza 0:a0c5877bd360 282 switch (at_group)
fahadmirza 0:a0c5877bd360 283 {
fahadmirza 0:a0c5877bd360 284 case AT_CTRL:
fahadmirza 0:a0c5877bd360 285 {
fahadmirza 0:a0c5877bd360 286 len = AT_VPRINTF("AT%s\r\n", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 287 break;
fahadmirza 0:a0c5877bd360 288 }
fahadmirza 0:a0c5877bd360 289 case AT_GET:
fahadmirza 0:a0c5877bd360 290 {
fahadmirza 0:a0c5877bd360 291 len = AT_VPRINTF("AT%s=?\r\n", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 292 break;
fahadmirza 0:a0c5877bd360 293 }
fahadmirza 0:a0c5877bd360 294 case AT_SET:
fahadmirza 0:a0c5877bd360 295 {
fahadmirza 0:a0c5877bd360 296 len = at_set_cmd_format(Cmd, ptr);
fahadmirza 0:a0c5877bd360 297 break;
fahadmirza 0:a0c5877bd360 298 }
fahadmirza 0:a0c5877bd360 299 default:
fahadmirza 0:a0c5877bd360 300 {
fahadmirza 0:a0c5877bd360 301 break;
fahadmirza 0:a0c5877bd360 302 }
fahadmirza 0:a0c5877bd360 303 }
fahadmirza 0:a0c5877bd360 304
fahadmirza 0:a0c5877bd360 305 return len;
fahadmirza 0:a0c5877bd360 306 }
fahadmirza 0:a0c5877bd360 307
fahadmirza 0:a0c5877bd360 308
fahadmirza 15:2860c960d2ff 309 /*******************************************************************************
fahadmirza 15:2860c960d2ff 310 * @Brief : Format the at set cmd
fahadmirza 15:2860c960d2ff 311 * @Param : Cmd - AT command
fahadmirza 15:2860c960d2ff 312 * ptr - generic pointer to the IN/OUT buffer
fahadmirza 15:2860c960d2ff 313 * @Return: Length of the formated frame to be send
fahadmirza 15:2860c960d2ff 314 ******************************************************************************/
fahadmirza 0:a0c5877bd360 315 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr)
fahadmirza 0:a0c5877bd360 316 {
fahadmirza 0:a0c5877bd360 317 uint32_t value; /*for 32_02X and 32_D*/
fahadmirza 0:a0c5877bd360 318
fahadmirza 0:a0c5877bd360 319
fahadmirza 0:a0c5877bd360 320 Offset = AT_VPRINTF("AT%s=", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 321 switch (Cmd)
fahadmirza 0:a0c5877bd360 322 {
fahadmirza 0:a0c5877bd360 323 case AT_SEND:
fahadmirza 0:a0c5877bd360 324 {
fahadmirza 0:a0c5877bd360 325 sSendDataString_t *SendData = (sSendDataString_t *)ptr;
fahadmirza 0:a0c5877bd360 326 Offset += AT_VPRINTF("%d,%d:%s", SendData->Ack, SendData->Port, SendData->Buffer);
fahadmirza 0:a0c5877bd360 327 break;
fahadmirza 0:a0c5877bd360 328 }
fahadmirza 0:a0c5877bd360 329 case AT_SENDB:
fahadmirza 0:a0c5877bd360 330 {
fahadmirza 0:a0c5877bd360 331 sSendDataBinary_t *SendData = (sSendDataBinary_t *)ptr;
fahadmirza 0:a0c5877bd360 332
fahadmirza 0:a0c5877bd360 333 Offset += AT_VPRINTF("%d,%d:", SendData->Ack, SendData->Port);
fahadmirza 0:a0c5877bd360 334 unsigned i;
fahadmirza 0:a0c5877bd360 335 for (i = 0; i < SendData->DataSize; i++)
fahadmirza 0:a0c5877bd360 336 {
fahadmirza 0:a0c5877bd360 337 Offset+=AT_VPRINTF("%02x", SendData->Buffer[i]);
fahadmirza 0:a0c5877bd360 338 }
fahadmirza 0:a0c5877bd360 339
fahadmirza 0:a0c5877bd360 340 break;
fahadmirza 0:a0c5877bd360 341 }
fahadmirza 0:a0c5877bd360 342 case AT_APPKEY:
fahadmirza 0:a0c5877bd360 343 case AT_NWKSKEY:
fahadmirza 0:a0c5877bd360 344 case AT_APPSKEY:
fahadmirza 18:24c232951ff8 345 case AT_DEVADR:
fahadmirza 0:a0c5877bd360 346 {
fahadmirza 0:a0c5877bd360 347 char *key = (char*) ptr;
fahadmirza 0:a0c5877bd360 348 Offset += AT_VPRINTF("%s", key);
fahadmirza 0:a0c5877bd360 349 break;
fahadmirza 0:a0c5877bd360 350 }
fahadmirza 0:a0c5877bd360 351 case AT_APPEUI:
fahadmirza 0:a0c5877bd360 352 case AT_DEVEUI:
fahadmirza 0:a0c5877bd360 353 {
fahadmirza 0:a0c5877bd360 354 char *eui = (char*)ptr;
fahadmirza 0:a0c5877bd360 355 Offset += AT_VPRINTF("%s", eui);
fahadmirza 0:a0c5877bd360 356 break;
fahadmirza 0:a0c5877bd360 357 }
fahadmirza 15:2860c960d2ff 358 case AT_RX1DL:
fahadmirza 0:a0c5877bd360 359 case AT_RX2DL:
fahadmirza 0:a0c5877bd360 360 case AT_JN1DL:
fahadmirza 0:a0c5877bd360 361 case AT_JN2DL:
fahadmirza 0:a0c5877bd360 362 case AT_FCU:
fahadmirza 0:a0c5877bd360 363 case AT_FCD:
fahadmirza 0:a0c5877bd360 364 {
fahadmirza 0:a0c5877bd360 365 value = *(uint32_t*)ptr;
fahadmirza 0:a0c5877bd360 366 Offset += AT_VPRINTF("%u", value);
fahadmirza 0:a0c5877bd360 367 break;
fahadmirza 0:a0c5877bd360 368 }
fahadmirza 0:a0c5877bd360 369 case AT_DR:
fahadmirza 0:a0c5877bd360 370 case AT_TXP:
fahadmirza 0:a0c5877bd360 371 case AT_PNM:
fahadmirza 0:a0c5877bd360 372 case AT_DCS:
fahadmirza 0:a0c5877bd360 373 case AT_ADR:
fahadmirza 0:a0c5877bd360 374 case AT_BAT:
fahadmirza 0:a0c5877bd360 375 {
fahadmirza 0:a0c5877bd360 376 uint8_t value_8 = *(uint8_t*)ptr;
fahadmirza 0:a0c5877bd360 377 Offset += AT_VPRINTF("%d", value_8);
fahadmirza 0:a0c5877bd360 378 break;
fahadmirza 0:a0c5877bd360 379 }
fahadmirza 0:a0c5877bd360 380 case AT_CLASS:
fahadmirza 0:a0c5877bd360 381 {
fahadmirza 0:a0c5877bd360 382 char value_c = *(char*)ptr;
fahadmirza 0:a0c5877bd360 383 Offset += AT_VPRINTF("%c", value_c);
fahadmirza 0:a0c5877bd360 384 break;
fahadmirza 0:a0c5877bd360 385 }
fahadmirza 0:a0c5877bd360 386 case AT_NJM:
fahadmirza 0:a0c5877bd360 387 {
fahadmirza 0:a0c5877bd360 388 Offset += AT_VPRINTF("%s", (char*)ptr);
fahadmirza 0:a0c5877bd360 389 break;
fahadmirza 0:a0c5877bd360 390 }
fahadmirza 0:a0c5877bd360 391 default:
fahadmirza 0:a0c5877bd360 392 {
fahadmirza 0:a0c5877bd360 393 //DBG_PRINTF ("Not supported\r\n");
fahadmirza 0:a0c5877bd360 394 break;
fahadmirza 0:a0c5877bd360 395 }
fahadmirza 0:a0c5877bd360 396 }
fahadmirza 0:a0c5877bd360 397
fahadmirza 0:a0c5877bd360 398 Offset += AT_VPRINTF("\r\n");
fahadmirza 0:a0c5877bd360 399 uint16_t len = Offset;
fahadmirza 0:a0c5877bd360 400 Offset = 0;
fahadmirza 0:a0c5877bd360 401
fahadmirza 0:a0c5877bd360 402 return len;
fahadmirza 0:a0c5877bd360 403 }
fahadmirza 0:a0c5877bd360 404
fahadmirza 0:a0c5877bd360 405
fahadmirza 15:2860c960d2ff 406 /*******************************************************************************
fahadmirza 15:2860c960d2ff 407 * @Brief : This function sends an AT cmd to the slave device
fahadmirza 15:2860c960d2ff 408 * @Param : len - length of the AT cmd to be sent
fahadmirza 15:2860c960d2ff 409 * @Return: eAtStatus_t return code
fahadmirza 15:2860c960d2ff 410 *******************************************************************************/
fahadmirza 0:a0c5877bd360 411 static eAtStatus_t at_cmd_send(uint16_t len)
fahadmirza 0:a0c5877bd360 412 {
fahadmirza 0:a0c5877bd360 413 /*transmit the command from master to slave*/
fahadmirza 0:a0c5877bd360 414 if( HW_UART_Modem_SendBytes(LoRa_AT_Cmd_Buff, len) == false)
fahadmirza 0:a0c5877bd360 415 {
fahadmirza 0:a0c5877bd360 416 return AT_UART_LINK_ERROR;
fahadmirza 0:a0c5877bd360 417 }
fahadmirza 0:a0c5877bd360 418
fahadmirza 0:a0c5877bd360 419 return AT_OK;
fahadmirza 0:a0c5877bd360 420 }
fahadmirza 0:a0c5877bd360 421
fahadmirza 0:a0c5877bd360 422
fahadmirza 15:2860c960d2ff 423 /*******************************************************************************
fahadmirza 15:2860c960d2ff 424 * @Brief : This function receives response from the slave device
fahadmirza 15:2860c960d2ff 425 * @Param : pdata - pointer to the value returned by the slave
fahadmirza 15:2860c960d2ff 426 * @Return: Return code coming from HXC slave
fahadmirza 15:2860c960d2ff 427 *******************************************************************************/
fahadmirza 0:a0c5877bd360 428 static eAtStatus_t at_cmd_receive(void *pdata)
fahadmirza 0:a0c5877bd360 429 {
fahadmirza 0:a0c5877bd360 430 bool ResponseComplete = false;
fahadmirza 0:a0c5877bd360 431 uint8_t i = 0;
fahadmirza 0:a0c5877bd360 432 eAtStatus_t RetCode = AT_END_ERROR;
fahadmirza 0:a0c5877bd360 433
fahadmirza 0:a0c5877bd360 434 // Cleanup the response buffer
fahadmirza 0:a0c5877bd360 435 memset1((uint8_t *)response, 0x00, sizeof(response));
fahadmirza 0:a0c5877bd360 436
fahadmirza 0:a0c5877bd360 437 uint32_t currentTime = TimerGetCurrentTime();
fahadmirza 0:a0c5877bd360 438 while(ResponseComplete != true)
fahadmirza 0:a0c5877bd360 439 {
fahadmirza 0:a0c5877bd360 440 if(HW_UART_Modem_IsNewCharReceived() == false)
fahadmirza 0:a0c5877bd360 441 {
fahadmirza 0:a0c5877bd360 442 if(TimerGetElapsedTime(currentTime) > HXC_TIMEOUT)
fahadmirza 0:a0c5877bd360 443 {
fahadmirza 0:a0c5877bd360 444 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 445 RetCode = AT_TIMEOUT;
fahadmirza 0:a0c5877bd360 446 }
fahadmirza 0:a0c5877bd360 447 }
fahadmirza 0:a0c5877bd360 448 else
fahadmirza 0:a0c5877bd360 449 {
fahadmirza 0:a0c5877bd360 450 // Process the response
fahadmirza 0:a0c5877bd360 451 response[i++] = HW_UART_Modem_GetNewChar();
fahadmirza 0:a0c5877bd360 452
fahadmirza 0:a0c5877bd360 453 // Wait up to line feed marker
fahadmirza 15:2860c960d2ff 454 if (response[i - 1] == '\n')
fahadmirza 0:a0c5877bd360 455 {
fahadmirza 0:a0c5877bd360 456 // Last two bytes are <CR><LF>, set CR as NULL byte.
fahadmirza 0:a0c5877bd360 457 response[i - 2] = '\0';
fahadmirza 0:a0c5877bd360 458 // Reset index
fahadmirza 0:a0c5877bd360 459 i = 0;
fahadmirza 0:a0c5877bd360 460
fahadmirza 0:a0c5877bd360 461 // Compare response with AT statuses
fahadmirza 0:a0c5877bd360 462 RetCode = at_cmd_responseAnalysing(response);
fahadmirza 0:a0c5877bd360 463 if(RetCode != AT_END_ERROR)
fahadmirza 0:a0c5877bd360 464 {
fahadmirza 0:a0c5877bd360 465 // We received a status. Response complete.
fahadmirza 0:a0c5877bd360 466 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 467 }
fahadmirza 0:a0c5877bd360 468 else if(pdata != NULL)
fahadmirza 0:a0c5877bd360 469 {
fahadmirza 0:a0c5877bd360 470 // If pdata isn't null that means we are using GET cmd to get
fahadmirza 0:a0c5877bd360 471 // return value. Copy the return value into pdata.
fahadmirza 0:a0c5877bd360 472
fahadmirza 0:a0c5877bd360 473 strcpy(pdata, response);
fahadmirza 0:a0c5877bd360 474 memset1((uint8_t *)response, 0x00, 16);
fahadmirza 0:a0c5877bd360 475 // Now, let's get the status
fahadmirza 0:a0c5877bd360 476 }
fahadmirza 0:a0c5877bd360 477 }
fahadmirza 0:a0c5877bd360 478 else
fahadmirza 0:a0c5877bd360 479 {
fahadmirza 0:a0c5877bd360 480 if (i == (DATA_RX_MAX_BUFF_SIZE-1))
fahadmirza 0:a0c5877bd360 481 {
fahadmirza 0:a0c5877bd360 482 // Frame overflow. Reset index and stop reading.
fahadmirza 0:a0c5877bd360 483 i = 0;
fahadmirza 0:a0c5877bd360 484 RetCode = AT_PARAM_OVERFLOW;
fahadmirza 0:a0c5877bd360 485 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 486 }
fahadmirza 0:a0c5877bd360 487 }
fahadmirza 0:a0c5877bd360 488 }
fahadmirza 0:a0c5877bd360 489 } // End while(ResponseComplete != true)
fahadmirza 0:a0c5877bd360 490
fahadmirza 15:2860c960d2ff 491 HW_UART_Modem_Ready();
fahadmirza 15:2860c960d2ff 492 return RetCode;
fahadmirza 0:a0c5877bd360 493 }
fahadmirza 0:a0c5877bd360 494
fahadmirza 0:a0c5877bd360 495
fahadmirza 15:2860c960d2ff 496 /*******************************************************************************
fahadmirza 15:2860c960d2ff 497 * @Brief : Analyze the response received by the device
fahadmirza 15:2860c960d2ff 498 * @Param : response: pointer to the received response
fahadmirza 15:2860c960d2ff 499 * @Return: eAtStatus_t error type
fahadmirza 15:2860c960d2ff 500 *******************************************************************************/
fahadmirza 0:a0c5877bd360 501 static eAtStatus_t at_cmd_responseAnalysing(const char *ReturnResp)
fahadmirza 0:a0c5877bd360 502 {
fahadmirza 0:a0c5877bd360 503 int i;
fahadmirza 0:a0c5877bd360 504
fahadmirza 0:a0c5877bd360 505 for (i = 0; i < ARRAY_SIZE(AT_RetCode); i++)
fahadmirza 0:a0c5877bd360 506 {
fahadmirza 0:a0c5877bd360 507 if (strncmp(ReturnResp, AT_RetCode[i].RetCodeStr, (AT_RetCode[i].SizeRetCodeStr-1)) == 0)
fahadmirza 0:a0c5877bd360 508 {
fahadmirza 0:a0c5877bd360 509 /* Command has been found */
fahadmirza 0:a0c5877bd360 510 return AT_RetCode[i].RetCode;
fahadmirza 0:a0c5877bd360 511 }
fahadmirza 0:a0c5877bd360 512 }
fahadmirza 0:a0c5877bd360 513 return AT_END_ERROR;
fahadmirza 0:a0c5877bd360 514 }
fahadmirza 0:a0c5877bd360 515
fahadmirza 0:a0c5877bd360 516
fahadmirza 15:2860c960d2ff 517 /*******************************************************************************
fahadmirza 15:2860c960d2ff 518 * @Brief : Format the AT frame to be sent to the modem (slave)
fahadmirza 15:2860c960d2ff 519 * @Param : Pointer to the format string
fahadmirza 15:2860c960d2ff 520 * @Return: Length of the string to be sent
fahadmirza 15:2860c960d2ff 521 *******************************************************************************/
fahadmirza 0:a0c5877bd360 522 uint16_t at_cmd_vprintf(const char *format, ...)
fahadmirza 0:a0c5877bd360 523 {
fahadmirza 0:a0c5877bd360 524 va_list args;
fahadmirza 0:a0c5877bd360 525 uint16_t len;
fahadmirza 0:a0c5877bd360 526
fahadmirza 0:a0c5877bd360 527 va_start(args, format);
fahadmirza 0:a0c5877bd360 528
fahadmirza 0:a0c5877bd360 529 len = tiny_vsnprintf_like(LoRa_AT_Cmd_Buff+Offset, sizeof(LoRa_AT_Cmd_Buff)-Offset, format, args);
fahadmirza 0:a0c5877bd360 530
fahadmirza 0:a0c5877bd360 531 va_end(args);
fahadmirza 0:a0c5877bd360 532
fahadmirza 0:a0c5877bd360 533 return len;
fahadmirza 0:a0c5877bd360 534 }
fahadmirza 0:a0c5877bd360 535
fahadmirza 15:2860c960d2ff 536 /*******************************************************************************
fahadmirza 15:2860c960d2ff 537 * @Brief : Reset the HXC client modem using HXC RESET pin
fahadmirza 15:2860c960d2ff 538 * @Param : None
fahadmirza 15:2860c960d2ff 539 * @Return : Module status
fahadmirza 15:2860c960d2ff 540 ******************************************************************************/
fahadmirza 0:a0c5877bd360 541 eAtStatus_t Modem_HardReset(void)
fahadmirza 0:a0c5877bd360 542 {
fahadmirza 0:a0c5877bd360 543 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 544 DelayMs(200);
fahadmirza 0:a0c5877bd360 545 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_SET);
fahadmirza 0:a0c5877bd360 546
fahadmirza 0:a0c5877bd360 547 // Check for the return status - OK
fahadmirza 0:a0c5877bd360 548 return at_cmd_receive(NULL);
fahadmirza 0:a0c5877bd360 549 }
fahadmirza 0:a0c5877bd360 550
fahadmirza 15:2860c960d2ff 551 /*******************************************************************************
fahadmirza 15:2860c960d2ff 552 * @Brief : Check if any downlink packet is received
fahadmirza 15:2860c960d2ff 553 * @Param : None
fahadmirza 15:2860c960d2ff 554 * @Return: TRUE or FALSE
fahadmirza 15:2860c960d2ff 555 ******************************************************************************/
fahadmirza 0:a0c5877bd360 556 bool Modem_IsNewDataReceived(void)
fahadmirza 0:a0c5877bd360 557 {
fahadmirza 0:a0c5877bd360 558 if(HW_UART_Modem_IsNewCharReceived() == false)
fahadmirza 0:a0c5877bd360 559 {
fahadmirza 0:a0c5877bd360 560 return false;
fahadmirza 0:a0c5877bd360 561 }
fahadmirza 0:a0c5877bd360 562 // Cleanup the response buffer
fahadmirza 0:a0c5877bd360 563 memset1((uint8_t *)response, 0x00, sizeof(response));
fahadmirza 0:a0c5877bd360 564 HW_UART_Modem_GetCharactersUntilNewLine(response, sizeof(response), HXC_TIMEOUT);
fahadmirza 0:a0c5877bd360 565 if(strncmp("rxdata", response, 6) == 0)
fahadmirza 0:a0c5877bd360 566 {
fahadmirza 0:a0c5877bd360 567 return true;
fahadmirza 0:a0c5877bd360 568 }
fahadmirza 0:a0c5877bd360 569 return false;
fahadmirza 0:a0c5877bd360 570 }
fahadmirza 0:a0c5877bd360 571 /************************ (C) COPYRIGHT Haxiot *****END OF FILE*****/
fahadmirza 0:a0c5877bd360 572