微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32 CAN LoopBack 自测模式成功

stm32 CAN LoopBack 自测模式成功

时间:11-13 来源:互联网 点击:
移植了stm32 外设库里面的代码。

//CAN_Polling 成功 DS1亮

//CAN_Interrupt 成功 DS2亮

失败时候 分别对应 DS3 DS4亮
下面显示关键代码

main.c

===========================

int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();

/* CAN1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

/* NVIC Configuration */
NVIC_Configuration();

LED_Init();

/* CAN transmit at 100Kb/s and receive by polling in loopback mode */
TestRx = CAN_Polling();

if (TestRx == FAILED)
{
/* Turn on led DS3 */
GPIO_ResetBits(GPIOC, GPIO_Pin_11);
}
else
{
/* Turn on led DS1 */ //CAN_Polling 成功 DS1亮
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
}

/* CAN transmit at 500Kb/s and receive by interrupt in loopback mode */
TestRx = CAN_Interrupt();

if (TestRx == FAILED)
{
/* Turn on led DS4 */
GPIO_ResetBits(GPIOC, GPIO_Pin_10);
}
else
{
/* Turn on led DS2 */ //CAN_Interrupt 成功 DS2亮
GPIO_ResetBits(GPIOC, GPIO_Pin_12);
}

/* Infinite loop */
while (1)
{
}
}

/**
* @brief Configures the CAN, transmit and receive by polling
* @param None
* @retval 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;
uint32_t i = 0;
uint8_t TransmitMailbox = 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=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 */
}

/**
* @brief Configures the CAN, transmit and receive using interrupt.
* @param None
* @retval 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;
uint32_t 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);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top