📄 probe_com.c
字号:
/*
*********************************************************************************************************
*********************************************************************************************************
** Static String-Handling Function
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* Check if a String Is Ready
*
* Description: This routine is called to check if a string is ready for transmission.
*
* Argument(s): None
*
* Returns : DEF_TRUE if a string is in the buffer for transmission.
* DEF_FALSE if no string is in the buffer for transmission.
*
* 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_INT32U wr_ix;
CPU_INT32U rd_ix;
wr_ix = ProbeComStrBufWrIx;
rd_ix = ProbeComStrBufRdIx;
if (wr_ix == rd_ix) {
rdy = DEF_FALSE;
} else {
rdy = DEF_TRUE;
}
return (rdy);
}
#endif
/*
*********************************************************************************************************
*********************************************************************************************************
** Static Packet-Handling Functions
*********************************************************************************************************
*********************************************************************************************************
*/
static CPU_INT08U ProbeCom_PktModifier (void)
{
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
if (ProbeCom_StrRdy() == DEF_TRUE) {
return (PROBE_COM_MODIFIER_STR_HAVE);
} else {
return (PROBE_COM_MODIFIER_NONE);
}
#else
return (PROBE_COM_MODIFIER_NONE);
#endif
}
/*
*********************************************************************************************************
* Send an FMT_ERROR Command
*
* Description: This routine is called to send an error response when the target receives a request it
* cannot handle.
*
* Argument(s): tx_buf is a pointer to the transmit buffer
*
* com_error is the error that occurred
*
* Returns : The number of bytes written to the tx buffer.
*
* Note(s) : (1) The TX format:
* (A) A 2-byte format , indicating the data segment format; AND
* (B) A 1-byte constant, PROBE_RS232_OK, if the location can be read; OR
* 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_CmdError (CPU_INT08U *tx_buf,
CPU_INT16U com_error)
{
#if (PROBE_COM_STAT_EN == DEF_ENABLED)
ProbeCom_ErrPktCtr++;
#endif
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_TX_ERROR); /* (a) TX packet format */
ProbeCom_StoINT8U( &tx_buf, com_error); /* (b) Target status */
ProbeCom_StoINT8U( &tx_buf, ProbeCom_PktModifier()); /* (c) Modifier */
return (PROBE_COM_SIZE_TX_HEADER); /* (d) Return TX data segment size */
/* = 4 (= Tx header size) */
}
/*
*********************************************************************************************************
* Respond to FMT_QUERY Command
*
* Description: This routine is called to parse the FMT_QUERY command. This command asks the target
* about its capabilities, which are returned in response.
*
* Argument(s): rx_buf is a pointer to the receive buffer
* tx_buf is a pointer to the transmit buffer
* rx_pkt_sz is the size of the receive packet
* tx_buf_sz is the size of the transmit buffer
*
* Returns : The number of bytes written to the tx buffer.
*
* Note(s) : (1) The RX format:
*
* (A) A 2-byte format , indicating the data segment format; AND
* (B) A 2-byte query , indicating the length of the remaining data.
*
* +-------------------------+-------------------------+
* | Format | Query |
* +-------------------------+-------------------------+
*
* (2) The TX format:
* (A) A 2-byte format , indicating the data segment format; AND
* (B) A 1-byte status , indicating the status after the request; AND
* (C) A 1-byte modifier; AND
* (E) A n-byte answer , the answer to the query.
*
* +-------------------------+------------+------------+
* | Format | Status | Modifier |
* +-------------------------+------------+------------+
* | Answer |
* | . |
* | . |
* | . |
* +---------------------------------------------------+
*
*********************************************************************************************************
*/
static CPU_INT16U ProbeCom_CmdQuery (CPU_INT08U *rx_buf,
CPU_INT08U *tx_buf,
CPU_INT16U rx_pkt_sz,
CPU_INT16U tx_buf_sz)
{
CPU_INT16U query;
CPU_INT16U nbytes;
CPU_INT08U error;
query = 0;
nbytes = 0;
error = PROBE_COM_STATUS_OK;
if (rx_pkt_sz == 4) { /* If RX data segment is the expected size */
/* = 2 (= Rx header size) */
/* + 2 (= Query ) */
query = ProbeCom_GetINT16U(&rx_buf); /* (a) Read the query */
switch (query) {
case PROBE_COM_QUERY_MAX_RX_SIZE:
case PROBE_COM_QUERY_MAX_TX_SIZE:
case PROBE_COM_QUERY_FMT_SUPPORT:
break;
default:
error = PROBE_COM_STATUS_QUERY_NOT_SUPPORTED;
break;
}
} else { /* If RX data segment is NOT the expected size */
error = PROBE_COM_STATUS_RX_PKT_WRONG_SIZE;
}
if (error != PROBE_COM_STATUS_OK) { /* Error */
return (ProbeCom_CmdError(tx_buf, error));
}
/* No error */
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_TX_QUERY); /* (a) TX packet format */
ProbeCom_StoINT8U( &tx_buf, PROBE_COM_STATUS_OK); /* (b) Target status */
ProbeCom_StoINT8U( &tx_buf, ProbeCom_PktModifier()); /* (c) Modifier */
/* (d) Save TX data segment data */
switch (query) {
case PROBE_COM_QUERY_MAX_RX_SIZE:
ProbeCom_StoINT32U(&tx_buf, PROBE_COM_RX_MAX_SIZE);
nbytes = 4;
break;
case PROBE_COM_QUERY_MAX_TX_SIZE:
ProbeCom_StoINT32U(&tx_buf, PROBE_COM_TX_MAX_SIZE);
nbytes = 4;
break;
case PROBE_COM_QUERY_FMT_SUPPORT:
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_RX_QUERY );
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_RX_SIMPLE_RD );
nbytes = 4;
#if (PROBE_COM_SUPPORT_WR == DEF_TRUE)
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_RX_SIMPLE_WR );
nbytes += 2;
#endif
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_RX_MULTIPLE_RD);
nbytes += 2;
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
ProbeCom_StoINT16U(&tx_buf, PROBE_COM_FMT_RX_STR_GET);
nbytes += 2;
#endif
break;
default:
break;
}
return (nbytes + PROBE_COM_SIZE_TX_HEADER); /* (e) Return TX data segment size */
/* = nbytes (= Tx data size) */
/* + 4 (= Tx header size) */
}
/*
*********************************************************************************************************
* Respond to FMT_SIMPLE_RD Request
*
* Description: This routine is called to parse the FMT_SIMPLE_RD request. This command causes the target
* to send data read from its memory for a certain {memory address, data length} pair (which
* is given in the request).
*
* Argument(s): rx_buf is a pointer to the receive buffer
* tx_buf is a pointer to the transmit buffer
* rx_pkt_sz is the size of the receive packet
* tx_buf_sz is the size of the transmit buffer
*
* Returns : The number of bytes written to the tx buffer.
*
* Note(s) : (1) The RX format:
* (A) A 2-byte format , indicating the data segment format; AND
* (B) A 2-byte length , indicating the number of bytes to read; AND
* (C) A 4-byte address , the starting address of the data to read.
*
* +-------------------------+-------------------------+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -