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.
main.cpp@0:61544337ff5e, 2018-05-03 (annotated)
- Committer:
- gunarthon
- Date:
- Thu May 03 20:39:50 2018 +0000
- Revision:
- 0:61544337ff5e
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| gunarthon | 0:61544337ff5e | 1 | #include "mbed.h" |
| gunarthon | 0:61544337ff5e | 2 | #include "main.h" |
| gunarthon | 0:61544337ff5e | 3 | #include "AnalogInDma.h" |
| gunarthon | 0:61544337ff5e | 4 | #include "Crosscorrel.h" |
| gunarthon | 0:61544337ff5e | 5 | #include "Definitions.h" |
| gunarthon | 0:61544337ff5e | 6 | #include <vector> |
| gunarthon | 0:61544337ff5e | 7 | #include <queue> |
| gunarthon | 0:61544337ff5e | 8 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 9 | enum { ADC_ACQ_LENGHT = 2 * ADC_LENGTH * 1}; |
| gunarthon | 0:61544337ff5e | 10 | enum { CAPUTURE_LENGTH = 512}; //6.64ms |
| gunarthon | 0:61544337ff5e | 11 | enum { ADC_MEAN_SIZE = 1024 }; |
| gunarthon | 0:61544337ff5e | 12 | enum { THREASHOLD = 100 }; |
| gunarthon | 0:61544337ff5e | 13 | enum { TAU = 100 }; |
| gunarthon | 0:61544337ff5e | 14 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 15 | enum { SERIAL_BUF_LENGTH = 1024 }; |
| gunarthon | 0:61544337ff5e | 16 | |
| gunarthon | 0:61544337ff5e | 17 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 18 | |
| gunarthon | 0:61544337ff5e | 19 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 20 | DigitalOut led1(LED1); |
| gunarthon | 0:61544337ff5e | 21 | |
| gunarthon | 0:61544337ff5e | 22 | DigitalOut adcFlag(D2); |
| gunarthon | 0:61544337ff5e | 23 | DigitalOut processFlag(D3); |
| gunarthon | 0:61544337ff5e | 24 | |
| gunarthon | 0:61544337ff5e | 25 | Serial pc(USBTX, USBRX); |
| gunarthon | 0:61544337ff5e | 26 | |
| gunarthon | 0:61544337ff5e | 27 | AnalogIn a0(A0); |
| gunarthon | 0:61544337ff5e | 28 | AnalogIn a1(A1); |
| gunarthon | 0:61544337ff5e | 29 | AnalogIn a2(A2); |
| gunarthon | 0:61544337ff5e | 30 | AnalogIn a3(A3); |
| gunarthon | 0:61544337ff5e | 31 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 32 | Timer timer; |
| gunarthon | 0:61544337ff5e | 33 | |
| gunarthon | 0:61544337ff5e | 34 | //------------------------------------------------------------------------------ |
| gunarthon | 0:61544337ff5e | 35 | DMA_HandleTypeDef hdma_adc1; |
| gunarthon | 0:61544337ff5e | 36 | |
| gunarthon | 0:61544337ff5e | 37 | AnalogInDma adc; |
| gunarthon | 0:61544337ff5e | 38 | |
| gunarthon | 0:61544337ff5e | 39 | uint32_t adcBuffer[ADC_ACQ_LENGHT]; |
| gunarthon | 0:61544337ff5e | 40 | vector<Signal> processQueue; |
| gunarthon | 0:61544337ff5e | 41 | |
| gunarthon | 0:61544337ff5e | 42 | int a = 0, b = 0; |
| gunarthon | 0:61544337ff5e | 43 | |
| gunarthon | 0:61544337ff5e | 44 | unsigned adcTime = 0; |
| gunarthon | 0:61544337ff5e | 45 | unsigned stayActive = 0; |
| gunarthon | 0:61544337ff5e | 46 | |
| gunarthon | 0:61544337ff5e | 47 | volatile bool captureReady = false; |
| gunarthon | 0:61544337ff5e | 48 | |
| gunarthon | 0:61544337ff5e | 49 | Signal adcValue; |
| gunarthon | 0:61544337ff5e | 50 | Signal adcMeanValue; |
| gunarthon | 0:61544337ff5e | 51 | Signal adcUnbiasedValue; |
| gunarthon | 0:61544337ff5e | 52 | |
| gunarthon | 0:61544337ff5e | 53 | void ProcessAdc(ADC_HandleTypeDef* AdcHandle, unsigned offset, unsigned length) |
| gunarthon | 0:61544337ff5e | 54 | { |
| gunarthon | 0:61544337ff5e | 55 | /* Prevent unused argument(s) compilation warning */ |
| gunarthon | 0:61544337ff5e | 56 | UNUSED(AdcHandle); |
| gunarthon | 0:61544337ff5e | 57 | adcFlag = 1; |
| gunarthon | 0:61544337ff5e | 58 | //adcTime = float(timer.read_us()) * 2 * ADC_LENGTH / ADC_ACQ_LENGHT; |
| gunarthon | 0:61544337ff5e | 59 | //timer.reset(); |
| gunarthon | 0:61544337ff5e | 60 | if(processQueue.size() < CAPUTURE_LENGTH) |
| gunarthon | 0:61544337ff5e | 61 | { |
| gunarthon | 0:61544337ff5e | 62 | led1 = 0; |
| gunarthon | 0:61544337ff5e | 63 | for(unsigned i = 0; i < length; i+= ADC_LENGTH) |
| gunarthon | 0:61544337ff5e | 64 | { |
| gunarthon | 0:61544337ff5e | 65 | for(unsigned ch = 0; ch < ADC_LENGTH; ch++) |
| gunarthon | 0:61544337ff5e | 66 | { |
| gunarthon | 0:61544337ff5e | 67 | adcValue[ch] = adcBuffer[offset+ch+i]; |
| gunarthon | 0:61544337ff5e | 68 | adcMeanValue[ch] += (adcValue[ch]-adcMeanValue[ch]) / ADC_MEAN_SIZE; |
| gunarthon | 0:61544337ff5e | 69 | adcUnbiasedValue[ch] = adcValue[ch] - adcMeanValue[ch]; |
| gunarthon | 0:61544337ff5e | 70 | } |
| gunarthon | 0:61544337ff5e | 71 | |
| gunarthon | 0:61544337ff5e | 72 | bool greaterThanThr = false; |
| gunarthon | 0:61544337ff5e | 73 | for(unsigned ch = 0; ch < ADC_LENGTH; ch++) |
| gunarthon | 0:61544337ff5e | 74 | if(adcUnbiasedValue[ch] > THREASHOLD) |
| gunarthon | 0:61544337ff5e | 75 | greaterThanThr = true; |
| gunarthon | 0:61544337ff5e | 76 | |
| gunarthon | 0:61544337ff5e | 77 | if(greaterThanThr) |
| gunarthon | 0:61544337ff5e | 78 | stayActive = true; |
| gunarthon | 0:61544337ff5e | 79 | |
| gunarthon | 0:61544337ff5e | 80 | if(stayActive) |
| gunarthon | 0:61544337ff5e | 81 | { |
| gunarthon | 0:61544337ff5e | 82 | if(processQueue.size() == 0) |
| gunarthon | 0:61544337ff5e | 83 | timer.reset(); |
| gunarthon | 0:61544337ff5e | 84 | processQueue.push_back(adcUnbiasedValue); |
| gunarthon | 0:61544337ff5e | 85 | } |
| gunarthon | 0:61544337ff5e | 86 | } |
| gunarthon | 0:61544337ff5e | 87 | } |
| gunarthon | 0:61544337ff5e | 88 | else { |
| gunarthon | 0:61544337ff5e | 89 | adcTime = timer.read_us(); |
| gunarthon | 0:61544337ff5e | 90 | captureReady = true; |
| gunarthon | 0:61544337ff5e | 91 | stayActive = false; |
| gunarthon | 0:61544337ff5e | 92 | } |
| gunarthon | 0:61544337ff5e | 93 | adcFlag = 0; |
| gunarthon | 0:61544337ff5e | 94 | } |
| gunarthon | 0:61544337ff5e | 95 | |
| gunarthon | 0:61544337ff5e | 96 | void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) |
| gunarthon | 0:61544337ff5e | 97 | { |
| gunarthon | 0:61544337ff5e | 98 | /* Prevent unused argument(s) compilation warning */ |
| gunarthon | 0:61544337ff5e | 99 | UNUSED(hadc); |
| gunarthon | 0:61544337ff5e | 100 | ProcessAdc(hadc, ADC_ACQ_LENGHT / 2, ADC_ACQ_LENGHT / 2); |
| gunarthon | 0:61544337ff5e | 101 | b++; |
| gunarthon | 0:61544337ff5e | 102 | } |
| gunarthon | 0:61544337ff5e | 103 | void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) |
| gunarthon | 0:61544337ff5e | 104 | { |
| gunarthon | 0:61544337ff5e | 105 | UNUSED(hadc); |
| gunarthon | 0:61544337ff5e | 106 | ProcessAdc(hadc, 0, ADC_ACQ_LENGHT / 2); |
| gunarthon | 0:61544337ff5e | 107 | b--; |
| gunarthon | 0:61544337ff5e | 108 | } |
| gunarthon | 0:61544337ff5e | 109 | |
| gunarthon | 0:61544337ff5e | 110 | |
| gunarthon | 0:61544337ff5e | 111 | // main() runs in its own thread in the OS |
| gunarthon | 0:61544337ff5e | 112 | int main() |
| gunarthon | 0:61544337ff5e | 113 | { |
| gunarthon | 0:61544337ff5e | 114 | pc.baud(1000000); |
| gunarthon | 0:61544337ff5e | 115 | //pc.attach(&serialInterruptTx, Serial::TxIrq); |
| gunarthon | 0:61544337ff5e | 116 | |
| gunarthon | 0:61544337ff5e | 117 | timer.start(); |
| gunarthon | 0:61544337ff5e | 118 | |
| gunarthon | 0:61544337ff5e | 119 | if(!adc.init()) |
| gunarthon | 0:61544337ff5e | 120 | _Error_Handler(__FILE__, __LINE__); |
| gunarthon | 0:61544337ff5e | 121 | if(!adc.start(adcBuffer, ADC_ACQ_LENGHT)) |
| gunarthon | 0:61544337ff5e | 122 | _Error_Handler(__FILE__, __LINE__); |
| gunarthon | 0:61544337ff5e | 123 | |
| gunarthon | 0:61544337ff5e | 124 | Crosscorrel crosscorrel; |
| gunarthon | 0:61544337ff5e | 125 | |
| gunarthon | 0:61544337ff5e | 126 | while (true) |
| gunarthon | 0:61544337ff5e | 127 | { |
| gunarthon | 0:61544337ff5e | 128 | a++; |
| gunarthon | 0:61544337ff5e | 129 | while(!captureReady); //wait for full capture |
| gunarthon | 0:61544337ff5e | 130 | |
| gunarthon | 0:61544337ff5e | 131 | NVIC_DisableIRQ(DMA1_Channel1_IRQn); |
| gunarthon | 0:61544337ff5e | 132 | |
| gunarthon | 0:61544337ff5e | 133 | //printf("Signals:\n"); |
| gunarthon | 0:61544337ff5e | 134 | for(unsigned t = 0; t < processQueue.size(); t++) //process captured signal |
| gunarthon | 0:61544337ff5e | 135 | { |
| gunarthon | 0:61544337ff5e | 136 | processFlag = 1; |
| gunarthon | 0:61544337ff5e | 137 | |
| gunarthon | 0:61544337ff5e | 138 | //serial debug sample |
| gunarthon | 0:61544337ff5e | 139 | pc.printf("%i,%i\n", int(processQueue[t][0]), int(processQueue[t][1])); |
| gunarthon | 0:61544337ff5e | 140 | } |
| gunarthon | 0:61544337ff5e | 141 | //printf("Crosscorrel:\n"); |
| gunarthon | 0:61544337ff5e | 142 | |
| gunarthon | 0:61544337ff5e | 143 | crosscorrel.GetMax(processQueue, 0, 1); |
| gunarthon | 0:61544337ff5e | 144 | |
| gunarthon | 0:61544337ff5e | 145 | processFlag = 0; |
| gunarthon | 0:61544337ff5e | 146 | |
| gunarthon | 0:61544337ff5e | 147 | adcFlag = 0; |
| gunarthon | 0:61544337ff5e | 148 | processQueue.clear(); |
| gunarthon | 0:61544337ff5e | 149 | captureReady = false; |
| gunarthon | 0:61544337ff5e | 150 | NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
| gunarthon | 0:61544337ff5e | 151 | } |
| gunarthon | 0:61544337ff5e | 152 | } |
| gunarthon | 0:61544337ff5e | 153 | |
| gunarthon | 0:61544337ff5e | 154 | void _Error_Handler(const char * file, int line) |
| gunarthon | 0:61544337ff5e | 155 | { |
| gunarthon | 0:61544337ff5e | 156 | /* USER CODE BEGIN Error_Handler_Debug */ |
| gunarthon | 0:61544337ff5e | 157 | /* User can add his own implementation to report the HAL error return state */ |
| gunarthon | 0:61544337ff5e | 158 | while(1) |
| gunarthon | 0:61544337ff5e | 159 | { |
| gunarthon | 0:61544337ff5e | 160 | } |
| gunarthon | 0:61544337ff5e | 161 | /* USER CODE END Error_Handler_Debug */ |
| gunarthon | 0:61544337ff5e | 162 | } |
| gunarthon | 0:61544337ff5e | 163 |
