ZeroCopyRx.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. typedef struct my_custom_pbuf
  2. {
  3. struct pbuf_custom p;
  4. void* dma_descriptor;
  5. } my_custom_pbuf_t;
  6. LWIP_MEMPOOL_DECLARE(RX_POOL, 10, sizeof(my_custom_pbuf_t), "Zero-copy RX PBUF pool");
  7. void my_pbuf_free_custom(void* p)
  8. {
  9. SYS_ARCH_DECL_PROTECT(old_level);
  10. my_custom_pbuf_t* my_puf = (my_custom_pbuf_t*)p;
  11. // invalidate data cache here - lwIP and/or application may have written into buffer!
  12. // (invalidate is faster than flushing, and no one needs the correct data in the buffer)
  13. invalidate_cpu_cache(p->payload, p->tot_len);
  14. SYS_ARCH_PROTECT(old_level);
  15. free_rx_dma_descriptor(my_pbuf->dma_descriptor);
  16. LWIP_MEMPOOL_FREE(RX_POOL, my_pbuf);
  17. SYS_ARCH_UNPROTECT(old_level);
  18. }
  19. void eth_rx_irq()
  20. {
  21. dma_descriptor* dma_desc = get_RX_DMA_descriptor_from_ethernet();
  22. my_custom_pbuf_t* my_pbuf = (my_custom_pbuf_t*)LWIP_MEMPOOL_ALLOC(RX_POOL);
  23. my_pbuf->p.custom_free_function = my_pbuf_free_custom;
  24. my_pbuf->dma_descriptor = dma_desc;
  25. invalidate_cpu_cache(dma_desc->rx_data, dma_desc->rx_length);
  26. struct pbuf* p = pbuf_alloced_custom(PBUF_RAW,
  27. dma_desc->rx_length,
  28. PBUF_REF,
  29. &my_pbuf->p,
  30. dma_desc->rx_data,
  31. dma_desc->max_buffer_size);
  32. if(netif->input(p, netif) != ERR_OK) {
  33. pbuf_free(p);
  34. }
  35. }