
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp@70:8cd7a8a2c153, 2017-05-17 (annotated)
- Committer:
- baxterja
- Date:
- Wed May 17 23:10:03 2017 +0000
- Revision:
- 70:8cd7a8a2c153
- Parent:
- 69:014d4bbd4e03
- Child:
- 72:0b554f5026b9
This is mostly working, however I get 4 samples repeated for each actual sample.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
baxterja | 70:8cd7a8a2c153 | 11 | #define PRINT_BUFFER_LENGTH 2000 |
baxterja | 70:8cd7a8a2c153 | 12 | |
bmazzeo | 67:ec0c58490ce6 | 13 | |
timmey9 | 22:523e316cbe70 | 14 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 15 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 16 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 17 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 18 | DigitalOut led_blue(LED_BLUE); |
bmazzeo | 58:4bee89daccff | 19 | DigitalOut status_0(D0); |
bmazzeo | 58:4bee89daccff | 20 | DigitalOut status_1(D1); |
bmazzeo | 66:72c5c24e532c | 21 | DigitalIn sw2(SW2); |
bmazzeo | 66:72c5c24e532c | 22 | DigitalIn sw3(SW3); |
timmey9 | 51:43143a3fc2d7 | 23 | |
timmey9 | 52:5a40cc58c4c2 | 24 | // defined in dma.cpp |
timmey9 | 45:d591d138cdeb | 25 | extern int len; |
bmazzeo | 54:1697dc574b96 | 26 | extern uint16_t static_input_array0[]; |
bmazzeo | 54:1697dc574b96 | 27 | extern uint16_t static_input_array1[]; |
bmazzeo | 55:2526b3317bc8 | 28 | extern uint16_t static_output_array0[]; |
bmazzeo | 57:7b8c49e1c1f6 | 29 | extern uint16_t sampling_status; |
bmazzeo | 57:7b8c49e1c1f6 | 30 | |
bmazzeo | 67:ec0c58490ce6 | 31 | // To set up FIR filtering |
baxterja | 69:014d4bbd4e03 | 32 | #define TOTAL_TAPS 16 |
baxterja | 69:014d4bbd4e03 | 33 | #define STATE_LENGTH 79 |
bmazzeo | 67:ec0c58490ce6 | 34 | #define BUFFER_LENGTH 64 |
bmazzeo | 67:ec0c58490ce6 | 35 | uint16_t numTaps = TOTAL_TAPS; |
bmazzeo | 67:ec0c58490ce6 | 36 | float State[STATE_LENGTH]= {0}; |
baxterja | 69:014d4bbd4e03 | 37 | 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 | 38 | arm_fir_instance_f32 S = {numTaps, State, Coeffs}; |
bmazzeo | 67:ec0c58490ce6 | 39 | |
bmazzeo | 67:ec0c58490ce6 | 40 | float filter_input_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 41 | float filter_output_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 42 | |
bmazzeo | 67:ec0c58490ce6 | 43 | |
baxterja | 69:014d4bbd4e03 | 44 | |
baxterja | 69:014d4bbd4e03 | 45 | #define pre_compute_length 500 |
baxterja | 69:014d4bbd4e03 | 46 | #define CarrierFrequency 200 |
baxterja | 69:014d4bbd4e03 | 47 | #define SAMPLEFREQUENCY 100000 |
baxterja | 69:014d4bbd4e03 | 48 | //float i_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 49 | //float q_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 50 | uint16_t out_val_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 51 | float twopi = 3.14159265359 * 2; |
baxterja | 69:014d4bbd4e03 | 52 | |
baxterja | 69:014d4bbd4e03 | 53 | |
baxterja | 69:014d4bbd4e03 | 54 | void pre_compute_tables() { |
baxterja | 69:014d4bbd4e03 | 55 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 69:014d4bbd4e03 | 56 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 69:014d4bbd4e03 | 57 | out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0); |
baxterja | 69:014d4bbd4e03 | 58 | //printf("%d\n\r",out_val_pre[precompute_counter]); |
baxterja | 69:014d4bbd4e03 | 59 | //i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 60 | //q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 61 | |
baxterja | 69:014d4bbd4e03 | 62 | } |
baxterja | 69:014d4bbd4e03 | 63 | } |
baxterja | 69:014d4bbd4e03 | 64 | |
baxterja | 69:014d4bbd4e03 | 65 | |
emilmont | 7:65188f4a8c25 | 66 | int main() { |
timmey9 | 22:523e316cbe70 | 67 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 68 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 69 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 70 | |
baxterja | 69:014d4bbd4e03 | 71 | int DAC_COUNTER = 0; |
baxterja | 69:014d4bbd4e03 | 72 | //bool is_magnified_sample = true; |
baxterja | 69:014d4bbd4e03 | 73 | pre_compute_tables(); |
baxterja | 69:014d4bbd4e03 | 74 | |
baxterja | 69:014d4bbd4e03 | 75 | |
timmey9 | 18:b17ddeeb1c09 | 76 | pc.baud(230400); |
timmey9 | 34:44cc9b76a507 | 77 | pc.printf("Starting\r\n"); |
timmey9 | 27:8c2b30c855d1 | 78 | |
baxterja | 70:8cd7a8a2c153 | 79 | |
baxterja | 70:8cd7a8a2c153 | 80 | uint16_t output_print_buffer[PRINT_BUFFER_LENGTH]; |
baxterja | 70:8cd7a8a2c153 | 81 | uint16_t output_print_buffer2[PRINT_BUFFER_LENGTH]; |
baxterja | 70:8cd7a8a2c153 | 82 | int print_buffer_count = 0; |
baxterja | 70:8cd7a8a2c153 | 83 | |
bmazzeo | 66:72c5c24e532c | 84 | pdb_init(); // Initalize PDB |
bmazzeo | 66:72c5c24e532c | 85 | dac_init(); // initializes DAC |
timmey9 | 45:d591d138cdeb | 86 | adc_init(); // always initialize adc before dma |
bmazzeo | 53:83a90a47c1fd | 87 | pc.printf("ADC Initialized\r\n"); |
timmey9 | 51:43143a3fc2d7 | 88 | dma_init(); // initializes DMAs |
bmazzeo | 55:2526b3317bc8 | 89 | dma_reset(); // This clears any DMA triggers that may have gotten things into a different state |
bmazzeo | 67:ec0c58490ce6 | 90 | pc.printf("Buffer Size: %i\r\n", len); |
bmazzeo | 54:1697dc574b96 | 91 | |
timmey9 | 46:a015ebf4663b | 92 | led_green = 1; |
timmey9 | 38:ec3b16c130d7 | 93 | |
timmey9 | 40:bd6d8c35e822 | 94 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 95 | |
bmazzeo | 64:bb4a4bd57681 | 96 | pdb_start(); |
baxterja | 70:8cd7a8a2c153 | 97 | //while(print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 70:8cd7a8a2c153 | 98 | while(1) |
bmazzeo | 61:a56cca07d4a6 | 99 | { |
bmazzeo | 61:a56cca07d4a6 | 100 | while(sampling_status == 0) |
bmazzeo | 61:a56cca07d4a6 | 101 | { |
bmazzeo | 61:a56cca07d4a6 | 102 | status_0 = 1; |
bmazzeo | 61:a56cca07d4a6 | 103 | } |
baxterja | 69:014d4bbd4e03 | 104 | /* |
baxterja | 69:014d4bbd4e03 | 105 | if (is_magnified_sample) |
baxterja | 69:014d4bbd4e03 | 106 | { |
baxterja | 69:014d4bbd4e03 | 107 | ADC0_SC1A = 0x0D; |
baxterja | 69:014d4bbd4e03 | 108 | } |
baxterja | 69:014d4bbd4e03 | 109 | else |
baxterja | 69:014d4bbd4e03 | 110 | { |
baxterja | 69:014d4bbd4e03 | 111 | ADC0_SC1A = 0x0C; |
baxterja | 69:014d4bbd4e03 | 112 | } |
baxterja | 69:014d4bbd4e03 | 113 | is_magnified_sample = !is_magnified_sample; |
baxterja | 69:014d4bbd4e03 | 114 | */ |
bmazzeo | 61:a56cca07d4a6 | 115 | sampling_status = 0; |
bmazzeo | 61:a56cca07d4a6 | 116 | status_0 = 0; |
bmazzeo | 66:72c5c24e532c | 117 | |
bmazzeo | 66:72c5c24e532c | 118 | if(sw2) |
bmazzeo | 61:a56cca07d4a6 | 119 | { |
bmazzeo | 66:72c5c24e532c | 120 | if(sw3) |
bmazzeo | 66:72c5c24e532c | 121 | { |
bmazzeo | 66:72c5c24e532c | 122 | //Default PASSTHROUGH Condition |
bmazzeo | 66:72c5c24e532c | 123 | status_1 = 1; |
baxterja | 70:8cd7a8a2c153 | 124 | //printf("%d\n\r",static_input_array0[0]); |
bmazzeo | 66:72c5c24e532c | 125 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 126 | { |
baxterja | 70:8cd7a8a2c153 | 127 | static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 70:8cd7a8a2c153 | 128 | |
baxterja | 70:8cd7a8a2c153 | 129 | //printf("%d\n\r",static_input_array0[i]); |
baxterja | 70:8cd7a8a2c153 | 130 | output_print_buffer[print_buffer_count+i] = static_input_array0[i]; |
baxterja | 70:8cd7a8a2c153 | 131 | output_print_buffer2[print_buffer_count+i] = static_input_array1[i]; |
baxterja | 69:014d4bbd4e03 | 132 | |
baxterja | 70:8cd7a8a2c153 | 133 | if (i>4&&static_input_array0[i]==static_input_array0[i-1]&&static_input_array0[i]==static_input_array0[i-2]) |
baxterja | 70:8cd7a8a2c153 | 134 | printf("Y"); |
baxterja | 70:8cd7a8a2c153 | 135 | //if (print_buffer_count+i>=PRINT_BUFFER_LENGTH) |
baxterja | 70:8cd7a8a2c153 | 136 | //{ |
baxterja | 70:8cd7a8a2c153 | 137 | // break; |
baxterja | 70:8cd7a8a2c153 | 138 | // } |
baxterja | 69:014d4bbd4e03 | 139 | |
bmazzeo | 66:72c5c24e532c | 140 | } |
baxterja | 70:8cd7a8a2c153 | 141 | //print_buffer_count+=len; |
bmazzeo | 66:72c5c24e532c | 142 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 143 | } |
bmazzeo | 66:72c5c24e532c | 144 | else |
bmazzeo | 66:72c5c24e532c | 145 | { |
bmazzeo | 66:72c5c24e532c | 146 | // Can show that buttons are active - Serial link working |
bmazzeo | 66:72c5c24e532c | 147 | status_1 = 1; |
baxterja | 69:014d4bbd4e03 | 148 | pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
baxterja | 69:014d4bbd4e03 | 149 | //pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
bmazzeo | 66:72c5c24e532c | 150 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 151 | { |
bmazzeo | 66:72c5c24e532c | 152 | static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 66:72c5c24e532c | 153 | } |
bmazzeo | 66:72c5c24e532c | 154 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 155 | } |
bmazzeo | 66:72c5c24e532c | 156 | } |
bmazzeo | 66:72c5c24e532c | 157 | else |
bmazzeo | 66:72c5c24e532c | 158 | { |
bmazzeo | 66:72c5c24e532c | 159 | // Here we can really put our DSP blocks |
bmazzeo | 66:72c5c24e532c | 160 | status_1 = 1; |
bmazzeo | 66:72c5c24e532c | 161 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 162 | { |
bmazzeo | 67:ec0c58490ce6 | 163 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 164 | //filter_input_array[i] = (float) (((int) static_input_array0[i]) - 0x8000); |
bmazzeo | 68:e5031c18fefb | 165 | filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); |
bmazzeo | 67:ec0c58490ce6 | 166 | } |
bmazzeo | 67:ec0c58490ce6 | 167 | |
bmazzeo | 67:ec0c58490ce6 | 168 | //filter_input_array[0] = (float) static_input_array0[0]; |
bmazzeo | 67:ec0c58490ce6 | 169 | |
bmazzeo | 68:e5031c18fefb | 170 | arm_fir_f32(&S, filter_input_array, filter_output_array, len); |
bmazzeo | 68:e5031c18fefb | 171 | |
bmazzeo | 67:ec0c58490ce6 | 172 | |
bmazzeo | 68:e5031c18fefb | 173 | // Scale for output |
bmazzeo | 68:e5031c18fefb | 174 | /* |
bmazzeo | 67:ec0c58490ce6 | 175 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 176 | { |
bmazzeo | 67:ec0c58490ce6 | 177 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 178 | //filter_output_array[i] = 0.25 * filter_input_array[i]; |
bmazzeo | 68:e5031c18fefb | 179 | filter_output_array[i] = 0.0625 * filter_output_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 180 | } |
bmazzeo | 68:e5031c18fefb | 181 | */ |
bmazzeo | 67:ec0c58490ce6 | 182 | |
bmazzeo | 67:ec0c58490ce6 | 183 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 184 | { |
bmazzeo | 67:ec0c58490ce6 | 185 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 186 | //static_output_array0[i] = (uint16_t) (( (int) filter_output_array[i] ) + 0x800); |
baxterja | 69:014d4bbd4e03 | 187 | //static_output_array0[i] = (uint16_t) (filter_output_array[i] + 0x8000) >> 4; |
bmazzeo | 67:ec0c58490ce6 | 188 | //static_output_array0[i] = (uint16_t) filter_input_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 189 | //static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 69:014d4bbd4e03 | 190 | |
baxterja | 69:014d4bbd4e03 | 191 | |
baxterja | 69:014d4bbd4e03 | 192 | static_output_array0[i] = out_val_pre[DAC_COUNTER]; |
baxterja | 69:014d4bbd4e03 | 193 | DAC_COUNTER++; |
baxterja | 69:014d4bbd4e03 | 194 | if (DAC_COUNTER>=pre_compute_length) |
baxterja | 69:014d4bbd4e03 | 195 | { |
baxterja | 69:014d4bbd4e03 | 196 | DAC_COUNTER = 0; |
baxterja | 69:014d4bbd4e03 | 197 | } |
baxterja | 69:014d4bbd4e03 | 198 | } |
baxterja | 69:014d4bbd4e03 | 199 | |
baxterja | 69:014d4bbd4e03 | 200 | |
bmazzeo | 66:72c5c24e532c | 201 | status_1 = 0; |
bmazzeo | 61:a56cca07d4a6 | 202 | } |
baxterja | 70:8cd7a8a2c153 | 203 | |
bmazzeo | 61:a56cca07d4a6 | 204 | } |
baxterja | 70:8cd7a8a2c153 | 205 | |
baxterja | 70:8cd7a8a2c153 | 206 | for(int i = 0; i<PRINT_BUFFER_LENGTH; i++) |
baxterja | 70:8cd7a8a2c153 | 207 | { |
baxterja | 70:8cd7a8a2c153 | 208 | printf("%d %d\n\r",output_print_buffer[i],output_print_buffer2[i]); |
baxterja | 70:8cd7a8a2c153 | 209 | } |
baxterja | 70:8cd7a8a2c153 | 210 | |
baxterja | 70:8cd7a8a2c153 | 211 | |
bmazzeo | 61:a56cca07d4a6 | 212 | } |
bmazzeo | 63:7903a33e2fd4 | 213 |