⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.cpp

📁 本程序用systemC仿真UART的功能
💻 CPP
字号:
#include "uart.h"#define UFCON_FIFO  ufcon&0x1#define UFCON_TXFIFO_RESET(ufcon) ufcon&0x4#define UFCON_RXFIFO_RESET(ufcon)  ufcon&0x2#define UTXH_EMPTY  utrstat&0x2#define PARITY  ulcon&0x38#define NO_PARITY 0x8#define ODD_PARITY  0x20#define EVEN_PARITY 0x28#define FORCE1_PARITY  0x30#define FORCE0_PARITY  0x38#define DATA_LEN5    0x00#define DATA_LEN6  0x01#define DATA_LEN7   0x02#define DATA_LEN8    0x03#define UTXH_ADDR    0X20#define STOPBIT      ulcon&0x4#define	STOP1        0X0#define STOP2	     0X4#define DATALENGTH   ulcon&0x3#define TRANSMIT_BUFFER_EMPTY_INTERRUPT  #define TRANSMITTER_EMPTY_INTERRUPT             #define T_FIFO_EMPTY_INTERRUPT        #define RECEIVE_BUFFER_READY_INTERRUPT #define FRAME_ERROR_INTERRUPT #define PARITY_ERROR_INTERRUPT#define OVERRUN_ERROR_INTERRUPTextern char lxhdata[1024];void uart::uartrw_rw(unsigned int address,unsigned int *data, int rw){	int i;	unsigned int addr;	addr=address-0x50000000;	char tmp[1];	for(i=0;i<1;i++)		tmp[i]='b';		if(!rw) { //write		switch(addr)		{			case 0x00:	ulcon=*data; //r/w				switch(DATALENGTH)				{					case	DATA_LEN5:	datalength=5; break;					case	DATA_LEN6:	datalength=6; break;					case	DATA_LEN7:	datalength=7; break;					case	DATA_LEN8:	datalength=8; break;				}					/*		switch(PARITY)				{								case ODD_PARITY:	  oddparity(txshifter); 	 break;					case EVEN_PARITY:  	  evenparity(txshifter);	 break;					case FORCE1_PARITY:	  force1(txshifter);		 break;					case FORCE0_PARITY:	    force0(txshifter);		 break;					default:	break;				}*/						switch(STOPBIT )				{					case STOP1:							//stopbit=1;							frame_length=DATALENGTH+5+3;						break;					case STOP2:								//stopbit=2;						frame_length=DATALENGTH+5+4;						break;				}				switch(ulcon&0x40)				{					case 0:		normal_mode=1; break;					case 0x40:	infra_red_mode=1; break;				}				break;			case   0x04:	ucon=*data;//r/w				break;			case	0x08:	ufcon=*data;//r/w					switch(ufcon&0x2)				{					case 0:		    receive_normal_mode=0;                  break;					case 0x2:		receive_normal_mode=1;					break;				}				switch(ufcon&0x4)				{					case  0:		trans_normal_mode=0;					break;					case  0x4:		trans_normal_mode=1;                     break;				}				break;			case	0x0c:	umcon=*data;                                    break;			case	0x10:	cout<<"utrstat is read_only! write fail!"<<endl;break;			case	0x14:	cout<<"uerstat is read_only! write fail!"<<endl;break;			case	0x18:	cout<<"ufstat is read_only! write fail!"<<endl;break;			case	0x1c:	cout<<"umstat is read_only! write fail!"<<endl;break;				case	0x20:	utxh=*data; 					sdata = utxh&0xff;					tmp[0] =(char)(utxh & 0xff);					utrstat &=~(0x2);					printf("------------------------------------------to send the data:  %s\n",tmp);					printf("\n");					send_to_physical_uart(tmp,1);					utrstat |= 0x2;					/*							if(ufcon&1) {								cout<<"b_buffer[head%16]:"<<utxh<<endl;								t_buffer[head%16]=utxh;//receive data from cpu to t_buffer								TxFifoCount = (ufstat&0xf0)>>4;								TxFifoCount++;								ufstat = ufstat&(~0xf0);								ufstat = ufstat|(TxFifoCount<<4);								cout<<"T_buffer head=   "<<head<<endl;								head=head+1;								utxh=0;													}*/							break;			case	0x24:	cout<<"urxh is read_only! write fail!"<<endl;break;			case	0x28:	ubrdiv=*data; cout<<"ubrdiv "<<ubrdiv<<endl;                               break;		}	}	else if(rw)	{//read					switch(addr)		{			case	0x00:	*data=ulcon;   break;			case    0x04:	*data=ucon;	   break;			case	0x08:	*data=ufcon;   break;			case	0x0c:	*data=umcon;   break;			case	0x10:	*data=utrstat; 				switch(utrstat&0x1)				{	unsigned int r_count;					case 0:		r_count=empty;					case 1:		r_count=full;								}				switch(utrstat&0x2)				{	unsigned int t_count;					case 0:		t_count=not_empty;					case 2:		t_count=empty;				}								break;			case	0x14:	*data=uerstat; break;			case	0x18:	*data=ufstat;  break;			case	0x1c:	*data=umstat;  break;			case    0x20:   cout<<"utxh is write_only,read fail;"<<endl;break;			case	0x24:	*data=urxh; 					break;			case	0x28:	*data=ubrdiv;  break;				}							}	}void uart::receive_action(){	char tmp[1500];	int  num;	int  i;	unsigned int ad;	while(1){		if(check_flag()==1)		{			commit_data_to_uart(tmp,num);						for(i=0;num>0;num--,i++)				tmp[i] =lxhdata[i];						clear_flag();		}		wait(1,SC_US);	}}	void uart::uart_action(unsigned int pc_count){   	unsigned int tmp;	unsigned int TxFifoCount;//	ubrdiv=20;/*	TxFifoCount=(ufstat&0xf0)>>4;	//t_count of the tx_fifo		if((pc_count%ubrdiv)==0) {//		cout<<"shifting============="<<shifting<<endl;	//	if(ufcon&0x1==0){//none tx_fifo			if(shifting){//start				tmp = txshifter&0x1;						uart_txport->txbit(tmp,1);				txshifter>>=1;				tx_count++;				cout<<"tx_count========"<<tx_count<<endl;				cout<<"utxh send the bit is: "<<tmp<<endl;								if(tx_count == frame_length)				{	shifting =0 ;					tx_count=0;					cout<<"stop===========";					utrstat|=0x4;//transmitter empty                                        TRANSMITTER_EMPTY_INTERRUPT;				}			}			else if( (utrstat&0x2)==0){//UTXH NOT EMPTY								txshifter=utxh;				cout<<"send the data is:   "<<txshifter<<endl;				utxh=0;//cleaning the utxh after send a data				utrstat|=0x2;//transmit buffer empty                                 TRANSMIT_BUFFER_EMPTY_INTERRUPT; 								switch(PARITY){				case ODD_PARITY: txshifter=oddparity(txshifter);  break;				case EVEN_PARITY: txshifter=evenparity(txshifter);break;				case FORCE1_PARITY: txshifter=force1(txshifter);  break;				case FORCE0_PARITY: txshifter=force0(txshifter);  break;				default:            break;				}				cout<<"send the check_data is:   "<<txshifter<<endl;				txshifter<<=1;				txshifter=stb(txshifter);				cout<<"utxh send the frame is: "<<txshifter<<endl;				if(frame_length>tx_count){					tmp = txshifter&0x1;					cout<<"tmp"<<tmp<<endl;					uart_txport->txbit(tmp,1);					txshifter>>=1;						tx_count=1;					shifting=1;					cout<<"==========shifting"<<shifting<<endl;				}			}			}		else if(UFCON_FIFO) { //tx_fifo				if(shifting){//start									tmp = txshifter&0x1;							uart_txport->txbit(tmp,1);				txshifter>>=1;				tx_count++;				cout<<"tx_count========"<<tx_count<<endl;				cout<<"txshifter send the bit is: "<<tmp<<endl;										if(tx_count == frame_length)				{	shifting =0 ;					tx_count=0;					cout<<"stop===========";					utrstat|=0x4;//transmitter empty                                        TRANSMITTER_EMPTY_INTERRUPT;//TX_SHIFTER EMPTY				}			}				else if( ((ufcon&0x1)==0x1)&&(shifting==0)) {				cout<<"rear==========="<<rear<<endl;				cout<<"t_buffer[rear%16]:  "<<t_buffer[rear%16]<<endl;				txshifter = t_buffer[rear%16];//send data from t_buffer to txshifter				cout<<"t_buffer transmit data is: "<<t_buffer[rear%16]<<endl;				rear=rear+1;				TxFifoCount--;				if(Ttrigger_level==0)                                        T_FIFO_EMPTY_INTERRUPT;				ufstat = ufstat&(~0xf0);				ufstat = ufstat|(TxFifoCount<<4);													    switch(PARITY){					case ODD_PARITY: txshifter=oddparity(txshifter);break;					case EVEN_PARITY: txshifter=evenparity(txshifter);break;					case FORCE1_PARITY: txshifter=force1(txshifter);break;					case FORCE0_PARITY: txshifter=force0(txshifter);break;				default:     break;				}				txshifter<<=1;//start bit				txshifter = stb(txshifter);//stop bit				cout<<"utxh send the frame is: "<<txshifter<<endl;									if(frame_length > tx_count){//bit transmit					tmp = txshifter&0x1;					uart_txport->txbit(tmp,1);//start tx send bit					txshifter>>=1;					tx_count=1;					cout<<"send the start bit============="<<tmp<<endl;					shifting=1;					cout<<"==========shifting"<<shifting<<endl;				}			}		}	}*/}	/*		unsigned int  uart::oddparity(unsigned int b){	int i;	int counter;	unsigned int a,xx;	int tmp;	a=txshifter;	b = a;	tmp =(ulcon&0x3)+5;	for(i=0,counter=0;i<tmp;i++){		if(a&1)			counter++;		a>>=1;	}	xx=1<<tmp;		if(counter%2)//奇数个1,校验位为0		b=b&(~xx);	else		b|=xx;//偶数个1,校验位为1	return b;	}unsigned int  uart:: evenparity(unsigned int b){	int i;	int counter;	unsigned int a,xx;	int tmp;	//txshifter=5;		a = b;		tmp =(ulcon&0x3)+5;    for(i=0,counter=0;i<tmp;i++){		if(a&1)			counter++;		a>>=1;	}	xx=1<<tmp;		if(counter%2==0)//偶数个1,校验位为0		b=b&(~xx);	else		b|=xx;//奇数个1,校验位为1	return b;}unsigned int uart:: force0(unsigned int b){		int i;	int counter;	unsigned int a,xx;	int tmp;	a=txshifter;	b = a;	tmp =ulcon&0x3+5;	for(i=0,counter=0;i<tmp;i++){		if(a&1)			counter++;		a>>=1;	}	xx=1<<tmp;	b|=xx;//校验位为1	return b;}unsigned int uart::force1(unsigned int b){	int i;	int counter;	unsigned int a,xx;	int tmp;	a=txshifter;	b = a;	tmp =ulcon&0x3+5;	for(i=0,counter=0;i<tmp;i++){		if(a&1)			counter++;		a>>=1;	}	xx=1<<tmp;	b=b&(~xx);//校验位为0	return b;}unsigned int uart::stb(unsigned int b){	unsigned int c=ulcon&0x4;	txshifter=b;	//how to implement frame error????	if(c==0x4)//stop bit has:2	{			unsigned int tmp,i,xx;		unsigned int f=ulcon&0x3;		tmp =f+5+2;		for(i=0;i<tmp;i++)		{			xx=3<<tmp;            txshifter|=xx;			b=txshifter;		}	}	else if(c==0x0)	{			unsigned int tmp,i,xx;		tmp =(ulcon&0x3)+5+1+1;		for(i=0;i<tmp;i++)		{				xx=1<<tmp;		txshifter|=xx;b=txshifter;		} 		cout<<"===============data:"<<b<<endl;	}		return b;}*/unsigned int uart::txbit(bool tb, int start){/*	frame_length=8;//????????????????how to define   frame_length while receiving	unsigned int p;	unsigned int rxstart;	unsigned int rxbit;	unsigned int xx,t;	p=(frame_length+1);	if(start)	{		rxstart=1;		rxbit=tb;		}   	 else 		rxstart=0;//	cout<<"rx  start------------------ "<<rxstart<<endl;//	cout<<"RXSHIFTER REVEIVE THE BIT IS: "<<rxbit<<endl;//	if(ufcon&0x1==0){//none_fifo		if(rxstart){//RECEIVER							if(rx_count!=0)			{//start receive  bit				unsigned int t;					t=rx_count-1;				rxshifter|=(rxbit<<t);				cout<<"rxshifter receive the frame is: "<<rxshifter<<endl;				rx_count++;					}			if(rx_count== p)			{						rx_count =1;				cout<<"receive is over----------"<<endl;				switch(PARITY){				case ODD_PARITY: rxshifter=oddparity_rx(rxshifter);break;				case EVEN_PARITY: rxshifter=evenparity_rx(rxshifter);break;	//			case force1_parity: rxshifter=force1(rxshifter);break;	//			case force0_parity: rxshifter=force0(rxshifter);break;				default:			break;				}										t=(ulcon&0x3)+5;				xx=0x3<<t;				rxshifter&=(~xx);//dispose the parity bit				urxh=rxshifter;				cout<<" urxh receive the data is: "<<urxh<<endl;				if(utrstat&0x1) {//receive a data but continue to receive another data occur the overrun error                                        uerstat |=1;                                        OVERRUN_ERROR_INTERRUPT;					cout<<"OVER_ERROR_INTERRUPT=============="<<endl;                                } else {                                        utrstat|=0x1;//urxh receive a data                                        RECEIVE_BUFFER_READY_INTERRUPT;                                }				rxshifter=0;//cleaning the urxh after receive a data			}		}			}	else if(ufcon&0x1==0x1){//rx_fifo		  if((ufstat&0xf)<0xf){				  if(rxstart){//RECEIVER			  			  if(rx_count!=0)			  {//start receive  bit				  unsigned int t;					  t=rx_count-1;				  rxshifter|=(rxbit<<t);				  cout<<"rxshifter receive the frame is: "<<rxshifter<<endl;				  rx_count++;					  }			  if(rx_count== p)			  {					  rx_count =1;				  cout<<"receive is over----------"<<endl;				  switch(PARITY){						case ODD_PARITY: rxshifter=oddparity_rx(rxshifter);break;						case EVEN_PARITY: rxshifter=evenparity_rx(rxshifter);break;					//	case force1_parity: rxshifter=force1(rxshifter);break;					//	case force0_parity: rxshifter=force0(rxshifter);break;						default:			break;				  }						  				  				  t=(ulcon&0x3)+5;				  xx=0x3<<t;				  rxshifter&=(~xx);//dispose the parity bit				  urxh=rxshifter;				  cout<<" urxh receive the data is: "<<urxh<<endl;				  rxshifter=0;//cleaning the urxh after receive a data			 					  				  r_buffer[front%16]=urxh;//from urxh to r_buffer		//		  r_errorb[front%16]=uerstatus_b;???????????error fifo //how to Implement rx_errfifo ??????				  cout<<"r_buffer front is:  "<<front<<endl;		   		  cout<<"r_fifo data is:  "<<r_buffer[front%16]<<endl;				  front=front+1;				  RxFifoCount=(ufstat&0xf);				  RxFifoCount++;				  if(ufstat&0x10f)//rx_fifo full                                  {                                             uerstat|=0x1;                                        OVERRUN_ERROR_INTERRUPT;//new data overrid the old data ,occor overrun error					cout<<"OVER_ERROR_INTERRUPT=============="<<endl;                                  }                                  if(RxFifoCount>Rtrigger_level)                                           RECEIVE_BUFFER_READY_INTERRUPT;                                   ufstat = ufstat&(~0xf);                                  ufstat = ufstat|RxFifoCount;			}										return(rxbit);		} 	  }	}*/}/*unsigned int uart::evenparity_rx(unsigned int b)/////////????????????????{	int i;	int counter;	unsigned int a,xx;	int tmp;	a=b;	tmp=DATALENGTH+1;	datalength=DATALENGTH+5;	a>>=1;//remove start bit	if(STOPBIT ==STOP1)	{		xx=(0x3<<tmp);		a&=(~xx);//remove stop bit			for(i=0,counter=0;i<tmp;i++){			if(a&1)				counter++;			a>>=1;		}		if(count%2==0)			cout<<"receive the data is successful!"<<endl;		else                        PARITY_ERROR_INTERRUPT;	}	else if(STOPBIT==STOP2)	{		for(i=0,counter=0;i<tmp;i++){						xx=(0x7<<tmp);			a&=(~xx);			cout<<"#################  a"<<a<<endl;			if(a&1)				counter++;			a>>=1;		}		if(counter%2==0)			cout<<"success!"<<endl;		else                        PARITY_ERROR_INTERRUPT;	}		return a;	   }unsigned int uart::oddparity_rx(unsigned int rxshifter){	int i;	int counter;	unsigned int a,b,c,d,frame,xx;	frame=0;	int tmp;	a=rxshifter;	b = a;	tmp=(ulcon&0x3)+1;	d=tmp+6;	c=tmp+7;	if(frame_length==d)	{				for(i=0,counter=0;i<tmp;i++){						xx=3<<tmp;			rxshifter&=(~xx);			a=rxshifter>>1;			if(a&1)				counter++;		}			}	else if(c==frame_length)	{		for(i=0,counter=0;i<tmp;i++){						xx=7<<tmp;			rxshifter&=(~xx);			a=rxshifter>>1;			if(a&1)				counter++;		}			}	if(counter%2==1)			cout<<"success!"<<endl;	else                        PARITY_ERROR_INTERRUPT;	return rxshifter;}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -