LPC1768IAP (detailed, there is the upper machine)

Source: Internet
Author: User

Before said Stm32 's IAP programming, today the weather is really good, handy to say lpc1788 's IAP programming (do not look in front of see the STM note content)

The first is the flash algorithm, lpc1768 and there is no register to let us operate flash, he built the IAP Flash algorithm, in the Technical Manual on page 525 has the following instructions

  

Its supported IAP commands have these

So we can make the relevant flash reading and writing excuses (see lpc1768 's Technical Manual for details)

Unsigned param_table[5];//passing parameter list

Unsigned result_table[5];//returns a list of results

Invoke the IAP command

void Iap_entry (unsigned param_tab[],unsigned result_tab[])

{

void (*IAP) (unsigned [],unsigned []);

IAP = (void (*) (unsigned [],unsigned [])) iap_address;

IAP (Param_tab,result_tab);

}

By this means you can invoke the IAP command, and we'll show you a command

Sectors ready for instructions.

Start sector area code end sector area code system clock

void Prepare_sector (unsigned start_sector,unsigned end_sector,unsigned cclk)

{

Param_table[0] = Prepare_sector_for_write;

PARAM_TABLE[1] = Start_sector;

PARAM_TABLE[2] = End_sector;

PARAM_TABLE[3] = CCLK;

Iap_entry (param_table,result_table);

}

This directive must be called before writing Flash and erasing flash

Specific full flash code please see the project file, will be uploaded at the end of the article

And then it's still five instructions.

"Iap_down"

"Iap_jump_app"

"Iap_over"

"Iap_set_flag"

"Iap_clear_flag"

The function is similar to the previous stm32, but the download algorithm changes because the STM32 supports writing a 16-bit data each time, and lpc1768 writes 8 bits of data each time, and each time the amount of data written is 128/256/512/1024/4096, It's not exactly the 2048 we used before, so the algorithm changes to look like this

U8 iapbuf[1024] = {0}; Array for caching data

U16 receivedatacur = 0; The length of the data already populated in the current iapbuffer, once filled in and then written to Flash and zeroed

U32 addrcur = flash_app1_addr; Current system write address, address increased by 2048 after each write

#define VU32 volatile unsigned int

Start download

void iap_down_s (void)

{

U16 i = 0;

U16 Receivecount;

if (Erase_user_flash ())

{

printf ("error\r\n");

Return

}

printf ("begin,wait data download\r\n");

Receivemode = 1;//serial port enters the download receive data mode

while (1)

{

Loop to receive data, each time must send 128 data down, if not 128, indicating that this is the last packet of data

After receiving a packet of data, return a decimal points, send the completion, the system is programmed to complete the return of a iap_over

if (Serial_buffer_length & 0x8000)

{

Receivecount = (U8) (SERIAL_BUFFER_LENGTH&0X00FF);

if (Receivecount = = 128)//satisfies a packet, fills and checks for 1024 bytes, with Write Flash

{

for (i = 0; i < Receivecount; i++)

{

Iapbuf[receivedatacur] = Serial_buffer[i];

receivedatacur++ after completion of receivedatacur++;//;

}

Receiveexpectcount = 0;//Clears the desired receive mode

Serial_buffer_length = 0;//Clear serial port full flag

printf ("."); /one point at a time to receive data

The Receivedatacur value needs to be detected at this time, and if it is full, it needs to be written

if (receivedatacur = = 1024)

{

Write in Flash

if (Write_flash (100000,addrcur, (unsigned*) iapbuf,1024))

{

Receivemode = 0;

Addrcur = flash_app1_addr;

receivedatacur = 0;

Return

}

Addrcur + = 1024;//address +2048

After writing, Receivedatacur to clear 0 wait for the next transmission

receivedatacur = 0;

}

else//It is possible that the last packet has 128 data but eventually there is no 2048 data, at which point an extension of one instruction is used to complete the last write

{

}

It's not full yet, wait for the next data.

}

else//does not satisfy a packet, indicating data transfer This is the last packet, written to Flash

{

No package is to be transferred to the cache.

for (i = 0; i < Receivecount; i++)

{

Iapbuf[receivedatacur] = Serial_buffer[i];

receivedatacur++ after completion of receivedatacur++;//;

}

Receiveexpectcount = 0;//Clears the desired receive mode

Serial_buffer_length = 0;//Clear serial port full flag

printf ("."); /one point at a time to receive data

To turn data that is not fully received into 0xFF

for (i= receivedatacur; i < 1024x768; i++)

{

Iapbuf[i] = 0xFF;

}

This data is then written to flash memory.

if (Write_flash (100000,addrcur, (unsigned*) iapbuf,1024))

{

Receivemode = 0;

Addrcur = flash_app1_addr;

receivedatacur = 0;

Return

}

printf ("\r\nwrite addr%x,length%d\r\n", addrcur,receivedatacur);

When you're done, return the address to the original location.

Addrcur = flash_app1_addr;

receivedatacur = 0;

After writing to exit the download cycle and tell the host computer, has been downloaded

printf ("Download over\r\n");

Also, exit the download cycle mode

Receivemode = 0;

Return

}

Because lpc1768 is a more unique sector, the following

We want to modify the definition of the address, and now the app code can not be stored in the 0x08002000 location, but in the location of the 0x00003000, we have 123 sectors as the IAP Code Storage area, and the last bit of the 2nd sector is defined as the application of the location of the cure mark exists, The function to write the Cure flag is as follows

unsigned char iapconfigbuffer[4096];//a sector 4K

Failed to return 1 successful return 0

U8 Iap_write_config_value (U8 Value)

{

U32 i = 0;

U8 *p;

p = (u8*) Sector_2_start;

The first thing to do is to read all the data from the third sector into RAM.

for (i = 0; i < 4096; i++)

{

Iapconfigbuffer[i] = *p;

p++;

}

Then check that the last data is equal to the data we want to set

if (iapconfigbuffer[4095] = = value)//equal without setting the

{

return 0;

}

Else

{

Erase the 2nd sector first

Prepare_sector (2,2,100000);

Erase_sector (2,2,100000);

if (result_table[0]! = cmd_success)

{

Return 1;//Erase failed

}

Sets the last element of the array to the specified value

IAPCONFIGBUFFER[4095] = value;

Prepare_sector (2,2,100000);//Find out which sector should be prepared according to the address

Write_data (100000,sector_2_start, (unsigned*) iapconfigbuffer,4096);

if (result_table[0]! = cmd_success)

{

return 1;

}

return 0;

}

}

To erase a sector, you must save the sector content, so you must define an array that can hold 4096 of data, read it first, erase it, and prevent data loss.

Flash storage address changed to this

#define APP_CONFIG_ADDR 0X00002FFF//configuration address

#define App_config_set_value 0X55//Set Value

#define App_config_clear_value 0XFF//Clear 0 value

#define FLASH_APP1_ADDR 0x00003000//First application start address (stored in FLASH)

Reserved space for IAP use

The corresponding configuration will be changed to the following

Of course, the same process as before in the main function

And for apps, the place to change is these

And this in the system initialization function Systeminit.

Modified into

#define Vect_tab_offset 0x3000

This basically can be completed, suddenly found that the app is not compiled, because the Startup_lpc17xx.s file in the 110 lines there is a paragraph

The assembly code in the flash location of the 0X000002FC store a 0xffffffff, but the app from 0x00003000 start, is unable to operate 0X000002FC Flash, we have to see what this location is stored

Can see, this paragraph is flash read protection, this paragraph we can comment out, because the IAP part of the boot code we did not change Ah, IAP code has been set CRP, that IAP Guide app as long as not to modify, CRP is we defined in the IAP, rest assured bold comment out.

Now we're a little different than a stm32-iap,lpc1788 IAP.

    1. Flash Erase algorithm has changed
    2. The program's storage space has changed.
    3. The position of the interrupt vector table has changed.
    4. CRP's not Stm32, it's a new thing.
    5. Because Flash's erase algorithm has changed, the way we receive data processing data at the bottom has changed

In summary, the agreement has not changed, we can still follow the previous agreement to use, then, the previous computer is still available

Yes, the host computer needs to download the bin file, you need to convert axf to bin file in Keil, set the following

Target's user interface

Notice Ah, this is my project distribution, I put an output directory in the directory of the project file, AXF generated into the output directory, if you do not have an output directory, then the output is deleted, set the location of the build AXF and the location of the project file is in the same directory can be

http://download.csdn.net/detail/dengrengong/8499921

LPC1768IAP (detailed, there is the upper machine)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.