cancel
Showing results for 
Search instead for 
Did you mean: 

CAN bus on STM32F103RB

NVan.966
Associate

Hello, I want to read all message coming from sensor using CAN bus. My configuration seems not okay because the CAN RX interrupt is not trigger. I made the code with CubeMX and below are my files:

Main.c

#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define UART_TX_BUFFER 8
#define CAN_RX_BUFFER 8
//#define CAN1 1
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
 
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
 
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;
DMA_HandleTypeDef hdma_usart3_rx;
DMA_HandleTypeDef hdma_usart3_tx;
 
/* USER CODE BEGIN PV */
uint8_t canCheckTX = 0;
uint8_t canCheckRX = 0;
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_CAN_Init(void);
static void MX_TIM1_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM4_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
uint8_t uart1Data[UART_TX_BUFFER];
CAN_RxHeaderTypeDef pHeader;
CAN_TxHeaderTypeDef ptHeader;
uint8_t canRxData[CAN_RX_BUFFER];
uint8_t canRxData1 = 29;
CAN_FilterTypeDef sFilterConfig;
uint8_t canTxData =10;
uint32_t TxMailBox;
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
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_CAN_Init();
  MX_TIM1_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_TIM4_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
	HAL_CAN_MspInit(&hcan);
	uint8_t i = 0;
	uint8_t dt = 56;
	for (i = 0; i< UART_TX_BUFFER; i++)
	{
		uart1Data[i] = 48 + i;
	}
	
	
	//canRx.StdId = 0x00; //the standard identifier.
	ptHeader.DLC = 8; //length of the frame that will be transmitted.
	ptHeader.IDE = CAN_ID_STD; //identifier for the message that will be transmitted.
	ptHeader.RTR = CAN_RTR_DATA;//the type of frame for the message that will be transmitted.
	ptHeader.StdId = 0x244;
 
	sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
	
	sFilterConfig.FilterIdHigh = 0x0000;
	sFilterConfig.FilterIdLow = 0x0000;
	sFilterConfig.FilterMaskIdHigh = 0x0000;
	sFilterConfig.FilterMaskIdLow = 0x0000;
	sFilterConfig.FilterBank = 0;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;
 
	HAL_CAN_ConfigFilter(&hcan,&sFilterConfig);
	
	
	HAL_CAN_Start(&hcan);
	HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING);
	
	//canRx.Timestamp = 1;
	//canRx.FilterMatchIndex = 0x00;
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
		//HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[]);
		//HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &canRx,canRxData);
		//HAL_Delay(10);
		HAL_Delay(10);
		//HAL_CAN_AddTxMessage(&hcan, &ptHeader, uart1Data, &TxMailBox);
		if (canCheckRX == 1)
		HAL_UART_Transmit(&huart1, &canRxData1, 1, 0);
		//HAL_UART_Transmit(&huart1, &canRxData1, pHeader.DLC, 1);
  }
  /* USER CODE END 3 */
}

stm32f1xx_it.c

#include "main.h"
#include "stm32f1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
 
/* USER CODE END TD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/* External variables --------------------------------------------------------*/
extern CAN_HandleTypeDef hcan;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern DMA_HandleTypeDef hdma_usart3_tx;
/* USER CODE BEGIN EV */
extern UART_HandleTypeDef huart1;
extern uint8_t uart1Data[];
extern CAN_RxHeaderTypeDef pHeader;
extern CAN_TxHeaderTypeDef ptHeader;
extern uint8_t canRxData[];
extern uint8_t canRxData1;
extern CAN_FilterTypeDef sFilterConfig;
extern uint8_t canTxData;
extern uint32_t TxMailBox;
extern uint8_t canCheckTX;
extern uint8_t canCheckRX;
 
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
 
  /* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan);
  /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
	//HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &pHeader, canRxData);
	//HAL_UART_Transmit(&huart1, &canRxData1, 1, 1);
	canCheckRX = 1;
  /* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}

Could I have your advice?

Thanks in advance!

0 REPLIES 0