基于STM32的窄带物联网图书馆座位智能管理系统(2)

  • @param pData Pointer to data buffer (u8 or u16 data elements).
  • @param Size Amount of data elements (u8 or u16) to be sent.
  • @param Timeout Specify timeout value.
  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
    {
    uint16_t *tmp;
    uint32_t tickstart = 0U;

/* Check that a Tx process is not already ongoing */
if (hirda->gState == HAL_IRDA_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}

/* Process Locked */
__HAL_LOCK(hirda);

hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
hirda->gState = HAL_IRDA_STATE_BUSY_TX;

/* Init tickstart for timeout management*/
tickstart = HAL_GetTick();

hirda->TxXferSize = Size;
hirda->TxXferCount = Size;
while (hirda->TxXferCount > 0U)
{
  hirda->TxXferCount--;
  if (hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  {
    if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
    {
      return HAL_TIMEOUT;
    }
    tmp = (uint16_t *) pData;
    hirda->Instance->DR = (*tmp & (uint16_t)0x01FF);
    if (hirda->Init.Parity == IRDA_PARITY_NONE)
    {
      pData += 2U;
    }
    else
    {
      pData += 1U;
    }
  }
  else
  {
    if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
    {
      return HAL_TIMEOUT;
    }
    hirda->Instance->DR = (*pData++ & (uint8_t)0xFF);
  }
}

if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
{
  return HAL_TIMEOUT;
}

/* At end of Tx process, restore hirda->gState to Ready */
hirda->gState = HAL_IRDA_STATE_READY;

/* Process Unlocked */
__HAL_UNLOCK(hirda);

return HAL_OK;

}
else
{
return HAL_BUSY;
}
}

/**

  • @brief Receive an amount of data in blocking mode.
  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  •    the received data is handled as a set of u16. In this case, Size must reflect the number
    
  •    of u16 available through pData.
    
  • @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  •          the configuration information for the specified IRDA module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).
  • @param Size Amount of data elements (u8 or u16) to be received.
  • @param Timeout Specify timeout value
  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
    {
    uint16_t *tmp;
    uint32_t tickstart = 0U;

/* Check that a Rx process is not already ongoing */
if (hirda->RxState == HAL_IRDA_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}

/* Process Locked */
__HAL_LOCK(hirda);

hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
hirda->RxState = HAL_IRDA_STATE_BUSY_RX;

/* Init tickstart for timeout management*/
tickstart = HAL_GetTick();

hirda->RxXferSize = Size;
hirda->RxXferCount = Size;

/* Check the remain data to be received */
while (hirda->RxXferCount > 0U)
{
  hirda->RxXferCount--;

  if (hirda->Init.WordLength == IRDA_WORDLENGTH_9B)
  {
    if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
    {
      return HAL_TIMEOUT;
    }
    tmp = (uint16_t *) pData ;
    if (hirda->Init.Parity == IRDA_PARITY_NONE)
    {
      *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x01FF);
      pData += 2U;
    }
    else
    {
      *tmp = (uint16_t)(hirda->Instance->DR & (uint16_t)0x00FF);
      pData += 1U;
    }
  }
  else
  {
    if (IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
    {
      return HAL_TIMEOUT;
    }
    if (hirda->Init.Parity == IRDA_PARITY_NONE)
    {
      *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x00FF);
    }
    else
    {
      *pData++ = (uint8_t)(hirda->Instance->DR & (uint8_t)0x007F);
    }
  }
}

/* At end of Rx process, restore hirda->RxState to Ready */
hirda->RxState = HAL_IRDA_STATE_READY;

/* Process Unlocked */
__HAL_UNLOCK(hirda);

return HAL_OK;

}
else
{
return HAL_BUSY;
}
}

/**

  • @brief Send an amount of data in non blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •    the sent data is handled as a set of u16. In this case, Size must reflect the number
    
  •    of u16 available through pData.
    
  • @param hirda Pointer to a IRDA_HandleTypeDef structure that contains

  •          the configuration information for the specified IRDA module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be sent.

  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t pData, uint16_t Size)
    {
    /
    Check that a Tx process is not already ongoing */
    if (hirda->gState == HAL_IRDA_STATE_READY)
    {
    if ((pData == NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    }

    /* Process Locked */
    __HAL_LOCK(hirda);

    hirda->pTxBuffPtr = pData;
    hirda->TxXferSize = Size;
    hirda->TxXferCount = Size;

    hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
    hirda->gState = HAL_IRDA_STATE_BUSY_TX;

    /* Process Unlocked */
    __HAL_UNLOCK(hirda);

    /* Enable the IRDA Transmit Data Register Empty Interrupt */
    SET_BIT(hirda->Instance->CR1, USART_CR1_TXEIE);

    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief Receive an amount of data in non blocking mode.

  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),

  •    the received data is handled as a set of u16. In this case, Size must reflect the number
    
  •    of u16 available through pData.
    
  • @param hirda Pointer to a IRDA_HandleTypeDef structure that contains

  •          the configuration information for the specified IRDA module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).

  • @param Size Amount of data elements (u8 or u16) to be received.

  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t pData, uint16_t Size)
    {
    /
    Check that a Rx process is not already ongoing */
    if (hirda->RxState == HAL_IRDA_STATE_READY)
    {
    if ((pData == NULL) || (Size == 0U))
    {
    return HAL_ERROR;
    }

    /* Process Locked */
    __HAL_LOCK(hirda);

    hirda->pRxBuffPtr = pData;
    hirda->RxXferSize = Size;
    hirda->RxXferCount = Size;

    hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
    hirda->RxState = HAL_IRDA_STATE_BUSY_RX;

    /* Process Unlocked */
    __HAL_UNLOCK(hirda);

    /* Enable the IRDA Parity Error and Data Register Not Empty Interrupts */
    SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);

    /* Enable the IRDA Error Interrupt: (Frame error, Noise error, Overrun error) */
    SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);

    return HAL_OK;
    }
    else
    {
    return HAL_BUSY;
    }
    }

/**

  • @brief Send an amount of data in DMA mode.
  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  •    the sent data is handled as a set of u16. In this case, Size must reflect the number
    
  •    of u16 available through pData.
    
  • @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  •          the configuration information for the specified IRDA module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).
  • @param Size Amount of data elements (u8 or u16) to be sent.
  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
    {
    uint32_t *tmp;

/* Check that a Tx process is not already ongoing */
if (hirda->gState == HAL_IRDA_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}

/* Process Locked */
__HAL_LOCK(hirda);

hirda->pTxBuffPtr = pData;
hirda->TxXferSize = Size;
hirda->TxXferCount = Size;

hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
hirda->gState = HAL_IRDA_STATE_BUSY_TX;

/* Set the IRDA DMA transfer complete callback */
hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;

/* Set the IRDA DMA half transfer complete callback */
hirda->hdmatx->XferHalfCpltCallback = IRDA_DMATransmitHalfCplt;

/* Set the DMA error callback */
hirda->hdmatx->XferErrorCallback = IRDA_DMAError;

/* Set the DMA abort callback */
hirda->hdmatx->XferAbortCallback = NULL;

/* Enable the IRDA transmit DMA channel */
tmp = (uint32_t *)&pData;
HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t *)tmp, (uint32_t)&hirda->Instance->DR, Size);

/* Clear the TC flag in the SR register by writing 0 to it */
__HAL_IRDA_CLEAR_FLAG(hirda, IRDA_FLAG_TC);

/* Process Unlocked */
__HAL_UNLOCK(hirda);

/* Enable the DMA transfer for transmit request by setting the DMAT bit
in the USART CR3 register */
SET_BIT(hirda->Instance->CR3, USART_CR3_DMAT);

return HAL_OK;

}
else
{
return HAL_BUSY;
}
}

/**

  • @brief Receives an amount of data in DMA mode.
  • @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  •    the received data is handled as a set of u16. In this case, Size must reflect the number
    
  •    of u16 available through pData.
    
  • @param hirda Pointer to a IRDA_HandleTypeDef structure that contains
  •          the configuration information for the specified IRDA module.
    
  • @param pData Pointer to data buffer (u8 or u16 data elements).
  • @param Size Amount of data elements (u8 or u16) to be received.
  • @note When the IRDA parity is enabled (PCE = 1) the data received contain the parity bit.
  • @retval HAL status
    */
    HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
    {
    uint32_t *tmp;

/* Check that a Rx process is not already ongoing */
if (hirda->RxState == HAL_IRDA_STATE_READY)
{
if ((pData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}

/* Process Locked */
__HAL_LOCK(hirda);

hirda->pRxBuffPtr = pData;
hirda->RxXferSize = Size;

hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
hirda->RxState = HAL_IRDA_STATE_BUSY_RX;

/* Set the IRDA DMA transfer complete callback */
hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;

/* Set the IRDA DMA half transfer complete callback */
hirda->hdmarx->XferHalfCpltCallback = IRDA_DMAReceiveHalfCplt;

/* Set the DMA error callback */
hirda->hdmarx->XferErrorCallback = IRDA_DMAError;

/* Set the DMA abort callback */
hirda->hdmarx->XferAbortCallback = NULL;

/* Enable the DMA channel */
tmp = (uint32_t *)&pData;
HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->DR, *(uint32_t *)tmp, Size);

/* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
__HAL_IRDA_CLEAR_OREFLAG(hirda);

/* Process Unlocked */
__HAL_UNLOCK(hirda);

/* Enable the IRDA Parity Error Interrupt */
SET_BIT(hirda->Instance->CR1, USART_CR1_PEIE);

/* Enable the IRDA Error Interrupt: (Frame error, Noise error, Overrun error) */
SET_BIT(hirda->Instance->CR3, USART_CR3_EIE);

/* Enable the DMA transfer for the receiver request by setting the DMAR bit
in the USART CR3 register */
SET_BIT(hirda->Instance->CR3, USART_CR3_DMAR);

return HAL_OK;

}
else
{
return HAL_BUSY;
}
}

/**

  • @brief Pauses the DMA Transfer.
  • @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_DMAPause(IRDA_HandleTypeDef *hirda)
    {
    uint32_t dmarequest = 0x00U;

/* Process Locked */
__HAL_LOCK(hirda);

dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR3_DMAT);
if ((hirda->gState == HAL_IRDA_STATE_BUSY_TX) && dmarequest)
{
/* Disable the IRDA DMA Tx request */
CLEAR_BIT(hirda->Instance->CR3, USART_CR3_DMAT);
}

dmarequest = HAL_IS_BIT_SET(hirda->Instance->CR3, USART_CR

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值