AnsweredAssumed Answered

CAN protocol with a timer interrupt (STM32F4 discovery board)

Question asked by joao.lee on Jul 2, 2015
I am working on the STM32F407VGT discovery board + Waveshare Open407V-D and keli uVision5.
And I use HAL driver and auto generated code from the STM32CubeMX.

I'm trying to transmit a messge by CAN1 node and receive a message by CAN2 node in the same board with 500kbps CAN protocol.

I will summarize about my code.
1. The period of timer 2 interrupt is 10 msec. tranmits a CAN message with a normal mode in the timer 2 interrupt.
2. In the CAN1 TX interrupt, just check the transmit status with LED.
3. In the CAN2 RX interrupt, receive the CAN message from the CAN1 node.

Currently, timer 2 interrupt works correctly. but CAN1 TX do not operate.
I don't know why CAN protocol do not operate. Please, help me!

Here is my code.

                                                               
 main.c
 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();
           
              /* Configure the system clock */
              SystemClock_Config();
           
              /* Initialize all configured peripherals */
              MX_GPIO_Init();
              MX_CAN1_Init();
              MX_CAN2_Init();
              MX_TIM2_Init();
              MX_TIM3_Init();
           
              /* USER CODE BEGIN 2 */
           
                HAL_TIM_Base_Start_IT(&htim2); // timer 2
                HAL_TIM_Base_Start_IT(&htim3); // timer 3
           
                // CAN 1
            //    CAN1_Filter_Init();
                pTxMsg_CAN1.StdId = 0x321; // 0x000 ~ 0x3FF
            //    pTxMsg_CAN1.ExtId = 0x01; // cannot influence anything
                pTxMsg_CAN1.IDE = CAN_ID_STD; // standard id
                pTxMsg_CAN1.RTR = CAN_RTR_DATA; // data frame
                pTxMsg_CAN1.DLC = 1; // 0~7
                pTxMsg_CAN1.Data[0] = 0x11; // 1byte, ~0xFF
                pTxMsg_CAN1.Data[1] = 0x22;
                
                hcan1.pTxMsg = &pTxMsg_CAN1;
           
            //    CAN 2
            //    CAN2_Filter_Init();
                pRxMsg_CAN2.StdId = 0x123;
            //    pRxMsg_CAN2.ExtId = 0x01;
                pRxMsg_CAN2.IDE = CAN_ID_STD;
                pRxMsg_CAN2.RTR = CAN_RTR_DATA;
                pRxMsg_CAN2.DLC = 1;
            //    pRxMsg_CAN2.Data[0] = 0x00;
            //    pTxMsg_CAN2.Data[1] = ;
                pRxMsg_CAN2.FMI = 0; // 0~0xFF
                pRxMsg_CAN2.FIFONumber = CAN_FIFO0;
                
                hcan2.pRxMsg = &pRxMsg_CAN2;
                
              /* USER CODE END 2 */
           
              /* Infinite loop */
              /* USER CODE BEGIN WHILE */
              while (1)
              {
              /* USER CODE END WHILE */
           
              /* USER CODE BEGIN 3 */
              //__NOP;
           
              }
              /* USER CODE END 3 */
           
            }
           
            /** System Clock Configuration
            */
            void SystemClock_Config(void)
            {
           
              RCC_OscInitTypeDef RCC_OscInitStruct;
              RCC_ClkInitTypeDef RCC_ClkInitStruct;
           
              __PWR_CLK_ENABLE();
           
              __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
           
              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 = 5;
              RCC_OscInitStruct.PLL.PLLN = 210;
              RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
              RCC_OscInitStruct.PLL.PLLQ = 7;
              HAL_RCC_OscConfig(&RCC_OscInitStruct);
           
              RCC_ClkInitStruct.ClockType = 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;
              HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
           
              HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
           
              HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
           
            }
           
            /* CAN1 init function */
            void MX_CAN1_Init(void)
            {
           
              hcan1.Instance = CAN1;
              hcan1.Init.Prescaler = 16;
              hcan1.Init.Mode = CAN_MODE_NORMAL;
              hcan1.Init.SJW = CAN_SJW_1TQ;
              hcan1.Init.BS1 = CAN_BS1_1TQ;
              hcan1.Init.BS2 = CAN_BS2_1TQ;
              hcan1.Init.TTCM = DISABLE;
              hcan1.Init.ABOM = DISABLE;
              hcan1.Init.AWUM = DISABLE;
              hcan1.Init.NART = DISABLE;
              hcan1.Init.RFLM = DISABLE;
              hcan1.Init.TXFP = DISABLE;
              HAL_CAN_Init(&hcan1);
           
            }
           
            /* CAN2 init function */
            void MX_CAN2_Init(void)
            {
           
              hcan2.Instance = CAN2;
              hcan2.Init.Prescaler = 16;
              hcan2.Init.Mode = CAN_MODE_NORMAL;
              hcan2.Init.SJW = CAN_SJW_1TQ;
              hcan2.Init.BS1 = CAN_BS1_1TQ;
              hcan2.Init.BS2 = CAN_BS2_1TQ;
              hcan2.Init.TTCM = DISABLE;
              hcan2.Init.ABOM = DISABLE;
              hcan2.Init.AWUM = DISABLE;
              hcan2.Init.NART = DISABLE;
              hcan2.Init.RFLM = DISABLE;
              hcan2.Init.TXFP = DISABLE;
              HAL_CAN_Init(&hcan2);
           
            }
           
            /* TIM2 init function */
            void MX_TIM2_Init(void)
            {
           
              TIM_ClockConfigTypeDef sClockSourceConfig;
              TIM_MasterConfigTypeDef sMasterConfig;
           
              htim2.Instance = TIM2;
              htim2.Init.Prescaler = 83;
              htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
              htim2.Init.Period = 9999;
              htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
              HAL_TIM_Base_Init(&htim2);
           
              sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
              HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
           
              sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
              sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
              HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
           
            }
           
            /* TIM3 init function */
            void MX_TIM3_Init(void)
            {
           
              TIM_ClockConfigTypeDef sClockSourceConfig;
              TIM_MasterConfigTypeDef sMasterConfig;
           
              htim3.Instance = TIM3;
              htim3.Init.Prescaler = 83;
              htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
              htim3.Init.Period = 19999;
              htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
              HAL_TIM_Base_Init(&htim3);
           
              sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
              HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
           
              sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
              sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
              HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
           
            }
           
            /** Configure pins as
                    * Analog
                    * Input
                    * Output
                    * EVENT_OUT
                    * EXTI
                 PC3   ------> I2S2_SD
                 PB10   ------> I2S2_CK
                 PA9   ------> USB_OTG_FS_VBUS
                 PA10   ------> USB_OTG_FS_ID
                 PA11   ------> USB_OTG_FS_DM
                 PA12   ------> USB_OTG_FS_DP
                 PB6   ------> I2C1_SCL
                 PB9   ------> I2C1_SDA
            */
            void MX_GPIO_Init(void)
            {
           
              GPIO_InitTypeDef GPIO_InitStruct;
           
              /* GPIO Ports Clock Enable */
              __GPIOE_CLK_ENABLE();
              __GPIOC_CLK_ENABLE();
              __GPIOH_CLK_ENABLE();
              __GPIOA_CLK_ENABLE();
              __GPIOB_CLK_ENABLE();
              __GPIOD_CLK_ENABLE();
           
              /*Configure GPIO pin : PE3 */
              GPIO_InitStruct.Pin = GPIO_PIN_3;
              GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PC0 */
              GPIO_InitStruct.Pin = GPIO_PIN_0;
              GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PC3 */
              GPIO_InitStruct.Pin = GPIO_PIN_3;
              GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
              HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
           
              /*Configure GPIO pins : PA0 PA4 PA6 */
              GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_6;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_PULLDOWN;
              HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PB2 */
              GPIO_InitStruct.Pin = GPIO_PIN_2;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PB10 */
              GPIO_InitStruct.Pin = GPIO_PIN_10;
              GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
              HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PB15 */
              GPIO_InitStruct.Pin = GPIO_PIN_15;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_PULLDOWN;
              HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
           
              /*Configure GPIO pins : PD12 PD13 PD14 PD15
                                       PD4 */
              GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
                                      |GPIO_PIN_4;
              GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
           
              /*Configure GPIO pins : PC6 PC7 */
              GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_PULLDOWN;
              HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PA9 */
              GPIO_InitStruct.Pin = GPIO_PIN_9;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
           
              /*Configure GPIO pins : PA10 PA11 PA12 */
              GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
              GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
              HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PD5 */
              GPIO_InitStruct.Pin = GPIO_PIN_5;
              GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
           
              /*Configure GPIO pins : PB6 PB9 */
              GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_9;
              GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
              GPIO_InitStruct.Pull = GPIO_PULLUP;
              GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
              GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
              HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
           
              /*Configure GPIO pin : PE1 */
              GPIO_InitStruct.Pin = GPIO_PIN_1;
              GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
              GPIO_InitStruct.Pull = GPIO_NOPULL;
              HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
           
            }
           
            /* USER CODE BEGIN 4 */
           
            void CAN1_Filter_Init(void)
            {
                CAN1_sFilterConfig.FilterIdHigh = 0x0000;
                CAN1_sFilterConfig.FilterIdLow= 0x0000;
                CAN1_sFilterConfig.FilterMaskIdHigh = 0x0000;
                CAN1_sFilterConfig.FilterMaskIdLow = 0x0000;
                CAN1_sFilterConfig.FilterFIFOAssignment= 0;
                CAN1_sFilterConfig.FilterNumber = 0; // CAN1 0~13, CAN2 14~27
                CAN1_sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // or CAN_FILTERMODE_IDLIST
                CAN1_sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //or CAN_FILTERSCALE_16BIT
                CAN1_sFilterConfig.FilterActivation= 1; // 0 not active, 1 active=ENABLE
                CAN1_sFilterConfig.BankNumber = 0;    
                HAL_CAN_ConfigFilter(&hcan1, &CAN1_sFilterConfig);
            }
           
           
            void CAN2_Filter_Init(void)
            {
                CAN2_sFilterConfig.FilterIdHigh = 0x0000;
                CAN2_sFilterConfig.FilterIdLow= 0x0000;
                CAN2_sFilterConfig.FilterMaskIdHigh = 0x0000;
                CAN2_sFilterConfig.FilterMaskIdLow = 0x0000;
                CAN2_sFilterConfig.FilterFIFOAssignment= 0;
                CAN2_sFilterConfig.FilterNumber = 14; // CAN1 0~13, CAN2 14~27
                CAN2_sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // or CAN_FILTERMODE_IDLIST
                CAN2_sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //or CAN_FILTERSCALE_16BIT
                CAN2_sFilterConfig.FilterActivation= 1; // 0 not active, 1 active=ENABLE
                CAN2_sFilterConfig.BankNumber = 0;    
                HAL_CAN_ConfigFilter(&hcan2, &CAN2_sFilterConfig);
            }
           

                                                               
 stm32f4xx_it.c
 void CAN1_TX_IRQHandler(void)
            {
              /* USER CODE BEGIN CAN1_TX_IRQn 0 */
           
              /* USER CODE END CAN1_TX_IRQn 0 */
              HAL_CAN_IRQHandler(&hcan1);
              /* USER CODE BEGIN CAN1_TX_IRQn 1 */
           
                counter_CAN1_TX++;    
              HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13); // 12 green, 13 orange, 14 red, 15 blue
           
              /* USER CODE END CAN1_TX_IRQn 1 */
            }
           
            /**
            * @brief This function handles CAN1 RX0 interrupts.
            */
            void CAN1_RX0_IRQHandler(void)
            {
              /* USER CODE BEGIN CAN1_RX0_IRQn 0 */
           
              /* USER CODE END CAN1_RX0_IRQn 0 */
              HAL_CAN_IRQHandler(&hcan1);
              /* USER CODE BEGIN CAN1_RX0_IRQn 1 */
           
                counter_CAN1_RX0++;
           
              /* USER CODE END CAN1_RX0_IRQn 1 */
            }
           
            /**
            * @brief This function handles TIM2 global interrupt.
            */
            void TIM2_IRQHandler(void)
            {
              /* USER CODE BEGIN TIM2_IRQn 0 */
           
              /* USER CODE END TIM2_IRQn 0 */
              HAL_TIM_IRQHandler(&htim2);
              /* USER CODE BEGIN TIM2_IRQn 1 */
                // 10 ms period
                tim2_counter++;
                
                if(tim2_counter == 100) // 1 sec
                {
                    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); // 12 green, 13 orange, 14 red, 15 blue
                    tim2_1sec_coutner++; // 1 sec        
                    tim2_counter = 0;
                }
           
                CAN1_Tx_Status = HAL_CAN_Transmit_IT(&hcan1); // tx start?
           
            //    for debugging        
                hcan1_status = HAL_CAN_GetState(&hcan1);
                hcan1_error = HAL_CAN_GetError(&hcan1);
                hcan2_status = HAL_CAN_GetState(&hcan2);
                hcan2_error = HAL_CAN_GetError(&hcan2);
                hcan1_ESR = hcan1.Instance->ESR;
                hcan2_ESR = hcan2.Instance->ESR;
                
              /* USER CODE END TIM2_IRQn 1 */
            }
           
            /**
            * @brief This function handles CAN2 TX interrupts.
            */
            void CAN2_TX_IRQHandler(void)
            {
              /* USER CODE BEGIN CAN2_TX_IRQn 0 */
           
              /* USER CODE END CAN2_TX_IRQn 0 */
              HAL_CAN_IRQHandler(&hcan2);
              /* USER CODE BEGIN CAN2_TX_IRQn 1 */
           
              /* USER CODE END CAN2_TX_IRQn 1 */
            }
           
            /**
            * @brief This function handles CAN2 RX0 interrupts.
            */
            void CAN2_RX0_IRQHandler(void)
            {
              /* USER CODE BEGIN CAN2_RX0_IRQn 0 */
           
              /* USER CODE END CAN2_RX0_IRQn 0 */
              HAL_CAN_IRQHandler(&hcan2);
              /* USER CODE BEGIN CAN2_RX0_IRQn 1 */
           
                counter_CAN2_RX0++;
                HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14); // 12 green, 13 orange, 14 red, 15 blue
           
                CAN2_Rx_Status = HAL_CAN_Receive_IT(&hcan2, CAN_FIFO0); // tx start?
           
                CAN2_ID = pRxMsg_CAN2.StdId;
                CAN2_DATA[0] = pRxMsg_CAN2.Data[0];
                CAN2_DATA[1] = pRxMsg_CAN2.Data[1];
           
              /* USER CODE END CAN2_RX0_IRQn 1 */
            }
           
            /* USER CODE BEGIN 1 */
           
            /* USER CODE END 1 */
            /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
           

Attachments

Outcomes