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

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jan 24 22:18:58 2019 +0000
Revision:
37:5488257a4876
Parent:
36:1f83068add40
zxc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fahadmirza 37:5488257a4876 1 /**
fahadmirza 37:5488257a4876 2 ******************************************************************************
fahadmirza 35:2a54129232a3 3 * @file hxc_client.c
fahadmirza 35:2a54129232a3 4 * @author Fahad (Haxiot)
fahadmirza 35:2a54129232a3 5 * @version V1.0.0
fahadmirza 35:2a54129232a3 6 * @date 15-July-2018
fahadmirza 34:0cbfedd10507 7 * @brief This file provides set of firmware functions to communicate
fahadmirza 36:1f83068add40 8 * with HXC Client using AT commands:
fahadmirza 33:83cd24bbb17a 9 * - AT_SET
fahadmirza 33:83cd24bbb17a 10 * - AT_GET
fahadmirza 33:83cd24bbb17a 11 * - AT_RUN
fahadmirza 0:a0c5877bd360 12 ******************************************************************************
fahadmirza 0:a0c5877bd360 13 * @attention
fahadmirza 0:a0c5877bd360 14 *
fahadmirza 37:5488257a4876 15 * _ _ _____ _______
fahadmirza 37:5488257a4876 16 * | | | | |_ _| |__ __|
fahadmirza 37:5488257a4876 17 * | |__| | __ ___ __ | | ___ | |
fahadmirza 37:5488257a4876 18 * | __ |/ _` \ \/ / | | / _ \| |
fahadmirza 37:5488257a4876 19 * | | | | (_| |> < _| || (_) | |
fahadmirza 37:5488257a4876 20 * |_| |_|\__,_/_/\_\_____\___/|_|
fahadmirza 37:5488257a4876 21 * (C)2017 HaxIoT
fahadmirza 37:5488257a4876 22 *
fahadmirza 37:5488257a4876 23 *
fahadmirza 0:a0c5877bd360 24 * <h2><center>&copy; COPYRIGHT(c) 2017 Haxiot</center></h2>
fahadmirza 0:a0c5877bd360 25 *
fahadmirza 0:a0c5877bd360 26 * Redistribution and use in source and binary forms, with or without modification,
fahadmirza 0:a0c5877bd360 27 * are permitted provided that the following conditions are met:
fahadmirza 0:a0c5877bd360 28 * 1. Redistributions of source code must retain the above copyright notice,
fahadmirza 0:a0c5877bd360 29 * this list of conditions and the following disclaimer.
fahadmirza 0:a0c5877bd360 30 * 2. Redistributions in binary form must reproduce the above copyright notice,
fahadmirza 0:a0c5877bd360 31 * this list of conditions and the following disclaimer in the documentation
fahadmirza 0:a0c5877bd360 32 * and/or other materials provided with the distribution.
fahadmirza 0:a0c5877bd360 33 * 3. Neither the name of Haxiot nor the names of its contributors
fahadmirza 0:a0c5877bd360 34 * may be used to endorse or promote products derived from this software
fahadmirza 0:a0c5877bd360 35 * without specific prior written permission.
fahadmirza 0:a0c5877bd360 36 *
fahadmirza 0:a0c5877bd360 37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
fahadmirza 0:a0c5877bd360 38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
fahadmirza 0:a0c5877bd360 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
fahadmirza 0:a0c5877bd360 40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
fahadmirza 0:a0c5877bd360 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
fahadmirza 0:a0c5877bd360 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
fahadmirza 0:a0c5877bd360 43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
fahadmirza 0:a0c5877bd360 44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
fahadmirza 0:a0c5877bd360 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
fahadmirza 0:a0c5877bd360 46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fahadmirza 0:a0c5877bd360 47 *
fahadmirza 0:a0c5877bd360 48 ******************************************************************************
fahadmirza 0:a0c5877bd360 49 */
fahadmirza 0:a0c5877bd360 50
fahadmirza 0:a0c5877bd360 51 /* Includes ------------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 52 #include <stdbool.h>
fahadmirza 0:a0c5877bd360 53 #include <stdint.h>
fahadmirza 0:a0c5877bd360 54 #include <stdarg.h>
fahadmirza 0:a0c5877bd360 55 #include <string.h>
fahadmirza 0:a0c5877bd360 56 #include "hw_usart.h"
fahadmirza 0:a0c5877bd360 57 #include "utilities.h"
fahadmirza 0:a0c5877bd360 58 #include "tiny_vsnprintf.h"
fahadmirza 0:a0c5877bd360 59 #include "hxc_client.h"
fahadmirza 0:a0c5877bd360 60 #include "hw_conf.h"
fahadmirza 0:a0c5877bd360 61 #include "delay.h"
fahadmirza 0:a0c5877bd360 62 #include "time_server.h"
fahadmirza 0:a0c5877bd360 63 #include "hw_gpio.h"
fahadmirza 0:a0c5877bd360 64
fahadmirza 32:2d0678039a09 65
fahadmirza 32:2d0678039a09 66 /* Private macro -------------------------------------------------------------*/
fahadmirza 32:2d0678039a09 67 #define AT_VPRINTF(...) at_cmd_vprintf(__VA_ARGS__)
fahadmirza 32:2d0678039a09 68 #define AT_VSSCANF(...) tiny_sscanf(__VA_ARGS__)
fahadmirza 32:2d0678039a09 69
fahadmirza 32:2d0678039a09 70 #define BAUD_RATE 9600U
fahadmirza 32:2d0678039a09 71 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
fahadmirza 32:2d0678039a09 72
fahadmirza 32:2d0678039a09 73 #define AT_RESPONSE_BUFF_SIZE 64U // Max size of the received buffer.
fahadmirza 32:2d0678039a09 74 #define DATA_TX_MAX_BUFF_SIZE 78U // Max size of the transmit buffer
fahadmirza 32:2d0678039a09 75 #define HXC_TIMEOUT 2000U // 2 seconds
fahadmirza 32:2d0678039a09 76
fahadmirza 32:2d0678039a09 77
fahadmirza 32:2d0678039a09 78 // These strings will be used to compare the responses return from HXC modem.
fahadmirza 32:2d0678039a09 79 // In direct relation with sAtRetCode_t
fahadmirza 32:2d0678039a09 80 #define OK "OK"
fahadmirza 32:2d0678039a09 81 #define ERROR "AT_ERROR"
fahadmirza 32:2d0678039a09 82 #define PARAM_ERROR "AT_PARAM_ERROR"
fahadmirza 32:2d0678039a09 83 #define BUSY_ERROR "AT_BUSY_ERROR"
fahadmirza 32:2d0678039a09 84 #define PARAM_OVERFLOW "AT_PARAM_OVERFLOW"
fahadmirza 32:2d0678039a09 85 #define INVALID_MODE "AT_INVALID_MODE"
fahadmirza 32:2d0678039a09 86 #define NO_NETWORK_JOINED "AT_NO_NETWORK_JOINED"
fahadmirza 32:2d0678039a09 87 #define PAYLOAD_SIZE_ERROR "AT_PAYLOAD_SIZE_ERROR"
fahadmirza 32:2d0678039a09 88
fahadmirza 32:2d0678039a09 89
fahadmirza 0:a0c5877bd360 90 /* Private typedef -----------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 91 // Type definition for return code analysis
fahadmirza 0:a0c5877bd360 92 typedef struct sAtRetCode
fahadmirza 0:a0c5877bd360 93 {
fahadmirza 32:2d0678039a09 94 char* RetCodeStr;
fahadmirza 0:a0c5877bd360 95 uint8_t SizeRetCodeStr;
fahadmirza 0:a0c5877bd360 96 eAtStatus_t RetCode;
fahadmirza 0:a0c5877bd360 97 }sAtRetCode_t;
fahadmirza 0:a0c5877bd360 98
fahadmirza 15:2860c960d2ff 99
fahadmirza 0:a0c5877bd360 100 /* Private functions ---------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 101 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr);
fahadmirza 0:a0c5877bd360 102 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr);
fahadmirza 0:a0c5877bd360 103 static eAtStatus_t at_cmd_send(uint16_t len);
fahadmirza 0:a0c5877bd360 104 static eAtStatus_t at_cmd_receive(void *pdata);
fahadmirza 32:2d0678039a09 105 static eAtStatus_t at_cmd_analyzeResponse(const char *ReturnResp);
fahadmirza 0:a0c5877bd360 106 static uint16_t at_cmd_vprintf(const char *format, ...);
fahadmirza 0:a0c5877bd360 107
fahadmirza 15:2860c960d2ff 108
fahadmirza 0:a0c5877bd360 109 /* Private Variables --------------------------------------------------------*/
fahadmirza 32:2d0678039a09 110 // NOTE: sizeof of a string take account of the NULL character too, unlike
fahadmirza 32:2d0678039a09 111 // strlen(). Hence subtract one from the sizeof.
fahadmirza 0:a0c5877bd360 112 static const sAtRetCode_t AT_RetCode[] = {
fahadmirza 32:2d0678039a09 113 {OK, sizeof(OK) - 1, AT_OK},
fahadmirza 32:2d0678039a09 114 {ERROR, sizeof(ERROR) - 1, AT_ERROR},
fahadmirza 32:2d0678039a09 115 {PARAM_ERROR, sizeof(PARAM_ERROR) - 1, AT_PARAM_ERROR},
fahadmirza 32:2d0678039a09 116 {NO_NETWORK_JOINED, sizeof(NO_NETWORK_JOINED) - 1, AT_NO_NET_JOINED},
fahadmirza 32:2d0678039a09 117 {BUSY_ERROR, sizeof(BUSY_ERROR) - 1, AT_BUSY_ERROR},
fahadmirza 32:2d0678039a09 118 {PARAM_OVERFLOW, sizeof(PARAM_OVERFLOW) - 1, AT_PARAM_OVERFLOW},
fahadmirza 32:2d0678039a09 119 {INVALID_MODE, sizeof(INVALID_MODE) - 1, AT_INVALID_MODE},
fahadmirza 32:2d0678039a09 120 {PAYLOAD_SIZE_ERROR, sizeof(PAYLOAD_SIZE_ERROR) - 1, AT_PAYLOAD_SIZE_ERROR}};
fahadmirza 0:a0c5877bd360 121
fahadmirza 0:a0c5877bd360 122
fahadmirza 0:a0c5877bd360 123 /*
fahadmirza 0:a0c5877bd360 124 * List of AT cmd supported by the HXC Client Module:
fahadmirza 0:a0c5877bd360 125 * HXC900 and HXC400
fahadmirza 0:a0c5877bd360 126 */
fahadmirza 0:a0c5877bd360 127 static const char *CmdTab[] =
fahadmirza 0:a0c5877bd360 128 {
fahadmirza 0:a0c5877bd360 129 "", // AT
fahadmirza 0:a0c5877bd360 130 "+RESET", // Reset modem
fahadmirza 0:a0c5877bd360 131 "+FD", // Factory default
fahadmirza 0:a0c5877bd360 132 "+DEVEUI", // Device identifier
fahadmirza 0:a0c5877bd360 133 "+DEVADR", // Device address
fahadmirza 0:a0c5877bd360 134 "+APPKEY", // Application key
fahadmirza 0:a0c5877bd360 135 "+NWKSKEY", // Network session key
fahadmirza 0:a0c5877bd360 136 "+APPSKEY", // Application session key
fahadmirza 0:a0c5877bd360 137 "+APPEUI", // Application identifier
fahadmirza 0:a0c5877bd360 138 "+ADR", // Adaptive data rate
fahadmirza 0:a0c5877bd360 139 "+TXP", // Transmit power
fahadmirza 0:a0c5877bd360 140 "+DR", // Data rate
fahadmirza 0:a0c5877bd360 141 "+DCS", // DCS duty cycle settings
fahadmirza 0:a0c5877bd360 142 "+PNM", // Public/Private network
fahadmirza 0:a0c5877bd360 143 "+RX2WND", // Rx2 window frequency and datarate
fahadmirza 0:a0c5877bd360 144 "+RX1DL", // Delay of the Rx1 window
fahadmirza 0:a0c5877bd360 145 "+RX2DL", // Delay of the Rx2 window
fahadmirza 0:a0c5877bd360 146 "+JN1DL", // Join delay on Rx1 window
fahadmirza 0:a0c5877bd360 147 "+JN2DL", // Join delay on Rx2 window
fahadmirza 0:a0c5877bd360 148 "+NJM", // Network join mode
fahadmirza 0:a0c5877bd360 149 "+NWKID", // Network ID
fahadmirza 0:a0c5877bd360 150 "+FCU", // Uplink frame counter
fahadmirza 0:a0c5877bd360 151 "+FCD", // Downlink frame counter
fahadmirza 0:a0c5877bd360 152 "+CLASS", // LoRa class
fahadmirza 0:a0c5877bd360 153 "+CH", // Channel configuration
fahadmirza 0:a0c5877bd360 154 "+JOIN", // Join network server
fahadmirza 0:a0c5877bd360 155 "+NJS", // Network join mode
fahadmirza 0:a0c5877bd360 156 "+SENDB", // Send binary formatted data
fahadmirza 0:a0c5877bd360 157 "+SEND", // Send data in ASCII format
fahadmirza 0:a0c5877bd360 158 "+RECVB", // Get the last received data
fahadmirza 0:a0c5877bd360 159 "+CFS", // Confirm status
fahadmirza 0:a0c5877bd360 160 "+SNR", // Signal to noise ratio
fahadmirza 0:a0c5877bd360 161 "+RSSI", // Signal strength indicator on received radio signal
fahadmirza 0:a0c5877bd360 162 "+MODE", // Modem mode
fahadmirza 0:a0c5877bd360 163 "+RFCFG", // LoRa only configuration
fahadmirza 0:a0c5877bd360 164 "+TXCW", // Continuous Tx
fahadmirza 0:a0c5877bd360 165 "+TX", // Send LoRa only data in raw format
fahadmirza 0:a0c5877bd360 166 "+RX", // Continuous Rx
fahadmirza 32:2d0678039a09 167 "+BAT", // Battery level
fahadmirza 0:a0c5877bd360 168 "+VER", // Firmware version of the HXC Client
fahadmirza 0:a0c5877bd360 169 };
fahadmirza 0:a0c5877bd360 170
fahadmirza 0:a0c5877bd360 171
fahadmirza 32:2d0678039a09 172 static char AtCmdBuff[DATA_TX_MAX_BUFF_SIZE];
fahadmirza 32:2d0678039a09 173 // Write position needed for AtCmdBuff[] during AT_SET
fahadmirza 32:2d0678039a09 174 static uint16_t Offset = 0;
fahadmirza 0:a0c5877bd360 175
fahadmirza 32:2d0678039a09 176 // Has to be the largest of the response e.g. APPKEY.
fahadmirza 32:2d0678039a09 177 static char AtResponseBuff[AT_RESPONSE_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 32:2d0678039a09 231 memset1((uint8_t *)AtCmdBuff, 0x00, DATA_TX_MAX_BUFF_SIZE);
fahadmirza 15:2860c960d2ff 232
fahadmirza 32:2d0678039a09 233 // Reset the UART circular buffer for each transmission to make sure
fahadmirza 32:2d0678039a09 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 32:2d0678039a09 241 // You are trying to use a command behavior that HXC Client doesn't
fahadmirza 32:2d0678039a09 242 // support. Check manual for possible AT command behaviors.
fahadmirza 0:a0c5877bd360 243 return AT_CMD_ERROR;
fahadmirza 0:a0c5877bd360 244 }
fahadmirza 0:a0c5877bd360 245
fahadmirza 0:a0c5877bd360 246 // Send AT cmd string
fahadmirza 0:a0c5877bd360 247 if(at_cmd_send(atCmdLen) != AT_OK)
fahadmirza 0:a0c5877bd360 248 {
fahadmirza 0:a0c5877bd360 249 return AT_UART_LINK_ERROR;
fahadmirza 0:a0c5877bd360 250 }
fahadmirza 0:a0c5877bd360 251
fahadmirza 0:a0c5877bd360 252 // Read response from HXC client for the AT cmd
fahadmirza 0:a0c5877bd360 253 if (at_group == AT_GET)
fahadmirza 0:a0c5877bd360 254 {
fahadmirza 0:a0c5877bd360 255 // Get the value
fahadmirza 0:a0c5877bd360 256 atStatus = at_cmd_receive(pdata);
fahadmirza 0:a0c5877bd360 257 }
fahadmirza 0:a0c5877bd360 258 else
fahadmirza 0:a0c5877bd360 259 {
fahadmirza 0:a0c5877bd360 260 // Check for the return status
fahadmirza 0:a0c5877bd360 261 atStatus = at_cmd_receive(NULL);
fahadmirza 0:a0c5877bd360 262 }
fahadmirza 0:a0c5877bd360 263
fahadmirza 0:a0c5877bd360 264 return atStatus;
fahadmirza 0:a0c5877bd360 265 }
fahadmirza 0:a0c5877bd360 266
fahadmirza 0:a0c5877bd360 267
fahadmirza 15:2860c960d2ff 268 /*******************************************************************************
fahadmirza 15:2860c960d2ff 269 * @Brief : Format the cmd in order to be send
fahadmirza 15:2860c960d2ff 270 * @Param : at_group - the behavior of AT cmd
fahadmirza 15:2860c960d2ff 271 * Cmd - AT command
fahadmirza 15:2860c960d2ff 272 * ptr - generic pointer to the IN/OUT buffer
fahadmirza 15:2860c960d2ff 273 * @Return: Length of the formated frame to be send
fahadmirza 15:2860c960d2ff 274 ******************************************************************************/
fahadmirza 0:a0c5877bd360 275 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr)
fahadmirza 0:a0c5877bd360 276 {
fahadmirza 0:a0c5877bd360 277 uint16_t len = 0; /*length of the formated command*/
fahadmirza 0:a0c5877bd360 278
fahadmirza 0:a0c5877bd360 279 switch (at_group)
fahadmirza 0:a0c5877bd360 280 {
fahadmirza 0:a0c5877bd360 281 case AT_CTRL:
fahadmirza 0:a0c5877bd360 282 {
fahadmirza 0:a0c5877bd360 283 len = AT_VPRINTF("AT%s\r\n", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 284 break;
fahadmirza 0:a0c5877bd360 285 }
fahadmirza 0:a0c5877bd360 286 case AT_GET:
fahadmirza 0:a0c5877bd360 287 {
fahadmirza 0:a0c5877bd360 288 len = AT_VPRINTF("AT%s=?\r\n", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 289 break;
fahadmirza 0:a0c5877bd360 290 }
fahadmirza 0:a0c5877bd360 291 case AT_SET:
fahadmirza 0:a0c5877bd360 292 {
fahadmirza 0:a0c5877bd360 293 len = at_set_cmd_format(Cmd, ptr);
fahadmirza 0:a0c5877bd360 294 break;
fahadmirza 0:a0c5877bd360 295 }
fahadmirza 0:a0c5877bd360 296 default:
fahadmirza 0:a0c5877bd360 297 {
fahadmirza 0:a0c5877bd360 298 break;
fahadmirza 0:a0c5877bd360 299 }
fahadmirza 0:a0c5877bd360 300 }
fahadmirza 0:a0c5877bd360 301
fahadmirza 0:a0c5877bd360 302 return len;
fahadmirza 0:a0c5877bd360 303 }
fahadmirza 0:a0c5877bd360 304
fahadmirza 0:a0c5877bd360 305
fahadmirza 15:2860c960d2ff 306 /*******************************************************************************
fahadmirza 15:2860c960d2ff 307 * @Brief : Format the at set cmd
fahadmirza 15:2860c960d2ff 308 * @Param : Cmd - AT command
fahadmirza 15:2860c960d2ff 309 * ptr - generic pointer to the IN/OUT buffer
fahadmirza 15:2860c960d2ff 310 * @Return: Length of the formated frame to be send
fahadmirza 15:2860c960d2ff 311 ******************************************************************************/
fahadmirza 0:a0c5877bd360 312 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr)
fahadmirza 0:a0c5877bd360 313 {
fahadmirza 0:a0c5877bd360 314 uint32_t value; /*for 32_02X and 32_D*/
fahadmirza 0:a0c5877bd360 315
fahadmirza 0:a0c5877bd360 316
fahadmirza 0:a0c5877bd360 317 Offset = AT_VPRINTF("AT%s=", CmdTab[Cmd]);
fahadmirza 0:a0c5877bd360 318 switch (Cmd)
fahadmirza 0:a0c5877bd360 319 {
fahadmirza 0:a0c5877bd360 320 case AT_SEND:
fahadmirza 0:a0c5877bd360 321 {
fahadmirza 0:a0c5877bd360 322 sSendDataString_t *SendData = (sSendDataString_t *)ptr;
fahadmirza 0:a0c5877bd360 323 Offset += AT_VPRINTF("%d,%d:%s", SendData->Ack, SendData->Port, SendData->Buffer);
fahadmirza 0:a0c5877bd360 324 break;
fahadmirza 0:a0c5877bd360 325 }
fahadmirza 0:a0c5877bd360 326 case AT_SENDB:
fahadmirza 0:a0c5877bd360 327 {
fahadmirza 0:a0c5877bd360 328 sSendDataBinary_t *SendData = (sSendDataBinary_t *)ptr;
fahadmirza 0:a0c5877bd360 329
fahadmirza 0:a0c5877bd360 330 Offset += AT_VPRINTF("%d,%d:", SendData->Ack, SendData->Port);
fahadmirza 0:a0c5877bd360 331 unsigned i;
fahadmirza 0:a0c5877bd360 332 for (i = 0; i < SendData->DataSize; i++)
fahadmirza 0:a0c5877bd360 333 {
fahadmirza 0:a0c5877bd360 334 Offset+=AT_VPRINTF("%02x", SendData->Buffer[i]);
fahadmirza 0:a0c5877bd360 335 }
fahadmirza 0:a0c5877bd360 336
fahadmirza 0:a0c5877bd360 337 break;
fahadmirza 0:a0c5877bd360 338 }
fahadmirza 0:a0c5877bd360 339 case AT_APPKEY:
fahadmirza 0:a0c5877bd360 340 case AT_NWKSKEY:
fahadmirza 0:a0c5877bd360 341 case AT_APPSKEY:
fahadmirza 18:24c232951ff8 342 case AT_DEVADR:
fahadmirza 21:f1d561ed31a1 343 case AT_APPEUI:
fahadmirza 21:f1d561ed31a1 344 case AT_DEVEUI:
fahadmirza 0:a0c5877bd360 345 {
fahadmirza 0:a0c5877bd360 346 char *key = (char*) ptr;
fahadmirza 0:a0c5877bd360 347 Offset += AT_VPRINTF("%s", key);
fahadmirza 21:f1d561ed31a1 348
fahadmirza 0:a0c5877bd360 349 break;
fahadmirza 0:a0c5877bd360 350 }
fahadmirza 15:2860c960d2ff 351 case AT_RX1DL:
fahadmirza 0:a0c5877bd360 352 case AT_RX2DL:
fahadmirza 0:a0c5877bd360 353 case AT_JN1DL:
fahadmirza 0:a0c5877bd360 354 case AT_JN2DL:
fahadmirza 0:a0c5877bd360 355 case AT_FCU:
fahadmirza 0:a0c5877bd360 356 case AT_FCD:
fahadmirza 0:a0c5877bd360 357 {
fahadmirza 0:a0c5877bd360 358 value = *(uint32_t*)ptr;
fahadmirza 0:a0c5877bd360 359 Offset += AT_VPRINTF("%u", value);
fahadmirza 0:a0c5877bd360 360 break;
fahadmirza 0:a0c5877bd360 361 }
fahadmirza 0:a0c5877bd360 362 case AT_DR:
fahadmirza 0:a0c5877bd360 363 case AT_TXP:
fahadmirza 0:a0c5877bd360 364 case AT_PNM:
fahadmirza 0:a0c5877bd360 365 case AT_DCS:
fahadmirza 0:a0c5877bd360 366 case AT_ADR:
fahadmirza 0:a0c5877bd360 367 case AT_BAT:
fahadmirza 0:a0c5877bd360 368 {
fahadmirza 0:a0c5877bd360 369 uint8_t value_8 = *(uint8_t*)ptr;
fahadmirza 0:a0c5877bd360 370 Offset += AT_VPRINTF("%d", value_8);
fahadmirza 0:a0c5877bd360 371 break;
fahadmirza 0:a0c5877bd360 372 }
fahadmirza 0:a0c5877bd360 373 case AT_CLASS:
fahadmirza 0:a0c5877bd360 374 {
fahadmirza 0:a0c5877bd360 375 char value_c = *(char*)ptr;
fahadmirza 0:a0c5877bd360 376 Offset += AT_VPRINTF("%c", value_c);
fahadmirza 0:a0c5877bd360 377 break;
fahadmirza 0:a0c5877bd360 378 }
fahadmirza 0:a0c5877bd360 379 case AT_NJM:
fahadmirza 0:a0c5877bd360 380 {
fahadmirza 0:a0c5877bd360 381 Offset += AT_VPRINTF("%s", (char*)ptr);
fahadmirza 0:a0c5877bd360 382 break;
fahadmirza 0:a0c5877bd360 383 }
fahadmirza 0:a0c5877bd360 384 default:
fahadmirza 0:a0c5877bd360 385 {
fahadmirza 0:a0c5877bd360 386 //DBG_PRINTF ("Not supported\r\n");
fahadmirza 0:a0c5877bd360 387 break;
fahadmirza 0:a0c5877bd360 388 }
fahadmirza 0:a0c5877bd360 389 }
fahadmirza 0:a0c5877bd360 390
fahadmirza 0:a0c5877bd360 391 Offset += AT_VPRINTF("\r\n");
fahadmirza 0:a0c5877bd360 392 uint16_t len = Offset;
fahadmirza 0:a0c5877bd360 393 Offset = 0;
fahadmirza 0:a0c5877bd360 394
fahadmirza 0:a0c5877bd360 395 return len;
fahadmirza 0:a0c5877bd360 396 }
fahadmirza 0:a0c5877bd360 397
fahadmirza 0:a0c5877bd360 398
fahadmirza 15:2860c960d2ff 399 /*******************************************************************************
fahadmirza 15:2860c960d2ff 400 * @Brief : This function sends an AT cmd to the slave device
fahadmirza 15:2860c960d2ff 401 * @Param : len - length of the AT cmd to be sent
fahadmirza 15:2860c960d2ff 402 * @Return: eAtStatus_t return code
fahadmirza 15:2860c960d2ff 403 *******************************************************************************/
fahadmirza 0:a0c5877bd360 404 static eAtStatus_t at_cmd_send(uint16_t len)
fahadmirza 0:a0c5877bd360 405 {
fahadmirza 0:a0c5877bd360 406 /*transmit the command from master to slave*/
fahadmirza 32:2d0678039a09 407 if( HW_UART_Modem_SendBytes(AtCmdBuff, len) == false)
fahadmirza 0:a0c5877bd360 408 {
fahadmirza 0:a0c5877bd360 409 return AT_UART_LINK_ERROR;
fahadmirza 0:a0c5877bd360 410 }
fahadmirza 0:a0c5877bd360 411
fahadmirza 0:a0c5877bd360 412 return AT_OK;
fahadmirza 0:a0c5877bd360 413 }
fahadmirza 0:a0c5877bd360 414
fahadmirza 0:a0c5877bd360 415
fahadmirza 15:2860c960d2ff 416 /*******************************************************************************
fahadmirza 15:2860c960d2ff 417 * @Brief : This function receives response from the slave device
fahadmirza 15:2860c960d2ff 418 * @Param : pdata - pointer to the value returned by the slave
fahadmirza 15:2860c960d2ff 419 * @Return: Return code coming from HXC slave
fahadmirza 15:2860c960d2ff 420 *******************************************************************************/
fahadmirza 0:a0c5877bd360 421 static eAtStatus_t at_cmd_receive(void *pdata)
fahadmirza 0:a0c5877bd360 422 {
fahadmirza 0:a0c5877bd360 423 bool ResponseComplete = false;
fahadmirza 0:a0c5877bd360 424 uint8_t i = 0;
fahadmirza 0:a0c5877bd360 425 eAtStatus_t RetCode = AT_END_ERROR;
fahadmirza 0:a0c5877bd360 426
fahadmirza 0:a0c5877bd360 427 // Cleanup the response buffer
fahadmirza 32:2d0678039a09 428 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
fahadmirza 0:a0c5877bd360 429
fahadmirza 0:a0c5877bd360 430 uint32_t currentTime = TimerGetCurrentTime();
fahadmirza 0:a0c5877bd360 431 while(ResponseComplete != true)
fahadmirza 0:a0c5877bd360 432 {
fahadmirza 0:a0c5877bd360 433 if(HW_UART_Modem_IsNewCharReceived() == false)
fahadmirza 0:a0c5877bd360 434 {
fahadmirza 0:a0c5877bd360 435 if(TimerGetElapsedTime(currentTime) > HXC_TIMEOUT)
fahadmirza 0:a0c5877bd360 436 {
fahadmirza 0:a0c5877bd360 437 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 438 RetCode = AT_TIMEOUT;
fahadmirza 0:a0c5877bd360 439 }
fahadmirza 0:a0c5877bd360 440 }
fahadmirza 0:a0c5877bd360 441 else
fahadmirza 0:a0c5877bd360 442 {
fahadmirza 32:2d0678039a09 443 AtResponseBuff[i++] = HW_UART_Modem_GetNewChar();
fahadmirza 0:a0c5877bd360 444
fahadmirza 0:a0c5877bd360 445 // Wait up to line feed marker
fahadmirza 32:2d0678039a09 446 if (AtResponseBuff[i - 1] == '\n')
fahadmirza 0:a0c5877bd360 447 {
fahadmirza 0:a0c5877bd360 448 // Last two bytes are <CR><LF>, set CR as NULL byte.
fahadmirza 32:2d0678039a09 449 AtResponseBuff[i - 2] = '\0';
fahadmirza 0:a0c5877bd360 450 i = 0;
fahadmirza 0:a0c5877bd360 451
fahadmirza 32:2d0678039a09 452 RetCode = at_cmd_analyzeResponse(AtResponseBuff);
fahadmirza 0:a0c5877bd360 453 if(RetCode != AT_END_ERROR)
fahadmirza 0:a0c5877bd360 454 {
fahadmirza 0:a0c5877bd360 455 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 456 }
fahadmirza 0:a0c5877bd360 457 else if(pdata != NULL)
fahadmirza 0:a0c5877bd360 458 {
fahadmirza 0:a0c5877bd360 459 // If pdata isn't null that means we are using GET cmd to get
fahadmirza 0:a0c5877bd360 460 // return value. Copy the return value into pdata.
fahadmirza 0:a0c5877bd360 461
fahadmirza 32:2d0678039a09 462 strcpy(pdata, AtResponseBuff);
fahadmirza 32:2d0678039a09 463 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
fahadmirza 0:a0c5877bd360 464 // Now, let's get the status
fahadmirza 0:a0c5877bd360 465 }
fahadmirza 0:a0c5877bd360 466 }
fahadmirza 0:a0c5877bd360 467 else
fahadmirza 0:a0c5877bd360 468 {
fahadmirza 32:2d0678039a09 469 if (i == (AT_RESPONSE_BUFF_SIZE - 1))
fahadmirza 0:a0c5877bd360 470 {
fahadmirza 0:a0c5877bd360 471 // Frame overflow. Reset index and stop reading.
fahadmirza 0:a0c5877bd360 472 i = 0;
fahadmirza 0:a0c5877bd360 473 RetCode = AT_PARAM_OVERFLOW;
fahadmirza 0:a0c5877bd360 474 ResponseComplete = true;
fahadmirza 0:a0c5877bd360 475 }
fahadmirza 0:a0c5877bd360 476 }
fahadmirza 0:a0c5877bd360 477 }
fahadmirza 0:a0c5877bd360 478 } // End while(ResponseComplete != true)
fahadmirza 0:a0c5877bd360 479
fahadmirza 15:2860c960d2ff 480 HW_UART_Modem_Ready();
fahadmirza 15:2860c960d2ff 481 return RetCode;
fahadmirza 0:a0c5877bd360 482 }
fahadmirza 0:a0c5877bd360 483
fahadmirza 0:a0c5877bd360 484
fahadmirza 15:2860c960d2ff 485 /*******************************************************************************
fahadmirza 15:2860c960d2ff 486 * @Brief : Analyze the response received by the device
fahadmirza 15:2860c960d2ff 487 * @Param : response: pointer to the received response
fahadmirza 15:2860c960d2ff 488 * @Return: eAtStatus_t error type
fahadmirza 15:2860c960d2ff 489 *******************************************************************************/
fahadmirza 32:2d0678039a09 490 static eAtStatus_t at_cmd_analyzeResponse(const char *ReturnResp)
fahadmirza 0:a0c5877bd360 491 {
fahadmirza 32:2d0678039a09 492 uint8_t i;
fahadmirza 0:a0c5877bd360 493
fahadmirza 0:a0c5877bd360 494 for (i = 0; i < ARRAY_SIZE(AT_RetCode); i++)
fahadmirza 0:a0c5877bd360 495 {
fahadmirza 32:2d0678039a09 496 if (strncmp(ReturnResp, AT_RetCode[i].RetCodeStr, (AT_RetCode[i].SizeRetCodeStr)) == 0)
fahadmirza 0:a0c5877bd360 497 {
fahadmirza 0:a0c5877bd360 498 /* Command has been found */
fahadmirza 0:a0c5877bd360 499 return AT_RetCode[i].RetCode;
fahadmirza 0:a0c5877bd360 500 }
fahadmirza 0:a0c5877bd360 501 }
fahadmirza 0:a0c5877bd360 502 return AT_END_ERROR;
fahadmirza 0:a0c5877bd360 503 }
fahadmirza 0:a0c5877bd360 504
fahadmirza 0:a0c5877bd360 505
fahadmirza 15:2860c960d2ff 506 /*******************************************************************************
fahadmirza 15:2860c960d2ff 507 * @Brief : Format the AT frame to be sent to the modem (slave)
fahadmirza 15:2860c960d2ff 508 * @Param : Pointer to the format string
fahadmirza 15:2860c960d2ff 509 * @Return: Length of the string to be sent
fahadmirza 15:2860c960d2ff 510 *******************************************************************************/
fahadmirza 32:2d0678039a09 511 static uint16_t at_cmd_vprintf(const char *format, ...)
fahadmirza 0:a0c5877bd360 512 {
fahadmirza 0:a0c5877bd360 513 va_list args;
fahadmirza 0:a0c5877bd360 514 uint16_t len;
fahadmirza 0:a0c5877bd360 515
fahadmirza 0:a0c5877bd360 516 va_start(args, format);
fahadmirza 0:a0c5877bd360 517
fahadmirza 32:2d0678039a09 518 len = tiny_vsnprintf_like(AtCmdBuff + Offset, DATA_TX_MAX_BUFF_SIZE - Offset, format, args);
fahadmirza 0:a0c5877bd360 519
fahadmirza 0:a0c5877bd360 520 va_end(args);
fahadmirza 0:a0c5877bd360 521
fahadmirza 0:a0c5877bd360 522 return len;
fahadmirza 0:a0c5877bd360 523 }
fahadmirza 0:a0c5877bd360 524
fahadmirza 15:2860c960d2ff 525 /*******************************************************************************
fahadmirza 15:2860c960d2ff 526 * @Brief : Reset the HXC client modem using HXC RESET pin
fahadmirza 15:2860c960d2ff 527 * @Param : None
fahadmirza 15:2860c960d2ff 528 * @Return : Module status
fahadmirza 15:2860c960d2ff 529 ******************************************************************************/
fahadmirza 0:a0c5877bd360 530 eAtStatus_t Modem_HardReset(void)
fahadmirza 0:a0c5877bd360 531 {
fahadmirza 0:a0c5877bd360 532 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 533 DelayMs(200);
fahadmirza 0:a0c5877bd360 534 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_SET);
fahadmirza 0:a0c5877bd360 535
fahadmirza 0:a0c5877bd360 536 // Check for the return status - OK
fahadmirza 0:a0c5877bd360 537 return at_cmd_receive(NULL);
fahadmirza 0:a0c5877bd360 538 }
fahadmirza 0:a0c5877bd360 539
fahadmirza 15:2860c960d2ff 540 /*******************************************************************************
fahadmirza 15:2860c960d2ff 541 * @Brief : Check if any downlink packet is received
fahadmirza 15:2860c960d2ff 542 * @Param : None
fahadmirza 15:2860c960d2ff 543 * @Return: TRUE or FALSE
fahadmirza 15:2860c960d2ff 544 ******************************************************************************/
fahadmirza 0:a0c5877bd360 545 bool Modem_IsNewDataReceived(void)
fahadmirza 0:a0c5877bd360 546 {
fahadmirza 0:a0c5877bd360 547 if(HW_UART_Modem_IsNewCharReceived() == false)
fahadmirza 0:a0c5877bd360 548 {
fahadmirza 0:a0c5877bd360 549 return false;
fahadmirza 0:a0c5877bd360 550 }
fahadmirza 0:a0c5877bd360 551 // Cleanup the response buffer
fahadmirza 32:2d0678039a09 552 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
fahadmirza 32:2d0678039a09 553
fahadmirza 32:2d0678039a09 554 HW_UART_Modem_GetCharactersUntilNewLine(AtResponseBuff, AT_RESPONSE_BUFF_SIZE, HXC_TIMEOUT);
fahadmirza 32:2d0678039a09 555
fahadmirza 32:2d0678039a09 556 if(strncmp("rxdata", AtResponseBuff, 6) == 0)
fahadmirza 0:a0c5877bd360 557 {
fahadmirza 0:a0c5877bd360 558 return true;
fahadmirza 0:a0c5877bd360 559 }
fahadmirza 0:a0c5877bd360 560 return false;
fahadmirza 0:a0c5877bd360 561 }
fahadmirza 32:2d0678039a09 562
fahadmirza 32:2d0678039a09 563 char* Modem_GetResponseBuffer(void)
fahadmirza 32:2d0678039a09 564 {
fahadmirza 32:2d0678039a09 565 return AtResponseBuff;
fahadmirza 32:2d0678039a09 566 }
fahadmirza 0:a0c5877bd360 567 /************************ (C) COPYRIGHT Haxiot *****END OF FILE*****/
fahadmirza 0:a0c5877bd360 568