-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflash.c
41 lines (32 loc) · 811 Bytes
/
flash.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
#include "flash.h"
enum {
readyBit = 0x80,
vppErrorBit = 0x08,
programErrorBit = 0x10,
protectedBlockErrorBit = 0x02
};
enum {
reset = 0xff, program = 0x40
};
int convertStatusToResult(int status)
{
if (status == readyBit)
return FLASH_SUCCESS;
io_write(0, reset);
if (status & vppErrorBit)
return FLASH_VPP_ERROR;
else if (status & programErrorBit)
return FLASH_PROGRAM_ERROR;
else if (status & protectedBlockErrorBit)
return FLASH_PROTECTED_BLOCK_ERROR;
return FLASH_INVALID_STATUS_ERROR;
}
int flash_program(io_address addr, io_data data)
{
int status = 0;
io_write(0, program);
io_write(addr, data);
while ((status & readyBit) == 0)
status = io_read(0);
return convertStatusToResult(status);
}