can.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "can_process.h"
  43. #include "bms_magene.h"
  44. /* USER CODE BEGIN 0 */
  45. CanTxMsgTypeDef CAN_TxMessaage;
  46. CanRxMsgTypeDef CAN_RxMessaage;
  47. CanRxMsgTypeDef CAN_RxMessaage1;
  48. #if defined BARD_250K
  49. const uint32_t Baudrate __attribute__((at(0x08018000))) = 0xAA55AA12; //低8位表示波特率分频系数,Bootloader取用
  50. #elif defined BARD_125K
  51. const uint32_t Baudrate __attribute__((at(0x08018000))) = 0xAA55AA24; //低8位表示波特率分频系数,Bootloader取用
  52. #else
  53. Error();
  54. #endif
  55. uint8_t CAN_RxBuf_PBU[255];
  56. CAN_Buf_TypeDef CAN_RxBuf_Struct_PBU = {0,255,0,0,0,0,CAN_RxBuf_PBU,0,FALSE};
  57. uint8_t CAN_RxBuf_BMS[255];
  58. CAN_Buf_TypeDef CAN_RxBuf_Struct_BMS = {0,255,0,0,0,0,CAN_RxBuf_BMS,0,FALSE};
  59. uint8_t CAN_RxBuf_HMI[255];
  60. CAN_Buf_TypeDef CAN_RxBuf_Struct_HMI = {0,255,0,0,0,0,CAN_RxBuf_HMI,0,FALSE};
  61. uint8_t CAN_RxBuf_CDL[255];
  62. CAN_Buf_TypeDef CAN_RxBuf_Struct_CDL = {0,255,0,0,0,0,CAN_RxBuf_CDL,0,FALSE};
  63. /* USER CODE END 0 */
  64. CAN_HandleTypeDef hcan;
  65. /* CAN init function */
  66. void MX_CAN_Init(void)
  67. {
  68. hcan.Instance = CAN1;
  69. hcan.Init.Prescaler = (Baudrate & 0x000000FF);
  70. hcan.Init.Mode = CAN_MODE_NORMAL;
  71. hcan.Init.SJW = CAN_SJW_1TQ;
  72. hcan.Init.BS1 = CAN_BS1_6TQ;
  73. hcan.Init.BS2 = CAN_BS2_1TQ;
  74. hcan.Init.TTCM = DISABLE;
  75. hcan.Init.ABOM = ENABLE;
  76. hcan.Init.AWUM = DISABLE;
  77. hcan.Init.NART = DISABLE;
  78. hcan.Init.RFLM = DISABLE;
  79. hcan.Init.TXFP = DISABLE;
  80. if (HAL_CAN_Init(&hcan) != HAL_OK)
  81. {
  82. _Error_Handler(__FILE__, __LINE__);
  83. }
  84. }
  85. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  86. {
  87. GPIO_InitTypeDef GPIO_InitStruct;
  88. if(canHandle->Instance==CAN1)
  89. {
  90. /* USER CODE BEGIN CAN1_MspInit 0 */
  91. /* USER CODE END CAN1_MspInit 0 */
  92. /* CAN1 clock enable */
  93. __HAL_RCC_CAN1_CLK_ENABLE();
  94. /**CAN GPIO Configuration
  95. PB8 ------> CAN_RX
  96. PB9 ------> CAN_TX
  97. */
  98. GPIO_InitStruct.Pin = GPIO_PIN_8;
  99. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  100. GPIO_InitStruct.Pull = GPIO_NOPULL;
  101. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  102. GPIO_InitStruct.Pin = GPIO_PIN_9;
  103. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  104. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  105. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  106. __HAL_AFIO_REMAP_CAN1_2();
  107. /* CAN1 interrupt Init */
  108. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 1, 1);
  109. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  110. HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 1, 1);
  111. HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
  112. /* USER CODE BEGIN CAN1_MspInit 1 */
  113. canHandle->pTxMsg = &CAN_TxMessaage;
  114. canHandle->pRxMsg = &CAN_RxMessaage;
  115. canHandle->pRx1Msg = &CAN_RxMessaage1;
  116. CANFilterConfig_Scale32_IdMask_StandardIdOnly();
  117. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
  118. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP1);
  119. /* USER CODE END CAN1_MspInit 1 */
  120. }
  121. }
  122. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  123. {
  124. if(canHandle->Instance==CAN1)
  125. {
  126. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  127. /* USER CODE END CAN1_MspDeInit 0 */
  128. /* Peripheral clock disable */
  129. __HAL_RCC_CAN1_CLK_DISABLE();
  130. /**CAN GPIO Configuration
  131. PB8 ------> CAN_RX
  132. PB9 ------> CAN_TX
  133. */
  134. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
  135. /* CAN1 interrupt Deinit */
  136. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  137. HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn);
  138. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  139. /* USER CODE END CAN1_MspDeInit 1 */
  140. }
  141. }
  142. /* USER CODE BEGIN 1 */
  143. //CAN接收数据处理
  144. void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,uint8_t ucLength)
  145. {
  146. for(uint8_t i=0;i<ucLength;i++)
  147. {
  148. *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
  149. if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
  150. {
  151. (* ptCANRx).ucBufWrInde = 0;
  152. }
  153. if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
  154. {
  155. (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
  156. (* ptCANRx).ucBufOvf = 1;
  157. }
  158. }
  159. }
  160. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
  161. {
  162. if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  163. {
  164. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  165. return;
  166. }
  167. switch(CanHandle->pRxMsg->StdId)
  168. {
  169. case ID_PBU_BC: case ID_PBU_TO_MC://接收PBU数据
  170. {
  171. CAN_RxBuf_Struct_PBU.ucBufID = CanHandle->pRxMsg->StdId;
  172. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU,CanHandle->pRxMsg->DLC);
  173. break;
  174. }
  175. case ID_BMS_BC: case ID_BMS_TO_MC://接收BMS数据
  176. {
  177. CAN_RxBuf_Struct_BMS.ucBufID = CanHandle->pRxMsg->StdId;
  178. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS,CanHandle->pRxMsg->DLC);
  179. break;
  180. }
  181. case ID_HMI_BC: case ID_HMI_TO_MC://接收HMI数据
  182. {
  183. CAN_RxBuf_Struct_HMI.ucBufID = CanHandle->pRxMsg->StdId;
  184. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI,CanHandle->pRxMsg->DLC);
  185. break;
  186. }
  187. case ID_CDL_BC: case ID_CDL_TO_MC:case ID_CDL_TO_MC_TE://接收CDL数据
  188. {
  189. CAN_RxBuf_Struct_CDL.ucBufID = CanHandle->pRxMsg->StdId;
  190. CAN_Rx_ISR(&CAN_RxBuf_Struct_CDL,CanHandle->pRxMsg->DLC);
  191. break;
  192. }
  193. default:break;
  194. }
  195. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
  196. }
  197. void HAL_CAN_Rx1CpltCallback(CAN_HandleTypeDef *CanHandle)
  198. {
  199. if((CanHandle->pRx1Msg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
  200. {
  201. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP1);//开接收中断
  202. return;
  203. }
  204. switch(CanHandle->pRx1Msg->StdId)
  205. {
  206. case BMS_MAGENE_BASE_RPDO + BMS_MAGENE_ADDRESS_MAIN: case BMS_MAGENE_BASE_RPDO + BMS_MAGENE_ADDRESS_BACK:
  207. {
  208. if(CanHandle->pRx1Msg->DLC == 8)//由于没有帧头帧尾和CRC,故保证数据的准确性必须为8字节才解析
  209. {
  210. BMS_Magene_CanData.RefreshFlag = 1;
  211. BMS_Magene_CanData.ID = CanHandle->pRx1Msg->StdId;
  212. memcpy((uint8_t*)BMS_Magene_CanData.Data, (uint8_t*)CanHandle->pRx1Msg->Data, 8);
  213. }
  214. break;
  215. }
  216. default:break;
  217. }
  218. __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP1);//开接收中断
  219. }
  220. //CAN发送数据
  221. void CAN_SendData(uint16_t ID, uint8_t *Data, uint16_t Length)
  222. {
  223. uint16_t LastPacketLen, PacketNum;
  224. uint16_t i,j;
  225. if(Length > 0)
  226. {
  227. //计算分包个数
  228. LastPacketLen = Length % 8;//最后一个数据包长度
  229. if(LastPacketLen == 0)
  230. {
  231. LastPacketLen = 8;
  232. PacketNum = Length / 8;
  233. }
  234. else
  235. {
  236. PacketNum = Length / 8 + 1;
  237. }
  238. //开始发送数据
  239. hcan.pTxMsg->StdId = ID;
  240. hcan.pTxMsg->RTR = CAN_RTR_DATA;
  241. hcan.pTxMsg->IDE = CAN_ID_STD;
  242. //发送前(PacketNum - 1)个数据包
  243. for(i = 0;i < (PacketNum - 1); i++)
  244. {
  245. hcan.pTxMsg->DLC = 8;
  246. for(j = 0;j < 8;j++)
  247. {
  248. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  249. }
  250. if(HAL_CAN_Transmit(&hcan, 10) == HAL_ERROR)
  251. {
  252. MX_CAN_Init();
  253. }
  254. }
  255. //发送最后一个数据包
  256. hcan.pTxMsg->DLC = LastPacketLen;
  257. for(j = 0;j < LastPacketLen;j++)
  258. {
  259. hcan.pTxMsg->Data[j] = Data[8 * i + j];
  260. }
  261. if(HAL_CAN_Transmit(&hcan, 10) == HAL_ERROR)
  262. {
  263. MX_CAN_Init();
  264. }
  265. }
  266. }
  267. //过滤器设置
  268. void CANFilterConfig_Scale32_IdMask_StandardIdOnly(void)
  269. {
  270. CAN_FilterConfTypeDef sFilterConfig;
  271. //设置过滤器组0,指定接收发送给PBU和CDL的数据
  272. sFilterConfig.FilterNumber = 0;//使用过滤器0
  273. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//配置为掩码模式
  274. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;//设置为32位宽
  275. //设置过滤寄存器
  276. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000) >> 16); //0XC020
  277. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;//0X0000
  278. //设置屏蔽寄存器
  279. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000) >> 16);//0XC1E0
  280. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  281. //FIFO0
  282. sFilterConfig.FilterFIFOAssignment = 0; //设置通过的数据帧进入到FIFO中
  283. sFilterConfig.FilterActivation = ENABLE;
  284. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  285. {
  286. Error_Handler();
  287. }
  288. //设置过滤器组1,指定接收广播数据
  289. sFilterConfig.FilterNumber = 1;
  290. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//配置为掩码模式
  291. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;//设置为32位宽
  292. //设置过滤寄存器
  293. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000) >> 16);
  294. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  295. //设置屏蔽寄存器
  296. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000) >> 16);;
  297. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  298. //FIFO0
  299. sFilterConfig.FilterFIFOAssignment = 0;
  300. sFilterConfig.FilterActivation = ENABLE;
  301. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  302. {
  303. Error_Handler();
  304. }
  305. //设置过滤器组2,指定接收enviolo的报文
  306. sFilterConfig.FilterNumber = 2;
  307. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;//配置为掩码模式
  308. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;//设置为32位宽
  309. //设置过滤寄存器
  310. sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)ID_BMS_MAGENE_FILTER << 21) & 0xFFFF0000) >> 16);
  311. sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)ID_BMS_MAGENE_FILTER << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
  312. //设置屏蔽寄存器
  313. sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)ID_BMS_MAGENE_MASK << 21) & 0xFFFF0000) >> 16);
  314. sFilterConfig.FilterMaskIdLow = 0xFFFF;
  315. //FIFO1
  316. sFilterConfig.FilterFIFOAssignment = 1;
  317. sFilterConfig.FilterActivation = ENABLE;
  318. if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  319. {
  320. Error_Handler();
  321. }
  322. }
  323. void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
  324. {
  325. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP0);
  326. __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP1);
  327. }
  328. /* USER CODE END 1 */
  329. /**
  330. * @}
  331. */
  332. /**
  333. * @}
  334. */
  335. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/