LPC5500的SDK中提供了非常豐富的串口例程(如下圖所示):
但是,偏偏沒有串口DMA發(fā)送+中斷接收這種組合。
實話說小編覺得這種組合才是大部分MCU場景中最常用的。尤其是對一些RX數據量不大(比如只是解析一些命令)但是TX數據量大的應用(比如定時,高頻發(fā)送數據)再合適不過,該架構模型簡單,非常容易理解。
本篇文章就手把手教大家分分鐘擼一個DMA發(fā)送+中斷接收的例程:
我們復制一個dma_transfer例程作為模板,重命名為dma_tx_int_rx(名字無所謂,自己起即可)。打開,編譯下載運行一下,確保原版demo在自己的板子上可以順利的運行,咱們把前期工作都鋪墊好。
下面我們開始改代碼:dma_transfer原版代碼是TX和RX都是dma傳輸,我們只需要將RX改為中斷接收數據即可。
-
首先將USART_TransferCreateHandleDMA函數修改下,將所有RX DMA有關的東西去掉,handler填NULL就可以。這樣usart_dmadriver就不會處理RX DMA有關的操作了:
USART_TransferCreateHandleDMA(DEMO_USART, &g_uartDmaHandle, USART_UserCallback, NULL,&g_uartTxDmaHandle, NULL);
2. 開啟usart RX中斷,沒啥可說的,常規(guī)操作:
/* Enable RX interrupt. */ USART_EnableInterrupts(DEMO_USART, kUSART_RxLevelInterruptEnable | kUSART_RxErrorInterruptEnable); EnableIRQ(DEMO_USART_IRQn);
#define DEMO_USART_IRQHandler FLEXCOMM0_IRQHandler #define DEMO_USART_IRQn FLEXCOMM0_IRQn
void DEMO_USART_IRQHandler(void) { uint8_t data; /* If new data arrived. */ if ((kUSART_RxFifoNotEmptyFlag | kUSART_RxError | kUSART_RxFifoFullFlag) & USART_GetStatusFlags(DEMO_USART)) { data = USART_ReadByte(DEMO_USART); PRINTF("usart rx interrupt:%c ", data); if(data == 's')
{
/* Send g_tipString out. */
xfer.data = g_tipString;
xfer.dataSize = sizeof(g_tipString) - 1;
g_uartDmaHandle.txState = kUSART_TxIdle;
USART_TransferSendDMA(DEMO_USART, &g_uartDmaHandle, &xfer);
}
}
if ((0U != (DEMO_USART->INTENSET & USART_INTENSET_TXIDLEEN_MASK)) && (0U != (DEMO_USART->INTSTAT &USART_INTSTAT_TXIDLE_MASK)))
{
USART_TransferDMAHandleIRQ(DEMO_USART, &g_uartDmaHandle);
}
}
4. 測試:改好代碼,下載運行:當串口敲入任意鍵時候,程序都會回顯接收到數據,當按鍵”s”時,會調用USART_TransferSendDMA函數使用DMA發(fā)送一串數據,并進入DMA發(fā)送完成回調函數。
代碼清單
以下是完整代碼清單(可以直接復制到usart_dma_transfer.c里運行):
/* * Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pin_mux.h"
#include "board.h"
#include "fsl_usart.h"
#include "fsl_usart_dma.h"
#include "fsl_dma.h"
#include "fsl_debug_console.h"
#include
#include "fsl_power.h"
#define DEMO_USART USART0
#define DEMO_USART_CLK_SRC kCLOCK_Flexcomm0
#define DEMO_USART_CLK_FREQ CLOCK_GetFlexCommClkFreq(0U)
#define USART_RX_DMA_CHANNEL 4
#define USART_TX_DMA_CHANNEL 5
#define EXAMPLE_UART_DMA_BASEADDR DMA0
#define DEMO_USART_IRQHandler FLEXCOMM0_IRQHandler
#define DEMO_USART_IRQn FLEXCOMM0_IRQn
#define ECHO_BUFFER_LENGTH 8
usart_transfer_t xfer;
usart_dma_handle_t g_uartDmaHandle;
dma_handle_t g_uartTxDmaHandle;
uint8_t g_tipString[] = "This string is send from UART_DMA ";
#define kUSART_TxIdle 0
void USART_UserCallback(USART_Type *base, usart_dma_handle_t *handle, status_t status, void *userData)
{
userData = userData;
if (kStatus_USART_TxIdle == status)
{
PRINTF("USART_UserCallback, status:0x%X ", status);
}
}
void DEMO_USART_IRQHandler(void) { uint8_t data; /* If new data arrived. */ if ((kUSART_RxFifoNotEmptyFlag | kUSART_RxError | kUSART_RxFifoFullFlag) & USART_GetStatusFlags(DEMO_USART)) { data = USART_ReadByte(DEMO_USART); PRINTF("usart rx interrupt:%c ", data); if(data == 's')
{
/* Send g_tipString out. */
xfer.data = g_tipString;
xfer.dataSize = sizeof(g_tipString) - 1;
g_uartDmaHandle.txState = kUSART_TxIdle;
USART_TransferSendDMA(DEMO_USART, &g_uartDmaHandle, &xfer);
}
}
if ((0U != (DEMO_USART->INTENSET & USART_INTENSET_TXIDLEEN_MASK)) && (0U != (DEMO_USART->INTSTAT &USART_INTSTAT_TXIDLE_MASK)))
{
USART_TransferDMAHandleIRQ(DEMO_USART, &g_uartDmaHandle);
}
}
int main(void)
{
usart_config_t config;
/* set BOD VBAT level to 1.65V */
POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false);
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
PRINTF("USART: TX DMA, RX INTERRUPT ");
PRINTF("press 's' for DMA TX tranmsit ");
USART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;
USART_Init(DEMO_USART, &config, DEMO_USART_CLK_FREQ);
/* Configure DMA. */
DMA_Init(EXAMPLE_UART_DMA_BASEADDR);
DMA_EnableChannel(EXAMPLE_UART_DMA_BASEADDR, USART_TX_DMA_CHANNEL);
DMA_EnableChannel(EXAMPLE_UART_DMA_BASEADDR, USART_RX_DMA_CHANNEL);
DMA_CreateHandle(&g_uartTxDmaHandle, EXAMPLE_UART_DMA_BASEADDR, USART_TX_DMA_CHANNEL);
USART_TransferCreateHandleDMA(DEMO_USART, &g_uartDmaHandle, USART_UserCallback, NULL, &g_uartTxDmaHandle, NULL);
/* Send g_tipString out. */
xfer.data = g_tipString;
xfer.dataSize = sizeof(g_tipString) - 1;
USART_TransferSendDMA(DEMO_USART, &g_uartDmaHandle, &xfer);
/* Enable RX interrupt. */
USART_EnableInterrupts(DEMO_USART, kUSART_RxLevelInterruptEnable | kUSART_RxErrorInterruptEnable);
EnableIRQ(DEMO_USART_IRQn);
while(1);
}
-
mcu
+關注
關注
146文章
17148瀏覽量
351197 -
串口
+關注
關注
14文章
1554瀏覽量
76516 -
dma
+關注
關注
3文章
561瀏覽量
100586
發(fā)布評論請先 登錄
相關推薦
評論