?? tms470r1a256_can_03.c
字號:
//------------------------------------------------------------------------------
// tms470r1a256_CAN_03.c - CAN Monitor Demo
//
// Description: This demo code serves as a simple CAN bus monitor. It uses the
// standard CAN controller (SCC) to receive any incoming CAN messages (with
// both 11-bit and 29-bit identifiers) using interrupts. These messages are
// decoded and output as text to the SCI1 module for display in a terminal
// program such as HyperTerm.
//
// TMS470R1A256
// /|\ +---------------+
// | | |
// +--|PLLDIS GIOA2|<--- Button#1
// | |
// | HET0|---> LED
// | |
// | XIN/XOUT|<--- 12MHz crystal
// | |
// | CANSTX|---> CAN network, 125kbit/s
// | CANSRX|<---
// | GIOB1|---> CAN transceiver enable
// | |
// +---------------+
//
// Andreas Dannenberg / John Mangino
// Texas Instruments Inc.
// April 12th 2005
// Built with IAR Embedded Workbench Version: 4.11A
//------------------------------------------------------------------------------
#include "stdio.h"
#include "yfuns.h" // Customize standard I/O
#include "intrinsic.h" // ARM7 specific intrinsics
#include "iotms470r1a256.h" // TMS470 register definitions
#include "tms470r1a256_bit_definitions.h"
void SCI_Init(void);
void CAN_Init(void);
void SCCB_irq_handler(void);
void main(void)
{
PCR = CLKDIV_1; // ICLK = SYSCLK = 12MHz
PCR |= PENABLE; // Enable peripherals
HETDIR = 0x813c3dd5; // Set HET as GIO outputs
HETDOUT = 0x813c3dd5; // All LEDs off
GIODIRB = X1; // Set GIOB1 to output
GIODCLRB = X1; // Output 0 on GIOB1 to enable
// external CAN transceiver
SCI_Init(); // Init SCI module
CAN_Init(); // Init standard CAN controller
REQMASK = (1 << CIM_SCCB); // Enable SCCB int mask
printf("\r\n\r\n *** TMS470 CAN Monitor Demo ***");
printf("\r\n ID RTR DLC D0 D1 D2 D3 D4 D5 D6 D7");
__enable_interrupt(); // Enable interrupts
while (1) // Loop forever...
{
}
}
//------------------------------------------------------------------------------
// SCI1 Initialization
//
// Configures the SCI1 UART module for transmit operation using a data
// rate of 115,200bps.
//------------------------------------------------------------------------------
void SCI_Init(void)
{
SCI1CTL3 &= ~SW_NRESET; // Reset SCI1 state machine
SCI1CCR = TIMING_MODE_ASYNC + CHAR_8; // Async, 8-bit Char
SCI1CTL2 |= TXENA; // TX enabled
SCI1CTL3 |= CLOCK; // Internal clock
SCI1LBAUD = 12; // 12MHz/8/115,200-1=12
SCI1PC3 |= TX_FUNC; // SCITX is the SCI transmit pin
SCI1CTL3 |= SW_NRESET; // Configure SCI1 state machine
}
//------------------------------------------------------------------------------
// Standard CAN Controller Initialization
//
// Sets up the CAN controller for operation at 125kbit/s. Two mailboxes are
// initialized for receiving (mailbox 0) and transmitting (mailbox 1) messages
// with a standard 11-bit ID of 0x400. Interrupts are enabled to indicate
// incoming messages.
//------------------------------------------------------------------------------
void CAN_Init(void)
{
// Use CANTX pin for the CAN transmit functions
CAN1TIOC = TXFUNC;
// Use CANRX pin for the CAN receive functions
CAN1RIOC = RXFUNC;
// Set SCC interrupt configuration. Mailbox 0 (receive mailbox) generates
// low-priority interrupts (int line 1 --> CIM_SCCB).
CAN1MIL = MIL0; // Use int line 1 for mbox 0
CAN1MIM = MIM0; // Enable int for mbox 0
CAN1GIM = I1EN; // Enable int line 1
// Setup master control register
// Enable configuration mode, activate auto bus on after bus off condition
CAN1MC = CCR + ABO;
// Wait until CPU has access to CAN configuration registers
while (!(CAN1ES & CCE));
// Setup CAN bit timing for 125kbit/s according to CiA specifications:
// 8us nominal bit time w/ 16TQs/bit, sample point is located at 7us (14TQ)
// BRP = 5 (Prescaler 6, w/ ICLK = 12MHz)
// Sample 3x, TSEG1 = 13, TSEG2 = 2, SJW = 1
CAN1BTC = (5 << 16) + SAM + TSEG1_13 + TSEG2_2 + SJW_1;
// Setup local acceptance mask LAM0
// Receive any incoming standard/extended ID message
CAN1LAM0 = LAMI + 0x1fffffff;
// Configure mailbox 0 for receive
CAN1MID0 = AME; // Use acceptance mask LAM0
CAN1MCF0 = 0x00;
CAN1MDL0 = 0x00;
CAN1MDH0 = 0x00;
CAN1MD = MD0; // Use mbox 0 for RX
CAN1OPC = OPC0; // Protect against overwrite
CAN1ME = ME0; // Enable mailbox 0
// Start CAN module
CAN1MC &= ~CCR;
// Wait until CAN module is started
while (CAN1ES & CCE);
}
//------------------------------------------------------------------------------
// SCI1 UART based character output function to be used by __write().
//------------------------------------------------------------------------------
int MyLowLevelPutchar(int x)
{
while (!(SCI1CTL2 & TXRDY)); // TX buffer ready?
SCI1TXBUF = x; // Transmit character
return x; // Indicate "no error"
}
//------------------------------------------------------------------------------
// Provide custom low-level __write() function to be used for console I/O.
// This function is based on the IAR template file <...>\arm\src\lib\write.c
//------------------------------------------------------------------------------
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
size_t nChars = 0;
if (buffer == 0)
{
/*
* This means that we should flush internal buffers. Since we
* don't we just return. (Remember, "handle" == -1 means that all
* handles should be flushed.)
*/
return 0;
}
/* This template only writes to "standard out" and "standard err",
* for all other file handles it returns failure.
*/
if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
{
return _LLIO_ERROR;
}
for (/* Empty */; size != 0; --size)
{
if (MyLowLevelPutchar(*buffer++) < 0)
{
return _LLIO_ERROR;
}
++nChars;
}
return nChars;
}
//------------------------------------------------------------------------------
// SCCB Interrupt Handler
//
// Checks if a new CAN message was received into mailbox 0, and adjusts the
// LED status variable and the actual LED according to the message contents.
//------------------------------------------------------------------------------
void SCCB_irq_handler(void)
{
if (CAN1GIF1 & GMIF1) // Msg received?
if ((CAN1GIF1 & 0xf) == MIV1_0) // Mailbox 0?
{
if (CAN1MID0 & IDE) // Extended identifier?
printf("\r\n%08x", CAN1MID0 & 0x1fffffff); // Print ext. ID
else
printf("\r\n %03x", (CAN1MID0 >> 18) & 0x7ff); // Print std. ID
if (CAN1MCF0 & RTR) // Remote TX request?
printf(" RTR"); // Yes
else
printf(" "); // No
unsigned int DLC_Temp = CAN1MCF0 & 0x7; // Get DLC field
printf(" %02x", DLC_Temp); // Print DLC
if (DLC_Temp--) { printf(" %02x", CAN1MDL0 >> 24);
if (DLC_Temp--) { printf(" %02x", (CAN1MDL0 >> 16) & 0xff);
if (DLC_Temp--) { printf(" %02x", (CAN1MDL0 >> 8) & 0xff);
if (DLC_Temp--) { printf(" %02x", CAN1MDL0 & 0xff);
if (DLC_Temp--) { printf(" %02x", CAN1MDH0 >> 24);
if (DLC_Temp--) { printf(" %02x", (CAN1MDH0 >> 16) & 0xff);
if (DLC_Temp--) { printf(" %02x", (CAN1MDH0 >> 8) & 0xff);
if (DLC_Temp) printf(" %02x", CAN1MDH0 & 0xff);
}}}}}}}
CAN1RMP = RMP0; // Clear flag, new msg can be
} // received now
}
//------------------------------------------------------------------------------
// TMS470R1A256 Standard Interrupt Handler
//------------------------------------------------------------------------------
#pragma vector = IRQV
__irq __arm void irq_handler(void)
{
switch ((IRQIVEC & 0xff) - 1) // Convert IVEC index to
{ // CIM interrupt channel
case CIM_SCCB : // SCC interrupt B
SCCB_irq_handler();
break;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -