1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
37 * Functions in this file are the minimum required functions for the XCanPs
38 * driver. See xcanps.h for a detailed description of the driver.
44 * MODIFICATION HISTORY:
46 * Ver Who Date Changes
47 * ----- ----- -------- -----------------------------------------------
48 * 1.00a xd/sv 01/12/10 First release
49 * 1.01a bss 12/27/11 Added the APIs XCanPs_SetTxIntrWatermark and
50 * XCanPs_GetTxIntrWatermark.
51 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
54 ******************************************************************************/
56 /***************************** Include Files *********************************/
60 /************************** Constant Definitions *****************************/
62 /**************************** Type Definitions *******************************/
64 /***************** Macros (Inline Functions) Definitions *********************/
66 /************************** Variable Definitions *****************************/
68 /************************** Function Prototypes ******************************/
70 static void StubHandler(void);
72 /*****************************************************************************/
75 * This function initializes a XCanPs instance/driver.
77 * The initialization entails:
78 * - Initialize all members of the XCanPs structure.
79 * - Reset the CAN device. The CAN device will enter Configuration Mode
80 * immediately after the reset is finished.
82 * @param InstancePtr is a pointer to the XCanPs instance.
83 * @param ConfigPtr points to the XCanPs device configuration structure.
84 * @param EffectiveAddr is the device base address in the virtual memory
85 * address space. If the address translation is not used then the
86 * physical address is passed.
87 * Unexpected errors may occur if the address mapping is changed
88 * after this function is invoked.
90 * @return XST_SUCCESS always.
94 ******************************************************************************/
95 s32 XCanPs_CfgInitialize(XCanPs *InstancePtr, XCanPs_Config *ConfigPtr,
99 Xil_AssertNonvoid(InstancePtr != NULL);
100 Xil_AssertNonvoid(ConfigPtr != NULL);
103 * Set some default values for instance data, don't indicate the device
104 * is ready to use until everything has been initialized successfully.
106 InstancePtr->IsReady = 0U;
107 InstancePtr->CanConfig.BaseAddr = EffectiveAddr;
108 InstancePtr->CanConfig.DeviceId = ConfigPtr->DeviceId;
111 * Set all handlers to stub values, let user configure this data later.
113 InstancePtr->SendHandler = (XCanPs_SendRecvHandler) StubHandler;
114 InstancePtr->RecvHandler = (XCanPs_SendRecvHandler) StubHandler;
115 InstancePtr->ErrorHandler = (XCanPs_ErrorHandler) StubHandler;
116 InstancePtr->EventHandler = (XCanPs_EventHandler) StubHandler;
119 * Indicate the component is now ready to use.
121 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
124 * Reset the device to get it into its initial state.
126 XCanPs_Reset(InstancePtr);
128 Status = XST_SUCCESS;
132 /*****************************************************************************/
135 * This function resets the CAN device. Calling this function resets the device
136 * immediately, and any pending transmission or reception is terminated at once.
137 * Both Object Layer and Transfer Layer are reset. This function does not reset
138 * the Physical Layer. All registers are reset to the default values, and no
139 * previous status will be restored. TX FIFO, RX FIFO and TX High Priority
140 * Buffer are also reset.
142 * When a reset is required due to an internal error, the driver notifies the
143 * upper layer software of this need through the error status code or interrupts.
144 * The upper layer software is responsible for calling this Reset function and
145 * then re-configuring the device.
147 * The CAN device will be in Configuration Mode immediately after this function
150 * @param InstancePtr is a pointer to the XCanPs instance.
156 ******************************************************************************/
157 void XCanPs_Reset(XCanPs *InstancePtr)
159 Xil_AssertVoid(InstancePtr != NULL);
160 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
162 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, \
163 XCANPS_SRR_SRST_MASK);
166 /****************************************************************************/
169 * This routine returns the current operation mode of the CAN device.
171 * @param InstancePtr is a pointer to the XCanPs instance.
174 * - XCANPS_MODE_CONFIG if the device is in Configuration Mode.
175 * - XCANPS_MODE_SLEEP if the device is in Sleep Mode.
176 * - XCANPS_MODE_NORMAL if the device is in Normal Mode.
177 * - XCANPS_MODE_LOOPBACK if the device is in Loop Back Mode.
178 * - XCANPS_MODE_SNOOP if the device is in Snoop Mode.
182 *****************************************************************************/
183 u8 XCanPs_GetMode(XCanPs *InstancePtr)
187 Xil_AssertNonvoid(InstancePtr != NULL);
188 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
190 StatusReg = XCanPs_GetStatus(InstancePtr);
192 if ((StatusReg & XCANPS_SR_CONFIG_MASK) != (u32)0) {
193 return (u8)XCANPS_MODE_CONFIG;
196 else if ((StatusReg & XCANPS_SR_SLEEP_MASK) != (u32)0) {
197 return (u8)XCANPS_MODE_SLEEP;
200 else if ((StatusReg & XCANPS_SR_NORMAL_MASK) != (u32)0) {
201 if ((StatusReg & XCANPS_SR_SNOOP_MASK) != (u32)0) {
202 return (u8)XCANPS_MODE_SNOOP;
204 return (u8)XCANPS_MODE_NORMAL;
209 * If this line is reached, the device is in Loop Back Mode.
211 return (u8)XCANPS_MODE_LOOPBACK;
215 /*****************************************************************************/
218 * This function allows the CAN device to enter one of the following operation
220 * - Configuration Mode: Pass in parameter XCANPS_MODE_CONFIG
221 * - Sleep Mode: Pass in parameter XCANPS_MODE_SLEEP
222 * - Normal Mode: Pass in parameter XCANPS_MODE_NORMAL
223 * - Loop Back Mode: Pass in parameter XCANPS_MODE_LOOPBACK.
224 * - Snoop Mode: Pass in parameter XCANPS_MODE_SNOOP.
226 * Read the xcanps.h file and device specification for detailed description of
227 * each operation mode.
229 * @param InstancePtr is a pointer to the XCanPs instance.
230 * @param OperationMode specify which operation mode to enter. Valid value
231 * is any of XCANPS_MODE_* defined in xcanps.h. Multiple modes
232 * can not be entered at the same time.
238 * This function does NOT ensure CAN device enters the specified operation mode
239 * before it returns the control to the caller. The caller is responsible for
240 * checking current operation mode using XCanPs_GetMode().
242 ******************************************************************************/
243 void XCanPs_EnterMode(XCanPs *InstancePtr, u8 OperationMode)
247 Xil_AssertVoid(InstancePtr != NULL);
248 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
249 Xil_AssertVoid((OperationMode == (u8)XCANPS_MODE_CONFIG) ||
250 (OperationMode == (u8)XCANPS_MODE_SLEEP) ||
251 (OperationMode == (u8)XCANPS_MODE_NORMAL) ||
252 (OperationMode == (u8)XCANPS_MODE_LOOPBACK) ||
253 (OperationMode == (u8)XCANPS_MODE_SNOOP));
255 CurrentMode = XCanPs_GetMode(InstancePtr);
258 * If current mode is Normal Mode and the mode to enter is Sleep Mode,
259 * or if current mode is Sleep Mode and the mode to enter is Normal
260 * Mode, no transition through Configuration Mode is needed.
262 if ((CurrentMode == (u8)XCANPS_MODE_NORMAL) &&
263 (OperationMode == (u8)XCANPS_MODE_SLEEP)) {
265 * Normal Mode ---> Sleep Mode
267 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
268 XCANPS_MSR_OFFSET, XCANPS_MSR_SLEEP_MASK);
271 } else if ((CurrentMode == (u8)XCANPS_MODE_SLEEP) &&
272 (OperationMode == (u8)XCANPS_MODE_NORMAL)) {
274 * Sleep Mode ---> Normal Mode
276 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
277 XCANPS_MSR_OFFSET, 0U);
281 /*This else was made for misra-c compliance*/
286 * If the mode transition is not any of the two cases above, CAN must
287 * enter Configuration Mode before switching into the target operation
290 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
291 XCANPS_SRR_OFFSET, 0U);
294 * Check if the device has entered Configuration Mode, if not, return to
297 if (XCanPs_GetMode(InstancePtr) != (u8)XCANPS_MODE_CONFIG) {
301 switch (OperationMode) {
302 case XCANPS_MODE_CONFIG:
304 * As CAN is in Configuration Mode already.
305 * Nothing is needed to be done here.
309 case XCANPS_MODE_SLEEP:
310 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
311 XCANPS_MSR_OFFSET, XCANPS_MSR_SLEEP_MASK);
312 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
313 XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK);
316 case XCANPS_MODE_NORMAL:
317 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
318 XCANPS_MSR_OFFSET, 0U);
319 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
320 XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK);
323 case XCANPS_MODE_LOOPBACK:
324 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
325 XCANPS_MSR_OFFSET, XCANPS_MSR_LBACK_MASK);
326 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
327 XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK);
330 case XCANPS_MODE_SNOOP:
331 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
332 XCANPS_MSR_OFFSET, XCANPS_MSR_SNOOP_MASK);
333 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
334 XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK);
338 /*This default was made for misra-c compliance*/
344 /*****************************************************************************/
347 * This function returns Status value from Status Register (SR). Use the
348 * XCANPS_SR_* constants defined in xcanps_hw.h to interpret the returned
351 * @param InstancePtr is a pointer to the XCanPs instance.
353 * @return The 32-bit value read from Status Register.
357 ******************************************************************************/
358 u32 XCanPs_GetStatus(XCanPs *InstancePtr)
361 Xil_AssertNonvoid(InstancePtr != NULL);
362 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
364 return XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
368 /*****************************************************************************/
371 * This function reads Receive and Transmit error counters.
373 * @param InstancePtr is a pointer to the XCanPs instance.
374 * @param RxErrorCount is a pointer to data in which the Receive Error
375 * counter value is returned.
376 * @param TxErrorCount is a pointer to data in which the Transmit Error
377 * counter value is returned.
383 ******************************************************************************/
384 void XCanPs_GetBusErrorCounter(XCanPs *InstancePtr, u8 *RxErrorCount,
389 Xil_AssertVoid(InstancePtr != NULL);
390 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
391 Xil_AssertVoid(RxErrorCount != NULL);
392 Xil_AssertVoid(TxErrorCount != NULL);
394 * Read Error Counter Register and parse it.
396 ErrorCount = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
398 *RxErrorCount = (u8)((ErrorCount & XCANPS_ECR_REC_MASK) >>
399 XCANPS_ECR_REC_SHIFT);
400 *TxErrorCount = (u8)(ErrorCount & XCANPS_ECR_TEC_MASK);
403 /*****************************************************************************/
406 * This function reads Error Status value from Error Status Register (ESR). Use
407 * the XCANPS_ESR_* constants defined in xcanps_hw.h to interpret the
410 * @param InstancePtr is a pointer to the XCanPs instance.
412 * @return The 32-bit value read from Error Status Register.
416 ******************************************************************************/
417 u32 XCanPs_GetBusErrorStatus(XCanPs *InstancePtr)
420 Xil_AssertNonvoid(InstancePtr != NULL);
421 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
423 return XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
427 /*****************************************************************************/
430 * This function clears Error Status bit(s) previously set in Error
431 * Status Register (ESR). Use the XCANPS_ESR_* constants defined in xcanps_hw.h
432 * to create the value to pass in. If a bit was cleared in Error Status Register
433 * before this function is called, it will not be modified.
435 * @param InstancePtr is a pointer to the XCanPs instance.
437 * @param Mask is he 32-bit mask used to clear bits in Error Status
438 * Register. Multiple XCANPS_ESR_* values can be 'OR'ed to clear
443 ******************************************************************************/
444 void XCanPs_ClearBusErrorStatus(XCanPs *InstancePtr, u32 Mask)
446 Xil_AssertVoid(InstancePtr != NULL);
447 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
449 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
450 XCANPS_ESR_OFFSET, Mask);
453 /*****************************************************************************/
456 * This function sends a CAN Frame. If the TX FIFO is not full then the given
457 * frame is written into the the TX FIFO otherwise, it returns an error code
459 * This function does not wait for the given frame being sent to CAN bus.
461 * @param InstancePtr is a pointer to the XCanPs instance.
462 * @param FramePtr is a pointer to a 32-bit aligned buffer containing the
463 * CAN frame to be sent.
466 * - XST_SUCCESS if TX FIFO was not full and the given frame was
467 * written into the FIFO.
468 * - XST_FIFO_NO_ROOM if there is no room in the TX FIFO for the
473 ******************************************************************************/
474 s32 XCanPs_Send(XCanPs *InstancePtr, u32 *FramePtr)
477 Xil_AssertNonvoid(InstancePtr != NULL);
478 Xil_AssertNonvoid(FramePtr != NULL);
479 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
481 if (XCanPs_IsTxFifoFull(InstancePtr) == TRUE) {
482 Status = XST_FIFO_NO_ROOM;
486 * Write IDR, DLC, Data Word 1 and Data Word 2 to the CAN device.
488 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
489 XCANPS_TXFIFO_ID_OFFSET, FramePtr[0]);
490 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
491 XCANPS_TXFIFO_DLC_OFFSET, FramePtr[1]);
492 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
493 XCANPS_TXFIFO_DW1_OFFSET, FramePtr[2]);
494 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
495 XCANPS_TXFIFO_DW2_OFFSET, FramePtr[3]);
497 Status = XST_SUCCESS;
502 /*****************************************************************************/
505 * This function receives a CAN Frame. This function first checks if RX FIFO is
506 * empty, if not, it then reads a frame from the RX FIFO into the given buffer.
507 * This function returns error code immediately if there is no frame in the RX
510 * @param InstancePtr is a pointer to the XCanPs instance.
511 * @param FramePtr is a pointer to a 32-bit aligned buffer where the CAN
512 * frame to be written.
515 * - XST_SUCCESS if RX FIFO was not empty and a frame was read from
516 * RX FIFO successfully and written into the given buffer.
517 * - XST_NO_DATA if there is no frame to be received from the FIFO.
521 ******************************************************************************/
522 s32 XCanPs_Recv(XCanPs *InstancePtr, u32 *FramePtr)
525 Xil_AssertNonvoid(InstancePtr != NULL);
526 Xil_AssertNonvoid(FramePtr != NULL);
527 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
529 if (XCanPs_IsRxEmpty(InstancePtr) == TRUE) {
530 Status = XST_NO_DATA;
534 * Read IDR, DLC, Data Word 1 and Data Word 2 from the CAN device.
536 FramePtr[0] = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
537 XCANPS_RXFIFO_ID_OFFSET);
538 FramePtr[1] = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
539 XCANPS_RXFIFO_DLC_OFFSET);
540 FramePtr[2] = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
541 XCANPS_RXFIFO_DW1_OFFSET);
542 FramePtr[3] = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
543 XCANPS_RXFIFO_DW2_OFFSET);
546 * Clear RXNEMP bit in ISR. This allows future XCanPs_IsRxEmpty() call
547 * returns correct RX FIFO occupancy/empty condition.
549 XCanPs_IntrClear(InstancePtr, XCANPS_IXR_RXNEMP_MASK);
551 Status = XST_SUCCESS;
556 /*****************************************************************************/
559 * This routine sends a CAN High Priority frame. This function first checks if
560 * TX High Priority Buffer is empty. If yes, it then writes the given frame into
561 * the Buffer. If not, this function returns immediately. This function does not
562 * wait for the given frame being sent to CAN bus.
564 * @param InstancePtr is a pointer to the XCanPs instance.
565 * @param FramePtr is a pointer to a 32-bit aligned buffer containing the
566 * CAN High Priority frame to be sent.
569 * - XST_SUCCESS if TX High Priority Buffer was not full and the
570 * given frame was written into the buffer.
571 * - XST_FIFO_NO_ROOM if there is no room in the TX High Priority
572 * Buffer for this frame.
576 * If the frame needs to be sent immediately and not delayed by processor's
577 * interrupt handling, the caller should disable interrupt at processor
578 * level before invoking this function.
580 ******************************************************************************/
581 s32 XCanPs_SendHighPriority(XCanPs *InstancePtr, u32 *FramePtr)
584 Xil_AssertNonvoid(InstancePtr != NULL);
585 Xil_AssertNonvoid(FramePtr != NULL);
586 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
588 if (XCanPs_IsHighPriorityBufFull(InstancePtr) == TRUE) {
589 Status = XST_FIFO_NO_ROOM;
593 * Write IDR, DLC, Data Word 1 and Data Word 2 to the CAN device.
595 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
596 XCANPS_TXHPB_ID_OFFSET, FramePtr[0]);
597 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
598 XCANPS_TXHPB_DLC_OFFSET, FramePtr[1]);
599 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
600 XCANPS_TXHPB_DW1_OFFSET, FramePtr[2]);
601 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
602 XCANPS_TXHPB_DW2_OFFSET, FramePtr[3]);
604 Status = XST_SUCCESS;
609 /*****************************************************************************/
612 * This routine enables individual acceptance filters. Up to 4 filters could
615 * @param InstancePtr is a pointer to the XCanPs instance.
616 * @param FilterIndexes specifies which filter(s) to enable. Use
617 * any XCANPS_AFR_UAF*_MASK to enable one filter, and "Or"
618 * multiple XCANPS_AFR_UAF*_MASK values if multiple filters need
619 * to be enabled. Any filter not specified in this parameter will
620 * keep its previous enable/disable setting.
627 ******************************************************************************/
628 void XCanPs_AcceptFilterEnable(XCanPs *InstancePtr, u32 FilterIndexes)
632 Xil_AssertVoid(InstancePtr != NULL);
633 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
636 * Calculate the new value and write to AFR.
638 EnabledFilters = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
640 EnabledFilters |= FilterIndexes;
641 EnabledFilters &= (u32)XCANPS_AFR_UAF_ALL_MASK;
642 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_AFR_OFFSET,
646 /*****************************************************************************/
649 * This routine disables individual acceptance filters. Up to 4 filters could
650 * be disabled. If all acceptance filters are disabled then all the received
651 * frames are stored in the RX FIFO.
653 * @param InstancePtr is a pointer to the XCanPs instance.
654 * @param FilterIndexes specifies which filter(s) to disable. Use
655 * any XCANPS_AFR_UAF*_MASK to disable one filter, and "Or"
656 * multiple XCANPS_AFR_UAF*_MASK values if multiple filters need
657 * to be disabled. Any filter not specified in this parameter will
658 * keep its previous enable/disable setting. If all acceptance
659 * filters are disabled then all received frames are stored in the
666 ******************************************************************************/
667 void XCanPs_AcceptFilterDisable(XCanPs *InstancePtr, u32 FilterIndexes)
671 Xil_AssertVoid(InstancePtr != NULL);
672 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
675 * Calculate the new value and write to AFR.
677 EnabledFilters = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
679 EnabledFilters &= (u32)XCANPS_AFR_UAF_ALL_MASK & (~FilterIndexes);
680 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_AFR_OFFSET,
684 /*****************************************************************************/
687 * This function returns enabled acceptance filters. Use XCANPS_AFR_UAF*_MASK
688 * defined in xcanps_hw.h to interpret the returned value. If no acceptance
689 * filters are enabled then all received frames are stored in the RX FIFO.
691 * @param InstancePtr is a pointer to the XCanPs instance.
693 * @return The value stored in Acceptance Filter Register.
698 ******************************************************************************/
699 u32 XCanPs_AcceptFilterGetEnabled(XCanPs *InstancePtr)
702 Xil_AssertNonvoid(InstancePtr != NULL);
703 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
705 return XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
710 /*****************************************************************************/
713 * This function sets values to the Acceptance Filter Mask Register (AFMR) and
714 * Acceptance Filter ID Register (AFIR) for the specified Acceptance Filter.
715 * Use XCANPS_IDR_* defined in xcanps_hw.h to create the values to set the
716 * filter. Read the xcanps.h file and device specification for details.
718 * This function should be called only after:
719 * - The given filter is disabled by calling XCanPs_AcceptFilterDisable()
720 * - And the CAN device is ready to accept writes to AFMR and AFIR, i.e.,
721 * XCanPs_IsAcceptFilterBusy() returns FALSE.
723 * @param InstancePtr is a pointer to the XCanPs instance.
724 * @param FilterIndex defines which Acceptance Filter Mask and ID Register
725 * to set. Use any single XCANPS_AFR_UAF*_MASK value.
726 * @param MaskValue is the value to write to the chosen Acceptance Filter
728 * @param IdValue is the value to write to the chosen Acceptance Filter
732 * - XST_SUCCESS if the values were set successfully.
733 * - XST_FAILURE if the given filter was not disabled, or the CAN
734 * device was not ready to accept writes to AFMR and AFIR.
738 ******************************************************************************/
739 s32 XCanPs_AcceptFilterSet(XCanPs *InstancePtr, u32 FilterIndex,
740 u32 MaskValue, u32 IdValue)
745 Xil_AssertNonvoid(InstancePtr != NULL);
746 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
747 Xil_AssertNonvoid((FilterIndex == XCANPS_AFR_UAF4_MASK) ||
748 (FilterIndex == XCANPS_AFR_UAF3_MASK) ||
749 (FilterIndex == XCANPS_AFR_UAF2_MASK) ||
750 (FilterIndex == XCANPS_AFR_UAF1_MASK));
753 * Return an error if the given filter is currently enabled.
755 EnabledFilters = XCanPs_AcceptFilterGetEnabled(InstancePtr);
756 if ((EnabledFilters & FilterIndex) == FilterIndex) {
757 Status = XST_FAILURE;
761 * If the CAN device is not ready to accept writes to AFMR and AFIR,
764 if (XCanPs_IsAcceptFilterBusy(InstancePtr) == TRUE) {
765 Status = XST_FAILURE;
769 * Write to the AFMR and AFIR of the specified filter.
771 switch (FilterIndex) {
772 case XCANPS_AFR_UAF1_MASK: /* Acceptance Filter No. 1 */
774 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
775 XCANPS_AFMR1_OFFSET, MaskValue);
776 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
777 XCANPS_AFIR1_OFFSET, IdValue);
780 case XCANPS_AFR_UAF2_MASK: /* Acceptance Filter No. 2 */
781 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
782 XCANPS_AFMR2_OFFSET, MaskValue);
783 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
784 XCANPS_AFIR2_OFFSET, IdValue);
787 case XCANPS_AFR_UAF3_MASK: /* Acceptance Filter No. 3 */
788 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
789 XCANPS_AFMR3_OFFSET, MaskValue);
790 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
791 XCANPS_AFIR3_OFFSET, IdValue);
794 case XCANPS_AFR_UAF4_MASK: /* Acceptance Filter No. 4 */
795 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
796 XCANPS_AFMR4_OFFSET, MaskValue);
797 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
798 XCANPS_AFIR4_OFFSET, IdValue);
802 /*This default was made for misra-c compliance*/
806 Status = XST_SUCCESS;
812 /*****************************************************************************/
815 * This function reads the values of the Acceptance Filter Mask and ID Register
816 * for the specified Acceptance Filter. Use XCANPS_IDR_* defined in xcanps_hw.h
817 * to interpret the values. Read the xcanps.h file and device specification for
820 * @param InstancePtr is a pointer to the XCanPs instance.
821 * @param FilterIndex defines which Acceptance Filter Mask Register to get
822 * Mask and ID from. Use any single XCANPS_FILTER_* value.
823 * @param MaskValue is a pointer to the data in which the Mask value read
824 * from the chosen Acceptance Filter Mask Register is returned.
825 * @param IdValue is a pointer to the data in which the ID value read
826 * from the chosen Acceptance Filter ID Register is returned.
832 ******************************************************************************/
833 void XCanPs_AcceptFilterGet(XCanPs *InstancePtr, u32 FilterIndex,
834 u32 *MaskValue, u32 *IdValue)
836 Xil_AssertVoid(InstancePtr != NULL);
837 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
838 Xil_AssertVoid((FilterIndex == XCANPS_AFR_UAF4_MASK) ||
839 (FilterIndex == XCANPS_AFR_UAF3_MASK) ||
840 (FilterIndex == XCANPS_AFR_UAF2_MASK) ||
841 (FilterIndex == XCANPS_AFR_UAF1_MASK));
842 Xil_AssertVoid(MaskValue != NULL);
843 Xil_AssertVoid(IdValue != NULL);
846 * Read from the AFMR and AFIR of the specified filter.
848 switch (FilterIndex) {
849 case XCANPS_AFR_UAF1_MASK: /* Acceptance Filter No. 1 */
850 *MaskValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
851 XCANPS_AFMR1_OFFSET);
852 *IdValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
853 XCANPS_AFIR1_OFFSET);
856 case XCANPS_AFR_UAF2_MASK: /* Acceptance Filter No. 2 */
857 *MaskValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
858 XCANPS_AFMR2_OFFSET);
859 *IdValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
860 XCANPS_AFIR2_OFFSET);
863 case XCANPS_AFR_UAF3_MASK: /* Acceptance Filter No. 3 */
864 *MaskValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
865 XCANPS_AFMR3_OFFSET);
866 *IdValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
867 XCANPS_AFIR3_OFFSET);
870 case XCANPS_AFR_UAF4_MASK: /* Acceptance Filter No. 4 */
871 *MaskValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
872 XCANPS_AFMR4_OFFSET);
873 *IdValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
874 XCANPS_AFIR4_OFFSET);
878 /*This default was made for misra-c compliance*/
883 /*****************************************************************************/
886 * This routine sets Baud Rate Prescaler value. The system clock for the CAN
887 * controller is divided by (Prescaler + 1) to generate the quantum clock
888 * needed for sampling and synchronization. Read the device specification
891 * Baud Rate Prescaler can be set only if the CAN device is in Configuration
892 * Mode. Call XCanPs_EnterMode() to enter Configuration Mode before using this
895 * @param InstancePtr is a pointer to the XCanPs instance.
896 * @param Prescaler is the value to set. Valid values are from 0 to 255.
899 * - XST_SUCCESS if the Baud Rate Prescaler value is set
901 * - XST_FAILURE if CAN device is not in Configuration Mode.
905 ******************************************************************************/
906 s32 XCanPs_SetBaudRatePrescaler(XCanPs *InstancePtr, u8 Prescaler)
909 Xil_AssertNonvoid(InstancePtr != NULL);
910 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
912 if (XCanPs_GetMode(InstancePtr) != (u8)XCANPS_MODE_CONFIG) {
913 Status = XST_FAILURE;
916 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_BRPR_OFFSET,
919 Status = XST_SUCCESS;
924 /*****************************************************************************/
927 * This routine gets Baud Rate Prescaler value. The system clock for the CAN
928 * controller is divided by (Prescaler + 1) to generate the quantum clock
929 * needed for sampling and synchronization. Read the device specification for
932 * @param InstancePtr is a pointer to the XCanPs instance.
934 * @return Current used Baud Rate Prescaler value. The value's range is
939 ******************************************************************************/
940 u8 XCanPs_GetBaudRatePrescaler(XCanPs *InstancePtr)
943 Xil_AssertNonvoid(InstancePtr != NULL);
944 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
946 ReadValue = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
948 return ((u8)ReadValue);
951 /*****************************************************************************/
954 * This routine sets Bit time. Time segment 1, Time segment 2 and
955 * Synchronization Jump Width are set in this function. Device specification
956 * requires the values passed into this function be one less than the actual
957 * values of these fields. Read the device specification for details.
959 * Bit time can be set only if the CAN device is in Configuration Mode.
960 * Call XCanPs_EnterMode() to enter Configuration Mode before using this
963 * @param InstancePtr is a pointer to the XCanPs instance.
964 * @param SyncJumpWidth is the Synchronization Jump Width value to set.
965 * Valid values are from 0 to 3.
966 * @param TimeSegment2 is the Time Segment 2 value to set. Valid values
968 * @param TimeSegment1 is the Time Segment 1 value to set. Valid values
972 * - XST_SUCCESS if the Bit time is set successfully.
973 * - XST_FAILURE if CAN device is not in Configuration Mode.
977 ******************************************************************************/
978 s32 XCanPs_SetBitTiming(XCanPs *InstancePtr, u8 SyncJumpWidth,
979 u8 TimeSegment2, u8 TimeSegment1)
984 Xil_AssertNonvoid(InstancePtr != NULL);
985 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
986 Xil_AssertNonvoid(SyncJumpWidth <= (u8)3U);
987 Xil_AssertNonvoid(TimeSegment2 <= (u8)7U);
988 Xil_AssertNonvoid(TimeSegment1 <= (u8)15U );
990 if (XCanPs_GetMode(InstancePtr) != (u8)XCANPS_MODE_CONFIG) {
991 Status = XST_FAILURE;
994 Value = ((u32) TimeSegment1) & XCANPS_BTR_TS1_MASK;
995 Value |= (((u32) TimeSegment2) << XCANPS_BTR_TS2_SHIFT) &
997 Value |= (((u32) SyncJumpWidth) << XCANPS_BTR_SJW_SHIFT) &
1000 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
1001 XCANPS_BTR_OFFSET, Value);
1003 Status = XST_SUCCESS;
1008 /*****************************************************************************/
1011 * This routine gets Bit time. Time segment 1, Time segment 2 and
1012 * Synchronization Jump Width values are read in this function. According to
1013 * device specification, the actual value of each of these fields is one
1014 * more than the value read. Read the device specification for details.
1016 * @param InstancePtr is a pointer to the XCanPs instance.
1017 * @param SyncJumpWidth will store the Synchronization Jump Width value
1018 * after this function returns. Its value ranges from 0 to 3.
1019 * @param TimeSegment2 will store the Time Segment 2 value after this
1020 * function returns. Its value ranges from 0 to 7.
1021 * @param TimeSegment1 will store the Time Segment 1 value after this
1022 * function returns. Its value ranges from 0 to 15.
1028 ******************************************************************************/
1029 void XCanPs_GetBitTiming(XCanPs *InstancePtr, u8 *SyncJumpWidth,
1030 u8 *TimeSegment2, u8 *TimeSegment1)
1034 Xil_AssertVoid(InstancePtr != NULL);
1035 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1036 Xil_AssertVoid(SyncJumpWidth != NULL);
1037 Xil_AssertVoid(TimeSegment2 != NULL);
1038 Xil_AssertVoid(TimeSegment1 != NULL);
1040 Value = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
1043 *TimeSegment1 = (u8) (Value & XCANPS_BTR_TS1_MASK);
1045 (u8) ((Value & XCANPS_BTR_TS2_MASK) >> XCANPS_BTR_TS2_SHIFT);
1047 (u8) ((Value & XCANPS_BTR_SJW_MASK) >> XCANPS_BTR_SJW_SHIFT);
1051 /****************************************************************************/
1054 * This routine sets the Rx Full threshold in the Watermark Interrupt Register.
1056 * @param InstancePtr is a pointer to the XCanPs instance.
1057 * @param Threshold is the threshold to be set. The valid values are
1061 * - XST_FAILURE - If the CAN device is not in Configuration Mode.
1062 * - XST_SUCCESS - If the Rx Full threshold is set in Watermark
1063 * Interrupt Register.
1065 * @note The threshold can only be set when the CAN device is in the
1066 * configuration mode.
1068 *****************************************************************************/
1069 s32 XCanPs_SetRxIntrWatermark(XCanPs *InstancePtr, u8 Threshold)
1074 Xil_AssertNonvoid(InstancePtr != NULL);
1075 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1076 Xil_AssertNonvoid(Threshold <= (u8)63);
1078 if (XCanPs_GetMode(InstancePtr) != (u8)XCANPS_MODE_CONFIG) {
1079 Status = XST_FAILURE;
1082 ThrReg = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
1085 ThrReg &= XCANPS_WIR_EW_MASK;
1086 ThrReg |= ((u32)Threshold & XCANPS_WIR_FW_MASK);
1087 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
1088 XCANPS_WIR_OFFSET, ThrReg);
1090 Status = XST_SUCCESS;
1095 /****************************************************************************/
1098 * This routine gets the Rx Full threshold from the Watermark Interrupt Register.
1100 * @param InstancePtr is a pointer to the XCanPs instance.
1102 * @return The Rx FIFO full watermark threshold value. The valid values
1107 *****************************************************************************/
1108 u8 XCanPs_GetRxIntrWatermark(XCanPs *InstancePtr)
1111 Xil_AssertNonvoid(InstancePtr != NULL);
1112 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1115 return (u8) (XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
1116 XCANPS_WIR_OFFSET) &
1117 XCANPS_WIR_FW_MASK);
1121 /****************************************************************************/
1124 * This routine sets the Tx Empty Threshold in the Watermark Interrupt Register.
1126 * @param InstancePtr is a pointer to the XCanPs instance.
1127 * @param Threshold is the threshold to be set. The valid values are
1131 * - XST_FAILURE - If the CAN device is not in Configuration Mode.
1132 * - XST_SUCCESS - If the threshold is set in Watermark
1133 * Interrupt Register.
1135 * @note The threshold can only be set when the CAN device is in the
1136 * configuration mode.
1138 *****************************************************************************/
1139 s32 XCanPs_SetTxIntrWatermark(XCanPs *InstancePtr, u8 Threshold)
1143 Xil_AssertNonvoid(InstancePtr != NULL);
1144 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1145 Xil_AssertNonvoid(Threshold <= (u8)63);
1147 if (XCanPs_GetMode(InstancePtr) != (u8)XCANPS_MODE_CONFIG) {
1148 Status = XST_FAILURE;
1151 ThrReg = XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
1154 ThrReg &= XCANPS_WIR_FW_MASK;
1155 ThrReg |= (((u32)Threshold << XCANPS_WIR_EW_SHIFT)
1156 & XCANPS_WIR_EW_MASK);
1157 XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr,
1158 XCANPS_WIR_OFFSET, ThrReg);
1160 Status = XST_SUCCESS;
1165 /****************************************************************************/
1168 * This routine gets the Tx Empty threshold from Watermark Interrupt Register.
1170 * @param InstancePtr is a pointer to the XCanPs instance.
1172 * @return The Tx Empty FIFO threshold value. The valid values are 1 to 63.
1176 *****************************************************************************/
1177 u8 XCanPs_GetTxIntrWatermark(XCanPs *InstancePtr)
1180 Xil_AssertNonvoid(InstancePtr != NULL);
1181 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
1184 return (u8) ((XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr,
1185 XCANPS_WIR_OFFSET) & XCANPS_WIR_EW_MASK) >>
1186 XCANPS_WIR_EW_SHIFT);
1191 /******************************************************************************/
1193 * This routine is a stub for the asynchronous callbacks. The stub is here in
1194 * case the upper layer forgot to set the handler(s). On initialization, all
1195 * handlers are set to this callback. It is considered an error for this handler
1198 ******************************************************************************/
1199 static void StubHandler(void)
1201 Xil_AssertVoidAlways();