📄 ui_ethernet.c
字号:
//*****************************************************************************
//
// ui_ethernet.c - A simple control interface utilizing Ethernet and the
// lwIP TCP/IP stack.
//
// Copyright (c) 2007-2008 Luminary Micro, Inc. All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws. All rights are reserved. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 716 of the BLDC motor application.
//
//*****************************************************************************
#include "../../DriverLib/hw_ints.h"
#include "../../DriverLib/hw_memmap.h"
#include "../../DriverLib/hw_nvic.h"
#include "../../DriverLib/hw_types.h"
#include "../../DriverLib/src/ethernet.h"
#include "../../DriverLib/src/gpio.h"
#include "../../DriverLib/src/flash.h"
#include "../../DriverLib/src/sysctl.h"
#include "commands.h"
#include "ui_ethernet.h"
#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include "lwip/stats.h"
#include "netif/etharp.h"
#include "luminaryif.h"
//*****************************************************************************
//
//! \page ui_ethernet_intro Introduction
//!
//! A generic, TCP packet-based protocol is utilized for communicating with
//! the motor drive board. This provides a method to control the motor drive,
//! adjust its parameters, and retrieve real-time performance data. .
//!
//! - The same protocol should be used for all motor drive boards,
//! regardless of the motor type (that is, AC induction, stepper, and
//! so on).
//! - The protocol should make reasonable attempts to protect against invalid
//! commands being acted upon.
//! - It should be possible to connect to a running motor drive board and lock
//! on to the real-time data stream without having to restart the data
//! stream.
//!
//! The code for handling the ethernet protocol is contained in
//! <tt>ui_ethernet.c</tt>, with <tt>ui_ethernet.h</tt> containing the
//! definitions for the structures, functions, and variables exported to the
//! remainder of the application. The file <tt>commands.h</tt> contains the
//! definitions for the commands, parameters, real-time data items, and
//! responses that are used in the ethernet protocol. The file
//! <tt>ui_common.h</tt> contains the definitions that are common to all of
//! the communications API modules that are supported by the motor RDKs.
//!
//! The ethernet module builds on the serial module in that the same message
//! format is used. This message is then transmitted/received using a TCP/IP
//! connection between the RDK board and the host application. The following
//! message formats are defined:
//!
//! \section cmf Command Message Format
//!
//! Commands are sent to the motor drive with the following format:
//!
//! \verbatim
//! {tag} {length} {command} {optional command data byte(s)} {checksum}
//! \endverbatim
//!
//! - The {tag} byte is 0xff.
//!
//! - The {length} byte contains the overall length of the command packet,
//! starting with the {tag} and ending with the {checksum}. The maximum
//! packet length is 255 bytes.
//!
//! - The {command} byte is the command being sent. Based on the command,
//! there may be optional command data bytes that follow.
//!
//! - The {checksum} byte is the value such that the sum of all bytes in the
//! command packet (including the checksum) will be zero. This is used to
//! validate a command packet and allow the target to synchronize with the
//! command stream being sent by the host.
//!
//! For example, the 0x01 command with no data bytes would be sent as
//! follows:
//!
//! \verbatim
//! 0xff 0x04 0x01 0xfc
//! \endverbatim
//!
//! And the 0x02 command with two data bytes (0xab and 0xcd) would be sent as
//! follows:
//!
//! \verbatim
//! 0xff 0x06 0x02 0xab 0xcd 0x81
//! \endverbatim
//!
//! \section smf Status Message Format
//!
//! Status messages are sent from the motor drive with the following format:
//!
//! \verbatim
//! {tag} {length} {data bytes} {checksum}
//! \endverbatim
//!
//! - The {tag} byte is 0xfe for command responses and 0xfd for real-time data.
//!
//! - The {length} byte contains the overall length of the status packet,
//! starting with the {tag} byte and ending with the {checksum}.
//!
//! - The contents of the data bytes are dependent upon the tag byte.
//!
//! - The {checksum} is the value such that the sum of all bytes in the status
//! packet (including the checksum) will be zero. This is used to validate a
//! status packet and allow the user interface to synchronize with the status
//! stream being sent by the target.
//!
//! For command responses ({tag} = 0xfe), the first data byte is the command
//! that is being responded to. The remaining bytes are the response, and are
//! dependent upon the command.
//!
//! For real-time data messages ({tag} = 0xfd), each real-time data item is
//! transmitted as a little-endian value (for example, for a 16-bit value, the
//! lower 8 bits first then the upper 8 bits). The data items are in the same
//! order as returned by the data item list (#CMD_GET_DATA_ITEMS) regardless of
//! the order that they were enabled.
//!
//! For example, if data items 1, 5, and 17 were enabled, and each was two
//! bytes in length, there would be 6 data bytes in the packet:
//!
//! \verbatim
//! 0xfd 0x09 {d1[0:7]} {d1[8:15]} {d5[0:7]} {d5[8:15]} {d17[0:7]}
//! {d17[8:15]} {checksum}
//! \endverbatim
//!
//! \section pi Parameter Interpretation
//!
//! The size and units of the parameters are dependent upon the motor drive;
//! the units are not conveyed in the serial protocol. Each parameter value is
//! transmitted in little endian format. Not all parameters are necessarily
//! supported by a motor drive, only those that are appropriate.
//!
//! \section itta Interface To The Application
//!
//! The ethernet protocol handler takes care of all the ethernet communications
//! and command interpretation. A set of functions provided by the application
//! and an array of structures that describe the parameters and real-time data
//! items supported by the motor drive. The functions are used when an
//! application-specific action needs to take place as a result of the ethernet
//! communication (such as starting the motor drive). The structures are used
//! to handle the parameters and real-time data items of the motor drive.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup ui_ethernet_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! The size of the UART transmit buffer. This should be appropriately sized
//! such that the maximum burst of output data can be contained in this buffer.
//! This value should be a power of two in order to make the modulo arithmetic
//! be fast (that is, an AND instead of a divide).
//
//*****************************************************************************
#ifndef UIETHERNET_MAX_XMIT
#define UIETHERNET_MAX_XMIT 64
#endif
//*****************************************************************************
//
//! The size of the UART receive buffer. This should be appropriately sized
//! such that the maximum size command packet can be contained in this buffer.
//! This value should be a power of two in order to make the modulo arithmetic
//! be fast (that is, an AND instead of a divide).
//
//*****************************************************************************
#ifndef UIETHERNET_MAX_RECV
#define UIETHERNET_MAX_RECV 64
#endif
//*****************************************************************************
//
//! A buffer to contain data received from the UART. A packet is processed out
//! of this buffer once the entire packet is contained within the buffer.
//
//*****************************************************************************
static unsigned char g_pucUIEthernetReceive[UIETHERNET_MAX_RECV];
//*****************************************************************************
//
//! The offset of the next byte to be read from g_pucUIEthernetReceive.
//
//*****************************************************************************
static unsigned long g_ulUIEthernetReceiveRead;
//*****************************************************************************
//
//! The offset of the next byte to be written to g_pucUIEthernetReceive.
//
//*****************************************************************************
static unsigned long g_ulUIEthernetReceiveWrite;
//*****************************************************************************
//
//! A buffer used to construct status packets before they are written to the
//! UART and/or g_pucUIEthernetTransmit.
//
//*****************************************************************************
static unsigned char g_pucUIEthernetResponse[UIETHERNET_MAX_XMIT];
//*****************************************************************************
//
//! A buffer used to construct real-time data packets before they are written
//! to the UART and/or g_pucUIEthernetTransmit.
//
//*****************************************************************************
static unsigned char g_pucUIEthernetData[UIETHERNET_MAX_XMIT];
//*****************************************************************************
//
//! A boolean that is true when the real-time data stream is enabled.
//
//*****************************************************************************
static tBoolean g_bEnableRealTimeData;
//*****************************************************************************
//
//! A bit array that contains a flag for each real-time data item. When the
//! corresponding flag is set, that real-time data item is enabled in the
//! real-time data stream; when the flag is clear, that real-time data item is
//! not part of the real-time data stream.
//
//*****************************************************************************
static unsigned long g_pulUIRealTimeData[(DATA_NUM_ITEMS + 31) / 32];
//*****************************************************************************
//
//! The fast TCP timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulTCPFastTimer = 0;
//*****************************************************************************
//
//! The slow TCP timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulTCPSlowTimer = 0;
//*****************************************************************************
//
//! The ARP timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulARPTimer = 0;
#if LWIP_DHCP
//*****************************************************************************
//
//! The Coarse DHCP timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulDHCPCoarseTimer = 0;
//*****************************************************************************
//
//! The Fine DHCP timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulDHCPFineTimer = 0;
//*****************************************************************************
//
//! The DHCP timeout timer for lwIP.
//
//*****************************************************************************
static unsigned long g_ulDHCPTimeoutTimer = 0;
#endif
//*****************************************************************************
//
//! The Network Interface Structure.
//
//*****************************************************************************
static struct netif g_EMAC_if;
//*****************************************************************************
//
//! The timeout for DHCP address request (in seconds).
//
//*****************************************************************************
#ifndef DHCP_EXPIRE_TIMER_SECS
#define DHCP_EXPIRE_TIMER_SECS 45
#endif
//*****************************************************************************
//
//! Default TCP/IP Address Configuration. Static IP Configuration is used if
//! DHCP times out.
//
//*****************************************************************************
//
//! The Default IP address to be used.
//
#ifndef DEFAULT_IPADDR0
#define DEFAULT_IPADDR0 169
#endif
#ifndef DEFAULT_IPADDR1
#define DEFAULT_IPADDR1 254
#endif
#ifndef DEFAULT_IPADDR2
#define DEFAULT_IPADDR2 89
#endif
#ifndef DEFAULT_IPADDR3
#define DEFAULT_IPADDR3 71
#endif
//
//! The Default Gateway address to be used.
//
#ifndef DEFAULT_GATEWAY_ADDR0
#define DEFAULT_GATEWAY_ADDR0 0
#endif
#ifndef DEFAULT_GATEWAY_ADDR1
#define DEFAULT_GATEWAY_ADDR1 0
#endif
#ifndef DEFAULT_GATEWAY_ADDR2
#define DEFAULT_GATEWAY_ADDR2 0
#endif
#ifndef DEFAULT_GATEWAY_ADDR3
#define DEFAULT_GATEWAY_ADDR3 0
#endif
//
//! The Default Network mask to be used.
//
#ifndef DEFAULT_NET_MASK0
#define DEFAULT_NET_MASK0 255
#endif
#ifndef DEFAULT_NET_MASK1
#define DEFAULT_NET_MASK1 255
#endif
#ifndef DEFAULT_NET_MASK2
#define DEFAULT_NET_MASK2 0
#endif
#ifndef DEFAULT_NET_MASK3
#define DEFAULT_NET_MASK3 0
#endif
//*****************************************************************************
//
//! The port to use for TCP connections for the Motor Drive UI protocol.
//
//*****************************************************************************
#ifndef UI_PROTO_PORT
#define UI_PROTO_PORT 23
#endif
//*****************************************************************************
//
//! The port to use for UDP connections for the Motor Drive UI protocol.
//! (This port is used for query purposes only).
//
//*****************************************************************************
#ifndef UI_QUERY_PORT
#define UI_QUERY_PORT 23
#endif
//*****************************************************************************
//
//! Pointer to the telnet session PCB data structure.
//
//*****************************************************************************
static struct tcp_pcb *g_psTelnetPCB = NULL;
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -