Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Fri Jan 30 06:16:39 2015 +0000
Revision:
40:bd6d8c35e822
Parent:
39:82dc3daecf32
Child:
41:3e0623d81b9a
ADC and DMA working.  Start and stop of ADC/DMA is also working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timmey9 20:f533b3c9296f 1 // Server code
donatien 0:bb128f0e952f 2 #include "mbed.h"
timmey9 24:a5891669afc5 3 #include <stdio.h>
timmey9 22:523e316cbe70 4
timmey9 22:523e316cbe70 5 // Analog sampling
timmey9 22:523e316cbe70 6 #include "PeripheralNames.h"
timmey9 22:523e316cbe70 7 #include "PeripheralPins.h"
timmey9 22:523e316cbe70 8 #include "fsl_adc_hal.h"
timmey9 22:523e316cbe70 9 #include "fsl_clock_manager.h"
timmey9 22:523e316cbe70 10 #include "fsl_dspi_hal.h"
timmey9 22:523e316cbe70 11 #include "AngleEncoder.h"
timmey9 39:82dc3daecf32 12 #include "adc.h"
timmey9 34:44cc9b76a507 13 #include "dma.h"
timmey9 39:82dc3daecf32 14 #include "pdb.h"
timmey9 28:4a833d59897b 15
timmey9 22:523e316cbe70 16 // Analog sampling
timmey9 22:523e316cbe70 17 #define MAX_FADC 6000000
timmey9 36:07d8a3143967 18 #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
timmey9 40:bd6d8c35e822 19 #define TOTAL_SAMPLES 100 // originally 30000 for 0.3 ms of sampling.
timmey9 22:523e316cbe70 20
timmey9 22:523e316cbe70 21 // for debug purposes
timmey9 18:b17ddeeb1c09 22 Serial pc(USBTX, USBRX);
timmey9 18:b17ddeeb1c09 23 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 24 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 25 DigitalOut led_blue(LED_BLUE);
timmey9 18:b17ddeeb1c09 26
timmey9 22:523e316cbe70 27 AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
timmey9 22:523e316cbe70 28 DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 29 DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 30
timmey9 22:523e316cbe70 31 // Analog sampling
timmey9 34:44cc9b76a507 32 Ticker Sampler;
timmey9 22:523e316cbe70 33
timmey9 40:bd6d8c35e822 34 uint16_t sample_array1[TOTAL_SAMPLES];
timmey9 25:abbc19af13f9 35 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 36 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 34:44cc9b76a507 37 float currA0 = 0;
timmey9 34:44cc9b76a507 38 float currA2 = 0;
timmey9 22:523e316cbe70 39
timmey9 22:523e316cbe70 40 // Declaration of functions
timmey9 39:82dc3daecf32 41
timmey9 22:523e316cbe70 42 void timed_sampling();
timmey9 22:523e316cbe70 43
timmey9 22:523e316cbe70 44 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 45 int rotary_count = 0;
timmey9 22:523e316cbe70 46 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 47
timmey9 40:bd6d8c35e822 48 void ADC0_IRQHandler(void);
timmey9 39:82dc3daecf32 49
timmey9 22:523e316cbe70 50 using namespace std;
timmey9 17:2f978f823020 51
emilmont 7:65188f4a8c25 52 int main() {
timmey9 22:523e316cbe70 53 led_blue = 1;
timmey9 35:df40c4566826 54 led_green = 1;
timmey9 18:b17ddeeb1c09 55 led_red = 1;
timmey9 34:44cc9b76a507 56
timmey9 18:b17ddeeb1c09 57 pc.baud(230400);
timmey9 34:44cc9b76a507 58 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 59
timmey9 40:bd6d8c35e822 60 dma_init(TOTAL_SAMPLES);
timmey9 39:82dc3daecf32 61 analog_initialization(A0,pc);
timmey9 38:ec3b16c130d7 62
timmey9 40:bd6d8c35e822 63 pc.printf("\r\n\r\n\r\n");
timmey9 37:8bdc71f3e874 64
timmey9 34:44cc9b76a507 65 while(1) {
timmey9 36:07d8a3143967 66 rotary_count++;
timmey9 34:44cc9b76a507 67 if(pc.readable() > 0) {
timmey9 34:44cc9b76a507 68 char temp = pc.getc();
timmey9 34:44cc9b76a507 69
timmey9 34:44cc9b76a507 70 switch(temp) {
timmey9 34:44cc9b76a507 71 case 's':
timmey9 40:bd6d8c35e822 72 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\r\n",i,sample_array1[i]*3.3/65535);
timmey9 36:07d8a3143967 73
timmey9 35:df40c4566826 74 pc.printf("\r\n");
timmey9 34:44cc9b76a507 75 break;
timmey9 34:44cc9b76a507 76 case 'f':
timmey9 39:82dc3daecf32 77 for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0xf;
timmey9 34:44cc9b76a507 78 break;
timmey9 39:82dc3daecf32 79 case 'd':
timmey9 39:82dc3daecf32 80 for(int i = 0; i < 3; i++) pc.printf("Sample[%i]: %x\r\n", i, sample_array1[i]);
timmey9 39:82dc3daecf32 81 break;
timmey9 40:bd6d8c35e822 82 case 'a':
timmey9 40:bd6d8c35e822 83 start_adc();
timmey9 40:bd6d8c35e822 84 wait(1);
timmey9 40:bd6d8c35e822 85 stop_adc();
timmey9 40:bd6d8c35e822 86 break;
timmey9 34:44cc9b76a507 87 }
timmey9 34:44cc9b76a507 88 }
timmey9 40:bd6d8c35e822 89 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("A%i: %f ",i,sample_array1[i]*3.3/65535);
timmey9 39:82dc3daecf32 90
timmey9 40:bd6d8c35e822 91 //pc.printf("ADC0_RA: %08x\r\n",ADC0_RA);
timmey9 40:bd6d8c35e822 92 //pc.printf("ADC0_RB: %08x\r\n",ADC0_RB);
timmey9 39:82dc3daecf32 93 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("B%i: %f ",i,sample_array2[i]*3.3/65535);
timmey9 39:82dc3daecf32 94 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("C%i: %i ",i,angle_array[i]);
timmey9 40:bd6d8c35e822 95
timmey9 35:df40c4566826 96 //pc.printf("DMA_DADDR: %08x \r", *dma_daddr);
timmey9 35:df40c4566826 97 //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2);
timmey9 40:bd6d8c35e822 98 //wait(1);
timmey9 17:2f978f823020 99 }
timmey9 22:523e316cbe70 100 }
timmey9 23:9e5141647775 101
timmey9 22:523e316cbe70 102 void timed_sampling() {
timmey9 39:82dc3daecf32 103 // start ADC conversion
timmey9 22:523e316cbe70 104 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 22:523e316cbe70 105 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 22:523e316cbe70 106
timmey9 22:523e316cbe70 107 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 108 // Put A on PTC0
timmey9 22:523e316cbe70 109 // Put B on PTC1
timmey9 22:523e316cbe70 110 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 111 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 112 {
timmey9 22:523e316cbe70 113 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 114 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 115 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 116 {rotary_count--;}
timmey9 22:523e316cbe70 117 else
timmey9 22:523e316cbe70 118 {rotary_count++;}
timmey9 22:523e316cbe70 119 #else
timmey9 22:523e316cbe70 120 {rotary_count++;}
timmey9 22:523e316cbe70 121 else
timmey9 22:523e316cbe70 122 {rotary_count--;}
timmey9 22:523e316cbe70 123 #endif
timmey9 22:523e316cbe70 124
timmey9 22:523e316cbe70 125 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 126 }
timmey9 40:bd6d8c35e822 127 }
timmey9 40:bd6d8c35e822 128