2020-06-30 12:00 PM
I have a project which is include Stm32f4 and CAN Trasceiver and I also using CAN Analysing to analyse the line. And I also have 120 Ohm.
When I configure CAN1 and try to send CANBUS data with
canheader.DLC = 8;
canheader.IDE = CAN_ID_STD;
canheader.RTR = CAN_RTR_DATA;
canheader.StdId = 0x10;
// canheader.ExtId = 0x01;
canheader.TransmitGlobalTime = DISABLE;
uint8_t candata[8] = {0xAA, 0XBB, 0XCC, 0XDD, 0XEE, 0XFF, 0X99, 0X88};
uint32_t TxMailbox=0;
HAL_CAN_AddTxMessage(&hcan1, &canheader, candata, &TxMailbox);
I see some data on the line but there is meanless. I think canbus not working correctly. And than I try to receive something. Receive interrupt not working. How can I solve this.
I enabled GPIOD clock in GPIO_Init function
This is my msp.c file:
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */
/* USER CODE END CAN1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_CAN1_CLK_ENABLE();
/**CAN1 GPIO Configuration
PD0 ------> CAN1_RX
PD1 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* USER CODE BEGIN CAN1_MspInit 1 */
HAL_NVIC_SetPriority(CAN1_TX_IRQn,4,0);
HAL_NVIC_SetPriority(CAN1_RX0_IRQn,4,0);
// HAL_NVIC_SetPriority(CAN1_RX1_IRQn,15,0);
// HAL_NVIC_SetPriority(CAN1_SCE_IRQn,15,0);
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
// HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
// HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
/* USER CODE END CAN1_MspInit 1 */
}
}
And this is the interrupt file:
/**
* @brief This function handles CAN1 RX0 interrupt request.
* @param None
* @retval None
*/
void CANx_RX_IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
SerialPrint("Hello !!\n");
}
And this is the CAN Initilizing function in main.c
static void MX_CAN1_Init(void)
{
CAN_FilterTypeDef sFilterConfig;
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 16;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = ENABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 0;
if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
SerialPrint("FILTER ERROR !!");
Error_Handler();
}
/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&hcan1) != HAL_OK)
{
SerialPrint("CAN START ERROR !!");
/* Start Error */
Error_Handler();
}
/*##-4- Activate CAN RX notification #######################################*/
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
SerialPrint("CAN NOTIFICATION ERROR !!");
/* Notification Error */
Error_Handler();
}
SerialPrint("Success CAN Init \n");
}
2020-06-30 12:26 PM
Hi,
First I propose to check Your init settings for CAN. Please look on the site: http://www.bittiming.can-wiki.info/#bxCAN and check Your settings for Prescaler, TimeSeg1 and TimeSeg2. What transmission speed did You want to set? What clock do You have set for CAN?
2020-06-30 12:42 PM
Okey you are right. I change my clock settings so I forget to change bit timing settings. :\
APB1 bus have 42 Mhz clock. I think my bit timing settings wrong.
I want to communicate 1Mb/s so I have to do, PRescaler=3,Seg1=11,Seg2=2 Isnt it? (42000000÷(3×(11+2+1)))
2020-06-30 12:51 PM
Hi,
Yes, now it is ok (PRescaler=3, Seg1=11, Seg2=2). Do You have communication in NORMAL or LOOPBACK mode?