Subversion Repositories HomeAutomation

Rev

Rev 342 | Blame | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  CAN module
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/CAN.c $
  7.  * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 353 $
  10.  ********************************************************************/
  11.  
  12. #include <CANdefs.h>
  13. #include <stackTasks.h>
  14. #include <CAN.h>
  15. #include <EEaccess.h>
  16. #include <funcdefs.h>
  17. #include <Tick.h>
  18.  
  19. static TICK heartbeat;
  20. static int MY_ID = DEFAULT_ID;
  21. static CAN_PACKET outCp;
  22.  
  23. static void canGetPacket(void);
  24.  
  25.  
  26. /*
  27. *   Function: canInit
  28. *
  29. *   Input:  none
  30. *   Output: none
  31. *   Pre-conditions: none
  32. *   Affects: Changes can registers.
  33. *   Depends: none.
  34. */
  35. void canInit()
  36. {
  37.     CANCON = 0x80; //request config mode
  38.     while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request
  39.  
  40.     // BAUD AND TQS
  41.  
  42.     // 500Khz@40Mhz (new by the book)
  43.     BRGCON1 = 0x04;
  44.     BRGCON2 = 0xD1;
  45.     BRGCON3 = 0x81;
  46.  
  47.     //BRGCON1=0;
  48.    
  49.     // Sync_Seq = 2 x TQ = 1
  50.     //BRGCON1bits.SJW1=0;
  51.     //BRGCON1bits.SJW0=1;
  52.    
  53.     // Setting BRP.
  54.     //BRGCON1=BRGCON1|CAN_BRP;
  55.    
  56.     // Maximum PHEG1
  57.     //BRGCON2bits.SEG2PHTS = 1;
  58.    
  59.     // Phase_Seg1 = 2 x TQ = 1
  60.     //BRGCON2bits.SEG1PH2 = 0;
  61.     //BRGCON2bits.SEG1PH1 = 0;
  62.     //BRGCON2bits.SEG1PH0 = 1;
  63.    
  64.     // Prop_Seq = 2 x TQ = 1
  65.     //BRGCON2bits.PRSEG2 = 0;
  66.     //BRGCON2bits.PRSEG1 = 0;
  67.     //BRGCON2bits.PRSEG0 = 1;
  68.    
  69.     //  Enableing bus wake-up
  70.     //BRGCON3bits.WAKDIS = 0;
  71.    
  72.     // Dont use filter when wakeup
  73.     //BRGCON3bits.WAKFIL = 0;
  74.    
  75.     // Phase_Seg2 = 2 x TQ = 1
  76.     //BRGCON3bits.SEG2PH2 = 0;
  77.     //BRGCON3bits.SEG2PH1 = 0;
  78.     //BRGCON3bits.SEG2PH0 = 1;
  79.  
  80.     ECANCON=0;
  81.     ECANCONbits.MDSEL1 = 1; // 0  For mode 1
  82.     ECANCONbits.MDSEL0 = 0; // 1  For mode 1
  83.  
  84.  
  85.     // FILTERS AND MASKS
  86.     RXB0CONbits.RXM1=1;
  87.     RXB0CONbits.RXM0=0;
  88.  
  89.  
  90.     // INTERRUPTS
  91.    
  92.     // Diable transmit interrupt
  93.     TXBIE = 0;
  94.  
  95.     //rx any interrupt
  96.     PIR3 = 0;
  97.     PIE3 = 0b00000011;
  98.  
  99.     // High interrupt
  100.     IPR3 =0b00000011;
  101.  
  102.     // Programmable buffers interrupt
  103.     BIE0  = 0xFF;  
  104.  
  105.  
  106.     //loopback mode or not
  107.     #ifdef CAN_LOOPBACK_MODE
  108.         CANCON = 0x40;//request loopback mode
  109.         while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode
  110.     #else
  111.         CANCON = 0x00;//request normal mode
  112.         while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode
  113.     #endif
  114.  
  115.  
  116.     // Init ticker service
  117.     tickInit();
  118.  
  119.  
  120.     // Read ID and NID if exist.
  121.     if (EERead(NODE_ID_EE)==NODE_HAS_ID)
  122.     {
  123.         MY_ID=+EERead(NODE_ID_EE + 1);
  124.     }
  125.  
  126.     // Send user program startup heatbeat
  127.     outCp.type=ptPGM;
  128.     outCp.pgm.class=pcCTRL;
  129.     outCp.pgm.id=pctAPPBOOT;
  130.     outCp.length=2;
  131.     outCp.data[1]=(BYTE)((APP_VERSION & 0xFF00)>>8);
  132.     outCp.data[0]=(BYTE)(APP_VERSION & 0xFF);
  133.     while(!canSendMessage(outCp,PRIO_HIGH));
  134. }
  135.  
  136.  
  137. /*
  138. *   Function: canISR
  139. *
  140. *   Input:  none
  141. *   Output: none
  142. *   Pre-conditions: a call to canInit()
  143. *   Affects: interrupt registers and receive data buffers
  144. *   Depends: ..
  145. */
  146. void canISR()
  147. {
  148.     ClrWdt();
  149.  
  150.     tickUpdate();
  151.  
  152.     if (PIR3bits.RXBnIF || PIR3bits.RXB1IF || PIR3bits.RXB0IF)
  153.     {
  154.         ECANCON=(ECANCON&0b00000)|(0b10000|(CANCON&0x0F));
  155.         if (RXB0CONbits.RXFUL) canGetPacket();
  156.    
  157.         if (PIR3bits.RXBnIF) {PIR3bits.RXBnIF = 0; return;}
  158.         if (PIR3bits.RXB1IF) {PIR3bits.RXB1IF = 0; return;}
  159.         if (PIR3bits.RXB0IF) {PIR3bits.RXB0IF = 0; return;}
  160.     }
  161.  
  162.    
  163.  
  164.     if ((tickGet()-heartbeat)>TICK_1S*5)
  165.     {
  166.         // Send alive heartbeat
  167.         outCp.type=ptPGM;
  168.         outCp.pgm.class=pcCTRL;
  169.         outCp.pgm.id=pctHEARTBEAT;
  170.         outCp.length=0;
  171.         canSendMessage(outCp,PRIO_HIGH);
  172.            
  173.         heartbeat = tickGet();
  174.     }
  175.  
  176.    
  177. }
  178.  
  179.  
  180. /*
  181. *   Function: canGetPacket
  182. *
  183. *   Input:  none
  184. *   Output: none
  185. *   Pre-conditions: a call to canISR and an packet is available and banked to RXB0
  186. *   Affects: Calls canParse() function in main for parsing received packet.
  187. *   Depends: none.
  188. */
  189. void canGetPacket()
  190. {
  191.         CAN_PACKET cp;
  192.  
  193.         //RXB0SIDH 28 27 26 25 24 23 22 21
  194.         //RXB0SIDL 20 19 18 xx xx xx 17 16
  195.         //RXB0EIDH 15 14 13 12 11 10 09 08
  196.         //RXB0EIDL 07 06 05 04 03 02 01 00
  197.  
  198.  
  199.         cp.type = (BYTE)((RXB0SIDH&0xC0)>>6);
  200.  
  201.  
  202.         if (cp.type==ptBOOT)
  203.         {
  204.             cp.boot.type  = ((RXB0SIDH&0x38)>>3);
  205.             cp.boot.offset= ((RXB0SIDH&0x7)<<5)+((RXB0SIDL&0xE0)>>3)+(RXB0SIDL&0x3);
  206.             cp.boot.rid   = RXB0EIDH;
  207.         }
  208.         else
  209.         {
  210.             cp.pgm.class= ((RXB0SIDH&0x3C)>>2);
  211.             cp.pgm.id   = (((WORD)RXB0SIDH&0x3)<<13)+(((WORD)RXB0SIDL&0xE0)<<5)+(((WORD)RXB0SIDL&0x3)<<8)+(WORD)RXB0EIDH;
  212.         }
  213.         cp.sid   = RXB0EIDL;
  214.  
  215.  
  216.         // Data length
  217.         cp.length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0;
  218.  
  219.         // Data one bye one, for speed
  220.         cp.data[0]=RXB0D0;  cp.data[1]=RXB0D1;
  221.         cp.data[2]=RXB0D2;  cp.data[3]=RXB0D3;
  222.         cp.data[4]=RXB0D4;  cp.data[5]=RXB0D5;
  223.         cp.data[6]=RXB0D6;  cp.data[7]=RXB0D7;
  224.  
  225.         // Clear ful status
  226.         RXB0CONbits.RXFUL=0;
  227.  
  228.  
  229.  
  230.         // Check if bootloader request specific.
  231.         // reset and id change
  232.         if (cp.type==ptBOOT)
  233.         {
  234.             if (cp.boot.rid==MY_ID)
  235.             {
  236.                 if (cp.boot.type==btADDR) Reset(); // Reset me.
  237.                 if (cp.boot.type==btCHANGEID)
  238.                 {
  239.                         // Change my ID and NID
  240.                         MY_ID=cp.data[0];
  241.                         EEWrite(NODE_ID_EE+1,cp.data[0]);
  242.                         EEWrite(NODE_ID_EE,NODE_HAS_ID);
  243.                         // Send ack with new id.
  244.                         outCp.type=ptBOOT;
  245.                         outCp.boot.type=btACK;
  246.                         outCp.length=0;
  247.                         while(!canSendMessage(outCp,PRIO_HIGH));
  248.                 }
  249.             }
  250.         }
  251.  
  252.  
  253.         //Call main parser
  254.         if (cp.type==ptPGM) canParse(cp);
  255. }
  256.  
  257.  
  258. /*
  259. *   Function: canSendMessage
  260. *
  261. *   Input:  CAN_MESSAGE to send
  262. *   Output: True if packet scheduled sucess, false otherwise.
  263. *   Pre-conditions: canInit
  264. *   Affects: ..
  265. *   Depends: ..
  266. */
  267. BOOL canSendMessage(CAN_PACKET cp, CAN_PRIORITY prio)
  268. {
  269.    
  270.     if ( TXB0CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00011; }
  271.     else if ( TXB1CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00100; }
  272.     else if ( TXB2CONbits.TXREQ == 0 )  { ECANCON=(ECANCON&0b00000)|0b00101; }
  273.     else { return FALSE;}  // None of the transmit buffers were empty.
  274.  
  275.     if (prio<0 || prio>3) prio = 0;
  276.  
  277.  
  278.     //RXB0SIDH 28 27 26 25 24 23 22 21
  279.     //RXB0SIDL 20 19 18 xx xx xx 17 16
  280.     //RXB0EIDH 15 14 13 12 11 10 09 08
  281.     //RXB0EIDL 07 06 05 04 03 02 01 00
  282.  
  283.  
  284.     if (cp.type==ptBOOT)
  285.     {
  286.         RXB0SIDH = ((cp.type&0x3)<<6)+((cp.boot.type&0x7)<<3)+((cp.boot.offset&0xE0)>>5);
  287.         RXB0SIDL = ((cp.boot.offset&0x1C)<<3)+(cp.boot.offset&0x3);
  288.         RXB0EIDH = cp.boot.rid;
  289.     }
  290.     else
  291.     {
  292.         RXB0SIDH = ((cp.type&0x3)<<6)+((cp.pgm.class&0xF)<<2)+((cp.pgm.id&0x6000)>>13);
  293.         RXB0SIDL = ((cp.pgm.id&0x1C00)>>5)+((cp.pgm.id&0x300)>>8);
  294.         RXB0EIDH = (cp.pgm.id&0xFF);
  295.     }
  296.     RXB0EIDL = MY_ID;
  297.  
  298.     // Data length
  299.     RXB0DLC = 0;
  300.     if (cp.length>8) RXB0DLC=8; else RXB0DLC=cp.length;
  301.    
  302.     // NOT Remote request
  303.     _asm
  304.         bcf RXB0DLC, 6, 0
  305.     _endasm
  306.    
  307.    
  308.     // Data one bye one, for speed
  309.     RXB0D0=cp.data[0];  RXB0D1=cp.data[1];
  310.     RXB0D2=cp.data[2];  RXB0D3=cp.data[3];
  311.     RXB0D4=cp.data[4];  RXB0D5=cp.data[5];
  312.     RXB0D6=cp.data[6];  RXB0D7=cp.data[7];
  313.     // set extended
  314.     RXB0SIDLbits.EXID=1;
  315.    
  316.     // Priority
  317.     RXB0CON = (RXB0CON & 0b11111100) | prio;
  318.    
  319.     // mark as redy to transmit
  320.     _asm
  321.         bsf RXB0CON, 3, 0
  322.     _endasm
  323.    
  324.     return TRUE;
  325.  
  326. }
  327.