ZHDA270 August 2026 MSPM0G5117 , MSPM0G5187
USB 转 UART:
/********************************************************
* Function name : usb2uart_popTask
* Description : USB -> UART. Pop task from queue and transmit data through
* UART using DMA transfer. Waits if UART TX already in progress.
* Called from main loop when fifoCount > 0.
* Parameter : None (modifies gUSB2UARTTask, uartTxInProgress)
* Return : None
**********************************************************/
void usb2uart_popTask(void)
{
if(uartTxInProgress) return;
//Current process task
TxElement_t *pTask = &gUSB2UARTTask.fifoPointer[gUSB2UARTTask.fifoOut];
//Transmit data through UART DMA
DL_DMA_setSrcAddr(DMA, DMA_CH0_CHAN_ID, (uint32_t) pTask->buffer);
DL_DMA_setDestAddr(DMA, DMA_CH0_CHAN_ID, (uint32_t)(&UART_0_INST->uart->TXDATA));
DL_DMA_setTransferSize(DMA, DMA_CH0_CHAN_ID, pTask->length);
DL_DMA_enableChannel(DMA, DMA_CH0_CHAN_ID);
uartTxInProgress = true;
//Update Task status
fifoCompleteRead(&gUSB2UARTTask, UART_TX_BUF_SIZE);
}
/********************************************************
* Function name : usb2uart_pushTask
* Description : USB -> UART. Read data from USB CDC interface and push
* into gUSB2UARTTask queue. Each task holds one USB frame
* (up to 64 bytes). Called from tud_cdc_rx_cb interrupt.
* Parameter : None (modifies gUSB2UARTTask)
* Return : None
**********************************************************/
void usb2uart_pushTask(void)
{
// Get current task slot in queue
TxElement_t *pTask = &gUSB2UARTTask.fifoPointer[gUSB2UARTTask.fifoIn];
// Read up to 64 bytes from USB CDC interface (interface 0 is the first/only CDC interface)
uint32_t cnt = tud_cdc_n_read(0,
pTask->buffer,
USB_FRAME_PACKAGE);
// Skip if no data received
if(cnt == 0) return;
pTask->length = cnt; // Store actual data length
// Advance queue pointer and mark task as ready
fifoCompleteWrite(&gUSB2UARTTask, UART_TX_BUF_SIZE);
}
UART 转 USB:
/********************************************************
* Function name : uart2usb_pushTask
* Description : UART -> USB. Read one byte from UART and accumulate into
* current task buffer. When buffer full (64 bytes) or when
* timeout triggers, move to next task. Called from UART RX
* interrupt handler.
* Parameter : None (modifies gUART2USBTask, TIMER_0_INST)
* Return : None
**********************************************************/
void uart2usb_pushTask(void)
{
uint8_t byte = DL_UART_Main_receiveData(UART_0_INST);
// Get current task buffer
TxElement_t *pTask = &gUART2USBTask.fifoPointer[gUART2USBTask.fifoIn];
// Check if current task buffer is full (64 bytes)
if (pTask->length < USB_FRAME_PACKAGE - 1) {
// Buffer not full: add byte and restart timeout timer
pTask->buffer[pTask->length++] = byte;
// Restart timeout to wait for more data
DL_Timer_setTimerCount(TIMER_0_INST, UART_TIMEOUT);
DL_Timer_startCounter(TIMER_0_INST);
}
else if(pTask->length == USB_FRAME_PACKAGE - 1){
// Adding last byte (position 63): task becomes full
pTask->buffer[pTask->length++] = byte;
// Move to next task since buffer is now full (64 bytes)
fifoCompleteWrite(&gUART2USBTask, USB_TX_BUF_SIZE);
}
else {
// Error: buffer overflow (should never happen if logic is correct)
__BKPT(0);
}
}
/********************************************************
* Function name : uart2usb_popTask
* Description : UART -> USB. Pop task from queue and transmit data through
* USB CDC interface. All data in one task is sent in same USB
* frame. Checks if USB TX buffer has space. Called from main
* loop when fifoCount > 0.
* Parameter : None (modifies gUART2USBTask)
* Return : None
**********************************************************/
void uart2usb_popTask(void)
{
// Get current task
TxElement_t *pTask = &gUART2USBTask.fifoPointer[gUART2USBTask.fifoOut];
// Wait for USB TX FIFO to be completely empty before sending next task
// This ensures clean task boundaries and clear completion points
if(tud_cdc_n_write_available(0) >= pTask->length){
// FIFO has enough space for entire task
uint32_t written = tud_cdc_n_write(0, pTask->buffer, pTask->length);
// Only advance queue if entire task was written in one go
if(written == pTask->length){
tud_cdc_n_write_flush(0);
fifoCompleteRead(&gUART2USBTask, USB_TX_BUF_SIZE);
}
}
}