Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 205 | arune | 1 | /** |
| 2 | * |
||
| 280 | arune | 3 | * |
| 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> |
||
| 18 | /* lib files */ |
||
| 19 | #include <can.h> |
||
| 280 | arune | 20 | //#include <serial.h> |
| 205 | arune | 21 | #include <timebase.h> |
| 22 | #include <ip_arp_udp_tcp.h> |
||
| 23 | #include <enc28j60.h> |
||
| 24 | #include <timeout.h> |
||
| 25 | #include <avr_compat.h> |
||
| 26 | #include <net.h> |
||
| 27 | |||
| 28 | // please modify the following two lines. mac and ip have to be unique |
||
| 29 | // in your local area network. You can not have the same numbers in |
||
| 30 | // two devices: |
||
| 31 | static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; |
||
| 32 | static uint8_t myip[4] = {193,11,254,22}; |
||
| 280 | arune | 33 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
| 34 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
||
| 35 | static uint16_t locDumperPrt =1200; // listen port |
||
| 36 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
||
| 205 | arune | 37 | static uint8_t gotserver = 0; |
| 38 | |||
| 39 | // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX |
||
| 40 | |||
| 41 | static uint8_t remotemac[6]; |
||
| 42 | static uint8_t remoteip[4]; |
||
| 43 | |||
| 44 | #define BUFFER_SIZE 250 |
||
| 45 | static uint8_t buf[BUFFER_SIZE+1]; |
||
| 46 | |||
| 212 | arune | 47 | #define SEND_BUFFER_SIZE 20 |
| 48 | static char sendbuf[SEND_BUFFER_SIZE+1]; |
||
| 49 | static uint8_t buffpoint; |
||
| 50 | |||
| 51 | /*---------------------------------------------------------------------------- |
||
| 52 | * Putchar for udp |
||
| 53 | * Implements a small buffer, sends packet over udp if buffer is full or |
||
| 54 | * \n-char is sent. |
||
| 55 | * Needs a timeout to force send. |
||
| 56 | *--------------------------------------------------------------------------*/ |
||
| 205 | arune | 57 | int udp_putchar(char c, FILE* stream) { |
| 58 | if (gotserver != 0) { |
||
| 212 | arune | 59 | sendbuf[buffpoint] = c; |
| 60 | buffpoint++; |
||
| 61 | |||
| 62 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
||
| 280 | arune | 63 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
| 212 | arune | 64 | buffpoint = 0; |
| 205 | arune | 65 | } |
| 66 | } |
||
| 67 | return 0; |
||
| 68 | } |
||
| 69 | |||
| 70 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
||
| 71 | |||
| 72 | /*----------------------------------------------------------------------------- |
||
| 73 | * Main Program |
||
| 74 | *---------------------------------------------------------------------------*/ |
||
| 75 | int main(void) { |
||
| 76 | |||
| 77 | //ethernet vars |
||
| 78 | uint16_t plen = 0; |
||
| 79 | uint8_t ledi=0; |
||
| 80 | uint8_t payloadlen=0; |
||
| 212 | arune | 81 | buffpoint = 0; |
| 205 | arune | 82 | |
| 83 | //can vars |
||
| 84 | Can_Message_t txMsg; |
||
| 85 | Can_Message_t rxMsg; |
||
| 86 | txMsg.Id = 3; |
||
| 87 | txMsg.DataLength = 0; |
||
| 88 | txMsg.RemoteFlag = 0; |
||
| 89 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
||
| 212 | arune | 90 | |
| 91 | |||
| 92 | uint32_t timeStamp = 0; |
||
| 205 | arune | 93 | |
| 212 | arune | 94 | Mcu_Init(); |
| 205 | arune | 95 | Timebase_Init(); |
| 280 | arune | 96 | //Serial_Init(); |
| 212 | arune | 97 | |
| 98 | //alla printf ska skriva till udp |
||
| 99 | stdout = &mystdout; //set the output stream |
||
| 205 | arune | 100 | |
| 101 | sei(); |
||
| 102 | |||
| 103 | // printf("CanInit..."); |
||
| 104 | if (Can_Init() != CAN_OK) { |
||
| 105 | // printf("FAILED!\n"); |
||
| 106 | } |
||
| 107 | else { |
||
| 108 | // printf("OK!\n"); |
||
| 109 | } |
||
| 110 | |||
| 111 | /* enable PB0, reset as output */ |
||
| 112 | DDRB|= (1<<DDB0); |
||
| 113 | /* set output to gnd, reset the ethernet chip */ |
||
| 114 | PORTB &= ~(1<<PB0); |
||
| 115 | delay_ms(20); |
||
| 116 | /* set output to Vcc, reset inactive */ |
||
| 117 | PORTB|= (1<<PB0); |
||
| 118 | delay_ms(100); |
||
| 119 | |||
| 120 | // LED |
||
| 121 | /* enable PB1, LED as output */ |
||
| 122 | DDRD|= (1<<DDD6); |
||
| 123 | /* set output to Vcc, LED off */ |
||
| 124 | PORTD|= (1<<PD6); |
||
| 125 | |||
| 212 | arune | 126 | //printf("Init enc\n"); |
| 205 | arune | 127 | /*initialize enc28j60*/ |
| 128 | enc28j60Init(mymac); |
||
| 129 | delay_ms(20); |
||
| 130 | |||
| 131 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
||
| 132 | // LEDB=yellow LEDA=green |
||
| 133 | // |
||
| 134 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
||
| 135 | enc28j60PhyWrite(PHLCON,0x476); |
||
| 136 | delay_ms(20); |
||
| 137 | |||
| 138 | /* set output to GND, red LED on */ |
||
| 139 | PORTD &= ~(1<<PD6); |
||
| 140 | ledi=1; |
||
| 141 | |||
| 142 | //init the ethernet/ip layer: |
||
| 143 | init_ip_arp_udp_tcp(mymac,myip,80); |
||
| 144 | |||
| 145 | /* main loop */ |
||
| 146 | while (1) { |
||
| 147 | /* service the CAN routines */ |
||
| 148 | Can_Service(); |
||
| 149 | |||
| 150 | /* send CAN message and check for CAN errors once every second */ |
||
| 151 | if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { |
||
| 152 | timeStamp = Timebase_CurrentTime(); |
||
| 153 | /* send txMsg */ |
||
| 154 | //txMsg.Id++; |
||
| 212 | arune | 155 | //txMsg.Id = 3; |
| 156 | //Can_Send(&txMsg); |
||
| 205 | arune | 157 | |
| 158 | } |
||
| 159 | |||
| 160 | /* check if any messages have been received */ |
||
| 161 | while (Can_Receive(&rxMsg) == CAN_OK) { |
||
| 212 | arune | 162 | 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)); |
| 163 | printf("data={ "); |
||
| 205 | arune | 164 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 165 | printf("%x ", rxMsg.Data.bytes[i]); |
||
| 166 | } |
||
| 167 | printf("}\n"); |
||
| 212 | arune | 168 | |
| 205 | arune | 169 | } |
| 170 | |||
| 212 | arune | 171 | //test-debug-kod |
| 172 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
||
| 173 | //printf("EIR_TXERIF set!\n"); |
||
| 174 | if ((enc28j60Read(ECON1) & ECON1_TXRTS)) { |
||
| 175 | //printf("ECON1_TXRTS set!\n"); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | if ((enc28j60Read(EIR) & EIR_TXIF)) { |
||
| 179 | //printf("EIR_TXIF set! "); |
||
| 180 | } |
||
| 181 | if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) { |
||
| 182 | //printf("ESTAT_TXABRT set!\n"); |
||
| 183 | //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1)); |
||
| 184 | enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT); |
||
| 185 | } |
||
| 186 | |||
| 205 | arune | 187 | // get the next new packet: |
| 188 | plen = enc28j60PacketReceive(BUFFER_SIZE, buf); |
||
| 189 | |||
| 190 | /*plen will ne unequal to zero if there is a valid |
||
| 191 | * packet (without crc error) */ |
||
| 192 | if(plen!=0){ |
||
| 193 | // arp is broadcast if unknown but a host may also |
||
| 194 | // verify the mac address by sending it to |
||
| 195 | // a unicast address. |
||
| 196 | if(eth_type_is_arp_and_my_ip(buf,plen)){ |
||
| 197 | make_arp_answer_from_request(buf); |
||
| 198 | } else { |
||
| 199 | // check if ip packets (icmp or udp) are for us: |
||
| 200 | if(eth_type_is_ip_and_my_ip(buf,plen)!=0){ |
||
| 201 | |||
| 202 | if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ |
||
| 203 | // a ping packet, let's send pong |
||
| 204 | make_echo_reply_from_request(buf,plen); |
||
| 212 | arune | 205 | //txMsg.Id = 6; |
| 206 | //Can_Send(&txMsg); |
||
| 205 | arune | 207 | |
| 208 | } |
||
| 209 | if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){ |
||
| 210 | payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; |
||
| 280 | arune | 211 | printf("hej %x %x\n", buf[UDP_DST_PORT_H_P], buf[UDP_DST_PORT_L_P]); |
| 212 | if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) { |
||
| 213 | printf("hoj\n"); |
||
| 214 | } else if (buf[UDP_DATA_P]=='t' && payloadlen==5){ |
||
| 205 | arune | 215 | uint8_t i=0; |
| 212 | arune | 216 | if (gotserver == 0) { |
| 217 | while(i<6){ |
||
| 218 | remotemac[i]=buf[ETH_SRC_MAC +i]; |
||
| 219 | i++; |
||
| 220 | } |
||
| 221 | i=0; |
||
| 222 | while(i<4){ |
||
| 223 | remoteip[i]=buf[IP_SRC_P +i]; |
||
| 224 | i++; |
||
| 225 | } |
||
| 226 | i=0; |
||
| 227 | |||
| 228 | gotserver = 1; |
||
| 280 | arune | 229 | make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt); |
| 212 | arune | 230 | } else { |
| 280 | arune | 231 | make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt); |
| 212 | arune | 232 | } |
| 205 | arune | 233 | } |
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | } |
||
| 243 | |||
| 244 | return 0; |
||
| 245 | } |