一、IO配置
以SCIA为例:使用的是GPIO35--SCITXDA GPIOA36--SCIRXDA
使用寄存器:
GPBPUD :设置上拉 GPIO32-GPIO63 对应位0 使能上拉
GPBQSEL1:
GPBMUX1:IO模式选择
代码如下:
GpioCtrlRegs.GPBPUD.bit.GPIO36 = 0; // Enable pull-up for GPIO28 (SCIRXDA)//使能内部上拉
GpioCtrlRegs.GPBPUD.bit.GPIO35 = 0; // Enable pull-up for GPIO29 (SCITXDA)//使能内部上拉
GpioCtrlRegs.GPBQSEL1.bit.GPIO36 = 3; // Asynch input GPIO28 (SCIRXDA) //设置为异步输入模式
GpioCtrlRegs.GPBMUX1.bit.GPIO36 = 1; // Configure GPIO28 for SCIRXDA operation //配置为外设模式
GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 1; // Configure GPIO29 for SCITXDA operation//配置为外设模式
设置FIFO寄存器:
void scia_fifo_init()
{
SciaRegs.SCIFFTX.all=0xE040;//使能FIFO;清除发送中断标志位;禁止FIFO发送中断;
//发送中断级别定义为0;
SciaRegs.SCIFFRX.all=0x204f;//清除FIFO溢出标志位;清除溢出接受中断标志位;禁止
//FF接受中断;接受中断级别为16;
SciaRegs.SCIFFCT.all=0x0;//SCITXBUF到移位寄存器传送不延迟
}
设置SCI相关寄存器: 寄存器描述见http://www.ti.com/lit/ug/sprufz5a/sprufz5a.pdf
void scia_echoback_init()
{
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
SciaRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SciaRegs.SCICTL1.all =0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
SciaRegs.SCICTL2.all =0x0003;
SciaRegs.SCICTL2.bit.TXINTENA =1;//开启接收中断
SciaRegs.SCICTL2.bit.RXBKINTENA =1;//开启发送中断
#if (CPU_FRQ_150MHZ)
SciaRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 37.5MHz.
SciaRegs.SCILBAUD =0x00E7;
#endif
#if (CPU_FRQ_100MHZ)
SciaRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 20MHz.
SciaRegs.SCILBAUD =0x0044;
#endif
SciaRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
}
波特率计算方式:
发生一个字节:
void scia_xmit(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
SciaRegs.SCITXBUF=a;
}
使用SICA 发送,串口中断接收:
配置参数如下:
static void Init_SCI_A(Uint32 Baud)
{
//Init IO
Uint16 BRR=0;
EALLOW;
GpioCtrlRegs.GPBPUD.bit.GPIO36 = 0; // Enable pull-up for GPIO28 (SCIRXDA)
GpioCtrlRegs.GPBPUD.bit.GPIO35 = 0; // Enable pull-up for GPIO29 (SCITXDA)
GpioCtrlRegs.GPBQSEL1.bit.GPIO36 = 3; // Asynch input GPIO28 (SCIRXDA)
GpioCtrlRegs.GPBMUX1.bit.GPIO36 = 1; // Configure GPIO28 for SCIRXDA operation
GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 1; // Configure GPIO29 for SCITXDA operation
EDIS;
//set RX interrput function
EALLOW;
PieVectTable.SCIRXINTA = &SciaRxIsr;
EDIS;
//set SCIA register
SciaRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SciaRegs.SCICTL1.all =0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
SciaRegs.SCICTL2.bit.TXINTENA =0;//close TX interrput
SciaRegs.SCICTL2.bit.RXBKINTENA =1;
//set Baud BRR= LSPCLK/(Baud*8)-1
BRR = SCI_LSPCLK/(Baud*8)-1;
SciaRegs.SCIHBAUD = (BRR>>8); // 9600 baud @LSPCLK = 37.5MHz.
SciaRegs.SCILBAUD = (BRR&0x00ff);
// SciaRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 37.5MHz.
// SciaRegs.SCILBAUD =0x00E7;
SciaRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
EALLOW;
//set SCI interrput Group
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER9.bit.INTx1=1; // PIE Group 9, int1 enable SCIA_RX
//PieCtrlRegs.PIEIER9.bit.INTx2=1; // PIE Group 9, INT2 enable SCIA_TX
EDIS;
}
发送数据代码:
//send one byte data
static void scia_xmit(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
SciaRegs.SCITXBUF=a;
}
//send string
void scia_msg(char * msg)
{
int i;
i = 0;
while(msg[i] != '\0')
{
scia_xmit(msg[i]);
i++;
}
}