####################################
# J-LINK GDB SERVER initialization #
####################################
define InitJlink
printf "Jlink Init:\n"
set verbose off
set complaints 0
set confirm off
set exec-done-display off
show exec-done-display
set trace-commands off
#set debug aix-thread off
#set debug dwarf2-die 0
set debug displaced off 
set debug expression 0
set debug frame 0
set debug infrun 0
set debug observer 0
set debug overload 0
#set debugvarobj 0
set pagination off
set print address off
set print symbol-filename off
set print symbol off
set print pretty off
set print object off
#set debug notification off
set debug parser off
set debug remote 0
target remote localhost:2331
set remotetimeout 10000
monitor device Cortex-M3
monitor endian little
monitor reset
# Set max speed
monitor speed 4000
set mem inaccessible-by-default off
# Setup GDB FOR FASTER DOWNLOADS
set remote memory-write-packet-size 8192
set remote memory-write-packet-size fixed
set $SPI_FLASH_BASE = 0x98000000
end
#############
# Boot_Flash
define SetBootFlash
printf "SetBoot = Flash:\n"
monitor long 0x40000210 = 0x00011113
end
# Boot RAM start_addr0() Run if ( v400001F4 & 0x8000000 ) && ( v40000210 & 0x80000000 ) 
define SetBootCall0
printf "SetBoot = Call0:\n"
monitor long 0x40000210 = 0x80011113
end
# Boot RAM start_addr1() Run if ( v40000210 & 0x20000000 ) 
define SetBootCall1
printf "SetBoot = Call1:\n"
monitor long 0x40000210 = 0x20011113
end
# Boot RAM start_addr2() Run if ( v40000210 & 0x10000000 )
define SetBootCall2
printf "SetBoot = Call2:\n"
monitor long 0x40000210 = 0x10011113
end
# Boot RAM start_addr3() Run if ( v400001F4 & 0x8000000 ) && ( v40000210 & 0x8000000 )
define SetBootCall3
printf "SetBoot = Call3:\n"
monitor long 0x40000210 = 0x8011113
end
# Boot RAM start_addr4() Init console, Run if ( v40000210 & 0x4000000 )
define SetBootCall4
printf "SetBoot = Call4:\n"
monitor long 0x40000210 = 0x4011113
end
# CPU CLK 166 MHz?
define SetClk166MHz
printf "SetCLK 166.66MHz:\n"
monitor long 0x40000014 = 0x00000011 
end
# CPU CLK 83 MHz?
define SetClk83MHz
printf "SetCLK 83.33MHz:\n"
monitor long 0x40000014 = 0x00000021 
end
###############
# System Init #
###############
define SystemInit
printf "System Init:\n"
monitor long 0x40000304 = 0x1FC00002
monitor long 0x40000250 = 0x400
monitor long 0x40000340 = 0x0
monitor long 0x40000230 = 0xdcc4
monitor long 0x40000210 = 0x11113
monitor long 0x400002c0 = 0x110011
monitor long 0x40000320 = 0xffffffff
end
############
# SPI Init #
############
define SPI_Init
printf "Init SPI:\n"
#enable spi flash peripheral clock
set $Temp = {int}(0x40000230)
set $Temp = ($Temp | 0x300)
set {int}(0x40000230) = $Temp
#enable spi flash peripheral
set $Temp = {int}(0x40000210)
set $Temp = ($Temp | 0x10)
set {int}(0x40000210) = $Temp
#select spi flash pinout (0 - internal), enable spi flash pins 
set $Temp = {int}(0x400002C0)
set $Temp = (($Temp & 0xFFFFFFF8) | 1)
set {int}(0x400002C0) = $Temp
#disable SPI FLASH operation
monitor long 0x40006008 = 0
#disable all interrupts
monitor long 0x4000602C = 0
#use first "slave select" pin
monitor long 0x40006010 = 1
#baud rate, default value
monitor long 0x40006014 = 2
#tx fifo threshold
monitor long 0x40006018 = 0
#rx fifo threshold
monitor long 0x4000601C = 0
#disable DMA
monitor long 0x4000604C = 0
end
###################
# SetFirwareSize #
###################
define SetFirwareSize
set $rambuffer = 0x10000300
printf "Get ImagesSize:\n"
restore $arg0 binary $rambuffer 0 0x20
set $Image1Size = {int}($rambuffer+0x10) + 32
set $Image1LoadAddr = {int}($rambuffer+0x14)
set $Image2Addr = {short}($rambuffer+0x18) * 1024
if $Image1Size != 0 && $Image1Size < 0x1000000
 if $Image2Addr == 0 
  set $Image2Addr = $Image1Size
 end 
 printf "Image1Size = %d\n", $Image1Size
 printf "Image1LoadAddr = 0x%08x\n", $Image1LoadAddr
 printf "Image2FlashAddr = 0x%08x\n", $Image2Addr
 set $parms1 = $rambuffer - $Image2Addr
 set $parms3 = $Image2Addr + 0x08
 restore $arg0 binary $parms1 $Image2Addr $parms3
 set $Image2Size = {int}($rambuffer)
 set $Image2LoadAddr = {int}($rambuffer+0x4)
 if $Image2Size != 0xFFFFFFFF && $Image2Size != 0
  set $Image2Size = $Image2Size + 16
  printf "Image2Size = %d\n", $Image2Size
  printf "Image2LoadAddr = 0x%08x\n", $Image2LoadAddr
  set $FirmwareSize = $Image2Addr + $Image2Size
  set $Image3Addr = $FirmwareSize 
  set $parms1 = $rambuffer - $FirmwareSize
  set $parms3 = $Image3Addr + 0x08
  restore $arg0 binary $parms1 $Image3Addr $parms3
  set $Image3Size = {int}($rambuffer)
  set $Image3LoadAddr = {int}($rambuffer+0x4)
  if $Image3Size != 0xFFFFFFFF && $Image3Size != 0
   set $Image3Size = $Image3Size + 16
   printf "Image3Size = %d\n", $Image3Size
   printf "Image3LoadAddr = 0x%08x\n", $Image3LoadAddr
   set $FirmwareSize = $Image3Addr + $Image3Size
   printf "FirmwareSize = %d\n", $FirmwareSize
  else
   set $Image3Size = 0
   if $Image3LoadAddr == 0x30000000
    set $FirmwareSize = $FirmwareSize + 8
   end
   printf "Image3 - None\n"
   printf "FirmwareSize = %d\n", $FirmwareSize
  end
 else
  set $Image2Size = 0
  printf "Image2 - None\n"
  set $FirmwareSize = $Image1Size
  printf "FirmwareSize = %d\n", $FirmwareSize
 end
else
 set $Image1Size = 0
 set $Image2Size = 0
 set $Image3Size = 0
 set $Image2Addr = 0
 set $Image3Addr = 0
 set $FirmwareSize = 0
 printf "Image not format Firmware!\n"
end 
end
#####################
# Flash Images Info #
#####################
define FlashImagesInfo
set $Image2Size = 0
set $Image3Size = 0
set $Image2Addr = 0
set $Image3Addr = 0
printf "Flash Info:\n"
set $Image1Size = {int}($SPI_FLASH_BASE + 0x10) + 32
set $Image1LoadAddr = {int}($SPI_FLASH_BASE + 0x14)
if $Image1LoadAddr == 0xFFFFFFFF
printf "Image1 - None\n"
else
set $Image2FlashAddr = {short}($SPI_FLASH_BASE + 0x18) * 1024
if $Image2FlashAddr == 0 
set $Image2FlashAddr = $Image1Size 
end 
set $Image2Size = {int}($Image2FlashAddr + $SPI_FLASH_BASE)
set $Image2LoadAddr = {int}($Image2FlashAddr + $SPI_FLASH_BASE + 0x4)
printf "Image1Size = %d\n", $Image1Size
printf "Image1LoadAddr = 0x%08x\n", $Image1LoadAddr
printf "Image2FlashAddr = 0x%08x\n", $Image2FlashAddr
if $Image2Size != 0xFFFFFFFF && $Image2Size != 0
printf "Image2Size = %d\n", $Image2Size
printf "Image2LoadAddr = 0x%08x\n", $Image2LoadAddr
set $Image3FlashAddr = $Image2FlashAddr + $Image2Size + 0x10
set $Image3Size = {int}($Image3FlashAddr + $SPI_FLASH_BASE)
set $Image3LoadAddr = {int}($Image3FlashAddr + $SPI_FLASH_BASE + 0x4)
if $Image3Size != 0xFFFFFFFF && $Image3Size !=0
printf "Image3FlashAddr = 0x%08x\n", $Image3FlashAddr
printf "Image3Size = %d\n", $Image3Size
printf "Image3LoadAddr = 0x%08x\n", $Image3LoadAddr
else
printf "Image3 - None\n"
end
else
printf "Image2 - None\n"
end
end
end
