MAX30001-MAX32630FTHR SYS EvKit

Dependencies:   USBDevice max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Streaming.cpp Source File

Streaming.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 "RpcServer.h"
00034 #include "RpcFifo.h"
00035 #include "Streaming.h"
00036 #include "Peripherals.h"
00037 #include "MAX30001.h"
00038 #include "Test_MAX30001.h"
00039 #include "Peripherals.h"
00040 
00041 bool streaming = FALSE;
00042 bool dataLogging = FALSE;
00043 
00044 /**
00045 * @brief Encodes a 0x55 0xAA signature and a simple checksum to the id byte in
00046 * the 32 bit field
00047 * @param id Streaming ID
00048 */
00049 uint32_t StreamIdChecksumCalculate(uint32_t id) {
00050   uint32_t sum;
00051   uint32_t calculated;
00052   sum = 0x55;
00053   sum += 0xAA;
00054   sum += id;
00055   sum &= 0xFF;
00056   sum = sum << 8;
00057   calculated = 0x55AA0000 + sum + id;
00058   return calculated;
00059 }
00060 
00061 /**
00062 * @brief Creates a packet that will be streamed via USB or saved into flash
00063 * datalog memory
00064 * @brief the packet created will be inserted into a fifo to be streamed at a
00065 * later time
00066 * @param id Streaming ID
00067 * @param buffer Pointer to a uint32 array that contains the data to include in
00068 * the packet
00069 * @param number Number of elements in the buffer
00070 */
00071 void StreamPacketUint32(uint32_t id, uint32_t *buffer, uint32_t number) {
00072   uint32_t checksumId;
00073   if (streaming == TRUE || dataLogging == TRUE) {
00074     checksumId = StreamIdChecksumCalculate(id);
00075     StreamFifoId(checksumId);
00076     StreamFifoTimeStamp();
00077     StreamFifoLength(number);
00078     StreamFifoUint32Array(buffer, number);
00079   }
00080   if (testing_max30001 == 1) {
00081     if (id == MAX30001_DATA_ECG)
00082       testing_ecg_flags[TESTING_ECG_FLAG] = 1;
00083     if (id == MAX30001_DATA_BIOZ)
00084       testing_ecg_flags[TESTING_BIOZ_FLAG] = 1;
00085     if (id == MAX30001_DATA_PACE)
00086       testing_ecg_flags[TESTING_PACE_FLAG] = 1;
00087     if (id == MAX30001_DATA_RTOR)
00088       testing_ecg_flags[TESTING_RTOR_FLAG] = 1;
00089   }
00090 }
00091 
00092 /**
00093 * @brief Insert a buffer into the out going fifo
00094 * @param buffer Array of uint32 to send to the fifo
00095 * @param len Length of the array
00096 */
00097 int StreamFifoUint32Array(uint32_t buffer[], uint32_t len) {
00098   int status;
00099   uint32_t i;
00100   for (i = 0; i < len; i++) {
00101     status = fifo_put32(GetStreamOutFifo(), buffer[i]);
00102     if (status == -1) {
00103       printf("FIFO_OF!");
00104       fflush(stdout);
00105       while (1)
00106         ;
00107     }
00108   }
00109   return 0;
00110 }
00111 
00112 /**
00113 * @brief Insert a timestamp into the out going fifo
00114 */
00115 int StreamFifoTimeStamp(void) {
00116   int status;
00117   // uint32_t timer = timestamp_GetCurrent(); //RTC_GetVal();
00118   uint32_t timer = (uint32_t)Peripherals::timestampTimer()->read_us();
00119   status = fifo_put32(GetStreamOutFifo(), timer);
00120   if (status == -1) {
00121     printf("FIFO_OF!");
00122     fflush(stdout);
00123     while (1)
00124       ;
00125   }
00126   return 0;
00127 }
00128 
00129 /**
00130 * @brief Insert a packet id into the out going fifo
00131 * @param id The uint32 packet id
00132 */
00133 int StreamFifoId(uint32_t id) {
00134   int status;
00135   status = fifo_put32(GetStreamOutFifo(), id);
00136   if (status == -1) {
00137     printf("FIFO_OF!");
00138     fflush(stdout);
00139     while (1)
00140       ;
00141   }
00142   return 0;
00143 }
00144 
00145 /**
00146 * @brief Insert a length value into the out going fifo
00147 * @param length A uint32 number representing a length
00148 */
00149 int StreamFifoLength(uint32_t length) {
00150   int status;
00151   status = fifo_put32(GetStreamOutFifo(), length);
00152   if (status == -1) {
00153     printf("FIFO_OF!");
00154     fflush(stdout);
00155     while (1)
00156       ;
00157   }
00158   return 0;
00159 }
00160 
00161 /**
00162 * @brief Return a value that indicates if the system is streaming data
00163 * @returns Returns a one or zero value
00164 */
00165 uint8_t IsStreaming(void) { return streaming; }
00166 
00167 /**
00168 * @brief Set a flag to indicate if streaming is enabled
00169 * @param state A one or zero value
00170 */
00171 void SetStreaming(uint8_t state) { streaming = state; }
00172 
00173 /**
00174 * @brief Set a flag to indicate if datalogging is enabled
00175 * @param state A one or zero value
00176 */
00177 void SetDataLoggingStream(uint8_t state) { dataLogging = state; }
00178