29
29
* to disable standard startup files in Toolchain->AVR/GNU Linker->General.
30
30
*
31
31
* 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)
36
36
*/
37
37
#define F_CPU_RESET (16E6/6)
38
38
@@ -100,6 +100,8 @@ __attribute__((naked)) __attribute__((section(".ctors"))) void boot(void)
100
100
init_uart ();
101
101
init_status_led ();
102
102
103
+ toggle_status_led ();
104
+
103
105
/*
104
106
* Start programming at start for application section
105
107
* 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)
131
133
*/
132
134
static bool is_bootloader_requested (void )
133
135
{
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;
137
144
}
138
- return true ;
145
+ return false ;
139
146
}
140
147
141
148
/*
@@ -144,7 +151,7 @@ static bool is_bootloader_requested(void)
144
151
static void init_uart (void )
145
152
{
146
153
/* Configure UART */
147
- USART1 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
154
+ USART0 .CTRLB = USART_RXEN_bm | USART_TXEN_bm ;
148
155
149
156
/* From datasheet:
150
157
* Baud rate compensated with factory stored frequency error
@@ -157,35 +164,35 @@ static void init_uart(void)
157
164
baud_reg_val *= (1024 + sigrow_val ); // sum resolution + error
158
165
baud_reg_val += 512 ; // compensate for rounding error
159
166
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
161
168
162
- PORTMUX .USARTROUTEA |= PORTMUX_USART1_ALT1_gc ;
169
+ PORTMUX .USARTROUTEA |= PORTMUX_USART0_ALT1_gc ;
163
170
164
- /* Set TxD (PB2 ) as output */
165
- VPORTC .DIR |= PIN4_bm ;
171
+ /* Set TxD (PA4 ) as output */
172
+ VPORTA .DIR |= PIN4_bm ;
166
173
}
167
174
168
175
static uint8_t uart_receive (void )
169
176
{
170
177
/* 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 ;
173
180
}
174
181
175
182
static void uart_send (uint8_t byte )
176
183
{
177
184
/* Data will be sent when TXDATA is written */
178
- USART1 .TXDATAL = byte ;
185
+ USART0 .TXDATAL = byte ;
179
186
}
180
187
181
188
static void init_status_led (void )
182
189
{
183
- /* Set LED0 (PB4 ) as output */
190
+ /* Set LED0 (PD6 ) as output */
184
191
VPORTD .DIR |= PIN6_bm ;
185
192
}
186
193
187
194
static void toggle_status_led (void )
188
195
{
189
- /* Toggle LED0 (PB4 ) */
196
+ /* Toggle LED0 (PD6 ) */
190
197
VPORTD .OUT ^= PIN6_bm ;
191
198
}
0 commit comments