📄 probe_com.c
字号:
* DEF_FALSE if the entire string could not be queued in the buffer.
*
* Caller(s) : Application.
*
* Note(s) : (1) The string buffer is implemented as a circular buffer. This function is one of two
* points of access for this buffer, the other being in the task or ISR which forms the
* tx packets. Only this function should modify the global current write index
* (ProbeCom_StrBufWrIx); only the task or ISR which forms the packets should modify the
* global current read index (ProbeCom_StrBufRdIx).
*
* (2) The global current write index (ProbeCom_StrBufWrIx) is the index of the next location
* in the buffer to write. The global current read index (ProbeCom_StrBufRdIx) is the
* index of the next location in the buffer to read.
*
* (3) The string buffer, an array of PROBE_COM_STR_BUF_SIZE bytes, can only hold
* (PROBE_COM_STR_BUF_SIZE - 1) bytes so that the condition
*
* ProbeCom_StrBufWrIx == ProbeCom_StrBufRdIx
*
* will be true if and only if the buffer is empty. Consequently, this function
* always leaves an empty space in the buffer.
*
* (4) If called from an ISR, dly MUST be 0.
*********************************************************************************************************
*/
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
CPU_BOOLEAN ProbeCom_TxStr (CPU_CHAR *pstr,
CPU_INT16U dly)
{
CPU_BOOLEAN ret;
CPU_INT32U len;
CPU_INT16U wr_ix;
CPU_INT16U rd_ix;
CPU_INT16U wr_ix_n;
CPU_INT16U nbytes_free;
CPU_INT16U nbytes_wr;
if (dly == 0) {
ret = ProbeCom_OS_Pend(DEF_FALSE);
} else {
ret = ProbeCom_OS_Pend(DEF_TRUE);
}
if (ret == DEF_FALSE) {
return (DEF_FALSE);
}
len = (CPU_INT32U)Str_Len(pstr); /* Determine length of the string (without NULL byte). */
while (len > 0) {
rd_ix = ProbeCom_StrBufRdIx;
wr_ix = ProbeCom_StrBufWrIx;
if (rd_ix > wr_ix) { /* If rd_ix > wr_ix, store str at */
nbytes_free = rd_ix - wr_ix - 1; /* buf interval [wr_ix, rd_ix - 1). */
} else {
if (rd_ix == 0) { /* If rd_ix <= wr_ix && rd_ix == 0, store str at */
/* buf interval [wr_ix, end_ix - 1). */
nbytes_free = PROBE_COM_STR_BUF_SIZE - wr_ix - 1;
} else { /* If rd_ix <= wr_ix && rd_ix != 0, store str at */
nbytes_free = PROBE_COM_STR_BUF_SIZE - wr_ix; /* buf interval [wr_ix, end_ix). */
}
}
if (nbytes_free == 0) { /* If the buf is full ... */
if (dly == 0) { /* (a) Rtn if dly = 0. */
ProbeCom_OS_Post();
return (DEF_FALSE);
} else { /* (b) Call OS fnct to dly and continue. */
ProbeCom_OS_Dly(dly);
}
} else {
if (nbytes_free > len) { /* If str is shorter than free space. */
nbytes_wr = len;
} else {
nbytes_wr = nbytes_free;
}
wr_ix_n = wr_ix + nbytes_wr; /* Assign wr ix after wr. */
if (wr_ix_n == PROBE_COM_STR_BUF_SIZE) { /* Wrap buf ix around. */
wr_ix_n = 0;
}
Mem_Copy((void *)&ProbeCom_StrBuf[wr_ix], /* Copy str to buf. */
(void *) pstr,
(CPU_SIZE_T) nbytes_wr);
ProbeCom_StrBufWrIx = wr_ix_n; /* Assign new global wr ix. */
pstr += nbytes_wr; /* Inc str ptr. */
len -= nbytes_wr; /* Dec str len. */
}
}
ProbeCom_OS_Post();
return (DEF_TRUE); /* Rtn TRUE to indicate success. */
}
#endif
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* ProbeCom_StrRdy()
*
* Description : Check if a string is ready for transmission.
*
* Argument(s) : none.
*
* Return(s) : DEF_TRUE if a string is in the buffer for transmission.
* DEF_FALSE if no string is in the buffer for transmission.
*
* Caller(s) : ProbeCom_PktModifier().
*
* Note(s) : (1) See Notes for 'ProbeCom_TxStr()'.
*********************************************************************************************************
*/
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
static CPU_BOOLEAN ProbeCom_StrRdy (void)
{
CPU_BOOLEAN rdy;
CPU_INT16U wr_ix;
CPU_INT16U rd_ix;
wr_ix = ProbeCom_StrBufWrIx;
rd_ix = ProbeCom_StrBufRdIx;
if (wr_ix == rd_ix) {
rdy = DEF_FALSE;
} else {
rdy = DEF_TRUE;
}
return (rdy);
}
#endif
/*
*********************************************************************************************************
* ProbeCom_PktModifier()
*
* Description : Get packet modfier byte.
*
* Argument(s) : none.
*
* Return(s) : The modifier byte.
*
* Caller(s) : Various command handlers (ProbeCom_Cmd????()).
*
* Note(s) : none.
*********************************************************************************************************
*/
static CPU_INT08U ProbeCom_PktModifier (void)
{
CPU_INT08U mod;
mod = PROBE_COM_MODIFIER_NONE;
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
if (ProbeCom_StrRdy() == DEF_TRUE) {
DEF_BIT_SET(mod, PROBE_COM_MODIFIER_STR_HAVE);
}
#endif
return (mod);
}
/*
*********************************************************************************************************
* ProbeCom_CmdErr()
*
* Description : Formulate error response when the target receives a request it cannot handle.
*
* Argument(s) : ptx_buf Pointer to the transmit buffer
*
* comm_err Error that occurred
*
* Return(s) : The number of bytes written to the tx buffer.
*
* Caller(s) : Various command handlers (ProbeCom_Cmd????()).
*
* Note(s) : (1) The TX format:
*
* (A) A 2-byte format , indicating the data segment format;
* (B) A 1-byte constant, PROBE_RS232_OK, if the location can be read;
* A 1-byte constant, PROBE_RS232_FAIL, if the location cannot be read.
* (C) A 1-byte modifier.
*
* +-------------------------+------------+------------+
* | Format | Status | Modifier |
* +-------------------------+------------+------------+
*********************************************************************************************************
*/
static CPU_INT16U ProbeCom_CmdErr (CPU_INT08U *ptx_buf,
CPU_INT08U comm_err)
{
#if (PROBE_COM_STAT_EN == DEF_ENABLED)
ProbeCom_ErrPktCtr++;
#endif
ProbeCom_StoINT16U(&ptx_buf, PROBE_COM_FMT_TX_ERROR); /* TX pkt format. */
ProbeCom_StoINT8U( &ptx_buf, comm_err); /* Target status. */
ProbeCom_StoINT8U( &ptx_buf, ProbeCom_PktModifier()); /* Modifier. */
return (PROBE_COM_SIZE_TX_HDR); /* Rtn TX data segment size = 4 (= Tx hdr size). */
}
/*
*********************************************************************************************************
* ProbeCom_CmdQuery()
*
* Description : Parse the FMT_QUERY command & formulate response. This command asks the target
* about its capabilities, which are returned in response.
*
* Argument(s) : prx_buf Pointer to the receive buffer
*
* ptx_buf Pointer to the transmit buffer
*
* rx_pkt_size Size of the receive packet
*
* tx_buf_size Size of the transmit buffer
*
* Return(s) : The number of bytes written to the tx buffer.
*
* Caller(s) : ProbeCom_ParseRxPkt().
*
* Note(s) : (1) The RX format:
*
* (A) A 2-byte format, indicating the data segment format;
* (B) A 2-byte query , indicating the query type.
*
* +-------------------------+-------------------------+
* | Format | Query |
* +-------------------------+-------------------------+
*
* (2) The TX format:
*
* (A) A 2-byte format, indicating the data segment format;
* (B) A 1-byte status, indicating the status after the request;
* (C) A 1-byte modifier;
* (E) A n-byte answer, the answer to the query.
*
* +-------------------------+------------+------------+
* | Format | Status | Modifier |
* +-------------------------+------------+------------+
* | Answer |
* | . |
* | . |
* | . |
* +---------------------------------------------------+
*
*********************************************************************************************************
*/
static CPU_INT16U ProbeCom_CmdQuery (CPU_INT08U *prx_buf,
CPU_INT08U *ptx_buf,
CPU_INT16U rx_pkt_size,
CPU_INT16U tx_buf_size)
{
CPU_INT16U query;
CPU_INT16U nbytes;
CPU_INT32U version;
query = 0;
nbytes = 0;
/* ------------------- CHK PKT SIZE ------------------- */
/* Expected size = 2 (= Rx header size) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -