can.c 12 KB

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