2025-04-24 10:43 AM - edited 2025-04-24 10:48 AM
When debugging, all seems to go well, no errors. But when running the code, there never seems to be a CAN message on the line. Hardware connections are checked, and they're fine.
See Main(), message function and hardware connection below (added CAN init function too now):
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_DMA_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_CAN1_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
CAN_TX_filter_init();
HSB_DebugPrint_Init(false);
HSB_VoltageModule_Init(0x68);
//HAL_ADC_Start_DMA(&hadc1, (uint32_t *) rawValues, 2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// MX_ADC1_Init();
// HSB_ReadCurrentModule(&MiliCurrentE, &MiliCurrentF);
// HSB_VoltageModule(&VoltageE, &VoltageF);
CurrentE = MiliCurrentE / 1000;
CurrentF = MiliCurrentF / 1000;
PowerE = VoltageE * CurrentE;
PowerF = VoltageF * CurrentF;
sendMessage('E');
sendMessage('F');
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
void sendMessage(uint8_t group){
uint16_t current;
uint16_t voltage;
uint16_t power;
switch(group){
case 'E':
current = (uint16_t)(CurrentE * 100);
voltage = (uint16_t)(VoltageE * 100);
power = (uint16_t)(PowerE * 100);
break;
case 'F':
current = (uint16_t)(CurrentF * 100);
voltage = (uint16_t)(VoltageF * 100);
power = (uint16_t)(PowerF * 100);
break;
default:
current = 0;
voltage = 0;
power = 0;
}
TxData[0] = group;
TxData[1] = (voltage & 0xFF00) >> 8;
TxData[2] = voltage & 0x00FF;
TxData[3] = (current & 0xFF00) >> 8;
TxData[4] = current & 0x00FF;
TxData[5] = (power & 0xFF00) >> 8;
TxData[6] = power & 0x00FF;
TxData[7] = 0;
if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, (uint8_t*)TxData, &TxMailbox) != HAL_OK)
{
uint32_t error = HAL_CAN_GetError(&hcan1);
HSB_DebugPrint("Error while sending Can Message : %un", error);
Error_Handler ();
}
}
void CAN_TX_filter_init(void)
{
TxHeader.StdId = 0x260;
TxHeader.ExtId = 0;
TxHeader.IDE = CAN_ID_STD;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.DLC = 8;
TxHeader.TransmitGlobalTime = DISABLE;
}
static void MX_CAN1_Init(void)
{
/* USER CODE BEGIN CAN1_Init 0 */
/* USER CODE END CAN1_Init 0 */
/* USER CODE BEGIN CAN1_Init 1 */
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 8;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_7TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_8TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
if (HAL_CAN_Start(&hcan1) != HAL_OK) {
Error_Handler();
}
/* USER CODE END CAN1_Init 2 */
}
2025-04-24 11:27 AM
Hello,
Not clear what do you mean by:
@oguh wrote:
When debugging, all seems to go well, no errors. But when running the code, there never seems to be a CAN message on the line.
Do you mean when you start a debug session, you can see CAN messages on the line but when you run the application in standalone you cannot see CAN frames?
Also please clarify how are you checking the CAN message? which tool are you using?
As you are using normal mode, did you establish a complete CAN bus like this?:
2025-04-24 11:46 AM - edited 2025-04-24 11:54 AM
I've never received a CAN message from this setup. Not in debug mode or when running the standalone application. I check this using either a logic analyser or a different CAN node that can receive CAN messages. And yes, the picture you provided was my exact setup.
2025-04-24 12:36 PM
@oguh wrote:
I've never received a CAN message from this setup.
You need to check the filters and the NVIC interrupt. but sill not sure I've understood well the issue.
Is it an issue with both with the transmit and receive?
Please provide more details about the other node connected on the bus.
What is the bitrate are you using? are you sure you have the same bitrates on all CAN nodes?
What is the system clock source?: internal or external?
If possible also to attach your ioc file + the content of:
CAN_TX_filter_init();
2025-04-24 1:13 PM
This, is not a filter init routine. That's just initializing the Tx Header.
void CAN_TX_filter_init(void)
{
TxHeader.StdId = 0x260;
TxHeader.ExtId = 0;
TxHeader.IDE = CAN_ID_STD;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.DLC = 8;
TxHeader.TransmitGlobalTime = DISABLE;
}
This would be a correct filter init routine for the sFilterConfig which calls HAL_CAN_ConfigFilter.
And it includes calling HAL_CAN_ActivateNotification, which you are missing.
void CAN_SetFilter(CAN_HandleTypeDef *hcan)
{
static CAN_FilterTypeDef sFilterConfig;
if(hcan == &hcan1)
{
sFilterConfig.FilterBank = 0;
}
#ifdef HCAN2
else if(hcan == &hcan2)
{
sFilterConfig.FilterBank = 14;
}
#endif
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_FILTER_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
if(HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
if(HAL_CAN_Start(hcan) != HAL_OK)
{
Error_Handler();
}
if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_TX_MAILBOX_EMPTY ) != HAL_OK) // enables CAN notification.
{
Error_Handler();
}
}