This is an old revision of the document!


Simple CAN

TheControllerAreaNetwork(CAN)APIsprovideasetoffunctionsforaccessingtheStellarisCAN modules.FunctionsareprovidedtoconfiguretheCANcontrollers,configuremessageobjects,and manageCANinterrupts. TheStellarisCANmoduleprovideshardwareprocessingoftheCANdatalinklayer.Itcanbe configuredwithmessagefiltersandpreloadedmessagedatasothatitcanautonomouslysend andreceivemessagesonthebus,andnotifytheapplicationaccordingly.Itautomaticallyhandles generationandcheckingofCRCs,errorprocessing,andretransmissionofCANmessages. ThemessageobjectsarestoredintheCANcontrollerandprovidethemaininterfacefortheCAN moduleontheCANbus.Thereare32messageobjectsthatcaneachbeprogrammedtohandle aseparatemessageID,orcanbechainedtogetherforasequenceofframeswiththesameID. Themessageidentifierfiltersprovidemaskingthatcanbeprogrammedtomatchanyorallofthe messageIDbits,andframetypes. Thisdriveriscontainedin driverlib/can.c,with driverlib/can.h containingtheAPIdefi- nitionsforusebyapplications.

Practice

//*****************************************************************************
//
// CAN message receiving.
//
//*****************************************************************************
 
#include <inc/hw_ints.h>
#include <inc/hw_memmap.h>
#include <inc/hw_types.h>
#include <driverlib/sysctl.h>
#include <driverlib/can.h>
 
//*****************************************************************************
//
// Main function.
//
//*****************************************************************************
int main(void)
{	
	tCANMsgObject psMsg;
	unsigned char pucData[8];
 
	//
	// Set the clocking to run from the PLL at 50MHz
	//
	SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
	   SYSCTL_XTAL_8MHZ);
 
	//
	// Enable peripherals.
	//	
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);		
 
	//
	// Configure CAN pins.
	//
	GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);	
 
	//
	// Enable the CAN controller.
	//    
	CANInit(CAN0_BASE);
	CANBitRateSet(CAN0_BASE, SysCtlClockGet(), CAN_BITRATE);
	CANEnable(CAN0_BASE);	
 
	//
	// Create receive message object.
	//	
	psMsg.ulMsgID     = 0x01;
	psMsg.ulMsgIDMask = 0xFF;
	psMsg.ulFlags     = MSG_OBJ_NO_FLAGS;	
	psMsg.pucMsgData  = pucData;
 
	//
	// Set message object on box 1.
	//
	CANMessageSet(CAN0_BASE, 1, &psEchoMsg, MSG_OBJ_TYPE_RX);
 
	//
	// Loop forever.
	//
	while (1)
	{	
		//
		// CAN message received ?
		//
		if (CANStatusGet(CAN0_BASE, CAN_STS_NEWDAT) & 1)
		{
			//
			// Get the message.
			//
			CANMessageGet(CAN0_BASE, CAN_MSG_OBJ_RX, &psMsg, true);
 
			//
			// Access pucData here.
			// To get the length of the data read psMsg.ulMsgLen
			//
		}
	}
}
en/examples/can/arm.1268592684.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0