/***************************************************************************
    filename:            : Usage 
    discreption          : RLX Probe GDB script file example for SSI SPI flash driver 
                           
                             -------------------
    created              : May 20 2009
    copyright            : (C) 2009 by Realtek Co.
    email                : yf_chen@realsil.com.cn
    ext.                 : 6134
***************************************************************************/
1. Setting your parameters in rlx_probe0.cfg of your RLX Probe Driver.
2. Modify target file name/target IP/flash driver name in gdb_flash_driver.txt.
   Please use cygwin or linux format for file name path.
   Set the flash controller fifo size to "$s0".
   Set the maximum memory buffer size to "$max_mem_size" .
   Set your file size (check it with hex editor) to "$file_size".
3. If your SPI flash controller address isn't equal to 0xb4000000 , please modify the 
"#define  SSI_BASE       0xb4000000" 
   in rlx_ep_fw_spi_flash.S according to your spec of chip.
   If you don't want to program flash begin from the start address ,please modify
"#define  SPI_WR_BASE    0x0"
   in rlx_ep_fw_spi_flash.S according to your spec of chip.
  Then , make clean and make all.
4. If the SPI flash base address isn't equal to 0x81000000 , please modify it 
   in gdb_flash_driver.txt (set $spi_addr = ).
5. If the driver(to be loaded to memory)target base address isn't 0x80200000 ,please modify link script in the directory and make all.
6. run RLX Probe Driver .
7. run rsdk-elf-gdb .
8. source gdb_flash_driver.txt in GDB console.

Then the GDB will load rlx_ep_fw_spi_flash.exe or rlx_ep_fw_spi_flash_le.exe(little endian)
and restore data in your target file ,then continue to program your data to SPI flash.
The script file and driver support only SSI spi flash controller and have passed 
test on Max and Spansion flash. 

潫gdb_flash_driver.txtϸ˵

1. Ϊgdbʾ趨
#set height to 0 to avoid "return" press needed for scroll
set height 0
set width 0

2. Ϊ趨flash driverļһǿִеΪGDB loadmemory

#set file name
file rlx_ep_fw_spi_flash_el.exe

3. ΪʹGDBRLX Probe DriverעTCP portIPصĻIP

#connection
target remote 172.29.17.34:26120

4. load ղָflash drivermemory

#load your file to memory
load

5.²ΪرһЩGDBʾ⾭ԵֹͣȴϢ֮Ķ

#disable all messages
set verbose off
set complaints 0
set confirm off
set exec-done-display off
#set debug aix-thread off
#set debug dwarf2-die 0
#set debug displaced off
set debug event 0
set debug expression 0
set debug frame 0
#set debug infrun off
set debug observer 0
set debug overload 0
set debugvarobj 0
set pagination off
#set remote memory-write-packet-size fixed
#set remote memory-write-packet-size 3000
#set download-write-size 3000

6. 趨ҪprogramSPI FLASHfile size

#set the file size
set $file_size = 0xcf5d4 

7.趨ʹõmemoryռ䣬Աbinaryļtargetϵ memoryУ

#set the memory size
set $max_mem_size = 32 * 1024 

8. 趨flash driverĴС

#set the data size to max memory size subtract 2k SIZE(program size)
set $prog_size = 2048

9.ʹõmemoryռȥflash driverĴСõԴbinary(Ҫ¼firmwareС

ȻҲԼ򵥴ֱԼַռ䣬õmemoryСɣҪ㡣
set $size =  $max_mem_size - $prog_size

10.Ҫٴloopprogram

#set loop times
set $loop_num = $file_size / $size

11.Ƿзmemoryռļ֣Աprogramһ

#set left data
set $left_size = $file_size % $size
printf "left size = %d\n",$left_size

12.趨bufferĴС

#set buffer size
set $buf_size = $size

13.趨ҪprogramݴŵƫƵַ

#set offset of buffer
set $buf_off = $prog_size

14. ʵprogramݴŵַ

#set the start address of memory buffer
#for example : 0x80200800
set $buf_addr = $pc + $buf_off

15. 趨flash controller fifoС

#set the flash controller fifo size
set $s0 = 0x80

16. 趨binary(programݣļʼַ

#set buffer start offset
set $start_addr = 0

17.趨binary(programݣļĽַһprogramĴС

#set end address of memory buffer
set $end_addr = $start_addr + $buf_size

18. 趨SPI Flashmemory mapַSPI register ʼַ

#set SPI flash address #0x81000000
set $spi_addr = 0x81000000

19. ҪرȽʱ趨صʼַַ

#set dump address
set $dump_start_addr = $spi_addr
set $dump_end_addr = $spi_addr + $buf_size - 1

20.趨CPUregister t4ΪʼַΪflash driverʹ

#set registers to be used in your file
#buffer start address
set $t4 = $pc + $buf_off
set $t4_buf = $pc + $buf_off
 21.趨CPUregister gpΪprogramĴСΪflash driverʹ
#set registers to be used in your file
set $gp = $size

22.趨t3ΪSPI programĻַʵûʹãΪflash driverli t3, SPI_WR_BASEֱ趨

#set the SPI program base address
set $t3 = 0

#set file name variable
#set $file_name = "rlx_ep_spi_test.bin"

#set file check name variable
#set $file_chk_name "rlx_ep_spi_test.chk"

23. ֹһЩGDBĴӡϢ

disable display

24. ӡһЩϢ

  printf "erase SPI flash !\n"

25. flash driverprogramλ趨һϵ㣬ԱprogramһͣٴPCµݽmemory
  
  b   debug_spi_fast_wr_end

26. whileѭֱprogramΪֹ

while $loop_num > 0

  #set breakpoint to symbol in your file

 #commands

#silent
#execute loop

27. PC˵ļһְᵽϵmemory

  #read data to memory from a file to be loaded to SPI flash
  restore rlx_ep_spi_test.bin binary $buf_addr-$start_addr $start_addr $end_addr
  28. ʼflash driver
  #continue 
  c
  29 . 趨gp Ϊflash driverпƻregister
  #p /x $s0
  #set the data size to be programed to spi flash
  set $gp = $size
  #p /x $t3 = $t3 + $buf_size
  #p /x $t3

30. 趨t4ַ
  set $t4 = $t4_buf
  31. 趨ļַ
  #set $buf_addr = $buf_addr  - $start_addr
  set $start_addr = $start_addr + $buf_size
  set $end_addr = $start_addr + $buf_size
  #printf "start_addr = 0x%x , end_addr = 0x%x\n" , $start_addr, $end_addr
 
  #set $dump_start_addr = $dump_start_addr + $buf_size
  #set $dump_end_addr = $dump_start_addr + $buf_size - 1
  32. loop count
  #decrease the counter 
  set $loop_num = $loop_num - 1
  p  $loop_num
  #printf "$start_addr = 0x%x\n",$start_addr
  #p /x *($start_addr | 0x80000000)


end

  #set breakpoint to symbol in your file
  #b   debug_spi_fast_wr_end

33 .軹вһݣprogram it

  # program the left data (not aligned to 30k)
  set $end_addr = $start_addr + $left_size
  printf "start_addr = 0x%x , end_addr = 0x%x\n" , $start_addr, $end_addr
  restore rlx_ep_spi_test.bin binary $buf_addr-$start_addr $start_addr $end_addr
  set $gp = $left_size
  set $t4 = $t4_buf
 
  #continue 
  c

  #printf your message 
  printf "spi flash success !\n"
   
  #printf your message 
  #set remotetimeout 1000
  #set $end_addr = $start_addr + $left_size

34. ʼӰmemoryspi flash) dumpݵPC˵ļУԱȶ
  set $dump_end_addr = $spi_addr + $end_addr
  printf "dump data to file for check from address 0x%x to address 0x%x...\n", $spi_addr , $dump_end_addr
   
  dump binary memory  rlx_ep_spi_test.chk $spi_addr $dump_end_addr
   
  printf "dump data finished ! please verify your data with hex compare software .\n"
  #quit from GDB
  #quit
  #confirm for GDB input
  #y