Rev 64 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 64 | Rev 67 | ||
|---|---|---|---|
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * Main application |
3 | * Main application |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: Main.c |
6 | * FileName: Main.c |
| 7 | * |
7 | * |
| 8 | * Author Date Comment |
8 | * Author Date Comment |
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
| 11 | ********************************************************************/ |
11 | ********************************************************************/ |
| 12 | 12 | ||
| 13 | #include <compiler.h> |
13 | #include <compiler.h> |
| 14 | #include <stackTasks.h> |
14 | #include <stackTasks.h> |
| 15 | #include <CANdefs.h> |
15 | #include <CANdefs.h> |
| 16 | #include <CAN.h> |
16 | #include <CAN.h> |
| 17 |
|
17 | //#include <Tick.h> |
| - | 18 | #include <boot.h> |
|
| - | 19 | ||
| - | 20 | /** V E C T O R R E M A P P I N G *******************************************/ |
|
| - | 21 | ||
| - | 22 | #pragma code _HIGH_INTERRUPT_VECTOR = 0x000008 |
|
| - | 23 | void _high_ISR (void) |
|
| - | 24 | { |
|
| - | 25 | _asm goto RM_HIGH_INTERRUPT_VECTOR _endasm |
|
| - | 26 | } |
|
| - | 27 | ||
| - | 28 | #pragma code _LOW_INTERRUPT_VECTOR = 0x000018 |
|
| - | 29 | void _low_ISR (void) |
|
| - | 30 | { |
|
| - | 31 | _asm goto RM_LOW_INTERRUPT_VECTOR _endasm |
|
| - | 32 | } |
|
| - | 33 | ||
| - | 34 | #pragma code |
|
| - | 35 | ||
| 18 | 36 | ||
| 19 | #ifdef USE_CAN |
37 | #ifdef USE_CAN |
| 20 | #include <CAN.h> |
38 | #include <CAN.h> |
| 21 | #endif |
39 | #endif |
| 22 | 40 | ||
| 23 | static CAN_PROTO_MESSAGE cm; |
41 | static CAN_PROTO_MESSAGE cm; |
| 24 | static BYTE ful=0; |
42 | static BYTE ful=0; |
| 25 | static DWORD cnt =0; |
43 | static DWORD cnt =0; |
| 26 | 44 | ||
| 27 | static void mainInit(void); |
45 | static void mainInit(void); |
| 28 | static BOOL canGetPacket(void); |
46 | static BOOL canGetPacket(void); |
| - | 47 | void TBLWT_Func(void); |
|
| - | 48 | void TBLWTPOSTINC_Func (void); |
|
| - | 49 | void TBLRD_Func(void); |
|
| - | 50 | ||
| - | 51 | #define TICK_SUB_COUNTER_10MS 312 |
|
| - | 52 | #define PACKET_TIMEOUT 14 |
|
| - | 53 | #define BOOTLOADER_INIT_TIMOUT 500 |
|
| - | 54 | ||
| - | 55 | DWORD tickCounter = 0; |
|
| - | 56 | DWORD tickSubCounter = 0; |
|
| 29 | 57 | ||
| 30 | |
58 | DWORD tickGet(void); |
| 31 | #define BOOTLOADER_INIT_TIMOUT (TICK_SECOND*5) |
- | |
| 32 | 59 | ||
| 33 | void main() |
60 | void main() |
| 34 | { |
61 | { |
| 35 | static PROGRAM_STATE pgs = pgsWATING_START; |
62 | static PROGRAM_STATE pgs = pgsWATING_START; |
| 36 | static |
63 | static DWORD t = 0; |
| 37 | static WORD programmerSid=0; |
64 | static WORD programmerSid=0; |
| 38 | 65 | ||
| 39 | static BYTE errMsg = ERR_NO_ERROR; |
66 | static BYTE errMsg = ERR_NO_ERROR; |
| 40 | static PROGRAM_STATE afterAckPgs = pgsWATING_START; |
67 | static PROGRAM_STATE afterAckPgs = pgsWATING_START; |
| 41 | static DWORD pgmAddress = 0; |
68 | static DWORD pgmAddress = 0; |
| 42 | static BYTE bytesReceived = 0; |
69 | static BYTE bytesReceived = 0; |
| 43 | 70 | ||
| - | 71 | DWORD tBoot; |
|
| - | 72 | ||
| - | 73 | static BYTE pgmData[64]; |
|
| - | 74 | ||
| 44 | static BYTE tmp; |
75 | static BYTE tmp; |
| 45 | 76 | ||
| 46 | // Inits |
77 | // Inits |
| 47 | - | ||
| 48 | mainInit(); |
78 | mainInit(); |
| 49 | 79 | ||
| 50 | #ifdef USE_CAN |
80 | #ifdef USE_CAN |
| 51 | canInit(); |
81 | canInit(); |
| 52 | #endif |
82 | #endif |
| 53 | - | ||
| 54 | 83 | ||
| 55 | - | ||
| 56 |
|
84 | tBoot = tickGet(); |
| 57 | - | ||
| 58 | 85 | ||
| 59 | 86 | ||
| 60 | for(;;) |
87 | for(;;) |
| 61 | { |
88 | { |
| - | 89 | BYTE i; |
|
| 62 | CAN_PROTO_MESSAGE outCm; |
90 | CAN_PROTO_MESSAGE outCm; |
| 63 | BOOL hasPacket = canGetPacket(); |
91 | BOOL hasPacket = canGetPacket(); |
| 64 | 92 | ||
| 65 |
|
93 | tickSubCounter++; |
| - | 94 | if (tickSubCounter>TICK_SUB_COUNTER_10MS) |
|
| 66 | { |
95 | { |
| 67 |
|
96 | tickCounter++; |
| - | 97 | tickSubCounter=0; |
|
| 68 | } |
98 | } |
| 69 | 99 | ||
| 70 | switch(pgs) |
100 | switch(pgs) |
| 71 | { |
101 | { |
| 72 | case pgsWATING_START: |
102 | case pgsWATING_START: |
| 73 | // Check if there is bootloader start packet |
103 | // Check if there is bootloader start packet |
| 74 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_INIT && cm.data[RID_INDEX]==MY_ID && cm.nid==MY_NID) |
104 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_INIT && cm.data[RID_INDEX]==MY_ID && cm.nid==MY_NID) |
| 75 | { |
105 | { |
| 76 | // Has new start packet |
106 | // Has new start packet |
| 77 | pgmAddress = (((DWORD)cm.data[ADDRU_INDEX])<<16)+(((DWORD)cm.data[ADDRH_INDEX])<<8)+((DWORD)cm.data[ADDRL_INDEX]); |
107 | pgmAddress = (((DWORD)cm.data[ADDRU_INDEX])<<16)+(((DWORD)cm.data[ADDRH_INDEX])<<8)+((DWORD)cm.data[ADDRL_INDEX]); |
| 78 | programmerSid = cm.sid; |
108 | programmerSid = cm.sid; |
| 79 | t = tickGet(); |
109 | t = tickGet(); |
| 80 | errMsg = ERR_NO_ERROR; |
110 | errMsg = ERR_NO_ERROR; |
| 81 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
111 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 82 | pgs = pgsSEND_ACK; |
112 | pgs = pgsSEND_ACK; |
| - | 113 | // save pgm adress. |
|
| - | 114 | ||
| 83 | } |
115 | } |
| 84 | 116 | ||
| 85 | // TODO TIMEOUT! |
- | |
| 86 | // |
117 | // Bootloader timeout |
| 87 |
|
118 | if ((tickGet()-tBoot)>BOOTLOADER_INIT_TIMOUT) |
| - | 119 | { |
|
| - | 120 | pgs = pgsDONE; |
|
| - | 121 | } |
|
| 88 | break; |
122 | break; |
| 89 | 123 | ||
| 90 | case pgsSEND_ACK: |
124 | case pgsSEND_ACK: |
| 91 | // send ack |
125 | // send ack |
| 92 | outCm.funct = FUNCT_BOOTLOADER; |
126 | outCm.funct = FUNCT_BOOTLOADER; |
| 93 | outCm.funcc = FUNCC_BOOT_ACK; |
127 | outCm.funcc = FUNCC_BOOT_ACK; |
| 94 | outCm.nid = MY_NID; |
128 | outCm.nid = MY_NID; |
| 95 | outCm.sid = MY_ID; |
129 | outCm.sid = MY_ID; |
| 96 | outCm.data_length = 8; |
130 | outCm.data_length = 8; |
| 97 | outCm.remote_request = FALSE; |
131 | outCm.remote_request = FALSE; |
| 98 | outCm.data[ERR_INDEX] = errMsg; |
132 | outCm.data[ERR_INDEX] = errMsg; |
| 99 | t = tickGet(); |
133 | t = tickGet(); |
| 100 | pgs = afterAckPgs; |
134 | pgs = afterAckPgs; |
| 101 | 135 | ||
| 102 | while(!canSendMessage(outCm,PRIO_HIGH)); |
136 | while(!canSendMessage(outCm,PRIO_HIGH)); |
| 103 | break; |
137 | break; |
| 104 | 138 | ||
| 105 | 139 | ||
| 106 | case pgsWAIT_FIRST_PGM_PACKET: |
140 | case pgsWAIT_FIRST_PGM_PACKET: |
| 107 | // wait for first pgm packet. |
141 | // wait for first pgm packet. |
| 108 | if ((tickGet()-t)>PACKET_TIMEOUT) |
142 | if ((tickGet()-t)>PACKET_TIMEOUT) |
| 109 | { |
143 | { |
| 110 | // Timeout, resend ack. |
144 | // Timeout, resend ack. |
| 111 | errMsg = ERR_NO_ERROR; |
145 | errMsg = ERR_NO_ERROR; |
| 112 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
146 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 113 | pgs = pgsSEND_ACK; |
147 | pgs = pgsSEND_ACK; |
| 114 | } |
148 | } |
| 115 | 149 | ||
| 116 | // If programming packet... |
150 | // If programming packet... |
| 117 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_PGM && cm.nid==MY_NID && cm.sid==programmerSid) |
151 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && (cm.funcc & 0x3)==FUNCC_BOOT_PGM && cm.nid==MY_NID && cm.sid==programmerSid) |
| 118 | { |
152 | { |
| - | 153 | BYTE localIndex = (BYTE)((cm.funcc & 0x1C)>>2)*8; |
|
| - | 154 | ||
| 119 | // Save bytes in memory, setup start adress etc.. |
155 | // Save bytes in memory, setup start adress etc.. |
| 120 | bytesReceived = 8; |
156 | bytesReceived = 8; |
| - | 157 | pgmData[localIndex+0]=cm.data[0]; pgmData[localIndex+1]=cm.data[1]; pgmData[localIndex+2]=cm.data[2]; pgmData[localIndex+3]=cm.data[3]; |
|
| - | 158 | pgmData[localIndex+4]=cm.data[4]; pgmData[localIndex+5]=cm.data[5]; pgmData[localIndex+6]=cm.data[6]; pgmData[localIndex+7]=cm.data[7]; |
|
| - | 159 | ||
| 121 | pgs = pgsWAIT_PGM_PACKET; |
160 | pgs = pgsWAIT_PGM_PACKET; |
| 122 | } |
161 | } |
| 123 | 162 | ||
| 124 | 163 | ||
| 125 | break; |
164 | break; |
| 126 | 165 | ||
| 127 | 166 | ||
| 128 | case pgsWAIT_PGM_PACKET: |
167 | case pgsWAIT_PGM_PACKET: |
| 129 | 168 | ||
| 130 | 169 | ||
| 131 | // If programming packet... |
170 | // If programming packet... |
| 132 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_PGM && cm.nid==MY_NID && cm.sid==programmerSid) |
171 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && (cm.funcc & 0x3)==FUNCC_BOOT_PGM && cm.nid==MY_NID && cm.sid==programmerSid) |
| 133 | { |
172 | { |
| - | 173 | BYTE localIndex = (BYTE)((cm.funcc & 0x1C)>>2)*8; |
|
| 134 | // Save bytes in memory, inc received bytes. |
174 | // Save bytes in memory, inc received bytes. |
| - | 175 | pgmData[localIndex+0]=cm.data[0]; pgmData[localIndex+1]=cm.data[1]; pgmData[localIndex+2]=cm.data[2]; pgmData[localIndex+3]=cm.data[3]; |
|
| - | 176 | pgmData[localIndex+4]=cm.data[4]; pgmData[localIndex+5]=cm.data[5]; pgmData[localIndex+6]=cm.data[6]; pgmData[localIndex+7]=cm.data[7]; |
|
| - | 177 | ||
| 135 | bytesReceived += 8; |
178 | bytesReceived += 8; |
| 136 | pgs = pgsWAIT_PGM_PACKET; |
179 | pgs = pgsWAIT_PGM_PACKET; |
| 137 | } |
180 | } |
| 138 | 181 | ||
| 139 | // If adress packet (control packet).. |
182 | // If adress packet (control packet).. |
| 140 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_ADDR && cm.nid==MY_NID && cm.sid==programmerSid) |
183 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_ADDR && cm.nid==MY_NID && cm.sid==programmerSid) |
| 141 | { |
184 | { |
| 142 | // Send ack and goto wait first packet. |
185 | // Send ack and goto wait first packet. |
| - | 186 | // save adress. |
|
| 143 | errMsg = ERR_NO_ERROR; |
187 | errMsg = ERR_NO_ERROR; |
| 144 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
188 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 145 | pgs = pgsSEND_ACK; |
189 | pgs = pgsSEND_ACK; |
| 146 | } |
190 | } |
| 147 | 191 | ||
| 148 | // If end packet... |
192 | // If end packet... |
| 149 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_DONE && cm.nid==MY_NID && cm.sid==programmerSid) |
193 | if (hasPacket==TRUE && cm.funct==FUNCT_BOOTLOADER && cm.funcc==FUNCC_BOOT_DONE && cm.nid==MY_NID && cm.sid==programmerSid) |
| 150 | { |
194 | { |
| 151 | // write program. |
195 | // write program. |
| 152 | // If no bytes received at all, assume end. |
196 | // If no bytes received at all, assume end. |
| 153 | // If not 64 bytes received, send error ack and wait for first packet. |
197 | // If not 64 bytes received, send error ack and wait for first packet. |
| 154 | // If 64, write. |
198 | // If 64, write. |
| 155 | 199 | ||
| 156 | if (bytesReceived==0) |
200 | if (bytesReceived==0) |
| 157 | { |
201 | { |
| 158 | // No received, done. |
202 | // No received, done. |
| 159 | errMsg = ERR_NO_ERROR; |
203 | errMsg = ERR_NO_ERROR; |
| 160 | afterAckPgs = pgsDONE; |
204 | afterAckPgs = pgsDONE; |
| 161 | pgs = pgsSEND_ACK; |
205 | pgs = pgsSEND_ACK; |
| 162 | } |
206 | } |
| 163 | else if (bytesReceived!=64) |
207 | else if (bytesReceived!=64) |
| 164 | { |
208 | { |
| 165 | errMsg = ERR_ERROR; |
209 | errMsg = ERR_ERROR; |
| 166 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
210 | afterAckPgs = pgsWAIT_FIRST_PGM_PACKET; |
| 167 | pgs = pgsSEND_ACK; |
211 | pgs = pgsSEND_ACK; |
| 168 | } |
212 | } |
| 169 | else |
213 | else |
| 170 | { |
214 | { |
| 171 | pgs = pgsWRITE_PROGRAM; |
215 | pgs = pgsWRITE_PROGRAM; |
| 172 | afterAckPgs = pgsDONE; |
216 | afterAckPgs = pgsDONE; |
| 173 | } |
217 | } |
| 174 | } |
218 | } |
| 175 | 219 | ||
| 176 | // If bytes received 64, write.. |
220 | // If bytes received 64, write.. |
| 177 | if (bytesReceived==64) |
221 | if (bytesReceived==64) |
| 178 | { |
222 | { |
| 179 | LED0_IO=~LED0_IO; |
223 | LED0_IO=~LED0_IO; |
| 180 | pgs = pgsWRITE_PROGRAM; |
224 | pgs = pgsWRITE_PROGRAM; |
| 181 | afterAckPgs = pgsWAIT_PGM_PACKET; |
225 | afterAckPgs = pgsWAIT_PGM_PACKET; |
| 182 | // TODO! Send ack more than once.. |
226 | // TODO! Send ack more than once.. |
| 183 | } |
227 | } |
| 184 | 228 | ||
| 185 | 229 | ||
| 186 | break; |
230 | break; |
| 187 | 231 | ||
| 188 | 232 | ||
| 189 | case pgsWRITE_PROGRAM: |
233 | case pgsWRITE_PROGRAM: |
| - | 234 | ||
| 190 | 235 | ||
| 191 | // Write program and send ack. |
236 | // Write program and send ack. |
| - | 237 | ||
| - | 238 | //Load Table pointer registers with base address to erase. |
|
| - | 239 | TBLPTRU = (BYTE)((pgmAddress & 0xFF0000)>>16); |
|
| - | 240 | TBLPTRH = (BYTE)((pgmAddress & 0xFF00)>>8); |
|
| - | 241 | TBLPTRL = (BYTE)(pgmAddress & 0xFF); |
|
| - | 242 | ||
| - | 243 | // point to Flash program memory |
|
| - | 244 | EECON1bits.EEPGD = 1; |
|
| - | 245 | // access Flash program memory |
|
| - | 246 | EECON1bits.CFGS = 0; |
|
| - | 247 | // enable write to memory |
|
| - | 248 | EECON1bits.WREN = 1; |
|
| - | 249 | // enable Row Erase operation |
|
| - | 250 | EECON1bits.FREE = 1; |
|
| - | 251 | // disable interrupts |
|
| - | 252 | INTCONbits.GIE = 0; |
|
| - | 253 | ||
| - | 254 | // Do it! |
|
| - | 255 | EECON2 = 0x55; |
|
| - | 256 | EECON2 = 0xAA; |
|
| - | 257 | EECON1bits.WR = 1; |
|
| - | 258 | ||
| - | 259 | // enable interrupts |
|
| - | 260 | INTCONbits.GIE = 1; |
|
| - | 261 | ||
| - | 262 | //Load Table pointer registers with base address to program. |
|
| - | 263 | TBLPTRU = (BYTE)((pgmAddress & 0xFF0000)>>16); |
|
| - | 264 | TBLPTRH = (BYTE)((pgmAddress & 0xFF00)>>8); |
|
| - | 265 | TBLPTRL = (BYTE)(pgmAddress & 0xFF); |
|
| - | 266 | ||
| - | 267 | // Load bytes |
|
| - | 268 | for(i=0;i<63;i++) |
|
| - | 269 | { |
|
| - | 270 | TABLAT=pgmData[i]; |
|
| - | 271 | TBLWTPOSTINC_Func(); |
|
| - | 272 | } |
|
| - | 273 | TABLAT=pgmData[63]; |
|
| - | 274 | TBLWT_Func(); |
|
| - | 275 | ||
| - | 276 | ||
| - | 277 | // point to Flash program memory |
|
| - | 278 | EECON1bits.EEPGD = 1; |
|
| - | 279 | // access Flash program memory |
|
| - | 280 | EECON1bits.CFGS = 0; |
|
| - | 281 | // enable write to memory |
|
| - | 282 | EECON1bits.WREN = 1; |
|
| - | 283 | // disable interrupts |
|
| - | 284 | INTCONbits.GIE = 0; |
|
| - | 285 | ||
| - | 286 | // Do it! |
|
| - | 287 | EECON2 = 0x55; |
|
| - | 288 | EECON2 = 0xAA; |
|
| - | 289 | EECON1bits.WR = 1; |
|
| - | 290 | ||
| - | 291 | // enable interrupts |
|
| - | 292 | INTCONbits.GIE = 1; |
|
| - | 293 | ||
| - | 294 | // disable write to memory |
|
| - | 295 | EECON1bits.WREN = 0; |
|
| - | 296 | ||
| 192 | pgmAddress += 64; |
297 | pgmAddress += 64; |
| 193 | bytesReceived = 0; |
298 | bytesReceived = 0; |
| 194 | errMsg = ERR_NO_ERROR; |
299 | errMsg = ERR_NO_ERROR; |
| 195 | pgs = pgsSEND_ACK; |
300 | pgs = pgsSEND_ACK; |
| 196 | 301 | ||
| 197 | break; |
302 | break; |
| 198 | 303 | ||
| 199 | 304 | ||
| 200 | case pgsDONE: |
305 | case pgsDONE: |
| 201 | // Goto main program. |
- | |
| 202 |
|
306 | _asm goto RM_RESET_VECTOR _endasm |
| 203 | { |
- | |
| 204 | - | ||
| 205 | LED0_IO=~LED0_IO; |
- | |
| 206 | t=tickGet(); |
- | |
| 207 | } |
- | |
| 208 | break; |
307 | break; |
| 209 | 308 | ||
| 210 | 309 | ||
| 211 | 310 | ||
| 212 | 311 | ||
| 213 | default: |
312 | default: |
| 214 | pgs = pgsWATING_START; |
313 | pgs = pgsWATING_START; |
| 215 | break; |
314 | break; |
| 216 | } |
315 | } |
| 217 | 316 | ||
| 218 | - | ||
| 219 | } |
- | |
| 220 | } |
- | |
| 221 | - | ||
| 222 | - | ||
| 223 | #pragma interruptlow HighISR |
- | |
| 224 | void HighISR(void) |
- | |
| 225 | { |
- | |
| 226 | tickUpdate(); |
- | |
| 227 | } |
- | |
| 228 | #pragma code highVector=0x08 |
- | |
| 229 | void HighVector (void) |
- | |
| 230 | { |
- | |
| 231 | _asm goto HighISR _endasm |
- | |
| 232 | } |
- | |
| 233 | #pragma code // Return to default code section |
- | |
| 234 | - | ||
| 235 | - | ||
| 236 | - | ||
| 237 | #pragma interruptlow LowISR |
- | |
| 238 | void LowISR(void) |
- | |
| 239 | { |
- | |
| 240 | 317 | ||
| 241 | } |
318 | } |
| 242 | #pragma code lowVector=0x18 |
- | |
| 243 | void LowVector (void) |
- | |
| 244 | { |
- | |
| 245 | _asm goto LowISR _endasm |
- | |
| 246 | } |
319 | } |
| 247 | #pragma code // Return to default code section |
- | |
| 248 | 320 | ||
| 249 | 321 | ||
| 250 | /* |
322 | /* |
| 251 | * Function: mainInit |
323 | * Function: mainInit |
| 252 | * |
324 | * |
| 253 | * Input: none |
325 | * Input: none |
| 254 | * Output: none |
326 | * Output: none |
| 255 | * Pre-conditions: none |
327 | * Pre-conditions: none |
| 256 | * Affects: Se code |
328 | * Affects: Se code |
| 257 | * Depends: none. |
329 | * Depends: none. |
| 258 | */ |
330 | */ |
| 259 | void mainInit() |
331 | void mainInit() |
| 260 | { |
332 | { |
| 261 | LED0_TRIS=0; |
333 | LED0_TRIS=0; |
| 262 | 334 | ||
| 263 | // Enable internal PORTB pull-ups |
335 | // Enable internal PORTB pull-ups |
| 264 | INTCON2bits.RBPU = 0; |
336 | INTCON2bits.RBPU = 0; |
| 265 | 337 | ||
| 266 | // Enable Interrupts |
338 | // Enable Interrupts |
| 267 | INTCONbits.GIEH = 1; |
339 | INTCONbits.GIEH = 1; |
| 268 | INTCONbits.GIEL = 1; |
340 | INTCONbits.GIEL = 1; |
| 269 | 341 | ||
| 270 | // Enable interrupt prirority |
342 | // Enable interrupt prirority |
| 271 | RCONbits.IPEN=1; |
343 | RCONbits.IPEN=1; |
| 272 | 344 | ||
| 273 | 345 | ||
| 274 | } |
346 | } |
| 275 | 347 | ||
| 276 | 348 | ||
| 277 | 349 | ||
| 278 | /// CAN------------------------------------------------------- |
350 | /// CAN------------------------------------------------------- |
| 279 | 351 | ||
| 280 | 352 | ||
| 281 | #ifdef USE_CAN |
353 | #ifdef USE_CAN |
| 282 | 354 | ||
| 283 | 355 | ||
| 284 | /* |
356 | /* |
| 285 | * Function: canInit |
357 | * Function: canInit |
| 286 | * |
358 | * |
| 287 | * Input: none |
359 | * Input: none |
| 288 | * Output: none |
360 | * Output: none |
| 289 | * Pre-conditions: none |
361 | * Pre-conditions: none |
| 290 | * Affects: Changes can registers. |
362 | * Affects: Changes can registers. |
| 291 | * Depends: none. |
363 | * Depends: none. |
| 292 | */ |
364 | */ |
| 293 | void canInit() |
365 | void canInit() |
| 294 | { |
366 | { |
| 295 | CANCON = 0x80; //request config mode |
367 | CANCON = 0x80; //request config mode |
| 296 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
368 | while ((CANSTAT & 0xE0) != 0x80 ); //wait for config mode request |
| 297 | 369 | ||
| 298 | // BAUD AND TQS |
370 | // BAUD AND TQS |
| 299 | 371 | ||
| 300 | BRGCON1=0; |
372 | BRGCON1=0; |
| 301 | 373 | ||
| 302 | // Sync_Seq = 2 x TQ = 1 |
374 | // Sync_Seq = 2 x TQ = 1 |
| 303 | BRGCON1bits.SJW1=0; |
375 | BRGCON1bits.SJW1=0; |
| 304 | BRGCON1bits.SJW0=1; |
376 | BRGCON1bits.SJW0=1; |
| 305 | 377 | ||
| 306 | // Setting BRP. |
378 | // Setting BRP. |
| 307 | BRGCON1=BRGCON1|CAN_BRP; |
379 | BRGCON1=BRGCON1|CAN_BRP; |
| 308 | 380 | ||
| 309 | // Maximum PHEG1 |
381 | // Maximum PHEG1 |
| 310 | BRGCON2bits.SEG2PHTS = 1; |
382 | BRGCON2bits.SEG2PHTS = 1; |
| 311 | 383 | ||
| 312 | // Phase_Seg1 = 2 x TQ = 1 |
384 | // Phase_Seg1 = 2 x TQ = 1 |
| 313 | BRGCON2bits.SEG1PH2 = 0; |
385 | BRGCON2bits.SEG1PH2 = 0; |
| 314 | BRGCON2bits.SEG1PH1 = 0; |
386 | BRGCON2bits.SEG1PH1 = 0; |
| 315 | BRGCON2bits.SEG1PH0 = 1; |
387 | BRGCON2bits.SEG1PH0 = 1; |
| 316 | 388 | ||
| 317 | // Prop_Seq = 2 x TQ = 1 |
389 | // Prop_Seq = 2 x TQ = 1 |
| 318 | BRGCON2bits.PRSEG2 = 0; |
390 | BRGCON2bits.PRSEG2 = 0; |
| 319 | BRGCON2bits.PRSEG1 = 0; |
391 | BRGCON2bits.PRSEG1 = 0; |
| 320 | BRGCON2bits.PRSEG0 = 1; |
392 | BRGCON2bits.PRSEG0 = 1; |
| 321 | 393 | ||
| 322 | // Enableing bus wake-up |
394 | // Enableing bus wake-up |
| 323 | BRGCON3bits.WAKDIS = 0; |
395 | BRGCON3bits.WAKDIS = 0; |
| 324 | 396 | ||
| 325 | // Dont use filter when wakeup |
397 | // Dont use filter when wakeup |
| 326 | BRGCON3bits.WAKFIL = 0; |
398 | BRGCON3bits.WAKFIL = 0; |
| 327 | 399 | ||
| 328 | // Phase_Seg2 = 2 x TQ = 1 |
400 | // Phase_Seg2 = 2 x TQ = 1 |
| 329 | BRGCON3bits.SEG2PH2 = 0; |
401 | BRGCON3bits.SEG2PH2 = 0; |
| 330 | BRGCON3bits.SEG2PH1 = 0; |
402 | BRGCON3bits.SEG2PH1 = 0; |
| 331 | BRGCON3bits.SEG2PH0 = 1; |
403 | BRGCON3bits.SEG2PH0 = 1; |
| 332 | 404 | ||
| 333 | ECANCON=0; |
405 | ECANCON=0; |
| 334 | ECANCONbits.MDSEL1 = 0; |
406 | ECANCONbits.MDSEL1 = 0; |
| 335 | ECANCONbits.MDSEL0 = 1; |
407 | ECANCONbits.MDSEL0 = 1; |
| 336 | 408 | ||
| 337 | 409 | ||
| 338 | // FILTERS AND MASKS |
410 | // FILTERS AND MASKS |
| 339 | RXB0CONbits.RXM1=1; |
411 | RXB0CONbits.RXM1=1; |
| 340 | RXB0CONbits.RXM0=0; |
412 | RXB0CONbits.RXM0=0; |
| 341 | 413 | ||
| 342 | 414 | ||
| 343 | // INTERRUPTS |
415 | // INTERRUPTS |
| 344 | 416 | ||
| 345 | // Diable transmit interrupt |
417 | // Diable transmit interrupt |
| 346 | TXBIE = 0; |
418 | TXBIE = 0; |
| 347 | 419 | ||
| 348 | //rx no interrupt |
420 | //rx no interrupt |
| 349 | PIR3 = 0; |
421 | PIR3 = 0; |
| 350 | PIE3 = 0; |
422 | PIE3 = 0; |
| 351 | 423 | ||
| 352 | 424 | ||
| 353 | // Programmable no buffers interrupt |
425 | // Programmable no buffers interrupt |
| 354 | BIE0 = 0x00; |
426 | BIE0 = 0x00; |
| 355 | 427 | ||
| 356 | 428 | ||
| 357 | //loopback mode or not |
429 | //loopback mode or not |
| 358 | #ifdef CAN_LOOPBACK_MODE |
430 | #ifdef CAN_LOOPBACK_MODE |
| 359 | CANCON = 0x40;//request loopback mode |
431 | CANCON = 0x40;//request loopback mode |
| 360 | while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode |
432 | while ((CANSTAT & 0xE0) != 0x40 );//wait for loopback mode |
| 361 | #else |
433 | #else |
| 362 | CANCON = 0x00;//request normal mode |
434 | CANCON = 0x00;//request normal mode |
| 363 | while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode |
435 | while ((CANSTAT & 0xE0) != 0x00 );//wait for normal mode |
| 364 | #endif |
436 | #endif |
| 365 | 437 | ||
| 366 | } |
438 | } |
| 367 | 439 | ||
| 368 | 440 | ||
| 369 | 441 | ||
| 370 | 442 | ||
| 371 | /* |
443 | /* |
| 372 | * Function: canGetPacket |
444 | * Function: canGetPacket |
| 373 | * |
445 | * |
| 374 | * Input: none |
446 | * Input: none |
| 375 | * Output: none |
447 | * Output: none |
| 376 | * Pre-conditions: a call to canISR and an packet is available and banked to RXB0 |
448 | * Pre-conditions: a call to canISR and an packet is available and banked to RXB0 |
| 377 | * Affects: Calls canParse() function in main for parsing received packet. |
449 | * Affects: Calls canParse() function in main for parsing received packet. |
| 378 | * Depends: none. |
450 | * Depends: none. |
| 379 | */ |
451 | */ |
| 380 | BOOL canGetPacket() |
452 | BOOL canGetPacket() |
| 381 | { |
453 | { |
| 382 | 454 | ||
| 383 | // Get which buffer that is ful. |
455 | // Get which buffer that is ful. |
| 384 | if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; } |
456 | if (RXB0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10000; } |
| 385 | else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; } |
457 | else if (RXB1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10001; } |
| 386 | else if (B0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10010; } |
458 | else if (B0CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10010; } |
| 387 | else if (B1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10011; } |
459 | else if (B1CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10011; } |
| 388 | else if (B2CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10100; } |
460 | else if (B2CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10100; } |
| 389 | else if (B3CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10101; } |
461 | else if (B3CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10101; } |
| 390 | else if (B4CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10110; } |
462 | else if (B4CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10110; } |
| 391 | else if (B5CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10111; } |
463 | else if (B5CONbits.RXFUL) { ECANCON=(ECANCON&0b00000)|0b10111; } |
| 392 | else return FALSE; |
464 | else return FALSE; |
| 393 | 465 | ||
| 394 | 466 | ||
| 395 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
467 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 396 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
468 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
| 397 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
469 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
| 398 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
470 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
| 399 | 471 | ||
| 400 | //funct xx xx xx xx 28 27 26 25 |
472 | //funct xx xx xx xx 28 27 26 25 |
| 401 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
473 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
| 402 | //nid xx xx 14 13 12 11 10 09 |
474 | //nid xx xx 14 13 12 11 10 09 |
| 403 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
475 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
| 404 | 476 | ||
| 405 | 477 | ||
| 406 | cm.funct=((RXB0SIDH & 0xF0)>>4); |
478 | cm.funct=((RXB0SIDH & 0xF0)>>4); |
| 407 | cm.funcc=(((WORD)(RXB0SIDH & 0x0F))<<6)+(((WORD)(RXB0SIDL & 0xE0))>>2)+(((WORD)(RXB0SIDL & 0x03))<<1)+(((WORD)(RXB0EIDH & 0x80))>>7); |
479 | cm.funcc=(((WORD)(RXB0SIDH & 0x0F))<<6)+(((WORD)(RXB0SIDL & 0xE0))>>2)+(((WORD)(RXB0SIDL & 0x03))<<1)+(((WORD)(RXB0EIDH & 0x80))>>7); |
| 408 | cm.nid=((RXB0EIDH & 0x7E)>>1); |
480 | cm.nid=((RXB0EIDH & 0x7E)>>1); |
| 409 | cm.sid=(((WORD)(RXB0EIDH & 0x01))<<8)+RXB0EIDL; |
481 | cm.sid=(((WORD)(RXB0EIDH & 0x01))<<8)+RXB0EIDL; |
| 410 | 482 | ||
| 411 | 483 | ||
| 412 | // Data length |
484 | // Data length |
| 413 | cm.data_length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0; |
485 | cm.data_length=(RXB0DLCbits.DLC3<<3)+(RXB0DLCbits.DLC2<<2)+(RXB0DLCbits.DLC1<<1)+RXB0DLCbits.DLC0; |
| 414 | 486 | ||
| 415 | // Remote request |
487 | // Remote request |
| 416 | cm.remote_request=(RXB0DLCbits.RXRTR==0?FALSE:TRUE); |
488 | cm.remote_request=(RXB0DLCbits.RXRTR==0?FALSE:TRUE); |
| 417 | 489 | ||
| 418 | // Data one bye one, for speed |
490 | // Data one bye one, for speed |
| 419 | cm.data[0]=RXB0D0; cm.data[1]=RXB0D1; |
491 | cm.data[0]=RXB0D0; cm.data[1]=RXB0D1; |
| 420 | cm.data[2]=RXB0D2; cm.data[3]=RXB0D3; |
492 | cm.data[2]=RXB0D2; cm.data[3]=RXB0D3; |
| 421 | cm.data[4]=RXB0D4; cm.data[5]=RXB0D5; |
493 | cm.data[4]=RXB0D4; cm.data[5]=RXB0D5; |
| 422 | cm.data[6]=RXB0D6; cm.data[7]=RXB0D7; |
494 | cm.data[6]=RXB0D6; cm.data[7]=RXB0D7; |
| 423 | 495 | ||
| 424 | // Clear ful status |
496 | // Clear ful status |
| 425 | RXB0CONbits.RXFUL=0; |
497 | RXB0CONbits.RXFUL=0; |
| 426 | 498 | ||
| 427 | return TRUE; |
499 | return TRUE; |
| 428 | } |
500 | } |
| 429 | 501 | ||
| 430 | 502 | ||
| 431 | /* |
503 | /* |
| 432 | * Function: canSendMessage |
504 | * Function: canSendMessage |
| 433 | * |
505 | * |
| 434 | * Input: CAN_MESSAGE to send |
506 | * Input: CAN_MESSAGE to send |
| 435 | * Output: True if packet scheduled sucess, false otherwise. |
507 | * Output: True if packet scheduled sucess, false otherwise. |
| 436 | * Pre-conditions: canInit |
508 | * Pre-conditions: canInit |
| 437 | * Affects: .. |
509 | * Affects: .. |
| 438 | * Depends: .. |
510 | * Depends: .. |
| 439 | */ |
511 | */ |
| 440 | BOOL canSendMessage(CAN_PROTO_MESSAGE cm,CAN_PRIORITY prio) |
512 | BOOL canSendMessage(CAN_PROTO_MESSAGE cm,CAN_PRIORITY prio) |
| 441 | { |
513 | { |
| 442 | 514 | ||
| 443 | if ( TXB0CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00011; } |
515 | if ( TXB0CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00011; } |
| 444 | if ( TXB1CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00100; } |
516 | if ( TXB1CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00100; } |
| 445 | if ( TXB2CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00101; } |
517 | if ( TXB2CONbits.TXREQ == 0 ) { ECANCON=(ECANCON&0b00000)|0b00101; } |
| 446 | // None of the transmit buffers were empty. |
518 | // None of the transmit buffers were empty. |
| 447 | else { return FALSE;} |
519 | else { return FALSE;} |
| 448 | 520 | ||
| 449 | if (prio<0 || prio>3) prio = 0; |
521 | if (prio<0 || prio>3) prio = 0; |
| 450 | 522 | ||
| 451 | 523 | ||
| 452 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
524 | //RXB0SIDH 28 27 26 25 24 23 22 21 |
| 453 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
525 | //RXB0SIDL 20 19 18 xx xx xx 17 16 |
| 454 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
526 | //RXB0EIDH 15 14 13 12 11 10 09 08 |
| 455 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
527 | //RXB0EIDL 07 06 05 04 03 02 01 00 |
| 456 | 528 | ||
| 457 | //funct xx xx xx xx 28 27 26 25 |
529 | //funct xx xx xx xx 28 27 26 25 |
| 458 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
530 | //funcc xx xx xx xx xx xx 24 23 22 21 20 19 18 17 16 15 |
| 459 | //nid xx xx 14 13 12 11 10 09 |
531 | //nid xx xx 14 13 12 11 10 09 |
| 460 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
532 | //sid xx xx xx xx xx xx xx 08 07 06 05 04 03 02 01 00 |
| 461 | 533 | ||
| 462 | RXB0SIDH = (BYTE)((cm.funct & 0x0F)<<4)+(BYTE)((cm.funcc & 0x03C0)>>6); |
534 | RXB0SIDH = (BYTE)((cm.funct & 0x0F)<<4)+(BYTE)((cm.funcc & 0x03C0)>>6); |
| 463 | RXB0SIDL = (BYTE)((cm.funcc & 0x003F)<<2)+(BYTE)((cm.funcc & 0x0006)>>1); |
535 | RXB0SIDL = (BYTE)((cm.funcc & 0x003F)<<2)+(BYTE)((cm.funcc & 0x0006)>>1); |
| 464 | RXB0EIDH = (BYTE)((cm.funcc & 0x0001)<<7)+(BYTE)((cm.nid & 0x3F)<<1)+(BYTE)((cm.sid & 0x0100)>>8); |
536 | RXB0EIDH = (BYTE)((cm.funcc & 0x0001)<<7)+(BYTE)((cm.nid & 0x3F)<<1)+(BYTE)((cm.sid & 0x0100)>>8); |
| 465 | RXB0EIDL = (BYTE)((cm.sid & 0x00FF)); |
537 | RXB0EIDL = (BYTE)((cm.sid & 0x00FF)); |
| 466 | 538 | ||
| 467 | // Data length |
539 | // Data length |
| 468 | RXB0DLC = 0; |
540 | RXB0DLC = 0; |
| 469 | if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length; |
541 | if (cm.data_length>8) RXB0DLC=8; else RXB0DLC=cm.data_length; |
| 470 | 542 | ||
| 471 | // Remote request |
543 | // Remote request |
| 472 | if (cm.remote_request==TRUE) |
544 | if (cm.remote_request==TRUE) |
| 473 | { |
545 | { |
| 474 | _asm |
546 | _asm |
| 475 | bsf RXB0DLC, 6, 0 |
547 | bsf RXB0DLC, 6, 0 |
| 476 | _endasm |
548 | _endasm |
| 477 | } |
549 | } |
| 478 | else |
550 | else |
| 479 | { |
551 | { |
| 480 | _asm |
552 | _asm |
| 481 | bcf RXB0DLC, 6, 0 |
553 | bcf RXB0DLC, 6, 0 |
| 482 | _endasm |
554 | _endasm |
| 483 | } |
555 | } |
| 484 | 556 | ||
| 485 | // Data one bye one, for speed |
557 | // Data one bye one, for speed |
| 486 | RXB0D0=cm.data[0]; RXB0D1=cm.data[1]; |
558 | RXB0D0=cm.data[0]; RXB0D1=cm.data[1]; |
| 487 | RXB0D2=cm.data[2]; RXB0D3=cm.data[3]; |
559 | RXB0D2=cm.data[2]; RXB0D3=cm.data[3]; |
| 488 | RXB0D4=cm.data[4]; RXB0D5=cm.data[5]; |
560 | RXB0D4=cm.data[4]; RXB0D5=cm.data[5]; |
| 489 | RXB0D6=cm.data[6]; RXB0D7=cm.data[7]; |
561 | RXB0D6=cm.data[6]; RXB0D7=cm.data[7]; |
| 490 | 562 | ||
| 491 | // set extended or not |
563 | // set extended or not |
| 492 | RXB0SIDLbits.EXID=1; |
564 | RXB0SIDLbits.EXID=1; |
| 493 | 565 | ||
| 494 | // Priority |
566 | // Priority |
| 495 | RXB0CON = (RXB0CON & 0b11111100) | prio; |
567 | RXB0CON = (RXB0CON & 0b11111100) | prio; |
| 496 | 568 | ||
| 497 | // mark as redy to transmit |
569 | // mark as redy to transmit |
| 498 | _asm |
570 | _asm |
| 499 | bsf RXB0CON, 3, 0 |
571 | bsf RXB0CON, 3, 0 |
| 500 | _endasm |
572 | _endasm |
| 501 | 573 | ||
| 502 | return TRUE; |
574 | return TRUE; |
| 503 | 575 | ||
| - | 576 | } |
|
| - | 577 | ||
| - | 578 | ||
| - | 579 | ||
| - | 580 | #endif //USE_CAN |
|
| - | 581 | ||
| - | 582 | ||
| - | 583 | ||
| - | 584 | ||
| - | 585 | void TBLRD_Func() |
|
| - | 586 | { |
|
| - | 587 | _asm |
|
| - | 588 | TBLRD |
|
| - | 589 | _endasm |
|
| - | 590 | } |
|
| - | 591 | ||
| - | 592 | void TBLWT_Func() |
|
| - | 593 | { |
|
| - | 594 | _asm |
|
| - | 595 | TBLWT |
|
| - | 596 | _endasm |
|
| - | 597 | } |
|
| - | 598 | ||
| - | 599 | ||
| - | 600 | void TBLWTPOSTINC_Func (void){ |
|
| - | 601 | _asm |
|
| - | 602 | TBLWTPOSTINC |
|
| - | 603 | _endasm |
|
| - | 604 | } |
|
| - | 605 | ||
| - | 606 | DWORD tickGet() |
|
| - | 607 | { |
|
| - | 608 | return tickCounter; |
|
| 504 | } |
609 | } |
| 505 | 610 | ||
| 506 | 611 | ||
| 507 | - | ||
| 508 | |
612 | #pragma code user = RM_RESET_VECTOR |
| 509 | 613 | ||