⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probe_com.c

📁 官方的UCOSii的移植文件
💻 C
📖 第 1 页 / 共 5 页
字号:

                                                                    /* ---------------- READ FROM RX PACKET --------------- */
static  CPU_INT08U  ProbeCom_GetINT8U     (CPU_INT08U **buf);

static  CPU_INT16U  ProbeCom_GetINT16U    (CPU_INT08U **buf);

#if ((!defined(CPU_CFG_ADDR_SIZE)) || \
     ((defined(CPU_CFG_ADDR_SIZE)) && \
              (CPU_CFG_ADDR_SIZE   != CPU_WORD_SIZE_16)))
static  CPU_INT32U  ProbeCom_GetINT32U    (CPU_INT08U **buf);
#endif

                                                                    /* ----------------- WRITE TO TX BUFFER --------------- */
static  void        ProbeCom_StoINT8U     (CPU_INT08U **buf,
                                           CPU_INT08U   data);

static  void        ProbeCom_StoINT16U    (CPU_INT08U **buf,
                                           CPU_INT16U   data);

#if 0
static  void        ProbeCom_StoINT32U    (CPU_INT08U **buf,
                                           CPU_INT32U   data);
#endif

                                                                    /* ------ DETERMINE IF STRING IS IN STRING BUFFER ----- */
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
static  CPU_BOOLEAN ProbeCom_StrRdy       (void);
#endif


/*
*********************************************************************************************************
*                                     LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*********************************************************************************************************
**                                          Global Functions
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                           ProbeCom_Init()
*
* Description : Initialize the module.
*
* Argument(s) : none.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  ProbeCom_Init (void)
{
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
    ProbeComStrBufWrIx = 0;
    ProbeComStrBufRdIx = 0;

    ProbeCom_OS_Init();
#endif

#if (PROBE_COM_STAT_EN == DEF_ENABLED)
    ProbeCom_RxPktCtr     = 0;
    ProbeCom_TxPktCtr     = 0;
    ProbeCom_TxSymByteCtr = 0;
    ProbeCom_TxSymCtr     = 0;
    ProbeCom_ErrPktCtr    = 0;

#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
    ProbeCom_TxStrCtr     = 0;
#endif
#endif

    ProbeComEndiannessTest = 0x12345678L;
}


/*
*********************************************************************************************************
*                                         ProbeCom_ParseRxPkt()
*
* Description : Parse a packet & formulate a response.
*
* Argument(s) : rx_pkt      Pointer to the receive  packet buffer
*
*               tx_pkt      Pointer to the transmit packet buffer
*
*               rx_pkt_sz   Size of the received packet
*
*               tx_pkt_sz   Size of the transmit packet buffer
*
* Return(s)   : The number of bytes in the data segment of the packet to transmit in response.
*********************************************************************************************************
*/

CPU_INT16U  ProbeCom_ParseRxPkt (void        *rx_pkt,
                                 void        *tx_pkt,
                                 CPU_INT16U   rx_pkt_sz,
                                 CPU_INT16U   tx_buf_sz)
{
    CPU_INT16U   tx_buf_wr;
    CPU_INT16U   format;
    CPU_INT08U  *rx_buf;
    CPU_INT08U  *tx_buf;


    if (rx_pkt_sz < 2) {
        return (0);
    }

    rx_buf  = (CPU_INT08U *)rx_pkt;
    tx_buf  = (CPU_INT08U *)tx_pkt;
    format  = (rx_buf[1] << 8) + rx_buf[0];
    rx_buf += 2;

#if (PROBE_COM_STAT_EN == DEF_ENABLED)
    ProbeCom_RxPktCtr++;
    ProbeCom_TxPktCtr++;
#endif

    switch (format) {
        case PROBE_COM_FMT_RX_QUERY:
             tx_buf_wr = ProbeCom_CmdQuery(     rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;

        case PROBE_COM_FMT_RX_SIMPLE_RD:
             tx_buf_wr = ProbeCom_CmdSimpleRd(  rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;

#if (PROBE_COM_SUPPORT_WR == DEF_TRUE)
        case PROBE_COM_FMT_RX_SIMPLE_WR:
             tx_buf_wr = ProbeCom_CmdSimpleWr(  rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;
#endif

        case PROBE_COM_FMT_RX_MULTIPLE_RD:
             tx_buf_wr = ProbeCom_CmdMultipleRd(rx_buf, tx_buf,  rx_pkt_sz, tx_buf_sz);
             break;

#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
        case PROBE_COM_FMT_RX_STR_GET:
             tx_buf_wr = ProbeCom_CmdStrGet(    rx_buf, tx_buf,  rx_pkt_sz, tx_buf_sz);
             break;
#endif

        default:
             tx_buf_wr = ProbeCom_CmdError(tx_buf, PROBE_COM_STATUS_UNKNOWN_REQUEST);
             break;
    }

    return (tx_buf_wr);
}


/*
*********************************************************************************************************
*                                    ProbeCom_TxStr()
*
* Description : Append a string in the string buffer.
*
* Argument(s) : s           Pointer to the string to send.
*
*               dly         Delay time (in milliseconds).  If this value is zero, then
*                           the function will return after queueing in the buffer the portion that fits
*                           immediately.  Otherwise, the function will delay for a certain number of
*                           milliseconds until the entire string has been queued in the buffer.
*
* Return(s)   : DEF_TRUE   if the entire string was queued in the buffer.
*               DEF_FALSE  if the entire string could not be queued in the buffer.
*
* 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
*                   (ProbeComStrBufWrIx); only the task or ISR which forms the packets should modify the
*                   global current read index (ProbeComStrBufRdIx).
*
*               (2) The global current write index (ProbeComStrBufWrIx) is the index of the next location
*                   in the buffer to write.  The global current read index (ProbeComStrBufRdIx) 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
*
*                                        ProbeComStrBufWrIx == ProbeComStrBufRdIx
*
*                   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    *s,
                             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(s);                                   /* Determine length of the string (without NULL byte    */

    while (DEF_TRUE) {
        if (len == 0) {                                             /* If entire string has been placed in buffer           */
            ProbeCom_OS_Post();
            return (DEF_TRUE);                                      /* ... Return DEF_TRUE to indicate success.             */
        }

        rd_ix = ProbeComStrBufRdIx;
        wr_ix = ProbeComStrBufWrIx;

        if (rd_ix > wr_ix) {                                        /* If rd_ix > wr_ix, store string into                  */
                                                                    /*           buffer locations [wr_ix, rd_ix - 1)        */
            nbytes_free = rd_ix - wr_ix - 1;

        } else {
            if (rd_ix == 0) {                                       /* If rd_ix <= wr_ix && rd_ix == 0, store string at     */
                                                                    /*           buffer locations [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 string at     */
                                                                    /*           buffer locations [wr_ix, end_ix)           */

                nbytes_free = PROBE_COM_STR_BUF_SIZE - wr_ix;
            }
        }

        if (nbytes_free == 0) {                                     /* If the buffer is full                                */
            if (dly == 0) {                                         /* (a) Return if dly = 0                                */
                ProbeCom_OS_Post();
                return (DEF_FALSE);
            } else {                                                /* (b) Call OS function to delay and continue           */
                ProbeCom_OS_Dly(dly);
                continue;
            }
        }

        if (nbytes_free > len) {                                    /* If string is shorter than free space                 */
            nbytes_wr = len;
        } else {
            nbytes_wr = nbytes_free;
        }

        wr_ix_n = wr_ix + nbytes_wr;                                /* Assign write index after write                       */

        if (wr_ix_n == PROBE_COM_STR_BUF_SIZE) {                    /* Wrap buffer index around                             */
            wr_ix_n = 0;
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -