Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Logging_RPC.cpp Source File

Logging_RPC.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  ******************************************************************************
00032  */
00033 #include "StringHelper.h"
00034 #include <stdint.h>
00035 #include "Streaming.h"
00036 #include "StringInOut.h"
00037 #include "DataLoggingService.h"
00038 #include "Peripherals.h"
00039 #include "Logging.h"
00040 
00041 extern char loggingMissionCmds[4096];
00042 uint32_t missionCmdIndex;
00043 
00044  //******************************************************************************
00045  int Logging_RPC_StartMissionDefine(char argStrs[32][32],
00046                                    char replyStrs[32][32]) {
00047   uint32_t i;
00048   uint32_t reply[1];
00049 
00050   // reset the missionCmdIndex to the beginning of the cmd buffer
00051   missionCmdIndex = 0;
00052   // clear the mission command buffer, fill with zeros
00053   for (i = 0; i < sizeof(loggingMissionCmds); i++) {
00054     loggingMissionCmds[i] = 0;
00055   }
00056 
00057   reply[0] = 0x80;
00058   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00059   return 0;
00060 }
00061 
00062 //******************************************************************************
00063 int Logging_RPC_AppendMissionCmd(char argStrs[32][32], char replyStrs[32][32]) {
00064   uint32_t reply[1];
00065   char *strPtr;
00066   uint32_t count = 0;
00067   uint8_t result = 0x80;
00068   // append the string to the mission cmd log
00069   strPtr = argStrs[0];
00070   while (*strPtr != 0) {
00071     loggingMissionCmds[missionCmdIndex] = *strPtr;
00072     missionCmdIndex++;
00073     strPtr++;
00074     // do not overrun buffer
00075     if (missionCmdIndex > (sizeof(loggingMissionCmds) - 2)) {
00076       result = 0xFF;
00077       break;
00078     }
00079     count++;
00080     // do not read more than max count in incoming string
00081     if (count > (32 * 32)) {
00082       result = 0xFF;
00083       break;
00084     }
00085   }
00086   if (result != 0x80) {
00087     reply[0] = 0xFF;
00088     FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00089     return 0;
00090   }
00091   // add cr/lf to the end of this cmd string
00092   loggingMissionCmds[missionCmdIndex++] = 13;
00093   loggingMissionCmds[missionCmdIndex++] = 10;
00094 
00095   reply[0] = 0x80;
00096   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00097   return 0;
00098 }
00099 
00100 //******************************************************************************
00101 int Logging_RPC_EndMissionDefine(char argStrs[32][32], char replyStrs[32][32]) {
00102   uint32_t reply[1];
00103   reply[0] = 0x80;
00104   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00105   return 0;
00106 }
00107 
00108 //******************************************************************************
00109 int Logging_RPC_WriteMission(char argStrs[32][32], char replyStrs[32][32]) {
00110   uint8_t page;
00111   char *ptr;
00112   uint32_t reply[1];
00113 
00114   Peripherals::s25FS512()->parameterSectorErase_Helper(0x0);
00115   ptr = loggingMissionCmds;
00116   for (page = 0; page < 16; page++) {
00117     Peripherals::s25FS512()->writePage_Helper(page, (uint8_t *)ptr, 0);
00118     ptr += 256;
00119   }
00120 
00121   reply[0] = 0x80;
00122   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00123   return 0;
00124 }
00125 
00126 //******************************************************************************
00127 int Logging_RPC_ReadMission(char argStrs[32][32], char replyStrs[32][32]) {
00128   char *ptr;
00129   uint32_t i;
00130   // read sector 0
00131   ptr = loggingMissionCmds;
00132   for (i = 0; i < 16; i++) {
00133     Peripherals::s25FS512()->readPages_Helper(i, i, (uint8_t *)ptr, 0);
00134     ptr += 256;
00135   }
00136   // strip header by shifting payload left
00137   ptr = loggingMissionCmds;
00138   for (i = 0; i < sizeof(loggingMissionCmds); i++) {
00139     if (*ptr == 13) {
00140       *ptr = ':';
00141     } else if (*ptr == 10) {
00142       *ptr = ' ';
00143     } 
00144     ptr++;
00145   }
00146   if (loggingMissionCmds[0] == 0xFF || loggingMissionCmds[0] == 0x0) {
00147     sprintf(loggingMissionCmds, "%s", "null ");
00148   }
00149   // send it out via uart
00150   putStr(loggingMissionCmds);
00151   replyStrs[0][0] = 0;
00152   return 0;
00153 }
00154 
00155 //******************************************************************************
00156 int Logging_RPC_EraseMission(char argStrs[32][32], char replyStrs[32][32]) {
00157   uint32_t reply[1];
00158   Peripherals::s25FS512()->parameterSectorErase_Helper(0x0);
00159   reply[0] = 0x80;
00160   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00161   return 0;
00162 }
00163 
00164 #define SECTOR_SIZE_256K 0x10000
00165 #define PAGE_INC_256K 0x400
00166 #define SECTOR_SIZE_4K 0x1000
00167 #define PAGE_INC_4K 0x10
00168 #define TOTAL_SECTOR_NUMBER 263
00169 
00170 //******************************************************************************
00171 int Logging_EraseWrittenSectors(char argStrs[32][32], char replyStrs[32][32]) {
00172   uint32_t reply[1];
00173   uint8_t data[512];
00174   uint32_t address;
00175   uint32_t pageNumber;
00176   uint32_t pageEmpty;
00177   uint32_t currentSector;
00178 
00179   pageNumber = PAGE_INC_4K;
00180   address = SECTOR_SIZE_4K;
00181   currentSector = 0;
00182   printf("Logging_EraseWrittenSectors ");
00183   fflush(stdout);
00184   printf("pageNumber %d 0x%X ", pageNumber, pageNumber);
00185   fflush(stdout);
00186   printf("SECTOR_SIZE_4K %d 0x%X ...", SECTOR_SIZE_4K, SECTOR_SIZE_4K);
00187   fflush(stdout);
00188   // always erase this sector... the first part is used for the mission resume
00189   // table
00190   Peripherals::s25FS512()->sectorErase_Helper(address);
00191   address += SECTOR_SIZE_4K;
00192   while (currentSector < TOTAL_SECTOR_NUMBER) {
00193     // sample the page
00194     pageNumber = address >> 8;
00195     Peripherals::s25FS512()->readPages_Helper(pageNumber, pageNumber, data, 0);
00196     pageEmpty = Peripherals::s25FS512()->isPageEmpty(data);
00197     if (pageEmpty == 0) {
00198       Peripherals::s25FS512()->sectorErase_Helper(address);
00199     } else {
00200       // stop processing... all of the sectors from here on out should be empty
00201       // printf("Logging_EraseWrittenSectors break ");  fflush(stdout);
00202       // break;
00203     }
00204     currentSector++;
00205     if (currentSector < 8) {
00206       address += SECTOR_SIZE_4K;
00207     }
00208     if (currentSector == 8) {
00209       address = SECTOR_SIZE_256K;
00210     }
00211     if (currentSector > 8) {
00212       address += SECTOR_SIZE_256K;
00213     }
00214   }
00215   reply[0] = 0x80;
00216   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00217   printf("Logging_EraseWrittenSectors done. \n");
00218   fflush(stdout);
00219   return 0;
00220 }
00221 
00222 //******************************************************************************
00223 int Logging_Start(char argStrs[32][32], char replyStrs[32][32]) {
00224   uint32_t reply[1];
00225   Logging_SetStart(true);
00226   reply[0] = 0x80; // indicate success
00227   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00228   return 0;
00229 }
00230 
00231 //******************************************************************************
00232 int Logging_GetLastWrittenPage(char argStrs[32][32], char replyStrs[32][32]) {
00233   uint32_t reply[1];
00234 
00235   uint32_t page;
00236   uint32_t lastPage;
00237   uint32_t pageEmpty;
00238   uint8_t data[512];
00239 
00240   printf("Logging_GetLastWrittenPage ");
00241   fflush(stdout);
00242   lastPage = Logging_GetLoggingEndPage();
00243   for (page = 2; page <= lastPage; page++) {
00244     // Peripherals::serial()->printf("checking page %d ",page); fflush(stdout);
00245     // sample the page
00246     Peripherals::s25FS512()->readPages_Helper(page, page, data, 0);
00247     pageEmpty = Peripherals::s25FS512()->isPageEmpty(data);
00248     if (pageEmpty != 0) {
00249       break;
00250     }
00251   }
00252   if (page > lastPage)
00253     page = lastPage;
00254   printf("last page %d, 0x%X ", page, page);
00255   fflush(stdout);
00256   reply[0] = page;
00257   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00258   return 0;
00259 }
00260 
00261 extern int highDataRate;
00262 //******************************************************************************
00263 int Logging_StartLoggingUsb(char argStrs[32][32], char replyStrs[32][32]) {
00264   uint32_t reply[1];
00265   // highDataRate = 0;
00266   LoggingService_StartLoggingUsb();
00267   reply[0] = 0x80;
00268   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00269   return 0;
00270 }
00271 //******************************************************************************
00272 int Logging_StartLoggingFlash(char argStrs[32][32], char replyStrs[32][32]) {
00273   uint32_t reply[1];
00274   // highDataRate = 0;
00275   LoggingService_StartLoggingFlash();
00276   reply[0] = 0x80;
00277   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00278   return 0;
00279 }