123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901 |
- /*
- ******************************************************************************
- * @file HAL_Uart.c
- * @version V1.0.0
- * @date 2020
- * @brief UART HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
- * @ Initialization and de-initialization functions
- * @ IO operation functions
- * @ Peripheral Control functions
- ******************************************************************************
- */
- #include "ACM32Fxx_HAL.h"
- /* If Use 'UART_MODE_TX_RX_DEBUG', Point to Debug Uart */
- UART_TypeDef *Uart_Debug = NULL;
- /* Private function prototypes -----------------------------------------------*/
- static void UART_Config_BaudRate(UART_HandleTypeDef *huart);
- static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart);
- __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
- __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
- __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
- /*********************************************************************************
- * Function : HAL_UART_IRQHandler
- * Description : Handle UART interrupt request.
- * Input : huart: UART handle.
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
- {
- uint32_t read_bytes_number;
- uint32_t isrflags, ieits, errorflags;
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return;
- #endif
- isrflags =READ_REG(huart->Instance->RIS);
- ieits =READ_REG(huart->Instance->IE);
- errorflags =0x00U;
- errorflags =(isrflags & (uint32_t)(UART_ICR_PEI|UART_ICR_OEI|UART_ICR_FEI|UART_ICR_BEI));
- /* Enable TXI */
- if (huart->Instance->IE & UART_IE_TXI)
- {
- if (huart->Instance->RIS & UART_RIS_TXI)
- {
- /* Clear TXI Status */
- SET_BIT(huart->Instance->ICR , UART_ICR_TXI);
- for(;;)
- {
- if(huart->lu32_TxCount == huart->lu32_TxSize)
- {
- huart->lu8_TxBusy = false;
- /* Disable TX interrupt */
- CLEAR_BIT(huart->Instance->IE, UART_IE_TXI);
- HAL_UART_TxCpltCallback(huart);
- break;
- }
- if (READ_BIT(huart->Instance->FR, UART_FR_TXFF))
- {
- break;
- }
- huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
- }
- }
- }
- /* RXI */
- if ((huart->Instance->IE & UART_IE_RXI || huart->Instance->IE& UART_IE_RTI) && errorflags == 0)
- {
- if (huart->Instance->RIS & UART_RIS_RXI)
- {
- read_bytes_number = 0;
- /* Clear RXI Status */
- SET_BIT(huart->Instance->ICR, UART_ICR_RXI);
- /* Receive end */
- while(huart->lu32_RxCount <huart->lu32_RxSize )
- {
- if(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
- {
- /* Store Data in buffer */
- huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
- read_bytes_number++;
- }
- else
- {
- break;
- }
- if (read_bytes_number == huart->lu32_fifo_level_minus1)
- {
- break;
- }
- }
- if(huart->lu32_RxCount ==huart->lu32_RxSize )
- {
- huart->lu8_RxBusy = false;
- /* Disable RX and RTI interrupt */
- CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));
- /* clear error interrupt */
- CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
- HAL_UART_RxCpltCallback(huart);
- }
- }
- else if(huart->Instance->RIS & UART_RIS_RTI)
- {
- /*clear RTI Status */
- SET_BIT(huart->Instance->ICR ,UART_ICR_RTI);
- while(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
- {
- huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
- }
- huart->lu8_RxBusy = false;
- /* Disable RX and RTI interrupt */
- CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));
- /* clear error interrupt */
- CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
- HAL_UART_RxCpltCallback(huart);
- }
- }
- /* if some errors occurred */
- if(errorflags != 0 &&(ieits & (UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI)))
- {
- /* UART parity error interrupt occurred */
- if (((isrflags & UART_RIS_PEI) != 0) && ((ieits & UART_IE_PEI) != 0))
- {
- /* Clear parity error status */
- SET_BIT(huart->Instance->ICR, UART_ICR_PEI);
- huart->ErrorCode |= HAL_UART_ERROR_PE;
- }
- /* UART break error interrupt occurred */
- if (((isrflags & UART_RIS_BEI) != 0) && ((ieits & UART_IE_BEI) != 0))
- {
- SET_BIT(huart->Instance->ICR, UART_RIS_BEI);
- huart->ErrorCode |= HAL_UART_ERROR_NE;
- }
- /* UART frame error interrupt occurred */
- if (((isrflags & UART_RIS_FEI) != 0) && ((ieits & UART_IE_FEI) != 0))
- {
- SET_BIT(huart->Instance->ICR, UART_RIS_FEI);
- huart->ErrorCode |= HAL_UART_ERROR_FE;
- }
- /* UART Over-Run interrupt occurred */
- if (((isrflags & UART_RIS_OEI) != 0) && ((ieits & UART_IE_OEI) != 0))
- {
- SET_BIT(huart->Instance->ICR, UART_RIS_OEI);
- huart->ErrorCode |= HAL_UART_ERROR_ORE;
- }
- /* clear error interrupt */
- CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
- HAL_UART_ErrorCallback(huart);
- }
- }
- /*********************************************************************************
- * Function : HAL_UART_Wait_Tx_Done
- * Description : wait Tx FIFO empty
- * Input : huart: UART handle.
- * Output :
- **********************************************************************************/
- static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- /* wait TX not busy*/
- while(READ_BIT(huart->Instance->FR, UART_FR_BUSY));
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_MspInit
- * Description : Initialize the UART MSP.
- * Input : huart: UART handle.
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
- {
- /*
- NOTE: This function should be modified, when the callback is needed,
- the HAL_UART_MspInit can be implemented in the user file.
- */
- /* For Example */
- GPIO_InitTypeDef GPIO_Uart1;
- if (huart->Instance == UART1)
- {
- /* Enable Clock */
- System_Module_Enable(EN_UART1);
- /* Initialization GPIO */
- /* A9:Tx A10:Rx */
- GPIO_Uart1.Pin = GPIO_PIN_9 | GPIO_PIN_10;
- GPIO_Uart1.Mode = GPIO_MODE_AF_PP;
- GPIO_Uart1.Pull = GPIO_PULLUP;
- GPIO_Uart1.Alternate = GPIO_FUNCTION_2;
- HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
- if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
- {
- /* A11:CTS */
- GPIO_Uart1.Pin = GPIO_PIN_11;
- HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
- }
- if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
- {
- /* A12:RTS */
- GPIO_Uart1.Pin = GPIO_PIN_12;
- HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
- }
- /* NVIC Config */
- NVIC_ClearPendingIRQ(UART1_IRQn);
- NVIC_SetPriority(UART1_IRQn, 5);
- NVIC_EnableIRQ(UART1_IRQn);
- }
- }
- /*********************************************************************************
- * Function : HAL_UART_Init
- * Description : Initialize the UART mode according to the specified parameters
- * in the UART_InitTypeDef
- * Input : huart: UART handle.
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- if (!IS_UART_WORDLENGTH(huart->Init.WordLength)) return HAL_ERROR;
- if (!IS_UART_STOPBITS(huart->Init.StopBits)) return HAL_ERROR;
- if (!IS_UART_PARITY(huart->Init.Parity)) return HAL_ERROR;
- if (!IS_UART_MODE(huart->Init.Mode)) return HAL_ERROR;
- if (!IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)) return HAL_ERROR;
- #endif
- /* Init the low level hardware : GPIO, CLOCK, NVIC */
- HAL_UART_MspInit(huart);
- /* Config BaudRate */
- UART_Config_BaudRate(huart);
- /* Set the UART Communication parameters */
- huart->Instance->LCRH = huart->Init.WordLength | UART_LCRH_FEN | huart->Init.StopBits | huart->Init.Parity;
- huart->Instance->CR = huart->Init.HwFlowCtl | huart->Init.Mode | UART_CR_UARTEN;
- if (huart->Init.Mode == UART_MODE_TX_RX_DEBUG)
- {
- Uart_Debug = huart->Instance;
- }
- else if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
- {
- huart->Instance->CR2 = UART_CR2_TXOE_SEL;
- }
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_MspDeInit
- * Description : DeInitialize the UART MSP.
- * Input : huart: UART handle.
- * Output :
- **********************************************************************************/
- __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
- {
- /*
- NOTE: This function should be modified, when the callback is needed,
- the HAL_UART_MspDeInit can be implemented in the user file.
- */
- if (huart->Instance == UART1)
- {
- /* Disable Clock */
- System_Module_Disable(EN_UART1);
- /* DeInitialization GPIO */
- /* A9:Tx A10:Rx */
- HAL_GPIO_DeInit(GPIOA,GPIO_PIN_9 | GPIO_PIN_10);
- if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
- {
- /* A11:CTS */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
- }
- if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
- {
- /* A12:RTS */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
- }
- /* NVIC DeInit */
- NVIC_DisableIRQ(UART1_IRQn);
- }
- else if(huart->Instance == UART2)
- {
- }
- }
- /*********************************************************************************
- * Function : HAL_UART_Init
- * Description : Initialize the UART mode according to the specified parameters
- * in the UART_InitTypeDef
- * Input : huart: UART handle.
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- /* DeInit the low level hardware : GPIO, CLOCK, NVIC */
- HAL_UART_MspDeInit(huart);
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Transmit
- * Description : Send an amount of data in blocking mode.
- * Input : huart: UART handle.
- * Input : fu8_Data: Pointer to data buffer.
- * Input : fu32_Size: Amount of data elements to be sent.
- * Input : fu32_Timeout: Timeout duration.
- * Output : HAL status
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
- {
- uint32_t lu32_Timeout;
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- huart->lu32_TxCount = 0;
- while (fu32_Size--)
- {
- huart->Instance->DR = *fu8_Data++;
- huart->lu32_TxCount++;
- /* have no timeout */
- if (fu32_Timeout == 0)
- {
- while (huart->Instance->FR & UART_FR_TXFF);
- }
- else
- {
- lu32_Timeout = fu32_Timeout *256;
- while (huart->Instance->FR & UART_FR_TXFF)
- {
- if (lu32_Timeout-- == 0)
- {
- return HAL_TIMEOUT;
- }
- }
- }
- }
- HAL_UART_Wait_Tx_Done(huart);
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Receive
- * Description : Receive an amount of data in blocking mode.
- * Input : huart: UART handle.
- * Input : fu8_Data: Pointer to data buffer.
- * Input : fu32_Size: Amount of data elements to be receive.
- * Input : fu32_Timeout: Timeout duration.
- * Output : HAL status
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
- {
- uint32_t lu32_Timeout;
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- huart->lu32_RxCount = 0;
- /* Half duplex Use Tx GPIO Receive Data */
- if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
- {
- huart->Instance->CR2 |= UART_CR2_RX_SEL;
- }
- while (fu32_Size--)
- {
- if (fu32_Timeout == 0)
- {
- while(huart->Instance->FR & UART_FR_RXFE);
- *fu8_Data++ = huart->Instance->DR;
- huart->lu32_RxCount++;
- }
- else
- {
- lu32_Timeout = fu32_Timeout * 256;
- while(huart->Instance->FR & UART_FR_RXFE)
- {
- if (lu32_Timeout-- == 0)
- {
- /* Clear Half duplex */
- huart->Instance->CR2 &= ~UART_CR2_RX_SEL;
- return HAL_TIMEOUT;
- }
- }
- *fu8_Data++ = huart->Instance->DR;
- huart->lu32_RxCount++;
- }
- }
- /* Clear Half duplex */
- huart->Instance->CR2 &= ~UART_CR2_RX_SEL;
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Transmit_IT
- * Description : Send an amount of data in interrupt mode.
- * Input : huart: UART handle.
- * Input : fu8_Data: Pointer to data buffer.
- * Input : fu32_Size: Amount of data elements to be receive.
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if (huart->lu8_TxBusy == true)
- {
- return HAL_BUSY;
- }
- if (fu32_Size == 0 || fu8_Data == NULL)
- {
- return HAL_ERROR;
- }
- huart->lu32_TxSize = fu32_Size;
- huart->lu32_TxCount = 0;
- huart->lu8_TxData = fu8_Data;
- huart->lu8_TxBusy = true;
- /* Clear TXI Status */
- huart->Instance->ICR = UART_ICR_TXI;
- /* FIFO Enable */
- SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
- /*FIFO Select*/
- SET_BIT(huart->Instance->IFLS,UART_TX_FIFO_1_2);
- for(;;)
- {
- /*Data Size less than 16Byte */
- if(fu32_Size == huart->lu32_TxCount)
- {
- huart->lu8_TxBusy = false;
- while ((huart->Instance->FR & UART_FR_BUSY)){}
- HAL_UART_TxCpltCallback(huart);
- return HAL_OK;
- }
- if(READ_BIT(huart->Instance->FR, UART_FR_TXFF))
- {
- break;
- }
- huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
- }
- /* Enable TX interrupt */
- huart->Instance->IE |= UART_IE_TXI;
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Receive_IT
- * Description : Receive an amount of data in interrupt mode.
- * Input :
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if (huart->lu8_RxBusy == true)
- {
- return HAL_BUSY;
- }
- if (fu32_Size == 0 || fu8_Data == NULL)
- {
- return HAL_ERROR;
- }
- huart->lu32_RxSize = fu32_Size;
- huart->lu32_RxCount = 0;
- huart->lu8_RxData = fu8_Data;
- huart->lu8_RxBusy = true;
- /* Clear RXI Status */
- huart->Instance->ICR = UART_ICR_RXI;
- /* FIFO Enable */
- SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
- /*FIFO Select*/
- MODIFY_REG(huart->Instance->IFLS, UART_IFLS_RXIFLSEL, UART_RX_FIFO_1_2);
- huart->lu32_fifo_level_minus1 = 8-1; // 16/2 - 1
- /* Enable the UART Errors interrupt */
- SET_BIT(huart->Instance->IE,UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
- /* Enable RX and RTI interrupt */
- SET_BIT(huart->Instance->IE,UART_IE_RXI|UART_IE_RTI);
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Transmit_DMA
- * Description : Send an amount of data in DMA mode.
- * Input : huart: UART handle.
- * Input : fu8_Data: Pointer to data buffer.
- * Input : fu32_Size: Amount of data elements to be Send.
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if (huart->lu8_TxBusy == true)
- {
- return HAL_BUSY;
- }
- if (fu32_Size == 0 || fu8_Data == NULL)
- {
- return HAL_ERROR;
- }
- huart->Instance->DMACR |= UART_DMACR_TXDMAE;
- if (HAL_DMA_Start_IT(huart->HDMA_Tx, (uint32_t)fu8_Data, (uint32_t)(&huart->Instance->DR), fu32_Size))
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_Receive_DMA
- * Description : Receive an amount of data in DMA mode.
- * Input : huart: UART handle.
- * Input : fu8_Data: Pointer to data buffer.
- * Input : fu32_Size: Amount of data elements to be receive.
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if (huart->lu8_RxBusy == true)
- {
- return HAL_BUSY;
- }
- if (fu32_Size == 0 || fu8_Data == NULL)
- {
- return HAL_ERROR;
- }
- huart->Instance->DMACR |= UART_DMACR_RXDMAE;
- if (HAL_DMA_Start_IT(huart->HDMA_Rx, (uint32_t)(&huart->Instance->DR), (uint32_t)fu8_Data, fu32_Size))
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_TxCpltCallback
- * Description : Tx Transfer completed callbacks.
- * Input :
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
- {
- /*
- NOTE: This function Should be modified, when the callback is needed,
- the HAL_UART_TxCpltCallback could be implemented in the user file.
- */
- }
- /*********************************************************************************
- * Function : HAL_UART_RxCpltCallback
- * Description : Rx Transfer completed callbacks.
- * Input :
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- /*
- NOTE: This function Should be modified, when the callback is needed,
- the HAL_UART_RxCpltCallback could be implemented in the user file.
- */
- }
- /*********************************************************************************
- * Function : HAL_UART_ErrorCallBack
- * Description : Recv Error callbacks.
- * Input :
- * Output :
- **********************************************************************************/
- __weak void HAL_UART_ErrorCallBack(UART_HandleTypeDef *huart)
- {
- /*
- NOTE: This function Should be modified, when the callback is needed,
- the HAL_UART_ErrorCallBack could be implemented in the user file.
- */
- }
- /*********************************************************************************
- * Function : UART_Config_BaudRate
- * Description : Config BaudRate
- * Input :
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- static void UART_Config_BaudRate(UART_HandleTypeDef *huart)
- {
- uint32_t lu32_PCLK;
- uint32_t lu32_IBAUD, lu32_FBAUD;
- uint64_t lu64_TempValue;
- lu32_PCLK = System_Get_APBClock();
- /* Integral part */
- lu32_IBAUD = lu32_PCLK / (huart->Init.BaudRate * 16);
- /* Fractional part */
- lu64_TempValue = lu32_PCLK % (huart->Init.BaudRate * 16);
- lu64_TempValue = (lu64_TempValue * 1000000) / (huart->Init.BaudRate * 16);
- lu32_FBAUD = (lu64_TempValue * 64 + 500000) / 1000000;
- if (lu32_FBAUD >= 64)
- {
- huart->Instance->IBRD = lu32_IBAUD + 1;
- huart->Instance->FBRD = 0;
- }
- else
- {
- huart->Instance->IBRD = lu32_IBAUD;
- huart->Instance->FBRD = lu32_FBAUD;
- }
- }
- /*********************************************************************************
- * Function : HAL_UART_GetState
- * Description : Return the uart State
- * Input :
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if(huart->lu8_TxBusy || huart->lu8_RxBusy)
- {
- return HAL_BUSY;
- }
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_GetError
- * Description : Return the uart Error
- * Input :
- * Output :
- **********************************************************************************/
- uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
- {
- return huart->ErrorCode;
- }
- /*********************************************************************************
- * Function : HAL_UART_Abort
- * Description : Abort ongoing transfers(blocking mode)
- * Input : UART handle
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- /*disble all interrupt*/
- huart->Instance->IE =0x00;
- /* Disable the UART DMA Tx request if enable */
- if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
- {
- CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
- /* Abort the UART Tx Channel */
- if(huart->HDMA_Tx)
- {
- /*Set the UART DMA Abort callback to Null */
- huart->HDMA_Tx->DMA_ITC_Callback =NULL;
- if(HAL_DMA_Abort(huart->HDMA_Tx)!=HAL_OK)
- {
- return HAL_TIMEOUT;
- }
- }
- }
- /* Disable the UART DMA Rx request if enable */
- if(READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
- {
- CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
- /* Abort the UART Rx Channel */
- if(huart->HDMA_Rx)
- {
- /*Set the UART DMA Abort callback to Null */
- huart->HDMA_Rx->DMA_ITC_Callback =NULL;
- if(HAL_DMA_Abort(huart->HDMA_Rx)!=HAL_OK)
- {
- return HAL_TIMEOUT;
- }
- }
- }
- /*Reset Tx and Rx Transfer size*/
- huart->lu32_TxSize = 0;
- huart->lu32_RxSize = 0;
- /* Restore huart->lu8_TxBusy and huart->lu8_RxBusy to Ready */
- huart->lu8_TxBusy = false;
- huart->lu8_RxBusy = false;
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_DMAPause
- * Description : Pause the DMA Transfer
- * Input : UART handle
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
- {
- /* Disable the UART DMA Tx request */
- CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
- }
- if (READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
- {
- /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
- CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);
- /* Disable the UART DMA Rx request */
- CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
- }
- return HAL_OK;
- }
- /*********************************************************************************
- * Function : HAL_UART_DMAResume
- * Description : Resume the DMA Transfer
- * Input : UART handle
- * Output :
- **********************************************************************************/
- HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
- {
- #if (USE_FULL_ASSERT == 1)
- if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
- #endif
- if (huart->lu8_TxBusy == false)
- {
- /* Enable the UART DMA Tx request */
- SET_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
- }
- if (huart->lu8_RxBusy == false)
- {
- /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
- SET_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);
- /* Enable the UART DMA Rx request */
- SET_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
- }
- return HAL_OK;
- }
- #if 0
- /*********************************************************************************
- * Function : fputc
- * Description :
- * Input :
- * Output :
- * Author : Chris_Kyle Data : 2020
- **********************************************************************************/
- int fputc(int ch, FILE *f)
- {
- if (Uart_Debug == NULL)
- {
- return 0;
- }
- Uart_Debug->DR = ch;
- while ((Uart_Debug->FR & UART_FR_BUSY));
- return ch;
- }
- #endif
|