2024-12-23 04:42 AM - last edited on 2024-12-24 12:20 AM by SofLit
Hi all
Can You provide me a correct code for how we can transfer TX frame using HAL .
please check my code below .
Issue what I am facing in my code is only I am seeing first ID data even data and ID are correctly stored inside buffer.
Please guide me .
PFA
2024-12-23 05:44 AM
@Ash1 wrote:
Hi
Can you suggest me a example for LOOPback mode
2024-12-23 05:45 AM
@Ash1 wrote:
Okay I will try loopback mode and check .
Can it be issue because of clock .how much clock we should give atleast I am running 40mhz and 6 prescaler
I've already asked you to share your project to have a closer look at it.
2024-12-23 06:06 AM - edited 2024-12-23 06:16 AM
In case you face an issue with the Loopack mode: Troubleshooting bxCAN issues in Loop Back mode on STM32 MCUs
2024-12-23 08:52 PM
Hi
How Can I attach project in reply
2024-12-23 09:07 PM
Hi all
Please check my code is it correct way for loopback mode in while loop I am sending two frames and enable RX interrupt mode in which Receive Id will be process.
so is it correct?
#include "main.h"
#include "stm32f4xx_hal_can.h"
/* USER CODE BEGIN PM */
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);
/**
* @brief This function handles CAN1 RX0 interrupt request.
* @PAram None
* @retval None
*/
void CAN1_TXIRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
void CAN1_RX0IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
void CAN1_RX1IRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
void CAN1_SCEIRQHandler(void)
{
HAL_CAN_IRQHandler(&hcan1);
}
void WWDG_APPIRQHandler(void)
{
}
void CANRxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef rx_header;
uint8_t rx_data[8];
static uint32_t counter0x3e8 = 0;
static uint32_t counter0x244 = 0;
if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_header, rx_data) != HAL_OK)
{
Error_Handler();
}
if(rx_header.StdId==0x3e8){
counter0x3e8++;
}
else if(rx_header.StdId==0x244){
counter0x244++;
}
}
void CANRxFifo0FullCallback(CAN_HandleTypeDef *hcan)
{
}
void CANRxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
}
void CANRxFifo1FullCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
{
}
void CANTxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
{
}
void CANSleepCallback(CAN_HandleTypeDef *hcan)
{
}
void CANWakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
{
}
void CANErrorCallback(CAN_HandleTypeDef *hcan)
{
}
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/*HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); // Set priority
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); // Enable interrupt*/
/* USER CODE END 2 */
HAL_NVIC_DisableIRQ(SPI2_IRQn);
/*HAL_NVIC_DisableIRQ(WWDG_IRQn);*/
//WWDG->CR = WWDG_CR_WDGA; // This disables the watchdog (clears the WDGA bit)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN1_Init();
/* Can initialization */
if (HAL_CAN_Start(&hcan1) != HAL_OK)
{
Error_Handler();
}
while (1)
{
// Example: Transmit a CAN message
CAN_TxHeaderTypeDef txHeader;
const uint8_t tx_data[8] = {0x11};
const uint8_t tx_data1[8] = {0x12};
uint32_t tx_mailbox_used;
txHeader.StdId = 0x3e8; // Example CAN ID
txHeader.RTR = CAN_RTR_DATA;
txHeader.IDE = CAN_ID_STD;
txHeader.DLC = 8;
if (HAL_CAN_AddTxMessage(&hcan1, &txHeader, tx_data, &tx_mailbox_used) != HAL_OK)
{
Error_Handler();
}
txHeader.StdId = 0x244; // Example CAN ID
txHeader.RTR = CAN_RTR_DATA;
txHeader.IDE = CAN_ID_STD;
txHeader.DLC = 8;
if (HAL_CAN_AddTxMessage(&hcan1, &txHeader, tx_data1, &tx_mailbox_used) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(10);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
static void MX_CAN1_Init(void)
{
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 6;
hcan1.Init.Mode = CAN_MODE_LOOPBACK;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_11TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = ENABLE;
hcan1.Init.ReceiveFifoLocked = ENABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
CAN_FilterTypeDef sFilterConfig;
/* USER CODE BEGIN CAN1_Init 2 */
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 = 14;
if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
/* Can initialization */
/* USER CODE END CAN1_Init 2 */
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_FULL);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_OVERRUN);
//HAL_CAN_ActivateNotification(&hcan1, CAN_IT_WAKEUP);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
//__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin */
GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/*I want to implement CAN communication in normal mode for STM32F407 discovery
board using HAL library functions. I want to transmit message id 0x123 with
tx_data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} in
polling mode. After that I want to receive this message id in interrupt based
mode by enabling CAN1_RX0 interrupt mode. */
2024-12-24 12:16 AM
@Ash1 did you run the Loopback example and test it? did it work?
2024-12-24 12:18 AM - edited 2024-12-24 12:20 AM
@Ash1 wrote:
Hi
How Can I attach project in reply
with drag and drop feature:
2024-12-24 02:00 AM
Hi sir
I ran the loopback mode and able to see both ID and data in my FIFO register.
In previous reply you said if loopback works may it is issue from receiver side.
In my receiver I am using (TJA1050 and which connected to CAN bus analyzer to see frame )
B.R
Ashish
2024-12-24 03:15 AM
If in loopback mode is working there is no issue in the MCU nor in your software. I don't think there is an issue with the HW as at least you received one ID. I suspect you have an issue with your CAN analyzer.