
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp
- Committer:
- timmey9
- Date:
- 2015-01-25
- Revision:
- 35:df40c4566826
- Parent:
- 34:44cc9b76a507
- Child:
- 36:07d8a3143967
File content as of revision 35:df40c4566826:
// Server code #include "mbed.h" #include <stdio.h> // Analog sampling #include "PeripheralNames.h" #include "PeripheralPins.h" #include "fsl_adc_hal.h" #include "fsl_clock_manager.h" #include "fsl_dspi_hal.h" #include "AngleEncoder.h" #include "dma.h" // Analog sampling #define MAX_FADC 6000000 #define SAMPLING_RATE 1000000 // In microseconds, so 10 us will be a sampling rate of 100 kHz #define TOTAL_SAMPLES 5 // originally 30000 for 0.3 ms of sampling. #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms #define FIRST_SAMPLE_INDEX 0 #define BEGIN_SAMPLING 0xFFFFFFFF #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1) // for debug purposes Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder // Analog sampling AnalogIn A0_pin(A0); AnalogIn A2_pin(A2); Ticker Sampler; uint32_t current_sample_index = WAITING_TO_BEGIN; uint16_t sample_array1[TOTAL_SAMPLES]; uint16_t sample_array2[TOTAL_SAMPLES]; uint16_t angle_array[TOTAL_SAMPLES]; float currA0 = 0; float currA2 = 0; // Declaration of functions void analog_initialization(PinName pin); void timed_sampling(); // Important globabl variables necessary for the sampling every interval int rotary_count = 0; uint32_t last_AMT20_AB_read = 0; using namespace std; int main() { led_blue = 1; led_green = 1; led_red = 1; pc.baud(230400); pc.printf("Starting\r\n"); analog_initialization(A0); analog_initialization(A2); ADC0->SC2 |= ADC_SC2_DMAEN_MASK; // DMA Enable ADC0->SC3 = 0; // Reset SC3 dma_init(sample_array1,TOTAL_SAMPLES); pc.printf("SampleArr: %08x\r\n", &sample_array1); uint16_t* dma_csr = (uint16_t*) 0x4000901C; uint32_t* dma_daddr = (uint32_t*) 0x40009010; *dma_csr |= 1; // Start the sampling loop current_sample_index = WAITING_TO_BEGIN; Sampler.attach_us(&timed_sampling, SAMPLING_RATE); pc.printf("\r\n\r\n\r\n"); while(1) { if(pc.readable() > 0) { char temp = pc.getc(); switch(temp) { case 's': for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f\t",i,sample_array1[i]*3.3/65535); pc.printf("\r\n"); break; case 'f': for(int i = 0; i < TOTAL_SAMPLES; i++) sample_array1[i] = 0; break; } } for(int i = 0; i < TOTAL_SAMPLES; i++) pc.printf("%i: %f ",i,sample_array1[i]*3.3/65535); pc.printf("\r"); //pc.printf("DMA_DADDR: %08x \r", *dma_daddr); //pc.printf("A1: %f\tA2: %f\r\n", currA0, currA2); wait(1); } } void timed_sampling() { // Write to SC1A to start conversion with channel 12 PTB2 //ADC0_SC1A = (ADC_SC1_ADCH(ADC_CHANNEL) | (ADC0_SC1A & (ADC_SC1_AIEN_MASK | ADC_SC1_DIFF_MASK))); //__disable_irq(); // Disable Interrupts // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another //uint32_t A0_value = adc_hal_get_conversion_value(0, 0); // ADC0_RA //uint32_t A2_value = adc_hal_get_conversion_value(1, 0); BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2) BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10) /* currA0 = (float) A0_value*3.3/65535; currA2 = (float) A2_value*3.3/65535; // The following updates the rotary counter for the AMT20 sensor // Put A on PTC0 // Put B on PTC1 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03; if (AMT20_AB != last_AMT20_AB_read) { // change "INVERT_ANGLE" to change whether relative angle counts up or down. if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U) #if INVERT_ANGLE == 1 {rotary_count--;} else {rotary_count++;} #else {rotary_count++;} else {rotary_count--;} #endif last_AMT20_AB_read = AMT20_AB; } //current_sample_index = BEGIN_SAMPLING; // Used to force extra time. if (current_sample_index == WAITING_TO_BEGIN) {} else { if (current_sample_index == BEGIN_SAMPLING) { current_sample_index = FIRST_SAMPLE_INDEX; } sample_array1[current_sample_index] = A0_value; sample_array2[current_sample_index] = A2_value; angle_array[current_sample_index] = rotary_count; if (current_sample_index == LAST_SAMPLE_INDEX) { current_sample_index = WAITING_TO_BEGIN; } else { current_sample_index++; } } */ //__enable_irq(); // Enable Interrupts } void analog_initialization(PinName pin) { ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); // MBED_ASSERT(adc != (ADCName)NC); uint32_t instance = adc >> ADC_INSTANCE_SHIFT; clock_manager_set_gate(kClockModuleADC, instance, true); uint32_t bus_clock; clock_manager_get_frequency(kBusClock, &bus_clock); uint32_t clkdiv; for (clkdiv = 0; clkdiv < 4; clkdiv++) { if ((bus_clock >> clkdiv) <= MAX_FADC) break; } if (clkdiv == 4) { clkdiv = 0x7; //Set max div } // adc is enabled/triggered when reading. adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2)); adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3)); adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref); adc_hal_set_resolution_mode(instance, kAdcSingleDiff16); adc_hal_configure_continuous_conversion(instance, false); adc_hal_configure_hw_trigger(instance, false); // sw trigger adc_hal_configure_hw_average(instance, false); adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4); adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail pinmap_pinout(pin, PinMap_ADC); } /* // read some registers for some info. uint32_t* dma_cr = (uint32_t*) 0x40008000; pc.printf("DMA_CR: %08x\r\n", *dma_cr); uint32_t* dma_eei = (uint32_t*) 0x40008014; pc.printf("DMA_EEI: %08x\r\n", *dma_eei); uint32_t* dma_erq = (uint32_t*) 0x4000800C; pc.printf("DMA_ERQ: %08x\r\n", *dma_erq); uint16_t* dma_csr = (uint16_t*) 0x4000901C; pc.printf("DMA_TD0_CSR: %04x\r\n\n", *dma_csr); uint32_t* dma_saddr = (uint32_t*) 0x40009000; pc.printf("DMA_SAADR: %08x\r\n", *dma_saddr); uint16_t* dma_soff = (uint16_t*) 0x40009004; pc.printf("DMA_SOFF: %04x\r\n", *dma_soff); uint16_t* dma_attr = (uint16_t*) 0x40009006; pc.printf("DMA_ATTR: %04x\r\n", *dma_attr); uint32_t* dma_minor = (uint32_t*) 0x40009008; pc.printf("DMA_MINOR: %08x\r\n", *dma_minor); uint32_t* dma_daddr = (uint32_t*) 0x40009010; pc.printf("DMA_DADDR: %08x\r\n", *dma_daddr); // read some registers for some info. pc.printf("DMA_CR: %08x\r\n", *dma_cr); pc.printf("DMA_EEI: %08x\r\n", *dma_eei); pc.printf("DMA_ERQ: %08x\r\n", *dma_erq); pc.printf("DMA_TD0_CSR: %04x\r\n\n", *dma_csr); pc.printf("DMA_SAADR: %08x\r\n", *dma_saddr); pc.printf("DMA_SOFF: %04x\r\n", *dma_soff); pc.printf("DMA_ATTR: %04x\r\n", *dma_attr); pc.printf("DMA_MINOR: %08x\r\n", *dma_minor); pc.printf("DMA_DADDR: %08x\r\n", *dma_daddr); pc.printf("SampleArr: %08x\r\n",&sample_array1); */