43
43
/* Baud rate configuration */
44
44
#define BOOT_BAUD (115200)
45
45
46
+ #define BAUD_REG_VAL (F_CPU_RESET * 64) / (BOOT_BAUD * 16)
47
+
46
48
/* Memory configuration
47
49
* BOOTEND_FUSE * 256 must be above Bootloader Program Memory Usage,
48
50
* this is 194 bytes at optimization level -O3, so BOOTEND_FUSE = 0x01
@@ -69,12 +71,12 @@ FUSES = {
69
71
typedef void (* const app_t )(void );
70
72
71
73
/* 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 );
78
80
79
81
/*
80
82
* Main boot function
@@ -100,7 +102,8 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
100
102
init_uart ();
101
103
init_status_led ();
102
104
103
- toggle_status_led ();
105
+ /* HACK: esp32 seems to send an extra byte at the beginning */
106
+ uart_receive ();
104
107
105
108
/*
106
109
* Start programming at start for application section
@@ -110,6 +113,12 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
110
113
while (app_ptr - MAPPED_PROGMEM_START <= (uint8_t * )PROGMEM_END ) {
111
114
/* Receive and echo data before loading to memory */
112
115
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
113
122
uart_send (rx_data );
114
123
115
124
/* Incremental load to page buffer before writing to Flash */
@@ -131,7 +140,7 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
131
140
/*
132
141
* Boot access request function
133
142
*/
134
- static bool is_bootloader_requested (void )
143
+ static inline bool is_bootloader_requested (void )
135
144
{
136
145
/* Check for boot request from firmware */
137
146
if (USERROW .USERROW31 == 0xEB ) {
@@ -148,7 +157,7 @@ static bool is_bootloader_requested(void)
148
157
/*
149
158
* Communication interface functions
150
159
*/
151
- static void init_uart (void )
160
+ static inline void init_uart (void )
152
161
{
153
162
/* Configure UART */
154
163
USART0 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
@@ -158,8 +167,7 @@ static void init_uart(void)
158
167
* Asynchronous communication without Auto-baud (Sync Field)
159
168
* 20MHz Clock, 3V
160
169
*/
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
163
171
int8_t sigrow_val = SIGROW .OSC16ERR5V ; // read signed error
164
172
baud_reg_val *= (1024 + sigrow_val ); // sum resolution + error
165
173
baud_reg_val += 512 ; // compensate for rounding error
@@ -172,26 +180,26 @@ static void init_uart(void)
172
180
VPORTA .DIR |= PIN4_bm ;
173
181
}
174
182
175
- static uint8_t uart_receive (void )
183
+ static inline uint8_t uart_receive (void )
176
184
{
177
185
/* Poll for data received */
178
186
while (!(USART0 .STATUS & USART_RXCIF_bm ));
179
187
return USART0 .RXDATAL ;
180
188
}
181
189
182
- static void uart_send (uint8_t byte )
190
+ static inline void uart_send (uint8_t byte )
183
191
{
184
192
/* Data will be sent when TXDATA is written */
185
193
USART0 .TXDATAL = byte ;
186
194
}
187
195
188
- static void init_status_led (void )
196
+ static inline void init_status_led (void )
189
197
{
190
198
/* Set LED0 (PD6) as output */
191
199
VPORTD .DIR |= PIN6_bm ;
192
200
}
193
201
194
- static void toggle_status_led (void )
202
+ static inline void toggle_status_led (void )
195
203
{
196
204
/* Toggle LED0 (PD6) */
197
205
VPORTD .OUT ^= PIN6_bm ;
0 commit comments