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

📄 can_device_qs.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 2 页
字号:
        case MSGOBJ_NUM_DATA_TX:
        {
            //
            // Clear the data transmit pending flag.
            //
            g_ulFlags &= (~FLAG_DATA_TX_PEND);
            break;
        }

        //
        // The data receive message object has received some data.
        //
        case MSGOBJ_NUM_DATA_RX:
        {
            //
            // Indicate that the data message object has new data.
            //
            g_ulFlags |= FLAG_DATA_RECV;
            break;
        }

        //
        // This was a status interrupt so read the current status to
        // clear the interrupt and return.
        //
        default:
        {
            CANStatusGet(CAN0_BASE, CAN_STS_CONTROL);
            return;
        }
    }

    //
    // Acknowledge the CAN controller interrupt has been handled.
    //
    CANIntClear(CAN0_BASE, ulStatus);
}

//*****************************************************************************
//
// Configure CAN message objects for the application.
//
// This function configures the message objects used by this application.
// The following four message objects used by this application:
// MSGOBJ_ID_BUTTON, MSGOBJ_ID_LED, MSGOBJ_ID_DATA_TX, and MSGOBJ_ID_DATA_RX.
//
// /return None.
//
//*****************************************************************************
void
CANConfigureNetwork(void)
{
    //
    // This is the message object used to send button updates.  This message
    // object will not be "set" right now as that would trigger a transmission.
    //
    g_MsgObjectButton.ulMsgID = MSGOBJ_ID_BUTTON;
    g_MsgObjectButton.ulMsgIDMask = 0;

    //
    // This enables interrupts for transmitted messages.
    //
    g_MsgObjectButton.ulFlags = MSG_OBJ_TX_INT_ENABLE;

    //
    // Set the length of the message, which should only be two bytes and the
    // data is always whatever is in g_pucButtonMsg.
    //
    g_MsgObjectButton.ulMsgLen = 2;
    g_MsgObjectButton.pucMsgData = g_pucButtonMsg;

    //
    // This message object will receive updates for the LED brightness.
    //
    g_MsgObjectLED.ulMsgID = MSGOBJ_ID_LED;
    g_MsgObjectLED.ulMsgIDMask = 0;

    //
    // This enables interrupts for received messages.
    //
    g_MsgObjectLED.ulFlags = MSG_OBJ_RX_INT_ENABLE;

    //
    // The length of the message, which should only be one byte.
    //
    g_MsgObjectLED.ulMsgLen = 1;
    g_MsgObjectLED.pucMsgData = &g_ucLEDLevel;
    CANMessageSet(CAN0_BASE, MSGOBJ_NUM_LED, &g_MsgObjectLED,
                  MSG_OBJ_TYPE_RX);

    //
    // This message object will transmit commands and command responses.  It
    // will not be "set" right now as that would trigger a transmission.
    //
    g_MsgObjectTx.ulMsgID = MSGOBJ_ID_DATA_TX;
    g_MsgObjectTx.ulMsgIDMask = 0;

    //
    // This enables interrupts for transmitted messages.
    //
    g_MsgObjectTx.ulFlags = MSG_OBJ_TX_INT_ENABLE;

    //
    // The length of the message, which should only be one byte.  Don't set
    // the pointer until it is used.
    //
    g_MsgObjectTx.ulMsgLen = 1;
    g_MsgObjectTx.pucMsgData = (unsigned char *)0xffffffff;

    //
    // This message object will receive commands or data from commands.
    //
    g_MsgObjectRx.ulMsgID = MSGOBJ_ID_DATA_RX;
    g_MsgObjectRx.ulMsgIDMask = 0;

    //
    // This enables interrupts for received messages.
    //
    g_MsgObjectRx.ulFlags = MSG_OBJ_RX_INT_ENABLE;

    //
    // The length of the message, which should only be one byte.  Don't set
    // the pointer until it is used.
    //
    g_MsgObjectRx.ulMsgLen = 1;
    g_MsgObjectRx.pucMsgData = (unsigned char *)0xffffffff;
    CANMessageSet(CAN0_BASE, MSGOBJ_NUM_DATA_RX, &g_MsgObjectRx,
                  MSG_OBJ_TYPE_RX);
}

//*****************************************************************************
//
// This functions sends out a button update message.
//
//*****************************************************************************
void
SendButtonMsg(unsigned char ucEvent, unsigned char ucButton)
{
    //
    // Set the flag to indicate that a button status is being sent.
    //
    g_ulFlags |= FLAG_BUTTON_PEND;

    //
    // Send the button status.
    //
    g_MsgObjectButton.pucMsgData[0] = ucEvent;
    g_MsgObjectButton.pucMsgData[1] = ucButton;

    CANMessageSet(CAN0_BASE, MSGOBJ_NUM_BUTTON, &g_MsgObjectButton,
                  MSG_OBJ_TYPE_TX);
}

//*****************************************************************************
//
// Handle any interrupts.
//
//*****************************************************************************
void
ProcessInterrupts(void)
{
    //
    // A request to set or clear the LED was received.
    //
    if(g_ulFlags & FLAG_UPDATE_LED)
    {
        //
        // Turn the LED on or off based on the request.
        //
        if((g_ucLEDLevel & LED_FLASH_VALUE_MASK) > 0)
        {
            //
            // Turn on LED.
            //
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
        }
        else
        {
            //
            // Turn off LED.
            //
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
        }

        //
        // If the flash once flag was set then start the count down.
        //
        if(g_ucLEDLevel & LED_FLASH_ONCE)
        {
            g_lFlashCounter = 10;
        }

        //
        // Clear the flag.
        //
        g_ulFlags &= ~(FLAG_UPDATE_LED);
    }

    //
    // If there is a button event pending then send it.
    //
    if(g_ucButtonFlags != 0)
    {
        //
        // If button zero was pressed, send the message.
        //
        if(g_ucButtonFlags & MSG_OBJ_B0_PRESSED)
        {
            SendButtonMsg(EVENT_BUTTON_PRESS, TARGET_BUTTON_UP);

            //
            // Clear the flag since this has been handled.
            //
            g_ucButtonFlags &= (~MSG_OBJ_B0_PRESSED);
        }

        //
        // If button zero was released, send the message.
        //
        if(g_ucButtonFlags & MSG_OBJ_B0_RELEASED)
        {
            SendButtonMsg(EVENT_BUTTON_RELEASED, TARGET_BUTTON_UP);

            //
            // Clear the flag since this has been handled.
            //
            g_ucButtonFlags &= (~MSG_OBJ_B0_RELEASED);
        }

        //
        // If button one was pressed, send the message.
        //
        if(g_ucButtonFlags & MSG_OBJ_B1_PRESSED)
        {
            SendButtonMsg(EVENT_BUTTON_PRESS, TARGET_BUTTON_DN);

            //
            // Clear the flag since this has been handled.
            //
            g_ucButtonFlags &= (~MSG_OBJ_B1_PRESSED);
        }

        //
        // If button one was released, send the message.
        //
        if(g_ucButtonFlags & MSG_OBJ_B1_RELEASED)
        {
            SendButtonMsg(EVENT_BUTTON_RELEASED, TARGET_BUTTON_DN);

            //
            // Clear the flag since this has been handled.
            //
            g_ucButtonFlags &= (~MSG_OBJ_B1_RELEASED);
        }
    }
}

//*****************************************************************************
//
// This function handles incoming commands.
//
//*****************************************************************************
void
ProcessCmd(void)
{
    unsigned char pucData[8];

    //
    // If no data has been received, then there is nothing to do.
    //
    if((g_ulFlags & FLAG_DATA_RECV) == 0)
    {
        return;
    }

    //
    // Receive the command.
    //
    g_MsgObjectRx.pucMsgData = pucData;
    g_MsgObjectRx.ulMsgLen = 8;
    CANMessageGet(CAN0_BASE, MSGOBJ_NUM_DATA_RX, &g_MsgObjectRx, 1);

    //
    // Clear the flag to indicate that the data has been read.
    //
    g_ulFlags &= (~FLAG_DATA_RECV);

    switch(g_MsgObjectRx.pucMsgData[0])
    {
        //
        // This is a request for the firmware version for this application.
        //
        case CMD_GET_VERSION:
        {
            //
            // Send the Version.
            //
            g_ulFlags |= FLAG_DATA_TX_PEND;

            g_MsgObjectTx.pucMsgData = (unsigned char *)&g_ulVersion;
            g_MsgObjectTx.ulMsgLen = 4;
            CANMessageSet(CAN0_BASE, MSGOBJ_NUM_DATA_TX,
                          &g_MsgObjectTx, MSG_OBJ_TYPE_TX);
            break;
        }
    }
}

//*****************************************************************************
//
// This is the main loop for the application.
//
//*****************************************************************************
int
main(void)
{
    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(DEVICE_IS_REVA2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    //
    // Set the clocking to run directly from the PLL at 25MHz.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Enable the pull-ups on the JTAG signals.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    GPIOPadConfigSet(GPIO_PORTC_BASE,
                     GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Configure CAN 0 Pins.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure GPIO Pins used for the Buttons.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Configure GPIO Pin used for the LED.
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    //
    // Turn off the LED.
    //
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    //
    // Enable the CAN controller.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Reset the state of all the message object and the state of the CAN
    // module to a known state.
    //
    CANInit(CAN0_BASE);

    //
    // Configure the bit clock parameters for the CAN device.
    //
    CANSetBitTiming(CAN0_BASE, &CANBitClkSettings[CANBAUD_250K]);

    //
    // Take the CAN0 device out of INIT state.
    //
    CANEnable(CAN0_BASE);

    //
    // Enable interrups from CAN controller.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR);

    //
    // Set up the message object that will receive all messages on the CAN
    // bus.
    //
    CANConfigureNetwork();

    //
    // Enable interrupts for the CAN in the NVIC.
    //
    IntEnable(INT_CAN0);

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Configure SysTick for a 10ms interrupt.
    //
    SysTickPeriodSet(SysCtlClockGet() / 100);
    SysTickEnable();
    SysTickIntEnable();

    //
    // Initialize the button status.
    //
    g_ucButtonStatus = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Forground handling of interrupts.
        //
        ProcessInterrupts();

        //
        // Handle any incoming commands.
        //
        ProcessCmd();
    }
}

⌨️ 快捷键说明

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