main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "iron.h"
  25. #include "pid.h"
  26. #include "settings.h"
  27. #include "adc_global.h"
  28. #include "buzzer.h"
  29. #include "rotary_encoder.h"
  30. #include "tempsensors.h"
  31. #include "voltagesensors.h"
  32. #include "ssd1306.h"
  33. #include "gui.h"
  34. #include "screen.h"
  35. #include "myTest.h"
  36. /* USER CODE END Includes */
  37. /* Private typedef -----------------------------------------------------------*/
  38. /* USER CODE BEGIN PTD */
  39. /* USER CODE END PTD */
  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */
  43. /* Private macro -------------------------------------------------------------*/
  44. /* USER CODE BEGIN PM */
  45. /* USER CODE END PM */
  46. /* Private variables ---------------------------------------------------------*/
  47. /* USER CODE BEGIN PV */
  48. #if defined DEBUG_PWM && defined SWO_PRINT
  49. volatile uint16_t dbg_prev_TIP_Raw, dbg_prev_TIP, dbg_prev_VIN, dbg_prev_PWR;
  50. volatile int16_t dbg_prev_NTC;
  51. volatile bool dbg_newData;
  52. #endif
  53. /* USER CODE END PV */
  54. /* Private function prototypes -----------------------------------------------*/
  55. /* USER CODE BEGIN PFP */
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. #if defined DEBUG_PWM && defined SWO_PRINT
  60. int _write(int32_t file, uint8_t *ptr, int32_t len)
  61. {
  62. for (int i = 0; i < len; i++)
  63. {
  64. ITM_SendChar(*ptr++);
  65. }
  66. return len;
  67. }
  68. #endif
  69. void Init(void){
  70. #if (defined OLED_SPI || defined OLED_I2C) && defined OLED_DEVICE
  71. ssd1306_init(&OLED_DEVICE, &FILL_DMA);
  72. #elif defined OLED_SPI || defined OLED_I2C
  73. ssd1306_init(&FILL_DMA);
  74. #endif
  75. guiInit();
  76. ADC_Init(&ADC_DEVICE);
  77. buzzer_init();
  78. restoreSettings();
  79. ironInit(&DELAY_TIMER, &PWM_TIMER,PWM_CHANNEL);
  80. RE_Init((RE_State_t *)&RE1_Data, ENC_L_GPIO_Port, ENC_L_Pin, ENC_R_GPIO_Port, ENC_R_Pin, ENC_SW_GPIO_Port, ENC_SW_Pin);
  81. oled_init(&RE_Get,&RE1_Data);
  82. }
  83. /* USER CODE END 0 */
  84. /**
  85. * @brief The application entry point.
  86. * @retval int
  87. */
  88. int main(void)
  89. {
  90. /* USER CODE BEGIN 1 */
  91. #ifdef DEBUG
  92. DebugOpts(); // Enable debug options in Debug build
  93. DWT->CTRL |= 1 ; // enable the counter
  94. DWT->CYCCNT = 0; // reset the counter
  95. // Now CPU cycles can be read in DWT->CYCCNT;
  96. #endif
  97. /* USER CODE END 1 */
  98. /* MCU Configuration--------------------------------------------------------*/
  99. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  100. /* USER CODE BEGIN Init */
  101. /* USER CODE END Init */
  102. /* Configure the system clock */
  103. /* USER CODE BEGIN SysInit */
  104. /* USER CODE END SysInit */
  105. /* Initialize all configured peripherals */
  106. /* USER CODE BEGIN 2 */
  107. /* USER CODE END 2 */
  108. /* Infinite loop */
  109. /* USER CODE BEGIN WHILE */
  110. for(uint32_t t=HAL_GetTick();(HAL_GetTick()-t)<100; ){ // Wait 100mS for voltage to stabilize? (Before calibrating ADC)
  111. HAL_IWDG_Refresh(&hiwdg);
  112. }
  113. Init();
  114. #ifdef RUN_MY_TEST
  115. myTest();
  116. #endif
  117. while (1){
  118. /* USER CODE END WHILE */
  119. /* USER CODE BEGIN 3 */
  120. #if defined DEBUG_PWM && defined SWO_PRINT
  121. if(dbg_newData){
  122. dbg_newData=0;
  123. printf(" LAST VAL RAW VAL PREV VAL RAW VAL\n"
  124. "TIP: %4u %3u\260C %4u %3u\260C %4u %3u\260C %4u %3u\260C \n"
  125. "PID: %3u%% %3u%%\n\n",
  126. TIP.last_avg, last_TIP, TIP.last_raw, last_TIP_Raw, TIP.prev_avg, dbg_prev_TIP, TIP.prev_raw, dbg_prev_TIP_Raw,
  127. Iron.CurrentIronPower, dbg_prev_PWR);
  128. }
  129. #endif
  130. checkSettings(); // Check if settings were modified
  131. oled_handle(); // Handle oled drawing
  132. }
  133. /* USER CODE END 3 */
  134. }
  135. /* USER CODE BEGIN 4 */
  136. // Called from SysTick IRQ every 1mS
  137. void Program_Handler(void) {
  138. static bool last_wake=0;
  139. bool now_wake = WAKE_input();
  140. if(last_wake!=now_wake){ // If wake sensor input changed
  141. last_wake=now_wake;
  142. if(systemSettings.settings.WakeInputMode==wakeInputmode_stand){ // In stand mode
  143. if(now_wake){
  144. setModefromStand(mode_run);
  145. }
  146. else{
  147. setModefromStand(systemSettings.settings.StandMode); // Set sleep or standby mode depending on system setting
  148. }
  149. }
  150. else{
  151. IronWake(source_wakeInput);
  152. }
  153. }
  154. handle_buzzer(); // Handle buzzer state
  155. RE_Process(&RE1_Data); // Handle Encoder
  156. }
  157. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *_htim){
  158. if(_htim == Iron.Pwm_Timer){
  159. #ifdef DEBUG_PWM
  160. //HAL_GPIO_WritePin(PWM_DBG_GPIO_Port, PWM_DBG_Pin,0); // Toggle TEST// PWM output low
  161. #endif
  162. __HAL_TIM_CLEAR_FLAG(Iron.Pwm_Timer,TIM_FLAG_CC1|TIM_FLAG_CC2|TIM_FLAG_CC3|TIM_FLAG_CC4); // Clear compare flags
  163. if(ADC_Status==ADC_Idle){ // ADC idle
  164. ADC_Status = ADC_Waiting; // Update status to waiting
  165. __HAL_TIM_ENABLE(Iron.Delay_Timer); // Enable Delay Timer and start counting (One-pulse mode)
  166. }
  167. else if(ADC_Status==ADC_Sampling){ // ADC busy?
  168. ADC_Stop_DMA(); // Stop ADC. Skip conversion.
  169. ADC_Status = ADC_Idle; // Set the ADC status
  170. }
  171. else if(ADC_Status==ADC_Waiting){ // ADC waiting(Delay timer running)
  172. if(Iron.Delay_Timer->Instance->CR1 & TIM_CR1_CEN){ // Delay timer running?
  173. __disable_irq();
  174. __HAL_TIM_SET_COUNTER(Iron.Delay_Timer,0); // Clear counter
  175. __HAL_TIM_CLEAR_FLAG(Iron.Delay_Timer,TIM_FLAG_UPDATE); // Clear flag
  176. __enable_irq();
  177. }
  178. __HAL_TIM_ENABLE(Iron.Delay_Timer); // Enable
  179. }
  180. }
  181. }
  182. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *_htim){
  183. #ifdef DEBUG_PWM
  184. if(_htim == Iron.Pwm_Timer){ // Delay Timer?
  185. //HAL_GPIO_WritePin(PWM_DBG_GPIO_Port, PWM_DBG_Pin,1); // Toggle TEST
  186. }
  187. #endif
  188. if(_htim == Iron.Delay_Timer){ // Delay Timer?
  189. __HAL_TIM_CLEAR_FLAG(Iron.Delay_Timer,TIM_FLAG_UPDATE); // Clear Delay Timer flag
  190. if(ADC_Status==ADC_Waiting){
  191. ADC_Start_DMA(); // Start ADC conversion
  192. }
  193. }
  194. }
  195. /* USER CODE END 4 */
  196. /**
  197. * @brief This function is executed in case of error occurrence.
  198. * @retval None
  199. */
  200. void Error_Handler(void)
  201. {
  202. /* USER CODE BEGIN Error_Handler_Debug */
  203. /* User can add his own implementation to report the HAL error return state */
  204. #ifdef DEBUG_ERROR
  205. #if (defined OLED_I2C || defined OLED_SPI) && defined OLED_DEVICE
  206. if(!oled.use_sw){
  207. display_abort();
  208. }
  209. #endif
  210. SetFailState(setError);
  211. buzzer_fatal_beep();
  212. Diag_init();
  213. char strOut[16];
  214. uint8_t outPos=0;
  215. uint8_t inPos=0;
  216. uint8_t ypos=12;
  217. sprintf(strOut,"ERR!! LINE:%u",line);
  218. u8g2_DrawStr(&u8g2, 0, 0, strOut);
  219. while(1){ // Divide string in chuncks that fit teh screen width
  220. strOut[outPos] = file[inPos]; // Copy char
  221. strOut[outPos+1] = 0; // Set out string null terminator
  222. uint8_t currentWidth = u8g2_GetStrWidth(&u8g2, strOut); // Get width
  223. if(currentWidth<OledWidth){ // If less than oled width, increase input string pos
  224. inPos++;
  225. }
  226. if( (currentWidth>OledWidth) || (strOut[outPos]==0) ){ // If width bigger than oled width or current char null(We reached end of input string)
  227. char current = strOut[outPos]; // Store current char
  228. strOut[outPos]=0; // Set current out char to null
  229. u8g2_DrawStr(&u8g2, 0, ypos, strOut); // Draw string
  230. if(current==0){ // If current is null, we reached end
  231. break; // Break
  232. }
  233. outPos=0; // Else, reset output position
  234. ypos += 12; // Increase Y position and continue copying the input string
  235. }
  236. else{
  237. outPos++; // Output buffer not filled yet, increase position
  238. }
  239. };
  240. #if (defined OLED_I2C || defined OLED_SPI) && defined OLED_DEVICE
  241. #ifdef I2C_TRY_HW
  242. if(oled.use_sw){
  243. update_display();
  244. }
  245. else{
  246. update_display_ErrorHandler();
  247. }
  248. #else
  249. update_display_ErrorHandler();
  250. #endif
  251. #else
  252. update_display();
  253. #endif
  254. Reset_onError();
  255. #endif
  256. while(1){
  257. }
  258. /* USER CODE END Error_Handler_Debug */
  259. }
  260. #ifdef USE_FULL_ASSERT
  261. /**
  262. * @brief Reports the name of the source file and the source line number
  263. * where the assert_param error has occurred.
  264. * @param file: pointer to the source file name
  265. * @param line: assert_param error line source number
  266. * @retval None
  267. */
  268. void assert_failed(uint8_t *file, uint32_t line)
  269. {
  270. /* USER CODE BEGIN 6 */
  271. /* User can add his own implementation to report the file name and line number,
  272. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  273. /* USER CODE END 6 */
  274. }
  275. #endif /* USE_FULL_ASSERT */
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/