Rev 1686 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1686 | Rev 1696 | ||
|---|---|---|---|
| Line 35... | Line 35... | ||
| 35 | #include <net.h> |
35 | #include <net.h> |
| 36 | 36 | ||
| 37 | static uint8_t mymac[6] = {ENC28J60_MAC1,ENC28J60_MAC2,ENC28J60_MAC3,ENC28J60_MAC4,ENC28J60_MAC5,ENC28J60_MAC6}; |
37 | static uint8_t mymac[6] = {ENC28J60_MAC1,ENC28J60_MAC2,ENC28J60_MAC3,ENC28J60_MAC4,ENC28J60_MAC5,ENC28J60_MAC6}; |
| 38 | //static uint8_t myip[4] = {192,168,0,50}; |
38 | //static uint8_t myip[4] = {192,168,0,50}; |
| 39 | static uint8_t myip[4] = {ENC28J60_IP1,ENC28J60_IP2,ENC28J60_IP3,ENC28J60_IP4}; |
39 | static uint8_t myip[4] = {ENC28J60_IP1,ENC28J60_IP2,ENC28J60_IP3,ENC28J60_IP4}; |
| 40 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
- | |
| 41 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
40 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
| 42 | //static uint16_t locDumperPrt =1200; // listen port |
- | |
| 43 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
41 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
| 44 | static uint8_t gotdumpserver = 0; |
- | |
| 45 | static uint8_t gotcan2serserver = 0; |
42 | static uint8_t gotcan2serserver = 0; |
| 46 | - | ||
| 47 | - | ||
| 48 | static uint8_t remotemac[6]; |
- | |
| 49 | static uint8_t remoteip[4]; |
- | |
| 50 | 43 | ||
| 51 | static uint8_t prgremotemac[6]; |
44 | static uint8_t prgremotemac[6]; |
| 52 | static uint8_t prgremoteip[4]; |
45 | static uint8_t prgremoteip[4]; |
| 53 | 46 | ||
| 54 | #define BUFFER_SIZE 250 |
47 | #define BUFFER_SIZE 250 |
| 55 | static uint8_t buf[BUFFER_SIZE+1]; |
48 | static uint8_t buf[BUFFER_SIZE+1]; |
| 56 | 49 | ||
| 57 | #define SEND_BUFFER_SIZE 20 |
- | |
| 58 | static char sendbuf[SEND_BUFFER_SIZE+1]; |
- | |
| 59 | static uint8_t buffpoint; |
50 | static uint8_t buffpoint; |
| 60 | 51 | ||
| 61 | #define C2S_START_BYTE 253 |
52 | #define C2S_START_BYTE 253 |
| 62 | #define C2S_END_BYTE 250 |
53 | #define C2S_END_BYTE 250 |
| 63 | #define C2S_PING_BYTE 251 |
54 | #define C2S_PING_BYTE 251 |
| Line 65... | Line 56... | ||
| 65 | #if USE_STDCAN == 0 |
56 | #if USE_STDCAN == 0 |
| 66 | volatile Can_Message_t rxMsg; // Message storage |
57 | volatile Can_Message_t rxMsg; // Message storage |
| 67 | volatile uint8_t rxMsgFull; // Synchronization flag |
58 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 68 | #endif |
59 | #endif |
| 69 | 60 | ||
| - | 61 | ||
| - | 62 | /* |
|
| - | 63 | #define SEND_BUFFER_SIZE 20 |
|
| - | 64 | static char sendbuf[SEND_BUFFER_SIZE+1]; |
|
| - | 65 | static uint16_t locDumperPrt =1200; // listen port |
|
| - | 66 | static uint8_t gotdumpserver = 0; |
|
| - | 67 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
|
| - | 68 | static uint8_t remotemac[6]; |
|
| - | 69 | static uint8_t remoteip[4]; |
|
| - | 70 | */ |
|
| 70 | /*---------------------------------------------------------------------------- |
71 | /*---------------------------------------------------------------------------- |
| 71 | * Putchar for udp |
72 | * Putchar for udp |
| 72 | * Implements a small buffer, sends packet over udp if buffer is full or |
73 | * Implements a small buffer, sends packet over udp if buffer is full or |
| 73 | * \n-char is sent. |
74 | * \n-char is sent. |
| 74 | * Needs a timeout to force send. |
75 | * Needs a timeout to force send. |
| Line 150... | Line 151... | ||
| 150 | sei(); |
151 | sei(); |
| 151 | 152 | ||
| 152 | /*initialize enc28j60*/ |
153 | /* initialize enc28j60 */ |
| 153 | if (enc28j60Init(mymac) != 1) { |
154 | if (enc28j60Init(mymac) != 1) { |
| 154 | /* Great place to set an IO for debug */ |
155 | /* Great place to set an IO for debug */ |
| 155 | } |
156 | } |
| 156 | delay_ms(20); |
157 | delay_ms(20); |
| 157 | 158 | ||
| 158 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
159 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
| 159 | // LEDB=yellow LEDA=green |
160 | // LEDB=yellow LEDA=green |
| 160 | // |
161 | // |
| Line 162... | Line 163... | ||
| 162 | enc28j60PhyWrite(PHLCON,0x476); |
163 | enc28j60PhyWrite(PHLCON,0x476); |
| 163 | delay_ms(20); |
164 | delay_ms(20); |
| 164 | 165 | ||
| 165 | //init the ethernet/ip layer: |
166 | //init the ethernet/ip layer: |
| 166 | init_ip_arp_udp_tcp(mymac,myip,80); |
167 | init_ip_arp_udp_tcp(mymac,myip,80); |
| 167 | 168 | ||
| 168 | /* main loop */ |
169 | /* main loop */ |
| 169 | while (1) { |
170 | while (1) { |
| 170 | /* service the CAN routines */ |
171 | /* service the CAN routines */ |
| 171 | //Can_Service(); |
- | |
| 172 | 172 | ||
| 173 | #if SENDTIMESTAMP == 1 |
173 | #if SENDTIMESTAMP == 1 |
| 174 | /* send CAN message and check for CAN errors once every second */ |
174 | /* send CAN message and check for CAN errors once every second */ |
| 175 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
175 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 176 | timeStamp = Timebase_CurrentTime(); |
176 | timeStamp = Timebase_CurrentTime(); |
| Line 182... | Line 182... | ||
| 182 | Can_Send(&txMsg); |
182 | Can_Send(&txMsg); |
| 183 | #else |
183 | #else |
| 184 | txMsg.Length = 0; |
184 | txMsg.Length = 0; |
| 185 | StdCan_Put(&txMsg); |
185 | StdCan_Put(&txMsg); |
| 186 | #endif |
186 | #endif |
| 187 | - | ||
| 188 | } |
187 | } |
| 189 | #endif |
188 | #endif |
| 190 | 189 | ||
| 191 | /* check if any messages have been received */ |
190 | /* check if any messages have been received */ |
| 192 | #if USE_STDCAN == 0 |
191 | #if USE_STDCAN == 0 |
| 193 | if (rxMsgFull) { |
192 | if (rxMsgFull) { |
| 194 | #else |
193 | #else |
| 195 | if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) { |
194 | if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) { |
| 196 | #endif |
195 | #endif |
| 197 | uint8_t i; |
196 | uint8_t i; |
| 198 | - | ||
| 199 | /*if (gotdumpserver != 0) { |
- | |
| 200 | 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)); |
- | |
| 201 | printf("data={ "); |
- | |
| 202 |
|
197 | /* Do we have any ip to send the can data to yet? */ |
| 203 | printf("%x ", rxMsg.Data.bytes[i]); |
- | |
| 204 | } |
- | |
| 205 | printf("}\n"); |
- | |
| 206 | }*/ |
- | |
| 207 | - | ||
| 208 | if (gotcan2serserver != 0) { |
198 | if (gotcan2serserver != 0) { |
| 209 |
|
199 | /* Send can frame raw in an udp packet */ |
| 210 | char sendbuf2[17]; |
200 | char sendbuf2[17]; |
| 211 | 201 | ||
| 212 | sendbuf2[0] = C2S_START_BYTE; |
202 | sendbuf2[0] = C2S_START_BYTE; |
| 213 | sendbuf2[1] = (uint8_t)rxMsg.Id; |
203 | sendbuf2[1] = (uint8_t)rxMsg.Id; |
| 214 | sendbuf2[2] = (uint8_t)(rxMsg.Id>>8); |
204 | sendbuf2[2] = (uint8_t)(rxMsg.Id>>8); |
| Line 218... | Line 208... | ||
| 218 | sendbuf2[5] = rxMsg.ExtendedFlag; |
208 | sendbuf2[5] = rxMsg.ExtendedFlag; |
| 219 | sendbuf2[6] = rxMsg.RemoteFlag; |
209 | sendbuf2[6] = rxMsg.RemoteFlag; |
| 220 | sendbuf2[7] = rxMsg.DataLength; |
210 | sendbuf2[7] = rxMsg.DataLength; |
| 221 | for (i=0; i<8; i++) { |
211 | for (i=0; i<8; i++) { |
| 222 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
212 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
| 223 | } |
213 | } |
| 224 | #else |
214 | #else |
| 225 | sendbuf2[5] = 1; |
215 | sendbuf2[5] = 1; |
| 226 | sendbuf2[6] = 0; |
216 | sendbuf2[6] = 0; |
| 227 | sendbuf2[7] = rxMsg.Length; |
217 | sendbuf2[7] = rxMsg.Length; |
| 228 | for (i=0; i<8; i++) { |
218 | for (i=0; i<8; i++) { |
| Line 232... | Line 222... | ||
| 232 | sendbuf2[16] = C2S_END_BYTE; |
222 | sendbuf2[16] = C2S_END_BYTE; |
| 233 | 223 | ||
| 234 | #ifndef ENC28J60_USART_SPI_MODE |
224 | #ifndef ENC28J60_USART_SPI_MODE |
| 235 | //MCP_INT_DISABLE(); |
225 | //MCP_INT_DISABLE(); |
| 236 | cli(); |
226 | cli(); |
| 237 | #endif |
227 | #endif |
| 238 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
228 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
| 239 | #ifndef ENC28J60_USART_SPI_MODE |
229 | #ifndef ENC28J60_USART_SPI_MODE |
| 240 | sei(); |
230 | sei(); |
| 241 | //MCP_INT_ENABLE(); |
231 | //MCP_INT_ENABLE(); |
| 242 | #endif |
232 | #endif |
| 243 | } |
233 | } |
| 244 | 234 | ||
| 245 | #if USE_STDCAN == 0 |
235 | #if USE_STDCAN == 0 |
| 246 | rxMsgFull = 0; |
236 | rxMsgFull = 0; |
| 247 | #endif |
237 | #endif |
| 248 | } |
238 | } |
| 249 | 239 | ||
| 250 | //test-debug-kod |
240 | //test-debug-kod |
| 251 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
241 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
| 252 | //printf("EIR_TXERIF set!\n"); |
242 | //printf("EIR_TXERIF set!\n"); |
| 253 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
243 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
| 254 | //printf("ECON1_TXRTS set!\n"); |
244 | //printf("ECON1_TXRTS set!\n"); |
| 255 | } |
245 | } |
| 256 | } |
246 | } |
| 257 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
247 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
| 258 | //printf("EIR_TXIF set! "); |
248 | //printf("EIR_TXIF set! "); |
| 259 | } |
249 | } |
| 260 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
250 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
| 261 | //printf("ESTAT_TXABRT set!\n"); |
251 | //printf("ESTAT_TXABRT set!\n"); |
| 262 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
252 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
| 263 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
253 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
| 264 | } |
254 | } |
| 265 | 255 | ||
| 266 | // get the next new packet: |
256 | // get the next new packet: |
| 267 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
257 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
| 268 | 258 | ||
| 269 | /*plen will ne unequal to zero if there is a valid |
259 | /*plen will ne unequal to zero if there is a valid |
| 270 | * packet (without crc error) */ |
260 | * packet (without crc error) */ |
| 271 | if(plen!=0){ |
261 | if (plen!=0){ |
| 272 | // arp is broadcast if unknown but a host may also |
262 | // arp is broadcast if unknown but a host may also |
| 273 | // verify the mac address by sending it to |
263 | // verify the mac address by sending it to |
| 274 | // a unicast address. |
264 | // a unicast address. |
| 275 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
265 | if (eth_type_is_arp_and_my_ip(buf,plen)) { |
| 276 | make_arp_answer_from_request(buf); |
266 | make_arp_answer_from_request(buf); |
| 277 | } else { |
267 | } else { |
| 278 |
|
268 | /* check if ip packets (icmp or udp) are for us by checking eth type and receiver ip adress: */ |
| 279 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
269 | if (eth_type_is_ip_and_my_ip(buf,plen)!=0) { |
| 280 | 270 | ||
| - | 271 | /* a ping packet, let's send pong */ |
|
| 281 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
272 | if (buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V) { |
| 282 | // a ping packet, let's send pong |
- | |
| 283 | make_echo_reply_from_request(buf,plen); |
273 | make_echo_reply_from_request(buf,plen); |
| 284 |
|
274 | } |
| 285 | //Can_Send(&txMsg); |
- | |
| 286 | 275 | ||
| 287 |
|
276 | /* If packet is an udp packet */ |
| 288 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
277 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V) { |
| 289 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
278 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
| 290 | uint8_t i = 0; |
279 | uint8_t i = 0; |
| 291 | 280 | ||
| 292 |
|
281 | /* if port is can2serial-port (default 1100) */ |
| 293 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
282 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
| 294 | while(i<6){ |
283 | while(i<6) { |
| 295 | prgremotemac[i]=buf[ETH_SRC_MAC +i]; |
284 | prgremotemac[i]=buf[ETH_SRC_MAC +i]; |
| 296 | i++; |
285 | i++; |
| 297 | } |
286 | } |
| Line 300... | Line 289... | ||
| 300 | prgremoteip[i]=buf[IP_SRC_P +i]; |
289 | prgremoteip[i]=buf[IP_SRC_P +i]; |
| 301 | i++; |
290 | i++; |
| 302 | } |
291 | } |
| 303 | gotcan2serserver = 1; |
292 | gotcan2serserver = 1; |
| 304 | 293 | ||
| - | 294 | /* If data is a raw can packet */ |
|
| 305 | if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) |
295 | if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) |
| 306 | { |
296 | { |
| 307 | #if USE_STDCAN == 0 |
297 | #if USE_STDCAN == 0 |
| 308 | Can_Message_t cm; |
298 | Can_Message_t cm; |
| 309 | //cm.Id = 0; |
- | |
| 310 | cm.DataLength = buf[UDP_DATA_P+7]; |
299 | cm.DataLength = buf[UDP_DATA_P+7]; |
| 311 | 300 | ||
| 312 | if (cm.DataLength > 8) |
301 | if (cm.DataLength > 8) |
| 313 | { |
302 | { |
| 314 | cm.DataLength = 8; |
303 | cm.DataLength = 8; |
| Line 323... | Line 312... | ||
| 323 | cm.Data.bytes[i] = buf[UDP_DATA_P+8+i]; |
312 | cm.Data.bytes[i] = buf[UDP_DATA_P+8+i]; |
| 324 | } |
313 | } |
| 325 | Can_Send(&cm); |
314 | Can_Send(&cm); |
| 326 | #else |
315 | #else |
| 327 | static StdCan_Msg_t cm; |
316 | static StdCan_Msg_t cm; |
| 328 | //cm.Id = 0; |
- | |
| 329 | cm.Length = buf[UDP_DATA_P+7]; |
317 | cm.Length = buf[UDP_DATA_P+7]; |
| 330 | 318 | ||
| 331 | if (cm.Length > 8) |
319 | if (cm.Length > 8) |
| 332 | { |
320 | { |
| 333 | cm.Length = 8; |
321 | cm.Length = 8; |
| Line 339... | Line 327... | ||
| 339 | for (i = 0; i < cm.Length; i++) { |
327 | for (i = 0; i < cm.Length; i++) { |
| 340 | cm.Data[i] = buf[UDP_DATA_P+8+i]; |
328 | cm.Data[i] = buf[UDP_DATA_P+8+i]; |
| 341 | } |
329 | } |
| 342 | StdCan_Put(&cm); |
330 | StdCan_Put(&cm); |
| 343 | #endif |
331 | #endif |
| 344 | /*if (gotdumpserver != 0) { |
- | |
| 345 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", cm.Id, (uint16_t)(cm.DataLength), (uint16_t)(cm.ExtendedFlag), (uint16_t)(cm.RemoteFlag)); |
- | |
| 346 | printf("data={ "); |
- | |
| 347 | for (i=0; i<cm.DataLength; i++) { |
- | |
| 348 | printf("%x ", cm.Data.bytes[i]); |
- | |
| 349 | } |
- | |
| 350 | printf("}\n"); |
- | |
| 351 | }*/ |
- | |
| 352 | } |
332 | } |
| - | 333 | /* of ir data is an udp ping */ |
|
| 353 | else if ((buf[UDP_DATA_P]==C2S_PING_BYTE) && (payloadlen |
334 | else if ((buf[UDP_DATA_P]==C2S_PING_BYTE) && (payloadlen<=2) && (payloadlen>0)) |
| 354 | { |
335 | { |
| 355 | char sendbuf3[1]; |
336 | char sendbuf3[1]; |
| 356 | sendbuf3[0] = C2S_PING_BYTE; |
337 | sendbuf3[0] = C2S_PING_BYTE; |
| 357 | 338 | ||
| 358 | #ifndef ENC28J60_USART_SPI_MODE |
339 | #ifndef ENC28J60_USART_SPI_MODE |
| 359 | cli(); |
340 | cli(); |
| 360 | #endif |
341 | #endif |
| 361 | send_udp(buf, sendbuf3, 1, rmCan2SerPrt, prgremotemac, prgremoteip); |
342 | send_udp(buf, sendbuf3, 1, rmCan2SerPrt, prgremotemac, prgremoteip); |
| 362 | #ifndef ENC28J60_USART_SPI_MODE |
343 | #ifndef ENC28J60_USART_SPI_MODE |
| 363 | sei(); |
344 | sei(); |
| 364 | #endif |
345 | #endif |
| 365 | } |
346 | } |
| 366 | //om port 1200 (dumparporten) |
- | |
| 367 | } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) { |
- | |
| 368 | if (gotdumpserver == 0) { |
- | |
| 369 | while(i<6){ |
- | |
| 370 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
- | |
| 371 | i++; |
- | |
| 372 | } |
- | |
| 373 | i=0; |
- | |
| 374 | while(i<4){ |
- | |
| 375 | remoteip[i]=buf[IP_SRC_P +i]; |
- | |
| 376 | i++; |
- | |
| 377 | } |
- | |
| 378 | i=0; |
- | |
| 379 | |
- | |
| 380 | gotdumpserver = 1; |
- | |
| 381 | make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt); |
- | |
| 382 | } else { |
- | |
| 383 | make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt); |
- | |
| 384 | } |
- | |
| 385 | }*/ |
- | |
| 386 | } |
- | |
| 387 | } |
- | |
| 388 | } |
- | |
| 389 | } |
- | |
| 390 | - | ||
| 391 | - | ||
| 392 | - | ||
| 393 | - | ||
| 394 | } |
347 | } |
| - | 348 | } |
|
| - | 349 | } |
|
| - | 350 | } |
|
| - | 351 | } |
|
| 395 | 352 | } |
|
| 396 | return 0; |
353 | return 0; |
| 397 | } |
354 | } |
| - | 355 | ||