Rev 730 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 205 | arune | 1 | /** |
| 2 | * |
||
| 280 | arune | 3 | * |
| 730 | arune | 4 | * |
| 205 | arune | 5 | * @date 2006-11-21 |
| 280 | arune | 6 | * @author Jimmy Myhrman, Anders Runeson |
| 205 | arune | 7 | * |
| 8 | */ |
||
| 9 | |||
| 10 | /*----------------------------------------------------------------------------- |
||
| 11 | * Includes |
||
| 12 | *---------------------------------------------------------------------------*/ |
||
| 13 | /* system files */ |
||
| 14 | #include <avr/io.h> |
||
| 15 | #include <avr/interrupt.h> |
||
| 16 | #include <avr/wdt.h> |
||
| 17 | #include <stdio.h> |
||
| 661 | arune | 18 | #include <string.h> |
| 19 | |||
| 205 | arune | 20 | /* lib files */ |
| 661 | arune | 21 | #include <mcp2515.h> |
| 280 | arune | 22 | //#include <serial.h> |
| 205 | arune | 23 | #include <timebase.h> |
| 661 | arune | 24 | #include <mcu.h> |
| 205 | arune | 25 | #include <ip_arp_udp_tcp.h> |
| 26 | #include <enc28j60.h> |
||
| 27 | #include <timeout.h> |
||
| 28 | #include <avr_compat.h> |
||
| 29 | #include <net.h> |
||
| 30 | |||
| 680 | arune | 31 | static uint8_t mymac[6] = {ENC28J60_MAC1,ENC28J60_MAC2,ENC28J60_MAC3,ENC28J60_MAC4,ENC28J60_MAC5,ENC28J60_MAC6}; |
| 541 | arune | 32 | //static uint8_t myip[4] = {192,168,0,50}; |
| 680 | arune | 33 | static uint8_t myip[4] = {ENC28J60_IP1,ENC28J60_IP2,ENC28J60_IP3,ENC28J60_IP4}; |
| 280 | arune | 34 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
| 35 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
||
| 541 | arune | 36 | //static uint16_t locDumperPrt =1200; // listen port |
| 280 | arune | 37 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
| 379 | arune | 38 | static uint8_t gotdumpserver = 0; |
| 39 | static uint8_t gotcan2serserver = 0; |
||
| 205 | arune | 40 | |
| 41 | |||
| 42 | static uint8_t remotemac[6]; |
||
| 43 | static uint8_t remoteip[4]; |
||
| 44 | |||
| 379 | arune | 45 | static uint8_t prgremotemac[6]; |
| 46 | static uint8_t prgremoteip[4]; |
||
| 47 | |||
| 205 | arune | 48 | #define BUFFER_SIZE 250 |
| 49 | static uint8_t buf[BUFFER_SIZE+1]; |
||
| 50 | |||
| 212 | arune | 51 | #define SEND_BUFFER_SIZE 20 |
| 52 | static char sendbuf[SEND_BUFFER_SIZE+1]; |
||
| 53 | static uint8_t buffpoint; |
||
| 54 | |||
| 281 | arune | 55 | #define C2S_START_BYTE 253 |
| 56 | #define C2S_END_BYTE 250 |
||
| 57 | |||
| 541 | arune | 58 | /* set to 0 for not sending timestamps on bus */ |
| 661 | arune | 59 | #define SENDTIMESTAMP 0 |
| 541 | arune | 60 | |
| 661 | arune | 61 | volatile Can_Message_t rxMsg; // Message storage |
| 62 | volatile uint8_t rxMsgFull; // Synchronization flag |
||
| 63 | |||
| 212 | arune | 64 | /*---------------------------------------------------------------------------- |
| 65 | * Putchar for udp |
||
| 66 | * Implements a small buffer, sends packet over udp if buffer is full or |
||
| 67 | * \n-char is sent. |
||
| 68 | * Needs a timeout to force send. |
||
| 69 | *--------------------------------------------------------------------------*/ |
||
| 661 | arune | 70 | /*int udp_putchar(char c, FILE* stream) { |
| 379 | arune | 71 | if (gotdumpserver != 0) { |
| 212 | arune | 72 | sendbuf[buffpoint] = c; |
| 73 | buffpoint++; |
||
| 74 | |||
| 75 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
||
| 280 | arune | 76 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
| 212 | arune | 77 | buffpoint = 0; |
| 205 | arune | 78 | } |
| 79 | } |
||
| 80 | return 0; |
||
| 81 | } |
||
| 82 | |||
| 83 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
||
| 661 | arune | 84 | */ |
| 205 | arune | 85 | |
| 661 | arune | 86 | void Can_Process(Can_Message_t* msg) { |
| 87 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
||
| 88 | |||
| 89 | if (!rxMsgFull) { |
||
| 90 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
||
| 91 | rxMsgFull = 1; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 205 | arune | 95 | /*----------------------------------------------------------------------------- |
| 96 | * Main Program |
||
| 97 | *---------------------------------------------------------------------------*/ |
||
| 98 | int main(void) { |
||
| 99 | |||
| 100 | //ethernet vars |
||
| 101 | uint16_t plen = 0; |
||
| 102 | uint8_t payloadlen=0; |
||
| 212 | arune | 103 | buffpoint = 0; |
| 205 | arune | 104 | |
| 105 | //can vars |
||
| 106 | Can_Message_t txMsg; |
||
| 107 | txMsg.Id = 3; |
||
| 108 | txMsg.DataLength = 0; |
||
| 109 | txMsg.RemoteFlag = 0; |
||
| 541 | arune | 110 | txMsg.ExtendedFlag = 1; |
| 212 | arune | 111 | |
| 661 | arune | 112 | rxMsgFull = 0; |
| 113 | |||
| 212 | arune | 114 | uint32_t timeStamp = 0; |
| 661 | arune | 115 | |
| 116 | //uint8_t sw=0; |
||
| 117 | // LED |
||
| 118 | /* enable PB1, LED as output */ |
||
| 119 | //DDRB|= (1<<DDB0); |
||
| 120 | |||
| 121 | /* set output to Vcc, LED off */ |
||
| 122 | //PORTB|= (1<<PB0); |
||
| 205 | arune | 123 | |
| 212 | arune | 124 | Mcu_Init(); |
| 205 | arune | 125 | Timebase_Init(); |
| 212 | arune | 126 | |
| 127 | //alla printf ska skriva till udp |
||
| 661 | arune | 128 | //stdout = &mystdout; //set the output stream |
| 205 | arune | 129 | |
| 130 | if (Can_Init() != CAN_OK) { |
||
| 131 | } |
||
| 132 | |||
| 661 | arune | 133 | sei(); |
| 541 | arune | 134 | |
| 205 | arune | 135 | /*initialize enc28j60*/ |
| 669 | arune | 136 | if (enc28j60Init(mymac) != 1) { |
| 137 | /* Great place to set an IO for debug */ |
||
| 138 | } |
||
| 205 | arune | 139 | delay_ms(20); |
| 140 | |||
| 141 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
||
| 142 | // LEDB=yellow LEDA=green |
||
| 143 | // |
||
| 144 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
||
| 145 | enc28j60PhyWrite(PHLCON,0x476); |
||
| 146 | delay_ms(20); |
||
| 147 | |||
| 148 | //init the ethernet/ip layer: |
||
| 149 | init_ip_arp_udp_tcp(mymac,myip,80); |
||
| 150 | |||
| 151 | /* main loop */ |
||
| 152 | while (1) { |
||
| 153 | /* service the CAN routines */ |
||
| 661 | arune | 154 | //Can_Service(); |
| 205 | arune | 155 | |
| 541 | arune | 156 | #if SENDTIMESTAMP == 1 |
| 205 | arune | 157 | /* send CAN message and check for CAN errors once every second */ |
| 541 | arune | 158 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 205 | arune | 159 | timeStamp = Timebase_CurrentTime(); |
| 541 | arune | 160 | /* send timestamp, to keep network alive */ |
| 161 | txMsg.Id = 0; |
||
| 162 | txMsg.DataLength = 0; |
||
| 163 | Can_Send(&txMsg); |
||
| 205 | arune | 164 | |
| 165 | } |
||
| 541 | arune | 166 | #endif |
| 205 | arune | 167 | |
| 168 | /* check if any messages have been received */ |
||
| 661 | arune | 169 | if (rxMsgFull) { //(Can_Receive(&rxMsg) == CAN_OK) { |
| 281 | arune | 170 | uint8_t i; |
| 661 | arune | 171 | |
| 541 | arune | 172 | /*if (gotdumpserver != 0) { |
| 379 | arune | 173 | 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)); |
| 174 | printf("data={ "); |
||
| 175 | for (i=0; i<rxMsg.DataLength; i++) { |
||
| 176 | printf("%x ", rxMsg.Data.bytes[i]); |
||
| 177 | } |
||
| 178 | printf("}\n"); |
||
| 541 | arune | 179 | }*/ |
| 212 | arune | 180 | |
| 379 | arune | 181 | if (gotcan2serserver != 0) { |
| 182 | //skicka ocks� som udppaket till rmCan2SerPrt |
||
| 183 | char sendbuf2[17]; |
||
| 184 | |||
| 185 | sendbuf2[0] = C2S_START_BYTE; |
||
| 186 | sendbuf2[1] = (uint8_t)rxMsg.Id; |
||
| 187 | sendbuf2[2] = (uint8_t)(rxMsg.Id>>8); |
||
| 188 | sendbuf2[3] = (uint8_t)(rxMsg.Id>>16); |
||
| 189 | sendbuf2[4] = (uint8_t)(rxMsg.Id>>24); |
||
| 190 | sendbuf2[5] = rxMsg.ExtendedFlag; |
||
| 191 | sendbuf2[6] = rxMsg.RemoteFlag; |
||
| 192 | sendbuf2[7] = rxMsg.DataLength; |
||
| 193 | for (i=0; i<8; i++) { |
||
| 194 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
||
| 195 | } |
||
| 196 | sendbuf2[16] = C2S_END_BYTE; |
||
| 197 | |||
| 661 | arune | 198 | MCP_INT_DISABLE(); |
| 379 | arune | 199 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
| 661 | arune | 200 | MCP_INT_ENABLE(); |
| 379 | arune | 201 | } |
| 661 | arune | 202 | |
| 203 | rxMsgFull = 0; |
||
| 205 | arune | 204 | } |
| 205 | |||
| 212 | arune | 206 | //test-debug-kod |
| 207 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
||
| 208 | //printf("EIR_TXERIF set!\n"); |
||
| 209 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
||
| 210 | //printf("ECON1_TXRTS set!\n"); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
||
| 214 | //printf("EIR_TXIF set! "); |
||
| 215 | } |
||
| 216 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
||
| 217 | //printf("ESTAT_TXABRT set!\n"); |
||
| 218 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
||
| 219 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
||
| 220 | } |
||
| 221 | |||
| 205 | arune | 222 | // get the next new packet: |
| 223 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
||
| 224 | |||
| 225 | /*plen will ne unequal to zero if there is a valid |
||
| 226 | * packet (without crc error) */ |
||
| 227 | if(plen!=0){ |
||
| 228 | // arp is broadcast if unknown but a host may also |
||
| 229 | // verify the mac address by sending it to |
||
| 230 | // a unicast address. |
||
| 231 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
||
| 232 | make_arp_answer_from_request(buf); |
||
| 233 | } else { |
||
| 234 | // check if ip packets (icmp or udp) are for us: |
||
| 235 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
||
| 236 | |||
| 237 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
||
| 238 | // a ping packet, let's send pong |
||
| 239 | make_echo_reply_from_request(buf,plen); |
||
| 212 | arune | 240 | //txMsg.Id = 6; |
| 241 | //Can_Send(&txMsg); |
||
| 205 | arune | 242 | |
| 243 | } |
||
| 244 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
||
| 245 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
||
| 379 | arune | 246 | uint8_t i = 0; |
| 247 | |||
| 248 | //om port 1100 (programmeringsporten) |
||
| 280 | arune | 249 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
| 379 | arune | 250 | while(i<6){ |
| 251 | prgremotemac[i]=buf[ETH_SRC_MAC +i]; |
||
| 252 | i++; |
||
| 253 | } |
||
| 254 | i=0; |
||
| 255 | while(i<4){ |
||
| 256 | prgremoteip[i]=buf[IP_SRC_P +i]; |
||
| 257 | i++; |
||
| 258 | } |
||
| 259 | gotcan2serserver = 1; |
||
| 260 | |||
| 261 | if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) { |
||
| 262 | Can_Message_t cm; |
||
| 263 | //cm.Id = 0; |
||
| 264 | cm.DataLength = buf[UDP_DATA_P+7]; |
||
| 265 | cm.RemoteFlag = buf[UDP_DATA_P+6]; |
||
| 266 | cm.ExtendedFlag = buf[UDP_DATA_P+5]; |
||
| 267 | 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); |
||
| 268 | uint8_t i; |
||
| 269 | for (i = 0; i < cm.DataLength; i++) { |
||
| 270 | cm.Data.bytes[i] = buf[UDP_DATA_P+8+i]; |
||
| 271 | } |
||
| 272 | Can_Send(&cm); |
||
| 273 | |||
| 541 | arune | 274 | /*if (gotdumpserver != 0) { |
| 379 | arune | 275 | 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)); |
| 276 | printf("data={ "); |
||
| 277 | for (i=0; i<cm.DataLength; i++) { |
||
| 278 | printf("%x ", cm.Data.bytes[i]); |
||
| 281 | arune | 279 | } |
| 379 | arune | 280 | printf("}\n"); |
| 541 | arune | 281 | }*/ |
| 281 | arune | 282 | } |
| 379 | arune | 283 | //om port 1200 (dumparporten) |
| 541 | arune | 284 | } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) { |
| 379 | arune | 285 | if (gotdumpserver == 0) { |
| 212 | arune | 286 | while(i<6){ |
| 287 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
||
| 288 | i++; |
||
| 289 | } |
||
| 290 | i=0; |
||
| 291 | while(i<4){ |
||
| 292 | remoteip[i]=buf[IP_SRC_P +i]; |
||
| 293 | i++; |
||
| 294 | } |
||
| 295 | i=0; |
||
| 296 | |||
| 379 | arune | 297 | gotdumpserver = 1; |
| 280 | arune | 298 | make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt); |
| 212 | arune | 299 | } else { |
| 280 | arune | 300 | make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt); |
| 212 | arune | 301 | } |
| 541 | arune | 302 | }*/ |
| 205 | arune | 303 | } |
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | } |
||
| 312 | |||
| 313 | return 0; |
||
| 314 | } |