forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathmain.c
More file actions
189 lines (151 loc) · 5.14 KB
/
Copy pathmain.c
File metadata and controls
189 lines (151 loc) · 5.14 KB
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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022-2025 Infineon Technologies AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// std includes
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
// MTB includes
#include "cybsp.h"
// micropython includes
#include "py/builtin.h"
#include "py/compile.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/stackctrl.h"
#include "py/runtime.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
#include "shared/readline/readline.h"
// port-specific includes
#include "mphalport.h"
typedef enum {
BOOT_MODE_NORMAL,
BOOT_MODE_SAFE
} boot_mode_t;
#if MICROPY_ENABLE_GC
extern uint8_t __StackTop, __StackSize;
extern uint8_t __HeapBase, __HeapLimit;
#endif
extern void machine_rtc_init_all(void);
extern void machine_pin_irq_deinit_all(void);
extern void machine_uart_deinit_all(void);
extern void mp_hal_ticks_init(void);
boot_mode_t check_boot_mode(void) {
boot_mode_t boot_mode;
// Initialize user LED
Cy_GPIO_Pin_FastInit(CYBSP_USER_LED_PORT, CYBSP_USER_LED_NUM, CY_GPIO_DM_STRONG, 0, HSIOM_SEL_GPIO);
// Initialize user button
Cy_GPIO_Pin_FastInit(CYBSP_USER_BTN1_PORT, CYBSP_USER_BTN1_NUM, CY_GPIO_DM_PULLUP, 1, HSIOM_SEL_GPIO);
// Added 5ms delay to allow bypass capacitor connected to the user button without external pull-up to charge.
Cy_SysLib_Delay(5);
if (Cy_GPIO_Read(CYBSP_USER_BTN1_PORT, CYBSP_USER_BTN1_NUM) == CYBSP_BTN_PRESSED) {
// Blink LED twice to indicate safe boot mode was entered
for (int i = 0; i < 4; i++) {
Cy_GPIO_Inv(CYBSP_USER_LED_PORT, CYBSP_USER_LED_NUM);
Cy_SysLib_Delay(500); // delay in milliseconds
}
boot_mode = BOOT_MODE_SAFE;
mp_printf(&mp_plat_print, "- DEVICE IS IN SAFE BOOT MODE -\n");
} else {
boot_mode = BOOT_MODE_NORMAL;
}
// Turn off LED after boot mode check
Cy_GPIO_Clr(CYBSP_USER_LED_PORT, CYBSP_USER_LED_NUM);
return boot_mode;
}
int main(void) {
cy_rslt_t result = CY_RSLT_SUCCESS;
/* Initialize the device and board peripherals. */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS) {
mp_raise_ValueError(MP_ERROR_TEXT("cybsp_init failed !\n"));
}
/* Enable global interrupts */
__enable_irq();
/* Initialize stdio interface */
mp_hal_stdio_init();
// Initialise the MicroPython runtime.
#if MICROPY_ENABLE_GC
gc_init(&__HeapBase, &__HeapLimit);
mp_cstack_init_with_top((void *)&__StackTop, (size_t)&__StackSize);
#endif
mp_hal_ticks_init();
soft_reset:
machine_rtc_init_all();
mp_init();
readline_init0();
#if MICROPY_VFS
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
#if MICROPY_VFS_LFS2
pyexec_frozen_module("_boot.py", false);
#endif
#endif
if (check_boot_mode() == BOOT_MODE_NORMAL) {
// Execute user scripts.
int ret = pyexec_file_if_exists("/boot.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset;
}
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
ret = pyexec_file_if_exists("/main.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset;
}
}
}
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
break;
}
} else {
if (pyexec_friendly_repl() != 0) {
break;
}
}
}
mp_printf(&mp_plat_print, "MPY: soft reboot\n");
machine_pin_irq_deinit_all();
machine_uart_deinit_all();
#if MICROPY_ENABLE_GC
gc_sweep_all();
#endif
mp_deinit();
goto soft_reset;
}
#if MICROPY_ENABLE_GC
void gc_collect(void) {
gc_collect_start();
gc_helper_collect_regs_and_stack();
gc_collect_end();
}
#endif
// Handle uncaught exceptions (should never be reached in a correct C implementation).
void nlr_jump_fail(void *val) {
for (;;) {
}
}