'------------------------------------------------------------------ ' Atmega168 and WIZ810MJ All you need to get a mailer working '------------------------------------------------------------------ ' ' It is a save mailer. Waiting for status-messages from the mail-server $regfile = "m168def.dat" $crystal = 8000000 $baud = 9600 $hwstack = 128 $swstack = 128 $framesize = 128 $include "w5100.inc" Dim Value As Byte Dim Adres As Word Dim Adresl As Byte At Adres Overlay Dim Adresh As Byte At Adres + 1 Overlay Dim Pointer As Word Dim Pointerl As Byte At Pointer Overlay Dim Pointerh As Byte At Pointer + 1 Overlay Dim Size As Word Dim Sizel As Byte At Size Overlay Dim Sizeh As Byte At Size + 1 Overlay Dim Bpointer As Word Dim X As Word Dim Freesize As Word Dim Sendsize As Integer Dim Tmp_str As String * 1 Dim Tmp_value As Byte Dim Startpos As Word Dim Tx_wr As Word Dim Offset As Word Dim Offsend As Word Dim Uppersize As Word Dim Startadress As Word Dim Highbyte As Byte Dim Lowbyte As Byte Dim Buffer As String * 200 Dim Glaenge As Word Dim Parameter As String * 3 Dim Reaction As String * 3 Dim Preaction As Byte Dim Timeout As Bit ' 0 = long board ' 1 = sandwichboard Const Board = 1 'Used Wiz5100 ports and pins Wiz5100_cs Alias Portb.2 'Chipselect WIZ810MJ Print "Start mailer.." Wait 1 #if Board 'sandwichboard Wiz5100_res Alias Portd.3 'reset of WIZ810MJ 'Other used ports and pins Relais1 Alias Portd.4 Relais2 Alias Portd.5 Relais3 Alias Portd.6 Relais4 Alias Portd.7 #elseif Wiz5100_res Alias Portc.0 'long board Relais1 Alias Portc.1 Relais2 Alias Portc.2 Relais3 Alias Portc.3 Relais4 Alias Portc.4 #endif 'Used ports and pins Config Wiz5100_cs = Output Config Wiz5100_res = Output '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 Wait 1 'Here we declare the used sub routines Declare Sub Wiz5100_init Declare Sub Wiz5100_readvalue(byval Reg As Word) Declare Sub Wiz5100_writevalue(byval Reg As Word , Byval Value As Byte) Declare Sub Wiz5100_reset Declare Sub Wiz5100_sendit Declare Sub Wiz5100_waitfor(byval Parameter As String) Dim Wiz5100_opcode_read As Byte Wiz5100_opcode_read = 15 Dim Wiz5100_opcode_write As Byte Wiz5100_opcode_write = 240 Call Wiz5100_init 'We initialize the Wiz810MJ 'Assign all 2k memory to every socket for RX Call Wiz5100_writevalue(w5100_rmsr , &B01010101) ' 'Assign all 2k memory to every socket for TX Call Wiz5100_writevalue(w5100_tmsr , &B01010101) 'protocol = tcp Call Wiz5100_writevalue(w5100_s0_mr , Sn_mr_tcp) Call Wiz5100_writevalue(w5100_s0_port0 , &H13) 'port 5000 Call Wiz5100_writevalue(w5100_s0_port1 , &H88) Call Wiz5100_writevalue(w5100_s0_dipr0 , 213) 'IP-number of mail-server Call Wiz5100_writevalue(w5100_s0_dipr1 , 51) Call Wiz5100_writevalue(w5100_s0_dipr2 , 130) Call Wiz5100_writevalue(w5100_s0_dipr3 , 46) Call Wiz5100_writevalue(w5100_s0_dport0 , 00) 'port 25 Call Wiz5100_writevalue(w5100_s0_dport1 , 25) 'give the open command Call Wiz5100_writevalue(w5100_s0_cr , Sn_cr_open) 'wait time to get ARP-request finished Wait 2 Do Call Wiz5100_readvalue(w5100_s0_sr) 'sock init? Loop Until Value = Sock_init 'give the connect command Call Wiz5100_writevalue(w5100_s0_cr , Sn_cr_connect) Call Wiz5100_readvalue(w5100_s0_ir) If Value.3 = 1 Then Print "Timeout" Timeout = 1 Goto Quit End If Wait 2 Do Call Wiz5100_readvalue(w5100_s0_sr) 'Sock Established Loop Until Value = Sock_established Call Wiz5100_waitfor( "220") If Timeout = 1 Then Goto Quit 'Here we start the conversation Buffer = "HELO" + Chr(13) + Chr(10) Call Wiz5100_sendit Call Wiz5100_waitfor( "250") If Timeout = 1 Then Goto Quit Buffer = "mail from: bzijlstra@home.nl" + Chr(13) + Chr(10) Call Wiz5100_sendit Call Wiz5100_waitfor( "250") Buffer = "rcpt to: bzijlstra@home.nl" + Chr(13) + Chr(10) Call Wiz5100_sendit Call Wiz5100_waitfor( "250") If Timeout = 1 Then Goto Quit Buffer = "Data" + Chr(13) + Chr(10) Call Wiz5100_sendit Call Wiz5100_waitfor( "354") If Timeout = 1 Then Goto Quit Buffer = "From: bzijlstra@home.nl" + Chr(13) + Chr(10) Buffer = Buffer + "To: bzijlstra@home.nl" + Chr(13) + Chr(10) Buffer = Buffer + "cc: bzijlstra@home.nl" + Chr(13) + Chr(10) Buffer = Buffer + "BCC: bzijlstra@home.nl" + Chr(13) + Chr(10) Buffer = Buffer + "Subject: WIZ810MJ-mailer" + Chr(13) + Chr(10) + Chr(13) + Chr(10) Buffer = Buffer + "This is a test from the WIZ810MJ-mailer" + Chr(13) + Chr(10) Buffer = Buffer + "On 24 august 2008" + Chr(13) + Chr(10) Buffer = Buffer + "." + Chr(13) + Chr(10) Call Wiz5100_sendit Call Wiz5100_waitfor( "250") If Timeout = 1 Then Goto Quit Print "Mail has been send" Goto Afterquit Quit: Print "Abort, mail not send" Afterquit: Buffer = "quit" + Chr(13) + Chr(10) Call Wiz5100_sendit 'Set Discon Flag Call Wiz5100_writevalue(w5100_s0_cr , &H8) End Sub Wiz5100_init Call Wiz5100_reset 'Hardware reset 'Register reset Call Wiz5100_writevalue(w5100_mr , &H80) 'Set gateway IP adress Call Wiz5100_writevalue(w5100_gar0 , 192) '192.168.0.254 My gateway Call Wiz5100_writevalue(w5100_gar1 , 168) Call Wiz5100_writevalue(w5100_gar2 , 0) Call Wiz5100_writevalue(w5100_gar3 , 254) 'Set Subnetmask Call Wiz5100_writevalue(w5100_subr0 , 255) '255.255.255.0 networkmask Call Wiz5100_writevalue(w5100_subr1 , 255) Call Wiz5100_writevalue(w5100_subr2 , 255) Call Wiz5100_writevalue(w5100_subr3 , 0) 'Set MAC Call Wiz5100_writevalue(w5100_shar0 , &H00) 'MAC-address Call Wiz5100_writevalue(w5100_shar1 , &H10) Call Wiz5100_writevalue(w5100_shar2 , &H20) Call Wiz5100_writevalue(w5100_shar3 , &H30) Call Wiz5100_writevalue(w5100_shar4 , &H40) Call Wiz5100_writevalue(w5100_shar5 , &H50) 'Set own IP adress Call Wiz5100_writevalue(w5100_sipr0 , 192) 'IP-number of device Call Wiz5100_writevalue(w5100_sipr1 , 168) Call Wiz5100_writevalue(w5100_sipr2 , 0) Call Wiz5100_writevalue(w5100_sipr3 , 75) End Sub Sub Wiz5100_readvalue(reg) Adres = Reg Reset Wiz5100_cs Spiout Wiz5100_opcode_read , 1 Spiout Adresh , 1 Spiout Adresl , 1 Spiin Value , 1 Set Wiz5100_cs End Sub Sub Wiz5100_writevalue(reg , Value ) Adres = Reg Reset Wiz5100_cs Spiout Wiz5100_opcode_write , 1 Spiout Adresh , 1 Spiout Adresl , 1 Spiout Value , 1 Set Wiz5100_cs End Sub Sub Wiz5100_reset Waitms 250 Wiz5100_res = 1 Waitms 250 Wiz5100_res = 0 Waitms 250 'Minimum 20 µs Wiz5100_res = 1 End Sub Sub Wiz5100_sendit Sendsize = Len(buffer) Freesize: Call Wiz5100_readvalue(w5100_s0_tx_fsr0) Freesize = Value Shift , Freesize , Left , 8 Call Wiz5100_readvalue(w5100_s0_tx_fsr1) Freesize = Freesize + Value If Freesize < Sendsize Then Goto Freesize End If Call Wiz5100_readvalue(w5100_s0_tx_wr0) Tx_wr = Value Shift , Tx_wr , Left , 8 Call Wiz5100_readvalue(w5100_s0_tx_wr1) Tx_wr = Tx_wr + Value 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 Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(pointer , Tmp_value) Incr Pointer Next X Pointer = &H4000 Incr Uppersize For X = Uppersize To Sendsize Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(pointer , Tmp_value) Incr Pointer Next X Else For X = 1 To Sendsize Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(pointer , Tmp_value) Incr Pointer Next X End If Call Wiz5100_readvalue(w5100_s0_tx_wr0) Startpos = Value Shift , Startpos , Left , 8 Call Wiz5100_readvalue(w5100_s0_tx_wr1) Startpos = Startpos + Value Glaenge = Startpos + Sendsize 'Send text Highbyte = High(glaenge) Call Wiz5100_writevalue(w5100_s0_tx_wr0 , Highbyte) Lowbyte = Low(glaenge) Call Wiz5100_writevalue(w5100_s0_tx_wr1 , Lowbyte) 'Set SEND flag Call Wiz5100_writevalue(w5100_s0_cr , Sn_cr_send) End Sub 'get reaction of the mailserver Sub Wiz5100_waitfor(byval Parameter As String) Timeout = 0 Do 'check on RECV Call Wiz5100_readvalue(w5100_s0_ir) If Value.3 = 1 Then Print "Timeout" Timeout = 1 Exit Sub End If Loop Until Value.2 = 1 Reaction = "" Preaction = 0 Call Wiz5100_readvalue(w5100_s0_rx_rd0) Pointerh = Value Call Wiz5100_readvalue(w5100_s0_rx_rd1) Pointerl = Value 'Socket 0 RX Received Size Call Wiz5100_readvalue(w5100_s0_rx_rsr0) Sizeh = Value Call Wiz5100_readvalue(w5100_s0_rx_rsr1) Sizel = Value Startpos = Pointer Offset = Startpos And &H7FF Startadress = &H6000 + Offset Offsend = Offset + Size 'sendsize Bpointer = Startadress If Offsend > &H800 Then Uppersize = &H800 - Offset For X = 1 To Uppersize Call Wiz5100_readvalue(bpointer) Print Chr(value); If Preaction < 3 Then Reaction = Reaction + Chr(value) Incr Preaction End If Incr Bpointer Next X Bpointer = &H6000 Incr Uppersize For X = Uppersize To Size Call Wiz5100_readvalue(bpointer) Print Chr(value); If Preaction < 3 Then Reaction = Reaction + Chr(value) Incr Preaction End If Incr Bpointer Next X Else For X = 1 To Size Call Wiz5100_readvalue(bpointer) Print Chr(value); If Preaction < 3 Then Reaction = Reaction + Chr(value) Incr Preaction End If Incr Bpointer Next X End If Print Pointer = Pointer + Size 'update pointer Call Wiz5100_writevalue(w5100_s0_rx_rd0 , Pointerh) Call Wiz5100_writevalue(w5100_s0_rx_rd1 , Pointerl) Call Wiz5100_writevalue(w5100_s0_cr , Sn_cr_recv) 'reset interrupt Call Wiz5100_writevalue(w5100_s0_ir , &B100) ' Call Wiz5100_readvalue(w5100_s0_cr) If Parameter <> Reaction Then Print "Unexpected reaction from mailserver " ; Reaction Goto Quit End If End Sub