微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > Freescale 9S12 系列单片机应用笔记(SCI)3

Freescale 9S12 系列单片机应用笔记(SCI)3

时间:11-20 来源:互联网 点击:

  1. echaracterisavailable,thefunctionreturns
  2. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  3. */
  4. unsignedcharSCIBufferIsEmpty(unsignedcharport);

  5. /**
  6. *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
  7. *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  8. *
  9. *@paramport,portcanbeSCI0/SCI1
  10. *@returnIfthebufferisfull,thefunctionreturnsTRUE
  11. *otherwise,thefunctionreturnsFALSE.
  12. */
  13. unsignedcharSCIBufferIsFull(unsignedcharport);

    #endif

  1. /**

  2. *SCI(SerialCommunicationInterface)BufferedSerialI/O
  3. *@filesci_ucos.c
  4. *@authorLiYuan
  5. *@platformmc9s12XX
  6. *@date2012-7-22
  7. *@version1.0.1
  8. */

  9. #include"derivative.h"/*derivative-specificdefinitions*/
  10. #include
  11. #include"includes.H"
  12. #include"sci.h"
  13. #include"sci_rtos.h"

    /**

  14. *GLOBALVARIABLES
  15. */
  16. SCI_RING_BUFSCI0Buf;
  17. SCI_RING_BUFSCI1Buf;

  18. /**
  19. *Toobtainacharacterfromthecommunicationschannel.
  20. *@paramport,portcanbeSCI0/SCI1
  21. *@paramto,istheamountoftime(inclockticks)thatthecallingfunctioniswillingto
  22. *waitforacharactertoarrive.Ifyouspecifyatimeoutof0,thefunctionwill
  23. *waitforeverforacharactertoarrive.
  24. *@paramerr,isapointertowhereanerrorcodewillbeplaced:
  25. **errissettoSCI_NO_ERRifacharacterhasbeenreceived
  26. **errissettoSCI_RX_TIMEOUTifatimeoutoccurred
  27. **errissettoSCI_BAD_CHifyouspecifyaninvalidchannelnumber
  28. *@returnThecharacterinthebuffer(orNULifatimeoutoccurred)
  29. */
  30. unsignedcharSCIGetCharB(unsignedcharport,unsignedshortto,INT8U*err)
  31. {
  32. #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
  33. OS_CPU_SRcpu_sr=0u;
  34. #endif
  35. unsignedcharc;
  36. unsignedcharoserr;
  37. SCI_RING_BUF*pbuf;

    switch(port)

  38. {/*Obtainpointertocommunicationschannel*/
  39. caseSCI0:
  40. pbuf=&SCI0Buf;
  41. break;

    caseSCI1:

  42. pbuf=&SCI1Buf;
  43. break;

    default:

  44. *err=SCI_BAD_CH;
  45. return(0);
  46. }
  47. OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
  48. if(oserr==OS_TIMEOUT)
  49. {/*Seeifcharactersreceivedwithintimeout*/
  50. *err=SCI_RX_TIMEOUT;/*No,returnerrorcode*/
  51. return(NUL);
  52. }
  53. else
  54. {
  55. OS_ENTER_CRITICAL();
  56. pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
  57. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  58. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE]){/*WrapOUTpointer*/
  59. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  60. }
  61. OS_EXIT_CRITICAL();
  62. *err=SCI_NO_ERR;
  63. return(c);
  64. }
  65. }

    /**

  66. *Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
  67. *channel.Thefunctionwillwaitforthebuffertoemptyoutifthebufferisfull.
  68. *Thefunctionreturnstoyourapplicationifthebufferdoesntemptywithinthespecified
  69. *timeout.Atimeoutvalueof0meansthatthecallingfunctionwillwaitforeverforthe
  70. *buffertoemptyout.ThecharactertosendisfirstinsertedintotheTxbufferandwill
  71. *besentbytheTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISR
  72. *willbeenabled.
  73. *
  74. *@paramport,portcanbeSCI0/SCI1
  75. *@paramcisthecharactertosend.
  76. *@paramtoisthetimeout(inclockticks)towaitincasethebufferisfull.Ifyou
  77. *specifyatimeoutof0,thefunctionwillwaitforeverforthebuffertoempty.
  78. *@returnSCI_NO_ERRifthecharacterwasplacedintheTxbuffer
  79. *SCI_TX_TIMEOUTifthebufferdidntemptywithinthespecifiedtimeoutperiod
  80. *SCI_BAD_CHifyouspecifyaninvalidchannelnumber
  81. */
  82. unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc,unsignedshortto)
  83. {
  84. #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
  85. OS_CPU_SRcpu_sr=0u;
  86. #endif

    SCI_RING_BUF*pbuf;

  87. unsignedcharoserr;
  88. switch(port)
  89. {/*Obtainpointertocommunicationschannel*/
  90. caseSCI0:
  91. pbuf=&SCI0Buf;
  92. break;

    caseSCI1:

  93. pbuf=&SCI1Buf;
  94. break;

    default:

  95. return(SCI_BAD_CH);
  96. }

    OSSemPend(pbuf->RingBufTxSem,to,&oserr);/*WaitforspaceinTxbuffer*/

  97. if(oserr==OS_TIMEOUT)
  98. {
  99. return(SCI_TX_TIMEOUT);/*Timedout,returnerrorcode*/
  100. }
  101. OS_ENTER_CRITICAL();
  102. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  103. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  104. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
  105. {
  106. pbuf->RingBufTxInPtr=&pbuf->pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpo

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top