summaryrefslogtreecommitdiffstats
path: root/flash/uart_boot/flash.c
blob: f27bb7ec0ab38b6570f0ad49516d0a82613f8987 (plain)
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
// flash.cpp : higher-level functions for flashing the chip
//

#include "scalar_types.h" // (U)INT8/16/32
#include "Uart.h" // platform abstraction for UART
#include "client.h" // client functions


// read the manufacturer and device ID
int ReadID(tUartHandle serial_handle, UINT32 base, UINT8* pManufacturerID, UINT8* pDeviceID)
{
	base &= 0xFFF80000; // round down to 512k align, to make shure

	WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
	WriteByte(serial_handle, base + 0x2AAA, 0x55);
	WriteByte(serial_handle, base + 0x5555, 0x90); // ID command
	SLEEP(20); // Atmel wants 20ms pause here

	*pManufacturerID = ReadByte(serial_handle, base + 0);
	*pDeviceID		 = ReadByte(serial_handle, base + 1);

	WriteByte(serial_handle, base + 0, 0xF0);	// reset flash (back to normal read mode)
	SLEEP(20); // Atmel wants 20ms pause here

	return 0;
}


// erase the sector which contains the given address
int EraseSector(tUartHandle serial_handle, UINT32 address)
{
	UINT32 base = address & 0xFFF80000; // round down to 512k align
	
	WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
	WriteByte(serial_handle, base + 0x2AAA, 0x55);
	WriteByte(serial_handle, base + 0x5555, 0x80); // eraze command
	WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
	WriteByte(serial_handle, base + 0x2AAA, 0x55);
	WriteByte(serial_handle, address, 0x30); // eraze the sector
	SLEEP(25); // sector eraze time: 25ms

	return 0;
}


// erase the whole flash
int EraseChip(tUartHandle serial_handle, UINT32 base)
{
	base &= 0xFFF80000; // round down to 512k align, to make shure
	
	WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
	WriteByte(serial_handle, base + 0x2AAA, 0x55);
	WriteByte(serial_handle, base + 0x5555, 0x80); // eraze command
	WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
	WriteByte(serial_handle, base + 0x2AAA, 0x55);
	WriteByte(serial_handle, base + 0x5555, 0x10); // chip eraze command
	SLEEP(100); // chip eraze time: 100ms

	return 0;
}


// program a bunch of bytes "by hand"
int ProgramBytes(tUartHandle serial_handle, UINT32 address, UINT8* pData, UINT32 size)
{
	UINT32 base = address & 0xFFF80000; // round down to 512k align

	while (size--)
	{
		WriteByte(serial_handle, base + 0x5555, 0xAA); // enter command mode
		WriteByte(serial_handle, base + 0x2AAA, 0x55);
		WriteByte(serial_handle, base + 0x5555, 0xA0); // byte program command
		WriteByte(serial_handle, address++, *pData++);
		// UART protocol is slow enough such that I don't have to wait 20us here
	}
	return 0;
}