STM32F10x 学习笔记7(USART实现串口通讯 3)
时间:11-20
来源:互联网
点击:
- lt:
- *err=COMM_BAD_CH;
- return(NUL);
- }
- OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
- if(oserr==OS_TIMEOUT)
- {/*Seeifcharactersreceivedwithintimeout*/
- *err=COMM_RX_TIMEOUT;/*No,returnerrorcode*/
- return(NUL);
- }
- else
- {
- OS_ENTER_CRITICAL();
- pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
- c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
- {/*WrapOUTpointer*/
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- }
- OS_EXIT_CRITICAL();
- *err=COMM_NO_ERR;
- return(c);
- }
- }
- voidCommPutStr(unsignedcharch,uint8_t*str)
- {
- while(0!=*str)
- {
- CommPutChar(ch,*str,0);
- str++;
- }
- }
- staticunsignedcharCOMGetTxChar(unsignedcharch,unsignedchar*err)
- {
- unsignedcharc;
- COMM_RING_BUF*pbuf;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- *err=COMM_BAD_CH;
- return(NUL);
- }
- if(pbuf->RingBufTxCtr>0)
- {/*Seeifbufferisempty*/
- pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
- c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
- {/*WrapOUTpointer*/
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- }
- OSSemPost(pbuf->RingBufTxSem);/*Indicatethatcharacterwillbesent*/
- *err=COMM_NO_ERR;
- return(c);/*Charactersarestillavailable*/
- }
- else
- {
- *err=COMM_TX_EMPTY;
- return(NUL);/*Bufferisempty*/
- }
- }
- /*
- *********************************************************************************************************
- *INITIALIZECOMMUNICATIONSMODULE
- *Description:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
- *mustcallthisfunctionbeforecallinganyotherfunctions.
- *Arguments:none
- *********************************************************************************************************
- */
- voidCOMInit(void)
- {
- COMM_RING_BUF*pbuf;
- pbuf=&Comm1Buf;/*InitializetheringbufferforCOMM1*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
- pbuf=&Comm2Buf;/*InitializetheringbufferforCOMM2*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
- }
- /*
- *********************************************************************************************************
- *SEEIFRXCHARACTERBUFFERISEMPTY
- *
- *
- *Description:Thisfunctioniscalledbyyourapplicationtoseeifanycharacterisavailablefromthe
- *communicationschannel.Ifatleastonecharacterisavailable,thefunctionreturns
- *FALSEotherwise,thefunctionreturnsTRUE.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *Returns:TRUEifthebufferISempty.
- *FALSEifthebufferISNOTemptyoryouhavespecifiedanincorrectchannel.
- *********************************************************************************************************
- */
- unsignedcharCommIsEmpty(unsignedcharch)
- {
- unsignedcharempty;
- COMM_RING_BUF*pbuf;
- OS_CPU_SRcpu_sr;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- return(TRUE);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)
- {/*Seeifbufferisempty*/
- empty=FALSE;/*BufferisNOTempty*/
- }
- else
- {
- empty=TRUE;/*Bufferisempty*/
- }
- OS_EXIT_CRITICAL();
- return(empty);
- }
- /*
- *********************************************************************************************************
- *SEEIFTXCHARACTERBUFFERISFULL
- *Description:Thisfunctioniscalle
STM32F10xUSART串口通 相关文章:
- STM32F10x 学习笔记8(USART实现串口通讯 DMA 方式)(11-20)
- STM32F10x 学习笔记5(USART实现串口通讯 1)(11-20)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)