/** * @brief Initializes the CAN peripheral according to the specified * parameters in the CAN_InitStruct. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains * the configuration information for the specified CAN. * @retval HAL status */ HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan) { uint32_t status = CAN_INITSTATUS_FAILED; /* Default init status */ uint32_t tickstart = 0; /* Check CAN handle */ if(hcan == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP)); assert_param(IS_CAN_MODE(hcan->Init.Mode)); assert_param(IS_CAN_SJW(hcan->Init.SJW)); assert_param(IS_CAN_BS1(hcan->Init.BS1)); assert_param(IS_CAN_BS2(hcan->Init.BS2)); assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler)); if(hcan->State == HAL_CAN_STATE_RESET) { /* Init the low level hardware */ HAL_CAN_MspInit(hcan); } /* Initialize the CAN state*/ hcan->State = HAL_CAN_STATE_BUSY; /* Exit from sleep mode */ hcan->Instance->MCR &= (~(uint32_t)CAN_MCR_SLEEP); /* Request initialisation */ hcan->Instance->MCR |= CAN_MCR_INRQ ; /* Get tickstart */ tickstart = HAL_GetTick(); /* Wait the acknowledge */ while((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { if((HAL_GetTick() - tickstart) > HAL_CAN_DEFAULT_TIMEOUT) { hcan->State= HAL_CAN_STATE_TIMEOUT; return HAL_TIMEOUT; } } /* Check acknowledge */ if ((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { /* Set the time triggered communication mode */ if (hcan->Init.TTCM == ENABLE) { hcan->Instance->MCR |= CAN_MCR_TTCM; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TTCM; } /* Set the automatic bus-off management */ if (hcan->Init.ABOM == ENABLE) { hcan->Instance->MCR |= CAN_MCR_ABOM; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_ABOM; } /* Set the automatic wake-up mode */ if (hcan->Init.AWUM == ENABLE) { hcan->Instance->MCR |= CAN_MCR_AWUM; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_AWUM; } /* Set the no automatic retransmission */ if (hcan->Init.NART == ENABLE) { hcan->Instance->MCR |= CAN_MCR_NART; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_NART; } /* Set the receive FIFO locked mode */ if (hcan->Init.RFLM == ENABLE) { hcan->Instance->MCR |= CAN_MCR_RFLM; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_RFLM; } /* Set the transmit FIFO priority */ if (hcan->Init.TXFP == ENABLE) { hcan->Instance->MCR |= CAN_MCR_TXFP; } else { hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TXFP; } /* Set the bit timing register */ hcan->Instance->BTR = (uint32_t)((uint32_t)hcan->Init.Mode) | \ ((uint32_t)hcan->Init.SJW) | \ ((uint32_t)hcan->Init.BS1) | \ ((uint32_t)hcan->Init.BS2) | \ ((uint32_t)hcan->Init.Prescaler - 1); /* Request leave initialisation */ hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_INRQ; /* Get timeout */ tickstart = HAL_GetTick(); /* Wait the acknowledge */ while((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { if((HAL_GetTick() - tickstart) > HAL_CAN_DEFAULT_TIMEOUT) { hcan->State= HAL_CAN_STATE_TIMEOUT; return HAL_TIMEOUT; } } /* Check acknowledged */ if ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { status = CAN_INITSTATUS_SUCCESS; } } if(status == CAN_INITSTATUS_SUCCESS) { /* Set CAN error code to none */ hcan->ErrorCode = HAL_CAN_ERROR_NONE; /* Initialize the CAN state */ hcan->State = HAL_CAN_STATE_READY; /* Return function status */ return HAL_OK; } else { /* Initialize the CAN state */ hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ return HAL_ERROR; } }
/** * @brief Initializes the CAN peripheral according to the specified * parameters in the CAN_InitStruct. * @param CANx: where x can be 1 to select the CAN peripheral. * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that * contains the configuration information for the CAN peripheral. * @retval : Constant indicates initialization succeed which will be * CANINITFAILED or CANINITOK. */ uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct) { uint8_t InitStatus = CANINITFAILED; uint32_t wait_ack = 0x00000000; /* Check the parameters */ assert_param(IS_CAN_ALL_PERIPH(CANx)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP)); assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode)); assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW)); assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1)); assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2)); assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler)); /* exit from sleep mode */ CANx->MCR &= ~MCR_SLEEP; /* Request initialisation */ CANx->MCR |= MCR_INRQ ; /* Wait the acknowledge */ while ((wait_ack != INAK_TimeOut)&&((CANx->MSR & MSR_INAK) != MSR_INAK)) { wait_ack++; } /* ...and check acknowledged */ if ((CANx->MSR & MSR_INAK) != MSR_INAK) { InitStatus = CANINITFAILED; } else { /* Set the time triggered communication mode */ if (CAN_InitStruct->CAN_TTCM == ENABLE) { CANx->MCR |= MCR_TTCM; } else { CANx->MCR &= ~MCR_TTCM; } /* Set the automatic bus-off management */ if (CAN_InitStruct->CAN_ABOM == ENABLE) { CANx->MCR |= MCR_ABOM; } else { CANx->MCR &= ~MCR_ABOM; } /* Set the automatic wake-up mode */ if (CAN_InitStruct->CAN_AWUM == ENABLE) { CANx->MCR |= MCR_AWUM; } else { CANx->MCR &= ~MCR_AWUM; } /* Set the no automatic retransmission */ if (CAN_InitStruct->CAN_NART == ENABLE) { CANx->MCR |= MCR_NART; } else { CANx->MCR &= ~MCR_NART; } /* Set the receive FIFO locked mode */ if (CAN_InitStruct->CAN_RFLM == ENABLE) { CANx->MCR |= MCR_RFLM; } else { CANx->MCR &= ~MCR_RFLM; } /* Set the transmit FIFO priority */ if (CAN_InitStruct->CAN_TXFP == ENABLE) { CANx->MCR |= MCR_TXFP; } else { CANx->MCR &= ~MCR_TXFP; } /* Set the bit timing register */ CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1); /* Request leave initialisation */ CANx->MCR &= ~MCR_INRQ; /* Wait the acknowledge */ wait_ack = 0x00; while ((wait_ack != INAK_TimeOut)&&((CANx->MSR & MSR_INAK) == MSR_INAK)) { wait_ack++; } /* ...and check acknowledged */ if ((CANx->MSR & MSR_INAK) == MSR_INAK) { InitStatus = CANINITFAILED; } else { InitStatus = CANINITOK ; } } /* At this step, return the status of initialization */ return InitStatus; }
/******************************************************************************* * 函数名称: CAN_Init * 功能描述: 根据CAN_InitStruct中指定的参数初始化CAN外围模块。 * 输入参数: CAN_InitStruct: 指向结构体CAN_InitTypeDef的指针,包含了CAN外围模块的配置信息。 * 输出参数: 无. * 返回参数: 一个说明CAN初始化是否成功的常量。CANINITFAILED = 初始化失败 * CANINITOK = 初始化成功 *******************************************************************************/ u8 CAN_Init(CAN_InitTypeDef* CAN_InitStruct) { u8 InitStatus = 0; u16 WaitAck = 0; /* Check the parameters [检查参数]*/ assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP)); assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode)); assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW)); assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1)); assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2)); assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler)); /* Request initialisation [请求初始化]*/ CAN->MCR = CAN_MCR_INRQ; /* ...and check acknowledged [检测应答]*/ if ((CAN->MSR & CAN_MSR_INAK) == 0) { InitStatus = CANINITFAILED; } else { /* Set the time triggered communication mode [设置定时器触发通讯模式]*/ if (CAN_InitStruct->CAN_TTCM == ENABLE) { CAN->MCR |= CAN_MCR_TTCM; } else { CAN->MCR &= ~CAN_MCR_TTCM; } /* Set the automatic bus-off management [设置自动总线关闭管理]*/ if (CAN_InitStruct->CAN_ABOM == ENABLE) { CAN->MCR |= CAN_MCR_ABOM; } else { CAN->MCR &= ~CAN_MCR_ABOM; } /* Set the automatic wake-up mode [设置自动唤醒模式]*/ if (CAN_InitStruct->CAN_AWUM == ENABLE) { CAN->MCR |= CAN_MCR_AWUM; } else { CAN->MCR &= ~CAN_MCR_AWUM; } /* Set the no automatic retransmission [设置非自动重发]*/ if (CAN_InitStruct->CAN_NART == ENABLE) { CAN->MCR |= CAN_MCR_NART; } else { CAN->MCR &= ~CAN_MCR_NART; } /* Set the receive FIFO locked mode [设置接收FIFO锁定模式]*/ if (CAN_InitStruct->CAN_RFLM == ENABLE) { CAN->MCR |= CAN_MCR_RFLM; } else { CAN->MCR &= ~CAN_MCR_RFLM; } /* Set the transmit FIFO priority [设置发送FIFO优先级]*/ if (CAN_InitStruct->CAN_TXFP == ENABLE) { CAN->MCR |= CAN_MCR_TXFP; } else { CAN->MCR &= ~CAN_MCR_TXFP; } /* Set the bit timing register [设置定时器的位]*/ CAN->BTR = (u32)((u32)CAN_InitStruct->CAN_Mode << 30) | ((u32)CAN_InitStruct->CAN_SJW << 24) | ((u32)CAN_InitStruct->CAN_BS1 << 16) | ((u32)CAN_InitStruct->CAN_BS2 << 20) | ((u32)CAN_InitStruct->CAN_Prescaler - 1); InitStatus = CANINITOK; /* Request leave initialisation [请求退出初始化]*/ CAN->MCR &= ~CAN_MCR_INRQ; /* Wait the acknowledge [等待肯定的应答]*/ for(WaitAck = 0x400; WaitAck > 0x0; WaitAck--) { } /* ...and check acknowledged [检测应答]*/ if ((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { InitStatus = CANINITFAILED; } } /* At this step, return the status of initialization [在这步,返回初始化状态]*/ return InitStatus; }
/******************************************************************************* * Function Name : CAN_Init * Description : Initializes the CAN peripheral according to the specified * parameters in the CAN_InitStruct. * Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains the configuration information for the CAN peripheral. * Output : None. * Return : Constant indicates initialization succeed which will be * CANINITFAILED or CANINITOK. *******************************************************************************/ u8 CAN_Init(CAN_InitTypeDef* CAN_InitStruct) { u8 InitStatus = 0; u16 WaitAck = 0; /* Check the parameters */ assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM)); assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP)); assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode)); assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW)); assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1)); assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2)); assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler)); /* Request initialisation */ CAN->MCR = MCR_INRQ; /* ...and check acknowledged */ if ((CAN->MSR & MSR_INAK) == 0) { InitStatus = CANINITFAILED; } else { /* Set the time triggered communication mode */ if (CAN_InitStruct->CAN_TTCM == ENABLE) { CAN->MCR |= MCR_TTCM; } else { CAN->MCR &= ~MCR_TTCM; } /* Set the automatic bus-off management */ if (CAN_InitStruct->CAN_ABOM == ENABLE) { CAN->MCR |= MCR_ABOM; } else { CAN->MCR &= ~MCR_ABOM; } /* Set the automatic wake-up mode */ if (CAN_InitStruct->CAN_AWUM == ENABLE) { CAN->MCR |= MCR_AWUM; } else { CAN->MCR &= ~MCR_AWUM; } /* Set the no automatic retransmission */ if (CAN_InitStruct->CAN_NART == ENABLE) { CAN->MCR |= MCR_NART; } else { CAN->MCR &= ~MCR_NART; } /* Set the receive FIFO locked mode */ if (CAN_InitStruct->CAN_RFLM == ENABLE) { CAN->MCR |= MCR_RFLM; } else { CAN->MCR &= ~MCR_RFLM; } /* Set the transmit FIFO priority */ if (CAN_InitStruct->CAN_TXFP == ENABLE) { CAN->MCR |= MCR_TXFP; } else { CAN->MCR &= ~MCR_TXFP; } /* Set the bit timing register */ CAN->BTR = (u32)((u32)CAN_InitStruct->CAN_Mode << 30) | ((u32)CAN_InitStruct->CAN_SJW << 24) | ((u32)CAN_InitStruct->CAN_BS1 << 16) | ((u32)CAN_InitStruct->CAN_BS2 << 20) | ((u32)CAN_InitStruct->CAN_Prescaler - 1); InitStatus = CANINITOK; /* Request leave initialisation */ CAN->MCR &= ~MCR_INRQ; /* Wait the acknowledge */ for(WaitAck = 0x400; WaitAck > 0x0; WaitAck--) { } /* ...and check acknowledged */ if ((CAN->MSR & MSR_INAK) == MSR_INAK) { InitStatus = CANINITFAILED; } } /* At this step, return the status of initialization */ return InitStatus; }
/** * @brief Initializes the CAN peripheral according to the specified * parameters in the CAN_InitStruct. * @param hcan: pointer to a CAN_HandleTypeDef structure that contains * the configuration information for the specified CAN. * @retval HAL status */ HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan) { uint32_t status = CAN_INITSTATUS_FAILED; /* Default init status */ uint32_t tickstart = 0; uint32_t tmp_mcr = 0; /* Check CAN handle */ if(hcan == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM)); assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP)); assert_param(IS_CAN_MODE(hcan->Init.Mode)); assert_param(IS_CAN_SJW(hcan->Init.SJW)); assert_param(IS_CAN_BS1(hcan->Init.BS1)); assert_param(IS_CAN_BS2(hcan->Init.BS2)); assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler)); if(hcan->State == HAL_CAN_STATE_RESET) { /* Allocate lock resource and initialize it */ hcan->Lock = HAL_UNLOCKED; /* Init the low level hardware */ HAL_CAN_MspInit(hcan); } /* Initialize the CAN state*/ hcan->State = HAL_CAN_STATE_BUSY; /* Exit from sleep mode */ CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP); /* Request initialisation */ SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ); /* Get timeout */ tickstart = HAL_GetTick(); /* Wait the acknowledge */ while(HAL_IS_BIT_CLR(hcan->Instance->MSR, CAN_MSR_INAK)) { if((HAL_GetTick()-tickstart) > CAN_TIMEOUT_VALUE) { hcan->State= HAL_CAN_STATE_TIMEOUT; /* Process unlocked */ __HAL_UNLOCK(hcan); return HAL_TIMEOUT; } } /* Check acknowledge */ if ((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { /* Set the time triggered communication mode */ if (hcan->Init.TTCM == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_TTCM); } /* Set the automatic bus-off management */ if (hcan->Init.ABOM == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_ABOM); } /* Set the automatic wake-up mode */ if (hcan->Init.AWUM == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_AWUM); } /* Set the no automatic retransmission */ if (hcan->Init.NART == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_NART); } /* Set the receive FIFO locked mode */ if (hcan->Init.RFLM == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_RFLM); } /* Set the transmit FIFO priority */ if (hcan->Init.TXFP == ENABLE) { SET_BIT(tmp_mcr, CAN_MCR_TXFP); } /* Update register MCR */ MODIFY_REG(hcan->Instance->MCR, CAN_MCR_TTCM | CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_NART | CAN_MCR_RFLM | CAN_MCR_TXFP, tmp_mcr); /* Set the bit timing register */ WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode | hcan->Init.SJW | hcan->Init.BS1 | hcan->Init.BS2 | (hcan->Init.Prescaler - 1) )); /* Request leave initialisation */ CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ); /* Get timeout */ tickstart = HAL_GetTick(); /* Wait the acknowledge */ while(HAL_IS_BIT_CLR(hcan->Instance->MSR, CAN_MSR_INAK)) { if((HAL_GetTick()-tickstart) > CAN_TIMEOUT_VALUE) { hcan->State= HAL_CAN_STATE_TIMEOUT; /* Process unlocked */ __HAL_UNLOCK(hcan); return HAL_TIMEOUT; } } /* Check acknowledged */ if (HAL_IS_BIT_SET(hcan->Instance->MSR, CAN_MSR_INAK)) { status = CAN_INITSTATUS_SUCCESS; } } if(status == CAN_INITSTATUS_SUCCESS) { /* Set CAN error code to none */ hcan->ErrorCode = HAL_CAN_ERROR_NONE; /* Initialize the CAN state */ hcan->State = HAL_CAN_STATE_READY; /* Return function status */ return HAL_OK; } else { /* Initialize the CAN state */ hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ return HAL_ERROR; } }