MAX30001-MAX32630FTHR SYS EvKit

Dependencies:   USBDevice max32630fthr

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 extern char missionFileName[32];
00044 extern char dataFileName[32];
00045  //******************************************************************************
00046  int Logging_RPC_StartMissionDefine(char argStrs[32][32],
00047                                    char replyStrs[32][32]) {
00048   uint32_t i;
00049   uint32_t reply[1];
00050 
00051   // reset the missionCmdIndex to the beginning of the cmd buffer
00052   missionCmdIndex = 0;
00053   // clear the mission command buffer, fill with zeros
00054   for (i = 0; i < sizeof(loggingMissionCmds); i++) {
00055     loggingMissionCmds[i] = 0;
00056   }
00057 
00058   reply[0] = 0x80;
00059   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00060   return 0;
00061 }
00062 
00063 //******************************************************************************
00064 int Logging_RPC_AppendMissionCmd(char argStrs[32][32], char replyStrs[32][32]) {
00065   uint32_t reply[1];
00066   char *strPtr;
00067   uint32_t count = 0;
00068   uint8_t result = 0x80;
00069   // append the string to the mission cmd log
00070   strPtr = argStrs[0];
00071   while (*strPtr != 0) {
00072     loggingMissionCmds[missionCmdIndex] = *strPtr;
00073     missionCmdIndex++;
00074     strPtr++;
00075     // do not overrun buffer
00076     if (missionCmdIndex > (sizeof(loggingMissionCmds) - 2)) {
00077       result = 0xFF;
00078       break;
00079     }
00080     count++;
00081     // do not read more than max count in incoming string
00082     if (count > (32 * 32)) {
00083       result = 0xFF;
00084       break;
00085     }
00086   }
00087   if (result != 0x80) {
00088     reply[0] = 0xFF;
00089     FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00090     return 0;
00091   }
00092   // add cr/lf to the end of this cmd string
00093   loggingMissionCmds[missionCmdIndex++] = 13;
00094   loggingMissionCmds[missionCmdIndex++] = 10;
00095 
00096   reply[0] = 0x80;
00097   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00098   return 0;
00099 }
00100 
00101 //******************************************************************************
00102 int Logging_RPC_EndMissionDefine(char argStrs[32][32], char replyStrs[32][32]) {
00103   uint32_t reply[1];
00104   reply[0] = 0x80;
00105   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00106   return 0;
00107 }
00108 
00109 //******************************************************************************
00110 int Logging_RPC_WriteMission(char argStrs[32][32], char replyStrs[32][32]) {
00111   char *ptr;
00112   uint32_t reply[1];
00113 
00114   ptr = loggingMissionCmds;
00115   FILE *fp = fopen(missionFileName, "w");
00116   if (fp != NULL){
00117       fprintf(fp, ptr);
00118       fclose(fp);
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   FILE *fp;
00131   ptr = loggingMissionCmds;
00132   fp = fopen(missionFileName, "r");
00133   if (fp != NULL) {
00134       do
00135       {
00136           char c = (char)fgetc(fp);
00137           *ptr++ = c;
00138       } while(!feof(fp));
00139       ptr--;
00140       *ptr = 0;
00141       fclose(fp);
00142   }
00143   ptr = loggingMissionCmds;
00144   for (i = 0; i < sizeof(loggingMissionCmds); i++) {
00145     if (*ptr == 13) {
00146       *ptr = ':';
00147     } else if (*ptr == 10) {
00148       *ptr = ' ';
00149     } 
00150     ptr++;
00151   }
00152 
00153   // send it out via uart
00154   putStr(loggingMissionCmds);
00155   replyStrs[0][0] = 0;
00156   return 0;
00157 }
00158 
00159 //******************************************************************************
00160 int Logging_RPC_EraseMission(char argStrs[32][32], char replyStrs[32][32]) {
00161   uint32_t reply[1];
00162   FILE *fp = fopen(missionFileName, "w");
00163   if (fp != NULL){
00164       fprintf(fp, "");
00165       fclose(fp);
00166   }
00167   reply[0] = 0x80;
00168   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00169   return 0;
00170 }
00171 
00172 #define SECTOR_SIZE_256K 0x10000
00173 #define PAGE_INC_256K 0x400
00174 #define SECTOR_SIZE_4K 0x1000
00175 #define PAGE_INC_4K 0x10
00176 #define TOTAL_SECTOR_NUMBER 263
00177 
00178 //******************************************************************************
00179 int Logging_EraseWrittenSectors(char argStrs[32][32], char replyStrs[32][32]) {
00180   uint32_t reply[1];
00181   uint8_t data[512];
00182 
00183   printf("Logging_EraseWrittenSectors ");
00184   FILE *fp = fopen(missionFileName, "w");
00185   if (fp != NULL){
00186       fprintf(fp, "");
00187       fclose(fp);
00188   }
00189 
00190   fp = fopen(dataFileName, "w");
00191   if (fp != NULL){
00192       fprintf(fp, "");
00193       fclose(fp);
00194   }
00195   reply[0] = 0x80;
00196   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00197   printf("Logging_EraseWrittenSectors done. \n");
00198   fflush(stdout);
00199   return 0;
00200 }
00201 
00202 //******************************************************************************
00203 int Logging_Start(char argStrs[32][32], char replyStrs[32][32]) {
00204   uint32_t reply[1];
00205   Logging_SetStart(true);
00206   reply[0] = 0x80; // indicate success
00207   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00208   return 0;
00209 }
00210 
00211 //******************************************************************************
00212 int Logging_GetLastWrittenPage(char argStrs[32][32], char replyStrs[32][32]) {
00213   uint32_t reply[1];
00214 
00215   uint32_t page;
00216   uint32_t lastPage;
00217   uint32_t pageEmpty;
00218   uint8_t data[512];
00219 
00220   printf("Logging_GetLastWrittenPage ");
00221   fflush(stdout);
00222   lastPage = Logging_GetLoggingEndPage();
00223   for (page = 2; page <= lastPage; page++) {
00224     // Peripherals::serial()->printf("checking page %d ",page); fflush(stdout);
00225     // sample the page
00226     Peripherals::s25FS512()->readPages_Helper(page, page, data, 0);
00227     pageEmpty = Peripherals::s25FS512()->isPageEmpty(data);
00228     if (pageEmpty != 0) {
00229       break;
00230     }
00231   }
00232   if (page > lastPage)
00233     page = lastPage;
00234   printf("last page %d, 0x%X ", page, page);
00235   fflush(stdout);
00236   reply[0] = page;
00237   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00238   return 0;
00239 }
00240 
00241 extern int highDataRate;
00242 //******************************************************************************
00243 int Logging_StartLoggingUsb(char argStrs[32][32], char replyStrs[32][32]) {
00244   uint32_t reply[1];
00245   // highDataRate = 0;
00246   LoggingService_StartLoggingUsb();
00247   reply[0] = 0x80;
00248   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00249   return 0;
00250 }
00251 //******************************************************************************
00252 int Logging_StartLoggingFlash(char argStrs[32][32], char replyStrs[32][32]) {
00253   uint32_t reply[1];
00254   // highDataRate = 0;
00255   LoggingService_StartLoggingFlash();
00256   reply[0] = 0x80;
00257   FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);
00258   return 0;
00259 }