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

📄 ui_ethernet.c

📁 Luminary Micro BLDC motor control software
💻 C
📖 第 1 页 / 共 5 页
字号:
//
//! Running count updated by the UI System Tick Handler for milliseconds.
//
//*****************************************************************************
volatile unsigned long g_ulEthernetTimer = 0;

//*****************************************************************************
//
//! Flag to indicate that a Real Time Data update is ready for transmission.
//
//*****************************************************************************
static tBoolean g_bSendRealTimeData = false;

//*****************************************************************************
//
//! This global is used to store the number of Ethernet messages that have been
//! received since power-up.
//
//*****************************************************************************
volatile unsigned long g_ulEthernetRXCount = 0;

//*****************************************************************************
//
//! This global is used to store the number of Ethernet messages that have been
//! transmitted since power-up.
//
//*****************************************************************************
volatile unsigned long g_ulEthernetTXCount = 0;

//*****************************************************************************
//
//! Counter for TCP connection timeout.
//
//*****************************************************************************
static unsigned long g_ulConnectionTimeout = 0;

//*****************************************************************************
//
//! Timeout value for TCP connection timeout timer.
//
//*****************************************************************************
volatile unsigned long g_ulConnectionTimeoutParameter = 0;

//*****************************************************************************
//
//! Close an existing Ethernet connection.
//!
//! \param pcb is the pointer to the TCP control structure.
//! 
//! This function is called when the the TCP connection should be closed.
//!
//! \return None.
//
//*****************************************************************************
static void
UIEthernetClose(struct tcp_pcb *pcb)
{
    //
    // Clear out all of the TCP callbacks.
    //
    tcp_arg(pcb, NULL);
    tcp_sent(pcb, NULL);
    tcp_recv(pcb, NULL);
    tcp_err(pcb, NULL);
    tcp_poll(pcb, NULL, 1);

    //
    // Clear the telnet data structure pointer, to indicate that
    // we are no longer connected.
    //
    g_psTelnetPCB = NULL;

    //
    // Close the TCP connection.
    //
    tcp_close(pcb);
}

//*****************************************************************************
//
//! Transmits a packet to the Ethernet controller.
//!
//! \param pucBuffer is a pointer to the packet to be transmitted.
//!
//! This function will send a packet via TCP/Ethernet.  It will compute the
//! checksum of the packet (based on the length in the second byte) and place
//! it at the end of the packet before sending the packet.
//!
//! \return Returns \b true if the entire packet was transmitted and \b false
//! if not.
//
//*****************************************************************************
static tBoolean
UIEthernetTransmit(unsigned char *pucBuffer)
{
    unsigned long ulIdx, ulSum, ulLength;
    err_t err;

    //
    // Compute the checksum for this packet and put it at the end.
    //
    for(ulIdx = 0, ulSum = 0, ulLength = pucBuffer[1]; ulIdx < (ulLength - 1);
        ulIdx++)
    {
        ulSum -= pucBuffer[ulIdx];
    }
    pucBuffer[ulLength - 1] = ulSum;

    //
    // Transmit the packet and flush the buffer to get the packet out
    // immediately.
    //
    err = tcp_write(g_psTelnetPCB, pucBuffer, ulLength, 1);
    if(err == ERR_OK)
    {
        g_ulEthernetTXCount++;
        err = tcp_output(g_psTelnetPCB);
    }

    //
    // Return an error if there are characters to be sent that would not fit
    // into the transmit buffer.
    //
    if(err != ERR_OK)
    {
        return(false);
    }
    else
    {
        return(true);
    }
}

//*****************************************************************************
//
//! Finds a parameter by ID.
//!
//! \param ucID is the ID of the parameter to locate.
//!
//! This function searches the list of parameters looking for one that matches
//! the provided ID.
//!
//! \return Returns the index of the parameter found, or 0xffff.ffff if the
//! parameter does not exist in the parameter list.
//
//*****************************************************************************
static unsigned long
UIEthernetFindParameter(unsigned char ucID)
{
    unsigned long ulIdx;

    //
    // Loop through the parameters.
    //
    for(ulIdx = 0; ulIdx < g_ulUINumParameters; ulIdx++)
    {
        //
        // See if this parameter matches the search ID.
        //
        if(g_sUIParameters[ulIdx].ucID == ucID)
        {
            //
            // Since this parameter matches, return its index.
            //
            return(ulIdx);
        }
    }

    //
    // The parameter could not be located, so return a failure.
    //
    return(0xffffffff);
}

//*****************************************************************************
//
//! Performs range checking on the value of a parameter.
//!
//! \param ulIdx is the index of the parameter to check.
//!
//! This function will perform range checking on the value of a parameter,
//! adjusting the parameter value if necessary to make it reside within the
//! predetermined range.
//!
//! \return None.
//
//*****************************************************************************
static void
UIEthernetRangeCheck(unsigned long ulIdx)
{
    unsigned long *pulValue;
    unsigned short *pusValue;
    long *plValue, lCheck;
    short *psValue, sCheck;
    char *pcValue, cCheck;

    //
    // See if a range exists for this parameter.  If not, then return without
    // checking the parameter's value.
    //
    if(((g_sUIParameters[ulIdx].ulMin == 0) &&
        (g_sUIParameters[ulIdx].ulMax == 0)) ||
       (g_sUIParameters[ulIdx].ucSize > 4))
    {
        return;
    }

    //
    // See if the value of this parameter is signed or unsigned.
    //
    if(g_sUIParameters[ulIdx].ulMin > g_sUIParameters[ulIdx].ulMax)
    {
        //
        // This is a signed parameter.  Determine the size of the parameter.
        //
        switch(g_sUIParameters[ulIdx].ucSize)
        {
            //
            // This parameter is a signed character.
            //
            case 1:
            {
                //
                // Get the signed value of this parameter.
                //
                pcValue = (char *)(g_sUIParameters[ulIdx].pucValue);

                //
                // Clamp the parameter value to the minimum if it is below the
                // minimum.
                //
                cCheck = (char)g_sUIParameters[ulIdx].ulMin;
                if(*pcValue < cCheck)
                {
                    *pcValue = cCheck;
                }

                //
                // Clamp the parameter value to the maximum if it is above the
                // maximum.
                //
                cCheck = (char)g_sUIParameters[ulIdx].ulMax;
                if(*pcValue > cCheck)
                {
                    *pcValue = cCheck;
                }

                //
                // This parameter value has been handled.
                //
                break;
            }

            //
            // This parameter is a signed short.
            //
            case 2:
            {
                //
                // Get the signed value of this parameter.
                //
                psValue = (short *)(g_sUIParameters[ulIdx].pucValue);

                //
                // Clamp the parameter value to the minimum if it is below the
                // minimum.
                //
                sCheck = (short)g_sUIParameters[ulIdx].ulMin;
                if(*psValue < sCheck)
                {
                    *psValue = sCheck;
                }

                //
                // Clamp the parameter value to the maximum if it is above the
                // maximum.
                //
                sCheck = (short)g_sUIParameters[ulIdx].ulMax;
                if(*psValue > sCheck)
                {
                    *psValue = sCheck;
                }

                //
                // This parameter value has been handled.
                //
                break;
            }

            //
            // This parameter is a signed long.
            //
            case 4:
            default:
            {
                //
                // Get the signed value of this parameter.
                //
                plValue = (long *)(g_sUIParameters[ulIdx].pucValue);

                //
                // Clamp the parameter value to the minimum if it is below the
                // minimum.
                //
                lCheck = (long)g_sUIParameters[ulIdx].ulMin;
                if(*plValue < lCheck)
                {
                    *plValue = lCheck;
                }

                //
                // Clamp the parameter value to the maximum if it is above the
                // maximum.
                //
                lCheck = (long)g_sUIParameters[ulIdx].ulMax;
                if(*plValue > lCheck)
                {
                    *plValue = lCheck;
                }

                //
                // This parameter value has been handled.
                //
                break;
            }
        }
    }
    else
    {
        //
        // This is an unsigned parameter.  Determine the size of the parameter.
        //
        switch(g_sUIParameters[ulIdx].ucSize)
        {
            //
            // This parameter is an unsigned character.
            //
            case 1:
            {
                //
                // Clamp the parameter value to the minimum if it is below the
                // minimum.
                //
                if(g_sUIParameters[ulIdx].pucValue[0] <
                   g_sUIParameters[ulIdx].ulMin)
                {
                    g_sUIParameters[ulIdx].pucValue[0] =
                        g_sUIParameters[ulIdx].ulMin;
                }

                //
                // Clamp the parameter value to the maximum if it is above the
                // maximum.
                //
                if(g_sUIParameters[ulIdx].pucValue[0] >
                   g_sUIParameters[ulIdx].ulMax)
                {
                    g_sUIParameters[ulIdx].pucValue[0] =
                        g_sUIParameters[ulIdx].ulMax;
                }

                //
                // This parameter value has been handled.
                //
                break;
            }

            //
            // This parameter is an unsigned short.
            //
            case 2:
            {
                //
                // Get the value of this parameter.
                //
                pusValue = (unsigned short *)(g_sUIParameters[ulIdx].pucValue);

                //
                // Clamp the parameter value to the minimum if it is below the
                // minimum.
                //
                if(*pusValue < g_sUIParameters[ulIdx].ulMin)
                {
                    *pusValue = g_sUIParameters[ulIdx].ulMin;
                }

                //
                // Clamp the parameter value to the maximum if it is above the
                // maximum.
                //
                if(*pusValue > g_sUIParameters[ulIdx].ulMax)
                {
                    *pusValue = g_sUIParameters[ulIdx].ulMax;
                }

                //
                // This parameter value has been handled.
                //
                break;
            }

            //
            // This parameter is an unsigned long.
            //
            case 4:
            default:
            {
                //
                // Get the value of this parameter.
                //

⌨️ 快捷键说明

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