cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX CAN problem

mail239955_stm1_st
Associate II
Posted on August 26, 2015 at 10:08

Hello,

I have 2 STM32F042C6 board. My transmitter board's code :

static CanTxMsgTypeDef TxMessage; // Static variable declaration
static CanRxMsgTypeDef RxMessage;
/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.pTxMsg = &TxMessage; // Pointer initialization
hcan.pRxMsg = &RxMessage;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_14TQ;
hcan.Init.BS2 = CAN_BS2_6TQ;
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);
}
void SendCan(void) {
while(!hcan.State == HAL_CAN_STATE_READY);
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);
}

My receiver board's code:

static CanTxMsgTypeDef TxMessage; // Static variable declaration
static CanRxMsgTypeDef RxMessage;
/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.pTxMsg = &TxMessage; // Pointer initialization
hcan.pRxMsg = &RxMessage;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_14TQ;
hcan.Init.BS2 = CAN_BS2_6TQ;
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);
}
void RecieveCan(void) {
while(!hcan.State == HAL_CAN_STATE_READY);
HAL_Delay(10);
HAL_CAN_Receive(&hcan,CAN_FIFO0,1000);
while(!hcan.State == HAL_CAN_STATE_READY);
HAL_Delay(10);
HAL_CAN_Receive(&hcan,CAN_FIFO1,1000);
}

When I watch RxMessage register, It's always empty. I have never been stuck in code before this STM32CubeMX thing. Any suggestions? Thank you. Serhat #hal #stm32cubemx #cubemx
1 REPLY 1
markb
Associate II
Posted on August 26, 2015 at 10:50

while(hcan.State != HAL_CAN_STATE_READY);