2929 * to disable standard startup files in Toolchain->AVR/GNU Linker->General.
3030 *
3131 * The example is written for ATtiny817 with the following pinout:
32- * USART0 TxD PB2
33- * USART0 RxD PB3
34- * LED0 PB4
35- * SW1 PC5 (external pull-up)
32+ * USART0 TxD PA4
33+ * USART0 RxD PA5
34+ * LED0 PD6
35+ * SW1 PC1 (external pull-up)
3636 */
3737#define F_CPU_RESET (16E6/6)
3838
@@ -100,6 +100,8 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
100100 init_uart ();
101101 init_status_led ();
102102
103+ toggle_status_led ();
104+
103105 /*
104106 * Start programming at start for application section
105107 * Subtract MAPPED_PROGMEM_START in condition to handle overflow on large flash sizes
@@ -131,11 +133,16 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
131133 */
132134static bool is_bootloader_requested (void )
133135{
134- /* Check if SW1 (PA0) is low */
135- if (VPORTA .IN & PIN0_bm ) {
136- return false;
136+ /* Check for boot request from firmware */
137+ if (USERROW .USERROW31 == 0xEB ) {
138+ /* Clear boot request*/
139+ USERROW .USERROW31 = 0xff ;
140+ _PROTECTED_WRITE_SPM (NVMCTRL .CTRLA , NVMCTRL_CMD_PAGEERASEWRITE_gc );
141+ while (NVMCTRL .STATUS & NVMCTRL_EEBUSY_bm );
142+
143+ return true;
137144 }
138- return true ;
145+ return false ;
139146}
140147
141148/*
@@ -144,7 +151,7 @@ static bool is_bootloader_requested(void)
144151static void init_uart (void )
145152{
146153 /* Configure UART */
147- USART1 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
154+ USART0 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
148155
149156 /* From datasheet:
150157 * Baud rate compensated with factory stored frequency error
@@ -157,35 +164,35 @@ static void init_uart(void)
157164 baud_reg_val *= (1024 + sigrow_val ); // sum resolution + error
158165 baud_reg_val += 512 ; // compensate for rounding error
159166 baud_reg_val /= 1024 ; // divide by resolution
160- USART1 .BAUD = (int16_t ) baud_reg_val ; // set adjusted baud rate
167+ USART0 .BAUD = (int16_t ) baud_reg_val ; // set adjusted baud rate
161168
162- PORTMUX .USARTROUTEA |= PORTMUX_USART1_ALT1_gc ;
169+ PORTMUX .USARTROUTEA |= PORTMUX_USART0_ALT1_gc ;
163170
164- /* Set TxD (PB2 ) as output */
165- VPORTC .DIR |= PIN4_bm ;
171+ /* Set TxD (PA4 ) as output */
172+ VPORTA .DIR |= PIN4_bm ;
166173}
167174
168175static uint8_t uart_receive (void )
169176{
170177 /* Poll for data received */
171- while (!(USART1 .STATUS & USART_RXCIF_bm ));
172- return USART1 .RXDATAL ;
178+ while (!(USART0 .STATUS & USART_RXCIF_bm ));
179+ return USART0 .RXDATAL ;
173180}
174181
175182static void uart_send (uint8_t byte )
176183{
177184 /* Data will be sent when TXDATA is written */
178- USART1 .TXDATAL = byte ;
185+ USART0 .TXDATAL = byte ;
179186}
180187
181188static void init_status_led (void )
182189{
183- /* Set LED0 (PB4 ) as output */
190+ /* Set LED0 (PD6 ) as output */
184191 VPORTD .DIR |= PIN6_bm ;
185192}
186193
187194static void toggle_status_led (void )
188195{
189- /* Toggle LED0 (PB4 ) */
196+ /* Toggle LED0 (PD6 ) */
190197 VPORTD .OUT ^= PIN6_bm ;
191198}
0 commit comments