PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
can.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "can.h"
56 
57 #if (defined(PDL_PERIPHERAL_CAN_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 
70 #define CAN_INSTANCE_COUNT (uint32_t)(sizeof(m_astcCanInstanceDataLut) / sizeof(m_astcCanInstanceDataLut[0]))
71 
73 #define CAN_STATUS_INTERRUPT (1 << 15)
74 
77 #define CAN_NO_LEC_CHANGE 7
78 
79 #define CAN_MSG_DIR_RX 0
80 #define CAN_MSG_DIR_TX 1
81 #define CAN_MSG_ID_EXTENDED 1
82 
83 /******************************************************************************/
84 /* Global variable definitions (declared in header file with 'extern') */
85 /******************************************************************************/
86 
89 {
90  #if (PDL_PERIPHERAL_ENABLE_CAN0 == PDL_ON)
91  {
92  &CAN0, // pstcInstance
93  NULL // stcInternData (not initialized yet)
94  },
95  #endif
96  #if (PDL_PERIPHERAL_ENABLE_CAN1 == PDL_ON)
97  {
98  &CAN1, // pstcInstance
99  NULL // stcInternData (not initialized yet)
100  },
101  #endif
102 };
103 
104 /******************************************************************************/
105 /* Local type definitions ('typedef') */
106 /******************************************************************************/
107 
108 /******************************************************************************/
109 /* Local function prototypes ('static') */
110 /******************************************************************************/
111 
112 /******************************************************************************/
113 /* Local variable definitions ('static') */
114 /******************************************************************************/
115 
116 /******************************************************************************/
117 /* Function implementation - global ('extern') and local ('static') */
118 /******************************************************************************/
119 
120 /******************************************************************************/
121 /* Local Functions */
122 /******************************************************************************/
123 
134 {
135  uint32_t u32Instance;
136 
137  for (u32Instance = 0; u32Instance < CAN_INSTANCE_COUNT; u32Instance++)
138  {
139  if (pstcCan == m_astcCanInstanceDataLut[u32Instance].pstcInstance)
140  {
141  return &m_astcCanInstanceDataLut[u32Instance].stcInternData;
142  }
143  }
144 
145  return NULL;
146 }
147 
155 static void Can_InitInterrupt(volatile stc_cann_t* pstcCan)
156 {
157  if (pstcCan == (stc_cann_t*)(&CAN0))
158  {
159  NVIC_ClearPendingIRQ(CAN0_IRQn);
160  NVIC_EnableIRQ(CAN0_IRQn);
161  NVIC_SetPriority(CAN0_IRQn, PDL_IRQ_LEVEL_CAN0);
162  }
163  else if (pstcCan == (stc_cann_t*)(&CAN1))
164  {
165  NVIC_ClearPendingIRQ(CAN1_IRQn);
166  NVIC_EnableIRQ(CAN1_IRQn);
167  NVIC_SetPriority(CAN1_IRQn, PDL_IRQ_LEVEL_CAN1);
168  }
169 
170  // Do nothing, if wrong instance. (Will never happen, because called
171  // only from Can_Init with previous CAN instance check before!)
172 }
173 
181 static void Can_DeInitInterrupt(volatile stc_cann_t* pstcCan)
182 {
183  if (pstcCan == (stc_cann_t*)(&CAN0))
184  {
185  NVIC_ClearPendingIRQ(CAN0_IRQn);
186  NVIC_DisableIRQ(CAN0_IRQn);
187  NVIC_SetPriority(CAN0_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
188  }
189  else if (pstcCan == (stc_cann_t*)(&CAN1))
190  {
191  NVIC_ClearPendingIRQ(CAN1_IRQn);
192  NVIC_DisableIRQ(CAN1_IRQn);
193  NVIC_SetPriority(CAN1_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
194  }
195 
196  // Do nothing, if wrong instance. (Will never happen, because called
197  // only from Can_DeInit with previous CAN instance check before!)
198 }
199 
200 
201 /******************************************************************************/
202 /* Global Functions */
203 /******************************************************************************/
204 
224 void CanIrqHandler( volatile stc_cann_t* pstcCan,
225  stc_can_intern_data_t* pstcCanInternData
226  )
227 {
228  uint16_t u16MessageBufferNumber ;
229  boolean_t bDone ;
230  stc_can_statr_t unCanStatusRegister ;
231  stc_can_ifncmsk_t unCanIf2Cmsk;
232 
233  bDone = FALSE ;
234 
235  while( bDone == FALSE )
236  {
237  u16MessageBufferNumber = pstcCan->INTR ;
238 
239  switch ( u16MessageBufferNumber )
240  {
241  // nothing more to do
242  case 0:
243  bDone = TRUE ;
244  break ;
245 
246  // Check for status interrupt
248  // Get status register, clear of status Irq is done automatically by readout
249  unCanStatusRegister = pstcCan->STATR_f;
250 
251  // Check for error if the error callback function is defined.
252  if (pstcCanInternData->stcCanNotificationCb.pfnCanErrorInterruptFunction != NULL)
253  {
254  // Check if bus off error was set (transmit error counter > 255)
255  if (unCanStatusRegister.BOFF != 0)
256  {
257  // Bus off
259  }
260 
261  // Check if error warning level is reached (receive and/or transmit error counter >= 96).
262  if (unCanStatusRegister.EWARN != 0)
263  {
264  // Warning level reached.
266  }
267  }
268 
269  // Check for status if the status change callback function is defined.
270  if (pstcCanInternData->stcCanNotificationCb.pfnCanStatusInterruptFunction != NULL)
271  {
272  // LEC different to CAN_NO_LEC_CHANGE, report last error.
273  if (unCanStatusRegister.LEC != CAN_NO_LEC_CHANGE)
274  {
275  pstcCanInternData->stcCanNotificationCb.pfnCanStatusInterruptFunction((en_can_status_t)(unCanStatusRegister.LEC));
276 
277  // Now reset LEC to special value CAN_NO_LEC_CHANGE
278  pstcCan->STATR_f.LEC = CAN_NO_LEC_CHANGE;
279  }
280  }
281  break ;
282 
283  // Default path is message buffer handling
284  default:
285  // Write command mask register to perform a selective read based on the bits set
286  unCanIf2Cmsk.WRRD = CAN_MSG_DIR_RX; // Set direction to rx
287  unCanIf2Cmsk.MASK = 1; // Transfer Identifier Mask + MDir + MXtd to Message Object
288  unCanIf2Cmsk.ARB = 1; // Transfer Identifier + Dir + Xtd + MsgVal to Message Object
289  unCanIf2Cmsk.CONTROL = 1; // Transfer Control Bits to Message Object
290  unCanIf2Cmsk.CIP = 1; // Clear Interrupt Pending Bit
291  unCanIf2Cmsk.NEWDAT = 1; // Set TXRQST/NEWDAT bit
292  unCanIf2Cmsk.DATAA = 1; // Transfer Data Bytes 0-3 to Mesage Object
293  unCanIf2Cmsk.DATAB = 1; // Transfer Data Bytes 4-7 to Mesage Object
294  pstcCan->IF2CMSK_f = unCanIf2Cmsk;
295 
296  // Write message buffer number to request reading
297  // the message buffer into the interface registers
298  // We can write full 16bit here because BUSY bit is not used in this mode
299  pstcCan->IF2CREQ = u16MessageBufferNumber ;
300 
301  // Check if message object is configured for reception or transmission
302  if (pstcCan->u32IF2ARB.DIR == CAN_MSG_DIR_RX)
303  {
304  // CAN message received, check if there is a callback function
305  // for this message buffer registered
306  if (pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pstcMsgBuffer != NULL)
307  {
308  stc_can_msg_t* pstcMsgBuffer = pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pstcMsgBuffer;
309  uint16_t* pu16Data = (uint16_t*)&pstcMsgBuffer->stcData.au8Data[0] ;
310 
311  // Check if 11- or 29-bit identifier was used for message.
312  if ( pstcCan->IF2ARB2_f.XTD != CAN_MSG_ID_EXTENDED )
313  {
314  // 11-bit identifier was used.
315  pstcMsgBuffer->stcIdentifier.bExtended = FALSE;
316  pstcMsgBuffer->stcIdentifier.u32Identifier = (pstcCan->u32IF2ARB.ID >> 18);
317  }
318  else
319  {
320  // 29-bit identifier was used.
321  pstcMsgBuffer->stcIdentifier.bExtended = TRUE;
322  pstcMsgBuffer->stcIdentifier.u32Identifier = pstcCan->u32IF2ARB.ID;
323  }
324 
325  // Get data bytes
326  pu16Data[0] = pstcCan->IF2DTA1_L;
327  pu16Data[1] = pstcCan->IF2DTA2_L;
328  pu16Data[2] = pstcCan->IF2DTB1_L;
329  pu16Data[3] = pstcCan->IF2DTB2_L;
330 
331  // Set data length of received data bytes.
332  pstcMsgBuffer->stcData.u8DataLengthCode = pstcCan->IF2MCTR_f.DLC;
333 
334  // Check if message lost flag is set in receive message buffer.
335  if ( pstcCan->IF2MCTR_f.MSGLST == 1 )
336  {
337  pstcMsgBuffer->bOverflow = TRUE ;
338 
339  // Reset message lost flag.
340  pstcCan->IF2MCTR_f.MSGLST = 0 ;
341 
342  // Write command mask register to perform a write to CAN control register.
343  // Pls. note that INTPND and NEWDAT are left unchanged if written with '0'
344  unCanIf2Cmsk = pstcCan->IF2CMSK_f;
345  unCanIf2Cmsk.WRRD = CAN_MSG_DIR_RX; // Set direction to tx
346  unCanIf2Cmsk.CONTROL = 1; // Transfer control bits
347  pstcCan->IF2CMSK_f = unCanIf2Cmsk;
348 
349  // Set message buffer number to update message buffer.
350  // We can write full 16bit here because BUSY bit is not used in this mode
351  pstcCan->IF2CREQ = u16MessageBufferNumber;
352  }
353 
354  // Check if data new flag is already set.
355  if ( pstcMsgBuffer->bNew == TRUE )
356  {
357  // New data flag was already set, that means that
358  // at least one message is lost, so overflow flag
359  // must be set.
360  pstcMsgBuffer->bOverflow = TRUE ;
361  }
362  else
363  {
364  // Set new data flag.
365  pstcMsgBuffer->bNew = TRUE ;
366  }
367 
368  // Call callback function if it was set previously.
369  if (pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pfnCanRxInterruptFunction != NULL)
370  {
371  pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pfnCanRxInterruptFunction((uint8_t)u16MessageBufferNumber, pstcMsgBuffer);
372  }
373  }
374  }
375  else // if (pstcCan->u32IF2ARB.DIR == CAN_MSG_DIR_RX)
376  {
377  // CAN message succesfully transmitted.
378  // Call callback function if it was set previously.
379  if (pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pfnCanTxInterruptFunction != NULL)
380  {
381  pstcCanInternData->stcCanInterruptHandling[u16MessageBufferNumber - 1].pfnCanTxInterruptFunction((uint8_t)u16MessageBufferNumber);
382  }
383  }
384  break ;
385  } // switch ( u16MessageBufferNumber )
386  } // while ( !bDone )
387 } // CanIrqHandler
388 
427 en_result_t Can_Init( volatile stc_cann_t* pstcCan, const stc_can_config_t* pstcConfig )
428 {
429  stc_can_intern_data_t* pstcCanInternData;
430  stc_can_btr_t unBTR;
431  stc_can_ifncmsk_t unIF1CMSK;
432  stc_can_ctrlr_t unCTRLR;
433  uint8_t u8MessageNumber;
434  uint8_t u8HclkDiv;
435  uint8_t u8HclkMul;
436 
437  PDL_ZERO_STRUCT(unBTR);
438  PDL_ZERO_STRUCT(unIF1CMSK);
439  PDL_ZERO_STRUCT(unCTRLR);
440 
441  // Check for NULL pointers
442  if ( pstcCan == NULL ||
443  pstcConfig == NULL
444  )
445  {
446  return ErrorInvalidParameter;
447  }
448 
449  // Check range of input parameters.....
454  )
455  {
456  return ErrorInvalidParameter;
457  }
458 
459  // Get pointer to internal data structure
460  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
461 
462  // Check for NULL
463  if (pstcCanInternData == NULL)
464  {
465  return ErrorInvalidParameter;
466  }
467 
468  if (TRUE == (pstcConfig->bTouchPrescaler))
469  {
470  // CAN dedicated clock prescaler
471  switch(pstcConfig->stcBitrate.enCanPrescaler)
472  {
473  case CanPreDiv11:
474  CANPRE = 0x00;
475  u8HclkDiv = 1;
476  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
477  {
478  return ErrorInvalidParameter;
479  }
480  break;
481  case CanPreDiv12:
482  CANPRE = 0x01;
483  u8HclkDiv = 2;
484  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
485  {
486  return ErrorInvalidParameter;
487  }
488  break;
489  case CanPreDiv14:
490  CANPRE = 0x02;
491  u8HclkDiv = 4;
492  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
493  {
494  return ErrorInvalidParameter;
495  }
496  break;
497  case CanPreDiv18:
498  CANPRE = 0x04;
499  u8HclkDiv = 8;
500  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
501  {
502  return ErrorInvalidParameter;
503  }
504  break;
505  case CanPreDiv23:
506  CANPRE = 0x08;
507  u8HclkMul = 2;
508  u8HclkDiv = 3;
509  if (((__HCLK * u8HclkMul) / u8HclkDiv) > CAN_MAX_CLK)
510  {
511  return ErrorInvalidParameter;
512  }
513  break;
514  case CanPreDiv13:
515  CANPRE = 0x09;
516  u8HclkDiv = 3;
517  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
518  {
519  return ErrorInvalidParameter;
520  }
521  break;
522  case CanPreDiv16:
523  CANPRE = 0x0A;
524  u8HclkDiv = 6;
525  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
526  {
527  return ErrorInvalidParameter;
528  }
529  break;
530  case CanPreDiv112:
531  CANPRE = 0x0B;
532  u8HclkDiv = 12;
533  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
534  {
535  return ErrorInvalidParameter;
536  }
537  break;
538  case CanPreDiv15:
539  CANPRE = 0x0C;
540  u8HclkDiv = 5;
541  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
542  {
543  return ErrorInvalidParameter;
544  }
545  break;
546  case CanPreDiv110:
547  CANPRE = 0x0E;
548  u8HclkDiv = 10;
549  if ((__HCLK / u8HclkDiv) > CAN_MAX_CLK)
550  {
551  return ErrorInvalidParameter;
552  }
553  break;
554  default:
555  return ErrorInvalidParameter;
556  }
557  }
558 
559  // Enable access to init and bit timing register.
560  pstcCan->CTRLR_f.INIT = 1;
561  pstcCan->CTRLR_f.CCE = 1;
562 
563  unBTR.BRP = pstcConfig->stcBitrate.u16Prescaler - 1 ;
564  unBTR.SJW = pstcConfig->stcBitrate.u8SyncJumpWidth - 1 ;
565  unBTR.TSEG1 = pstcConfig->stcBitrate.u8TimeSegment1 - 1 ;
566  unBTR.TSEG2 = pstcConfig->stcBitrate.u8TimeSegment2 - 1 ;
567  pstcCan->BTR_f = unBTR ; // Write to Hardware
568 
569  pstcCan->BRPER = ((pstcConfig->stcBitrate.u16Prescaler - 1) >> 6);
570 
571  // Now disable access to bit timing register
572  pstcCan->CTRLR_f.CCE = 0;
573 
574  // Reset status register.
575  pstcCan->STATR = 0x0000;
576 
577  // Reset message valid bit of each message to invalid.
578  pstcCan->IF1ARB = 0 ;
579 
580  // Reset message control register.
581  pstcCan->IF1MCTR = 0x0000;
582 
583  // Write command mask register to for update arbitration and control part of message object.
584  unIF1CMSK.WRRD = CAN_MSG_DIR_TX;
585  unIF1CMSK.ARB = 1;
586  unIF1CMSK.CONTROL = 1;
587  pstcCan->IF1CMSK_f = unIF1CMSK;
588 
589  // Set each message buffer number to update message buffer with reset message valid bit.
590  for (u8MessageNumber = 1; u8MessageNumber <= CAN_MESSAGE_BUFFER_COUNT; u8MessageNumber++)
591  {
592  pstcCan->IF1CREQ = u8MessageNumber ;
593  }
594 
595  // Set notification callback functions
597  pstcCanInternData->stcCanNotificationCb.pfnCanErrorInterruptFunction = pstcConfig->pfnErrorCallback;
598 
599  // Enable interrupts
600  unCTRLR.EIE = 1; // Error interrupt enable
601  unCTRLR.SIE = 1; // Status change interrupt enable
602  unCTRLR.IE = 1; // Module interrupt enable
603 
604  // Set automatic retransmission field.
605  if (pstcConfig->bDisableAutomaticRetransmission == TRUE)
606  {
607  unCTRLR.DAR = 1; // Disable automatic retransmission
608  }
609 
610  pstcCan->CTRLR_f = unCTRLR;
611 
612  Can_InitInterrupt(pstcCan);
613 
614  // At last leave init phase ...
615  pstcCan->CTRLR_f.INIT = 0 ;
616 
617  return Ok;
618 } // Can_Init
619 
634 en_result_t Can_DeInit( volatile stc_cann_t* pstcCan )
635 {
636  stc_can_intern_data_t* pstcCanInternData; // Pointer to internal data
637  uint8_t u8MessageNumber;
638 
639  // Check for NULL pointer
640  if ( pstcCan == NULL )
641  {
642  return ErrorInvalidParameter;
643  }
644 
645  // Get pointer to internal data structure
646  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
647 
648  // Check for NULL
649  if (pstcCanInternData == NULL)
650  {
651  return ErrorInvalidParameter;
652  }
653 
654  Can_DeInitInterrupt(pstcCan);
655 
656  // Reset CAN control register.
657  pstcCan->CTRLR = 0x0000;
658 
659  pstcCan->CTRLR_f.INIT = 1; // Enter INIT phase
660  pstcCan->CTRLR_f.CCE = 1; // Enable access to bit timing register.
661 
662  // Reset CAN bit timing register (0x2301).
663  pstcCan->BTR_f.BRP = 1;
664  pstcCan->BTR_f.SJW = 0;
665  pstcCan->BTR_f.TSEG1 = 3;
666  pstcCan->BTR_f.TSEG2 = 2;
667 
668  // Reset CAN baud rate prescaler extension register.
669  pstcCan->BRPER = 0x00;
670 
671  // Disable access to bit timing register.
672  pstcCan->CTRLR_f.CCE = 0;
673 
674  // Reset CAN status register.
675  pstcCan->STATR = 0x00;
676 
677  // Reset CAN test register.
678  pstcCan->TESTR = 0x00;
679 
680  // Reset CAN IFx command mask register.
681  pstcCan->IF1CMSK = 0x00;
682  pstcCan->IF2CMSK = 0x00;
683 
684  // Reset CAN IFx mask register.
685  pstcCan->IF1MSK = UINT32_MAX ;
686  pstcCan->IF2MSK = UINT32_MAX ;
687 
688  // Reset CAN IFx arbitration register.
689  pstcCan->IF1ARB = 0x00;
690  pstcCan->IF2ARB = 0x00;
691 
692  // Reset CAN IFx message control register.
693  pstcCan->IF1MCTR = 0x00;
694  pstcCan->IF2MCTR = 0x00;
695 
696  // Reset CAN IFx data A and Data B register.
697  pstcCan->IF1DTA1_L = 0x00;
698  pstcCan->IF1DTA2_L = 0x00;
699  pstcCan->IF1DTB1_L = 0x00;
700  pstcCan->IF1DTB2_L = 0x00;
701  pstcCan->IF2DTA1_L = 0x00;
702  pstcCan->IF2DTA2_L = 0x00;
703  pstcCan->IF2DTB1_L = 0x00;
704  pstcCan->IF2DTB2_L = 0x00;
705 
706  // Reset CAN prescaler
707  CANPRE = 0x00;
708 
709  // Set each message buffer number to update message buffer with reset message valid bit.
710  for (u8MessageNumber = 0; u8MessageNumber < CAN_MESSAGE_BUFFER_COUNT; u8MessageNumber++)
711  {
712  // Reset transmit callback function.
713  pstcCanInternData->stcCanInterruptHandling[u8MessageNumber].pstcMsgBuffer = NULL;
714  pstcCanInternData->stcCanInterruptHandling[u8MessageNumber].pfnCanTxInterruptFunction = NULL;
715  pstcCanInternData->stcCanInterruptHandling[u8MessageNumber].pfnCanRxInterruptFunction = NULL;
716  }
717 
718  // Reset notification callback functions
719  pstcCanInternData->stcCanNotificationCb.pfnCanStatusInterruptFunction = NULL;
720  pstcCanInternData->stcCanNotificationCb.pfnCanErrorInterruptFunction = NULL;
721 
722  return Ok;
723 } // Can_DeInit
724 
738 static en_result_t CanIsMbUpdatePossible( volatile stc_cann_t* pstcCan,
739  uint8_t u8MsgBuf
740  )
741 {
742  uint16_t u16CmpMask ;
743 
744  // No parameter checking is necessary here because this has been done
745  // by the caller already.
746 
747  // Check if a transmission is pending
748  // Get bit mask
749  u16CmpMask = 1 ;
750  u16CmpMask <<= (u8MsgBuf % 16) ;
751 
752 
753  // If Remote-Frames are active ...
754  if( pstcCan->IF1MCTR_f.RMTEN == 1 )
755  {
756  return Ok ; // Update of Message Buffer is safe now
757  }
758 
759  switch ( u8MsgBuf >> 3 ) // Which register to use (each reg holds 16 flags)
760  {
761  case 0:
762  if ( pstcCan->TREQR1 & u16CmpMask )
763  {
764  return ErrorOperationInProgress ;
765  }
766  break ;
767 
768  case 1:
769  if ( pstcCan->TREQR2 & u16CmpMask )
770  {
771  return ErrorOperationInProgress ;
772  }
773  break ;
774 
775  default: // hope this will never see the daylight
776  return ErrorInvalidParameter ;
777  }
778 
779  return Ok ; // Update of Message Buffer is safe now
780 } // CanIsMbUpdatePossible
781 
817  uint8_t u8MsgBuf,
818  const stc_can_msg_id_t* pstcMsgId,
819  can_tx_msg_func_ptr_t pfnCallback
820  )
821 {
822  stc_can_intern_data_t* pstcCanInternData ; // Pointer to internal data
823 
824  // Check for NULL pointer
825  if ( pstcCan == NULL ||
826  pstcMsgId == NULL
827  )
828  {
829  return ErrorInvalidParameter;
830  }
831 
832  // Check for message buffer index in range
833  if ( u8MsgBuf >= CAN_MESSAGE_BUFFER_COUNT )
834  {
835  return ErrorInvalidParameter;
836  }
837 
838  // Get pointer to internal data structure
839  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
840 
841  // Check for NULL
842  if (pstcCanInternData == NULL)
843  {
844  return ErrorInvalidParameter;
845  }
846 
847  if ( CanIsMbUpdatePossible( pstcCan, u8MsgBuf ) != Ok )
848  {
849  return ErrorOperationInProgress ;
850  }
851 
852  // Set message valid bit to 0, afterwards message objects can be updated...
853  pstcCan->u32IF1ARB.MSGVAL = 0 ;
854 
855  // Write command mask register for updating arbitration register.
856  pstcCan->IF1CMSK_f.WRRD = CAN_MSG_DIR_TX;
857  pstcCan->IF1CMSK_f.ARB = 1; // Write Arbitraion
858 
859  // Set message buffer number to update message buffer.
860  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
861 
862  // Set transmit callback function.
863  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pfnCanTxInterruptFunction = pfnCallback;
864 
865  // Write configuration to IFx arbitration1/2
866  if (pstcMsgId->bExtended != CAN_MSG_ID_EXTENDED)
867  {
868  // Just 11 bit identifier.
869  pstcCan->u32IF1ARB.ID = pstcMsgId->u32Identifier << (2 + 16) ;
870  pstcCan->u32IF1ARB.XTD = 0 ;
871  }
872  else
873  {
874  // Write 29 bit identifier.
875  pstcCan->u32IF1ARB.ID = pstcMsgId->u32Identifier ;
876  pstcCan->u32IF1ARB.XTD = 1 ;
877  }
878 
879  // We can NOT use a combined register write here because this would destroy
880  // the u29ID bit field!
881  // Set message direction to transmit.
882  pstcCan->u32IF1ARB.DIR = CAN_MSG_DIR_TX ;
883  // Set message buffer valid.
884  pstcCan->u32IF1ARB.MSGVAL = 1 ;
885 
886 
887  // ---------
888 
889  // Set message buffer number to update message buffer.
890  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
891 
892  return Ok;
893 } // Can_SetTransmitMsgBuffer
894 
920  uint8_t u8MsgBuf,
921  const stc_can_msg_data_t* pstcMsgData,
922  en_can_tx_mode_t enTxMode
923  )
924 {
925  stc_can_intern_data_t* pstcCanInternData; // Pointer to internal data
926  uint16_t u16Data;
927  uint8_t u8DataLengthCode;
928 
929  // Check for NULL pointers
930  if ( pstcCan == NULL ||
931  pstcMsgData == NULL
932  )
933  {
934  return ErrorInvalidParameter;
935  }
936 
937  // Check for message buffer index in range
938  if ( u8MsgBuf >= CAN_MESSAGE_BUFFER_COUNT )
939  {
940  return ErrorInvalidParameter;
941  }
942 
943  // Get pointer to internal data structure
944  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
945 
946  // Check for NULL
947  if ( pstcCanInternData == NULL )
948  {
949  return ErrorInvalidParameter;
950  }
951 
952  if ( CanIsMbUpdatePossible( pstcCan, u8MsgBuf ) != Ok )
953  {
954  return ErrorOperationInProgress ;
955  }
956 
957  // Write command mask register to read arbitration
958  pstcCan->IF1CMSK_f.WRRD = CAN_MSG_DIR_RX;
959  pstcCan->IF1CMSK_f.ARB = 1; // Write Arbitration
960 
961  // Set message buffer number to update message buffer.
962  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
963 
964  // Check if setup was done previously.
965  if ( pstcCan->u32IF1ARB.MSGVAL == 0 )
966  {
967  return ErrorUninitialized;
968  }
969 
970  u8DataLengthCode = (pstcMsgData->u8DataLengthCode > 8)
971  ? 8
972  : pstcMsgData->u8DataLengthCode ;
973 
974 
975  // Set message valid bit to 0, afterwards message objects can be updated...
976  pstcCan->u32IF1ARB.MSGVAL = 0 ;
977 
978  // Write command mask register to update arbitration register.
979  pstcCan->IF1CMSK_f.WRRD = CAN_MSG_DIR_TX;
980  pstcCan->IF1CMSK_f.ARB = 1; // Write Arbitration
981 
982  // Set message buffer number to update message buffer.
983  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
984 
985  // Set message control register.
986  pstcCan->IF1MCTR_f.NEWDAT = 1; // New data has been written
987 
988  if (TRUE == pstcCanInternData->bEobZero)
989  {
990  pstcCan->IF1MCTR_f.EOB = 0; // Use as FIFO message buffer
991  }
992  else
993  {
994  pstcCan->IF1MCTR_f.EOB = 1; // Use as single message buffer
995  }
996 
997  pstcCan->IF1MCTR_f.TXIE = 1; // Transmit interrrupt enable
998 
999  pstcCan->IF1MCTR_f.DLC = u8DataLengthCode;
1000 
1001  if ( enTxMode == CanImmediateTransmit )
1002  {
1003  pstcCan->IF1MCTR_f.RMTEN = 0 ; // Remote frame ignored
1004  }
1005  else
1006  {
1007  pstcCan->IF1MCTR_f.RMTEN = 1 ; // TXRQST is set on recv of a remote frame
1008  }
1009 
1010  u16Data = 0;
1011 
1012  // Update of data bytes.
1013  switch (u8DataLengthCode)
1014  {
1015  case 8:
1016  u16Data = pstcMsgData->au8Data[7] << 8;
1017  // ... fall through to process remaining bytes
1018  case 7:
1019  u16Data |= pstcMsgData->au8Data[6] ;
1020  pstcCan->IF1DTB2_L = u16Data;
1021  // ... fall through to process remaining bytes
1022  case 6:
1023  u16Data = pstcMsgData->au8Data[5] << 8;
1024  // ... fall through to process remaining bytes
1025  case 5:
1026  u16Data |= pstcMsgData->au8Data[4];
1027  pstcCan->IF1DTB1_L = u16Data;
1028  // ... fall through to process remaining bytes
1029  case 4:
1030  u16Data = pstcMsgData->au8Data[3] << 8;
1031  // ... fall through to process remaining bytes
1032  case 3:
1033  u16Data |= pstcMsgData->au8Data[2];
1034  pstcCan->IF1DTA2_L = u16Data;
1035  // ... fall through to process remaining bytes
1036  case 2:
1037  u16Data = pstcMsgData->au8Data[1] << 8;
1038  // ... fall through to process remaining bytes
1039  case 1:
1040  u16Data |= pstcMsgData->au8Data[0];
1041  pstcCan->IF1DTA1_L = u16Data ;
1042  break ;
1043  default:
1044  break ;
1045  }
1046 
1047  // Set message valid bit to 1, afterwards message object is ready to transmit.
1048  pstcCan->u32IF1ARB.MSGVAL = 1 ;
1049 
1050  // Write command mask register to update arbitration, control,
1051  // transmit request and data register.
1052  pstcCan->IF1CMSK_f.WRRD = CAN_MSG_DIR_TX;
1053  pstcCan->IF1CMSK_f.ARB = 1; // Write Arbitration
1054  pstcCan->IF1CMSK_f.CONTROL = 1; // Write Control
1055  pstcCan->IF1CMSK_f.NEWDAT = 1; // Write TXREQ/NEWDAT
1056  pstcCan->IF1CMSK_f.DATAA = 1; // Write Data A
1057  pstcCan->IF1CMSK_f.DATAB = 1; // Write Data B
1058 
1059  // Set message buffer number to update message buffer
1060  // This will send the msg out ...
1061  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
1062 
1063  return Ok;
1064 } // Can_UpdateAndTransmitMsgBuffer
1065 
1089  uint8_t u8MsgBuf,
1090  const stc_can_msg_data_t* pstcMsgData,
1091  en_can_tx_mode_t enTxMode
1092  )
1093 {
1094  stc_can_intern_data_t* pstcCanInternData; // Pointer to internal data
1095  en_result_t enFuncResult;
1096 
1097  // Check for NULL pointers
1098  if ( pstcCan == NULL ||
1099  pstcMsgData == NULL
1100  )
1101  {
1102  return ErrorInvalidParameter;
1103  }
1104 
1105  // Get pointer to internal data structure
1106  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
1107 
1108  // Check for NULL
1109  if ( pstcCanInternData == NULL )
1110  {
1111  return ErrorInvalidParameter;
1112  }
1113 
1114  // Preapare EOB = 0
1115  pstcCanInternData->bEobZero = TRUE;
1116 
1117  enFuncResult = Can_UpdateAndTransmitMsgBuffer(pstcCan, u8MsgBuf, pstcMsgData, enTxMode);
1118 
1119  // Reset EOB to '1'
1120  pstcCanInternData->bEobZero = FALSE;
1121 
1122  return enFuncResult;
1123 } // Can_UpdateAndTransmitFifoMsgBuffer
1124 
1172  uint8_t u8MsgBuf,
1173  stc_can_msg_t* pstcMsgBuffer,
1174  uint32_t u32MsgIdMask,
1175  can_rx_msg_func_ptr_t pfnCallback
1176  )
1177 {
1178  stc_can_intern_data_t* pstcCanInternData; // Pointer to internal data
1179  stc_can_ifncmsk_t unIF1CMSK;
1180  stc_can_ifnarb_t unIF1ARB;
1181 
1182  PDL_ZERO_STRUCT(unIF1CMSK);
1183  PDL_ZERO_STRUCT(unIF1ARB);
1184 
1185  // Check for NULL pointers
1186  if ( pstcCan == NULL ||
1187  pstcMsgBuffer == NULL
1188  )
1189  {
1190  return ErrorInvalidParameter;
1191  }
1192 
1193  // Check for message buffer index in range
1194  if ( u8MsgBuf >= CAN_MESSAGE_BUFFER_COUNT )
1195  {
1196  return ErrorInvalidParameter;
1197  }
1198 
1199  // Get pointer to internal data structure
1200  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
1201 
1202  // Check for NULL
1203  if ( pstcCanInternData == NULL )
1204  {
1205  return ErrorInvalidParameter;
1206  }
1207 
1208  // Disable message buffer at first (MSGVAL = 0) (otherwise a pending
1209  // reception on this MB could cause an interrupt, using inconsistent data)
1210  pstcCan->u32IF1ARB.MSGVAL = 0 ;
1211 
1212  // Set receive callback function and message buffer.
1213  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pfnCanRxInterruptFunction = pfnCallback;
1214  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pstcMsgBuffer = pstcMsgBuffer;
1215 
1216  // Configure mask register.
1217  pstcCan->IF1MSK = 0 ;
1218 
1219  // Set direction to read and message valid bit to 1.
1220  unIF1ARB.DIR = CAN_MSG_DIR_RX; // Set direction read
1221  unIF1ARB.MSGVAL = 1; // Set message valid bit to 1
1222 
1223  // Update arbitration register and mask identifier.
1224  if ( pstcMsgBuffer->stcIdentifier.bExtended != CAN_MSG_ID_EXTENDED )
1225  {
1226  // Just 11-bit identifier.
1227  unIF1ARB.ID = ((pstcMsgBuffer->stcIdentifier.u32Identifier) << 18) ;
1228  unIF1ARB.XTD = 0; // Clear extended bit
1229  pstcCan->IF1MSK_f.MSK = (u32MsgIdMask << 18) ;
1230  }
1231  else
1232  {
1233  // Write 29-bit identifier.
1234  unIF1ARB.ID = pstcMsgBuffer->stcIdentifier.u32Identifier ;
1235  unIF1ARB.XTD = 1 ; // Set extended bit
1236  pstcCan->IF1MSK_f.MSK = u32MsgIdMask ;
1237  }
1238 
1239  // Setup IF1ARB in hardware
1240  pstcCan->u32IF1ARB = unIF1ARB;
1241  pstcCan->IF1MSK_f.MXTD = 1;
1242 
1243  // Update message control register, enable receive interrupt and mask flag.
1244  pstcCan->IF1MCTR = 0x0000;
1245  pstcCan->IF1MCTR_f.RXIE = 1; // Receive interrupt enable
1246  pstcCan->IF1MCTR_f.UMASK = 1; // Use acceptance mask
1247 
1248  if (TRUE == pstcCanInternData->bEobZero)
1249  {
1250  pstcCan->IF1MCTR_f.EOB = 0; // Use as FIFO message buffer
1251  }
1252  else
1253  {
1254  pstcCan->IF1MCTR_f.EOB = 1; // Use as single message buffer
1255  }
1256 
1257  // Write command mask register
1258  unIF1CMSK.WRRD = CAN_MSG_DIR_TX; // Write to message RAM
1259  unIF1CMSK.MASK = 1; // Update mask
1260  unIF1CMSK.ARB = 1; // Update arbitration
1261  unIF1CMSK.CONTROL = 1; // Update control
1262  pstcCan->IF1CMSK_f = unIF1CMSK;
1263 
1264  // Set message buffer number to update message buffer.
1265  pstcCan->IF1CREQ = u8MsgBuf + 1 ;
1266 
1267  pstcMsgBuffer->bNew = FALSE ; // Preset to default
1268  pstcMsgBuffer->bOverflow = FALSE ; // Preset to default
1269 
1270  return Ok;
1271 } // Can_SetReceiveMsgBuffer
1272 
1292  uint8_t u8MsgBuf,
1293  stc_can_msg_t* pstcMsgBuffer,
1294  uint32_t u32MsgIdMask,
1295  can_rx_msg_func_ptr_t pfnCallback
1296  )
1297 {
1298  stc_can_intern_data_t* pstcCanInternData; // Pointer to internal data
1299 
1300  en_result_t enFuncResult;
1301 
1302  // Check for NULL pointers
1303  if ( pstcCan == NULL ||
1304  pstcMsgBuffer == NULL
1305  )
1306  {
1307  return ErrorInvalidParameter;
1308  }
1309 
1310  // Get pointer to internal data structure
1311  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
1312 
1313  // Check for NULL
1314  if ( pstcCanInternData == NULL )
1315  {
1316  return ErrorInvalidParameter;
1317  }
1318 
1319  // Preapare EOB = 0
1320  pstcCanInternData->bEobZero = TRUE;
1321 
1322  enFuncResult = Can_SetReceiveMsgBuffer(pstcCan, u8MsgBuf, pstcMsgBuffer, u32MsgIdMask, pfnCallback);
1323 
1324  // Reset EOB to '1'
1325  pstcCanInternData->bEobZero = FALSE;
1326 
1327  return enFuncResult;
1328 } // Can_SetReceiveFifoMsgBuffer
1329 
1348 en_result_t Can_ResetMsgBuffer( volatile stc_cann_t* pstcCan, uint8_t u8MsgBuf )
1349 {
1350  stc_can_intern_data_t* pstcCanInternData ; // Pointer to internal data
1351 
1352  // Check for NULL pointer
1353  if ( pstcCan == NULL )
1354  {
1355  return ErrorInvalidParameter ;
1356  }
1357 
1358  // Check for message buffer number in range
1359  if ( u8MsgBuf >= CAN_MESSAGE_BUFFER_COUNT )
1360  {
1361  return ErrorInvalidParameter ;
1362  }
1363 
1364  // Get pointer to internal data structure
1365  pstcCanInternData = CanGetInternDataPtr( pstcCan ) ;
1366 
1367  // Check for NULL
1368  if ( pstcCanInternData == NULL )
1369  {
1370  return ErrorInvalidParameter ;
1371  }
1372 
1373  // Reset message control register.
1374  pstcCan->IF1MCTR = 0 ;
1375 
1376  // Reset message valid bit
1377  pstcCan->u32IF1ARB.MSGVAL = 0 ;
1378 
1379  // Reset msg buffer and callback functions
1380  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pstcMsgBuffer = NULL ;
1381  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pfnCanTxInterruptFunction = NULL ;
1382  pstcCanInternData->stcCanInterruptHandling[u8MsgBuf].pfnCanRxInterruptFunction = NULL ;
1383 
1384  return Ok ;
1385 } // Can_ResetMsgBuffer
1386 
1388 
1389 #endif // #if (defined(PDL_PERIPHERAL_CAN_ACTIVE))
1390 
1391 /******************************************************************************/
1392 /* EOF (not truncated) */
1393 /******************************************************************************/