⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can.c

📁 uCOS-II V2.84 LM3S6965 TCPIP Demo
💻 C
📖 第 1 页 / 共 5 页
字号:
//*****************************************************************************
//
// can.c - Driver for the CAN module.
//
// Copyright (c) 2006-2007 Luminary Micro, Inc.  All rights reserved.
// 
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 1392 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup can_api
//! @{
//
//*****************************************************************************
#include "../hw_ints.h"
#include "../hw_memmap.h"
#include "../hw_types.h"
#include "../hw_nvic.h"
#include "../hw_can.h"
#include "debug.h"
#include "interrupt.h"
#include "sysctl.h"
#include "can.h"

//*****************************************************************************
//
// This is the maximum number that can be stored as an 11bit Message
// identifier
//
//*****************************************************************************
#define CAN_MAX_11BIT_MSG_ID    (0x7ff)

//*****************************************************************************
//
// This is used as the loop delay for accessing the CAN controller registers.
//
//*****************************************************************************
#define CAN_RW_DELAY            (5)

//*****************************************************************************
//
//! Reads a CAN controller register.
//!
//! \param ulRegAddress is the full address of the CAN register to be read.
//!
//! This function takes care of the synchronization necessary to read from a
//! CAN controller register.
//!
//! \note This function takes care of delay required to access CAN registers.
//! This delay is required when accessing CAN registers directly.
//!
//! \return The current value of the register that was requested by
//! ulRegAddress.
//
//*****************************************************************************
unsigned long
CANReadReg(unsigned long ulRegAddress)
{
    volatile int iDelay;
    unsigned long ulRetVal;
    unsigned long ulIntNumber;
    unsigned long ulReenableInts;

    //
    // Get the CAN interrupt number from the register base address.
    //
    ulIntNumber = CANGetIntNumber(ulRegAddress & 0xfffff000);

    //
    // Make sure that the CAN base address was valid.
    //
    ASSERT(ulIntNumber != (unsigned long)-1);

    //
    // Remember current state so that CAN interrupts are only re-enabled if
    // they were already enabled.
    //
    ulReenableInts = HWREG(NVIC_EN1) & (1 << (ulIntNumber - 48));

    //
    // If the CAN interrupt was enabled then disable it.
    //
    if(ulReenableInts)
    {
        IntDisable(ulIntNumber);
    }

    //
    // Trigger the inital read to the CAN controller.  The value returned at
    // this point is not valid.
    //
    HWREG(ulRegAddress);

    //
    // This delay is necessary for the CAN have the correct data on the bus.
    //
    for(iDelay = 0; iDelay < CAN_RW_DELAY; iDelay++)
    {
    }

    //
    // Do the final read that has the valid value of the register.
    //
    ulRetVal = HWREG(ulRegAddress);

    //
    // Reenable CAN interrupts if they were enabled before this call.
    //
    if(ulReenableInts)
    {
        IntEnable(ulIntNumber);
    }

    return(ulRetVal);
}

//*****************************************************************************
//
//! Writes a CAN controller register.
//!
//! \param ulRegAddress is the full address of the CAN register to be written.
//! \param ulRegValue is the value to write into the register specified by
//! ulRegAddress.
//!
//! This function takes care of the synchronization necessary to write to a
//! CAN controller register.
//!
//! \note The delays in this function are required when accessing CAN registers
//! directly.
//!
//! \return None.
//
//*****************************************************************************
void
CANWriteReg(unsigned long ulRegAddress, unsigned long ulRegValue)
{
    volatile int iDelay;

    //
    // Trigger the inital write to the CAN controller.  The value will not make
    // it out to the CAN controller for CAN_RW_DELAY cycles.
    //
    HWREG(ulRegAddress) = ulRegValue;

    //
    // Delay to allow the CAN controller to receive the new data.
    //
    for(iDelay = 0; iDelay < CAN_RW_DELAY; iDelay++)
    {
    }
}

//*****************************************************************************
//
//! This function copies data from a buffer to the CAN Data registers.
//!
//! \param pucData is a pointer to the data to be written out to the CAN
//! controller's data registers.
//! \param pulRegister is an unsigned long pointer to the first register of
//! the CAN controller's data registers.  For example in order to use the IF1
//! register set on CAN controller 0 the value would be: (CAN0_BASE
//! + CAN_O_IF1DA1).
//! \param iSize is the number of bytes to copy into the CAN controller.
//!
//! This function takes the steps necessary to copy data from a contiguous
//! buffer in memory into the non-contiguous data registers used by the CAN
//! controller. This function is rarely used outside of the CANMessageSet()
//! function.
//!
//! \return None.
//
//*****************************************************************************
void
CANWriteDataReg(unsigned char *pucData, unsigned long *pulRegister, int iSize)
{
    int iIdx;
    unsigned long ulValue;

    //
    // Loop always copies 1 or 2 bytes per iteration.
    //
    for(iIdx = 0; iIdx < iSize; )
    {

        //
        // Write out the data 16 bits at a time since this is how the
        // registers are aligned in memory.
        //
        ulValue = pucData[iIdx++];

        //
        // Only write the second byte if needed otherwise it will be zero.
        //
        if(iIdx < iSize)
        {
            ulValue |= (pucData[iIdx++] << 8);
        }
        CANWriteReg((unsigned long)(pulRegister++), ulValue);
    }
}

//*****************************************************************************
//
//! This function copies data from a buffer to the CAN Data registers.
//!
//! \param pucData is a pointer to location to store the data read from the
//! CAN controller's data registers.
//! \param pulRegister is an unsigned long pointer to the first register of
//! the CAN controller's data registers.  For example in order to use the IF1
//! register set on CAN controller 1 the value would be: (CAN0_BASE
//! + CAN_O_IF1DA1).
//! \param iSize is the number of bytes to copy from the CAN controller.
//!
//! This function takes the steps necessary to copy data to a contiguous
//! buffer in memory from the non-contiguous data registers used by the CAN
//! controller.  This function is rarely used outside of the CANMessageGet()
//! function.
//!
//! \return None.
//
//*****************************************************************************
void
CANReadDataReg(unsigned char *pucData, unsigned long *pulRegister, int iSize)
{
    int iIdx;
    unsigned long ulValue;

    //
    // Loop always copies 1 or 2 bytes per iteration.
    //
    for(iIdx = 0; iIdx < iSize; )
    {
        //
        // Read out the data 16 bits at a time since this is how the
        // registers are aligned in memory.
        //
        ulValue = CANReadReg((unsigned long)(pulRegister++));

        //
        // Store the first byte.
        //
        pucData[iIdx++] = (unsigned char)ulValue;

        //
        // Only read the second byte if needed.
        //
        if(iIdx < iSize)
        {
            pucData[iIdx++] = (unsigned char)(ulValue >> 8);
        }
    }
}

//*****************************************************************************
//
//! Initializes the CAN controller after reset.
//!
//! \param ulBase is the base address of the CAN controller.
//!
//! After reset, the CAN controller is left in the disabled state.  However,
//! the memory used for message objects contains undefined values and must
//! be cleared prior to enabling the CAN controller the first time.
//! This prevents unwanted transmission or reception of data before the message
//! objects are configured.  This function must be called before enabling the
//! controller the first time.
//!
//! \return None.
//
//*****************************************************************************
void
CANInit(unsigned long ulBase)
{
    int iMsg;

    //
    // Make sure that the address passed in is valid.
    //
    ASSERT((ulBase == CAN0_BASE) ||
           (ulBase == CAN1_BASE));

    //
    // Place CAN controller in init state, regardless of previous state
    // This will put controller in idle, and allow the message object
    // RAM to be programmed.
    //
    CANWriteReg(ulBase + CAN_O_CTL, CAN_CTL_INIT);

    //
    // Wait for busy bit to clear
    //
    while(CANReadReg(ulBase + CAN_O_IF1CRQ) & CAN_IFCRQ_BUSY)
    {
    }

    //
    // Clear the message value bit in the arbitration register.
    // This indicates the message is not valid and is a "safe"
    // condition to leave the message object.  The same arb reg
    // is used to program all the message objects.
    //
    CANWriteReg(ulBase + CAN_O_IF1CMSK, CAN_IFCMSK_WRNRD | CAN_IFCMSK_ARB |
                CAN_IFCMSK_CONTROL);
    CANWriteReg(ulBase + CAN_O_IF1ARB2, 0);
    CANWriteReg(ulBase + CAN_O_IF1MCTL, 0);

    //
    // Loop through to program all 32 message objects
    //
    for(iMsg = 1; iMsg <= 32; iMsg++)
    {
        //
        // Wait for busy bit to clear
        //
        while(CANReadReg(ulBase + CAN_O_IF1CRQ) & CAN_IFCRQ_BUSY)
        {
        }

        //
        // Initiate programming the message object
        //
        CANWriteReg(ulBase + CAN_O_IF1CRQ, iMsg);
    }

    //
    // Make sure that the interrupt and new data flags are updated for the
    // message objects.
    //
    CANWriteReg(ulBase + CAN_O_IF1CMSK, CAN_IFCMSK_NEWDAT |
                CAN_IFCMSK_CLRINTPND);

    //
    // Loop through to program all 32 message objects
    //
    for(iMsg = 1; iMsg <= 32; iMsg++)
    {
        //
        // Wait for busy bit to clear.
        //
        while(CANReadReg(ulBase + CAN_O_IF1CRQ) & CAN_IFCRQ_BUSY)
        {
        }

        //
        // Initiate programming the message object
        //
        CANWriteReg(ulBase + CAN_O_IF1CRQ, iMsg);
    }

    //
    // Acknowledge any pending status interrupts.
    //
    CANReadReg(ulBase + CAN_O_STS);
}

//*****************************************************************************
//
//! Enables the CAN controller.
//!
//! \param ulBase is the base address of the CAN controller to enable.
//!
//! Enables the CAN controller for message processing.  Once enabled, the
//! controller will automatically transmit any pending frames, and process
//! any received frames.  The controller can be stopped by calling
//! CANDisable().  Prior to calling CANEnable(), CANInit() should have been
//! called to initialize the controller and the CAN bus clock should be
//! configured by calling CANSetBitTiming().
//!
//! \return None.
//
//*****************************************************************************
void
CANEnable(unsigned long ulBase)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -