初级会员

- 积分
- 56
- 金钱
- 56
- 注册时间
- 2018-3-13
- 在线时间
- 41 小时
|
10金钱
- /**
- * @brief Receive an amount of data in blocking mode.
- * @param huart UART handle.
- * @param pData pointer to data buffer.
- * @param Size amount of data to be received.
- * @param Timeout Timeout duration.
- * [url=home.php?mod=space&uid=60778]@note[/url] When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
- * address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
- * (as received data will be handled using u16 pointer cast). Depending on compilation chain,
- * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint16_t* tmp;
- uint16_t uhMask;
- uint32_t tickstart = 0;
- /* Check that a Rx process is not already ongoing */
- if(huart->RxState == HAL_UART_STATE_READY) <font color="#ff0000">//这里进不去,发送第二个字节才能进,应该在配置上加什么样的初始化</font>
- {
- if((pData == NULL ) || (Size == 0U))
- {
- return HAL_ERROR;
- }
- /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
- should be aligned on a u16 frontier, as data to be received from RDR will be
- handled through a u16 cast. */
- if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
- {
- if((((uint32_t)pData)&1U) != 0U)
- {
- return HAL_ERROR;
- }
- }
- /* Process Locked */
- __HAL_LOCK(huart);
- huart->ErrorCode = HAL_UART_ERROR_NONE;
- huart->RxState = HAL_UART_STATE_BUSY_RX;
- /* Init tickstart for timeout managment*/
- tickstart = HAL_GetTick();
- huart->RxXferSize = Size;
- huart->RxXferCount = Size;
- /* Computation of UART mask to apply to RDR register */
- UART_MASK_COMPUTATION(huart);
- uhMask = huart->Mask;
- /* as long as data have to be received */
- while(huart->RxXferCount > 0U)
- {
- huart->RxXferCount--;
- if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
- {
- return HAL_TIMEOUT;
- }
- if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
- {
- tmp = (uint16_t*) pData ;
- *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
- pData +=2U;
- }
- else
- {
- *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
- }
- }
- /* At end of Rx process, restore huart->RxState to Ready */
- huart->RxState = HAL_UART_STATE_READY;
- /* Process Unlocked */
- __HAL_UNLOCK(huart);
- return HAL_OK;
- }
- else
- {
- return HAL_BUSY;
- }
- }
复制代码
|
|