/** * @brief DeInitializes the IRDA peripheral * @param hirda: IRDA handle * @retval HAL status */ HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda) { /* Check the IRDA handle allocation */ if(hirda == NULL) { return HAL_ERROR; } /* Check the USART/UART associated to the IRDA handle */ assert_param(IS_IRDA_INSTANCE(hirda->Instance)); hirda->State = HAL_IRDA_STATE_BUSY; /* DeInit the low level hardware */ HAL_IRDA_MspDeInit(hirda); /* Disable the Peripheral */ __HAL_IRDA_DISABLE(hirda); hirda->ErrorCode = HAL_IRDA_ERROR_NONE; hirda->State = HAL_IRDA_STATE_RESET; /* Release Lock */ __HAL_UNLOCK(hirda); return HAL_OK; }
/** * @brief Initializes the IRDA mode according to the specified * parameters in the IRDA_InitTypeDef and create the associated handle. * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains * the configuration information for the specified IRDA module. * @retval HAL status */ HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda) { /* Check the IRDA handle allocation */ if(hirda == NULL) { return HAL_ERROR; } /* Check the IRDA instance parameters */ assert_param(IS_IRDA_INSTANCE(hirda->Instance)); /* Check the IRDA mode parameter in the IRDA handle */ assert_param(IS_IRDA_POWERMODE(hirda->Init.IrDAMode)); if(hirda->State == HAL_IRDA_STATE_RESET) { /* Allocate lock resource and initialize it */ hirda->Lock = HAL_UNLOCKED; /* Init the low level hardware */ HAL_IRDA_MspInit(hirda); } hirda->State = HAL_IRDA_STATE_BUSY; /* Disable the IRDA peripheral */ __HAL_IRDA_DISABLE(hirda); /* Set the IRDA communication parameters */ IRDA_SetConfig(hirda); /* In IrDA mode, the following bits must be kept cleared: - LINEN, STOP and CLKEN bits in the USART_CR2 register, - SCEN and HDSEL bits in the USART_CR3 register.*/ CLEAR_BIT(hirda->Instance->CR2, (USART_CR2_LINEN | USART_CR2_STOP | USART_CR2_CLKEN)); CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL)); /* Enable the IRDA peripheral */ __HAL_IRDA_ENABLE(hirda); /* Set the prescaler */ MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler); /* Configure the IrDA mode */ MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.IrDAMode); /* Enable the IrDA mode by setting the IREN bit in the CR3 register */ SET_BIT(hirda->Instance->CR3, USART_CR3_IREN); /* Initialize the IRDA state*/ hirda->ErrorCode = HAL_IRDA_ERROR_NONE; hirda->State= HAL_IRDA_STATE_READY; return HAL_OK; }
/** * @brief Initialize the IRDA mode according to the specified * parameters in the IRDA_InitTypeDef and initialize the associated handle. * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains * the configuration information for the specified IRDA module. * @retval HAL status */ HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda) { /* Check the IRDA handle allocation */ if(hirda == NULL) { return HAL_ERROR; } /* Check the USART/UART associated to the IRDA handle */ assert_param(IS_IRDA_INSTANCE(hirda->Instance)); if(hirda->gState == HAL_IRDA_STATE_RESET) { /* Allocate lock resource and initialize it */ hirda->Lock = HAL_UNLOCKED; /* Init the low level hardware : GPIO, CLOCK */ HAL_IRDA_MspInit(hirda); } hirda->gState = HAL_IRDA_STATE_BUSY; /* Disable the Peripheral to update the configuration registers */ __HAL_IRDA_DISABLE(hirda); /* Set the IRDA Communication parameters */ if (IRDA_SetConfig(hirda) == HAL_ERROR) { return HAL_ERROR; } /* In IRDA mode, the following bits must be kept cleared: - LINEN, STOP and CLKEN bits in the USART_CR2 register, - SCEN and HDSEL bits in the USART_CR3 register.*/ hirda->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP); hirda->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL); /* set the UART/USART in IRDA mode */ hirda->Instance->CR3 |= USART_CR3_IREN; /* Enable the Peripheral */ __HAL_IRDA_ENABLE(hirda); /* TEACK and/or REACK to check before moving hirda->gState and hirda->RxState to Ready */ return (IRDA_CheckIdleState(hirda)); }
/** * @brief Configures the IRDA peripheral. * @param hirda: pointer to a IRDA_HandleTypeDef structure that contains * the configuration information for the specified IRDA module. * @retval None */ static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda) { uint32_t tmpreg = 0x00; /* Check the parameters */ assert_param(IS_IRDA_INSTANCE(hirda->Instance)); assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate)); assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength)); assert_param(IS_IRDA_PARITY(hirda->Init.Parity)); assert_param(IS_IRDA_MODE(hirda->Init.Mode)); /*-------------------------- IRDA CR2 Configuration ------------------------*/ /* Clear STOP[13:12] bits */ hirda->Instance->CR2 &= (uint32_t)~((uint32_t)USART_CR2_STOP); /*-------------------------- USART CR1 Configuration -----------------------*/ tmpreg = hirda->Instance->CR1; /* Clear M, PCE, PS, TE and RE bits */ tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ USART_CR1_RE)); /* Configure the USART Word Length, Parity and mode: Set the M bits according to hirda->Init.WordLength value Set PCE and PS bits according to hirda->Init.Parity value Set TE and RE bits according to hirda->Init.Mode value */ tmpreg |= (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode; /* Write to USART CR1 */ hirda->Instance->CR1 = (uint32_t)tmpreg; /*-------------------------- USART CR3 Configuration -----------------------*/ /* Clear CTSE and RTSE bits */ hirda->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE)); /*-------------------------- USART BRR Configuration -----------------------*/ if((hirda->Instance == USART1) || (hirda->Instance == USART6)) { hirda->Instance->BRR = __IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate); } else { hirda->Instance->BRR = __IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate); } }
/** * @brief Configures the IRDA peripheral. * @param hirda: Pointer to a IRDA_HandleTypeDef structure that contains * the configuration information for the specified IRDA module. * @retval None */ static void IRDA_SetConfig(IRDA_HandleTypeDef *hirda) { /* Check the parameters */ assert_param(IS_IRDA_INSTANCE(hirda->Instance)); assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate)); assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength)); assert_param(IS_IRDA_PARITY(hirda->Init.Parity)); assert_param(IS_IRDA_MODE(hirda->Init.Mode)); /*-------------------------- IRDA CR2 Configuration ------------------------*/ /* Clear STOP[13:12] bits */ CLEAR_BIT(hirda->Instance->CR2, USART_CR2_STOP); /*-------------------------- USART CR1 Configuration -----------------------*/ /* Configure the USART Word Length, Parity and mode: Set the M bits according to hirda->Init.WordLength value Set PCE and PS bits according to hirda->Init.Parity value Set TE and RE bits according to hirda->Init.Mode value */ MODIFY_REG(hirda->Instance->CR1, ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)), (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode); /*-------------------------- USART CR3 Configuration -----------------------*/ /* Clear CTSE and RTSE bits */ CLEAR_BIT(hirda->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE)); /*-------------------------- USART BRR Configuration -----------------------*/ if(hirda->Instance == USART1) { hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK2Freq(), hirda->Init.BaudRate); } else { hirda->Instance->BRR = IRDA_BRR(HAL_RCC_GetPCLK1Freq(), hirda->Init.BaudRate); } }