-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.c
286 lines (256 loc) · 7.04 KB
/
bootloader.c
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
/*
* This file is part of StormLoader, the Storm Bootloader
*
* StormLoader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StormLoader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StormLoader. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2014, Michael Andersen <[email protected]>
*/
#include <usart.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <flashcalw.h>
#include <spi.h>
#include "info.h"
#include "bootloader.h"
#include "ASF/common/services/clock/sam4l/sysclk.h"
#include "ASF/common/services/ioport/ioport.h"
uint8_t byte_escape;
uint8_t tx_stage_ram [TXBUFSZ];
uint16_t tx_ptr;
uint16_t tx_left;
uint8_t rx_stage_ram [RXBUFSZ];
uint16_t rx_ptr;
const sam_usart_opt_t bl_settings = {
115200,
US_MR_CHRL_8_BIT,
US_MR_PAR_NO, //TODO change
US_MR_NBSTOP_1_BIT,
US_MR_CHMODE_NORMAL
};
void bl_init(void)
{
byte_escape = 0;
tx_ptr = tx_left = 0;
tx_ptr = tx_left = 0;
rx_ptr = 0;
//Enable BL USART
ioport_set_pin_mode(PIN_PB10A_USART3_TXD, MUX_PB10A_USART3_TXD);
ioport_disable_pin(PIN_PB10A_USART3_TXD);
ioport_set_pin_mode(PIN_PB09A_USART3_RXD, MUX_PB09A_USART3_RXD);
ioport_disable_pin(PIN_PB09A_USART3_RXD);
sysclk_enable_peripheral_clock(USART3);
usart_reset(USART3);
usart_init_rs232(USART3, &bl_settings, sysclk_get_main_hz());
usart_enable_tx(USART3);
usart_enable_rx(USART3);
//Enable flash SPI
ioport_set_pin_mode(PIN_PC05A_SPI_MOSI, MUX_PC05A_SPI_MOSI);
ioport_disable_pin(PIN_PC05A_SPI_MOSI);
ioport_set_pin_mode(PIN_PC04A_SPI_MISO, MUX_PC04A_SPI_MISO);
ioport_disable_pin(PIN_PC04A_SPI_MISO);
ioport_set_pin_mode(PIN_PC06A_SPI_SCK, MUX_PC06A_SPI_SCK);
ioport_disable_pin(PIN_PC06A_SPI_SCK);
ioport_set_pin_mode(PIN_PC03A_SPI_NPCS0, MUX_PC03A_SPI_NPCS0); //FL CS
ioport_disable_pin(PIN_PC03A_SPI_NPCS0);
ioport_set_pin_mode(PIN_PC01A_SPI_NPCS3, MUX_PC01A_SPI_NPCS3); //RAD CS
ioport_disable_pin(PIN_PC01A_SPI_NPCS3);
spi_enable_clock(SPI);
spi_reset(SPI);
spi_set_master_mode(SPI);
spi_disable_mode_fault_detect(SPI);
spi_disable_loopback(SPI);
spi_set_peripheral_chip_select_value(SPI, spi_get_pcs(0));
//spi_set_fixed_peripheral_select(SPI);
spi_set_variable_peripheral_select(SPI);
spi_disable_peripheral_select_decode(SPI);
spi_set_delay_between_chip_select(SPI, 0);
spi_set_transfer_delay(SPI, 0, FLASH_POSTCS_DELAY, FLASH_IXF_DELAY);
spi_set_bits_per_transfer(SPI, 0, 8);
spi_set_baudrate_div(SPI, 0, spi_calc_baudrate_div(FLASH_BAUD_RATE, sysclk_get_cpu_hz()));
spi_configure_cs_behavior(SPI, 0, SPI_CS_KEEP_LOW);
spi_set_clock_polarity(SPI, 0, 1); //SPI mode 3
spi_set_clock_phase(SPI, 0, 0);
spi_enable(SPI);
//Test pin toggle
ioport_set_pin_dir(PIN_PB08, IOPORT_DIR_OUTPUT);
}
uint16_t bl_spi_xfer(uint8_t* send, uint8_t* recv, uint16_t len)
{
uint16_t i;
uint16_t recvd = 0;
uint32_t timeout;
//Throw away existing buffer
while(spi_is_rx_full(SPI))
{
spi_get(SPI);
}
for (i = 0; i < len; i++)
{
for(timeout = 100000; timeout > 0 && !spi_is_tx_ready(SPI);timeout--);
spi_write(SPI, send[i], spi_get_pcs(0), i==(len-1));
for(timeout = 100000; timeout > 0 && !spi_is_rx_ready(SPI);timeout--);
uint16_t dw;
dw = spi_get(SPI);
recv[recvd] = (uint8_t) dw;
recvd ++;
}
return recvd;
}
uint16_t bl_spi_tx(uint8_t* send, uint16_t len, uint8_t do_end)
{
uint16_t i;
uint16_t recvd = 0;
uint32_t timeout;
for (i = 0; i < len; i++)
{
for(timeout = 100000; timeout > 0 && !spi_is_tx_empty(SPI);timeout--);
spi_write(SPI, send[i], spi_get_pcs(0), (i==(len-1))&&do_end);
}
for(timeout = 100000; timeout > 0 && !spi_is_tx_ready(SPI);timeout--);
return recvd;
}
uint16_t bl_spi_rx(uint8_t* recv, uint16_t len)
{
uint16_t i;
uint16_t recvd = 0;
uint32_t timeout;
//Throw away existing buffer
while(spi_is_rx_full(SPI))
{
spi_get(SPI);
}
for (i = 0; i < len; i++)
{
for(timeout = 100000; timeout > 0 && !spi_is_tx_ready(SPI);timeout--);
spi_write(SPI, 0x00, spi_get_pcs(0), i==(len-1));
for(timeout = 100000; timeout > 0 && !spi_is_rx_ready(SPI);timeout--);
uint16_t dw;
dw = spi_get(SPI);
recv[recvd] = (uint8_t) dw;
recvd ++;
}
return recvd;
}
void bl_loop_poll(void)
{
if (usart_is_rx_ready(USART3))
{
uint32_t ch;
usart_getchar(USART3, &ch);
if (rx_ptr == RXBUFSZ)
{
tx_ptr = 0;
tx_left = 1;
tx_stage_ram[0] = RES_OVERFLOW;
}
else
{
bl_rxb(ch);
}
}
if (usart_is_tx_ready(USART3))
{
if (tx_left > 0)
{
bl_txb(tx_stage_ram[tx_ptr++]);
tx_left--;
}
}
}
void bl_txb(uint8_t b)
{
usart_putchar(USART3, b);
}
void bl_rxb(uint8_t b)
{
if (byte_escape && b == ESCAPE_CHAR)
{
byte_escape = 0;
rx_stage_ram[rx_ptr++] = b;
}
else if (byte_escape)
{
bl_cmd(b);
byte_escape = 0;
}
else if (b == ESCAPE_CHAR)
{
byte_escape = 1;
}
else
{
rx_stage_ram[rx_ptr++] = b;
}
}
void bl_cmd(uint8_t b)
{
switch(b)
{
case CMD_PING:
bl_c_ping();
break;
case CMD_INFO:
bl_c_info();
break;
case CMD_ID:
bl_c_id();
break;
case CMD_RESET:
bl_c_reset();
break;
case CMD_WPAGE:
bl_c_wpage();
break;
case CMD_EPAGE:
bl_c_epage();
break;
case CMD_XEBLOCK:
bl_c_xeblock();
break;
case CMD_XWPAGE:
bl_c_xwpage();
break;
case CMD_CRCRX:
bl_c_crcrx();
break;
case CMD_RRANGE:
bl_c_rrange();
break;
case CMD_XRRANGE:
bl_c_xrrange();
break;
case CMD_SATTR:
bl_c_sattr();
break;
case CMD_GATTR:
bl_c_gattr();
break;
case CMD_CRCIF:
bl_c_crcif();
break;
case CMD_CRCEF:
bl_c_crcef();
break;
case CMD_XEPAGE:
bl_c_xepage();
break;
case CMD_XFINIT:
bl_c_xfinit();
break;
default:
bl_c_unknown();
break;
}
}