Subversion Repositories HomeAutomation

Rev

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

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