Atmega32 a Wiznet

Host

Atmega32 a Wiznet
« kdy: 20. 03. 2013, 12:21:52 »
Zkouším rozchodit Atmega32 s modulem Wiznet(http://www.jameco.com/Jameco/Products/ProdDS/2124365.pdf).
Máme mezi sebou propojené piny MISO,MOSI,SCK,SS.
Podle níže přiloženého kódu pak PORTC.0 jako reset, PORTB.1 na INT, PORTB.2 na CS.

Jelikož jsem to nikdy dříve neřešil, tak jsem na internetu našel tento kód
Kód: [Vybrat]
$regfile = "m32def.dat"
$crystal = 16000000
$baud = 9600                                                'put terminal on 9600 baud

$hwstack = 80
$swstack = 80
$framesize = 90

'Used variables
Dim Second As Byte
Dim Minute As Byte
Dim Hour As Byte
Dim Day As Word

Dim A As Byte
Dim Wert As Byte
Dim Status As Byte
Dim Oldstatus As Byte
Dim Pointer As Word
Dim Tmp_value As Byte
Dim Tmp_str As String * 1
Dim Highbyte As Byte
Dim Lowbyte As Byte
Dim X As Integer
Dim Userpass As String * 20
Dim Tmp_auth As String * 20
Dim Authentification As Byte
Dim Website As Byte                                         'Flag for website ok / websitewrong selection
Dim Buffer As String * 700                                  '700 Bytes should be enough for the great header of firefox browsers

Const Http_auth = 0                                         '0= no HTTP-AUTH, 1= with HTTP-AUTH

Userpass = "admin:demo"                                     'Place a : between username and password (max. 20 character all together)
Tmp_auth = Base64enc(userpass)

'Used Wiz5100 ports and pins
Wiz5100_cs Alias Portb.2                                    'Chipselect WIZ5100
Wiz5100_int Alias Portb.1                                   'INT of WIZ5100
Wiz5100_res Alias Portc.0 = 1                               'Reset of WIZ5100

'Used ports and pins

Config Wiz5100_cs = Output
Config Wiz5100_int = Input
Config Wiz5100_res = Output


'Other used ports and pins
Relais1 Alias Portd.4
Relais2 Alias Portd.5
Relais3 Alias Portd.6
Relais4 Alias Portd.7


'Config the port/pin directions

'Config Led = Output
Config Relais1 = Output
Config Relais2 = Output
Config Relais3 = Output
Config Relais4 = Output

Relais1 = 0
Relais2 = 0
Relais3 = 0
Relais4 = 0

'Config Watchdog = 2048                                      'Watchdog configuration for stable reboot if system hangs

'Configuration of the SPI-bus
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0

'Init the spi pins
Spiinit

Const Ip[1] = 10
Const Ip[2] = 0                                             'Own IP-Adress
Const Ip[3] = 1
Const Ip[4] = 172

Const Sn[1] = 255                                           'Own Networkmask
Const Sn[2] = 255
Const Sn[3] = 255
Const Sn[4] = 0

Const Gw[1] = 10                                            'IP-Adress of the gateway
Const Gw[2] = 0
Const Gw[3] = 1
Const Gw[4] = 1

Const Mac[1] = &H00                                         'Own MAC-Adress
Const Mac[2] = &H20
Const Mac[3] = &H18
Const Mac[4] = &HB1
Const Mac[5] = &H15
Const Mac[6] = &H5F

Const Wiz5100_opcode_read = 15                              'OP-Code for Read from the Wiz5100
Const Wiz5100_opcode_write = 240                            'OP-Code for write to the Wiz5100

'Here we declare the used sub routines
Declare Sub Wiz5100_init
Declare Sub Wiz5100_readvalue(byval Highbyte As Byte , Byval Lowbyte As Byte)
Declare Sub Wiz5100_writevalue(byval Highbyte As Byte , Byval Lowbyte As Byte , Byval Value As Byte)
Declare Sub Wiz5100_receive
Declare Sub Wiz5100_send
Declare Sub Wiz5100_disconnect
Declare Sub Wiz5100_reset

Call Wiz5100_init                                           'We initialize the Wiz5100

Start Watchdog                                              'Start the watchdog

Config Timer1 = Timer , Prescale = 256                      'Config timer1 fora 1 second interval
Enable Timer1

On Timer1 Isr_timer1                                        'Timer interrupt routine

Enable Interrupts                                           'Activate the Interrupts

Timer1 = 68570                                              '34285                                              'Startvalue for the second timer

Do
   Reset Watchdog                                           'Reset the watchdog

   'Get socket status
   Call Wiz5100_readvalue(&H4 , &H3)
   Status = Wert

   If Status <> Oldstatus Then
      Oldstatus = Status
   End If

   'Connection was closed, we start the socket new
   If Status = &H0 Or Status = &H1C Then
      Call Wiz5100_disconnect()
   End If

   'As long as connection is etablished (&H17) we will look if client send new data
   If Status = &H17 Then
      Buffer = ""
      Call Wiz5100_receive()
   End If
Loop

End

'Disconnect connection: close socket, reopen socket and wait for new connection
Sub Wiz5100_disconnect
   Call Wiz5100_writevalue(&H4 , &H1 , &H0)                 'CLOSE
   Call Wiz5100_writevalue(&H4 , &H1 , &H1)                 'OPEN
   Call Wiz5100_writevalue(&H4 , &H1 , &H2)                 'LISTEN
End Sub

'Send headers and websites
Sub Wiz5100_send
   Local Sendsize As Integer , Pos_tx As Word , Freesize As Word , Offset As Word , Tx_wr As Word
   Local Startadress As Word , Offsend As Word , Uppersize As Word , Glaenge As Word , Startpos As Word
   Local Adctemp As Word , Tablewidth As Word , Color As String * 7

   'We restore the used website data
   If Website = 0 Then
      Restore Websitewrong
   Elseif Authentification = 0 Then
      Restore Websiteok
   Else
      Restore Websiteauth
   End If

   Do
      Read Buffer

      'Look for the end of a website
      If Buffer = "%END%" Then
         Exit Do
      End If


      'Look for variable and replace them, here we can insert new own variables if we want
     If Buffer = "%MRELAIS1%" Then
            Buffer = "<br/><form action=" + Chr(34) + "/" + Chr(34) + " method=" + Chr(34) + "POST" + Chr(34) + ">"
            Buffer = Buffer + "<input type=" + Chr(34) + "hidden" + Chr(34) + " name=" + Chr(34) + "MRELAIS1" + Chr(34) + " value=" + Chr(34) + "0" + Chr(34) + ">"
            Buffer = Buffer + "<input type = " + Chr(34) + "submit" + Chr(34) + " value = " + Chr(34) + "Reboot System 1" + Chr(34) + "></form>"
     End If
     If Buffer = "%MRELAIS2%" Then
            Buffer = "<br/><form action=" + Chr(34) + "/" + Chr(34) + " method=" + Chr(34) + "POST" + Chr(34) + ">"
            Buffer = Buffer + "<input type=" + Chr(34) + "hidden" + Chr(34) + " name=" + Chr(34) + "MRELAIS2" + Chr(34) + " value=" + Chr(34) + "0" + Chr(34) + ">"
            Buffer = Buffer + "<input type = " + Chr(34) + "submit" + Chr(34) + " value = " + Chr(34) + "Reboot System 2" + Chr(34) + "></form>"
     End If
     If Buffer = "%MRELAIS3%" Then
            Buffer = "<br/><form action=" + Chr(34) + "/" + Chr(34) + " method=" + Chr(34) + "POST" + Chr(34) + ">"
            Buffer = Buffer + "<input type=" + Chr(34) + "hidden" + Chr(34) + " name=" + Chr(34) + "MRELAIS3" + Chr(34) + " value=" + Chr(34) + "0" + Chr(34) + ">"
            Buffer = Buffer + "<input type = " + Chr(34) + "submit" + Chr(34) + " value = " + Chr(34) + "Reboot System 3" + Chr(34) + "></form>"
     End If
     If Buffer = "%MRELAIS4%" Then
            Buffer = "<br/><form action=" + Chr(34) + "/" + Chr(34) + " method=" + Chr(34) + "POST" + Chr(34) + ">"
            Buffer = Buffer + "<input type=" + Chr(34) + "hidden" + Chr(34) + " name=" + Chr(34) + "MRELAIS4" + Chr(34) + " value=" + Chr(34) + "0" + Chr(34) + ">"
            Buffer = Buffer + "<input type = " + Chr(34) + "submit" + Chr(34) + " value = " + Chr(34) + "Reboot System 4" + Chr(34) + "></form>"
      End If


     If Buffer = "%IP%" Then
         'Here we show the client IP-adress
         Call Wiz5100_readvalue(&H4 , &HC)
         Buffer = Str(wert) + "."

         Call Wiz5100_readvalue(&H4 , &HD)
         Buffer = Buffer + Str(wert) + "."

         Call Wiz5100_readvalue(&H4 , &HE)
         Buffer = Buffer + Str(wert) + "."

         Call Wiz5100_readvalue(&H4 , &HF)
         Buffer = Buffer + Str(wert)
      Elseif Buffer = "%DAY%" Then
         Buffer = Str(day)
      Elseif Buffer = "%HOUR%" Then
         Buffer = Str(hour)
      Elseif Buffer = "%MINUTE%" Then
         Buffer = Str(minute)
      Elseif Buffer = "%SECOND%" Then
         Buffer = Str(second)
      End If

      Sendsize = Len(buffer)

      Freesize:
         Call Wiz5100_readvalue(&H4 , &H20)
         Freesize = Wert
         Shift , Freesize , Left , 8
         Call Wiz5100_readvalue(&H4 , &H21)
         Freesize = Freesize + Wert

         If Freesize < Sendsize Then
            Goto Freesize
         End If

      Call Wiz5100_readvalue(&H4 , &H24)
      Tx_wr = Wert
      Shift , Tx_wr , Left , 8
      Call Wiz5100_readvalue(&H4 , &H25)
      Tx_wr = Tx_wr + Wert

      Startpos = Tx_wr

      Offset = Tx_wr And &H7FF

      Startadress = &H4000 + Offset

      Offsend = Offset + Sendsize

      Pointer = Startadress

      If Offsend > &H800 Then

         Uppersize = &H800 - Offset

         For X = 1 To Uppersize
            Highbyte = High(pointer)
            Lowbyte = Low(pointer)
            Tmp_str = Mid(buffer , X , 1)
            Tmp_value = Asc(tmp_str)
            Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value)
            Incr Pointer
         Next X

         Pointer = &H4000
         Incr Uppersize

         For X = Uppersize To Sendsize
            Highbyte = High(pointer)
            Lowbyte = Low(pointer)
            Tmp_str = Mid(buffer , X , 1)
            Tmp_value = Asc(tmp_str)
            Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value)
            Incr Pointer
         Next X
      Else
         For X = 1 To Sendsize
            Highbyte = High(pointer)
            Lowbyte = Low(pointer)
            Tmp_str = Mid(buffer , X , 1)
            Tmp_value = Asc(tmp_str)
            Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value)
            Incr Pointer
         Next X
      End If

      Call Wiz5100_readvalue(&H4 , &H24)
      Startpos = Wert
      Shift , Startpos , Left , 8
      Call Wiz5100_readvalue(&H4 , &H25)
      Startpos = Startpos + Wert

      Glaenge = Startpos + Sendsize

      'Send Site
      Highbyte = High(glaenge)
      Call Wiz5100_writevalue(&H4 , &H24 , Highbyte)

      Lowbyte = Low(glaenge)
      Call Wiz5100_writevalue(&H4 , &H25 , Lowbyte)

     'Set SEND flag
      Call Wiz5100_writevalue(&H4 , &H1 , &H20)
   Loop

   'Set DISCON flag
   Call Wiz5100_writevalue(&H4 , &H1 , &H8)
End Sub

'Receive data
Sub Wiz5100_receive
   Local I As Word
   Local Top As Word
   Local Bytes_rcv As Word
   Local Headerend As String * 4

   'Check for new incomming data
   Call Wiz5100_readvalue(&H4 , &H26)
   Bytes_rcv = Wert
   Shift , Bytes_rcv , Left , 8
   Call Wiz5100_readvalue(&H4 , &H27)
   Bytes_rcv = Bytes_rcv + Wert

   If Bytes_rcv > 0 Then
      Bytes_rcv = Bytes_rcv - 1

      For I = 0 To Bytes_rcv
          Top = I + &H6000
          Highbyte = High(top)
          Lowbyte = Low(top)
          Call Wiz5100_readvalue(highbyte , Lowbyte)

          'We only use correct header, correct header must be < 700 bytes!
          If Bytes_rcv < 700 Then
             Buffer = Buffer + Chr(wert)
          End If
      Next

      'Is header complete or is it a post reply?
      Headerend = Chr(13) + Chr(10) + Chr(13) + Chr(10)

      If Instr(buffer , Headerend) > 0 Then

         Website = 1

         #if Http_auth
            If Instr(buffer , Tmp_auth) > 0 Then
               'Authentification Found And Correct
               Authentification = 0

        If Instr(buffer , "POST /") = 1 Then

               'Toggle RELAIS1
               If Instr(buffer , "MRELAIS1=0") > 0 Then
                  Relais1 = 1
                  Waitms 500
                  Relais1 = 0
               End If

               'Toggle RELAIS2
               If Instr(buffer , "MRELAIS2=0") > 0 Then
                  Relais2 = 1
                  Waitms 500
                  Relais2 = 0
               End If

               'Toggle RELAIS3
               If Instr(buffer , "MRELAIS3=0") > 0 Then
                  Relais3 = 1
                  Waitms 500
                  Relais3 = 0
               End If

               'Toggle RELAIS4
               If Instr(buffer , "MRELAIS4=0") > 0 Then
                  Relais4 = 1
                  Waitms 500
                  Relais4 = 0
               End If

               End If
            Else
               'Authentification not found or not correct
               Authentification = 1
            End If
         #else

            Authentification = 0

            If Instr(buffer , "POST /") = 1 Then

               'Toggle RELAIS1
               If Instr(buffer , "MRELAIS1=0") > 0 Then
                  Relais1 = 1
                  Waitms 500
                  Relais1 = 0
               End If

               'Toggle RELAIS2
               If Instr(buffer , "MRELAIS2=0") > 0 Then
                  Relais2 = 1
                  Waitms 500
                  Relais2 = 0
               End If

               'Toggle RELAIS3
               If Instr(buffer , "MRELAIS3=0") > 0 Then
                  Relais3 = 1
                  Waitms 500
                  Relais3 = 0
               End If

               'Toggle RELAIS4
               If Instr(buffer , "MRELAIS4=0") > 0 Then
                  Relais4 = 1
                  Waitms 500
                  Relais4 = 0
               End If

            End If
         #endif

         'Set RECV flag
         Call Wiz5100_writevalue(&H4 , &H1 , &H40)

         Call Wiz5100_send()
      Elseif Bytes_rcv > 700 Then
         'Set RECV flag
         Call Wiz5100_writevalue(&H4 , &H1 , &H40)

         Website = 0

         Call Wiz5100_send()
      End If
   End If
End Sub

Sub Wiz5100_init
    Call Wiz5100_reset                                      'Hardware reset

   'Register reset
   Call Wiz5100_writevalue(&H0 , &H0 , &H80)

   'Set gateway IP adress
   Call Wiz5100_writevalue(&H0 , &H1 , Gw[1])
   Call Wiz5100_writevalue(&H0 , &H2 , Gw[2])
   Call Wiz5100_writevalue(&H0 , &H3 , Gw[3])
   Call Wiz5100_writevalue(&H0 , &H4 , Gw[4])

   'Set Subnetmask
   Call Wiz5100_writevalue(&H0 , &H5 , Sn[1])
   Call Wiz5100_writevalue(&H0 , &H6 , Sn[2])
   Call Wiz5100_writevalue(&H0 , &H7 , Sn[3])
   Call Wiz5100_writevalue(&H0 , &H8 , Sn[4])

   'Set MAC
   Call Wiz5100_writevalue(&H0 , &H9 , Mac[1])
   Call Wiz5100_writevalue(&H0 , &HA , Mac[2])
   Call Wiz5100_writevalue(&H0 , &HB , Mac[3])
   Call Wiz5100_writevalue(&H0 , &HC , Mac[4])
   Call Wiz5100_writevalue(&H0 , &HD , Mac[5])
   Call Wiz5100_writevalue(&H0 , &HE , Mac[6])

   'Set own IP adress
   Call Wiz5100_writevalue(&H0 , &HF , Ip[1])
   Call Wiz5100_writevalue(&H0 , &H10 , Ip[2])
   Call Wiz5100_writevalue(&H0 , &H11 , Ip[3])
   Call Wiz5100_writevalue(&H0 , &H12 , Ip[4])

   'Initialize socket 0 with TCP on port 80
   Call Wiz5100_writevalue(&H4 , &H0 , &H1 )                'TCP

   Call Wiz5100_writevalue(&H4 , &H4 , &H0 )                'Port 80 HTTP
   Call Wiz5100_writevalue(&H4 , &H5 , &H50 )

   Call Wiz5100_disconnect()
End Sub

Sub Wiz5100_writevalue(byval Highbyte As Byte , Byval Lowbyte As Byte , Byval Value As Byte)
   Reset Wiz5100_cs
   A = Wiz5100_opcode_write
   Spiout A , 1
   A = Highbyte
   Spiout A , 1
   A = Lowbyte
   Spiout A , 1
   Spiout Value , 1
   Set Wiz5100_cs
End Sub

Sub Wiz5100_readvalue(byval Highbyte As Byte , Byval Lowbyte As Byte)
   Reset Wiz5100_cs
   A = Wiz5100_opcode_read
   Spiout A , 1
   A = Highbyte
   Spiout A , 1
   A = Lowbyte
   Spiout A , 1
   Spiin Wert , 1
   Set Wiz5100_cs
End Sub

'The W5100 gets its reset from the general Arduino reset
Sub Wiz5100_reset
'   Wiz5100_res = 0
'   Waitus 30                                                'Minimum 20 µs
'   Wiz5100_res = 1
End Sub

Isr_timer1:                                                 'ISR of Timer1
Timer1 = 68570                                              'Timer1 must start from 34285 again

Incr Second                                                 'Here we increase the seconds
If Second = 60 Then
   Second = 0
   Incr Minute                                              'Here we increase the minutes
   If Minute = 60 Then
      Minute = 0
      Incr Hour                                             'Here we increase the hours
      If Hour = 24 Then
         Hour = 0
         Incr Day                                           'Here we increase the days
      End If
   End If
End If
Return

'Every Website must end with a stand alone Data "%END%" in the last line
Websiteauth:
Data "HTTP/1.0 401 Unauthorized{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}WWW-Authenticate: Basic realm={034}Website{034}{013}{010}Content-Type: text/html{013}{010}{013}{010}"
Data "<html><head><meta http-equiv={034}Pragma{034} Content={034}no cache{034}/>"
Data "<title>WIZ810MJ AVR I/O-Webserver</title></head><body><center><table width={034}600{034}><tr><td align={034}center{034}><font face={034}verdana{034} color={034}#000000{034}>"
Data "<H1>Remote reboot<br></H1><br/>"
Data "<b>This webserver needs a username/password<br><br><br/>"
Data "<br><br><font face={034}verdana{034} color={034}#000000{034}><a href={034}http://www.sencilo.nl{034}>"
Data "</b></a></font></body></html>"
Data "%END%"

Websitewrong:
Data "HTTP/1.0 200 Document follows{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}Content-Type: text/html{013}{010}{013}{010}"
Data "<html><head><meta http-equiv={034}Pragma{034} Content={034}no cache{034}/>"
Data "<title>Remote reboot</title></head><body><center><table width={034}600{034}><tr><td align={034}center{034}><font face={034}verdana{034} color={034}#000000{034}>"
Data "<H1>Remote reboot<br></H1></font><br/>"
Data "<font face={034}verdana{034} color={034}#C00000{034}>"
Data "</b></font></body></html>"
Data "%END%"

'A Variable must stand alone in a separate Line !!!
Websiteok:
Data "HTTP/1.0 200 Document follows{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}Content-Type: text/html{013}{010}{013}{010}"
Data "<html><head><meta http-equiv={034}Pragma{034} Content={034}no cache{034}/>"
Data "<title>Remote reboot</title></head><body><center><table width={034}600{034}><tr><td align={034}center{034}><font face={034}verdana{034} color={034}#000000{034}>"
Data "<H1>Remote reboot</H1><br>System 1"
Data "%MRELAIS1%"
Data "<br>System 2"
Data "%MRELAIS2%"
Data "<br>System 3"
Data "%MRELAIS3%"
Data "<br>System 4"
Data "%MRELAIS4%"
Data "<br><br><font face={034}verdana{034} color={034}#000000{034}>Your IP-number is: <b>"
Data "%IP%"
Data "</b><br>This server is online: <b>"
Data "%DAY%"
Data "</b> Days, <b>"
Data "%HOUR%"
Data "</b> Hours, <b>"
Data "%MINUTE%"
Data "</b> Minutes and <b>"
Data "%SECOND%"
Data "</b> Seconds<br>"
Data "<br><hr>http://benshobbycorner.nl - 2010<br>"
Data "</a></font></td></tr></table></center></body></html>"
Data "%END%"

Jenomže se mi to stále nedaří rozjet.
Modul nereaguje na ping. Pokud nechám připojený reset tak se ani nenastartuje.
Pokud reset odpojím tak sice naskočí, ale nedá se na něj dostat.
Mohl by někdo prosím poradit?


Re:Atmega32 a Wiznet
« Odpověď #1 kdy: 20. 03. 2013, 13:26:46 »
A nechces si prvne ten hw rozjet se zarucene funkcnim kodem treba v Arduino IDE a teprve pak experimentovat tady s tim desem, co se neda cist? ;)

Host

Re:Atmega32 a Wiznet
« Odpověď #2 kdy: 20. 03. 2013, 13:50:52 »
No já právě doufal, že s tímhle vzorovým souborem to pojede.
Vše sem zapojil tak je popsáno v kódu a jak vyžaduje SPI a nic  :'(
Tak nevím.
Asi budu muset.


Re:Atmega32 a Wiznet
« Odpověď #3 kdy: 20. 03. 2013, 13:57:57 »
Kdyz si vezmes vzorovej kod s Arduino IDE (napr. http://arduino.cc/en/Tutorial/WebServer ), tak ti X lidi potvrdi, ze funguje. Pokud budes pouzivat basic, imho si vyrazne snizis sance, ze ti nekdo pomuze.

Host

Re:Atmega32 a Wiznet
« Odpověď #4 kdy: 20. 03. 2013, 14:05:28 »
Fajn zítra se do toho vrhnu.
Díky za tip :)


Re:Atmega32 a Wiznet
« Odpověď #5 kdy: 20. 03. 2013, 14:18:22 »
Jenom se priprav na to, ze rozchodit zrovna 32ku s Arduino IDE neni uplne trivialni. Zas na druhou stranu kdyz se ti to povede, mas uz naveky k dispozici super nastroj a hromadu knihoven a vzorovych kodu.