STM32F429 没有收到 CAN 报文
Posted
技术标签:
【中文标题】STM32F429 没有收到 CAN 报文【英文标题】:STM32F429 is not receiving the CAN Message 【发布时间】:2015-02-03 14:36:58 【问题描述】:我正在使用 STM32F429 微控制器,需要在 CAN2 和 PCAN View 之间实现 CAN 总线通信。我能够从 CAN2 传输消息,但我无法接收任何消息。我在微控制器中使用 TJA1041A CAN 收发器。问题是在调试过程中,我的 CAN 总线已正确初始化,但尽管我已初始化 FIFO0,但它并没有进入接收命令。特此附上程序以供进一步参考。我使用 STM32 HAL Cube 进行编程。
/**
******************************************************************************
* File Name : main.c
* Date : 05/12/2014 09:43:55
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2014 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);
static void MX_CAN2_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
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();
/* USER CODE BEGIN 2 */
__GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_Initpins;
GPIO_Initpins.Mode = GPIO_MODE_OUTPUT_PP ;
GPIO_Initpins.Pin = GPIO_PIN_5|GPIO_PIN_7;
GPIO_Initpins.Pull = GPIO_NOPULL ;
HAL_GPIO_Init(GPIOD, &GPIO_Initpins);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_SET);
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
/* Infinite loop */
CanTxMsgTypeDef TxMess;
TxMess.StdId = 0x123;
TxMess.DLC = 0x0;
TxMess.Data[0] = 0x12;
TxMess.IDE = 0;
TxMess.RTR = 0;
hcan2.pTxMsg = &TxMess;
HAL_CAN_Transmit(&hcan2,50);
HAL_Delay(500);
/* USER CODE END 2 */
CanRxMsgTypeDef RMess;
RMess.FIFONumber = CAN_FIFO1;
RMess.FMI = 14;
RMess.StdId = 0x541;
RMess.DLC = 0;
RMess.RTR = 0;
RMess.IDE = CAN_ID_STD;
hcan2.pRxMsg = &RMess;
HAL_CAN_Receive_IT(&hcan2,CAN_FIFO1);
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
HAL_CAN_Receive(&hcan2,CAN_FIFO1,0);
/* 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_SCALE3);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
/* 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);
/*CAN_FilterConfTypeDef CAN_Filters;
CAN_Filters.BankNumber = 0;
CAN_Filters.FilterActivation = ENABLE;
CAN_Filters.FilterFIFOAssignment = CAN_FILTER_FIFO0 ;
CAN_Filters.FilterIdHigh = 0x00;
CAN_Filters.FilterIdLow = 0x00;
CAN_Filters.FilterMaskIdHigh = 0x00;
CAN_Filters.FilterMaskIdLow = 0x00;
CAN_Filters.FilterMode = CAN_FILTERMODE_IDMASK;
CAN_Filters.FilterNumber = 0;
CAN_Filters.FilterScale = CAN_FILTERSCALE_32BIT;
HAL_CAN_ConfigFilter(&hcan1, &CAN_Filters);*/
/* CAN2 init function */
void MX_CAN2_Init(void)
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 2;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SJW = CAN_SJW_1TQ;
hcan2.Init.BS1 = CAN_BS1_5TQ;
hcan2.Init.BS2 = CAN_BS2_2TQ;
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);
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
/* GPIO Ports Clock Enable */
__GPIOH_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
#endif
/**
* @
*/
/**
* @
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
谢谢
【问题讨论】:
有人找到解决上述问题的方法了吗? 【参考方案1】:如果CAN2可以发送报文,那么CAN1(主)和CAN2(从)之间的时钟和主从配置都可以。
如果有接收问题,这些应该与 FIFO 配置或验收过滤器配置有关。
您配置了 FIFO,但您似乎没有配置“CAN2StartBank”。 CAN1 和 CAN2 也共享滤波器组,默认情况下,滤波器组被分成两半,因此前半部分用于 CAN1,后半部分用于 CAN2。
来自参考手册章节“CAN过滤器寄存器”:
CAN 过滤器主寄存器 (CAN_FMR)
地址偏移:0x200 复位值:0x2A1C 0E01 这个所有位
寄存器由软件设置和清除。 ... CAN2SB[5:0]: CAN2 start bank 这些位由
设置和清除软件。它们在
中定义了 CAN2 接口(Slave)的起始组范围 0 到 27。注意:当 CAN2SB[5:0] = 28d 时,所有的过滤器到
可以使用CAN1。当 CAN2SB[5:0] 设置为 0 时,不分配滤波器 到 CAN1。
你设置了
CAN_Filters.BankNumber = 0;
但你应该从后半部分选择一个默认设置的银行,或者如果你不需要 CAN1,请将 CAN2StartBank 值更改为 0。
您还必须确保在过滤器配置期间过滤器处于初始化模式。
CAN 过滤器主寄存器 (CAN_FMR)
Bit 0 FINIT:滤波器初始化模式滤波器组的初始化模式
0:主动过滤器模式。
1:过滤器的初始化模式
遗憾的是,我现在没有硬件可以自行测试。
【讨论】:
感谢您的回复...即使更改了我的 BankNumber = 14,我也无法从 PCAN 设备收到任何消息。 CAN1->FMR &= ~((uint32_t)CAN_FMR_CAN2SB); CAN1->FMR |= (uint32_t)(sFilterConfig->BankNumber 以上是 Slave Bank 的声明,我只使用 CAn2,因为我不需要 CAn1 连接【参考方案2】:您应该为 CAN2 接口取一个 FilterNumber 14;默认情况下,RESET 位 CAN2SB[5:0] 初始化为 01110 = dec 14 - 因此 CAN1 接口应该使用过滤器 0..13 和 CAN2 14..27。您可以通过 CAN_SlaveStartBank(uint_8) 方法将起始 bank 从 14 更改为 1..27 内的任何值。
【讨论】:
【参考方案3】:尝试将CAN2接口的Filterbank设置为14以上,似乎bank设置有问题。我们也有同样的问题,但是设置15后就可以了。
【讨论】:
【参考方案4】:检查CAN FMR
注册。 CAN2SB
在CAN1
和CAN2
外设寄存器中的默认值是0x0E
。您应该将CAN1FMR->CANS2B
设置为0x00
,并将CAN2FMR->CANS2B
设置为0x0E
(任意但不相同的数字)。因此,您将过滤器组分成两半,它可以正常工作
【讨论】:
以上是关于STM32F429 没有收到 CAN 报文的主要内容,如果未能解决你的问题,请参考以下文章