/*********************************************************************//**
* @briefCAN_IRQ Handler, control receive message operation
* param[in]none
* @return none
**********************************************************************/
void CAN_IRQHandler()
{
uint8_t IntStatus;
IntStatus = CAN_IntGetStatus(LPC_CAN1);
//check receive interrupt
if((IntStatus >> 0) & 0x01)
{
TOGGLE_RX_LED;
}
}
int main(void) {
/* Pin configuration
* CAN1: select P0.21 as RD1. P0.22 as TD1 */
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 0;
PinCfg.Portnum = 21;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 22;
PINSEL_ConfigPin(&PinCfg);
//Initialize CAN1
CAN_Init(LPC_CAN1, 125000);
//Enable Interrupt
CAN_IRQCmd(LPC_CAN1, CANINT_RIE, ENABLE);
CAN_SetAFMode(LPC_CANAF,CAN_AccBP);
//Enable CAN Interrupt
NVIC_EnableIRQ(CAN_IRQn);
// Force the counter to be placed into memory
volatile static int i = 0 ;
// Enter an infinite loop, just incrementing a counter
while(1) {
i++ ;
}
return 0 ; |
//Transmission flag updated in timer0 interrupt uint8_t flag_tx = FALSE; |
void CAN_InitMessage(void)
{
TXMsg.format = [color=#f00]EXT_ID_FORMAT;[/color]
...
RXMsg.format =[color=#f00] 0x00;[/color]
...
}
|
if(flag_tx)
{
flag_tx = FALSE;
[color=#f00]CAN_InitMessage();[/color]
CAN_SendMsg(LPC_CAN1, &TXMsg);
}
|
/*
===============================================================================
Name : LPC17_CAN_Test.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#include "LPC17xx.h"
#include "lpc17xx_timer.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_can.h"
#include <string.h>
/** PINSEL configuration definition for CAN**/
PINSEL_CFG_Type CAN_PinCfg; //Parameters for pin configuration.
/** PINSEL configuration definition for GPIO**/
PINSEL_CFG_Type GPIO_PinCfg; //Parameters for pin configuration.
/** CAN variable definition **/
CAN_MSG_Type TXMsg, RXMsg; // messages for test Bypass mode
CAN_MSG_Type RspMsg; // Respond message
//CAN messages initialization.
void CAN_InitMessage(void);
//CAN interruption Handler.
void CAN_IRQHandler(void);
//CAN initialization.
void INIT_CAN(void);
volatile uint32_t timer;//systick timer
volatile uint8_t flag_tx = FALSE;//transmit flag
/*********************************************************************//**
* @briefCAN_IRQ Handler, control receive message operation
* param[in]none
* @return none
**********************************************************************/
void CAN_IRQHandler(void)
{
uint8_t IntStatus;
IntStatus = CAN_IntGetStatus(LPC_CAN1);
//check receive interrupt
if((IntStatus >> 0) & 0x01)
{
CAN_ReceiveMsg(LPC_CAN1,&RXMsg);
memcpy(&RspMsg,&RXMsg,sizeof(CAN_MSG_Type)); //copy respond message
RspMsg.id += 0x100;//Respond ID = receive + 0x100
CAN_SendMsg(LPC_CAN1, &RspMsg);//and send it...
}
}
//SysTick_Handler
void SysTick_Handler(void)
{
timer++;
if(timer >= 100)//reached tx delay
{
timer =0;//reset timer
flag_tx = TRUE;//set transmit flag
}//end reached tx delay
}
void INIT_CAN(void)
{
/* Pin configuration
* CAN1: select P0.0 as RD1. P0.1 as TD1 */
CAN_PinCfg.Funcnum = 1;
CAN_PinCfg.OpenDrain = 0;
CAN_PinCfg.Pinmode = 0;
CAN_PinCfg.Portnum = 0;
CAN_PinCfg.Pinnum = 0;
PINSEL_ConfigPin(&CAN_PinCfg);
CAN_PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&CAN_PinCfg);
//Initialize CAN1
CAN_Init(LPC_CAN1, 125000);
//Configures CAN AF mode
CAN_SetAFMode(LPC_CANAF,CAN_AccBP);
//Configures CAN mode.
CAN_ModeConfig(LPC_CAN1, CAN_OPERATING_MODE, ENABLE);
//Enable Interrupt
CAN_IRQCmd(LPC_CAN1, CANINT_RIE, ENABLE);
//Enable CAN Interrupt
NVIC_EnableIRQ(CAN_IRQn);
//Intialize CAN TX and RX messages.
CAN_InitMessage();
}
/*********************************************************************//**
* @briefInitialize transmit and receive message for Bypass operation
* @param[in]none
* @return none
**********************************************************************/
void CAN_InitMessage(void)
{
TXMsg.format = STD_ID_FORMAT;
TXMsg.id = 0x208;
TXMsg.len = 8;
TXMsg.type = DATA_FRAME;
TXMsg.dataA[0] = TXMsg.dataA[1] = TXMsg.dataA[2] = TXMsg.dataA[3] = 0x00000000;
TXMsg.dataB[0] = TXMsg.dataB[1] = TXMsg.dataB[2] = TXMsg.dataB[3] = 0x00000000;
RXMsg.format = STD_ID_FORMAT;
RXMsg.id = 0x00;
RXMsg.len = 0x00;
RXMsg.type = 0x00;
RXMsg.dataA[0] = RXMsg.dataA[1] = RXMsg.dataA[2] = RXMsg.dataA[3] = 0x00000000;
RXMsg.dataB[0] = RXMsg.dataB[1] = RXMsg.dataB[2] = RXMsg.dataB[3] = 0x00000000;
}
//Main Function//
int main(void)
{
INIT_CAN();
SysTick_Config(SystemCoreClock / 1000); //1ms timer
while(1)//loop
{
if(flag_tx)
{
flag_tx = FALSE;
TXMsg.dataA[1]++;//change message
CAN_SendMsg(LPC_CAN1, &TXMsg);
}
}
return 0 ;
}
|
//init CAN, [color=#f00]Pin muxing is done in board_sysinit.c already !!![/color]
void CAN_init(void)
{
[color=#f00]//if no PIN setup is done, it's time now...
// Chip_IOCON_PinMux(LPC_IOCON,0,0,IOCON_MODE_INACT,IOCON_FUNC1);// P0.0 CAN-RD1
// Chip_IOCON_PinMux(LPC_IOCON,0,1,IOCON_MODE_INACT,IOCON_FUNC1);// P0.1 CAN-TD1[/color]
Chip_CAN_Init(LPC_CAN, LPC_CANAF, LPC_CANAF_RAM);
Chip_CAN_SetBitRate(LPC_CAN, 100000);
Chip_CAN_EnableInt(LPC_CAN, CAN_IER_BITMASK);
Chip_CAN_SetAFMode(LPC_CANAF, CAN_AF_BYBASS_MODE);
NVIC_EnableIRQ(CAN_IRQn);
} |
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include "lpc17xx_timer.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_can.h"
/** PINSEL configuration definition for CAN**/
PINSEL_CFG_Type CAN_PinCfg; //Parameters for pin configuration.
/** PINSEL configuration definition for GPIO**/
PINSEL_CFG_Type GPIO_PinCfg; //Parameters for pin configuration.
/** CAN variable definition **/
CAN_MSG_Type TXMsg, RXMsg; // messages for test Bypass mode
//CAN messages initialization.
void CAN_InitMessage(void);
//CAN interruption Handler.
void CAN_IRQHandler(void);
//Timer 0 initialization.
void INIT_Timer_0(void);
//CAN initialization.
void INIT_CAN(void);
//Transmission flag updated in timer0 interrupt
uint8_t flag_tx = FALSE;
/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
* @briefTIMER0 interrupt handler sub-routine
* @param[in]None
* @return None
**********************************************************************/
void TIMER0_IRQHandler(void)
{
if (TIM_GetIntStatus(LPC_TIM0, TIM_MR0_INT)== SET)
{
flag_tx = TRUE;
}
TIM_ClearIntPending(LPC_TIM0, TIM_MR0_INT);
}
/*********************************************************************//**
* @briefCAN_IRQ Handler, control receive message operation
* param[in]none
* @return none
**********************************************************************/
void CAN_IRQHandler(void)
{
uint8_t IntStatus;
IntStatus = CAN_IntGetStatus(LPC_CAN1);
//check receive interrupt
if((IntStatus >> 0) & 0x01)
{
CAN_ReceiveMsg(LPC_CAN1,&RXMsg);
}
}
//Inicialização de Timer 0.
void INIT_Timer_0(void)
{
//timer init
TIM_TIMERCFG_Type TIM_ConfigStruct;
TIM_MATCHCFG_Type TIM_MatchConfigStruct ;
//PINSEL_CFG_Type PinCfg;
// Conifg P1.28 as MAT0.0
GPIO_PinCfg.Funcnum = 2;
GPIO_PinCfg.OpenDrain = 0;
GPIO_PinCfg.Pinmode = 0;
GPIO_PinCfg.Portnum = 3;
GPIO_PinCfg.Pinnum = 25;
PINSEL_ConfigPin(&GPIO_PinCfg);
// Initialize timer 0, prescale count time of 100uS
TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
TIM_ConfigStruct.PrescaleValue= 100;
// use channel 0, MR0
TIM_MatchConfigStruct.MatchChannel = 0;
// Enable interrupt when MR0 matches the value in TC register
TIM_MatchConfigStruct.IntOnMatch = TRUE;
//Enable reset on MR0: TIMER will reset if MR0 matches it
TIM_MatchConfigStruct.ResetOnMatch = TRUE;
//Stop on MR0 if MR0 matches it
TIM_MatchConfigStruct.StopOnMatch = FALSE;
//Toggle MR0.0 pin if MR0 matches it
TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE;
// Set Match value, count value of 1000 (10000 * 100uS = 1000000uS = 1S --> 1 Hz)
TIM_MatchConfigStruct.MatchValue = 10000;
// Set configuration for Tim_config and Tim_MatchConfig
TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);
/* preemption = 1, sub-priority = 1 */
NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));
/* Enable interrupt for timer 0 */
NVIC_EnableIRQ(TIMER0_IRQn);
// To start timer 0
TIM_Cmd(LPC_TIM0,ENABLE);
}
void INIT_CAN(void)
{
/* Pin configuration
* CAN1: select P0.21 as RD1. P0.22 as TD1 */
CAN_PinCfg.Funcnum = 3;
CAN_PinCfg.OpenDrain = 0;
CAN_PinCfg.Pinmode = 0;
CAN_PinCfg.Portnum = 0;
CAN_PinCfg.Pinnum = 21;
PINSEL_ConfigPin(&CAN_PinCfg);
CAN_PinCfg.Pinnum = 22;
PINSEL_ConfigPin(&CAN_PinCfg);
//Initialize CAN1
CAN_Init(LPC_CAN1, 125000);
//Configures CAN AF mode
CAN_SetAFMode(LPC_CANAF,CAN_AccBP);
//Configures CAN mode.
CAN_ModeConfig(LPC_CAN1, CAN_SELFTEST_MODE, ENABLE);
//Enable Interrupt
CAN_IRQCmd(LPC_CAN1, CANINT_RIE, ENABLE);
//Enable CAN Interrupt
NVIC_EnableIRQ(CAN_IRQn);
//Intialize CAN TX and RX messages.
CAN_InitMessage();
}
/*********************************************************************//**
* @briefInitialize transmit and receive message for Bypass operation
* @param[in]none
* @return none
**********************************************************************/
void CAN_InitMessage(void)
{
TXMsg.format = EXT_ID_FORMAT;
TXMsg.id = 0x208;
TXMsg.len = 8;
TXMsg.type = DATA_FRAME;
TXMsg.dataA[0] = TXMsg.dataA[1] = TXMsg.dataA[2] = TXMsg.dataA[3] = 0x00000000;
TXMsg.dataB[0] = TXMsg.dataB[1] = TXMsg.dataB[2] = TXMsg.dataB[3] = 0x00000000;
RXMsg.format = 0x00;
RXMsg.id = 0x00;
RXMsg.len = 0x00;
RXMsg.type = 0x00;
RXMsg.dataA[0] = RXMsg.dataA[1] = RXMsg.dataA[2] = RXMsg.dataA[3] = 0x00000000;
RXMsg.dataB[0] = RXMsg.dataB[1] = RXMsg.dataB[2] = RXMsg.dataB[3] = 0x00000000;
}
//Main Function//
int main(void) {
// TODO: insert code here
INIT_Timer_0();
INIT_CAN();
// Enter an infinite loop, sending a can message every timer 0 interruption (1 second).
while(1) {
if(flag_tx)
{
flag_tx = FALSE;
CAN_InitMessage();
CAN_SendMsg(LPC_CAN1, &TXMsg);
}
}
return 0 ;
} |
PinCfg.Pinnum = 0; PinCfg.Portnum = 21; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 22; PINSEL_ConfigPin(&PinCfg); |
void CAN_IRQHandler()
{
uint8_t IntStatus;
IntStatus = CAN_IntGetStatus(LPC_CAN1);
//check receive interrupt
if((IntStatus >> 0) & 0x01)
{
CAN_ReceiveMsg(LPC_CAN1,&RXMsg);
}
}
//CAN 1 Initialization.
void INIT_CAN(void)
{
/* Pin configuration
* CAN1: select P0.21 as RD1. P0.22 as TD1 */
PinCfg.Funcnum = 3;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 0;
PinCfg.Portnum = 21;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 22;
PINSEL_ConfigPin(&PinCfg);
//Initialize CAN1
CAN_Init(LPC_CAN1, 125000);
//Configures CAN operation mode.
CAN_ModeConfig(LPC_CAN1, CAN_SELFTEST_MODE, ENABLE);
//Enable Interrupt
CAN_IRQCmd(LPC_CAN1, CANINT_RIE, ENABLE);
CAN_SetAFMode(LPC_CANAF,CAN_AccBP);
//Enable CAN Interrupt
NVIC_EnableIRQ(CAN_IRQn);
CAN_InitMessage();
}
//Timer 0 Initialization.
void INIT_Timer_0(void)
{
//timer init
TIM_TIMERCFG_Type TIM_ConfigStruct;
TIM_MATCHCFG_Type TIM_MatchConfigStruct ;
//PINSEL_CFG_Type PinCfg;
// Conifg P1.28 as MAT0.0
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Portnum = 3;
PinCfg.Pinnum = 25;
PINSEL_ConfigPin(&PinCfg);
// Initialize timer 0, prescale count time of 100uS
TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
TIM_ConfigStruct.PrescaleValue= 100;
// use channel 0, MR0
TIM_MatchConfigStruct.MatchChannel = 0;
// Enable interrupt when MR0 matches the value in TC register
TIM_MatchConfigStruct.IntOnMatch = TRUE;
//Enable reset on MR0: TIMER will reset if MR0 matches it
TIM_MatchConfigStruct.ResetOnMatch = TRUE;
//Stop on MR0 if MR0 matches it
TIM_MatchConfigStruct.StopOnMatch = FALSE;
//Toggle MR0.0 pin if MR0 matches it
TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE;
// Set Match value, count value of 1000 (5000 * 100uS = 500000uS = 0,5S --> 2 Hz)
TIM_MatchConfigStruct.MatchValue = 5000;
// Set configuration for Tim_config and Tim_MatchConfig
TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);
/* preemption = 1, sub-priority = 1 */
NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));
/* Enable interrupt for timer 0 */
NVIC_EnableIRQ(TIMER0_IRQn);
// To start timer 0
TIM_Cmd(LPC_TIM0,ENABLE);
}
void CAN_InitMessage(void)
{
TXMsg.format = EXT_ID_FORMAT;
TXMsg.id = 0x208;
TXMsg.len = 8;
TXMsg.type = DATA_FRAME;
TXMsg.dataA[0] = TXMsg.dataA[1] = TXMsg.dataA[2] = TXMsg.dataA[3] = 0x00000000;
TXMsg.dataB[0] = TXMsg.dataB[1] = TXMsg.dataB[2] = TXMsg.dataB[3] = 0x00000000;
RXMsg.format = 0x00;
RXMsg.id = 0x00;
RXMsg.len = 0x00;
RXMsg.type = 0x00;
RXMsg.dataA[0] = RXMsg.dataA[1] = RXMsg.dataA[2] = RXMsg.dataA[3] = 0x00000000;
RXMsg.dataB[0] = RXMsg.dataB[1] = RXMsg.dataB[2] = RXMsg.dataB[3] = 0x00000000;
}
int main(void) {
INIT_Timer_0();
INIT_CAN();
// Force the counter to be placed into memory
volatile static int i = 0 ;
// Enter an infinite loop, just incrementing a counter
while(1) {
i++ ;
}
return 0 ;
}
|