// CAN IO configuration ------------------------------------// // Enable GPIOB & AFEN(Alternate Function) clock // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); // remap(Remap1_CAN) CAN to PB8, PB9 as CAN_RX, CAN_TX - to avoid v glitch on CAN_TX pin GPIO_PinRemapConfig(GPIO_Remap1_CAN, ENABLE);// Remap1 of CAN
// Set PB8 as floating IN - CAN_RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure);
// Set PB9 as PP AF first - CAN_TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure);
// Get clock prescaler for CAN RCC_GetClocksFreq(&RCC_ClocksStatus); // Get the multipier for APB1_CLK - based on 8M Hz prescaler = RCC_ClocksStatus.PCLK1_Frequency/8000000; // CAN configuration --------------------------------------// // CAN configured as follow: // - BaudRate = 100k baud // - CAN_TTCM disabled // - CAN_ABOM disabled // - CAN_AWUM disabled // - CAN_NART disabled // - CAN_RFLM disabled // - CAN_TXFP disabled // - Normal Mode RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
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();
/******************************************************************************* * 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
/******************************************************************************* * 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);
/******************************************************************************* * 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);
#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****/