了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序)。
之前用C实现过多款PIC单片机的bootloader, 包括8-bit的PIC16,PIC18;16-bit的PIC24, dsPIC30, dsPIC33; 和32-bit的PIC32MX,PIC32MZ,我为这些类型PIC都写过C bootloader。今天要介绍的serial bootloader 是为我的RL78单片机学习板而写。RL78是Renesas的16-bit单片机。这是我第一次为Renesas的单片机写C 语言bootloader,遇到不少问题,通过不懈努力,总算成功的完成了我的第一个Renesas RL78 serial bootloader。我将这个RL78 serial bootloader命名为hyperboot_rl78, 因为hyperboot_rl78用到的上位机是hyperterm (超级终端)。写这篇博文的目的是来总结这次hyperboot_rl78的实现过程,总结内容包括bootloader的设计, bootloader的逻辑,bootloader的使用,实现环境,实现过程中遇到的问题,以及如何解决的。以上都会一一在这篇博文中介绍到。
首先介绍实现环境,IDE是CS+, C编译器是CC-RL, 硬件板是我的学习板, 详细如下:
IDE : CS+ for CC V7.00.00 [13 Jun 2018]
Compiler: CC-RL V1.07.00
MCU: RL78 F14 R5F10PPJ
Hardware: QB-R5F10PPJ-TB(e)
我的bootloader和application的memory map设计如下:
bootloader code range: 0x0000~0x33FF
application code range: 0x3400~0x3FFFF
application reset vector remap:0x4000
interrupt vector remap: 0x4006~0x40CA
bootloader 不使用interrupt,interrupt vector 区间0x4~0x7C remap到application的code区间0x4006~0x40CA。
bootloader code range设置, bootloader interrupt remap 都需要通过修改CS+ bootloader工程的Linker Option来实现。具体可以查阅CS+的帮助。
application code range设置, application reset vector remap 也需要修改CS+ application工程的Linker Option来实现。
另外,interrupt vector remap 到application code区间0x4006~0x40CA, application使用中断的话,需要在代码中建Juamp Table, 并将interrupt service routine的地址映射到Jump Table中。 我的做法是在application CS+ 工程的Linker Option中新增 .app_vector section, .app_vector section的地址范围0x4000~0x40CA(包括reset vector 和 interrupt vector).
Jump Table指定到.app_vetor section, 在Jump Table 添加跳转指令和_start 地址(cstartup for reset vector)或interrupt service routine的地址(for interrupt vector)到相对应的位置中。Jump Table我是用CC-RL的汇编指令实现的,为此我查阅了RL78的Software Manual。Jump Table 中的汇编代码例子如下:
.DB _BR_ ;BR .DB2 _r_uart0_interrupt_send ;0x16, Interrupt Service Routine
上面的bootloader 和application的Linker Option设置及Jump Table的实现费时最长,主要是对CS+项目的设置不熟, 对CC-RL的使用也不熟,查帮助,一步一步试错尝试,当所有的设置都OK了,Jump Table也实现了,我才长舒一口气,感觉本bootloader的实现即将完成。
虽然一切bootloader 和 application的设置都OK了,也感觉bootloader的实现即将完成。但实际上并没有那么顺利,原因是bootloader 需要用到FSL, FSL是操作flash的library. 需要添加FSL的头文件和library到bootloader的代码工程中。仔细的查阅了FSL 的介绍文档(r01us0050ej0103_rl78.pdf),发现FSL需要我们根据需求配置FCD, RCD, BCD等section 到bootloader的ROM 空间或RAM空间。 由于我只使用到FSL的基本功能,并且采用status check internal mode. 所以我将FCD,RCD, BCD配置到bootloader 的ROM 空间, 当然也是通过修改bootloader的Linker Option来完成这项配置。
bootloader 的配置完善了,接着就是bootloader功能和逻辑的实现了。 bootloader 的上位机是串口终端hyperterm(超级终端)。 hyperterm的配置(串口配置)如下:
baud rate: 19200
data bytes: 8
parity: none
stop bits: 1
flow control : none
line delay: 150ms
上电后, bootloader 初始化uart (baudrate-data bytes-parity-stop bits-flow control 要和hyperterm的配置一致,19200-8-none-1-none, 否则通信不了)。bootloader 然后每隔1S左右打印一个 '.' , 打印完6个'.' (6S)就跳转到application. 如果要更新程序,就在6S内在hyperterm 窗口中敲一回车。bootloader 收到回车(代表握手成功)就开始擦除application code range. 打印 “Erasing *********" 每擦除一个block (1024 bytes) 就增加打印一个 ”*“直到所有application blocks都擦除完毕,并打印”Done“(代表擦除成功)。
hyperterm 窗口显示了bootloader 完成Erase后打印的”Done“,就操作hyperterm采用发送文本的方式发送application 的hex文件,一行接一行的发送,每发送一行就暂停150ms (配置hyperterm参数line delay = 150ms).
bootloader 每接收一行,原样打印,并解读里面的数据类型,地址,长度,数据,和Checksum, 如果数据类型为S1或S2,就将接收到的数据烧写到对应的地址。直到最后一行也处理完毕。整个烧写过程都没问题就打印”Program Rewritten Complete“ “(代表更新成功)。并跳转到application。 如果烧写过程有问题就打印”Program Rewritten Complete with Error“。
以上就是bootloader的更新程序的整个逻辑。代码框架如下:
void M_Bootloader_MainFunc(void) { switch (bootState) { case BOOT_HANDSHAKE: M_Bootloader_Handshake(); break; case BOOT_ERASE: M_Bootloader_Erase(); break; case BOOT_RECEIVE: M_Bootloader_Receive(); break; case BOOT_PROGRAM: M_Bootloader_Write(); break; case BOOT_VERIFY: M_Bootloader_IVerify(); break; case BOOT_PREJUMP: Prejump_To_Application(); break; case BOOT_JUMPTO_APP: Jump_To_Application(); break; default: break; } }
整个过程, hyperterm中打印出来的内容就是更新流程log. 下面是一个完整的更新流程的log.
....BOOT Mode Erasing************************************************************************* ***********************************************Done S00E0000726C3738463134706D6F7439 S1130000D040FFFF064009400C400F40124015400D S113001018401B401E402140244027402A402D40C8 S113002030403340364039403C403F4042404540F8 S113003048404B404E405140544057405A405D4028 S113004060406340664069407C407F408240854018 S113005088408B408E409140944097409A409D4008 S1130060A040A340A640A940AC40AF40B240B54038 S1130070B840BB40BE40C140C440C740CA400 S10700C077FFE0855D S10D00C4000000000000000000002E S10D00CEFFFFFFFFFFFFFFFFFFFF2E S113340048656C6C6F20776F726C640D0A000D0A4E S109341064000D0A6C00CB S1134000EDD040ED4C42ED4C42ED4C42ED4C42EDD6 S11340104C42ED4C42ED4C42ED4C42ED4C42ED4CE9 S113402042EDA841ED2341ED4C42ED4C42EDC64139 S1134030ED4C42ED4C42ED4C42ED4C42ED4C42ED28 S11340404C42ED4C42ED4C42ED4C42ED4C42ED4CB9 S113405042ED4C42ED4C42ED4C42ED4C42ED4C42B3 S1134060ED4C42ED4C42ED4C42ED4C42ED4C42EDF8 S11340704C42ED4C42ED4C42ED4C42ED4C42ED4C89 S113408042ED4C42ED4C42ED4C42 S1134090ED4C42ED4C42ED4C42ED4C42ED4C42EDC8 S11340A04C42ED4C42ED4C42ED4C42ED4C42ED4C59 S11040B042ED4C42ED4C42ED4C42ED4C42D1 S11340D0CBF820FEFC4B43003600B43025B4EF048B S11340E0CC0000A747DFF93620FE3020FEEF04CCD9 S11340F00000A747DFF94100320000EF08B311497F S113410062454800B4F643DFF44100320000EF0892 S1134110B3114962454820FEF643DFF4FC954200A2 S1134120F6EFFEC1C3C5C78EFD708EFCC1C7AF02DA S113413001605C0798008800318EBF06018800D1B9 S113414061E888008D129801AF16B44214B461D3AB S113415045EB12B4880199A212B4A214B4AF16B4F8 S11341604214B4DF338F24B40CBCDD1B0CF8DD081F S11341700CE8DD134C08DF205A067F3203003012AE S113418034FCEB4400EF116A0680320300300E3435 S1134190FCEB4400EF028801712BE1C6C09EFC6079 S11341A09EFDC6C4C2C061FCC1C5AF10B46168DD68 S11341B00EEB0EB4899D10A515BF0EB4B210B471E8 S11341C01BE1C4C061FCC1C3C5C78EFD708EFCC1B8 S11341D0E6140218B412F61661D8A1021AB4BF1A72 S11341E0B413BF18B415021CB4121761D8A1021E6F S11341F0B4BF1EB413BF1CB4AF18B4DB1AB436007A S113420000340A00CEFB0B001705DF0661C8FC1B57 S11342104500AF1CB4DB1EB436000034F401CEFB01 S11342200B001705DF0561C87A0640AF22B4AF2042 S1134230B460310308E73024B4FCD34400715BE17B S1134240C09EFC609EFDC6C4C2C061FC00EFFD615F S1044250FC6D S113425116C80300AEF82761D339315E12F643DD87 S113426127BBBC02BC04BC06BC08BC0ABC0CBC0E0B S1134271BC10BC12BC14BC16BC18BC1ABC1CBC1EA1 S11342813704200037B3EFD6AEF847DD06F6BBA7F7 S1134291A7EFF5D7FE0200EFFE2004FC8F4400FCDB S11342A1464500717AFA5A06BFF6B802B800A80268 S11342B1440000A80061F8446400DE1300A802145D S11342C1A800040100121561D8A1B80213EFDD3270 S11342D10E00300034FCEB44001004D7 S11342E104CA0000CA0100CA0200CA0300CA0400C9 S11342F1CA0500CA0600CA0700CA0800FC504300E8 S1134301FC7B4300FC0A4400FC114400FC144500FE S1134311FC1F450034780015CA000004180016F685 S11343217182B802B800A802440000A80061F844F0 S11343310A00DE1300A80214A8000401001215618A S1134341D8A1B80213EFDD1004D7717BFAEF8D36D3 S1134351C40234C402718215A116CEA000717AA1DF S1134361714BA4CA030071A336C402716AA1716BB3 S1134371A4710BA1CA04C271A3D7CF0AB418F50B57 S1134381B4D7BF02B413BF04B415BF06B4CF08B485 S1134391043002B4FC7D45009F00B44CFFDF0800EB S11343A100FCDE4500EFF1D716400CB4015110DFDB S11343B11617BF02B413BF04B415BF06B4F508B48D S11343C13002B4FC7D45009F00B4D7400CB401DF3A S11343D11EBF02B4CF08B4033002B4FC7D45009F74 S11343E100B44CFFDF080000FCDE4500EFF1D7CF3D S11343F100B4105110D7300AB4FC624500E50CB486 S1134401D7FCE44500F50CB4D7CD06C0CE263FD782 S113441136F00030330071A200000000BC26EF002A S1134421347000AAA4086C0308BAA4711AE5711BBC S1134431E1712AE5712BE1711AED711AE9712AED25 S1134441712AE9302280BA98309780BA9CC9100049 S11344519A89718A99300700BA96302201BA9A3042 S11344619740BA9EC912009AAAA8086C0108BAA872 S1134471AAB0BAB0AAAA086C0108BAAA51406F211D S1134481FF9E216A012051DF5F21FF9E21D7341253 S113449101AA06086C0108BA06AA08086C0108BA40 S11344A108A9086C0308B9711BE1712BE1711BE5C3 S11344B1712BE5D7341401711AE5712AE5A9086C49 S11344C10308B9AA06085CFE08BA06711BE1712B40 S11344D1E1D714136168DF04308100D7F6BF14B447 S11344E113BF16B415BF12B4F6D714136168DF04F1 S11344F1308100D736E5FF15BF0EB413BF10B47178 S113450192EB0EB4899D10A515BF0EB4B210B4710F S113451193F6D7710AE4710BE0D7CEABACD7304038 S1134521021404800016718289718B99715AE571A4 S11345315BE1715AED715AE9CA0200CA010030BF48 S11345415DBFF006D7344002715BE1715BE58971AF S11345518A99D734400289718B99715AE5715BE16B S1044561D77E S11345627100900016BF04088C019F0108E503083E S1134572F2FCF8FF0E62CF800804D716E503088C1C S113458202088C03BF0408340010CF02080F8C0603 S11345929F81084C03DD2415080E00081E01BF008C S11345A208148C04088C05BF06081251004E06DD5F S11345B21BF98108FCF8FF0E62EF20300004BF06ED S11345C2088C003129F005BF0008EFE5FB04080060 S11345D2899BA7A5B3F643DFF75100D7FC04F00E7D S11145E262D7FC08F00E71089000F58008D72F S21403FF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9 S21403FF10FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9 S21403FF20FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD9 S21403FF30FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC9 S21403FF40FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB9 S21403FF50FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA9 S21403FF60FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF99 S21403FF70FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF89 S21403FF80FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF79 S21403FF90FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF69 S21403FFA0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF59 S21403FFB0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF49 S21403FFC0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF39 S21403FFD0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF29 S21403FFE0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF19 S21403FFF0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF09 S9030000FC Program Rewritten Complete Hello world l d
hyperboot_rl78 有以下两个缺点:
1。效率低, 每行delay 150ms
2。和上位机hyperterm(超级终端)之间交互没有通信协定。
接下来计划完成一个全新RL78 bootloader,一个上位机,之间通信采用自定义协议, 或Xmodem协议,开发平台为CS+ 或 IAR。