can.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_PBU[255];
  46. CAN_Buf_TypeDef CAN_RxBuf_Struct_PBU = {0,255,0,0,0,0,CAN_RxBuf_PBU,0,FALSE};
  47. uint8_t CAN_RxBuf_BMS[255];
  48. CAN_Buf_TypeDef CAN_RxBuf_Struct_BMS = {0,255,0,0,0,0,CAN_RxBuf_BMS,0,FALSE};
  49. uint8_t CAN_RxBuf_HMI[255];
  50. CAN_Buf_TypeDef CAN_RxBuf_Struct_HMI = {0,255,0,0,0,0,CAN_RxBuf_HMI,0,FALSE};
  51. uint8_t CAN_RxBuf_CDL[255];
  52. CAN_Buf_TypeDef CAN_RxBuf_Struct_CDL = {0,255,0,0,0,0,CAN_RxBuf_CDL,0,FALSE};
  53. ComOK_Check_Struct_t IsComOK_PBU = {TRUE, 0};
  54. ComOK_Check_Struct_t IsComOK_HMI = {TRUE, 0};
  55. ComOK_Check_Struct_t IsComOK_BMS = {TRUE, 0};
  56. /* USER CODE END 0 */
  57. CAN_HandleTypeDef hcan;
  58. /* CAN init function */
  59. void MX_CAN_Init(void)
  60. {
  61. hcan.Instance = CAN1;
  62. hcan.Init.Prescaler = 48;
  63. hcan.Init.Mode = CAN_MODE_NORMAL;
  64. hcan.Init.SJW = CAN_SJW_1TQ;
  65. hcan.Init.BS1 = CAN_BS1_4TQ;
  66. hcan.Init.BS2 = CAN_BS2_1TQ;
  67. hcan.Init.TTCM = DISABLE;
  68. hcan.Init.ABOM = ENABLE;
  69. hcan.Init.AWUM = DISABLE;
  70. hcan.Init.NART = DISABLE;
  71. hcan.Init.RFLM = DISABLE;
  72. hcan.Init.TXFP = DISABLE;
  73. if (HAL_CAN_Init(&hcan) != HAL_OK)
  74. {
  75. _Error_Handler(__FILE__, __LINE__);
  76. }
  77. }
  78. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  79. {
  80. GPIO_InitTypeDef GPIO_InitStruct;
  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. /**CAN GPIO Configuration
  88. PB8 ------> CAN_RX
  89. PB9 ------> CAN_TX
  90. */
  91. GPIO_InitStruct.Pin = GPIO_PIN_8;
  92. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  95. GPIO_InitStruct.Pin = GPIO_PIN_9;
  96. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  97. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  98. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  99. __HAL_AFIO_REMAP_CAN1_2();
  100. /* CAN1 interrupt Init */
  101. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 1, 1);
  102. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  103. /* USER CODE BEGIN CAN1_MspInit 1 */
  104. canHandle->pTxMsg = &CAN_TxMessaage;
  105. canHandle->pRxMsg = &CAN_RxMessaage;
  106. CANFilterConfig_Scale32_IdMask_StandardIdOnly();
  107. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  108. /* USER CODE END CAN1_MspInit 1 */
  109. }
  110. }
  111. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  112. {
  113. if(canHandle->Instance==CAN1)
  114. {
  115. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  116. /* USER CODE END CAN1_MspDeInit 0 */
  117. /* Peripheral clock disable */
  118. __HAL_RCC_CAN1_CLK_DISABLE();
  119. /**CAN GPIO Configuration
  120. PB8 ------> CAN_RX
  121. PB9 ------> CAN_TX
  122. */
  123. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
  124. /* CAN1 interrupt Deinit */
  125. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  126. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  127. /* USER CODE END CAN1_MspDeInit 1 */
  128. }
  129. }
  130. /* USER CODE BEGIN 1 */
  131. //CAN接收数据处理
  132. void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,uint8_t ucLength)
  133. {
  134. for(uint8_t i=0;i<ucLength;i++)
  135. {
  136. *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
  137. if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
  138. {
  139. (* ptCANRx).ucBufWrInde = 0;
  140. }
  141. if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
  142. {
  143. (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
  144. (* ptCANRx).ucBufOvf = 1;
  145. }
  146. }
  147. }
  148. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  149. {
  150. if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  151. {
  152. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  153. return;
  154. }
  155. switch(CanHandle->pRxMsg->StdId)
  156. {
  157. case ID_PBU_BC: case ID_PBU_TO_MC://接收PBU数据
  158. {
  159. CAN_RxBuf_Struct_PBU.ucBufID = CanHandle->pRxMsg->StdId;
  160. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU,CanHandle->pRxMsg->DLC);
  161. break;
  162. }
  163. case ID_BMS_BC: case ID_BMS_TO_MC://接收BMS数据
  164. {
  165. CAN_RxBuf_Struct_BMS.ucBufID = CanHandle->pRxMsg->StdId;
  166. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS,CanHandle->pRxMsg->DLC);
  167. break;
  168. }
  169. case ID_HMI_BC: case ID_HMI_TO_MC://接收HMI数据
  170. {
  171. CAN_RxBuf_Struct_HMI.ucBufID = CanHandle->pRxMsg->StdId;
  172. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI,CanHandle->pRxMsg->DLC);
  173. break;
  174. }
  175. case ID_CDL_BC: case ID_CDL_TO_MC://接收CDL数据
  176. {
  177. CAN_RxBuf_Struct_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  178. CAN_Rx_ISR(&CAN_RxBuf_Struct_CDL,CanHandle->pRxMsg->DLC);
  179. break;
  180. }
  181. default:break;
  182. }
  183. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  184. }
  185. //CAN发送数据
  186. void CAN_SendData(uint16_t ID, uint8_t *Data, uint16_t Length)
  187. {
  188. uint16_t LastPacketLen, PacketNum;
  189. uint16_t i,j;
  190. if(Length > 0)
  191. {
  192. //计算分包个数
  193. LastPacketLen = Length % 8;//最后一个数据包长度
  194. if(LastPacketLen == 0)
  195. {
  196. LastPacketLen = 8;
  197. PacketNum = Length / 8;
  198. }
  199. else
  200. {
  201. PacketNum = Length / 8 + 1;
  202. }
  203. //开始发送数据
  204. hcan.pTxMsg->StdId = ID;
  205. hcan.pTxMsg->RTR = CAN_RTR_DATA;
  206. hcan.pTxMsg->IDE = CAN_ID_STD;
  207. //发送前(PacketNum - 1)个数据包
  208. for(i = 0;i < (PacketNum - 1); i++)
  209. {
  210. hcan.pTxMsg->DLC = 8;
  211. for(j = 0;j < 8;j++)
  212. {
  213. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  214. }
  215. if(HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
  216. {
  217. MX_CAN_Init();
  218. }
  219. }
  220. //发送最后一个数据包
  221. hcan.pTxMsg->DLC = LastPacketLen;
  222. for(j = 0;j < LastPacketLen;j++)
  223. {
  224. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  225. }
  226. if(HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
  227. {
  228. MX_CAN_Init();
  229. }
  230. }
  231. }
  232. //过滤器设置
  233. void CANFilterConfig_Scale32_IdMask_StandardIdOnly(void)
  234. {
  235. CAN_FilterConfTypeDef sFilterConfig;
  236. //设置过滤器组0,指定接收发送给PBU的数据
  237. sFilterConfig.FilterNumber = 0;
  238. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  239. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  240. //设置过滤寄存器
  241. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000) >> 16);
  242. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  243. //设置屏蔽寄存器
  244. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000) >> 16);;
  245. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  246. sFilterConfig.FilterFIFOAssignment = 0;
  247. sFilterConfig.FilterActivation = ENABLE;
  248. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  249. {
  250. Error_Handler();
  251. }
  252. //设置过滤器组1,指定接收广播数据
  253. sFilterConfig.FilterNumber = 1;
  254. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  255. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  256. //设置过滤寄存器
  257. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000) >> 16);
  258. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  259. //设置屏蔽寄存器
  260. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000) >> 16);;
  261. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  262. sFilterConfig.FilterFIFOAssignment = 0;
  263. sFilterConfig.FilterActivation = ENABLE;
  264. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  265. {
  266. Error_Handler();
  267. }
  268. }
  269. /* USER CODE END 1 */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/