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

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

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

  1. lt:
  2. *err=COMM_BAD_CH;
  3. return(NUL);
  4. }
  5. OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
  6. if(oserr==OS_TIMEOUT)
  7. {/*Seeifcharactersreceivedwithintimeout*/
  8. *err=COMM_RX_TIMEOUT;/*No,returnerrorcode*/
  9. return(NUL);
  10. }
  11. else
  12. {
  13. OS_ENTER_CRITICAL();
  14. pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
  15. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  16. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
  17. {/*WrapOUTpointer*/
  18. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  19. }
  20. OS_EXIT_CRITICAL();
  21. *err=COMM_NO_ERR;
  22. return(c);
  23. }
  24. }
  25. voidCommPutStr(unsignedcharch,uint8_t*str)
  26. {
  27. while(0!=*str)
  28. {
  29. CommPutChar(ch,*str,0);
  30. str++;
  31. }
  32. }
  33. staticunsignedcharCOMGetTxChar(unsignedcharch,unsignedchar*err)
  34. {
  35. unsignedcharc;
  36. COMM_RING_BUF*pbuf;
  37. switch(ch)
  38. {/*Obtainpointertocommunicationschannel*/
  39. caseCOM1:
  40. pbuf=&Comm1Buf;
  41. break;
  42. caseCOM2:
  43. pbuf=&Comm2Buf;
  44. break;
  45. default:
  46. *err=COMM_BAD_CH;
  47. return(NUL);
  48. }
  49. if(pbuf->RingBufTxCtr>0)
  50. {/*Seeifbufferisempty*/
  51. pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
  52. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
  53. if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
  54. {/*WrapOUTpointer*/
  55. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  56. }
  57. OSSemPost(pbuf->RingBufTxSem);/*Indicatethatcharacterwillbesent*/
  58. *err=COMM_NO_ERR;
  59. return(c);/*Charactersarestillavailable*/
  60. }
  61. else
  62. {
  63. *err=COMM_TX_EMPTY;
  64. return(NUL);/*Bufferisempty*/
  65. }
  66. }
  67. /*
  68. *********************************************************************************************************
  69. *INITIALIZECOMMUNICATIONSMODULE
  70. *Description:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
  71. *mustcallthisfunctionbeforecallinganyotherfunctions.
  72. *Arguments:none
  73. *********************************************************************************************************
  74. */
  75. voidCOMInit(void)
  76. {
  77. COMM_RING_BUF*pbuf;
  78. pbuf=&Comm1Buf;/*InitializetheringbufferforCOMM1*/
  79. pbuf->RingBufRxCtr=0;
  80. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  81. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  82. pbuf->RingBufRxSem=OSSemCreate(0);
  83. pbuf->RingBufTxCtr=0;
  84. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  85. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  86. pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
  87. pbuf=&Comm2Buf;/*InitializetheringbufferforCOMM2*/
  88. pbuf->RingBufRxCtr=0;
  89. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  90. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  91. pbuf->RingBufRxSem=OSSemCreate(0);
  92. pbuf->RingBufTxCtr=0;
  93. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  94. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  95. pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
  96. }
  97. /*
  98. *********************************************************************************************************
  99. *SEEIFRXCHARACTERBUFFERISEMPTY
  100. *
  101. *
  102. *Description:Thisfunctioniscalledbyyourapplicationtoseeifanycharacterisavailablefromthe
  103. *communicationschannel.Ifatleastonecharacterisavailable,thefunctionreturns
  104. *FALSEotherwise,thefunctionreturnsTRUE.
  105. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
  106. *COMM1
  107. *COMM2
  108. *Returns:TRUEifthebufferISempty.
  109. *FALSEifthebufferISNOTemptyoryouhavespecifiedanincorrectchannel.
  110. *********************************************************************************************************
  111. */
  112. unsignedcharCommIsEmpty(unsignedcharch)
  113. {
  114. unsignedcharempty;
  115. COMM_RING_BUF*pbuf;
  116. OS_CPU_SRcpu_sr;
  117. switch(ch)
  118. {/*Obtainpointertocommunicationschannel*/
  119. caseCOM1:
  120. pbuf=&Comm1Buf;
  121. break;
  122. caseCOM2:
  123. pbuf=&Comm2Buf;
  124. break;
  125. default:
  126. return(TRUE);
  127. }
  128. OS_ENTER_CRITICAL();
  129. if(pbuf->RingBufRxCtr>0)
  130. {/*Seeifbufferisempty*/
  131. empty=FALSE;/*BufferisNOTempty*/
  132. }
  133. else
  134. {
  135. empty=TRUE;/*Bufferisempty*/
  136. }
  137. OS_EXIT_CRITICAL();
  138. return(empty);
  139. }
  140. /*
  141. *********************************************************************************************************
  142. *SEEIFTXCHARACTERBUFFERISFULL
  143. *Description:Thisfunctioniscalle

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

网站地图

Top