1. 程式人生 > >自己用C語言寫RL78 serial bootloader

自己用C語言寫RL78 serial bootloader

      瞭解更多關於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。