?? can_net.c
字號:
//*****************************************************************************
//
// can_net.c - This is the portion of the quick start application for CAN.
//
// 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_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_device_qs/can_common.h"
#include "can_net.h"
#include "audio.h"
//*****************************************************************************
//
// 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 transmit updates for the LED. This message object transmits a single
// byte that indicates the brightness level for the LED on the target board.
//
//*****************************************************************************
tCANMsgObject g_MsgObjectLED;
//*****************************************************************************
//
// This holds the information for the button receive message object. It is
// used to receive messages from the target board when button press and
// release events occur. There are two buttons and two events(press/release).
//
//*****************************************************************************
tCANMsgObject g_MsgObjectButton;
//*****************************************************************************
//
// 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_0 to receive data successfully.
//
//*****************************************************************************
#define MSGOBJ_ID_DATA_TX (MSGOBJ_ID_DATA_0)
//*****************************************************************************
//
// 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_1 to transmit data successfully.
//
//*****************************************************************************
#define MSGOBJ_ID_DATA_RX (MSGOBJ_ID_DATA_1)
//****************************************************************************
//
// 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 flag is used by the g_ulFlags global variable to indicate that a
// request to update the LED brightness is being transmitted. This flag will
// be cleared once the message has been sent.
//
//*****************************************************************************
#define FLAG_LED_TX_PEND 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
//*****************************************************************************
//
// This global holds the flags used to indicate the state of the message
// objects.
//
//*****************************************************************************
static volatile unsigned long g_ulFlags=0;
//*****************************************************************************
//
// This holds the constant that holds the firmware version for this
// application.
//
//*****************************************************************************
unsigned long const g_ulVersion = CURRENT_VERSION;
//*****************************************************************************
//
// This global is used by the button message object to store the events that
// are coming back from the target board.
//
//*****************************************************************************
static unsigned char g_pucButtonMsg[2];
//*****************************************************************************
//
// This value holds the current LED brightness level.
//
//*****************************************************************************
unsigned char g_ucLEDLevel=0;
//*****************************************************************************
//
// This function handles connection with the other CAN device and also
// handles incoming commands.
//
// /return None.
//
//*****************************************************************************
void
CANMain(void)
{
unsigned char pucData[8];
//
// The data has been received.
//
if((g_ulFlags & FLAG_DATA_RECV) == 0)
{
return;
}
//
// Read the data from the message object.
//
g_MsgObjectRx.pucMsgData = pucData;
g_MsgObjectRx.ulMsgLen = 8;
CANMessageGet(CAN0_BASE, MSGOBJ_NUM_DATA_RX, &g_MsgObjectRx, 1);
//
// Indicate that the data has been read.
//
g_ulFlags &= (~FLAG_DATA_RECV);
switch(g_MsgObjectRx.pucMsgData[0])
{
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);
}
}
//
// Clear the flag.
//
g_ulFlags &= ~(FLAG_DATA_RECV);
}
//*****************************************************************************
//
// This function sends a message to retrieve the firmware version from the
// target board.
//
//*****************************************************************************
int
CANGetTargetVersion(unsigned long *pulVersion)
{
static unsigned char ucVerCmd = CMD_GET_VERSION;
//
// If there was already a previous message being transmitted then just
// return.
//
if(g_ulFlags & FLAG_DATA_TX_PEND)
{
return(-1);
}
//
// A transmit request is about to be pending.
//
g_ulFlags |= FLAG_DATA_TX_PEND;
//
// Send the button update request.
//
g_MsgObjectTx.pucMsgData = &ucVerCmd;
g_MsgObjectTx.ulMsgLen = 1;
CANMessageSet(CAN0_BASE, MSGOBJ_NUM_DATA_TX, &g_MsgObjectTx,
MSG_OBJ_TYPE_TX);
//
// Wait for some data back from the target.
//
while ((g_ulFlags & FLAG_DATA_RECV) == 0)
{
}
//
// Read the data from the message object.
//
g_MsgObjectRx.pucMsgData = (unsigned char *)pulVersion;
g_MsgObjectRx.ulMsgLen = 4;
CANMessageGet(CAN0_BASE, MSGOBJ_NUM_DATA_RX, &g_MsgObjectRx, 1);
return(0);
}
//*****************************************************************************
//
// This function sends a message to set the current brightness for the LED on
// the target board.
//
//*****************************************************************************
void
CANUpdateTargetLED(unsigned char ucLevel, tBoolean bFlash)
{
//
// If there was already a previous message being transmitted then just
// return.
//
if(g_ulFlags & FLAG_LED_TX_PEND)
{
return;
}
//
// Set the global LED level.
//
g_ucLEDLevel = ucLevel;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -