Alchemy
Loading...
Searching...
No Matches

atmel.c

Go to the documentation of this file.
4
6
7/// Erases the Atmel chip and waits for completion with its configured timing.
9 u8 savedCode[64];
10 u16 result;
11 volatile u8 *command;
12 volatile u8 *unlock;
13
15
17
18 command = (volatile u8 *)0x0E005555;
19 *command = 0xAA;
20 unlock = (volatile u8 *)0x0E002AAA;
21 *unlock = 0x55;
22 *command = 0x80;
23 *command = 0xAA;
24 *unlock = 0x55;
25 *command = 0x10;
26
27 result = gFlash_Handler3(3, (u8 *)0x0E000000, 255);
28
29 REG_WAITCNT = (REG_WAITCNT & 0xFFFC) | 3;
30
31 return result;
32}
33
34/// Erases one small Atmel sector by programming every byte to 0xff.
37 u8 *cursor;
38 u16 savedIme;
39 s32 bytesRemaining;
40 u16 result;
41
42 cursor = (u8 *)(0x0E000000 + (sector << info->type.sector.shift));
43 savedIme = REG_IME;
44 REG_IME = 0;
45 *(volatile u8 *)0x0E005555 = 0xAA;
46 *(volatile u8 *)0x0E002AAA = 0x55;
47 *(volatile u8 *)0x0E005555 = 0xA0;
48 bytesRemaining = info->type.sector.size;
49 while (bytesRemaining != 0) {
50 *cursor = 0xFF;
51 cursor++;
52 bytesRemaining--;
53 }
54 cursor--;
55 REG_IME = savedIme;
56 result = gFlash_Handler3(1, cursor, 0xFF);
57 if (result != 0) {
58 result = (result & 0xFF00) | 2;
59 }
60 return result;
61}
62
63/// Erases thirty-two small sectors, trying each sector at most twice.
65 // Both structured retry-loop trials add four bytes to the native EN body.
66 u8 savedCode[64];
67 u16 result;
68 u16 smallSector;
69 u16 attemptsRemaining;
70 u16 sectorsErased;
71
72 if ((u32)sector > 15)
73 return 0x80ff;
74
77
78 smallSector = sector << 4;
79 smallSector = (u16)(smallSector << 1);
80 sectorsErased = 0;
81nextSector:
82 attemptsRemaining = 2;
83 goto eraseAttempt;
84retryErase:
85 attemptsRemaining -= 1;
86 if (attemptsRemaining == 0)
87 goto advanceSector;
88eraseAttempt:
89 result = EraseAtmelFlashSector(smallSector);
90 if (result != 0)
91 goto retryErase;
92advanceSector:
93 smallSector += 1;
94 if (result == 0) {
95 sectorsErased += 1;
96 if ((u32)sectorsErased <= 31)
97 goto nextSector;
98 }
99 REG_WAITCNT = (REG_WAITCNT & 0xfffc) | 3;
100 return result;
101}
102
103/// Programs a small Atmel sector with interrupts disabled, then polls its last
104/// byte.
105u16 ProgramAtmelFlashSector(u32 sector, const u8 *source) {
106 const u8 *input;
107 volatile u8 *destination;
108 u32 savedIme;
109 s32 remaining;
110
111 input = source;
112 sector = (u16)sector;
113 destination =
114 (volatile u8 *)(0x0e000000 +
116 savedIme = REG_IME;
117 REG_IME = 0;
118 *(volatile u8 *)0x0e005555 = 0xaa;
119 *(volatile u8 *)0x0e002aaa = 0x55;
120 *(volatile u8 *)0x0e005555 = 0xa0;
121
123 if (remaining != 0) {
124 do {
125 *destination++ = *input++;
126 remaining--;
127 } while (remaining != 0);
128 }
129
130 destination--;
131 input--;
132 REG_IME = savedIme;
133 return gFlash_Handler3(1, destination, *input);
134}
135
137
138/// Programs an Atmel block in small sectors, trying each sector at most twice.
140 // The ordinary decrementing retry loop shortens the native EN body by 16
141 // bytes.
142 u8 savedCode[64];
143 u16 result;
144 u16 smallSector;
145 u16 attemptsRemaining;
146
147 if ((u32)sector > 15)
148 return 0x80ff;
149
150 CopyFlashReadRoutineToRam(savedCode);
152
153 smallSector = sector << 5;
155
156 while (gFlashNumRemainingBytes != 0) {
157 attemptsRemaining = 2;
158 goto programAttempt;
159 do {
160 attemptsRemaining -= 1;
161 if (attemptsRemaining == 0)
162 break;
163 programAttempt:
164 result = ProgramAtmelFlashSector(smallSector, source);
165 } while (result != 0);
166
167 if (result != 0)
168 break;
169
172 smallSector += 1;
173 }
174
175 REG_WAITCNT = (REG_WAITCNT & 0xfffc) | 3;
176 return result;
177}
u16 ProgramAtmelFlashSector(u32 sector, const u8 *source)
Programs a small Atmel sector with interrupts disabled, then polls its last byte.
Definition atmel.c:105
u16 EraseAtmelFlashSector(u16 sector)
Erases one small Atmel sector by programming every byte to 0xff.
Definition atmel.c:35
u16 EraseAtmelFlashBlock(u16 sector)
Erases thirty-two small sectors, trying each sector at most twice.
Definition atmel.c:64
u16 gFlashNumRemainingBytes
u16 EraseAtmelFlashChip(void)
Erases the Atmel chip and waits for completion with its configured timing.
Definition atmel.c:8
u16 ProgramAtmelFlashBlock(u16 sector, u8 *source)
Programs an Atmel block in small sectors, trying each sector at most twice.
Definition atmel.c:139
u16 gFlash_Handler3
Credits scroll position in pixels, in the low half of a flash work cell.
void CopyFlashReadRoutineToRam(void *destination)
Copies the byte reader to RAM and installs its Thumb entry point.
Definition flash.c:161
Flash memory: erasing, programming and reading sectors.
The flash chips: sector size and count, and each chip type's routines.
u16(*) FlashWaitProc(u8 timingIndex, volatile u8 *address, u8 expected)
Definition flash_data.h:9
struct FlashSetupInfo gFlash_ChipAtmel
struct FlashSetupInfo gFlash_ChipAtmelLayout
The hardware's I/O registers and their bits, as pret names them.
#define REG_WAITCNT
Definition io_reg.h:307
#define REG_IME
Definition io_reg.h:308
Settings for each flash chip model (44 bytes), in flash_setup's field order from flashset....
Definition flash_data.h:29
struct FlashType type
Definition flash_data.h:33
struct FlashSector sector
Definition flash_data.h:22
u16 wait[2]
Low two bits of REG_WAITCNT.
Definition flash_data.h:23
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11