Subversion Repositories HomeAutomation

Rev

Rev 389 | Rev 661 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  *
  3.  *
  4.  *
  5.  * @date    2006-11-21
  6.  * @author  Jimmy Myhrman, Anders Runeson
  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>
  20. //#include <serial.h>
  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] = {192,168,0,50};
  33. static uint8_t myip[4] = {193,11,254,22};
  34. static uint16_t rmDumperPrt =1200; // remote port for dumping data
  35. static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
  36. //static uint16_t locDumperPrt =1200; // listen port
  37. static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
  38. static uint8_t gotdumpserver = 0;
  39. static uint8_t gotcan2serserver = 0;
  40.  
  41. // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
  42.  
  43. static uint8_t remotemac[6];
  44. static uint8_t remoteip[4];
  45.  
  46. static uint8_t prgremotemac[6];
  47. static uint8_t prgremoteip[4];
  48.  
  49. #define BUFFER_SIZE 250
  50. static uint8_t buf[BUFFER_SIZE+1];
  51.  
  52. #define SEND_BUFFER_SIZE 20
  53. static char sendbuf[SEND_BUFFER_SIZE+1];
  54. static uint8_t buffpoint;
  55.  
  56. #define C2S_START_BYTE 253
  57. #define C2S_END_BYTE 250
  58.  
  59. /* set to 0 for not sending timestamps on bus */
  60. #define SENDTIMESTAMP   1
  61.  
  62. /*----------------------------------------------------------------------------
  63.  * Putchar for udp
  64.  * Implements a small buffer, sends packet over udp if buffer is full or
  65.  * \n-char is sent.
  66.  * Needs a timeout to force send.
  67.  *--------------------------------------------------------------------------*/
  68. int udp_putchar(char c, FILE* stream) {
  69.     if (gotdumpserver != 0) {
  70.         sendbuf[buffpoint] = c;
  71.         buffpoint++;
  72.        
  73.         if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
  74.             send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
  75.             buffpoint = 0;
  76.         }
  77.     }
  78.     return 0;
  79. }
  80.  
  81. static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
  82.  
  83. /*-----------------------------------------------------------------------------
  84.  * Main Program
  85.  *---------------------------------------------------------------------------*/
  86. int main(void) {
  87.  
  88.     //ethernet vars
  89.     uint16_t plen = 0;
  90.     uint8_t payloadlen=0;
  91.     buffpoint = 0;
  92.    
  93.     //can vars
  94.     Can_Message_t txMsg;
  95.     Can_Message_t rxMsg;
  96.     txMsg.Id = 3;
  97.     txMsg.DataLength = 0;
  98.     txMsg.RemoteFlag = 0;
  99.     txMsg.ExtendedFlag = 1;
  100.  
  101.     uint32_t timeStamp = 0;
  102.    
  103.     Mcu_Init();
  104.     Timebase_Init();
  105.  
  106.     //alla printf ska skriva till udp
  107.     stdout = &mystdout;    //set the output stream
  108.    
  109.     sei();
  110.  
  111.     if (Can_Init() != CAN_OK) {
  112.     }
  113.  
  114.     /* set output to gnd, reset the ethernet chip */
  115.     PORTC &= ~(1<<PC1);
  116.     /* set PC1 as output */
  117.     DDRC |= (1<<DDC1);
  118.  
  119.     delay_ms(20);
  120.     /* set PC1 as input, resetpin pulled high */
  121.     DDRC &= ~(1<<DDC1);
  122.    
  123.     delay_ms(100);
  124.    
  125.     /*initialize enc28j60*/
  126.     enc28j60Init(mymac);
  127.     delay_ms(20);
  128.    
  129.     /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  130.     // LEDB=yellow LEDA=green
  131.     //
  132.     // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  133.     enc28j60PhyWrite(PHLCON,0x476);
  134.     delay_ms(20);
  135.    
  136.     //init the ethernet/ip layer:
  137.     init_ip_arp_udp_tcp(mymac,myip,80);
  138.  
  139.     /* main loop */
  140.     while (1) {
  141.         /* service the CAN routines */
  142.         Can_Service();
  143.        
  144. #if SENDTIMESTAMP == 1
  145.         /* send CAN message and check for CAN errors once every second */
  146.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  147.             timeStamp = Timebase_CurrentTime();
  148.             /* send timestamp, to keep network alive */
  149.             txMsg.Id = 0;
  150.             txMsg.DataLength = 0;
  151.             Can_Send(&txMsg);
  152.            
  153.         }
  154. #endif
  155.        
  156.         /* check if any messages have been received */
  157.         while (Can_Receive(&rxMsg) == CAN_OK) {
  158.             uint8_t i;
  159.  
  160.             /*if (gotdumpserver != 0) {
  161.                 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));
  162.                 printf("data={ ");
  163.                 for (i=0; i<rxMsg.DataLength; i++) {
  164.                     printf("%x ", rxMsg.Data.bytes[i]);
  165.                 }
  166.                 printf("}\n");
  167.             }*/
  168.            
  169.             if (gotcan2serserver != 0) {
  170.                 //skicka ocks� som udppaket till rmCan2SerPrt
  171.                 char sendbuf2[17];
  172.                
  173.                 sendbuf2[0] = C2S_START_BYTE;
  174.                 sendbuf2[1] = (uint8_t)rxMsg.Id;
  175.                 sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
  176.                 sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
  177.                 sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
  178.                 sendbuf2[5] = rxMsg.ExtendedFlag;
  179.                 sendbuf2[6] = rxMsg.RemoteFlag;
  180.                 sendbuf2[7] = rxMsg.DataLength;
  181.                 for (i=0; i<8; i++) {
  182.                     sendbuf2[8+i] = rxMsg.Data.bytes[i];
  183.                 }
  184.                 sendbuf2[16] = C2S_END_BYTE;
  185.                
  186.                 send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
  187.             }
  188.         }
  189.        
  190.         //test-debug-kod
  191.         if ((enc28j60Read(EIR) & EIR_TXERIF)) {
  192.             //printf("EIR_TXERIF set!\n");
  193.             if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
  194.                 //printf("ECON1_TXRTS set!\n");
  195.             }
  196.         }
  197.         if ((enc28j60Read(EIR) & EIR_TXIF)) {
  198.             //printf("EIR_TXIF set! ");
  199.         }
  200.         if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
  201.             //printf("ESTAT_TXABRT set!\n");
  202.             //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
  203.             enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
  204.         }
  205.        
  206.         // get the next new packet:
  207.         plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
  208.  
  209.         /*plen will ne unequal to zero if there is a valid
  210.          * packet (without crc error) */
  211.         if(plen!=0){
  212.             // arp is broadcast if unknown but a host may also
  213.             // verify the mac address by sending it to
  214.             // a unicast address.
  215.             if(eth_type_is_arp_and_my_ip(buf,plen)){
  216.                     make_arp_answer_from_request(buf);
  217.             } else {
  218.                 // check if ip packets (icmp or udp) are for us:
  219.                 if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
  220.                    
  221.                     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
  223.                             make_echo_reply_from_request(buf,plen);
  224.                             //txMsg.Id = 6;
  225.                             //Can_Send(&txMsg);
  226.                            
  227.                     }
  228.                     if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
  229.                         payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
  230.                         uint8_t i = 0;
  231.                        
  232.                         //om port 1100 (programmeringsporten)
  233.                         if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
  234.                             while(i<6){
  235.                                 prgremotemac[i]=buf[ETH_SRC_MAC +i];
  236.                                 i++;
  237.                             }
  238.                             i=0;
  239.                             while(i<4){
  240.                                 prgremoteip[i]=buf[IP_SRC_P +i];
  241.                                 i++;
  242.                             }
  243.                             gotcan2serserver = 1;
  244.  
  245.                             if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) {
  246.                                 Can_Message_t cm;
  247.                                 //cm.Id = 0;
  248.                                 cm.DataLength = buf[UDP_DATA_P+7];
  249.                                 cm.RemoteFlag = buf[UDP_DATA_P+6];
  250.                                 cm.ExtendedFlag = buf[UDP_DATA_P+5];
  251.                                 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);
  252.                                 uint8_t i;
  253.                                 for (i = 0; i < cm.DataLength; i++) {
  254.                                     cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
  255.                                 }
  256.                                 Can_Send(&cm);
  257.                                
  258.                                 /*if (gotdumpserver != 0) {
  259.                                     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));
  260.                                     printf("data={ ");
  261.                                     for (i=0; i<cm.DataLength; i++) {
  262.                                         printf("%x ", cm.Data.bytes[i]);
  263.                                     }
  264.                                     printf("}\n");
  265.                                 }*/
  266.                             }
  267.                         //om port 1200 (dumparporten)
  268.                         } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
  269.                             if (gotdumpserver == 0) {
  270.                                 while(i<6){
  271.                                     remotemac[i]=buf[ETH_SRC_MAC +i];
  272.                                     i++;
  273.                                 }
  274.                                 i=0;
  275.                                 while(i<4){
  276.                                     remoteip[i]=buf[IP_SRC_P +i];
  277.                                     i++;
  278.                                 }
  279.                                 i=0;
  280.                                
  281.                                 gotdumpserver = 1;
  282.                                 make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
  283.                             } else {
  284.                                 make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
  285.                             }
  286.                         }*/
  287.                     }                      
  288.                 }
  289.             }
  290.         }
  291.        
  292.  
  293.                
  294.        
  295.     }
  296.    
  297.     return 0;
  298. }
  299.