Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 865 of 93612. IoT Connectivity and SecurityFree lesson

Integrating Network Drivers with the TCP/IP Stack

Develop hardware abstraction layers to bridge low-level Ethernet or Wi-Fi peripheral drivers with the LwIP network interface input functions.

In this lesson, you will learn how to create a Hardware Abstraction Layer (HAL) to seamlessly bridge raw Ethernet frames from your peripheral driver into the LwIP TCP/IP stack.

The Glue: The Network Interface (netif)

In an embedded system, your network driver sits at the hardware level, reacting to interrupts when a frame arrives. However, the LwIP stack lives at the protocol level (IP, TCP, UDP). To connect these, we use the struct netif interface. The core of this integration is the netif->input function pointer, which tells the stack where to send a packet once it has been retrieved from the hardware.

Implementing the HAL Bridge

When your hardware interrupt handler (which we've already covered in our previous sessions) triggers, your driver shouldn't process the packet itself. Instead, it should perform the following three-step handshake:

  1. Allocate a PBUF: Use pbuf_alloc() to reserve memory for the incoming packet.
  2. Copy/Reference Data: Move the data from the hardware DMA buffer into the pbuf.
  3. Inject into LwIP: Call the netif->input() function.

Here is how that bridge typically looks in your driver’s receive routine:

err_t low_level_input(struct netif *netif, struct pbuf *p) {
    // 1. Allocate a pbuf (usually PBUF_RAW)
    p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
    
    if (p != NULL) {
        // 2. Transfer from hardware buffer to pbuf
        memcpy(p->payload, rx_buffer, length);
        
        // 3. Hand off to the stack
        if (netif->input(p, netif) != ERR_OK) {
            pbuf_free(p);
        }
    }
    return ERR_OK;
}

By decoupling the hardware receive logic from the protocol processing, you ensure that your driver remains focused solely on frame integrity and buffer management, while LwIP handles the complexities of IP reassembly and protocol state machines. This separation is crucial for debugging; if your networking fails, you can isolate whether the issue lies in the HAL bridge (by checking if netif->input is being called) or within the stack configuration itself.

Key Takeaway: The network driver’s primary role is to wrap raw hardware frames into pbuf structures and hand them off to the netif->input interface, effectively shielding the TCP/IP stack from hardware-specific register operations.