first version of TDEM on Nucleo board
Dependencies: mbed-src anaDMA stm32_adafruit
main.cpp
00001 #include "mbed.h" 00002 #include "AnaloginDMA.h" 00003 #include "ff_gen_drv.h" 00004 #include "stm32_adafruit_lcd.h" 00005 #include "stm32_adafruit_sd.h" 00006 #include "stm32f4xx_nucleo.h" 00007 00008 extern Diskio_drvTypeDef SD_Driver; 00009 00010 PwmOut mypwm(PWM_OUT); // This is the TX pulse pin 00011 AnalogInDMA adc1(PA_0); // This is the ADC pin 00012 PwmOut buzzer(PC_9); // This is the digital out pin connected to the buzzer 00013 InterruptIn event(PA_0); // This sets a semaphore on rising front of pulse pin 00014 DigitalIn button(USER_BUTTON); 00015 Timeout timer; // This sets the net reading period 00016 00017 uint16_t reading[50][500]; // most current ADC capture values 00018 uint16_t data_buf[500]; 00019 bool sema, end_period; 00020 int n, nbsamples; 00021 00022 int linenr; 00023 FIL gp_file; 00024 FIL grid_file; 00025 char buf[256]; 00026 int filenum; 00027 00028 // system parameters 00029 float Gradient_Threshold = 0.05; // audio threshold 00030 int reading_period = 10000; // in µsec 00031 int integration_ratio = 10; 00032 int pulse_period = 207; 00033 int pulse_width = 200; 00034 00035 00036 void read_one_pulse () { 00037 sema = false; 00038 while (!sema) 00039 ; 00040 adc1.read(&reading[n][0],nbsamples); 00041 } 00042 00043 void trigger () { 00044 sema = true; 00045 } 00046 00047 void TO() { 00048 end_period = true; 00049 } 00050 00051 static int TFT_ShieldDetect(void) 00052 { 00053 GPIO_InitTypeDef GPIO_InitStruct; 00054 00055 /* Enable GPIO clock */ 00056 __GPIOB_CLK_ENABLE(); 00057 00058 GPIO_InitStruct.Pin = GPIO_PIN_0; 00059 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00060 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00061 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 00062 00063 if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) != 0) 00064 { 00065 return 1; 00066 } 00067 else 00068 { 00069 return 0; 00070 } 00071 } 00072 00073 int get_num(void) { 00074 FIL f; 00075 int i; 00076 UINT br; 00077 if ( f_open( &f, "NUM.BIN" , FA_READ ) == 0) { 00078 f_read (&f,&i,4,&br); 00079 i++; 00080 f_close(&f); 00081 } 00082 else 00083 i = 1; 00084 f_open( &f, "NUM.BIN" , FA_WRITE | FA_OPEN_ALWAYS ); 00085 f_write(&f,&i,4,&br); 00086 f_close(&f); 00087 return(i); 00088 } 00089 00090 void OpenGrid(char* grid_name){ 00091 int i; 00092 f_unlink( grid_name ); 00093 i = f_open( &grid_file, grid_name , FA_WRITE | FA_OPEN_ALWAYS ); 00094 } 00095 00096 void CloseGrid(void){ 00097 f_close( &grid_file ); 00098 } 00099 00100 00101 void Save_Buffer(void) { 00102 int i,j; 00103 UINT byteswritten; 00104 // data_buf[0] = T1TC; 00105 j = (nbsamples+5)*2; 00106 i = 0; 00107 00108 00109 i = f_write(&grid_file,&linenr,2,&byteswritten); 00110 i = f_write(&grid_file,data_buf,j,&byteswritten); 00111 // i = f_write(&grid_file,buf,strlen(buf),&byteswritten); 00112 if (i > 0) { 00113 // generate_tics (5, 200); 00114 return; 00115 } 00116 if (byteswritten < j) { 00117 // generate_tics (5, 200); 00118 return; 00119 } 00120 f_sync(&grid_file); 00121 } 00122 00123 void convert_file(void) { 00124 UINT i,byteswritten,bytesread; 00125 float r = 3300.0/1024.0; 00126 // sprintf(buf,"grid%i.BIN",filenum); 00127 if ( f_open( &gp_file, "grid.$$$" , FA_READ ) == 0) { 00128 // sprintf(buf,"grid%i.TXT",filenum); 00129 i = f_open( &grid_file, "grid.txt" , FA_WRITE | FA_OPEN_ALWAYS ); 00130 strcpy(buf,"SESSION\r\n"); 00131 f_write(&grid_file,buf,strlen(buf),&byteswritten); 00132 while (1) { 00133 f_read (&gp_file,&linenr,4,(UINT *)&bytesread); 00134 if (bytesread < 4 || linenr == 0xFFFFFFFF) 00135 break; 00136 sprintf(buf,"$ %u 0 ",linenr); 00137 f_write(&grid_file,buf,strlen(buf),&byteswritten); 00138 f_read (&gp_file,&data_buf,(nbsamples+5)*2,(UINT *)&bytesread); 00139 if (bytesread < (nbsamples+5)*2 || data_buf[0] == 0xFFFFFFFF) 00140 break; 00141 sprintf(buf,"%i ",data_buf[0]); 00142 f_write(&grid_file,buf,strlen(buf),&byteswritten); 00143 for (i = 1;i<nbsamples+1;i++) { 00144 sprintf(buf,"%0.0f ",r*data_buf[i]); 00145 f_write(&grid_file,buf,strlen(buf),&byteswritten); 00146 } 00147 for (i = nbsamples+1;i<nbsamples+5;i++) { 00148 sprintf(buf,"%i ",data_buf[i]); 00149 f_write(&grid_file,buf,strlen(buf),&byteswritten); 00150 } 00151 f_write(&grid_file,"\r\n",2,&byteswritten); 00152 } 00153 00154 } 00155 f_truncate (&grid_file); 00156 f_close( &gp_file ); 00157 f_close( &grid_file ); 00158 filenum = get_num(); 00159 sprintf(buf,"grid%i.TXT",filenum); 00160 f_rename("grid.txt",buf ); 00161 // generate_tics (5, 200); 00162 00163 } 00164 00165 00166 int main() { 00167 00168 uint16_t ref_reading[500]; // Reference reading of ground signal 00169 int sample_count; 00170 int delta, j; 00171 UINT i; 00172 char SD_Path[4]; 00173 FATFS SD_FatFs; /* File system object for SD card logical drive */ 00174 00175 00176 00177 if(TFT_ShieldDetect() == 0) 00178 { 00179 /* Initialize the LCD */ 00180 BSP_LCD_Init(); 00181 /* Initialize the Joystick available on adafruit 1.8" TFT shield */ 00182 BSP_JOY_Init(); 00183 FATFS_LinkDriver(&SD_Driver, SD_Path); 00184 /* Initialize the SD mounted on adafruit 1.8" TFT shield */ 00185 BSP_SD_Init(); 00186 f_mount(&SD_FatFs, (TCHAR const*)"/", 0); 00187 } 00188 mypwm.period_us(pulse_period); 00189 mypwm.pulsewidth_us(0); 00190 buzzer.period_ms(0); 00191 buzzer.write(50); 00192 event.rise(&trigger); 00193 00194 while (button == 1) //Wait to start session until button is pressed and released 00195 ; 00196 while (button == 0) 00197 ; 00198 sample_count = 0; 00199 OpenGrid("grid.$$$"); 00200 while(1) { 00201 end_period = false; 00202 timer.attach_us(&TO,reading_period); 00203 mypwm.pulsewidth_us(pulse_width); // Start TX pulses 00204 nbsamples = pulse_period*2; 00205 for (n=0;n<integration_ratio;n++) 00206 read_one_pulse(); // Accumulate ADC captures into reading 00207 mypwm.pulsewidth_us(0); // Stop TX pulses 00208 for (i=0; i < nbsamples;i++) { 00209 data_buf[j] = 0; 00210 for (j=0; j < integration_ratio;j++) 00211 data_buf[j] += reading[j][i]; 00212 } 00213 for (i=0; i < nbsamples;i++) 00214 data_buf[i] /= integration_ratio; // Average reading 00215 if (sample_count == 0) { // If first reading of session, save reading into ground reference signal level 00216 for (i=0; i < integration_ratio;i++) 00217 ref_reading[i] = data_buf[i]; 00218 sample_count++; 00219 } 00220 delta = ref_reading[0] - data_buf[0]; // Delta is the gradient 00221 if (abs(delta) > Gradient_Threshold ) // if hte graient is larger than a threshold, start buzzing 00222 buzzer.period_ms(1); 00223 else 00224 buzzer.period_ms(0); 00225 if (delta != 0 && abs(delta) <= 0.01 ) // If delta is small enough but not nul, update the ref_reading with th ecurrent reading through a powerful low pass FIR filter. 00226 for (i=0; i < integration_ratio;i++) 00227 ref_reading[i] = (ref_reading[i]*127 + reading[0][i] ) / 128; 00228 Save_Buffer(); 00229 if (button == 0) { // pushing again on the button and relasing it stops the session 00230 while (button == 0) 00231 ; 00232 data_buf[0] = 0xFFFF; 00233 f_write(&grid_file,data_buf,(nbsamples+1)*2,&i); CloseGrid(); 00234 return(0); 00235 } 00236 while (!end_period) // Wait for end of reading period (i.e. 10msec) 00237 ; 00238 } 00239 } 00240
Generated on Sun Jul 24 2022 11:47:23 by 1.7.2