123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- /**
- ******************************************************************************
- * File Name : CAN.c
- * Description : This file provides code for the configuration
- * of the CAN instances.
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2019 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "can.h"
- #include "gpio.h"
- /* USER CODE BEGIN 0 */
- CanTxMsgTypeDef CAN_TxMessaage;
- CanRxMsgTypeDef CAN_RxMessaage;
- uint8_t CAN_RxBuf_MC[255];
- CAN_Buf_TypeDef CAN_RxBuf_Struct_MC = {0,255,0,0,0,0,CAN_RxBuf_MC};
- uint8_t CAN_RxBuf_BMS[255];
- CAN_Buf_TypeDef CAN_RxBuf_Struct_BMS = {0,255,0,0,0,0,CAN_RxBuf_BMS};
- uint8_t CAN_RxBuf_HMI[255];
- CAN_Buf_TypeDef CAN_RxBuf_Struct_HMI = {0,255,0,0,0,0,CAN_RxBuf_HMI};
- uint8_t CAN_RxBuf_CDL[255];
- CAN_Buf_TypeDef CAN_RxBuf_Struct_CDL = {0,255,0,0,0,0,CAN_RxBuf_CDL};
- /* USER CODE END 0 */
- CAN_HandleTypeDef hcan;
- /* CAN init function */
- void MX_CAN_Init(void)
- {
- hcan.Instance = CAN1;
- hcan.Init.Prescaler = 48;
- hcan.Init.Mode = CAN_MODE_NORMAL;
- hcan.Init.SJW = CAN_SJW_1TQ;
- hcan.Init.BS1 = CAN_BS1_4TQ;
- hcan.Init.BS2 = CAN_BS2_1TQ;
- hcan.Init.TTCM = DISABLE;
- hcan.Init.ABOM = ENABLE;
- hcan.Init.AWUM = ENABLE;
- hcan.Init.NART = DISABLE;
- hcan.Init.RFLM = DISABLE;
- hcan.Init.TXFP = DISABLE;
- if (HAL_CAN_Init(&hcan) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspInit 0 */
- /* USER CODE END CAN1_MspInit 0 */
- /* CAN1 clock enable */
- __HAL_RCC_CAN1_CLK_ENABLE();
-
- /**CAN GPIO Configuration
- PA11 ------> CAN_RX
- PA12 ------> CAN_TX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_11;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_12;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* CAN1 interrupt Init */
- HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 1, 1);
- HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
- /* USER CODE BEGIN CAN1_MspInit 1 */
- canHandle->pTxMsg = &CAN_TxMessaage;
- canHandle->pRxMsg = &CAN_RxMessaage;
- CANFilterConfig_Scale32_IdMask_StandardIdOnly();
- __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);
- /* USER CODE END CAN1_MspInit 1 */
- }
- }
- void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
- {
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspDeInit 0 */
- /* USER CODE END CAN1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_CAN1_CLK_DISABLE();
-
- /**CAN GPIO Configuration
- PA11 ------> CAN_RX
- PA12 ------> CAN_TX
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
- /* CAN1 interrupt Deinit */
- HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
- /* USER CODE BEGIN CAN1_MspDeInit 1 */
- /* USER CODE END CAN1_MspDeInit 1 */
- }
- }
- /* USER CODE BEGIN 1 */
- //CAN接收数据处理
- void CAN_Rx_ISR(CAN_Buf_TypeDef*ptCANRx,uint8_t ucLength)
- {
- for(uint8_t i=0;i<ucLength;i++)
- {
- *((* ptCANRx).pcBufAddr + (* ptCANRx).ucBufWrInde) = hcan.pRxMsg->Data[i];
- if(++(* ptCANRx).ucBufWrInde >= (* ptCANRx).ucBufSize)
- {
- (* ptCANRx).ucBufWrInde = 0;
- }
- if(++(* ptCANRx).ucBufCnt > (* ptCANRx).ucBufSize)
- {
- (* ptCANRx).ucBufCnt = (* ptCANRx).ucBufSize;
- (* ptCANRx).ucBufOvf = 1;
- }
- }
- }
- void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
- {
- if((CanHandle->pRxMsg->IDE != CAN_ID_STD)||(CanHandle->pRxMsg->DLC == 0))
- {
- __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
- return;
- }
- switch(CanHandle->pRxMsg->StdId)
- {
- case ID_MC_BC:
- case ID_MC_TO_PBU://接收MC数据
- {
- CAN_RxBuf_Struct_MC.ucBufID = CanHandle->pRxMsg->StdId;
- CAN_Rx_ISR(&CAN_RxBuf_Struct_MC,CanHandle->pRxMsg->DLC);
- break;
- }
- case ID_BMS_BC:
- case ID_BMS_TO_PBU://接收BMS数据
- {
- CAN_RxBuf_Struct_BMS.ucBufID = CanHandle->pRxMsg->StdId;
- CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS,CanHandle->pRxMsg->DLC);
- break;
- }
- case ID_HMI_BC:
- case ID_HMI_TO_PBU://接收HMI数据
- {
- CAN_RxBuf_Struct_HMI.ucBufID = CanHandle->pRxMsg->StdId;
- CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI,CanHandle->pRxMsg->DLC);
- break;
- }
- case ID_CDL_BC:
- case ID_CDL_TO_PBU_TE:
- case ID_CDL_TO_PBU://接收CDL数据
- {
- CAN_RxBuf_Struct_CDL.ucBufID = CanHandle->pRxMsg->StdId;
- CAN_Rx_ISR(&CAN_RxBuf_Struct_CDL,CanHandle->pRxMsg->DLC);
- break;
- }
- default:break;
- }
- __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_FMP0);//开接收中断
- }
- //CAN发送数据
- void CAN_SendData(uint16_t ID, uint8_t *Data, uint16_t Length)
- {
- uint16_t LastPacketLen, PacketNum;
- uint16_t i,j;
-
- if(Length > 0)
- {
- //计算分包个数
- LastPacketLen = Length % 8;//最后一个数据包长度
- if(LastPacketLen == 0)
- {
- LastPacketLen = 8;
- PacketNum = Length / 8;
- }
- else
- {
- PacketNum = Length / 8 + 1;
- }
- //开始发送数据
- hcan.pTxMsg->StdId = ID;
- hcan.pTxMsg->RTR = CAN_RTR_DATA;
- hcan.pTxMsg->IDE = CAN_ID_STD;
- //发送前(PacketNum - 1)个数据包
- for(i = 0;i < (PacketNum - 1); i++)
- {
- hcan.pTxMsg->DLC = 8;
- for(j = 0;j < 8;j++)
- {
- hcan.pTxMsg->Data[j] = Data[8 * i + j];
- }
- HAL_CAN_Transmit(&hcan, 10);
- }
- //发送最后一个数据包
- hcan.pTxMsg->DLC = LastPacketLen;
- for(j = 0;j < LastPacketLen;j++)
- {
- hcan.pTxMsg->Data[j] = Data[8 * i + j];
- }
- HAL_CAN_Transmit(&hcan, 10);
- }
- }
- //过滤器设置
- void CANFilterConfig_Scale32_IdMask_StandardIdOnly(void)
- {
- CAN_FilterConfTypeDef sFilterConfig;
-
- //设置过滤器组0,指定接收发送给PBU的数据
- sFilterConfig.FilterNumber = 0;
- sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
- sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-
- //设置过滤寄存器
- sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)(0x0703&0X605) << 21) & 0xFFFF0000) >> 16);//0x0703&0X605=0X601
- sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)(0x0703&0X605) << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
-
- //设置屏蔽寄存器
- sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)(0x070F&0X609) << 21) & 0xFFFF0000) >> 16);//0x070F&0X60F=0X609
- sFilterConfig.FilterMaskIdLow = 0xFFFF;
-
- sFilterConfig.FilterFIFOAssignment = 0;
- sFilterConfig.FilterActivation = ENABLE;
-
- if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
- {
- Error_Handler();
- }
-
- //设置过滤器组1,指定接收广播数据
- sFilterConfig.FilterNumber = 1;
- sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
- sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
-
- //设置过滤寄存器
- sFilterConfig.FilterIdHigh = (uint16_t)((((uint32_t)0x0700 << 21) & 0xFFFF0000) >> 16);
- sFilterConfig.FilterIdLow = (uint16_t)(((uint32_t)0x0700 << 21) | CAN_ID_STD | CAN_RTR_DATA) & 0xFFFF;
-
- //设置屏蔽寄存器
- sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)0x070F << 21) & 0xFFFF0000) >> 16);;
- sFilterConfig.FilterMaskIdLow = 0xFFFF;
-
- sFilterConfig.FilterFIFOAssignment = 0;
- sFilterConfig.FilterActivation = ENABLE;
-
- if(HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
- {
- __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP0);
- }
- /* USER CODE END 1 */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|