Subversion Repositories HomeAutomation

Rev

Rev 88 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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