Subversion Repositories HomeAutomation

Rev

Rev 97 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  Main application
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/branches/modules/EmbeddedSoftware/PIC/canToSerial/Source/Main.c $
  7.  * Last changed:    $LastChangedDate: 2006-11-14 21:22:17 +0100 (Tue, 14 Nov 2006) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 97 $
  10.  ********************************************************************/
  11.  
  12. #include <compiler.h>
  13. #include <stackTasks.h>
  14. #include <Tick.h>
  15.  
  16. #ifdef USE_CAN
  17.     #include <CAN.h>
  18. #endif
  19.  
  20. #ifdef USE_UART
  21.     #include <uart.h>
  22. #endif
  23.  
  24.  
  25.  
  26. static void mainInit(void);
  27.  
  28. void main()
  29. {
  30.     static TICK t = 0;
  31.  
  32.     // Inits
  33.  
  34.     mainInit();
  35.  
  36.     #ifdef USE_CAN
  37.         canInit();
  38.     #endif
  39.  
  40.     #ifdef USE_UART
  41.         uartInit();
  42.     #endif
  43.  
  44.     tickInit();
  45.  
  46.    
  47.  
  48.     while(1)
  49.     {  
  50.     }
  51.  
  52. }
  53.  
  54.  
  55. #pragma interruptlow HighISR
  56. void HighISR(void)
  57. {
  58.  
  59.  
  60.     #ifdef USE_UART
  61.         uartISR();
  62.     #endif
  63.  
  64.     #ifdef USE_CAN
  65.         canISR();
  66.     #endif
  67.  
  68.  
  69.     tickUpdate();
  70. }
  71. #pragma code highVector=0x08
  72. void HighVector (void)
  73. {
  74.     _asm goto HighISR _endasm
  75. }
  76. #pragma code // Return to default code section
  77.  
  78.  
  79.  
  80. #pragma interruptlow LowISR
  81. void LowISR(void)
  82. {
  83.  
  84. }
  85. #pragma code lowVector=0x18
  86. void LowVector (void)
  87. {
  88.     _asm goto LowISR _endasm
  89. }
  90. #pragma code // Return to default code section
  91.  
  92.  
  93. /*
  94. *   Function: mainInit
  95. *
  96. *   Input:  none
  97. *   Output: none
  98. *   Pre-conditions: none
  99. *   Affects: Se code
  100. *   Depends: none.
  101. */
  102. void mainInit()
  103. {
  104.     LED0_TRIS=0;   
  105.  
  106.     // Enable internal PORTB pull-ups
  107.     INTCON2bits.RBPU = 0;
  108.    
  109.     // Enable Interrupts
  110.     INTCONbits.GIEH = 1;
  111.     INTCONbits.GIEL = 1;
  112.  
  113.  
  114.     // Enable interrupt prirority
  115.     RCONbits.IPEN=1;
  116.    
  117.  
  118. }
  119.  
  120. /*
  121. *   Function: canParse
  122. *
  123. *   Input:  Received can message
  124. *   Output: none
  125. *   Pre-conditions: canInit and received packet.
  126. *   Affects: Sensors/actuators/etc. See code.
  127. *   Depends: none.
  128. */
  129. #ifdef USE_CAN
  130. void canParse(CAN_MESSAGE cm)
  131. {
  132.     int i=0;
  133.    
  134.     uartPutc(UART_START_BYTE);
  135.     uartPutc((BYTE)(cm.ident));
  136.     uartPutc((BYTE)(cm.ident>>8));
  137.     uartPutc((BYTE)(cm.ident>>16));
  138.     uartPutc((BYTE)(cm.ident>>24));
  139.     uartPutc(cm.extended);
  140.     uartPutc(cm.remote_request);
  141.     uartPutc(cm.data_length);
  142.     for(i=0;i<8;i++) uartPutc(cm.data[i]);
  143.     uartPutc(UART_END_BYTE);
  144.  
  145. }
  146. #endif
  147.  
  148.  
  149. /*
  150. *   Function: uartParse
  151. *
  152. *   Input:  Received uart message
  153. *   Output: none
  154. *   Pre-conditions: uartInit and received byte.
  155. *   Affects: Sensors/actuators/etc. See code.
  156. *   Depends: none.
  157. */
  158. #ifdef USE_UART
  159. void uartParse(BYTE c)
  160. {
  161.     BYTE i;
  162.     unsigned int wait = 0;
  163.     CAN_MESSAGE cm;
  164.     static BOOL waitingMessage = FALSE;
  165.     static TICK timeout=0;
  166.     static BYTE count=0;
  167.  
  168.     if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
  169.     {
  170.         waitingMessage=FALSE;
  171.     }
  172.  
  173.  
  174.     if (waitingMessage==TRUE)
  175.     {
  176.  
  177.         timeout=tickGet();
  178.  
  179.  
  180.         // UART END
  181.         if(count>=15)
  182.         {
  183.             if (c==UART_END_BYTE)
  184.             {
  185.                 LED0_IO=~LED0_IO;
  186.                 //canParse(cm);
  187.                 while(!canSendMessage(cm,PRIO_HIGEST));
  188.             }
  189.             waitingMessage=FALSE;
  190.             return;
  191.         }
  192.  
  193.         // data
  194.         if(count>=7)
  195.         {
  196.             cm.data[count-7]=c;
  197.             count++;
  198.             return;
  199.         }
  200.  
  201.         // data length
  202.         if(count>=6)
  203.         {
  204.             cm.data_length=c;
  205.             count++;
  206.             return;
  207.         }
  208.  
  209.         // remote request
  210.         if(count>=5)
  211.         {
  212.             cm.remote_request=c;
  213.             count++;
  214.             return;
  215.         }
  216.  
  217.         // extended
  218.         if(count>=4)
  219.         {
  220.             cm.extended=c;
  221.             count++;
  222.             return;
  223.         }
  224.  
  225.         // ident
  226.         if(count>=0)
  227.         {
  228.             cm.ident+=((DWORD)c<<(count*8));
  229.             count++;
  230.             return;
  231.         }
  232.    
  233.     }
  234.  
  235.  
  236.     if (c==UART_START_BYTE && waitingMessage==FALSE)
  237.     {
  238.             waitingMessage=TRUE;
  239.             timeout=tickGet();
  240.             count=0;
  241.             cm.ident=0;
  242.             return;
  243.     }
  244.  
  245.  
  246. }
  247. #endif
  248.