• Visit https://www.embeddedcomputers.net/ for Hardware; Software and all other things related to FlashcatUSB

Ambit256 Script SPI

Status
Not open for further replies.

D3m0n

Administrator
Staff member
# bcusb requires 3.3v for ISP mode to work

MyDevice = "Ambit256" #Name of device
Imgfile = "images\Ambit256-ISP.jpg" #Name of image

FwBase = 0x10000
FWSize = 983040
Fw1Base = 0x100000
Fw1Size = 983040
Boot-CfgBase = 0x0
Boot-CfgSize = 65536
LogBase = 0x10000
LogSize = 65536

JTAG.MemoryType("SPI")
SPIFLASH = JTAG.MemoryInit()

t1 = Tab.Create(MyDevice)
t2 = Tab.Create(Imgfile)


Tab(t1).AddGroup("Boot-Cfg",10,10,100,150)
Tab(t1).AddGroup("Firmware",110,10,100,150)
Tab(t1).AddGroup("Firmware1",210,10,100,150)
Tab(t1).AddGroup("Log",310,10,100,150)
Tab(t1).AddButton("ReadBoot-Cfg","Read",20,42)
Tab(t1).AddButton("WriteBoot-Cfg","Write",20,102)
Tab(t1).AddButton("ReadFirmware","Read",120,42)
Tab(t1).AddButton("WriteFirmware","Write",120,102)
Tab(t1).AddButton("ReadFirmware1","Read",220,42)
Tab(t1).AddButton("WriteFirmware1","Write",220,102)
Tab(t1).AddButton("ReadLog","Read",320,42)
Tab(t1).AddButton("WriteLog","Write",320,102)
Tab(t2).AddImage("LOGO",ImgFile,80,40)
Tab(t1).AddProgress(20,270,404) #Adds a progress bar (updates automatically from any function)


CreateEvent(ReadBoot-Cfg)
Status("Reading the Boot-Cfg")
Tab(t1).ButtonDisable()
MyData = Memory(SPIFLASH).ReadVerify(Boot-CfgBase,Boot-CfgSize)
if (MyData = Nothing)
Status("Error: data read back failed")
Tab(t1).ButtonEnable()
Exit Event
endif
Prompt = "Choose filename to save the firmware"
SaveFile(MyData,Prompt,"Boot-cfg.bin")
Status("Successfully read Boot-Cfg from Flash")
Tab(t1).ButtonEnable()
EndEvent


CreateEvent(WriteBoot-Cfg)
Tab(t1).ButtonDisable()
Prompt = "Choose a Boot-Cfg to write into Flash"
MyData = OpenFile(Prompt,"Boot-Cfg files (*.bin)|*.bin")
if (MyData = Nothing)
goto WriteBoot-CfgExit
endif
if not (Len(MyData) = Boot-CfgSize)
Status("Error: File is not the size of the Boot")
goto WriteBoot-CfgExit
endif
Memory(SPIFLASH).Write(MyData,Boot-CfgBase,Boot-CfgSize)
Status("New Boot-Cfg successfully written")
WriteBoot-CfgExit:
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(ReadFirmware)
Status("Saving the Ambit256's Firmware")
Tab(t1).ButtonDisable()
MyData = Memory(SPIFLASH).ReadVerify(FwBase,FwSize)
if (MyData = Nothing)
Status("Error: data read back failed")
Tab(t1).ButtonEnable()
Exit Event
endif
Prompt = "Choose filename to save the firmware"
SaveFile(MyData,Prompt,"Ambit256.bin")
Status("Successfully read firmware from Flash")
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(WriteFirmware)
Status("Programming the Ambit256's Firmware")
Tab(t1).ButtonDisable()
Prompt = "Choose a firmware to install"
MyData = OpenFile(Prompt,"Firmware files (*.bin,*.p7)|*.bin;*.p7")
if (MyData = Nothing)
WriteErr = "User cancelled opperation"
goto ExitWriteFwErr
endif
If (HWORD(MyData,0) = 12418) #Remove header if .p7 fw
Writeline("Removing .p7 firmware header")
HeadLen = HWORD(MyData,2) + 52 #increases Headlen by 7
NewLen = Len(MyData) - HeadLen
Resize(MyData,HeadLen,NewLen) #Removes the p7 header

endif
FwLen = Len(MyData)
Memory(SPIFLASH).Write(MyData,FwBase,FwLen)
FwSize = FwLen
Status("New firmware successfully installed")
Tab(t1).ButtonEnable()
Exit
ExitWriteFwErr:
Status(WriteErr)
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(ReadFirmware1)
Status("Saving the Ambit256's Firmware")
Tab(t1).ButtonDisable()
MyData = Memory(SPIFLASH).ReadVerify(Fw1Base,Fw1Size)
if (MyData = Nothing)
Status("Error: data read back failed")
Tab(t1).ButtonEnable()
Exit Event
endif
Prompt = "Choose filename to save the firmware"
SaveFile(MyData,Prompt,"Ambit256_1.bin")
Status("Successfully read firmware1 from Flash")
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(WriteFirmware1)
Status("Programming the Ambit256's Firmware")
Tab(t1).ButtonDisable()
Prompt = "Choose a firmware to install"
MyData = OpenFile(Prompt,"Firmware files (*.bin,*.p7)|*.bin;*.p7")
if (MyData = Nothing)
WriteErr = "User cancelled opperation"
goto ExitWriteFwErr
endif
If (HWORD(MyData,0) = 12418) #Remove header if .p7 fw
Writeline("Removing .p7 firmware header")
HeadLen = HWORD(MyData,2) + 52 #increases Headlen by 7
NewLen = Len(MyData) - HeadLen
Resize(MyData,HeadLen,NewLen) #Removes the p7 header
endif
FwLen = Len(MyData)
Memory(SPIFLASH).Write(MyData,Fw1Base,FwLen)
Fw1Size = FwLen
Status("New firmware successfully installed")
Tab(t1).ButtonEnable()
Exit
ExitWriteFwErr:
Status(WriteErr)
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(ReadLog)
Status("Reading the Log")
Tab(t1).ButtonDisable()
MyData = Memory(SPIFLASH).ReadVerify(LogBase,LogSize)
if (MyData = Nothing)
Status("Error: data read back failed")
Tab(t1).ButtonEnable()
Exit Event
endif
Prompt = "Choose filename to save the firmware"
SaveFile(MyData,Prompt,"Log.bin")
Status("Successfully read Log from Flash")
Tab(t1).ButtonEnable()
EndEvent

CreateEvent(WriteLog)
Tab(t1).ButtonDisable()
Prompt = "Choose a Log to write into Flash"
MyData = OpenFile(Prompt,"Log files (*.bin)|*.bin")
if (MyData = Nothing)
goto WriteLogExit
endif
if not (Len(MyData) = LogSize)
Status("Error: File is not the size of the Log")
goto WriteLogExit
endif
Memory(SPIFLASH).Write(MyData,LogBase,LogSize)
Status("New Log successfully written")
WriteCfgExit:
Tab(t1).ButtonEnable()
EndEvent
 

Attachments

  • Ambit256_SPI.bcs
    5.5 KB · Views: 41
Status
Not open for further replies.
Back
Top