2626
2727#include <stdint.h>
2828#include <stdbool.h>
29-
3029#include <std.h>
30+
31+ #include "py/mpconfig.h"
32+ #include MICROPY_HAL_H
3133#include "hw_ints.h"
3234#include "hw_types.h"
3335#include "hw_gpio.h"
5052#include "cc3200_hal.h"
5153#include "debug.h"
5254#include "pybwdt.h"
55+ #include "mperror.h"
5356
5457
5558//*****************************************************************************
6063#define BOOTMGR_HASH_SIZE 32
6164#define BOOTMGR_BUFF_SIZE 512
6265
63- #define BOOTMGR_WAIT_SAFE_MODE_MS 2000
64- #define BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS 250
65-
66- #define BOOTMGR_SAFE_MODE_ENTER_MS 1000
67- #define BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS 100
68-
69- #define BOOTMGR_PINS_PRCM PRCM_GPIOA3
70- #define BOOTMGR_PINS_PORT GPIOA3_BASE
71- #define BOOTMGR_LED_PIN_NUM PIN_21
72- #define BOOTMGR_SFE_PIN_NUM PIN_18
73- #define BOOTMGR_LED_PORT_PIN GPIO_PIN_1 // GPIO25
74- #define BOOTMGR_SFE_PORT_PIN GPIO_PIN_4 // GPIO28
66+ #define BOOTMGR_WAIT_SAFE_MODE_MS 1600
67+ #define BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS 200
7568
69+ #define BOOTMGR_SAFE_MODE_ENTER_MS 700
70+ #define BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS 70
7671
7772//*****************************************************************************
7873// Exported functions declarations
@@ -159,18 +154,12 @@ static void bootmgr_board_init(void) {
159154 // Enable the Data Hashing Engine
160155 HASH_Init ();
161156
162- // Enable GPIOA3 Peripheral Clock
163- MAP_PRCMPeripheralClkEnable (BOOTMGR_PINS_PRCM , PRCM_RUN_MODE_CLK );
164-
165- // Configure the bld
166- MAP_PinTypeGPIO (BOOTMGR_LED_PIN_NUM , PIN_MODE_0 , false);
167- MAP_PinConfigSet (BOOTMGR_LED_PIN_NUM , PIN_STRENGTH_6MA , PIN_TYPE_STD );
168- MAP_GPIODirModeSet (BOOTMGR_PINS_PORT , BOOTMGR_LED_PORT_PIN , GPIO_DIR_MODE_OUT );
157+ // Init the system led and the system switch
158+ mperror_init0 ();
169159
170- // Configure the safe mode pin
171- MAP_PinTypeGPIO (BOOTMGR_SFE_PIN_NUM , PIN_MODE_0 , false);
172- MAP_PinConfigSet (BOOTMGR_SFE_PIN_NUM , PIN_STRENGTH_4MA , PIN_TYPE_STD_PU );
173- MAP_GPIODirModeSet (BOOTMGR_PINS_PORT , BOOTMGR_SFE_PORT_PIN , GPIO_DIR_MODE_IN );
160+ // clear the safe boot request, since we should not trust
161+ // the register's state after reset
162+ mperror_clear_safe_boot ();
174163}
175164
176165//*****************************************************************************
@@ -252,13 +241,14 @@ static void bootmgr_load_and_execute (_u8 *image) {
252241//*****************************************************************************
253242static bool safe_mode_boot (void ) {
254243 _u32 count = 0 ;
255- while (! MAP_GPIOPinRead (BOOTMGR_PINS_PORT , BOOTMGR_SFE_PORT_PIN ) &&
256- ((BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS * count ++ ) < BOOTMGR_WAIT_SAFE_MODE_MS )) {
244+ while (MAP_GPIOPinRead (MICROPY_SAFE_BOOT_PORT , MICROPY_SAFE_BOOT_PORT_PIN ) &&
245+ ((BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS * count ++ ) < BOOTMGR_WAIT_SAFE_MODE_MS )) {
257246 // toogle the led
258- MAP_GPIOPinWrite (BOOTMGR_PINS_PORT , BOOTMGR_LED_PORT_PIN , ~MAP_GPIOPinRead (GPIOA3_BASE , BOOTMGR_LED_PORT_PIN ));
247+ MAP_GPIOPinWrite (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN , ~MAP_GPIOPinRead (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN ));
259248 UtilsDelay (UTILS_DELAY_US_TO_COUNT (BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS * 1000 ));
260249 }
261- return MAP_GPIOPinRead (BOOTMGR_PINS_PORT , BOOTMGR_SFE_PORT_PIN ) ? false : true;
250+ mperror_deinit_sfe_pin ();
251+ return MAP_GPIOPinRead (MICROPY_SAFE_BOOT_PORT , MICROPY_SAFE_BOOT_PORT_PIN ) ? true : false;
262252}
263253
264254//*****************************************************************************
@@ -268,14 +258,16 @@ static void bootmgr_image_loader(sBootInfo_t *psBootInfo) {
268258 _i32 fhandle ;
269259 if (safe_mode_boot ()) {
270260 _u32 count = 0 ;
271- while ((BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * count ++ ) > BOOTMGR_SAFE_MODE_ENTER_MS ) {
261+ while ((BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * count ++ ) < BOOTMGR_SAFE_MODE_ENTER_MS ) {
272262 // toogle the led
273- MAP_GPIOPinWrite (BOOTMGR_PINS_PORT , BOOTMGR_LED_PORT_PIN , ~MAP_GPIOPinRead (GPIOA3_BASE , BOOTMGR_LED_PORT_PIN ));
263+ MAP_GPIOPinWrite (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN , ~MAP_GPIOPinRead (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN ));
274264 UtilsDelay (UTILS_DELAY_US_TO_COUNT (BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * 1000 ));
275265 }
276266 psBootInfo -> ActiveImg = IMG_ACT_FACTORY ;
277267 // turn the led off
278- MAP_GPIOPinWrite (BOOTMGR_PINS_PORT , BOOTMGR_LED_PORT_PIN , 0 );
268+ MAP_GPIOPinWrite (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN , 0 );
269+ // request a safe boot to the application
270+ mperror_request_safe_boot ();
279271 }
280272 // do we have a new update image that needs to be verified?
281273 else if ((psBootInfo -> ActiveImg == IMG_ACT_UPDATE ) && (psBootInfo -> Status == IMG_STATUS_CHECK )) {
@@ -350,7 +342,7 @@ int main (void) {
350342 // could not be loaded, so, loop forever and signal the crash to the user
351343 while (true) {
352344 // keep the bld on
353- MAP_GPIOPinWrite (BOOTMGR_PINS_PORT , BOOTMGR_LED_PORT_PIN , BOOTMGR_LED_PORT_PIN );
345+ MAP_GPIOPinWrite (MICROPY_SYS_LED_PORT , MICROPY_SYS_LED_PORT_PIN , MICROPY_SYS_LED_PORT_PIN );
354346 __asm volatile (" dsb \n"
355347 " isb \n"
356348 " wfi \n" );
0 commit comments