赞
踩
UART在嵌入式开发中非常重要,因为很容易在PC找到串口,用来跟嵌入式设备进行简单的通信。比如常见的命令行交互方式……可以输出Log,可以输入命令等等。因此几乎所有的IC都会支持有UART功能。
速度可达到3Mbps
单独的328的TX FIFO和3212的RX FIFO,这里FIFO应当是32bit的宽度,TX可以FIFO 8个这样的元素,RX可以FIFO 12个这样的元素。FIFO长度可编程,可配为深度为1。
FIFO trigger levels of ⅛, ¼, ½, ¾, and ⅞
支持硬件流控
上图可看出,TX FIFO和RX FIFO的最大深度都是32 Bytes,只是RX FIFO每个单元除了一个Byte,还有额外的4个状态信息的bit。
串口驱动说明:
Generic API function | API function | Description |
---|---|---|
UART_init() | UARTCC26XX_init() | Initialize UART driver |
UART_open() | UARTCC26XX_open() | Initialize UART HW and set system dependencies |
UART_close() | UARTCC26XX_close() | Disable UART HW and release system dependencies |
UART_control() | UARTCC26XX_control() | Configure an already opened UART handle |
UART_read() | UARTCC26XX_read() | Start read from UART |
UART_readCancel() | UARTCC26XX_readCancel() | Cancel ongoing read from UART |
UART_write() | UARTCC26XX_write() | Start write to UART |
UART_writeCancel() | UARTCC26XX_writeCancel() | Cancel ongoing write to UART |
– TX和RX有自己独立的DMA通道
– 只要FIFO有数据时,可以从FIFO一个一个接收data,也可以在FIFO level达到时一次性接收data
– 只要FIFO有空间时,可以一个一个写data到FIFO,也可以一次写FIFO level个data
如下的实例是Block方式UART接收方式,一次接收100个Byte的数据
// ## Basic Receive # // Receive 100 bytes over UART in ::UART_MODE_BLOCKING. // @code UART_Handle handle; UART_Params params; uint8_t rxBuf[100]; // Receive buffer uint32_t timeoutUs = 5000; // 5ms timeout, default timeout is no timeout (BIOS_WAIT_FOREVER) // Init UART and specify non-default parameters UART_Params_init(¶ms); params.baudRate = 9600; params.writeDataMode = UART_DATA_BINARY; params.readTimeout = timeoutUs / Clock_tickPeriod; // Default tick period is 10us // Open the UART and do the read handle = UART_open(Board_UART, ¶ms); int rxBytes = UART_read(handle, rxBuf, 100);
Block模式下接收部分数据
// ## Receive with Return Partial # // This use case will read in ::UART_MODE_BLOCKING until the wanted amount of bytes is // received or until a started reception is inactive for a 32-bit period. // This UART_read() call can also be used when unknown amount of bytes shall // be read. Note: The partial return is also possible in ::UART_MODE_CALLBACK mode. UART_Handle handle; UART_Params params; uint8_t rxBuf[100]; // Receive buffer // Init UART and specify non-default parameters UART_Params_init(¶ms); params.baudRate = 9600; params.writeDataMode = UART_DATA_BINARY; // Open the UART and initiate the partial read handle = UART_open(Board_UART, ¶ms); // Enable RETURN_PARTIAL UART_control(handle, UARTCC26XX_CMD_RETURN_PARTIAL_ENABLE, NULL); // Begin read int rxBytes = UART_read(handle, rxBuf, 100));
//## Basic Transmit #
// This case will configure the UART to send the data in txBuf in BLOCKING_MODE.
UART_Handle handle;
UART_Params params;
uint8_t txBuf[] = "Hello World"; // Transmit buffer
// Init UART and specify non-default parameters
UART_Params_init(¶ms);
params.baudRate = 9600;
params.writeDataMode = UART_DATA_BINARY;
// Open the UART and do the write
handle = UART_open(Board_UART, ¶ms);
UART_write(handle, txBuf, sizeof(txBuf));
//## Receive Continously in ::UART_MODE_CALLBACK \anchor USE_CASE_CB # // This case will configure the UART to receive continously in // ::UART_MODE_CALLBACK, 16 bytes at the time and transmit them back via UART TX. // Note that UART_Params.readTimeout is not in use when using ::UART_MODE_CALLBACK mode. #define MAX_NUM_RX_BYTES 1000 // Maximum RX bytes to receive in one go #define MAX_NUM_TX_BYTES 1000 // Maximum TX bytes to send in one go uint32_t wantedRxBytes; // Number of bytes received so far int8_t rxBuf[MAX_NUM_RX_BYTES]; // Receive buffer uint8_t txBuf[MAX_NUM_TX_BYTES]; // Transmit buffer // Callback function static void readCallback(UART_Handle handle, void *rxBuf, size_t size) { // Copy bytes from RX buffer to TX buffer for(size_t i = 0; i < size; i++) txBuf[i] = ((uint8_t*)rxBuf)[i]; // Echo the bytes received back to transmitter UART_write(handle, txBuf, size); // Start another read, with size the same as it was during first call to // UART_read() UART_read(handle, rxBuf, wantedRxBytes); } static void taskFxn(UArg a0, UArg a1) { UART_Handle handle; UART_Params params; // Init UART and specify non-default parameters UART_Params_init(¶ms); params.baudRate = 9600; params.writeDataMode = UART_DATA_BINARY; params.readMode = UART_MODE_CALLBACK; params.readDataMode = UART_DATA_BINARY; params.readCallback = readCallback; // Open the UART and initiate the first read handle = UART_open(Board_UART, ¶ms); wantedRxBytes = 16; int rxBytes = UART_read(handle, rxBuf, wantedRxBytes); while(true); // Wait forever }
从上面的Demo看到,主要是用到了UART_Write()和UART_Read()这两个接口,其具体实现的流程图如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。