Subversion Repositories HomeAutomation

Rev

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

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