#include "stm32f10x.h"
#include "stdio.h"
void LED_Configuration(void);
void USART_Configuration(void);
typedef enum{FAIL=0,PASS=1} TestStatus;
TestStatus RxStatus;
extern TestStatus CAN_Configuration(void);
#define LED1_ON GPIO_SetBits(GPIOB,GPIO_Pin_5);
#define LED1_OFF GPIO_ResetBits(GPIOB,GPIO_Pin_5);
int main(void)
{
LED_Configuration();
USART_Configuration();
printf("\r\n This is a LoopBackr test r\n");
CAN_Configuration();
printf("\r\n Can init success r\n");
RxStatus=CAN_Configuration();
while(1)
{
if(RxStatus==1)
{
LED1_ON;
}
else
{
LED1_OFF;
}
}
}
void LED_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_Odd;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
TestStatus CAN_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
uint8_t TransmitMailbox=0;
u32 i;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
/****** CAN ×ÜÏßÒý½ÅµÄÅäÖÃ******/
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
/****** CAN ×ÜÏßÖжϵÄÅäÖÃ******/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
/****** CAN ×ÜÏßµÄÅäÖÃ******/
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
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; //½ÓÊÕFIFOËø¶¨Ä£Ê½
CAN_InitStructure.CAN_TXFP=DISABLE; //·¢ËÍFIFOÓÅÏȼ¶
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; //»·»Øģʽ£¬×Ô¶¯ÊÕ·¢
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //ͬ²½ÌøÔ¾1¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_BS1=CAN_BS1_6tq; //ʱ¼ä¶Î1Ϊ6¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //ʱ¼ä¶Î2Ϊ3¸öʱ¼äµ¥Ôª
CAN_InitStructure.CAN_Prescaler=4; //·ÖƵֵ
CAN_Init(CAN1,&CAN_InitStructure);
/****** CAN ×ÜÏß¹ýÂËÆ÷µÄÅäÖÃ******/
CAN_FilterInitStructure.CAN_FilterNumber=0; //Ö¸¶¨³õʼ»¯µÄ¹ýÂËÆ÷±àºÅΪ0 (0-13)
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //±êʶ·ûÆÁ±ÎΪģʽ
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //1¸ö32λµÄ¹ýÂËÆ÷
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; //É趨¹ýÂËÆ÷µÄ±êʶ·û(32λΪÆä¸ß¶Î£¬16λΪµÚÒ»¸ö)·¶Î§ÊÇ0x0000-0xffff
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; //É趨¹ýÂËÆ÷µÄ±êʶ·û(32λΪÆäµÍ¶Î£¬16λΪµÚ¶þ¸ö)·¶Î§ÊÇ0x0000-0xffff
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; //É趨¹ýÂËÆ÷ÆÁ±Î±êʶ·û»òÕß¹ýÂ˱êʶ·û
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; //É趨¹ýÂËÆ÷ÆÁ±Î±êʶ·û»òÕß¹ýÂ˱êʶ·û
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FilterFIFO0; //¹ýÂËÆ÷FIFOÖ¸Ïò¹ýÂËÆ÷0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //ʹÄܹýÂËÆ÷
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
/****** CAN ×ÜÏß·¢Ë͵ÄÅäÖÃ******/
TxMessage.StdId=0x11; //±ê×¼±êʶ·û
TxMessage.IDE=CAN_Id_Standard; //ʹÓñê×¼±êʶ·û
TxMessage.RTR=CAN_RTR_Data; //Êý¾ÝÖ¡
TxMessage.DLC=2; //´«ÊäÏûÏ¢µÄÖ¡³¤¶ÈΪ2
TxMessage.Data[0]=0xAB; //Êý¾Ý1
TxMessage.Data[1]=0xCD; //Êý¾Ý2
CAN_Transmit(CAN1,&TxMessage);
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++;
}
/****** CAN ×ÜÏß½ÓÊÕµÄÅäÖÃ******/
RxMessage.StdId=0;
RxMessage.IDE=CAN_Id_Standard;
RxMessage.DLC=2;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
if((RxMessage.StdId!=0x11)&&(RxMessage.IDE!=CAN_Id_Standard)&&(RxMessage.DLC!=2)&&((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xABCD))
{
return FAIL;
}
else
{
return PASS;
}
}
|