| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | //***************************************************************************** |
| 2 | // File Name : netstacktest.c |
||
| 3 | // |
||
| 4 | // Title : example usage of network library functions |
||
| 5 | // Revision : 1.0 |
||
| 6 | // Notes : |
||
| 7 | // Target MCU : Atmel AVR series |
||
| 8 | // Editor Tabs : 4 |
||
| 9 | // |
||
| 10 | // Revision History: |
||
| 11 | // When Who Description of change |
||
| 12 | // ----------- ----------- ----------------------- |
||
| 13 | // 10-Aug-2005 pstang Created the program |
||
| 14 | //***************************************************************************** |
||
| 15 | |||
| 16 | #include <avr/io.h> |
||
| 17 | #include <string.h> |
||
| 18 | #include <stdlib.h> |
||
| 19 | |||
| 20 | // include avrlib basics |
||
| 21 | #include "global.h" |
||
| 22 | #include "timer.h" |
||
| 23 | #include "uart.h" |
||
| 24 | #include "rprintf.h" |
||
| 25 | #include "vt100.h" |
||
| 26 | #include "debug.h" |
||
| 27 | |||
| 28 | // include network support |
||
| 29 | #include "net/net.h" |
||
| 30 | #include "net/nic.h" |
||
| 31 | #include "net/arp.h" |
||
| 32 | #include "net/icmp.h" |
||
| 33 | #include "net/ip.h" |
||
| 34 | #include "net/netstack.h" |
||
| 35 | |||
| 36 | // network defines |
||
| 37 | #define IPADDRESS IPDOT(192l,168l,1l,12l) |
||
| 38 | #define NETMASK IPDOT(255l,255l,255l,0l) |
||
| 39 | #define GATEWAY IPDOT(192l,168l,1l,1l) |
||
| 40 | |||
| 41 | #define LOOPBACK_PORT 7 // UDP packets sent to this port will be returned to sender |
||
| 42 | #define CONTROL_PORT 4950 // UDP packets sent to this port will be used for control |
||
| 43 | #define SERIAL_PORT 4951 // UDP packets sent to this port will be printed via serial |
||
| 44 | |||
| 45 | // timer defines |
||
| 46 | #define TIMER_PRESCALE 1024 |
||
| 47 | #define TIMER_INTERVAL (F_CPU/TIMER_PRESCALE/100) // 100ms interval |
||
| 48 | |||
| 49 | // global variables |
||
| 50 | static volatile unsigned long UptimeMs; |
||
| 51 | |||
| 52 | // functions |
||
| 53 | void processCommand(u16 len, u08* data); |
||
| 54 | void serviceLocal(void); |
||
| 55 | void systickHandler(void); |
||
| 56 | |||
| 57 | // prototypes |
||
| 58 | int main(void) |
||
| 59 | { |
||
| 60 | struct netEthAddr myEthAddress; |
||
| 61 | |||
| 62 | timerInit(); |
||
| 63 | uartInit(); |
||
| 64 | uartSetBaudRate(115200); |
||
| 65 | rprintfInit(uartSendByte); |
||
| 66 | timerPause(100); |
||
| 67 | vt100ClearScreen(); |
||
| 68 | rprintf("\r\nNetwork Stack Example\r\n"); |
||
| 69 | |||
| 70 | // initialize systick timer |
||
| 71 | rprintf("Initializing Periodic Timer\r\n"); |
||
| 72 | timer2SetPrescaler(TIMERRTC_CLK_DIV1024); |
||
| 73 | timerAttach(TIMER2OVERFLOW_INT, systickHandler); |
||
| 74 | |||
| 75 | // init network stack |
||
| 76 | rprintf("Initializing Network Stack\r\n"); |
||
| 77 | netstackInit(IPADDRESS, NETMASK, GATEWAY); |
||
| 78 | |||
| 79 | nicGetMacAddress(&myEthAddress.addr[0]); |
||
| 80 | rprintfProgStrM("Eth Addr is: "); netPrintEthAddr(&myEthAddress); rprintfCRLF(); |
||
| 81 | rprintfProgStrM("IP Addr is: "); netPrintIPAddr(ipGetConfig()->ip); rprintfCRLF(); |
||
| 82 | |||
| 83 | rprintf("Network Stack is up!\r\n"); |
||
| 84 | rprintf("Starting packet receive loop\r\n"); |
||
| 85 | |||
| 86 | while(1) |
||
| 87 | { |
||
| 88 | // service local stuff |
||
| 89 | serviceLocal(); |
||
| 90 | // service the network |
||
| 91 | netstackService(); |
||
| 92 | } |
||
| 93 | |||
| 94 | return 0; |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet) |
||
| 99 | { |
||
| 100 | u16 payloadlen=0; |
||
| 101 | u08* payloaddata=0; |
||
| 102 | u16 i; |
||
| 103 | |||
| 104 | // get UDP payload length |
||
| 105 | payloadlen = htons(packet->udp.udplen); |
||
| 106 | payloadlen -= 8; // subtract header |
||
| 107 | // get UDP payload data |
||
| 108 | payloaddata = &((unsigned char*)packet)[IP_HEADER_LEN+UDP_HEADER_LEN]; |
||
| 109 | |||
| 110 | rprintf("UDP packet, len: %d\r\n", len); |
||
| 111 | // debugPrintHexTable(len, (unsigned char*)packet); |
||
| 112 | |||
| 113 | if(packet->udp.destport == HTONS(CONTROL_PORT)) |
||
| 114 | { |
||
| 115 | // command packet |
||
| 116 | processCommand(payloadlen, payloaddata); |
||
| 117 | } |
||
| 118 | else if(packet->udp.destport == HTONS(SERIAL_PORT)) |
||
| 119 | { |
||
| 120 | // serial output |
||
| 121 | for(i=0; i<payloadlen; i++) |
||
| 122 | uartSendByte(payloaddata[i]); |
||
| 123 | } |
||
| 124 | else if(packet->udp.destport == HTONS(LOOPBACK_PORT)) |
||
| 125 | { |
||
| 126 | // loopback - return packet to sender |
||
| 127 | udpSend(htonl(packet->ip.srcipaddr), LOOPBACK_PORT, payloadlen, payloaddata); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet) |
||
| 133 | { |
||
| 134 | rprintf("Received TCP/IP Packet: len=%d\r\n", len); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | void processCommand(u16 len, u08* data) |
||
| 139 | { |
||
| 140 | rprintf("Received UDP command: CMD=0x%x ARG0=0x%x\r\n", data[0], data[1]); |
||
| 141 | |||
| 142 | // do something based on command |
||
| 143 | switch(data[0]) |
||
| 144 | { |
||
| 145 | case 'C': // set PORTC |
||
| 146 | PORTC = data[1]; |
||
| 147 | break; // set DDRC |
||
| 148 | case 'c': |
||
| 149 | PORTC = data[1]; |
||
| 150 | break; |
||
| 151 | default: |
||
| 152 | rprintf("Unknown UDP command\r\n"); |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | void serviceLocal(void) |
||
| 158 | { |
||
| 159 | int c; |
||
| 160 | unsigned char buffer[100]; |
||
| 161 | |||
| 162 | if( (c = uartGetByte()) != -1) |
||
| 163 | { |
||
| 164 | // echo command to terminal |
||
| 165 | rprintfChar(c); |
||
| 166 | // process command |
||
| 167 | switch(c) |
||
| 168 | { |
||
| 169 | case 'i': |
||
| 170 | rprintfProgStrM("\r\nInitializing Ethernet Controller\r\n"); |
||
| 171 | nicInit(); |
||
| 172 | break; |
||
| 173 | case 'd': |
||
| 174 | rprintfProgStrM("\r\nEthernet Controller State\r\n"); |
||
| 175 | nicRegDump(); |
||
| 176 | break; |
||
| 177 | case 't': |
||
| 178 | rprintfProgStrM("\r\nCurrent Uptime: "); |
||
| 179 | rprintfNum(10, 9, FALSE, ' ', UptimeMs); |
||
| 180 | rprintfProgStrM("ms\r\n"); |
||
| 181 | break; |
||
| 182 | case 'c': |
||
| 183 | rprintfProgStrM("\r\nCrashing System....\r\n"); |
||
| 184 | while(1); |
||
| 185 | break; |
||
| 186 | case 'u': |
||
| 187 | rprintfProgStrM("\r\nSending UDP packet\r\n"); |
||
| 188 | strcpy(&buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN], "hello"); |
||
| 189 | udpSend((ipGetConfig()->ip|~ipGetConfig()->netmask), CONTROL_PORT, 6, &buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN]); |
||
| 190 | break; |
||
| 191 | case '?': |
||
| 192 | rprintfProgStrM("\r\nCommands:\r\n"); |
||
| 193 | rprintfProgStrM("(i) Initialize Ethernet Controller\r\n"); |
||
| 194 | rprintfProgStrM("(d) Dump Ethernet Controller State\r\n"); |
||
| 195 | rprintfProgStrM("(u) Send Broadcast UDP frame\r\n"); |
||
| 196 | rprintfProgStrM("(t) Print current uptime\r\n"); |
||
| 197 | rprintfProgStrM("(?) Help\r\n"); |
||
| 198 | break; |
||
| 199 | case '\r': |
||
| 200 | default: |
||
| 201 | break; |
||
| 202 | } |
||
| 203 | // print new prompt |
||
| 204 | rprintfProgStrM("\r\ncmd>"); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | void systickHandler(void) |
||
| 209 | { |
||
| 210 | // set timer for 10ms interval |
||
| 211 | TCNT2 = (unsigned char)-TIMER_INTERVAL; |
||
| 212 | // count up on uptime |
||
| 213 | UptimeMs += 10; |
||
| 214 | } |
Powered by WebSVN v2.8.3