Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Thu Dec 06 11:40:19 2018 +0000
Revision:
8:45a0205a298f
Parent:
6:be97d38e0b01
Backing up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:09f915e6f9f6 1 #include "mbed.h"
andrewboyson 2:06fa34661f19 2 #include "log.h"
andrewboyson 2:06fa34661f19 3 #include "esp.h"
andrewboyson 2:06fa34661f19 4 #include "io.h"
andrewboyson 2:06fa34661f19 5 #include "cfg.h"
andrewboyson 3:accba7e07a0d 6 #include "uart.h"
andrewboyson 0:09f915e6f9f6 7
andrewboyson 0:09f915e6f9f6 8 #define RECV_TIMEOUT_MS 5000
andrewboyson 0:09f915e6f9f6 9
andrewboyson 0:09f915e6f9f6 10 //State
andrewboyson 0:09f915e6f9f6 11 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 12 #define IDLE 0
andrewboyson 0:09f915e6f9f6 13 #define IN_LINE 1
andrewboyson 0:09f915e6f9f6 14 #define IPD_WAIT_START 2
andrewboyson 0:09f915e6f9f6 15 #define IPD_READ 3
andrewboyson 0:09f915e6f9f6 16 #define SEND_DATA 4
andrewboyson 0:09f915e6f9f6 17 static int state; //Initialised in init
andrewboyson 0:09f915e6f9f6 18
andrewboyson 0:09f915e6f9f6 19 //Solicited responses received
andrewboyson 0:09f915e6f9f6 20 //============================
andrewboyson 0:09f915e6f9f6 21 #define LINE_LENGTH 256
andrewboyson 0:09f915e6f9f6 22 int EspLineAvailable; //Initialised in init; can be one of the values defined in esp.h
andrewboyson 0:09f915e6f9f6 23 char EspLine[LINE_LENGTH];
andrewboyson 2:06fa34661f19 24 static char* pLineNext; //Initialised in init to EspLine
andrewboyson 2:06fa34661f19 25
andrewboyson 2:06fa34661f19 26 static int addRawCharToBuffer(char* pBuff, char** ppNext, int len, char c)
andrewboyson 0:09f915e6f9f6 27 {
andrewboyson 2:06fa34661f19 28
andrewboyson 2:06fa34661f19 29 // if *ppNext is at the last position in pBuff (pBuff+len-1) then we are full and should stop
andrewboyson 2:06fa34661f19 30 if (*ppNext >= pBuff + len - 1) return -1;
andrewboyson 2:06fa34661f19 31
andrewboyson 2:06fa34661f19 32 // Put the char into *ppNext and NUL into *ppNext + 1.
andrewboyson 2:06fa34661f19 33 **ppNext = c;
andrewboyson 2:06fa34661f19 34 ++*ppNext;
andrewboyson 2:06fa34661f19 35 **ppNext = '\0';
andrewboyson 2:06fa34661f19 36
andrewboyson 2:06fa34661f19 37 return 0;
andrewboyson 2:06fa34661f19 38 }
andrewboyson 2:06fa34661f19 39 static int addCharToBuffer(char* pBuff, char** ppNext, int len, char c, int includeCrLf)
andrewboyson 2:06fa34661f19 40 {
andrewboyson 2:06fa34661f19 41 if (!pBuff) return -1;
andrewboyson 0:09f915e6f9f6 42 switch (c)
andrewboyson 0:09f915e6f9f6 43 {
andrewboyson 2:06fa34661f19 44 case '\0':
andrewboyson 2:06fa34661f19 45 if (addRawCharToBuffer(pBuff, ppNext, len, '\\')) return -1;
andrewboyson 2:06fa34661f19 46 if (addRawCharToBuffer(pBuff, ppNext, len, '0' )) return -1; //This is the character zero '0' not NUL '\0'
andrewboyson 0:09f915e6f9f6 47 break;
andrewboyson 0:09f915e6f9f6 48 case '\r':
andrewboyson 0:09f915e6f9f6 49 case '\n':
andrewboyson 2:06fa34661f19 50 if (includeCrLf && addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 51 break;
andrewboyson 0:09f915e6f9f6 52 default:
andrewboyson 2:06fa34661f19 53 if (addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
andrewboyson 0:09f915e6f9f6 54 break;
andrewboyson 0:09f915e6f9f6 55 }
andrewboyson 0:09f915e6f9f6 56 return 0;
andrewboyson 0:09f915e6f9f6 57 }
andrewboyson 2:06fa34661f19 58 static int addChar(char c)
andrewboyson 2:06fa34661f19 59 {
andrewboyson 4:e076884ef8bd 60 int r = addCharToBuffer(EspLine, &pLineNext, LINE_LENGTH, c, false);
andrewboyson 2:06fa34661f19 61 return r;
andrewboyson 2:06fa34661f19 62 }
andrewboyson 0:09f915e6f9f6 63 //Unsolicited ntp or http requests
andrewboyson 0:09f915e6f9f6 64 //================================
andrewboyson 0:09f915e6f9f6 65 void *EspIpdBuffer[4];
andrewboyson 0:09f915e6f9f6 66 int EspIpdBufferLen[4];
andrewboyson 0:09f915e6f9f6 67 int EspIpdReserved[4];
andrewboyson 0:09f915e6f9f6 68 int EspIpdLength;
andrewboyson 0:09f915e6f9f6 69 int EspIpdId;
andrewboyson 0:09f915e6f9f6 70 int EspDataAvailable;
andrewboyson 0:09f915e6f9f6 71
andrewboyson 0:09f915e6f9f6 72
andrewboyson 0:09f915e6f9f6 73 //Stuff to send
andrewboyson 0:09f915e6f9f6 74 //=============
andrewboyson 0:09f915e6f9f6 75 int EspLengthToSend;
andrewboyson 0:09f915e6f9f6 76 const void * EspDataToSend;
andrewboyson 0:09f915e6f9f6 77
andrewboyson 6:be97d38e0b01 78 void EspSendCommandF(char *fmt, ...)
andrewboyson 0:09f915e6f9f6 79 {
andrewboyson 0:09f915e6f9f6 80 va_list argptr;
andrewboyson 0:09f915e6f9f6 81 va_start(argptr, fmt);
andrewboyson 0:09f915e6f9f6 82 int size = vsnprintf(NULL, 0, fmt, argptr);
andrewboyson 0:09f915e6f9f6 83 char snd[size + 1];
andrewboyson 0:09f915e6f9f6 84 vsprintf(snd, fmt, argptr);
andrewboyson 0:09f915e6f9f6 85 va_end(argptr);
andrewboyson 6:be97d38e0b01 86 EspSendCommand(snd);
andrewboyson 0:09f915e6f9f6 87 }
andrewboyson 6:be97d38e0b01 88 void EspSendCommand(char* p)
andrewboyson 6:be97d38e0b01 89 {
andrewboyson 3:accba7e07a0d 90 while(*p) UartSendPush(*p++);
andrewboyson 0:09f915e6f9f6 91 }
andrewboyson 0:09f915e6f9f6 92 void EspSendData(int length, const void * snd)
andrewboyson 0:09f915e6f9f6 93 {
andrewboyson 0:09f915e6f9f6 94 const char* p = (char*)snd;
andrewboyson 0:09f915e6f9f6 95 const char* e = (char*)snd + length;
andrewboyson 3:accba7e07a0d 96 while (p < e) UartSendPush(*p++);
andrewboyson 0:09f915e6f9f6 97 }
andrewboyson 0:09f915e6f9f6 98
andrewboyson 0:09f915e6f9f6 99
andrewboyson 0:09f915e6f9f6 100 //Reset ESP8266 zone
andrewboyson 0:09f915e6f9f6 101 #define RESET_TIME_MS 250
andrewboyson 0:09f915e6f9f6 102 static DigitalOut espRunOnHighResetOnLow(p26);
andrewboyson 0:09f915e6f9f6 103 int reset; //Set to 1 by EspReset (and hence EspInit); set to 0 by EspReleasefromReset
andrewboyson 0:09f915e6f9f6 104 void EspResetAndStop()
andrewboyson 0:09f915e6f9f6 105 {
andrewboyson 0:09f915e6f9f6 106 reset = true;
andrewboyson 3:accba7e07a0d 107 UartReset();
andrewboyson 0:09f915e6f9f6 108 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 109 *pLineNext = '\0';
andrewboyson 0:09f915e6f9f6 110 EspLengthToSend = 0;
andrewboyson 0:09f915e6f9f6 111 EspDataToSend = NULL;
andrewboyson 0:09f915e6f9f6 112 state = IDLE;
andrewboyson 0:09f915e6f9f6 113 }
andrewboyson 0:09f915e6f9f6 114 void EspReleaseFromReset(void)
andrewboyson 0:09f915e6f9f6 115 {
andrewboyson 0:09f915e6f9f6 116 reset = false;
andrewboyson 0:09f915e6f9f6 117 }
andrewboyson 0:09f915e6f9f6 118 void pulseResetPin()
andrewboyson 0:09f915e6f9f6 119 {
andrewboyson 0:09f915e6f9f6 120 static Timer resetTimer;
andrewboyson 0:09f915e6f9f6 121
andrewboyson 0:09f915e6f9f6 122 if (reset)
andrewboyson 0:09f915e6f9f6 123 {
andrewboyson 0:09f915e6f9f6 124 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 125 resetTimer.reset();
andrewboyson 0:09f915e6f9f6 126 espRunOnHighResetOnLow = 0;
andrewboyson 0:09f915e6f9f6 127 }
andrewboyson 0:09f915e6f9f6 128 else
andrewboyson 0:09f915e6f9f6 129 {
andrewboyson 0:09f915e6f9f6 130 resetTimer.start();
andrewboyson 0:09f915e6f9f6 131 if (resetTimer.read_ms() > RESET_TIME_MS)
andrewboyson 0:09f915e6f9f6 132 {
andrewboyson 0:09f915e6f9f6 133 resetTimer.stop();
andrewboyson 0:09f915e6f9f6 134 espRunOnHighResetOnLow = 1;
andrewboyson 0:09f915e6f9f6 135 }
andrewboyson 0:09f915e6f9f6 136 }
andrewboyson 0:09f915e6f9f6 137 }
andrewboyson 0:09f915e6f9f6 138 //General commands
andrewboyson 2:06fa34661f19 139 int EspInit()
andrewboyson 0:09f915e6f9f6 140 {
andrewboyson 0:09f915e6f9f6 141 EspResetAndStop();
andrewboyson 3:accba7e07a0d 142 UartBaud(CfgBaud);
andrewboyson 0:09f915e6f9f6 143 for (int i = 0; i < 4; i++)
andrewboyson 0:09f915e6f9f6 144 {
andrewboyson 0:09f915e6f9f6 145 EspIpdBuffer[i] = NULL;
andrewboyson 0:09f915e6f9f6 146 EspIpdBufferLen[i] = 0;
andrewboyson 0:09f915e6f9f6 147 EspIpdReserved[i] = 0;
andrewboyson 0:09f915e6f9f6 148 }
andrewboyson 2:06fa34661f19 149 return 0;
andrewboyson 0:09f915e6f9f6 150 }
andrewboyson 0:09f915e6f9f6 151
andrewboyson 0:09f915e6f9f6 152 //Main loop zone
andrewboyson 0:09f915e6f9f6 153 int handleCharacter(char c)
andrewboyson 0:09f915e6f9f6 154 {
andrewboyson 0:09f915e6f9f6 155 //Static variables. Initialised on first load.
andrewboyson 0:09f915e6f9f6 156 static char ipdHeader[20];
andrewboyson 0:09f915e6f9f6 157 static char * pipdHeader; //Set to ipdHeader when a '+' is received
andrewboyson 0:09f915e6f9f6 158 static int bytesRcvd; //Set to 0 when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 159 static void * pData; //Set to EspIpdBuffer[EspIpdId] when the ':' following the +IPD is received
andrewboyson 0:09f915e6f9f6 160
andrewboyson 0:09f915e6f9f6 161 switch(state)
andrewboyson 0:09f915e6f9f6 162 {
andrewboyson 0:09f915e6f9f6 163 case IDLE:
andrewboyson 0:09f915e6f9f6 164 if (c == '+')
andrewboyson 0:09f915e6f9f6 165 {
andrewboyson 0:09f915e6f9f6 166 pipdHeader = ipdHeader;
andrewboyson 0:09f915e6f9f6 167 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 168 state = IPD_WAIT_START;
andrewboyson 0:09f915e6f9f6 169 }
andrewboyson 0:09f915e6f9f6 170 else if (c == '>')
andrewboyson 0:09f915e6f9f6 171 {
andrewboyson 0:09f915e6f9f6 172 state = SEND_DATA;
andrewboyson 0:09f915e6f9f6 173 }
andrewboyson 0:09f915e6f9f6 174 else
andrewboyson 0:09f915e6f9f6 175 {
andrewboyson 0:09f915e6f9f6 176 pLineNext = EspLine;
andrewboyson 0:09f915e6f9f6 177 *pLineNext = 0;
andrewboyson 2:06fa34661f19 178 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 179 if (r)
andrewboyson 0:09f915e6f9f6 180 {
andrewboyson 0:09f915e6f9f6 181 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 182 state = IDLE;
andrewboyson 0:09f915e6f9f6 183 }
andrewboyson 0:09f915e6f9f6 184 else
andrewboyson 0:09f915e6f9f6 185 {
andrewboyson 0:09f915e6f9f6 186 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 187 }
andrewboyson 0:09f915e6f9f6 188 }
andrewboyson 0:09f915e6f9f6 189 break;
andrewboyson 0:09f915e6f9f6 190 case IN_LINE:
andrewboyson 0:09f915e6f9f6 191 if (c == '\n')
andrewboyson 0:09f915e6f9f6 192 {
andrewboyson 0:09f915e6f9f6 193 EspLineAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 194 state = IDLE;
andrewboyson 0:09f915e6f9f6 195 }
andrewboyson 0:09f915e6f9f6 196 else
andrewboyson 0:09f915e6f9f6 197 {
andrewboyson 2:06fa34661f19 198 int r = addChar(c);
andrewboyson 0:09f915e6f9f6 199 if (r)
andrewboyson 0:09f915e6f9f6 200 {
andrewboyson 0:09f915e6f9f6 201 EspLineAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 202 state = IDLE;
andrewboyson 0:09f915e6f9f6 203 }
andrewboyson 0:09f915e6f9f6 204 }
andrewboyson 0:09f915e6f9f6 205 break;
andrewboyson 0:09f915e6f9f6 206 case IPD_WAIT_START:
andrewboyson 0:09f915e6f9f6 207 if (pipdHeader == ipdHeader && c != 'I') //If the first character after the '+' is not 'I' then start a line instead
andrewboyson 0:09f915e6f9f6 208 {
andrewboyson 0:09f915e6f9f6 209 pLineNext = EspLine;
andrewboyson 2:06fa34661f19 210 addChar('+');
andrewboyson 2:06fa34661f19 211 addChar(c);
andrewboyson 0:09f915e6f9f6 212 state = IN_LINE;
andrewboyson 0:09f915e6f9f6 213 }
andrewboyson 0:09f915e6f9f6 214 else
andrewboyson 0:09f915e6f9f6 215 {
andrewboyson 0:09f915e6f9f6 216 *pipdHeader++ = c;
andrewboyson 0:09f915e6f9f6 217 *pipdHeader = 0;
andrewboyson 0:09f915e6f9f6 218 if (c == ':')
andrewboyson 0:09f915e6f9f6 219 {
andrewboyson 0:09f915e6f9f6 220 sscanf(ipdHeader, "IPD,%d,%d:", &EspIpdId, &EspIpdLength);
andrewboyson 0:09f915e6f9f6 221 bytesRcvd = 0;
andrewboyson 0:09f915e6f9f6 222 pData = EspIpdBuffer[EspIpdId];
andrewboyson 0:09f915e6f9f6 223 state = IPD_READ;
andrewboyson 0:09f915e6f9f6 224 }
andrewboyson 0:09f915e6f9f6 225 }
andrewboyson 0:09f915e6f9f6 226 break;
andrewboyson 0:09f915e6f9f6 227 case IPD_READ:
andrewboyson 0:09f915e6f9f6 228 bytesRcvd++;
andrewboyson 0:09f915e6f9f6 229 if (bytesRcvd <= EspIpdBufferLen[EspIpdId])
andrewboyson 0:09f915e6f9f6 230 {
andrewboyson 0:09f915e6f9f6 231 *(char *)pData = c;
andrewboyson 0:09f915e6f9f6 232 pData = (char *)pData + 1;
andrewboyson 0:09f915e6f9f6 233 }
andrewboyson 0:09f915e6f9f6 234 if (bytesRcvd == EspIpdLength)
andrewboyson 0:09f915e6f9f6 235 {
andrewboyson 0:09f915e6f9f6 236 if (bytesRcvd <= EspIpdBufferLen[EspIpdId]) EspDataAvailable = ESP_AVAILABLE;
andrewboyson 0:09f915e6f9f6 237 else EspDataAvailable = ESP_OVERFLOW;
andrewboyson 0:09f915e6f9f6 238 state = IDLE;
andrewboyson 0:09f915e6f9f6 239 }
andrewboyson 0:09f915e6f9f6 240 break;
andrewboyson 0:09f915e6f9f6 241 case SEND_DATA:
andrewboyson 0:09f915e6f9f6 242 EspSendData(EspLengthToSend, EspDataToSend);
andrewboyson 0:09f915e6f9f6 243 state = IDLE;
andrewboyson 0:09f915e6f9f6 244 break;
andrewboyson 0:09f915e6f9f6 245 default:
andrewboyson 0:09f915e6f9f6 246 LogF("Unknown state %d\n\r", state);
andrewboyson 0:09f915e6f9f6 247 return -1;
andrewboyson 0:09f915e6f9f6 248 }
andrewboyson 0:09f915e6f9f6 249 return 0;
andrewboyson 0:09f915e6f9f6 250 }
andrewboyson 0:09f915e6f9f6 251 int isTimeout()
andrewboyson 0:09f915e6f9f6 252 {
andrewboyson 0:09f915e6f9f6 253 static Timer receiveTimer;
andrewboyson 0:09f915e6f9f6 254
andrewboyson 0:09f915e6f9f6 255 if (state == IDLE)
andrewboyson 0:09f915e6f9f6 256 {
andrewboyson 0:09f915e6f9f6 257 receiveTimer.stop();
andrewboyson 0:09f915e6f9f6 258 receiveTimer.reset();
andrewboyson 0:09f915e6f9f6 259 }
andrewboyson 0:09f915e6f9f6 260 else
andrewboyson 0:09f915e6f9f6 261 {
andrewboyson 0:09f915e6f9f6 262 receiveTimer.start();
andrewboyson 0:09f915e6f9f6 263 if (receiveTimer.read_ms() > RECV_TIMEOUT_MS) return true;
andrewboyson 0:09f915e6f9f6 264 }
andrewboyson 0:09f915e6f9f6 265 return false;
andrewboyson 0:09f915e6f9f6 266 }
andrewboyson 0:09f915e6f9f6 267 int EspMain()
andrewboyson 0:09f915e6f9f6 268 {
andrewboyson 0:09f915e6f9f6 269
andrewboyson 0:09f915e6f9f6 270 pulseResetPin();
andrewboyson 0:09f915e6f9f6 271
andrewboyson 0:09f915e6f9f6 272 //Reset line availability one shot
andrewboyson 0:09f915e6f9f6 273 EspLineAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 274 EspDataAvailable = ESP_IDLE;
andrewboyson 0:09f915e6f9f6 275
andrewboyson 0:09f915e6f9f6 276 if (isTimeout())
andrewboyson 0:09f915e6f9f6 277 {
andrewboyson 0:09f915e6f9f6 278 if (state == IN_LINE) EspLineAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 279 else EspDataAvailable = ESP_TIMEOUT;
andrewboyson 0:09f915e6f9f6 280 state = IDLE;
andrewboyson 0:09f915e6f9f6 281 return 0;
andrewboyson 0:09f915e6f9f6 282 }
andrewboyson 0:09f915e6f9f6 283
andrewboyson 0:09f915e6f9f6 284 //Handle any incoming characters
andrewboyson 3:accba7e07a0d 285 int c = UartRecvPull();
andrewboyson 0:09f915e6f9f6 286 if (c != EOF)
andrewboyson 0:09f915e6f9f6 287 {
andrewboyson 6:be97d38e0b01 288 if (CfgLogNetwork)
andrewboyson 6:be97d38e0b01 289 {
andrewboyson 6:be97d38e0b01 290 LogPush(c);
andrewboyson 6:be97d38e0b01 291 }
andrewboyson 0:09f915e6f9f6 292 int r = handleCharacter(c); //This will set the EspAvailable one-shots
andrewboyson 0:09f915e6f9f6 293 if (r) return -1;
andrewboyson 0:09f915e6f9f6 294 }
andrewboyson 0:09f915e6f9f6 295
andrewboyson 0:09f915e6f9f6 296 return 0;
andrewboyson 0:09f915e6f9f6 297 }
andrewboyson 0:09f915e6f9f6 298
andrewboyson 0:09f915e6f9f6 299
andrewboyson 0:09f915e6f9f6 300