1 /* Project includes. */ 2 #include "main.h" 3 4 5 /* App includes. */ 6 #include "app.h" 7 #include "logger.h" 8 9 /* Application includes. */ 10 11 12 /********************** macros and definitions *******************************/ 13 14 #define TEST_1 (1) 15 #define TEST_2 (2) 16 #define TEST_3 (3) 17 18 #define TEST_NUMBER TEST_1 //TEST_1, TEST_2, TEST_3 19 20 #define SAMPLES_COUNTER (100) 21 #define AVERAGER_SIZE (16) 22 23 /********************** external data declaration *****************************/ 24 25 extern ADC_HandleTypeDef hadc1; 26 27 /********************** external functions definition ************************/ 28 29 30 31 /********************** internal data declaration ****************************/ 32 uint32_t tickstart; 33 uint16_t sample_idx; 34 35 #if TEST_3==TEST_NUMBER 36 uint16_t sample_array[SAMPLES_COUNTER]; 37 bool b_trig_new_conversion; 38 #endif 39 40 /********************** internal data definition *****************************/ 41 42 /********************** internal functions definitions ***********************/ 43 bool test1_tick(); 44 bool test2_tick(); 45 bool test3_tick(); 46 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc); 47 HAL_StatusTypeDef ADC_Poll_Read(uint16_t *value); 48 49 /********************** internal functions declaration ***********************/ 50 51 52 53 void app_init(void) 54 { 55 56 #if TEST_3==TEST_NUMBER 57 HAL_NVIC_SetPriority(ADC1_2_IRQn, 2, 0); 58 HAL_NVIC_EnableIRQ(ADC1_2_IRQn); 59 #endif 60 61 sample_idx = 0; 62 LOGGER_LOG ("Test #%u starts\n", TEST_NUMBER); 63 tickstart = HAL_GetTick(); 64 } 65 66 void app_update(void) 67 { 68 static bool b_test_done = false; 69 70 if (!b_test_done) { 71 #if TEST_1==TEST_NUMBER 72 b_test_done = test1_tick(); 73 #endif 74 #if TEST_2==TEST_NUMBER 75 b_test_done = test2_tick(); 76 #endif 77 #if TEST_3==TEST_NUMBER 78 b_test_done = test3_tick(); 79 #endif 80 81 if (b_test_done) { 82 LOGGER_LOG("Test #%u ends. Ticks: %lu\n", TEST_NUMBER, HAL_GetTick()-tickstart); 83 } 84 } 85 86 } 87 88 89 // Requests start of conversion, waits until conversion done 90 HAL_StatusTypeDef ADC_Poll_Read(uint16_t *value) { 91 HAL_StatusTypeDef res; 92 93 res=HAL_ADC_Start(&hadc1); 94 if ( HAL_OK==res ) { 95 res=HAL_ADC_PollForConversion(&hadc1, 0); 96 if ( HAL_OK==res ) { 97 *value = HAL_ADC_GetValue(&hadc1); 98 } 99 } 100 return res; 101 } 102 103 #if TEST_3==TEST_NUMBER 104 /* ADC callback */ 105 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { 106 107 sample_array[sample_idx++] = HAL_ADC_GetValue(&hadc1); 108 if (sample_idx<SAMPLES_COUNTER) { 109 b_trig_new_conversion = true; 110 } 111 } 112 #endif 113 114 115 #if TEST_1==TEST_NUMBER 116 bool test1_tick() { 117 118 uint16_t value; 119 bool b_done = false; 120 121 if (sample_idx>=SAMPLES_COUNTER) { 122 b_done = true; 123 goto test1_tick_end; 124 } 125 126 if (HAL_OK==ADC_Poll_Read(&value)) { 127 LOGGER_LOG("%u\n", value); 128 } 129 else 130 { 131 LOGGER_LOG("error\n"); 132 } 133 sample_idx++; 134 test1_tick_end: 135 return b_done; 136 } 137 #endif 138 139 #if TEST_2==TEST_NUMBER 140 bool test2_tick() { 141 142 uint32_t averaged = 0; 143 uint16_t value; 144 bool b_done = false; 145 146 if (sample_idx>=SAMPLES_COUNTER) { 147 b_done = true; 148 goto test2_tick_end; 149 } 150 151 for (uint16_t averager=0 ; averager<AVERAGER_SIZE ; averager++) { 152 //Activate the ADC peripheral and start conversions 153 if (HAL_OK==ADC_Poll_Read(&value)) { 154 averaged += value; 155 } 156 } 157 averaged = averaged / AVERAGER_SIZE; 158 LOGGER_LOG("%lu\n", averaged); 159 160 sample_idx++; 161 test2_tick_end: 162 return b_done; 163 } 164 #endif 165 166 #if TEST_3==TEST_NUMBER 167 bool test3_tick() { 168 169 bool b_done = false; 170 171 if (sample_idx>=SAMPLES_COUNTER) { 172 b_done = true; 173 goto test3_tick_end; 174 } 175 176 /* start of first conversion */ 177 if (0==sample_idx) { 178 b_trig_new_conversion = true; 179 } 180 181 182 if (b_trig_new_conversion) { 183 b_trig_new_conversion = false; 184 HAL_ADC_Start_IT(&hadc1); 185 } 186 187 test3_tick_end: 188 if (b_done) { 189 for (sample_idx=0;sample_idx<SAMPLES_COUNTER;sample_idx++) { 190 LOGGER_LOG("%u\n",sample_array[sample_idx] ); 191 } 192 } 193 return b_done; 194 } 195 #endif 196 197 /********************** end of file ******************************************/
