1 #include <system_def.h>
3 #include <hal_machperiph.h>
8 #define IAP_CMD_PREPARE 50
9 #define IAP_CMD_WRITE 51
10 #define IAP_CMD_ERASE 52
11 #define IAP_CMD_READ_PARTID 54
13 unsigned int command[5];
14 unsigned int result[2];
16 extern void iap_asm_entry (unsigned int *,unsigned int *);
17 #define iap_entry iap_asm_entry
20 inline int addr2sec(unsigned long addr)
24 #elif defined INC_LPC214x_H || defined INC_LPC2348_H
25 inline int addr2sec(unsigned long addr)
27 if (addr<0x8000) return (addr>>12);
28 else if (addr<0x78000) return (addr>>15)+7;
29 else return 22+((addr&0x7fff)>>12);
32 #error "Undefined type of CPU for function addr2sec!"
35 int lpcisp_read_partid()
37 command[0] = IAP_CMD_READ_PARTID;
38 iap_entry(command, result);
42 int lpcisp_prepare_sectors(unsigned char start, unsigned char end)
44 command[0] = IAP_CMD_PREPARE;
47 command[3] = system_frequency/1000;
49 iap_entry(command, result);
51 return (CMD_SUCCESS == *result);
54 int lpcisp_erase_sectors(unsigned char start, unsigned char end)
56 command[0] = IAP_CMD_ERASE;
59 command[3] = system_frequency/1000;
61 iap_entry(command, result);
63 return (CMD_SUCCESS == *result);
66 int lpcisp_erase(void *addr, int len)
71 start=addr2sec((unsigned long)addr);
72 end=addr2sec((unsigned long)addr+len-1);
74 if (end<start) return 0;
78 lpcisp_prepare_sectors(start,end);
79 if (CMD_SUCCESS != *result) return 0;
81 lpcisp_erase_sectors(start,end);
85 return (CMD_SUCCESS == *result);
88 int lpcisp_write(void *addr_des, const void *addr_src, int len)
93 start=addr2sec((unsigned long)addr_des);
98 lpcisp_prepare_sectors(start,end);
99 if (CMD_SUCCESS != *result) return 0;
101 command[0] = IAP_CMD_WRITE;
102 command[1] = (unsigned int)addr_des;
103 command[2] = (unsigned int)addr_src;
105 command[4] = system_frequency/1000;
107 iap_entry(command, result);
109 restore_flags(flags);
111 return (CMD_SUCCESS == *result);