Subversion Repositories HomeAutomation

Rev

Rev 730 | Rev 1115 | 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. 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. #if MCP_CS_BIT != PB2
  101.     /* If slave select is not set as output it might change SPI hw to slave
  102.      * See ch 18.3.2 (18.3 SS Pin Functionality) in ATmega48/88/168-datasheet */
  103.     DDRB |= (1<<PB2);
  104. #endif
  105.  
  106.  
  107.     //ethernet vars
  108.     uint16_t plen = 0;
  109.     uint8_t payloadlen=0;
  110.     buffpoint = 0;
  111.    
  112.     //can vars
  113.     Can_Message_t txMsg;
  114.     txMsg.Id = 3;
  115.     txMsg.DataLength = 0;
  116.     txMsg.RemoteFlag = 0;
  117.     txMsg.ExtendedFlag = 1;
  118.  
  119.     rxMsgFull = 0;
  120.    
  121.     uint32_t timeStamp = 0;
  122.  
  123.     //uint8_t sw=0;
  124.     // LED
  125.     /* enable PB1, LED as output */
  126.     //DDRB|= (1<<DDB0);
  127.    
  128.     /* set output to Vcc, LED off */
  129.     //PORTB|= (1<<PB0);
  130.    
  131.     Mcu_Init();
  132.     Timebase_Init();
  133.  
  134.     //alla printf ska skriva till udp
  135.     //stdout = &mystdout;    //set the output stream
  136.  
  137.     if (Can_Init() != CAN_OK) {
  138.     }
  139.  
  140.     sei();
  141.  
  142.     /*initialize enc28j60*/
  143.     if (enc28j60Init(mymac) != 1) {
  144.         /* Great place to set an IO for debug */
  145.     }
  146.     delay_ms(20);
  147.    
  148.     /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  149.     // LEDB=yellow LEDA=green
  150.     //
  151.     // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  152.     enc28j60PhyWrite(PHLCON,0x476);
  153.     delay_ms(20);
  154.    
  155.     //init the ethernet/ip layer:
  156.     init_ip_arp_udp_tcp(mymac,myip,80);
  157.  
  158.     /* main loop */
  159.     while (1) {
  160.         /* service the CAN routines */
  161.         //Can_Service();
  162.        
  163. #if SENDTIMESTAMP == 1
  164.         /* send CAN message and check for CAN errors once every second */
  165.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  166.             timeStamp = Timebase_CurrentTime();
  167.             /* send timestamp, to keep network alive */
  168.             txMsg.Id = 0;
  169.             txMsg.DataLength = 0;
  170.             Can_Send(&txMsg);
  171.            
  172.         }
  173. #endif
  174.        
  175.         /* check if any messages have been received */
  176.         if (rxMsgFull) { //(Can_Receive(&rxMsg) == CAN_OK) {
  177.             uint8_t i;
  178.            
  179.             /*if (gotdumpserver != 0) {
  180.                 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));
  181.                 printf("data={ ");
  182.                 for (i=0; i<rxMsg.DataLength; i++) {
  183.                     printf("%x ", rxMsg.Data.bytes[i]);
  184.                 }
  185.                 printf("}\n");
  186.             }*/
  187.            
  188.             if (gotcan2serserver != 0) {
  189.                 //skicka ocks� som udppaket till rmCan2SerPrt
  190.                 char sendbuf2[17];
  191.                
  192.                 sendbuf2[0] = C2S_START_BYTE;
  193.                 sendbuf2[1] = (uint8_t)rxMsg.Id;
  194.                 sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
  195.                 sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
  196.                 sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
  197.                 sendbuf2[5] = rxMsg.ExtendedFlag;
  198.                 sendbuf2[6] = rxMsg.RemoteFlag;
  199.                 sendbuf2[7] = rxMsg.DataLength;
  200.                 for (i=0; i<8; i++) {
  201.                     sendbuf2[8+i] = rxMsg.Data.bytes[i];
  202.                 }
  203.                 sendbuf2[16] = C2S_END_BYTE;
  204.                
  205.                 MCP_INT_DISABLE();
  206.                 send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
  207.                 MCP_INT_ENABLE();
  208.             }
  209.            
  210.             rxMsgFull = 0;
  211.         }
  212.        
  213.         //test-debug-kod
  214.         if ((enc28j60Read(EIR) & EIR_TXERIF)) {
  215.             //printf("EIR_TXERIF set!\n");
  216.             if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
  217.                 //printf("ECON1_TXRTS set!\n");
  218.             }
  219.         }
  220.         if ((enc28j60Read(EIR) & EIR_TXIF)) {
  221.             //printf("EIR_TXIF set! ");
  222.         }
  223.         if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
  224.             //printf("ESTAT_TXABRT set!\n");
  225.             //printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
  226.             enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
  227.         }
  228.        
  229.         // get the next new packet:
  230.         plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
  231.  
  232.         /*plen will ne unequal to zero if there is a valid
  233.          * packet (without crc error) */
  234.         if(plen!=0){
  235.             // arp is broadcast if unknown but a host may also
  236.             // verify the mac address by sending it to
  237.             // a unicast address.
  238.             if(eth_type_is_arp_and_my_ip(buf,plen)){
  239.                     make_arp_answer_from_request(buf);
  240.             } else {
  241.                 // check if ip packets (icmp or udp) are for us:
  242.                 if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
  243.                    
  244.                     if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  245.                             // a ping packet, let's send pong
  246.                             make_echo_reply_from_request(buf,plen);
  247.                             //txMsg.Id = 6;
  248.                             //Can_Send(&txMsg);
  249.                            
  250.                     }
  251.                     if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
  252.                         payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
  253.                         uint8_t i = 0;
  254.                        
  255.                         //om port 1100 (programmeringsporten)
  256.                         if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
  257.                             while(i<6){
  258.                                 prgremotemac[i]=buf[ETH_SRC_MAC +i];
  259.                                 i++;
  260.                             }
  261.                             i=0;
  262.                             while(i<4){
  263.                                 prgremoteip[i]=buf[IP_SRC_P +i];
  264.                                 i++;
  265.                             }
  266.                             gotcan2serserver = 1;
  267.  
  268.                             if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) {
  269.                                 Can_Message_t cm;
  270.                                 //cm.Id = 0;
  271.                                 cm.DataLength = buf[UDP_DATA_P+7];
  272.                                 cm.RemoteFlag = buf[UDP_DATA_P+6];
  273.                                 cm.ExtendedFlag = buf[UDP_DATA_P+5];
  274.                                 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);
  275.                                 uint8_t i;
  276.                                 for (i = 0; i < cm.DataLength; i++) {
  277.                                     cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
  278.                                 }
  279.                                 Can_Send(&cm);
  280.                                
  281.                                 /*if (gotdumpserver != 0) {
  282.                                     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));
  283.                                     printf("data={ ");
  284.                                     for (i=0; i<cm.DataLength; i++) {
  285.                                         printf("%x ", cm.Data.bytes[i]);
  286.                                     }
  287.                                     printf("}\n");
  288.                                 }*/
  289.                             }
  290.                         //om port 1200 (dumparporten)
  291.                         } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
  292.                             if (gotdumpserver == 0) {
  293.                                 while(i<6){
  294.                                     remotemac[i]=buf[ETH_SRC_MAC +i];
  295.                                     i++;
  296.                                 }
  297.                                 i=0;
  298.                                 while(i<4){
  299.                                     remoteip[i]=buf[IP_SRC_P +i];
  300.                                     i++;
  301.                                 }
  302.                                 i=0;
  303.                                
  304.                                 gotdumpserver = 1;
  305.                                 make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
  306.                             } else {
  307.                                 make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
  308.                             }
  309.                         }*/
  310.                     }                      
  311.                 }
  312.             }
  313.         }
  314.        
  315.  
  316.                
  317.        
  318.     }
  319.    
  320.     return 0;
  321. }
  322.