这儿给个使用串口收发中止的完成.
首先是头文件:
- /*sci_buffered.h*/
-
#ifndef_SCI_BUFFERED_H_
- #define_SCI_BUFFERED_H_
#defineSCI_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
- #defineSCI_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
#defineSCI_NO_ERR0/*Functioncallwassuccessful*/
- #defineSCI_BAD_CH1/*Invalidcommunicationsportchannel*/
- #defineSCI_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
- #defineSCI_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
- #defineSCI_TX_EMPTY4/*IftheTxbufferisempty.*/
- /**
- *Toobtainacharacterfromthecommunicationschannel.
- *@paramport,portcanbeSCI0/SCI1
- *@paramerr,isapointertowhereanerrorcodewillbeplaced:
- **errissettoSCI_NO_ERRifacharacterisavailable
- **errissettoSCI_RX_EMPTYiftheRxbufferisempty
- **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
- *@returnThereceivedchariferr==SCI_NO_ERR
- */
- unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err);
/**
- *Tosendacharacteronthecommunicationschannel.
- *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
- *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
- *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
- *COMM_TX_FULLifthebufferwasfull
- *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
- */
- unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc);
/**
- *Toinitializethecommunicationsmodule.
- *Youmustcallthisfunctionbeforecallinganyotherfunctions.
- */
- voidSCIBufferInit(void);
/**
- *Toseeifanycharacterisavailablefromthecommunicationschannel.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfatleastonecharacterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport);
/**
- *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
- *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfthebufferisfull,thefunctionreturnsTRUE
- *otherwise,thefunctionreturnsFALSE.
- */
- unsignedcharSCIBufferIsFull(unsignedcharport);
#endif
然后 是 c 文件.
-
/**
- *SCI(SerialCommunicationInterface)BufferedSerialI/O
- *@filesci_buffered.c
- *@authorLiYuan
- *@platformmc9s12XX
- *@date2012-7-22
- *@version1.0.1
- */
- #include”derivative.h”/*derivative-specificdefinitions*/
- #include
- #include”sci.h”
- #include”sci_buffered.h”
//#defineOS_ENTER_CRITICAL()_asm(“pshc;sei”)
- //#defineOS_EXIT_CRITICAL()_asm(“pulc”)
#defineOS_ENTER_CRITICAL()_asm(“tpa;sei;staacpu_sr”)
- #defineOS_EXIT_CRITICAL()_asm(“ldaacpu_sr;tap”)
/**
- *DATATYPES
- */
- typedefstruct{
- shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
- unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufRx[SCI_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
- shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
- unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufTx[SCI_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
- }SCI_RING_BUF;
/**
- *GLOBALVARIABLES
- */
SCI_RING_BUFSCI0Buf;
- SCI_RING_BUFSCI1Buf;
- /**
- *Toobtainacharacterfromthecommunicationschannel.
- *@paramport,portcanbeSCI0/SCI1
- *@paramerr,isapointertowhereanerrorcodewillbeplaced:
- **errissettoSCI_NO_ERRifacharacterisavailable
- **errissettoSCI_RX_EMPTYiftheRxbufferisempty
- **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
- *@returnThereceivedchariferr==SCI_NO_ERR
- */
- unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err)
- {
- unsignedcharcpu_sr;
- unsignedcharc;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
- {
- pbuf->RingBufRxCtr–;/*No,decrementcharactercount*/
- c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
- {
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
- }
- OS_EXIT_CRITICAL();
- *err=SCI_NO_ERR;
- return(c);
- }
- else
- {
- OS_EXIT_CRITICAL();
- *err=SCI_RX_EMPTY;
- c=0;/*Bufferisempty,return0*/
- return(c);
- }
- }
- /**
- *Tosendacharacteronthecommunicationschannel.
- *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
- *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
- *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
- *COMM_TX_FULLifthebufferwasfull
- *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
- */
- unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc)
- {
- unsignedcharcpu_sr;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(SCI_BAD_CH);
- }
OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- {
- pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
- {
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
- }
- if(pbuf->RingBufTxCtr==1)
- {/*Seeifthisisthefirstcharacter*/
- SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
- OS_EXIT_CRITICAL();
- }
- else
- {
- OS_EXIT_CRITICAL();
- }
- return(SCI_NO_ERR);
- }
- else
- {
- OS_EXIT_CRITICAL();
- return(SCI_TX_FULL);
- }
- }
/**
- *Toinitializethecommunicationsmodule.
- *Youmustcallthisfunctionbeforecallinganyotherfunctions.
- */
- voidSCIBufferInit(void)
- {
- SCI_RING_BUF*pbuf;
- /*InitializetheringbufferforSCI0*/
- pbuf=&SCI0Buf;
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- /*InitializetheringbufferforSCI1*/
- pbuf=&SCI1Buf;
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- }
- /**
- *Toseeifanycharacterisavailablefromthecommunicationschannel.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfatleastonecharacterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport)
- {
- unsignedcharcpu_sr;
- unsignedcharempty;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(1);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
- {
- empty=0;/*BufferisNOTempty*/
- }
- else
- {
- empty=1;/*Bufferisempty*/
- }
- OS_EXIT_CRITICAL();
- return(empty);
- }
- /**
- *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
- *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfthebufferisfull,thefunctionreturnsTRUE
- *otherwise,thefunctionreturnsFALSE.
- */
- unsignedcharSCIBufferIsFull(unsignedcharport)
- {
- unsignedcharcpu_sr;
- charfull;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(1);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- {/*Seeifbufferisfull*/
- full=0;/*BufferisNOTfull*/
- }
- else
- {
- full=1;/*Bufferisfull*/
- }
- OS_EXIT_CRITICAL();
- return(full);
- }
- //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
- staticvoidSCIPutRxChar(unsignedcharport,unsignedcharc)
- {
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return;
- }
- if(pbuf->RingBufRxCtr
- {
- pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
- {/*WrapINpointer*/
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- }
- }
- }
- //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
- //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
- //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
- //lastcharactertosend.
- staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
- {
- unsignedcharc;
- SCI_RING_BUF*pbuf;
switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- /*Seeifbufferisempty*/
- if(pbuf->RingBufTxCtr>0)
- {
- pbuf->RingBufTxCtr–;/*No,decrementcharactercount*/
- c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
- {
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];/*WrapOUTpointer*/
- }
- *err=SCI_NO_ERR;
- return(c);/*Charactersarestillavailable*/
- }else{
- *err=SCI_TX_EMPTY;
- return(0);/*Bufferisempty*/
- }
- }
- interruptVectorNumber_Vsci0voidSCI0_ISR(void)
- {
- charstatus;
- unsignedchardata;
- unsignedcharerr;
- status=SCI0SR1;
- if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=SCI0DRL;
- }
- elseif(status&0x20)//ReceiveDataRegFullFlag
- {
- data=SCI0DRL;
- SCIPutRxChar(SCI0,data);//Insertreceivedcharacterintobuffer
- }
- elseif(status&0x80)
- {
- data=SCIGetTxChar(SCI0,&err);//Getnextcharactertosend.
- if(err==SCI_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- SCI0CR2_SCTIE=0;
- SCI0CR2_TCIE=0;
- }
- else
- {
- SCI0DRL=data;//Yes,Sendcharacter
- }
- }
- }
interruptVectorNumber_Vsci1voidSCI1_ISR(void)
- {
- charstatus;
- unsignedchardata;
- unsignedcharerr;
- status=SCI1SR1;
- if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=SCI1DRL;
- }
- elseif(status&0x20)//ReceiveDataRegFullFlag
- {
- data=SCI1DRL;
- SCIPutRxChar(SCI1,data);//Insertreceivedcharacterintobuffer
- }
- elseif(status&0x80)
- {
- data=SCIGetTxChar(SCI1,&err);//Getnextcharactertosend.
- if(err==SCI_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- SCI1CR2_SCTIE=0;
- SCI1CR2_TCIE=0;
- }
- else
- {
- SCI1DRL=data;//Yes,Sendcharacter
- }
- }
- }
最终给个比如阐明用法:
- #include
/*commondefinesandmacros*/ - #include”derivative.h”/*derivative-specificdefinitions*/
- #include”sci.h”
- #include”sci_buffered.h”
-
voidmain(void)
- {
- /*putyourowncodehere*/
- unsignedcharC,err;
- longl=0x1234L;
- inti=0x5678;
- CRGInit();
- SCIInit(SCI0);
- SCIInit(SCI1);
- SCIBufferInit();
- SCISetIEBit(SCI0,SCI_RIE);
- SCISetIEBit(SCI1,SCI_RIE);
EnableInterrupts;
- SCIPutShortBigEndian(SCI1,i);
- SCIPutShortLittleEndian(SCI1,i);
- SCIPutLongBigEndian(SCI1,l);
- SCIPutLongLittleEndian(SCI1,l);
- for(;;)
- {
- _FEED_COP();/*feedsthedog*/
- C=SCIGetCharB(SCI1,&err);
- if(err==SCI_NO_ERR)
- {
- SCIPutCharB(SCI1,C);
- }
- }/*loopforever*/
- /*pleasemakesurethatyouneverleavemain*/
- }
- 比如很简单,就不多解说了。
下一篇介绍在实时操作系统 uC/OS-II 上完成串口驱动。