I write in C language serial bootloader of PIC32 single-chip microcomputer

Source: Internet
Author: User

Since December 15, my spare time has been tossing the PIC32MZ EC Starter Kit purchased at the end of 15. and successively wrote more than 10 essays documenting the course of my toss. Recently completed by the C language to achieve the PIC32 UART bootloader, the use of serial communication, suitable for Microchip PIC32MZ EC, the corresponding modification can also be applied to PIC32MX, PIC32MZ EF and so on. The Uart bootloader is compiled with XC32, and the computer-side communication software is--hyperterminal with HyperTerminal (also using SECURECRT). and similar to what I wrote before, Hyperbootloader_pic16/pic18/dspic is one of the family series. In order to guarantee the family identity, I'm called HYPERBOOTLOADER_PIC32.

BOOTLOADER PLACEMENT

The HYPERBOOTLOADER_PIC32 sequence is placed in the boot flash memory as shown in.

  BOOTLOADER LINKER SCRIPT

We cannot use the default linker script because we need to place bootloader in a specific location. We need to modify linker script to place the bootloader, the following is the main part of the modification.

_RESET_ADDR =0xbfc00000; _bev_excpt_addr=0xbfc00380; _dbg_excpt_addr=0xbfc00480; _simple_tlb_refill_excpt_addr= _ebase_address +0; _cache_err_excpt_addr= _ebase_address +0x100; _gen_excpt_addr= _ebase_address +0x180;/************************************************************************* * Memory Regions * * Memory Regions Without attributes cannot is used for orphaned sections. * Only sections specifically assigned to these regions can is allocated * into these regions. * * The Debug exception vector is located at 0x9fc00480. * * The config_<address> sections is used to locate the config words at * their absolute addresses. *************************************************************************/memory{/*Bootloader user code:only in the Lower Boot Alias*/kseg0_program_mem (Rx): ORIGIN=0x9fc02000, LENGTH =0xdf00/*Lower Boot Alias Last (5th) Page:not used*/kseg0_lba_last_page (Rx): ORIGIN=0x9fc10000, LENGTH =0x4000/*Interrupt Vector Table*/Kseg0_boot_mem:origin=0x9fc01000, LENGTH =0x1000/*Reset and C Startup module*/Kseg1_boot_mem:origin=0xbfc00000, LENGTH =0x480/*Cache TLB Initialization Table*/Kseg1_boot_mem_4b0:origin=0xbfc004b0, LENGTH =0xb50

  BOOTLOADER MAIN CODE

After the modified linker script is added to the Bootloader project, followed by bootloader coding, the following is the main part of bootloader code

        if(rowreceived) { for(ix =0; IX < Incrbytes; ix++) {UART_PUTC (Framebuffer[ix]); } UART_PUTC ('\ n'); Cksum= Bcount =Getxbyte (Framebuffer[len_nibble1_index],framebuffer[len_nibble2_index]); sourceaddr.v[1] =Getxbyte (Framebuffer[addrh_nibble1_index],framebuffer[addrh_nibble2_index]); sourceaddr.v[0] =Getxbyte (Framebuffer[addrl_nibble1_index],framebuffer[addrl_nibble2_index]); RecType=Getxbyte (Framebuffer[type_nibble1_index],framebuffer[type_nibble2_index]); Switch(rectype) { Caselinear_address:sourceaddr.v[3] = Getxbyte (framebuffer[type_nibble2_index+1],framebuffer[type_nibble2_index+2]); sourceaddr.v[2] = Getxbyte (framebuffer[type_nibble2_index+3],framebuffer[type_nibble2_index+4]);                Checksum ();  Break;  CaseDATA:if(SourceAddr.word.HW = =0x1fc0)) Break; if(Sourceaddr.val >= (eraseaddr.val+page_size)) {Eraseaddr.val= (sourceaddr.val/page_size) *page_size;                Nvmerasepage (Eraseaddr.val); } rowreceived=0;  for(ix=0; IX <2*Bcount;) {PData.byte. LB = Getxbyte (framebuffer[type_nibble2_index+1+ix+0],framebuffer[type_nibble2_index+1+ix+1]); PData.byte. HB = Getxbyte (framebuffer[type_nibble2_index+1+ix+2],framebuffer[type_nibble2_index+1+ix+3]); PData.byte. UB = Getxbyte (framebuffer[type_nibble2_index+1+ix+4],framebuffer[type_nibble2_index+1+ix+5]); PData.byte. MB = Getxbyte (framebuffer[type_nibble2_index+1+ix+6],framebuffer[type_nibble2_index+1+ix+7]); unsignedintError =Nvmwriteword (Sourceaddr.val, pdata.val); if(Error &0x3000) >0) UART_PUTC ('N');//For DebugError =0; Sourceaddr.val+=4; IX+=8; if(rowreceived) UART_PUTC ('X');//For Debug} Checksum (); //buf_init ();                 Break;  Caseend:checksum (); U1mode=0x0; U1sta=0x0; Triscset=0x2000;/*RC13 Input*/LATCCLR=0x6000;/*Clear LATC*/IPC28CLR=0x1f00;/*Clear IPC28*/Iec3bits.u1rxie=0;/*Clear IEC3*/                (*((void(*) (void)) (user_app_reset_address))) ;  Break; }            //rowreceived = 0;Incrbytes =0; }

Bootloader compile successfully, use programmer to burn to target, then unplug programmer. Also later again the renewal of the programmer did not need programmer again.

  Application LINKER SCRIPT

Bootloader and Appication are also placed in flash memory, both of which are required to ensure that they cannot be overlap. So application need to avoid bootloader, so need to change application linker script. The following is the main part of the modification.

_RESET_ADDR =0x9d001000; _simple_tlb_refill_excpt_addr= _ebase_address +0; _cache_err_excpt_addr= _ebase_address +0x100; _gen_excpt_addr= _ebase_address +0x180;/************************************************************************* * Memory Regions * * Memory Regions Without attributes cannot is used for orphaned sections. * Only sections specifically assigned to these regions can is allocated * into these regions. * * The Debug exception vector is located at 0x9fc00480. * The config_<address> sections is used to locate the config words at * their absolute addresses. *************************************************************************/memory{Kseg1_boot_mem:origin=0x9d001000, LENGTH =0x480kseg0_program_mem (Rx): ORIGIN=0x9d000000+0x1000+0x480, LENGTH =0x200000- (0x1000+0x480)/*All C files would be located here*/Kseg0_boot_mem:origin=0x9d000000, LENGTH =0x0Kseg0_data_mem (w!X): ORIGIN =0x80000000, LENGTH =0x80000Sfrs:origin=0xbf800000, LENGTH =0x100000Kseg2_ebi_data_mem:origin=0xC0000000, LENGTH =0x4000000Kseg2_sqi_data_mem:origin=0xd0000000, LENGTH =0x4000000Kseg3_ebi_data_mem:origin=0xe0000000, LENGTH =0x4000000Kseg3_sqi_data_mem:origin=0xf0000000, LENGTH =0x4000000}

  How to use HYPERBOOTLOADER_PIC32

1. compile Hyperbootloader_pic32 with XC32 ( you need to modify linker script before compiling).

  2. use Pickit3 to burn the HYPERBOOTLOADER_PIC32 hex file to the target board.

  3. Unplug the pickit3 burner, connect the target board with the PC's serial port, open HyperTerminal, set as follows:115200-8-none-1-none, line Delay- 20ms.

  4. Restart the target board, the HyperTerminal will appear booting ... Words.

  5. within 6 seconds, open the Send Text file dialog box and select the application hex file that you want to burn (before compiling, you need to modify linker script), click Confirm, Hyperbootloader will transmit the received data back to the computer HyperTerminal and burn the data to the correct location on the target Board program memory.

  6. after burning, automatically jumps to the application again.

Each time you update the application, just repeat steps 4 ~ 6.

  NOTE

Hyperbootloader does not support configuration bits burning, requires application configuration bits and HYPERBOOTLOADER_PIC32 configuration BITS remains consistent.

I write in C language serial bootloader of PIC32 single-chip microcomputer

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.