Subversion Repositories HomeAutomation

Rev

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

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