Ubiei Ubiei / Mbed OS FTHR_USBMSD_Demo_27

Dependencies:   USBMSD_BD max32630fthr USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "max32630fthr.h"
00003 #include "USBMSD_BD.h"
00004 #include "SDBlockDevice.h"
00005 #include "HeapBlockDevice.h"
00006 #include "FATFileSystem.h"
00007 
00008 #include "StringInOut.h"
00009 #include "Streaming.h"
00010 #include "Peripherals.h"
00011 #include "MAX30001.h"
00012 #include "RpcServer.h"
00013 #include "DataLoggingService.h"
00014 
00015 #include "Test_MAX30001.h"
00016 #include "Test_Utilities.h"
00017 
00018 #define BLOCK_SIZE   512
00019 
00020 
00021 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00022 
00023 DigitalOut rLED(LED1);
00024 DigitalOut gLED(LED2);
00025 DigitalOut bLED(LED3);
00026 
00027 DigitalOut outLED(P3_4);
00028 DigitalIn Button(P3_5);
00029 
00030 // Physical block device, can be any device that supports the BlockDevice API
00031 // HeapBlockDevice bd(512*BLOCK_SIZE, BLOCK_SIZE);
00032 //SDBlockDevice bd(P0_5, P0_6, P0_4, P0_7);
00033 
00034 // File system declaration
00035 //FATFileSystem fs("fs");
00036 
00037 //Another define the FAT File system and SD Card
00038 SDBlockDevice sd(P0_5, P0_6, P0_4, P0_7);
00039 FATFileSystem fs("sd", &sd);
00040 
00041 
00042 
00043 // USB MSD 
00044 //USBMSD_BD msd(&bd);  
00045 
00046 
00047 /// DigitalOut for CS
00048 DigitalOut cs(P5_6);
00049 /// SPI Master 2 with SPI0_SS for use with MAX30001
00050 SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // used by MAX30001
00051 
00052 /// ECG device
00053 MAX30001 max30001(&spi, &cs);
00054 InterruptIn max30001_InterruptB(P5_5);
00055 InterruptIn max30001_Interrupt2B(P5_4);
00056 
00057 // main() runs in its own thread in the OS
00058 // (note the calls to Thread::wait below for delays)
00059 
00060 uint32_t    ECG_Data[5120];
00061 uint32_t    BIOZ_Data[1280];
00062 uint32_t    PACE_Data[1280];
00063 uint32_t    RTOR_Data[1280];
00064 
00065 uint32_t    ECG_Data_Size = 0;
00066 uint32_t    BIOZ_Data_Size = 0;
00067 uint32_t    PACE_Data_Size = 0;
00068 uint32_t    RTOR_Data_Size = 0;
00069 
00070 uint32_t    global_key_judge = 0;
00071 int         current_log_number = 0;
00072 char        BIOZ_file_name[20] = "/sd/BIOZ";
00073 int         event_tag = 0;
00074 
00075 
00076 int main()
00077 {
00078     uint32_t     i, id;
00079     uint32_t    reg;
00080     int partVersion; // 0 = 30004
00081     
00082     int totalPass = 1;
00083     int pass;
00084     uint32_t foundEcg = 0;
00085     uint32_t foundBioz = 0;
00086     uint32_t foundPace = 0;
00087     uint32_t foundRtoR = 0;
00088     
00089     // local input state of the RPC
00090     int inputState;
00091     // RPC request buffer
00092     char request[128];
00093     // RPC reply buffer
00094     char reply[128];
00095     int  err;
00096     
00097     //int32_t i;
00098     printf("SD Card Example\n");
00099     gLED = LED_ON;
00100     rLED = LED_ON;
00101 
00102     FILE *fp = fopen("/sd/myfile12345678.txt", "a");
00103     for(i=0 ; i< 3000 ;  i++)
00104         fprintf(fp, "%s %d %s","Hello World!!!!", i, "\n");
00105     fclose(fp);
00106 
00107     rLED = LED_OFF;
00108     printf("SD Card Writing Over\n");
00109     
00110     // Open the index file
00111     printf("Opening \"/sd/index.txt\"... ");
00112     fflush(stdout);
00113     FILE *f_index = fopen("/sd/index.txt", "r+");
00114     //FILE *f_index;
00115     printf("%s\n", (!f_index ? "Fail :(" : "OK"));
00116     int index_number;
00117 
00118     if(!f_index)
00119     {
00120         printf("No index file found, creating a new index file...\n");
00121         fflush(stdout);
00122         f_index = fopen("/sd/index.txt", "w+");
00123         printf("Creat index file %s\n", (!f_index ? "Fail :(" : "OK"));
00124         index_number = current_log_number;
00125         fprintf(f_index, "%d", index_number);
00126         
00127         fclose(f_index);
00128     }
00129     
00130     int index_number2 = 0;
00131     printf("Index file found...\n");
00132     fflush(stdout);
00133     f_index = fopen("/sd/index.txt", "r+");
00134     
00135     printf("Open index.txt \n");
00136     printf("Openning %s\n", (!f_index ? "Fail :(" : "OK"));
00137         
00138     //Get current stream position
00139     long pos = ftell(f_index);
00140         
00141     // rewind(f_index);
00142 
00143     err = fscanf(f_index, "%d", &index_number2);
00144     printf("Scanning %s\n", (err < 0 ? "Fail :(" : "OK"));
00145     printf("err = %d\n", err);
00146         
00147     //index_number2 = fgetc(f_index);
00148     printf("index_number1 = %d \n", index_number2);
00149     index_number2 += 1;
00150         
00151     fseek(f_index, pos, SEEK_SET);
00152     printf("index_number2 = %d \n", index_number2);
00153     err = fprintf(f_index, "%d\n", index_number2);
00154     printf("Saving %s\n", (err < 0 ? "Fail :(" : "OK"));
00155         
00156     fflush(stdout);
00157     err = fclose(f_index);       
00158     printf("Closing %s\n", (err < 0 ? "Fail :(" : "OK"));
00159     current_log_number = index_number2;
00160     
00161     int j = 0;
00162     /*
00163     while (true) {
00164         gLED = !gLED;
00165         fp = fopen("/sd/myfile222.txt", "a");
00166         for(i=0 ; i< 300 ;  i++)
00167             fprintf(fp, "%s %d %d %s","Hello World!!!!", j, i, "\n");
00168         fclose(fp);
00169         j++;
00170         printf("write j %d\n", j);
00171         // ThisThread::sleep_for(500ms);
00172     }
00173     */
00174 
00175     printf("--- Mbed OS filesystem example 1 ---\n");
00176     rLED = LED_ON;
00177     gLED = LED_OFF;
00178     bLED = LED_OFF;
00179     
00180     // set NVIC priorities for GPIO to prevent priority inversion
00181     printf("Init NVIC Priorities...\n");
00182     fflush(stdout);
00183     NVIC_SetPriority(GPIO_P0_IRQn, 5);
00184     NVIC_SetPriority(GPIO_P1_IRQn, 5);
00185     NVIC_SetPriority(GPIO_P2_IRQn, 5);
00186     NVIC_SetPriority(GPIO_P3_IRQn, 5);
00187     NVIC_SetPriority(GPIO_P4_IRQn, 5);
00188     NVIC_SetPriority(GPIO_P5_IRQn, 5);
00189     NVIC_SetPriority(GPIO_P6_IRQn, 5);
00190 
00191     printf("Init MAX30001 callbacks, interrupts...\n");
00192     fflush(stdout);
00193     max30001_InterruptB.disable_irq();
00194     max30001_Interrupt2B.disable_irq();
00195     max30001_InterruptB.mode(PullUp);
00196     max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler);
00197     max30001_Interrupt2B.mode(PullUp);
00198     max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler);
00199     max30001_InterruptB.enable_irq();
00200     max30001_Interrupt2B.enable_irq();
00201     MAX30001_AllowInterrupts(1);
00202     max30001.max30001_sw_rst(); // Do a software reset of the MAX30001
00203 
00204     PushButton pushButton(SW1);
00205     Timer timer;
00206     
00207     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,
00208                                     MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_NO_INT,  //  en_dcloffint_loc,  en_bint_loc,   en_bovf_loc,
00209                                     MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_NO_INT,  //  en_bover_loc,      en_bundr_loc,  en_bcgmon_loc,
00210                                     MAX30001::MAX30001_INT_B,    MAX30001::MAX30001_NO_INT,   MAX30001::MAX30001_NO_INT,  //  en_pint_loc,       en_povf_loc,   en_pedge_loc,
00211                                     MAX30001::MAX30001_INT_2B,   MAX30001::MAX30001_INT_B,    MAX30001::MAX30001_NO_INT,  //  en_lonint_loc,     en_rrint_loc,  en_samp_loc,
00212                                     MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR);                            //  intb_Type,         int2b_Type)
00213     
00214     max30001.onDataAvailable(&StreamPacketUint32);
00215 
00216     Thread::wait(100);
00217     Peripherals::setMAX30001(&max30001);
00218 
00219     max30001.max30001_reg_read(max30001.INFO, &id);
00220     // read id twice because it needs to be read twice
00221     max30001.max30001_reg_read(max30001.INFO, &id);
00222     printf("INFO_01 = %x \n", id);
00223     
00224     partVersion = id >> 12;
00225     partVersion = partVersion & 0x3;
00226     
00227     partVersion = 1;
00228     // display header
00229     if (partVersion == 0)
00230         printf("Testing MAX30004|\n");
00231     if (partVersion == 1) {
00232         printf("Testing MAX30001|\n");
00233         printf("Testing ECG, RtoR, BioZ, PACE|\n");
00234     }
00235     
00236     // initialize the RPC server
00237     printf("Init RPC Server...\n");
00238     fflush(stdout);
00239     RPC_init();
00240     // initialize the logging service
00241     printf("Init LoggingService...\n");
00242     fflush(stdout);
00243     
00244     // start main loop
00245     printf("Start main loop...\n");
00246     fflush(stdout);
00247     
00248     printf("partVersion = %d \n", partVersion);
00249     // clear testing flags
00250     testing_ecg_flags[TESTING_ECG_FLAG] = 0;
00251     testing_ecg_flags[TESTING_BIOZ_FLAG] = 0;
00252     testing_ecg_flags[TESTING_PACE_FLAG] = 0;
00253     testing_ecg_flags[TESTING_RTOR_FLAG] = 0;
00254     
00255     // start streams
00256     // testing_max30001 = 1;
00257     if (partVersion == 1)
00258     printf("Start Streaming ECG, RtoR, PACE, BIOZ, CAL enabled, "
00259                  "verifying streams...|\n");
00260 
00261     if (partVersion == 3)
00262         printf(
00263         "Start Streaming ECG, RtoR, CAL enabled, verifying streams...|\n");
00264     // max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0);
00265     // max30001.max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0);
00266     // max30001.max30001_reg_read(max30001.CNFG_CAL, &reg);
00267     // printf("CNFG_CAL = %x \n", reg);
00268     
00269     //max30001.max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 0x1F, 0b00,
00270     //                               0b00, 0b0, 0b01);
00271     //max30001.max30001_reg_read(max30001.CNFG_GEN, &reg);
00272     //printf("CNFG_GEN_1 = %x \n", reg);
00273     
00274     //if (partVersion == 1)
00275     //    max30001.max30001_PACE_InitStart(0b1, 0b0, 0b0, 0b1, 0x0, 0b0, 0b00, 0b0,
00276     //                                  0b0);
00277     //max30001.max30001_reg_read(max30001.CNFG_GEN, &reg);
00278     //printf("CNFG_GEN_2 = %x \n", reg);
00279 
00280     if (partVersion == 1)
00281         max30001.max30001_BIOZ_InitStart(0b1, 0b0, 0b0, 0b00, 
00282                                         0b00, 0b00, 7, 0b0,
00283                                         0b010, 0b0, 0b11, 0b00, 0b00, 
00284                                         2, 0b0, 0b011, 0b0000, 0b0000);
00285     /*
00286     int MAX30001::max30001_BIOZ_InitStart(
00287     uint8_t En_bioz, uint8_t Openp, uint8_t Openn, uint8_t Calp_sel,
00288     uint8_t Caln_sel, uint8_t CG_mode, uint8_t B_fit, uint8_t Rate,
00289     uint8_t Ahpf, uint8_t Ext_rbias, uint8_t Gain, uint8_t Dhpf, uint8_t Dlpf,
00290     uint8_t Fcgen, uint8_t Cgmon, uint8_t Cgmag, uint8_t Phoff, uint8_t Inapow_mode) {
00291     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
00292     En_bioz: CNFG_GEN D[18], 1; 0 = Bioz disable, 1 = Bioz enable;
00293     Openp: CNFG_BMUX D[21], 0; 0 = BIP is internally connected to BioZ channel, 1 = BIP is isolate from Bioz Channel
00294     Openn: CNFG_BMUX D[20], 0; 0 = BIN is internally connected to BioZ channel, 1 = BIN is isolate from Bioz Channel
00295     Calp_sel: CNFG_BMUX D[19:18]: 00; 00 = No calibration signal applied; 01 = input connected to VMID, 10 = connected to VCALP, 11= VCALN
00296     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
00297     Caln_sel: CNFG_BMUX D[17:16]: 00; 00 = No calibration signal applied; 01 = input connected to VMID, 10 = connected to VCALP, 11= VCALN
00298     CG_mode: CNFG_BMUX D[13:12]: 00; 00 = Unchopped sources with low pass filter
00299     B_fit: MNGR_INT D[18:16]: 0x111; 000 to 111 = 1 to 8 Bioz FIFO interrupt Threshold
00300     Rate: CNFG_BioZ D[23]: 0; BioZ Data Rate; when FMSTR = 00, 0=64sps, 1=32; FMSTR=01, 0=62.5, 1=31.25; FMSTR=10, 0=50,1=25; FMSTR=11, 0=49.95, 1=24.98
00301     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
00302     Ahpf: CNFG_BioZ D[22:20]: BioZ/PACE Channel Analog High-Pass Filter Cutoff Frequency and Bypass
00303                                 0b010; 000=125Hz, 001=300, 010=800, 011=2000, 100=3700, 101=7200 11x=Bypass AHPF
00304     Ext_rbias: CNFG_BioZ D[19]: 0; 0 = Internal Bias Generator used; 1 = External Bias Generator used;
00305     Gain: CNFG_BioZ D[17:16]:00; BioZ Channel Gain Setting, 00=10V/V; 01=20V/V; 10=40V/V; 11=80V/V
00306     Dhpf: CNFG_BioZ D[15:14]:00; BioZ Channel Digital High-Pass Filter Cutoff Frequency 00=Bypass(DC); 01=0.05Hz; 1x=0.50Hz
00307     Dlpf: CNFG_BioZ D[13:12]:00; BioZ Channel Digital Low-Pass Filter Cutoff Frequency: 00=Bypass, 01=4Hz, 10=8Hz, 11=16Hz
00308     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
00309     Fcgen: CNFG_BioZ D[11:8]:2; BioZ Current Generator Modulation Frequency: 0000=4*fMSTR, 0001=2*fMSTR, 0010=fMSTR(40000Hz)
00310     Cgmon: CNFG_BioZ D[7]:0; BioZ Current Generator Monitor: 0 = Current Generator Monistor disable; 1 = enable
00311     Cgmag: CNFG_BioZ D[6:4]:0b001; BioZ Current Generator Magnitude: 
00312                             000=Off, 001 = 8uA, 010=16uA, 011=32uA, 100=48uA, 101=64uA; 110=80uA, 111=96uA
00313     Phoff: CNFG_BioZ D[3:0]:0000; BioZ Current Generator Modulation Phase Offset: 
00314                             BIOZ_FCGEN[3:0] = 0000: Phase Offset = BIOZ_PHOFF[3:2]*45.00° (0 to 135.00)
00315     Inapow_mode: CNFG_BioZ D[18]:0; BioZ Channel Instrumentation Amplifier (INA) Power Mode, 0=BioZ INA is in low power mode, 1=in low noise mode
00316     */
00317 
00318     max30001.max30001_reg_read(max30001.CNFG_GEN, &reg);
00319     printf("CNFG_GEN = %x \n", reg);   
00320     
00321     //max30001.max30001_RtoR_InitStart(0b1, 0b0011, 0b1111, 0b00, 0b0011, 0b000001,
00322     //                                    0b00, 0b000, 0b01);
00323     //max30001.max30001_reg_read(max30001.CNFG_GEN, &reg);
00324     printf("CNFG_GEN_4 = %x \n", reg);
00325     
00326     
00327     max30001.max30001_Rbias_FMSTR_Init(0b01, 0b10, 0b1, 0b1, 0b00);
00328     
00329     max30001.max30001_reg_read(max30001.CNFG_ECG, &reg);
00330     printf("CNFG_ECG = %x \n", reg);
00331     max30001.max30001_reg_read(max30001.CNFG_GEN, &reg);
00332     printf("CNFG_GEN_5 = %x \n", reg);
00333     max30001.max30001_reg_read(max30001.MNGR_INT, &reg);
00334     printf("MNGR_INT = %x \n", reg);
00335     max30001.max30001_reg_read(max30001.CNFG_EMUX, &reg);
00336     printf("CNFG_EMUX = %x \n", reg);
00337     max30001.max30001_reg_read(max30001.CNFG_BIOZ, &reg);
00338     printf("CNFG_BIOZ = %x \n", reg);    
00339     
00340     max30001.max30001_synch();
00341     
00342     timer.start();
00343 
00344     wait_us(20);
00345     
00346     timer.stop();
00347     
00348     rLED = LED_OFF;
00349     bLED = LED_OFF;
00350     gLED = LED_OFF;
00351     
00352     sprintf(BIOZ_file_name, "%s_%d", BIOZ_file_name, current_log_number);
00353     strcat(BIOZ_file_name, ".txt");
00354     printf("%s\n", BIOZ_file_name);
00355     
00356     while (true) {
00357         //wait_ms(50);
00358         //printf("%% \n");        
00359         rLED = LED_OFF;
00360         gLED = LED_ON;
00361         
00362         if ( Button == 0 )
00363             event_tag = 1;
00364         else
00365             event_tag = 0;
00366         
00367         //printf("ECG_Data_Size = %d \n", ECG_Data_Size);
00368         fp = fopen("/sd/ECG.txt", "a");
00369         // fp = fopen(ECG_file_name, "a");
00370         if( ECG_Data_Size > 60 )
00371         {
00372             
00373             for(i=0 ; i<ECG_Data_Size ; i++)
00374             {
00375                 fprintf(fp, "%d %d %x %s",ECG_Data_Size, i, ECG_Data[i], "\n");
00376 
00377                 // printf("%s %x %s","ECG", ECG_Data[i], "\n");
00378             }
00379             ECG_Data_Size = 0;
00380             // printf("Save ECG \n");
00381             rLED = !rLED;
00382 
00383         }
00384         fclose(fp);
00385 
00386         fp = fopen(BIOZ_file_name, "a");
00387         // fp = fopen("/sd/BIOZ.txt", "a");
00388         if( BIOZ_Data_Size > 30 )
00389         {
00390             for(i=0 ; i<BIOZ_Data_Size ; i++)
00391             {
00392                 fprintf(fp, "%d %d %x %d %s",BIOZ_Data_Size, i, BIOZ_Data[i], event_tag, "\n");
00393 
00394             }
00395             BIOZ_Data_Size = 0;
00396             printf("Save BIOZ \n");
00397             rLED = !rLED;
00398             outLED = !outLED;
00399         }
00400         fclose(fp);
00401 
00402     }
00403 }
00404