Rev 85 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 85 | Rev 86 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * Main application |
3 | * Main application |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: Main.c |
6 | * FileName: $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canBase/Source/Main.c $ |
| 7 | * |
- | |
| 8 | * |
7 | * Last changed: $LastChangedDate: 2006-11-14 10:55:38 +0100 (Tue, 14 Nov 2006) $ |
| 9 | * |
8 | * By: $LastChangedBy: johboh $ |
| 10 | * |
9 | * Revision: $Revision: 86 $ |
| 11 | ********************************************************************/ |
10 | ********************************************************************/ |
| 12 | 11 | ||
| 13 | 12 | ||
| 14 | #include <compiler.h> |
13 | #include <compiler.h> |
| 15 | #include <stackTasks.h> |
14 | #include <stackTasks.h> |
| 16 | #include <Tick.h> |
15 | #include <Tick.h> |
| 17 | 16 | ||
| 18 | #ifdef USE_CAN |
17 | #ifdef USE_CAN |
| 19 | #include <CAN.h> |
18 | #include <CAN.h> |
| 20 | #endif |
19 | #endif |
| 21 | 20 | ||
| 22 | static void mainInit(void); |
21 | static void mainInit(void); |
| 23 | 22 | ||
| 24 | unsigned int heartcnt=0; |
- | |
| 25 | 23 | ||
| 26 | void main() |
24 | void main() |
| 27 | { |
25 | { |
| 28 | static TICK t = 0; |
26 | static TICK t = 0; |
| 29 | 27 | ||
| 30 | // Inits |
28 | // Inits |
| 31 | 29 | ||
| 32 | mainInit(); |
30 | mainInit(); |
| 33 | 31 | ||
| 34 | #ifdef USE_CAN |
32 | #ifdef USE_CAN |
| 35 | canInit(); |
33 | canInit(); |
| 36 | #endif |
34 | #endif |
| 37 | 35 | ||
| 38 | tickInit(); |
36 | tickInit(); |
| 39 | 37 | ||
| 40 | while(1) |
38 | while(1) |
| 41 | { |
39 | { |
| 42 |
|
40 | static TICK t = 0; |
| 43 | if ((tickGet()-t)> |
41 | if ((tickGet()-t)>TICK_SECOND/2) |
| 44 | { |
42 | { |
| 45 |
|
43 | LED0_IO=~LED0_IO; |
| 46 | - | ||
| 47 | t = tickGet(); |
- | |
| 48 | - | ||
| 49 | // Send heartbeat |
- | |
| 50 | cm.ident=0x00000666; |
- | |
| 51 | cm.extended=FALSE; |
- | |
| 52 | cm.data_length=5; |
- | |
| 53 | cm.data[0]='H'; |
- | |
| 54 | cm.data[1]=(BYTE)((heartcnt & 0x000000FF)); |
- | |
| 55 | cm.data[2]=(BYTE)((heartcnt & 0x0000FF00)>>1); |
- | |
| 56 | cm.data[3]=(BYTE)((heartcnt & 0x00FF0000)>>2); |
- | |
| 57 | cm.data[4]=(BYTE)((heartcnt & 0xFF000000)>>3); |
- | |
| 58 | - | ||
| 59 | heartcnt++; |
- | |
| 60 | - | ||
| 61 | while(!canSendMessage(cm,PRIO_LOW)); |
- | |
| 62 | } |
44 | } |
| 63 | #endif |
- | |
| 64 | } |
45 | } |
| 65 | } |
46 | } |
| 66 | 47 | ||
| 67 | 48 | ||
| 68 | #pragma interrupt high_isr |
49 | #pragma interrupt high_isr |
| 69 | void high_isr(void) |
50 | void high_isr(void) |
| 70 | { |
51 | { |
| 71 | 52 | ||
| 72 | #ifdef USE_CAN |
53 | #ifdef USE_CAN |
| 73 | canISR(); |
54 | canISR(); |
| 74 | #endif |
55 | #endif |
| 75 | - | ||
| 76 | 56 | ||
| 77 | if (INTCONbits.INT0IE && INTCONbits.INT0IF) |
- | |
| 78 | { |
- | |
| 79 | static TICK t = 0; |
- | |
| 80 | CAN_MESSAGE cm; |
- | |
| 81 | - | ||
| 82 | if ((tickGet()-t)>TICK_SECOND/2) |
- | |
| 83 | { |
- | |
| 84 | - | ||
| 85 | LED0_IO=~LED0_IO; |
- | |
| 86 | - | ||
| 87 | // Send heartbeat |
- | |
| 88 | cm.ident=0x1F910091; |
- | |
| 89 | cm.extended=TRUE; |
- | |
| 90 | cm.remote_request=TRUE; |
- | |
| 91 | cm.data_length=1; |
- | |
| 92 | cm.data[0]=0x01; |
- | |
| 93 | cm.data[1]=0x07; |
- | |
| 94 | cm.data[2]=0x09; |
- | |
| 95 | cm.data[3]=0x08; |
- | |
| 96 | cm.data[4]=0x03; |
- | |
| 97 | - | ||
| 98 | while(!canSendMessage(cm,PRIO_LOW)); |
- | |
| 99 | - | ||
| 100 | t=tickGet(); |
- | |
| 101 | } |
- | |
| 102 | - | ||
| 103 | INTCONbits.INT0IF=0; |
- | |
| 104 | } |
- | |
| 105 | - | ||
| 106 | - | ||
| 107 | tickUpdate(); |
57 | tickUpdate(); |
| 108 | } |
58 | } |
| 109 | 59 | ||
| 110 | 60 | ||
| 111 | #pragma interrupt low_isr |
61 | #pragma interrupt low_isr |
| 112 | void low_isr(void) |
62 | void low_isr(void) |
| Line 126... | Line 76... | ||
| 126 | * Depends: none. |
76 | * Depends: none. |
| 127 | */ |
77 | */ |
| 128 | void mainInit() |
78 | void mainInit() |
| 129 | { |
79 | { |
| 130 | LED0_TRIS=0; |
80 | LED0_TRIS=0; |
| 131 | - | ||
| 132 | // Enable internal PORTB pull-ups |
- | |
| 133 | INTCON2bits.RBPU = 0; |
- | |
| 134 | 81 | ||
| 135 | // Enable Interrupts |
82 | // Enable Interrupts |
| 136 | INTCONbits.GIEH = 1; |
83 | INTCONbits.GIEH = 1; |
| 137 | INTCONbits.GIEL = 1; |
84 | INTCONbits.GIEL = 1; |
| 138 | - | ||
| 139 | - | ||
| 140 | // RB0 interrupt |
- | |
| 141 | INTCON2bits.INTEDG0 = 1; //rising edge |
- | |
| 142 | INTCONbits.INT0IE = 1; |
- | |
| 143 | 85 | ||
| 144 | // Enable interrupt prirority |
86 | // Enable interrupt prirority |
| 145 | RCONbits.IPEN=1; |
87 | RCONbits.IPEN=1; |
| 146 | 88 | ||
| 147 | 89 | ||
| Line 157... | Line 99... | ||
| 157 | * Depends: none. |
99 | * Depends: none. |
| 158 | */ |
100 | */ |
| 159 | #ifdef USE_CAN |
101 | #ifdef USE_CAN |
| 160 | void canParse(CAN_MESSAGE cm) |
102 | void canParse(CAN_MESSAGE cm) |
| 161 | { |
103 | { |
| 162 |
|
104 | switch(cm.funct) |
| 163 | { |
105 | { |
| 164 |
|
106 | case FUNCT_BOOTLOADER: |
| 165 | if (cm. |
107 | if (cm.funcc==FUNCC_BOOT_INIT) { _asm goto 0x0 _endasm } |
| 166 | if (cm.data[0]==0x80) |
- | |
| 167 |
|
108 | break; |
| 168 | cm.ident=0x00000303; |
- | |
| 169 | cm.extended=FALSE; |
- | |
| 170 | cm.data_length=1; |
- | |
| 171 | cm.data[0]=LED0_IO; |
- | |
| 172 | while(!canSendMessage(cm,PRIO_LOW)); |
- | |
| 173 | - | ||
| 174 | } |
109 | } |
| 175 | } |
110 | } |
| - | 111 | #endif |
|
| - | 112 | ||
| 176 | 113 | ||
| 177 | - | ||
| 178 | } |
- | |
| 179 |
|
114 | // Below are mandantory when using bootloader |
| - | 115 | // define DEBUG_MODE to use without bootloader. |
|
| 180 | 116 | ||
| 181 | #ifndef DEBUG_MODE |
117 | #ifndef DEBUG_MODE |
| 182 | extern void _startup (void); |
118 | extern void _startup (void); |
| 183 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
119 | #pragma code _RESET_INTERRUPT_VECTOR = 0x001000 |
| 184 | void _reset (void) |
120 | void _reset (void) |