cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f042 can problem

mail239955_stm1_st
Associate II
Posted on August 27, 2015 at 09:41


void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{


GPIO_InitTypeDef GPIO_InitStruct;

if(hcan->Instance==CAN)

{

/* USER CODE BEGIN CAN_MspInit 0 */


/* USER CODE END CAN_MspInit 0 */

/* Peripheral clock enable */

__CAN_CLK_ENABLE();

/**CAN GPIO Configuration 

PB8 ------> CAN_RX

PB9 ------> CAN_TX 

*/

GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;


GPIO_InitStruct.Alternate = GPIO_AF4_CAN;


HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


/* USER CODE BEGIN CAN_MspInit 1 */


/* USER CODE END CAN_MspInit 1 */

}


}


/* CAN init function */

void MX_CAN_Init(void)

{


hcan.Instance = CAN;


hcan.pTxMsg = &TxMessage; // Pointer initialization

hcan.pRxMsg = &RxMessage; 

hcan.Init.Prescaler = 3;

hcan.Init.Mode = CAN_MODE_NORMAL;

hcan.Init.SJW = CAN_SJW_1TQ;

hcan.Init.BS1 = CAN_BS1_13TQ;

hcan.Init.BS2 = CAN_BS2_2TQ; 

hcan.Init.TTCM = DISABLE;

hcan.Init.ABOM = DISABLE;

hcan.Init.AWUM = DISABLE;

hcan.Init.NART = DISABLE;

hcan.Init.RFLM = DISABLE;

hcan.Init.TXFP = DISABLE;

HAL_CAN_Init(&hcan);


}





while(1) //main while

{

while(hcan.State == HAL_CAN_STATE_BUSY);


TxMessage.IDE = CAN_ID_STD;

TxMessage.StdId = 0x001;

TxMessage.ExtId = 0x001;

TxMessage.RTR = CAN_RTR_DATA; //For Remote Request CAN_RTR_REMOTE

TxMessage.DLC = 8;

TxMessage.Data[0] = 0x01;

TxMessage.Data[1] = 0x02;

TxMessage.Data[2] = 0x03;

TxMessage.Data[3] = 0x04;

TxMessage.Data[4] = 0x05;

TxMessage.Data[5] = 0x06;

TxMessage.Data[6] = 0x07;

TxMessage.Data[7] = 0x08;

HAL_CAN_Transmit(&hcan,100);


while(hcan.State == HAL_CAN_STATE_BUSY);

HAL_Delay(10);

HAL_CAN_Receive(&hcan,CAN_FIFO0,100);




while(hcan.State == HAL_CAN_STATE_BUSY);

HAL_Delay(10);

HAL_CAN_Receive(&hcan,CAN_FIFO1,100);

}
I'm running the same code in two boards. 
I can see the CAN signals with oscilloscope. But the hcan.pRxMsg is always empty.
I use latest CubeMx. 
Am i missing sth?

#stm32cube #can #bug #stm32-can
2 REPLIES 2
jpeacock
Associate III
Posted on August 27, 2015 at 14:11

Where do you set the RX filters?

mail239955_stm1_st
Associate II
Posted on August 27, 2015 at 18:24

/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.pTxMsg = &TxMessage; // Pointer initialization
hcan.pRxMsg = &RxMessage;
hcan.Init.Prescaler = 3;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_13TQ;
hcan.Init.BS2 = CAN_BS2_2TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
HAL_CAN_Init(&hcan);
CAN_FilterConfTypeDef sFilterConfig;
/* Configure the CAN Filter 0 for FIFO 0 with node id */
sFilterConfig.FilterNumber = 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_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
myerror = HAL_ERROR;
}
}

CubeMX should make a default filtering setup by itself!!! I lose so many time with this little bugs since this cubemx thing.. Thank you peacock.jack... Tyvm