Subversion Repositories HomeAutomation

Rev

Rev 730 | 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. static uint8_t mymac[6] = {ENC28J60_MAC1,ENC28J60_MAC2,ENC28J60_MAC3,ENC28J60_MAC4,ENC28J60_MAC5,ENC28J60_MAC6};
  32. //static uint8_t myip[4] = {192,168,0,50};
  33. static uint8_t myip[4] = {ENC28J60_IP1,ENC28J60_IP2,ENC28J60_IP3,ENC28J60_IP4};
  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.  
  42. static uint8_t remotemac[6];
  43. static uint8_t remoteip[4];
  44.  
  45. static uint8_t prgremotemac[6];
  46. static uint8_t prgremoteip[4];
  47.  
  48. #define BUFFER_SIZE 250
  49. static uint8_t buf[BUFFER_SIZE+1];
  50.  
  51. #define SEND_BUFFER_SIZE 20
  52. static char sendbuf[SEND_BUFFER_SIZE+1];
  53. static uint8_t buffpoint;
  54.  
  55. #define C2S_START_BYTE 253
  56. #define C2S_END_BYTE 250
  57.  
  58. /* set to 0 for not sending timestamps on bus */
  59. #define SENDTIMESTAMP   0
  60.  
  61. volatile Can_Message_t rxMsg; // Message storage
  62. volatile uint8_t rxMsgFull;   // Synchronization flag
  63.  
  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.  *--------------------------------------------------------------------------*/
  70. /*int udp_putchar(char c, FILE* stream) {
  71.     if (gotdumpserver != 0) {
  72.         sendbuf[buffpoint] = c;
  73.         buffpoint++;
  74.        
  75.         if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
  76.             send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
  77.             buffpoint = 0;
  78.         }
  79.     }
  80.     return 0;
  81. }
  82.  
  83. static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE);
  84. */
  85.  
  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.  
  95. /*-----------------------------------------------------------------------------
  96.  * Main Program
  97.  *---------------------------------------------------------------------------*/
  98. int main(void) {
  99.  
  100.     //ethernet vars
  101.     uint16_t plen = 0;
  102.     uint8_t payloadlen=0;
  103.     buffpoint = 0;
  104.    
  105.     //can vars
  106.     Can_Message_t txMsg;
  107.     txMsg.Id = 3;
  108.     txMsg.DataLength = 0;
  109.     txMsg.RemoteFlag = 0;
  110.     txMsg.ExtendedFlag = 1;
  111.  
  112.     rxMsgFull = 0;
  113.    
  114.     uint32_t timeStamp = 0;
  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);
  123.    
  124.     Mcu_Init();
  125.     Timebase_Init();
  126.  
  127.     //alla printf ska skriva till udp
  128.     //stdout = &mystdout;    //set the output stream
  129.  
  130.     if (Can_Init() != CAN_OK) {
  131.     }
  132.  
  133.     sei();
  134.  
  135.     /*initialize enc28j60*/
  136.     if (enc28j60Init(mymac) != 1) {
  137.         /* Great place to set an IO for debug */
  138.     }
  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 */
  154.         //Can_Service();
  155.        
  156. #if SENDTIMESTAMP == 1
  157.         /* send CAN message and check for CAN errors once every second */
  158.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  159.             timeStamp = Timebase_CurrentTime();
  160.             /* send timestamp, to keep network alive */
  161.             txMsg.Id = 0;
  162.             txMsg.DataLength = 0;
  163.             Can_Send(&txMsg);
  164.            
  165.         }
  166. #endif
  167.        
  168.         /* check if any messages have been received */
  169.         if (rxMsgFull) { //(Can_Receive(&rxMsg) == CAN_OK) {
  170.             uint8_t i;
  171.            
  172.             /*if (gotdumpserver != 0) {
  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");
  179.             }*/
  180.            
  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.                
  198.                 MCP_INT_DISABLE();
  199.                 send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
  200.                 MCP_INT_ENABLE();
  201.             }
  202.            
  203.             rxMsgFull = 0;
  204.         }
  205.        
  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.        
  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);
  240.                             //txMsg.Id = 6;
  241.                             //Can_Send(&txMsg);
  242.                            
  243.                     }
  244.                     if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
  245.                         payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
  246.                         uint8_t i = 0;
  247.                        
  248.                         //om port 1100 (programmeringsporten)
  249.                         if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
  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.                                
  274.                                 /*if (gotdumpserver != 0) {
  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]);
  279.                                     }
  280.                                     printf("}\n");
  281.                                 }*/
  282.                             }
  283.                         //om port 1200 (dumparporten)
  284.                         } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
  285.                             if (gotdumpserver == 0) {
  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.                                
  297.                                 gotdumpserver = 1;
  298.                                 make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
  299.                             } else {
  300.                                 make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
  301.                             }
  302.                         }*/
  303.                     }                      
  304.                 }
  305.             }
  306.         }
  307.        
  308.  
  309.                
  310.        
  311.     }
  312.    
  313.     return 0;
  314. }
  315.