cancel
Showing results for 
Search instead for 
Did you mean: 

Combining CAN2 and USART2

Rde K.1
Associate III

Hello,

At the moment I'm working on a project using the STM32F4 discovery board that combines CAN2 and USART2. I managed to get them both working separately, but when I combine them in the same problem I get weird output on the serial monitor. When I then try my old program to only test the UART connection it works fine. Any ideas on why the combination of CAN and UART isn't working?

My code:

int main(void)
{
  HAL_Init();
 
  /* Configure the system clock to 168 MHz */
  SystemClock_Config();
 
  UART2_Configuration();
 
  /* Infinite loop */
  while (1)
  {
	if(CAN_Polling() == HAL_OK)
	{
		char Message[] = "CAN message sent.";
		HAL_UART_Transmit(&UART_Handler, (uint8_t *)Message, strlen(Message), HAL_MAX_DELAY);
	}
	else
	{
		char Message[] = "CAN failed.";
		HAL_UART_Transmit(&UART_Handler, (uint8_t *)Message, strlen(Message), HAL_MAX_DELAY);
	}
	HAL_Delay(500);
  }
}
 
HAL_StatusTypeDef CAN_Polling(void)
{
  CAN_FilterTypeDef  sFilterConfig;
 
  /*##-1- Configure the CAN peripheral #######################################*/
  CanHandle.Instance = CAN2;
 
  CanHandle.Init.TimeTriggeredMode = DISABLE;
  CanHandle.Init.AutoBusOff = DISABLE;
  CanHandle.Init.AutoWakeUp = DISABLE;
  CanHandle.Init.AutoRetransmission = ENABLE;
  CanHandle.Init.ReceiveFifoLocked = DISABLE;
  CanHandle.Init.TransmitFifoPriority = DISABLE;
  CanHandle.Init.Mode = CAN_MODE_NORMAL;
  CanHandle.Init.SyncJumpWidth = CAN_SJW_1TQ;
  CanHandle.Init.TimeSeg1 = CAN_BS1_4TQ;
  CanHandle.Init.TimeSeg2 = CAN_BS2_2TQ;
  CanHandle.Init.Prescaler = 6;
 
  if(HAL_CAN_Init(&CanHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
 
  /*##-2- Configure the CAN Filter ###########################################*/
  sFilterConfig.FilterBank = 15;
  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(&CanHandle, &sFilterConfig) != HAL_OK)
  {
    /* Filter configuration Error */
    Error_Handler();
  }
 
  /*##-3- Start the CAN peripheral ###########################################*/
  if (HAL_CAN_Start(&CanHandle) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
 
  /*##-4- Start the Transmission process #####################################*/
  TxHeader.StdId = 0x11;
  TxHeader.RTR = CAN_RTR_DATA;
  TxHeader.IDE = CAN_ID_STD;
  TxHeader.DLC = 2;
  TxHeader.TransmitGlobalTime = DISABLE;
  TxData[0] = 0xCA;
  TxData[1] = 0xFE;
 
  /* Request transmission */
  if(HAL_CAN_AddTxMessage(&CanHandle, &TxHeader, TxData, &TxMailbox) != HAL_OK)
  {
    /* Transmission request Error */
    Error_Handler();
  }
 
  /* Wait transmission complete */
  while(HAL_CAN_GetTxMailboxesFreeLevel(&CanHandle) != 3) {
	  HAL_Delay(50);
  }
 
  if (HAL_CAN_ActivateNotification(&CanHandle, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
	{
	  /* Notification Error */
	  Error_Handler();
	}
 
 
 
  return HAL_OK; /* Test Passed */
}
 
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
  /* Get RX message */
  if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }
 
  char Message[] = "CAN received.";
  HAL_UART_Transmit(&UART_Handler, (uint8_t *)Message, strlen(Message), HAL_MAX_DELAY);
}
 
void UART2_Configuration(void){
	__HAL_RCC_GPIOA_CLK_ENABLE(); //Enable clock to PORTA - UART2 pins PA2 and PA3
	__HAL_RCC_USART2_CLK_ENABLE(); // Enable clock to UART2 module
 
	GPIO_InitTypeDef UART2_GPIO_Handler; // Create GPIO_InitTypeDef struct instance
	UART2_GPIO_Handler.Pin = GPIO_PIN_2 | GPIO_PIN_3;
	UART2_GPIO_Handler.Mode = GPIO_MODE_AF_PP;
	UART2_GPIO_Handler.Pull = GPIO_PULLUP;
	UART2_GPIO_Handler.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	UART2_GPIO_Handler.Alternate = GPIO_AF7_USART2;
	HAL_GPIO_Init(GPIOA, &UART2_GPIO_Handler);
 
	//UART configuration
	UART_Handler.Instance = USART2;
	UART_Handler.Init.BaudRate = 115200;
	UART_Handler.Init.Mode = UART_MODE_TX_RX;
	UART_Handler.Init.WordLength = UART_WORDLENGTH_8B;
	UART_Handler.Init.StopBits = UART_STOPBITS_1;
	UART_Handler.Init.OverSampling = UART_OVERSAMPLING_16;
	HAL_UART_Init(&UART_Handler);
}

2 REPLIES 2
Rde K.1
Associate III

The serial output:

0693W00000Y7y2RQAR.png

Javier1
Principal

Are you Reseting the canbus every time you run CAN_Polling() by any chance, if this is true ,why?

we dont need to firmware by ourselves, lets talk