2014-08-21 11:08 AM
Hi,
I am trying to use CANbus on STM32F303VC6. I get it working in loopback mode. But the message is not getting sent in Normal mode. I have turned on SB21 and SB22 to connect the ports PA11 and PA12 to P2. When I debug the code, I find it getting stuck in CAN_TransmitStatus() function due to pending message. Please point out what might be the problem. #include ''stm32f30x.h'' #include ''stm32f3_discovery.h'' #include ''stm32f30x_it.h'' #include ''stm32f30x_rcc.h'' #include ''stm32f30x_can.h'' #include ''stm32f30x_gpio.h'' #include ''stm32f30x_misc.h'' #include ''CAN.h'' void Init_TxMes(CanTxMsg *TxMessage){ /* Transmit INITIALIZATION*/ TxMessage->IDE = CAN_ID_STD; TxMessage->DLC = 2; TxMessage->StdId = 0x321; TxMessage->RTR = CAN_RTR_DATA; } void Init_RxMes(CanRxMsg *RxMessage) { uint8_t i = 0; RxMessage->StdId = 0; RxMessage->IDE = CAN_ID_STD; RxMessage->DLC = 0; RxMessage->FMI = 0; for (i = 0; i < 8; i++) { RxMessage->Data[i] = 0; } } void CAN_setup(void) { NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //Enable the clock for CAN RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource11 | GPIO_PinSource12 , GPIO_AF_9); /* Configure CAN1 RX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure CAN1 TX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the CAN global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn; NVIC_Init(&NVIC_InitStructure); CAN_DeInit(CAN1); /* Configure CAN1 **************************************************/ /* Struct init*/ 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; CAN_InitStructure.CAN_TXFP = ENABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; CAN_InitStructure.CAN_Prescaler =6; /*Initializes the CAN1 */ CAN_Init(CAN1,&CAN_InitStructure); /*CAN1 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 = 0x6420; 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); } int main (void) { int i; uint8_t TransmitMailbox = 0; CAN_setup (); // setup CAN interface Init_TxMes(&TxMessage); while(1){ TxMessage.Data[0] = 0xAB; TxMessage.Data[1] = 0xCD; TransmitMailbox = CAN_Transmit(CAN1,&TxMessage); i = 0; while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF)) // I put a breakpoint here to check and found the message is pending i++; }2014-08-21 11:56 AM
And you have this connected to a transceiver, and another CAN receiver? ...
2014-08-22 01:39 AM
Yes. I have it connected to a bus sniffer, PCAN-View.
2014-08-22 03:46 AM
Found the problem... When the GPIO_PinAFConfig() is done after GPIO_Init(), it started working!!
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_9); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_9);