Rev 47 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
47 | dimkam | 1 | /** |
2 | * \file main.c |
||
3 | * W5300 example applications for ATMEGA128 of AVR |
||
4 | * This code is optimized at AVR-GCC 4.3.3(20090313) |
||
5 | * |
||
6 | * This file shows how to use SOCKET_API_FUNC "WIZnet SOCKET APIs" and |
||
7 | * how to implement your internet application with W5300.\n |
||
8 | * |
||
9 | * \author MidnightCow |
||
10 | * \date 15/05/2008 |
||
11 | * \version 1.1.0 |
||
12 | * |
||
13 | * |
||
14 | * Revision History : |
||
15 | * ---------- ------- ----------- ---------------------------- |
||
16 | * Date Version Author Description |
||
17 | * ---------- ------- ----------- ---------------------------- |
||
18 | * 24/03/2008 1.0.0 MidnightCow Release with W5300 launching |
||
19 | * ---------- ------- ----------- ---------------------------- |
||
20 | * 01/05/2008 1.0.1 MidnightCow Modify 'w5300.c'. Refer M_01052008. |
||
21 | * ---------- ------- ----------- ---------------------------- |
||
22 | * 15/05/2008 1.1.0 MidnightCow Refer M_15052008. |
||
23 | * Modify 'w5300.c', 'w5300.h' and 'socket.c'. |
||
24 | * ---------- ------- ----------- ---------------------------- |
||
25 | * 15/03/2012 1.2.1 Dongeun Solution of ARP problem |
||
26 | * Modify 'w5300.c', 'w5300.h' and 'socket.c'. |
||
27 | * ---------- ------- ----------- ---------------------------- |
||
28 | * 12/07/2012 1.2.2 Dongeun Solution of ARP problem |
||
29 | * Modify 'socket.c'. |
||
30 | * ---------- ------- ----------- ---------------------------- |
||
31 | * |
||
32 | * |
||
33 | * The Main() includes the example program of W5300 such as |
||
34 | * - loopback_tcps() \n |
||
35 | * - loopback_tcpc() \n |
||
36 | * - loopback_udp() |
||
37 | * |
||
38 | */ |
||
39 | |||
40 | #include <avr/io.h> |
||
41 | #include <avr/interrupt.h> |
||
42 | #include <stdio.h> |
||
43 | |||
44 | #include "socket.h" |
||
45 | #include "types.h" |
||
46 | #include "uart.h" |
||
47 | |||
48 | void initMCU(void); // Initialize MCU |
||
49 | |||
50 | /* Definition of example code */ |
||
51 | void loopback_tcps(SOCKET s, uint16 port, uint8* buf,uint16 mode); |
||
52 | void loopback_tcpc(SOCKET s, uint8* addr, uint16 port, uint8* buf,uint16 mode); |
||
53 | void loopback_udp(SOCKET s, uint16 port, uint8* buf, uint16 mode); |
||
54 | |||
55 | |||
56 | uint8 IP[4] = {192,168,1,86}; // IP Address |
||
57 | uint8 Gateway[4] = {192,168,1,1}; // Gateway Address |
||
58 | uint8 Subnet [4] = {255,255,255,0}; // Subnet Address |
||
59 | uint8 MAC[6] = {0x00,0x08,0xDC,0x11,0x22,0x86}; // MAC Address |
||
60 | uint8 ServerIP[4] = {192,168,1,202}; // "TCP SERVER" IP address for loopback_tcpc() |
||
61 | |||
62 | /* |
||
63 | * It executes example program such as loopback_tcps(), loopback_tcpc(), and loopback_udp(). |
||
64 | */ |
||
65 | int main(void) |
||
66 | { |
||
67 | unsigned long dd = 100000; |
||
68 | uint8 SN[4]; // for check subnet mask |
||
69 | |||
70 | uint8 tx_mem_conf[8] = {8,8,8,8,8,8,8,8}; // for setting TMSR regsiter |
||
71 | uint8 rx_mem_conf[8] = {8,8,8,8,8,8,8,8}; // for setting RMSR regsiter |
||
72 | uint8 *data_buf = (uint8 *)0x2000; // buffer for loopack data |
||
73 | |||
74 | initUART(); // initiate serial port |
||
75 | |||
76 | initMCU(); // initiate MCU |
||
77 | |||
78 | /* initiate W5300 */ |
||
79 | iinchip_init(); |
||
80 | setIMR(1 << INT5); |
||
81 | printf("\r\nIMR = 0x%04x\r\n",getIMR()); |
||
82 | printf("EIMSK = 0x%04x\r\n",EIMSK); |
||
83 | |||
84 | while(dd--); |
||
85 | |||
86 | /* allocate internal TX/RX Memory of W5300 */ |
||
87 | if(!sysinit(tx_mem_conf,rx_mem_conf)) |
||
88 | { |
||
89 | printf("MEMORY CONFIG ERR.\r\n"); |
||
90 | while(1); |
||
91 | } |
||
92 | |||
93 | setMR(getMR()| MR_FS); // If Little-endian, set MR_FS. |
||
94 | setSHAR(MAC); // set source hardware address |
||
95 | |||
96 | #ifdef __DEF_IINCHIP_PPP__ |
||
97 | if(pppinit((uint8*)"test01", 6, (uint8*)"pppoe1000", 9)!=1) |
||
98 | { |
||
99 | printf("PPPoE fail.\r\n"); |
||
100 | while(1); |
||
101 | } |
||
102 | close(0); |
||
103 | #else |
||
104 | /* configure network information */ |
||
105 | setGAR(Gateway); // set gateway IP address |
||
106 | setSUBR(Subnet); |
||
107 | setSIPR(IP); // set source IP address |
||
108 | #endif |
||
109 | |||
110 | /* verify network information */ |
||
111 | getSHAR(MAC); // get source hardware address |
||
112 | getGAR(Gateway); // get gateway IP address |
||
113 | getSUBR(SN); // get subnet mask address |
||
114 | getSIPR(IP); // get source IP address |
||
115 | |||
116 | |||
117 | printf("SHAR : %02x:%02x:%02x:%02x:%02x:%02x\r\n",MAC[0],MAC[1],MAC[2],MAC[3],MAC[4],MAC[5]); |
||
118 | printf("GWR : %d.%d.%d.%d\r\n",Gateway[0],Gateway[1],Gateway[2],Gateway[3]); |
||
119 | printf("SUBR : %d.%d.%d.%d\r\n",SN[0],SN[1],SN[2],SN[3]); |
||
120 | printf("SIPR : %d.%d.%d.%d\r\n",IP[0],IP[1],IP[2],IP[3]); |
||
121 | |||
122 | while(1) |
||
123 | { |
||
124 | loopback_tcps(0,5000,data_buf,0); |
||
125 | // loopback_tcps(1,5001,data_buf,0); |
||
126 | // loopback_tcps(2,5002,data_buf,0); |
||
127 | // loopback_tcps(3,5003,data_buf,0); |
||
128 | // loopback_tcps(4,5004,data_buf,0); |
||
129 | // loopback_tcps(5,5005,data_buf,0); |
||
130 | loopback_tcpc(6,ServerIP, 5006,data_buf,0); |
||
131 | loopback_udp(7,3000,data_buf,0); |
||
132 | } |
||
133 | |||
134 | #ifdef __DEF_IINCHIP_PPP__ |
||
135 | { |
||
136 | uint8 ppp_mac[6]; |
||
137 | getPDHAR(ppp_mac); |
||
138 | pppterm(ppp_mac, getPSIDR()); |
||
139 | } |
||
140 | #endif |
||
141 | |||
142 | while(1); |
||
143 | } |
||
144 | |||
145 | void initMCU(void) |
||
146 | { |
||
147 | EICRA = 0x00; // External Interrupt Control Register A clear |
||
148 | EICRB = 0x02; // External Interrupt Control Register B clear // edge |
||
149 | EIMSK = (1 << INT5); // External Interrupt Mask Register : 0x10 |
||
150 | EIFR = 0xFF; // External Interrupt Flag Register all clear |
||
151 | |||
152 | DDRE &= ~(1 << INT5); // Set PE Direction |
||
153 | PORTE |= (1 << INT5); // Set PE Default value |
||
154 | sei(); // enable interrupts |
||
155 | |||
156 | MCUCR |= (1 << SRE) | (1 << SRW10); |
||
157 | XMCRA |= (1 << SRL2) | (1 << SRW00); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * "TCP SERVER" loopback program. |
||
162 | */ |
||
163 | void loopback_tcps(SOCKET s, uint16 port, uint8* buf, uint16 mode) |
||
164 | { |
||
165 | uint32 len; |
||
166 | |||
167 | switch(getSn_SSR(s)) // check SOCKET status |
||
168 | { |
||
169 | // ------------ |
||
170 | case SOCK_ESTABLISHED: // ESTABLISHED? |
||
171 | if(getSn_IR(s) & Sn_IR_CON) // check Sn_IR_CON bit |
||
172 | { |
||
173 | printf("%d : Connect OK\r\n",s); |
||
174 | setSn_IR(s,Sn_IR_CON); // clear Sn_IR_CON |
||
175 | } |
||
176 | if((len=getSn_RX_RSR(s)) > 0) // check the size of received data |
||
177 | { |
||
178 | len = recv(s,buf,len); // recv |
||
179 | if(len !=send(s,buf,len)) // send |
||
180 | { |
||
181 | printf("%d : Send Fail.len=%ld\r\n",s,len); |
||
182 | } |
||
183 | } |
||
184 | break; |
||
185 | |||
186 | // --------------- |
||
187 | case SOCK_CLOSE_WAIT: // PASSIVE CLOSED |
||
188 | disconnect(s); // disconnect |
||
189 | break; |
||
190 | |||
191 | // -------------- |
||
192 | case SOCK_CLOSED: // CLOSED |
||
193 | close(s); // close the SOCKET |
||
194 | socket(s,Sn_MR_TCP,port,mode); // open the SOCKET |
||
195 | break; |
||
196 | |||
197 | // --------------- |
||
198 | case SOCK_INIT: // The SOCKET opened with TCP mode |
||
199 | listen(s); // listen to any connection request from "TCP CLIENT" |
||
200 | printf("%d : LOOPBACK_TCPS(%d) Started.\r\n",s,port); |
||
201 | break; |
||
202 | |||
203 | default: |
||
204 | break; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * "TCP CLIENT" loopback program. |
||
210 | */ |
||
211 | void loopback_tcpc(SOCKET s, uint8* addr, uint16 port, uint8* buf, uint16 mode) |
||
212 | { |
||
213 | uint32 len; |
||
214 | static uint16 any_port = 6000; |
||
215 | |||
216 | switch(getSn_SSR(s)) // check SOCKET status |
||
217 | { |
||
218 | // ------------ |
||
219 | case SOCK_ESTABLISHED: // ESTABLISHED? |
||
220 | if(getSn_IR(s) & Sn_IR_CON) // check Sn_IR_CON bit |
||
221 | { |
||
222 | printf("%d : Connect OK\r\n",s); |
||
223 | setSn_IR(s,Sn_IR_CON); // clear Sn_IR_CON |
||
224 | } |
||
225 | if((len=getSn_RX_RSR(s)) > 0) // check the size of received data |
||
226 | { |
||
227 | len = recv(s,buf,len); // recv |
||
228 | if(len !=send(s,buf,len)) // send |
||
229 | { |
||
230 | printf("%d : Send Fail.len=%ld\r\n",s,len); |
||
231 | } |
||
232 | } |
||
233 | break; |
||
234 | |||
235 | // --------------- |
||
236 | case SOCK_CLOSE_WAIT: // PASSIVE CLOSED |
||
237 | disconnect(s); // disconnect |
||
238 | break; |
||
239 | |||
240 | // -------------- |
||
241 | case SOCK_CLOSED: // CLOSED |
||
242 | close(s); // close the SOCKET |
||
243 | socket(s,Sn_MR_TCP,any_port++,mode); // open the SOCKET with TCP mode and any source port number |
||
244 | break; |
||
245 | |||
246 | // ------------------------------ |
||
247 | case SOCK_INIT: // The SOCKET opened with TCP mode |
||
248 | connect(s, addr, port); // Try to connect to "TCP SERVER" |
||
249 | printf("%d : LOOPBACK_TCPC(%d.%d.%d.%d:%d) Started.\r\n",s,addr[0],addr[1],addr[2],addr[3],port); |
||
250 | break; |
||
251 | |||
252 | default: |
||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * UDP loopback program. |
||
259 | */ |
||
260 | void loopback_udp(SOCKET s, uint16 port, uint8* buf, uint16 mode) |
||
261 | { |
||
262 | uint32 len; |
||
263 | uint8 destip[4]; |
||
264 | uint16 destport; |
||
265 | |||
266 | switch(getSn_SSR(s)) |
||
267 | { |
||
268 | // ------------------------------- |
||
269 | case SOCK_UDP: // |
||
270 | if((len=getSn_RX_RSR(s)) > 0) // check the size of received data |
||
271 | { |
||
272 | len = recvfrom(s,buf,len,destip,&destport); // receive data from a destination |
||
273 | if(len !=sendto(s,buf,len,destip,destport)) // send the data to the destination |
||
274 | { |
||
275 | printf("%d : Sendto Fail.len=%ld,",s,len); |
||
276 | printf("%d.%d.%d.%d(%d)\r\n",destip[0],destip[1],destip[2],destip[3],destport); |
||
277 | } |
||
278 | } |
||
279 | break; |
||
280 | // ----------------- |
||
281 | case SOCK_CLOSED: // CLOSED |
||
282 | close(s); // close the SOCKET |
||
283 | socket(s,Sn_MR_UDP,port,mode); // open the SOCKET with UDP mode |
||
284 | break; |
||
285 | |||
286 | default: |
||
287 | break; |
||
288 | } |
||
289 | } |