在线时间2208 小时
UID2073122
注册时间2016-1-12
NXP金币119
TA的每日心情 | 开心 2020-6-18 08:45 |
---|
签到天数: 812 天 连续签到: 1 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 6321
- 最后登录
- 2025-7-15
|
本帖最后由 原来的你 于 2016-11-10 23:30 编辑
本节我们讲解LPC824的DMA,有了DMA可以减轻LPC824 MCU的负责,本次我们把DMA结合串口一起使用。
int main(void)
{
int bytes, idx;
uint8_t buff[UARTRXBUFFSIZE];
SystemCoreClockUpdate();
Board_Init();
Init_UART_PinMux();
DEBUGSTR("EEPROM demo\r\n");
DEBUGOUT("DMA-UART Clock: %uMHz\r\n", SystemCoreClock / 1000000);
DEBUGOUT("Device ID: 0x%x\r\n", Chip_SYSCTL_GetDeviceID());
Board_LED_Set(0, false);
Chip_UART_Init(LPC_USART0);
Chip_UART_ConfigData(LPC_USART0, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
Chip_Clock_SetUSARTNBaseClockRate((115200 * 16), true);
Chip_UART_SetBaud(LPC_USART0, 115200);
Chip_UART_Enable(LPC_USART0);
Chip_UART_TXEnable(LPC_USART0);
/* DMA initialization - enable DMA clocking and reset DMA if needed */
Chip_DMA_Init(LPC_DMA);//初始化DMA
/* Enable DMA controller and use driver provided DMA table for current descriptors */
Chip_DMA_Enable(LPC_DMA);//使能DMA
Chip_DMA_SetSRAMBase(LPC_DMA, DMA_ADDR(Chip_DMA_Table));//设置DMA内存地址
/* Setup UART 0 TX DMA support */
dmaTXSetup();//使能串口0 DMA发送功能
/* Setup UART 0 RX DMA support */
dmaRXSetup();//使能串口0 DMA接收功能
/* Enable the DMA IRQ */
NVIC_EnableIRQ(DMA_IRQn);//使能DMA IRQ
/* Enqueue a bunch of strings in DMA transmit descriptors and start
transmit. In this use of DMA, the descriptors aren't chained, so
the DMA restarts the next queued descriptor in the DMA interrupt
handler. */
for (idx = 0; idx < DMASENDSTRCNT; idx++) {
sprintf(dmaSendStr[idx], "DMA send string (unlinked) #%d\r\n", idx);//打印DMA发送数据
dmaTXSend((uint8_t *) dmaSendStr[idx], strlen(dmaSendStr[idx]));//DMA发送数据
}
/* Wait for UART TX DMA channel to go inactive */ //等待串口DMA发送完成
while (1) {
__WFI();
if (Chip_DMA_GetActiveChannels(LPC_DMA) & (1 << DMAREQ_USART0_TX)) {
break;
}
}
/* Receive buffers are queued. The DMA interrupt will only trigger on a
full DMA buffer receive, so if the UART is idle, but the DMA is only
partially complete, the DMA interrupt won't fire. For UART data
receive where data is not continuous, a timeout method will be
required to flush the DMA when the DMA has pending data and no
data has been received on the UART in a specified timeout */
dmaRXQueue();
/* Get RX data via DMA and send it out on TX via DMA *///通过DMA接收数据并且通过DMA发送出来
while (1) {
/* Sleep until something happens */
__WFI();
/* Did any data come in? */
bytes = checkRxData(buff);
if (bytes > 0) {
/* RX data received, send it via TX DMA */
dmaTXSend(buff, bytes);
}
}
}
/* Setup DMA UART TX support, but do not queue descriptors yet */
static void dmaTXSetup(void)
{
/* Setup UART 0 TX channel for the following configuration:
- Peripheral DMA request (UART 0 TX channel)
- Single transfer
- Low channel priority */
Chip_DMA_EnableChannel(LPC_DMA, DMAREQ_USART0_TX);//选择串口0发送作为DMA通道
Chip_DMA_EnableIntChannel(LPC_DMA, DMAREQ_USART0_TX);//使能串口0发送中断
Chip_DMA_SetupChannelConfig(LPC_DMA, DMAREQ_USART0_TX,
(DMA_CFG_PERIPHREQEN | DMA_CFG_TRIGBURST_SNGL | DMA_CFG_CHPRIORITY(3)));//配置DMA发送
countTXDescUsed = 0;
}
/* Setup DMA UART RX support, but do not queue descriptors yet */
static void dmaRXSetup(void)
{
/* Setup UART 0 RX channel for the following configuration:
- Peripheral DMA request (UART 0 RX channel)
- Single transfer
- Low channel priority */
Chip_DMA_EnableChannel(LPC_DMA, DMAREQ_USART0_RX);//选择串口0接收作为DMA通道
Chip_DMA_EnableIntChannel(LPC_DMA, DMAREQ_USART0_RX);//选择串口0接收中断
Chip_DMA_SetupChannelConfig(LPC_DMA, DMAREQ_USART0_RX,
(DMA_CFG_PERIPHREQEN | DMA_CFG_TRIGBURST_SNGL | DMA_CFG_CHPRIORITY(3)));//配置DMA接收
}
|
-
|