Subversion Repositories NaviCtrl

Rev

Rev 1 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 196
Line 1... Line 1...
1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
1
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
2
* File Name          : 91x_can.c
2
* File Name          : 91x_can.c
3
* Author             : MCD Application Team
3
* Author             : MCD Application Team
-
 
4
* Version            : V2.1
4
* Date First Issued  : 05/18/2006 : Version 1.0
5
* Date               : 12/22/2008
5
* Description        : This file provides all the CAN software functions.
6
* Description        : This file provides all the CAN firmware functions.
6
********************************************************************************
-
 
7
* History:
-
 
8
* 05/22/2007 : Version 1.2
-
 
9
* 05/24/2006 : Version 1.1
-
 
10
* 05/18/2006 : Version 1.0
-
 
11
********************************************************************************
7
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
8
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
9
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
10
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
11
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
Line 40... Line 36...
40
#define STD_RANGE_ID_ARB(range_start, range_end)        ((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))
36
#define STD_RANGE_ID_ARB(range_start, range_end)        ((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))
Line 41... Line 37...
41
 
37
 
42
#define STD_FIXED_ID_MSK(id)    ((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
38
#define STD_FIXED_ID_MSK(id)    ((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
Line 43... Line 39...
43
#define STD_FIXED_ID_ARB(id)    ((u16)(FIXED_ID_ARB(id) << 2))
39
#define STD_FIXED_ID_ARB(id)    ((u16)(FIXED_ID_ARB(id) << 2))
-
 
40
 
-
 
41
#define EXT_RANGE_ID_MSK_L(range_start, range_end)     \
-
 
42
  ((u16)(RANGE_ID_MSK((range_start), (range_end))))
44
 
43
 
-
 
44
#define EXT_RANGE_ID_MSK_H(range_start, range_end)      \
-
 
45
  ((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 16) & 0x1FFF) 
-
 
46
 
-
 
47
#define EXT_RANGE_ID_ARB_L(range_start, range_end)      \
45
#define EXT_RANGE_ID_MSK_L(range_start, range_end)      ((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 11))
48
  ((u16)(RANGE_ID_ARB((range_start), (range_end))))
46
#define EXT_RANGE_ID_MSK_H(range_start, range_end)      ((u16)(STD_RANGE_ID_MSK((range_start), (range_end)) | ((RANGE_ID_MSK((range_start), (range_end)) >> 27) & 0x03)))
49
 
47
#define EXT_RANGE_ID_ARB_L(range_start, range_end)      ((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 11))
50
#define EXT_RANGE_ID_ARB_H(range_start, range_end)       \
48
#define EXT_RANGE_ID_ARB_H(range_start, range_end)      ((u16)(STD_RANGE_ID_ARB((range_start), (range_end)) | ((RANGE_ID_ARB((range_start), (range_end)) >> 27) & 0x03)))
51
  ((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 16) & 0x1FFF)
-
 
52
 
49
 
53
#define EXT_FIXED_ID_MSK_L(id)  ((u16)(FIXED_ID_MSK(id)))
-
 
54
 
50
#define EXT_FIXED_ID_MSK_L(id)  ((u16)(FIXED_ID_MSK(id) >> 11))
55
#define EXT_FIXED_ID_MSK_H(id)  ((u16)(FIXED_ID_MSK(id) >> 16 ) & 0x1FFF)
-
 
56
 
51
#define EXT_FIXED_ID_MSK_H(id)  ((u16)(STD_FIXED_ID_MSK(id) | ((FIXED_ID_MSK(id) >> 27) & 0x03)))
57
#define EXT_FIXED_ID_ARB_L(id)  ((u16)(FIXED_ID_ARB(id)))
Line 52... Line 58...
52
#define EXT_FIXED_ID_ARB_L(id)  ((u16)(FIXED_ID_ARB(id) >> 11))
58
 
53
#define EXT_FIXED_ID_ARB_H(id)  ((u16)(STD_FIXED_ID_ARB(id) | ((FIXED_ID_ARB(id) >> 27) & 0x03)))
59
#define EXT_FIXED_ID_ARB_H(id)  ((u16)(FIXED_ID_ARB(id) >> 16) & 0x1FFF)
Line 54... Line 60...
54
 
60
 
Line 81... Line 87...
81
{
87
{
82
  /* Reset the CAN registers values*/
88
  /* Reset the CAN registers values*/
Line 83... Line 89...
83
 
89
 
84
  SCU_APBPeriphReset(__CAN,ENABLE);         /*CAN peripheral is under Reset */
90
  SCU_APBPeriphReset(__CAN,ENABLE);         /*CAN peripheral is under Reset */
85
  SCU_APBPeriphReset(__CAN,DISABLE);        /*CAN peripheral Reset off*/
-
 
86
 
91
  SCU_APBPeriphReset(__CAN,DISABLE);        /*CAN peripheral Reset off*/
87
 
92
   
Line 88... Line 93...
88
}
93
}
89
 
94
 
90
/*******************************************************************************
95
/*******************************************************************************
Line 218... Line 223...
218
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
223
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
219
*                  - idType: specifies the identifier type of the frames that
224
*                  - idType: specifies the identifier type of the frames that
220
*                    will be transmitted using this message object.
225
*                    will be transmitted using this message object.
221
*                    This parameter can be one of the following values:
226
*                    This parameter can be one of the following values:
222
*                          - CAN_STD_ID (standard ID, 11-bit)
227
*                          - CAN_STD_ID (standard ID, 11-bit)
223
*                          - CAN_EXT_ID (extended ID, 29-bit)                                
228
*                          - CAN_EXT_ID (extended ID, 29-bit)
-
 
229
*                  - RemoteEN : specifies if the CAN message will answer remote
-
 
230
*                   frames with exactly matching ID; It can be
-
 
231
*                          - ENABLE : remote frame with matching ID is answered
-
 
232
*                          - DISABLE : remote frames with matching ID is not
-
 
233
*                            answered
224
* Output         : None                                                      
234
* Output         : None                                                      
225
* Return         : An ErrorStatus enumuration value:
235
* Return         : An ErrorStatus enumuration value:
226
*                         - SUCCESS: Interface to treat the message
236
*                         - SUCCESS: Interface to treat the message
227
*                         - ERROR: No interface to treat the message
237
*                         - ERROR: No interface to treat the message
228
*******************************************************************************/
238
*******************************************************************************/
229
ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType)
239
ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType, FunctionalState RemoteEN)
230
{
240
{
231
  u32 msg_if=0;
241
  u32 msg_if=0;
Line 232... Line 242...
232
 
242
 
233
  if ((msg_if = GetFreeIF()) == 2)
243
  if ((msg_if = GetFreeIF()) == 2)
Line 240... Line 250...
240
                           | CAN_CMR_ARB
250
                           | CAN_CMR_ARB
241
                           | CAN_CMR_CONTROL
251
                           | CAN_CMR_CONTROL
242
                           | CAN_CMR_DATAA
252
                           | CAN_CMR_DATAA
243
                           | CAN_CMR_DATAB;
253
                           | CAN_CMR_DATAB;
Line 244... Line 254...
244
 
254
 
245
  CAN->sMsgObj[msg_if].M1R = 0;
255
  CAN->sMsgObj[msg_if].M1R = 0xFFFF;
Line 246... Line 256...
246
  CAN->sMsgObj[msg_if].A1R = 0;
256
  CAN->sMsgObj[msg_if].A1R = 0;
247
 
257
 
248
  if (idType == CAN_STD_ID)
258
  if (idType == CAN_STD_ID)
249
  {
259
  {
250
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR;
260
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | 0x1FFF;
251
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
261
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
252
  }
262
  }
253
  else
263
  else
254
  {
264
  {
255
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD;
265
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD | 0x1FFF;
Line 256... Line 266...
256
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
266
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
-
 
267
  }
Line 257... Line 268...
257
  }
268
 
258
 
269
  CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB | ( RemoteEN ? CAN_MCR_RMTEN : 0)
259
  CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB;
270
                            | ( RemoteEN ? CAN_MCR_UMASK : 0);
260
 
271
 
Line 308... Line 319...
308
                           | CAN_CMR_DATAA
319
                           | CAN_CMR_DATAA
309
                           | CAN_CMR_DATAB;
320
                           | CAN_CMR_DATAB;
Line 310... Line 321...
310
 
321
 
311
  if (idType == CAN_STD_ID)
322
  if (idType == CAN_STD_ID)
312
  {
323
  {
313
    CAN->sMsgObj[msg_if].M1R = 0;
324
    CAN->sMsgObj[msg_if].M1R = 0xFFFF;
Line 314... Line 325...
314
    CAN->sMsgObj[msg_if].M2R = STD_RANGE_ID_MSK(idLow, idHigh);
325
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | STD_RANGE_ID_MSK(idLow, idHigh);
315
 
326
 
316
    CAN->sMsgObj[msg_if].A1R = 0;
327
    CAN->sMsgObj[msg_if].A1R = 0;
317
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
328
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
Line 336... Line 347...
336
 
347
 
337
  return SUCCESS;
348
  return SUCCESS;
Line 338... Line 349...
338
}
349
}
339
 
350
 
340
/*******************************************************************************
351
/*******************************************************************************
341
* Function Name  : CAN_InvalidateAllMsgObj                                   
352
* Function Name  : CAN_SetUnusedAllMsgObj                                    
342
* Description    : Configures all the message objects as unused.              
353
* Description    : Configures all the message objects as unused.              
343
* Input          : None                                                      
354
* Input          : None                                                      
-
 
355
* Output         : None                                                      
-
 
356
* Return         : An ErrorStatus enumuration value:
344
* Output         : None                                                      
357
*                         - SUCCESS: Interface to treat the message
345
* Return         : None                                                      
358
*                         - ERROR: No interface to treat the messageNone                                                      
346
*******************************************************************************/
359
*******************************************************************************/
347
void CAN_InvalidateAllMsgObj(void)
360
ErrorStatus CAN_SetUnusedAllMsgObj(void)
348
{
361
{
-
 
362
  u32 i=0;
349
  u32 i=0;
363
  for (i = 0; i < 32; i++)
-
 
364
  {
-
 
365
    if ( CAN_SetUnusedMsgObj(i) == ERROR)
-
 
366
      return ERROR;
350
  for (i = 0; i < 32; i++)
367
  }
Line 351... Line -...
351
    CAN_SetUnusedMsgObj(i);
-
 
352
}
368
  return SUCCESS;
353
 
369
}
354
 
370
 
355
/*******************************************************************************
371
/*******************************************************************************
356
* Function Name  : CAN_ReleaseMessage                                        
372
* Function Name  : CAN_ReleaseMessage                                        
Line 374... Line 390...
374
  CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
390
  CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
Line 375... Line 391...
375
 
391
 
376
  return SUCCESS;
392
  return SUCCESS;
Line -... Line 393...
-
 
393
}
377
}
394
 
378
 
395
 
-
 
396
/*******************************************************************************
379
/*******************************************************************************
397
* Function Name  : CAN_UpdateMsgObj
380
* Function Name  : CAN_SendMessage                                          
398
* Description    : Updates the CAN message object with the pCanMsg fields, it
381
* Description    : Start transmission of a message                          
399
*                  does not start the transmission of the CAN message object
382
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                    
-
 
383
*                : - pCanMsg: pointer to the message structure containing data    
400
* Input 1        : message object number, from 0 to 31
384
*                    to transmit.
401
* Input 2        : pointer to the message structure containing data to transmit
385
* Output         : None                                                      
402
* Output         : None
386
* Return         : An ErrorStatus enumuration value:
403
* Return         : An ErrorStatus enumuration value:
387
*                         - SUCCESS: Transmission OK
404
*                         - SUCCESS: Interface to treat the message
388
*                         - ERROR: No transmission
405
*                         - ERROR: No interface to treat the message
389
*******************************************************************************/
406
*******************************************************************************/
390
ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
407
ErrorStatus CAN_UpdateMsgObj(u32 msgobj, canmsg* pCanMsg)
391
{
-
 
392
  if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
408
{
393
  {
-
 
394
    return ERROR;                    
-
 
395
  }
-
 
Line 396... Line 409...
396
 
409
 if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
397
  CAN->SR &= ~CAN_SR_TXOK;
410
  return ERROR;
Line 398... Line 411...
398
 
411
 
Line 399... Line 412...
399
  /* read the Arbitration and Message Control*/
412
  /* read the Arbitration and Message Control */
400
  CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;
413
  CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;
401
 
414
 
402
  CAN->sMsgObj[0].CRR = 1 + msgobj;
415
  CAN->sMsgObj[0].CRR = 1 + msgobj;
Line 403... Line 416...
403
 
416
 
404
  if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
417
  while (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
405
  {
418
  {
406
    return ERROR;                    
419
    /*Wait*/
407
  }
420
  }
408
 
421
 
Line 409... Line 422...
409
  /* update the contents needed for transmission*/
422
  /* update the contents needed for transmission */
410
  CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
423
  CAN->sMsgObj[0].CMR =   CAN_CMR_WRRD
411
                      | CAN_CMR_ARB
424
                          | CAN_CMR_ARB
412
                      | CAN_CMR_CONTROL
425
                          | CAN_CMR_CONTROL
413
                      | CAN_CMR_DATAA
426
                          | CAN_CMR_DATAA
-
 
427
                          | CAN_CMR_DATAB;
414
                      | CAN_CMR_DATAB;
428
 
415
 
429
  if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
416
  if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
430
  {
417
  {
431
    /* standard ID */
418
    /* standard ID*/
432
    CAN->sMsgObj[0].A1R = 0;
419
    CAN->sMsgObj[0].A1R = 0;
433
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
-
 
434
                          | STD_FIXED_ID_ARB(pCanMsg->Id);
420
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
435
  }
Line 421... Line 436...
421
  }
436
  else
-
 
437
  {
Line 422... Line 438...
422
  else
438
    /* extended ID*/
423
  {
439
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
424
    /* extended ID*/
440
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
425
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
441
                          | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
Line 426... Line 442...
426
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
442
  }
427
  }
443
 
-
 
444
  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT
-
 
445
                         | pCanMsg->Dlc;
-
 
446
 
-
 
447
  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1] << 8) | pCanMsg->Data[0];
428
 
448
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3] << 8) | pCanMsg->Data[2];
429
  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT | CAN_MCR_TXRQST | pCanMsg->Dlc;
449
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5] << 8) | pCanMsg->Data[4];
Line 430... Line 450...
430
 
450
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7] << 8) | pCanMsg->Data[6];
-
 
451
 
-
 
452
  CAN->sMsgObj[0].CRR = 1 + msgobj;
-
 
453
 
-
 
454
  while ( CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
-
 
455
  {
-
 
456
     /* wait */
-
 
457
  }
-
 
458
  return SUCCESS;
-
 
459
}
-
 
460
 
-
 
461
/*******************************************************************************
-
 
462
* Function Name  : CAN_SendMessage                                          
-
 
463
* Description    : Start transmission of a message                          
-
 
464
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                    
-
 
465
*                : - pCanMsg: pointer to the message structure containing data    
-
 
466
*                    to transmit.
-
 
467
* Output         : None                                                      
-
 
468
* Return         : An ErrorStatus enumuration value:
-
 
469
*                         - SUCCESS: Transmission OK
-
 
470
*                         - ERROR: No transmission
-
 
471
*******************************************************************************/
-
 
472
ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
-
 
473
{
-
 
474
   
-
 
475
  if (CAN_UpdateMsgObj(msgobj, pCanMsg) == ERROR)
-
 
476
  return ERROR;
-
 
477
 
-
 
478
  CAN->SR &= ~CAN_SR_TXOK;
-
 
479
 
-
 
480
  return ( CAN_TransmitRequest( msgobj));
-
 
481
}
-
 
482
 
-
 
483
/*******************************************************************************
-
 
484
* Function Name  : CAN_TransmitRequest                                            
-
 
485
* Description    : This function requests the transmission of a message object                                 
-
 
486
* Input          : msgobj: number of the message object that should be
-
 
487
*                  transmitted  
-
 
488
* Output         : None                                                      
-
 
489
* Return         :  An ErrorStatus enumuration value:
-
 
490
*                         - SUCCESS: Transmission OK
-
 
491
*                         - ERROR: No transmission started
-
 
492
*******************************************************************************/
-
 
493
ErrorStatus CAN_TransmitRequest( u32 msgobj )
-
 
494
{
-
 
495
    u16 msg_if;
-
 
496
   
-
 
497
    if ((msg_if = GetFreeIF()) == 2)
-
 
498
    return ERROR;
431
  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
499
 
432
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
500
     /* Set the transmit request in the command mask register */
433
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
501
     CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD | CAN_CMR_TXRQSTNEWDAT;
434
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
502
 
435
 
503
    /* Write the message object number in the command request register */
Line 453... Line 521...
453
* Return         : An ErrorStatus enumuration value:
521
* Return         : An ErrorStatus enumuration value:
454
*                         - SUCCESS: Reception OK
522
*                         - SUCCESS: Reception OK
455
*                         - ERROR: No message pending
523
*                         - ERROR: No message pending
456
*******************************************************************************/
524
*******************************************************************************/
457
ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg)
525
ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg)
458
{
526
{
-
 
527
  u32 tempId;
-
 
528
 
459
  if (!CAN_IsMessageWaiting(msgobj))
529
  if (!CAN_GetMsgReceiveStatus(msgobj))
460
  {
530
  {
461
    return ERROR;
531
    return ERROR;
462
  }
532
  }
Line 463... Line 533...
463
 
533
 
Line 487... Line 557...
487
  }
557
  }
488
  else
558
  else
489
  {
559
  {
490
    /* extended ID*/
560
    /* extended ID*/
491
    pCanMsg->IdType = CAN_EXT_ID;
561
    pCanMsg->IdType = CAN_EXT_ID;
492
    pCanMsg->Id  = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF);
562
    tempId = ((u32)(CAN->sMsgObj[1].A2R & 0x1FFF) << 16);
493
    pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
563
    pCanMsg->Id  = CAN->sMsgObj[1].A1R | tempId;
494
    pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
-
 
495
  }
564
  }
Line 496... Line 565...
496
 
565
 
Line 497... Line 566...
497
  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
566
  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
Line 511... Line 580...
511
/*******************************************************************************
580
/*******************************************************************************
512
* Function Name  : CAN_WaitEndOfTx                                          
581
* Function Name  : CAN_WaitEndOfTx                                          
513
* Description    : Waits until current transmission is finished.              
582
* Description    : Waits until current transmission is finished.              
514
* Input          : None                                                      
583
* Input          : None                                                      
515
* Output         : None                                                      
584
* Output         : None                                                      
516
* Return         : An ErrorStatus enumuration value:
585
* Return         : None
517
*                         - SUCCESS: Transmission ended
-
 
518
*                         - ERROR: Transmission did not occur yet
-
 
519
*******************************************************************************/
586
*******************************************************************************/
520
ErrorStatus CAN_WaitEndOfTx(void)
587
void CAN_WaitEndOfTx(void)
521
{
588
{
522
  if ((CAN->SR & CAN_SR_TXOK) == 0)
589
  while ((CAN->SR & CAN_SR_TXOK) == 0)
523
  {
590
  {
524
    return ERROR;
591
    /*Wait*/
525
  }
592
  }
526
  CAN->SR &= ~CAN_SR_TXOK;
593
  CAN->SR &= ~CAN_SR_TXOK;
527
 
-
 
528
  return SUCCESS;
-
 
529
}
594
}
Line 530... Line 595...
530
 
595
 
531
/*******************************************************************************
596
/*******************************************************************************
532
* Function Name  : CAN_BasicSendMessage                                      
597
* Function Name  : CAN_BasicSendMessage                                      
Line 543... Line 608...
543
{
608
{
544
  /* clear NewDat bit in IF2 to detect next reception*/
609
  /* clear NewDat bit in IF2 to detect next reception*/
545
  CAN->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;
610
  CAN->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;
Line 546... Line 611...
546
 
611
 
-
 
612
  CAN->SR &= ~CAN_SR_TXOK;
547
  CAN->SR &= ~CAN_SR_TXOK;
613
 
548
  CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
614
  CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
549
                      | CAN_CMR_ARB
615
                      | CAN_CMR_ARB
550
                      | CAN_CMR_CONTROL
616
                      | CAN_CMR_CONTROL
551
                      | CAN_CMR_DATAA
617
                      | CAN_CMR_DATAA
Line 557... Line 623...
557
    CAN->sMsgObj[0].A1R = 0;
623
    CAN->sMsgObj[0].A1R = 0;
558
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
624
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
559
  }
625
  }
560
  else
626
  else
561
  {
627
  {
562
    /* extended ID*/
628
    /* extended ID */
563
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
629
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
564
    CAN->sMsgObj[0].A2R = ((CAN->sMsgObj[0].A2R) & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
630
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | CAN_A2R_XTD | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
565
  }
631
  }
Line 566... Line 632...
566
 
632
 
Line 567... Line 633...
567
  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;
633
  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;
568
 
634
 
569
  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
635
  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
570
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
636
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
Line 571... Line 637...
571
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
637
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
572
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
638
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
573
 
-
 
574
  /* request transmission*/
-
 
575
  if (CAN->sMsgObj[0].CRR == CAN_CRR_BUSY )
-
 
576
  {
639
 
577
    return ERROR;
640
  /* request the transmission*/
578
  }
641
  CAN->sMsgObj[0].CRR = CAN_CRR_BUSY;
Line 579... Line 642...
579
 
642
 
580
  return SUCCESS;
643
  return SUCCESS;
Line 590... Line 653...
590
*                         - SUCCESS: Reception OK
653
*                         - SUCCESS: Reception OK
591
*                         - ERROR: No message pending
654
*                         - ERROR: No message pending
592
*******************************************************************************/
655
*******************************************************************************/
593
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
656
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
594
{
657
{
-
 
658
  u32 tmpId;
-
 
659
 
595
  if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
660
  if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
596
  {
661
  {
597
    return ERROR;
662
    return ERROR;
598
  }
663
  }
Line 612... Line 677...
612
  }
677
  }
613
  else
678
  else
614
  {
679
  {
615
    /* extended ID*/
680
    /* extended ID*/
616
    pCanMsg->IdType = CAN_EXT_ID;
681
    pCanMsg->IdType = CAN_EXT_ID;
617
    pCanMsg->Id  = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF);
682
    tmpId = ((u32)(CAN->sMsgObj[1].A2R & 0x1FFF) << 16);
618
    pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
683
    pCanMsg->Id  = CAN->sMsgObj[1].A1R | tmpId;
619
    pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
-
 
620
  }
684
  }
Line 621... Line 685...
621
 
685
 
Line 622... Line 686...
622
  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
686
  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
Line 714... Line 778...
714
  CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
778
  CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
715
  CAN->sMsgObj[1].CRR = 1 + msgobj;
779
  CAN->sMsgObj[1].CRR = 1 + msgobj;
716
}
780
}
Line 717... Line 781...
717
 
781
 
718
/*******************************************************************************
782
/*******************************************************************************
719
* Function Name  : CAN_IsMessageWaiting                                      
783
* Function Name  : CAN_GetMsgReceiveStatus
720
* Description    : Tests the waiting status of a received message.            
784
* Description    : Test the waiting status of a received message
721
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
785
* Input 1        : message object number, from 0 to 31
722
* Output         : None                                                      
786
* Output         : None
723
* Return         : A non-zero value if the corresponding message object has  
787
* Return         : SET value if the corresponding message object has
724
*                  received a message waiting to be copied, else 0.          
788
*                  received a message waiting to be copied, else RESET
725
*******************************************************************************/
789
*******************************************************************************/
726
u32 CAN_IsMessageWaiting(u32 msgobj)
790
FlagStatus CAN_GetMsgReceiveStatus(u32 msgobj)
-
 
791
{
-
 
792
  if( msgobj < 16 )
-
 
793
  {
-
 
794
    if ( CAN->ND1R & (1 << msgobj) )
-
 
795
        return SET;
-
 
796
    else
-
 
797
        return RESET;
-
 
798
  }
-
 
799
  else
727
{
800
  {
-
 
801
     if ( CAN->ND2R & (1 << (msgobj - 16) ) )
-
 
802
         return SET;
-
 
803
     else
-
 
804
         return RESET;
728
  return (msgobj < 16 ? CAN->ND1R & (1 << msgobj) : CAN->ND2R & (1 << (msgobj-16)));
805
  }
Line 729... Line 806...
729
}
806
}
730
 
807
 
731
/*******************************************************************************
808
/*******************************************************************************
-
 
809
* Function Name  : CAN_GetMsgTransmitRequestStatus
-
 
810
* Description    : Test the request status of a transmitted message
-
 
811
* Input 1        : message object number, from 0 to 31
-
 
812
* Output         : None
-
 
813
* Return         : SET if the corresponding message is requested
-
 
814
*                  to transmit, else RESET
-
 
815
*******************************************************************************/
-
 
816
FlagStatus CAN_GetMsgTransmitRequestStatus(u32 msgobj)
-
 
817
{
-
 
818
   if( msgobj < 16 )
-
 
819
  {
-
 
820
    if ( CAN->TXR1R & (1 << msgobj) )
-
 
821
        return SET;
-
 
822
    else
-
 
823
        return RESET;
-
 
824
  }
-
 
825
  else
-
 
826
  {
-
 
827
     if ( CAN->TXR2R & (1 << (msgobj - 16) ) )
-
 
828
         return SET;
-
 
829
     else
-
 
830
         return RESET;
-
 
831
  }  
-
 
832
}
-
 
833
 
-
 
834
/*******************************************************************************
-
 
835
* Function Name  : CAN_GetMsgInterruptStatus
-
 
836
* Description    : Test the interrupt status of a message object
-
 
837
* Input 1        : message object number, from 0 to 31
-
 
838
* Output         : None
-
 
839
* Return         : SET if the corresponding message has an
-
 
840
*                  interrupt pending, else RESET
-
 
841
*******************************************************************************/
-
 
842
FlagStatus CAN_GetMsgInterruptStatus(u32 msgobj)
-
 
843
{
-
 
844
   if( msgobj < 16 )
-
 
845
  {
-
 
846
    if ( CAN->IP1R & (1 << msgobj) )
-
 
847
        return SET;
-
 
848
    else
-
 
849
        return RESET;
-
 
850
  }
-
 
851
  else
-
 
852
  {
-
 
853
     if ( CAN->IP2R & (1 << (msgobj - 16) ) )
-
 
854
         return SET;
-
 
855
     else
-
 
856
         return RESET;
-
 
857
  }  
-
 
858
}
-
 
859
 
-
 
860
/*******************************************************************************
732
* Function Name  : CAN_IsTransmitRequested                                  
861
* Function Name  : CAN_GetMsgValidStatus
-
 
862
* Description    : Test the validity of a message object (ready to use)
-
 
863
* Input 1        : message object number, from 0 to 31
-
 
864
* Output         : None
-
 
865
* Return         : SET if the corresponding message object is valid
-
 
866
*                  else RESET
-
 
867
*******************************************************************************/
-
 
868
FlagStatus CAN_GetMsgValidStatus(u32 msgobj)
-
 
869
{
-
 
870
  if( msgobj < 16 )
-
 
871
  {
-
 
872
    if ( CAN->MV1R & (1 << msgobj) )
-
 
873
        return SET;
-
 
874
    else
-
 
875
        return RESET;
-
 
876
  }
-
 
877
  else
-
 
878
  {
-
 
879
     if ( CAN->MV2R & (1 << (msgobj - 16) ) )
-
 
880
         return SET;
-
 
881
     else
-
 
882
         return RESET;
-
 
883
  }  
-
 
884
}
-
 
885
 
-
 
886
/*******************************************************************************
-
 
887
* Function Name  : CAN_GetFlagStatus                                          
-
 
888
* Description    : Returns the flasg status of the flag passed in parameter      
-
 
889
* Input          : One of the following parameters:
-
 
890
*                  - CAN_SR_TXOK
-
 
891
*                  - CAN_SR_RXOK
-
 
892
*                  - CAN_SR_EPASS
733
* Description    : Tests the request status of a transmitted message.          
893
*                  - CAN_SR_EWARN
734
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
-
 
735
* Output         : None                                                      
894
*                  - CAN_SR_BOFF          
736
* Return         : A non-zero value if the corresponding message is requested
895
* Output         : None                                                      
737
*                  to transmit, else 0.                                      
896
* Return         : 1 if the flag is set else 0                                          
738
*******************************************************************************/
897
*******************************************************************************/
739
u32 CAN_IsTransmitRequested(u32 msgobj)
898
FlagStatus CAN_GetFlagStatus ( u32 CAN_Flag )
-
 
899
{
-
 
900
  if( CAN->SR & CAN_Flag)
-
 
901
   {
-
 
902
     return SET;
-
 
903
   }
740
{
904
     else
Line 741... Line 905...
741
  return (msgobj < 16 ? CAN->TXR1R & (1 << msgobj) : CAN->TXR2R & (1 << (msgobj-16)));
905
      return RESET;
742
}
906
}
743
 
907
 
744
/*******************************************************************************
908
/*******************************************************************************
745
* Function Name  : CAN_IsInterruptPending                                    
909
* Function Name  : CAN_GetTransmitErrorCounter                                            
746
* Description    : Tests the interrupt status of a message object.            
910
* Description    : Reads the CAN cell transmit error counter        
747
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
-
 
748
* Output         : None                                                      
911
* Input          : None                      
749
* Return         : A non-zero value if the corresponding message has an      
912
* Output         : None                                                      
750
*                  interrupt pending, else 0.                                
913
* Return         : Transmit Error Counter value between 0..255                                            
751
*******************************************************************************/
914
*******************************************************************************/
752
u32 CAN_IsInterruptPending(u32 msgobj)
915
u32 CAN_GetTransmitErrorCounter ( void )
Line 753... Line 916...
753
{
916
{
754
  return (msgobj < 16 ? CAN->IP1R & (1 << msgobj) : CAN->IP2R & (1 << (msgobj-16)));
917
 return( CAN->ERR & 0x00FF );
755
}
918
}
756
 
919
 
757
/*******************************************************************************
920
/*******************************************************************************
758
* Function Name  : CAN_IsObjectValid                                        
921
* Function Name  : CAN_GetReceiveErrorCounter                                            
759
* Description    : Tests the validity of a message object (ready to use).      
-
 
760
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
922
* Description    : Reads the CAN cell receive error counter        
761
* Output         : None                                                      
923
* Input          : None                      
762
* Return         : A non-zero value if the corresponding message object is  
924
* Output         : None                                                      
763
*                  valid, else 0.                                            
925
* Return         : Receive Error Counter value between 0..127                                            
764
*******************************************************************************/
926
*******************************************************************************/
-
 
927
u32 CAN_GetReceiveErrorCounter ( void )
765
u32 CAN_IsObjectValid(u32 msgobj)
928
{