forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxboot.h
304 lines (261 loc) · 10.1 KB
/
xboot.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/************************************************************************/
/* XBoot Extensible AVR Bootloader */
/* */
/* tested with ATXMEGA64A3, ATXMEGA128A1, ATXMEGA256A1, ATXMEGA32A4 */
/* */
/* xboot.h */
/* */
/* Alex Forencich <[email protected]> */
/* */
/* Copyright (c) 2010 Alex Forencich */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files(the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#ifndef __XBOOT_H
#define __XBOOT_H
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
// token pasting
#define token_paste2_int(x, y) x ## y
#define token_paste2(x, y) token_paste2_int(x, y)
#define token_paste3_int(x, y, z) x ## y ## z
#define token_paste3(x, y, z) token_paste3_int(x, y, z)
// Configuration
// clock config
#define USE_DFLL
// use 32MHz osc if makefile calls for it
#if (F_CPU == 32000000L)
// defaults to 2MHz RC oscillator
// define USE_32MHZ_RC to override
#define USE_32MHZ_RC
#endif
// AVR1008 fixes
// Really only applicable to 256a3 rev A and B devices
//#define USE_AVR1008_EEPROM
// bootloader entrace
#define USE_ENTER_DELAY
//#define USE_ENTER_PIN
#define USE_ENTER_UART
//#define USE_ENTER_I2C
//#define USE_ENTER_FIFO
// bootloader exit
//#define LOCK_SPM_ON_EXIT
// bootloader communication
#define USE_LED
#define USE_UART
//#define USE_UART_EN_PIN
//#define USE_I2C
//#define USE_I2C_ADDRESS_NEGOTIATION
//#define USE_ATTACH_LED
//#define USE_FIFO
// General Options
//#define USE_INTERRUPTS
//#define USE_WATCHDOG
// bootloader features
#define ENABLE_BLOCK_SUPPORT
#define ENABLE_FLASH_BYTE_SUPPORT
#define ENABLE_EEPROM_BYTE_SUPPORT
#define ENABLE_LOCK_BITS
#define ENABLE_FUSE_BITS
#define ENABLE_FLASH_ERASE_WRITE
// ENTER_PIN
#define ENTER_PORT PORTC
#define ENTER_PIN 0
#define ENTER_PIN_CTRL token_paste3(ENTER_PORT.PIN, ENTER_PIN, CTRL)
#define ENTER_PIN_STATE 0
#define ENTER_PIN_PUEN 1
// ENTER_DELAY
#define ENTER_BLINK_COUNT 3
#define ENTER_BLINK_WAIT 30000
// ENTER_UART
//#define ENTER_UART_NEED_SYNC
// ENTER_FIFO
//#define ENTER_FIFO_NEED_SYNC
// WATCHDOG
// Select only one
//#define WATCHDOG_TIMEOUT WDT_PER_8CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_16CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_32CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_64CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_128CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_256CLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_512CLK_gc
#define WATCHDOG_TIMEOUT WDT_PER_1KCLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_2KCLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_4KCLK_gc
//#define WATCHDOG_TIMEOUT WDT_PER_8KCLK_gc
// LED
#define LED_PORT PORTA
#define LED_PIN 0
#define LED_INV 1
// UART
#define UART_BAUD_RATE 115200
#define UART_PORT_NAME C
#define UART_NUMBER 0
#if (UART_NUMBER == 0)
#define UART_TX_PIN 3
#else
#define UART_TX_PIN 7
#endif
#define UART_PORT token_paste2(PORT, UART_PORT_NAME)
#define UART_DEVICE_PORT token_paste2(UART_PORT_NAME, UART_NUMBER)
#define UART_DEVICE token_paste2(USART, UART_DEVICE_PORT)
#define UART_DEVICE_RXC_ISR token_paste3(USART, UART_DEVICE_PORT, _RXC_vect)
#define UART_DEVICE_DRE_ISR token_paste3(USART, UART_DEVICE_PORT, _DRE_vect)
#define UART_DEVICE_TXC_ISR token_paste3(USART, UART_DEVICE_PORT, _TXC_vect)
// UART RS485 Enable Output
#define UART_EN_PORT PORTC
#define UART_EN_PIN 4
#define UART_EN_PIN_INV 0
// FIFO
#define FIFO_DATA_PORT PORTC
#define FIFO_CTL_PORT PORTD
#define FIFO_RXF_N_bm 1<<3
#define FIFO_TXE_N_bm 1<<2
#define FIFO_RD_N_bm 1<<1
#define FIFO_WR_N_bm 1<<0
#define FIFO_BIT_REVERSE
#ifdef __AVR_XMEGA__
// BAUD Rate Values
// Known good at 2MHz
#if (F_CPU == 2000000L) && (UART_BAUD_RATE == 19200)
#define UART_BSEL_VALUE 12
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 1
#elif (F_CPU == 2000000L) && (UART_BAUD_RATE == 38400)
#define UART_BSEL_VALUE 22
#define UART_BSCALE_VALUE -2
#define UART_CLK2X 1
#elif (F_CPU == 2000000L) && (UART_BAUD_RATE == 57600)
#define UART_BSEL_VALUE 26
#define UART_BSCALE_VALUE -3
#define UART_CLK2X 1
#elif (F_CPU == 2000000L) && (UART_BAUD_RATE == 115200)
#define UART_BSEL_VALUE 19
#define UART_BSCALE_VALUE -4
#define UART_CLK2X 1
// Known good at 32MHz
#elif (F_CPU == 32000000L) && (UART_BAUD_RATE == 19200)
#define UART_BSEL_VALUE 103
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 0
#elif (F_CPU == 32000000L) && (UART_BAUD_RATE == 38400)
#define UART_BSEL_VALUE 51
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 0
#elif (F_CPU == 32000000L) && (UART_BAUD_RATE == 57600)
#define UART_BSEL_VALUE 34
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 0
#elif (F_CPU == 32000000L) && (UART_BAUD_RATE == 115200)
#define UART_BSEL_VALUE 16
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 0
// None of the above, so calculate something
#else
#warning Not using predefined BAUD rate, possible BAUD rate error!
#if (F_CPU == 2000000L)
#define UART_BSEL_VALUE ((F_CPU) / ((uint32_t)UART_BAUD_RATE * 8) - 1)
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 1
#else
#define UART_BSEL_VALUE ((F_CPU) / ((uint32_t)UART_BAUD_RATE * 16) - 1)
#define UART_BSCALE_VALUE 0
#define UART_CLK2X 0
#endif
#endif
#endif // __AVR_XMEGA__
// I2C
#ifdef __AVR_XMEGA__
#define I2C_DEVICE_PORT C
#define I2C_DEVICE token_paste2(TWI, I2C_DEVICE_PORT)
#define I2C_DEVICE_ISR token_paste3(TWI, I2C_DEVICE_PORT, _TWIS_vect)
#define I2C_MATCH_ANY 1
#define I2C_ADDRESS 0x10
#define I2C_GC_ENABLE 1
#endif // __AVR_XMEGA__
// I2C Address Autonegotiation
// Note: only works on XMega chips for the time being
// There is no easy way to get this to work on regular
// ATMega chips as they have no unique part ID number
#define I2C_AUTONEG_DIS_PROMISC 1
#define I2C_AUTONEG_DIS_GC 0
#define I2C_AUTONEG_PORT PORTA
#define I2C_AUTONEG_PIN 2
// Attach LED
#define ATTACH_LED_PORT PORTA
#define ATTACH_LED_PIN 1
#define ATTACH_LED_INV 1
#ifndef EEPROM_BYTE_ADDRESS_MASK
#if EEPROM_PAGE_SIZE == 32
#define EEPROM_BYTE_ADDRESS_MASK 0x1f
#else
#error Unknown EEPROM page size! Please add new byte address value!
#endif
#endif
#ifdef USE_INTERRUPTS
#ifndef NEED_INTERRUPTS
#define NEED_INTERRUPTS
#endif // NEED_INTERRUPTS
#endif // USE_INTERRUPTS
#ifdef USE_AVR1008_EEPROM
#ifndef NEED_INTERRUPTS
#define NEED_INTERRUPTS
#endif // NEED_INTERRUPTS
#endif // USE_AVR1008_EEPROM
// communication modes
#define MODE_UNDEF 0
#define MODE_UART 1
#define MODE_I2C 2
#define MODE_FIFO 3
// types
typedef uint32_t ADDR_T;
// Includes
#include "protocol.h"
#include "sp_driver.h"
#include "eeprom_driver.h"
#include "uart.h"
#include "i2c.h"
#include "fifo.h"
#include "watchdog.h"
// globals
#ifdef USE_INTERRUPTS
extern volatile unsigned char comm_mode;
extern volatile unsigned char rx_buff0;
extern volatile unsigned char rx_buff1;
extern volatile unsigned char rx_char_cnt;
extern volatile unsigned char tx_buff0;
extern volatile unsigned char tx_char_cnt;
#else
extern unsigned char comm_mode;
#endif // USE_INTERRUPTS
// Functions
unsigned char __attribute__ ((noinline)) ow_slave_read_bit(void);
void __attribute__ ((noinline)) ow_slave_write_bit(unsigned char b);
void ow_slave_wait_bit(void);
unsigned char __attribute__ ((noinline)) get_char(void);
void __attribute__ ((noinline)) send_char(unsigned char c);
unsigned int __attribute__ ((noinline)) get_2bytes(void);
unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address);
void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address);
#endif // __XBOOT_H