bootloader/iap.c

138 lines
3.5 KiB
C
Raw Normal View History

2022-05-19 08:57:38 -04:00
/*
===============================================================================
Name : iap.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include "iap.h"
2022-05-28 13:30:55 -04:00
unsigned int command[5];
unsigned int output[5];
2022-05-19 08:57:38 -04:00
IAP iap_entry =(IAP) IAP_LOCATION;
2022-05-28 13:30:55 -04:00
int addr_to_sector(int addr){
int tmp = addr & 0xfffff;
tmp = tmp >> 12;
if(tmp <= 15) return tmp;
else{
if(tmp >= 0x10 && tmp <=0x17)
return 16;
else if(tmp >= 0x18 && tmp <= 0x1f)
return 17;
else if(tmp >= 0x20 && tmp <= 0x27)
return 18;
else if(tmp >= 0x28 && tmp <= 0x2f)
return 19;
else if(tmp >= 0x30 && tmp <= 0x37)
return 20;
else if(tmp >= 0x38 && tmp <= 0x3f)
return 21;
else if(tmp >= 0x40 && tmp <= 0x47)
return 22;
else if(tmp >= 0x48 && tmp <= 0x4f)
return 23;
else if(tmp >= 0x50 && tmp <= 0x57) return 24;
else if(tmp >= 0x58 && tmp <= 0x3f) return 25;
else if(tmp >= 0x60 && tmp <= 0x67) return 26;
else if(tmp >= 0x68 && tmp <= 0x6f) return 27;
else if(tmp >= 0x70 && tmp <= 0x77) return 28;
else if(tmp >= 0x78 && tmp <= 0x7f) return 29;
}
}
2022-05-19 08:57:38 -04:00
void iap_entry_wrapped(void){
__disable_irq();
iap_entry(command, output);
__enable_irq();
}
void iap_prepare_sectors(error_code* status, int start, int end){
command[0] = 50;
command[1] = start;
command[2] = end;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
*status = 0;
}else{
2022-05-24 08:53:35 -04:00
*status = output[0];
2022-05-19 08:57:38 -04:00
}
}
void iap_erase_sectors(error_code* status, int start, int end){
iap_prepare_sectors(status, start, end);
command[0] = 52; //erase sectors
command[1] = start; //start sector
command[2] = end; //end sector
command[3] = IAP_CLOCK_100M;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
}else{
2022-05-24 08:53:35 -04:00
*status = output[0];
2022-05-19 08:57:38 -04:00
}
}
uint32_t iap_read_part_id(error_code* status){
//iap_prepare_sectors(dst_flash, dst_flash+num_bytes-1);
2022-05-19 15:37:51 -04:00
iap_prepare_sectors(status, 2, 10);
2022-05-19 08:57:38 -04:00
command[0] = 54;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
*status = 0;
return output[1];
}else{
*status = -1;
return -1;
}
}
2022-05-28 13:30:55 -04:00
void iap_copy_to_flash(error_code* status, long unsigned int * dst_flash, long unsigned int* src_ram, int num_bytes){
iap_prepare_sectors(status, addr_to_sector(dst_flash), addr_to_sector(dst_flash+num_bytes));
2022-05-19 08:57:38 -04:00
command[0] = 51;
command[1] = dst_flash;
2022-05-22 17:44:17 -04:00
command[2] = src_ram;
2022-05-19 08:57:38 -04:00
command[3] = num_bytes;
2022-05-24 08:53:35 -04:00
command[4] = IAP_CLOCK_100M;
2022-05-19 08:57:38 -04:00
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
*status = 0;
}else{
2022-05-22 17:44:17 -04:00
*status = output[0];
2022-05-19 08:57:38 -04:00
}
}
void iap_read_serial(error_code* status, uint32_t* res){
2022-05-19 15:37:51 -04:00
//iap_prepare_sectors(status, 2, 9);
2022-05-19 08:57:38 -04:00
command[0] = 58;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
*status = 0;
res[0] = output[1];
res[1] = output[2];
res[2] = output[3];
res[3] = output[4];
}else{
2022-05-28 13:30:55 -04:00
*status = output[0];
2022-05-19 08:57:38 -04:00
}
}
// TODO: insert other include files here
// TODO: insert other definitions and declarations here
/*
int main(void) {
LPC_PINCON->PINSEL4 &= ~0xFF; //Configure the PORT2 Pins as GPIO;
LPC_GPIO2->FIODIR = 0xff; //Configure the PORT2 pins as OUTPUT;
iap_read_part_id();
iap_erase_sectors(2, 9);
iap_copy_to_flash(0x8000,(uint8_t*)test, 4);
}*/