blob: 854de204544ec1c3ed498952be3456809319a16a [file] [log] [blame]
Jörg Hohensohn6a4e4c82003-11-30 11:37:43 +00001// 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
10int 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
30int 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
47int 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"
64int 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;
Jens Arnoldc519e632004-11-19 22:44:45 +000077}
78