Freescale 9S12 系列单片机应用笔记(SCI)3
时间:11-20
来源:互联网
点击:
- echaracterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport);
- /**
- *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
- *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfthebufferisfull,thefunctionreturnsTRUE
- *otherwise,thefunctionreturnsFALSE.
- */
- unsignedcharSCIBufferIsFull(unsignedcharport);
#endif
/**
- *SCI(SerialCommunicationInterface)BufferedSerialI/O
- *@filesci_ucos.c
- *@authorLiYuan
- *@platformmc9s12XX
- *@date2012-7-22
- *@version1.0.1
- */
- #include"derivative.h"/*derivative-specificdefinitions*/
- #include
- #include"includes.H"
- #include"sci.h"
- #include"sci_rtos.h"
/**
- *GLOBALVARIABLES
- */
- SCI_RING_BUFSCI0Buf;
- SCI_RING_BUFSCI1Buf;
- /**
- *Toobtainacharacterfromthecommunicationschannel.
- *@paramport,portcanbeSCI0/SCI1
- *@paramto,istheamountoftime(inclockticks)thatthecallingfunctioniswillingto
- *waitforacharactertoarrive.Ifyouspecifyatimeoutof0,thefunctionwill
- *waitforeverforacharactertoarrive.
- *@paramerr,isapointertowhereanerrorcodewillbeplaced:
- **errissettoSCI_NO_ERRifacharacterhasbeenreceived
- **errissettoSCI_RX_TIMEOUTifatimeoutoccurred
- **errissettoSCI_BAD_CHifyouspecifyaninvalidchannelnumber
- *@returnThecharacterinthebuffer(orNULifatimeoutoccurred)
- */
- unsignedcharSCIGetCharB(unsignedcharport,unsignedshortto,INT8U*err)
- {
- #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
- OS_CPU_SRcpu_sr=0u;
- #endif
- unsignedcharc;
- unsignedcharoserr;
- SCI_RING_BUF*pbuf;
switch(port)
- {/*Obtainpointertocommunicationschannel*/
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
- if(oserr==OS_TIMEOUT)
- {/*Seeifcharactersreceivedwithintimeout*/
- *err=SCI_RX_TIMEOUT;/*No,returnerrorcode*/
- return(NUL);
- }
- else
- {
- OS_ENTER_CRITICAL();
- pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
- c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE]){/*WrapOUTpointer*/
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- }
- OS_EXIT_CRITICAL();
- *err=SCI_NO_ERR;
- return(c);
- }
- }
/**
- *Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
- *channel.Thefunctionwillwaitforthebuffertoemptyoutifthebufferisfull.
- *Thefunctionreturnstoyourapplicationifthebufferdoesntemptywithinthespecified
- *timeout.Atimeoutvalueof0meansthatthecallingfunctionwillwaitforeverforthe
- *buffertoemptyout.ThecharactertosendisfirstinsertedintotheTxbufferandwill
- *besentbytheTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISR
- *willbeenabled.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@paramcisthecharactertosend.
- *@paramtoisthetimeout(inclockticks)towaitincasethebufferisfull.Ifyou
- *specifyatimeoutof0,thefunctionwillwaitforeverforthebuffertoempty.
- *@returnSCI_NO_ERRifthecharacterwasplacedintheTxbuffer
- *SCI_TX_TIMEOUTifthebufferdidntemptywithinthespecifiedtimeoutperiod
- *SCI_BAD_CHifyouspecifyaninvalidchannelnumber
- */
- unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc,unsignedshortto)
- {
- #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
- OS_CPU_SRcpu_sr=0u;
- #endif
SCI_RING_BUF*pbuf;
- unsignedcharoserr;
- switch(port)
- {/*Obtainpointertocommunicationschannel*/
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return(SCI_BAD_CH);
- }
OSSemPend(pbuf->RingBufTxSem,to,&oserr);/*WaitforspaceinTxbuffer*/
- if(oserr==OS_TIMEOUT)
- {
- return(SCI_TX_TIMEOUT);/*Timedout,returnerrorcode*/
- }
- OS_ENTER_CRITICAL();
- pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
- {
- pbuf->RingBufTxInPtr=&pbuf->pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpo