-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan_drv.c
420 lines (384 loc) · 15.7 KB
/
can_drv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* can_drv.c
The contents of this file were originally from the CAN Software Library
provided by Atmel. Use of this file is subject to Atmel's End User
License Agreement.
*/
//******************************************************************************
//! @file $RCSfile: can_drv.c,v $
//!
//! Copyright (c) 2007 Atmel.
//!
//! Use of this program is subject to Atmel's End User License Agreement.
//! Please read file license.txt for copyright notice.
//!
//! @brief This file contains the low level functions (drivers) of:
//! - CAN (Controller Array Network)
//! - for AT90CAN128/64/32
//!
//! This file can be parsed by Doxygen for automatic documentation generation.
//! This file has been validated with AVRStudio-413528/WinAVR-20070122.
//!
//! @version $Revision: 3.20 $ $Name: jtellier $
//!
//! @todo
//! @bug
//******************************************************************************
//_____ I N C L U D E S ________________________________________________________
#include "config.h"
#include "can_drv.h"
//_____ D E F I N I T I O N S __________________________________________________
//_____ F U N C T I O N S ______________________________________________________
//------------------------------------------------------------------------------
// @fn can_clear_all_mob
//!
//! This function clears the Mailbox content.
//! It reset CANSTMOB, CANCDMOB, CANIDTx & CANIDMx and clears data FIFO of
//! MOb[0] upto MOb[LAST_MOB_NB].
//!
//! @warning: This version doesn't clears the data FIFO
//!
//! @param none
//!
//! @return none
//------------------------------------------------------------------------------
void can_clear_all_mob(void)
{
uint8_t mob_number;
/*
uint8_t data_index;
*/
for (mob_number = 0; mob_number < NB_MOB; mob_number++)
{
CANPAGE = (mob_number << 4); //! Page index
Can_clear_mob(); //! All MOb Registers=0
/*
for (data_index = 0; data_index < NB_DATA_MAX; data_index++)
{
CANMSG = 0; //! MOb data FIFO
}
*/
}
}
//------------------------------------------------------------------------------
// @fn can_get_mob_free
//!
//! This function returns the number of the first MOb available or 0xFF if
//! no MOb is available.
//!
//! @warning none.
//!
//! @param none.
//!
//! @return Handle of MOb.
//! - MOb[0] upto MOb[LAST_MOB_NB]
//! - 0xFF if no MOb
//------------------------------------------------------------------------------
uint8_t can_get_mob_free(void)
{
uint8_t mob_number, page_saved;
page_saved = CANPAGE;
for (mob_number = 0; mob_number < NB_MOB; mob_number++)
{
Can_set_mob(mob_number);
if ((CANCDMOB & 0xC0) == 0x00) //! Disable configuration
{
CANPAGE = page_saved;
return (mob_number);
}
}
CANPAGE = page_saved;
return (NO_MOB);
}
//------------------------------------------------------------------------------
// @fn can_get_mob_status
//!
//! This function returns information "MOB completed its job"
//! if one of the RXOK or TXOK Flag is set or "MOB not completed its job
//! if no RXOK and TXOK flags are set.
//! Previously, this function checks if the MOb is configured or not and in
//! case of the MOB not configured, the function returns "MOB_DISABLE".
//!
//! @warning none.
//!
//! @param none.
//!
//! @return MOb Status.
//! - MOB_NOT_COMPLETED
//! - MOB_TX_COMPLETED
//! - MOB_RX_COMPLETED
//! - MOB_RX_DLC_WARNING
//! - MOB_DISABLE
//! or should be a combination of the following errors
//! - MOB_ACK_ERROR
//! - MOB_FORM_ERROR
//! - MOB_CRC_ERROR
//! - MOB_STUFF_ERROR
//! - MOB_BIT_ERROR
//------------------------------------------------------------------------------
uint8_t can_get_mob_status(void)
{
uint8_t mob_status, canstmob_copy;
// Test if MOb ENABLE or DISABLE
if ((CANCDMOB & 0xC0) == 0x00) {return(MOB_DISABLE);}
canstmob_copy = CANSTMOB; // Copy for test integrity
// If MOb is ENABLE, test if MOb is COMPLETED
// - MOb Status = 0x20 then MOB_RX_COMPLETED
// - MOb Status = 0x40 then MOB_TX_COMPLETED
// - MOb Status = 0xA0 then MOB_RX_COMPLETED_DLCW
mob_status = canstmob_copy & ((1<<DLCW)|(1<<TXOK)|(1<<RXOK));
if ( (mob_status==MOB_RX_COMPLETED) || \
(mob_status==MOB_TX_COMPLETED) || \
(mob_status==MOB_RX_COMPLETED_DLCW) ) { return(mob_status); }
// If MOb is ENABLE & NOT_COMPLETED, test if MOb is in ERROR
// - MOb Status bit_0 = MOB_ACK_ERROR
// - MOb Status bit_1 = MOB_FORM_ERROR
// - MOb Status bit_2 = MOB_CRC_ERROR
// - MOb Status bit_3 = MOB_STUFF_ERROR
// - MOb Status bit_4 = MOB_BIT_ERROR
mob_status = canstmob_copy & ERR_MOB_MSK;
if (mob_status != 0) { return(mob_status); }
// If CANSTMOB = 0 then MOB_NOT_COMPLETED
return(MOB_NOT_COMPLETED);
}
//------------------------------------------------------------------------------
// @fn can_get_data
//!
//! This function copy the data from the selected MOb to the address
//! passed as parameter.
//!
//! @warning none.
//!
//! @param CAN message data address.
//!
//! @return none.
//------------------------------------------------------------------------------
void can_get_data(uint8_t* p_can_message_data)
{
uint8_t data_index;
for (data_index = 0; data_index < (Can_get_dlc()); data_index++)
{
*(p_can_message_data + data_index) = CANMSG;
}
}
//------------------------------------------------------------------------------
// @fn can_auto_baudrate
//!
//! This function programs itself the CANBTx registers if there is some
//! communication (activity) on the CAN bus.
//!
//! @warning complex function not yet implemented
//!
//! @param Evaluation needed
//! ==0: start the evaluation from faster baudrate
//! ==1: (re)start an evaluation with CANBTx registers contents
//!
//! @return Baudrate Status
//! ==0: research of bit timing configuration failed
//! ==1: baudrate performed
//------------------------------------------------------------------------------
uint8_t can_auto_baudrate (uint8_t mode)
{
uint8_t u8_temp0; //! Temporary variable
uint8_t brp, prs, ntq, phs1, phs2; //! Bit timing segment variables
uint8_t phs1_inc; //! Computing needed
uint8_t bt_not_found, wait_for_rx, evaluate; //! Keys for "while()" loops
uint8_t try_conf; //! Key for configurate CAN
uint8_t ovrtim_flag=0; //! Timer overflow count
uint16_t conf_index; //! Count of bit timing configuration tried
uint8_t bt_performed; //! Return flag
//! --- Default setting
phs1_inc = evaluate = 0;
bt_performed = 0;
conf_index = 0;
bt_not_found = 1;
//! --- Init segment variables with MIN values if mode=0
//! or init segment variables with CANBTx if mode=1
if (mode==0)
{
brp = BRP_MIN;
ntq = NTQ_MIN;
phs1 = PHS1_MIN;
phs2 = PHS2_MIN;
prs = ntq - ( phs1 + phs2 + 1 );
try_conf = 1; //! Try this configuration
wait_for_rx = 1; //! Enable "while (wait_for_rx ..." loop
}
else //! mode = 1
{
brp = Max ((((CANBT1 & BRP_MSK) >> 1) +1) , BRP_MIN );
prs = Max ((((CANBT2 & PRS_MSK) >> 1) +1) , PRS_MIN );
phs1 = Max ((((CANBT3 & PHS1_MSK) >> 1) +1) , PHS1_MIN);
phs2 = Max ((((CANBT3 & PHS2_MSK) >> 4) +1) , PHS2_MIN);
ntq = Max ((prs + phs1 + phs2 + 1) , NTQ_MIN);
phs1_inc = evaluate = 1; //! To enter in "while (evaluate ..." loop
try_conf = 0; //! Look for the next configuration
wait_for_rx = 0; //! Skip "while (wait_for_rx ..." loop
}
//! --- Clear all MOb's (CANMSG not cleared)
for (u8_temp0 = 0; u8_temp0 < NB_MOB; u8_temp0++)
{
Can_set_mob(u8_temp0); //! Page index
Can_clear_mob(); //! All MOb Registers = 0x00
}
while (bt_not_found == 1)
{
if (try_conf == 1)
{
Can_reset();
conf_index++;
ovrtim_flag=0;
//! --- CANBTx registers update (sjw = phs2/2, 3 sample points)
CANBT1 = ((brp-1) << BRP);
CANBT2 = (((phs2 >> 1)-1) << SJW) |((prs-1) << PRS);
CANBT3 = (((phs2-1) << PHS2) | ((phs1-1) << PHS1) | (1<<SMP));
//! --- Set CAN-Timer - Used for time-out
//! There are 641 (0x281) possible evaluations. The first one provides the faster
//! the faster bit timing, the last one gives the slower. It is necessary to
//! modulate the time-out versus bit timing (0x281>>3=0x50, matching an U8).
CANTCON = (uint8_t)(conf_index >> 3);
//! --- MOb configuration
Can_set_mob(MOB_0); //! Use MOb-0
CANSTMOB = 0; //! Reset MOb status (undone by "Can_reset()")
CANCDMOB = (MOB_Rx_ENA << CONMOB); //! MOb 0 in receive mode
//! CAN controller configuration
CANGCON = (1<<LISTEN) | (1<<ENASTB);//! Enable CAN controller in "listen" mode
while ((CANGSTA & (1<<ENFG)) == 0); //! Wait for Enable OK
CANGIT = 0xFF; //! Reset General errors and OVRTIM flag
}
//! --- WAIT_FOR_RX LOOP:
//! ================
//! Try to perform a CAN message reception in "LISTEN" mode without error and
//! before a time_out done by CAN-Timer.
//! Else gives the hand to "EVALUATE LOOP" to have a new set of bit timing.
while (wait_for_rx == 1)
{
u8_temp0 = CANSTMOB;
//! --- RxOK received ?
if ((u8_temp0 & (1<<RXOK)) != 0)
{ //! --- It is the successful output of "can_auto_baudrate" function
wait_for_rx = 0; //! Out of "while (wait_for_rx ..." loop
evaluate = 0; //! Will skip "while (evaluate ..." loop
bt_not_found = 0; //! Out of "while (bt_not_found ..." loop
bt_performed = 1; //! Return flag = TRUE
DISABLE_MOB; //! Disable MOb-0
CANGCON = 0x00; //! Disable CAN controller & reset "listen" mode
while ((CANGSTA & (1<<ENFG)) != 0); //! Wait for Disable OK
}
//! --- Else stop if any errors
else
{
//! --- MOb error ?
if ((u8_temp0 & ((1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))) !=0)
{
evaluate = 1; //! Will enter in "while (evaluate ..." loop
wait_for_rx = 0; //! Out of "while (wait_for_rx ..." loop
}
u8_temp0 = CANGIT;
//! --- Time_out reached ?
if ((u8_temp0 & (1<<OVRTIM)) !=0 )
{
if (ovrtim_flag==0)
{
//! --- First Time_out
CANGIT |= (1<<OVRTIM); // Reset OVRTIM
ovrtim_flag++;
}
else
{
//! --- Second Time_out
CANGIT |= (1<<OVRTIM); // Reset OVRTIM
evaluate = 1; //! Will enter in "while (evaluate ..." loop
wait_for_rx = 0; //! Out of "while (wait_for_rx ..." loop
}
}
//! --- General error ?
if ((u8_temp0 & ((1<<SERG)|(1<<CERG)|(1<<FERG)|(1<<AERG))) !=0)
{
evaluate = 1; //! Will enter in "while (evaluate ..." loop
wait_for_rx = 0; //! Out of "while (wait_for_rx ..." loop
try_conf = 1; //! Try this configuration
}
}
} // while (wait_for_rx ...
//! --- EVALUATE LOOP:
//! =============
//! Compute a new bit timing configuration. First, Phase 1 is increased,
//! then Phase2=Phase1 and if Phase1>5, Phase1 can be equal to Phase2 or
//! Phase2+1. After this, the number of TQ is increased up to its high
//! limit and after it is the Prescaler. During the computing high (80%)
//! and low (75%) limits of sampling point location are tested. SJW and
//! the number of sampling points are not calculated in this loop.
while (evaluate == 1)
{
if (phs1_inc != 0) phs1++;
phs1_inc = 1;
// --- The following test takes into account the previous incrementation of phs1
if ((phs1 > PHS1_MAX) && (phs2 >= PHS2_MAX))
{
phs1 = PHS1_MIN;
phs2 = PHS2_MIN;
phs1_inc = 0;
if (ntq != NTQ_MAX) ntq++;
else
{
ntq = NTQ_MIN;
if (brp != BRP_MAX) brp++;
else
{
//! --- It is the failing of "can_auto_baudrate" function
evaluate = 0; //! Out of "while (evaluate ..." loop
bt_performed = 0; //! Return flag = FALSE
bt_not_found = 0; //! Out of "while (bt_not_found ..." loop
DISABLE_MOB; //! Disable MOb-0
CANGCON = 0x00; //! Disable CAN controller & reset "listen" mode
while ((CANGSTA & (1<<ENFG)) != 0); //! Wait for Disable OK
}
}
}
else // if (phs1 > PHS1_MAX ...
{
//! --- If psh1 > 5 then phs1 =phs2 or =phs2+1, else phs1=phs2
if (phs1>5)
{
if (phs1>(phs2+1)) phs1=(++phs2);
}
else
{
phs2=phs1;
}
prs = ntq - ( phs1 + phs2 + 1 );
//! --- Test PRS limits
if ((prs <= PRS_MAX) && (prs >= PRS_MIN))
{
//! --- Values accepted if 80% >= sampling point >= 75%
if (((phs2<<2) >= (1+prs+phs1)) && ((phs2+phs2+phs2) <= (1+prs+phs1)))
{
evaluate = 0; //! Out of "while (evaluate ..." loop &
wait_for_rx = 1; //! new "while (bt_not_found ..." loop
}
}
}
} // while (evaluate ...
} // while (bt_not_found ...
return (bt_performed);
}
//------------------------------------------------------------------------------
// @fn can_fixed_baudrate
//!
//! This function programs the CANBTx registers with the predefined values
//! CONF_CANBT1, CONF_CANBT2, CONF_CANBT3.
//!
//! @warning
//!
//! @param (unused!)
//!
//! @return Baudrate Status
//! fixed = 1: baudrate performed
//------------------------------------------------------------------------------
uint8_t can_fixed_baudrate(uint8_t mode)
{
Can_reset();
Can_conf_bt();
return 1;
}