Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Fri Jan 30 08:14:36 2015 +0000
Revision:
43:c593a8b9688f
Parent:
42:52a92a8d2cc7
Child:
44:41c262caf898
Added PIT3 timer for transfering from PortC to DMA2.  PIT3 may be triggering the ADCs as well.

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 43:c593a8b9688f 14 #include "pit.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 43:c593a8b9688f 26 DigitalOut test1(PTB19);
timmey9 43:c593a8b9688f 27 DigitalOut test2(PTB18);
timmey9 18:b17ddeeb1c09 28
timmey9 22:523e316cbe70 29 AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
timmey9 22:523e316cbe70 30 DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 31 DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 32
timmey9 22:523e316cbe70 33 // Analog sampling
timmey9 34:44cc9b76a507 34 Ticker Sampler;
timmey9 22:523e316cbe70 35
timmey9 40:bd6d8c35e822 36 uint16_t sample_array1[TOTAL_SAMPLES];
timmey9 25:abbc19af13f9 37 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 38 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 34:44cc9b76a507 39 float currA0 = 0;
timmey9 34:44cc9b76a507 40 float currA2 = 0;
timmey9 22:523e316cbe70 41
timmey9 41:3e0623d81b9a 42
timmey9 41:3e0623d81b9a 43
timmey9 22:523e316cbe70 44 // Declaration of functions
timmey9 39:82dc3daecf32 45
timmey9 22:523e316cbe70 46 void timed_sampling();
timmey9 22:523e316cbe70 47
timmey9 22:523e316cbe70 48 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 49 int rotary_count = 0;
timmey9 22:523e316cbe70 50 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 51
timmey9 41:3e0623d81b9a 52 DMA dma(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, &rotary_count);
timmey9 41:3e0623d81b9a 53
timmey9 39:82dc3daecf32 54
timmey9 22:523e316cbe70 55 using namespace std;
timmey9 17:2f978f823020 56
emilmont 7:65188f4a8c25 57 int main() {
timmey9 22:523e316cbe70 58 led_blue = 1;
timmey9 35:df40c4566826 59 led_green = 1;
timmey9 18:b17ddeeb1c09 60 led_red = 1;
timmey9 34:44cc9b76a507 61
timmey9 18:b17ddeeb1c09 62 pc.baud(230400);
timmey9 34:44cc9b76a507 63 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 64
timmey9 41:3e0623d81b9a 65 dma.reset();
timmey9 42:52a92a8d2cc7 66 adc_init(A1);
timmey9 42:52a92a8d2cc7 67 adc_init(A2);
timmey9 43:c593a8b9688f 68 pit_init(pc);
timmey9 38:ec3b16c130d7 69
timmey9 40:bd6d8c35e822 70 pc.printf("\r\n\r\n\r\n");
timmey9 37:8bdc71f3e874 71
timmey9 34:44cc9b76a507 72 while(1) {
timmey9 43:c593a8b9688f 73
timmey9 36:07d8a3143967 74 rotary_count++;
timmey9 43:c593a8b9688f 75 if(rotary_count & 0x01) AMT20_A.mode(PullUp);
timmey9 43:c593a8b9688f 76 else AMT20_A.mode(PullDown);
timmey9 43:c593a8b9688f 77 if((rotary_count>>1) & 0x01) AMT20_B.mode(PullUp);
timmey9 43:c593a8b9688f 78 else AMT20_B.mode(PullDown);
timmey9 43:c593a8b9688f 79
timmey9 34:44cc9b76a507 80 if(pc.readable() > 0) {
timmey9 34:44cc9b76a507 81 char temp = pc.getc();
timmey9 34:44cc9b76a507 82
timmey9 34:44cc9b76a507 83 switch(temp) {
timmey9 40:bd6d8c35e822 84 case 'a':
timmey9 41:3e0623d81b9a 85 adc_start();
timmey9 40:bd6d8c35e822 86 wait(1);
timmey9 41:3e0623d81b9a 87 adc_stop();
timmey9 42:52a92a8d2cc7 88 // then proceed to 's' to display the array
timmey9 42:52a92a8d2cc7 89 case 's':
timmey9 43:c593a8b9688f 90 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\t %f\t %i%i\r\n",i,sample_array1[i]*3.3/65535,sample_array2[i]*3.3/65535, (angle_array[i]>>1)&0x01, angle_array[i]&0x01);
timmey9 42:52a92a8d2cc7 91 pc.printf("\r\n");
timmey9 42:52a92a8d2cc7 92 break;
timmey9 42:52a92a8d2cc7 93 case 'f':
timmey9 43:c593a8b9688f 94 for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array1[i] = 0; sample_array2[i] = 0; angle_array[i] = 0;}
timmey9 42:52a92a8d2cc7 95 pc.printf("Arrays cleared\r\n");
timmey9 40:bd6d8c35e822 96 break;
timmey9 43:c593a8b9688f 97 case 'r':
timmey9 43:c593a8b9688f 98 pc.printf("Quadrature: %i%i \r\n", (HW_GPIO_PDIR_RD(HW_PORTC)>>1)&0x01, HW_GPIO_PDIR_RD(HW_PORTC)&0x01);
timmey9 34:44cc9b76a507 99 }
timmey9 34:44cc9b76a507 100 }
timmey9 17:2f978f823020 101 }
timmey9 22:523e316cbe70 102 }
timmey9 23:9e5141647775 103
timmey9 22:523e316cbe70 104 void timed_sampling() {
timmey9 22:523e316cbe70 105
timmey9 22:523e316cbe70 106 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 107 // Put A on PTC0
timmey9 22:523e316cbe70 108 // Put B on PTC1
timmey9 22:523e316cbe70 109 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 110 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 111 {
timmey9 22:523e316cbe70 112 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 113 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 114 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 115 {rotary_count--;}
timmey9 22:523e316cbe70 116 else
timmey9 22:523e316cbe70 117 {rotary_count++;}
timmey9 22:523e316cbe70 118 #else
timmey9 22:523e316cbe70 119 {rotary_count++;}
timmey9 22:523e316cbe70 120 else
timmey9 22:523e316cbe70 121 {rotary_count--;}
timmey9 22:523e316cbe70 122 #endif
timmey9 22:523e316cbe70 123
timmey9 22:523e316cbe70 124 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 125 }
timmey9 42:52a92a8d2cc7 126 }