This is the latest working repository used in our demo video for the Maxim to display temperature readings on Bluetooth

Dependencies:   USBDevice

Committer:
darienf
Date:
Sat Apr 10 03:05:42 2021 +0000
Revision:
3:36de8b9e4b1a
Parent:
HSP/main.cpp@0:832122ce6748
ayoooo

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 "mbed.h"
darienf 0:832122ce6748 34 #include "MAX14720.h"
darienf 0:832122ce6748 35 #include "MAX30101.h"
darienf 0:832122ce6748 36 #include "MAX30205.h"
darienf 0:832122ce6748 37 #include "LIS2DH.h"
darienf 0:832122ce6748 38 #include "USBSerial.h"
darienf 0:832122ce6748 39 #include "RpcServer.h"
darienf 0:832122ce6748 40 #include "StringInOut.h"
darienf 0:832122ce6748 41 #include "Peripherals.h"
darienf 0:832122ce6748 42 #include "BMP280.h"
darienf 0:832122ce6748 43 #include "MAX30001.h"
darienf 0:832122ce6748 44 #include "DataLoggingService.h"
darienf 0:832122ce6748 45 #include "MAX30101_helper.h"
darienf 0:832122ce6748 46 #include "S25FS512.h"
darienf 0:832122ce6748 47 #include "QuadSpiInterface.h"
darienf 0:832122ce6748 48 #include "PushButton.h"
darienf 0:832122ce6748 49 #include "BLE.h"
darienf 0:832122ce6748 50 #include "HspBLE.h"
darienf 0:832122ce6748 51 #include "USBSerial.h"
darienf 0:832122ce6748 52 #include "Streaming.h"
darienf 0:832122ce6748 53
darienf 0:832122ce6748 54 /// define the HVOUT Boost Voltage default for the MAX14720 PMIC
darienf 0:832122ce6748 55 #define HVOUT_VOLTAGE 4500 // set to 4500 mV
darienf 0:832122ce6748 56
darienf 0:832122ce6748 57 /// define all I2C addresses
darienf 0:832122ce6748 58 #define MAX30205_I2C_SLAVE_ADDR_TOP (0x92)
darienf 0:832122ce6748 59 #define MAX30205_I2C_SLAVE_ADDR_BOTTOM (0x90)
darienf 0:832122ce6748 60 #define MAX14720_I2C_SLAVE_ADDR (0x54)
darienf 0:832122ce6748 61 #define BMP280_I2C_SLAVE_ADDR (0xEC)
darienf 0:832122ce6748 62 #define MAX30101_I2C_SLAVE_ADDR (0xAE)
darienf 0:832122ce6748 63 #define LIS2DH_I2C_SLAVE_ADDR (0x32)
darienf 0:832122ce6748 64
darienf 0:832122ce6748 65 ///
darienf 0:832122ce6748 66 /// wire Interfaces
darienf 0:832122ce6748 67 ///
darienf 0:832122ce6748 68 /// Define with Maxim VID and a Maxim assigned PID, set to version 0x0001 and non-blocking
darienf 0:832122ce6748 69 USBSerial usbSerial(0x0b6a, 0x0100, 0x0001, false);
darienf 0:832122ce6748 70 /// I2C Master 1
darienf 0:832122ce6748 71 I2C i2c1(I2C1_SDA, I2C1_SCL); // used by MAX30205 (1), MAX30205 (2), BMP280
darienf 0:832122ce6748 72 /// I2C Master 2
darienf 0:832122ce6748 73 I2C i2c2(I2C2_SDA, I2C2_SCL); // used by MAX14720, MAX30101, LIS2DH
darienf 0:832122ce6748 74 /// SPI Master 0 with SPI0_SS for use with MAX30001
darienf 0:832122ce6748 75 SPI spi(SPI0_MOSI, SPI0_MISO, SPI0_SCK, SPI0_SS); // used by MAX30001
darienf 0:832122ce6748 76 /// SPI Master 1
darienf 0:832122ce6748 77 QuadSpiInterface quadSpiInterface(SPI1_MOSI, SPI1_MISO, SPI1_SCK,
darienf 0:832122ce6748 78 SPI1_SS); // used by S25FS512
darienf 0:832122ce6748 79
darienf 0:832122ce6748 80 ///
darienf 0:832122ce6748 81 /// Devices
darienf 0:832122ce6748 82 ///
darienf 0:832122ce6748 83 /// Pressure Sensor
darienf 0:832122ce6748 84 BMP280 bmp280(&i2c1, BMP280_I2C_SLAVE_ADDR);
darienf 0:832122ce6748 85 /// Top Local Temperature Sensor
darienf 0:832122ce6748 86 MAX30205 MAX30205_top(&i2c1, MAX30205_I2C_SLAVE_ADDR_TOP);
darienf 0:832122ce6748 87 /// Bottom Local Temperature Sensor
darienf 0:832122ce6748 88 MAX30205 MAX30205_bottom(&i2c1, MAX30205_I2C_SLAVE_ADDR_BOTTOM);
darienf 0:832122ce6748 89 /// Accelerometer
darienf 0:832122ce6748 90 LIS2DH lis2dh(&i2c2, LIS2DH_I2C_SLAVE_ADDR);
darienf 0:832122ce6748 91 InterruptIn lis2dh_Interrupt(P4_7);
darienf 0:832122ce6748 92 /// PMIC
darienf 0:832122ce6748 93 MAX14720 max14720(&i2c2, MAX14720_I2C_SLAVE_ADDR);
darienf 0:832122ce6748 94 /// Optical Oximeter
darienf 0:832122ce6748 95 MAX30101 max30101(&i2c2, MAX30101_I2C_SLAVE_ADDR);
darienf 0:832122ce6748 96 InterruptIn max30101_Interrupt(P4_0);
darienf 0:832122ce6748 97 /// External Flash
darienf 0:832122ce6748 98 S25FS512 s25fs512(&quadSpiInterface);
darienf 0:832122ce6748 99 /// ECG device
darienf 0:832122ce6748 100 MAX30001 max30001(&spi);
darienf 0:832122ce6748 101 InterruptIn max30001_InterruptB(P3_6);
darienf 0:832122ce6748 102 InterruptIn max30001_Interrupt2B(P4_5);
darienf 0:832122ce6748 103 /// PWM used as fclk for the MAX30001
darienf 0:832122ce6748 104 PwmOut pwmout(P1_7);
darienf 0:832122ce6748 105 /// HSP platform LED
darienf 0:832122ce6748 106 HspLed hspLed(LED_RED);
darienf 0:832122ce6748 107 /// Packet TimeStamp Timer, set for 1uS
darienf 0:832122ce6748 108 Timer timestampTimer;
darienf 0:832122ce6748 109 /// HSP Platform push button
darienf 0:832122ce6748 110 PushButton pushButton(SW1);
darienf 0:832122ce6748 111
darienf 0:832122ce6748 112 /// BLE instance
darienf 0:832122ce6748 113 static BLE ble;
darienf 0:832122ce6748 114
darienf 0:832122ce6748 115 /// HSP BluetoothLE specific functions
darienf 0:832122ce6748 116 HspBLE hspBLE(&ble);
darienf 0:832122ce6748 117 USBSerial *usbSerialPtr;
darienf 0:832122ce6748 118
darienf 0:832122ce6748 119 int main() {
darienf 0:832122ce6748 120 // hold results for returning funtcoins
darienf 0:832122ce6748 121 int result;
darienf 0:832122ce6748 122 // local input state of the RPC
darienf 0:832122ce6748 123 int inputState;
darienf 0:832122ce6748 124 // RPC request buffer
darienf 0:832122ce6748 125 char request[128];
darienf 0:832122ce6748 126 // RPC reply buffer
darienf 0:832122ce6748 127 char reply[128];
darienf 0:832122ce6748 128
darienf 0:832122ce6748 129 // display start banner
darienf 0:832122ce6748 130 printf("Maxim Integrated mbed hSensor 3.0.0 10/14/16\n");
darienf 0:832122ce6748 131 fflush(stdout);
darienf 0:832122ce6748 132
darienf 0:832122ce6748 133 // initialize HVOUT on the MAX14720 PMIC
darienf 0:832122ce6748 134 printf("Init MAX14720...\n");
darienf 0:832122ce6748 135 fflush(stdout);
darienf 0:832122ce6748 136 result = max14720.init();
darienf 0:832122ce6748 137 if (result == MAX14720_ERROR)
darienf 0:832122ce6748 138 printf("Error initializing MAX14720");
darienf 0:832122ce6748 139 max14720.boostEn = MAX14720::BOOST_ENABLED;
darienf 0:832122ce6748 140 max14720.boostSetVoltage(HVOUT_VOLTAGE);
darienf 0:832122ce6748 141
darienf 0:832122ce6748 142 // turn on red led
darienf 0:832122ce6748 143 printf("Init HSPLED...\n");
darienf 0:832122ce6748 144 fflush(stdout);
darienf 0:832122ce6748 145 hspLed.on();
darienf 0:832122ce6748 146
darienf 0:832122ce6748 147 // set NVIC priorities for GPIO to prevent priority inversion
darienf 0:832122ce6748 148 printf("Init NVIC Priorities...\n");
darienf 0:832122ce6748 149 fflush(stdout);
darienf 0:832122ce6748 150 NVIC_SetPriority(GPIO_P0_IRQn, 5);
darienf 0:832122ce6748 151 NVIC_SetPriority(GPIO_P1_IRQn, 5);
darienf 0:832122ce6748 152 NVIC_SetPriority(GPIO_P2_IRQn, 5);
darienf 0:832122ce6748 153 NVIC_SetPriority(GPIO_P3_IRQn, 5);
darienf 0:832122ce6748 154 NVIC_SetPriority(GPIO_P4_IRQn, 5);
darienf 0:832122ce6748 155 NVIC_SetPriority(GPIO_P5_IRQn, 5);
darienf 0:832122ce6748 156 NVIC_SetPriority(GPIO_P6_IRQn, 5);
darienf 0:832122ce6748 157 // used by the MAX30001
darienf 0:832122ce6748 158 NVIC_SetPriority(SPI1_IRQn, 0);
darienf 0:832122ce6748 159
darienf 0:832122ce6748 160 // Be able to statically reference these devices anywhere in the application
darienf 0:832122ce6748 161 Peripherals::setS25FS512(&s25fs512);
darienf 0:832122ce6748 162 Peripherals::setMAX30205_top(&MAX30205_top);
darienf 0:832122ce6748 163 Peripherals::setMAX30205_bottom(&MAX30205_bottom);
darienf 0:832122ce6748 164 Peripherals::setBMP280(&bmp280);
darienf 0:832122ce6748 165 Peripherals::setLIS2DH(&lis2dh);
darienf 0:832122ce6748 166 //Peripherals::setUSBSerial(&usbSerial);
darienf 0:832122ce6748 167 Peripherals::setTimestampTimer(&timestampTimer);
darienf 0:832122ce6748 168 Peripherals::setHspLed(&hspLed);
darienf 0:832122ce6748 169 Peripherals::setMAX30101(&max30101);
darienf 0:832122ce6748 170 Peripherals::setI2c1(&i2c1);
darienf 0:832122ce6748 171 Peripherals::setI2c2(&i2c2);
darienf 0:832122ce6748 172 Peripherals::setPushButton(&pushButton);
darienf 0:832122ce6748 173 Peripherals::setBLE(&ble);
darienf 0:832122ce6748 174 Peripherals::setMAX14720(&max14720);
darienf 0:832122ce6748 175 Peripherals::setMAX30001(&max30001);
darienf 0:832122ce6748 176 Peripherals::setHspBLE(&hspBLE);
darienf 0:832122ce6748 177 usbSerialPtr = &usbSerial;
darienf 0:832122ce6748 178
darienf 0:832122ce6748 179 // init the S25FS256 external flash device
darienf 0:832122ce6748 180 printf("Init S25FS512...\n");
darienf 0:832122ce6748 181 fflush(stdout);
darienf 0:832122ce6748 182 s25fs512.init();
darienf 0:832122ce6748 183
darienf 0:832122ce6748 184 // init the BMP280
darienf 0:832122ce6748 185 printf("Init BMP280...\n");
darienf 0:832122ce6748 186 fflush(stdout);
darienf 0:832122ce6748 187 bmp280.init(BMP280::OVERSAMPLING_X2_P, BMP280::OVERSAMPLING_X1_T,
darienf 0:832122ce6748 188 BMP280::FILT_OFF, BMP280::NORMAL_MODE, BMP280::T_62_5);
darienf 0:832122ce6748 189
darienf 0:832122ce6748 190 // Initialize BLE base layer
darienf 0:832122ce6748 191 printf("Init HSPBLE...\n");
darienf 0:832122ce6748 192 fflush(stdout);
darienf 0:832122ce6748 193 hspBLE.init();
darienf 0:832122ce6748 194
darienf 0:832122ce6748 195 // start blinking led1
darienf 0:832122ce6748 196 printf("Init HSPLED Blink...\n");
darienf 0:832122ce6748 197 fflush(stdout);
darienf 0:832122ce6748 198 hspLed.blink(1000);
darienf 0:832122ce6748 199
darienf 0:832122ce6748 200 // MAX30101 initialize interrupt
darienf 0:832122ce6748 201 printf("Init MAX30101 callbacks, interrupt...\n");
darienf 0:832122ce6748 202 fflush(stdout);
darienf 0:832122ce6748 203 max30101.onInterrupt(&MAX30101_OnInterrupt);
darienf 0:832122ce6748 204 max30101.onDataAvailable(&StreamPacketUint32);
darienf 0:832122ce6748 205 max30101_Interrupt.fall(&MAX30101::MidIntHandler);
darienf 0:832122ce6748 206
darienf 0:832122ce6748 207 //
darienf 0:832122ce6748 208 // MAX30001
darienf 0:832122ce6748 209 //
darienf 0:832122ce6748 210 printf("Init MAX30001 callbacks, interrupts...\n");
darienf 0:832122ce6748 211 fflush(stdout);
darienf 0:832122ce6748 212 max30001_InterruptB.disable_irq();
darienf 0:832122ce6748 213 max30001_Interrupt2B.disable_irq();
darienf 0:832122ce6748 214 max30001_InterruptB.mode(PullUp);
darienf 0:832122ce6748 215 max30001_InterruptB.fall(&MAX30001::Mid_IntB_Handler);
darienf 0:832122ce6748 216 max30001_Interrupt2B.mode(PullUp);
darienf 0:832122ce6748 217 max30001_Interrupt2B.fall(&MAX30001::Mid_Int2B_Handler);
darienf 0:832122ce6748 218 max30001_InterruptB.enable_irq();
darienf 0:832122ce6748 219 max30001_Interrupt2B.enable_irq();
darienf 0:832122ce6748 220 max30001.AllowInterrupts(1);
darienf 0:832122ce6748 221 // Configuring the FCLK for the ECG, set to 32.768KHZ
darienf 0:832122ce6748 222 printf("Init MAX30001 PWM...\n");
darienf 0:832122ce6748 223 fflush(stdout);
darienf 0:832122ce6748 224 //pwmout.period_us(31);
darienf 0:832122ce6748 225 //pwmout.write(0.5); // 0-1 is 0-100%, 0.5 = 50% duty cycle.
darienf 0:832122ce6748 226 max30001.FCLK_MaximOnly(); // mbed does not provide the resolution necessary, so for now we have a specific solution...
darienf 0:832122ce6748 227 max30001.sw_rst(); // Do a software reset of the MAX30001
darienf 0:832122ce6748 228 max30001.INT_assignment(MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_enint_loc, en_eovf_loc, en_fstint_loc,
darienf 0:832122ce6748 229 MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_dcloffint_loc, en_bint_loc, en_bovf_loc,
darienf 0:832122ce6748 230 MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_bover_loc, en_bundr_loc, en_bcgmon_loc,
darienf 0:832122ce6748 231 MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_pint_loc, en_povf_loc, en_pedge_loc,
darienf 0:832122ce6748 232 MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, // en_lonint_loc, en_rrint_loc, en_samp_loc,
darienf 0:832122ce6748 233 MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR); // intb_Type, int2b_Type)
darienf 0:832122ce6748 234 max30001.onDataAvailable(&StreamPacketUint32);
darienf 0:832122ce6748 235
darienf 0:832122ce6748 236 // initialize the LIS2DH accelerometer and interrupts
darienf 0:832122ce6748 237 printf("Init LIS2DH interrupt...\n");
darienf 0:832122ce6748 238 fflush(stdout);
darienf 0:832122ce6748 239 lis2dh.init();
darienf 0:832122ce6748 240 lis2dh_Interrupt.fall(&LIS2DHIntHandler);
darienf 0:832122ce6748 241 lis2dh_Interrupt.mode(PullUp);
darienf 0:832122ce6748 242 // initialize the RPC server
darienf 0:832122ce6748 243 printf("Init RPC Server...\n");
darienf 0:832122ce6748 244 fflush(stdout);
darienf 0:832122ce6748 245 RPC_init();
darienf 0:832122ce6748 246 // initialize the logging service
darienf 0:832122ce6748 247 printf("Init LoggingService...\n");
darienf 0:832122ce6748 248 fflush(stdout);
darienf 0:832122ce6748 249 LoggingService_Init();
darienf 0:832122ce6748 250
darienf 0:832122ce6748 251 // start main loop
darienf 0:832122ce6748 252 printf("Start main loop...\n");
darienf 0:832122ce6748 253 fflush(stdout);
darienf 0:832122ce6748 254 while (1) {
darienf 0:832122ce6748 255 // get a RPC string if one is available
darienf 0:832122ce6748 256 inputState = getLine(request, sizeof(request));
darienf 0:832122ce6748 257 // if a string has been captured, process string
darienf 0:832122ce6748 258 if (inputState == GETLINE_DONE) {
darienf 0:832122ce6748 259 // process the RPC string
darienf 0:832122ce6748 260 RPC_call(request, reply);
darienf 0:832122ce6748 261 // output the reply
darienf 0:832122ce6748 262 putStr(reply);
darienf 0:832122ce6748 263 }
darienf 0:832122ce6748 264 // process any logging or streaming requests
darienf 0:832122ce6748 265 LoggingService_ServiceRoutine();
darienf 0:832122ce6748 266 // determine if we are doing a large USB transfer of flash datalog sensor data
darienf 0:832122ce6748 267 // skip updating the ble and sleeping, this conditional branch increases download
darienf 0:832122ce6748 268 // rates by about %450 (time to download 32M flash from 90 minutes to about 20 minutes)
darienf 0:832122ce6748 269 if (RPC_IsTransferingFlashPages() == false) {
darienf 0:832122ce6748 270 // allow for ble processing
darienf 0:832122ce6748 271 ble.waitForEvent();
darienf 0:832122ce6748 272 }
darienf 0:832122ce6748 273 }
darienf 0:832122ce6748 274 }