CAN回环模式测试代码
#include "stm32f10x.h"
#include "delay.h"
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
vu32 ret; /* for return of the interrupt handling */
volatile TestStatus TestRx;
ErrorStatus HSEStartUpStatus;
TestStatus CAN_Polling(void);
TestStatus CAN_Interrupt(void);
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PE.02, PE.03, PE.04 and PE.05 LED as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure CAN pin: RX PB8*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure CAN pin: TX PB9*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE ); //重影射CAN IO脚到 PB8,PB9
GPIO_SetBits(GPIOE, GPIO_Pin_2);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
GPIO_SetBits(GPIOE, GPIO_Pin_4);
}
//系统中断管理
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
|RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
|RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
, ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
}
//配置所有外设
void Init_All_Periph(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
// USART1_Configuration();
}
int main(void)
{
Init_All_Periph();
/* CAN transmit at 100Kb/s and receive by polling in loopback mode*/
TestRx = CAN_Polling();
if (TestRx == FAILED)
{
/* Turn on led connected to PE.02 pin (LD2) */
GPIO_SetBits(GPIOE, GPIO_Pin_2);
}
else
{
/* Turn on led connected to PD.02 pin (LD2) */
GPIO_ResetBits(GPIOE, GPIO_Pin_2);
}
/* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/
TestRx = CAN_Interrupt();
if (TestRx == FAILED)
{
/* Turn on led connected to PE.04 pin (LD4) */
GPIO_SetBits(GPIOE, GPIO_Pin_4);
}
else
{
/* Turn on led connected to PE.04 pin (LD4) */
GPIO_ResetBits(GPIOE, GPIO_Pin_4);
}
while(1)
{
}
}
/*******************************************************************************
* Function Name : CAN_Polling
* Description : Configures the CAN and transmit and receive by polling
* Input : None
* Output : None
* Return : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
TestStatus CAN_Polling(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
u32 i = 0;
u8 TransmitMailbox;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* transmit */
TxMessage.StdId=0x11;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.IDE=CAN_ID_STD;
TxMessage.DLC=2;
TxMessage.Data[0]=0xCA;
TxMessage.Data[1]=0xFE;
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (i != 0xFF))
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF))
{
i++;
}
/* receive */
RxMessage.StdId=0x00;
RxMessage.IDE=CAN_ID_STD;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
if (RxMessage.StdId!=0x11) return FAILED;
if (RxMessage.IDE!=CAN_ID_STD) return FAILED;
if (RxMessage.DLC!=2) return FAILED;
if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE) return FAILED;
return PASSED; /* Test Passed */
}
/*******************************************************************************
* Function Name : CAN_Interrupt
* Description : Configures the CAN and transmit and receive by interruption
* Input : None
* Output : None
* Return : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
TestStatus CAN_Interrupt(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
u32 i = 0;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=1;
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
/* transmit 1 message */
TxMessage.StdId=0x00;
TxMessage.ExtId=0x1234;
TxMessage.IDE=CAN_ID_EXT;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=2;
TxMessage.Data[0]=0xDE;
TxMessage.Data[1]=0xCA;
CAN_Transmit(CAN1,&TxMessage);
/* initialize the value that will be returned */
ret = 0xFF;
/* receive message with interrupt handling */
i=0;
while((ret == 0xFF) && (i < 0xFFF))
{
i++;
}
if (i == 0xFFF)
{
ret=0;
}
/* disable interrupt handling */
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
return (TestStatus)ret;
}
/*******************************************************************************
* Function Name : USB_LP_CAN_RX0_IRQHandler
* Description : This function handles USB Low Priority or CAN RX0 interrupts
* requests.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
RxMessage.StdId=0x00;
RxMessage.ExtId=0x00;
RxMessage.IDE=0;
RxMessage.DLC=0;
RxMessage.FMI=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
if((RxMessage.ExtId==0x1234) && (RxMessage.IDE==CAN_ID_EXT)
&& (RxMessage.DLC==2) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA))
{
//设置测试点,观察允许状态
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
delay_nms(1000); // LED1闪烁一次,发送一个字符
GPIO_SetBits(GPIOE,GPIO_Pin_5);
delay_nms(1000);
ret = 1;
}
else
{
ret = 0;
}
}
下面简单谈谈这次调试CAN的一些经验和建议吧,等有时间在详细谈谈。
首先,在进入CAN的调试实验之前要阅读下相关的CAN的资料,当然不局限于STM32的数据手册,这样是为了便于从基础掌握CAN的通信协议及工作流程。
其次,详细的阅读STM32数据手册关于CAN的那一节,重点理解各个寄存器的配置,及各个数据位代表的含义;理解并掌握CAN的初始化配置,发送和接受配置以及过滤器的配置;掌握CAN的波特率的设置;掌握CAN中断服务程序的编写;掌握数据发送或接收状态下,各个寄存器内容的变换情况,最好利用JLINK单步调试以便观察。
再次,认真学习CAN的硬件电路配置情况,虽然咱们使用了开发板,但是为了能够让自己对CAN的认识达到更高一层,还是有必要进行系统的学习。个人认为,在第二步中已经较好的掌握了STM32的CAN控制器的话,可以着装看看CAN的总线收发器也就是驱动器,不要认为电路简单就忽略这部分的学习。要了解VP230(CAN驱动器)的硬件配置,包括各个引脚的功能,尤其是要注意Rs引脚的配置(本次DIY,关于这个问题,我想提个建议就是R51这个电阻不要焊接,我最初调试失败的原因估计十之八九是Rs引脚配置问题),工作在高速或者斜率模式。
最后,关于程序调试,当然要先从自发自收的测试模式开始,在能够完成自发自收后,然后进入双机通信,需要注意的是:双机通信测试时不能单独测试发送或者接收程序,只有当发送和接收程序都是正确且硬件上无误才能实现双机通信,任何一点错误都能造成通信的失败。可能各位DIY友由于硬件设备的条件有限,不可能具有示波器或者CAN检测仪。我建议关于这个问题,可以在程序的调试过程中加入很多检测程序(我这里用的是在关键代码部分加入LED闪烁语句,以便判断当然CAN的工作状态)。
暂时先更新这些,有时间继续谈谈个人看法,欢迎批评指正。
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |