Debian Codegraph @

ports/stm32/boards/LEGO_HUB_NO6/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) 2021-2022 Damien P. George
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 "py/mphal.h"
28 #include "irq.h"
29
30 void board_init(void) {
31 if (query_irq() == IRQ_STATE_DISABLED) {
32 enable_irq(IRQ_STATE_ENABLED);
33 }
34
35 // Enable 3V3 for all ports
36 mp_hal_pin_output(pyb_pin_PORT_3V3_EN);
37 mp_hal_pin_high(pyb_pin_PORT_3V3_EN);
38
39 // Port A
40 // Enable RX/TX buffer
41 mp_hal_pin_output(pyb_pin_PORTA_EN);
42 mp_hal_pin_low(pyb_pin_PORTA_EN);
43
44 // Port B
45 // Enable RX/TX buffer
46 mp_hal_pin_output(pyb_pin_PORTB_EN);
47 mp_hal_pin_low(pyb_pin_PORTB_EN);
48
49 // Port C
50 // Enable RX/TX buffer
51 mp_hal_pin_output(pyb_pin_PORTC_EN);
52 mp_hal_pin_low(pyb_pin_PORTC_EN);
53
54 // Port D
55 // Enable RX/TX buffer
56 mp_hal_pin_output(pyb_pin_PORTD_EN);
57 mp_hal_pin_low(pyb_pin_PORTD_EN);
58
59 // Port E
60 // Enable RX/TX buffer
61 mp_hal_pin_output(pyb_pin_PORTE_EN);
62 mp_hal_pin_low(pyb_pin_PORTE_EN);
63 // Disable RS485 driver
64 mp_hal_pin_output(pyb_pin_PORTE_RTS);
65 mp_hal_pin_low(pyb_pin_PORTE_RTS);
66
67 // Port F
68 // Enable RX/TX buffer
69 mp_hal_pin_output(pyb_pin_PORTF_EN);
70 mp_hal_pin_low(pyb_pin_PORTF_EN);
71 // Disable RS485 driver
72 mp_hal_pin_output(pyb_pin_PORTF_RTS);
73 mp_hal_pin_low(pyb_pin_PORTF_RTS);
74 }
75
76 #if BUILDING_MBOOT
77
78 #include "drivers/memory/spiflash.h"
79 #include "mboot/mboot.h"
80 #include "boardctrl.h"
81 #include "adc.h"
82 #include "hub_display.h"
83
84 #define RESET_MODE_NUM_STATES (4)
85 #define RESET_MODE_TIMEOUT_CYCLES (8)
86
87 #define PATTERN_B (0x00651946)
88 #define PATTERN_F (0x0021184e)
89 #define PATTERN_N (0x01296ad2)
90 #define PATTERN_S (0x0064104c)
91
92 // Location and value for the SPI flash update key. If this key exists at the defined
93 // location then mboot will attempt to do a filesystem-load update of the main firmware.
94 // This makes the update robust to power failures: if the update does not complete then
95 // it will be restarted the next time it powers up. Only when it fully completes will
96 // this key be erased, and then the application can run.
97 #define SPIFLASH_UPDATE_KEY_ADDR (1020 * 1024)
98 #define SPIFLASH_UPDATE_KEY_VALUE (0x12345678)
99
100 static void board_led_pattern(int reset_mode, uint16_t brightness) {
101 static const uint32_t pixels[] = {
102 0,
103 PATTERN_N,
104 PATTERN_S,
105 PATTERN_F,
106 PATTERN_B,
107 };
108 uint32_t pixel = pixels[reset_mode];
109 for (int i = 0; i < 25; ++i) {
110 hub_display_set(i, brightness * ((pixel >> i) & 1));
111 }
112 hub_display_update();
113 }
114
115 static void board_button_init(void) {
116 mp_hal_pin_config(pyb_pin_BUTTONS_ADC, MP_HAL_PIN_MODE_ADC, MP_HAL_PIN_PULL_NONE, 0);
117 adc_config(ADC1, 12);
118 }
119
120 static int board_button_state(void) {
121 uint16_t value = adc_config_and_read_u16(ADC1, 1, ADC_SAMPLETIME_15CYCLES);
122 return value < 44000;
123 }
124
125 void board_mboot_cleanup(int reset_mode) {
126 board_led_pattern(0, 0);
127 hub_display_off();
128 }
129
130 void board_mboot_led_init(void) {
131 hub_display_on();
132 }
133
134 void board_mboot_led_state(int led, int state) {
135 if (state) {
136 hub_display_set(28 + led, 0x7fff);
137 hub_display_set(31 + led, 0x7fff);
138 } else {
139 hub_display_set(28 + led, 0);
140 hub_display_set(31 + led, 0);
141 }
142 hub_display_update();
143 }
144
145 int board_mboot_get_reset_mode(uint32_t *initial_r0) {
146 board_button_init();
147 int reset_mode = BOARDCTRL_RESET_MODE_NORMAL;
148 if (board_button_state()) {
149 // Cycle through reset modes while USR is held.
150 // Timeout is roughly 20s, where reset_mode=1.
151 systick_init();
152 hub_display_on();
153 reset_mode = 0;
154 for (int i = 0; i < (RESET_MODE_NUM_STATES * RESET_MODE_TIMEOUT_CYCLES + 1) * 32; i++) {
155 if (i % 32 == 0) {
156 if (++reset_mode > RESET_MODE_NUM_STATES) {
157 reset_mode = BOARDCTRL_RESET_MODE_NORMAL;
158 }
159 board_led_pattern(reset_mode, 0x7fff);
160 }
161 if (!board_button_state()) {
162 break;
163 }
164 mp_hal_delay_ms(19);
165 }
166 // Flash the selected reset mode.
167 for (int i = 0; i < 6; i++) {
168 board_led_pattern(reset_mode, 0x0fff);
169 mp_hal_delay_ms(50);
170 board_led_pattern(reset_mode, 0x7fff);
171 mp_hal_delay_ms(50);
172 }
173 mp_hal_delay_ms(300);
174 } else {
175 // Button not pressed, check flash for update key and start an update if the key exists.
176
177 // Initialise the external SPI flash.
178 MBOOT_SPIFLASH_SPIFLASH->config = MBOOT_SPIFLASH_CONFIG;
179 mp_spiflash_init(MBOOT_SPIFLASH_SPIFLASH);
180
181 // Read in the key.
182 uint32_t buf;
183 mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR, 4, (uint8_t *)&buf);
184
185 if (buf == SPIFLASH_UPDATE_KEY_VALUE) {
186 // The key has the correct value, so read in the FS-load elements and enter the bootloader.
187 mp_spiflash_read(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, ELEM_DATA_SIZE, ELEM_DATA_START);
188 *initial_r0 = MBOOT_INITIAL_R0_KEY_FSLOAD;
189 reset_mode = BOARDCTRL_RESET_MODE_BOOTLOADER;
190 }
191 }
192 board_led_pattern(0, 0);
193 return reset_mode;
194 }
195
196 void board_mboot_state_change(int state, uint32_t arg) {
197 if (state == MBOOT_STATE_FSLOAD_START) {
198 // The FS-load update is about to start. Program the update key and FS-load elements
199 // into the flash so they can be retrieved if there is a power failure during the update.
200 mp_spiflash_erase_block(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR);
201 mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, ELEM_DATA_SIZE, ELEM_DATA_START);
202 uint32_t key = SPIFLASH_UPDATE_KEY_VALUE;
203 mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR, 4, (const uint8_t *)&key);
204 } else if (state == MBOOT_STATE_FSLOAD_END) {
205 // The FS-load update completed (either with success or failure), so erase the
206 // update key and write the result of the FS-load operation into flash.
207 mp_spiflash_erase_block(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR);
208 mp_spiflash_write(MBOOT_SPIFLASH_SPIFLASH, SPIFLASH_UPDATE_KEY_ADDR + 4, 4, (const uint8_t *)&arg);
209 }
210 mboot_state_change_default(state, arg);
211 }
212
213 #endif