4343/* Baud rate configuration */
4444#define BOOT_BAUD (115200)
4545
46+ #define BAUD_REG_VAL (F_CPU_RESET * 64) / (BOOT_BAUD * 16)
47+
4648/* Memory configuration
4749 * BOOTEND_FUSE * 256 must be above Bootloader Program Memory Usage,
4850 * this is 194 bytes at optimization level -O3, so BOOTEND_FUSE = 0x01
@@ -69,12 +71,12 @@ FUSES = {
6971typedef void (* const app_t )(void );
7072
7173/* Interface function prototypes */
72- static bool is_bootloader_requested (void );
73- static void init_uart (void );
74- static uint8_t uart_receive (void );
75- static void uart_send (uint8_t byte );
76- static void init_status_led (void );
77- static void toggle_status_led (void );
74+ static inline bool is_bootloader_requested (void );
75+ static inline void init_uart (void );
76+ static inline uint8_t uart_receive (void );
77+ static inline void uart_send (uint8_t byte );
78+ static inline void init_status_led (void );
79+ static inline void toggle_status_led (void );
7880
7981/*
8082 * Main boot function
@@ -100,7 +102,8 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
100102 init_uart ();
101103 init_status_led ();
102104
103- toggle_status_led ();
105+ /* HACK: esp32 seems to send an extra byte at the beginning */
106+ uart_receive ();
104107
105108 /*
106109 * Start programming at start for application section
@@ -110,6 +113,12 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
110113 while (app_ptr - MAPPED_PROGMEM_START <= (uint8_t * )PROGMEM_END ) {
111114 /* Receive and echo data before loading to memory */
112115 uint8_t rx_data = uart_receive ();
116+ #if 0
117+ if (app_ptr == (uint8_t * )MAPPED_APPLICATION_START && rx_data == 0 ) {
118+ // skip first character if 0x00
119+ continue ;
120+ }
121+ #endif
113122 uart_send (rx_data );
114123
115124 /* Incremental load to page buffer before writing to Flash */
@@ -131,7 +140,7 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
131140/*
132141 * Boot access request function
133142 */
134- static bool is_bootloader_requested (void )
143+ static inline bool is_bootloader_requested (void )
135144{
136145 /* Check for boot request from firmware */
137146 if (USERROW .USERROW31 == 0xEB ) {
@@ -148,7 +157,7 @@ static bool is_bootloader_requested(void)
148157/*
149158 * Communication interface functions
150159 */
151- static void init_uart (void )
160+ static inline void init_uart (void )
152161{
153162 /* Configure UART */
154163 USART0 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
@@ -158,8 +167,7 @@ static void init_uart(void)
158167 * Asynchronous communication without Auto-baud (Sync Field)
159168 * 20MHz Clock, 3V
160169 */
161- int32_t baud_reg_val = (F_CPU_RESET * 64 ) / (BOOT_BAUD * 16 ); // ideal BAUD register value
162- assert (baud_reg_val >= 0x4A ); // Verify legal min BAUD register value with max neg comp
170+ int32_t baud_reg_val = BAUD_REG_VAL ; // ideal BAUD register value
163171 int8_t sigrow_val = SIGROW .OSC16ERR5V ; // read signed error
164172 baud_reg_val *= (1024 + sigrow_val ); // sum resolution + error
165173 baud_reg_val += 512 ; // compensate for rounding error
@@ -172,26 +180,26 @@ static void init_uart(void)
172180 VPORTA .DIR |= PIN4_bm ;
173181}
174182
175- static uint8_t uart_receive (void )
183+ static inline uint8_t uart_receive (void )
176184{
177185 /* Poll for data received */
178186 while (!(USART0 .STATUS & USART_RXCIF_bm ));
179187 return USART0 .RXDATAL ;
180188}
181189
182- static void uart_send (uint8_t byte )
190+ static inline void uart_send (uint8_t byte )
183191{
184192 /* Data will be sent when TXDATA is written */
185193 USART0 .TXDATAL = byte ;
186194}
187195
188- static void init_status_led (void )
196+ static inline void init_status_led (void )
189197{
190198 /* Set LED0 (PD6) as output */
191199 VPORTD .DIR |= PIN6_bm ;
192200}
193201
194- static void toggle_status_led (void )
202+ static inline void toggle_status_led (void )
195203{
196204 /* Toggle LED0 (PD6) */
197205 VPORTD .OUT ^= PIN6_bm ;
0 commit comments