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

📄 ew_can.c

📁 包含了NXP众多的MCU的例子程序
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
*****************************************************************************
**
**      Project     : EW CAN Library for Philips LPC2129
**
**      Modulename  : CAN
**
**      Filename    : ew_can.c
**
**      Abstract    : The module contain CAN library for the Philips LPC2129
**                    CAN controller.
**                    The software contain initalization, run-time and
**                    interrupt handling functions.
**
**                    The init function contain initialization code for the
**                    CAN peripheral and both CAN controller #1 and #2 are
**                    used.
**
**                    Run-time functions are normally called by the
**                    application software to
**                     1. Load CAN filters used by the application
**                     2. Manage the CAN peripheral to view if messages has
**                        been received or overrun or errors has occured.
**                     3. Read received CAN messages
**                     4. Transmit CAN messages
**
**                    Interrupt functions are used when interrupts has been
**                    enabled.
**
**                    Application software can change the behaviour of this
**                    CAN library by updating the <user_can.h> file.
**                    <user_can.h> file include user code macros definitions.
**
**      Functions   : EW_Init_CAN()             Initialize CAN
**
**                    EW_AddFilter_CAN()        Add filter
**                    EW_Manage_CAN()           Manage CAN operation
**                    EW_ReceiveData_CAN()      Receive data
**                    EW_SendData_CAN()         Send data
**
**                    EW_IntHandler_RX_CAN1()   RX message CAN1 interrupt
**                    EW_IntHandler_RX_CAN2()   RX message CAN2 interrupt
**                    EW_IntHandler_TX_CAN1()   TX message CAN1 interrupt
**                    EW_IntHandler_TX_CAN2()   TX message CAN2 interrupt
**                    EW_IntHandler_OTHER_CAN() Other CAN interrupts
**
**      Date        : 2005-12-05
**
**      Version     : 1.00A
**
**      (c) Copyright IAR Systems 2005
**
**      THE SOFTWARE IS PROVIDED "AS IS"  WITHOUT ANY WARRANTY OF ANY KIND,
**      IAR SYSTEMS DOES NOT WARRANT THAT THE SOFTWARE WILL BE ERROR-FREE
**      OR THAT SUCH ERRORS WILL BE CORRECTED, AS A RESULT, IAR SYSTEMS
**      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.
**
*****************************************************************************
*/

/*
**===========================================================================
**  1       GENERAL
**  1.1     Revisions
**
**===========================================================================
*/

/*
**===========================================================================
**  1.2     References
**
**  No   Identification          Name or Description
**  ==   ===================     =======================================
**  1    May 03, 2004            Philips LPC2119/2129/2194/2292/2294
**                               User Manual, Preliminary Release
**  2    August 5, 2005          Philips ERRATA SHEET - Version 1.8 LPC2129
**===========================================================================
*/

/*
**===========================================================================
**  2.      INCLUDE FILES
**  2.1     Standard include files
**===========================================================================
*/

/*
**===========================================================================
**  2.2     Application include files
**===========================================================================
*/
#include "iolpc2129.h"      /* Defines Special function registers */
#include "ew_can.h"         /* Module driver header file */
#include "user_can_def.h"   /* Application specific CAN definitions */



/*
**===========================================================================
**  3.      DECLARATIONS
**  3.1     Internal constants
**===========================================================================
*/
#define CAN_REG_BASE                    (0xE0000000)
#define ACCEPTANCE_FILTER_RAM_BASE      (CAN_REG_BASE + 0x00038000)
#define ACCEPTANCE_FILTER_REGISTER_BASE (CAN_REG_BASE + 0x0003C000)
#define CENTRAL_CAN_REGISTER_BASE       (CAN_REG_BASE + 0x00040000)
#define CAN_1_BASE                      (CAN_REG_BASE + 0x00044000)
#define CAN_2_BASE                      (CAN_REG_BASE + 0x00048000)


/*--- SFR register bits ---*/
#define C1GSR_RBS  0x01        /* Bit 0: RBS (Received buffer status)  */
#define C1CMR_RRB  0x04        /* Bit 2: RRB (Release receiver buffer) */


/*
**===========================================================================
**  3.2     Internal macros
**===========================================================================
*/

/*
**===========================================================================
**  3.3     Internal type definitions
**===========================================================================
*/

/*
**===========================================================================
**  3.4     Global variables (declared as 'extern' in some header file)
**===========================================================================
*/

/*
**------------------------------------
Counters used only during driver test
long SEM1count;
long SEM2count;
**------------------------------------
*/

/*
**===========================================================================
**  3.5     Internal function prototypes (defined in Section 5)
**===========================================================================
*/

/*
**===========================================================================
**  3.6     Internal variables
**===========================================================================
*/

/*
**===========================================================================
**  4.      GLOBAL FUNCTIONS (declared as 'extern' in some header file)
**===========================================================================
*/

void EW_Init_CAN( void )
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**    Initializes the CAN module.
**
**  Parameters:
**      None
**
**  Returns:
**      None
**
**---------------------------------------------------------------------------
*/
{
    /*--- Handle user code on function entry ---*/
    ENTER_EW_INIT_CAN;

    /*--- Enable Pins for CAN port 1 and 2 ---*/
    PINSEL1 |= EW_CAN_PINSEL1;

    /*--- Enable CAN modules clock ---*/
    PCONP |= (1<<13) | (1<<14);

    /*--- All Rx messages on all CAN buses are ignored ---*/
    AFMR = AcceptanceFilterOff;

    /*--- Enter reset mode ---*/
    C1MOD |= 0x01;
    C2MOD |= 0x01;

    /*
    **-----------------------------------
    ** Select the CAN interrupt handling
    **-----------------------------------
    */
    C1IER = EW_C1IER_CAN;
    C2IER = EW_C2IER_CAN;

    /*
    **-----------------------
    ** Clear status register
    **-----------------------
    */
    C1GSR = 0;
    C2GSR = 0;

    /*
    **-----------------------------------------------------
    ** Select the CAN bitrate, sample method, TSEG and SJW
    **-----------------------------------------------------
    */
    C1BTR = EW_C1BTR_CAN;
    C2BTR = EW_C2BTR_CAN;

    /*
    **--------------------------------------------------------------
    ** Acceptance filter and central CAN register updates. Note the
    ** filter table is updated with the function EW_AddFilter_CAN()
    **--------------------------------------------------------------
    */
    SFF_sa     = 0;
    SFF_GRP_sa = 0;
    EFF_sa     = 0;
    EFF_GRP_sa = 0;
    ENDofTable = 0;

    /*--- Acceptance filter mode ----*/
    AFMR = EW_AFMR_CAN;

    /*
    **---------------------------------------------------------------------------
    ** Select the CANMOD used by the CAN driver
    **---------------------------------------------------------------------------
    */
    /*--- Set mode register but do not release reset ---*/
    C1MOD = ( EW_C1MOD_CAN | 0x01 );
    C2MOD = ( EW_C2MOD_CAN | 0x01 );

    /*--- Release CAN reset ---*/
    C1MOD = ( C1MOD & ~0x01 );
    C2MOD = ( C2MOD & ~0x01 );

    /*--- Handle user code on function exit ---*/
    EXIT_EW_INIT_CAN;

} /* EW_Init_CAN */



S16 EW_AddFilter_CAN( CAN_Filter *pNewFilter )
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      The new filter is inserted into the look-up filter table RAM.
**
**      The CAN_Filter must contain the following information depending on
**      the added filter type.
**      CAN_FULL:   Two standard IDs with controller info.
**      STD:        Two standard IDs with controller info.
**      STD_GRP:    Lower and Upper standard ID bound with controller info.
**      EXT:        Extended ID with controller info.
**      EXT_GRP:    Lower and Upper extended id bound with controller info.
**
**      Please note; When CAN_FULL and STD messages shall be inserted
**                   two IDs will be inserted per call. The driver
**                   requires that these IDs can be inserted into table
**                   at the same word while keeping a sorted list.
**
**  Parameters:
**      pNewFilter  Pointer to the new Filter information
**
**  Returns:
**      EW_OK       if filter successfully added
**      EW_FULL     if filter table is full
**      EW_ERROR    if error
**
**---------------------------------------------------------------------------
*/
{
    U32 ii, Size;
    U32 FilterID;
    U32 Address;
    U32 *pFilter;
    U32 id0, id1, id3, id4;

    /*--- Handle user code on function entry ---*/
    ENTER_EW_ADDFILTER_CAN;

    /*--- Can we add new item? ---*/
    Size = ENDofTable + 4;
    Size += SFF_sa * 6;
    if( Size > 0x7ff )
    {
        /*--- Acceptance filter table full ---*/
        return EW_FULL;
    }

    /*--- Prepare filter values ---*/
    switch ( pNewFilter->FilterType )
    {
    case EW_CAN_FULL:
    case EW_CAN_STD:
    case EW_CAN_STD_GRP:
        /*--- Prepare new pair of IDs ---*/
        FilterID = pNewFilter->ID1;                    /* ID lower */
        FilterID |= ( pNewFilter->Disable1 << 12);     /* Disable bit */
        FilterID |= ( pNewFilter->Controller1 << 13);  /* Controller */
        FilterID <<= 16;                               /* shift to lower column */

        FilterID |= pNewFilter->ID2;                   /* ID upper */
        FilterID |= ( pNewFilter->Disable1 << 12);     /* Disable bit */
        FilterID |= ( pNewFilter->Controller2 << 13);  /* Controller */
        break;

    case EW_CAN_EXT:
    case EW_CAN_EXT_GRP:
        /*--- Prepare new extend ID ---*/
        FilterID = pNewFilter->ID1;                    /* ID */
        FilterID |= ( pNewFilter->Disable1 << 28);     /* Disable bit */
        FilterID |= ( pNewFilter->Controller1 << 29);  /* Controller */
        break;

    default:
        return EW_ERROR;
    }

    /*--- All Rx messages on all CAN buses are ignored ---*/
    AFMR = AcceptanceFilterOff;

    /*--- Enter reset mode ---*/
    C1MOD |= 0x01;
    C2MOD |= 0x01;

    /*--- Set pointer to first location of filter ---*/
    pFilter = (U32 *) ACCEPTANCE_FILTER_RAM_BASE;

    /*--- What kind of filter will be handled? ---*/
    switch ( pNewFilter->FilterType )
    {
    case EW_CAN_FULL:
        /*--- Find place to add new fullCAN filter ---*/
        ii = 0;

        /*--- Loop through all existing fullCAN filters ---*/
        while ( ii < SFF_sa )
        {
            /*--- New filter < Stored filter? ---*/
            if ( ( FilterID & 0xc7ffc7ff ) < ( *pFilter & 0xc7ffc7ff ) )
            {
                /*--- yes break ---*/
                break;
            }
            ii += 4;
            pFilter++;
        }

        /*--- Save current table ID ---*/
        id0 = *pFilter;

        /*--- Update start address registers ---*/
        SFF_sa += 4;
        SFF_GRP_sa += 4;
        EFF_sa += 4;
        EFF_GRP_sa += 4;
        break;

    case EW_CAN_STD:
        /*--- Find place to add new standard IDs filter ---*/
        ii = SFF_sa;
        pFilter += (ii / 4);

        /*--- Loop through all existing standard filters ---*/
        while ( ii < SFF_GRP_sa )
        {
            /*--- New filter < Stored filter? ---*/
            if ( ( FilterID & 0xc7ffc7ff ) < ( *pFilter & 0xc7ffc7ff ) )
            {
                /*--- yes break ---*/
                break;
            }
            ii += 4;
            pFilter++;
        }

        /*--- Save current table ID ---*/
        id0 = *pFilter;

        /*--- Add new Filter ID ---*/
        *pFilter = FilterID;

        /*--- Update start address registers ---*/
        SFF_GRP_sa += 4;
        EFF_sa += 4;
        EFF_GRP_sa += 4;
        break;

    case EW_CAN_STD_GRP:
        /*--- Find place to add new standard IDs group filter ---*/
        ii = SFF_GRP_sa;
        pFilter += (ii / 4);

        /*--- Loop through all existing standard filters ---*/
        while ( ii < EFF_sa )
        {
            /*--- New filter < Stored filter? ---*/
            if ( ( FilterID & 0xc7ffc7ff ) < ( *pFilter & 0xc7ffc7ff ) )
            {
                /*--- yes break ---*/
                break;
            }
            ii += 4;
            pFilter++;
        }

        /*--- Save current table ID ---*/
        id0 = *pFilter;

        /*--- Add new Filter ID ---*/
        *pFilter = FilterID;

        /*--- Update start address registers ---*/
        EFF_sa += 4;
        EFF_GRP_sa += 4;
        break;

    case EW_CAN_EXT:
        /*--- Find place to add new extended ID filter ---*/
        ii = EFF_sa;
        pFilter += (ii / 4);

        /*--- Loop through all existing standard filters ---*/
        while ( ii < EFF_GRP_sa )
        {
            /*--- New filter < Stored filter? ---*/
            if ( ( FilterID & 0xc7ffffff ) < ( *pFilter & 0xc7ffffff ) )
            {
                /*--- yes break ---*/
                break;
            }
            ii += 4;
            pFilter++;
        }

        /*--- Save current table ID ---*/
        id0 = *pFilter;

        /*--- Add new Filter ID ---*/
        *pFilter = FilterID;

        // Update start address registers
        EFF_GRP_sa += 4;
        break;

    case EW_CAN_EXT_GRP:
        /*--- Find place to add new extended IDs group filter ---*/
        ii = EFF_GRP_sa;
        pFilter += (ii / 4);

        /*--- Loop through all existing standard filters ---*/
        while ( ii < ENDofTable )
        {
            /*--- New filter < Stored filter? ---*/
            if ( ( FilterID & 0xc7ffffff ) < ( *pFilter & 0xc7ffffff ) )
            {
                /*--- yes break ---*/
                break;
            }

⌨️ 快捷键说明

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