Rev 281 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 281 | Rev 379 | ||
|---|---|---|---|
| Line 32... | Line 32... | ||
| 32 | static uint8_t myip[4] = {193,11,254,22}; |
32 | static uint8_t myip[4] = {193,11,254,22}; |
| 33 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
33 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
| 34 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
34 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
| 35 | static uint16_t locDumperPrt =1200; // listen port |
35 | static uint16_t locDumperPrt =1200; // listen port |
| 36 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
36 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
| 37 | static uint8_t |
37 | static uint8_t gotdumpserver = 0; |
| - | 38 | static uint8_t gotcan2serserver = 0; |
|
| 38 | 39 | ||
| 39 | // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX |
40 | // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX |
| 40 | 41 | ||
| 41 | static uint8_t remotemac[6]; |
42 | static uint8_t remotemac[6]; |
| 42 | static uint8_t remoteip[4]; |
43 | static uint8_t remoteip[4]; |
| - | 44 | ||
| - | 45 | static uint8_t prgremotemac[6]; |
|
| - | 46 | static uint8_t prgremoteip[4]; |
|
| 43 | 47 | ||
| 44 | #define BUFFER_SIZE 250 |
48 | #define BUFFER_SIZE 250 |
| 45 | static uint8_t buf[BUFFER_SIZE+1]; |
49 | static uint8_t buf[BUFFER_SIZE+1]; |
| 46 | 50 | ||
| 47 | #define SEND_BUFFER_SIZE 20 |
51 | #define SEND_BUFFER_SIZE 20 |
| Line 56... | Line 60... | ||
| 56 | * Implements a small buffer, sends packet over udp if buffer is full or |
60 | * Implements a small buffer, sends packet over udp if buffer is full or |
| 57 | * \n-char is sent. |
61 | * \n-char is sent. |
| 58 | * Needs a timeout to force send. |
62 | * Needs a timeout to force send. |
| 59 | *--------------------------------------------------------------------------*/ |
63 | *--------------------------------------------------------------------------*/ |
| 60 | int udp_putchar(char c, FILE* stream) { |
64 | int udp_putchar(char c, FILE* stream) { |
| 61 | if ( |
65 | if (gotdumpserver != 0) { |
| 62 | sendbuf[buffpoint] = c; |
66 | sendbuf[buffpoint] = c; |
| 63 | buffpoint++; |
67 | buffpoint++; |
| 64 | 68 | ||
| 65 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
69 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
| 66 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
70 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
| 67 | buffpoint = 0; |
71 | buffpoint = 0; |
| 68 | } |
72 | } |
| 69 | } |
73 | } |
| 70 | return 0; |
74 | return 0; |
| 71 | } |
75 | } |
| 72 | 76 | ||
| 73 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
77 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
| 74 | 78 | ||
| 75 | /*----------------------------------------------------------------------------- |
79 | /*----------------------------------------------------------------------------- |
| 76 | * Main Program |
80 | * Main Program |
| 77 | *---------------------------------------------------------------------------*/ |
81 | *---------------------------------------------------------------------------*/ |
| 78 | int main(void) { |
82 | int main(void) { |
| 79 | 83 | ||
| 80 | //ethernet vars |
84 | //ethernet vars |
| 81 | uint16_t plen = 0; |
85 | uint16_t plen = 0; |
| 82 | uint8_t ledi=0; |
86 | uint8_t ledi=0; |
| 83 | uint8_t payloadlen=0; |
87 | uint8_t payloadlen=0; |
| 84 | buffpoint = 0; |
88 | buffpoint = 0; |
| Line 88... | Line 92... | ||
| 88 | Can_Message_t rxMsg; |
92 | Can_Message_t rxMsg; |
| 89 | txMsg.Id = 3; |
93 | txMsg.Id = 3; |
| 90 | txMsg.DataLength = 0; |
94 | txMsg.DataLength = 0; |
| 91 | txMsg.RemoteFlag = 0; |
95 | txMsg.RemoteFlag = 0; |
| 92 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
96 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
| 93 | 97 | ||
| 94 | 98 | ||
| 95 | uint32_t timeStamp = 0; |
99 | uint32_t timeStamp = 0; |
| 96 | 100 | ||
| 97 | Mcu_Init(); |
101 | Mcu_Init(); |
| 98 | Timebase_Init(); |
102 | Timebase_Init(); |
| 99 | //Serial_Init(); |
103 | //Serial_Init(); |
| 100 | 104 | ||
| 101 | //alla printf ska skriva till udp |
105 | //alla printf ska skriva till udp |
| 102 | stdout = &mystdout; //set the output stream |
106 | stdout = &mystdout; //set the output stream |
| 103 | 107 | ||
| 104 | sei(); |
108 | sei(); |
| 105 | 109 | ||
| 106 | // printf("CanInit..."); |
110 | // printf("CanInit..."); |
| 107 | if (Can_Init() != CAN_OK) { |
111 | if (Can_Init() != CAN_OK) { |
| 108 | // printf("FAILED!\n"); |
112 | // printf("FAILED!\n"); |
| 109 | } |
113 | } |
| 110 | else { |
114 | else { |
| 111 | // printf("OK!\n"); |
115 | // printf("OK!\n"); |
| 112 | } |
116 | } |
| 113 | 117 | ||
| 114 | /* enable PB0, reset as output */ |
118 | /* enable PB0, reset as output */ |
| 115 | DDRB|= (1<<DDB0); |
119 | DDRB|= (1<<DDB0); |
| 116 | /* set output to gnd, reset the ethernet chip */ |
120 | /* set output to gnd, reset the ethernet chip */ |
| 117 | PORTB &= ~(1<<PB0); |
121 | PORTB &= ~(1<<PB0); |
| 118 | delay_ms(20); |
122 | delay_ms(20); |
| 119 | /* set output to Vcc, reset inactive */ |
123 | /* set output to Vcc, reset inactive */ |
| 120 | PORTB|= (1<<PB0); |
124 | PORTB|= (1<<PB0); |
| 121 | delay_ms(100); |
125 | delay_ms(100); |
| 122 | 126 | ||
| 123 | // LED |
127 | // LED |
| Line 161... | Line 165... | ||
| 161 | } |
165 | } |
| 162 | 166 | ||
| 163 | /* check if any messages have been received */ |
167 | /* check if any messages have been received */ |
| 164 | while (Can_Receive(&rxMsg) == CAN_OK) { |
168 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 165 | uint8_t i; |
169 | uint8_t i; |
| - | 170 | ||
| - | 171 | if (gotdumpserver != 0) { |
|
| 166 | 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)); |
172 | 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)); |
| 167 | printf("data={ "); |
173 | printf("data={ "); |
| 168 | for (i=0; i<rxMsg.DataLength; i++) { |
174 | for (i=0; i<rxMsg.DataLength; i++) { |
| 169 | printf("%x ", rxMsg.Data.bytes[i]); |
175 | printf("%x ", rxMsg.Data.bytes[i]); |
| 170 | } |
176 | } |
| 171 | printf("}\n"); |
177 | printf("}\n"); |
| - | 178 | } |
|
| 172 | 179 | ||
| - | 180 | if (gotcan2serserver != 0) { |
|
| 173 | //skicka |
181 | //skicka ocks� som udppaket till rmCan2SerPrt |
| 174 | char sendbuf2[17]; |
182 | char sendbuf2[17]; |
| - | 183 | ||
| 175 | sendbuf2[0] = C2S_START_BYTE; |
184 | sendbuf2[0] = C2S_START_BYTE; |
| 176 | sendbuf2[1] = (uint8_t)rxMsg.Id; |
185 | sendbuf2[1] = (uint8_t)rxMsg.Id; |
| 177 | sendbuf2[2] = (uint8_t)(rxMsg.Id>>8); |
186 | sendbuf2[2] = (uint8_t)(rxMsg.Id>>8); |
| 178 | sendbuf2[3] = (uint8_t)(rxMsg.Id>>16); |
187 | sendbuf2[3] = (uint8_t)(rxMsg.Id>>16); |
| 179 | sendbuf2[4] = (uint8_t)(rxMsg.Id>>24); |
188 | sendbuf2[4] = (uint8_t)(rxMsg.Id>>24); |
| 180 | sendbuf2[5] = rxMsg.ExtendedFlag; |
189 | sendbuf2[5] = rxMsg.ExtendedFlag; |
| 181 | sendbuf2[6] = rxMsg.RemoteFlag; |
190 | sendbuf2[6] = rxMsg.RemoteFlag; |
| 182 | sendbuf2[7] = rxMsg.DataLength; |
191 | sendbuf2[7] = rxMsg.DataLength; |
| 183 | for |
192 | for (i=0; i<8; i++) { |
| 184 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
193 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
| 185 | } |
194 | } |
| 186 | sendbuf2[16] = C2S_END_BYTE; |
195 | sendbuf2[16] = C2S_END_BYTE; |
| - | 196 | ||
| 187 | send_udp(buf, sendbuf2, |
197 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
| - | 198 | } |
|
| 188 | } |
199 | } |
| 189 | 200 | ||
| 190 | //test-debug-kod |
201 | //test-debug-kod |
| 191 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
202 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
| 192 | //printf("EIR_TXERIF set!\n"); |
203 | //printf("EIR_TXERIF set!\n"); |
| 193 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
204 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
| 194 | //printf("ECON1_TXRTS set!\n"); |
205 | //printf("ECON1_TXRTS set!\n"); |
| 195 | } |
206 | } |
| 196 | } |
207 | } |
| 197 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
208 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
| 198 | //printf("EIR_TXIF set! "); |
209 | //printf("EIR_TXIF set! "); |
| 199 | } |
210 | } |
| 200 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
211 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
| 201 | //printf("ESTAT_TXABRT set!\n"); |
212 | //printf("ESTAT_TXABRT set!\n"); |
| 202 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
213 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
| 203 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
214 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
| 204 | } |
215 | } |
| 205 | 216 | ||
| 206 | // get the next new packet: |
217 | // get the next new packet: |
| 207 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
218 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
| 208 | 219 | ||
| 209 | /*plen will ne unequal to zero if there is a valid |
220 | /*plen will ne unequal to zero if there is a valid |
| 210 | * packet (without crc error) */ |
221 | * packet (without crc error) */ |
| Line 215... | Line 226... | ||
| 215 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
226 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
| 216 | make_arp_answer_from_request(buf); |
227 | make_arp_answer_from_request(buf); |
| 217 | } else { |
228 | } else { |
| 218 | // check if ip packets (icmp or udp) are for us: |
229 | // check if ip packets (icmp or udp) are for us: |
| 219 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
230 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
| 220 | 231 | ||
| 221 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
232 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
| 222 | // a ping packet, let's send pong |
233 | // a ping packet, let's send pong |
| 223 | make_echo_reply_from_request(buf,plen); |
234 | make_echo_reply_from_request(buf,plen); |
| 224 | //txMsg.Id = 6; |
235 | //txMsg.Id = 6; |
| 225 | //Can_Send(&txMsg); |
236 | //Can_Send(&txMsg); |
| 226 | 237 | ||
| 227 | } |
238 | } |
| 228 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
239 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
| 229 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
240 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
| - | 241 | uint8_t i = 0; |
|
| - | 242 | ||
| 230 | // |
243 | //om port 1100 (programmeringsporten) |
| 231 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
244 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
| - | 245 | while(i<6){ |
|
| 232 |
|
246 | prgremotemac[i]=buf[ETH_SRC_MAC +i]; |
| - | 247 | i++; |
|
| - | 248 | } |
|
| - | 249 | i=0; |
|
| - | 250 | while(i<4){ |
|
| - | 251 | prgremoteip[i]=buf[IP_SRC_P +i]; |
|
| - | 252 | i++; |
|
| - | 253 | } |
|
| - | 254 | gotcan2serserver = 1; |
|
| - | 255 | ||
| 233 |
|
256 | if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) { |
| 234 | Can_Message_t cm; |
257 | Can_Message_t cm; |
| 235 | //cm.Id = 0; |
258 | //cm.Id = 0; |
| 236 | cm.DataLength = buf[UDP_DATA_P+7]; |
259 | cm.DataLength = buf[UDP_DATA_P+7]; |
| 237 | cm.RemoteFlag = buf[UDP_DATA_P+6]; |
260 | cm.RemoteFlag = buf[UDP_DATA_P+6]; |
| 238 | cm.ExtendedFlag = buf[UDP_DATA_P+5]; |
261 | cm.ExtendedFlag = buf[UDP_DATA_P+5]; |
| 239 | cm.Id = (uint32_t)buf[UDP_DATA_P+1] + ((uint32_t)buf[UDP_DATA_P+2] << 8) + ((uint32_t)buf[UDP_DATA_P+3] << 16) + ((uint32_t)buf[UDP_DATA_P+4] << 24); |
262 | cm.Id = (uint32_t)buf[UDP_DATA_P+1] + ((uint32_t)buf[UDP_DATA_P+2] << 8) + ((uint32_t)buf[UDP_DATA_P+3] << 16) + ((uint32_t)buf[UDP_DATA_P+4] << 24); |
| 240 | uint8_t i; |
263 | uint8_t i; |
| 241 | for (i = 0; i < cm.DataLength; i++) { |
264 | for (i = 0; i < cm.DataLength; i++) { |
| 242 | cm.Data.bytes[i] = buf[UDP_DATA_P+8+i]; |
265 | cm.Data.bytes[i] = buf[UDP_DATA_P+8+i]; |
| 243 | } |
266 | } |
| 244 | Can_Send(&cm); |
267 | Can_Send(&cm); |
| - | 268 | ||
| - | 269 | if (gotdumpserver != 0) { |
|
| - | 270 | 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)); |
|
| - | 271 | printf("data={ "); |
|
| - | 272 | for (i=0; i<cm.DataLength; i++) { |
|
| - | 273 | printf("%x ", cm.Data.bytes[i]); |
|
| - | 274 | } |
|
| - | 275 | printf("}\n"); |
|
| 245 | } |
276 | } |
| 246 | printf("hoj\n"); |
- | |
| 247 | } |
277 | } |
| 248 |
|
278 | //om port 1200 (dumparporten) |
| 249 |
|
279 | } else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) { |
| 250 | if ( |
280 | if (gotdumpserver == 0) { |
| 251 | while(i<6){ |
281 | while(i<6){ |
| 252 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
282 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
| 253 | i++; |
283 | i++; |
| 254 | } |
284 | } |
| 255 | i=0; |
285 | i=0; |
| Line 257... | Line 287... | ||
| 257 | remoteip[i]=buf[IP_SRC_P +i]; |
287 | remoteip[i]=buf[IP_SRC_P +i]; |
| 258 | i++; |
288 | i++; |
| 259 | } |
289 | } |
| 260 | i=0; |
290 | i=0; |
| 261 | 291 | ||
| 262 |
|
292 | gotdumpserver = 1; |
| 263 | make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt); |
293 | make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt); |
| 264 | } else { |
294 | } else { |
| 265 | make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt); |
295 | make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt); |
| 266 | } |
296 | } |
| 267 | } |
297 | } |