Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
baxterja
Date:
Wed May 17 19:33:26 2017 +0000
Revision:
69:014d4bbd4e03
Parent:
68:e5031c18fefb
Child:
70:8cd7a8a2c153
Child:
71:973fac37c5b3
This is the still working code before I change it so that the adc channel swaps every other sample.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
timmey9 22:523e316cbe70 2
timmey9 52:5a40cc58c4c2 3 // Sampling
bmazzeo 53:83a90a47c1fd 4 #include "DMA_sampling/adc.h"
bmazzeo 54:1697dc574b96 5 #include "DMA_sampling/dac.h"
bmazzeo 55:2526b3317bc8 6 #include "DMA_sampling/pdb.h"
timmey9 22:523e316cbe70 7
bmazzeo 67:ec0c58490ce6 8 // DSP
bmazzeo 67:ec0c58490ce6 9 #include "dsp.h"
bmazzeo 67:ec0c58490ce6 10
bmazzeo 67:ec0c58490ce6 11
timmey9 22:523e316cbe70 12 // for debug purposes
timmey9 18:b17ddeeb1c09 13 Serial pc(USBTX, USBRX);
timmey9 18:b17ddeeb1c09 14 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 15 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 16 DigitalOut led_blue(LED_BLUE);
bmazzeo 58:4bee89daccff 17 DigitalOut status_0(D0);
bmazzeo 58:4bee89daccff 18 DigitalOut status_1(D1);
bmazzeo 66:72c5c24e532c 19 DigitalIn sw2(SW2);
bmazzeo 66:72c5c24e532c 20 DigitalIn sw3(SW3);
timmey9 51:43143a3fc2d7 21
timmey9 52:5a40cc58c4c2 22 // defined in dma.cpp
timmey9 45:d591d138cdeb 23 extern int len;
bmazzeo 54:1697dc574b96 24 extern uint16_t static_input_array0[];
bmazzeo 54:1697dc574b96 25 extern uint16_t static_input_array1[];
bmazzeo 55:2526b3317bc8 26 extern uint16_t static_output_array0[];
bmazzeo 57:7b8c49e1c1f6 27 extern uint16_t sampling_status;
bmazzeo 57:7b8c49e1c1f6 28
bmazzeo 67:ec0c58490ce6 29 // To set up FIR filtering
baxterja 69:014d4bbd4e03 30 #define TOTAL_TAPS 16
baxterja 69:014d4bbd4e03 31 #define STATE_LENGTH 79
bmazzeo 67:ec0c58490ce6 32 #define BUFFER_LENGTH 64
bmazzeo 67:ec0c58490ce6 33 uint16_t numTaps = TOTAL_TAPS;
bmazzeo 67:ec0c58490ce6 34 float State[STATE_LENGTH]= {0};
baxterja 69:014d4bbd4e03 35 float Coeffs[TOTAL_TAPS] = {0.0351885543264657, 0.0326718041339497, 0.0447859388579763, 0.0571840193174261, 0.0688188647212066, 0.0786841934234295, 0.0858044659079242, 0.0895870376972757, 0.0895870376972757, 0.0858044659079242, 0.0786841934234295, 0.0688188647212066, 0.0571840193174261, 0.0447859388579763, 0.0326718041339497, 0.0351885543264657};
bmazzeo 67:ec0c58490ce6 36 arm_fir_instance_f32 S = {numTaps, State, Coeffs};
bmazzeo 67:ec0c58490ce6 37
bmazzeo 67:ec0c58490ce6 38 float filter_input_array[BUFFER_LENGTH] = {0};
bmazzeo 67:ec0c58490ce6 39 float filter_output_array[BUFFER_LENGTH] = {0};
bmazzeo 67:ec0c58490ce6 40
bmazzeo 67:ec0c58490ce6 41
baxterja 69:014d4bbd4e03 42
baxterja 69:014d4bbd4e03 43 #define pre_compute_length 500
baxterja 69:014d4bbd4e03 44 #define CarrierFrequency 200
baxterja 69:014d4bbd4e03 45 #define SAMPLEFREQUENCY 100000
baxterja 69:014d4bbd4e03 46 //float i_mod_pre[pre_compute_length];
baxterja 69:014d4bbd4e03 47 //float q_mod_pre[pre_compute_length];
baxterja 69:014d4bbd4e03 48 uint16_t out_val_pre[pre_compute_length];
baxterja 69:014d4bbd4e03 49 float twopi = 3.14159265359 * 2;
baxterja 69:014d4bbd4e03 50
baxterja 69:014d4bbd4e03 51
baxterja 69:014d4bbd4e03 52 void pre_compute_tables() {
baxterja 69:014d4bbd4e03 53 // This function will precompute the cos and sin tables used in the rest of the program
baxterja 69:014d4bbd4e03 54 for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){
baxterja 69:014d4bbd4e03 55 out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0);
baxterja 69:014d4bbd4e03 56 //printf("%d\n\r",out_val_pre[precompute_counter]);
baxterja 69:014d4bbd4e03 57 //i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
baxterja 69:014d4bbd4e03 58 //q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter));
baxterja 69:014d4bbd4e03 59
baxterja 69:014d4bbd4e03 60 }
baxterja 69:014d4bbd4e03 61 }
baxterja 69:014d4bbd4e03 62
baxterja 69:014d4bbd4e03 63
emilmont 7:65188f4a8c25 64 int main() {
timmey9 22:523e316cbe70 65 led_blue = 1;
timmey9 35:df40c4566826 66 led_green = 1;
timmey9 18:b17ddeeb1c09 67 led_red = 1;
timmey9 34:44cc9b76a507 68
baxterja 69:014d4bbd4e03 69 int DAC_COUNTER = 0;
baxterja 69:014d4bbd4e03 70 //bool is_magnified_sample = true;
baxterja 69:014d4bbd4e03 71 pre_compute_tables();
baxterja 69:014d4bbd4e03 72
baxterja 69:014d4bbd4e03 73
timmey9 18:b17ddeeb1c09 74 pc.baud(230400);
timmey9 34:44cc9b76a507 75 pc.printf("Starting\r\n");
timmey9 27:8c2b30c855d1 76
bmazzeo 66:72c5c24e532c 77 pdb_init(); // Initalize PDB
bmazzeo 66:72c5c24e532c 78 dac_init(); // initializes DAC
timmey9 45:d591d138cdeb 79 adc_init(); // always initialize adc before dma
bmazzeo 53:83a90a47c1fd 80 pc.printf("ADC Initialized\r\n");
timmey9 51:43143a3fc2d7 81 dma_init(); // initializes DMAs
bmazzeo 55:2526b3317bc8 82 dma_reset(); // This clears any DMA triggers that may have gotten things into a different state
bmazzeo 67:ec0c58490ce6 83 pc.printf("Buffer Size: %i\r\n", len);
bmazzeo 54:1697dc574b96 84
timmey9 46:a015ebf4663b 85 led_green = 1;
timmey9 38:ec3b16c130d7 86
timmey9 40:bd6d8c35e822 87 pc.printf("\r\n\r\n\r\n");
timmey9 37:8bdc71f3e874 88
bmazzeo 64:bb4a4bd57681 89 pdb_start();
bmazzeo 64:bb4a4bd57681 90 while(1)
bmazzeo 61:a56cca07d4a6 91 {
bmazzeo 61:a56cca07d4a6 92 while(sampling_status == 0)
bmazzeo 61:a56cca07d4a6 93 {
bmazzeo 61:a56cca07d4a6 94 status_0 = 1;
bmazzeo 61:a56cca07d4a6 95 }
baxterja 69:014d4bbd4e03 96 /*
baxterja 69:014d4bbd4e03 97 if (is_magnified_sample)
baxterja 69:014d4bbd4e03 98 {
baxterja 69:014d4bbd4e03 99 ADC0_SC1A = 0x0D;
baxterja 69:014d4bbd4e03 100 }
baxterja 69:014d4bbd4e03 101 else
baxterja 69:014d4bbd4e03 102 {
baxterja 69:014d4bbd4e03 103 ADC0_SC1A = 0x0C;
baxterja 69:014d4bbd4e03 104 }
baxterja 69:014d4bbd4e03 105 is_magnified_sample = !is_magnified_sample;
baxterja 69:014d4bbd4e03 106 */
bmazzeo 61:a56cca07d4a6 107 sampling_status = 0;
bmazzeo 61:a56cca07d4a6 108 status_0 = 0;
bmazzeo 66:72c5c24e532c 109
bmazzeo 66:72c5c24e532c 110 if(sw2)
bmazzeo 61:a56cca07d4a6 111 {
bmazzeo 66:72c5c24e532c 112 if(sw3)
bmazzeo 66:72c5c24e532c 113 {
bmazzeo 66:72c5c24e532c 114 //Default PASSTHROUGH Condition
bmazzeo 66:72c5c24e532c 115 status_1 = 1;
bmazzeo 66:72c5c24e532c 116 for(int i = 0; i < len; i++)
bmazzeo 66:72c5c24e532c 117 {
baxterja 69:014d4bbd4e03 118 static_output_array0[i] = static_input_array0[i] >> 4;
baxterja 69:014d4bbd4e03 119
baxterja 69:014d4bbd4e03 120
baxterja 69:014d4bbd4e03 121
baxterja 69:014d4bbd4e03 122
bmazzeo 66:72c5c24e532c 123 }
bmazzeo 66:72c5c24e532c 124 status_1 = 0;
bmazzeo 66:72c5c24e532c 125 }
bmazzeo 66:72c5c24e532c 126 else
bmazzeo 66:72c5c24e532c 127 {
bmazzeo 66:72c5c24e532c 128 // Can show that buttons are active - Serial link working
bmazzeo 66:72c5c24e532c 129 status_1 = 1;
baxterja 69:014d4bbd4e03 130 pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]);
baxterja 69:014d4bbd4e03 131 //pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]);
bmazzeo 66:72c5c24e532c 132 for(int i = 0; i < len; i++)
bmazzeo 66:72c5c24e532c 133 {
bmazzeo 66:72c5c24e532c 134 static_output_array0[i] = static_input_array0[i] >> 4;
bmazzeo 66:72c5c24e532c 135 }
bmazzeo 66:72c5c24e532c 136 status_1 = 0;
bmazzeo 66:72c5c24e532c 137 }
bmazzeo 66:72c5c24e532c 138 }
bmazzeo 66:72c5c24e532c 139 else
bmazzeo 66:72c5c24e532c 140 {
bmazzeo 66:72c5c24e532c 141 // Here we can really put our DSP blocks
bmazzeo 66:72c5c24e532c 142 status_1 = 1;
bmazzeo 66:72c5c24e532c 143 for(int i = 0; i < len; i++)
bmazzeo 66:72c5c24e532c 144 {
bmazzeo 67:ec0c58490ce6 145 //static_output_array0[i] = static_input_array0[i] >> 4;
bmazzeo 67:ec0c58490ce6 146 //filter_input_array[i] = (float) (((int) static_input_array0[i]) - 0x8000);
bmazzeo 68:e5031c18fefb 147 filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000);
bmazzeo 67:ec0c58490ce6 148 }
bmazzeo 67:ec0c58490ce6 149
bmazzeo 67:ec0c58490ce6 150 //filter_input_array[0] = (float) static_input_array0[0];
bmazzeo 67:ec0c58490ce6 151
bmazzeo 68:e5031c18fefb 152 arm_fir_f32(&S, filter_input_array, filter_output_array, len);
bmazzeo 68:e5031c18fefb 153
bmazzeo 67:ec0c58490ce6 154
bmazzeo 68:e5031c18fefb 155 // Scale for output
bmazzeo 68:e5031c18fefb 156 /*
bmazzeo 67:ec0c58490ce6 157 for(int i = 0; i < len; i++)
bmazzeo 67:ec0c58490ce6 158 {
bmazzeo 67:ec0c58490ce6 159 //static_output_array0[i] = static_input_array0[i] >> 4;
bmazzeo 67:ec0c58490ce6 160 //filter_output_array[i] = 0.25 * filter_input_array[i];
bmazzeo 68:e5031c18fefb 161 filter_output_array[i] = 0.0625 * filter_output_array[i];
bmazzeo 67:ec0c58490ce6 162 }
bmazzeo 68:e5031c18fefb 163 */
bmazzeo 67:ec0c58490ce6 164
bmazzeo 67:ec0c58490ce6 165 for(int i = 0; i < len; i++)
bmazzeo 67:ec0c58490ce6 166 {
bmazzeo 67:ec0c58490ce6 167 //static_output_array0[i] = static_input_array0[i] >> 4;
bmazzeo 67:ec0c58490ce6 168 //static_output_array0[i] = (uint16_t) (( (int) filter_output_array[i] ) + 0x800);
baxterja 69:014d4bbd4e03 169 //static_output_array0[i] = (uint16_t) (filter_output_array[i] + 0x8000) >> 4;
bmazzeo 67:ec0c58490ce6 170 //static_output_array0[i] = (uint16_t) filter_input_array[i];
bmazzeo 67:ec0c58490ce6 171 //static_output_array0[i] = static_input_array0[i] >> 4;
baxterja 69:014d4bbd4e03 172
baxterja 69:014d4bbd4e03 173
baxterja 69:014d4bbd4e03 174 static_output_array0[i] = out_val_pre[DAC_COUNTER];
baxterja 69:014d4bbd4e03 175 DAC_COUNTER++;
baxterja 69:014d4bbd4e03 176 if (DAC_COUNTER>=pre_compute_length)
baxterja 69:014d4bbd4e03 177 {
baxterja 69:014d4bbd4e03 178 DAC_COUNTER = 0;
baxterja 69:014d4bbd4e03 179 }
baxterja 69:014d4bbd4e03 180 }
baxterja 69:014d4bbd4e03 181
baxterja 69:014d4bbd4e03 182
bmazzeo 66:72c5c24e532c 183 status_1 = 0;
bmazzeo 61:a56cca07d4a6 184 }
bmazzeo 61:a56cca07d4a6 185 }
bmazzeo 61:a56cca07d4a6 186 }
bmazzeo 63:7903a33e2fd4 187