UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Mon May 27 03:07:42 2019 +0000
Revision:
13:7cda5bef6390
Parent:
8:b18a8764ecae
Child:
17:c2709a9c0a68
add uprintf(UART_printf); (this function can be disabled by uartOn=false)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 2:a694440145e9 1 /** --- Includes --- */
Inscape_ao 2:a694440145e9 2 #include "mbed.h"
Inscape_ao 2:a694440145e9 3 #include "TimeManager.h"
Inscape_ao 2:a694440145e9 4 #include "UartReceiver.h"
Inscape_ao 2:a694440145e9 5 #include "CommandParser.h"
Inscape_ao 2:a694440145e9 6 #include "global.h"
Inscape_ao 2:a694440145e9 7 #include "string.h"
Inscape_ao 2:a694440145e9 8
Inscape_ao 2:a694440145e9 9 static int sampleHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 10 static int softResetHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 11 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 12 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 13 static int startSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 14 static int endSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 15 static int writeSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 16
Inscape_ao 7:9ab8809f9693 17 static int configIDHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 18 static int configWriteHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 19 static int configReadHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 20
Inscape_ao 7:9ab8809f9693 21 static int startRunHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 22 static int stopReqHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 23 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 24 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 25 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 26
Inscape_ao 2:a694440145e9 27 /* Event Hankder */
Inscape_ao 2:a694440145e9 28 CmdParseRule rules[] = {
Inscape_ao 7:9ab8809f9693 29 /* -- sample -- */
Inscape_ao 2:a694440145e9 30 {"CMD", sampleHanlder, 0},
Inscape_ao 7:9ab8809f9693 31 /* Control SD-Card(NOT-SDHC) */
Inscape_ao 5:a37e3a15444b 32 {"SDS", startSDStore, 0},
Inscape_ao 5:a37e3a15444b 33 {"SDE", endSDStore, 0},
Inscape_ao 5:a37e3a15444b 34 {"SDW", writeSDStore, 0}, /* for TEST */
Inscape_ao 7:9ab8809f9693 35 /* Get Timestamp */
Inscape_ao 2:a694440145e9 36 {"GTS", getTimeHanlder, 0},
Inscape_ao 7:9ab8809f9693 37 /* Set Timestamp */
Inscape_ao 2:a694440145e9 38 {"TYR", setTimeHanlder, TimeManager::SetTimeMethod::Year},
Inscape_ao 2:a694440145e9 39 {"TMO", setTimeHanlder, TimeManager::SetTimeMethod::Month},
Inscape_ao 2:a694440145e9 40 {"TDA", setTimeHanlder, TimeManager::SetTimeMethod::Day},
Inscape_ao 2:a694440145e9 41 {"THR", setTimeHanlder, TimeManager::SetTimeMethod::Hour},
Inscape_ao 2:a694440145e9 42 {"TMI", setTimeHanlder, TimeManager::SetTimeMethod::Min},
Inscape_ao 2:a694440145e9 43 {"TSE", setTimeHanlder, TimeManager::SetTimeMethod::Sec},
Inscape_ao 7:9ab8809f9693 44 /* Sensing Control */
Inscape_ao 7:9ab8809f9693 45 {"CRS", startRunHandler, 0}, /* Run(start) */
Inscape_ao 7:9ab8809f9693 46 {"CRE", stopReqHandler, 0}, /* Stop -- req */
Inscape_ao 7:9ab8809f9693 47 {"CPS", checkRunStatHandler, 0}, /* Check ReadyToRun */
Inscape_ao 7:9ab8809f9693 48 {"RRP", setRepeatCountHandler, 0}, /* set repeat setting => Count */
Inscape_ao 7:9ab8809f9693 49 {"RMC", setRepeatStrideHandler, 0}, /* set repeat setting => Stride */
Inscape_ao 7:9ab8809f9693 50 /* Device Control */
Inscape_ao 7:9ab8809f9693 51 {"SRS", softResetHanlder, 0}, /* TODO: Software Reset */
Inscape_ao 7:9ab8809f9693 52 {"CID", configIDHandler, 0}, /* Config ID Access */
Inscape_ao 7:9ab8809f9693 53 {"CFW", configWriteHandler, 0}, /* Config Write */
Inscape_ao 7:9ab8809f9693 54 {"CFR", configReadHandler, 0}, /* Config Read */
Inscape_ao 2:a694440145e9 55 };
Inscape_ao 2:a694440145e9 56
Inscape_ao 2:a694440145e9 57 int getNumOfRules = sizeof(rules)/sizeof(CmdParseRule);
Inscape_ao 2:a694440145e9 58
Inscape_ao 7:9ab8809f9693 59 /****************************************************/
Inscape_ao 7:9ab8809f9693 60 /* Event Handlers (Device Control) */
Inscape_ao 7:9ab8809f9693 61 /****************************************************/
Inscape_ao 7:9ab8809f9693 62 static int startRunHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 63 {
Inscape_ao 7:9ab8809f9693 64 bool success;
Inscape_ao 7:9ab8809f9693 65 success = pDevRept->start();
Inscape_ao 7:9ab8809f9693 66 if (!success) {
Inscape_ao 7:9ab8809f9693 67 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 68 return 1;
Inscape_ao 7:9ab8809f9693 69 }
Inscape_ao 7:9ab8809f9693 70 pC->reply();
Inscape_ao 7:9ab8809f9693 71 return 0;
Inscape_ao 7:9ab8809f9693 72 }
Inscape_ao 7:9ab8809f9693 73
Inscape_ao 7:9ab8809f9693 74 static int stopReqHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 75 {
Inscape_ao 7:9ab8809f9693 76 bool success;
Inscape_ao 7:9ab8809f9693 77 success = pDevRept->stop();
Inscape_ao 7:9ab8809f9693 78 if (!success) {
Inscape_ao 7:9ab8809f9693 79 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 80 return 1;
Inscape_ao 7:9ab8809f9693 81 }
Inscape_ao 7:9ab8809f9693 82 pC->reply();
Inscape_ao 7:9ab8809f9693 83 return 0;
Inscape_ao 7:9ab8809f9693 84 }
Inscape_ao 7:9ab8809f9693 85
Inscape_ao 7:9ab8809f9693 86 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 87 {
Inscape_ao 7:9ab8809f9693 88 bool success;
Inscape_ao 7:9ab8809f9693 89 success = pDevRept->readyToStart();
Inscape_ao 7:9ab8809f9693 90 if (!success) {
Inscape_ao 7:9ab8809f9693 91 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 92 return 1;
Inscape_ao 7:9ab8809f9693 93 }
Inscape_ao 7:9ab8809f9693 94 pC->reply();
Inscape_ao 7:9ab8809f9693 95 return 0;
Inscape_ao 7:9ab8809f9693 96 }
Inscape_ao 7:9ab8809f9693 97
Inscape_ao 7:9ab8809f9693 98 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 99 {
Inscape_ao 7:9ab8809f9693 100 bool success;
Inscape_ao 7:9ab8809f9693 101 int setvalue = atoi(arg);
Inscape_ao 7:9ab8809f9693 102 success = pDevRept->setRepeatCount(setvalue);
Inscape_ao 7:9ab8809f9693 103 if (!success) {
Inscape_ao 7:9ab8809f9693 104 pC->reply(false, 4);
Inscape_ao 7:9ab8809f9693 105 return 1;
Inscape_ao 7:9ab8809f9693 106 }
Inscape_ao 7:9ab8809f9693 107 pC->reply();
Inscape_ao 7:9ab8809f9693 108 return 0;
Inscape_ao 7:9ab8809f9693 109 }
Inscape_ao 7:9ab8809f9693 110 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 111 {
Inscape_ao 7:9ab8809f9693 112 bool success;
Inscape_ao 7:9ab8809f9693 113 char msflag = arg[3];
Inscape_ao 7:9ab8809f9693 114 char valsrc[4] = {0};
Inscape_ao 7:9ab8809f9693 115 int setvalue;
Inscape_ao 7:9ab8809f9693 116
Inscape_ao 7:9ab8809f9693 117 /* this Argment is "xxxs" or "xxxm" */
Inscape_ao 7:9ab8809f9693 118 memcpy(valsrc, arg, 3);
Inscape_ao 7:9ab8809f9693 119 setvalue = atoi(valsrc);
Inscape_ao 7:9ab8809f9693 120
Inscape_ao 7:9ab8809f9693 121 if (msflag == 'm') {
Inscape_ao 7:9ab8809f9693 122 setvalue *= 60; /* setvalue x 60(min to sec)*/
Inscape_ao 7:9ab8809f9693 123 } else if (msflag == 's') {
Inscape_ao 7:9ab8809f9693 124 /* NOP setvalue = setvalue */
Inscape_ao 7:9ab8809f9693 125 } else {
Inscape_ao 7:9ab8809f9693 126 /* invalid format */
Inscape_ao 7:9ab8809f9693 127 pC->reply(false, 6);
Inscape_ao 7:9ab8809f9693 128 return 1;
Inscape_ao 7:9ab8809f9693 129 }
Inscape_ao 7:9ab8809f9693 130 success = pDevRept->setRepeatStride(setvalue);
Inscape_ao 7:9ab8809f9693 131 if (!success) {
Inscape_ao 7:9ab8809f9693 132 pC->reply(false, 5);
Inscape_ao 7:9ab8809f9693 133 return 1;
Inscape_ao 7:9ab8809f9693 134 }
Inscape_ao 7:9ab8809f9693 135 pC->reply();
Inscape_ao 7:9ab8809f9693 136 return 0;
Inscape_ao 7:9ab8809f9693 137 }
Inscape_ao 8:b18a8764ecae 138
Inscape_ao 7:9ab8809f9693 139 /****************************************************/
Inscape_ao 7:9ab8809f9693 140 /* Event Handlers (Device Control) */
Inscape_ao 7:9ab8809f9693 141 /****************************************************/
Inscape_ao 7:9ab8809f9693 142 static int configIDHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 143 {
Inscape_ao 7:9ab8809f9693 144 bool success;
Inscape_ao 7:9ab8809f9693 145 int setvalue = strtol(arg, NULL, 16);
Inscape_ao 7:9ab8809f9693 146 success = pDevRept->setConfigId(setvalue);
Inscape_ao 7:9ab8809f9693 147 if (!success) {
Inscape_ao 7:9ab8809f9693 148 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 149 return 1;
Inscape_ao 7:9ab8809f9693 150 }
Inscape_ao 7:9ab8809f9693 151 pC->reply();
Inscape_ao 7:9ab8809f9693 152 return 0;
Inscape_ao 7:9ab8809f9693 153 }
Inscape_ao 7:9ab8809f9693 154
Inscape_ao 7:9ab8809f9693 155 static int configWriteHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 156 {
Inscape_ao 7:9ab8809f9693 157 bool success;
Inscape_ao 7:9ab8809f9693 158 int setvalue = strtol(arg, NULL, 16);
Inscape_ao 7:9ab8809f9693 159 success = pDevRept->setConfigValue(setvalue);
Inscape_ao 7:9ab8809f9693 160 if (!success) {
Inscape_ao 7:9ab8809f9693 161 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 162 return 1;
Inscape_ao 7:9ab8809f9693 163 }
Inscape_ao 7:9ab8809f9693 164 pC->reply();
Inscape_ao 7:9ab8809f9693 165 return 0;
Inscape_ao 7:9ab8809f9693 166 }
Inscape_ao 7:9ab8809f9693 167
Inscape_ao 7:9ab8809f9693 168 static int configReadHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 169 {
Inscape_ao 7:9ab8809f9693 170 bool success;
Inscape_ao 7:9ab8809f9693 171 int getvalue;
Inscape_ao 7:9ab8809f9693 172 Serial *pUart = pC->getCurrentUart();
Inscape_ao 7:9ab8809f9693 173
Inscape_ao 7:9ab8809f9693 174 success = pDevRept->getConfigValue(&getvalue);
Inscape_ao 7:9ab8809f9693 175 if (!success) {
Inscape_ao 7:9ab8809f9693 176 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 177 return 1;
Inscape_ao 7:9ab8809f9693 178 }
Inscape_ao 13:7cda5bef6390 179 uprintf(":%d CFG 0004 %04x\n", pC->getDeviceID(), getvalue);
Inscape_ao 7:9ab8809f9693 180 return 0;
Inscape_ao 7:9ab8809f9693 181 }
Inscape_ao 7:9ab8809f9693 182
Inscape_ao 7:9ab8809f9693 183 static int softResetHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 184 {
Inscape_ao 7:9ab8809f9693 185 bool success;
Inscape_ao 7:9ab8809f9693 186 success = pDevRept->resetAllStatus();
Inscape_ao 7:9ab8809f9693 187 if (!success) {
Inscape_ao 7:9ab8809f9693 188 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 189 return 1;
Inscape_ao 7:9ab8809f9693 190 }
Inscape_ao 7:9ab8809f9693 191 pC->reply();
Inscape_ao 7:9ab8809f9693 192 return 0;
Inscape_ao 7:9ab8809f9693 193 }
Inscape_ao 2:a694440145e9 194
Inscape_ao 2:a694440145e9 195 /****************************************************/
Inscape_ao 2:a694440145e9 196 /* Event Handlers */
Inscape_ao 2:a694440145e9 197 /****************************************************/
Inscape_ao 2:a694440145e9 198 /* Sample Command */
Inscape_ao 2:a694440145e9 199 static int sampleHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 200 {
Inscape_ao 2:a694440145e9 201 pC->reply();
Inscape_ao 2:a694440145e9 202 return 0;
Inscape_ao 2:a694440145e9 203 }
Inscape_ao 2:a694440145e9 204
Inscape_ao 5:a37e3a15444b 205 /****************************************************/
Inscape_ao 5:a37e3a15444b 206 /* Event Handlers (SD Control) */
Inscape_ao 5:a37e3a15444b 207 /****************************************************/
Inscape_ao 5:a37e3a15444b 208 static int startSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 209 {
Inscape_ao 5:a37e3a15444b 210 if (pSds->startFileWithTimeStamp("","txt") != true) {
Inscape_ao 5:a37e3a15444b 211 pC->reply(false, 1);
Inscape_ao 5:a37e3a15444b 212 return 1;
Inscape_ao 5:a37e3a15444b 213 }
Inscape_ao 5:a37e3a15444b 214 pC->reply();
Inscape_ao 5:a37e3a15444b 215 return 0;
Inscape_ao 5:a37e3a15444b 216 }
Inscape_ao 5:a37e3a15444b 217
Inscape_ao 5:a37e3a15444b 218 static int endSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 219 {
Inscape_ao 5:a37e3a15444b 220 FILE *fp;
Inscape_ao 5:a37e3a15444b 221 if ((fp = pSds->getFilePointer()) == NULL) {
Inscape_ao 5:a37e3a15444b 222 /* NOP */
Inscape_ao 5:a37e3a15444b 223 pC->reply(false, 2);
Inscape_ao 5:a37e3a15444b 224 return 2;
Inscape_ao 5:a37e3a15444b 225 }
Inscape_ao 5:a37e3a15444b 226 fprintf(fp, "call endSDStore(%s)\n", pSds->getFileName());
Inscape_ao 5:a37e3a15444b 227 pSds->syncFile();
Inscape_ao 5:a37e3a15444b 228 pSds->closeFile();
Inscape_ao 5:a37e3a15444b 229 pC->reply();
Inscape_ao 5:a37e3a15444b 230 return 0;
Inscape_ao 5:a37e3a15444b 231 }
Inscape_ao 5:a37e3a15444b 232
Inscape_ao 5:a37e3a15444b 233 static int writeSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 234 {
Inscape_ao 5:a37e3a15444b 235 FILE *fp;
Inscape_ao 5:a37e3a15444b 236 char curr[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 5:a37e3a15444b 237
Inscape_ao 5:a37e3a15444b 238 if ((fp = pSds->getFilePointer()) == NULL) {
Inscape_ao 5:a37e3a15444b 239 /* NOP */
Inscape_ao 5:a37e3a15444b 240 pC->reply(false, 2);
Inscape_ao 5:a37e3a15444b 241 return 2;
Inscape_ao 5:a37e3a15444b 242 }
Inscape_ao 5:a37e3a15444b 243 pTM->getTimeStamp(curr);
Inscape_ao 5:a37e3a15444b 244 fprintf(fp, "call writeSDStore at %s\n", curr);
Inscape_ao 5:a37e3a15444b 245 pSds->syncFile();
Inscape_ao 5:a37e3a15444b 246 pC->reply();
Inscape_ao 5:a37e3a15444b 247 return 0;
Inscape_ao 5:a37e3a15444b 248 }
Inscape_ao 5:a37e3a15444b 249
Inscape_ao 5:a37e3a15444b 250 /****************************************************/
Inscape_ao 5:a37e3a15444b 251 /* Event Handlers (Time Control) */
Inscape_ao 5:a37e3a15444b 252 /****************************************************/
Inscape_ao 2:a694440145e9 253 /* Time Set Command */
Inscape_ao 2:a694440145e9 254 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 255 {
Inscape_ao 2:a694440145e9 256 bool success = false;
Inscape_ao 2:a694440145e9 257 int setvalue = atoi(arg);
Inscape_ao 2:a694440145e9 258
Inscape_ao 2:a694440145e9 259 switch (exarg) {
Inscape_ao 2:a694440145e9 260 case TimeManager::SetTimeMethod::Year:
Inscape_ao 2:a694440145e9 261 case TimeManager::SetTimeMethod::Month:
Inscape_ao 2:a694440145e9 262 case TimeManager::SetTimeMethod::Day:
Inscape_ao 2:a694440145e9 263 case TimeManager::SetTimeMethod::Hour:
Inscape_ao 2:a694440145e9 264 case TimeManager::SetTimeMethod::Min:
Inscape_ao 2:a694440145e9 265 case TimeManager::SetTimeMethod::Sec:
Inscape_ao 2:a694440145e9 266 success = pTM->setCurrentTime(exarg,setvalue);
Inscape_ao 2:a694440145e9 267 break;
Inscape_ao 2:a694440145e9 268 }
Inscape_ao 2:a694440145e9 269 pC->reply(success, (success)? 0 : setvalue );
Inscape_ao 2:a694440145e9 270 return 0;
Inscape_ao 2:a694440145e9 271 }
Inscape_ao 2:a694440145e9 272
Inscape_ao 2:a694440145e9 273 /* Time Get Command */
Inscape_ao 2:a694440145e9 274 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 275 {
Inscape_ao 2:a694440145e9 276 int len;
Inscape_ao 2:a694440145e9 277 char timestamp[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 2:a694440145e9 278 Serial *pUart = pC->getCurrentUart();
Inscape_ao 2:a694440145e9 279 len = pTM->getTimeStamp(timestamp);
Inscape_ao 13:7cda5bef6390 280 uprintf(":%d RTS %04d %s\n", pC->getDeviceID(), len, timestamp);
Inscape_ao 2:a694440145e9 281 return 0;
Inscape_ao 2:a694440145e9 282 }