can.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. ******************************************************************************
  3. * File Name : CAN.c
  4. * Description : This file provides code for the configuration
  5. * of the CAN instances.
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2017 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "can.h"
  36. #include "gpio.h"
  37. /* USER CODE BEGIN 0 */
  38. /* USER CODE END 0 */
  39. CAN_HandleTypeDef hcan;
  40. #if defined CAN_250K
  41. uint16_t BaudrateByDefine = 18;
  42. #elif defined CAN_125K
  43. uint16_t BaudrateByDefine = 36;
  44. #else
  45. Error;
  46. #endif
  47. /* CAN init function */
  48. void MX_CAN_Init(void)
  49. {
  50. hcan.Instance = CAN1;
  51. uint32_t Bd = *(uint32_t*)(0x08018000); //取App波特率参数
  52. if(Bd == 0xAA55AA12)
  53. hcan.Init.Prescaler = 0x12; //250K
  54. else if(Bd == 0xAA55AA24)
  55. hcan.Init.Prescaler = 0x24; //125K
  56. else
  57. hcan.Init.Prescaler = BaudrateByDefine; //根据宏定义确定波特率
  58. hcan.Init.Mode = CAN_MODE_NORMAL;
  59. hcan.Init.SJW = CAN_SJW_1TQ;
  60. hcan.Init.BS1 = CAN_BS1_6TQ;
  61. hcan.Init.BS2 = CAN_BS2_1TQ;
  62. hcan.Init.TTCM = DISABLE;
  63. hcan.Init.ABOM = ENABLE;
  64. hcan.Init.AWUM = ENABLE;
  65. hcan.Init.NART = DISABLE;
  66. hcan.Init.RFLM = DISABLE;
  67. hcan.Init.TXFP = DISABLE;
  68. if (HAL_CAN_Init(&hcan) != HAL_OK)
  69. {
  70. Error_Handler();
  71. }
  72. }
  73. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  74. {
  75. GPIO_InitTypeDef GPIO_InitStruct;
  76. if(canHandle->Instance==CAN1)
  77. {
  78. /* USER CODE BEGIN CAN1_MspInit 0 */
  79. static CanTxMsgTypeDef TxMessage;
  80. static CanRxMsgTypeDef RxMessage;
  81. /* USER CODE END CAN1_MspInit 0 */
  82. /* Peripheral clock enable */
  83. __HAL_RCC_CAN1_CLK_ENABLE();
  84. /**CAN GPIO Configuration
  85. PB8 ------> CAN_RX
  86. PB9 ------> CAN_TX
  87. */
  88. GPIO_InitStruct.Pin = GPIO_PIN_8;
  89. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  90. GPIO_InitStruct.Pull = GPIO_NOPULL;
  91. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  92. GPIO_InitStruct.Pin = GPIO_PIN_9;
  93. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  95. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  96. __HAL_AFIO_REMAP_CAN1_2();
  97. /* Peripheral interrupt init */
  98. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  99. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  100. /* USER CODE BEGIN CAN1_MspInit 1 */
  101. hcan.pTxMsg = &TxMessage;
  102. hcan.pRxMsg = &RxMessage;
  103. Can_Filter_Config();
  104. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  105. /* USER CODE END CAN1_MspInit 1 */
  106. }
  107. }
  108. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  109. {
  110. if(canHandle->Instance==CAN1)
  111. {
  112. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  113. /* USER CODE END CAN1_MspDeInit 0 */
  114. /* Peripheral clock disable */
  115. __HAL_RCC_CAN1_CLK_DISABLE();
  116. /**CAN GPIO Configuration
  117. PB8 ------> CAN_RX
  118. PB9 ------> CAN_TX
  119. */
  120. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
  121. /* Peripheral interrupt Deinit*/
  122. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  123. }
  124. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  125. /* USER CODE END CAN1_MspDeInit 1 */
  126. }
  127. /* USER CODE BEGIN 1 */
  128. #define ID_TO_MC_FILTER 0x701
  129. #define ID_TO_MC_MASK 0x70F
  130. #define ID_BC_FILTER 0x700
  131. #define ID_BC_MASK 0x70F
  132. void Can_Filter_Config(void)
  133. {
  134. CAN_FilterConfTypeDef sFilterConfig;
  135. // sFilterConfig.FilterNumber = 0;
  136. // sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  137. // sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  138. // sFilterConfig.FilterIdHigh = 0x0000;
  139. // sFilterConfig.FilterIdLow = 0x0000;
  140. // sFilterConfig.FilterMaskIdHigh = 0x0000;
  141. // sFilterConfig.FilterMaskIdLow = 0x0000;
  142. // sFilterConfig.FilterFIFOAssignment = 0;
  143. // sFilterConfig.FilterActivation = ENABLE;
  144. // sFilterConfig.BankNumber = 14;
  145. // HAL_CAN_ConfigFilter(&hcan, &sFilterConfig);
  146. //设置过滤器组0,指定接收发送给PBU的数据
  147. sFilterConfig.FilterNumber = 0;
  148. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  149. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  150. //设置过滤寄存器
  151. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000) >> 16);
  152. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  153. //设置屏蔽寄存器
  154. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000) >> 16);;
  155. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  156. sFilterConfig.FilterFIFOAssignment = 0;
  157. sFilterConfig.FilterActivation = ENABLE;
  158. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. //设置过滤器组1,指定接收广播数据
  163. sFilterConfig.FilterNumber = 1;
  164. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  165. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  166. //设置过滤寄存器
  167. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000) >> 16);
  168. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  169. //设置屏蔽寄存器
  170. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000) >> 16);;
  171. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  172. sFilterConfig.FilterFIFOAssignment = 0;
  173. sFilterConfig.FilterActivation = ENABLE;
  174. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. }
  179. /* USER CODE END 1 */
  180. /**
  181. * @}
  182. */
  183. /**
  184. * @}
  185. */
  186. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/