STM32Ethernet
Arduino Ethernet library for STM32 boards
Loading...
Searching...
No Matches
stm32_eth.cpp File Reference

This file implements Ethernet network interface drivers for Arduino STM32Ethernet library. More...

#include "Arduino.h"
#include "stm32_eth.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "lwip/timeouts.h"
#include "netif/ethernet.h"
#include "ethernetif.h"
#include "lwip/dhcp.h"
#include "lwip/prot/dhcp.h"
#include "lwip/dns.h"
Include dependency graph for stm32_eth.cpp:

Classes

struct  stm32_eth_config
 

Macros

#define TIME_CHECK_ETH_LINK_STATE   500U
 
#define TIMEOUT_DNS_REQUEST   10000U
 
#define MAX_DHCP_TRIES   4
 
#define DEFAULT_ETHERNET_TIMER   TIM14
 
#define ETH_TIM_IRQ_PRIO   15
 
#define ETH_TIM_IRQ_SUBPRIO   0
 

Functions

void _stm32_eth_scheduler (void)
 Run one LwIP scheduler pass.
 
void stm32_eth_init (const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask)
 Initialize the Ethernet interface and the LwIP stack.
 
uint8_t stm32_eth_is_init (void)
 Check whether the Ethernet interface has been initialized.
 
void stm32_eth_set_macaddr (const uint8_t *mac)
 Set the MAC address of the Ethernet interface.
 
void stm32_eth_get_macaddr (uint8_t *mac)
 Copy the MAC address of the Ethernet interface.
 
uint8_t stm32_eth_link_up (void)
 Check whether the Ethernet link is up.
 
void stm32_eth_scheduler (void)
 Run one LwIP scheduler pass.
 
uint32_t stm32_eth_get_ipaddr (void)
 Return the IPv4 address of the network interface.
 
uint32_t stm32_eth_get_gwaddr (void)
 Return the gateway address of the network interface.
 
uint32_t stm32_eth_get_netmaskaddr (void)
 Return the netmask of the network interface.
 
uint32_t stm32_eth_get_dnsaddr (void)
 Return the first DNS server address.
 
uint32_t stm32_eth_get_dhcpaddr (void)
 Return the address of the DHCP server.
 
void User_notification (struct netif *netif)
 Notify the user about the network interface status.
 
ip_addr_t * u8_to_ip_addr (uint8_t *ipu8, ip_addr_t *ipaddr)
 Convert an IPv4 address from 4 bytes into an ip_addr_t.
 
uint32_t ip_addr_to_u32 (ip_addr_t *ipaddr)
 Convert an ip_addr_t IPv4 address into a uint32_t.
 
struct pbuf * stm32_new_data (struct pbuf *p, const uint8_t *buffer, size_t size)
 Allocate a pbuf holding the given data.
 
struct pbuf * stm32_free_data (struct pbuf *p)
 Free a pbuf chain.
 
uint16_t stm32_get_data (struct pbuf_data *data, uint8_t *buffer, size_t size)
 Copy received data from a pbuf_data into a user buffer.
 

Variables

struct netif gnetif
 
__IO uint8_t DHCP_state = DHCP_OFF
 

Detailed Description

This file implements Ethernet network interface drivers for Arduino STM32Ethernet library.

Author
WI6LABS
Version
V1.0.0
Date
24-May-2017
Attention

© COPYRIGHT(c) 2016 STMicroelectronics

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of STMicroelectronics nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Macro Definition Documentation

◆ DEFAULT_ETHERNET_TIMER

#define DEFAULT_ETHERNET_TIMER   TIM14

◆ ETH_TIM_IRQ_PRIO

#define ETH_TIM_IRQ_PRIO   15

◆ ETH_TIM_IRQ_SUBPRIO

#define ETH_TIM_IRQ_SUBPRIO   0

◆ MAX_DHCP_TRIES

#define MAX_DHCP_TRIES   4

◆ TIME_CHECK_ETH_LINK_STATE

#define TIME_CHECK_ETH_LINK_STATE   500U

◆ TIMEOUT_DNS_REQUEST

#define TIMEOUT_DNS_REQUEST   10000U

Function Documentation

◆ _stm32_eth_scheduler()

void _stm32_eth_scheduler ( void  )

Run one LwIP scheduler pass.

Reads received packets (unless ETH_INPUT_USE_IT is defined), checks the link status every TIME_CHECK_ETH_LINK_STATE ms, handles LwIP timeouts and runs the periodic DHCP processing. Called by the timer callback and, on older cores, by stm32_eth_scheduler().

◆ ip_addr_to_u32()

uint32_t ip_addr_to_u32 ( ip_addr_t *  ipaddr)

Convert an ip_addr_t IPv4 address into a uint32_t.

Parameters
ipaddrAddress to convert.
Returns
The address as a uint32_t (raw network byte order).

◆ stm32_eth_get_dhcpaddr()

uint32_t stm32_eth_get_dhcpaddr ( void  )

Return the address of the DHCP server.

Returns
DHCP server address as a uint32_t (raw network byte order).

◆ stm32_eth_get_dnsaddr()

uint32_t stm32_eth_get_dnsaddr ( void  )

Return the first DNS server address.

Returns
DNS server address (server 0) as a uint32_t (raw network byte order).

◆ stm32_eth_get_gwaddr()

uint32_t stm32_eth_get_gwaddr ( void  )

Return the gateway address of the network interface.

Returns
Gateway address as a uint32_t (raw network byte order).

◆ stm32_eth_get_ipaddr()

uint32_t stm32_eth_get_ipaddr ( void  )

Return the IPv4 address of the network interface.

Returns
Address as a uint32_t (raw network byte order).

◆ stm32_eth_get_macaddr()

void stm32_eth_get_macaddr ( uint8_t *  mac)

Copy the MAC address of the Ethernet interface.

Parameters
macReceives the MAC address (6 bytes). Nothing is copied if NULL.

◆ stm32_eth_get_netmaskaddr()

uint32_t stm32_eth_get_netmaskaddr ( void  )

Return the netmask of the network interface.

Returns
Netmask as a uint32_t (raw network byte order).

◆ stm32_eth_init()

void stm32_eth_init ( const uint8_t *  mac,
const uint8_t *  ip,
const uint8_t *  gw,
const uint8_t *  netmask 
)

Initialize the Ethernet interface and the LwIP stack.

LwIP initialization and the scheduler timer setup run only on the first call. Every call reconfigures the network interface and runs one scheduler pass.

A NULL argument selects the default value:

  • mac: the MAC address is left unchanged.
  • ip, gw, netmask: set to zero when LWIP_DHCP is enabled, otherwise to the compile-time defaults (IP_ADDR*, GW_ADDR*, NETMASK_ADDR*).
Parameters
macMAC address (6 bytes), or NULL to keep the default MAC address.
ipIPv4 address (4 bytes), or NULL for the default.
gwGateway address (4 bytes), or NULL for the default.
netmaskNetmask (4 bytes), or NULL for the default.

◆ stm32_eth_is_init()

uint8_t stm32_eth_is_init ( void  )

Check whether the Ethernet interface has been initialized.

Returns
1 if the Ethernet hardware is initialized, 0 otherwise.

◆ stm32_eth_link_up()

uint8_t stm32_eth_link_up ( void  )

Check whether the Ethernet link is up.

Returns
Non-zero if the link is up, 0 otherwise.

◆ stm32_eth_scheduler()

void stm32_eth_scheduler ( void  )

Run one LwIP scheduler pass.

Reads received packets (unless ETH_INPUT_USE_IT is defined), checks the link status, handles LwIP timeouts and runs the periodic DHCP processing.

◆ stm32_eth_set_macaddr()

void stm32_eth_set_macaddr ( const uint8_t *  mac)

Set the MAC address of the Ethernet interface.

The address is stored only before the interface is initialized.

Parameters
macMAC address (6 bytes). Ignored if NULL.

◆ stm32_free_data()

struct pbuf * stm32_free_data ( struct pbuf *  p)

Free a pbuf chain.

Parameters
pPbuf to free; NULL is ignored.
Returns
Always NULL.

◆ stm32_get_data()

uint16_t stm32_get_data ( struct pbuf_data *  data,
uint8_t *  buffer,
size_t  size 
)

Copy received data from a pbuf_data into a user buffer.

Frees the pbufs that are fully consumed.

Parameters
dataSource of the received data.
bufferDestination buffer.
sizeMaximum number of bytes to copy.
Returns
Number of bytes copied, or 0 on invalid arguments or when no data is available.

◆ stm32_new_data()

struct pbuf * stm32_new_data ( struct pbuf *  p,
const uint8_t *  buffer,
size_t  size 
)

Allocate a pbuf holding the given data.

If p is NULL, allocates a new pbuf and copies buffer into it. Otherwise, allocates a larger pbuf, copies the contents of p and appends buffer, then frees p. On failure, returns NULL and leaves p unchanged.

Parameters
pExisting pbuf to extend, or NULL to create a new one.
bufferData to store.
sizeNumber of bytes to store from buffer.
Returns
Pointer to the pbuf holding the data, or NULL on failure.

◆ u8_to_ip_addr()

ip_addr_t * u8_to_ip_addr ( uint8_t *  ipu8,
ip_addr_t *  ipaddr 
)

Convert an IPv4 address from 4 bytes into an ip_addr_t.

Parameters
ipu8Address bytes (4 bytes).
ipaddrReceives the converted address.
Returns
Pointer to ipaddr.

◆ User_notification()

void User_notification ( struct netif *  netif)

Notify the user about the network interface status.

If the interface is down and DHCP was started by the user, sets the DHCP state to DHCP_LINK_DOWN.

Parameters
netifNetwork interface to check.

Variable Documentation

◆ DHCP_state

__IO uint8_t DHCP_state = DHCP_OFF

◆ gnetif

struct netif gnetif