first version of TDEM on Nucleo board

Dependencies:   mbed-src anaDMA stm32_adafruit

main.cpp

Committer:
willybayot
Date:
2015-01-05
Revision:
6:74471623ffc3
Parent:
3:e0293a583075
Child:
7:6dd9921fb208

File content as of revision 6:74471623ffc3:

#include "mbed.h"
#include "AnaloginDMA.h"
#include "ff_gen_drv.h"
#include "stm32_adafruit_lcd.h"
#include "stm32_adafruit_sd.h"

extern Diskio_drvTypeDef  SD_Driver; 

PwmOut mypwm(PWM_OUT);          // This is the TX pulse pin
AnalogInDMA adc1(PA_0);              // This is the ADC pin
PwmOut buzzer(PC_9);             // This is the digital out pin connected to the buzzer
InterruptIn event(PA_0);          // This sets a semaphore on rising front of pulse pin
DigitalIn button(USER_BUTTON);
Timeout timer;                  // This sets the net reading period

uint16_t reading[50][500];                  // most current ADC capture values 
bool sema, end_period;
int n;
// system parameters
float Gradient_Threshold = 0.05;    // audio threshold 
int reading_period = 10000;         // in µsec
int integration_ratio = 10;
int pulse_period = 207;
int pulse_width = 200;


void read_one_pulse () {
    sema = false;
    while (!sema)
        ;
    adc1.read(&reading[n][0],pulse_period*2);
    }

void trigger () {
    sema = true;
    }

void TO() {
   end_period = true;
   }

static int TFT_ShieldDetect(void)
{
  GPIO_InitTypeDef  GPIO_InitStruct; 

  /* Enable GPIO clock */
  __GPIOB_CLK_ENABLE();
  
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  
  if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) != 0)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}
   
int main() {
 

uint16_t ref_reading[500];          // Reference reading of ground signal
int sample_count;
int delta, i;
char SD_Path[4];
FATFS SD_FatFs;  /* File system object for SD card logical drive */
 
    if(TFT_ShieldDetect() == 0)
        {
        /* Initialize the LCD */
        BSP_LCD_Init();
        /* Initialize the Joystick available on adafruit 1.8" TFT shield */
        BSP_JOY_Init();
       }
    FATFS_LinkDriver(&SD_Driver, SD_Path);
     /* Initialize the SD mounted on adafruit 1.8" TFT shield */
    BSP_SD_Init();
    f_mount(&SD_FatFs, (TCHAR const*)"/", 0);  

    mypwm.period_us(pulse_period);
    mypwm.pulsewidth_us(0);
    buzzer.period_ms(0);
    buzzer.write(50);
    event.rise(&trigger);
    
    while (button == 1)     //Wait to start session until button is pressed and released
        ;
    while (button == 0)
        ;       
    sample_count = 0;
    while(1) {
        end_period = false;
        timer.attach_us(&TO,reading_period);
        mypwm.pulsewidth_us(pulse_width);       // Start TX pulses
        for (n=0;n<integration_ratio;n++)
            read_one_pulse();                   // Accumulate ADC captures into reading
        mypwm.pulsewidth_us(0);                 // Stop TX pulses
        for (i=1; i < integration_ratio;i++)
            reading[0][0] += reading[0][i];
        reading[0][0] /= integration_ratio;           // Average reading
        if (sample_count == 0) {                // If first reading of session, save reading into ground reference signal level
            for (i=0; i < integration_ratio;i++)
                ref_reading[i] = reading[0][i];
            sample_count++;
            }
        delta = ref_reading[0] - reading[0][0];          // Delta is the gradient
        if (abs(delta) > Gradient_Threshold )   // if hte graient is larger than a threshold, start buzzing
            buzzer.period_ms(1);
        else 
            buzzer.period_ms(0);
        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.
            for (i=0; i < integration_ratio;i++)
               ref_reading[i] = (ref_reading[i]*127 + reading[0][i] ) / 128;
        if (button == 0) {                      // pushing again on the button and relasing it stops the session
            while (button == 0)
                ;
            return(0);
            }
        while (!end_period)             // Wait for end of reading period (i.e. 10msec)
            ;
      }
}