Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20606

BASCOM-AVR : Updated SD-Card / AES Bootloader for XMega : REPLY

$
0
0
Here is the new code - smaler for more own code - with SDHC support [code:1:861ccfb4e8] $regfile = "xm256a3def.dat" $crystal = 2000000 $hwstack = 256 $swstack = 512 $framesize = 512 $loader = &H20000 '$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014 $lib "glcd_no_fs.lib" $map Config Sysclock = 2mhz , Prescalea = 1 , Prescalebc = 1_1 'use the 2MHz startup sysclock Disable Interrupts 'we do not use ints '---------------- Config part ------------------------------- Const Maxwordbit = 7 Const Maxword =(2 ^ Maxwordbit) * 2 Const Maxwordshift = Maxwordbit + 1 Const Loadsize = &H1EFFF * 2 'xmega256 application flash in words * 2 Const Filenamebin = "Master.bin" 'Filename of the file to load 'Const Filenamebindone = "M.Bin" 'Should the file be renamed after flashing? Const Lcdoutput = 1 'Want to use LCD Output? Code is for loading Bargraph on T6963 240*64 Const Encrypted = 1 'Want to load AES-Encrypted Bin-Files? Const Lockbits = 1 'Want the lockbits to be checked and modified? Const Lockbyte = &H14 'Value of lockbyte Const Checklockbits1_0 = 1 'check for external read/write is disabled Const Checkjtagdisabled = 1 'check for jtag is disabled '---------------- Config part ------------------------------- #if Lcdoutput = 1 Dim Loopcounter As Word Dim Linecounter As Byte Config Graphlcd = 240 * 64 , Dataport = Portf , Controlport = Porte , Ce = 5 , Cd = 6 , Wr = 3 , Rd = 4 , Reset = 7 , Mode = 8 Cursor Off Noblink Cls Locate 1 , 1 #endif Dim Btemp1 As Byte Dim Strname1 As String * 12 #if Varexist( "FileNameBinDone") Dim Strname2 As String * 12 #endif Dim J As Byte , Spmcrval As Byte ' self program command byte value Dim Z As Long 'this is the Z pointer word Dim Vl As Byte , Vh As Byte ' these bytes are used for the data values Dim Wrd As Word , Page As Word 'these vars contain the page and word address Dim Buf(128) As Byte Dim Lfilelen As Long Dim Initfilesystemerror As Byte Dim W1 As Word , B1 As Byte #if Encrypted = 1 Dim Key(16) As Byte $include "include/key.inc" #endif '---------------- Want the lockbits to be checked and modified? ---------------- #if Lockbits = 1 If Nvm_lockbits <> Lockbyte Then Nvm_data0 = Lockbyte Nvm_data1 = 0 Nvm_data2 = 0 Nvm_cmd = &H08 'Load the NVM CMD register with the Write Lock Bit command Cpu_ccp = &HD8 '0xD8 IOREG Protected IO register (this disables interrupts for 4 cycles) Nvm_ctrla.0 = 1 'Non-Volatile Memory Command Execute Bitwait Nvm_status.7 , Reset 'wait till bit is 0 Nvm_cmd = 0 End If #endif '---------------- Want the lockbits to be checked and modified? ---------------- '---------------- check for external read/write is disabled -------------------- #if Checklockbits1_0 = 1 And Lcdoutput = 0 If Nvm_lockbits.0 = 1 Or Nvm_lockbits.1 = 1 Then Stop #endif #if Checklockbits1_0 = 1 And Lcdoutput = 1 If Nvm_lockbits.0 = 1 Or Nvm_lockbits.1 = 1 Then Lcd "Lock" Stop End If #endif '---------------- check for external read/write is disabled -------------------- '---------------- check for jtag is disabled ----------------------------------- #if Checkjtagdisabled = 1 And Lcdoutput = 0 Nvm_addr0 = 4 'load Addressbyte 0 Nvm_addr1 = 0 'load Addressbyte 1 Nvm_addr2 = 0 'load Addressbyte 2 Nvm_cmd = &H07 'Read fuse byte at index ADDR0 into DATA0 Cpu_ccp = &HD8 '0xD8 IOREG Protected IO register (this disables interrupts for 4 cycles) Nvm_ctrla.0 = 1 'Non-Volatile Memory Command Execute Bitwait Nvm_status.7 , Reset 'wait till bit is 0 Btemp1 = Nvm_data0 Nvm_cmd = 0 If Btemp1.0 = 0 Then Stop #endif #if Checkjtagdisabled = 1 And Lcdoutput = 1 Nvm_addr0 = 4 'load Addressbyte 0 Nvm_addr1 = 0 'load Addressbyte 1 Nvm_addr2 = 0 'load Addressbyte 2 Nvm_cmd = &H07 'Read fuse byte at index ADDR0 into DATA0 Cpu_ccp = &HD8 '0xD8 IOREG Protected IO register (this disables interrupts for 4 cycles) Nvm_ctrla.0 = 1 'Non-Volatile Memory Command Execute Bitwait Nvm_status.7 , Reset 'wait till bit is 0 Btemp1 = Nvm_data0 Nvm_cmd = 0 If Btemp1.0 = 0 Then Lcd "JTAG" Stop End If #endif '---------------- check for jtag is disabled ----------------------------------- $include "include/Config_mmcsd_hc.bas" If Gbdriveerror <> 0 Then #if Lcdoutput = 1 Lcd "SD fail" #endif Goto waitstartmain End If $include "include/CONFIG_AVR-DOS.bas" Btemp1 = Initfilesystem(1) ' Get File system ' use 0 for drive without Master boot record If Btemp1 <> 0 Then #if Lcdoutput = 1 Lcd "FS fail" #endif Goto Waitstartmain End If Lfilelen = Filelen(filenamebin) If Lfilelen > 0 And Lfilelen < Loadsize Then #if Lcdoutput = 1 Lcd "Aktualisierung..." #endif Gosub Flashbin ' Size OK End If #if Lcdoutput = 1 If Lfilelen > Loadsize Then Lcd "Dateilänge" End If #endif '--- Jump to mainprogram ------------------------------------------------------- waitstartmain: waitms 3000 'neu Startmain: Rampz = 0 Goto _reset End '--- Subroutines - ------------------------------------------------------------- '--- BIN-File Part ------------------------------------------------------------- Flashbin: Open Filenamebin For Binary As #1 If Gbdoserror > 0 Then #if Lcdoutput = 1 Lcd "Dateifehler" #endif Goto waitstartmain End If Page = 0 Wrd = 0 Spmcrval = &H20 : Gosub Do_spm ' erase all app pages #if Lcdoutput = 1 Line(19 , 20) -(220 , 20) , 255 'oben Line(19 , 30) -(220 , 30) , 255 'unten Line(221 , 21) -(220 , 29) , 255 'rechts Line(19 , 21) -(19 , 29) , 255 'links Linecounter = 20 #endif Do #if Encrypted = 1 Get #1 , Buf(1) , , 128 Aesdecrypt Key(1) , Buf(1) , 128 #else Get #1 , Buf(1) , , 128 #endif #if Lcdoutput = 1 Incr Loopcounter If Loopcounter >= 10 Then Loopcounter = 0 Line(linecounter , 21) -(linecounter , 29) , 255 Incr Linecounter End If #endif Gosub Writepage If Eof(#1) > 0 Then If Wrd > 0 Then 'if there was something left in the page Wrd = 0 'Z pointer needs wrd to be 0 Spmcrval = &H24 : Gosub Do_spm 'write page End If Close #1 #if Varexist( "FileNameBinDone") Strname1 = Filenamebin Strname2 = Filenamebindone Kill Strname2 Name Strname1 As Strname2 #else Kill Filenamebin #endif #if Lcdoutput = 1 Do Line(linecounter , 21) -(linecounter , 29) , 255 Incr Linecounter Waitms 20 Loop Until Linecounter = 220 Locate 2 , 1 Lcd "erfolgreich" #endif Exit Do End If Loop Return '--- Ende BIN-File Part -------------------------------------------------------- '--- Programming Part ---------------------------------------------------------- Writepage: For J = 1 To 128 Step 2 'we write 2 bytes into a page Vl = Buf(j) : Vh = Buf(j + 1) 'get Low and High bytes lds r0, {vl} 'store them into r0 and r1 registers lds r1, {vh} Spmcrval = &H23 : Gosub Do_spm 'write value into page at word address Wrd = Wrd + 2 ' word address increases with 2 because LS bit of Z is not used If Wrd = Maxword Then ' page is full Wrd = 0 'Z pointer needs wrd to be 0 Spmcrval = &H24 : Gosub Do_spm 'write page Page = Page + 1 'next page End If Next Return Do_spm: Z = Page 'make equal to page Shift Z , Left , Maxwordshift 'shift to proper place Z = Z + Wrd 'add word lds r30,{Z} lds r31,{Z+1} #if _romsize > 65536 lds r24,{Z+2} sts rampz,r24 ' we need to set rampz also for the M128 #endif Nvm_cmd = Spmcrval Cpu_ccp = &H9D spm 'this is an asm instruction Do_spm_busy: lds r23, NVM_STATUS sbrc r23,7 'if busy bit is cleared skip next instruc tion rjmp do_spm_busy Return '--- Ende Programming Part ----------------------------------------------------- [/code:1:861ccfb4e8]

Viewing all articles
Browse latest Browse all 20606

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>