1 #include <system_def.h>
3 #include <hal_machperiph.h>
5 #define IAP_PLL_FULL_SPEED 1
10 #define IAP_CMD_PREPARE 50
11 #define IAP_CMD_WRITE 51
12 #define IAP_CMD_ERASE 52
13 #define IAP_CMD_READ_PARTID 54
18 extern void iap_asm_entry (unsigned int *,unsigned int *);
19 #define iap_entry iap_asm_entry
22 inline int addr2sec(unsigned long addr)
26 #elif defined INC_LPC214x_H || defined INC_LPC2348_H
27 inline int addr2sec(unsigned long addr)
29 if (addr<0x8000) return (addr>>12);
30 else if (addr<0x78000) return (addr>>15)+7;
31 else return 22+((addr&0x7fff)>>12);
34 #error "Undefined type of CPU for function addr2sec!"
37 int lpcisp_read_partid()
39 command[0] = IAP_CMD_READ_PARTID;
40 iap_entry(command, result);
44 int lpcisp_prepare_sectors(unsigned char start, unsigned char end)
46 command[0] = IAP_CMD_PREPARE;
49 command[3] = FOSC/1000;
51 iap_entry(command, result);
53 return (CMD_SUCCESS == *result);
56 int lpcisp_erase_sectors(unsigned char start, unsigned char end)
58 command[0] = IAP_CMD_ERASE;
61 command[3] = FOSC/1000;
63 iap_entry(command, result);
65 return (CMD_SUCCESS == *result);
68 int lpcisp_erase(void *addr, int len)
73 start=addr2sec((unsigned long)addr);
74 end=addr2sec((unsigned long)addr+len-1);
76 if (end<start) return 0;
79 #ifndef IAP_PLL_FULL_SPEED
83 lpcisp_prepare_sectors(start,end);
84 if (CMD_SUCCESS != *result) return 0;
86 lpcisp_erase_sectors(start,end);
88 #ifndef IAP_PLL_FULL_SPEED
93 return (CMD_SUCCESS == *result);
96 int lpcisp_write(void *addr_des, const void *addr_src, int len)
101 start=addr2sec((unsigned long)addr_des);
105 #ifndef IAP_PLL_FULL_SPEED
109 lpcisp_prepare_sectors(start,end);
110 if (CMD_SUCCESS != *result) return 0;
112 command[0] = IAP_CMD_WRITE;
113 command[1] = (unsigned int)addr_des;
114 command[2] = (unsigned int)addr_src;
116 command[4] = FOSC/1000;
118 iap_entry(command, result);
120 #ifndef IAP_PLL_FULL_SPEED
123 restore_flags(flags);
125 return (CMD_SUCCESS == *result);