can.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "uart_process.h"
  42. #include "gpio.h"
  43. /* USER CODE BEGIN 0 */
  44. CanTxMsgTypeDef CAN_TxMessaage;
  45. CanRxMsgTypeDef CAN_RxMessaage;
  46. const uint16_t ID_Index[CAN_ID_NUM] =
  47. {
  48. ID_MC_BC ,//0x710
  49. ID_MC_TO_BMS ,//0x712
  50. ID_MC_TO_PBU ,//0x713
  51. ID_MC_TO_HMI ,//0x714
  52. ID_MC_TO_CDL ,//0x715
  53. ID_MC_TE_TO_CDL ,//0x615
  54. ID_BMS_BC ,//0x720
  55. ID_BMS_TO_MC ,//0x721
  56. ID_BMS_TO_PBU ,//0x723
  57. ID_BMS_TO_HMI ,//0x724
  58. ID_BMS_TO_CDL ,//0x725
  59. ID_PBU_BC ,//0x730
  60. ID_PBU_TO_MC ,//0x731
  61. ID_PBU_TO_BMS ,//0x732
  62. ID_PBU_TO_HMI ,//0x734
  63. ID_PBU_TO_CDL ,//0x735
  64. ID_PBU_TE_TO_CDL ,//0x635
  65. ID_HMI_BC ,//0x740
  66. ID_HMI_TO_MC ,//0x741
  67. ID_HMI_TO_BMS ,//0x742
  68. ID_HMI_TO_PBU ,//0x743
  69. ID_HMI_TO_CDL //0x745
  70. };
  71. uint8_t CAN_RxBuf[CAN_ID_NUM][CAN_BUFF_SIZE];
  72. CAN_Buf_TypeDef CAN_RxBuf_Struct[CAN_ID_NUM];
  73. /* USER CODE END 0 */
  74. CAN_HandleTypeDef hcan;
  75. /* CAN init function */
  76. void MX_CAN_Init(void)
  77. {
  78. hcan.Instance = CAN1;
  79. hcan.Init.Prescaler = (HAL_GPIO_ReadPin(BautrateSwitch_GPIO_Port, BautrateSwitch_Pin) == GPIO_PIN_SET) ? 24 : 48;
  80. hcan.Init.Mode = CAN_MODE_NORMAL;
  81. hcan.Init.SJW = CAN_SJW_1TQ;
  82. hcan.Init.BS1 = CAN_BS1_4TQ;
  83. hcan.Init.BS2 = CAN_BS2_1TQ;
  84. hcan.Init.TTCM = DISABLE;
  85. hcan.Init.ABOM = ENABLE;
  86. hcan.Init.AWUM = DISABLE;
  87. hcan.Init.NART = DISABLE;
  88. hcan.Init.RFLM = DISABLE;
  89. hcan.Init.TXFP = DISABLE;
  90. if (HAL_CAN_Init(&hcan) != HAL_OK)
  91. {
  92. _Error_Handler(__FILE__, __LINE__);
  93. }
  94. }
  95. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  96. {
  97. GPIO_InitTypeDef GPIO_InitStruct;
  98. if(canHandle->Instance==CAN1)
  99. {
  100. /* USER CODE BEGIN CAN1_MspInit 0 */
  101. /* USER CODE END CAN1_MspInit 0 */
  102. /* CAN1 clock enable */
  103. __HAL_RCC_CAN1_CLK_ENABLE();
  104. /**CAN GPIO Configuration
  105. PA11 ------> CAN_RX
  106. PA12 ------> CAN_TX
  107. */
  108. GPIO_InitStruct.Pin = GPIO_PIN_11;
  109. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  110. GPIO_InitStruct.Pull = GPIO_NOPULL;
  111. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  112. GPIO_InitStruct.Pin = GPIO_PIN_12;
  113. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  114. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  115. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  116. /* CAN1 interrupt Init */
  117. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  118. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  119. /* USER CODE BEGIN CAN1_MspInit 1 */
  120. canHandle->pTxMsg = &CAN_TxMessaage;
  121. canHandle->pRxMsg = &CAN_RxMessaage;
  122. CANFilterConfig_Scale32_IdMask_StandardIdOnly();
  123. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  124. /* USER CODE END CAN1_MspInit 1 */
  125. }
  126. }
  127. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  128. {
  129. if(canHandle->Instance==CAN1)
  130. {
  131. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  132. /* USER CODE END CAN1_MspDeInit 0 */
  133. /* Peripheral clock disable */
  134. __HAL_RCC_CAN1_CLK_DISABLE();
  135. /**CAN GPIO Configuration
  136. PA11 ------> CAN_RX
  137. PA12 ------> CAN_TX
  138. */
  139. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
  140. /* CAN1 interrupt Deinit */
  141. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  142. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  143. /* USER CODE END CAN1_MspDeInit 1 */
  144. }
  145. }
  146. /* USER CODE BEGIN 1 */
  147. void CAN_RxBuf_Init(void)
  148. {
  149. uint8_t i;
  150. for(i=0; i<CAN_ID_NUM; i++)
  151. {
  152. CAN_RxBuf_Struct[i].ucBufID = 0;
  153. CAN_RxBuf_Struct[i].ucBufSize = CAN_BUFF_SIZE;
  154. CAN_RxBuf_Struct[i].ucBufWrInde = 0;
  155. CAN_RxBuf_Struct[i].ucBufRdInde = 0;
  156. CAN_RxBuf_Struct[i].ucBufCnt = 0;
  157. CAN_RxBuf_Struct[i].ucBufOvf = 0;
  158. CAN_RxBuf_Struct[i].pcBufAddr = CAN_RxBuf[i];
  159. CAN_RxBuf_Struct[i].DelayTimeCnt = 0;
  160. CAN_RxBuf_Struct[i].IsWaitRX_Flag = FALSE;
  161. }
  162. }
  163. void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,uint8_t ucLength)
  164. {
  165. for(uint8_t i=0;i<ucLength;i++)
  166. {
  167. *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
  168. if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
  169. {
  170. (* ptCANRx).ucBufWrInde = 0;
  171. }
  172. if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
  173. {
  174. (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
  175. (* ptCANRx).ucBufOvf = 1;
  176. }
  177. }
  178. }
  179. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  180. {
  181. if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  182. {
  183. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  184. return;
  185. }
  186. //根据ID索引数组下标
  187. uint16_t Index;
  188. for(Index=0 ;Index<CAN_ID_NUM; Index++)
  189. {
  190. if(CanHandle->pRxMsg->StdId == ID_Index[Index])
  191. {
  192. //数据转移到队列
  193. CAN_RxBuf_Struct[Index].ucBufID = CanHandle->pRxMsg->StdId;
  194. CAN_Rx_ISR(&CAN_RxBuf_Struct[Index], CanHandle->pRxMsg->DLC);
  195. T_RecvCANMsg = HAL_GetTick();
  196. break;
  197. }
  198. }
  199. #if 0
  200. switch(CanHandle->pRxMsg->StdId)
  201. {
  202. //MC广播的数据
  203. case ID_MC_BC:
  204. {
  205. CAN_RxBuf_Struct_MC_BC.ucBufID = CanHandle->pRxMsg->StdId;
  206. CAN_Rx_ISR(&CAN_RxBuf_Struct_MC_BC,CanHandle->pRxMsg->DLC);
  207. break;
  208. }
  209. //MC发送给CDL的数据
  210. case ID_MC_TO_CDL:
  211. {
  212. CAN_RxBuf_Struct_MC_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  213. CAN_Rx_ISR(&CAN_RxBuf_Struct_MC_TO_CDL,CanHandle->pRxMsg->DLC);
  214. break;
  215. }
  216. //MC_TE发送给CDL的数据
  217. case ID_MC_TE_TO_CDL:
  218. {
  219. CAN_RxBuf_Struct_MC_TE_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  220. CAN_Rx_ISR(&CAN_RxBuf_Struct_MC_TE_TO_CDL,CanHandle->pRxMsg->DLC);
  221. break;
  222. }
  223. //PBU广播的数据
  224. case ID_PBU_BC:
  225. {
  226. CAN_RxBuf_Struct_PBU_BC.ucBufID = CanHandle->pRxMsg->StdId;
  227. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU_BC,CanHandle->pRxMsg->DLC);
  228. break;
  229. }
  230. //PBU发送给CDL的数据
  231. case ID_PBU_TO_CDL:
  232. {
  233. CAN_RxBuf_Struct_PBU_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  234. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU_TO_CDL,CanHandle->pRxMsg->DLC);
  235. break;
  236. }
  237. //PBU_TE发送给CDL的数据
  238. case ID_PBU_TE_TO_CDL:
  239. {
  240. CAN_RxBuf_Struct_PBU_TE_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  241. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU_TE_TO_CDL,CanHandle->pRxMsg->DLC);
  242. break;
  243. }
  244. //BMS广播的数据
  245. case ID_BMS_BC:
  246. {
  247. CAN_RxBuf_Struct_BMS_BC.ucBufID = CanHandle->pRxMsg->StdId;
  248. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS_BC,CanHandle->pRxMsg->DLC);
  249. break;
  250. }
  251. //BMS发送给CDL的数据
  252. case ID_BMS_TO_CDL:
  253. {
  254. CAN_RxBuf_Struct_BMS_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  255. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS_TO_CDL,CanHandle->pRxMsg->DLC);
  256. break;
  257. }
  258. //HMI广播的数据
  259. case ID_HMI_BC:
  260. {
  261. CAN_RxBuf_Struct_HMI_BC.ucBufID = CanHandle->pRxMsg->StdId;
  262. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI_BC,CanHandle->pRxMsg->DLC);
  263. break;
  264. }
  265. //HMI发送给CDL的数据
  266. case ID_HMI_TO_CDL:
  267. {
  268. CAN_RxBuf_Struct_HMI_TO_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  269. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI_TO_CDL,CanHandle->pRxMsg->DLC);
  270. break;
  271. }
  272. default:break;
  273. }
  274. #endif
  275. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  276. }
  277. //CAN发送数据
  278. void CAN_SendData(uint16_t ID, uint8_t *Data, uint16_t Length)
  279. {
  280. uint16_t LastPacketLen, PacketNum;
  281. uint16_t i,j;
  282. if(Transmit_UARTOrCAN == 2)
  283. {
  284. return;
  285. }
  286. if(Length > 0)
  287. {
  288. //计算分包个数
  289. LastPacketLen = Length % 8;//最后一个数据包长度
  290. if(LastPacketLen == 0)
  291. {
  292. LastPacketLen = 8;
  293. PacketNum = Length / 8;
  294. }
  295. else
  296. {
  297. PacketNum = Length / 8 + 1;
  298. }
  299. //开始发送数据
  300. hcan.pTxMsg->StdId = ID;
  301. hcan.pTxMsg->RTR = CAN_RTR_DATA;
  302. hcan.pTxMsg->IDE = CAN_ID_STD;
  303. //发送前(PacketNum - 1)个数据包
  304. for(i = 0;i < (PacketNum - 1); i++)
  305. {
  306. hcan.pTxMsg->DLC = 8;
  307. for(j = 0;j < 8;j++)
  308. {
  309. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  310. }
  311. if(HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
  312. {
  313. MX_CAN_Init();
  314. }
  315. }
  316. //发送最后一个数据包
  317. hcan.pTxMsg->DLC = LastPacketLen;
  318. for(j = 0;j < LastPacketLen;j++)
  319. {
  320. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  321. }
  322. // if(HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
  323. // {
  324. // //MX_CAN_Init();
  325. // if(0 == Transmit_UARTOrCAN)
  326. // {
  327. // Transmit_UARTOrCAN = 2;
  328. // HAL_Delay(100);
  329. // HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin1, GPIO_PIN_SET);
  330. // HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin2, GPIO_PIN_SET);
  331. // }
  332. // }
  333. // else
  334. // {
  335. // Transmit_UARTOrCAN = 1;
  336. // }
  337. }
  338. }
  339. //过滤器设置
  340. void CANFilterConfig_Scale32_IdMask_StandardIdOnly(void)
  341. {
  342. CAN_FilterConfTypeDef sFilterConfig;
  343. //设置过滤器组0,指定接收发送给CDL的数据
  344. sFilterConfig.FilterNumber = 0;
  345. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  346. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  347. //设置过滤寄存器
  348. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_FILTER << 21) & 0xFFFF0000) >> 16);
  349. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  350. //设置屏蔽寄存器
  351. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_MASK << 21) & 0xFFFF0000) >> 16);;
  352. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  353. sFilterConfig.FilterFIFOAssignment = 0;
  354. sFilterConfig.FilterActivation = ENABLE;
  355. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  356. {
  357. Error_Handler();
  358. }
  359. //设置过滤器组1,指定接收广播数据
  360. sFilterConfig.FilterNumber = 1;
  361. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  362. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  363. //设置过滤寄存器
  364. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_FILTER << 21) & 0xFFFF0000) >> 16);
  365. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  366. //设置屏蔽寄存器
  367. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_MASK << 21) & 0xFFFF0000) >> 16);;
  368. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  369. sFilterConfig.FilterFIFOAssignment = 0;
  370. sFilterConfig.FilterActivation = ENABLE;
  371. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  372. {
  373. Error_Handler();
  374. }
  375. }
  376. /* USER CODE END 1 */
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */
  383. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/