| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2021-2022 Damien P. George |
| 7 |
* Copyright (c) 2022 David Lechner <[email protected]> |
| 8 |
* |
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
* of this software and associated documentation files (the "Software"), to deal |
| 11 |
* in the Software without restriction, including without limitation the rights |
| 12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
* copies of the Software, and to permit persons to whom the Software is |
| 14 |
* furnished to do so, subject to the following conditions: |
| 15 |
* |
| 16 |
* The above copyright notice and this permission notice shall be included in |
| 17 |
* all copies or substantial portions of the Software. |
| 18 |
* |
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
* THE SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "py/mphal.h" |
| 29 |
#include "irq.h" |
| 30 |
|
| 31 |
void board_init(void) { |
| 32 |
if (query_irq() == IRQ_STATE_DISABLED) { |
| 33 |
enable_irq(IRQ_STATE_ENABLED); |
| 34 |
} |
| 35 |
|
| 36 |
// Enable 3V3 for all ports |
| 37 |
mp_hal_pin_output(pyb_pin_PORT_3V3_EN); |
| 38 |
mp_hal_pin_high(pyb_pin_PORT_3V3_EN); |
| 39 |
|
| 40 |
// Port A |
| 41 |
// Enable RX/TX buffer |
| 42 |
mp_hal_pin_output(pyb_pin_PORTA_EN); |
| 43 |
mp_hal_pin_low(pyb_pin_PORTA_EN); |
| 44 |
|
| 45 |
// Port B |
| 46 |
// Enable RX/TX buffer |
| 47 |
mp_hal_pin_output(pyb_pin_PORTB_EN); |
| 48 |
mp_hal_pin_low(pyb_pin_PORTB_EN); |
| 49 |
} |
| 50 |
|
| 51 |
#if BUILDING_MBOOT |
| 52 |
|
| 53 |
#include "drivers/memory/spiflash.h" |
| 54 |
#include "mboot/mboot.h" |
| 55 |
#include "boardctrl.h" |
| 56 |
#include "adc.h" |
| 57 |
#include "hub_display.h" |
| 58 |
|
| 59 |
#define RESET_MODE_NUM_STATES (4) |
| 60 |
#define RESET_MODE_TIMEOUT_CYCLES (8) |
| 61 |
|
| 62 |
// Location and value for the SPI flash update key. If this key exists at the defined |
| 63 |
// location then mboot will attempt to do a filesystem-load update of the main firmware. |
| 64 |
// This makes the update robust to power failures: if the update does not complete then |
| 65 |
// it will be restarted the next time it powers up. Only when it fully completes will |
| 66 |
// this key be erased, and then the application can run. |
| 67 |
#define SPIFLASH_UPDATE_KEY_ADDR (1020 * 1024) |
| 68 |
#define SPIFLASH_UPDATE_KEY_VALUE (0x12345678) |
| 69 |
|
| 70 |
static void board_led_pattern(int reset_mode, uint16_t brightness) { |
| 71 |
switch (reset_mode) { |
| 72 |
case BOARDCTRL_RESET_MODE_NORMAL: |
| 73 |
// set status light to red |
| 74 |
hub_display_set(3, brightness); |
| 75 |
hub_display_set(4, 0); |
| 76 |
hub_display_set(5, 0); |
| 77 |
break; |
| 78 |
case BOARDCTRL_RESET_MODE_SAFE_MODE: |
| 79 |
// set status light to green |
| 80 |
hub_display_set(3, 0); |
| 81 |
hub_display_set(4, brightness); |
| 82 |
hub_display_set(5, 0); |
| 83 |
break; |
| 84 |
case BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM: |
| 85 |
// set status light to blue |
| 86 |
hub_display_set(3, 0); |
| 87 |
hub_display_set(4, 0); |
| 88 |
hub_display_set(5, brightness); |
| 89 |
break; |
| 90 |
case BOARDCTRL_RESET_MODE_BOOTLOADER: |
| 91 |
// set status light to white |
| 92 |
hub_display_set(3, brightness); |
| 93 |
hub_display_set(4, brightness); |
| 94 |
hub_display_set(5, brightness); |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
hub_display_update(); |
| 99 |
} |
| 100 |
|
| 101 |
static void board_battery_init(void) { |
| 102 |
mp_hal_pin_config(pyb_pin_BAT_VMON_ADC, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0); |
| 103 |
adc_config(ADC1, 12); |
| 104 |
} |
| 105 |
|
| 106 |
// returns true if the battery is pressed, otherwise false |
| 107 |
static int board_battery_state(void) { |
| 108 |
uint16_t value = adc_config_and_read_u16(ADC1, 6, ADC_SAMPLETIME_15CYCLES); |
| 109 |
// If battery voltage is above USB voltage, then we consider the battery |
| 110 |
// to be present. |
| 111 |
return value > 41100; // 41100 is approx 5.5V |
| 112 |
} |
| 113 |
|
| 114 |
static void board_button_init(void) { |
| 115 |
mp_hal_pin_input(pyb_pin_BUTTON); |
| 116 |
} |
| 117 |
|
| 118 |
// returns true if the button is pressed, otherwise false |
| 119 |
static int board_button_state(void) { |
| 120 |
// button is active low |
| 121 |
return !mp_hal_pin_read(pyb_pin_BUTTON); |
| 122 |
} |
| 123 |
|
| 124 |
void board_mboot_cleanup(int reset_mode) { |
| 125 |
board_led_pattern(0, 0); |
| 126 |
hub_display_off(); |
| 127 |
} |
| 128 |
|
| 129 |
void board_mboot_led_init(void) { |
| 130 |
hub_display_on(); |
| 131 |
} |
| 132 |
|
| 133 |
void board_mboot_led_state(int led, int state) { |
| 134 |
if (state) { |
| 135 |
hub_display_set(3 + led, 0x7fff); |
| 136 |
} else { |
| 137 |
hub_display_set(3 + led, 0); |
| 138 |
} |
| 139 |
|
| 140 |
hub_display_update(); |
| 141 |
} |
| 142 |
|
| 143 |
int board_mboot_get_reset_mode(uint32_t *initial_r0) { |
| 144 |
board_battery_init(); |
| 145 |
board_button_init(); |
| 146 |
int reset_mode = BOARDCTRL_RESET_MODE_NORMAL; |
| 147 |
|
| 148 |
if (board_battery_state()) { |
| 149 |
// Battery is present, check flash for update key and start an update if the key exists. |
| 150 |
// Otherwise, boot normally. |
| 151 |
|
| 152 |
// Initialise the external SPI flash. |
| 153 |
MBOOT_SPIFLASH_SPIFLASH->config = MBOOT_SPIFLASH_CONFIG; |
| 154 |
mp_spiflash_init(MBOOT_SPIFLASH_SPIFLASH); |
| 155 |
|
| 156 |
// Read in the key. |
| 157 |
uint32_t buf; |
| 158 |
mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR, 4, (uint8_t *)&buf); |
| 159 |
|
| 160 |
if (buf == SPIFLASH_UPDATE_KEY_VALUE) { |
| 161 |
// The key has the correct value, so read in the FS-load elements and enter the bootloader. |
| 162 |
mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, ELEM_DATA_SIZE, ELEM_DATA_START); |
| 163 |
*initial_r0 = MBOOT_INITIAL_R0_KEY_FSLOAD; |
| 164 |
reset_mode = BOARDCTRL_RESET_MODE_BOOTLOADER; |
| 165 |
} |
| 166 |
} else { |
| 167 |
// Battery is not present. Cycle through reset modes until button is pressed. |
| 168 |
systick_init(); |
| 169 |
hub_display_on(); |
| 170 |
reset_mode = 0; |
| 171 |
for (int i = 0; i < (RESET_MODE_NUM_STATES * RESET_MODE_TIMEOUT_CYCLES + 1) * 64; i++) { |
| 172 |
if (i % 64 == 0) { |
| 173 |
if (++reset_mode > RESET_MODE_NUM_STATES) { |
| 174 |
reset_mode = BOARDCTRL_RESET_MODE_NORMAL; |
| 175 |
} |
| 176 |
board_led_pattern(reset_mode, 0x7fff); |
| 177 |
} |
| 178 |
|
| 179 |
if (board_button_state()) { |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
mp_hal_delay_ms(19); |
| 184 |
} |
| 185 |
|
| 186 |
// Flash the selected reset mode. |
| 187 |
for (int i = 0; i < 6; i++) { |
| 188 |
board_led_pattern(reset_mode, 0x0fff); |
| 189 |
mp_hal_delay_ms(50); |
| 190 |
board_led_pattern(reset_mode, 0x7fff); |
| 191 |
mp_hal_delay_ms(50); |
| 192 |
} |
| 193 |
|
| 194 |
mp_hal_delay_ms(300); |
| 195 |
} |
| 196 |
|
| 197 |
board_led_pattern(0, 0); |
| 198 |
return reset_mode; |
| 199 |
} |
| 200 |
|
| 201 |
void board_mboot_state_change(int state, uint32_t arg) { |
| 202 |
if (state == MBOOT_STATE_FSLOAD_START) { |
| 203 |
// The FS-load update is about to start. Program the update key and FS-load elements |
| 204 |
// into the flash so they can be retrieved if there is a power failure during the update. |
| 205 |
mp_spiflash_erase_block(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR); |
| 206 |
mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, ELEM_DATA_SIZE, ELEM_DATA_START); |
| 207 |
uint32_t key = SPIFLASH_UPDATE_KEY_VALUE; |
| 208 |
mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR, 4, (const uint8_t *)&key); |
| 209 |
} else if (state == MBOOT_STATE_FSLOAD_END) { |
| 210 |
// The FS-load update completed (either with success or failure), so erase the |
| 211 |
// update key and write the result of the FS-load operation into flash. |
| 212 |
mp_spiflash_erase_block(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR); |
| 213 |
mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, 4, (const uint8_t *)&arg); |
| 214 |
} |
| 215 |
|
| 216 |
mboot_state_change_default(state, arg); |
| 217 |
} |
| 218 |
|
| 219 |
#endif |