Skip to content

Instantly share code, notes, and snippets.

@MrThanlon
Created July 28, 2020 09:21
Show Gist options
  • Save MrThanlon/731e0b4232972e8bf1a2a5d1c17c49c2 to your computer and use it in GitHub Desktop.
Save MrThanlon/731e0b4232972e8bf1a2a5d1c17c49c2 to your computer and use it in GitHub Desktop.
stm32tx
uint8_t txBuffer[256];
uint8_t txBufferPointer = 0;
uint8_t uartReady = 1;
/**
* @brief Sends an amount of data in asynchronous mode.
* @param huart: pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @param pData: Pointer to data buffer
* @param Size: Amount of data to be sent
* @retval HAL status
*/
void HAL_UART_Transmit_Queue(UART_HandleTypeDef *huart, uint8_t* pData, uint16_t size) {
// append to buffer
if (txBufferPointer + size > 255) {
// overflowed
size = 255 - txBufferPointer;
}
memcpy(txBuffer+txBufferPointer, pData, size);
txBufferPointer += size;
if (uartReady) {
HAL_UART_Transmit_IT(huart, txBuffer, txBufferPointer);
txBufferPointer = 0;
uartReady = 0;
}
}
/**
* @brief Tx Transfer completed callbacks.
* @param huart: pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @retval None
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (txBufferPointer) {
// continue
HAL_UART_Transmit_IT(huart, txBuffer, txBufferPointer);
txBufferPointer = 0;
uartReady = 0;
} else {
uartReady = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment