summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_blinky_simple_startup/str91x_lib/src/91x_can.c
blob: e560d03c5a46e51b54ea7f6c7a4074ec4d7ec68b (plain)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_can.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the CAN software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_can.h"
#include "91x_scu.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Macro Name     : xxx_ID_MSK, xxx_ID_ARB                                    */
/* Description    : Form the Mask and Arbitration registers value to filter   */
/*                  a range of identifiers or a fixed identifier, for standard*/
/*                  and extended IDs                                          */
/*----------------------------------------------------------------------------*/
#define RANGE_ID_MSK(range_start, range_end)	(~((range_end) - (range_start)))
#define RANGE_ID_ARB(range_start, range_end)	((range_start) & (range_end))

#define FIXED_ID_MSK(id)	RANGE_ID_MSK((id), (id))
#define FIXED_ID_ARB(id)	RANGE_ID_ARB((id), (id))

#define STD_RANGE_ID_MSK(range_start, range_end)	((u16)((RANGE_ID_MSK((range_start), (range_end)) & 0x7FF) << 2))
#define STD_RANGE_ID_ARB(range_start, range_end)	((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))

#define STD_FIXED_ID_MSK(id)	((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
#define STD_FIXED_ID_ARB(id)	((u16)(FIXED_ID_ARB(id) << 2))

#define EXT_RANGE_ID_MSK_L(range_start, range_end)	((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 11))
#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)))
#define EXT_RANGE_ID_ARB_L(range_start, range_end)	((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 11))
#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)))

#define EXT_FIXED_ID_MSK_L(id)	((u16)(FIXED_ID_MSK(id) >> 11))
#define EXT_FIXED_ID_MSK_H(id)	((u16)(STD_FIXED_ID_MSK(id) | ((FIXED_ID_MSK(id) >> 27) & 0x03)))
#define EXT_FIXED_ID_ARB_L(id)	((u16)(FIXED_ID_ARB(id) >> 11))
#define EXT_FIXED_ID_ARB_H(id)	((u16)(STD_FIXED_ID_ARB(id) | ((FIXED_ID_ARB(id) >> 27) & 0x03)))

/* macro to format the timing register value from the timing parameters*/
#define CAN_TIMING(tseg1, tseg2, sjw, brp)	((((tseg2-1) & 0x07) << 12) | (((tseg1-1) & 0x0F) << 8) | (((sjw-1) & 0x03) << 6) | ((brp-1) & 0x3F))

/* Private variables ---------------------------------------------------------*/
/* array of pre-defined timing parameters for standard bitrates*/
u16 CanTimings[] = {       /* value   bitrate     NTQ  TSEG1  TSEG2  SJW  BRP */
  CAN_TIMING(11, 4, 4, 5), /* 0x3AC4  100 kbit/s  16   11     4      4    5   */
  CAN_TIMING(11, 4, 4, 4), /* 0x3AC3  125 kbit/s  16   11     4      4    4   */
  CAN_TIMING( 4, 3, 3, 4), /* 0x2383  250 kbit/s   8    4     3      3    4   */
  CAN_TIMING(13, 2, 1, 1), /* 0x1C00  500 kbit/s  16   13     2      1    1   */
  CAN_TIMING( 4, 3, 1, 1), /* 0x2300  1 Mbit/s     8    4     3      1    1   */
};

/* Private function prototypes -----------------------------------------------*/
static u32 GetFreeIF(void);
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : CAN_DeInit                                                
* Description    : Deinitializes the CAN peripheral registers to their default     
*                  reset values.                                     
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_DeInit (void)
{
  /* Reset the CAN registers values*/
 
  SCU_APBPeriphReset(__CAN,ENABLE);         /*CAN peripheral is under Reset */
  SCU_APBPeriphReset(__CAN,DISABLE);        /*CAN peripheral Reset off*/
 
  
}

/*******************************************************************************
* Function Name  : CAN_Init                                                  
* Description    : Initializes the CAN peripheral according to the specified 
*                  parameters in the CAN_InitStruct.                                            
* Input          : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
*                  contains the configuration information for the CAN peripheral. 
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
{
  CAN_EnterInitMode(CAN_CR_CCE | CAN_InitStruct->CAN_ConfigParameters);
  CAN_SetBitrate(CAN_InitStruct->CAN_Bitrate);
  CAN_LeaveInitMode();
  CAN_LeaveTestMode();
}

/*******************************************************************************
* Function Name  : CAN_StructInit		                        
* Description    : Fills each CAN_InitStruct member with its reset value.	      
* Input          : CAN_InitStruct : pointer to a CAN_InitTypeDef structure which       
*                  will be initialized. 
* Output         : None                  
* Return         : None.						      
*******************************************************************************/
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
{
/* Reset CAN init structure parameters values */
  CAN_InitStruct->CAN_ConfigParameters = 0x0;
  CAN_InitStruct->CAN_Bitrate = 0x2301;
}

/*******************************************************************************
* Function Name  : CAN_SetBitrate                                            
* Description    : Setups a standard CAN bitrate.                              
* Input          : bitrate: specifies the bit rate.                       
* Output         : None                                                      
* Return         : None                                                                         
*******************************************************************************/
void CAN_SetBitrate(u32 bitrate)
{
  CAN->BTR = CanTimings[bitrate];  /* write the predefined timing value */
  CAN->BRPR = 0; 		     /* clear the Extended Baud Rate Prescaler */
}

/*******************************************************************************
* Function Name  : CAN_SetTiming                                             
* Description    : Setups the CAN timing with specific parameters             
* Input          : - tseg1: specifies Time Segment before the sample point.
*                    This parameter must be a number between 1 and 16.       
*                  - tseg2: Time Segment after the sample point. This parameter 
*                    must be a number between 1 and 8.        
*                  - sjw: Synchronisation Jump Width. This parameter must be                 
*                     a number between 1 and 4.
*                  - brp: Baud Rate Prescaler. This parameter must be a number
*                    between 1 and 1024.                                         
* Output         : None                                                      
* Return         : None                                                                       
*******************************************************************************/
void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp)
{
  CAN->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp);
  CAN->BRPR = ((brp-1) >> 6) & 0x0F;
}

/*******************************************************************************
* Function Name  : GetFreeIF                                             
* Description    : Searchs the first free message interface, starting from 0.  
* Input          : None                                                      
* Output         : None                                                      
* Return         : A free message interface number (0 or 1) if found, else 2 
*******************************************************************************/
static u32 GetFreeIF(void)
{
  if ((CAN->sMsgObj[0].CRR & CAN_CRR_BUSY) == 0)
    return 0;
  else if ((CAN->sMsgObj[1].CRR & CAN_CRR_BUSY) == 0)
    return 1;
  else
   return 2;
}

/*******************************************************************************
* Function Name  : CAN_SetUnusedMsgObj                                       
* Description    : Configures the message object as unused                   
* Input          : msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Interface to treat the message
*                         - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetUnusedMsgObj(u32 msgobj)
{
  u32 msg_if=0;

  if ((msg_if = GetFreeIF()) == 2)
  {
    return ERROR;
  }

  CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
                           | CAN_CMR_MASK
                           | CAN_CMR_ARB
                           | CAN_CMR_CONTROL
                           | CAN_CMR_DATAA
                           | CAN_CMR_DATAB;

  CAN->sMsgObj[msg_if].M1R = 0;
  CAN->sMsgObj[msg_if].M2R = 0;

  CAN->sMsgObj[msg_if].A1R = 0;
  CAN->sMsgObj[msg_if].A2R = 0;

  CAN->sMsgObj[msg_if].MCR = 0;

  CAN->sMsgObj[msg_if].DA1R = 0;
  CAN->sMsgObj[msg_if].DA2R = 0;
  CAN->sMsgObj[msg_if].DB1R = 0;
  CAN->sMsgObj[msg_if].DB2R = 0;

 CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
 
 return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_SetTxMsgObj                                           
* Description    : Configures the message object as TX.                        
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
*                  - idType: specifies the identifier type of the frames that
*                    will be transmitted using this message object.
*                    This parameter can be one of the following values:
*                          - CAN_STD_ID (standard ID, 11-bit)
*                          - CAN_EXT_ID (extended ID, 29-bit)                                
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Interface to treat the message
*                         - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType)
{
  u32 msg_if=0;

  if ((msg_if = GetFreeIF()) == 2)
  {
    return ERROR;
  }
  
  CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
                           | CAN_CMR_MASK
                           | CAN_CMR_ARB
                           | CAN_CMR_CONTROL
                           | CAN_CMR_DATAA
                           | CAN_CMR_DATAB;

  CAN->sMsgObj[msg_if].M1R = 0;
  CAN->sMsgObj[msg_if].A1R = 0;

  if (idType == CAN_STD_ID)
  {
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR;
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
  }
  else
  {
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD;
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
  }

  CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB;

  CAN->sMsgObj[msg_if].DA1R = 0;
  CAN->sMsgObj[msg_if].DA2R = 0;
  CAN->sMsgObj[msg_if].DB1R = 0;
  CAN->sMsgObj[msg_if].DB2R = 0;

  CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
  
  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_SetRxMsgObj                                           
* Description    : Configures the message object as RX.                        
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                    
*                  - idType: specifies the identifier type of the frames that
*                    will be transmitted using this message object.
*                    This parameter can be one of the following values:
*                          - CAN_STD_ID (standard ID, 11-bit)
*                          - CAN_EXT_ID (extended ID, 29-bit)                               
*                  - idLow: specifies the low part of the identifier range used      
*                    for acceptance filtering.
*                  - idHigh: specifies the high part of the identifier range    
*                    used for acceptance filtering.
*                  - singleOrFifoLast: specifies the end-of-buffer indicator.
*                    This parameter can be one of the following values:
*                          - TRUE: for a single receive object or a FIFO receive
*                            object that is the last one of the FIFO. 
*                          - FALSE: for a FIFO receive object that is not the 
*                            last one. 
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Interface to treat the message
*                         - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh, bool singleOrFifoLast)
{
  u32 msg_if=0;

  if ((msg_if = GetFreeIF()) == 2)
  {
    return ERROR;
  }
  
  CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
                           | CAN_CMR_MASK
                           | CAN_CMR_ARB
                           | CAN_CMR_CONTROL
                           | CAN_CMR_DATAA
                           | CAN_CMR_DATAB;

  if (idType == CAN_STD_ID)
  {
    CAN->sMsgObj[msg_if].M1R = 0;
    CAN->sMsgObj[msg_if].M2R = STD_RANGE_ID_MSK(idLow, idHigh);

    CAN->sMsgObj[msg_if].A1R = 0;
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
  }
  else
  {
    CAN->sMsgObj[msg_if].M1R = EXT_RANGE_ID_MSK_L(idLow, idHigh);
    CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | EXT_RANGE_ID_MSK_H(idLow, idHigh);

    CAN->sMsgObj[msg_if].A1R = EXT_RANGE_ID_ARB_L(idLow, idHigh);
    CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_XTD | EXT_RANGE_ID_ARB_H(idLow, idHigh);
  }

  CAN->sMsgObj[msg_if].MCR = CAN_MCR_RXIE | CAN_MCR_UMASK | (singleOrFifoLast ? CAN_MCR_EOB : 0);

  CAN->sMsgObj[msg_if].DA1R = 0;
  CAN->sMsgObj[msg_if].DA2R = 0;
  CAN->sMsgObj[msg_if].DB1R = 0;
  CAN->sMsgObj[msg_if].DB2R = 0;

  CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
  
  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_InvalidateAllMsgObj				      
* Description    : Configures all the message objects as unused.               
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_InvalidateAllMsgObj(void)
{
  u32 i=0;
  for (i = 0; i < 32; i++)
    CAN_SetUnusedMsgObj(i);
}


/*******************************************************************************
* Function Name  : CAN_ReleaseMessage					      
* Description    : Releases the message object                                
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                     
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Interface to treat the message
*                         - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_ReleaseMessage(u32 msgobj)
{
  u32 msg_if=0;

  if ((msg_if = GetFreeIF()) == 2)
  {
    return ERROR;
  }

  CAN->sMsgObj[msg_if].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
  CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
  
  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_SendMessage                                           
* Description    : Start transmission of a message                           
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                    
*                : - pCanMsg: pointer to the message structure containing data     
*                    to transmit.
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Transmission OK
*                         - ERROR: No transmission
*******************************************************************************/
ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
{
  if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
  {
    return ERROR;                    
  }

  CAN->SR &= ~CAN_SR_TXOK;

  /* read the Arbitration and Message Control*/
  CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;

  CAN->sMsgObj[0].CRR = 1 + msgobj;

  if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
  {
    return ERROR;                    
  }

  /* update the contents needed for transmission*/
  CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
                      | CAN_CMR_ARB
                      | CAN_CMR_CONTROL
                      | CAN_CMR_DATAA
                      | CAN_CMR_DATAB;

  if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
  {
    /* standard ID*/
    CAN->sMsgObj[0].A1R = 0;
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
  }
  else
  {
    /* extended ID*/
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
  }

  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT | CAN_MCR_TXRQST | pCanMsg->Dlc;

  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];

  CAN->sMsgObj[0].CRR = 1 + msgobj;

  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_ReceiveMessage                                        
* Description    : Gets the message, if received.
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                     
*                  - release: specifies the message release indicator.
*                    This parameter can be one of the following values:
*                          - TRUE: the message object is released when getting  
*                            the data.
*                          - FALSE: the message object is not released.
*                  - pCanMsg: pointer to the message structure where received   
*                    data is copied.
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Reception OK
*                         - ERROR: No message pending
*******************************************************************************/
ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg)
{
  if (!CAN_IsMessageWaiting(msgobj))
  {
    return ERROR;
  }

  CAN->SR &= ~CAN_SR_RXOK;

  /* read the message contents*/
  CAN->sMsgObj[1].CMR = CAN_CMR_MASK
                      | CAN_CMR_ARB
                      | CAN_CMR_CONTROL
                      | CAN_CMR_CLRINTPND
                      | (release ? CAN_CMR_TXRQSTNEWDAT : 0)
                      | CAN_CMR_DATAA
                      | CAN_CMR_DATAB;

  CAN->sMsgObj[1].CRR = 1 + msgobj;

  if (CAN->sMsgObj[1].CRR & CAN_CRR_BUSY)
  {
    return ERROR;                    
  }
  
  if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
  {
    /* standard ID*/
    pCanMsg->IdType = CAN_STD_ID;
    pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
  }
  else
  {
    /* extended ID*/
    pCanMsg->IdType = CAN_EXT_ID;
    pCanMsg->Id  = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF); 
    pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
    pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
  }

  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;

  pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
  pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
  pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
  pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
  pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
  pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
  pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
  pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);

  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_WaitEndOfTx                                           
* Description    : Waits until current transmission is finished.               
* Input          : None                                                      
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Transmission ended
*                         - ERROR: Transmission did not occur yet
*******************************************************************************/
ErrorStatus CAN_WaitEndOfTx(void)
{
  if ((CAN->SR & CAN_SR_TXOK) == 0)
  {
    return ERROR;
  }
  CAN->SR &= ~CAN_SR_TXOK;
  
  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_BasicSendMessage                                      
* Description    : Starts transmission of a message in BASIC mode. This mode 
*                  does not use the message RAM.             
* Input          : pCanMsg: Pointer to the message structure containing data to       
*                  transmit.                                                  
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Transmission OK
*                         - ERROR: No transmission
*******************************************************************************/
ErrorStatus CAN_BasicSendMessage(canmsg* pCanMsg)
{
  /* clear NewDat bit in IF2 to detect next reception*/
  CAN->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;

  CAN->SR &= ~CAN_SR_TXOK;
  CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
                      | CAN_CMR_ARB
                      | CAN_CMR_CONTROL
                      | CAN_CMR_DATAA
                      | CAN_CMR_DATAB;

  if (pCanMsg->IdType == CAN_STD_ID)
  {
    /* standard ID*/
    CAN->sMsgObj[0].A1R = 0;
    CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
  }
  else
  {
    /* extended ID*/
    CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
    CAN->sMsgObj[0].A2R = ((CAN->sMsgObj[0].A2R) & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
  }

  CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;

  CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
  CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
  CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
  CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];

  /* request transmission*/
  if (CAN->sMsgObj[0].CRR == CAN_CRR_BUSY )
  {
    return ERROR;
  }

  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_BasicReceiveMessage                                   
* Description    : Gets the message in BASIC mode, if received. This mode does
*                  not use the message RAM.                
* Input          : pCanMsg: pointer to the message structure where message is copied.    
* Output         : None                                                      
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Reception OK
*                         - ERROR: No message pending
*******************************************************************************/
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
{
  if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
  {
    return ERROR;
  }

  CAN->SR &= ~CAN_SR_RXOK;

  CAN->sMsgObj[1].CMR = CAN_CMR_ARB
                      | CAN_CMR_CONTROL
                      | CAN_CMR_DATAA
                      | CAN_CMR_DATAB;

  if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
  {
    /* standard ID*/
    pCanMsg->IdType = CAN_STD_ID;
    pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
  }
  else
  {
    /* extended ID*/
    pCanMsg->IdType = CAN_EXT_ID;
    pCanMsg->Id  = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF);
    pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
    pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
  }

  pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;

  pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
  pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
  pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
  pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
  pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
  pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
  pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
  pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);

  return SUCCESS;
}

/*******************************************************************************
* Function Name  : CAN_EnterInitMode                                         
* Description    : Switchs the CAN into initialization mode. This function must
*                  be used in conjunction with CAN_LeaveInitMode().                 
* Input          : InitMask: specifies the CAN configuration in normal mode.      
* Output         : None                                                      
* Return         : None                                                          
*******************************************************************************/
void CAN_EnterInitMode(u8 InitMask)
{
  CAN->CR = InitMask | CAN_CR_INIT;
  CAN->SR = 0;					/* reset the status*/
}

/*******************************************************************************
* Function Name  : CAN_LeaveInitMode                                         
* Description    : Leaves the initialization mode (switch into normal mode).
*                  This function must be used in conjunction with CAN_EnterInitMode().  
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_LeaveInitMode(void)
{
  CAN->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
}

/*******************************************************************************
* Function Name  : CAN_EnterTestMode                                         
* Description    : Switchs the CAN into test mode. This function must be used in
*                  conjunction with CAN_LeaveTestMode().                            
* Input          : TestMask: specifies the configuration in test modes.     
* Output         : None                                                      
* Return         : None                                                            
*******************************************************************************/
void CAN_EnterTestMode(u8 TestMask)
{
  CAN->CR |= CAN_CR_TEST;
  CAN->TESTR |= TestMask;
}

/*******************************************************************************
* Function Name  : CAN_LeaveTestMode                                         
* Description    : Leaves the current test mode (switch into normal mode).
*                  This function must be used in conjunction with CAN_EnterTestMode().    
* Input          : None                                                      
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_LeaveTestMode(void)
{
  CAN->CR |= CAN_CR_TEST;
  CAN->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
  CAN->CR &= ~CAN_CR_TEST;
}

/*******************************************************************************
* Function Name  : CAN_ReleaseTxMessage                                      
* Description    : Releases the transmit message object.
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                     
* Output         : None                                                      
* Return         : None                                                                        
*******************************************************************************/
void CAN_ReleaseTxMessage(u32 msgobj)
{
  CAN->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
  CAN->sMsgObj[0].CRR = 1 + msgobj;
}

/*******************************************************************************
* Function Name  : CAN_ReleaseRxMessage                                      
* Description    : Releases the receive message object.                        
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : None                                                                      
*******************************************************************************/
void CAN_ReleaseRxMessage(u32 msgobj)
{
  CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
  CAN->sMsgObj[1].CRR = 1 + msgobj;
}

/*******************************************************************************
* Function Name  : CAN_IsMessageWaiting                                      
* Description    : Tests the waiting status of a received message.             
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                       
* Output         : None                                                      
* Return         : A non-zero value if the corresponding message object has  
*                  received a message waiting to be copied, else 0.           
*******************************************************************************/
u32 CAN_IsMessageWaiting(u32 msgobj)
{
  return (msgobj < 16 ? CAN->ND1R & (1 << msgobj) : CAN->ND2R & (1 << (msgobj-16)));
}

/*******************************************************************************
* Function Name  : CAN_IsTransmitRequested                                   
* Description    : Tests the request status of a transmitted message.          
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : A non-zero value if the corresponding message is requested
*                  to transmit, else 0.                                       
*******************************************************************************/
u32 CAN_IsTransmitRequested(u32 msgobj)
{
  return (msgobj < 16 ? CAN->TXR1R & (1 << msgobj) : CAN->TXR2R & (1 << (msgobj-16)));
}

/*******************************************************************************
* Function Name  : CAN_IsInterruptPending                                    
* Description    : Tests the interrupt status of a message object.             
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : A non-zero value if the corresponding message has an      
*                  interrupt pending, else 0.                                 
*******************************************************************************/
u32 CAN_IsInterruptPending(u32 msgobj)
{
  return (msgobj < 16 ? CAN->IP1R & (1 << msgobj) : CAN->IP2R & (1 << (msgobj-16)));
}

/*******************************************************************************
* Function Name  : CAN_IsObjectValid                                         
* Description    : Tests the validity of a message object (ready to use).      
* Input          : - msgobj: specifies the Message object number, from 0 to 31.                      
* Output         : None                                                      
* Return         : A non-zero value if the corresponding message object is   
*                  valid, else 0.                                             
*******************************************************************************/
u32 CAN_IsObjectValid(u32 msgobj)
{
  return (msgobj < 16 ? CAN->MV1R & (1 << msgobj) : CAN->MV2R & (1 << (msgobj-16)));
}
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud