cosduino_bootloader
Costycnc bootloader , cosduino bootloader , costycnc cosduino
A bootloader is just a normal AVR application that is positioned in a special region
of flash memory. In this project I refer only to the atmega8 !!!!
In sd_boot/sd_boot1.bas file
$regfile = "m8def.dat"
$crystal = 8000000
$loader = $e00
Means that this programs is write at adress E00 hex adress.This means that when power on or reset atmega8, atmega8 read first time by this adress.
(After execute the boot program ,normally,begin to read and execute from adress 0)
Normally ,without bootloader when power on or reset atmega8 ,atmega8 read and execute data from adress 0!
In this project, when power on or reset atmega8 the boot program :
- if SDCARD is inserted read data from SDCARD and write program to address 0
-if sdcard is not inserted ... jump to adress 0!
For the beginning only this you need to know about boot !!!
First lines of program is for initialize sdcard...this code below do this !
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 64 , Noss = 1 , Spiin = 255
Spiinit
sbi portb,2 'Set Sd_cs
Sdcommand = &HFF
Spiout Sdcommand , 1
For Sdtempb = 1 To 74
Gosub Sdcommand1
Next Sdtempb
cbi portb,2 'Reset Sd_cs
Sdsectorc = &H40000000
Sdsectorf = &H0095
Gosub Sdreadsectortest
Sdsectorc = &H48000001
Sdsectorf = &HAA87
Gosub Sdreadsectortest
If Sdresponse(1) = &B00000001 Then
Sdsectorc = &H7A000000
Sdsectorf = &H00FD
Gosub Sdreadsectortest
Do
Sdsectorc = &H77000000
Sdsectorf = &H0065
Gosub Sdreadsectortest
Sdsectorc = &H69400000
Sdsectorf = &H0077
Gosub Sdreadsectortest
Incr Sdtempw
Loop Until Sdresponse(1) = 0 Or Sdtempw = &HFFFF
Gosub Sdresponse2_5
After the card has been initialized you can read from any address from SDCARD!
My bootloader is very small because not use library for find file address of your program from SCARD, so this needs to do it manually.
File is hex or bin program (your aplication!)
In this video see how can find this address on sdcard!
After find this address ,write here ( Sdsectord = your progam address )
Sdsectord = 1651264 '1239168
Gosub Sdreadsector
After modify this address you can load this boot program in Microcontroller Atmega8.
You can load with this module:
See here how do it : atmega8_load
This boot program write 12 sectors by SDCARD on flash memory Atmega8 (512x12 = approx 6K ... 2k is bootloader ... so ,totally is 8k )
This routine write this 12 sectors:
$asm
'Z adresa unde scrie in flash
'Y lungime buffer de scris
'X adresa memorie temporara de scris -- $60
clr r18
clr r19
ldi r22,2
clr r21
ldi r17,$40
Ss1:
ldi xl,$60 'set again X registry to begin data memory ($60)
clr xh
Ss:
rcall write_p
Add R18 , r17
sbic sreg,0
inc r19
cp r19,r22
brne ss
push r16
push r17
push r18
push r19
push r20
push r21
push r22
push r23
push r24
push r25
push r26
push r27
push r28
push r29
push r30
push r31
$end Asm
Incr Sdsectord
Gosub Sdreadsector
$asm
pop r31
pop r30
pop r29
pop r28
pop r27
pop r26
pop r25
pop r24
pop r23
pop r22
pop r21
pop r20
pop r19
pop r18
pop r17
pop r16
inc r22
inc r22
inc r21
cpi r21,12 'poate fi facut 14
brne ss1
$end Asm
End If
jmp 0
And you can see at the end ... JMP 0 instruction ... now you know that's the point of this instruction!
Success!