Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

main.cpp

Committer:
timmey9
Date:
2015-01-30
Revision:
41:3e0623d81b9a
Parent:
40:bd6d8c35e822
Child:
42:52a92a8d2cc7

File content as of revision 41:3e0623d81b9a:

// Server code
#include "mbed.h"
#include <stdio.h>

// Analog sampling
#include "PeripheralNames.h"
#include "PeripheralPins.h"
#include "fsl_adc_hal.h"
#include "fsl_clock_manager.h"
#include "fsl_dspi_hal.h"
#include "AngleEncoder.h"
#include "adc.h"
#include "dma.h"
#include "pdb.h"

// Analog sampling
#define MAX_FADC 6000000
#define SAMPLING_RATE       10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
#define TOTAL_SAMPLES       100 // originally 30000 for 0.3 ms of sampling.

// for debug purposes
Serial pc(USBTX, USBRX);
DigitalOut led_red(LED_RED);
DigitalOut led_green(LED_GREEN);
DigitalOut led_blue(LED_BLUE);

AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder

// Analog sampling
Ticker Sampler;

uint16_t sample_array1[TOTAL_SAMPLES];
uint16_t sample_array2[TOTAL_SAMPLES];
uint16_t angle_array[TOTAL_SAMPLES];
float currA0 = 0;
float currA2 = 0;



// Declaration of functions

void timed_sampling();

// Important globabl variables necessary for the sampling every interval
int rotary_count = 0;
uint32_t last_AMT20_AB_read = 0;

DMA dma(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, &rotary_count);


using namespace std;
 
int main() {
    led_blue = 1;
    led_green = 1;
    led_red = 1;
    
    pc.baud(230400);
    pc.printf("Starting\r\n");
    
    dma.reset();
    adc_init(A0);
    
    pc.printf("\r\n\r\n\r\n");
    
    while(1) {
        rotary_count++;
        if(pc.readable() > 0) {
            char temp = pc.getc();
            
            switch(temp) {
                case 's':
                    for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\r\n",i,sample_array1[i]*3.3/65535);
                    
                    pc.printf("\r\n");
                    break;
                case 'f':
                    for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0xf;
                    break;
                case 'd':
                    for(int i = 0; i < 3; i++) pc.printf("Sample[%i]: %x\r\n", i, sample_array1[i]);
                    break;
                case 'a':
                    adc_start();
                    wait(1);
                    adc_stop();
                    break;
            }
        }
        //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("A%i: %f  ",i,sample_array1[i]*3.3/65535);
        
        //pc.printf("ADC0_RA:   %08x\r\n",ADC0_RA);
        //pc.printf("ADC0_RB:   %08x\r\n",ADC0_RB);
        //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("B%i: %f  ",i,sample_array2[i]*3.3/65535);
        //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("C%i: %i  ",i,angle_array[i]);
        
        //pc.printf("DMA_DADDR: %08x                  \r", *dma_daddr);
        //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2);
        //wait(1);
    }
}

void timed_sampling() {
    // start ADC conversion
    BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12);      // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
    BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14);      // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
    
    // The following updates the rotary counter for the AMT20 sensor
    // Put A on PTC0
    // Put B on PTC1
    uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
    if (AMT20_AB != last_AMT20_AB_read)
    {
        // change "INVERT_ANGLE" to change whether relative angle counts up or down.
        if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
        #if INVERT_ANGLE == 1
            {rotary_count--;}
        else
            {rotary_count++;}
        #else
            {rotary_count++;}
        else
            {rotary_count--;}
        #endif
        
        last_AMT20_AB_read = AMT20_AB;        
    }
}