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

STM32F10x 学习笔记6(USART实现串口通讯 2)

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

  1. */
  2. voidCOMBufferInit(void);
  3. /************************************************************
  4. *function:COMBufferIsEmpty
  5. *parameter:charport,portcanbeCOM1/COM2
  6. *return:char
  7. *usage:Thisfunctioniscalledbyyourapplicationtosee
  8. *ifanycharacterisavailablefromthecommunicationschannel.
  9. *Ifatleastonecharacterisavailable,thefunctionreturns
  10. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  11. *changelog:
  12. *************************************************************/
  13. unsignedcharCOMBufferIsEmpty(unsignedcharport);
  14. /************************************************************
  15. *function:COMBufferIsFull
  16. *parameter:charport,portcanbeCOM1/COM2
  17. *return:char
  18. *usage:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
  19. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  20. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
  21. *changelog:
  22. *************************************************************/
  23. unsignedcharCOMBufferIsFull(unsignedcharport);
  24. #endif

  1. /*
  2. *file:com_buffered.c
  3. *author:LiYuan
  4. *platform:STM32F107
  5. *date:2013-5-5
  6. *version:0.0.1
  7. *description:UARTRingBuffer
  8. **/
  9. #include"stm32f10x_usart.h"
  10. #include"com_buffered.h"
  11. #defineOS_ENTER_CRITICAL()__set_PRIMASK(1)
  12. #defineOS_EXIT_CRITICAL()__set_PRIMASK(0)
  13. /**
  14. *EnablesTransmiterinterrupt.
  15. **/
  16. staticvoidCOMEnableTxInt(unsignedcharport)
  17. {
  18. staticUSART_TypeDef*map[2]={USART1,USART2};
  19. USART_ITConfig(map[port],USART_IT_TXE,ENABLE);
  20. }
  21. /*
  22. *********************************************************************************************************
  23. *DATATYPES
  24. *********************************************************************************************************
  25. */
  26. typedefstruct{
  27. shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
  28. unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  29. unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  30. unsignedcharRingBufRx[COM_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
  31. shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
  32. unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  33. unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  34. unsignedcharRingBufTx[COM_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
  35. }COM_RING_BUF;
  36. /*
  37. *********************************************************************************************************
  38. *GLOBALVARIABLES
  39. *********************************************************************************************************
  40. */
  41. COM_RING_BUFCOM1Buf;
  42. COM_RING_BUFCOM2Buf;
  43. /************************************************************
  44. *function:COMGetCharB
  45. *parameter:charport,portcanbeCOM1/COM2
  46. *parameter:char*errisapointertowhereanerrorcodewillbeplaced:
  47. **errissettoCOM_NO_ERRifacharacterisavailable
  48. **errissettoCOM_RX_EMPTYiftheRxbufferisempty
  49. **errissettoCOM_BAD_CHifyouhavespecifiedaninvalidchannel
  50. *return:char
  51. *usage:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
  52. *channel.
  53. *changelog:
  54. *************************************************************/
  55. unsignedcharCOMGetCharB(unsignedcharport,unsignedchar*err)
  56. {
  57. //unsignedcharcpu_sr;
  58. unsignedcharc;
  59. COM_RING_BUF*pbuf;
  60. switch(port)
  61. {/*Obtainpointertocommunicationschannel*/
  62. caseCOM1:
  63. pbuf=&COM1Buf;
  64. break;
  65. caseCOM2:
  66. pbuf=&COM2Buf;
  67. break;
  68. default:
  69. *err=COM_BAD_CH;
  70. return(0);
  71. }
  72. OS_ENTER_CRITICAL();
  73. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  74. {
  75. pbuf->RingBufRxCtr--;/*No,decrementcharactercount*/
  76. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  77. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COM_RX_BUF_SIZE])
  78. {
  79. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
  80. }
  81. OS_EXIT_CRITICAL();
  82. *err=COM_NO_ERR;
  83. return(c);
  84. }else{
  85. OS_EXIT_CRITICAL();
  86. *err=COM_RX

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

网站地图

Top