微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F10x 学习笔记7(USART实现串口通讯 3)

STM32F10x 学习笔记7(USART实现串口通讯 3)

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

  1. dbyyourapplicationtoseeifanymorecharacterscanbeplaced
  2. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  3. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
  4. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
  5. *COMM1
  6. *COMM2
  7. *Returns:TRUEifthebufferISfull.
  8. *FALSEifthebufferISNOTfulloryouhavespecifiedanincorrectchannel.
  9. *********************************************************************************************************
  10. */
  11. unsignedcharCommIsFull(unsignedcharch)
  12. {
  13. unsignedcharfull;
  14. COMM_RING_BUF*pbuf;
  15. OS_CPU_SRcpu_sr;
  16. switch(ch)
  17. {/*Obtainpointertocommunicationschannel*/
  18. caseCOM1:
  19. pbuf=&Comm1Buf;
  20. break;
  21. caseCOM2:
  22. pbuf=&Comm2Buf;
  23. break;
  24. default:
  25. return(TRUE);
  26. }
  27. OS_ENTER_CRITICAL();
  28. if(pbuf->RingBufTxCtr
  29. {/*Seeifbufferisfull*/
  30. full=FALSE;/*BufferisNOTfull*/
  31. }
  32. else
  33. {
  34. full=TRUE;/*Bufferisfull*/
  35. }
  36. OS_EXIT_CRITICAL();
  37. return(full);
  38. }
  39. /*
  40. *********************************************************************************************************
  41. *OUTPUTCHARACTER
  42. *
  43. *
  44. *Description:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
  45. *channel.Thefunctionwillwaitforthebuffertoemptyoutifthebufferisfull.
  46. *Thefunctionreturnstoyourapplicationifthebufferdoesntemptywithinthespecified
  47. *timeout.Atimeoutvalueof0meansthatthecallingfunctionwillwaitforeverforthe
  48. *buffertoemptyout.ThecharactertosendisfirstinsertedintotheTxbufferandwill
  49. *besentbytheTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISR
  50. *willbeenabled.
  51. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
  52. *COMM1
  53. *COMM2
  54. *cisthecharactertosend.
  55. *toisthetimeout(inclockticks)towaitincasethebufferisfull.Ifyou
  56. *specifyatimeoutof0,thefunctionwillwaitforeverforthebuffertoempty.
  57. *Returns:COMM_NO_ERRifthecharacterwasplacedintheTxbuffer
  58. *COMM_TX_TIMEOUTifthebufferdidntemptywithinthespecifiedtimeoutperiod
  59. *COMM_BAD_CHifyouspecifyaninvalidchannelnumber
  60. *********************************************************************************************************
  61. */
  62. unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto)
  63. {
  64. unsignedcharoserr;
  65. COMM_RING_BUF*pbuf;
  66. OS_CPU_SRcpu_sr;
  67. switch(ch)
  68. {/*Obtainpointertocommunicationschannel*/
  69. caseCOM1:
  70. pbuf=&Comm1Buf;
  71. break;
  72. caseCOM2:
  73. pbuf=&Comm2Buf;
  74. break;
  75. default:
  76. return(COMM_BAD_CH);
  77. }
  78. OSSemPend(pbuf->RingBufTxSem,to,&oserr);/*WaitforspaceinTxbuffer*/
  79. if(oserr==OS_TIMEOUT)
  80. {
  81. return(COMM_TX_TIMEOUT);/*Timedout,returnerrorcode*/
  82. }
  83. OS_ENTER_CRITICAL();
  84. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  85. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  86. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
  87. {/*WrapINpointer*/
  88. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  89. }
  90. if(pbuf->RingBufTxCtr==1)
  91. {/*Seeifthisisthefirstcharacter*/
  92. COMEnableTxInt(ch);/*Yes,EnableTxinterrupts*/
  93. }
  94. OS_EXIT_CRITICAL();
  95. return(COMM_NO_ERR);
  96. }
  97. /*
  98. *********************************************************************************************************
  99. *INSERTCHARACTERINTORINGBUFFER
  100. *
  101. *
  102. *Description:ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
  103. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
  104. *COMM1
  105. *COMM2
  106. *cisthecharactertoinsertintotheringbuffer.Ifthebufferisfull,the
  107. *characterwillnotbeinserted,itwillbelost.
  108. *********************************************************************************************************
  109. */
  110. staticvoidCOMPutRxChar(unsignedcharch,unsignedcharc)
  111. {
  112. COMM_RING_BUF*pbuf;
  113. switch(ch)
  114. {/*Obtainpointertocommunicationschannel*/
  115. caseCOM1:
  116. pbuf=&Comm1Buf;
  117. break;
  118. caseCOM2:
  119. pbuf=&Comm2Buf;
  120. break;
  121. default:
  122. return;
  123. }
  124. if(pbuf->RingBufRxCtr
  125. {/*Seeifbufferisfull*/
  126. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
  127. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
  128. if(pbuf->RingBufRxInPtr==&pbuf->if(pbuf->RingBufRxInPtr==&pbuf->R

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

网站地图

Top