在做异步串口UART实验,对厂家提供的例程没看明白,例程中没有任何注释,想了几天都没想明白,代码和原理是如何对应的?例程
/*********************************************************************
** Module Name: UART **
** Author: CTH **
** Version: 1.0 **
** CreateDate: 2008-8-10 **
** Description: **
** Remark: **
** Revision History: 2008-8-10 **
** Web: http://www.study-kit.com **
**********************************************************************/
/*********************************************************************
** start:2014-3-15 pm 2:00 ******************
** end :2014-3- ******************
*********************************************************************/
#include <csl.h>
#include <csl_pll.h>
#include <csl_emif.h>
#include <csl_chip.h>
#include <stdio.h>
#define UART_BASE_ADDR 0x200008 // UART寄存器地址
/*异步通信接口芯片INS 8520 内部寄存器*/
#define RHR *((int *)(UART_BASE_ADDR+0))
#define THR *((int *)(UART_BASE_ADDR+0))
#define IER *((int *)(UART_BASE_ADDR+1))
#define ISR *((int *)(UART_BASE_ADDR+2))
#define FCR *((int *)(UART_BASE_ADDR+2))
#define LCR *((int *)(UART_BASE_ADDR+3))
#define MCR *((int *)(UART_BASE_ADDR+4))
#define LSR *((int *)(UART_BASE_ADDR+5))
#define MSR *((int *)(UART_BASE_ADDR+6))
#define SCR *((int *)(UART_BASE_ADDR+7))
#define DLL *((int *)(UART_BASE_ADDR+0))
#define DLM *((int *)(UART_BASE_ADDR+1))
#define DLL_DATA 0x48//定义UART分频系数,在11.05926M时,波特率为9600BPS
#define DLM_DATA 0x00
CSLBool b;
Uint16 eventId0;
int old_intm;
interrupt void int0(void); //外部中断0
unsigned char str1[]={"study-kit.com "};
unsigned char str2[]={"TEL:010-82623343"};
extern void VECSTART(void);
void Delay(unsigned int nDelay);
/*锁相环的设置*/
PLL_Config myConfig = {
0, //IAI: the PLL locks using the same process that was underway
//before the idle mode was entered
1, //IOB: If the PLL indicates a break in the phase lock,
//it switches to its bypass mode and restarts the PLL phase-locking
//sequence
12, //PLL multiply value; multiply 12 times
0 //Divide by 2 PLL divide value; it can be either PLL divide value
//(when PLL is enabled), or Bypass-mode divide value
//(PLL in bypass mode, if PLL multiply value is set to 1)
};
/*SDRAM的EMIF设置*/
EMIF_Config emiffig = {
0x221, //EGCR : the MEMFREQ = 00,the clock for the memory is equal to cpu frequence
// the WPE = 0 ,forbiden the writing posting when we debug the EMIF
// the MEMCEN = 1,the memory clock is reflected on the CLKMEM pin
// the NOHOLD = 1,HOLD requests are not recognized by the EMIF
0xFFFF, //EMI_RST: any write to this register resets the EMIF state machine
0x1fff, //CE3_1: CE0 space control register 1
0x00ff, //CE3_2: CE0 space control register 2
0x00ff, //CE3_3: CE0 space control register 3
0x1fff, //CE1_1: CE0 space control register 1
// Asynchronous, 16Bit
0x00ff, //CE1_2: CE0 space control register 2
0x00ff, //CE1_3: CE0 space control register 3
0x1FFF, //CE2_1: CE0 space control register 1
// Asynchronous, 16Bit
0xFFFF, //CE2_2: CE0 space control register 2
0x00FF, //CE2_3: CE0 space control register 3
0x1fff, //CE3_1: CE0 space control register 1
0x00ff, //CE3_2: CE0 space control register 2
0x00ff, //CE3_3: CE0 space control register 3
0x2911, //SDC1: SDRAM control register 1
// TRC = 8
// SDSIZE = 0;SDWID = 0
// RFEN = 1
// TRCD = 2
// TRP = 2
0x0410, //SDPER : SDRAM period register
// 7ns *4096
0x07FF, //SDINIT: SDRAM initialization register
// any write to this register to init the all CE spaces,
// do it after hardware reset or power up the C55x device
0x0131 //SDC2: SDRAM control register 2
// SDACC = 0;
// TMRD = 01;
// TRAS = 0101;
// TACTV2ACTV = 0001;
};
/*中断配置*/
void INTconfig()
{
/* Temporarily disable all maskable interrupts */
IRQ_setVecs((Uint32)(&VECSTART));
/* Temporarily disable all maskable interrupts */
old_intm = IRQ_globalDisable();
/* Get Event Id associated with External INT1(8019), for use with */
eventId0 = IRQ_EVT_INT0;
/* Clear any pending INT0 interrupts */
IRQ_clear(eventId0);
/* Place interrupt service routine address at */
/* associated vector location */
IRQ_plug(eventId0,&int0);
/* Enable INT0(8019) interrupt */
IRQ_enable(eventId0);
/* Enable all maskable interrupts */
IRQ_globalEnable();
}
void delay(unsigned int d_time)
{
while(d_time--);
}
main()
{
unsigned int temp,data,i,k;
/*初始化CSL库*/
CSL_init();
/*EMIF为全EMIF接口*/
CHIP_RSET(XBSR,0x0a01);
/*设置系统的运行速度为144MHz*/
PLL_config(&myConfig);
/*初始化DSP的EMIF*/
EMIF_config(&emiffig);
INTconfig();
/*UART初始化*/
LCR = 0x80; //设置波提率 通信线路控制寄存器
temp=LCR; //通信线路控制寄存器
DLL = DLL_DATA; //写除数低8位
DLM = DLM_DATA; //写除数高8位
temp=DLL_DATA;
LCR = 0x00; //设置通信的数据格式,D7=0
LCR = 0x03; //8位数据,一个停止位
FCR = 0x00; //FIFO控制寄存器,在非FIFO模式
MCR = 0x08; //调制解调控制寄存器,OUT2=1,中断信号可通过系统总线送给8520中断控制寄存器
IER = 0x00; //中断使能寄存器禁止,4种中断都禁止
while(1)
{
/*发送字符串*/
/*for(k=0;k<16;k++)
{
THR=str1[k];
delay(200);
}
for(k=0;k<16;k++)
{
THR=str2[k];
delay(200);
}
delay(5000); */
temp=LSR; //通信线路状态寄存器
data=ISR; //中断识别寄存器,判别中断的类型 ,但是从下面的代码中,不明白它的作用体现在哪条语句?
THR =0x55; //发送保持寄存器,要发送的数据是U,对应16进制ASCII
if(temp&0x01)
{
data=RHR; //发送的字符已到达接收缓冲寄存器
THR=data;
temp=0;
}
delay(5000);
}
}
void Delay(unsigned int nDelay)
{
int ii,jj,kk=0;
for ( ii=0;ii<nDelay;ii++ )
{
for ( jj=0;jj<1024;jj++ )
{
kk++;
}
}
}
//External INT0(EXINT)中断处理函数
interrupt void int0()
{
unsigned int temp,data,i;
temp=LSR;
data=ISR;
if(temp&0x01)//LSR的最低位不为0,表示数据已经准备好接收
{
data=RHR; //接收保持寄存器的值送到发送保持寄存器
THR=data;
temp=0;
}
}
/******************************************************************************\
* End of UART.c
\******************************************************************************/
不明白的地方有:
1)IER = 0x00; 中断都禁止了,又怎么产生中断?
2)while中的temp应该是只能检测到LSR的第0位DR的状态,为1表示接受数据准备好,当中断使能时,这时候是不是应该进入中断的?这里if语句的功能是什么?
3)中断程序中,ISR的作用体现在哪里,似乎没用到?while程序和中断程序为什么是一样的代码,中断中应该是字符的写入或者从接受缓冲寄存器中读数据,那while中语句又是什么意思?
感觉原理明白了,但是结合到代码,又有好多疑问,没想通。 |