Maxim Integrated / Mbed OS MAX30208_Demo

Dependencies:   max32630fthr USBDevice

Committer:
tlyp
Date:
Wed Jul 15 15:06:32 2020 +0000
Revision:
0:d996cb30964c
Child:
1:bf1f12445c37
Testing Program for MAX30208 Temp Sensor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tlyp 0:d996cb30964c 1 /*******************************************************************************
tlyp 0:d996cb30964c 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
tlyp 0:d996cb30964c 3 *
tlyp 0:d996cb30964c 4 * Permission is hereby granted, free of charge, to any person obtaining a
tlyp 0:d996cb30964c 5 * copy of this software and associated documentation files (the "Software"),
tlyp 0:d996cb30964c 6 * to deal in the Software without restriction, including without limitation
tlyp 0:d996cb30964c 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tlyp 0:d996cb30964c 8 * and/or sell copies of the Software, and to permit persons to whom the
tlyp 0:d996cb30964c 9 * Software is furnished to do so, subject to the following conditions:
tlyp 0:d996cb30964c 10 *
tlyp 0:d996cb30964c 11 * The above copyright notice and this permission notice shall be included
tlyp 0:d996cb30964c 12 * in all copies or substantial portions of the Software.
tlyp 0:d996cb30964c 13 *
tlyp 0:d996cb30964c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tlyp 0:d996cb30964c 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tlyp 0:d996cb30964c 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tlyp 0:d996cb30964c 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tlyp 0:d996cb30964c 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tlyp 0:d996cb30964c 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tlyp 0:d996cb30964c 20 * OTHER DEALINGS IN THE SOFTWARE.
tlyp 0:d996cb30964c 21 *
tlyp 0:d996cb30964c 22 * Except as contained in this notice, the name of Maxim Integrated
tlyp 0:d996cb30964c 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
tlyp 0:d996cb30964c 24 * Products, Inc. Branding Policy.
tlyp 0:d996cb30964c 25 *
tlyp 0:d996cb30964c 26 * The mere transfer of this software does not imply any licenses
tlyp 0:d996cb30964c 27 * of trade secrets, proprietary technology, copyrights, patents,
tlyp 0:d996cb30964c 28 * trademarks, maskwork rights, or any other form of intellectual
tlyp 0:d996cb30964c 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
tlyp 0:d996cb30964c 30 * ownership rights.
tlyp 0:d996cb30964c 31 *******************************************************************************
tlyp 0:d996cb30964c 32 */
tlyp 0:d996cb30964c 33 #include "mbed.h"
tlyp 0:d996cb30964c 34 #include "max32630fthr.h"
tlyp 0:d996cb30964c 35 #include "USBSerial.h"
tlyp 0:d996cb30964c 36 #include "MAX30208.h"
tlyp 0:d996cb30964c 37
tlyp 0:d996cb30964c 38 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
tlyp 0:d996cb30964c 39
tlyp 0:d996cb30964c 40 // Hardware serial port over DAPLink
tlyp 0:d996cb30964c 41 Serial uart(P2_1, P2_0);
tlyp 0:d996cb30964c 42
tlyp 0:d996cb30964c 43 // Virtual serial port over USB
tlyp 0:d996cb30964c 44 USBSerial microUSB;
tlyp 0:d996cb30964c 45 DigitalOut rLED(LED1);
tlyp 0:d996cb30964c 46 DigitalOut gLED(LED2);
tlyp 0:d996cb30964c 47 DigitalOut bLED(LED3);
tlyp 0:d996cb30964c 48
tlyp 0:d996cb30964c 49 I2C i2c(P3_4, P3_5); //sda,scl
tlyp 0:d996cb30964c 50
tlyp 0:d996cb30964c 51 MAX30208 TempSensor(i2c, 0x50); //Constructor takes 7-bit slave adrs
tlyp 0:d996cb30964c 52
tlyp 0:d996cb30964c 53 int main()
tlyp 0:d996cb30964c 54 {
tlyp 0:d996cb30964c 55
tlyp 0:d996cb30964c 56 microUSB.printf("micro USB serial port\r\n");
tlyp 0:d996cb30964c 57 rLED = LED_ON;
tlyp 0:d996cb30964c 58 gLED = LED_ON;
tlyp 0:d996cb30964c 59 bLED = LED_OFF;
tlyp 0:d996cb30964c 60
tlyp 0:d996cb30964c 61 rLED = LED_OFF;
tlyp 0:d996cb30964c 62
tlyp 0:d996cb30964c 63 MAX30208::Configuration_InterruptEnable InterruptEnable;
tlyp 0:d996cb30964c 64 MAX30208::Configuration_FIFOConfig2 FIFOConfig2;
tlyp 0:d996cb30964c 65 MAX30208::Configuration_GPIOSetup GPIOSetup;
tlyp 0:d996cb30964c 66 MAX30208::Configuration_GPIOControl GPIOControl;
tlyp 0:d996cb30964c 67
tlyp 0:d996cb30964c 68
tlyp 0:d996cb30964c 69 uint16_t val;
tlyp 0:d996cb30964c 70
tlyp 0:d996cb30964c 71 while(1) {
tlyp 0:d996cb30964c 72
tlyp 0:d996cb30964c 73 TempSensor.takeDataMeasurment();
tlyp 0:d996cb30964c 74 wait_ms(50);
tlyp 0:d996cb30964c 75 microUSB.printf("Before = %d\r\n",val);
tlyp 0:d996cb30964c 76 TempSensor.readData(val);
tlyp 0:d996cb30964c 77 microUSB.printf("After = %d\r\n",val);
tlyp 0:d996cb30964c 78 float temp = TempSensor.toCelsius(val);
tlyp 0:d996cb30964c 79 microUSB.printf("Temp C = %f\r\n",temp);
tlyp 0:d996cb30964c 80 temp = TempSensor.toFahrenheit(temp);
tlyp 0:d996cb30964c 81 microUSB.printf("Temp F = %f\r\n",temp);
tlyp 0:d996cb30964c 82
tlyp 0:d996cb30964c 83
tlyp 0:d996cb30964c 84
tlyp 0:d996cb30964c 85 //readInterruptRegister
tlyp 0:d996cb30964c 86 if (TempSensor.readInterruptRegister(InterruptEnable) == 0){
tlyp 0:d996cb30964c 87 microUSB.printf("InterruptRegister intial config = %d\r\n", InterruptEnable.all);
tlyp 0:d996cb30964c 88 }
tlyp 0:d996cb30964c 89 InterruptEnable.config.TEMP_HI_EN = 1;
tlyp 0:d996cb30964c 90 InterruptEnable.config.TEMP_LO_EN = 1;
tlyp 0:d996cb30964c 91 if (TempSensor.writeInterruptRegister(InterruptEnable) != 0){
tlyp 0:d996cb30964c 92 microUSB.printf("Error writing Interrupt register\r\n");
tlyp 0:d996cb30964c 93 }
tlyp 0:d996cb30964c 94
tlyp 0:d996cb30964c 95 //readFIFOConfig2
tlyp 0:d996cb30964c 96 if (TempSensor.readFIFOConfig2(FIFOConfig2) == 0){
tlyp 0:d996cb30964c 97 microUSB.printf("FIFOConfig2 initial config = %d\r\n", FIFOConfig2.all);
tlyp 0:d996cb30964c 98 }
tlyp 0:d996cb30964c 99 FIFOConfig2.config.FIFO_RO = 1;
tlyp 0:d996cb30964c 100 FIFOConfig2.config.A_FULL_TYPE = 1;
tlyp 0:d996cb30964c 101 if (TempSensor.writeFIFOConfig2(FIFOConfig2) != 0){
tlyp 0:d996cb30964c 102 microUSB.printf("Error writing FIFO Config2 register\r\n");
tlyp 0:d996cb30964c 103 }
tlyp 0:d996cb30964c 104
tlyp 0:d996cb30964c 105 //readGPIOSetup
tlyp 0:d996cb30964c 106 if (TempSensor.readGPIOSetup(GPIOSetup) == 0){
tlyp 0:d996cb30964c 107 microUSB.printf("GPIOSetup initial config = %d\r\n", GPIOSetup.all);
tlyp 0:d996cb30964c 108 }
tlyp 0:d996cb30964c 109 GPIOSetup.config.GPIO0_MODE = 2;
tlyp 0:d996cb30964c 110 GPIOSetup.config.GPIO1_MODE = 3;
tlyp 0:d996cb30964c 111 if (TempSensor.writeGPIOSetup(GPIOSetup) != 0){
tlyp 0:d996cb30964c 112 microUSB.printf("Error writing GPIO Setup register\r\n");
tlyp 0:d996cb30964c 113 }
tlyp 0:d996cb30964c 114
tlyp 0:d996cb30964c 115 //readGPIOControl
tlyp 0:d996cb30964c 116 if (TempSensor.readGPIOControl(GPIOControl) == 0){
tlyp 0:d996cb30964c 117 microUSB.printf("GPIOControl initial config = %d\r\n", GPIOControl.all);
tlyp 0:d996cb30964c 118 }
tlyp 0:d996cb30964c 119 GPIOControl.config.GPIO0_LL = 1;
tlyp 0:d996cb30964c 120 GPIOControl.config.GPIO1_LL = 1;
tlyp 0:d996cb30964c 121 if (TempSensor.writeGPIOControl(GPIOControl) != 0){
tlyp 0:d996cb30964c 122 microUSB.printf("Error writing register\r\n");
tlyp 0:d996cb30964c 123 }
tlyp 0:d996cb30964c 124
tlyp 0:d996cb30964c 125 //writeInterruptRegister
tlyp 0:d996cb30964c 126 if (TempSensor.readInterruptRegister(InterruptEnable) == 0){
tlyp 0:d996cb30964c 127 microUSB.printf("Interrupt Register new config = %d\r\n", InterruptEnable.all);
tlyp 0:d996cb30964c 128 }
tlyp 0:d996cb30964c 129 InterruptEnable.config.TEMP_HI_EN = 0;
tlyp 0:d996cb30964c 130 InterruptEnable.config.TEMP_LO_EN = 0;
tlyp 0:d996cb30964c 131 if (TempSensor.writeInterruptRegister(InterruptEnable) != 0){
tlyp 0:d996cb30964c 132 microUSB.printf("Error writing Interrupt Registerregister\r\n");
tlyp 0:d996cb30964c 133 }
tlyp 0:d996cb30964c 134
tlyp 0:d996cb30964c 135 //writeFIFOConfig2
tlyp 0:d996cb30964c 136 if (TempSensor.readFIFOConfig2(FIFOConfig2) == 0){
tlyp 0:d996cb30964c 137 microUSB.printf("FIFOConfig2 new config = %d\r\n", FIFOConfig2.all);
tlyp 0:d996cb30964c 138 }
tlyp 0:d996cb30964c 139 FIFOConfig2.config.FIFO_RO = 0;
tlyp 0:d996cb30964c 140 FIFOConfig2.config.A_FULL_TYPE = 0;
tlyp 0:d996cb30964c 141 if (TempSensor.writeFIFOConfig2(FIFOConfig2) != 0){
tlyp 0:d996cb30964c 142 microUSB.printf("Error writing register\r\n");
tlyp 0:d996cb30964c 143 }
tlyp 0:d996cb30964c 144
tlyp 0:d996cb30964c 145 //writeGPIOSetup
tlyp 0:d996cb30964c 146 if (TempSensor.readGPIOSetup(GPIOSetup) == 0){
tlyp 0:d996cb30964c 147 microUSB.printf("GPIOSetup new config = %d\r\n", GPIOSetup.all);
tlyp 0:d996cb30964c 148 }
tlyp 0:d996cb30964c 149 GPIOSetup.config.GPIO0_MODE = 0;
tlyp 0:d996cb30964c 150 GPIOSetup.config.GPIO1_MODE = 0;
tlyp 0:d996cb30964c 151 if (TempSensor.writeGPIOSetup(GPIOSetup) != 0){
tlyp 0:d996cb30964c 152 microUSB.printf("Error writing register\r\n");
tlyp 0:d996cb30964c 153 }
tlyp 0:d996cb30964c 154
tlyp 0:d996cb30964c 155 /*//readGPIOControl
tlyp 0:d996cb30964c 156 if (TempSensor.readGPIOControl(GPIOControl) == 0){
tlyp 0:d996cb30964c 157 microUSB.printf("GPIOControl new config = %d\r\n", GPIOControl.all);
tlyp 0:d996cb30964c 158 }
tlyp 0:d996cb30964c 159 GPIOControl.config.GPIO0_LL = 0;
tlyp 0:d996cb30964c 160 GPIOControl.config.GPIO1_LL = 0;
tlyp 0:d996cb30964c 161 if (TempSensor.writeGPIOControl(GPIOControl) != 0){
tlyp 0:d996cb30964c 162 microUSB.printf("Error writing register\r\n");
tlyp 0:d996cb30964c 163 }
tlyp 0:d996cb30964c 164 */
tlyp 0:d996cb30964c 165 TempSensor.resetDevice();
tlyp 0:d996cb30964c 166
tlyp 0:d996cb30964c 167 //NEED MORE WORK HERE, GOTTA CHECK A CONDITION ON THIS GUY
tlyp 0:d996cb30964c 168 //TempSensor.readStatus()
tlyp 0:d996cb30964c 169
tlyp 0:d996cb30964c 170 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 171 uint16_t PointerValue;
tlyp 0:d996cb30964c 172
tlyp 0:d996cb30964c 173 //Write Pointer Test
tlyp 0:d996cb30964c 174 TempSensor.readWritePointer(PointerValue);
tlyp 0:d996cb30964c 175 microUSB.printf("Current Write Pointer Value: %d\r\n",PointerValue);
tlyp 0:d996cb30964c 176 microUSB.printf("New Value Measured\r\n");
tlyp 0:d996cb30964c 177 TempSensor.takeDataMeasurment();
tlyp 0:d996cb30964c 178 wait_ms(50);
tlyp 0:d996cb30964c 179 TempSensor.readWritePointer(PointerValue);
tlyp 0:d996cb30964c 180 microUSB.printf("Current Write Pointer Value: %d\r\n",PointerValue);
tlyp 0:d996cb30964c 181
tlyp 0:d996cb30964c 182 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 183
tlyp 0:d996cb30964c 184 //Read Pointer Test
tlyp 0:d996cb30964c 185 TempSensor.readReadPointer(PointerValue);
tlyp 0:d996cb30964c 186 microUSB.printf("Current Read Pointer Value: %d\r\n",PointerValue);
tlyp 0:d996cb30964c 187 TempSensor.readData(val);
tlyp 0:d996cb30964c 188 microUSB.printf("New Value Read\r\n");
tlyp 0:d996cb30964c 189 TempSensor.readReadPointer(PointerValue);
tlyp 0:d996cb30964c 190 microUSB.printf("Current Read Pointer Value: %d\r\n",PointerValue);
tlyp 0:d996cb30964c 191 microUSB.printf("Read Pointer Written to '15'\r\n");
tlyp 0:d996cb30964c 192 TempSensor.writeReadPointer(0x0F);
tlyp 0:d996cb30964c 193 TempSensor.readReadPointer(PointerValue);
tlyp 0:d996cb30964c 194 microUSB.printf("Current Read Pointer Value: %d\r\n",PointerValue);
tlyp 0:d996cb30964c 195
tlyp 0:d996cb30964c 196 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 197
tlyp 0:d996cb30964c 198 uint16_t Counter;
tlyp 0:d996cb30964c 199 TempSensor.resetDevice();
tlyp 0:d996cb30964c 200
tlyp 0:d996cb30964c 201 //Data Counter
tlyp 0:d996cb30964c 202 TempSensor.readDataCounter(Counter);
tlyp 0:d996cb30964c 203 microUSB.printf("Initial Data Read: %d\r\n",Counter);
tlyp 0:d996cb30964c 204 microUSB.printf("Reading 16 temperatures\r\n");
tlyp 0:d996cb30964c 205 for(int i=0;i<16;i++){
tlyp 0:d996cb30964c 206 TempSensor.takeDataMeasurment();
tlyp 0:d996cb30964c 207 wait_ms(50);
tlyp 0:d996cb30964c 208 }
tlyp 0:d996cb30964c 209 TempSensor.readDataCounter(Counter);
tlyp 0:d996cb30964c 210 microUSB.printf("Curent Data Read: %d\r\n",Counter);
tlyp 0:d996cb30964c 211
tlyp 0:d996cb30964c 212 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 213 //Data Overflow
tlyp 0:d996cb30964c 214 microUSB.printf("Reading 30 temperatures\r\n");
tlyp 0:d996cb30964c 215 for (int i =0;i<30;i++){
tlyp 0:d996cb30964c 216 TempSensor.takeDataMeasurment();
tlyp 0:d996cb30964c 217 wait_ms(50);
tlyp 0:d996cb30964c 218 }
tlyp 0:d996cb30964c 219 TempSensor.readOverflow(Counter);
tlyp 0:d996cb30964c 220 microUSB.printf("Overflow Counter: %d\r\n",Counter);
tlyp 0:d996cb30964c 221
tlyp 0:d996cb30964c 222 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 223 uint16_t config;
tlyp 0:d996cb30964c 224 TempSensor.resetDevice();
tlyp 0:d996cb30964c 225 //Config1
tlyp 0:d996cb30964c 226 TempSensor.readFIFOConfig1(config);
tlyp 0:d996cb30964c 227 microUSB.printf("FIFO Config init Value: %d\r\n",config);
tlyp 0:d996cb30964c 228 microUSB.printf("Writing '3' to FIFO Config 1\r\n");
tlyp 0:d996cb30964c 229 TempSensor.writeFIFOConfig1(0x03);
tlyp 0:d996cb30964c 230 TempSensor.readFIFOConfig1(config);
tlyp 0:d996cb30964c 231 microUSB.printf("FIFO Config Value: %d\r\n",config);
tlyp 0:d996cb30964c 232
tlyp 0:d996cb30964c 233 microUSB.printf("\r\n\r\n");
tlyp 0:d996cb30964c 234 TempSensor.resetDevice();
tlyp 0:d996cb30964c 235 //read/write alarm
tlyp 0:d996cb30964c 236 uint16_t HI;
tlyp 0:d996cb30964c 237 uint16_t LO;
tlyp 0:d996cb30964c 238 TempSensor.readAlarmHigh(HI);
tlyp 0:d996cb30964c 239 TempSensor.readAlarmLow(LO);
tlyp 0:d996cb30964c 240
tlyp 0:d996cb30964c 241 microUSB.printf("Inital Alarm High: %d\r\n",HI);
tlyp 0:d996cb30964c 242 microUSB.printf("Inital Alarm Low: %d\r\n",LO);
tlyp 0:d996cb30964c 243 microUSB.printf("Setting High to '4321'\r\n");
tlyp 0:d996cb30964c 244 microUSB.printf("Setting High to '1234'\r\n");
tlyp 0:d996cb30964c 245 TempSensor.writeAlarmHigh(0x10E1);
tlyp 0:d996cb30964c 246 TempSensor.writeAlarmLow(0x04D2);
tlyp 0:d996cb30964c 247 TempSensor.readAlarmHigh(HI);
tlyp 0:d996cb30964c 248 TempSensor.readAlarmLow(LO);
tlyp 0:d996cb30964c 249
tlyp 0:d996cb30964c 250 microUSB.printf("Alarm High: %d\r\n",HI);
tlyp 0:d996cb30964c 251 microUSB.printf("Alarm Low: %d\r\n",LO);
tlyp 0:d996cb30964c 252
tlyp 0:d996cb30964c 253 microUSB.printf("\r\n\r\n\r\n\r\n");
tlyp 0:d996cb30964c 254 TempSensor.resetDevice();
tlyp 0:d996cb30964c 255 wait(5);
tlyp 0:d996cb30964c 256 }
tlyp 0:d996cb30964c 257 }
tlyp 0:d996cb30964c 258