blob: 9b2facfa81d5c83fbf8ecd44f474bdfe1fa94e2e [file] [log] [blame]
Harald Welted8a003d2017-02-27 20:31:09 +01001#include "board.h"
2#include "utils.h"
3#include "usb/device/dfu/dfu.h"
4#include "usb/common/dfu/usb_dfu.h"
5#include "manifest.h"
6
7unsigned int g_unique_id[4];
8
9/*----------------------------------------------------------------------------
10 * Callbacks
11 *----------------------------------------------------------------------------*/
12
13#if 0
14void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
15{
16 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
17 simtrace_config = cfgnum;
18}
19#endif
20
21/* returns '1' in case we should break any endless loop */
22static void check_exec_dbg_cmd(void)
23{
24 int ch;
25
26 if (!UART_IsRxReady())
27 return;
28
29 ch = UART_GetChar();
30
31 //board_exec_dbg_cmd(ch);
32}
33
34/*------------------------------------------------------------------------------
35 * Main
36 *------------------------------------------------------------------------------*/
37#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
38extern int main(void)
39{
40 uint8_t isUsbConnected = 0;
41 unsigned int i = 0;
42
43 LED_Configure(LED_NUM_RED);
44 LED_Configure(LED_NUM_GREEN);
45 LED_Set(LED_NUM_RED);
46
47 /* Disable watchdog */
48 WDT_Disable(WDT);
49
50 //req_ctx_init();
51
52 PIO_InitializeInterrupts(0);
53
54 EEFC_ReadUniqueID(g_unique_id);
55
56 printf("\r\n\r\n"
57 "=============================================================================\r\n"
58 "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\r\n"
59 "=============================================================================\r\n",
60 manifest_revision, manifest_board);
61
62 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
63 g_unique_id[0], g_unique_id[1],
64 g_unique_id[2], g_unique_id[3]);
65
66 //board_main_top();
67
68 TRACE_INFO("USB init...\r\n");
69 USBDFU_Initialize(&dfu_descriptors);
70
71 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
72 check_exec_dbg_cmd();
73#if 0
74 if (i >= MAX_USB_ITER * 3) {
75 TRACE_ERROR("Resetting board (USB could "
76 "not be configured)\r\n");
77 NVIC_SystemReset();
78 }
79#endif
80 i++;
81 }
82
83 TRACE_INFO("entering main loop...\r\n");
84 while (1) {
85#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
86 const char rotor[] = { '-', '\\', '|', '/' };
87 putchar('\b');
88 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
89#endif
90 check_exec_dbg_cmd();
91 //osmo_timers_prepare();
92 //osmo_timers_update();
93
94 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
95
96 if (isUsbConnected) {
97 isUsbConnected = 0;
98 }
99 } else if (isUsbConnected == 0) {
100 TRACE_INFO("USB is now configured\r\n");
101 LED_Set(LED_NUM_GREEN);
102 LED_Clear(LED_NUM_RED);
103
104 isUsbConnected = 1;
105 }
106 }
107}