; --------------------------------------------------------------------------------
; @Title: Flash programming script using the Hard Reset Configuration Work (HRCW)
; @Description:
;   Program the hard reset configuration word (HRCW) to flash
;   using flash programming through boundary scan on the Freescale MPC8272ADS
;   evaluation board
; @Author: rweiss
; @Board: MPC8272ADS
; @Chip: MPC8272, MPC8271, MPC8248, MPC8247
; @Copyright: (C) 1989-2015 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: flashprog_hrcw_using_boundaryscan.cmm 12093 2018-02-08 16:42:15Z rweiss $


;debugger setup
  SYStem.RESet
  SYStem.CPU MPC8272
  SYStem.BdmClock 10.MHz
  
;check if BSDL file is available
  LOCAL &bsdlfile

  ; use pq27e_reva.bsdl for MASK 0K50M (CPU revision A)

  PRINT "Plese select BSDL file..."
  DIALOG.FILE "pq27e_rev?.bsdl"
  ENTRY %LINE &bsdlfile

  IF "&bsdlfile"==""
  (
    DIALOG.OK "Get BSDL file from nxp.com (pq27e_reva.bsdl or pq27e_rev0.bsdl, depending on processor version)"
    ENDDO
  )

;set up BSDL support
  BSDL.RESet
  BSDL.FILE "&bsdlfile"
  
;initialize JTAG  
  JTAG.LOCK
  BSDL.HARDRESET
  BSDL.SOFTRESET

  IF !BSDL.CHECK.BYPASS()
  (
    DIALOG.OK "Problem with JTAG communication."
    GOTO end
  )
  IF !BSDL.CHECK.IDCODE()
  (  
    DIALOG.OK "Detected incompatible processor."
    GOTO end
  )

;set up BSDL flash interface
  BSDL.FLASH.IFDefine RESet
  BSDL.FLASH.IFDefine NOR 1. 30. 32.       ;device 1, 30 address pins, 32 data pins
  
;control pin assignments  
  BSDL.FLASH.IFMap CE   CS_B(0)            ;chip select of NOR flash
  
  BSDL.FLASH.IFMap OE   OE_B_SDRAS_B_GPL2  ;output enable
  BSDL.FLASH.IFMap OE2  BCTL0_B            ;this signal is used as /OE for external buffers
  
  BSDL.FLASH.IFMap WE   WE_DQM_BS_B(0)     ;byte write enables
  BSDL.FLASH.IFMap WE2  WE_DQM_BS_B(1)     ;byte write enables
  BSDL.FLASH.IFMap WE3  WE_DQM_BS_B(2)     ;byte write enables
  BSDL.FLASH.IFMap WE4  WE_DQM_BS_B(3)     ;byte write enables
  //BSDL.FLASH.IFMap ALE  ALE_IRQ4_B       ;address latch enable (not required on EVB)
 
;address pin assignments
  BSDL.FLASH.IFMap A29  A(0)
  BSDL.FLASH.IFMap A28  A(1)
  BSDL.FLASH.IFMap A27  A(2)
  BSDL.FLASH.IFMap A26  A(3)
  BSDL.FLASH.IFMap A25  A(4)
  BSDL.FLASH.IFMap A24  A(5)
  BSDL.FLASH.IFMap A23  A(6)
  BSDL.FLASH.IFMap A22  A(7)
  BSDL.FLASH.IFMap A21  A(8)
  BSDL.FLASH.IFMap A20  A(9)
  BSDL.FLASH.IFMap A19  A(10)
  BSDL.FLASH.IFMap A18  A(11)
  BSDL.FLASH.IFMap A17  A(12)
  BSDL.FLASH.IFMap A16  A(13)
  BSDL.FLASH.IFMap A15  A(14)
  BSDL.FLASH.IFMap A14  A(15)
  BSDL.FLASH.IFMap A13  A(16)
  BSDL.FLASH.IFMap A12  A(17)
  BSDL.FLASH.IFMap A11  A(18)
  BSDL.FLASH.IFMap A10  A(19)
  BSDL.FLASH.IFMap A9   A(20)
  BSDL.FLASH.IFMap A8   A(21)
  BSDL.FLASH.IFMap A7   A(22)
  BSDL.FLASH.IFMap A6   A(23)
  BSDL.FLASH.IFMap A5   A(24)
  BSDL.FLASH.IFMap A4   A(25)
  BSDL.FLASH.IFMap A3   A(26)
  BSDL.FLASH.IFMap A2   A(27)
  BSDL.FLASH.IFMap A1   A(28)
  BSDL.FLASH.IFMap A0   A(29)
  
;data pin assignments
  BSDL.FLASH.IFMap DQ31 D(0)
  BSDL.FLASH.IFMap DQ30 D(1)
  BSDL.FLASH.IFMap DQ29 D(2)
  BSDL.FLASH.IFMap DQ28 D(3)
  BSDL.FLASH.IFMap DQ27 D(4)
  BSDL.FLASH.IFMap DQ26 D(5)
  BSDL.FLASH.IFMap DQ25 D(6)
  BSDL.FLASH.IFMap DQ24 D(7)
  BSDL.FLASH.IFMap DQ23 D(8)
  BSDL.FLASH.IFMap DQ22 D(9)
  BSDL.FLASH.IFMap DQ21 D(10)
  BSDL.FLASH.IFMap DQ20 D(11)
  BSDL.FLASH.IFMap DQ19 D(12)
  BSDL.FLASH.IFMap DQ18 D(13)
  BSDL.FLASH.IFMap DQ17 D(14)
  BSDL.FLASH.IFMap DQ16 D(15)
  BSDL.FLASH.IFMap DQ15 D(16)
  BSDL.FLASH.IFMap DQ14 D(17)
  BSDL.FLASH.IFMap DQ13 D(18)
  BSDL.FLASH.IFMap DQ12 D(19)
  BSDL.FLASH.IFMap DQ11 D(20)
  BSDL.FLASH.IFMap DQ10 D(21)
  BSDL.FLASH.IFMap DQ9  D(22)
  BSDL.FLASH.IFMap DQ8  D(23)
  BSDL.FLASH.IFMap DQ7  D(24)
  BSDL.FLASH.IFMap DQ6  D(25)
  BSDL.FLASH.IFMap DQ5  D(26)
  BSDL.FLASH.IFMap DQ4  D(27)
  BSDL.FLASH.IFMap DQ3  D(28)
  BSDL.FLASH.IFMap DQ2  D(29)
  BSDL.FLASH.IFMap DQ1  D(30)
  BSDL.FLASH.IFMap DQ0  D(31)

;initialize boundary scan chain
  BSDL.FLASH.INIT SAFE

;static signals: CS1..11 and WE4..7  
  BSDL.SET PORT CS_B(1) 1
  BSDL.SET PORT CS_B(2) 1
  BSDL.SET PORT CS_B(3) 1
  BSDL.SET PORT CS_B(4) 1
  BSDL.SET PORT CS_B(5) 1
  BSDL.SET PORT CS6_BCTL1_SMI_B 1
  BSDL.SET PORT CS7_TLBISYNC_B  1
  BSDL.SET PORT WE_DQM_BS_B(4) 1
  BSDL.SET PORT WE_DQM_BS_B(5) 1
  BSDL.SET PORT WE_DQM_BS_B(6) 1
  BSDL.SET PORT WE_DQM_BS_B(7) 1

;set up FLASH programming
  FLASH.RESet
  FLASH.BSDLaccess ON
  FLASH.Create 1. 0++0x7fffff 0x40000 I28F001B Long
  
;enable read access to FLASH (using boundary scan)
  FLASH.AUTO ALL

;show FLASH declaration
  WinPOS 0.42857 0.25 63. 34. 23. 1. W000
  FLASH.List

;show contents of first flash sector
  WinPOS 69.0 0.25 78. 25. 13. 1. W001
  Data.dump 0x00000000 /Long

  
  DIALOG.YESNO "The Data.dump window shows the current contents of the flash." "The data is read using boundary scan." "Do you want to program the HRCW to flash now?"
  LOCAL &yes
  ENTRY &yes

  IF !&yes
  (
    PRINT "Aborted."
    GOTO end
  )

;assign the HRCW bytes to the macros &byte0..&byte3
  LOCAL &byte0 &byte1 &byte2 &byte3
  &byte0=0x0C
  &byte1=0xB2
  &byte2=0x36
  &byte3=0x45

;unlock and erase first flash sector
  FLASH.UNLOCK  0--0x0003FFFF
  FLASH.Erase   0--0x0003FFFF

;enable flash programming  
  FLASH.Program 0--0x0003FFFF

;write data to flash (4 * 64 bits) 
  Data.Set 0x00++0x07 %Long (0x01010101*&byte0)
  Data.Set 0x08++0x07 %Long (0x01010101*&byte1)
  Data.Set 0x10++0x07 %Long (0x01010101*&byte2)
  Data.Set 0x18++0x07 %Long (0x01010101*&byte3)
  
;disable flash programming  
  FLASH.Program OFF

;enable read access to FLASH (using boundary scan) again to check flash contents
  FLASH.AUTO ALL
  
  ( ;command block with local error handler (only valid inside block)
    ON ERROR GOTO FLASH_FAILED
    Data.Set 0x00++0x07 %Long (0x01010101*&byte0) /ComPare
    Data.Set 0x08++0x07 %Long (0x01010101*&byte1) /ComPare
    Data.Set 0x10++0x07 %Long (0x01010101*&byte2) /ComPare
    Data.Set 0x18++0x07 %Long (0x01010101*&byte3) /ComPare
  )
  
  DIALOG.OK "Flash programming of HRCW successful."
  GOTO end
  
FLASH_FAILED:
  DIALOG.OK "Flash programming failed."
  
end:
  FLASH.AUTO off
  JTAG.UNLOCK
ENDDO

