📄 tms470r1b1m_can_02.c
字号:
//------------------------------------------------------------------------------
// tms470r1b1m_CAN_02.c - CAN Demo (Interrupt Version)
//
// Description: This demo code demonstrates the use of the High End CAN
// Controller 1 (HECC1) in an interrupt-driven version. Multiple CAN nodes
// running this code example can be wired together, and will all toggle their
// LEDs when the button of any node is pressed.
//
// On Button press, a message with the standard msg ID of 0x400 is send out and
// the LED is toggled. When a message with the ID of 0x400 is received,
// the LED status is set according to the data field of the incoming message.
// 11-bit standard identifier messages are used.
//
// SYSCLK = MCLK = ACLK = 8 x 7.3728MHz = 58.9824MHz
// ICLK = SYSCLK / 2 = 29.4912MHz
//
// //*An external 7.3728MHz XTAL with proper load caps is required*//
//
// TMS-FET470B1M
// +---------------+
// | OSCIN|-
// +--|PLLDIS | 7.3728MHz
// | | OSCOUT|-
// -+- | |
// | HET0|---> LED
// | GIOA4|<--- Button#1
// | |
// | CAN1HTX|---> CAN network, ~125kbit/s
// | CAN1HRX|<---
// | GIOA3|---> CAN transceiver enable
// | |
// +---------------+
//
// Andreas Dannenberg / John Mangino
// Texas Instruments Inc.
// July 29th 2005
// Built with IAR Embedded Workbench Version: 4.30A
//------------------------------------------------------------------------------
#include "intrinsic.h"
#include "iotms470r1b1m.h"
#include "tms470r1b1m_bit_definitions.h"
unsigned int LED_State;
void CAN_Init(void);
void HECC1B_irq_handler(void);
void GIOB_irq_handler(void);
void main(void)
{
PCR = CLKDIV_2; // ICLK = SYSCLK / 2
GCR = ZPLL_CLK_DIV_PRE_1; // SYSCLK = 8 x fOSC
PCR |= PENABLE; // Enable peripherals
HETDCLR = 0xffffffff; // Clear HET output latches
HETDIR = 0xffffffff; // Set HET as GIO outputs
GIODCLRA = 0x08; // Clear GIOA3 output latch to
// enable ext. CAN transceiver
GIODIRA = 0x08; // Set GIOA3 to output
GIOENA1 = A4; // Enable int for button 1
GIOPOL1 = 0x00; // Rising polarity
GIOPRY1 = 0x00; // Set to low priority (GIOB)
GIOFLG1 = 0x00; // Clear GIO int flag register
CAN_Init(); // Init High End CAN controller
LED_State = 0; // Init LED status var
REQMASK = (1 << CIM_GIOB) + (1 << CIM_HECC1B);// Enable GIOB/HECC1B int mask
__enable_interrupt(); // Enable interrupts
while (1) // Loop forever...
{
}
}
//------------------------------------------------------------------------------
// High End 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 CAN1HTX pin for the CAN transmit functions
HECC1CANTIOC = TXFUNC;
// Use CAN1HRX pin for the CAN receive functions
HECC1CANRIOC = RXFUNC;
// Set HECC1 interrupt configuration. Mailbox 0 (receive mailbox) generates
// low-priority interrupts (int line 1 --> CIM_HECC1B).
HECC1CANMIL = MIL0; // Use int line 1 for mbox 0
HECC1CANMIM = MIM0; // Enable int for mbox 0
HECC1CANGIM = I1EN; // Enable int line 1
// Setup master control register
// Enable configuration mode, activate auto bus on after bus off condition
HECC1CANMC = CCR + ABO;
// Wait until CPU has access to CAN configuration registers
while (!(HECC1CANES & CCE));
// Setup CAN bit timing for ~125kbit/s
// 8.07us nominal bit time w/ 17TQs/bit, sample point is located
// at 7.12us (15TQ)
// BRP = 13 (Prescaler 14, w/ ICLK = 29.4912MHz)
// Sample 3x, TSEG1 = 14, TSEG2 = 2, SJW = 1
HECC1CANBTC = (13 << 16) + SAM + TSEG1_14 + TSEG2_2 + SJW_1;
// Setup local acceptance mask LAM0
// Treat all incoming MID bits as significant
HECC1CANLAM0 = 0x00 << 18;
// Configure mailbox 0 for receive
HECC1CANMID0 = AME + 0x400 << 18; // Set ID for messages to RX,
HECC1CANMCF0 = 0x00; // uses acceptance mask LAM0
HECC1CANMDL0 = 0x00;
HECC1CANMDH0 = 0x00;
// Configure mailbox 1 for transmit
HECC1CANMID1 = 0x400 << 18; // Set ID for msgs to transmit
HECC1CANMCF1 = DLC_1; // Send one byte
HECC1CANMDL1 = 0x00;
HECC1CANMDH1 = 0x00;
HECC1CANMD = MD0; // Use mbox 0 for RX, 1 for TX
HECC1CANOPC = OPC0; // Protect against overwrite
HECC1CANME = ME1 + ME0; // Enable mailboxes 1 and 0
// Start CAN module
HECC1CANMC &= ~CCR;
// Wait until CAN module is started
while (HECC1CANES & CCE);
}
//------------------------------------------------------------------------------
// HECC1B 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 HECC1B_irq_handler(void)
{
if (HECC1CANGIF1 & GMIF1) // Msg received?
if ((HECC1CANGIF1 & 0xf) == MIV1_0) // Mailbox 0?
{
LED_State = HECC1CANMDL0 >> 24; // Read new state from CAN msg
if (LED_State) // Update LED
HETDSET = 0x01; // LED on
else
HETDCLR = 0x01; // LED off
HECC1CANRMP = RMP0; // Clear flag, new msg can be
} // received now
}
//------------------------------------------------------------------------------
// GIOB Interrupt Handler
//
// Checks if button 1 has caused the interrupt, toggles the LED status
// variable, and sends a CAN message out of mailbox 1 containing the new
// LED status. Then, the LED on the local board is updated.
//------------------------------------------------------------------------------
void GIOB_irq_handler(void)
{
if (GIOFLG1 & A4) // Was button 1 pressed?
{
LED_State ^= 0x01; // Toggle status var
HECC1CANME &= ~ME1; // Disable mailbox 1
HECC1CANMDL1 = LED_State << 24; // Update msg data byte D0
HECC1CANME |= ME1; // Re-enable mailbox 1
HECC1CANTRS = TRS1; // Send message 1
while (!(HECC1CANTA & TA1)); // Wait for transmission end
if (LED_State) // Update LED
HETDSET = 0x01; // LED on
else
HETDCLR = 0x01; // LED off
HECC1CANTA = TA1; // Clear TX ACK flag
while (HECC1CANTA & TA1); // Wait for flag to be cleared
for (volatile unsigned int i = 0; i < 800000; i++); // Button debounce
GIOFLG1 = A4; // Clear int flag
}
}
//------------------------------------------------------------------------------
// TMS470R1B1M Standard Interrupt Handler
//------------------------------------------------------------------------------
__irq __arm void irq_handler(void)
{
switch ((IRQIVEC & 0xff) - 1) // Convert IVEC index to
{ // CIM interrupt channel
case CIM_HECC1B : // HECC1 interrupt B
HECC1B_irq_handler();
break;
case CIM_GIOB : // GIO interrupt B
GIOB_irq_handler();
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -