Debian Codegraph @

ports/esp32/boards/ARDUINO_NANO_ESP32/board_init.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2023 Arduino SA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <string.h>
28 #include "py/mphal.h"
29 #include "shared/tinyusb/mp_usbd_cdc.h"
30
31 #include <esp_system.h>
32 #include <esp_ota_ops.h>
33 #include <esp_partition.h>
34
35 #include "double_tap.h"
36 #include "usb.h"
37
38 #define LED_RED GPIO_NUM_46
39 #define LED_GREEN GPIO_NUM_0
40 #define LED_BLUE GPIO_NUM_45
41 #define DELAY_US 60000
42
43 static bool _recovery_marker_found; // double tap detected
44 static bool _recovery_active; // running from factory partition
45
46 static void rgb_pulse_delay() {
47 // initialize RGB signals from weak pinstraps
48 mp_hal_pin_output(LED_RED);
49 mp_hal_pin_output(LED_GREEN);
50 mp_hal_pin_output(LED_BLUE);
51
52 static const uint8_t SEQ[] = { 1, 3, 2, 6, 7, 5, 4, 5, 7, 6, 2, 3, 1 };
53 for (int idx = 0; idx < sizeof(SEQ); ++idx) {
54 int v = SEQ[idx & 7];
55 mp_hal_pin_write(LED_RED, !(v & 1));
56 mp_hal_pin_write(LED_GREEN, !(v & 2));
57 mp_hal_pin_write(LED_BLUE, !(v & 4));
58
59 // busy wait, we can't use task delay yet
60 mp_hal_delay_us_fast(DELAY_US);
61 }
62
63 // reset pins to digital HIGH before leaving
64 mp_hal_pin_write(LED_RED, 1);
65 mp_hal_pin_write(LED_GREEN, 1);
66 mp_hal_pin_write(LED_BLUE, 1);
67 }
68
69 void NANO_ESP32_enter_bootloader(void) {
70 if (!_recovery_active) {
71 // check for valid partition scheme
72 const esp_partition_t *ota_part = esp_ota_get_next_update_partition(NULL);
73 const esp_partition_t *fact_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
74 if (ota_part && fact_part) {
75 // set tokens so the recovery FW will find them
76 double_tap_mark();
77 // invalidate other OTA image
78 esp_partition_erase_range(ota_part, 0, 4096);
79 // activate factory partition
80 esp_ota_set_boot_partition(fact_part);
81 }
82 }
83
84 esp_restart();
85 }
86
87 void NANO_ESP32_board_startup(void) {
88 boardctrl_startup();
89
90 // mark current partition as valid
91 const esp_partition_t *running = esp_ota_get_running_partition();
92 esp_ota_img_states_t ota_state;
93 if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
94 if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
95 esp_ota_mark_app_valid_cancel_rollback();
96 }
97 }
98
99 const esp_partition_t *part = esp_ota_get_running_partition();
100 _recovery_active = (part->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY);
101
102 double_tap_init();
103
104 _recovery_marker_found = double_tap_check_match();
105 if (_recovery_marker_found && !_recovery_active) {
106 // double tap detected in user application, reboot to factory
107 NANO_ESP32_enter_bootloader();
108 }
109
110 // delay with mark set then proceed
111 // - for normal startup, to detect first double tap
112 // - in recovery mode, to ignore several short presses
113 double_tap_mark();
114 rgb_pulse_delay();
115 double_tap_invalidate();
116 }