$crystal = 11059200
$baud = 19200 'this loader uses serial com
'It is VERY IMPORTANT that the baud rate matches the one of the boot loader
'do not try to use buffered com as we can not use interrupts
'$regfile = "m8def.dat"
'Const Loaderchip = 8
'$regfile = "m168def.dat"
'Const Loaderchip = 168
'$regfile = "m16def.dat"
'Const Loaderchip = 16
$regfile = "m32def.dat"
Const Loaderchip = 32
'$regfile = "m88def.dat"
'Const Loaderchip = 88
(Bootoader.bas)
Zusätzlich
müssen die Fuses
passend eingestellt werden. Insbesondere muss ein Boot-Flash-Bereich
von 1024 Words eingerichtet werden. Bei Start springt der Controller
zuerst die Adresse
3C00 an um eventuelle ein neues Programm hochzuladen. Wenn nichts
anliegt,
verzweigt das Boot-Programm auf den normalen Startvektor bei 0000.
Nach dieser Vorbereitung kann man einen beliebigen Quelltext für den Mega32 in Bascom laden, kompilieren und mit Program/Send to chip in den Controller übertragen. Es funktioniert wahlweise über USB oder RS232, kein externes Programm mehr! Ist bereits ein Programm geladen, muss beim nächsten mal zuerst mit Program/Send to chip der Bootvorgang beauftragt werden und dann einmal kurz auf den Resetknopf des ES-M32 gedrückt werden. Dann startet der Ladevorgang. Nach dem Laden startet das neue Programm von allein.
So weit, so gut. Aber dann gab es ein Problem. Die geladene Firmware verwendete
ebenfalls die serielle Schnittstelle, und ebenfalls mit 19200 Baud. Wenn nun
ein PC-Programm schon vor dem Start des Controllers versucht, Daten
auszutauschen, dann kann es passieren, dass der Bootloader sich angesprochen
fühlt. Das gibt dann Chaos, weil er antwortet, aber nicht verstanden
wird.
Also was genau läuft da ab? Ich habe einmal die Aktion des Bascom über eine
zweite Schnittstelle belauscht. Bascom sendet einfach nur beliebig lange das
Zauberbyte 123. Sobald der Bootlader ebenfalls mit 123 antwortet, ist er
erkannt und kann angesprochen werden.
$timeout = 400000 'we use a timeoutAuszug aus dem originalen Bootlader (Bootoader.bas)
'When you get LOADER errors during the upload, increase the timeout value
'for example at 16 Mhz, use 200000
Bretries = 5 'we try 5 times
Testfor123:
#if Cdebug
Print "Try " ; Bretries
Print "Wait"
#endif
Bstatus = Waitkey() 'wait for the loader to send a byte
#if Cdebug
Print "Got "
#endif
Print Chr(bstatus);
If Bstatus = 123 Then 'did we received value 123 ?
Bkind = 0 'normal flash loader
Goto Loader
Elseif Bstatus = 124 Then ' EEPROM
Bkind = 1 ' EEPROM loader
Goto Loader
Elseif Bstatus <> 0 Then
Decr Bretries
If Bretries <> 0 Then Goto Testfor123 'we test again
End If
For J = 1 To 10 'this is a simple indication that we start the normal reset vector
Toggle Portb.2 : Waitms 100
Next
#if Cdebug
Print "RESET"
#endif
Goto _reset 'goto the normal reset vector at address 0
Testfor123:Der modifizierte Einstieg (Bootoader32.bas)
Bstatus = Waitkey() 'wait for the loader to send a byte
If Bstatus = 123 Then 'did we received value 123 ?
Print Chr(bstatus);
Bkind = 0 'normal flash loader
Goto Loader
Else 'try again
Bstatus = Waitkey() 'wait for the loader to send a byte
If Bstatus = 123 Then 'did we received value 123 ?
Print Chr(bstatus);
Bkind = 0 'normal flash loader
Goto Loader
End If
End If
Goto _reset 'goto the normal reset vector at address 0
'this is the loader routine. It is a Xmodem-checksum reception routine
Loader:
Bstatus = Waitkey() 'wait for the loader to send a byte
If Bstatus = 123 Then 'did we received value 123 ?
Print Chr(bstatus);
Bkind = 0 'normal flash loader
Goto Loader2
Else
Bstatus = Waitkey() 'wait for the loader to send a byte
If Bstatus = 123 Then 'did we received value 123 ?
Print Chr(bstatus);
Bkind = 0 'normal flash loader
Goto Loader2
End If
End If
Goto _reset 'goto the normal reset vector at address 0
Loader2:
Do
Bstatus = Waitkey()
Loop Until Bstatus = 0
If Bkind = 0 Then
Spmcrval = 3 : Gosub Do_spm ' erase the first page
Spmcrval = 17 : Gosub Do_spm ' re-enable page
End If
'--------------------------------------------------------------
' ESM32 SIOS
' ATMEGA32, 11,0592 MHz
' Erweiterung: Start des Bootladers mit 123, 123
'--------------------------------------------------------------
$regfile = "m32def.dat"
$baud = 19200
$crystal = 11059200
$hwstack = 32
$swstack = 64
$framesize = 64
Dim Command As Byte
Dim N As Byte
Dim L As Byte
Dim H As Byte
Dim D As Byte
Dim Dd As Word
Open "com1:" For Binary As #1
Config Portd = Output
Portc = 255
Config Adc = Single , Prescaler = Auto , Reference = Off ' Pre 8...32
Start Adc
Config Timer1 = Pwm , Prescale = 8 , Pwm = 10 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down
Start Timer1
Do
Get #1 , Command
'SIOS Device 10
If Command = 1 Then Put #1 , 10
'Firmware V2.1
If Command = 2 Then Put #1 , 21
'Output Port D
If Command = 16 Then
Get #1 , D
Portd = D
End If
'Input Port C
If Command = 32 Then
D = Pinc
Put # 1 , D
End If
'ADC 8 Bit
If Command = 48 Then
Dd = Getadc(0)
Shift Dd , Right , 2
D = Dd
Put #1 , D
End If
...
'ADC 10 Bit
If Command = 56 Then
Dd = Getadc(0)
D = High(dd)
Put #1 , D
Get #1 , D
D = Low(dd)
Put #1 , D
End If
If Command = 57 Then
Dd = Getadc(1)
D = High(dd)
Put #1 , D
Get #1 , D
D = Low(dd)
Put #1 , D
End If
...
'PWM Output 10 Bit
If Command = 72 Then
Get #1 , H
Dd = 256 * H
Get #1 , L
Dd = Dd + L
Pwm1a = Dd
End If
If Command = 73 Then
Get #1 , H
Dd = 256 * H
Get #1 , L
Pwm1b = Dd
End If
If Command = 123 Then 'Bootloader
Get #1 , D
If D = 123 Then Jmp $3c00
End If
Loop
End
'Arduino/Bascom-Test
$regfile = "m168def.dat"
$hwstack = 32
$swstack = 64
$framesize = 64
$crystal = 16000000
Baud = 19200
Config Portb.5 = Output
'Ports 0...7 = PortD, Ports 9...13 = Port B
'13-LED = PortB.5
Do
Toggle Portb.5
Waitms 500
Print "Hallo"
Loop
End