Darien Figueroa / Mbed OS Final_Program

Dependencies:   USBDevice

Committer:
darienf
Date:
Wed Apr 07 22:37:59 2021 +0000
Revision:
0:832122ce6748
painnie

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 0:832122ce6748 1 /*******************************************************************************
darienf 0:832122ce6748 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
darienf 0:832122ce6748 3 *
darienf 0:832122ce6748 4 * Permission is hereby granted, free of charge, to any person obtaining a
darienf 0:832122ce6748 5 * copy of this software and associated documentation files (the "Software"),
darienf 0:832122ce6748 6 * to deal in the Software without restriction, including without limitation
darienf 0:832122ce6748 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
darienf 0:832122ce6748 8 * and/or sell copies of the Software, and to permit persons to whom the
darienf 0:832122ce6748 9 * Software is furnished to do so, subject to the following conditions:
darienf 0:832122ce6748 10 *
darienf 0:832122ce6748 11 * The above copyright notice and this permission notice shall be included
darienf 0:832122ce6748 12 * in all copies or substantial portions of the Software.
darienf 0:832122ce6748 13 *
darienf 0:832122ce6748 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
darienf 0:832122ce6748 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
darienf 0:832122ce6748 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
darienf 0:832122ce6748 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
darienf 0:832122ce6748 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
darienf 0:832122ce6748 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
darienf 0:832122ce6748 20 * OTHER DEALINGS IN THE SOFTWARE.
darienf 0:832122ce6748 21 *
darienf 0:832122ce6748 22 * Except as contained in this notice, the name of Maxim Integrated
darienf 0:832122ce6748 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
darienf 0:832122ce6748 24 * Products, Inc. Branding Policy.
darienf 0:832122ce6748 25 *
darienf 0:832122ce6748 26 * The mere transfer of this software does not imply any licenses
darienf 0:832122ce6748 27 * of trade secrets, proprietary technology, copyrights, patents,
darienf 0:832122ce6748 28 * trademarks, maskwork rights, or any other form of intellectual
darienf 0:832122ce6748 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
darienf 0:832122ce6748 30 * ownership rights.
darienf 0:832122ce6748 31 *******************************************************************************
darienf 0:832122ce6748 32 */
darienf 0:832122ce6748 33 #include "RpcServer.h"
darienf 0:832122ce6748 34 #include "RpcFifo.h"
darienf 0:832122ce6748 35 #include "Streaming.h"
darienf 0:832122ce6748 36 #include "Peripherals.h"
darienf 0:832122ce6748 37 #include "Test_MAX30101.h"
darienf 0:832122ce6748 38 #include "Test_MAX30001.h"
darienf 0:832122ce6748 39 #include "MAX30001.h"
darienf 0:832122ce6748 40 #include "MAX30101.h"
darienf 0:832122ce6748 41 #include "Peripherals.h"
darienf 0:832122ce6748 42
darienf 0:832122ce6748 43 bool streaming = FALSE;
darienf 0:832122ce6748 44 bool dataLogging = FALSE;
darienf 0:832122ce6748 45
darienf 0:832122ce6748 46 /**
darienf 0:832122ce6748 47 * @brief Encodes a 0x55 0xAA signature and a simple checksum to the id byte in
darienf 0:832122ce6748 48 * the 32 bit field
darienf 0:832122ce6748 49 * @param id Streaming ID
darienf 0:832122ce6748 50 */
darienf 0:832122ce6748 51 uint32_t StreamIdChecksumCalculate(uint32_t id) {
darienf 0:832122ce6748 52 uint32_t sum;
darienf 0:832122ce6748 53 uint32_t calculated;
darienf 0:832122ce6748 54 sum = 0x55;
darienf 0:832122ce6748 55 sum += 0xAA;
darienf 0:832122ce6748 56 sum += id;
darienf 0:832122ce6748 57 sum &= 0xFF;
darienf 0:832122ce6748 58 sum = sum << 8;
darienf 0:832122ce6748 59 calculated = 0x55AA0000 + sum + id;
darienf 0:832122ce6748 60 return calculated;
darienf 0:832122ce6748 61 }
darienf 0:832122ce6748 62
darienf 0:832122ce6748 63 /**
darienf 0:832122ce6748 64 * @brief Creates a packet that will be streamed via USB or saved into flash
darienf 0:832122ce6748 65 * datalog memory
darienf 0:832122ce6748 66 * @brief the packet created will be inserted into a fifo to be streamed at a
darienf 0:832122ce6748 67 * later time
darienf 0:832122ce6748 68 * @param id Streaming ID
darienf 0:832122ce6748 69 * @param buffer Pointer to a uint32 array that contains the data to include in
darienf 0:832122ce6748 70 * the packet
darienf 0:832122ce6748 71 * @param number Number of elements in the buffer
darienf 0:832122ce6748 72 */
darienf 0:832122ce6748 73 void StreamPacketUint32(uint32_t id, uint32_t *buffer, uint32_t number) {
darienf 0:832122ce6748 74 uint32_t checksumId;
darienf 0:832122ce6748 75 if (streaming == TRUE || dataLogging == TRUE) {
darienf 0:832122ce6748 76 checksumId = StreamIdChecksumCalculate(id);
darienf 0:832122ce6748 77 StreamFifoId(checksumId);
darienf 0:832122ce6748 78 StreamFifoTimeStamp();
darienf 0:832122ce6748 79 StreamFifoLength(number);
darienf 0:832122ce6748 80 StreamFifoUint32Array(buffer, number);
darienf 0:832122ce6748 81 }
darienf 0:832122ce6748 82 if (testing_max30001 == 1) {
darienf 0:832122ce6748 83 if (id == MAX30001_DATA_ECG)
darienf 0:832122ce6748 84 testing_ecg_flags[TESTING_ECG_FLAG] = 1;
darienf 0:832122ce6748 85 if (id == MAX30001_DATA_BIOZ)
darienf 0:832122ce6748 86 testing_ecg_flags[TESTING_BIOZ_FLAG] = 1;
darienf 0:832122ce6748 87 if (id == MAX30001_DATA_PACE)
darienf 0:832122ce6748 88 testing_ecg_flags[TESTING_PACE_FLAG] = 1;
darienf 0:832122ce6748 89 if (id == MAX30001_DATA_RTOR)
darienf 0:832122ce6748 90 testing_ecg_flags[TESTING_RTOR_FLAG] = 1;
darienf 0:832122ce6748 91 }
darienf 0:832122ce6748 92 if (testing_max30101 == 1) {
darienf 0:832122ce6748 93 if (id == (MAX30101_OXIMETER_DATA + 1))
darienf 0:832122ce6748 94 testing_max30101_flags[TESTING_HR_FLAG] = 1;
darienf 0:832122ce6748 95 if (id == (MAX30101_OXIMETER_DATA + 2))
darienf 0:832122ce6748 96 testing_max30101_flags[TESTING_SPO2_FLAG] = 1;
darienf 0:832122ce6748 97 if (id == (MAX30101_OXIMETER_DATA + 3))
darienf 0:832122ce6748 98 testing_max30101_flags[TESTING_MULTI_FLAG] = 1;
darienf 0:832122ce6748 99 }
darienf 0:832122ce6748 100 }
darienf 0:832122ce6748 101
darienf 0:832122ce6748 102 /**
darienf 0:832122ce6748 103 * @brief Insert a buffer into the out going fifo
darienf 0:832122ce6748 104 * @param buffer Array of uint32 to send to the fifo
darienf 0:832122ce6748 105 * @param len Length of the array
darienf 0:832122ce6748 106 */
darienf 0:832122ce6748 107 int StreamFifoUint32Array(uint32_t buffer[], uint32_t len) {
darienf 0:832122ce6748 108 int status;
darienf 0:832122ce6748 109 uint32_t i;
darienf 0:832122ce6748 110 for (i = 0; i < len; i++) {
darienf 0:832122ce6748 111 status = fifo_put32(GetStreamOutFifo(), buffer[i]);
darienf 0:832122ce6748 112 if (status == -1) {
darienf 0:832122ce6748 113 printf("FIFO_OF!");
darienf 0:832122ce6748 114 fflush(stdout);
darienf 0:832122ce6748 115 while (1)
darienf 0:832122ce6748 116 ;
darienf 0:832122ce6748 117 }
darienf 0:832122ce6748 118 }
darienf 0:832122ce6748 119 return 0;
darienf 0:832122ce6748 120 }
darienf 0:832122ce6748 121
darienf 0:832122ce6748 122 /**
darienf 0:832122ce6748 123 * @brief Insert a timestamp into the out going fifo
darienf 0:832122ce6748 124 */
darienf 0:832122ce6748 125 int StreamFifoTimeStamp(void) {
darienf 0:832122ce6748 126 int status;
darienf 0:832122ce6748 127 // uint32_t timer = timestamp_GetCurrent(); //RTC_GetVal();
darienf 0:832122ce6748 128 uint32_t timer = (uint32_t)Peripherals::timestampTimer()->read_us();
darienf 0:832122ce6748 129 status = fifo_put32(GetStreamOutFifo(), timer);
darienf 0:832122ce6748 130 if (status == -1) {
darienf 0:832122ce6748 131 printf("FIFO_OF!");
darienf 0:832122ce6748 132 fflush(stdout);
darienf 0:832122ce6748 133 while (1)
darienf 0:832122ce6748 134 ;
darienf 0:832122ce6748 135 }
darienf 0:832122ce6748 136 return 0;
darienf 0:832122ce6748 137 }
darienf 0:832122ce6748 138
darienf 0:832122ce6748 139 /**
darienf 0:832122ce6748 140 * @brief Insert a packet id into the out going fifo
darienf 0:832122ce6748 141 * @param id The uint32 packet id
darienf 0:832122ce6748 142 */
darienf 0:832122ce6748 143 int StreamFifoId(uint32_t id) {
darienf 0:832122ce6748 144 int status;
darienf 0:832122ce6748 145 status = fifo_put32(GetStreamOutFifo(), id);
darienf 0:832122ce6748 146 if (status == -1) {
darienf 0:832122ce6748 147 printf("FIFO_OF!");
darienf 0:832122ce6748 148 fflush(stdout);
darienf 0:832122ce6748 149 while (1)
darienf 0:832122ce6748 150 ;
darienf 0:832122ce6748 151 }
darienf 0:832122ce6748 152 return 0;
darienf 0:832122ce6748 153 }
darienf 0:832122ce6748 154
darienf 0:832122ce6748 155 /**
darienf 0:832122ce6748 156 * @brief Insert a length value into the out going fifo
darienf 0:832122ce6748 157 * @param length A uint32 number representing a length
darienf 0:832122ce6748 158 */
darienf 0:832122ce6748 159 int StreamFifoLength(uint32_t length) {
darienf 0:832122ce6748 160 int status;
darienf 0:832122ce6748 161 status = fifo_put32(GetStreamOutFifo(), length);
darienf 0:832122ce6748 162 if (status == -1) {
darienf 0:832122ce6748 163 printf("FIFO_OF!");
darienf 0:832122ce6748 164 fflush(stdout);
darienf 0:832122ce6748 165 while (1)
darienf 0:832122ce6748 166 ;
darienf 0:832122ce6748 167 }
darienf 0:832122ce6748 168 return 0;
darienf 0:832122ce6748 169 }
darienf 0:832122ce6748 170
darienf 0:832122ce6748 171 /**
darienf 0:832122ce6748 172 * @brief Return a value that indicates if the system is streaming data
darienf 0:832122ce6748 173 * @returns Returns a one or zero value
darienf 0:832122ce6748 174 */
darienf 0:832122ce6748 175 uint8_t IsStreaming(void) { return streaming; }
darienf 0:832122ce6748 176
darienf 0:832122ce6748 177 /**
darienf 0:832122ce6748 178 * @brief Set a flag to indicate if streaming is enabled
darienf 0:832122ce6748 179 * @param state A one or zero value
darienf 0:832122ce6748 180 */
darienf 0:832122ce6748 181 void SetStreaming(uint8_t state) { streaming = state; }
darienf 0:832122ce6748 182
darienf 0:832122ce6748 183 /**
darienf 0:832122ce6748 184 * @brief Set a flag to indicate if datalogging is enabled
darienf 0:832122ce6748 185 * @param state A one or zero value
darienf 0:832122ce6748 186 */
darienf 0:832122ce6748 187 void SetDataLoggingStream(uint8_t state) { dataLogging = state; }