📄 can_device_qs.c
字号:
//*****************************************************************************
//
// can_device_qs.c - Target application that runs on the CAN device board and
// uses the CAN controller to communicate with the main
// board.
//
// Copyright (c) 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. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. 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 1952 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************
#include "../../../hw_types.h"
#include "../../../hw_sysctl.h"
#include "../../../hw_memmap.h"
#include "../../../hw_ints.h"
#include "../../../src/debug.h"
#include "../../../src/interrupt.h"
#include "../../../src/sysctl.h"
#include "../../../src/systick.h"
#include "../../../src/gpio.h"
#include "../../../utils/diag.h"
#include "../../../src/can.h"
#include "can_common.h"
//*****************************************************************************
//
//! \addtogroup ek_lm3s2965_list
//! <h1>CAN Device Board Quickstart Application (can_device_qs)</h1>
//!
//! This application uses the CAN controller to communicate with the evaluation
//! board that is running the example game. It receives messages over CAN to
//! turn on, turn off, or to pulse the LED on the device board. It also sends
//! CAN messages when either of the up and down buttons are pressed or
//! released.
//
//*****************************************************************************
//*****************************************************************************
//
// This is the message identifier used to receive data from the host
// application board. The host application must use the message identifier
// specified by MSGOBJ_ID_DATA_0 to transmit data successfully.
//
//*****************************************************************************
#define MSGOBJ_ID_DATA_RX (MSGOBJ_ID_DATA_0)
//*****************************************************************************
//
// This is the message identifier used to transmit data to the host
// application board. The host application must use the message identifier
// specified by MSGOBJ_ID_DATA_1 to receive data successfully.
//
//*****************************************************************************
#define MSGOBJ_ID_DATA_TX (MSGOBJ_ID_DATA_1)
//*****************************************************************************
//
// These are used by the button message object to send button events to the
// main board.
//
//*****************************************************************************
#define MSG_OBJ_B0_PRESSED 0x01
#define MSG_OBJ_B0_RELEASED 0x02
#define MSG_OBJ_B1_PRESSED 0x04
#define MSG_OBJ_B1_RELEASED 0x08
//****************************************************************************
//
// This is the message object number used by the Button message object.
//
//*****************************************************************************
#define MSGOBJ_NUM_BUTTON 1
//*****************************************************************************
//
// This is the message object number used by the LED message object.
//
//*****************************************************************************
#define MSGOBJ_NUM_LED 2
//*****************************************************************************
//
// This is the message object number used to transfer data.
//
//*****************************************************************************
#define MSGOBJ_NUM_DATA_TX 3
//*****************************************************************************
//
// This is the message object number used to receive data.
//
//*****************************************************************************
#define MSGOBJ_NUM_DATA_RX 4
//*****************************************************************************
//
// This holds the information for the data receive message object that is used
// to receive commands.
//
//*****************************************************************************
tCANMsgObject g_MsgObjectRx;
//*****************************************************************************
//
// This holds the information for the data send message object that is used
// to send commands and to send command responses.
//
//*****************************************************************************
tCANMsgObject g_MsgObjectTx;
//*****************************************************************************
//
// This holds the information for the LED message object that is used
// to receive updates for the LED. This message object receives a single
// byte that indicates the brightness level for the LED.
//
//*****************************************************************************
tCANMsgObject g_MsgObjectLED;
//*****************************************************************************
//
// This holds the information for the button request message object. It is
// used to transmit the current status of the buttons on the target board.
// It does this by sending a single byte containing the bitmask of the
// buttons.
//
//*****************************************************************************
tCANMsgObject g_MsgObjectButton;
//*****************************************************************************
//
// The counter of the number of consecutive times that the buttons have
// remained constant.
//
//*****************************************************************************
static unsigned long g_ulDebounceCounter;
//*****************************************************************************
//
// The counter value used to turn off the led after receiving a command to
// pulse the LED.
//
//*****************************************************************************
static long g_lFlashCounter = -1;
//*****************************************************************************
//
// This variable holds the last stable raw button status.
//
//*****************************************************************************
volatile unsigned char g_ucButtonStatus;
//*****************************************************************************
//
// This variable holds flags indicating which buttons have been pressed and
// released.
//
//*****************************************************************************
volatile unsigned char g_ucButtonFlags = 0;
//*****************************************************************************
//
// This used to hold the message data for the button message object.
//
//*****************************************************************************
unsigned char g_pucButtonMsg[2];
//*****************************************************************************
//
// This value holds the current LED brightness level.
//
//*****************************************************************************
unsigned char g_ucLEDLevel = 0;
//*****************************************************************************
//
// This holds the constant that holds the firmware version for this
// application.
//
//*****************************************************************************
static unsigned long const g_ulVersion = CURRENT_VERSION;
//*****************************************************************************
//
// This global holds the flags used to indicate the state of the message
// objects.
//
//*****************************************************************************
static volatile unsigned long g_ulFlags = 0;
//*****************************************************************************
//
// This flag is used by the g_ulFlags global variable to indicate that a
// button response transmission is pending an that no more responses can be
// sent until this flag clears. This flag will be cleared by the interrupt
// handler when the tramission has completed.
//
//*****************************************************************************
#define FLAG_BUTTON_PEND 0x00000001
//*****************************************************************************
//
// This flag is used by the g_ulFlags global variable to indicate that a
// request to update the LED brightness has been received and has been read
// into the g_MsgObjectLED structure. This flag will be cleared once the
// brightness has been updated.
//
//*****************************************************************************
#define FLAG_UPDATE_LED 0x00000002
//*****************************************************************************
//
// This flag is used by the g_ulFlags global variable to indicate that a
// data transmission is in process and that no further commands or responses
// can be sent until this flag is cleared. This flag will be cleared by the
// interrupt handler when the tramission has completed.
//
//*****************************************************************************
#define FLAG_DATA_TX_PEND 0x00000004
//*****************************************************************************
//
// This flag is used by the g_ulFlags global variable to indicate that data
// has been received and ready to be read. The data may either be a command
// or response to a command. This flag will be cleared once the data has
// been processed.
//
//*****************************************************************************
#define FLAG_DATA_RECV 0x00000008
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// This is the interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
unsigned long ulStatus;
unsigned char ucTemp;
static unsigned long ulLastStatus = 0;
//
// Read the current value of the button pins.
//
ulStatus = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);
if(ulStatus != ulLastStatus)
{
//
// Something changed reset the compare and reset the debounce counter.
//
ulLastStatus = ulStatus;
g_ulDebounceCounter = 0;
}
else
{
//
// Increment the number of counts with the push button in a different
// state.
//
g_ulDebounceCounter++;
//
// If four consecutive counts have had the push button in a different
// state then the state has changed.
//
if(g_ulDebounceCounter == 4)
{
//
// XOR to see what has changed.
//
ucTemp = g_ucButtonStatus ^ ulStatus;
//
// If something changed on button 0 then see what changed.
//
if(ucTemp & GPIO_PIN_0)
{
if(GPIO_PIN_0 & ulStatus)
{
//
// Button released.
//
g_ucButtonFlags |= MSG_OBJ_B0_RELEASED;
}
else
{
//
// Button pressed.
//
g_ucButtonFlags |= MSG_OBJ_B0_PRESSED;
}
}
//
// If something changed on button 1 then see what changed.
//
if(ucTemp & GPIO_PIN_1)
{
if(GPIO_PIN_1 & ulStatus)
{
//
// Button released.
//
g_ucButtonFlags |= MSG_OBJ_B1_RELEASED;
}
else
{
//
// Button pressed.
//
g_ucButtonFlags |= MSG_OBJ_B1_PRESSED;
}
}
//
// Save the new stable state for comparison.
//
g_ucButtonStatus = (unsigned char)ulStatus;
}
}
//
// Clear the LED if it is time.
//
if(g_lFlashCounter == 0)
{
//
// Turn off LED.
//
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
}
//
// Let this go below just below zero so that the LED is not repeatedly
// turned off.
//
if(g_lFlashCounter >= 0)
{
g_lFlashCounter--;
}
}
//*****************************************************************************
//
// The CAN controller interrupt handler.
//
// /return None.
//
//*****************************************************************************
void
CANHandler(void)
{
unsigned long ulStatus;
//
// Find the cause of the interrupt, if it is a status interrupt then just
// acknowledge the interrupt by reading the status register.
//
ulStatus = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE);
switch(ulStatus)
{
//
// Let the forground loop handle sending this, just set a flag to
// indicate that the data should be sent.
//
case MSGOBJ_NUM_BUTTON:
{
//
// Indicate a pending button transmission is complete.
//
g_ulFlags &= (~FLAG_BUTTON_PEND);
break;
}
case MSGOBJ_NUM_LED:
{
//
// Read the new LED level and let the foreground handle it.
//
CANMessageGet(CAN0_BASE, MSGOBJ_NUM_LED, &g_MsgObjectLED, 1);
//
// Limit the LED Level to MAX_LED_BRIGHTNESS.
//
if((g_ucLEDLevel & LED_FLASH_VALUE_MASK) > MAX_LED_BRIGHTNESS)
{
g_ucLEDLevel = MAX_LED_BRIGHTNESS |
(g_ucLEDLevel & LED_FLASH_ONCE);
}
//
// Indicate that the LED needs to be updated.
//
g_ulFlags |= FLAG_UPDATE_LED;
break;
}
//
// The data transmit message object has been sent successfully.
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -