📄 initialization.c
字号:
#include "CAN_RX.h"
/*********************************************************************************
** **
** Init_PLL - Configures the PLL so that the CAN BRP can easily be derived. **
** Sets Core Clock (CCLK) to 450MHz and System Clock (SCLK) to **
** 112.5MHz. **
** **
*********************************************************************************/
void Init_PLL()
{
*pPLL_CTL = SET_MSEL(18); /* (25MHz Xtal x (MSEL=18))::CCLK=450MHz */
idle();
*pPLL_DIV = SET_SSEL(4); /* (450MHz/(SSEL=4))::SCLK=112.5MHz */
ssync();
} /* end Init_PLL */
/*********************************************************************************
** **
** Init_Port - Sets up the PORTs for CAN use and configures the PFx pins for **
** access to the on-board LEDs. **
** **
*********************************************************************************/
void Init_Port ()
{
*pPORT_MUX = PJCE_CAN; /* Enable CAN Pins On Port J */
ssync();
/* Configure Port F pins for LED access */
*pPORTFIO_DIR = PF6|PF7|PF8|PF9|PF10|PF11; /* PF6-11 Output LEDs */
ssync();
} /* End Init_Port () */
/*********************************************************************************
** **
** Init_CAN_Timing - Sets up the CAN TIMING and CLOCK registers. Provided **
** comments indicate how to derive the BRP based on the **
** known SCLK value. See CAN chapter of HRM for more **
** details regarding the CAN TIMING and CLOCK registers. **
** **
*********************************************************************************/
void Init_CAN_Timing()
{
/* ===================================================
* BIT TIMING:
*
* CAN_TIMING : SJW = 3, TSEG2 = 3, TSEG1 = 4
* SJW <= TSEG2 <= TSEG1
* ===================================================
* CAN_CLOCK : Prescaler (BRP)
* ===================================================
* 500kHz CAN Clock :: tBIT = 2us
* ===================================================
* tBIT = TQ * (1 + (TSEG1 + 1) + (TSEG2 + 1))
* 2e-6 = TQ * (1 + 5 + 4)
* TQ = 2e-7
*
* TQ = (BRP+1) / SCLK
* 2e-7 = (BRP+1) / 112.5e6
* (BRP+1) = 22.5
* BRP = 21.5 = ~22 = CAN_CLOCK
* ==================================================
* Set Bit Configuration Registers ...
* ==================================================*/
*pCAN_TIMING = 0x0334;
*pCAN_CLOCK = 22;
ssync();
} /* End Init_CAN_Timing() */
/*********************************************************************************
** **
** Init_CAN_Mailboxes - Configures MB24 to transmit a specific message ID with **
** a message length of 1 byte. Configures MB7 to receive **
** a specific message ID. **
** **
*********************************************************************************/
void Init_CAN_Mailboxes()
{
short msgID;
volatile char mbID;
/* Mailbox 24 Will Transmit ACK back to transmitter via ID 0x007 */
msgID = 0x007;
mbID = 24;
/* ID1, mask disabled, remote frame disable, 11 bit identifier */
*(pCAN_MB_ID1(mbID)) = msgID << 2;
*(pCAN_MB_LENGTH(mbID)) = 1; /* DLC = 1 byte */
/* Mailbox 7 Will Receive CAN Command From Transmitter via ID 0x411 */
msgID = 0x411;
mbID = 7;
/* ID1, mask disabled, remote frame disable, 11 bit identifier */
*(pCAN_MB_ID1(mbID)) = msgID << 2;
ssync();
} /* End Init_CAN_Mailboxes() */
/*********************************************************************************
** **
** Init_Interrupts - Assigns interrupt priorities for CAN TX and CAN RX. **
** Registers handlers for each and enables the interrupt **
** in both the CEC and SIC. **
** **
*********************************************************************************/
void Init_Interrupts()
{
/* Configure Interrupt Priorities */
*pSIC_IAR0 = 0x77777777;
*pSIC_IAR1 = 0x07777777; /* CAN RX IRQ : 0=IVG7 */
*pSIC_IAR2 = 0x77777771; /* CAN TX IRQ : 1=IVG8 */
*pSIC_IAR3 = 0x77777777;
/* Register Interrupt Handlers and Enable Core Interrupts */
register_handler(ik_ivg7, CAN_RCV_HANDLER);
register_handler(ik_ivg8, CAN_XMT_HANDLER);
/* Enable SIC Level Interrupts */
*pSIC_IMASK |= (IRQ_CAN_RX|IRQ_CAN_TX);
} /* end Init_Interrupts */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -