如何在低速 MSP430 上获取高速 UART 数据

How does one get high-speed UART data on a low-speed MSP430

我的项目有一个通过 UART 连接到 Bluegiga 蓝牙模块的 MSP430。 MCU 必须能够从 BG 模块接收可变长度的消息。在当前架构中,每个接收到的字节都会生成一个 UART 中断以允许消息处理,并且功率限制对 MSP430 的时钟速度施加了限制。这使得 MSP430 很难跟上任何快于 9600bps 的 UART 速度。结果是一个缓慢的通信接口。加快数据速率会导致超限错误、丢失字节和中断通信。

关于在这种情况下如何在不牺牲数据完整性的情况下提高通信速度的任何想法?

通过使用 MSP430 上 3 个可用 DMA 通道中的 2 个来填充环形缓冲区,然后由 CPU 处理,我能够实现 12 倍的速度提升。这有点棘手,因为 MSP430 DMA 中断仅在大小寄存器达到零时生成,所以我不能直接填充环形缓冲区,因为消息大小是可变的。

使用一个 DMA 通道作为单字节缓冲区,在 UART 接收到每个字节时触发,然后触发填充环形缓冲区的第二个 DMA 通道。

下面是说明该方法的示例 C 代码。请注意,它包含了来自 MSP430 库的引用。

#include "dma.h"

#define BLUETOOTH_RXQUEUE_SIZE <size_of_ring_buffer>

static int headIndex = 0;
static int tailIndex = 0;

static char uartRecvByte;
static char bluetoothRXQueue[BLUETOOTH_RXQUEUE_SIZE];

/*!********************************************************************************
  * \brief Initialize DMA hardware
  *
  * \param none
  *
  * \return none
  *
  ******************************************************************************/
void init(void)
{
    // This is the primary buffer.
    //  It will be triggered by UART Rx and generate an interrupt.
    //  It's purpose is to service every byte recieved by the UART while
    //    also waking up the CPU to let it know something happened.
    //  It uses a single address of RAM to store the data, so it needs to be
    //    serviced before the next byte is received from the UART.
    //  This was done so that any byte received triggers processing of the data.
    DMA_initParam dmaSettings;
    dmaSettings.channelSelect = DMA_CHANNEL_2;
    dmaSettings.transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE;
    dmaSettings.transferSize = 1;
    dmaSettings.transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE;
    dmaSettings.triggerSourceSelect = DMA_TRIGGERSOURCE_20; // USCA1RXIFG, or any UART recieve trigger
    dmaSettings.triggerTypeSelect = DMA_TRIGGER_RISINGEDGE;
    DMA_init(&dmaSettings);
    DMA_setSrcAddress(DMA_CHANNEL_2, (UINT32)&UCA1RXBUF, DMA_DIRECTION_UNCHANGED);
    DMA_setDstAddress(DMA_CHANNEL_2, (UINT32)&uartRecvByte, DMA_DIRECTION_UNCHANGED);

    // This is the secondary buffer.
    //  It will be triggered when DMA_CHANNEL_2 copies a byte and will store bytes into a ring buffer.
    //  It's purpose is to pull data from DMA_CHANNEL_2 as quickly as possible
    //    and add it to the ring buffer.
    dmaSettings.channelSelect = DMA_CHANNEL_0;
    dmaSettings.transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE;
    dmaSettings.transferSize = BLUETOOTH_RXQUEUE_SIZE;
    dmaSettings.transferUnitSelect = DMA_SIZE_SRCBYTE_DSTBYTE;
    dmaSettings.triggerSourceSelect = DMA_TRIGGERSOURCE_30; // DMA2IFG
    dmaSettings.triggerTypeSelect = DMA_TRIGGER_RISINGEDGE;
    DMA_init(&dmaSettings);
    DMA_setSrcAddress(DMA_CHANNEL_0, (UINT32)&uartRecvByte, DMA_DIRECTION_UNCHANGED);
    DMA_setDstAddress(DMA_CHANNEL_0, (UINT32)&bluetoothRXQueue, DMA_DIRECTION_INCREMENT);

    DMA_enableTransfers(DMA_CHANNEL_2);
    DMA_enableTransfers(DMA_CHANNEL_0);
    DMA_enableInterrupt(DMA_CHANNEL_2);
}

/*!********************************************************************************
  * \brief DMA Interrupt for receipt of data from the Bluegiga module
  *
  * \param none
  *
  * \return none
  *
  * \par Further Detail
  * \n Dependencies:   N/A
  * \n Processing:     Clear the interrupt and update the circular buffer head
  * \n Error Handling: N/A
  * \n Tests:          N/A
  * \n Special Considerations: N/A
  *
  ******************************************************************************/
void DMA_Interrupt(void)
{
    DMA_clearInterrupt(DMA_CHANNEL_2);
    headIndex = BLUETOOTH_RXQUEUE_SIZE - DMA_getTransferSize(DMA_CHANNEL_0);

    if (headIndex == tailIndex)
    {
        // This indicates ring buffer overflow.
    }
    else
    {
        // Perform processing on the current state of the ring buffer here.
        // If only partial data has been received, just leave.  Either set a flag
        // or generate an event to process the message outside of the interrupt.
        // Once the message is processed, move the tailIndex.
    }
}