first version of TDEM on Nucleo board

Dependencies:   mbed-src anaDMA stm32_adafruit

Committer:
willybayot
Date:
Mon Jan 05 16:07:29 2015 +0000
Revision:
7:6dd9921fb208
Parent:
6:74471623ffc3
v

Who changed what in which revision?

UserRevisionLine numberNew contents of line
willybayot 0:a9ab98434bf2 1 #include "mbed.h"
willybayot 1:02d3732e1d76 2 #include "AnaloginDMA.h"
willybayot 6:74471623ffc3 3 #include "ff_gen_drv.h"
willybayot 6:74471623ffc3 4 #include "stm32_adafruit_lcd.h"
willybayot 6:74471623ffc3 5 #include "stm32_adafruit_sd.h"
willybayot 7:6dd9921fb208 6 #include "stm32f4xx_nucleo.h"
willybayot 0:a9ab98434bf2 7
willybayot 6:74471623ffc3 8 extern Diskio_drvTypeDef SD_Driver;
willybayot 6:74471623ffc3 9
willybayot 0:a9ab98434bf2 10 PwmOut mypwm(PWM_OUT); // This is the TX pulse pin
willybayot 1:02d3732e1d76 11 AnalogInDMA adc1(PA_0); // This is the ADC pin
willybayot 0:a9ab98434bf2 12 PwmOut buzzer(PC_9); // This is the digital out pin connected to the buzzer
willybayot 0:a9ab98434bf2 13 InterruptIn event(PA_0); // This sets a semaphore on rising front of pulse pin
willybayot 0:a9ab98434bf2 14 DigitalIn button(USER_BUTTON);
willybayot 0:a9ab98434bf2 15 Timeout timer; // This sets the net reading period
willybayot 0:a9ab98434bf2 16
willybayot 1:02d3732e1d76 17 uint16_t reading[50][500]; // most current ADC capture values
willybayot 7:6dd9921fb208 18 uint16_t data_buf[500];
willybayot 0:a9ab98434bf2 19 bool sema, end_period;
willybayot 7:6dd9921fb208 20 int n, nbsamples;
willybayot 7:6dd9921fb208 21
willybayot 7:6dd9921fb208 22 int linenr;
willybayot 7:6dd9921fb208 23 FIL gp_file;
willybayot 7:6dd9921fb208 24 FIL grid_file;
willybayot 7:6dd9921fb208 25 char buf[256];
willybayot 7:6dd9921fb208 26 int filenum;
willybayot 7:6dd9921fb208 27
willybayot 0:a9ab98434bf2 28 // system parameters
willybayot 0:a9ab98434bf2 29 float Gradient_Threshold = 0.05; // audio threshold
willybayot 0:a9ab98434bf2 30 int reading_period = 10000; // in µsec
willybayot 0:a9ab98434bf2 31 int integration_ratio = 10;
willybayot 0:a9ab98434bf2 32 int pulse_period = 207;
willybayot 0:a9ab98434bf2 33 int pulse_width = 200;
willybayot 0:a9ab98434bf2 34
willybayot 0:a9ab98434bf2 35
willybayot 0:a9ab98434bf2 36 void read_one_pulse () {
willybayot 0:a9ab98434bf2 37 sema = false;
willybayot 0:a9ab98434bf2 38 while (!sema)
willybayot 0:a9ab98434bf2 39 ;
willybayot 7:6dd9921fb208 40 adc1.read(&reading[n][0],nbsamples);
willybayot 0:a9ab98434bf2 41 }
willybayot 0:a9ab98434bf2 42
willybayot 0:a9ab98434bf2 43 void trigger () {
willybayot 0:a9ab98434bf2 44 sema = true;
willybayot 0:a9ab98434bf2 45 }
willybayot 0:a9ab98434bf2 46
willybayot 0:a9ab98434bf2 47 void TO() {
willybayot 0:a9ab98434bf2 48 end_period = true;
willybayot 0:a9ab98434bf2 49 }
willybayot 6:74471623ffc3 50
willybayot 6:74471623ffc3 51 static int TFT_ShieldDetect(void)
willybayot 6:74471623ffc3 52 {
willybayot 6:74471623ffc3 53 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 6:74471623ffc3 54
willybayot 6:74471623ffc3 55 /* Enable GPIO clock */
willybayot 6:74471623ffc3 56 __GPIOB_CLK_ENABLE();
willybayot 6:74471623ffc3 57
willybayot 6:74471623ffc3 58 GPIO_InitStruct.Pin = GPIO_PIN_0;
willybayot 6:74471623ffc3 59 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
willybayot 6:74471623ffc3 60 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
willybayot 6:74471623ffc3 61 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
willybayot 6:74471623ffc3 62
willybayot 6:74471623ffc3 63 if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) != 0)
willybayot 6:74471623ffc3 64 {
willybayot 6:74471623ffc3 65 return 1;
willybayot 6:74471623ffc3 66 }
willybayot 6:74471623ffc3 67 else
willybayot 6:74471623ffc3 68 {
willybayot 6:74471623ffc3 69 return 0;
willybayot 6:74471623ffc3 70 }
willybayot 6:74471623ffc3 71 }
willybayot 7:6dd9921fb208 72
willybayot 7:6dd9921fb208 73 int get_num(void) {
willybayot 7:6dd9921fb208 74 FIL f;
willybayot 7:6dd9921fb208 75 int i;
willybayot 7:6dd9921fb208 76 UINT br;
willybayot 7:6dd9921fb208 77 if ( f_open( &f, "NUM.BIN" , FA_READ ) == 0) {
willybayot 7:6dd9921fb208 78 f_read (&f,&i,4,&br);
willybayot 7:6dd9921fb208 79 i++;
willybayot 7:6dd9921fb208 80 f_close(&f);
willybayot 7:6dd9921fb208 81 }
willybayot 7:6dd9921fb208 82 else
willybayot 7:6dd9921fb208 83 i = 1;
willybayot 7:6dd9921fb208 84 f_open( &f, "NUM.BIN" , FA_WRITE | FA_OPEN_ALWAYS );
willybayot 7:6dd9921fb208 85 f_write(&f,&i,4,&br);
willybayot 7:6dd9921fb208 86 f_close(&f);
willybayot 7:6dd9921fb208 87 return(i);
willybayot 7:6dd9921fb208 88 }
willybayot 7:6dd9921fb208 89
willybayot 7:6dd9921fb208 90 void OpenGrid(char* grid_name){
willybayot 7:6dd9921fb208 91 int i;
willybayot 7:6dd9921fb208 92 f_unlink( grid_name );
willybayot 7:6dd9921fb208 93 i = f_open( &grid_file, grid_name , FA_WRITE | FA_OPEN_ALWAYS );
willybayot 7:6dd9921fb208 94 }
willybayot 7:6dd9921fb208 95
willybayot 7:6dd9921fb208 96 void CloseGrid(void){
willybayot 7:6dd9921fb208 97 f_close( &grid_file );
willybayot 7:6dd9921fb208 98 }
willybayot 0:a9ab98434bf2 99
willybayot 7:6dd9921fb208 100
willybayot 7:6dd9921fb208 101 void Save_Buffer(void) {
willybayot 7:6dd9921fb208 102 int i,j;
willybayot 7:6dd9921fb208 103 UINT byteswritten;
willybayot 7:6dd9921fb208 104 // data_buf[0] = T1TC;
willybayot 7:6dd9921fb208 105 j = (nbsamples+5)*2;
willybayot 7:6dd9921fb208 106 i = 0;
willybayot 7:6dd9921fb208 107
willybayot 7:6dd9921fb208 108
willybayot 7:6dd9921fb208 109 i = f_write(&grid_file,&linenr,2,&byteswritten);
willybayot 7:6dd9921fb208 110 i = f_write(&grid_file,data_buf,j,&byteswritten);
willybayot 7:6dd9921fb208 111 // i = f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 112 if (i > 0) {
willybayot 7:6dd9921fb208 113 // generate_tics (5, 200);
willybayot 7:6dd9921fb208 114 return;
willybayot 7:6dd9921fb208 115 }
willybayot 7:6dd9921fb208 116 if (byteswritten < j) {
willybayot 7:6dd9921fb208 117 // generate_tics (5, 200);
willybayot 7:6dd9921fb208 118 return;
willybayot 7:6dd9921fb208 119 }
willybayot 7:6dd9921fb208 120 f_sync(&grid_file);
willybayot 7:6dd9921fb208 121 }
willybayot 0:a9ab98434bf2 122
willybayot 7:6dd9921fb208 123 void convert_file(void) {
willybayot 7:6dd9921fb208 124 UINT i,byteswritten,bytesread;
willybayot 7:6dd9921fb208 125 float r = 3300.0/1024.0;
willybayot 7:6dd9921fb208 126 // sprintf(buf,"grid%i.BIN",filenum);
willybayot 7:6dd9921fb208 127 if ( f_open( &gp_file, "grid.$$$" , FA_READ ) == 0) {
willybayot 7:6dd9921fb208 128 // sprintf(buf,"grid%i.TXT",filenum);
willybayot 7:6dd9921fb208 129 i = f_open( &grid_file, "grid.txt" , FA_WRITE | FA_OPEN_ALWAYS );
willybayot 7:6dd9921fb208 130 strcpy(buf,"SESSION\r\n");
willybayot 7:6dd9921fb208 131 f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 132 while (1) {
willybayot 7:6dd9921fb208 133 f_read (&gp_file,&linenr,4,(UINT *)&bytesread);
willybayot 7:6dd9921fb208 134 if (bytesread < 4 || linenr == 0xFFFFFFFF)
willybayot 7:6dd9921fb208 135 break;
willybayot 7:6dd9921fb208 136 sprintf(buf,"$ %u 0 ",linenr);
willybayot 7:6dd9921fb208 137 f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 138 f_read (&gp_file,&data_buf,(nbsamples+5)*2,(UINT *)&bytesread);
willybayot 7:6dd9921fb208 139 if (bytesread < (nbsamples+5)*2 || data_buf[0] == 0xFFFFFFFF)
willybayot 7:6dd9921fb208 140 break;
willybayot 7:6dd9921fb208 141 sprintf(buf,"%i ",data_buf[0]);
willybayot 7:6dd9921fb208 142 f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 143 for (i = 1;i<nbsamples+1;i++) {
willybayot 7:6dd9921fb208 144 sprintf(buf,"%0.0f ",r*data_buf[i]);
willybayot 7:6dd9921fb208 145 f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 146 }
willybayot 7:6dd9921fb208 147 for (i = nbsamples+1;i<nbsamples+5;i++) {
willybayot 7:6dd9921fb208 148 sprintf(buf,"%i ",data_buf[i]);
willybayot 7:6dd9921fb208 149 f_write(&grid_file,buf,strlen(buf),&byteswritten);
willybayot 7:6dd9921fb208 150 }
willybayot 7:6dd9921fb208 151 f_write(&grid_file,"\r\n",2,&byteswritten);
willybayot 7:6dd9921fb208 152 }
willybayot 7:6dd9921fb208 153
willybayot 7:6dd9921fb208 154 }
willybayot 7:6dd9921fb208 155 f_truncate (&grid_file);
willybayot 7:6dd9921fb208 156 f_close( &gp_file );
willybayot 7:6dd9921fb208 157 f_close( &grid_file );
willybayot 7:6dd9921fb208 158 filenum = get_num();
willybayot 7:6dd9921fb208 159 sprintf(buf,"grid%i.TXT",filenum);
willybayot 7:6dd9921fb208 160 f_rename("grid.txt",buf );
willybayot 7:6dd9921fb208 161 // generate_tics (5, 200);
willybayot 7:6dd9921fb208 162
willybayot 7:6dd9921fb208 163 }
willybayot 7:6dd9921fb208 164
willybayot 7:6dd9921fb208 165
willybayot 7:6dd9921fb208 166 int main() {
willybayot 7:6dd9921fb208 167
willybayot 7:6dd9921fb208 168 uint16_t ref_reading[500]; // Reference reading of ground signal
willybayot 7:6dd9921fb208 169 int sample_count;
willybayot 7:6dd9921fb208 170 int delta, j;
willybayot 7:6dd9921fb208 171 UINT i;
willybayot 7:6dd9921fb208 172 char SD_Path[4];
willybayot 7:6dd9921fb208 173 FATFS SD_FatFs; /* File system object for SD card logical drive */
willybayot 7:6dd9921fb208 174
willybayot 7:6dd9921fb208 175
willybayot 6:74471623ffc3 176
willybayot 6:74471623ffc3 177 if(TFT_ShieldDetect() == 0)
willybayot 6:74471623ffc3 178 {
willybayot 6:74471623ffc3 179 /* Initialize the LCD */
willybayot 6:74471623ffc3 180 BSP_LCD_Init();
willybayot 6:74471623ffc3 181 /* Initialize the Joystick available on adafruit 1.8" TFT shield */
willybayot 6:74471623ffc3 182 BSP_JOY_Init();
willybayot 7:6dd9921fb208 183 FATFS_LinkDriver(&SD_Driver, SD_Path);
willybayot 7:6dd9921fb208 184 /* Initialize the SD mounted on adafruit 1.8" TFT shield */
willybayot 7:6dd9921fb208 185 BSP_SD_Init();
willybayot 7:6dd9921fb208 186 f_mount(&SD_FatFs, (TCHAR const*)"/", 0);
willybayot 7:6dd9921fb208 187 }
willybayot 0:a9ab98434bf2 188 mypwm.period_us(pulse_period);
willybayot 0:a9ab98434bf2 189 mypwm.pulsewidth_us(0);
willybayot 0:a9ab98434bf2 190 buzzer.period_ms(0);
willybayot 0:a9ab98434bf2 191 buzzer.write(50);
willybayot 0:a9ab98434bf2 192 event.rise(&trigger);
willybayot 0:a9ab98434bf2 193
willybayot 0:a9ab98434bf2 194 while (button == 1) //Wait to start session until button is pressed and released
willybayot 0:a9ab98434bf2 195 ;
willybayot 0:a9ab98434bf2 196 while (button == 0)
willybayot 0:a9ab98434bf2 197 ;
willybayot 0:a9ab98434bf2 198 sample_count = 0;
willybayot 7:6dd9921fb208 199 OpenGrid("grid.$$$");
willybayot 0:a9ab98434bf2 200 while(1) {
willybayot 0:a9ab98434bf2 201 end_period = false;
willybayot 0:a9ab98434bf2 202 timer.attach_us(&TO,reading_period);
willybayot 0:a9ab98434bf2 203 mypwm.pulsewidth_us(pulse_width); // Start TX pulses
willybayot 7:6dd9921fb208 204 nbsamples = pulse_period*2;
willybayot 0:a9ab98434bf2 205 for (n=0;n<integration_ratio;n++)
willybayot 0:a9ab98434bf2 206 read_one_pulse(); // Accumulate ADC captures into reading
willybayot 0:a9ab98434bf2 207 mypwm.pulsewidth_us(0); // Stop TX pulses
willybayot 7:6dd9921fb208 208 for (i=0; i < nbsamples;i++) {
willybayot 7:6dd9921fb208 209 data_buf[j] = 0;
willybayot 7:6dd9921fb208 210 for (j=0; j < integration_ratio;j++)
willybayot 7:6dd9921fb208 211 data_buf[j] += reading[j][i];
willybayot 7:6dd9921fb208 212 }
willybayot 7:6dd9921fb208 213 for (i=0; i < nbsamples;i++)
willybayot 7:6dd9921fb208 214 data_buf[i] /= integration_ratio; // Average reading
willybayot 0:a9ab98434bf2 215 if (sample_count == 0) { // If first reading of session, save reading into ground reference signal level
willybayot 3:e0293a583075 216 for (i=0; i < integration_ratio;i++)
willybayot 7:6dd9921fb208 217 ref_reading[i] = data_buf[i];
willybayot 0:a9ab98434bf2 218 sample_count++;
willybayot 0:a9ab98434bf2 219 }
willybayot 7:6dd9921fb208 220 delta = ref_reading[0] - data_buf[0]; // Delta is the gradient
willybayot 0:a9ab98434bf2 221 if (abs(delta) > Gradient_Threshold ) // if hte graient is larger than a threshold, start buzzing
willybayot 0:a9ab98434bf2 222 buzzer.period_ms(1);
willybayot 0:a9ab98434bf2 223 else
willybayot 0:a9ab98434bf2 224 buzzer.period_ms(0);
willybayot 0:a9ab98434bf2 225 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.
willybayot 3:e0293a583075 226 for (i=0; i < integration_ratio;i++)
willybayot 3:e0293a583075 227 ref_reading[i] = (ref_reading[i]*127 + reading[0][i] ) / 128;
willybayot 7:6dd9921fb208 228 Save_Buffer();
willybayot 0:a9ab98434bf2 229 if (button == 0) { // pushing again on the button and relasing it stops the session
willybayot 0:a9ab98434bf2 230 while (button == 0)
willybayot 0:a9ab98434bf2 231 ;
willybayot 7:6dd9921fb208 232 data_buf[0] = 0xFFFF;
willybayot 7:6dd9921fb208 233 f_write(&grid_file,data_buf,(nbsamples+1)*2,&i); CloseGrid();
willybayot 0:a9ab98434bf2 234 return(0);
willybayot 0:a9ab98434bf2 235 }
willybayot 0:a9ab98434bf2 236 while (!end_period) // Wait for end of reading period (i.e. 10msec)
willybayot 0:a9ab98434bf2 237 ;
willybayot 0:a9ab98434bf2 238 }
willybayot 0:a9ab98434bf2 239 }
willybayot 0:a9ab98434bf2 240