下面是我的程序,希望香主帮我看看那里有问题。 int main(void) {
#ifdef DEBUG debug(); #endif
/* System Clocks Configuration */ RCC_Configuration();
/* NVIC Configuration */ NVIC_Configuration(); /* GPIO ports pins Configuration */ GPIO_Configuration(); while (1) { /* CAN transmit at 100Kb/s and receive by polling in loopback mode*/ TestRx = CAN_Polling();
if (TestRx == FAILED) { /* Turn on led connected to PC.06 pin (LD3) */ GPIO_SetBits(GPIOC, GPIO_Pin_6); } else { /* Turn on led connected to PC.04 pin (LD5) */ GPIO_SetBits(GPIOC, GPIO_Pin_4); }
/* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/ TestRx = CAN_Interrupt();
if (TestRx == FAILED) { /* Turn on led connected to PC.05 pin (LD4) */ GPIO_SetBits(GPIOC, GPIO_Pin_5); } else { /* Turn on led connected to PC.07 pin (LD2) */ GPIO_SetBits(GPIOC, GPIO_Pin_7); } } while (1) { } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* Select HSE as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Wait till HSE is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x04) { } } /* GPIOA and GPIOC clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC |RCC_APB2Periph_AFIO , ENABLE);
/* CAN Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 |RCC_APB1Periph_CAN, ENABLE); }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure CAN pin: RX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure CAN pin: TX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE); }
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures the NVIC and Vector Table base address. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#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_CAN_RX0_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/******************************************************************************* * 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;
GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE);
/* CAN register init */ CAN_DeInit(); 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_Normal; 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(&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=CAN_FIFO0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); CAN_WakeUp(); /* transmit */ CAN->MCR |= 0x00000000; TxMessage.StdId=0x0010; TxMessage.RTR=CAN_RTR_DATA; TxMessage.IDE=CAN_ID_STD; TxMessage.DLC=8; TxMessage.Data[0]=0xCA; TxMessage.Data[1]=0xFE; TxMessage.Data[2]=0xCA; TxMessage.Data[3]=0xCA; TxMessage.Data[4]=0xCA; TxMessage.Data[5]=0xCA; TxMessage.Data[6]=0xCA; TxMessage.Data[7]=0xCA; TransmitMailbox=CAN_Transmit(&TxMessage); CAN->sTxMailBox[TransmitMailbox].TIR |= 0x00000001; i = 0; while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF)) { i++; }
i = 0; while((CAN_MessagePending(CAN_FIFO0) < 1) && (i != 0xFF)) { i++; }
/* receive */ RxMessage.StdId=0x0001; RxMessage.IDE=CAN_ID_STD; RxMessage.DLC=2; RxMessage.Data[0]=0x00; RxMessage.Data[1]=0x00; CAN_Receive(CAN_FIFO0, &RxMessage);
//if (RxMessage.StdId!=0x01) 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(); 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_Normal; 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(&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(CAN_IT_FMP0, ENABLE);
/* transmit 1 message */ TxMessage.StdId=0x12; TxMessage.ExtId=0x34; TxMessage.IDE=CAN_ID_EXT; TxMessage.RTR=CAN_RTR_DATA; TxMessage.DLC=2; TxMessage.Data[0]=0xDE; TxMessage.Data[1]=0xCA; CAN_Transmit(&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(CAN_IT_FMP0, DISABLE);
return (TestStatus)ret; }
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert_param error has occurred. * Input : - file: pointer to the source file name * - line: assert_param error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) { } } #endif /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/ |