Subversion Repositories HomeAutomation

Rev

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