Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Tue Jan 27 17:12:37 2015 +0000
Revision:
37:8bdc71f3e874
Parent:
36:07d8a3143967
Child:
38:ec3b16c130d7
Starting to add PDB to trigger ADC.  Using the mbed library, but about to switch to mbed-src.

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 22:523e316cbe70 12
timmey9 34:44cc9b76a507 13 #include "dma.h"
timmey9 28:4a833d59897b 14
timmey9 22:523e316cbe70 15 // Analog sampling
timmey9 22:523e316cbe70 16 #define MAX_FADC 6000000
timmey9 36:07d8a3143967 17 #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
timmey9 36:07d8a3143967 18 #define TOTAL_SAMPLES 3 // originally 30000 for 0.3 ms of sampling.
timmey9 22:523e316cbe70 19
timmey9 22:523e316cbe70 20 #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms
timmey9 22:523e316cbe70 21 #define FIRST_SAMPLE_INDEX 0
timmey9 22:523e316cbe70 22 #define BEGIN_SAMPLING 0xFFFFFFFF
timmey9 22:523e316cbe70 23 #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1)
timmey9 22:523e316cbe70 24
timmey9 21:1fb5023b72af 25
timmey9 22:523e316cbe70 26 // for debug purposes
timmey9 18:b17ddeeb1c09 27 Serial pc(USBTX, USBRX);
timmey9 18:b17ddeeb1c09 28 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 29 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 30 DigitalOut led_blue(LED_BLUE);
timmey9 18:b17ddeeb1c09 31
timmey9 22:523e316cbe70 32 AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
timmey9 22:523e316cbe70 33 DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 34 DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 35
timmey9 22:523e316cbe70 36 // Analog sampling
timmey9 22:523e316cbe70 37 AnalogIn A0_pin(A0);
timmey9 22:523e316cbe70 38 AnalogIn A2_pin(A2);
timmey9 34:44cc9b76a507 39 Ticker Sampler;
timmey9 22:523e316cbe70 40
timmey9 22:523e316cbe70 41 uint32_t current_sample_index = WAITING_TO_BEGIN;
timmey9 25:abbc19af13f9 42 uint16_t sample_array1[TOTAL_SAMPLES];
timmey9 25:abbc19af13f9 43 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 44 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 34:44cc9b76a507 45 float currA0 = 0;
timmey9 34:44cc9b76a507 46 float currA2 = 0;
timmey9 22:523e316cbe70 47
timmey9 22:523e316cbe70 48 // Declaration of functions
timmey9 22:523e316cbe70 49 void analog_initialization(PinName pin);
timmey9 22:523e316cbe70 50 void timed_sampling();
timmey9 22:523e316cbe70 51
timmey9 22:523e316cbe70 52 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 53 int rotary_count = 0;
timmey9 22:523e316cbe70 54 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 55
timmey9 22:523e316cbe70 56 using namespace std;
timmey9 17:2f978f823020 57
emilmont 7:65188f4a8c25 58 int main() {
timmey9 22:523e316cbe70 59 led_blue = 1;
timmey9 35:df40c4566826 60 led_green = 1;
timmey9 18:b17ddeeb1c09 61 led_red = 1;
timmey9 34:44cc9b76a507 62
timmey9 18:b17ddeeb1c09 63 pc.baud(230400);
timmey9 34:44cc9b76a507 64 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 65
timmey9 37:8bdc71f3e874 66 // initialize the Programmable Delay Block
timmey9 37:8bdc71f3e874 67 PDB0->MOD = ;// the period of the PDB
timmey9 37:8bdc71f3e874 68 PDB0->SC |= PDB_SC_CONT_MASK; // run PDB in continuous mode
timmey9 37:8bdc71f3e874 69 PDB0->SC |= PDB_SC_PDBEN_MASK; // enables the PDB
timmey9 37:8bdc71f3e874 70
timmey9 37:8bdc71f3e874 71
timmey9 37:8bdc71f3e874 72
timmey9 37:8bdc71f3e874 73
timmey9 27:8c2b30c855d1 74 analog_initialization(A0);
timmey9 27:8c2b30c855d1 75 analog_initialization(A2);
timmey9 27:8c2b30c855d1 76
timmey9 37:8bdc71f3e874 77 // put in high power mode for faster frequencies
timmey9 37:8bdc71f3e874 78 ADC0->CFG1 |= ADC_CFG1_ADLPC_MASK;
timmey9 37:8bdc71f3e874 79
timmey9 37:8bdc71f3e874 80 // put in continuous conversion mode
timmey9 37:8bdc71f3e874 81 pc.printf("ADC0_SC3: %08x\r\n", ADC0->SC3);
timmey9 37:8bdc71f3e874 82
timmey9 36:07d8a3143967 83 // enable the DMA
timmey9 36:07d8a3143967 84 ADC0->SC2 |= ADC_SC2_DMAEN_MASK;
timmey9 36:07d8a3143967 85 ADC1->SC2 |= ADC_SC2_DMAEN_MASK;
timmey9 35:df40c4566826 86 ADC0->SC3 = 0; // Reset SC3
timmey9 36:07d8a3143967 87 ADC1->SC3 = 0; // Reset SC3
timmey9 35:df40c4566826 88
timmey9 36:07d8a3143967 89 dma_init(sample_array1, sample_array2, angle_array, TOTAL_SAMPLES);
timmey9 35:df40c4566826 90 pc.printf("SampleArr: %08x\r\n", &sample_array1);
timmey9 35:df40c4566826 91 uint16_t* dma_csr = (uint16_t*) 0x4000901C;
timmey9 35:df40c4566826 92 uint32_t* dma_daddr = (uint32_t*) 0x40009010;
timmey9 35:df40c4566826 93 *dma_csr |= 1;
timmey9 35:df40c4566826 94
timmey9 27:8c2b30c855d1 95 // Start the sampling loop
timmey9 27:8c2b30c855d1 96 current_sample_index = WAITING_TO_BEGIN;
timmey9 34:44cc9b76a507 97 Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
timmey9 27:8c2b30c855d1 98
timmey9 35:df40c4566826 99 pc.printf("\r\n\r\n\r\n");
timmey9 35:df40c4566826 100
timmey9 34:44cc9b76a507 101 while(1) {
timmey9 36:07d8a3143967 102 rotary_count++;
timmey9 34:44cc9b76a507 103 if(pc.readable() > 0) {
timmey9 34:44cc9b76a507 104 char temp = pc.getc();
timmey9 34:44cc9b76a507 105
timmey9 34:44cc9b76a507 106 switch(temp) {
timmey9 34:44cc9b76a507 107 case 's':
timmey9 35:df40c4566826 108 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\t",i,sample_array1[i]*3.3/65535);
timmey9 36:07d8a3143967 109
timmey9 35:df40c4566826 110 pc.printf("\r\n");
timmey9 34:44cc9b76a507 111 break;
timmey9 34:44cc9b76a507 112 case 'f':
timmey9 34:44cc9b76a507 113 for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0;
timmey9 34:44cc9b76a507 114 break;
timmey9 34:44cc9b76a507 115
timmey9 34:44cc9b76a507 116 }
timmey9 34:44cc9b76a507 117 }
timmey9 36:07d8a3143967 118 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("A%i: %f ",i,sample_array1[i]*3.3/65535);
timmey9 36:07d8a3143967 119 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("B%i: %f ",i,sample_array2[i]*3.3/65535);
timmey9 36:07d8a3143967 120 for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("C%i: %i ",i,angle_array[i]);
timmey9 35:df40c4566826 121 pc.printf("\r");
timmey9 35:df40c4566826 122 //pc.printf("DMA_DADDR: %08x \r", *dma_daddr);
timmey9 35:df40c4566826 123 //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2);
timmey9 34:44cc9b76a507 124 wait(1);
timmey9 17:2f978f823020 125 }
timmey9 22:523e316cbe70 126 }
timmey9 23:9e5141647775 127
timmey9 22:523e316cbe70 128 void timed_sampling() {
timmey9 35:df40c4566826 129
timmey9 35:df40c4566826 130 // Write to SC1A to start conversion with channel 12 PTB2
timmey9 35:df40c4566826 131 //ADC0_SC1A = (ADC_SC1_ADCH(ADC_CHANNEL) | (ADC0_SC1A & (ADC_SC1_AIEN_MASK | ADC_SC1_DIFF_MASK)));
timmey9 35:df40c4566826 132
timmey9 35:df40c4566826 133 //__disable_irq(); // Disable Interrupts
timmey9 34:44cc9b76a507 134
timmey9 34:44cc9b76a507 135
timmey9 22:523e316cbe70 136 // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another
timmey9 35:df40c4566826 137 //uint32_t A0_value = adc_hal_get_conversion_value(0, 0); // ADC0_RA
timmey9 35:df40c4566826 138 //uint32_t A2_value = adc_hal_get_conversion_value(1, 0);
timmey9 35:df40c4566826 139
timmey9 35:df40c4566826 140
timmey9 22:523e316cbe70 141 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 142 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 143
timmey9 35:df40c4566826 144 /*
timmey9 34:44cc9b76a507 145 currA0 = (float) A0_value*3.3/65535;
timmey9 34:44cc9b76a507 146 currA2 = (float) A2_value*3.3/65535;
timmey9 36:07d8a3143967 147 */
timmey9 35:df40c4566826 148
timmey9 22:523e316cbe70 149 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 150 // Put A on PTC0
timmey9 22:523e316cbe70 151 // Put B on PTC1
timmey9 22:523e316cbe70 152 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 153 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 154 {
timmey9 22:523e316cbe70 155 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 156 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 157 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 158 {rotary_count--;}
timmey9 22:523e316cbe70 159 else
timmey9 22:523e316cbe70 160 {rotary_count++;}
timmey9 22:523e316cbe70 161 #else
timmey9 22:523e316cbe70 162 {rotary_count++;}
timmey9 22:523e316cbe70 163 else
timmey9 22:523e316cbe70 164 {rotary_count--;}
timmey9 22:523e316cbe70 165 #endif
timmey9 22:523e316cbe70 166
timmey9 22:523e316cbe70 167 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 168 }
timmey9 36:07d8a3143967 169 /*
timmey9 22:523e316cbe70 170 //current_sample_index = BEGIN_SAMPLING; // Used to force extra time.
timmey9 22:523e316cbe70 171 if (current_sample_index == WAITING_TO_BEGIN) {}
timmey9 22:523e316cbe70 172 else
timmey9 28:4a833d59897b 173 {
timmey9 22:523e316cbe70 174 if (current_sample_index == BEGIN_SAMPLING) {
timmey9 22:523e316cbe70 175 current_sample_index = FIRST_SAMPLE_INDEX;
timmey9 22:523e316cbe70 176 }
timmey9 22:523e316cbe70 177
timmey9 27:8c2b30c855d1 178 sample_array1[current_sample_index] = A0_value;
timmey9 27:8c2b30c855d1 179 sample_array2[current_sample_index] = A2_value;
timmey9 22:523e316cbe70 180 angle_array[current_sample_index] = rotary_count;
timmey9 22:523e316cbe70 181
timmey9 22:523e316cbe70 182 if (current_sample_index == LAST_SAMPLE_INDEX) {
timmey9 22:523e316cbe70 183 current_sample_index = WAITING_TO_BEGIN;
timmey9 22:523e316cbe70 184 }
timmey9 22:523e316cbe70 185 else { current_sample_index++; }
timmey9 28:4a833d59897b 186 }
timmey9 35:df40c4566826 187 */
timmey9 35:df40c4566826 188 //__enable_irq(); // Enable Interrupts
timmey9 22:523e316cbe70 189 }
timmey9 22:523e316cbe70 190
timmey9 22:523e316cbe70 191 void analog_initialization(PinName pin)
timmey9 22:523e316cbe70 192 {
timmey9 22:523e316cbe70 193 ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
timmey9 22:523e316cbe70 194 // MBED_ASSERT(adc != (ADCName)NC);
timmey9 22:523e316cbe70 195
timmey9 22:523e316cbe70 196 uint32_t instance = adc >> ADC_INSTANCE_SHIFT;
timmey9 22:523e316cbe70 197
timmey9 22:523e316cbe70 198 clock_manager_set_gate(kClockModuleADC, instance, true);
timmey9 22:523e316cbe70 199
timmey9 22:523e316cbe70 200 uint32_t bus_clock;
timmey9 22:523e316cbe70 201 clock_manager_get_frequency(kBusClock, &bus_clock);
timmey9 22:523e316cbe70 202 uint32_t clkdiv;
timmey9 22:523e316cbe70 203 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
timmey9 22:523e316cbe70 204 if ((bus_clock >> clkdiv) <= MAX_FADC)
timmey9 22:523e316cbe70 205 break;
timmey9 22:523e316cbe70 206 }
timmey9 22:523e316cbe70 207 if (clkdiv == 4) {
timmey9 22:523e316cbe70 208 clkdiv = 0x7; //Set max div
timmey9 22:523e316cbe70 209 }
timmey9 22:523e316cbe70 210 // adc is enabled/triggered when reading.
timmey9 22:523e316cbe70 211 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
timmey9 22:523e316cbe70 212 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
timmey9 22:523e316cbe70 213 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
timmey9 22:523e316cbe70 214 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
timmey9 37:8bdc71f3e874 215 adc_hal_configure_continuous_conversion(instance, false); // true=continuous conversion mode, false = single conversion mode
timmey9 37:8bdc71f3e874 216 adc_hal_configure_hw_trigger(instance, true); // true=hw trigger, false=sw trigger
timmey9 22:523e316cbe70 217 adc_hal_configure_hw_average(instance, false);
timmey9 22:523e316cbe70 218 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
timmey9 22:523e316cbe70 219 adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail
timmey9 22:523e316cbe70 220
timmey9 22:523e316cbe70 221 pinmap_pinout(pin, PinMap_ADC);
timmey9 35:df40c4566826 222 }
timmey9 35:df40c4566826 223
timmey9 35:df40c4566826 224
timmey9 35:df40c4566826 225
timmey9 35:df40c4566826 226 /*
timmey9 35:df40c4566826 227 // read some registers for some info.
timmey9 35:df40c4566826 228 uint32_t* dma_cr = (uint32_t*) 0x40008000;
timmey9 35:df40c4566826 229 pc.printf("DMA_CR: %08x\r\n", *dma_cr);
timmey9 35:df40c4566826 230
timmey9 35:df40c4566826 231 uint32_t* dma_eei = (uint32_t*) 0x40008014;
timmey9 35:df40c4566826 232 pc.printf("DMA_EEI: %08x\r\n", *dma_eei);
timmey9 35:df40c4566826 233
timmey9 35:df40c4566826 234 uint32_t* dma_erq = (uint32_t*) 0x4000800C;
timmey9 35:df40c4566826 235 pc.printf("DMA_ERQ: %08x\r\n", *dma_erq);
timmey9 35:df40c4566826 236
timmey9 35:df40c4566826 237 uint16_t* dma_csr = (uint16_t*) 0x4000901C;
timmey9 35:df40c4566826 238 pc.printf("DMA_TD0_CSR: %04x\r\n\n", *dma_csr);
timmey9 35:df40c4566826 239
timmey9 35:df40c4566826 240 uint32_t* dma_saddr = (uint32_t*) 0x40009000;
timmey9 35:df40c4566826 241 pc.printf("DMA_SAADR: %08x\r\n", *dma_saddr);
timmey9 35:df40c4566826 242
timmey9 35:df40c4566826 243 uint16_t* dma_soff = (uint16_t*) 0x40009004;
timmey9 35:df40c4566826 244 pc.printf("DMA_SOFF: %04x\r\n", *dma_soff);
timmey9 35:df40c4566826 245
timmey9 35:df40c4566826 246 uint16_t* dma_attr = (uint16_t*) 0x40009006;
timmey9 35:df40c4566826 247 pc.printf("DMA_ATTR: %04x\r\n", *dma_attr);
timmey9 35:df40c4566826 248
timmey9 35:df40c4566826 249 uint32_t* dma_minor = (uint32_t*) 0x40009008;
timmey9 35:df40c4566826 250 pc.printf("DMA_MINOR: %08x\r\n", *dma_minor);
timmey9 35:df40c4566826 251
timmey9 35:df40c4566826 252 uint32_t* dma_daddr = (uint32_t*) 0x40009010;
timmey9 35:df40c4566826 253 pc.printf("DMA_DADDR: %08x\r\n", *dma_daddr);
timmey9 35:df40c4566826 254
timmey9 35:df40c4566826 255
timmey9 35:df40c4566826 256 // read some registers for some info.
timmey9 35:df40c4566826 257 pc.printf("DMA_CR: %08x\r\n", *dma_cr);
timmey9 35:df40c4566826 258 pc.printf("DMA_EEI: %08x\r\n", *dma_eei);
timmey9 35:df40c4566826 259 pc.printf("DMA_ERQ: %08x\r\n", *dma_erq);
timmey9 35:df40c4566826 260 pc.printf("DMA_TD0_CSR: %04x\r\n\n", *dma_csr);
timmey9 35:df40c4566826 261 pc.printf("DMA_SAADR: %08x\r\n", *dma_saddr);
timmey9 35:df40c4566826 262 pc.printf("DMA_SOFF: %04x\r\n", *dma_soff);
timmey9 35:df40c4566826 263 pc.printf("DMA_ATTR: %04x\r\n", *dma_attr);
timmey9 35:df40c4566826 264 pc.printf("DMA_MINOR: %08x\r\n", *dma_minor);
timmey9 35:df40c4566826 265 pc.printf("DMA_DADDR: %08x\r\n", *dma_daddr);
timmey9 35:df40c4566826 266 pc.printf("SampleArr: %08x\r\n",&sample_array1);
timmey9 35:df40c4566826 267 */