Maxim Integrated / Mbed OS MAX30001-MAX32630FTHR-ECG-EVKIT

Dependencies:   SDFileSystem USBDevice max32630fthr

Fork of MAX30001 SYS EvKit by Emre Eken

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.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 "mbed.h"
00034 #include "max32630fthr.h"
00035 #include "USBSerial.h"
00036 #include "RpcServer.h"
00037 #include "StringInOut.h"
00038 #include "Peripherals.h"
00039 #include "MAX30001.h"
00040 #include "DataLoggingService.h"
00041 #include "PushButton.h"
00042 #include "USBSerial.h"
00043 #include "Streaming.h"
00044 #include "SDFileSystem.h"
00045 #include "version.h"
00046 
00047 
00048 //Init PMIC on FTHR board and set logic thresholds to 3.3V
00049 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00050 
00051 SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd");  // mosi, miso, sclk, cs
00052 
00053 ///
00054 /// wire Interfaces
00055 ///
00056 /// Define with Maxim VID and a Maxim assigned PID, set to version 0x0001 and non-blocking
00057 USBSerial usbSerial(0x0b6a, 0x7531, 0x0001, false);
00058 
00059 
00060 //SD card insertion detection pin
00061 DigitalIn SDDetect(P2_2, PullUp);
00062 
00063 
00064 /// DigitalOut for CS
00065 DigitalOut cs(P5_6);
00066 /// SPI Master 2 with SPI0_SS for use with MAX30001
00067 SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // used by MAX30001
00068 /// SPI Master 1
00069 QuadSpiInterface quadSpiInterface(SPI1_MOSI, SPI1_MISO, SPI1_SCK,
00070                                   SPI1_SS); // used by S25FS512
00071 ///Debug port
00072 Serial debug(USBTX, USBRX);
00073 
00074 ///
00075 /// Devices
00076 ///
00077 
00078 /// External Flash
00079 S25FS512 s25fs512(&quadSpiInterface);
00080 /// ECG device
00081 MAX30001 max30001(&spi, &cs);
00082 InterruptIn max30001_InterruptB(P5_5);
00083 InterruptIn max30001_Interrupt2B(P5_4);
00084 
00085 
00086 /// HSP platform LED
00087 HspLed hspLed(LED_RED);
00088 /// Packet TimeStamp Timer, set for 1uS
00089 Timer timestampTimer;
00090 /// HSP Platform push button
00091 PushButton pushButton(SW1);
00092 
00093 int main() {
00094   
00095   //boost baudrate so we can get messages while running gui
00096   debug.baud(115200);
00097   
00098   // local input state of the RPC
00099   int inputState;
00100   // RPC request buffer
00101   char request[128];
00102   // RPC reply buffer
00103   char reply[128];
00104 
00105   // display start banner
00106   debug.printf("Maxim Integrated mbed hSensor %d.%d.%d %02d/%02d/%02d\n", 
00107     VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, 
00108     VERSION_MONTH, VERSION_DAY, VERSION_SHORT_YEAR);
00109   fflush(stdout);
00110 
00111   // turn on red led
00112   debug.printf("Init HSPLED...\n");
00113   fflush(stdout);
00114   hspLed.on();
00115 
00116   // set NVIC priorities for GPIO to prevent priority inversion
00117   debug.printf("Init NVIC Priorities...\n");
00118   fflush(stdout);
00119   NVIC_SetPriority(GPIO_P0_IRQn, 5);
00120   NVIC_SetPriority(GPIO_P1_IRQn, 5);
00121   NVIC_SetPriority(GPIO_P2_IRQn, 5);
00122   NVIC_SetPriority(GPIO_P3_IRQn, 5);
00123   NVIC_SetPriority(GPIO_P4_IRQn, 5);
00124   NVIC_SetPriority(GPIO_P5_IRQn, 5);
00125   NVIC_SetPriority(GPIO_P6_IRQn, 5);
00126   // used by the MAX30001
00127   NVIC_SetPriority(SPIM2_IRQn, 0);
00128 
00129   // Be able to statically reference these devices anywhere in the application
00130   Peripherals::setS25FS512(&s25fs512);
00131   Peripherals::setUSBSerial(&usbSerial);
00132   Peripherals::setTimestampTimer(&timestampTimer);
00133   Peripherals::setHspLed(&hspLed);
00134   Peripherals::setPushButton(&pushButton);
00135   Peripherals::setMAX30001(&max30001);
00136   Peripherals::setSdFS(&sd);
00137   Peripherals::setSDDetect(&SDDetect);
00138 
00139   // init the S25FS256 external flash device
00140   debug.printf("Init S25FS512...\n");
00141   fflush(stdout);
00142   s25fs512.init();
00143 
00144   // start blinking led1
00145   debug.printf("Init HSPLED Blink...\n");
00146   fflush(stdout);
00147   hspLed.blink(1000);
00148 
00149   //
00150   // MAX30001
00151   //
00152   debug.printf("Init MAX30001 callbacks, interrupts...\n");
00153   fflush(stdout);
00154   max30001_InterruptB.disable_irq();
00155   max30001_Interrupt2B.disable_irq();
00156   max30001_InterruptB.mode(PullUp);
00157   max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler);
00158   max30001_Interrupt2B.mode(PullUp);
00159   max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler);
00160   max30001_InterruptB.enable_irq();
00161   max30001_Interrupt2B.enable_irq();
00162   MAX30001_AllowInterrupts(1);
00163   max30001.max30001_sw_rst(); // Do a software reset of the MAX30001
00164   max30001.max30001_INT_assignment(MAX30001::MAX30001_INT_B,    MAX30001::MAX30001_NO_INT,   MAX30001::MAX30001_NO_INT,  //  en_enint_loc,      en_eovf_loc,   en_fstint_loc,
00165                                      MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_NO_INT,  //  en_dcloffint_loc,  en_bint_loc,   en_bovf_loc,
00166                                      MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_NO_INT,  //  en_bover_loc,      en_bundr_loc,  en_bcgmon_loc,
00167                                      MAX30001::MAX30001_INT_B,    MAX30001::MAX30001_NO_INT,   MAX30001::MAX30001_NO_INT,  //  en_pint_loc,       en_povf_loc,   en_pedge_loc,
00168                                      MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_B,    MAX30001::MAX30001_NO_INT,  //  en_lonint_loc,     en_rrint_loc,  en_samp_loc,
00169                                      MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR);                            //  intb_Type,         int2b_Type)
00170   max30001.onDataAvailable(&StreamPacketUint32);
00171 
00172   // initialize the RPC server
00173   debug.printf("Init RPC Server...\n");
00174   fflush(stdout);
00175   RPC_init();
00176   // initialize the logging service
00177   debug.printf("Init LoggingService...\n");
00178   fflush(stdout);
00179   LoggingService_Init();
00180   // initialize the SD disk
00181   sd.disk_initialize();
00182 
00183   // start main loop
00184   debug.printf("Start main loop...\n");
00185   fflush(stdout);
00186   
00187   while (1) 
00188   {
00189     // get a RPC string if one is available
00190     inputState = getLine(request, sizeof(request));
00191     // if a string has been captured, process string
00192     if (inputState == GETLINE_DONE) 
00193     {
00194       //Send request to debug port
00195       debug.printf(request);
00196       // process the RPC string
00197       RPC_call(request, reply);
00198       //Send reply to debug port
00199       debug.printf(reply);
00200       // output the reply
00201       putStr(reply);
00202     }
00203     
00204     // process any logging or streaming requests
00205     LoggingService_ServiceRoutine();
00206   }
00207 }
00208