赞
踩
https://shequ.stmicroelectronics.cn/thread-619797-1-1.html
《STM32中文参考手册》
STM32的CAN支持时间触发模式,所谓时间触发,实际上就是记录当下发送或接收一帧CAN数据时的时间数据,这个时间数据并不是和平常意义上的时间戳那样从1970年1月1日开始记录到当下的时间,而是一个普普通通的16位计数器的计数值。
工作原理是,当使能STM32的时间触发功能后,CAN内部的一个16位计数器就开始从0计数,计满65535后归零再开始一个新的计数周期,循环往复......这个计数器的计数频率是CAN的波特率(每个CAN位时间计数一次)
然鹅STM32使用时间触发模式是有一个局限的,必须要保证发送帧的长度为8字节,而且要关闭自动重传功能!使能该模式后,每当发送一个CAN数据帧时,时间戳数据会自动覆盖在发送帧的第7和第8字节上,所以用这个功能时得小心点,控制咱自己每次的发送数据长度在6个字节以内,并且在第1到第6字节上(如果头铁非要放在第7第8字节上的话,时间戳终究还是会覆盖掉自己的数据的......)
STM32在使用CAN时间触发模式时的相关寄存器:
关注位7和位4,在使用时间触发模式功能时,TTCM = 1(允许时间触发模式)NART = 1(禁止自动重传
TDTxR寄存器的高16位就是那个一直在工作的计数器,发送一个CAN帧时,时间戳信息通过访问这个计数器来实现的(工作状态下为只读)
关注位31-16、位8、位3-0,
TIME[15:0] :记录时间戳计数器值
TGT = 1,发送CAN数据时将TIME[15:0]的值覆盖在第7、第8字节
DLC = 8,发送数据帧长度必须为8字节
关注位13-16,TIME[15:0]记录时间戳计数器值,接收一个CAN数据帧时,通过访问这个计数器来获取时间戳。
波特率为500k,开启CAN1的接收中断,使能时间触发模式。
首先定义好局部变量和全局变量以便测试:
can.c中:
can.h中:
CAN初始化函数:
这里注意,一定一定一定要关闭自动重传和使能时间触发:
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.TimeTriggeredMode = ENABLE;
就是用来配置MCR寄存器中的NART位和TTCM位,通过调用HAL_CAN_Init(&hcan)实现寄存器各位的赋值。详细过程在stm32f1xx_hal_can.c中的HAL_CAN_Init(CAN_HandleTypeDef *hcan)函数中实现:
HAL官方库函数HAL_CAN_Init(CAN_HandleTypeDef *hcan)中对MCR寄存器的TTMC和NART位的配置
在can的初始化函数中再添加滤波器配置,并使能can中断和开启can外设。
这里开启FIFO-0,掩码均为0x0000,来者不拒~
STM32官方定义的CAN_TxHeaderTypeDef 结构体有 StdId、ExtId、IDE、RTR、DLC、TransmitGlobalTime几个成员变量,因此我们在发送函数中进行相应的赋值:
can发送函数:
其中Tx_Header.TransmitGlobalTime = ENABLE; 就是将TDTxR寄存器中的TGT位置1,否则无法正常使用发送时间戳功能!!(为啥我要强调这里,就是因为之前在这里踩了个大坑...找了半天原因才发现...)
编写CAN接收函数,虽然STM32官方CAN_RxHeaderTypeDef结构体有如下图中所示成员变量,但是其中的赋值都是通过官方库函数HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &Rx_Header, buf)实现的,所以这里无需我们再操作,只需调用 HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &Rx_Header, buf)
这个函数,并返回Timestamp值就可以了~
can接收函数:
在stm32f1xx_hal_can.c中有一个神奇的官方HAL库函数:
uint32_t HAL_CAN_GetTxTimestamp(const CAN_HandleTypeDef *hacn, uint32_t TxMailbox),它的作用就是返回发送一帧CAN数据时的时间戳信息。
但是TxMailbox参数被限定在以下几个范围:
意思就是在调用这个函数的时候就要明确目前使用的是哪一个发送邮箱!可是这我们该咋知道呢?哎~其实同样在stm32f1xx_hal_can.c中的发送函数中:
HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, const CAN_TxHeaderTypeDef *pHeader, const uint8_t aData[], uint32_t *pTxMailbox)
其中定义了两个 32位无符号整型变量 transmitmailbox,以及tsr 并通过READ_REG进行赋值。
tsr就是读取了目前TSR寄存器的状态:
注意位28、27、26就是用来记录当前三个发送邮箱的工作状态,位25-24表示下一个空的邮箱号。CAN_TSR_TME0 是 0000 0100 0000 0000 0000 0000 0000 0000,对应TSR寄存器中位26为1,同理CAN_TSR_TME1和CAN_TSR_TME2对应位27、位28为1,其他位均为0,再和tsr按位相与,如果为0,说明对应的发送邮箱非空!如果不为0,则对应的发送邮箱为空!
因此下面的代码就很好理解了,如果if成立,则至少有一个邮箱是空的。
CAN_TSR_CODE 为 0000 0011 0000 0000 0000 0000 0000 0000,对应TSR寄存器中位25-24,tsr & CAN_TSR_CODE 获取当前可以使用的空邮箱号(即将要发送CAN帧要用的空邮箱号,就是我们uint32_t HAL_CAN_GetTxTimestamp(const CAN_HandleTypeDef *hacn, uint32_t TxMailbox)函数中需要传入的TxMailbox参数!),然后再通过移位的操作得到真实值。
因此我们这里编写一个获取发送时间戳函数:
中断处理就很简单,基本没啥操作,手动添加一个标志位(main中清零),并且获取接收时刻的时间戳值就行了。
主函数while(1)循环编写如下代码:
直接运行代码,单片机以1Hz的频率向CAN总线发送数据帧:
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
通过串口调试助手和USBCAN上位机软件测试,CAN数据帧的最后两个字节被时间戳信息覆盖,并且和串口打印出来的时间戳值保持一致!起飞~
为了便于测试接收功能,消除延迟函数可能带来的麻烦,注释掉while(1)中的can发送和HAL_Delay部分:
运行代码,USBCAN上位机软件以1Hz的频率向CAN总线发送10条CAN数据,上位机软件同步接收到10个时间戳信息~
源码贴这了,欢迎各路大佬批评指正~
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "can.h"
- #include "usart.h"
- #include "gpio.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 */
- /* 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 -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
-
- /* 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_CAN_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
-
- /******can接收时间戳功能验证******/
-
- if(canRx0 != 0)
- {
- canRx0 = 0; //复位中断标志
- printf("receive time = %x\r\n",globalTime); //打印接收时间戳
- }
-
- /******can接收时间戳功能验证******/
-
-
- /******can发送时间戳功能验证******/
- /*
- myCanTransmit(); //发送can数据帧
- uint32_t time = getTxGlobalTime(); //获取时间戳
- printf("transmit time = %x\r\n",time); //打印验证
- */
- /******can发送时间戳功能验证******/
-
-
- //HAL_Delay(1000);
-
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /* USER CODE BEGIN 4 */
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #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 /* USE_FULL_ASSERT */
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file can.c
- * @brief This file provides code for the configuration
- * of the CAN instances.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "can.h"
-
- /* USER CODE BEGIN 0 */
-
- static CAN_FilterTypeDef sFilterConfig; //接收FIFO及滤波器配置结构体
- static CAN_TxHeaderTypeDef Tx_Header; //CAN发送配置结构体
- static CAN_RxHeaderTypeDef Rx_Header; //CAN接收配置结构体
- uint8_t canTransmit[8] = {1,2,3,4,5,6,7,8}; //can发送测试帧
- uint8_t canRev[8] = {0}; //can接收数组,用于接收数据
- uint16_t globalTime = 0; //用于can中断接收时间戳
- uint8_t canRx0 = 0; //CAN1接收中断标志位
- /* USER CODE END 0 */
-
- CAN_HandleTypeDef hcan;
-
- /* CAN init function */
- void MX_CAN_Init(void)
- {
-
- /* USER CODE BEGIN CAN_Init 0 */
-
- /* USER CODE END CAN_Init 0 */
-
- /* USER CODE BEGIN CAN_Init 1 */
-
- /* USER CODE END CAN_Init 1 */
- hcan.Instance = CAN1;
- hcan.Init.Prescaler = 4;
- hcan.Init.Mode = CAN_MODE_NORMAL;
- hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan.Init.TimeSeg1 = CAN_BS1_9TQ;
- hcan.Init.TimeSeg2 = CAN_BS2_8TQ;
- hcan.Init.TimeTriggeredMode = ENABLE; //使能时间触发模式
- hcan.Init.AutoBusOff = DISABLE;
- hcan.Init.AutoWakeUp = DISABLE;
- hcan.Init.AutoRetransmission = DISABLE;
- hcan.Init.ReceiveFifoLocked = DISABLE;
- hcan.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN CAN_Init 2 */
-
- /*******CAN过滤器配置*******/
-
- sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
- sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
- sFilterConfig.FilterIdHigh = 0x0000;
- sFilterConfig.FilterIdLow = 0x0000;
- sFilterConfig.FilterMaskIdHigh = 0x0000;
- sFilterConfig.FilterMaskIdLow = 0x0000;
- sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; //配置FIFO-0
- sFilterConfig.FilterActivation = CAN_FILTER_ENABLE;
- sFilterConfig.FilterBank = 0;
- sFilterConfig.SlaveStartFilterBank = 0;
- if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
- {
- Error_Handler();
- }
-
- /*******CAN过滤器配置*******/
-
-
- /******CAN启动******/
-
- HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING); //开启FIFO-0 挂起中断
- HAL_CAN_Start(&hcan); //启动CAN外设
-
- /******CAN启动******/
-
-
- /* USER CODE END CAN_Init 2 */
-
- }
-
- void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
- {
-
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspInit 0 */
-
- /* USER CODE END CAN1_MspInit 0 */
- /* CAN1 clock enable */
- __HAL_RCC_CAN1_CLK_ENABLE();
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**CAN GPIO Configuration
- PA11 ------> CAN_RX
- PA12 ------> CAN_TX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_11;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_12;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- /* CAN1 interrupt Init */
- HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
- /* USER CODE BEGIN CAN1_MspInit 1 */
-
- /* USER CODE END CAN1_MspInit 1 */
- }
- }
-
- void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
- {
-
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspDeInit 0 */
-
- /* USER CODE END CAN1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_CAN1_CLK_DISABLE();
-
- /**CAN GPIO Configuration
- PA11 ------> CAN_RX
- PA12 ------> CAN_TX
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
-
- /* CAN1 interrupt Deinit */
- HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
- /* USER CODE BEGIN CAN1_MspDeInit 1 */
-
- /* USER CODE END CAN1_MspDeInit 1 */
- }
- }
-
- /* USER CODE BEGIN 1 */
-
- /******can发送函数******/
-
- void myCanTransmit(void)
- {
- uint32_t canTxMailbox;
- if((canTransmit != NULL))
- {
- Tx_Header.StdId = 0x00; //发送ID
- Tx_Header.IDE = CAN_ID_STD; //标准ID
- Tx_Header.RTR = CAN_RTR_DATA; //数据帧
- Tx_Header.DLC = 8; //发送长度必须为8位!!!
- Tx_Header.TransmitGlobalTime = ENABLE; //使能时间戳功能
- while(HAL_CAN_AddTxMessage(&hcan, &Tx_Header, canTransmit, &canTxMailbox) != HAL_OK); //将消息传入某个发送邮箱
- while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) != 3); //等待发送,发送完成后,空邮箱数目为0
- }
- }
-
- /******can发送函数******/
-
-
-
- /******can接收函数******/
-
- uint16_t myCanRev(uint8_t *buf)
- {
- while(HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &Rx_Header, buf) != HAL_OK); //can接收数据内容在 buf 数组中
- return Rx_Header.Timestamp; //返回时间戳信息
- }
-
- /******can接收函数******/
-
-
-
- /******can发送时间戳获取******/
-
- uint32_t getTxGlobalTime(void)
- {
- uint32_t tsr = READ_REG(hcan.Instance->TSR);
- if((tsr & CAN_TSR_TME0) != 0U)
- {
- return HAL_CAN_GetTxTimestamp(&hcan, CAN_TX_MAILBOX0);
- }
- else if((tsr & CAN_TSR_TME1) != 0U)
- {
- return HAL_CAN_GetTxTimestamp(&hcan, CAN_TX_MAILBOX1);
- }
- else return HAL_CAN_GetTxTimestamp(&hcan, CAN_TX_MAILBOX2);
- }
-
- /******can发送时间戳获取******/
-
-
- /* USER CODE END 1 */
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file can.h
- * @brief This file contains all the function prototypes for
- * the can.c file
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __CAN_H__
- #define __CAN_H__
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
-
- /* USER CODE BEGIN Includes */
-
- /* USER CODE END Includes */
-
- extern CAN_HandleTypeDef hcan;
-
- /* USER CODE BEGIN Private defines */
-
- /* USER CODE END Private defines */
-
- void MX_CAN_Init(void);
-
- /* USER CODE BEGIN Prototypes */
-
- extern uint8_t canTransmit[8];
- extern uint8_t canRev[8];
- extern uint16_t globalTime;
- uint16_t myCanRev(uint8_t *buf);
- void myCanTransmit(void);
- uint32_t getTxGlobalTime(void);
- extern uint8_t canRx0;
- /* USER CODE END Prototypes */
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif /* __CAN_H__ */
-
- void USB_LP_CAN1_RX0_IRQHandler(void)
- {
- /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
- canRx0 = 1;
- globalTime = myCanRev(canRev);
- /* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
- HAL_CAN_IRQHandler(&hcan);
- /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
-
- /* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。