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

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

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

  1. aseSCI0:
  2. pbuf=&SCI0Buf;
  3. break;

    caseSCI1:

  4. pbuf=&SCI1Buf;
  5. break;

    default:

  6. *err=SCI_BAD_CH;
  7. return(0);
  8. }
  9. OS_ENTER_CRITICAL();
  10. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  11. {
  12. pbuf->RingBufRxCtr--;/*No,decrementcharactercount*/
  13. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  14. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
  15. {
  16. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
  17. }
  18. OS_EXIT_CRITICAL();
  19. *err=SCI_NO_ERR;
  20. return(c);
  21. }
  22. else
  23. {
  24. OS_EXIT_CRITICAL();
  25. *err=SCI_RX_EMPTY;
  26. c=0;/*Bufferisempty,return0*/
  27. return(c);
  28. }
  29. }

  30. /**
  31. *Tosendacharacteronthecommunicationschannel.
  32. *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  33. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  34. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  35. *
  36. *@paramport,portcanbeSCI0/SCI1
  37. *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  38. *COMM_TX_FULLifthebufferwasfull
  39. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  40. */
  41. unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc)
  42. {
  43. unsignedcharcpu_sr;
  44. SCI_RING_BUF*pbuf;
  45. /*Obtainpointertocommunicationschannel*/
  46. switch(port)
  47. {
  48. caseSCI0:
  49. pbuf=&SCI0Buf;
  50. break;

    caseSCI1:

  51. pbuf=&SCI1Buf;
  52. break;

    default:

  53. return(SCI_BAD_CH);
  54. }

    OS_ENTER_CRITICAL();

  55. if(pbuf->RingBufTxCtr
  56. {
  57. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  58. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  59. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
  60. {
  61. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
  62. }
  63. if(pbuf->RingBufTxCtr==1)
  64. {/*Seeifthisisthefirstcharacter*/
  65. SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
  66. OS_EXIT_CRITICAL();
  67. }
  68. else
  69. {
  70. OS_EXIT_CRITICAL();
  71. }
  72. return(SCI_NO_ERR);
  73. }
  74. else
  75. {
  76. OS_EXIT_CRITICAL();
  77. return(SCI_TX_FULL);
  78. }
  79. }

    /**

  80. *Toinitializethecommunicationsmodule.
  81. *Youmustcallthisfunctionbeforecallinganyotherfunctions.
  82. */
  83. voidSCIBufferInit(void)
  84. {
  85. SCI_RING_BUF*pbuf;
  86. /*InitializetheringbufferforSCI0*/
  87. pbuf=&SCI0Buf;
  88. pbuf->RingBufRxCtr=0;
  89. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  90. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  91. pbuf->RingBufTxCtr=0;
  92. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  93. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  94. /*InitializetheringbufferforSCI1*/
  95. pbuf=&SCI1Buf;
  96. pbuf->RingBufRxCtr=0;
  97. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  98. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  99. pbuf->RingBufTxCtr=0;
  100. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  101. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  102. }

  103. /**
  104. *Toseeifanycharacterisavailablefromthecommunicationschannel.
  105. *
  106. *@paramport,portcanbeSCI0/SCI1
  107. *@returnIfatleastonecharacterisavailable,thefunctionreturns
  108. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  109. */
  110. unsignedcharSCIBufferIsEmpty(unsignedcharport)
  111. {
  112. unsignedcharcpu_sr;
  113. unsignedcharempty;
  114. SCI_RING_BUF*pbuf;
  115. /*Obtainpointertocommunicationschannel*/
  116. switch(port)
  117. {
  118. caseSCI0:
  119. pbuf=&SCI0Buf;
  120. break;

    caseSCI1:

  121. pbuf=&SCI1Buf;
  122. break;

    default:

  123. return(1);
  124. }
  125. OS_ENTER_CRITICAL();
  126. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  127. {
  128. empty=0;/*BufferisNOTempty*/
  129. }
  130. else
  131. {
  132. empty=1;/*Bufferisempty*/
  133. }
  134. OS_EXIT_CRITICAL();
  135. return(empty);
  136. }

  137. /**
  138. *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
  139. *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  140. *
  141. *@paramport,portcanbeSCI0/SCI1
  142. *@returnIfthebufferisfull,thefunctionreturnsTRUE
  143. *otherwise,thefunctionreturnsFALSE.
  144. */
  145. unsignedcharSCIBufferIsFull(unsignedcharport)
  146. {
  147. unsignedcharcpu_sr;
  148. charfull;
  149. SCI_RING_BUF*pbuf;
  150. /*Obtainpointertocommunicationschannel*/
  151. switch(port)
  152. {
  153. caseSCI0:
  154. pbuf=&SCI0Buf;
  155. break;

    caseSCI1:

  156. pbuf=&SCI1Buf;
  157. break;

    default:

  158. return(1);
  159. }
  160. OS_ENTER_CRITICAL();
  161. if(pbuf->RingBufTxCtr
  162. {/*Seeifbufferisfull*/
  163. full=0;/*BufferisNOTfull*/
  164. }
  165. else
  166. {
  167. full=1;/*Bufferisfull*/
  168. }
  169. OS_EXIT_CRITICAL();
  170. return(full);
  171. }

  172. //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
  173. staticvoidSCIPutRxChar(u

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

网站地图

Top