Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Impedance_Fast_Circuitry by
main.cpp@71:973fac37c5b3, 2017-05-18 (annotated)
- Committer:
- baxterja
- Date:
- Thu May 18 17:51:33 2017 +0000
- Revision:
- 71:973fac37c5b3
- Parent:
- 69:014d4bbd4e03
Dr. Mazzeo's code saving output to a print_buffer
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 | |
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 | |
baxterja | 71:973fac37c5b3 | 29 | |
baxterja | 71:973fac37c5b3 | 30 | #define PRINT_BUFFER_LENGTH 2000 |
baxterja | 71:973fac37c5b3 | 31 | uint16_t print_buffer[PRINT_BUFFER_LENGTH] = {0}; |
baxterja | 71:973fac37c5b3 | 32 | |
bmazzeo | 67:ec0c58490ce6 | 33 | // To set up FIR filtering |
baxterja | 69:014d4bbd4e03 | 34 | #define TOTAL_TAPS 16 |
baxterja | 69:014d4bbd4e03 | 35 | #define STATE_LENGTH 79 |
bmazzeo | 67:ec0c58490ce6 | 36 | #define BUFFER_LENGTH 64 |
bmazzeo | 67:ec0c58490ce6 | 37 | uint16_t numTaps = TOTAL_TAPS; |
bmazzeo | 67:ec0c58490ce6 | 38 | float State[STATE_LENGTH]= {0}; |
baxterja | 69:014d4bbd4e03 | 39 | 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 | 40 | arm_fir_instance_f32 S = {numTaps, State, Coeffs}; |
bmazzeo | 67:ec0c58490ce6 | 41 | |
bmazzeo | 67:ec0c58490ce6 | 42 | float filter_input_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 43 | float filter_output_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 44 | |
bmazzeo | 67:ec0c58490ce6 | 45 | |
baxterja | 69:014d4bbd4e03 | 46 | |
baxterja | 69:014d4bbd4e03 | 47 | #define pre_compute_length 500 |
baxterja | 69:014d4bbd4e03 | 48 | #define CarrierFrequency 200 |
baxterja | 69:014d4bbd4e03 | 49 | #define SAMPLEFREQUENCY 100000 |
baxterja | 69:014d4bbd4e03 | 50 | //float i_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 51 | //float q_mod_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 52 | uint16_t out_val_pre[pre_compute_length]; |
baxterja | 69:014d4bbd4e03 | 53 | float twopi = 3.14159265359 * 2; |
baxterja | 69:014d4bbd4e03 | 54 | |
baxterja | 69:014d4bbd4e03 | 55 | |
baxterja | 69:014d4bbd4e03 | 56 | void pre_compute_tables() { |
baxterja | 69:014d4bbd4e03 | 57 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 69:014d4bbd4e03 | 58 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 69:014d4bbd4e03 | 59 | out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0); |
baxterja | 69:014d4bbd4e03 | 60 | //printf("%d\n\r",out_val_pre[precompute_counter]); |
baxterja | 69:014d4bbd4e03 | 61 | //i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 62 | //q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency * TimerInterruptInMicroSeconds * 1e-6 * precompute_counter)); |
baxterja | 69:014d4bbd4e03 | 63 | |
baxterja | 69:014d4bbd4e03 | 64 | } |
baxterja | 69:014d4bbd4e03 | 65 | } |
baxterja | 69:014d4bbd4e03 | 66 | |
baxterja | 69:014d4bbd4e03 | 67 | |
emilmont | 7:65188f4a8c25 | 68 | int main() { |
timmey9 | 22:523e316cbe70 | 69 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 70 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 71 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 72 | |
baxterja | 69:014d4bbd4e03 | 73 | int DAC_COUNTER = 0; |
baxterja | 71:973fac37c5b3 | 74 | int print_buffer_count = 0; |
baxterja | 69:014d4bbd4e03 | 75 | //bool is_magnified_sample = true; |
baxterja | 69:014d4bbd4e03 | 76 | pre_compute_tables(); |
baxterja | 69:014d4bbd4e03 | 77 | |
baxterja | 69:014d4bbd4e03 | 78 | |
timmey9 | 18:b17ddeeb1c09 | 79 | pc.baud(230400); |
timmey9 | 34:44cc9b76a507 | 80 | pc.printf("Starting\r\n"); |
timmey9 | 27:8c2b30c855d1 | 81 | |
bmazzeo | 66:72c5c24e532c | 82 | pdb_init(); // Initalize PDB |
bmazzeo | 66:72c5c24e532c | 83 | dac_init(); // initializes DAC |
timmey9 | 45:d591d138cdeb | 84 | adc_init(); // always initialize adc before dma |
bmazzeo | 53:83a90a47c1fd | 85 | pc.printf("ADC Initialized\r\n"); |
timmey9 | 51:43143a3fc2d7 | 86 | dma_init(); // initializes DMAs |
bmazzeo | 55:2526b3317bc8 | 87 | dma_reset(); // This clears any DMA triggers that may have gotten things into a different state |
bmazzeo | 67:ec0c58490ce6 | 88 | pc.printf("Buffer Size: %i\r\n", len); |
bmazzeo | 54:1697dc574b96 | 89 | |
timmey9 | 46:a015ebf4663b | 90 | led_green = 1; |
timmey9 | 38:ec3b16c130d7 | 91 | |
timmey9 | 40:bd6d8c35e822 | 92 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 93 | |
bmazzeo | 64:bb4a4bd57681 | 94 | pdb_start(); |
baxterja | 71:973fac37c5b3 | 95 | while(print_buffer_count<PRINT_BUFFER_LENGTH) |
bmazzeo | 61:a56cca07d4a6 | 96 | { |
bmazzeo | 61:a56cca07d4a6 | 97 | while(sampling_status == 0) |
bmazzeo | 61:a56cca07d4a6 | 98 | { |
bmazzeo | 61:a56cca07d4a6 | 99 | status_0 = 1; |
bmazzeo | 61:a56cca07d4a6 | 100 | } |
baxterja | 69:014d4bbd4e03 | 101 | /* |
baxterja | 69:014d4bbd4e03 | 102 | if (is_magnified_sample) |
baxterja | 69:014d4bbd4e03 | 103 | { |
baxterja | 69:014d4bbd4e03 | 104 | ADC0_SC1A = 0x0D; |
baxterja | 69:014d4bbd4e03 | 105 | } |
baxterja | 69:014d4bbd4e03 | 106 | else |
baxterja | 69:014d4bbd4e03 | 107 | { |
baxterja | 69:014d4bbd4e03 | 108 | ADC0_SC1A = 0x0C; |
baxterja | 69:014d4bbd4e03 | 109 | } |
baxterja | 69:014d4bbd4e03 | 110 | is_magnified_sample = !is_magnified_sample; |
baxterja | 69:014d4bbd4e03 | 111 | */ |
bmazzeo | 61:a56cca07d4a6 | 112 | sampling_status = 0; |
bmazzeo | 61:a56cca07d4a6 | 113 | status_0 = 0; |
bmazzeo | 66:72c5c24e532c | 114 | |
bmazzeo | 66:72c5c24e532c | 115 | if(sw2) |
bmazzeo | 61:a56cca07d4a6 | 116 | { |
bmazzeo | 66:72c5c24e532c | 117 | if(sw3) |
bmazzeo | 66:72c5c24e532c | 118 | { |
bmazzeo | 66:72c5c24e532c | 119 | //Default PASSTHROUGH Condition |
bmazzeo | 66:72c5c24e532c | 120 | status_1 = 1; |
bmazzeo | 66:72c5c24e532c | 121 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 122 | { |
baxterja | 69:014d4bbd4e03 | 123 | static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 71:973fac37c5b3 | 124 | print_buffer[print_buffer_count] = static_input_array0[i]; |
baxterja | 71:973fac37c5b3 | 125 | print_buffer_count++; |
baxterja | 71:973fac37c5b3 | 126 | if (print_buffer_count>=PRINT_BUFFER_LENGTH) |
baxterja | 71:973fac37c5b3 | 127 | break; |
baxterja | 69:014d4bbd4e03 | 128 | |
baxterja | 69:014d4bbd4e03 | 129 | |
bmazzeo | 66:72c5c24e532c | 130 | } |
bmazzeo | 66:72c5c24e532c | 131 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 132 | } |
bmazzeo | 66:72c5c24e532c | 133 | else |
bmazzeo | 66:72c5c24e532c | 134 | { |
bmazzeo | 66:72c5c24e532c | 135 | // Can show that buttons are active - Serial link working |
bmazzeo | 66:72c5c24e532c | 136 | status_1 = 1; |
baxterja | 69:014d4bbd4e03 | 137 | pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
baxterja | 69:014d4bbd4e03 | 138 | //pc.printf("DSP: %d\r\n",out_val_pre[DAC_COUNTER]); |
bmazzeo | 66:72c5c24e532c | 139 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 140 | { |
bmazzeo | 66:72c5c24e532c | 141 | static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 66:72c5c24e532c | 142 | } |
bmazzeo | 66:72c5c24e532c | 143 | status_1 = 0; |
bmazzeo | 66:72c5c24e532c | 144 | } |
bmazzeo | 66:72c5c24e532c | 145 | } |
bmazzeo | 66:72c5c24e532c | 146 | else |
bmazzeo | 66:72c5c24e532c | 147 | { |
bmazzeo | 66:72c5c24e532c | 148 | // Here we can really put our DSP blocks |
bmazzeo | 66:72c5c24e532c | 149 | status_1 = 1; |
bmazzeo | 66:72c5c24e532c | 150 | for(int i = 0; i < len; i++) |
bmazzeo | 66:72c5c24e532c | 151 | { |
bmazzeo | 67:ec0c58490ce6 | 152 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 153 | //filter_input_array[i] = (float) (((int) static_input_array0[i]) - 0x8000); |
bmazzeo | 68:e5031c18fefb | 154 | filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); |
bmazzeo | 67:ec0c58490ce6 | 155 | } |
bmazzeo | 67:ec0c58490ce6 | 156 | |
bmazzeo | 67:ec0c58490ce6 | 157 | //filter_input_array[0] = (float) static_input_array0[0]; |
bmazzeo | 67:ec0c58490ce6 | 158 | |
bmazzeo | 68:e5031c18fefb | 159 | arm_fir_f32(&S, filter_input_array, filter_output_array, len); |
bmazzeo | 68:e5031c18fefb | 160 | |
bmazzeo | 67:ec0c58490ce6 | 161 | |
bmazzeo | 68:e5031c18fefb | 162 | // Scale for output |
bmazzeo | 68:e5031c18fefb | 163 | /* |
bmazzeo | 67:ec0c58490ce6 | 164 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 165 | { |
bmazzeo | 67:ec0c58490ce6 | 166 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 167 | //filter_output_array[i] = 0.25 * filter_input_array[i]; |
bmazzeo | 68:e5031c18fefb | 168 | filter_output_array[i] = 0.0625 * filter_output_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 169 | } |
bmazzeo | 68:e5031c18fefb | 170 | */ |
bmazzeo | 67:ec0c58490ce6 | 171 | |
bmazzeo | 67:ec0c58490ce6 | 172 | for(int i = 0; i < len; i++) |
bmazzeo | 67:ec0c58490ce6 | 173 | { |
bmazzeo | 67:ec0c58490ce6 | 174 | //static_output_array0[i] = static_input_array0[i] >> 4; |
bmazzeo | 67:ec0c58490ce6 | 175 | //static_output_array0[i] = (uint16_t) (( (int) filter_output_array[i] ) + 0x800); |
baxterja | 69:014d4bbd4e03 | 176 | //static_output_array0[i] = (uint16_t) (filter_output_array[i] + 0x8000) >> 4; |
bmazzeo | 67:ec0c58490ce6 | 177 | //static_output_array0[i] = (uint16_t) filter_input_array[i]; |
bmazzeo | 67:ec0c58490ce6 | 178 | //static_output_array0[i] = static_input_array0[i] >> 4; |
baxterja | 69:014d4bbd4e03 | 179 | |
baxterja | 69:014d4bbd4e03 | 180 | |
baxterja | 69:014d4bbd4e03 | 181 | static_output_array0[i] = out_val_pre[DAC_COUNTER]; |
baxterja | 69:014d4bbd4e03 | 182 | DAC_COUNTER++; |
baxterja | 69:014d4bbd4e03 | 183 | if (DAC_COUNTER>=pre_compute_length) |
baxterja | 69:014d4bbd4e03 | 184 | { |
baxterja | 69:014d4bbd4e03 | 185 | DAC_COUNTER = 0; |
baxterja | 69:014d4bbd4e03 | 186 | } |
baxterja | 69:014d4bbd4e03 | 187 | } |
baxterja | 69:014d4bbd4e03 | 188 | |
baxterja | 69:014d4bbd4e03 | 189 | |
bmazzeo | 66:72c5c24e532c | 190 | status_1 = 0; |
bmazzeo | 61:a56cca07d4a6 | 191 | } |
bmazzeo | 61:a56cca07d4a6 | 192 | } |
baxterja | 71:973fac37c5b3 | 193 | for(int i = 0; i<PRINT_BUFFER_LENGTH; i++) |
baxterja | 71:973fac37c5b3 | 194 | { |
baxterja | 71:973fac37c5b3 | 195 | printf("%d\n\r",print_buffer[i]); |
baxterja | 71:973fac37c5b3 | 196 | } |
bmazzeo | 61:a56cca07d4a6 | 197 | } |
bmazzeo | 63:7903a33e2fd4 | 198 |