repo time

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Committer:
darienf
Date:
Tue Apr 06 06:41:40 2021 +0000
Revision:
20:6d2af70c92ab
another repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 20:6d2af70c92ab 1 /*******************************************************************************
darienf 20:6d2af70c92ab 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
darienf 20:6d2af70c92ab 3 *
darienf 20:6d2af70c92ab 4 * Permission is hereby granted, free of charge, to any person obtaining a
darienf 20:6d2af70c92ab 5 * copy of this software and associated documentation files (the "Software"),
darienf 20:6d2af70c92ab 6 * to deal in the Software without restriction, including without limitation
darienf 20:6d2af70c92ab 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
darienf 20:6d2af70c92ab 8 * and/or sell copies of the Software, and to permit persons to whom the
darienf 20:6d2af70c92ab 9 * Software is furnished to do so, subject to the following conditions:
darienf 20:6d2af70c92ab 10 *
darienf 20:6d2af70c92ab 11 * The above copyright notice and this permission notice shall be included
darienf 20:6d2af70c92ab 12 * in all copies or substantial portions of the Software.
darienf 20:6d2af70c92ab 13 *
darienf 20:6d2af70c92ab 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
darienf 20:6d2af70c92ab 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
darienf 20:6d2af70c92ab 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
darienf 20:6d2af70c92ab 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
darienf 20:6d2af70c92ab 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
darienf 20:6d2af70c92ab 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
darienf 20:6d2af70c92ab 20 * OTHER DEALINGS IN THE SOFTWARE.
darienf 20:6d2af70c92ab 21 *
darienf 20:6d2af70c92ab 22 * Except as contained in this notice, the name of Maxim Integrated
darienf 20:6d2af70c92ab 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
darienf 20:6d2af70c92ab 24 * Products, Inc. Branding Policy.
darienf 20:6d2af70c92ab 25 *
darienf 20:6d2af70c92ab 26 * The mere transfer of this software does not imply any licenses
darienf 20:6d2af70c92ab 27 * of trade secrets, proprietary technology, copyrights, patents,
darienf 20:6d2af70c92ab 28 * trademarks, maskwork rights, or any other form of intellectual
darienf 20:6d2af70c92ab 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
darienf 20:6d2af70c92ab 30 * ownership rights.
darienf 20:6d2af70c92ab 31 *******************************************************************************
darienf 20:6d2af70c92ab 32 */
darienf 20:6d2af70c92ab 33 #include "mbed.h"
darienf 20:6d2af70c92ab 34 #include "Logging.h"
darienf 20:6d2af70c92ab 35 #include "Streaming.h"
darienf 20:6d2af70c92ab 36 #include "RpcServer.h"
darienf 20:6d2af70c92ab 37 #include "S25FS512.h"
darienf 20:6d2af70c92ab 38 #include "BMP280.h"
darienf 20:6d2af70c92ab 39 #include "PacketFifo.h"
darienf 20:6d2af70c92ab 40 #include "DataLoggingService.h"
darienf 20:6d2af70c92ab 41 #include "ServiceNonInterrupt.h"
darienf 20:6d2af70c92ab 42 #include "HspLed.h"
darienf 20:6d2af70c92ab 43 #include "MAX30001_helper.h"
darienf 20:6d2af70c92ab 44 #include "MAX30101_helper.h"
darienf 20:6d2af70c92ab 45 #include "StringInOut.h"
darienf 20:6d2af70c92ab 46 #include "StringHelper.h"
darienf 20:6d2af70c92ab 47 #include "Peripherals.h"
darienf 20:6d2af70c92ab 48 #include "Device_Logging.h"
darienf 20:6d2af70c92ab 49
darienf 20:6d2af70c92ab 50 /// BMP280 logging object reference
darienf 20:6d2af70c92ab 51 extern Device_Logging *bmp280_Logging;
darienf 20:6d2af70c92ab 52 /// MAX14720 instance 0 logging object reference
darienf 20:6d2af70c92ab 53 extern Device_Logging *MAX30205_0_Logging;
darienf 20:6d2af70c92ab 54 /// MAX14720 instance 1 logging object reference
darienf 20:6d2af70c92ab 55 extern Device_Logging *MAX30205_1_Logging;
darienf 20:6d2af70c92ab 56
darienf 20:6d2af70c92ab 57 #define PING_PONG_BUFFER_SIZE 512
darienf 20:6d2af70c92ab 58 #define HALF_OF_PING_PONG_BUFFER_SIZE PING_PONG_BUFFER_SIZE / 2
darienf 20:6d2af70c92ab 59 #define MISSION_DEFINITION_SIZE 4096
darienf 20:6d2af70c92ab 60
darienf 20:6d2af70c92ab 61 eLoggingTrigger loggingTrigger;
darienf 20:6d2af70c92ab 62
darienf 20:6d2af70c92ab 63 /// buffer where mission strings are stored
darienf 20:6d2af70c92ab 64 char loggingMissionCmds[MISSION_DEFINITION_SIZE];
darienf 20:6d2af70c92ab 65 /// This houses two 256 byte ram concatenated to act as a ping-pong
darienf 20:6d2af70c92ab 66 uint8_t PingPong_SRAM[PING_PONG_BUFFER_SIZE];
darienf 20:6d2af70c92ab 67 uint32_t buttonTrigger = 0;
darienf 20:6d2af70c92ab 68
darienf 20:6d2af70c92ab 69 eLoggingOutput loggingOutput;
darienf 20:6d2af70c92ab 70 // extern int bleStartCommand;
darienf 20:6d2af70c92ab 71 bool volatile globalFlag;
darienf 20:6d2af70c92ab 72 extern int highDataRate;
darienf 20:6d2af70c92ab 73 static uint32_t currentPage;
darienf 20:6d2af70c92ab 74 static uint32_t sramIndex;
darienf 20:6d2af70c92ab 75 /// flag to indicate that sram buffer 0 is dirty and will need to be flushed
darienf 20:6d2af70c92ab 76 static uint32_t sram_buffer_0_dirty;
darienf 20:6d2af70c92ab 77 /// flag to indicate that sram buffer 1 is dirty and will need to be flushed
darienf 20:6d2af70c92ab 78 static uint32_t sram_buffer_1_dirty;
darienf 20:6d2af70c92ab 79 /// usb byte buffer for sending out a bulk transfer
darienf 20:6d2af70c92ab 80 static uint8_t usb_block[64];
darienf 20:6d2af70c92ab 81 /// running index used to accumulate bytes to send as a block via bulk transfer
darienf 20:6d2af70c92ab 82 static uint16_t usb_block_index = 0;
darienf 20:6d2af70c92ab 83
darienf 20:6d2af70c92ab 84 typedef enum {
darienf 20:6d2af70c92ab 85 eStartEvent_NULL,
darienf 20:6d2af70c92ab 86 eStartEvent_BLE,
darienf 20:6d2af70c92ab 87 eStartEvent_BUTTON,
darienf 20:6d2af70c92ab 88 eStartEvent_RPC_TO_USB,
darienf 20:6d2af70c92ab 89 eStartEvent_RPC_TO_FLASH
darienf 20:6d2af70c92ab 90 } eStartEvent;
darienf 20:6d2af70c92ab 91 static eStartEvent startEvent;
darienf 20:6d2af70c92ab 92
darienf 20:6d2af70c92ab 93 /**
darienf 20:6d2af70c92ab 94 * @brief Sets a flag to start USB logging (streaming)
darienf 20:6d2af70c92ab 95 */
darienf 20:6d2af70c92ab 96 void LoggingService_StartLoggingUsb(void) {
darienf 20:6d2af70c92ab 97 loggingTrigger = eTriggerLog_RPC_USB;
darienf 20:6d2af70c92ab 98 }
darienf 20:6d2af70c92ab 99
darienf 20:6d2af70c92ab 100 /**
darienf 20:6d2af70c92ab 101 * @brief Sets a flag to start flash logging
darienf 20:6d2af70c92ab 102 */
darienf 20:6d2af70c92ab 103 void LoggingService_StartLoggingFlash(void) {
darienf 20:6d2af70c92ab 104 loggingTrigger = eTriggerLog_RPC_FLASH;
darienf 20:6d2af70c92ab 105 }
darienf 20:6d2af70c92ab 106
darienf 20:6d2af70c92ab 107 /**
darienf 20:6d2af70c92ab 108 * @brief Checks the various logging start condition
darienf 20:6d2af70c92ab 109 * @return 1 if a start condition is true, 0 if there is no start condition
darienf 20:6d2af70c92ab 110 */
darienf 20:6d2af70c92ab 111 static bool _LoggingService_CheckStartCondition(void) {
darienf 20:6d2af70c92ab 112 bool buttonPressed;
darienf 20:6d2af70c92ab 113 buttonPressed = Peripherals::pushButton()->GetButtonFallState();
darienf 20:6d2af70c92ab 114
darienf 20:6d2af70c92ab 115 // default not logging USB or flash
darienf 20:6d2af70c92ab 116 loggingOutput = eLogToNothing;
darienf 20:6d2af70c92ab 117 startEvent = eStartEvent_NULL;
darienf 20:6d2af70c92ab 118 if (buttonPressed) {
darienf 20:6d2af70c92ab 119 Peripherals::pushButton()->clearButtonFallState();
darienf 20:6d2af70c92ab 120 // a falling state has been detected... wait for a fraction of a second and
darienf 20:6d2af70c92ab 121 // re-read the pin
darienf 20:6d2af70c92ab 122 // only start datalogging if the pin was released within this wait time
darienf 20:6d2af70c92ab 123 wait(0.75f);
darienf 20:6d2af70c92ab 124 int buttonRead = Peripherals::pushButton()->Read();
darienf 20:6d2af70c92ab 125 // if after a period of time the button is still pressed then get out
darienf 20:6d2af70c92ab 126 if (buttonRead == 0)
darienf 20:6d2af70c92ab 127 return 0;
darienf 20:6d2af70c92ab 128 buttonTrigger = 0;
darienf 20:6d2af70c92ab 129
darienf 20:6d2af70c92ab 130 loggingTrigger = eTriggerLog_BUTTON;
darienf 20:6d2af70c92ab 131 loggingOutput = eLogToFlash;
darienf 20:6d2af70c92ab 132 startEvent = eStartEvent_BUTTON;
darienf 20:6d2af70c92ab 133 return true;
darienf 20:6d2af70c92ab 134 }
darienf 20:6d2af70c92ab 135 if (loggingTrigger == eTriggerLog_RPC_FLASH) {
darienf 20:6d2af70c92ab 136 loggingOutput = eLogToFlash;
darienf 20:6d2af70c92ab 137 startEvent = eStartEvent_RPC_TO_FLASH;
darienf 20:6d2af70c92ab 138 return true;
darienf 20:6d2af70c92ab 139 }
darienf 20:6d2af70c92ab 140 if (Peripherals::hspBLE()->getStartDataLogging()) {
darienf 20:6d2af70c92ab 141 loggingTrigger = eTriggerLog_BLE;
darienf 20:6d2af70c92ab 142 loggingOutput = eLogToFlash;
darienf 20:6d2af70c92ab 143 startEvent = eStartEvent_BLE;
darienf 20:6d2af70c92ab 144 return true;
darienf 20:6d2af70c92ab 145 }
darienf 20:6d2af70c92ab 146 // check if start is from RPC call for USB streaming
darienf 20:6d2af70c92ab 147 if (loggingTrigger == eTriggerLog_RPC_USB) {
darienf 20:6d2af70c92ab 148 loggingOutput = eLogtoUsb;
darienf 20:6d2af70c92ab 149 startEvent = eStartEvent_RPC_TO_USB;
darienf 20:6d2af70c92ab 150 return true;
darienf 20:6d2af70c92ab 151 }
darienf 20:6d2af70c92ab 152 return false;
darienf 20:6d2af70c92ab 153 }
darienf 20:6d2af70c92ab 154
darienf 20:6d2af70c92ab 155 /**
darienf 20:6d2af70c92ab 156 * @brief Read the mission string from flash into a buffer
darienf 20:6d2af70c92ab 157 * @return false if a mission was not defined, true if mission was read and
darienf 20:6d2af70c92ab 158 * buffered
darienf 20:6d2af70c92ab 159 */
darienf 20:6d2af70c92ab 160 static bool _LoggingService_ReadMissionFromFlash(void) {
darienf 20:6d2af70c92ab 161 // get mission from flash
darienf 20:6d2af70c92ab 162 Logging_ReadMissionFromFlash((uint8_t *)loggingMissionCmds);
darienf 20:6d2af70c92ab 163 if (Logging_IsMissionDefined((uint8_t *)loggingMissionCmds) == 0) {
darienf 20:6d2af70c92ab 164 return false;
darienf 20:6d2af70c92ab 165 }
darienf 20:6d2af70c92ab 166 printf(loggingMissionCmds);
darienf 20:6d2af70c92ab 167 fflush(stdout);
darienf 20:6d2af70c92ab 168 RPC_ProcessCmds(loggingMissionCmds);
darienf 20:6d2af70c92ab 169 return true;
darienf 20:6d2af70c92ab 170 }
darienf 20:6d2af70c92ab 171
darienf 20:6d2af70c92ab 172 /**
darienf 20:6d2af70c92ab 173 * @brief Process a RPC command that is pointed to.
darienf 20:6d2af70c92ab 174 * @param cmd RPC string to process
darienf 20:6d2af70c92ab 175 */
darienf 20:6d2af70c92ab 176 void ProcessCmd(char *cmd) {
darienf 20:6d2af70c92ab 177 char cmd_[256];
darienf 20:6d2af70c92ab 178 char reply[512];
darienf 20:6d2af70c92ab 179 strcpy(cmd_, cmd);
darienf 20:6d2af70c92ab 180 RPC_call(cmd_, reply);
darienf 20:6d2af70c92ab 181 }
darienf 20:6d2af70c92ab 182
darienf 20:6d2af70c92ab 183 /**
darienf 20:6d2af70c92ab 184 * @brief Buffer sensor fifo data in ram buffers, when a ram buffer is full (a
darienf 20:6d2af70c92ab 185 * flash page worth of data is accumulated) then flash that buffer.
darienf 20:6d2af70c92ab 186 * A buffer ping pong method is used so that one buffer can be flashing as
darienf 20:6d2af70c92ab 187 * the other buffer fills with sensor fifo data.
darienf 20:6d2af70c92ab 188 * @param fifoData Sensor data taken from the fifo to be stored into flash
darienf 20:6d2af70c92ab 189 */
darienf 20:6d2af70c92ab 190 static void _LoggingServer_OutputToFlash(uint32_t fifoData) {
darienf 20:6d2af70c92ab 191 uint32_t index;
darienf 20:6d2af70c92ab 192 char str[128];
darienf 20:6d2af70c92ab 193 uint8_t *ptr;
darienf 20:6d2af70c92ab 194 //
darienf 20:6d2af70c92ab 195 // Log To Flash
darienf 20:6d2af70c92ab 196 //
darienf 20:6d2af70c92ab 197 // i.e. there is data, read one 32-bit size data at a time.
darienf 20:6d2af70c92ab 198 // put the fifo data into the ping-pong SRAM
darienf 20:6d2af70c92ab 199 PingPong_SRAM[sramIndex++] = fifoData & 0xFF; // LSByte goes into index N
darienf 20:6d2af70c92ab 200 PingPong_SRAM[sramIndex++] = (fifoData >> 8) & 0xFF;
darienf 20:6d2af70c92ab 201 PingPong_SRAM[sramIndex++] = (fifoData >> 16) & 0xFF;
darienf 20:6d2af70c92ab 202 PingPong_SRAM[sramIndex++] = (fifoData >> 24) & 0xFF; // MSByte goes into index N+3
darienf 20:6d2af70c92ab 203
darienf 20:6d2af70c92ab 204 // flag this buffer as dirty
darienf 20:6d2af70c92ab 205 if (sramIndex <= 256)
darienf 20:6d2af70c92ab 206 sram_buffer_0_dirty = 1;
darienf 20:6d2af70c92ab 207 else
darienf 20:6d2af70c92ab 208 sram_buffer_1_dirty = 1;
darienf 20:6d2af70c92ab 209
darienf 20:6d2af70c92ab 210 if (sramIndex == 256 ||
darienf 20:6d2af70c92ab 211 sramIndex == 512) // Either Ping SRAM or Pong SRAM location is full
darienf 20:6d2af70c92ab 212 { // therefore write to Flash
darienf 20:6d2af70c92ab 213
darienf 20:6d2af70c92ab 214 index = sramIndex - 256;
darienf 20:6d2af70c92ab 215 ptr = &PingPong_SRAM[index];
darienf 20:6d2af70c92ab 216 sprintf(str, "currentPage=%d", currentPage);
darienf 20:6d2af70c92ab 217 Peripherals::s25FS512()->writePage_Helper(currentPage, ptr, 0);
darienf 20:6d2af70c92ab 218
darienf 20:6d2af70c92ab 219 // this page is no longer dirty
darienf 20:6d2af70c92ab 220 if (index == 0)
darienf 20:6d2af70c92ab 221 sram_buffer_0_dirty = 0;
darienf 20:6d2af70c92ab 222 if (index == 256)
darienf 20:6d2af70c92ab 223 sram_buffer_1_dirty = 0;
darienf 20:6d2af70c92ab 224
darienf 20:6d2af70c92ab 225 currentPage++;
darienf 20:6d2af70c92ab 226 }
darienf 20:6d2af70c92ab 227 sramIndex = sramIndex % 512; // Wrap around the index
darienf 20:6d2af70c92ab 228 }
darienf 20:6d2af70c92ab 229
darienf 20:6d2af70c92ab 230 /**
darienf 20:6d2af70c92ab 231 * @brief If flash ram buffers are flagged as dirty, flush to flash
darienf 20:6d2af70c92ab 232 */
darienf 20:6d2af70c92ab 233 static void _LoggingServer_WriteDirtySramBufferToFlash(void) {
darienf 20:6d2af70c92ab 234 uint8_t *ptr = PingPong_SRAM;
darienf 20:6d2af70c92ab 235 if (sram_buffer_0_dirty == 0 && sram_buffer_1_dirty == 0)
darienf 20:6d2af70c92ab 236 return;
darienf 20:6d2af70c92ab 237 if (sram_buffer_0_dirty == 1) {
darienf 20:6d2af70c92ab 238 ptr += 0;
darienf 20:6d2af70c92ab 239 }
darienf 20:6d2af70c92ab 240 if (sram_buffer_1_dirty == 1) {
darienf 20:6d2af70c92ab 241 ptr += 256;
darienf 20:6d2af70c92ab 242 }
darienf 20:6d2af70c92ab 243 printf("_LoggingServer_WriteDirtySramBufferToFlash:%d,%d\n",
darienf 20:6d2af70c92ab 244 sram_buffer_0_dirty, sram_buffer_1_dirty);
darienf 20:6d2af70c92ab 245 fflush(stdout);
darienf 20:6d2af70c92ab 246 // s25fs512_WritePage_Helper(currentPage, ptr, 0);
darienf 20:6d2af70c92ab 247 Peripherals::s25FS512()->writePage_Helper(currentPage, ptr, 0);
darienf 20:6d2af70c92ab 248 }
darienf 20:6d2af70c92ab 249
darienf 20:6d2af70c92ab 250 /**
darienf 20:6d2af70c92ab 251 * @brief Initialize the USB block running index
darienf 20:6d2af70c92ab 252 * @param fifoData Sensor data taken from the fifo to be sent out USB
darienf 20:6d2af70c92ab 253 */
darienf 20:6d2af70c92ab 254 static void _LoggingServer_OutputToCdcAcm(uint32_t fifoData) {
darienf 20:6d2af70c92ab 255 uint8_t *ptr;
darienf 20:6d2af70c92ab 256 uint8_t str[16];
darienf 20:6d2af70c92ab 257 sprintf((char *)str, "%X ", fifoData);
darienf 20:6d2af70c92ab 258 ptr = str;
darienf 20:6d2af70c92ab 259 usb_block_index = 0;
darienf 20:6d2af70c92ab 260 while (*ptr != 0) {
darienf 20:6d2af70c92ab 261 usb_block[usb_block_index] = *ptr;
darienf 20:6d2af70c92ab 262 ptr++;
darienf 20:6d2af70c92ab 263 usb_block_index++;
darienf 20:6d2af70c92ab 264 }
darienf 20:6d2af70c92ab 265 Peripherals::usbSerial()->writeBlock(usb_block, usb_block_index);
darienf 20:6d2af70c92ab 266 }
darienf 20:6d2af70c92ab 267
darienf 20:6d2af70c92ab 268 /**
darienf 20:6d2af70c92ab 269 * @brief Initialize the USB block running index
darienf 20:6d2af70c92ab 270 */
darienf 20:6d2af70c92ab 271 static void _LoggingServer_OutputToCdcAcm_Start(void) { usb_block_index = 0; }
darienf 20:6d2af70c92ab 272
darienf 20:6d2af70c92ab 273 /**
darienf 20:6d2af70c92ab 274 * @brief Buffer up fifoData from sensors, do a USB block transfer if buffer is
darienf 20:6d2af70c92ab 275 * full
darienf 20:6d2af70c92ab 276 * @param fifoData Sensor data taken from the fifo to be send out USB within a
darienf 20:6d2af70c92ab 277 * bulk block transfer
darienf 20:6d2af70c92ab 278 * @return Return the success status of the writeblock operation
darienf 20:6d2af70c92ab 279 */
darienf 20:6d2af70c92ab 280 static bool _LoggingServer_OutputToCdcAcm_Block(uint32_t fifoData) {
darienf 20:6d2af70c92ab 281 uint8_t str[64];
darienf 20:6d2af70c92ab 282 uint8_t *ptr;
darienf 20:6d2af70c92ab 283 bool result;
darienf 20:6d2af70c92ab 284 //
darienf 20:6d2af70c92ab 285 // Log to CDCACM
darienf 20:6d2af70c92ab 286 //
darienf 20:6d2af70c92ab 287 result = true;
darienf 20:6d2af70c92ab 288 sprintf((char *)str, "%X ", fifoData);
darienf 20:6d2af70c92ab 289 ptr = str;
darienf 20:6d2af70c92ab 290 while (*ptr != 0) {
darienf 20:6d2af70c92ab 291 usb_block[usb_block_index] = *ptr;
darienf 20:6d2af70c92ab 292 ptr++;
darienf 20:6d2af70c92ab 293 usb_block_index++;
darienf 20:6d2af70c92ab 294 if (usb_block_index >= 64) {
darienf 20:6d2af70c92ab 295 result = Peripherals::usbSerial()->writeBlock(usb_block, 64);
darienf 20:6d2af70c92ab 296 usb_block_index = 0;
darienf 20:6d2af70c92ab 297 }
darienf 20:6d2af70c92ab 298 }
darienf 20:6d2af70c92ab 299 return result;
darienf 20:6d2af70c92ab 300 }
darienf 20:6d2af70c92ab 301
darienf 20:6d2af70c92ab 302 /**
darienf 20:6d2af70c92ab 303 * @brief Output a full USB block via bulk transfer
darienf 20:6d2af70c92ab 304 */
darienf 20:6d2af70c92ab 305 static void _LoggingServer_OutputToCdcAcm_End(void) {
darienf 20:6d2af70c92ab 306 if (usb_block_index == 0)
darienf 20:6d2af70c92ab 307 return;
darienf 20:6d2af70c92ab 308 Peripherals::usbSerial()->writeBlock(usb_block, usb_block_index - 1);
darienf 20:6d2af70c92ab 309 }
darienf 20:6d2af70c92ab 310
darienf 20:6d2af70c92ab 311 /**
darienf 20:6d2af70c92ab 312 * @brief Blink LED pattern that indicates that the flash end boundary has been
darienf 20:6d2af70c92ab 313 * reached
darienf 20:6d2af70c92ab 314 */
darienf 20:6d2af70c92ab 315 static void BlinkEndOfDatalogging(void) {
darienf 20:6d2af70c92ab 316 // blink to signal end of logging
darienf 20:6d2af70c92ab 317 Peripherals::hspLed()->pattern(0x55555555, 20);
darienf 20:6d2af70c92ab 318 wait(2);
darienf 20:6d2af70c92ab 319 }
darienf 20:6d2af70c92ab 320
darienf 20:6d2af70c92ab 321 /**
darienf 20:6d2af70c92ab 322 * @brief Reads the first data page of flash, if all FF's then the page is empty
darienf 20:6d2af70c92ab 323 * @return 1 if the flash is empty as indicated by the first data page of the
darienf 20:6d2af70c92ab 324 * flash, 0 if not
darienf 20:6d2af70c92ab 325 */
darienf 20:6d2af70c92ab 326 int isFlashEmpty(void) {
darienf 20:6d2af70c92ab 327 int i;
darienf 20:6d2af70c92ab 328 uint8_t data[256];
darienf 20:6d2af70c92ab 329 int firstDataPage = Logging_GetLoggingStartPage();
darienf 20:6d2af70c92ab 330 Peripherals::s25FS512()->readPages_Helper(firstDataPage, firstDataPage, data, 0);
darienf 20:6d2af70c92ab 331 for (i = 0; i < 256; i++) {
darienf 20:6d2af70c92ab 332 if (data[i] != 0xFF)
darienf 20:6d2af70c92ab 333 return 0;
darienf 20:6d2af70c92ab 334 }
darienf 20:6d2af70c92ab 335 return 1;
darienf 20:6d2af70c92ab 336 }
darienf 20:6d2af70c92ab 337
darienf 20:6d2af70c92ab 338 /**
darienf 20:6d2af70c92ab 339 * @brief Blink LED pattern that indicates that the flash is not empty and a new
darienf 20:6d2af70c92ab 340 * flash logging session can not occur
darienf 20:6d2af70c92ab 341 */
darienf 20:6d2af70c92ab 342 void BlinkFlashNotEmpty(void) {
darienf 20:6d2af70c92ab 343 Peripherals::hspLed()->pattern(0x55555555, 20);
darienf 20:6d2af70c92ab 344 wait(1);
darienf 20:6d2af70c92ab 345 }
darienf 20:6d2af70c92ab 346
darienf 20:6d2af70c92ab 347 void ExecuteDefaultMission(void) {
darienf 20:6d2af70c92ab 348 ProcessCmd("/MAX30001/CAL_InitStart 01 01 01 03 7FF 00");
darienf 20:6d2af70c92ab 349 ProcessCmd("/MAX30001/ECG_InitStart 01 01 01 00 02 03 1F 0 00 00 01");
darienf 20:6d2af70c92ab 350 ProcessCmd("/MAX30001/RtoR_InitStart 01 03 0F 00 03 01 00 00 01");
darienf 20:6d2af70c92ab 351 ProcessCmd("/MAX30001/Rbias_FMSTR_Init 01 02 01 01 00");
darienf 20:6d2af70c92ab 352 ProcessCmd("/LIS2DH/InitStart 02 01");
darienf 20:6d2af70c92ab 353 }
darienf 20:6d2af70c92ab 354
darienf 20:6d2af70c92ab 355 void LoggingService_Init(void) { loggingTrigger = eTriggerLog_NULL; }
darienf 20:6d2af70c92ab 356
darienf 20:6d2af70c92ab 357 /**
darienf 20:6d2af70c92ab 358 * @brief This routine checks to see if a USB or flash logging action needs to be taken
darienf 20:6d2af70c92ab 359 * The routine checks for a start condition via button press, USB command, or BLE command
darienf 20:6d2af70c92ab 360 * Once one of these start conditions is present, the logging begins until stopped or memory is full
darienf 20:6d2af70c92ab 361 * @return 1 if successful, 0 if error or logging was aborted and no logging occurred
darienf 20:6d2af70c92ab 362 */
darienf 20:6d2af70c92ab 363 uint8_t LoggingService_ServiceRoutine(void) {
darienf 20:6d2af70c92ab 364 uint32_t fifoData;
darienf 20:6d2af70c92ab 365 uint32_t endPage;
darienf 20:6d2af70c92ab 366 USBSerial *usbSerial = Peripherals::usbSerial();
darienf 20:6d2af70c92ab 367 // BMP280 *bmp280 = Peripherals::bmp280();
darienf 20:6d2af70c92ab 368 bool buttonPressed;
darienf 20:6d2af70c92ab 369 int packetBurstCount = 0;
darienf 20:6d2af70c92ab 370 HspLed *hspLed = Peripherals::hspLed();
darienf 20:6d2af70c92ab 371
darienf 20:6d2af70c92ab 372 sramIndex = 0;
darienf 20:6d2af70c92ab 373 // only start logging if conditions exist
darienf 20:6d2af70c92ab 374
darienf 20:6d2af70c92ab 375 if (_LoggingService_CheckStartCondition() == false) return 0;
darienf 20:6d2af70c92ab 376 printf("Begin Logging...");
darienf 20:6d2af70c92ab 377 if (startEvent == eStartEvent_NULL) printf("eStartEvent_NULL...");
darienf 20:6d2af70c92ab 378 if (startEvent == eStartEvent_BLE) printf("eStartEvent_BLE...");
darienf 20:6d2af70c92ab 379 if (startEvent == eStartEvent_BUTTON) printf("eStartEvent_BUTTON...");
darienf 20:6d2af70c92ab 380 if (startEvent == eStartEvent_RPC_TO_USB) printf("eStartEvent_RPC_TO_USB...");
darienf 20:6d2af70c92ab 381 if (startEvent == eStartEvent_RPC_TO_FLASH) printf("eStartEvent_RPC_TO_FLASH...");
darienf 20:6d2af70c92ab 382 fflush(stdout);
darienf 20:6d2af70c92ab 383
darienf 20:6d2af70c92ab 384 // start logging stuttered blink pattern
darienf 20:6d2af70c92ab 385 hspLed->pattern(0xA0F3813, 20);
darienf 20:6d2af70c92ab 386
darienf 20:6d2af70c92ab 387 if (startEvent == eStartEvent_RPC_TO_FLASH ||
darienf 20:6d2af70c92ab 388 startEvent == eStartEvent_BUTTON) {
darienf 20:6d2af70c92ab 389 // check to see if datalog already in flash... abort and force user to erase
darienf 20:6d2af70c92ab 390 // flash if needed
darienf 20:6d2af70c92ab 391 if (loggingOutput == eLogToFlash) {
darienf 20:6d2af70c92ab 392 if (isFlashEmpty() == 0) {
darienf 20:6d2af70c92ab 393 Logging_SetStart(false);
darienf 20:6d2af70c92ab 394 // bleStartCommand = 0x00;
darienf 20:6d2af70c92ab 395 BlinkFlashNotEmpty();
darienf 20:6d2af70c92ab 396 hspLed->blink(1000);
darienf 20:6d2af70c92ab 397 printf("Abort Logging, flash log exists. ");
darienf 20:6d2af70c92ab 398 fflush(stdout);
darienf 20:6d2af70c92ab 399 return 0;
darienf 20:6d2af70c92ab 400 }
darienf 20:6d2af70c92ab 401 }
darienf 20:6d2af70c92ab 402 }
darienf 20:6d2af70c92ab 403
darienf 20:6d2af70c92ab 404 if (startEvent == eStartEvent_BLE) {
darienf 20:6d2af70c92ab 405 // check for mission in flash
darienf 20:6d2af70c92ab 406 if (_LoggingService_ReadMissionFromFlash() == false) {
darienf 20:6d2af70c92ab 407 // if there is no mission in flash then do a default mission for the sake
darienf 20:6d2af70c92ab 408 // of ble Android app working "out-of-the-box" and stream RtoR and Accel
darienf 20:6d2af70c92ab 409 printf("No Mission in Flash...ExecuteDefaultMission...");
darienf 20:6d2af70c92ab 410 fflush(stdout);
darienf 20:6d2af70c92ab 411 ExecuteDefaultMission();
darienf 20:6d2af70c92ab 412 // do not log this data
darienf 20:6d2af70c92ab 413 loggingOutput = eLogToNothing;
darienf 20:6d2af70c92ab 414 } else {
darienf 20:6d2af70c92ab 415 // there is a mission in flash check if there is already logged data
darienf 20:6d2af70c92ab 416 if (isFlashEmpty() == 0) {
darienf 20:6d2af70c92ab 417 // just do default mission
darienf 20:6d2af70c92ab 418 printf("Logged Data Detected...ExecuteDefaultMission...");
darienf 20:6d2af70c92ab 419 fflush(stdout);
darienf 20:6d2af70c92ab 420 ExecuteDefaultMission();
darienf 20:6d2af70c92ab 421 // do not log this data
darienf 20:6d2af70c92ab 422 loggingOutput = eLogToNothing;
darienf 20:6d2af70c92ab 423 } else {
darienf 20:6d2af70c92ab 424 // flag that we are logging to flash
darienf 20:6d2af70c92ab 425 loggingOutput = eLogToFlash;
darienf 20:6d2af70c92ab 426 }
darienf 20:6d2af70c92ab 427 }
darienf 20:6d2af70c92ab 428 }
darienf 20:6d2af70c92ab 429
darienf 20:6d2af70c92ab 430 // if we are logging to flash then read mission in flash
darienf 20:6d2af70c92ab 431 if (loggingOutput == eLogToFlash) {
darienf 20:6d2af70c92ab 432 if (_LoggingService_ReadMissionFromFlash() ==
darienf 20:6d2af70c92ab 433 false) { // if there is no mission in flash then get out
darienf 20:6d2af70c92ab 434 Logging_SetStart(false);
darienf 20:6d2af70c92ab 435 Peripherals::hspLed()->pattern(0xC3C3C3C3, 20);
darienf 20:6d2af70c92ab 436 wait(2);
darienf 20:6d2af70c92ab 437 printf("Abort Logging, Mission does not exist. ");
darienf 20:6d2af70c92ab 438 fflush(stdout);
darienf 20:6d2af70c92ab 439 return 0;
darienf 20:6d2af70c92ab 440 }
darienf 20:6d2af70c92ab 441 currentPage = Logging_GetLoggingStartPage();
darienf 20:6d2af70c92ab 442 endPage = Logging_GetLoggingEndPage();
darienf 20:6d2af70c92ab 443 }
darienf 20:6d2af70c92ab 444
darienf 20:6d2af70c92ab 445 MAX30001_Helper_SetupInterrupts();
darienf 20:6d2af70c92ab 446 if (MAX30001_AnyStreamingSet() == 1) {
darienf 20:6d2af70c92ab 447 MAX30001_Helper_StartSync();
darienf 20:6d2af70c92ab 448 }
darienf 20:6d2af70c92ab 449
darienf 20:6d2af70c92ab 450 SetDataLoggingStream(TRUE);
darienf 20:6d2af70c92ab 451 ServiceNonInterrupt_Init();
darienf 20:6d2af70c92ab 452 ServiceNonInterrupt_StartTimer();
darienf 20:6d2af70c92ab 453
darienf 20:6d2af70c92ab 454 while (usbSerial->readable()) {
darienf 20:6d2af70c92ab 455 usbSerial->_getc();
darienf 20:6d2af70c92ab 456 }
darienf 20:6d2af70c92ab 457 fifo_clear(GetUSBIncomingFifo()); // clear USB serial incoming fifo
darienf 20:6d2af70c92ab 458 fifo_clear(GetStreamOutFifo());
darienf 20:6d2af70c92ab 459
darienf 20:6d2af70c92ab 460 sram_buffer_0_dirty = 0;
darienf 20:6d2af70c92ab 461 sram_buffer_1_dirty = 0;
darienf 20:6d2af70c92ab 462
darienf 20:6d2af70c92ab 463
darienf 20:6d2af70c92ab 464 if (loggingOutput == eLogToNothing) printf("eLogToNothing..."); fflush(stdout);
darienf 20:6d2af70c92ab 465 if (loggingOutput == eLogToFlash) printf("eLogToFlash..."); fflush(stdout);
darienf 20:6d2af70c92ab 466 if (loggingOutput == eLogtoUsb) printf("eLogtoUsb..."); fflush(stdout);
darienf 20:6d2af70c92ab 467 printf("highDataRate=%d...",highDataRate); fflush(stdout);
darienf 20:6d2af70c92ab 468
darienf 20:6d2af70c92ab 469
darienf 20:6d2af70c92ab 470 Peripherals::timestampTimer()->reset();
darienf 20:6d2af70c92ab 471 Peripherals::timestampTimer()->start();
darienf 20:6d2af70c92ab 472
darienf 20:6d2af70c92ab 473 _LoggingServer_OutputToCdcAcm_Start();
darienf 20:6d2af70c92ab 474 while (1) {
darienf 20:6d2af70c92ab 475 if (loggingOutput == eLogToFlash) {
darienf 20:6d2af70c92ab 476 // check if we are at the end of flash
darienf 20:6d2af70c92ab 477 if (currentPage >= endPage) {
darienf 20:6d2af70c92ab 478 BlinkEndOfDatalogging(); // blink for 3 seconds to signal end of logging
darienf 20:6d2af70c92ab 479 break;
darienf 20:6d2af70c92ab 480 }
darienf 20:6d2af70c92ab 481 }
darienf 20:6d2af70c92ab 482
darienf 20:6d2af70c92ab 483 if (startEvent == eStartEvent_BUTTON) {
darienf 20:6d2af70c92ab 484 buttonPressed = Peripherals::pushButton()->GetButtonFallState();
darienf 20:6d2af70c92ab 485 if (buttonPressed) {
darienf 20:6d2af70c92ab 486 Peripherals::pushButton()->clearButtonFallState();
darienf 20:6d2af70c92ab 487 // if there is a dirty sram buffer... flush it to flash
darienf 20:6d2af70c92ab 488 _LoggingServer_WriteDirtySramBufferToFlash();
darienf 20:6d2af70c92ab 489 BlinkEndOfDatalogging(); // blink for 3 seconds to signal end of logging
darienf 20:6d2af70c92ab 490 break;
darienf 20:6d2af70c92ab 491 }
darienf 20:6d2af70c92ab 492 }
darienf 20:6d2af70c92ab 493
darienf 20:6d2af70c92ab 494 if (loggingTrigger == eTriggerLog_BLE) {
darienf 20:6d2af70c92ab 495 if (Peripherals::hspBLE()->getStartDataLogging() == false) {
darienf 20:6d2af70c92ab 496 // if there is a dirty sram buffer... flush it to flash
darienf 20:6d2af70c92ab 497 _LoggingServer_WriteDirtySramBufferToFlash();
darienf 20:6d2af70c92ab 498 BlinkEndOfDatalogging(); // blink for 3 seconds to signal end of logging
darienf 20:6d2af70c92ab 499 break;
darienf 20:6d2af70c92ab 500 }
darienf 20:6d2af70c92ab 501 }
darienf 20:6d2af70c92ab 502
darienf 20:6d2af70c92ab 503 if (startEvent == eStartEvent_RPC_TO_USB ||
darienf 20:6d2af70c92ab 504 startEvent == eStartEvent_RPC_TO_FLASH) {
darienf 20:6d2af70c92ab 505 if (usbSerial->available()) {
darienf 20:6d2af70c92ab 506 if (loggingOutput == eLogToFlash) {
darienf 20:6d2af70c92ab 507 _LoggingServer_WriteDirtySramBufferToFlash();
darienf 20:6d2af70c92ab 508 }
darienf 20:6d2af70c92ab 509 wait(0.2f);
darienf 20:6d2af70c92ab 510 while (usbSerial->available()) {
darienf 20:6d2af70c92ab 511 usbSerial->_getc();
darienf 20:6d2af70c92ab 512 }
darienf 20:6d2af70c92ab 513 fifo_clear(GetUSBIncomingFifo()); // clear USB serial incoming fifo
darienf 20:6d2af70c92ab 514 fifo_clear(GetStreamOutFifo());
darienf 20:6d2af70c92ab 515 break;
darienf 20:6d2af70c92ab 516 }
darienf 20:6d2af70c92ab 517 }
darienf 20:6d2af70c92ab 518
darienf 20:6d2af70c92ab 519 // check to see if data is available
darienf 20:6d2af70c92ab 520 packetBurstCount = 0;
darienf 20:6d2af70c92ab 521 while (PacketFifo_Empty() == 0) {
darienf 20:6d2af70c92ab 522 if (packetBurstCount >= 100)
darienf 20:6d2af70c92ab 523 break;
darienf 20:6d2af70c92ab 524 fifoData = PacketFifo_GetUint32();
darienf 20:6d2af70c92ab 525 if (loggingOutput == eLogToFlash) {
darienf 20:6d2af70c92ab 526 _LoggingServer_OutputToFlash(fifoData);
darienf 20:6d2af70c92ab 527 }
darienf 20:6d2af70c92ab 528 if (loggingOutput == eLogtoUsb) {
darienf 20:6d2af70c92ab 529 if (highDataRate == 0)
darienf 20:6d2af70c92ab 530 _LoggingServer_OutputToCdcAcm(fifoData);
darienf 20:6d2af70c92ab 531 else
darienf 20:6d2af70c92ab 532 _LoggingServer_OutputToCdcAcm_Block(fifoData);
darienf 20:6d2af70c92ab 533 }
darienf 20:6d2af70c92ab 534 packetBurstCount++;
darienf 20:6d2af70c92ab 535 }
darienf 20:6d2af70c92ab 536
darienf 20:6d2af70c92ab 537 if (PacketFifo_Empty() != 0) {
darienf 20:6d2af70c92ab 538 Peripherals::ble()->waitForEvent();
darienf 20:6d2af70c92ab 539 }
darienf 20:6d2af70c92ab 540 ServiceNonInterrupt_BMP280(bmp280_Logging);
darienf 20:6d2af70c92ab 541 ServiceNonInterrupt_MAX30205(MAX30205_0_Logging,
darienf 20:6d2af70c92ab 542 Peripherals::max30205_top(),
darienf 20:6d2af70c92ab 543 PACKET_MAX30205_TEMP_TOP);
darienf 20:6d2af70c92ab 544 ServiceNonInterrupt_MAX30205(MAX30205_1_Logging,
darienf 20:6d2af70c92ab 545 Peripherals::max30205_bottom(),
darienf 20:6d2af70c92ab 546 PACKET_MAX30205_TEMP_BOTTOM);
darienf 20:6d2af70c92ab 547 }
darienf 20:6d2af70c92ab 548 _LoggingServer_OutputToCdcAcm_End();
darienf 20:6d2af70c92ab 549 printf("End Logging.\n");
darienf 20:6d2af70c92ab 550 fflush(stdout);
darienf 20:6d2af70c92ab 551
darienf 20:6d2af70c92ab 552 bmp280_Logging->stop();
darienf 20:6d2af70c92ab 553 MAX30205_0_Logging->stop();
darienf 20:6d2af70c92ab 554 MAX30205_1_Logging->stop();
darienf 20:6d2af70c92ab 555 MAX30001_Helper_Stop(); // if any MAX30001 streams have been started, stop
darienf 20:6d2af70c92ab 556 // them
darienf 20:6d2af70c92ab 557 MAX30101_Helper_Stop(); // if any MAX30101 streams have been started, stop
darienf 20:6d2af70c92ab 558 // them
darienf 20:6d2af70c92ab 559 Peripherals::lis2dh()->stop();
darienf 20:6d2af70c92ab 560 SetDataLoggingStream(FALSE);
darienf 20:6d2af70c92ab 561 Peripherals::timestampTimer()->stop();
darienf 20:6d2af70c92ab 562 hspLed->blink(1000);
darienf 20:6d2af70c92ab 563 // default to non-usb packet speed optimizing
darienf 20:6d2af70c92ab 564 highDataRate = 0;
darienf 20:6d2af70c92ab 565 loggingTrigger = eTriggerLog_NULL;
darienf 20:6d2af70c92ab 566 return 1;
darienf 20:6d2af70c92ab 567 }