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

📄 ui_ethernet.c

📁 Luminary Micro BLDC motor control software
💻 C
📖 第 1 页 / 共 5 页
字号:
                pulValue = (unsigned long *)(g_sUIParameters[ulIdx].pucValue);

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

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

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

//*****************************************************************************
//
//! Scans for packets in the receive buffer.
//!
//! This function will scan through g_pucUIEthernetReceive looking for valid
//! command packets.  When found, the command packets will be handled.
//!
//! \return None.
//
//*****************************************************************************
static void
UIEthernetScanReceive(void)
{
    unsigned char ucSum, ucSize;
    unsigned long ulIdx;

    //
    // Loop while there is data in the receive buffer.
    //
    while(g_ulUIEthernetReceiveRead != g_ulUIEthernetReceiveWrite)
    {
        //
        // See if this character is the tag for the start of a command packet.
        //
        if(g_pucUIEthernetReceive[g_ulUIEthernetReceiveRead] != TAG_CMD)
        {
            //
            // Skip this character.
            //
            g_ulUIEthernetReceiveRead =
                (g_ulUIEthernetReceiveRead + 1) % UIETHERNET_MAX_RECV;

            //
            // Keep scanning for a start of command packet tag.
            //
            continue;
        }

        //
        // See if there are additional characters in the receive buffer.
        //
        if(((g_ulUIEthernetReceiveRead + 1) % UIETHERNET_MAX_RECV) ==
           g_ulUIEthernetReceiveWrite)
        {
            //
            // There are no additional characters in the receive buffer after
            // the start of command packet byte, so stop scanning for now.
            //
            break;
        }

        //
        // See if the packet size byte is valid.  A command packet must be at
        // least four bytes and can not be larger than the receive buffer size.
        //
        ucSize = g_pucUIEthernetReceive[(g_ulUIEthernetReceiveRead + 1) %
                                      UIETHERNET_MAX_RECV];
        if((ucSize < 4) || (ucSize > (UIETHERNET_MAX_RECV - 1)))
        {
            //
            // The packet size is too large, so either this is not the start of
            // a packet or an invalid packet was received.  Skip this start of
            // command packet tag.
            //
            g_ulUIEthernetReceiveRead =
                (g_ulUIEthernetReceiveRead + 1) % UIETHERNET_MAX_RECV;

            //
            // Keep scanning for a start of command packet tag.
            //
            continue;
        }

        //
        // Determine the number of bytes in the receive buffer.
        //
        ulIdx = g_ulUIEthernetReceiveWrite - g_ulUIEthernetReceiveRead;
        if(g_ulUIEthernetReceiveRead > g_ulUIEthernetReceiveWrite)
        {
            ulIdx += UIETHERNET_MAX_RECV;
        }

        //
        // If the entire command packet is not in the receive buffer then stop
        // scanning for now.
        //
        if(ulIdx < ucSize)
        {
            break;
        }

        //
        // The entire command packet is in the receive buffer, so compute its
        // checksum.
        //
        for(ulIdx = 0, ucSum = 0; ulIdx < ucSize; ulIdx++)
        {
            ucSum += g_pucUIEthernetReceive[
                (g_ulUIEthernetReceiveRead + ulIdx) % UIETHERNET_MAX_RECV];
        }

        //
        // Skip this packet if the checksum is not correct (that is, it is
        // probably not really the start of a packet).
        //
        if(ucSum != 0)
        {
            //
            // Skip this character.
            //
            g_ulUIEthernetReceiveRead =
                (g_ulUIEthernetReceiveRead + 1) % UIETHERNET_MAX_RECV;

            //
            // Keep scanning for a start of command packet tag.
            //
            continue;
        }

        //
        // A valid command packet was received, so process it now.
        //
        switch(g_pucUIEthernetReceive[(g_ulUIEthernetReceiveRead + 2) %
                                    UIETHERNET_MAX_RECV])
        {
            //
            // The command to get the target type.
            //
            case CMD_ID_TARGET:
            {
                //
                // Fill in the response.
                //
                g_pucUIEthernetResponse[0] = TAG_STATUS;
                g_pucUIEthernetResponse[1] = 0x05;
                g_pucUIEthernetResponse[2] = CMD_ID_TARGET;
                g_pucUIEthernetResponse[3] = g_ulUITargetType;

                //
                // Send the response.
                //
                UIEthernetTransmit(g_pucUIEthernetResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to upgrade the firmware.
            //
            case CMD_UPGRADE:
            {
                //
                // Clear out all of the TCP callbacks.
                //
                tcp_arg(g_psTelnetPCB, NULL);
                tcp_sent(g_psTelnetPCB, NULL);
                tcp_recv(g_psTelnetPCB, NULL);
                tcp_err(g_psTelnetPCB, NULL);
                tcp_poll(g_psTelnetPCB, NULL, 1);

                //
                // Close the TCP connection.
                //
                tcp_abort(g_psTelnetPCB);

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

                //
                // Pass the upgrade request to the application.
                //
                UIUpgrade();

                //
                // Control should never return here, but enter an infinite loop
                // just in case it does.
                //
                while(1)
                {
                }
            }

            //
            // The command to get a list of the parameters.
            //
            case CMD_GET_PARAMS:
            {
                //
                // Fill in the response.
                //
                g_pucUIEthernetResponse[0] = TAG_STATUS;
                g_pucUIEthernetResponse[1] = g_ulUINumParameters + 4;
                g_pucUIEthernetResponse[2] = CMD_GET_PARAMS;
                for(ulIdx = 0; ulIdx < g_ulUINumParameters; ulIdx++)
                {
                    g_pucUIEthernetResponse[ulIdx + 3] =
                        g_sUIParameters[ulIdx].ucID;
                }

                //
                // Send the response.
                //
                UIEthernetTransmit(g_pucUIEthernetResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to get a description of a parameter.
            //
            case CMD_GET_PARAM_DESC:
            {
                //
                // Find the parameter.
                //
                ucSum =
                    g_pucUIEthernetReceive[(g_ulUIEthernetReceiveRead + 3) %
                                         UIETHERNET_MAX_RECV];
                ulIdx = UIEthernetFindParameter(ucSum);

                //
                // Fill in the response.
                //
                g_pucUIEthernetResponse[0] = TAG_STATUS;
                g_pucUIEthernetResponse[2] = CMD_GET_PARAM_DESC;

                //
                // If a parameter was not specified, or if the parameter could
                // not be found, then return a zero length.
                //
                if((ucSize != 5) || (ulIdx == 0xffffffff))
                {
                    g_pucUIEthernetResponse[1] = 0x05;
                    g_pucUIEthernetResponse[3] = 0x00;
                }

                //
                // If the length of the parameter is greater than a 32-bit
                // value, then return just the size of the parameter.
                //
                else if(g_sUIParameters[ulIdx].ucSize > 4)
                {
                    g_pucUIEthernetResponse[1] = 0x05;
                    g_pucUIEthernetResponse[3] = g_sUIParameters[ulIdx].ucSize;
                }

                //
                // Otherwise, return the size, minimum, maximum, and step size
                // for the parameter.
                //
                else
                {
                    //
                    // Set the size of the response packet.
                    //
                    g_pucUIEthernetResponse[1] =
                        (g_sUIParameters[ulIdx].ucSize * 3) + 5;

                    //
                    // Set the size of the parameter value.
                    //
                    g_pucUIEthernetResponse[3] = g_sUIParameters[ulIdx].ucSize;

                    //
                    // Loop through the bytes of the parameter value.
                    //
                    ucSize = g_sUIParameters[ulIdx].ucSize;
                    for(ucSum = 0; ucSum < ucSize; ucSum++)
                    {
                        //
                        // Set this byte of the parameter minimum value.
                        //
                        g_pucUIEthernetResponse[ucSum + 4] =
                            ((g_sUIParameters[ulIdx].ulMin >> (ucSum * 8)) &
                             0xff);

                        //
                        // Set this byte of the parameter maximum value.
                        //
                        g_pucUIEthernetResponse[ucSum + ucSize + 4] =
                            ((g_sUIParameters[ulIdx].ulMax >> (ucSum * 8)) &
                             0xff);

                        //
                        // Set this byte of the parameter step size.
                        //
                        g_pucUIEthernetResponse[ucSum + (ucSize << 1) + 4] =
                            ((g_sUIParameters[ulIdx].ulStep >> (ucSum * 8)) &
                             0xff);
                    }
                }

                //
                // Send the response.
                //
                UIEthernetTransmit(g_pucUIEthernetResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to get the value of a parameter.
            //
            case CMD_GET_PARAM_VALUE:
            {
                //
                // Find the parameter.
                //
                ucSum =
                    g_pucUIEthernetReceive[(g_ulUIEthernetReceiveRead + 3) %
                                         UIETHERNET_MAX_RECV];
                ulIdx = UIEthernetFindParameter(ucSum);

                //
                // Fill in the response.
                //
                g_pucUIEthernetResponse[0] = TAG_STATUS;
                g_pucUIEthernetResponse[2] = CMD_GET_PARAM_VALUE;

                //
                // If a parameter was not specified, or if the parameter could
                // not be found, then return no value.
                //
                if((ucSize != 5) || (ulIdx == 0xffffffff))
                {
                    g_pucUIEthernetResponse[1] = 0x04;
                }

                //
                // Return the current value of the parameter.
                //
                else
                {
                    //
                    // Set the response packet size based on the size of the
                    // parameter.
                    //
                    g_pucUIEthernetResponse[1] =
                        g_sUIParameters[ulIdx].ucSize + 4;

                    //
                    // Copy the parameter value to the response packet.
                    //
                    for(ucSum = 0; ucSum < g_sUIParameters[ulIdx].ucSize;
                        ucSum++)
                    {
                        g_pucUIEthernetResponse[ucSum + 3] =
                            g_sUIParameters[ulIdx].pucValue[ucSum];
                    }
                }

                //
                // Send the response.
                //
                UIEthernetTransmit(g_pucUIEthernetResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to set the value of a parameter.
            //
            case CMD_SET_PARAM_VALUE:
            {
                //
                // Find the parameter.

⌨️ 快捷键说明

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