Microchip® Advanced Software Framework

sam0_spi_ksz8851snl.c File Reference

KSZ8851SNL SAM0 driver for lwIP.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

#include "port.h"
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "netif/etharp.h"
#include "netif/ppp_oe.h"
#include <string.h>
#include "ksz8851snl.h"
#include "ksz8851snl_reg.h"
#include "netif/sam0_spi_ksz8851snl.h"
#include "conf_eth.h"

Data Structures

struct  ksz8851snl_device
 ksz8851snl driver structure. More...
 

Macros

#define IFNAME0   'e'
 Network interface identifier. More...
 
#define IFNAME1   'n'
 
#define NET_LINK_SPEED   100000000
 Network link speed. More...
 
#define NET_MTU   1500
 Maximum transfer unit. More...
 

Functions

err_t ethernetif_init (struct netif *netif)
 Should be called at the beginning of the program to set up the network interface. More...
 
void ethernetif_input (struct netif *netif)
 This function should be called when a packet is ready to be read from the interface. More...
 
static void INTN_Handler ()
 Handler for INTN falling edge interrupt. More...
 
static void ksz8851snl_low_level_init (struct netif *netif)
 Initialize ksz8851snl ethernet controller. More...
 
static struct pbufksz8851snl_low_level_input (struct netif *netif)
 Use pre-allocated pbuf as DMA source and return the incoming packet. More...
 
static err_t ksz8851snl_low_level_output (struct netif *netif, struct pbuf *p)
 This function should do the actual transmission of the packet. More...
 
static void ksz8851snl_rx_init (struct ksz8851snl_device *ps_ksz8851snl_dev)
 Set up the RX descriptor ring buffers. More...
 
static void ksz8851snl_rx_populate_queue (struct ksz8851snl_device *p_ksz8851snl_dev)
 Populate the RX descriptor ring buffers with pbufs. More...
 
static void ksz8851snl_task (void *pvParameters)
 This function waits for the notification semaphore from the interrupt, processes the incoming packet and then passes it to the lwIP stack. More...
 
static void ksz8851snl_tx_init (struct ksz8851snl_device *ps_ksz8851snl_dev)
 Set up the TX descriptor ring buffers. More...
 
static void ksz8851snl_update (struct netif *netif)
 Update Micrel state machine and perform required actions. More...
 

Variables

volatile uint32_t g_intn_flag = 0
 
static struct ksz8851snl_device gs_ksz8851snl_dev
 ksz8851snl driver instance. More...
 
static uint8_t gs_uc_mac_address []
 MAC address to use. More...
 
static uint16_t pending_frame = 0
 

#define IFNAME0   'e'

Network interface identifier.

Referenced by ethernetif_init().

#define IFNAME1   'n'

Referenced by ethernetif_init().

#define NET_LINK_SPEED   100000000

Network link speed.

Referenced by ethernetif_init().

#define NET_MTU   1500

Maximum transfer unit.

Referenced by ksz8851snl_low_level_init().

err_t ethernetif_init ( struct netif netif)

Should be called at the beginning of the program to set up the network interface.

It calls the function ksz8851snl_low_level_init() to do the actual setup of the hardware.

Parameters
netifthe lwIP network interface structure for this ethernetif.
Returns
ERR_OK if the loopif is initialized. ERR_MEM if private data couldn't be allocated. any other err_t on error.

References err, ERR_MEM, ERR_OK, gs_ksz8851snl_dev, id, IFNAME0, IFNAME1, ksz8851snl_low_level_init(), ksz8851snl_low_level_output(), ksz8851snl_task(), netif::linkoutput, LWIP_ASSERT, netif::name, NET_LINK_SPEED, ksz8851snl_device::netif, NETIF_INIT_SNMP, NULL, netif::output, snmp_ifType_ethernet_csmacd, netif::state, ksz8851snl_device::sync_sem, sys_sem_new(), and sys_thread_new().

Referenced by ethernet_configure_interface().

void ethernetif_input ( struct netif netif)

This function should be called when a packet is ready to be read from the interface.

It uses the function ksz8851snl_low_level_input() that handles the actual reception of bytes from the network interface. Then the type of the received packet is determined and the appropriate input function is called.

Parameters
netifthe lwIP network interface structure for this ethernetif.

References ERR_OK, htons, netif::input, ksz8851snl_low_level_input(), ksz8851snl_update(), LWIP_DEBUGF, NETIF_DEBUG, NULL, pbuf::payload, and pbuf_free().

Referenced by ethernet_task(), and ksz8851snl_task().

static void INTN_Handler ( )
static

Handler for INTN falling edge interrupt.

References g_intn_flag, gs_ksz8851snl_dev, pdFALSE, ksz8851snl_device::sync_sem, and xSemaphoreGiveFromISR.

Referenced by ksz8851snl_low_level_init().

static void ksz8851snl_low_level_init ( struct netif netif)
static

Initialize ksz8851snl ethernet controller.

Note
Called from ethernetif_init().
Parameters
netifthe lwIP network interface structure for this ethernetif.

References configure_intn(), netif::flags, gs_ksz8851snl_dev, gs_uc_mac_address, netif::hwaddr, netif::hwaddr_len, INTN_Handler(), ksz8851snl_init(), ksz8851snl_rx_init(), ksz8851snl_tx_init(), LWIP_ASSERT, LWIP_DEBUGF, netif::mtu, NET_MTU, NETIF_DEBUG, NETIF_FLAG_BROADCAST, NETIF_FLAG_DHCP, and NETIF_FLAG_ETHARP.

Referenced by ethernetif_init().

static struct pbuf* ksz8851snl_low_level_input ( struct netif netif)
static

Use pre-allocated pbuf as DMA source and return the incoming packet.

Parameters
netifthe lwIP network interface structure for this ethernetif.
Returns
a pbuf filled with the received packet (including MAC header). 0 on memory error.

References ksz8851snl_rx_populate_queue(), LINK_STATS_INC, LWIP_DEBUGF, NETIF_DEBUG, pbuf::payload, ksz8851snl_device::rx_desc, ksz8851snl_device::rx_pbuf, netif::state, pbuf::tot_len, ksz8851snl_device::us_rx_head, and ksz8851snl_device::us_rx_tail.

Referenced by ethernetif_input().

static err_t ksz8851snl_low_level_output ( struct netif netif,
struct pbuf p 
)
static

This function should do the actual transmission of the packet.

The packet is contained in the pbuf that is passed to the function. This pbuf might be chained.

Parameters
netifthe lwIP network interface structure for this ethernetif.
pthe MAC packet to send (e.g. IP packet including MAC addresses and type).
Returns
ERR_OK if the packet could be sent. an err_t value if the packet couldn't be sent.

References ERR_IF, ERR_OK, LINK_STATS_INC, LWIP_DEBUGF, NETIF_DEBUG, pbuf::payload, pbuf_ref(), netif::state, ksz8851snl_device::sync_sem, pbuf::tot_len, ksz8851snl_device::tx_desc, ksz8851snl_device::tx_pbuf, ksz8851snl_device::us_tx_head, ksz8851snl_device::us_tx_tail, and xSemaphoreGive.

Referenced by ethernetif_init().

static void ksz8851snl_rx_init ( struct ksz8851snl_device ps_ksz8851snl_dev)
static

Set up the RX descriptor ring buffers.

This function sets up the descriptor list used for RX packets.

Parameters
ps_ksz8851snl_devPointer to driver data structure.

References ksz8851snl_rx_populate_queue(), ksz8851snl_device::rx_desc, ksz8851snl_device::rx_pbuf, ksz8851snl_device::us_rx_head, and ksz8851snl_device::us_rx_tail.

Referenced by ksz8851snl_low_level_init().

static void ksz8851snl_rx_populate_queue ( struct ksz8851snl_device p_ksz8851snl_dev)
static

Populate the RX descriptor ring buffers with pbufs.

Parameters
p_ksz8851snl_devPointer to driver data structure.

References LWIP_ASSERT, LWIP_DEBUGF, NETIF_DEBUG, pbuf_alloc(), pbuf_clen(), PBUF_POOL, PBUF_POOL_BUFSIZE, PBUF_RAW, ksz8851snl_device::rx_desc, and ksz8851snl_device::rx_pbuf.

Referenced by ksz8851snl_low_level_input(), ksz8851snl_rx_init(), and ksz8851snl_update().

static void ksz8851snl_task ( void *  pvParameters)
static

This function waits for the notification semaphore from the interrupt, processes the incoming packet and then passes it to the lwIP stack.

Parameters
pvParametersA pointer to the ksz8851snl_device instance.

References ethernetif_input(), ksz8851snl_device::netif, pending_frame, ksz8851snl_device::sync_sem, sys_arch_sem_wait(), ksz8851snl_device::tx_desc, and ksz8851snl_device::us_tx_tail.

Referenced by ethernetif_init().

static void ksz8851snl_tx_init ( struct ksz8851snl_device ps_ksz8851snl_dev)
static

Set up the TX descriptor ring buffers.

This function sets up the descriptor list used for TX packets.

Parameters
ps_ksz8851snl_devPointer to driver data structure.

References ksz8851snl_device::tx_desc, ksz8851snl_device::us_tx_head, and ksz8851snl_device::us_tx_tail.

Referenced by ksz8851snl_low_level_init().

volatile uint32_t g_intn_flag = 0

Referenced by INTN_Handler(), and ksz8851snl_update().

struct ksz8851snl_device gs_ksz8851snl_dev
static

ksz8851snl driver instance.

Referenced by ethernetif_init(), INTN_Handler(), and ksz8851snl_low_level_init().

uint8_t gs_uc_mac_address[]
static
Initial value:
=
{
ETHERNET_CONF_ETHADDR0,
ETHERNET_CONF_ETHADDR1,
ETHERNET_CONF_ETHADDR2,
ETHERNET_CONF_ETHADDR3,
ETHERNET_CONF_ETHADDR4,
ETHERNET_CONF_ETHADDR5
}

MAC address to use.

Referenced by ksz8851snl_low_level_init().

uint16_t pending_frame = 0
static