Jared Baxter / Mbed 2 deprecated Impedance_Fast_Circuitry_print_V_I

Dependencies:   mbed-dsp mbed

Fork of Impedance_Fast_Circuitry by Jared Baxter

Committer:
timmey9
Date:
Fri Jan 30 06:59:19 2015 +0000
Revision:
41:3e0623d81b9a
Parent:
40:bd6d8c35e822
Child:
42:52a92a8d2cc7
Added DMA class.  Made changes to ADC.  This code still needs to be tested.

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 41:3e0623d81b9a 40
timmey9 41:3e0623d81b9a 41
timmey9 22:523e316cbe70 42 // Declaration of functions
timmey9 39:82dc3daecf32 43
timmey9 22:523e316cbe70 44 void timed_sampling();
timmey9 22:523e316cbe70 45
timmey9 22:523e316cbe70 46 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 47 int rotary_count = 0;
timmey9 22:523e316cbe70 48 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 49
timmey9 41:3e0623d81b9a 50 DMA dma(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES, &rotary_count);
timmey9 41:3e0623d81b9a 51
timmey9 39:82dc3daecf32 52
timmey9 22:523e316cbe70 53 using namespace std;
timmey9 17:2f978f823020 54
emilmont 7:65188f4a8c25 55 int main() {
timmey9 22:523e316cbe70 56 led_blue = 1;
timmey9 35:df40c4566826 57 led_green = 1;
timmey9 18:b17ddeeb1c09 58 led_red = 1;
timmey9 34:44cc9b76a507 59
timmey9 18:b17ddeeb1c09 60 pc.baud(230400);
timmey9 34:44cc9b76a507 61 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 62
timmey9 41:3e0623d81b9a 63 dma.reset();
timmey9 41:3e0623d81b9a 64 adc_init(A0);
timmey9 38:ec3b16c130d7 65
timmey9 40:bd6d8c35e822 66 pc.printf("\r\n\r\n\r\n");
timmey9 37:8bdc71f3e874 67
timmey9 34:44cc9b76a507 68 while(1) {
timmey9 36:07d8a3143967 69 rotary_count++;
timmey9 34:44cc9b76a507 70 if(pc.readable() > 0) {
timmey9 34:44cc9b76a507 71 char temp = pc.getc();
timmey9 34:44cc9b76a507 72
timmey9 34:44cc9b76a507 73 switch(temp) {
timmey9 34:44cc9b76a507 74 case 's':
timmey9 40:bd6d8c35e822 75 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\r\n",i,sample_array1[i]*3.3/65535);
timmey9 36:07d8a3143967 76
timmey9 35:df40c4566826 77 pc.printf("\r\n");
timmey9 34:44cc9b76a507 78 break;
timmey9 34:44cc9b76a507 79 case 'f':
timmey9 39:82dc3daecf32 80 for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0xf;
timmey9 34:44cc9b76a507 81 break;
timmey9 39:82dc3daecf32 82 case 'd':
timmey9 39:82dc3daecf32 83 for(int i = 0; i < 3; i++) pc.printf("Sample[%i]: %x\r\n", i, sample_array1[i]);
timmey9 39:82dc3daecf32 84 break;
timmey9 40:bd6d8c35e822 85 case 'a':
timmey9 41:3e0623d81b9a 86 adc_start();
timmey9 40:bd6d8c35e822 87 wait(1);
timmey9 41:3e0623d81b9a 88 adc_stop();
timmey9 40:bd6d8c35e822 89 break;
timmey9 34:44cc9b76a507 90 }
timmey9 34:44cc9b76a507 91 }
timmey9 40:bd6d8c35e822 92 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("A%i: %f ",i,sample_array1[i]*3.3/65535);
timmey9 39:82dc3daecf32 93
timmey9 40:bd6d8c35e822 94 //pc.printf("ADC0_RA: %08x\r\n",ADC0_RA);
timmey9 40:bd6d8c35e822 95 //pc.printf("ADC0_RB: %08x\r\n",ADC0_RB);
timmey9 39:82dc3daecf32 96 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("B%i: %f ",i,sample_array2[i]*3.3/65535);
timmey9 39:82dc3daecf32 97 //for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("C%i: %i ",i,angle_array[i]);
timmey9 40:bd6d8c35e822 98
timmey9 35:df40c4566826 99 //pc.printf("DMA_DADDR: %08x \r", *dma_daddr);
timmey9 35:df40c4566826 100 //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2);
timmey9 40:bd6d8c35e822 101 //wait(1);
timmey9 17:2f978f823020 102 }
timmey9 22:523e316cbe70 103 }
timmey9 23:9e5141647775 104
timmey9 22:523e316cbe70 105 void timed_sampling() {
timmey9 39:82dc3daecf32 106 // start ADC conversion
timmey9 22:523e316cbe70 107 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 108 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 109
timmey9 22:523e316cbe70 110 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 111 // Put A on PTC0
timmey9 22:523e316cbe70 112 // Put B on PTC1
timmey9 22:523e316cbe70 113 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 114 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 115 {
timmey9 22:523e316cbe70 116 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 117 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 118 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 119 {rotary_count--;}
timmey9 22:523e316cbe70 120 else
timmey9 22:523e316cbe70 121 {rotary_count++;}
timmey9 22:523e316cbe70 122 #else
timmey9 22:523e316cbe70 123 {rotary_count++;}
timmey9 22:523e316cbe70 124 else
timmey9 22:523e316cbe70 125 {rotary_count--;}
timmey9 22:523e316cbe70 126 #endif
timmey9 22:523e316cbe70 127
timmey9 22:523e316cbe70 128 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 129 }
timmey9 40:bd6d8c35e822 130 }
timmey9 40:bd6d8c35e822 131