cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I'm using an STM32F072VB eval board and having issues receiving a CAN message and transmitting the same message to UART. I'm using IXXAT CAN Analyzer and basing my code on the CAN_Networking example which worked fine. I attached my code, any help?

canucktut
Associate II
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. /* Private includes ----------------------------------------------------------*/
  6.  
  7. /* Private variables ---------------------------------------------------------*/
  8. CAN_HandleTypeDef hcan;
  9.  
  10. UART_HandleTypeDef huart2;
  11. UART_HandleTypeDef huart3;
  12. DMA_HandleTypeDef hdma_usart2_tx;
  13.  
  14. void SystemClock_Config(void);
  15.  
  16. static void MX_CAN_Init(void);
  17. static void MX_USART2_UART_Init(void);
  18.  
  19. /* USER CODE BEGIN 0 */
  20. CAN_HandleTypeDef hcan;
  21. CAN_TxHeaderTypeDef TxHeader;
  22. CAN_RxHeaderTypeDef RxHeader;
  23. uint32_t TxMailbox;
  24. uint8_t TxData[32];
  25. uint8_t RxData[32];
  26. uint8_t Data[8] = {0xDE, 0xAD, 0xBE, 0xEF};
  27. uint8_t test[16] = "CAN works\r\n";
  28. uint8_t start[30] = "Starting program\r\n";
  29.  
  30. uint8_t nbinterrupt;
  31.  
  32.  
  33. int main(void)
  34. {
  35. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  36. HAL_Init();
  37.  
  38. /* Configure the system clock */
  39. SystemClock_Config();
  40.  
  41. /* Initialize all configured peripherals */
  42. MX_GPIO_Init();
  43. MX_DMA_Init();
  44. MX_CAN_Init();
  45. MX_I2C1_Init();
  46. MX_I2C2_Init();
  47. MX_SPI1_Init();
  48. MX_USART2_UART_Init();
  49. MX_USART3_UART_Init();
  50. MX_CRC_Init();
  51.  
  52.  
  53. HAL_UART_Transmit(&huart2, start, sizeof(start), 100); //forward command to day cam
  54.  
  55. nbinterrupt = 0;
  56.  
  57. while (1)
  58. {
  59. /*if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, test, &TxMailbox) == HAL_OK)
  60. {
  61. HAL_Delay(1000);
  62. Error_Handler();
  63. }*/
  64. // if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
  65. // {
  66. /* if (HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  67. {
  68. if (RxData[0] == 0xDE &&
  69. RxData[1] == 0xAD &&
  70. RxData[2] == 0xBE &&
  71. RxData[3] == 0xEF)
  72. {
  73. HAL_UART_Transmit(&huart2, RxData, 32, 100); //forward command to day cam
  74. HAL_Delay(1000);
  75. }
  76.  
  77. }
  78. else Error_Handler();
  79.  
  80. // }*/
  81. //Error_Handler();
  82.  
  83. void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
  84.  
  85. if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, Data, &TxMailbox) != HAL_OK)
  86. {
  87. /* Transmission request Error */
  88. Error_Handler();
  89. }
  90. else
  91. {
  92. HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
  93. HAL_Delay(1000);
  94. }
  95.  
  96.  
  97. }
  98.  
  99. }
  100.  
  101.  
  102. static void MX_CAN_Init(void)
  103. {
  104. CAN_FilterTypeDef sFilterConfig;
  105.  
  106. /*##-1- Configure the CAN peripheral #######################################*/
  107. hcan.Instance = CAN;
  108. hcan.Init.Prescaler = 12;
  109. hcan.Init.Mode = CAN_MODE_LOOPBACK;
  110. hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  111. hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
  112. hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  113. hcan.Init.TimeTriggeredMode = DISABLE;
  114. hcan.Init.AutoBusOff = DISABLE;
  115. hcan.Init.AutoWakeUp = DISABLE;
  116. hcan.Init.AutoRetransmission = DISABLE;
  117. hcan.Init.ReceiveFifoLocked = DISABLE;
  118. hcan.Init.TransmitFifoPriority = ENABLE;
  119.  
  120. if (HAL_CAN_Init(&hcan) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124.  
  125. /*##-2- Configure the CAN Filter ###########################################*/
  126. sFilterConfig.FilterBank = 0;
  127. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  128. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  129. sFilterConfig.FilterIdHigh = 0xffff;
  130. sFilterConfig.FilterIdLow = 0x0000;
  131. sFilterConfig.FilterMaskIdHigh = 0xffff;
  132. sFilterConfig.FilterMaskIdLow = 0x0000;
  133. sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  134. sFilterConfig.FilterActivation = ENABLE;
  135. sFilterConfig.SlaveStartFilterBank = 14;
  136.  
  137.  
  138. if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  139. {
  140. /* Filter configuration Error */
  141. Error_Handler();
  142. }
  143.  
  144. /*##-3- Start the CAN peripheral ###########################################*/
  145. if (HAL_CAN_Start(&hcan) != HAL_OK)
  146. {
  147. /* Start Error */
  148. Error_Handler();
  149. }
  150.  
  151. /*##-4- Activate CAN RX notification #######################################*/
  152. if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  153. {
  154. /* Notification Error */
  155. Error_Handler();
  156. }
  157.  
  158. /*##-5- Configure Transmission process #####################################*/
  159. TxHeader.StdId = 0x321;
  160. TxHeader.ExtId = 0x01;
  161. TxHeader.RTR = CAN_RTR_DATA;
  162. TxHeader.IDE = CAN_ID_EXT;
  163. TxHeader.DLC = 4;
  164. TxHeader.TransmitGlobalTime = DISABLE;
  165.  
  166.  
  167. /*##-6- Configure Reception process #####################################*/
  168. if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
  169. {
  170. /* Reception Missing */
  171. Error_Handler();
  172. }
  173.  
  174. }
  175.  
  176.  
  177. static void MX_USART2_UART_Init(void)
  178. {
  179.  
  180. /* USER CODE END USART2_Init 1 */
  181. huart2.Instance = USART2;
  182. huart2.Init.BaudRate = 115200;
  183. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  184. huart2.Init.StopBits = UART_STOPBITS_1;
  185. huart2.Init.Parity = UART_PARITY_NONE;
  186. huart2.Init.Mode = UART_MODE_TX;
  187. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  188. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  189. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  190. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  191. if (HAL_UART_Init(&huart2) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* USER CODE BEGIN USART2_Init 2 */
  196.  
  197. /* USER CODE END USART2_Init 2 */
  198.  
  199. }
  200.  
  201.  
  202. /* USER CODE BEGIN 4 */
  203. void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
  204. {
  205. /* Get RX message */
  206. if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  207. {
  208. /* Reception Error */
  209. Error_Handler();
  210. }
  211.  
  212. /* Display LEDx */
  213. if ((RxHeader.ExtId == 0x01) && (RxHeader.IDE == CAN_ID_EXT) && (RxHeader.DLC == 4))
  214. {
  215. nbinterrupt++;
  216. HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
  217.  
  218. //LED_Display(RxData[0]);
  219. // ubKeyNumber = RxData[0];
  220. }
  221. }

2 REPLIES 2
JoniS
Senior

Did you enable filter? Atleast "pass all" filter of 0x00 must be enabled.

canucktut
Associate II

thx for the reply, yes its enabled, i'm just testing the CAN loopback and transmit to a usart to see that it works, but still has issues. I'll attach the whole main.c file and left out unrelated code.

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
#include <malloc.h>
/* Private includes ----------------------------------------------------------*/
 
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
 
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart2_tx;
 
void SystemClock_Config(void);
 
static void MX_CAN_Init(void);
static void MX_USART2_UART_Init(void);
 
/* USER CODE BEGIN 0 */
CAN_HandleTypeDef     hcan;
CAN_TxHeaderTypeDef   TxHeader;
CAN_RxHeaderTypeDef   RxHeader;
uint32_t              TxMailbox;
uint8_t               TxData[32];
uint8_t               RxData[32];
uint8_t               Data[8] = {0xDE, 0xAD, 0xBE, 0xEF};
uint8_t               test[16] = "CAN works\r\n";
uint8_t               start[30] = "Starting program\r\n";
 
uint8_t              nbinterrupt;
 
 
int main(void)
{
  /* 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_DMA_Init();
  MX_CAN_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_CRC_Init();
 
 
  HAL_UART_Transmit(&huart2, start, sizeof(start), 100); //forward command to day cam
 
  nbinterrupt = 0;
 
  while (1)
      {
	       /*if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, test, &TxMailbox) == HAL_OK)
	          {
	            HAL_Delay(1000);
	            Error_Handler();
	          }*/
	 // if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
	  //  {
	 	  /*	if (HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
	     	{
	 	  		if (RxData[0] == 0xDE &&
	 	  		    RxData[1] == 0xAD &&
				    RxData[2] == 0xBE &&
				    RxData[3] == 0xEF)
	 	  		{
	 	  			HAL_UART_Transmit(&huart2, RxData, 32, 100); //forward command to day cam
	 	  			HAL_Delay(1000);
	 	  	    }
 
	     	}
	 	  	else Error_Handler();
 
	   // }*/
	  //Error_Handler();
 
	  void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
 
     if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, Data, &TxMailbox) != HAL_OK)
        {
               /* Transmission request Error */
    	      Error_Handler();
        }
      else
        {
    	      HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
              HAL_Delay(1000);
        }
 
 
      }
 
}
 
 
static void MX_CAN_Init(void)
{
	CAN_FilterTypeDef  sFilterConfig;
 
		  /*##-1- Configure the CAN peripheral #######################################*/
		  hcan.Instance = CAN;
		  hcan.Init.Prescaler = 12;
		  hcan.Init.Mode = CAN_MODE_LOOPBACK;
		  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
		  hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
		  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
		  hcan.Init.TimeTriggeredMode = DISABLE;
		  hcan.Init.AutoBusOff = DISABLE;
		  hcan.Init.AutoWakeUp = DISABLE;
		  hcan.Init.AutoRetransmission = DISABLE;
		  hcan.Init.ReceiveFifoLocked = DISABLE;
		  hcan.Init.TransmitFifoPriority = ENABLE;
 
		  if (HAL_CAN_Init(&hcan) != HAL_OK)
		  {
		    Error_Handler();
		  }
 
		  /*##-2- Configure the CAN Filter ###########################################*/
		  sFilterConfig.FilterBank = 0;
		  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
		  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
		  sFilterConfig.FilterIdHigh = 0xffff;
		  sFilterConfig.FilterIdLow = 0x0000;
		  sFilterConfig.FilterMaskIdHigh = 0xffff;
		  sFilterConfig.FilterMaskIdLow = 0x0000;
		  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
		  sFilterConfig.FilterActivation = ENABLE;
		  sFilterConfig.SlaveStartFilterBank = 14;
 
 
		  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
		    {
		      /* Filter configuration Error */
		      Error_Handler();
		    }
 
		  /*##-3- Start the CAN peripheral ###########################################*/
		  if (HAL_CAN_Start(&hcan) != HAL_OK)
		    {
		      /* Start Error */
		      Error_Handler();
		    }
 
		  /*##-4- Activate CAN RX notification #######################################*/
		  if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
		    {
		      /* Notification Error */
	    	     Error_Handler();
		    }
 
		  /*##-5- Configure Transmission process #####################################*/
		  TxHeader.StdId = 0x321;
		  TxHeader.ExtId = 0x01;
		  TxHeader.RTR = CAN_RTR_DATA;
		  TxHeader.IDE = CAN_ID_EXT;
		  TxHeader.DLC = 4;
		  TxHeader.TransmitGlobalTime = DISABLE;
 
 
		  /*##-6- Configure Reception process #####################################*/
		  if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
		   {
		     /* Reception Missing */
		     Error_Handler();
		   }
 
}
 
 
static void MX_USART2_UART_Init(void)
{
 
  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */
 
}
 
 
/* USER CODE BEGIN 4 */
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();
  }
 
  /* Display LEDx */
  if ((RxHeader.ExtId == 0x01) && (RxHeader.IDE == CAN_ID_EXT) && (RxHeader.DLC == 4))
  {
	  nbinterrupt++;
 	  HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
 
      //LED_Display(RxData[0]);
      //  ubKeyNumber = RxData[0];
  }
}