kinetis的UART串口(DMA模式)
扫描二维码
随时随地手机看文章
前面的例子中,串口的收发采用中断模式,虽然在一定程度上解放了CPU,但每个字节都要中断一次,在115200波特率下,约8.7uS就要中断一次,CPU仍然很累。直接存储器访问(DMA)方式可以进一步解放CPU,本例采用DAM方式实现每次100字节数据发送与接收。DMA处理发送是最有效的方法,因为程序明确知道有多少数据要发送,直接将数据存放数组的首地址和长度交给DMA即可由DAM连续发完这些数据,如果需要可以设置让DMA发完后产生中断。对于接收,用DMA的问题在于不知道接收多少个数,无法在收到数据后通知CPU。一般采用这样的做法:用DMA收下所有数据放到环形缓冲区里,但不产生中断。这样虽不能通知CPU何时收到了数据,但确可以收下所有数据。每隔一段时间CPU查询该缓冲区,发现有数据就处理。这样虽响应的及时性差些,但一般场合都是可以接受的。
要使用UART的DMA方式,需做下面3件事情:
1、UART5_C2寄存器的发送、接收中断使能,接收使能。
2、UART5_C5寄存器的DMA收和DMA发使能。
3、设置DMAMUX,将相应请求源(中断源)映射到相应DMA通道,并使能相应通道。请求源编号见表3-24。
4、设置DMA控制器,主要是TCD的设置,包括源、目的地址、传输长度、地址递增等。
5、如果需要DMA传输完成产生中断,则要NVICISER寄存器使能DMA对应中断,中断向量表填入中断服务程序入口。
6、想发数据的时候设置UART5_C2的发送使能,会立即因发送数据寄存器空而产生DMA请求。
示例代码用通道0处理发送,完成后产生中断,中断服务程序会再启动发送;通道1处理数据接收,不产生中断。因使用了回环,发送的数据都被自身接收到了,可以看出发送、接收的过程没有CPU的干预,发送完100字节(实际可以很长)才产生一次中断,在此期间MCU可以做各种事情。
下面是完整代码:
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#define GPIO_PIN_MASK 0x3C000000
#define GPIO_PIN(x) ((1<
#include
#include "derivative.h" /* include peripheral declarations */
struct _uart_buf
{
int index;
char buf[100];
} uart_tx,uart_rx;
void MCG_Init()
{
SIM_SCGC6 |= 0x20000000; //SIM_SCGC6: RTC=1
if ((RTC_CR & RTC_CR_OSCE_MASK) == 0u)//Only if the OSCILLATOR is not already enabLED
{
RTC_CR &= ~0x3C00; //RTC_CR: SC2P=0,SC4P=0,SC8P=0,SC16P=0
RTC_CR |= 0x0100; //RTC_CR: OSCE=1
RTC_CR &= ~0x0200; //RTC_CR: CLKO=0
}
/* System cLOCk initialization */
/* SIM_CLKDIV1: OUTDIV1=0,OUTDIV2=1,OUTDIV3=1,OUTDIV4=3,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
SIM_CLKDIV1 = (uint32_t)0x01130000UL; /* Update system prescalers */
/* SIM_SOPT2:PLLFLLSEL=0 */
SIM_SOPT2 &= (uint32_t)~0x00010000UL; /* Select FLL as a clock source for various peripherals */
/* SIM_SOPT1: OSC32KSEL=0 */
SIM_SOPT1 &= (uint32_t)~0x00080000UL; /* System oscillator drives 32 kHzclock for various peripherals */
/* Switch to FEE Mode */
SIM_SOPT2 |= (uint32_t)0x01UL;// SIM_SOPT2: MCGCLKSEL=1 0-System oscillator (OSCCLK), 1-32 kHz RTC oscillator
MCG_C2 = (uint8_t)0x00U; // MCG_C2: ??=0,??=0,RANGE=0,HGO=0,EREFS=0,LP=0,IRCS=0
MCG_C1 = (uint8_t)0x02U; // MCG_C1: CLKS=0,FRDIV=0,IREFS=0,IRCLKEN=1,IREFSTEN=0
MCG_C4 |= 0xE0; //MCG_C4: DMX32=1,DRST_DRS=3
MCG_C5 = 0x00; // MCG_C5: ??=0,PLLCLKEN=0,PLLSTEN=0,PRDIV=0
MCG_C6 = 0x00;// MCG_C6: LOLIE=0,PLLS=0,CME=0,VDIV=0
while((MCG_S & MCG_S_IREFST_MASK) != 0x00U) //Check that the source of the FLL reference clock is the external reference clock.
{
}
while((MCG_S & 0x0CU) != 0x00U) // Wait until output of the FLL is selected
{
}
}
void UART_Init()
{
// SIM_SCGC1: UART5=1
SIM_SCGC1 |= (uint32_t)0x0800UL;
// SIM_SCGC5: PORTE=1
SIM_SCGC5 |= (uint32_t)0x2000UL;
// PORTE_PCR9: ISF=0,MUX=3 做UART
PORTE_PCR9 = (uint32_t)((PORTE_PCR9 & (uint32_t)~0x01000400UL) | (uint32_t)0x0300UL);
// PORTE_PCR8: ISF=0,MUX=3 做UART
PORTE_PCR8 = (uint32_t)((PORTE_PCR8 & (uint32_t)~0x01000400UL) | (uint32_t)0x0300UL);
UART5_C4 = 0x14; //波特率微调
UART5_BDH = (312>>8) & 0x1F;//设波特率9600bps
UART5_BDL = 312&0xFF;
UART5_C2 = (1<<7)|(1<<5)|(1<<2);//允许收、发中断,允许接收
UART5_C5 = (1<<7)|(1<<5);//允许收、发中断产生DMA请求
UART5_C1 |= 1<<7;//使用回环模式
}
void dma0_init()
{
SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
DMAMUX_CHCFG0 = (1<<7) | 13;
SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
DMA_CR = 0;
DMA_TCD0_SADDR = (unsigned long)&uart_tx.buf[0];//DMA源地址
DMA_TCD0_DADDR = (unsigned long)&UART5_D;//DMA目的地址
DMA_TCD0_NBYTES_MLNO = 1;
DMA_TCD0_ATTR = 0;//8位传送,关闭模特性
DMA_TCD0_SOFF = 1;//每次操作完源地址,源地址增加1
DMA_TCD0_DOFF = 0;//每次操作完目标地址,目标地址不增加
DMA_TCD0_SLAST = 0;//DMA完成一次输出之后即major_loop衰减完之后不更改源地址
DMA_TCD0_DLASTSGA = 0;//DMA完成一次输出之后即major_loop衰减完之后不更改目标地址
DMA_TCD0_CITER_ELINKNO = 100;
DMA_TCD0_BITER_ELINKNO = 100;
DMA_TCD0_CSR = 0;
DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
DMA_TCD0_CSR |= DMA_CSR_DREQ_MASK;
NVICISER0 |= 1<<0;//;//使能中断NVICISERn=1<
DMA_ERQ |= (1 << 0);//启动
}
void dma1_init()
{
//SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
DMAMUX_CHCFG1 = (1<<7) | 12;
//SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
//DMA_CR = 0;
DMA_TCD1_SADDR = (unsigned long)&UART5_D;//DMA源地址
DMA_TCD1_DADDR = (unsigned long)&uart_rx.buf[0];//DMA目的地址
DMA_TCD1_NBYTES_MLNO = 1;
DMA_TCD1_ATTR = 0;//8位传送
DMA_TCD1_SOFF = 0;//每次操作完源地址,源地址不增加
DMA_TCD1_DOFF = 1;//每次操作完目标地址,目标地址增加1
DMA_TCD1_SLAST = 0;//DMA完成一次输出之后即major_loop衰减完之后不更改源地址
DMA_TCD1_DLASTSGA = 0;//DMA完成一次输出之后即major_loop衰减完之后不更改目标地址
DMA_TCD1_CITER_ELINKNO = 100;
DMA_TCD1_BITER_ELINKNO = 100;
DMA_TCD1_CSR = 0;
DMA_TCD1_CSR &= ~DMA_CSR_INTMAJOR_MASK;
DMA_TCD1_CSR |= DMA_CSR_DREQ_MASK;
DMA_ERQ |= (1 << 1);//启动
}
int main(void)
{
int i;
MCG_Init();
dma0_init();
dma1_init();
UART_Init();
for(i=0;i<100;i++)
{
uart_tx.buf[i] = i;
uart_rx.buf[i] = 0;
}
uart_tx.index = 1;
uart_rx.index = 0;
printf("Hello (Kinetis) World in 'C' from MK60DX256Z derivative! nr");
UART5_C2 |= 1<<3;
for(;;)
{
}
return 0;
}
void dam0_isr(void)
{
static unsigned char cnt=0;
DMA_INT = 0x1; // clear dma int flag
cnt++;
MEMSet(uart_tx.buf,cnt,100);
DMA_TCD0_SADDR = (unsigned long)&uart_tx.buf[0];//DMA源地址
DMA_ERQ |= (1 << 0);//启动
//与UART接收对应的DMA1未使用中断,在这里也同时对其重设目的地址并启动
DMA_TCD1_DADDR = (unsigned long)&uart_rx.buf[0];
DMA_ERQ |= (1 << 1);//启动
}
将“kinetis_sysinit.c”的“__vect_table”中16号中断“(tIsrFunc)UNASSIGNED_ISR”换成“(tIsrFunc)dam0_isr”