📄 comm.c
字号:
/*
*********************************************************************************************************
* Embedded Systems Building Blocks
* Complete and Ready-to-Use Modules in C
*串行口通信程序
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDES
*********************************************************************************************************
*/
#include "..\includes\INCLUDES.H"
extern bit SemTIbusy;
/*
*********************************************************************************************************
* DATA TYPES
*********************************************************************************************************
*/
idata INT8U RingBufRxCtr; /* Number of characters in the Rx ring buffer */
idata INT8U *RingBufRxInPtr; /* Pointer to where next character will be inserted */
idata INT8U *RingBufRxOutPtr; /* Pointer from where next character will be extracted */
idata INT8U RingBufRx[COMM_RX_BUF_SIZE]; /* Ring buffer character storage (Rx) */
idata INT8U RingBufTxCtr; /* Number of characters in the Tx ring buffer */
idata INT8U *RingBufTxInPtr; /* Pointer to where next character will be inserted */
idata INT8U *RingBufTxOutPtr; /* Pointer from where next character will be extracted */
idata INT8U RingBufTx[COMM_TX_BUF_SIZE]; /* Ring buffer character storage (Tx) */
/*
*********************************************************************************************************
* GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* REMOVE CHARACTER FROM RING BUFFER
*
*从接收缓冲区取数,应用中取数
* Description : This function is called by your application to obtain a character from the communications
* channel.
* Arguments 'err' is a pointer to where an error code will be placed:
* *err is set to COMM_NO_ERR if a character is available
* *err is set to COMM_RX_EMPTY if the Rx buffer is empty
* *err is set to COMM_BAD_CH if you have specified an invalid channel
* Returns : The character in the buffer (or NUL if the buffer is empty)
*********************************************************************************************************
*/
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
extern OS_CPU_SR cpu_sr;
#endif
INT8U CommGetChar (INT8U *err)
{
INT8U c;
OS_ENTER_CRITICAL();
if (RingBufRxCtr > 0) { /* See if buffer is empty */
RingBufRxCtr--; /* No, decrement character count */
c = *RingBufRxOutPtr++; /* Get character from buffer */
if (RingBufRxOutPtr == &RingBufRx[COMM_RX_BUF_SIZE]) { /* Wrap OUT pointer */
RingBufRxOutPtr = &RingBufRx[0];
}
OS_EXIT_CRITICAL();
*err = COMM_NO_ERR;
return (c);
} else {
OS_EXIT_CRITICAL();
*err = COMM_RX_EMPTY;
c = NUL; /* Buffer is empty, return NUL */
return (c);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET TX CHARACTER FROM RING BUFFER
*
*从发送缓冲区取数,中断中发数调用
* Description : This function is called by the Tx ISR to extract the next character from the Tx buffer.
* The function returns FALSE if the buffer is empty after the character is extracted from
* the buffer. This is done to signal the Tx ISR to disable interrupts because this is the
* last character to send.
* Arguments 'err' is a pointer to where an error code will be deposited:
* *err is set to COMM_NO_ERR if at least one character was left in the
* buffer.
* *err is set to COMM_TX_EMPTY if the Tx buffer is empty.
* *err is set to COMM_BAD_CH if you have specified an incorrect channel
* Returns : The next character in the Tx buffer or NUL if the buffer is empty.
*********************************************************************************************************
*/
INT8U CommGetTxChar (INT8U *err)
{
INT8U c;
if (RingBufTxCtr > 0) { /* See if buffer is empty */
RingBufTxCtr--; /* No, decrement character count */
c = *RingBufTxOutPtr++; /* Get character from buffer */
if (RingBufTxOutPtr == &RingBufTx[COMM_TX_BUF_SIZE]) { /* Wrap OUT pointer */
RingBufTxOutPtr = &RingBufTx[0];
}
*err = COMM_NO_ERR;
return (c); /* Characters are still available */
} else {
*err = COMM_TX_EMPTY;
return (NUL); /* Buffer is empty */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZE COMMUNICATIONS MODULE
*
*
* Description : This function is called by your application to initialize the communications module. You
* must call this function before calling any other functions.
* Arguments : none
*********************************************************************************************************
*/
void CommInit(void)
{
RingBufRxCtr = 0;
RingBufRxInPtr = &RingBufRx[0];
RingBufRxOutPtr = &RingBufRx[0];
RingBufTxCtr = 0;
RingBufTxInPtr = &RingBufTx[0];
RingBufTxOutPtr = &RingBufTx[0];
}
void CommRxInit(void)
{
RingBufRxCtr = 0;
RingBufRxInPtr = &RingBufRx[0];
RingBufRxOutPtr = &RingBufRx[0];
}
/*
void CommTxInit(void)
{
RingBufTxCtr = 0;
RingBufTxInPtr = &RingBufTx[0];
RingBufTxOutPtr = &RingBufTx[0];
}
*/
/*$PAGE*/
/*
*********************************************************************************************************
* SEE IF RX CHARACTER BUFFER IS EMPTY
*测试接收缓冲区是否为空
*
* Description : This function is called by your application to see if any character is available from the
* communications channel. If at least one character is available, the function returns
* FALSE otherwise, the function returns TRUE.
* Returns : TRUE if the buffer IS empty.
* FALSE if the buffer IS NOT empty or you have specified an incorrect channel
*********************************************************************************************************
*/
BOOLEAN CommIsEmpty ()
{
BOOLEAN empty;
OS_ENTER_CRITICAL();
if (RingBufRxCtr > 0) { /* See if buffer is empty */
empty = FALSE; /* Buffer is NOT empty */
} else {
empty = TRUE; /* Buffer is empty */
}
OS_EXIT_CRITICAL();
return (empty);
}
/*
*********************************************************************************************************
* SEE IF TX CHARACTER BUFFER IS FULL
*测试发送缓区是否满?
*
* Description : This function is called by your application to see if any more characters can be placed
* in the Tx buffer. In other words, this function check to see if the Tx buffer is full.
* If the buffer is full, the function returns TRUE otherwise, the function returns FALSE.
* Returns : TRUE if the buffer IS full.
* FALSE if the buffer IS NOT full or you have specified an incorrect channel
*********************************************************************************************************
*/
BOOLEAN CommIsFull ()
{
BOOLEAN full;
OS_ENTER_CRITICAL();
if (RingBufTxCtr < COMM_TX_BUF_SIZE) { /* See if buffer is full */
full = FALSE; /* Buffer is NOT full */
} else {
full = TRUE; /* Buffer is full */
}
OS_EXIT_CRITICAL();
return (full);
}
/*
*********************************************************************************************************
* OUTPUT CHARACTER
*
*向发送缓冲区放数
* Description : This function is called by your application to send a character on the communications
* channel. The character to send is first inserted into the Tx buffer and will be sent by
* the Tx ISR. If this is the first character placed into the buffer, the Tx ISR will be
* enabled. If the Tx buffer is full, the character will not be sent (i.e. it will be lost)
* Arguments 'c' is the character to send.
* Returns : COMM_NO_ERR if the function was successful (the buffer was not full)
* COMM_TX_FULL if the buffer was full
* COMM_BAD_CH if you have specified an incorrect channel
*********************************************************************************************************
*/
INT8U CommPutChar (INT8U c)
{
if(RingBufTxCtr == 0) {
while(SemTIbusy);
}
OS_ENTER_CRITICAL();
if (RingBufTxCtr < COMM_TX_BUF_SIZE) { /* See if buffer is full */
RingBufTxCtr++; /* No, increment character count */
*RingBufTxInPtr++ = c; /* Put character into buffer */
if (RingBufTxInPtr == &RingBufTx[COMM_TX_BUF_SIZE]) { /* Wrap IN pointer */
RingBufTxInPtr = &RingBufTx[0];
}
if (RingBufTxCtr == 1) { /* See if this is the first character */
CommTxIntEn(); /* Yes, Enable Tx interrupts */
OS_EXIT_CRITICAL();
} else {
OS_EXIT_CRITICAL();
}
return (COMM_NO_ERR);
} else {
OS_EXIT_CRITICAL();
return (COMM_TX_FULL);
}
}
/*
*********************************************************************************************************
* INSERT CHARACTER INTO RING BUFFER
*
*向接收缓冲区放数(接收中断调用)
* Description : This function is called by the Rx ISR to insert a character into the receive ring buffer.
* Arguments : 'c' is the character to insert into the ring buffer. If the buffer is full, the
* character will not be inserted, it will be lost.
*********************************************************************************************************
*/
void CommPutRxChar (INT8U c)
{
if (RingBufRxCtr < COMM_RX_BUF_SIZE) { /* See if buffer is full */
RingBufRxCtr++; /* No, increment character count */
*RingBufRxInPtr++ = c; /* Put character into buffer */
if (RingBufRxInPtr == &RingBufRx[COMM_RX_BUF_SIZE]) { /* Wrap IN pointer */
RingBufRxInPtr = &RingBufRx[0];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -