Jared Baxter
/
Impedance_Fast_Circuitry
Fork of DSP_200kHz by
main.cpp@34:44cc9b76a507, 2015-01-25 (annotated)
- Committer:
- timmey9
- Date:
- Sun Jan 25 02:45:58 2015 +0000
- Revision:
- 34:44cc9b76a507
- Parent:
- 33:9806eb964362
- Child:
- 35:df40c4566826
Analog reading works. Trying to add DMA capabilities.
Who changed what in which revision?
User | Revision | Line number | New 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 | 34:44cc9b76a507 | 17 | #define SAMPLING_RATE 1000 // In microseconds, so 10 us will be a sampling rate of 100 kHz |
timmey9 | 34:44cc9b76a507 | 18 | #define TOTAL_SAMPLES 10 // 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 | 30:6a4ef939a93e | 60 | led_green = 0; |
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 | 27:8c2b30c855d1 | 66 | analog_initialization(A0); |
timmey9 | 27:8c2b30c855d1 | 67 | analog_initialization(A2); |
timmey9 | 27:8c2b30c855d1 | 68 | |
timmey9 | 34:44cc9b76a507 | 69 | dma_init(sample_array1,TOTAL_SAMPLES); |
timmey9 | 27:8c2b30c855d1 | 70 | |
timmey9 | 27:8c2b30c855d1 | 71 | // Start the sampling loop |
timmey9 | 27:8c2b30c855d1 | 72 | current_sample_index = WAITING_TO_BEGIN; |
timmey9 | 34:44cc9b76a507 | 73 | Sampler.attach_us(&timed_sampling, SAMPLING_RATE); |
timmey9 | 27:8c2b30c855d1 | 74 | |
timmey9 | 34:44cc9b76a507 | 75 | while(1) { |
timmey9 | 34:44cc9b76a507 | 76 | if(pc.readable() > 0) { |
timmey9 | 34:44cc9b76a507 | 77 | char temp = pc.getc(); |
timmey9 | 34:44cc9b76a507 | 78 | |
timmey9 | 34:44cc9b76a507 | 79 | switch(temp) { |
timmey9 | 34:44cc9b76a507 | 80 | case 's': |
timmey9 | 34:44cc9b76a507 | 81 | for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: ",i,sample_array1[i]); |
timmey9 | 34:44cc9b76a507 | 82 | break; |
timmey9 | 34:44cc9b76a507 | 83 | case 'f': |
timmey9 | 34:44cc9b76a507 | 84 | for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0; |
timmey9 | 34:44cc9b76a507 | 85 | break; |
timmey9 | 34:44cc9b76a507 | 86 | |
timmey9 | 34:44cc9b76a507 | 87 | } |
timmey9 | 34:44cc9b76a507 | 88 | } |
timmey9 | 34:44cc9b76a507 | 89 | pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2); |
timmey9 | 34:44cc9b76a507 | 90 | wait(1); |
timmey9 | 17:2f978f823020 | 91 | } |
timmey9 | 22:523e316cbe70 | 92 | |
timmey9 | 34:44cc9b76a507 | 93 | |
timmey9 | 22:523e316cbe70 | 94 | } |
timmey9 | 23:9e5141647775 | 95 | |
timmey9 | 22:523e316cbe70 | 96 | void timed_sampling() { |
timmey9 | 34:44cc9b76a507 | 97 | __disable_irq(); // Disable Interrupts |
timmey9 | 34:44cc9b76a507 | 98 | |
timmey9 | 34:44cc9b76a507 | 99 | |
timmey9 | 22:523e316cbe70 | 100 | // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another |
timmey9 | 22:523e316cbe70 | 101 | uint32_t A0_value = adc_hal_get_conversion_value(0, 0); |
timmey9 | 22:523e316cbe70 | 102 | uint32_t A2_value = adc_hal_get_conversion_value(1, 0); |
timmey9 | 22:523e316cbe70 | 103 | 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 | 104 | 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 | 105 | |
timmey9 | 34:44cc9b76a507 | 106 | currA0 = (float) A0_value*3.3/65535; |
timmey9 | 34:44cc9b76a507 | 107 | currA2 = (float) A2_value*3.3/65535; |
timmey9 | 34:44cc9b76a507 | 108 | |
timmey9 | 22:523e316cbe70 | 109 | // The following updates the rotary counter for the AMT20 sensor |
timmey9 | 22:523e316cbe70 | 110 | // Put A on PTC0 |
timmey9 | 22:523e316cbe70 | 111 | // Put B on PTC1 |
timmey9 | 22:523e316cbe70 | 112 | uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; |
timmey9 | 22:523e316cbe70 | 113 | //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time. |
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 | 22:523e316cbe70 | 130 | //current_sample_index = BEGIN_SAMPLING; // Used to force extra time. |
timmey9 | 22:523e316cbe70 | 131 | if (current_sample_index == WAITING_TO_BEGIN) {} |
timmey9 | 22:523e316cbe70 | 132 | else |
timmey9 | 28:4a833d59897b | 133 | { |
timmey9 | 22:523e316cbe70 | 134 | if (current_sample_index == BEGIN_SAMPLING) { |
timmey9 | 22:523e316cbe70 | 135 | current_sample_index = FIRST_SAMPLE_INDEX; |
timmey9 | 22:523e316cbe70 | 136 | } |
timmey9 | 22:523e316cbe70 | 137 | |
timmey9 | 27:8c2b30c855d1 | 138 | sample_array1[current_sample_index] = A0_value; |
timmey9 | 27:8c2b30c855d1 | 139 | sample_array2[current_sample_index] = A2_value; |
timmey9 | 22:523e316cbe70 | 140 | angle_array[current_sample_index] = rotary_count; |
timmey9 | 22:523e316cbe70 | 141 | |
timmey9 | 22:523e316cbe70 | 142 | if (current_sample_index == LAST_SAMPLE_INDEX) { |
timmey9 | 22:523e316cbe70 | 143 | current_sample_index = WAITING_TO_BEGIN; |
timmey9 | 22:523e316cbe70 | 144 | } |
timmey9 | 22:523e316cbe70 | 145 | else { current_sample_index++; } |
timmey9 | 28:4a833d59897b | 146 | } |
timmey9 | 22:523e316cbe70 | 147 | |
timmey9 | 34:44cc9b76a507 | 148 | __enable_irq(); // Enable Interrupts |
timmey9 | 22:523e316cbe70 | 149 | } |
timmey9 | 22:523e316cbe70 | 150 | |
timmey9 | 22:523e316cbe70 | 151 | void analog_initialization(PinName pin) |
timmey9 | 22:523e316cbe70 | 152 | { |
timmey9 | 22:523e316cbe70 | 153 | ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
timmey9 | 22:523e316cbe70 | 154 | // MBED_ASSERT(adc != (ADCName)NC); |
timmey9 | 22:523e316cbe70 | 155 | |
timmey9 | 22:523e316cbe70 | 156 | uint32_t instance = adc >> ADC_INSTANCE_SHIFT; |
timmey9 | 22:523e316cbe70 | 157 | |
timmey9 | 22:523e316cbe70 | 158 | clock_manager_set_gate(kClockModuleADC, instance, true); |
timmey9 | 22:523e316cbe70 | 159 | |
timmey9 | 22:523e316cbe70 | 160 | uint32_t bus_clock; |
timmey9 | 22:523e316cbe70 | 161 | clock_manager_get_frequency(kBusClock, &bus_clock); |
timmey9 | 22:523e316cbe70 | 162 | uint32_t clkdiv; |
timmey9 | 22:523e316cbe70 | 163 | for (clkdiv = 0; clkdiv < 4; clkdiv++) { |
timmey9 | 22:523e316cbe70 | 164 | if ((bus_clock >> clkdiv) <= MAX_FADC) |
timmey9 | 22:523e316cbe70 | 165 | break; |
timmey9 | 22:523e316cbe70 | 166 | } |
timmey9 | 22:523e316cbe70 | 167 | if (clkdiv == 4) { |
timmey9 | 22:523e316cbe70 | 168 | clkdiv = 0x7; //Set max div |
timmey9 | 22:523e316cbe70 | 169 | } |
timmey9 | 22:523e316cbe70 | 170 | // adc is enabled/triggered when reading. |
timmey9 | 22:523e316cbe70 | 171 | adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2)); |
timmey9 | 22:523e316cbe70 | 172 | adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3)); |
timmey9 | 22:523e316cbe70 | 173 | adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref); |
timmey9 | 22:523e316cbe70 | 174 | adc_hal_set_resolution_mode(instance, kAdcSingleDiff16); |
timmey9 | 22:523e316cbe70 | 175 | adc_hal_configure_continuous_conversion(instance, false); |
timmey9 | 22:523e316cbe70 | 176 | adc_hal_configure_hw_trigger(instance, false); // sw trigger |
timmey9 | 22:523e316cbe70 | 177 | adc_hal_configure_hw_average(instance, false); |
timmey9 | 22:523e316cbe70 | 178 | adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4); |
timmey9 | 22:523e316cbe70 | 179 | adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail |
timmey9 | 22:523e316cbe70 | 180 | |
timmey9 | 22:523e316cbe70 | 181 | pinmap_pinout(pin, PinMap_ADC); |
timmey9 | 34:44cc9b76a507 | 182 | } |