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

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jan 24 22:14:32 2019 +0000
Revision:
34:0cbfedd10507
Parent:
33:83cd24bbb17a
Child:
35:2a54129232a3
test

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