Subversion Repositories HomeAutomation

Rev

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

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