Getting started with Arduino and WIZ820io
(Difference between revisions)
Line 20: | Line 20: | ||
<source lang="bascomavr"> | <source lang="bascomavr"> | ||
+ | $crystal = 16000000 'Arduino is running at 16MHz | ||
+ | $baud = 57600 '57600 Baud | ||
+ | $timeout = 900000 | ||
+ | |||
+ | $regfile = "m328pdef.dat" | ||
+ | $loader = $3c00 'sets the bootsize to 1024 words under flash size | ||
+ | Const Pagemsb = 6 '64 words in a page reguires 6 bits | ||
+ | |||
+ | Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0 | ||
+ | |||
+ | Dim Retries As Byte | ||
+ | Dim Receivedbyte As Byte | ||
+ | Dim Kindofdata As Byte | ||
+ | Dim Spmcsrvalue As Byte | ||
+ | Dim Calculatedchecksum As Byte | ||
+ | Dim Receivedblock As Byte | ||
+ | Dim Countedblock As Byte | ||
+ | Dim Invertedblock As Byte | ||
+ | Dim Receivedchecksum As Byte | ||
+ | Dim Receivedbytes(128) As Byte | ||
+ | Dim J As Byte | ||
+ | Dim Vl As Byte | ||
+ | Dim Vh As Byte | ||
+ | Dim Wrd As Word | ||
+ | Dim Page As Word | ||
+ | Dim Z As Long | ||
+ | |||
+ | Disable Interrupts | ||
+ | |||
+ | Const Maxword =(2 ^ Pagemsb) * 2 | ||
+ | Const Zpagemsb = Pagemsb + 1 | ||
+ | Const Soh = &H01 | ||
+ | Const Stx = &H02 | ||
+ | Const Eot = &H04 | ||
+ | Const Ack = &H06 | ||
+ | Const Nak = &H15 | ||
+ | Const Can = &H18 | ||
+ | |||
+ | Retries = 5 | ||
+ | Testformagicbyte: | ||
+ | Receivedbyte = Waitkey() | ||
+ | Print Chr(receivedbyte); | ||
+ | If Receivedbyte = 123 Then | ||
+ | Kindofdata = 0 | ||
+ | Goto Loader | ||
+ | Elseif Receivedbyte = 124 Then | ||
+ | Kindofdata = 1 | ||
+ | Goto Loader | ||
+ | Elseif Receivedbyte <> 0 Then | ||
+ | Decr Retries | ||
+ | If Retries <> 0 Then Goto Testformagicbyte | ||
+ | End If | ||
+ | Goto _reset | ||
+ | |||
+ | Loader: | ||
+ | Do | ||
+ | Receivedbyte = Waitkey() | ||
+ | Loop Until Receivedbyte = 0 | ||
+ | |||
+ | If Kindofdata = 0 Then | ||
+ | Spmcsrvalue = 3 : Gosub Do_spm | ||
+ | Spmcsrvalue = 17 : Gosub Do_spm | ||
+ | End If | ||
+ | Retries = 10 | ||
+ | |||
+ | Do | ||
+ | Calculatedchecksum = 0 | ||
+ | Print Chr(nak); | ||
+ | Do | ||
+ | Receivedbyte = Waitkey() | ||
+ | Select Case Receivedbyte | ||
+ | Case &H01: '<SOH> | ||
+ | Incr Countedblock | ||
+ | Calculatedchecksum = 1 | ||
+ | Receivedblock = Waitkey() | ||
+ | Calculatedchecksum = Calculatedchecksum + Receivedblock | ||
+ | Invertedblock = Waitkey() | ||
+ | Calculatedchecksum = Calculatedchecksum + Invertedblock | ||
+ | For J = 1 To 128 | ||
+ | Receivedbytes(j) = Waitkey() | ||
+ | Calculatedchecksum = Calculatedchecksum + Receivedbytes(j) | ||
+ | Next | ||
+ | Receivedchecksum = Waitkey() | ||
+ | If Countedblock = Receivedblock Then | ||
+ | If Receivedchecksum = Calculatedchecksum Then | ||
+ | Gosub Writepage | ||
+ | Print Chr(ack); | ||
+ | Else | ||
+ | Print Chr(nak); | ||
+ | End If | ||
+ | Else | ||
+ | Print Chr(nak); | ||
+ | End If | ||
+ | Case &H04: '<EOT> | ||
+ | If Wrd > 0 And Kindofdata = 0 Then | ||
+ | Wrd = 0 | ||
+ | Spmcsrvalue = 5 : Gosub Do_spm | ||
+ | Spmcsrvalue = 17 : Gosub Do_spm | ||
+ | End If | ||
+ | Print Chr(ack); | ||
+ | Waitms 20 | ||
+ | Goto _reset | ||
+ | Case &H18: '<CAN> | ||
+ | Goto _reset | ||
+ | Case Else | ||
+ | Exit Do | ||
+ | End Select | ||
+ | Loop | ||
+ | |||
+ | If Retries > 0 Then | ||
+ | Waitms 1000 | ||
+ | Decr Retries | ||
+ | Else | ||
+ | Goto _reset | ||
+ | End If | ||
+ | Loop | ||
+ | |||
+ | Writepage: | ||
+ | If Kindofdata = 0 Then | ||
+ | For J = 1 To 128 Step 2 | ||
+ | Vl = Receivedbytes(j) | ||
+ | Vh = Receivedbytes(j + 1) | ||
+ | lds r0, {vl} | ||
+ | lds r1, {vh} | ||
+ | Spmcsrvalue = 1 : Gosub Do_spm | ||
+ | Wrd = Wrd + 2 | ||
+ | If Wrd = Maxword Then | ||
+ | Wrd = 0 | ||
+ | Spmcsrvalue = 5 : Gosub Do_spm | ||
+ | Spmcsrvalue = 17 : Gosub Do_spm | ||
+ | Page = Page + 1 | ||
+ | Spmcsrvalue = 3 : Gosub Do_spm | ||
+ | Spmcsrvalue = 17 : Gosub Do_spm | ||
+ | End If | ||
+ | Next | ||
+ | Else | ||
+ | For J = 1 To 128 | ||
+ | Writeeeprom Receivedbytes(j) , Wrd | ||
+ | Wrd = Wrd + 1 | ||
+ | Next | ||
+ | End If | ||
+ | Return | ||
+ | |||
+ | Do_spm: | ||
+ | Bitwait Spmcsr.0 , Reset | ||
+ | Bitwait Eecr.1 , Reset | ||
+ | Z = Page | ||
+ | Shift Z , Left , Zpagemsb | ||
+ | Z = Z + Wrd | ||
+ | lds r30, {Z} | ||
+ | lds r31, {Z + 1} | ||
+ | #if _romsize > 65536 | ||
+ | lds r24, {Z + 2} | ||
+ | sts rampz, r24 | ||
+ | #endif | ||
+ | Spmcsr = Spmcsrvalue | ||
+ | spm | ||
+ | nop | ||
+ | nop | ||
+ | Return | ||
</source> | </source> |
Revision as of 14:38, 17 February 2013
Used Hardware:
To get started an Arduino Duemilanove board is used with an Arduino prototyping shield and prototyping jump wires.
To get the 3.3V for the WIZ820io power supply an voltage regulator is needed to transform the 5V from the Arduino board to 3.3V.
(The maximum current draw from FTDI chip on the Arduino Duemilanove is max. 50mA and therefore not usable to power the WIZ820io)
Used Bootloader:
You can use the Arduino bootloader but in this example an AVRISP MKII is used to flash an Bascom-AVR Bootloader.
For this you need an AVRISP MKII and AVR Studio to flash the bootloader on the Atmega328P on the Arduino.
Following you can find the Bootloader written in Bascom-AVR:
$crystal = 16000000 'Arduino is running at 16MHz $baud = 57600 '57600 Baud $timeout = 900000 $regfile = "m328pdef.dat" $loader = $3c00 'sets the bootsize to 1024 words under flash size Const Pagemsb = 6 '64 words in a page reguires 6 bits Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0 Dim Retries As Byte Dim Receivedbyte As Byte Dim Kindofdata As Byte Dim Spmcsrvalue As Byte Dim Calculatedchecksum As Byte Dim Receivedblock As Byte Dim Countedblock As Byte Dim Invertedblock As Byte Dim Receivedchecksum As Byte Dim Receivedbytes(128) As Byte Dim J As Byte Dim Vl As Byte Dim Vh As Byte Dim Wrd As Word Dim Page As Word Dim Z As Long Disable Interrupts Const Maxword =(2 ^ Pagemsb) * 2 Const Zpagemsb = Pagemsb + 1 Const Soh = &H01 Const Stx = &H02 Const Eot = &H04 Const Ack = &H06 Const Nak = &H15 Const Can = &H18 Retries = 5 Testformagicbyte: Receivedbyte = Waitkey() Print Chr(receivedbyte); If Receivedbyte = 123 Then Kindofdata = 0 Goto Loader Elseif Receivedbyte = 124 Then Kindofdata = 1 Goto Loader Elseif Receivedbyte <> 0 Then Decr Retries If Retries <> 0 Then Goto Testformagicbyte End If Goto _reset Loader: Do Receivedbyte = Waitkey() Loop Until Receivedbyte = 0 If Kindofdata = 0 Then Spmcsrvalue = 3 : Gosub Do_spm Spmcsrvalue = 17 : Gosub Do_spm End If Retries = 10 Do Calculatedchecksum = 0 Print Chr(nak); Do Receivedbyte = Waitkey() Select Case Receivedbyte Case &H01: '<SOH> Incr Countedblock Calculatedchecksum = 1 Receivedblock = Waitkey() Calculatedchecksum = Calculatedchecksum + Receivedblock Invertedblock = Waitkey() Calculatedchecksum = Calculatedchecksum + Invertedblock For J = 1 To 128 Receivedbytes(j) = Waitkey() Calculatedchecksum = Calculatedchecksum + Receivedbytes(j) Next Receivedchecksum = Waitkey() If Countedblock = Receivedblock Then If Receivedchecksum = Calculatedchecksum Then Gosub Writepage Print Chr(ack); Else Print Chr(nak); End If Else Print Chr(nak); End If Case &H04: '<EOT> If Wrd > 0 And Kindofdata = 0 Then Wrd = 0 Spmcsrvalue = 5 : Gosub Do_spm Spmcsrvalue = 17 : Gosub Do_spm End If Print Chr(ack); Waitms 20 Goto _reset Case &H18: '<CAN> Goto _reset Case Else Exit Do End Select Loop If Retries > 0 Then Waitms 1000 Decr Retries Else Goto _reset End If Loop Writepage: If Kindofdata = 0 Then For J = 1 To 128 Step 2 Vl = Receivedbytes(j) Vh = Receivedbytes(j + 1) lds r0, {vl} lds r1, {vh} Spmcsrvalue = 1 : Gosub Do_spm Wrd = Wrd + 2 If Wrd = Maxword Then Wrd = 0 Spmcsrvalue = 5 : Gosub Do_spm Spmcsrvalue = 17 : Gosub Do_spm Page = Page + 1 Spmcsrvalue = 3 : Gosub Do_spm Spmcsrvalue = 17 : Gosub Do_spm End If Next Else For J = 1 To 128 Writeeeprom Receivedbytes(j) , Wrd Wrd = Wrd + 1 Next End If Return Do_spm: Bitwait Spmcsr.0 , Reset Bitwait Eecr.1 , Reset Z = Page Shift Z , Left , Zpagemsb Z = Z + Wrd lds r30, {Z} lds r31, {Z + 1} #if _romsize > 65536 lds r24, {Z + 2} sts rampz, r24 #endif Spmcsr = Spmcsrvalue spm nop nop Return