/* * ECE 3849 Lab2 starter project * * Gene Bogdanov 9/13/2017 */ /* XDCtools Header files */ #include #include #include /* BIOS Header files */ #include #include #include #include #include #include "driverlib/fpu.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" #include "driverlib/timer.h" #include "Crystalfontz128x128_ST7735.h" #include #include "grlib/grlib.h" #include #include #include "inc/hw_memmap.h" #include "driverlib/gpio.h" #include "driverlib/pwm.h" #include "driverlib/pin_map.h" #include "sampling.h" #include "buttons.h" #include "frequency.h" #include "audio.h" #include "kiss_fft.h" #include "_kiss_fft_guts.h" #define PI 3.14159265358979f tContext sContext; uint32_t cputime_unloaded; #define FFT_EN false #define LOCAL_BUF_LEN 128 uint16_t adc_buffer_sample[LOCAL_BUF_LEN]; // copy of g adc buffer uint8_t adc_buffer_processed[LOCAL_BUF_LEN]; // copy of g adc buffer #define FFT_BUF_LEN 1024 uint16_t adc_buffer_fft_sample[FFT_BUF_LEN]; // copy of g adc buffer uint8_t adc_buffer_fft_processed[FFT_BUF_LEN]; // copy of g adc buffer kiss_fft_cfg cfg; static kiss_fft_cpx in[FFT_BUF_LEN], out[FFT_BUF_LEN]; typedef struct { bool fft; uint8_t voltage_scale; uint8_t time_scale; uint8_t trigger_mode; } Options; Options options = { .fft = false, // oscilloscope mode .voltage_scale = 4, // 2v .time_scale = 5, // 20us .trigger_mode = 1, // rising }; // assuming square lcd #define HEIGHT LCD_VERTICAL_MAX #define WIDTH LCD_HORIZONTAL_MAX #define PIXELS_PER_DIV 20 #define VIN_RANGE 3.3 // volts #define ADC_BITS 12 #define ADC_OFFSET 100 #define VOLTAGE_SCALES 5 const char * const gVoltageScaleStr[VOLTAGE_SCALES] = { "100 mV", "200 mV", "500 mV", " 1 V", " 2 V" }; const float gVoltageScale[VOLTAGE_SCALES] = { 0.1, 0.2, 0.5, 1., 2. }; int Trigger(bool rising); uint32_t cpu_load_count(void) { uint32_t i = 0; TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT); TimerEnable(TIMER1_BASE, TIMER_A); // start one-shot timer while (!(TimerIntStatus(TIMER1_BASE, false) & TIMER_TIMA_TIMEOUT)) i++; return i; } void start_cputimer() { // initialize timer 1 in one-shot mode for polled timing SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerDisable(TIMER1_BASE, TIMER_BOTH); TimerConfigure(TIMER1_BASE, TIMER_CFG_ONE_SHOT); TimerLoadSet(TIMER1_BASE, TIMER_A, gSystemClock/100); // 10ms interval // baseline load cputime_unloaded = cpu_load_count(); } /* * ======== main ======== */ int main(void) { IntMasterDisable(); // hardware initialization goes here start_signal(); start_sampler(); ButtonInit(); start_cputimer(); start_frequency_scan(); configure_audio(); Crystalfontz128x128_Init(); // Initialize the LCD display driver Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP); // set screen orientation GrContextInit(&sContext, &g_sCrystalfontz128x128); // Initialize the grlib graphics context GrContextFontSet(&sContext, &g_sFontFixed6x8); // select font #define KISS_FFT_CFG_SIZE (sizeof(struct kiss_fft_state)+sizeof(kiss_fft_cpx)*(FFT_BUF_LEN-1)) static char kiss_fft_cfg_buffer[KISS_FFT_CFG_SIZE]; size_t buffer_size = KISS_FFT_CFG_SIZE; cfg = kiss_fft_alloc(FFT_BUF_LEN, 0, kiss_fft_cfg_buffer, &buffer_size); /* Start BIOS */ BIOS_start(); return (0); } void capture_waveform(UArg arg1, UArg arg2) { IntMasterEnable(); while(true) { Semaphore_pend(capture_sem, BIOS_WAIT_FOREVER); int32_t adc_current_index; int32_t i; if (options.fft) { adc_current_index = getADCBufferIndex(); int32_t j = 0; for(i=adc_current_index-FFT_BUF_LEN; i(b))?(a):(b)) #define CONSTRAIN(x) MAX(MIN(HEIGHT - 1, x), 0) if(options.fft) { // fft for(i=0; i x_stop; x--) { if (rising) { if ( gADCBuffer[ADC_BUFFER_WRAP(x)] >= ADC_OFFSET && gADCBuffer[ADC_BUFFER_WRAP(x-1)] < ADC_OFFSET) { break; } } else { // falling edge trigger if ( gADCBuffer[ADC_BUFFER_WRAP(x)] <= ADC_OFFSET && gADCBuffer[ADC_BUFFER_WRAP(x-1)] > ADC_OFFSET) { break; } } } if (x == x_stop) // for loop ran to the end x = getADCBufferIndex() - (WIDTH / 2); // reset x back to how it was initialized return x; }