can.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file can.c
  5. * @brief This file provides code for the configuration
  6. * of the CAN instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "can.h"
  22. /* USER CODE BEGIN 0 */
  23. void MX_CAN1_Filer(void)
  24. {
  25. CAN_FilterTypeDef CAN1_FilerConf;
  26. CAN1_FilerConf.FilterIdHigh = 0; // 过滤掉不是发送给本机的数据帧//32位ID
  27. CAN1_FilerConf.FilterIdLow = 0; // 确保收到的是标准数据帧
  28. CAN1_FilerConf.FilterMaskIdHigh = 0; // 32位MASK
  29. CAN1_FilerConf.FilterMaskIdLow = 0; // 0xFFFF;
  30. CAN1_FilerConf.FilterFIFOAssignment = CAN_FILTER_FIFO0; // 过滤器0关联到FIFO0
  31. CAN1_FilerConf.FilterBank = 0; // 过滤器0
  32. CAN1_FilerConf.FilterMode = CAN_FILTERMODE_IDMASK; // 屏蔽位模式
  33. CAN1_FilerConf.FilterScale = CAN_FILTERSCALE_32BIT;
  34. CAN1_FilerConf.FilterActivation = ENABLE;
  35. CAN1_FilerConf.SlaveStartFilterBank = 14;
  36. if (HAL_CAN_ConfigFilter(&hcan, &CAN1_FilerConf) != HAL_OK)
  37. {
  38. Error_Handler();
  39. }
  40. }
  41. /* USER CODE END 0 */
  42. CAN_HandleTypeDef hcan;
  43. /* CAN init function */
  44. void MX_CAN_Init(void)
  45. {
  46. /* USER CODE BEGIN CAN_Init 0 */
  47. /* USER CODE END CAN_Init 0 */
  48. /* USER CODE BEGIN CAN_Init 1 */
  49. /* USER CODE END CAN_Init 1 */
  50. hcan.Instance = CAN1;
  51. hcan.Init.Prescaler = 18;
  52. hcan.Init.Mode = CAN_MODE_NORMAL;
  53. hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  54. hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
  55. hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  56. hcan.Init.TimeTriggeredMode = DISABLE;
  57. hcan.Init.AutoBusOff = ENABLE;
  58. hcan.Init.AutoWakeUp = DISABLE;
  59. hcan.Init.AutoRetransmission = DISABLE;
  60. hcan.Init.ReceiveFifoLocked = DISABLE;
  61. hcan.Init.TransmitFifoPriority = DISABLE;
  62. if (HAL_CAN_Init(&hcan) != HAL_OK)
  63. {
  64. Error_Handler();
  65. }
  66. /* USER CODE BEGIN CAN_Init 2 */
  67. MX_CAN1_Filer();
  68. if (HAL_CAN_Start(&hcan) != HAL_OK)
  69. {
  70. Error_Handler();
  71. }
  72. if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  73. {
  74. Error_Handler();
  75. }
  76. /* USER CODE END CAN_Init 2 */
  77. }
  78. void HAL_CAN_MspInit(CAN_HandleTypeDef *canHandle)
  79. {
  80. GPIO_InitTypeDef GPIO_InitStruct = {0};
  81. if (canHandle->Instance == CAN1)
  82. {
  83. /* USER CODE BEGIN CAN1_MspInit 0 */
  84. /* USER CODE END CAN1_MspInit 0 */
  85. /* CAN1 clock enable */
  86. __HAL_RCC_CAN1_CLK_ENABLE();
  87. __HAL_RCC_GPIOB_CLK_ENABLE();
  88. /**CAN GPIO Configuration
  89. PB8 ------> CAN_RX
  90. PB9 ------> CAN_TX
  91. */
  92. GPIO_InitStruct.Pin = GPIO_PIN_8;
  93. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  94. GPIO_InitStruct.Pull = GPIO_PULLUP;
  95. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  96. GPIO_InitStruct.Pin = GPIO_PIN_9;
  97. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  98. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  99. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  100. __HAL_AFIO_REMAP_CAN1_2();
  101. /* CAN1 interrupt Init */
  102. HAL_NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 0, 0);
  103. HAL_NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
  104. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  105. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  106. /* USER CODE BEGIN CAN1_MspInit 1 */
  107. /* USER CODE END CAN1_MspInit 1 */
  108. }
  109. }
  110. void HAL_CAN_MspDeInit(CAN_HandleTypeDef *canHandle)
  111. {
  112. if (canHandle->Instance == CAN1)
  113. {
  114. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  115. /* USER CODE END CAN1_MspDeInit 0 */
  116. /* Peripheral clock disable */
  117. __HAL_RCC_CAN1_CLK_DISABLE();
  118. /**CAN GPIO Configuration
  119. PB8 ------> CAN_RX
  120. PB9 ------> CAN_TX
  121. */
  122. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8 | GPIO_PIN_9);
  123. /* CAN1 interrupt Deinit */
  124. /* USER CODE BEGIN CAN1:USB_HP_CAN1_TX_IRQn disable */
  125. /**
  126. * Uncomment the line below to disable the "USB_HP_CAN1_TX_IRQn" interrupt
  127. * Be aware, disabling shared interrupt may affect other IPs
  128. */
  129. /* HAL_NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); */
  130. /* USER CODE END CAN1:USB_HP_CAN1_TX_IRQn disable */
  131. /* USER CODE BEGIN CAN1:USB_LP_CAN1_RX0_IRQn disable */
  132. /**
  133. * Uncomment the line below to disable the "USB_LP_CAN1_RX0_IRQn" interrupt
  134. * Be aware, disabling shared interrupt may affect other IPs
  135. */
  136. /* HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); */
  137. /* USER CODE END CAN1:USB_LP_CAN1_RX0_IRQn disable */
  138. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  139. /* USER CODE END CAN1_MspDeInit 1 */
  140. }
  141. }
  142. /* USER CODE BEGIN 1 */
  143. /* USER CODE END 1 */