Freescale 9S12 系列单片机应用笔记(SCI)3
时间:11-20
来源:互联网
点击:
- pbuf->RingBufTxInPtr=&pbuf->pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
- }
- if(pbuf->RingBufTxCtr==1)/*Seeifthisisthefirstcharacter*/
- {
- SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
- }
- OS_EXIT_CRITICAL();
- return(SCI_NO_ERR);
- }
/**
- *Toinitializethecommunicationsmodule.
- *Youmustcallthisfunctionbeforecallinganyotherfunctions.
- */
- voidSCIBufferInit(void)
- {
- SCI_RING_BUF*pbuf;
pbuf=&SCI0Buf;/*InitializetheringbufferforSCI0*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(SCI_TX_BUF_SIZE);
pbuf=&SCI1Buf;/*InitializetheringbufferforSCI1*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(SCI_TX_BUF_SIZE);
- }
/**
- *Toseeifanycharacterisavailablefromthecommunicationschannel.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfatleastonecharacterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport)
- {
#ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
- OS_CPU_SRcpu_sr=0u;
- #endif
- unsignedcharempty;
- SCI_RING_BUF*pbuf;
- switch(port)
- {/*Obtainpointertocommunicationschannel*/
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
- default:
- return(0xff);
- break;
- }
- 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)
- {
- #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
- OS_CPU_SRcpu_sr=0u;
- #endif
- charfull;
- SCI_RING_BUF*pbuf;
- switch(port)
- {/*Obtainpointertocommunicationschannel*/
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(255);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- full=0;/*BufferisNOTfull*/
- }else{
- full=1;/*Bufferisfull*/
- }
- OS_EXIT_CRITICAL();
- return(full);
- }
- //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
- staticvoidSCIPutRxChar(unsignedcharport,unsignedcharc)
- {
SCI_RING_BUF*pbuf;
switch(port)
- {/*Obtainpointertocommunicationschannel*/
- 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];
- }
- (void)OSSemPost(pbuf->RingBufRxSem);/*Indicatethatcharacterwasreceived*/
- }
- }
- //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
- //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
- //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
- //lastcharactertosend.
- staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
- {
- unsignedcharc;
- SCI_RING_BUF*pbuf;
switch(port)
- {/*Obtainpointertocommunicationschannel*/
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- if(pbuf->RingBufTxCtr>0){/*Seeifbufferisempty*/
- pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
- c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufTxOutPtr==&pbuf->if(pbuf->RingBufTxOutPtr==&p