-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
397 lines (317 loc) · 9.41 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "FreeRTOS.h"
#include "task.h"
#include "fsl_debug_console.h"
#include "lvgl_support.h"
#include "pin_mux.h"
#include "board.h"
#include "lvgl.h"
#include "gui_guider.h"
#include "events_init.h"
#include "custom.h"
#include "lvgl_demo_utils.h"
#include "lvgl_freertos.h"
#include "fsl_gpio.h"
#include "fsl_port.h"
#include "fsl_smartdma.h"
#include "fsl_inputmux_connections.h"
#include "fsl_inputmux.h"
#include "fsl_flash.h"
#include "fsl_flash_ffr.h"
#include "app_main.h"
#include "app_lvgl.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define I2C_RELEASE_SDA_PORT PORT4
#define I2C_RELEASE_SCL_PORT PORT4
#define I2C_RELEASE_SDA_GPIO GPIO4
#define I2C_RELEASE_SDA_PIN 0U
#define I2C_RELEASE_SCL_GPIO GPIO4
#define I2C_RELEASE_SCL_PIN 1U
#define I2C_RELEASE_BUS_COUNT 100U
/*******************************************************************************
* Variables
******************************************************************************/
static volatile bool s_lvgl_initialized = false;
lv_ui guider_ui;
AppLogic_t g_app;
static flash_config_t s_flashDriver;
#ifdef __ARMCC_VERSION
extern uint8_t Image$$ER_m_svm$$Base[];
#define m_svm_start (uint32_t)Image$$ER_m_svm$$Base
#elif defined(__GNUC__)
extern uint8_t __base_m_svm_model[];
#define m_svm_start __base_m_svm_model
#endif
static SemaphoreHandle_t s_lvgl_semphr;
/*******************************************************************************
* Prototypes
******************************************************************************/
#if LV_USE_LOG
static void print_cb(const char *buf)
{
PRINTF("\r%s\n", buf);
}
#endif
static uint32_t get_idle_time_cb(void)
{
return (getIdleTaskTime() / 1000);
}
static void reset_idle_time_cb(void)
{
resetIdleTaskTime();
}
bool app_lvgl_lock_acquire(uint32_t block_msec) {
if (xSemaphoreTake(s_lvgl_semphr, pdMS_TO_TICKS(block_msec)) != pdPASS) {
return false;
}
return true;
}
void app_lvgl_lock_release(void) {
xSemaphoreGive(s_lvgl_semphr);
}
uint32_t app_lvgl_ms_ticks(void) {
return xTaskGetTickCount();
}
static void AppTask(void *param)
{
#if LV_USE_LOG
lv_log_register_print_cb(print_cb);
#endif
TickType_t tick_start;
TickType_t tick_end;
TickType_t delay_target;
lv_timer_register_get_idle_cb(get_idle_time_cb);
lv_timer_register_reset_idle_cb(reset_idle_time_cb);
lv_port_pre_init();
lv_init();
lv_port_disp_init();
lv_port_indev_init();
s_lvgl_initialized = true;
setup_ui(&guider_ui);
events_init(&guider_ui);
custom_init(&guider_ui);
for (;;)
{
tick_start = xTaskGetTickCount();
//if (app_lvgl_lock_acquire(10000))
{
lv_timer_handler();
app_lvgl_lock_release();
}
tick_end = xTaskGetTickCount();
if (tick_end - tick_start >= pdMS_TO_TICKS(LV_DISP_DEF_REFR_PERIOD)) {
/* The task takes too long to finish, use minimum delay target */
delay_target = 1;
} else {
delay_target = pdMS_TO_TICKS(LV_DISP_DEF_REFR_PERIOD) - (tick_end - tick_start);
}
vTaskDelay(delay_target);
}
}
/*******************************************************************************
* Code
******************************************************************************/
void BOARD_I2C_ReleaseBus(void);
__WEAK void* get_ad_model(void) {
return svm_model_valid((void*)m_svm_start) ? (void*)m_svm_start : NULL;
}
uint32_t get_flash_address(){
return m_svm_start;
}
uint32_t save_model_to_flash(void* model, uint32_t model_len){
taskENTER_CRITICAL();
// total 8K sector erase, the model must be little than this
FLASH_Erase(&s_flashDriver, m_svm_start, 0x2000, kFLASH_ApiEraseKey);
for(int i = 0; i < model_len; i += 256){
FLASH_Program(&s_flashDriver, m_svm_start + i, (uint8_t*)model + i, 256);
}
taskEXIT_CRITICAL();
return m_svm_start;
}
static void BOARD_InitSmartDMA(void)
{
RESET_ClearPeripheralReset(kMUX_RST_SHIFT_RSTn);
INPUTMUX_Init(INPUTMUX0);
INPUTMUX_AttachSignal(INPUTMUX0, 0, kINPUTMUX_FlexioToSmartDma);
/* Turnoff clock to inputmux to save power. Clock is only needed to make changes */
INPUTMUX_Deinit(INPUTMUX0);
SMARTDMA_InitWithoutFirmware();
NVIC_EnableIRQ(SMARTDMA_IRQn);
NVIC_SetPriority(SMARTDMA_IRQn, 3);
}
static void i2c_release_bus_delay(void)
{
SDK_DelayAtLeastUs(100U, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
}
void BOARD_I2C_ReleaseBus(void)
{
uint8_t i = 0;
gpio_pin_config_t pin_config;
port_pin_config_t i2c_pin_config = {0};
/* Config pin mux as gpio */
i2c_pin_config.pullSelect = kPORT_PullUp;
i2c_pin_config.mux = kPORT_MuxAsGpio;
pin_config.pinDirection = kGPIO_DigitalOutput;
pin_config.outputLogic = 1U;
CLOCK_EnableClock(kCLOCK_Port4);
PORT_SetPinConfig(I2C_RELEASE_SCL_PORT, I2C_RELEASE_SCL_PIN, &i2c_pin_config);
PORT_SetPinConfig(I2C_RELEASE_SCL_PORT, I2C_RELEASE_SDA_PIN, &i2c_pin_config);
GPIO_PinInit(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, &pin_config);
GPIO_PinInit(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, &pin_config);
/* Drive SDA low first to simulate a start */
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
i2c_release_bus_delay();
/* Send 9 pulses on SCL and keep SDA high */
for (i = 0; i < 9; i++)
{
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
i2c_release_bus_delay();
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
i2c_release_bus_delay();
i2c_release_bus_delay();
}
/* Send stop */
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
i2c_release_bus_delay();
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
i2c_release_bus_delay();
CLOCK_EnableClock(kCLOCK_Port3);
PORT_SetPinConfig(PORT3, 2, &i2c_pin_config);
PORT_SetPinConfig(PORT3, 3, &i2c_pin_config);
GPIO_PinInit(GPIO3, 3, &pin_config);
GPIO_PinInit(GPIO3, 2, &pin_config);
/* Drive SDA low first to simulate a start */
GPIO_PinWrite(GPIO3, 2, 0U);
i2c_release_bus_delay();
/* Send 9 pulses on SCL and keep SDA high */
for (i = 0; i < 9; i++)
{
GPIO_PinWrite(GPIO3, 3, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(GPIO3, 2, 1U);
i2c_release_bus_delay();
GPIO_PinWrite(GPIO3, 3, 1U);
i2c_release_bus_delay();
i2c_release_bus_delay();
}
/* Send stop */
GPIO_PinWrite(GPIO3, 3, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(GPIO3, 2, 0U);
i2c_release_bus_delay();
GPIO_PinWrite(GPIO3, 3, 1U);
i2c_release_bus_delay();
GPIO_PinWrite(GPIO3, 2, 1U);
i2c_release_bus_delay();
}
/*!
* @brief Main function
*/
int main(void)
{
BaseType_t stat;
/* Init board hardware. */
/* attach FRO 12M to FLEXCOMM4 (debug console) */
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
/* attach FRO 12M to FLEXCOMM2 */
CLOCK_SetClkDiv(kCLOCK_DivFlexcom2Clk, 1u);
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM2);
CLOCK_SetClkDiv(kCLOCK_DivFlexioClk, 1u);
CLOCK_AttachClk(kPLL0_to_FLEXIO);
BOARD_InitBootClocks();
BOARD_I2C_ReleaseBus();
BOARD_InitBootPins();
BOARD_InitDebugConsole();
DEMO_InitUsTimer();
// Init the LED for show
CLOCK_EnableClock(kCLOCK_Gpio3);
INIT_LED_PINS(RED, LOGIC_LED_OFF);
INIT_LED_PINS(GREEN, LOGIC_LED_OFF);
LED_RED_INIT(0);
LED_GREEN_INIT(0);
LED_GREEN_ON();
FLASH_Init(&s_flashDriver);
FFR_Init(&s_flashDriver);
PRINTF("Anomaly detection demo.\r\n");
/* Init smartdma. */
BOARD_InitSmartDMA();
s_lvgl_semphr = xSemaphoreCreateBinary();
if (s_lvgl_semphr == NULL) {
return -2;
}
xSemaphoreGive(s_lvgl_semphr);
g_app.pADModel = get_ad_model();
g_app.qh_NewSample = xQueueCreate(1, APP_FEATURE_SIZE);
g_app.health = APP_HEALTH_ABS_MAX * 0.5f;
if (g_app.pADModel != NULL)
{
svm_get_model_param_gamma_nu(g_app.pADModel, &g_app.gamma, &g_app.nu);
}
else
{
g_app.gamma = DEFAULT_GAMMA;
g_app.nu = DEFAULT_NU;
}
stat = xTaskCreate(AppTask, "lvgl", 1024, NULL, 3, NULL);
if (pdPASS != stat)
{
PRINTF("Failed to create lvgl task");
while (1)
;
}
if (xTaskCreate(app_sensor_task, "SENS", 4096, NULL, configMAX_PRIORITIES-1, NULL) != pdPASS) {
PRINTF("Failed to create sensor task.\r\n");
while (1) {}
}
if (xTaskCreate(app_algo_task, "ALGO", 0x1000, NULL, configMAX_PRIORITIES-1, NULL) != pdPASS) {
PRINTF("Failed to create sensor task.\r\n");
while (1) {}
}
vTaskStartScheduler();
for (;;)
{
} /* should never get here */
}
/*!
* @brief Malloc failed hook.
*/
void vApplicationMallocFailedHook(void)
{
PRINTF("Malloc failed. Increase the heap size.");
for (;;)
;
}
/*!
* @brief FreeRTOS tick hook.
*/
void vApplicationTickHook(void)
{
if (s_lvgl_initialized)
{
//lv_tick_inc(1);
}
}
/*!
* @brief Stack overflow hook.
*/
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
(void)pcTaskName;
(void)xTask;
for (;;)
;
}