Rev 205 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 205 | Rev 212 | ||
|---|---|---|---|
| Line 15... | Line 15... | ||
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include <can.h> |
19 | #include <can.h> |
| 20 |
|
20 | #include <serial.h> |
| 21 | #include <timebase.h> |
21 | #include <timebase.h> |
| 22 | #include <ip_arp_udp_tcp.h> |
22 | #include <ip_arp_udp_tcp.h> |
| 23 | #include <enc28j60.h> |
23 | #include <enc28j60.h> |
| 24 | #include <timeout.h> |
24 | #include <timeout.h> |
| 25 | #include <avr_compat.h> |
25 | #include <avr_compat.h> |
| Line 38... | Line 38... | ||
| 38 | static uint8_t remotemac[6]; |
38 | static uint8_t remotemac[6]; |
| 39 | static uint8_t remoteip[4]; |
39 | static uint8_t remoteip[4]; |
| 40 | 40 | ||
| 41 | #define BUFFER_SIZE 250 |
41 | #define BUFFER_SIZE 250 |
| 42 | static uint8_t buf[BUFFER_SIZE+1]; |
42 | static uint8_t buf[BUFFER_SIZE+1]; |
| 43 | static uint8_t replybuf[BUFFER_SIZE+1]; |
- | |
| 44 | 43 | ||
| - | 44 | #define SEND_BUFFER_SIZE 20 |
|
| - | 45 | static char sendbuf[SEND_BUFFER_SIZE+1]; |
|
| - | 46 | static uint8_t buffpoint; |
|
| - | 47 | ||
| - | 48 | /*---------------------------------------------------------------------------- |
|
| - | 49 | * Putchar for udp |
|
| - | 50 | * Implements a small buffer, sends packet over udp if buffer is full or |
|
| - | 51 | * \n-char is sent. |
|
| - | 52 | * Needs a timeout to force send. |
|
| - | 53 | *--------------------------------------------------------------------------*/ |
|
| 45 | int udp_putchar(char c, FILE* stream) { |
54 | int udp_putchar(char c, FILE* stream) { |
| 46 | if (gotserver != 0) { |
55 | if (gotserver != 0) { |
| - | 56 | sendbuf[buffpoint] = c; |
|
| 47 |
|
57 | buffpoint++; |
| - | 58 | ||
| 48 |
|
59 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
| 49 |
|
60 | send_udp(buf, sendbuf, buffpoint, remoteport, remotemac, remoteip); |
| 50 |
|
61 | buffpoint = 0; |
| 51 | } |
62 | } |
| 52 | //send_udp(replybuf, &c, 1, remoteport, remotemac, remoteip); |
- | |
| 53 | make_udp_reply_from_request(buf, "hello", 5, remoteport); |
- | |
| 54 | //send_udp(buf, &c, 1, remoteport, remotemac, remoteip); |
- | |
| 55 | } |
63 | } |
| 56 | return 0; |
64 | return 0; |
| 57 | } |
65 | } |
| 58 | 66 | ||
| 59 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
67 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
| Line 65... | Line 73... | ||
| 65 | 73 | ||
| 66 | //ethernet vars |
74 | //ethernet vars |
| 67 | uint16_t plen = 0; |
75 | uint16_t plen = 0; |
| 68 | uint8_t ledi=0; |
76 | uint8_t ledi=0; |
| 69 | uint8_t payloadlen=0; |
77 | uint8_t payloadlen=0; |
| 70 | - | ||
| 71 |
|
78 | buffpoint = 0; |
| 72 | 79 | ||
| 73 | //can vars |
80 | //can vars |
| 74 | Can_Message_t txMsg; |
81 | Can_Message_t txMsg; |
| 75 | Can_Message_t rxMsg; |
82 | Can_Message_t rxMsg; |
| 76 | txMsg.Id = 3; |
83 | txMsg.Id = 3; |
| 77 | txMsg.DataLength = 0; |
84 | txMsg.DataLength = 0; |
| 78 | txMsg.RemoteFlag = 0; |
85 | txMsg.RemoteFlag = 0; |
| 79 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
86 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
| - | 87 | ||
| - | 88 | ||
| - | 89 | uint32_t timeStamp = 0; |
|
| 80 | 90 | ||
| - | 91 | Mcu_Init(); |
|
| 81 | Timebase_Init(); |
92 | Timebase_Init(); |
| 82 |
|
93 | Serial_Init(); |
| 83 | #if defined(__AVR_ATmega88__) |
- | |
| - | 94 | ||
| 84 | // |
95 | //alla printf ska skriva till udp |
| 85 | // set the clock prescaler. First write CLKPCE to enable setting of clock the |
- | |
| 86 | // next four instructions. |
- | |
| 87 | CLKPR=(1<<CLKPCE); |
- | |
| 88 | CLKPR=0; // 8 MHZ |
- | |
| 89 | #endif |
- | |
| 90 | - | ||
| 91 |
|
96 | stdout = &mystdout; //set the output stream |
| 92 | 97 | ||
| 93 | sei(); |
98 | sei(); |
| 94 | 99 | ||
| 95 | // printf("CanInit..."); |
100 | // printf("CanInit..."); |
| 96 | if (Can_Init() != CAN_OK) { |
101 | if (Can_Init() != CAN_OK) { |
| 97 | // printf("FAILED!\n"); |
102 | // printf("FAILED!\n"); |
| 98 | } |
103 | } |
| Line 106... | Line 111... | ||
| 106 | PORTB &= ~(1<<PB0); |
111 | PORTB &= ~(1<<PB0); |
| 107 | delay_ms(20); |
112 | delay_ms(20); |
| 108 | /* set output to Vcc, reset inactive */ |
113 | /* set output to Vcc, reset inactive */ |
| 109 | PORTB|= (1<<PB0); |
114 | PORTB|= (1<<PB0); |
| 110 | delay_ms(100); |
115 | delay_ms(100); |
| 111 | 116 | ||
| 112 | // LED |
117 | // LED |
| 113 | /* enable PB1, LED as output */ |
118 | /* enable PB1, LED as output */ |
| 114 | DDRD|= (1<<DDD6); |
119 | DDRD|= (1<<DDD6); |
| 115 | /* set output to Vcc, LED off */ |
120 | /* set output to Vcc, LED off */ |
| 116 | PORTD|= (1<<PD6); |
121 | PORTD|= (1<<PD6); |
| 117 | 122 | ||
| - | 123 | //printf("Init enc\n"); |
|
| 118 | /*initialize enc28j60*/ |
124 | /*initialize enc28j60*/ |
| 119 | enc28j60Init(mymac); |
125 | enc28j60Init(mymac); |
| 120 | delay_ms(20); |
126 | delay_ms(20); |
| 121 | 127 | ||
| 122 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
128 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
| 123 | // LEDB=yellow LEDA=green |
129 | // LEDB=yellow LEDA=green |
| 124 | // |
130 | // |
| 125 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
131 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
| 126 | // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); |
- | |
| 127 | enc28j60PhyWrite(PHLCON,0x476); |
132 | enc28j60PhyWrite(PHLCON,0x476); |
| 128 | delay_ms(20); |
133 | delay_ms(20); |
| 129 | 134 | ||
| 130 | /* set output to GND, red LED on */ |
135 | /* set output to GND, red LED on */ |
| 131 | PORTD &= ~(1<<PD6); |
136 | PORTD &= ~(1<<PD6); |
| Line 136... | Line 141... | ||
| 136 | 141 | ||
| 137 | /* main loop */ |
142 | /* main loop */ |
| 138 | while (1) { |
143 | while (1) { |
| 139 | /* service the CAN routines */ |
144 | /* service the CAN routines */ |
| 140 | Can_Service(); |
145 | Can_Service(); |
| 141 | - | ||
| 142 | //PORTD &= ~(1<<PD6); |
- | |
| 143 | 146 | ||
| 144 | /* send CAN message and check for CAN errors once every second */ |
147 | /* send CAN message and check for CAN errors once every second */ |
| 145 | if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { |
148 | if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { |
| 146 | timeStamp = Timebase_CurrentTime(); |
149 | timeStamp = Timebase_CurrentTime(); |
| 147 | /* send txMsg */ |
150 | /* send txMsg */ |
| 148 | //txMsg.Id++; |
151 | //txMsg.Id++; |
| 149 | txMsg. |
152 | //txMsg.Id = 3; |
| 150 | Can_Send |
153 | //Can_Send(&txMsg); |
| 151 | if (ledi){ |
- | |
| 152 | /* set output to Vcc, LED off */ |
- | |
| 153 | PORTD|= (1<<PD6); |
- | |
| 154 | ledi=0; |
- | |
| 155 | }else{ |
- | |
| 156 | /* set output to GND, LED on */ |
- | |
| 157 | PORTD &= ~(1<<PD6); |
- | |
| 158 | ledi=1; |
- | |
| 159 | } |
- | |
| 160 | 154 | ||
| 161 | } |
155 | } |
| 162 | 156 | ||
| 163 | /* check if any messages have been received */ |
157 | /* check if any messages have been received */ |
| 164 | while (Can_Receive(&rxMsg) == CAN_OK) { |
158 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 165 |
|
159 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
| 166 |
|
160 | printf("data={ "); |
| 167 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
161 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 168 | printf("%x ", rxMsg.Data.bytes[i]); |
162 | printf("%x ", rxMsg.Data.bytes[i]); |
| 169 | } |
163 | } |
| 170 | printf("}\n"); |
164 | printf("}\n"); |
| 171 |
|
165 | |
| 172 | if (gotserver != 0) { |
- | |
| 173 | uint8_t i = 0; |
- | |
| 174 | while(i<BUFFER_SIZE+1){ |
- | |
| 175 | buf[i]=replybuf[i]; |
- | |
| 176 |
|
166 | } |
| 177 |
|
167 | |
| 178 | send_udp(buf, "hello", 6, remoteport, remotemac, remoteip); |
- | |
| 179 | //make_udp_reply_from_request(buf, "hello", 5, remoteport); |
- | |
| 180 |
|
168 | //test-debug-kod |
| 181 |
|
169 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
| 182 |
|
170 | //printf("EIR_TXERIF set!\n"); |
| 183 | i++; |
- | |
| 184 | } |
- | |
| 185 |
|
171 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
| 186 |
|
172 | //printf("ECON1_TXRTS set!\n"); |
| 187 | } |
173 | } |
| - | 174 | } |
|
| - | 175 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
|
| - | 176 | //printf("EIR_TXIF set! "); |
|
| - | 177 | } |
|
| - | 178 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
|
| - | 179 | //printf("ESTAT_TXABRT set!\n"); |
|
| 188 | //printf(" |
180 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
| - | 181 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
|
| 189 | } |
182 | } |
| 190 | 183 | ||
| 191 | // get the next new packet: |
184 | // get the next new packet: |
| 192 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
185 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
| 193 | 186 | ||
| 194 | /*plen will ne unequal to zero if there is a valid |
187 | /*plen will ne unequal to zero if there is a valid |
| 195 | * packet (without crc error) */ |
188 | * packet (without crc error) */ |
| 196 | if(plen!=0){ |
189 | if(plen!=0){ |
| 197 | // arp is broadcast if unknown but a host may also |
190 | // arp is broadcast if unknown but a host may also |
| 198 | // verify the mac address by sending it to |
191 | // verify the mac address by sending it to |
| Line 200... | Line 193... | ||
| 200 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
193 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
| 201 | make_arp_answer_from_request(buf); |
194 | make_arp_answer_from_request(buf); |
| 202 | } else { |
195 | } else { |
| 203 | // check if ip packets (icmp or udp) are for us: |
196 | // check if ip packets (icmp or udp) are for us: |
| 204 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
197 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
| 205 | //if (ledi){ |
- | |
| 206 | /* set output to Vcc, LED off */ |
- | |
| 207 | // PORTD|= (1<<PD6); |
- | |
| 208 | // ledi=0; |
- | |
| 209 | //}else{ |
- | |
| 210 | /* set output to GND, LED on */ |
- | |
| 211 | // PORTD &= ~(1<<PD6); |
- | |
| 212 | // ledi=1; |
- | |
| 213 | //} |
- | |
| 214 | 198 | ||
| 215 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
199 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
| 216 | // a ping packet, let's send pong |
200 | // a ping packet, let's send pong |
| 217 | make_echo_reply_from_request(buf,plen); |
201 | make_echo_reply_from_request(buf,plen); |
| 218 | txMsg. |
202 | //txMsg.Id = 6; |
| 219 | Can_Send |
203 | //Can_Send(&txMsg); |
| 220 | 204 | ||
| 221 | } |
205 | } |
| 222 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
206 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
| 223 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
207 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
| 224 | if (buf[UDP_DATA_P]=='t' && payloadlen==5){ |
208 | if (buf[UDP_DATA_P]=='t' && payloadlen==5){ |
| 225 | uint8_t i=0; |
209 | uint8_t i=0; |
| 226 | 210 | if (gotserver == 0) { |
|
| 227 | while(i<6){ |
211 | while(i<6){ |
| 228 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
212 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
| 229 | i++; |
213 | i++; |
| 230 | } |
214 | } |
| 231 | i=0; |
215 | i=0; |
| 232 | while(i<4){ |
216 | while(i<4){ |
| 233 | remoteip[i]=buf[IP_SRC_P +i]; |
217 | remoteip[i]=buf[IP_SRC_P +i]; |
| 234 | i++; |
218 | i++; |
| 235 | } |
219 | } |
| 236 | i=0; |
220 | i=0; |
| 237 | while(i<BUFFER_SIZE+1){ |
- | |
| 238 | replybuf[i]=buf[i]; |
- | |
| 239 | i++; |
- | |
| 240 | } |
- | |
| 241 | 221 | ||
| 242 | gotserver = 1; |
222 | gotserver = 1; |
| 243 | make_udp_reply_from_request(buf,"Got server",10,remoteport); |
223 | make_udp_reply_from_request(buf,"Got server",10,remoteport); |
| - | 224 | } else { |
|
| - | 225 | make_udp_reply_from_request(buf,"Already got server",18,remoteport); |
|
| - | 226 | } |
|
| 244 | } |
227 | } |
| 245 | } |
228 | } |
| 246 | } |
229 | } |
| 247 | } |
230 | } |
| 248 | } |
231 | } |