diff options
Diffstat (limited to 'flash/uart_boot/flash.c')
-rw-r--r-- | flash/uart_boot/flash.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/flash/uart_boot/flash.c b/flash/uart_boot/flash.c new file mode 100644 index 0000000000..f27bb7ec0a --- /dev/null +++ b/flash/uart_boot/flash.c | |||
@@ -0,0 +1,77 @@ | |||
1 | // flash.cpp : higher-level functions for flashing the chip | ||
2 | // | ||
3 | |||
4 | #include "scalar_types.h" // (U)INT8/16/32 | ||
5 | #include "Uart.h" // platform abstraction for UART | ||
6 | #include "client.h" // client functions | ||
7 | |||
8 | |||
9 | // read the manufacturer and device ID | ||
10 | int ReadID(tUartHandle serial_handle, UINT32 base, UINT8* pManufacturerID, UINT8* pDeviceID) | ||
11 | { | ||
12 | base &= 0xFFF80000; // round down to 512k align, to make shure | ||
13 | |||
14 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
15 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
16 | WriteByte(serial_handle, base + 0x5555, 0x90); // ID command | ||
17 | SLEEP(20); // Atmel wants 20ms pause here | ||
18 | |||
19 | *pManufacturerID = ReadByte(serial_handle, base + 0); | ||
20 | *pDeviceID = ReadByte(serial_handle, base + 1); | ||
21 | |||
22 | WriteByte(serial_handle, base + 0, 0xF0); // reset flash (back to normal read mode) | ||
23 | SLEEP(20); // Atmel wants 20ms pause here | ||
24 | |||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | |||
29 | // erase the sector which contains the given address | ||
30 | int EraseSector(tUartHandle serial_handle, UINT32 address) | ||
31 | { | ||
32 | UINT32 base = address & 0xFFF80000; // round down to 512k align | ||
33 | |||
34 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
35 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
36 | WriteByte(serial_handle, base + 0x5555, 0x80); // eraze command | ||
37 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
38 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
39 | WriteByte(serial_handle, address, 0x30); // eraze the sector | ||
40 | SLEEP(25); // sector eraze time: 25ms | ||
41 | |||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | |||
46 | // erase the whole flash | ||
47 | int EraseChip(tUartHandle serial_handle, UINT32 base) | ||
48 | { | ||
49 | base &= 0xFFF80000; // round down to 512k align, to make shure | ||
50 | |||
51 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
52 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
53 | WriteByte(serial_handle, base + 0x5555, 0x80); // eraze command | ||
54 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
55 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
56 | WriteByte(serial_handle, base + 0x5555, 0x10); // chip eraze command | ||
57 | SLEEP(100); // chip eraze time: 100ms | ||
58 | |||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | |||
63 | // program a bunch of bytes "by hand" | ||
64 | int ProgramBytes(tUartHandle serial_handle, UINT32 address, UINT8* pData, UINT32 size) | ||
65 | { | ||
66 | UINT32 base = address & 0xFFF80000; // round down to 512k align | ||
67 | |||
68 | while (size--) | ||
69 | { | ||
70 | WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode | ||
71 | WriteByte(serial_handle, base + 0x2AAA, 0x55); | ||
72 | WriteByte(serial_handle, base + 0x5555, 0xA0); // byte program command | ||
73 | WriteByte(serial_handle, address++, *pData++); | ||
74 | // UART protocol is slow enough such that I don't have to wait 20us here | ||
75 | } | ||
76 | return 0; | ||
77 | } \ No newline at end of file | ||