/
main.c
327 lines (271 loc) · 10.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "iron.h"
#include "pid.h"
#include "settings.h"
#include "adc_global.h"
#include "buzzer.h"
#include "rotary_encoder.h"
#include "tempsensors.h"
#include "voltagesensors.h"
#include "display.h"
#include "gui.h"
#include "screen.h"
#include "myTest.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#ifdef __BASE_FILE__
#undef __BASE_FILE__
#define __BASE_FILE__ "main.c"
#endif
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
#if defined DEBUG_PWM && defined SWO_PRINT
volatile uint16_t dbg_prev_TIP_Raw, dbg_prev_TIP, dbg_prev_VIN, dbg_prev_PWR;
volatile int16_t dbg_prev_NTC;
volatile bool dbg_newData;
#endif
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#if defined DEBUG_PWM && defined SWO_PRINT
int _write(int32_t file, uint8_t *ptr, int32_t len)
{
for (int i = 0; i < len; i++)
{
ITM_SendChar(*ptr++);
}
return len;
}
#endif
#ifdef DEBUG_ALLOC
struct mallinfo mi;
uint32_t max_allocated;
#endif
// Allocate max possible ram, then release it. This fills the heap pool and avoids internal fragmentation due (ST's?) poor malloc implementation.
void malloc_fragmentation_fix(void){
uint32_t *ptr = NULL;
uint32_t try=20480; // Current ram usage is ~5KB, so we should have have another 5/15KB free, depending on the stm32 used.
while(!ptr && try){
ptr = _malloc(try);
try-=16;
}
_free(ptr);
#ifdef DEBUG_ALLOC
max_allocated=0; //Clear max allocated result
#endif
}
void Init(void){
#if (defined DISPLAY_SPI || defined DISPLAY_I2C) && defined DISPLAY_DEVICE
lcd_init(&DISPLAY_DEVICE, &FILL_DMA);
#elif defined DISPLAY_SPI || defined DISPLAY_I2C
lcd_init(&FILL_DMA);
#endif
guiInit();
ADC_Init(&ADC_DEVICE);
buzzer_init();
restoreSettings();
ironInit(&READ_TIMER, &PWM_TIMER,PWM_CHANNEL);
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);
oled_init(&RE_Get,&RE1_Data);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
malloc_fragmentation_fix();
#if defined DEBUG && !defined STM32F072xB
DebugOpts(); // Enable debug options in Debug build
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable DWT
DWT->CYCCNT = 0; // Clear counter
DWT->CTRL = DWT_CTRL_CYCCNTENA_Msk; // Enable counter
// Now CPU cycles can be read in DWT->CYCCNT;
#endif
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
configurePWMpin(output_Low);
for(uint32_t t=HAL_GetTick();(HAL_GetTick()-t)<500; ){ // Wait 500ms for voltage to stabilize? (Before calibrating ADC)
HAL_IWDG_Refresh(&hiwdg);
}
Init();
#ifdef RUN_MY_TEST
myTest();
#endif
while (1){
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
#if defined DEBUG_PWM && defined SWO_PRINT
if(dbg_newData){
dbg_newData=0;
printf(" LAST VAL RAW VAL PREV VAL RAW VAL\n"
"TIP: %4u %3u\260C %4u %3u\260C %4u %3u\260C %4u %3u\260C \n"
"PID: %3u%% %3u%%\n\n",
TIP.last_avg, last_TIP, TIP.last_raw, last_TIP_Raw, TIP.prev_avg, dbg_prev_TIP, TIP.prev_raw, dbg_prev_TIP_Raw,
Iron.CurrentIronPower, dbg_prev_PWR);
}
#endif
checkSettings(); // Check if settings were modified
oled_handle(); // Handle oled drawing
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
// Called from SysTick IRQ every 1mS
void Program_Handler(void) {
handle_buzzer(); // Handle buzzer state
RE_Process(&RE1_Data); // Handle Encoder
if(systemSettings.Profile.WakeInputMode!=mode_stand){
readWake();
}
}
/*
*
* Timer working in load preload mode! The value loaded now is loaded on next event. That's why the values are reversed!
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *_htim){
if(_htim == Iron.Read_Timer){
__HAL_TIM_CLEAR_FLAG(Iron.Read_Timer,TIM_FLAG_UPDATE);
if(ADC_Status==ADC_Idle){
__HAL_TIM_SET_AUTORELOAD(Iron.Read_Timer,systemSettings.Profile.readPeriod-(systemSettings.Profile.readDelay+1)); // load (period-delay) time
if(systemSettings.settings.activeDetection && !Iron.Error.safeMode){
configurePWMpin(output_High); // Force PWM high for a few uS (typically 5-10uS)
while(__HAL_TIM_GET_COUNTER(Iron.Read_Timer)<(PWM_DETECT_TIME/5));
}
configurePWMpin(output_Low); // Force PWM low
ADC_Status = ADC_Waiting;
}
else if(ADC_Status==ADC_Waiting){
__HAL_TIM_SET_AUTORELOAD(Iron.Read_Timer,systemSettings.Profile.readDelay); // Load Delay time
ADC_Start_DMA();
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
#ifdef DEBUG_ERROR
#if (defined DISPLAY_I2C || defined DISPLAY_SPI) && defined DISPLAY_DEVICE
if(!oled.use_sw){
display_dma_abort();
}
#endif
setSafeMode(enable);
buzzer_fatal_beep();
Oled_error_init();
char strOut[16];
uint8_t outPos=0;
uint8_t inPos=0;
uint8_t ypos=16;
sprintf(strOut,"Line %u",line);
u8g2_DrawStr(&u8g2, 0, 0, strOut);
while(1){ // Divide string in chuncks that fit teh screen width
strOut[outPos] = file[inPos]; // Copy char
strOut[outPos+1] = 0; // Set out string null terminator
uint8_t currentWidth = u8g2_GetStrWidth(&u8g2, strOut); // Get width
if(currentWidth<displayWidth){ // If less than oled width, increase input string pos
inPos++;
}
if( (currentWidth>displayWidth) || (strOut[outPos]==0) ){ // If width bigger than oled width or current char null(We reached end of input string)
char current = strOut[outPos]; // Store current char
strOut[outPos]=0; // Set current out char to null
u8g2_DrawStr(&u8g2, 0, ypos, strOut); // Draw string
if(current==0){ // If current is null, we reached end
break; // Break
}
outPos=0; // Else, reset output position
ypos += 12; // Increase Y position and continue copying the input string
}
else{
outPos++; // Output buffer not filled yet, increase position
}
};
#if (defined DISPLAY_I2C || defined DISPLAY_SPI) && defined DISPLAY_DEVICE
#ifdef I2C_TRY_HW
if(oled.use_sw){
update_display();
}
else{
update_display_ErrorHandler();
}
#else
update_display_ErrorHandler();
#endif
#else
update_display();
#endif
buttonReset();
#endif
while(1){
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/