Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX30101 mbed vt100
main.cpp
00001 #include "mbed.h" 00002 #include "vt100.h" 00003 #include "MSS.h" 00004 #include "MAX30101.h" 00005 /** 00006 * MAX30101 I2C addresss 00007 * Write 0xAE 0b1010_1110 00008 * Read 0xAF 0b1010_1111 00009 * 7bit address = 0b101_0111 = 0x57 00010 */ 00011 #define MAX30101_I2C_ADDRESS (0x57) 00012 00013 #ifndef FIFO_DEPTH 00014 #define FIFO_DEPTH 32 00015 #endif 00016 00017 vt100 *tty = 0 ; 00018 MAX30101 *max30101 = 0 ; 00019 InterruptIn *int0 ; 00020 00021 /* data read test following the pseudo code 00022 * written in the datasheet 00023 */ 00024 void doPseudo(void) 00025 { 00026 uint32_t data[3] ; 00027 uint8_t fifo_wr_ptr, fifo_rd_ptr ; 00028 int num_available_samples = 0 ; 00029 // int num_samples_to_read = 0 ; 00030 int i ; 00031 00032 fifo_wr_ptr = max30101->getFIFO_WR_PTR() ; 00033 fifo_rd_ptr = max30101->getFIFO_RD_PTR() ; 00034 num_available_samples = (fifo_wr_ptr + FIFO_DEPTH - fifo_rd_ptr) % FIFO_DEPTH ; 00035 printf("=== %d data ===\n", num_available_samples) ; 00036 for (i = 0 ; i < num_available_samples ; i++ ) { 00037 data[0] = max30101->readFIFO() ; /* LED1 */ 00038 data[1] = max30101->readFIFO() ; /* LED2 */ 00039 data[2] = max30101->readFIFO() ; /* LED3 */ 00040 printf("LED1: 0x%05X, LED2: 0x%05X, LED3: 0x%05X\n", 00041 data[0], data[1], data[2]) ; 00042 } 00043 } 00044 00045 void initSPO2(void) 00046 { 00047 uint8_t config_byte = 0 ; 00048 uint16_t slot_config = 0 ; 00049 00050 /* config for SPO2 (0x0A) */ 00051 config_byte = 00052 (0x01 << 5) /* SPO2_ADC_RGE */ 00053 // | (0x05 << 3) /* SPO2_SR 101 = 1000 samples/sec */ 00054 | (0x00 << 3) /* SPO2_SR 000 = 50 samples/sec */ 00055 | (0x00) /* LED_PW 00 = 69us */ 00056 ; 00057 max30101->setSPO2_CONFIG(config_byte) ; 00058 00059 /* slot configuration */ 00060 slot_config = 00061 (0x02 << 12) /* SLOT2 LED2(IR) */ 00062 | (0x01 << 8) /* SLOT1 LED1(RED) */ 00063 | (0x00 << 4) /* SLOT4 none */ 00064 | (0x00) /* SLOT3 none */ 00065 ; 00066 max30101->setSLOT(slot_config) ; 00067 00068 /* Inititalize FIFO */ 00069 /* config for FIFO Configuration (0x08) */ 00070 config_byte = 00071 (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ 00072 | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ 00073 // | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ 00074 | (0x09) /* FIFO ALMOST FULL at 9 */ 00075 ; 00076 max30101->setFIFO_CONFIG(config_byte) ; 00077 00078 00079 } 00080 00081 void readSPO2_FIFO(void) 00082 { 00083 uint32_t data[2] ; 00084 uint8_t fifo_wr_ptr, fifo_rd_ptr ; 00085 int num_available_samples = 0 ; 00086 int num_samples_to_read = 6 ; 00087 int i ; 00088 00089 fifo_wr_ptr = max30101->getFIFO_WR_PTR() ; 00090 fifo_rd_ptr = max30101->getFIFO_RD_PTR() ; 00091 num_available_samples = (fifo_wr_ptr + FIFO_DEPTH - fifo_rd_ptr) % FIFO_DEPTH ; 00092 00093 if (num_available_samples < num_samples_to_read) { 00094 num_samples_to_read = num_available_samples / 2 ; 00095 } else { 00096 num_samples_to_read = num_available_samples ; 00097 } 00098 printf("=== %d data %d to read===\n", 00099 num_available_samples, num_samples_to_read) ; 00100 for (i = 0 ; i < num_samples_to_read ; i++ ) { 00101 data[0] = max30101->readFIFO() ; /* LED1 */ 00102 data[1] = max30101->readFIFO() ; /* IR */ 00103 // printf("LED1: 0x%05X, IR: 0x%05X\n", data[0], data[1]) ; 00104 } 00105 // max30101->setFIFO_WR_PTR(0) ; 00106 // max30101->setFIFO_RD_PTR(0) ; 00107 } 00108 00109 void SPO2_isr(void) 00110 { 00111 uint16_t flag, config ; 00112 float temp ; 00113 int temp_int, temp_frac ; 00114 00115 flag = max30101->getIntStatus() ; 00116 config = max30101->getIntEnable() ; 00117 // printf("Int: 0x%04X Enable: 0x%04X\n", flag, config) ; 00118 if (flag & INT_ST_A_FULL) { /* A_FULL_EN */ 00119 // printf("FIFO almost full!\n") ; 00120 readSPO2_FIFO() ; 00121 } 00122 if (flag & INT_ST_PPG_RGY) { /* New FIFO Data Ready */ 00123 printf("New FIFO Data Ready\n") ; 00124 doPseudo() ; 00125 } 00126 if (flag & INT_ST_ALC_OVF) { /* Ambient Light Cancellaration Overflow */ 00127 printf("Ambient Light Cancellaration Overflow\n") ; 00128 } 00129 if (flag & INT_ST_PROX_INT) {/* Proximity Threshold Triggered */ 00130 printf("Proximity Threshold Triggered\n") ; 00131 } 00132 if (flag & INT_ST_PWR_RDY) {/* Power Ready Flag */ 00133 printf("Power Ready!\n") ; 00134 } 00135 if (flag & INT_ST_DIE_TEMP_RDY) {/* Internal Temperature Ready Flag */ 00136 printf("DIE Temperature Ready!\n") ; 00137 temp_int = max30101->getTEMP_INT() ; 00138 temp_frac = max30101->getTEMP_FRAC() ; 00139 temp = ((float)temp_int)+(((float)temp_frac)/16.0) ; 00140 printf("Temp: %.2f\n", temp) ; 00141 } 00142 } 00143 00144 void doSPO2(void) 00145 { 00146 uint8_t config_byte = 0 ; 00147 uint16_t slot_config = 0 ; 00148 uint16_t init_config = 0 ; 00149 00150 int0->fall(&SPO2_isr) ; 00151 /* Enter into SpO2 Mode. Initiate a Temperature measurement */ 00152 /* I2C Write Command sets MODE[2:0] = 0x03 */ 00153 config_byte = 0x03 ; /* SpO2 mode */ 00154 max30101->setMODE_CONFIG(config_byte) ; 00155 00156 /* Mask the SPO2_RDY Interrupt */ 00157 /* Temperature Measurement Complete, Interrupt Generated */ 00158 /* TEMP_RDY interrupt triggers */ 00159 /* alerting the central to read the data */ 00160 /* Temp Data is Read, Interrupt Cleared */ 00161 /* FIFO is Almost Full, Interrupt Generated */ 00162 /* FIFO Data is read, Interrupt Cleared */ 00163 /* Next Sample is Stored. */ 00164 00165 } 00166 00167 void initHR(void) 00168 { 00169 00170 } 00171 00172 void init(void) 00173 { 00174 uint32_t slot_config = 0 ; 00175 uint8_t mode ; 00176 int0 = new InterruptIn(PIN_INT1) ; 00177 tty = new vt100() ; 00178 tty->cls() ; 00179 max30101 = new MAX30101(PIN_SDA, PIN_SCL, MAX30101_I2C_ADDRESS) ; 00180 max30101->reset() ; 00181 00182 /* config for Mode Configuration (0x09) */ 00183 00184 mode = 0x07 ; /* Green, Red, and/or IR */ 00185 // mode = 0x03 ; /* SpO2 mode */ 00186 max30101->setMODE_CONFIG(mode) ; 00187 00188 /* config for FIFO Configuration (0x08) */ 00189 mode = 00190 // (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ 00191 (0x05 << 5) /* SMP_AVE = 5 -> avarage of 32 data */ 00192 | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ 00193 // | (0x00 << 4) /* FIFO_ROLLOVER_EN = 0 */ 00194 | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ 00195 ; 00196 max30101->setFIFO_CONFIG(mode) ; 00197 00198 max30101->setLED1_PA(0x80) ; 00199 max30101->setLED2_PA(0x80) ; 00200 max30101->setLED3_PA(0x80) ; 00201 max30101->setPILOT_PA(0x80) ; 00202 #if 1 00203 slot_config = 00204 (0x02 << 24) /* SLOT2 LED2(IR) */ 00205 | (0x01 << 16) /* SLOT1 LED1(RED) */ 00206 | (0x00 << 8) /* SLOT4 none */ 00207 | (0x03) /* SLOT3 LED3(GREEN) */ 00208 ; 00209 max30101->setSLOT(slot_config) ; 00210 #endif 00211 mode = 0x01 ; 00212 max30101->setPROX_INT_THR(mode) ; 00213 00214 // max30101->setIntEnable(0xF002) ; 00215 } 00216 00217 void reportID(void) 00218 { 00219 uint8_t id, rev ; 00220 id = max30101->getID() ; 00221 rev = max30101->getRev() ; 00222 printf("MAX30101 ID: 0x%02X, Rev: 0x%02X\n", id, rev) ; 00223 } 00224 00225 void doInt(void) 00226 { 00227 uint16_t flag ; 00228 flag = max30101->getIntStatus() ; 00229 printf("Int: 0x%04X\n", flag) ; 00230 if (flag & INT_ST_A_FULL) { /* A_FULL_EN */ 00231 printf("FIFO almost full!\n") ; 00232 } 00233 if (flag & INT_ST_PPG_RGY) { /* New FIFO Data Ready */ 00234 doPseudo() ; 00235 } 00236 if (flag & INT_ST_ALC_OVF) { /* Ambient Light Cancellaration Overflow */ 00237 printf("Ambient Light Cancellaration Overflow\n") ; 00238 } 00239 if (flag & INT_ST_PROX_INT) {/* Proximity Threshold Triggered */ 00240 doPseudo() ; 00241 } 00242 if (flag & INT_ST_PWR_RDY) {/* Power Ready Flag */ 00243 printf("Power Ready!\n") ; 00244 } 00245 if (flag & INT_ST_DIE_TEMP_RDY) {/* Internal Temperature Ready Flag */ 00246 printf("DIE Temperature Ready!\n") ; 00247 } 00248 max30101->setIntEnable(0xE002) ; 00249 } 00250 00251 void doHR(void) 00252 { 00253 } 00254 00255 int main() { 00256 uint16_t int_config = 0 ; 00257 uint8_t flag = 0 ; 00258 float temp = 0.0 ; 00259 00260 init() ; 00261 00262 printf("test MAX30101 for %s (%s)\n", BOARD_NAME, __DATE__) ; 00263 reportID() ; 00264 00265 initSPO2() ; 00266 int0->fall(&SPO2_isr) ; 00267 max30101->setMODE_CONFIG(MODE_SPO2) ; 00268 max30101->setIntEnable( INT_EN_A_FULL | INT_EN_DIE_TEMP_RDY | INT_ST_PPG_RGY ) ; 00269 int_config = max30101->getIntEnable() ; 00270 printf("Int enable: 0x%04X\n", int_config) ; 00271 printf("Temperature, Interrupt flags\n") ; 00272 00273 while(1) { 00274 max30101->setTEMP_EN() ; /* trigger temperature read */ 00275 temp = max30101->getTEMP() ; 00276 flag = max30101->getIntStatus() ; 00277 printf("%.2f, 0x%02X\n", temp, flag) ; 00278 wait(1) ; 00279 } 00280 }
Generated on Thu Jul 14 2022 03:54:26 by
1.7.2