can.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. ******************************************************************************
  3. * File Name : CAN.c
  4. * Description : This file provides code for the configuration
  5. * of the CAN instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "can.h"
  41. #include "gpio.h"
  42. /* USER CODE BEGIN 0 */
  43. CanTxMsgTypeDef CAN_TxMessaage;
  44. CanRxMsgTypeDef CAN_RxMessaage;
  45. uint8_t CAN_RxBuf_MC[255];
  46. CAN_Buf_TypeDef CAN_RxBuf_Struct_MC = {0,255,0,0,0,0,CAN_RxBuf_MC};
  47. uint8_t CAN_RxBuf_BMS[255];
  48. CAN_Buf_TypeDef CAN_RxBuf_Struct_BMS = {0,255,0,0,0,0,CAN_RxBuf_BMS};
  49. uint8_t CAN_RxBuf_HMI[255];
  50. CAN_Buf_TypeDef CAN_RxBuf_Struct_HMI = {0,255,0,0,0,0,CAN_RxBuf_HMI};
  51. uint8_t CAN_RxBuf_CDL[255];
  52. CAN_Buf_TypeDef CAN_RxBuf_Struct_CDL = {0,255,0,0,0,0,CAN_RxBuf_CDL};
  53. /* USER CODE END 0 */
  54. CAN_HandleTypeDef hcan;
  55. /* CAN init function */
  56. void MX_CAN_Init(void)
  57. {
  58. hcan.Instance = CAN1;
  59. hcan.Init.Prescaler = 24;
  60. hcan.Init.Mode = CAN_MODE_NORMAL;
  61. hcan.Init.SJW = CAN_SJW_1TQ;
  62. hcan.Init.BS1 = CAN_BS1_4TQ;
  63. hcan.Init.BS2 = CAN_BS2_1TQ;
  64. hcan.Init.TTCM = DISABLE;
  65. hcan.Init.ABOM = ENABLE;
  66. hcan.Init.AWUM = ENABLE;
  67. hcan.Init.NART = DISABLE;
  68. hcan.Init.RFLM = DISABLE;
  69. hcan.Init.TXFP = DISABLE;
  70. if (HAL_CAN_Init(&hcan) != HAL_OK)
  71. {
  72. _Error_Handler(__FILE__, __LINE__);
  73. }
  74. }
  75. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  76. {
  77. GPIO_InitTypeDef GPIO_InitStruct;
  78. if(canHandle->Instance==CAN1)
  79. {
  80. /* USER CODE BEGIN CAN1_MspInit 0 */
  81. /* USER CODE END CAN1_MspInit 0 */
  82. /* CAN1 clock enable */
  83. __HAL_RCC_CAN1_CLK_ENABLE();
  84. /**CAN GPIO Configuration
  85. PA11 ------> CAN_RX
  86. PA12 ------> CAN_TX
  87. */
  88. GPIO_InitStruct.Pin = GPIO_PIN_11;
  89. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  90. GPIO_InitStruct.Pull = GPIO_NOPULL;
  91. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  92. GPIO_InitStruct.Pin = GPIO_PIN_12;
  93. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  95. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  96. /* CAN1 interrupt Init */
  97. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 1, 1);
  98. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  99. /* USER CODE BEGIN CAN1_MspInit 1 */
  100. canHandle->pTxMsg = &CAN_TxMessaage;
  101. canHandle->pRxMsg = &CAN_RxMessaage;
  102. CANFilterConfig_Scale32_IdMask_StandardIdOnly();
  103. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  104. /* USER CODE END CAN1_MspInit 1 */
  105. }
  106. }
  107. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  108. {
  109. if(canHandle->Instance==CAN1)
  110. {
  111. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  112. /* USER CODE END CAN1_MspDeInit 0 */
  113. /* Peripheral clock disable */
  114. __HAL_RCC_CAN1_CLK_DISABLE();
  115. /**CAN GPIO Configuration
  116. PA11 ------> CAN_RX
  117. PA12 ------> CAN_TX
  118. */
  119. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
  120. /* CAN1 interrupt Deinit */
  121. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  122. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  123. /* USER CODE END CAN1_MspDeInit 1 */
  124. }
  125. }
  126. /* USER CODE BEGIN 1 */
  127. //CAN接收数据处理
  128. void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,uint8_t ucLength)
  129. {
  130. for(uint8_t i=0;i<ucLength;i++)
  131. {
  132. *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
  133. if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
  134. {
  135. (* ptCANRx).ucBufWrInde = 0;
  136. }
  137. if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
  138. {
  139. (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
  140. (* ptCANRx).ucBufOvf = 1;
  141. }
  142. }
  143. }
  144. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  145. {
  146. if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  147. {
  148. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  149. return;
  150. }
  151. switch(CanHandle->pRxMsg->StdId)
  152. {
  153. case ID_MC_BC:
  154. case ID_MC_TO_PBU://接收MC数据
  155. {
  156. CAN_RxBuf_Struct_MC.ucBufID = CanHandle->pRxMsg->StdId;
  157. CAN_Rx_ISR(&CAN_RxBuf_Struct_MC,CanHandle->pRxMsg->DLC);
  158. break;
  159. }
  160. case ID_BMS_BC:
  161. case ID_BMS_TO_PBU://接收BMS数据
  162. {
  163. CAN_RxBuf_Struct_BMS.ucBufID = CanHandle->pRxMsg->StdId;
  164. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS,CanHandle->pRxMsg->DLC);
  165. break;
  166. }
  167. case ID_HMI_BC:
  168. case ID_HMI_TO_PBU://接收HMI数据
  169. {
  170. CAN_RxBuf_Struct_HMI.ucBufID = CanHandle->pRxMsg->StdId;
  171. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI,CanHandle->pRxMsg->DLC);
  172. break;
  173. }
  174. case ID_CDL_BC:
  175. case ID_CDL_TO_PBU_TE:
  176. case ID_CDL_TO_PBU://接收CDL数据
  177. {
  178. CAN_RxBuf_Struct_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  179. CAN_Rx_ISR(&CAN_RxBuf_Struct_CDL,CanHandle->pRxMsg->DLC);
  180. break;
  181. }
  182. default:break;
  183. }
  184. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  185. }
  186. //CAN发送数据
  187. void CAN_SendData(uint16_t ID, uint8_t *Data, uint16_t Length)
  188. {
  189. uint16_t LastPacketLen, PacketNum;
  190. uint16_t i,j;
  191. if(Length > 0)
  192. {
  193. //计算分包个数
  194. LastPacketLen = Length % 8;//最后一个数据包长度
  195. if(LastPacketLen == 0)
  196. {
  197. LastPacketLen = 8;
  198. PacketNum = Length / 8;
  199. }
  200. else
  201. {
  202. PacketNum = Length / 8 + 1;
  203. }
  204. //开始发送数据
  205. hcan.pTxMsg->StdId = ID;
  206. hcan.pTxMsg->RTR = CAN_RTR_DATA;
  207. hcan.pTxMsg->IDE = CAN_ID_STD;
  208. //发送前(PacketNum - 1)个数据包
  209. for(i = 0;i < (PacketNum - 1); i++)
  210. {
  211. hcan.pTxMsg->DLC = 8;
  212. for(j = 0;j < 8;j++)
  213. {
  214. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  215. }
  216. HAL_CAN_Transmit(&hcan, 10);
  217. }
  218. //发送最后一个数据包
  219. hcan.pTxMsg->DLC = LastPacketLen;
  220. for(j = 0;j < LastPacketLen;j++)
  221. {
  222. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  223. }
  224. HAL_CAN_Transmit(&hcan, 10);
  225. }
  226. }
  227. //过滤器设置
  228. void CANFilterConfig_Scale32_IdMask_StandardIdOnly(void)
  229. {
  230. CAN_FilterConfTypeDef sFilterConfig;
  231. //设置过滤器组0,指定接收发送给PBU的数据
  232. sFilterConfig.FilterNumber = 0;
  233. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  234. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  235. //设置过滤寄存器
  236. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)(0x0703&0X605) << 21) & 0xFFFF0000) >> 16);//0x0703&0X605=0X601
  237. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)(0x0703&0X605) << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  238. //设置屏蔽寄存器
  239. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)(0x070F&0X609) << 21) & 0xFFFF0000) >> 16);//0x070F&0X60F=0X609
  240. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  241. sFilterConfig.FilterFIFOAssignment = 0;
  242. sFilterConfig.FilterActivation = ENABLE;
  243. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  244. {
  245. Error_Handler();
  246. }
  247. //设置过滤器组1,指定接收广播数据
  248. sFilterConfig.FilterNumber = 1;
  249. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  250. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  251. //设置过滤寄存器
  252. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)0x0700 << 21) & 0xFFFF0000) >> 16);
  253. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)0x0700 << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  254. //设置屏蔽寄存器
  255. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)0x070F << 21) & 0xFFFF0000) >> 16);;
  256. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  257. sFilterConfig.FilterFIFOAssignment = 0;
  258. sFilterConfig.FilterActivation = ENABLE;
  259. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  260. {
  261. Error_Handler();
  262. }
  263. }
  264. void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  265. {
  266. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP0);
  267. }
  268. /* USER CODE END 1 */
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */
  275. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/