Freescale 9S12 系列單片機應(yīng)用筆記(SCI)3
- /*sci_ucos.h*/
#ifndef_SCI_RTOS_H_
#define_SCI_RTOS_H_
#defineSCI_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
- #defineSCI_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
/*
- *********************************************************************************************************
- *CONSTANTS
- *********************************************************************************************************
- */
#ifndefNUL
- #defineNUL0x00
- #endif
/*ERRORCODES*/
- #defineSCI_NO_ERR0/*Functioncallwassuccessful*/
- #defineSCI_BAD_CH1/*Invalidcommunicationsportchannel*/
- #defineSCI_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
- #defineSCI_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
- #defineSCI_TX_EMPTY4/*IftheTxbufferisempty.*/
- #defineSCI_RX_TIMEOUT5/*Ifatimeoutoccurredwhilewaitingforacharacter*/
- #defineSCI_TX_TIMEOUT6/*Ifatimeoutoccurredwhilewaitingtosendachar.*/
- #defineSCI_PARITY_NONE0/*Definesforsettingparity*/
- #defineSCI_PARITY_ODD1
- #defineSCI_PARITY_EVEN2
- /*
- *********************************************************************************************************
- *DATATYPES
- *********************************************************************************************************
- */
- typedefstruct{
- shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
- OS_EVENT*RingBufRxSem;/*PointertoRxsemaphore*/
- unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufRx[SCI_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
- shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
- OS_EVENT*RingBufTxSem;/*PointertoTxsemaphore*/
- unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufTx[SCI_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
- }SCI_RING_BUF;
- /**
- *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(unsignedcharch,unsignedshortto,unsignedchar*err);
/**
- *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);
/**
- *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
/**
- *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->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->RingBufTx[SCI_TX_BUF_SIZE]){/*WrapOUTpointer*/
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- }
- (void)OSSemPost(pbuf->RingBufTxSem);/*Indicatethatcharacterwillbesent*/
- *err=SCI_NO_ERR;
- return(c);/*Charactersarestillavailable*/
- }else{
- *err=SCI_TX_EMPTY;
- return(NUL);/*Bufferisempty*/
- }
- }
- voidSCI0_ISR_Handler(void)
- {
- charstatus;
- chardata;
- 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
- SCIDisTxInt(SCI0);
- }
- else
- {
- SCI0DRL=data;//Yes,Sendcharacter
- }
- }
- }
- voidSCI1_ISR_Handler(void)
- {
- charstatus;
- chardata;
- 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
- SCIDisTxInt(SCI1);
- }
- else
- {
- SCI1DRL=data;//Yes,Sendcharacter
- }
- }
- }
#pragmaCODE_SEGNON_BANKED
- interruptVectorNumber_Vsci0voidSCI0_ISR(void)
- {
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmldaaPPAGE;//3~,GetcurrentvalueofPPAGEregister
- __asmpsha;//2~,PushPPAGEregisterontocurrenttasksstack
- #endif
- __asmincOSIntNesting;//OSIntNesting++;
//if(OSIntNesting==1)
- //{
- //OSTCBCur->OSTCBStkPtr=StackPointer;
- //}
- __asm
- {
- ldabOSIntNesting
- cmpb#$01
- bneSCI0ISR1
ldxOSTCBCur
- sts0,x
- SCI0ISR1:
- }
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmcallSCI0_ISR_Handler;
- __asmcallOSIntExit;
- #else
- __asmjsrSCI0_ISR_Handler;
- __asmjsrOSIntExit;
- #endif
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmpula;//3~,GetvalueofPPAGEregister
- __asmstaaPPAGE;//3~,StoreintoCPUsPPAGEregister
- #endif
}
interruptVectorNumber_Vsci1voidSCI1_ISR(void)
- {
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmldaaPPAGE;//3~,GetcurrentvalueofPPAGEregister
- __asmpsha;//2~,PushPPAGEregisterontocurrenttasksstack
- #endif
- __asmincOSIntNesting;//OSIntNesting++;
//if(OSIntNesting==1)
- //{
- //OSTCBCur->OSTCBStkPtr=StackPointer;
- //}
- __asm
- {
- ldabOSIntNesting
- cmpb#$01
- bneSCI1ISR1
ldxOSTCBCur
- sts0,x
- SCI1ISR1:
- }
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmcallSCI1_ISR_Handler;
- __asmcallOSIntExit;
- #else
- __asmjsrSCI1_ISR_Handler;
- __asmjsrOSIntExit;
- #endif
#ifdefined(__BANKED__)||defined(__LARGE__)||defined(__PPAGE__)
- __asmpula;//3~,GetvalueofPPAGEregister
- __asmstaaPPAGE;//3~,StoreintoCPUsPPAGEregister
- #endif
}
下面給個簡單的例子:
#include
/*commondefinesandmacros*/ - #include"derivative.h"/*derivative-specificdefinitions*/
#include"INCLUDES.H"
- #include"crg.h"
- #include"sci.h"
- #include"sci_rtos.h"
OS_STKAppStartTaskStk[64];
staticvoidAppStartTask(void*pdata);
voidmain(void)
- {
- /*putyourowncodehere*/
- OS_CPU_SRcpu_sr;
CRGInit();
- CRGSetRTIFreqency(0x54);//200Hz
EnableInterrupts;
- OS_ENTER_CRITICAL();
- SCIInit(SCI0);
- SCIInit(SCI1);
- OS_EXIT_CRITICAL();
- OSInit();
- SCISetIEBit(SCI0,SCI_RIE);
- SCISetIEBit(SCI1,SCI_RIE);
- SCIBufferInit();
- (void)OSTaskCreate(AppStartTask,(void*)0x4321,(void*)&AppStartTaskStk[63],0);
- (void)OSStart();
- for(;;)
- {
- _FEED_COP();/*feedsthedog*/
- }/*loopforever*/
- /*pleasemakesurethatyouneverleavemain*/
- }
staticvoidAppStartTask(void*pdata)
- {
- INT8Uerr;
- charC;
- (void)pdata;
- for(;;)
- {
- C=SCIGetCharB(SCI1,0,&err);
- if(err==SCI_NO_ERR)
- (void)SCIPutCharB(SCI1,C,0);
- }
- }
評論