Subversion Repositories HomeAutomation

Rev

Rev 95 | Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  Main application
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/Main.c $
  7.  * Last changed:    $LastChangedDate: 2006-11-15 09:40:52 +0100 (Wed, 15 Nov 2006) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 100 $
  10.  ********************************************************************/
  11.  
  12.  
  13. #include <compiler.h>
  14. #include <stackTasks.h>
  15. #include <Tick.h>
  16. #include <funcdefs.h>
  17. #include <EEaccess.h>
  18.  
  19. #ifdef USE_CAN
  20.     #include <CAN.h>
  21. #endif
  22.  
  23. static void mainInit(void);
  24.  
  25.  
  26. static int MY_ID = DEFAULT_ID;
  27. static BYTE MY_NID = DEFAULT_NID;
  28.  
  29. void main()
  30. {
  31.     static TICK t = 0;
  32.     CAN_MESSAGE cm2;
  33.  
  34.     // Inits
  35.  
  36.     mainInit();
  37.  
  38.     #ifdef USE_CAN
  39.         canInit();
  40.     #endif
  41.  
  42.     tickInit();
  43.  
  44.     // Read ID and NID if exist.
  45.     if (EERead(NODE_ID_EE)==NODE_HAS_ID)
  46.     {
  47.         MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
  48.         MY_NID=EERead(NODE_ID_EE + 3);
  49.     }
  50.  
  51.     // Dummy bootmessage
  52.     cm2.data[0]=0x91;
  53.     cm2.data_length=1;
  54.     canSendMessage(cm2,PRIO_HIGH);
  55.  
  56.  
  57.     while(1)
  58.     {
  59.         static TICK t = 0;
  60.         if ((tickGet()-t)>TICK_SECOND)
  61.         {
  62.             LED0_IO=~LED0_IO;
  63.             t = tickGet();
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. #pragma interrupt high_isr
  70. void high_isr(void)
  71. {
  72.     ClrWdt();
  73.  
  74.     #ifdef USE_CAN
  75.         canISR();
  76.     #endif
  77.  
  78.     tickUpdate();
  79.  
  80. }
  81.  
  82.  
  83. #pragma interrupt low_isr
  84. void low_isr(void)
  85. {
  86.  
  87. }
  88.  
  89.  
  90.  
  91. /*
  92. *   Function: mainInit
  93. *
  94. *   Input:  none
  95. *   Output: none
  96. *   Pre-conditions: none
  97. *   Affects: Se code
  98. *   Depends: none.
  99. */
  100. void mainInit()
  101. {
  102.     LED0_TRIS=0;   
  103.  
  104.     // Enable Interrupts
  105.     INTCONbits.GIEH = 1;
  106.     INTCONbits.GIEL = 1;
  107.  
  108.     // Enable interrupt prirority
  109.     RCONbits.IPEN=1;
  110.    
  111.  
  112. }
  113.  
  114. /*
  115. *   Function: canParse
  116. *
  117. *   Input:  Received can message
  118. *   Output: none
  119. *   Pre-conditions: canInit and received packet.
  120. *   Affects: Sensors/actuators/etc. See code.
  121. *   Depends: none.
  122. */
  123. #ifdef USE_CAN
  124. void canParse(CAN_MESSAGE cm)
  125. {
  126.     ClrWdt();
  127.  
  128.     switch(cm.funct)
  129.     {
  130.         case FUNCT_BOOTLOADER:
  131.             if (cm.funcc==FUNCC_BOOT_INIT && cm.nid==MY_NID && ((((unsigned int)cm.data[BOOT_DATA_RID_HIGH_INDEX])<<8)+cm.data[BOOT_DATA_RID_LOW_INDEX])==MY_ID) { Reset(); }
  132.         break;
  133.     }
  134. }
  135. #endif
  136.  
  137.  
  138. // Below are mandantory when using bootloader
  139. // define DEBUG_MODE to use without bootloader.
  140.  
  141. #ifndef DEBUG_MODE
  142. extern void _startup (void);
  143. #pragma code _RESET_INTERRUPT_VECTOR = 0x001000
  144. void _reset (void)
  145. {
  146.     _asm goto _startup _endasm
  147. }
  148. #pragma code
  149. #endif
  150.  
  151. #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
  152. void _high_ISR (void)
  153. {
  154.     _asm GOTO high_isr _endasm
  155. }
  156. #pragma code
  157.  
  158. #pragma code _LOW_INTERRUPT_VECTOR = 0x001018
  159. void _low_ISR (void)
  160. {
  161.     _asm GOTO low_isr _endasm
  162. }
  163. #pragma code
  164.