2016-04-22 09:51 AM
Hello,
i am new to HAL Libraries. i am learning to work with CAN BUS using HAL Libraries. Can any one please post an example of STM32F CAN BUS using HAL Libraries.as it will help to have an idea how should we program STM32 boards. Thanks in advance. #stm32 #stm32 #hal #hal #can #can #cubemx #cubemx #!stm32f42017-01-05 11:51 AM
Please be aware that the forum was just ported to a new platform, old users and posts may not be responsive.
Ask yourself if framing a new question, with complete details of the part you are using and the code and configuration you have now might be more productive?
2017-01-05 12:16 PM
Posted on January 05, 2017 at 20:16
Thanks for your advice, if you know something about these libraries would be helpful, so if you can help me my code is:
CAN CONFIGURATIONS
/* CAN1 init function */
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 16;
//hcan1.Init.Prescaler = 80;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_1TQ;
hcan1.Init.BS2 = CAN_BS2_1TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = DISABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(canHandle->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
PA11 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
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(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN CAN1_MspInit 1 */
/* USER CODE END CAN1_MspInit 1 */
}
}
MAIN CODE
int main(void)
{
/* USER CODE BEGIN 1 */
vInit_CAN_Block(&xCAN_Block); //Initialize CAN Block
vInit_Queue_CAN(&xCAN_Queue); //Initialize CAN Queue
vInit_CAN_Message(&xCAN_Message); //Initialize CAN Message
/************Tests Variables************/
pvrxCAN_Message MessageTest_TX;
pvrxCAN_Message MessageTest_RX;
/**************************************/
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN1_Init();
/* USER CODE BEGIN 2 */
/*****************************************************************Tests Tx*****************************************************************/
MessageTest_TX.uiCommand=1; //Standard Identifier
MessageTest_TX.ucMessageLength=1; //1 Byte
vprvxCANMessage_To_CANHandleTypeDef( MessageTest_TX,(hcan1.pTxMsg)); //Transform to Private Structure to HAL Structure to send the message
HAL_CAN_Transmit(&hcan1,50); // Send Message
/*******************************************************************************************************************************************/
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/*****************************************************************Tests Rx*****************************************************************/
HAL_CAN_Receive(&hcan1,CAN_FIFO0,50);
//if(hcan1.State == HAL_CAN_STATE_READY)
if(hcan1.State == HAL_CAN_STATE_TIMEOUT )
{
LED_1_OFF; //Reset LED 0
LED_1_OFF; //Reset LED 1
vprvxCANHandleTypeDef_To_CANMessage(&MessageTest_RX,(hcan1.pRxMsg));////Transform to Private Structure to HAL Structure to send the message
if(MessageTest_RX.uiCommand == 2)
{
LED_0_ON;
}//if(MessageTest_RX.uiCommand == 0x2)
else
{
LED_1_ON;
}//else
}//f(hcan1.State == HAL_CAN_STATE_READY)
/*******************************************************************************************************************************************/
}
/* USER CODE END 3 */
}
2017-09-26 03:05 AM
can u please send come for can communation in stm32f4
mailto:naveenreddy4d@gmail.com
2018-06-14 04:50 AM
Hi Clive ,
i am using STM32F417 MCU , am trying test CAN1,CAN2 Transmit & Receive .with below code , For CAN1 am able to transmit and receive data on CAN bus using PCAN viewer.
But when am transmitting data on CAN2 am not able to receive any data on bus.Am getting bit stuff error on PCAN viewer.
I have enabled CAN1,CAN2 both to be able to use CAN2 .
What should i missed in CAN2 configuration being slave (from ST point of design) ?.Though its not a slave on CAN network.I suppose CAN2 alone can transmit data on CAN bus.
The dependency of CAN2 from CAN1 is comes into picture when there is a reception issue due to filter config.
But is there any dependency for transmit as well ?
Here is my code ,,,,i used CAN1 init just for the purpose of CAN2 to work .
I would like to transmit data from CAN2 to PCAN (USB -CAN) device .
I have already verified my tranceiver using CAN1 ,So am sure about the hardware setup.
Thanks in advance for your help .
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/CAN_HandleTypeDef CanHandle1;CAN_HandleTypeDef CanHandle2;CAN_TxHeaderTypeDef TxHeader;CAN_RxHeaderTypeDef RxHeader;uint8_t TxData[8];uint8_t RxData[8];uint32_t TxMailbox;int main(void)
{ /* USER CODE BEGIN 1 *//* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init(); //MX_CAN2_Init(); /* USER CODE BEGIN 2 */ CAN1_Config(); CAN2_Config(); /* USER CODE END 2 *//* Infinite loop */
/* USER CODE BEGIN WHILE */ while (1) {/* USER CODE END WHILE */ /* Set the data to be transmitted */ TxData[0] = 0x11; TxData[1] = 0x22; TxData[2] = 0x33; TxData[3] = 0x44; TxData[4] = 0x55; TxData[5] = 0x66; TxData[6] = 0x77; TxData[7] = 0x88; /* Start the Transmission process */ if (HAL_CAN_AddTxMessage(&CanHandle2, &TxHeader, TxData, &TxMailbox) != HAL_OK) { /* Transmission request Error */ HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET); Error_Handler(); } //while(HAL_CAN_GetTxMailboxesFreeLevel(&CanHandle) != 3) {}HAL_Delay(10);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET); HAL_Delay(100); HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET); HAL_Delay(100); //CAN2_RX();}}static void CAN1_Config(void)
{ CAN_FilterTypeDef sFilterConfig; uint32_t Clk; /*##-1- Configure the CAN peripheral #######################################*/ CanHandle1.Instance = CAN1;CanHandle1.Init.TimeTriggeredMode = DISABLE;
CanHandle1.Init.AutoBusOff = DISABLE; CanHandle1.Init.AutoWakeUp = DISABLE; CanHandle1.Init.AutoRetransmission = DISABLE; CanHandle1.Init.ReceiveFifoLocked = DISABLE; CanHandle1.Init.TransmitFifoPriority = DISABLE; CanHandle1.Init.Mode = CAN_MODE_NORMAL;//CAN_MODE_LOOPBACK; CanHandle1.Init.SyncJumpWidth = CAN_SJW_1TQ; CanHandle1.Init.TimeSeg1 = CAN_BS1_11TQ; CanHandle1.Init.TimeSeg2 = CAN_BS2_2TQ; CanHandle1.Init.Prescaler = 12;//3 if (HAL_CAN_Init(&CanHandle1) != HAL_OK) { /* Initialization Error */ Error_Handler(); }// /*##-2- Configure the CAN Filter ###########################################*/
// sFilterConfig.FilterBank = 0;// sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDMASK;// sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;// sFilterConfig.FilterIdHigh = 0x0000;//(0x321<< 5);// sFilterConfig.FilterIdLow = 0x0000;// sFilterConfig.FilterMaskIdHigh = 0x0000;//; 0xFFE0;// sFilterConfig.FilterMaskIdLow = 0x0000;// sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;// sFilterConfig.FilterActivation = ENABLE;// sFilterConfig.SlaveStartFilterBank = 13;// if (HAL_CAN_ConfigFilter(&CanHandle1, &sFilterConfig) != HAL_OK)
// {// /* Filter configuration Error */// Error_Handler();// }// /*##-3- Start the CAN peripheral ###########################################*/
// if (HAL_CAN_Start(&CanHandle1) != HAL_OK)// {// /* Start Error */// Error_Handler();// }}static void CAN2_Config(void)
{ CAN_FilterTypeDef sFilterConfig; uint32_t Clk; /*##-1- Configure the CAN peripheral #######################################*/ CanHandle2.Instance = CAN2;CanHandle2.Init.TimeTriggeredMode = DISABLE;
CanHandle2.Init.AutoBusOff = DISABLE; CanHandle2.Init.AutoWakeUp = DISABLE; CanHandle2.Init.AutoRetransmission = DISABLE; CanHandle2.Init.ReceiveFifoLocked = DISABLE; CanHandle2.Init.TransmitFifoPriority = DISABLE; CanHandle2.Init.Mode = CAN_MODE_NORMAL;//CAN_MODE_LOOPBACK; CanHandle2.Init.SyncJumpWidth = CAN_SJW_1TQ; CanHandle2.Init.TimeSeg1 = CAN_BS1_11TQ; CanHandle2.Init.TimeSeg2 = CAN_BS2_2TQ; CanHandle2.Init.Prescaler = 12;//3 if (HAL_CAN_Init(&CanHandle2) != HAL_OK) { /* Initialization Error */ Error_Handler(); }/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 14; sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDMASK; sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; sFilterConfig.FilterIdHigh = 0x0000;//(0x321<< 5); sFilterConfig.FilterIdLow = 0x000E; sFilterConfig.FilterMaskIdHigh = 0x0000;//; 0xFFE0; sFilterConfig.FilterMaskIdLow = 0x0000; sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; sFilterConfig.FilterActivation = ENABLE; sFilterConfig.SlaveStartFilterBank = 27;if (HAL_CAN_ConfigFilter(&CanHandle2, &sFilterConfig) != HAL_OK)
{ /* Filter configuration Error */ Error_Handler(); }/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&CanHandle2) != HAL_OK) { /* Start Error */ Error_Handler(); }// /*##-4- Activate CAN RX notification #######################################*/
// if (HAL_CAN_ActivateNotification(&CanHandle2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)// {// /* Notification Error */// // Error_Handler();// }}
Thanks
BR
Prabu.A2018-06-14 06:29 AM
HI Clive
Thanks for the reply. I could see the Tx starting pulse on CAN bus .
I just verified GPIO clock and TX,RX pins. Looks fine.
About the external transceiver as i said ,it was working for CAN1.
I did loopback test for CAN1(OK) , but not for CAN2.i used exactly same config of CAN1 for CAN2 .
Prabu
2018-06-14 07:11 AM
Not a huge fan of the HAL/CubeMX implementation.
I'd probably focus on the GPIO clock and pin configurations (assorted MSP code), and the external transceiver.
Does Loop-Back function normally?
2018-06-14 10:04 AM
I don't like what you did with the Filter ID, but don't know if that impacts the situation
Seems like an issue at the HAL level, you'd need to dig into that a bit, not really something I can expend resources on.
I'd proof the hardware with the SPL.
2024-06-06 01:57 AM
hi nina , i hope you're okay , plz can u help me for my code i have problem in my code , i wanna send a data via can and nothing reciving in the other board
2024-06-06 02:06 AM
Hello @achref-22 ,
I invite you to open a new thread and explain in details the issue and provide details about the HW + provide your code ..
Thank you.