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

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

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

  1. pbuf->RingBufTxInPtr=&pbuf->pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
  2. }
  3. if(pbuf->RingBufTxCtr==1)/*Seeifthisisthefirstcharacter*/
  4. {
  5. SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
  6. }
  7. OS_EXIT_CRITICAL();
  8. return(SCI_NO_ERR);
  9. }

    /**

  10. *Toinitializethecommunicationsmodule.
  11. *Youmustcallthisfunctionbeforecallinganyotherfunctions.
  12. */
  13. voidSCIBufferInit(void)
  14. {
  15. SCI_RING_BUF*pbuf;

    pbuf=&SCI0Buf;/*InitializetheringbufferforSCI0*/

  16. pbuf->RingBufRxCtr=0;
  17. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  18. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  19. pbuf->RingBufRxSem=OSSemCreate(0);
  20. pbuf->RingBufTxCtr=0;
  21. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  22. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  23. pbuf->RingBufTxSem=OSSemCreate(SCI_TX_BUF_SIZE);

    pbuf=&SCI1Buf;/*InitializetheringbufferforSCI1*/

  24. pbuf->RingBufRxCtr=0;
  25. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  26. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  27. pbuf->RingBufRxSem=OSSemCreate(0);
  28. pbuf->RingBufTxCtr=0;
  29. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  30. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  31. pbuf->RingBufTxSem=OSSemCreate(SCI_TX_BUF_SIZE);
  32. }

    /**

  33. *Toseeifanycharacterisavailablefromthecommunicationschannel.
  34. *
  35. *@paramport,portcanbeSCI0/SCI1
  36. *@returnIfatleastonecharacterisavailable,thefunctionreturns
  37. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  38. */
  39. unsignedcharSCIBufferIsEmpty(unsignedcharport)
  40. {

    #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/

  41. OS_CPU_SRcpu_sr=0u;
  42. #endif
  43. unsignedcharempty;
  44. SCI_RING_BUF*pbuf;
  45. switch(port)
  46. {/*Obtainpointertocommunicationschannel*/
  47. caseSCI0:
  48. pbuf=&SCI0Buf;
  49. break;

    caseSCI1:

  50. pbuf=&SCI1Buf;
  51. break;
  52. default:
  53. return(0xff);
  54. break;
  55. }
  56. OS_ENTER_CRITICAL();
  57. if(pbuf->RingBufRxCtr>0)
  58. {/*Seeifbufferisempty*/
  59. empty=0;/*BufferisNOTempty*/
  60. }
  61. else
  62. {
  63. empty=1;/*Bufferisempty*/
  64. }
  65. OS_EXIT_CRITICAL();
  66. return(empty);

    }

    /**

  67. *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
  68. *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  69. *
  70. *@paramport,portcanbeSCI0/SCI1
  71. *@returnIfthebufferisfull,thefunctionreturnsTRUE
  72. *otherwise,thefunctionreturnsFALSE.
  73. */
  74. unsignedcharSCIBufferIsFull(unsignedcharport)
  75. {
  76. #ifOS_CRITICAL_METHOD==3u/*AllocatestorageforCPUstatusregister*/
  77. OS_CPU_SRcpu_sr=0u;
  78. #endif
  79. charfull;
  80. SCI_RING_BUF*pbuf;
  81. switch(port)
  82. {/*Obtainpointertocommunicationschannel*/
  83. caseSCI0:
  84. pbuf=&SCI0Buf;
  85. break;

    caseSCI1:

  86. pbuf=&SCI1Buf;
  87. break;

    default:

  88. return(255);
  89. }
  90. OS_ENTER_CRITICAL();
  91. if(pbuf->RingBufTxCtr
  92. full=0;/*BufferisNOTfull*/
  93. }else{
  94. full=1;/*Bufferisfull*/
  95. }
  96. OS_EXIT_CRITICAL();
  97. return(full);
  98. }

  99. //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
  100. staticvoidSCIPutRxChar(unsignedcharport,unsignedcharc)
  101. {

    SCI_RING_BUF*pbuf;

    switch(port)

  102. {/*Obtainpointertocommunicationschannel*/
  103. caseSCI0:
  104. pbuf=&SCI0Buf;
  105. break;

    caseSCI1:

  106. pbuf=&SCI1Buf;
  107. break;

    default:

  108. return;
  109. }
  110. if(pbuf->RingBufRxCtr
  111. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
  112. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
  113. if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE]){/*WrapINpointer*/
  114. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  115. }
  116. (void)OSSemPost(pbuf->RingBufRxSem);/*Indicatethatcharacterwasreceived*/
  117. }
  118. }

  119. //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
  120. //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
  121. //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
  122. //lastcharactertosend.
  123. staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
  124. {
  125. unsignedcharc;
  126. SCI_RING_BUF*pbuf;

    switch(port)

  127. {/*Obtainpointertocommunicationschannel*/
  128. caseSCI0:
  129. pbuf=&SCI0Buf;
  130. break;

    caseSCI1:

  131. pbuf=&SCI1Buf;
  132. break;

    default:

  133. *err=SCI_BAD_CH;
  134. return(0);
  135. }
  136. if(pbuf->RingBufTxCtr>0){/*Seeifbufferisempty*/
  137. pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
  138. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
  139. if(pbuf->RingBufTxOutPtr==&pbuf->if(pbuf->RingBufTxOutPtr==&p

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

网站地图

Top