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
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "AnalogInDma.h" 00004 #include "Crosscorrel.h" 00005 #include "Definitions.h" 00006 #include <vector> 00007 #include <queue> 00008 //------------------------------------------------------------------------------ 00009 enum { ADC_ACQ_LENGHT = 2 * ADC_LENGTH * 1}; 00010 enum { CAPUTURE_LENGTH = 512}; //6.64ms 00011 enum { ADC_MEAN_SIZE = 1024 }; 00012 enum { THREASHOLD = 100 }; 00013 enum { TAU = 100 }; 00014 //------------------------------------------------------------------------------ 00015 enum { SERIAL_BUF_LENGTH = 1024 }; 00016 00017 //------------------------------------------------------------------------------ 00018 00019 //------------------------------------------------------------------------------ 00020 DigitalOut led1(LED1); 00021 00022 DigitalOut adcFlag(D2); 00023 DigitalOut processFlag(D3); 00024 00025 Serial pc(USBTX, USBRX); 00026 00027 AnalogIn a0(A0); 00028 AnalogIn a1(A1); 00029 AnalogIn a2(A2); 00030 AnalogIn a3(A3); 00031 //------------------------------------------------------------------------------ 00032 Timer timer; 00033 00034 //------------------------------------------------------------------------------ 00035 DMA_HandleTypeDef hdma_adc1; 00036 00037 AnalogInDma adc; 00038 00039 uint32_t adcBuffer[ADC_ACQ_LENGHT]; 00040 vector<Signal> processQueue; 00041 00042 int a = 0, b = 0; 00043 00044 unsigned adcTime = 0; 00045 unsigned stayActive = 0; 00046 00047 volatile bool captureReady = false; 00048 00049 Signal adcValue; 00050 Signal adcMeanValue; 00051 Signal adcUnbiasedValue; 00052 00053 void ProcessAdc(ADC_HandleTypeDef* AdcHandle, unsigned offset, unsigned length) 00054 { 00055 /* Prevent unused argument(s) compilation warning */ 00056 UNUSED(AdcHandle); 00057 adcFlag = 1; 00058 //adcTime = float(timer.read_us()) * 2 * ADC_LENGTH / ADC_ACQ_LENGHT; 00059 //timer.reset(); 00060 if(processQueue.size() < CAPUTURE_LENGTH) 00061 { 00062 led1 = 0; 00063 for(unsigned i = 0; i < length; i+= ADC_LENGTH) 00064 { 00065 for(unsigned ch = 0; ch < ADC_LENGTH; ch++) 00066 { 00067 adcValue[ch] = adcBuffer[offset+ch+i]; 00068 adcMeanValue[ch] += (adcValue[ch]-adcMeanValue[ch]) / ADC_MEAN_SIZE; 00069 adcUnbiasedValue[ch] = adcValue[ch] - adcMeanValue[ch]; 00070 } 00071 00072 bool greaterThanThr = false; 00073 for(unsigned ch = 0; ch < ADC_LENGTH; ch++) 00074 if(adcUnbiasedValue[ch] > THREASHOLD) 00075 greaterThanThr = true; 00076 00077 if(greaterThanThr) 00078 stayActive = true; 00079 00080 if(stayActive) 00081 { 00082 if(processQueue.size() == 0) 00083 timer.reset(); 00084 processQueue.push_back(adcUnbiasedValue); 00085 } 00086 } 00087 } 00088 else { 00089 adcTime = timer.read_us(); 00090 captureReady = true; 00091 stayActive = false; 00092 } 00093 adcFlag = 0; 00094 } 00095 00096 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) 00097 { 00098 /* Prevent unused argument(s) compilation warning */ 00099 UNUSED(hadc); 00100 ProcessAdc(hadc, ADC_ACQ_LENGHT / 2, ADC_ACQ_LENGHT / 2); 00101 b++; 00102 } 00103 void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) 00104 { 00105 UNUSED(hadc); 00106 ProcessAdc(hadc, 0, ADC_ACQ_LENGHT / 2); 00107 b--; 00108 } 00109 00110 00111 // main() runs in its own thread in the OS 00112 int main() 00113 { 00114 pc.baud(1000000); 00115 //pc.attach(&serialInterruptTx, Serial::TxIrq); 00116 00117 timer.start(); 00118 00119 if(!adc.init()) 00120 _Error_Handler(__FILE__, __LINE__); 00121 if(!adc.start(adcBuffer, ADC_ACQ_LENGHT)) 00122 _Error_Handler(__FILE__, __LINE__); 00123 00124 Crosscorrel crosscorrel; 00125 00126 while (true) 00127 { 00128 a++; 00129 while(!captureReady); //wait for full capture 00130 00131 NVIC_DisableIRQ(DMA1_Channel1_IRQn); 00132 00133 //printf("Signals:\n"); 00134 for(unsigned t = 0; t < processQueue.size(); t++) //process captured signal 00135 { 00136 processFlag = 1; 00137 00138 //serial debug sample 00139 pc.printf("%i,%i\n", int(processQueue[t][0]), int(processQueue[t][1])); 00140 } 00141 //printf("Crosscorrel:\n"); 00142 00143 crosscorrel.GetMax(processQueue, 0, 1); 00144 00145 processFlag = 0; 00146 00147 adcFlag = 0; 00148 processQueue.clear(); 00149 captureReady = false; 00150 NVIC_EnableIRQ(DMA1_Channel1_IRQn); 00151 } 00152 } 00153 00154 void _Error_Handler(const char * file, int line) 00155 { 00156 /* USER CODE BEGIN Error_Handler_Debug */ 00157 /* User can add his own implementation to report the HAL error return state */ 00158 while(1) 00159 { 00160 } 00161 /* USER CODE END Error_Handler_Debug */ 00162 } 00163
Generated on Sat Aug 13 2022 13:32:45 by
1.7.2