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

📄 probe_com.c

📁 uCOS-II example for MC9S12DPxxx
💻 C
📖 第 1 页 / 共 5 页
字号:

/*
*********************************************************************************************************
*                                            ProbeCom_TxStr()
*
* Description : Append a string in the string buffer.
*
* Argument(s) : pstr        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.
*
* 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 (DEF_TRUE) {
        if (len == 0) {                                         /* If entire str has been placed in buf ...             */
            ProbeCom_OS_Post();
            return (DEF_TRUE);                                  /* ... rtn TRUE to indicate success.                    */
        }

        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.                                         */
        }
    }
}
#endif


/*
*********************************************************************************************************
*                                         ProbeCom_TxTelemetry()
*
* Description : Queue telemetry data in the telemetry buffer.
*
* Argument(s) : id          ID which identifies the type of the data.  (See Notes #1.)
*
*               pdata       Pointer to buffer which holds the telemetry data.
*
*               nbr_octets  Number of data octets held in 'pdata'.
*
*               timestamp   Timestamp identifying generation time of data.
*
* Return(s)   : DEF_TRUE   if the telemetry data was          queued in the buffer.
*               DEF_FALSE  if the telemetry data could not be queued in the buffer.
*
* Caller(s)   : Application.
*
* Note(s)     : (1) This target code will NOT enforce any format on the ID.  In an attempt to
*                   anticipate expansion of this feature & prevent user-generated incompatibilities, the
*                   following format for the 4-byte ID is suggested :
*
*                   (a) Bytes 0-1 (lower 16-bits) should hold a unique global ID for this type of data.
*                       For example, an identifier might be allocated for OS task data or for a sensor
*                       reading.
*
*                       (1) IDs above 38400 should be treated as RESERVED.  These IDs may, in the
*                           future, be specified for special monitoring capabilities.
*
*                       (2) IDs below 38400 should be treated as available for user information.
*
*                   (b) Bytes 2-3 (upper 16-bits) should either be 0 (0x0000) or hold a local ID for
*                       objects of the global ID type.  For example, if the global ID matches the ID
*                       for OS task data, then the local ID may specified the task number, priority or
*                       ID (employed by the OS).
*
*               (2) CPU_CRITICAL_ENTER() & CPU_CRITICAL_EXIT() REQUIRED to prevent pool access from multiple
*                   tasks/contexts.  The task/context which adds telemetry data to the queue is one of
*                   these; the task/ISR which actually transmits that data is another.  If the latter is
*                   a task (NOT an ISR), then these COULD be replaced by a semaphore lock.
*
*               (3) New buffers are unlinked from the FRONT of the free pool before being filled with
*                   telemetry data.  Full buffers are linked to the BACK of the ready pool after being
*                   filled with telemetry data.  Buffers may be linked to the front of the ready pool
*                   upon partial transmission of a large buffer; see 'ProbeCom_CmdTelemetryGet()' Note #4.
*********************************************************************************************************
*/

#if (PROBE_COM_SUPPORT_TELEMETRY == DEF_TRUE)
CPU_BOOLEAN  ProbeCom_TxTelemetry (CPU_INT32U   id,
                                   void        *pdata,
                                   CPU_INT16U   nbr_octets,
                                   CPU_INT32U   timestamp)
{
    PROBE_COM_TELEMETRY_BUF  *pbuf;
#if (CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL)
    CPU_SR                    cpu_sr;
#endif


    CPU_CRITICAL_ENTER();
    pbuf = ProbeCom_TelemetryFreePoolPtr;
    if (pbuf == (PROBE_COM_TELEMETRY_BUF *)0) {
        CPU_CRITICAL_EXIT();
        return (DEF_FALSE);
    }
    ProbeCom_TelemetryFreePoolPtr = (PROBE_COM_TELEMETRY_BUF *)pbuf->NextBufPtr;
    ProbeCom_TelemetryBufNbrFree--;
    CPU_CRITICAL_EXIT();

    pbuf->ID        = id;
    pbuf->Timestamp = timestamp;
    pbuf->BufLen    = nbr_octets;
    pbuf->BufIx     = 0;

    Mem_Copy((void     *)pbuf->Buf,
             (void     *)pdata,
             (CPU_SIZE_T)nbr_octets);

    CPU_CRITICAL_ENTER();
    if (ProbeCom_TelemetryRdyPoolPtr == (PROBE_COM_TELEMETRY_BUF *)0) {
        pbuf->PrevBufPtr               = (void *)0;
        pbuf->NextBufPtr               = (void *)0;
        ProbeCom_TelemetryRdyPoolPtr = pbuf;
    } else {
        pbuf->PrevBufPtr                         = (void *)ProbeCom_TelemetryRdyPoolPtr;
        pbuf->NextBufPtr                         = (void *)0;
        ProbeCom_TelemetryRdyPoolPtr->NextBufPtr = pbuf;
    }
    ProbeCom_TelemetryBufNbrRdy++;
    CPU_CRITICAL_EXIT();

    return (DEF_TRUE);
}
#endif


/*
*********************************************************************************************************
*********************************************************************************************************
**                                  STATIC STRING-HANDLING 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_TelemetryRdy()
*
* Description : Check if telemetry data is ready for transmission.
*
* Argument(s) : none.
*
* Return(s)   : DEF_TRUE   if    telemetry data is queued for transmission.
*               DEF_FALSE  if no telemetry data is queued buffer for transmission.
*
* Caller(s)   : ProbeCom_PktModifier().
*
* Note(s)     : (1) See Notes for 'ProbeCom_TxTelemetry()'.
*********************************************************************************************************
*/

#if (PROBE_COM_SUPPORT_TELEMETRY == DEF_TRUE)
static  CPU_BOOLEAN  ProbeCom_TelemetryRdy (void)
{
    CPU_BOOLEAN  rdy;


    if (ProbeCom_TelemetryRdyPoolPtr != (PROBE_COM_TELEMETRY_BUF *)0) {
        rdy = DEF_FALSE;
    } else {
        rdy = DEF_TRUE;
    }

    return (rdy);
}

⌨️ 快捷键说明

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