adc_global.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * adc_global.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "adc_global.h"
  8. #include "buzzer.h"
  9. #include "iron.h"
  10. #include "tempsensors.h"
  11. #include "voltagesensors.h"
  12. #include "board.h"
  13. volatile adc_measures_t ADC_measures[ADC_BFSIZ];
  14. volatile ADC_Status_t ADC_Status;
  15. volatile ADCDataTypeDef_t TIP = {
  16. adc_buffer: &ADC_measures[0].TIP
  17. };
  18. #ifdef USE_VIN
  19. volatile ADCDataTypeDef_t VIN = {
  20. adc_buffer: &ADC_measures[0].VIN
  21. };
  22. #endif
  23. #ifdef USE_NTC
  24. volatile ADCDataTypeDef_t NTC = {
  25. adc_buffer: &ADC_measures[0].NTC
  26. };
  27. #endif
  28. #ifdef USE_VREF
  29. volatile ADCDataTypeDef_t VREF = {
  30. adc_buffer: &ADC_measures[0].VREF
  31. };
  32. #endif
  33. static ADC_HandleTypeDef *adc_device;
  34. uint8_t ADC_Cal(void){
  35. return HAL_ADCEx_Calibration_Start(adc_device);
  36. }
  37. void ADC_Init(ADC_HandleTypeDef *adc){
  38. adc_device=adc;
  39. ADC_ChannelConfTypeDef sConfig = {0};
  40. #ifdef STM32F072xB
  41. adc_device->Instance->CHSELR &= ~(0x7FFFF); // Disable all regular channels
  42. sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  43. #endif
  44. #if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
  45. adc_device->Init.NbrOfConversion = ADC_Num;
  46. #endif
  47. adc_device->Init.ExternalTrigConv = ADC_SOFTWARE_START; // Set software trigger
  48. if (HAL_ADC_Init(adc_device) != HAL_OK) { Error_Handler(); }
  49. sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5; // More sampling time to compensate high input impedances
  50. #ifdef ADC_CH_1ST
  51. #if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
  52. sConfig.Rank = ADC_REGULAR_RANK_1;
  53. #endif
  54. sConfig.Channel = ADC_CH_1ST;
  55. if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
  56. #endif
  57. #ifdef ADC_CH_2ND
  58. #if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
  59. sConfig.Rank = ADC_REGULAR_RANK_2;
  60. #endif
  61. sConfig.Channel = ADC_CH_2ND;
  62. if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
  63. #endif
  64. #ifdef ADC_CH_3RD
  65. #if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
  66. sConfig.Rank = ADC_REGULAR_RANK_3;
  67. #endif
  68. sConfig.Channel = ADC_CH_3RD;
  69. if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
  70. #endif
  71. #ifdef ADC_CH_4TH
  72. #if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
  73. sConfig.Rank = ADC_REGULAR_RANK_4;
  74. #endif
  75. sConfig.Channel = ADC_CH_4TH;
  76. if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
  77. #endif
  78. if(ADC_Cal() != HAL_OK ){
  79. Error_Handler();
  80. }
  81. ADC_Status = ADC_Idle;
  82. buzzer_short_beep();
  83. }
  84. void ADC_Start_DMA(){
  85. if(ADC_Status!=ADC_Waiting){
  86. Error_Handler();
  87. }
  88. if( PWM_GPIO_Port->IDR & PWM_Pin ){ // Check if PWM is active
  89. buzzer_long_beep(); // Generate warning with a beep
  90. }
  91. #ifdef DEBUG_PWM
  92. HAL_GPIO_WritePin(PWM_DBG_GPIO_Port, PWM_DBG_Pin,1); // Toggle TEST
  93. #endif
  94. ADC_Status=ADC_Sampling;
  95. if(HAL_ADC_Start_DMA(adc_device, (uint32_t*)ADC_measures, sizeof(ADC_measures)/ sizeof(uint16_t) )!=HAL_OK){ // Start ADC conversion now
  96. Error_Handler();
  97. }
  98. }
  99. void ADC_Stop_DMA(void){
  100. HAL_ADC_Stop_DMA(adc_device);
  101. }
  102. /*
  103. * Some credits: https://kiritchatterjee.wordpress.com/2014/11/10/a-simple-digital-low-pass-filter-in-c/
  104. */
  105. void DoAverage(volatile ADCDataTypeDef_t* InputData){
  106. volatile uint16_t *inputBuffer=InputData->adc_buffer;
  107. uint32_t adc_sum,avg_data;
  108. uint16_t max=0, min=0xffff;
  109. uint8_t shift;
  110. InputData->prev_avg=InputData->last_avg;
  111. InputData->prev_raw=InputData->last_raw;
  112. // Make the average of the ADC buffer
  113. adc_sum = 0;
  114. for(uint16_t x = 0; x < ADC_BFSIZ; x++) {
  115. adc_sum += *inputBuffer;
  116. if(*inputBuffer > max){
  117. max = *inputBuffer;
  118. }
  119. if(*inputBuffer < min){
  120. min = *inputBuffer;
  121. }
  122. inputBuffer += ADC_Num;
  123. }
  124. //Remove highest and lowest values
  125. adc_sum -= (min + max);
  126. // Calculate average
  127. avg_data = adc_sum / (ADC_BFSIZ -2) ;
  128. InputData->last_raw = avg_data;
  129. if(systemSettings.Profile.filterFactor > 0) { // Advanced filtering enabled?
  130. if(systemSettings.Profile.filterFactor>8){ // Limit coefficient, more than 8 will cause overflow
  131. systemSettings.Profile.filterFactor=8;
  132. }
  133. shift = systemSettings.Profile.filterFactor; // Set EMA factor setting from system settings
  134. // Fixed point shift
  135. uint32_t RawData = avg_data << 12;
  136. // Compute EMA of input
  137. #define LIMIT_FILTERING
  138. #ifdef LIMIT_FILTERING
  139. #define SMOOTH_START 50 // Start difference to apply partial filtering override
  140. #define SMOOTH_END 150 // Max difference to completely override filter
  141. #define SMOOTH_DIFF (SMOOTH_END-SMOOTH_START)
  142. #if defined DEBUG_PWM && SWO_PRINT
  143. extern bool dbg_newData;
  144. #endif
  145. int32_t diff = (int32_t)avg_data - (int32_t)(InputData->EMA_of_Input>>12); // Check difference between stored EMA and last average
  146. int32_t abs_diff=abs(diff);
  147. if(abs_diff>SMOOTH_END){ // If huge (Filtering will delay too much the response)
  148. InputData->EMA_of_Input = RawData; // Reset filter
  149. #if defined DEBUG_PWM && SWO_PRINT
  150. dbg_newData=1; // Enable flag to debug the data
  151. #endif
  152. }
  153. else if(abs_diff>SMOOTH_START){ // If medium, smoothen the difference
  154. InputData->EMA_of_Input += ((diff*(abs_diff-SMOOTH_START))/SMOOTH_DIFF)<<12;
  155. #if defined DEBUG_PWM && SWO_PRINT
  156. //dbg_newData=1; // Meh, just some noise, not important
  157. #endif
  158. }
  159. else{
  160. InputData->EMA_of_Input = ( ((InputData->EMA_of_Input << shift) - InputData->EMA_of_Input) + RawData +(1<<(shift-1)))>>shift;
  161. }
  162. #else
  163. InputData->EMA_of_Input = ( ((InputData->EMA_of_Input << shift) - InputData->EMA_of_Input) + RawData +(1<<(shift-1)))>>shift;
  164. #endif
  165. InputData->last_avg = InputData->EMA_of_Input>>12;
  166. }
  167. else {
  168. InputData->last_avg=avg_data;
  169. }
  170. }
  171. uint16_t ADC_to_mV (uint16_t adc){
  172. /*
  173. * Instead running ( ADC*(3300/4095) ),
  174. * We previously multiply (3300/4095)*2^20 = 845006
  175. * Then we can use the fast hardware multiplier and
  176. * divide just with bit rotation.
  177. *
  178. * So it becomes Vadc = (ADC * 845006) >>20
  179. * Max possible input = 20 bit number, more will cause overflow to the 32 bit variable
  180. * Calculated to use 12 bit max input from ADC (4095)
  181. * Much, much faster than floats!
  182. */
  183. return( ((uint32_t)845006*adc)>>20 );
  184. }
  185. // Don't call this function, only the ADC ISR should use it.
  186. void handle_ADC_Data(void){
  187. DoAverage(&TIP);
  188. #ifdef USE_VREF
  189. DoAverage(&VREF);
  190. #endif
  191. #ifdef USE_NTC
  192. DoAverage(&NTC);
  193. #endif
  194. #ifdef USE_VIN
  195. DoAverage(&VIN);
  196. #endif
  197. }
  198. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* _hadc){
  199. #if defined DEBUG_PWM && SWO_PRINT
  200. extern bool dbg_newData;
  201. extern uint16_t dbg_prev_TIP_Raw, dbg_prev_TIP, dbg_prev_VIN, dbg_prev_PWR;
  202. extern int16_t dbg_prev_NTC;
  203. bool dbg_t=dbg_newData;
  204. #endif
  205. if(_hadc == adc_device){
  206. if(ADC_Status!=ADC_Sampling){
  207. Error_Handler();
  208. }
  209. ADC_Stop_DMA(); // Reset the ADC
  210. ADC_Status = ADC_Idle;
  211. HAL_IWDG_Refresh(&hiwdg); // This is the main reset of the watchdog
  212. // If anything critical stalls (PWM, ADC, hanlleIron) this won't be updated anymore
  213. // causing a system reset
  214. if( PWM_GPIO_Port->IDR & PWM_Pin ){ // If PWM is active
  215. buzzer_long_beep(); // Beep to warn the issue, skip adc
  216. TIP.last_avg = systemSettings.Profile.noIronValue - 1; // Set reading = max temperature to force PID to throttle down
  217. TIP.last_raw = systemSettings.Profile.noIronValue - 1; // But avoiding no iron error
  218. }
  219. else{
  220. handle_ADC_Data(); // Else, update data normally
  221. }
  222. #if defined DEBUG_PWM && defined SWO_PRINT
  223. if(dbg_t!=dbg_newData){ // Save values before handleIron() updates them
  224. dbg_prev_TIP_Raw=last_TIP_Raw; // If filter was resetted, print values
  225. dbg_prev_TIP=last_TIP;
  226. dbg_prev_VIN=last_VIN;
  227. dbg_prev_NTC=last_NTC;
  228. dbg_prev_PWR=Iron.CurrentIronPower;
  229. }
  230. #endif
  231. handleIron(); // Handle iron
  232. runAwayCheck(); // Check runaway condition
  233. #ifdef DEBUG_PWM
  234. HAL_GPIO_WritePin(PWM_DBG_GPIO_Port, PWM_DBG_Pin,0); // Toggle TEST
  235. #endif
  236. }
  237. }