; --------------------------------------------------------------------------------
; @Title: Flash declaration for Zhixin Z20K internal flash
; @Description:
;   Script arguments:
;     DO Z20K [PREPAREONLY] [CPU=<cpu>]
;       PREPAREONLY    only declares flash but does not execute flash programming
;       CPU=<cpu>      selects CPU derivative <cpu>
;   Example:
;     DO Z20K PREPAREONLY
; @Chip: Z20K11xM/Z20K14xM
; @copyright : Copyright (c) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.

; --------------------------------------------------------------------------------
LOCAL &parameters &param_prepareonly &param_cpu
ENTRY %LINE &parameters
&param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)
&param_cpu=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"CPU=","")

; --------------------------------------------------------------------------------
; Start debugging

IF !SYStem.Up()
(
  SYStem.RESet

  IF "&param_cpu"!=""
  (
    IF "&param_cpu"=="Z20K114M"||"&param_cpu"=="Z20K116M"||"&param_cpu"=="Z20K118M"
      SYStem.CPU CortexM0+
    ELSE IF "&param_cpu"=="Z20K144M"||"&param_cpu"=="Z20K146M"||"&param_cpu"=="Z20K148M"
      SYStem.CPU CortexM4
    ELSE
    (
      PRINT %ERROR "Device is not supported!"
      ENDDO
    )
  )
  ELSE
  (
    PRINT %ERROR "Enter correct value!"
    ENDDO
  )

  SYStem.CONFIG.DEBUGPORTTYPE SWD

  ;IF &param_masserase
  ;  GOSUB MassErase
)

; Disable watchdog
GOSUB DisableWatchdog

; --------------------------------------------------------------------------------
; Flash declaration
FLASH.RESet
GOSUB FlashDeclaration

FLASH.Erase ALL

; Flash script ends here if called with parameter PREPAREONLY
IF &param_prepareonly
  ENDDO PREPAREDONE

ENDDO

; --------------------------------------------------------------------------------
DisableWatchdog:
(
  LOCAL &tmp1 &tmp2
  &tmp1=Data.Long(ST:0x20000000)
  &tmp2=Data.Long(ST:0x20000004)
  Register.SWAP
  Data.Assemble ST:0x20000000  str  r1,[r0]
  Data.Assemble ,              str  r3,[r2]
  Data.Assemble ,              str  r5,[r4]
  Data.Assemble ,              bkpt #0
  Register.Set PC 0x20000000
  Register.Set SP 0x20001000
  Register.Set R0 0x40052014
  Register.Set R1 0xB0D9A1C4
  Register.Set R2 0x40052014
  Register.Set R3 0x1A1E3B0F
  Register.Set R4 0x40052008
  Register.Set R5 0x01040040
  Go.direct
  WAIT !STATE.RUN()

  Data.Set ST:0x20000000 %Long &tmp1
  Data.Set ST:0x20000004 %Long &tmp2
  Register.SWAP

  RETURN
)


; --------------------------------------------------------------------------------
; Flash declaration depending on selected CPU
FlashDeclaration:
(
  PRIVATE &FlashDriver &PFlashSize &IFRFlashSize &PFlashSectorSize &IFRFlashSectorSize &DFlashSize &DFlashSectorSize
  
  &DFlashSize=0x00
  
  IF "&param_cpu"=="Z20K114M"
  (
    &FlashDriver="Z20K114M_FlashDriver.bin"
    &PFlashSize=0x10000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000
  )
  ELSE IF "&param_cpu"=="Z20K116M"
  (
    &FlashDriver="Z20K116M_FlashDriver.bin"
    &PFlashSize=0x20000
    &DFlashSize=0x20000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &DFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000
  )
  ELSE IF "&param_cpu"=="Z20K118M"
  (
    &FlashDriver="Z20K118M_FlashDriver.bin"
    &PFlashSize=0x40000
    &DFlashSize=0x20000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &DFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000
  )
  ELSE IF "&param_cpu"=="Z20K144M"
  (
    &FlashDriver="Z20K144M_FlashDriver.bin"
    &PFlashSize=0x80000
    &DFlashSize=0x20000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &DFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000

  )
  ELSE IF "&param_cpu"=="Z20K146M"
  (
    &FlashDriver="Z20K146M_FlashDriver.bin"
    &PFlashSize=0x100000
    &DFlashSize=0x20000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &DFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000

  )
  ELSE IF "&param_cpu"=="Z20K148M"
  (
    &FlashDriver="Z20K148M_FlashDriver.bin"
    &PFlashSize=0x200000
    &DFlashSize=0x20000
    &IFRFlashSize=0x8000
    &PFlashSectorSize=0x2000
    &DFlashSectorSize=0x2000
    &IFRFlashSectorSize=0x2000

  )
  ELSE
  (
    PRINT %ERROR "Device is not supported!"
    ENDDO
  )

  FLASH.Create 1. 0x00000000++(&PFlashSize-0x1) &PFlashSectorSize TARGET Long

  IF &DFlashSize!=0x00
    FLASH.Create 2. 0x01000000++(&DFlashSize-0x1) &DFlashSectorSize TARGET Long
  
  FLASH.Create 3. 0x02000000++(&IFRFlashSize-0x1) &IFRFlashSectorSize TARGET Long

  IF "&param_cpu"=="Z20K114M"
    FLASH.TARGET 0x20000000 0x20001000 0xEE0 ~~~~/&param_cpu/&FlashDriver
  ELSE
    FLASH.TARGET 0x20000000 0x20001000 0x1000 ~~~~/&param_cpu/&FlashDriver

  RETURN
)
