Category Archives: Uncategorized

FreeBSD Port Changes 6.2 -> 8.4

[root@te-clan /usr/ports/lang/php5]# make
Unknown modifier 'u'

Unknown modifier 'u'

Unknown modifier 'u'

Variable OPTIONS_DEFAULT is recursive.

Unknown modifier ‘u’ … Variable ALL_OPTIONS is recursive.
Subversion and port-tree upgrade (cvs is deprecated)

RoundCube

RoundCube: Howto Upgrade

PHP Extensions

/usr/ports/databases/php5-pdo_mysql
 /usr/local/lib/php/20100525/

Filter

FreeBSD, PHP 5.4.8 throws: “Fatal error: Call to undefined function filter_var()”

pecl-intl

ICU – International Components for Unicode
ICU 50

Old: icu-3.8.1_3         International Components for Unicode (from IBM)
New: icu-50.1.2          International Components for Unicode (from IBM)
/usr/ports/devel/icu
/usr/ports/devel/pecl-intl

php5-exif

/usr/ports/graphics/php5-exif

file / libmagic

# tail /web/global/.logs/webmail2-error.log
stderr: PHP Warning:  finfo_open(): Failed to load magic database at '/usr/share/misc/magic'.

# pkg_info -L file-5.15
Information for file-5.15:

Files:
/usr/local/share/licenses/file-5.15/catalog.mk
/usr/local/share/licenses/file-5.15/LICENSE
/usr/local/share/licenses/file-5.15/BSD
/usr/local/man/man1/file.1.gz
/usr/local/man/man3/libmagic.3.gz
/usr/local/man/man5/magic.5.gz
/usr/local/bin/file
/usr/local/include/magic.h
/usr/local/lib/libmagic.a
/usr/local/lib/libmagic.la
/usr/local/lib/libmagic.so
/usr/local/lib/libmagic.so.1
/usr/local/share/file/magic.mgc

Seggers JLink

IDA Pro

Hex-Rays IDA Pro

v8 vs. v9

Version 8.0
Identical to version 7.0 with the following exception:
- SWD support for non-3.3V targets.

Version 9.1
- New design based on STM32F205.

Version 9.2
Identical to version 9.1 with the following exception:
- Pin 1 (VTref) is used for measuring target reference
  voltage only. Buffers on J-Link side are no longer
  powered through this pin but via the J-Link internal
  voltage supplied via USB.

Boards

STM32: Hello World (Shenzhen LC Technology STM32F103C8 development board)
Shenzhen LC Technology Co.,Ltd.

J-Link Device Name Settings

GNU ARM Eclipse & Segger J-Link Device Name Settings

J-Link Commander
Reading peripheral register of Cortex M0 MCU using JLink and GDB

Commands

To switch to serial wire debug (SWD):
> si 1

Set the device profile for the target MCU:
> device EFM32GG990F1024
> device STM32F103C8

Erase the flash:
> erase

Download a binary file:
> loadbin c:\blink.bin, 0x0
> loadbin /home/andreas/Downloads/generic_boot20_pc13.bin 0x0

Reset the MCU:
> r

Run the MCU (Go):
> g

Halt the MCU:
> h

To verify that a binary file was properly downloaded to MCU flash:
> verifybin c:\blink.bin, 0x0

To upload memory from MCU flash and save it to a
binary file (start-address, length):
> savebin c:\upload.bin, 0x0, 0x1084
$ /opt/SEGGER/JLink/JLinkExe -device STM32F103C8 -if SWD -speed 4000

SEGGER J-Link Commander V5.12f (Compiled May 17 2016 16:01:56)
DLL version V5.12f, compiled May 17 2016 16:01:51

Connecting to J-Link via USB...O.K.
Firmware: J-Link V9 compiled Apr 22 2016 11:47:06
Hardware version: V9.20
S/N: xxxxxxxxx
License(s): GDB, RDI, FlashBP, FlashDL, JFlash, RDDI
VTref = 3.322V

J-Link>showemulist
J-Link[0]: Connection: USB, Serial number: xxxxxxxxx, ProductName: xxxxxxxxxx

J-Link>connect
Device "STM32F103C8" selected.

Found SWD-DP with ID 0x1BA01477
Found SWD-DP with ID 0x1BA01477
Found Cortex-M3 r1p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl 0 @ E00FF000
ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 001BB000 SCS
ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 001BB002 DWT
ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 000BB003 FPB
ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 001BB001 ITM
ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 001BB923 TPIU-Lite
Cortex-M3 identified.

J-Link>loadbin /home/andreas/Downloads/F103_blink_ledNUCLEO_F103RB.bin 0x0
Halting CPU for downloading file.
Downloading file [/home/andreas/Downloads/F103_blink_ledNUCLEO_F103RB.bin]...
Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Programming flash [100%] Done.
Verifying flash   [100%] Done.
J-Link: Flash download: Flash programming performed for 1 range (24576 bytes)
J-Link: Flash download: Total time needed: 1.071s (Prepare: 0.055s, Compare: 0.004s, Erase: 0.352s, Program: 0.654s, Verify: 0.000s, Restore: 0.003s)
O.K.

J-Link>loadbin /home/andreas/Downloads/generic_boot20_pc13.bin 0x0
Halting CPU for downloading file.
Downloading file [/home/andreas/Downloads/generic_boot20_pc13.bin]...
Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Programming flash [100%] Done.
Verifying flash   [100%] Done.
J-Link: Flash download: Flash programming performed for 1 range (8192 bytes)
J-Link: Flash download: Total time needed: 0.480s (Prepare: 0.066s, Compare: 0.002s, Erase: 0.176s, Program: 0.218s, Verify: 0.000s, Restore: 0.016s)
O.K.

Toolchain

GNU ARM Eclipse
How to install the ARM toolchain?
GNU ARM Embedded Toolchain
Ubuntu PPA: GNU ARM Embedded Toolchain

$ sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
$ sudo apt-get update
$ sudo apt-get install gcc-arm-embedded

GCC Option Summary

-v            Print the commands executed to run
              the stages of compilation.
-x language   Specify explicitly the language for
              the following input files.
-E            Stop after the preprocessing stage;
              do not run the compiler proper.
-Wp,option    You can use -Wp,option to bypass the
              compiler driver and pass option
              directly through to the preprocessor. 

$ echo | arm-none-eabi-gcc -v -x c -E -
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
Target: arm-none-eabi
Configured with: /build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/src/gcc/configure --target=arm-none-eabi --prefix=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native --libexecdir=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/lib --infodir=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/share/doc/gcc-arm-none-eabi/info --mandir=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/share/doc/gcc-arm-none-eabi/man --htmldir=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/share/doc/gcc-arm-none-eabi/html --pdfdir=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/build/gcc-arm-none-eabi-CE0_5N/gcc-arm-none-eabi-6-2016q4/install-native/arm-none-eabi --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Tools for ARM Embedded Processors' --with-multilib-list=rmprofile
Thread model: single
gcc version 6.2.1 20161205 (release) [ARM/embedded-6-branch revision 243739] (GNU Tools for ARM Embedded Processors) 
COLLECT_GCC_OPTIONS='-v' '-E'
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/cc1 -E -quiet -v -iprefix /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/ -isysroot /usr/bin/../arm-none-eabi -D__USES_INITFINI__ -
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/include"
ignoring nonexistent directory "/usr/bin/../arm-none-eabi/usr/local/include"
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/include-fixed"
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/include"
ignoring nonexistent directory "/usr/bin/../arm-none-eabi/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/include
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/include-fixed
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/include
End of search list.
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"
COMPILER_PATH=/usr/bin/../lib/gcc/arm-none-eabi/6.2.1/:/usr/bin/../lib/gcc/:/usr/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/bin/
LIBRARY_PATH=/usr/bin/../lib/gcc/arm-none-eabi/6.2.1/:/usr/bin/../lib/gcc/:/usr/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/lib/:/usr/bin/../arm-none-eabi/lib/
COLLECT_GCC_OPTIONS='-v' '-E'

$ echo | arm-none-eabi-gcc -E -Wp,-v -
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/include"
ignoring nonexistent directory "/usr/bin/../arm-none-eabi/usr/local/include"
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/include-fixed"
ignoring duplicate directory "/usr/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/include"
ignoring nonexistent directory "/usr/bin/../arm-none-eabi/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/include
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/include-fixed
 /usr/bin/../lib/gcc/arm-none-eabi/6.2.1/../../../../arm-none-eabi/include
End of search list.
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"

Eclipse

SEGGER J-Link install
The J-Link hardware debugging Eclipse plug-in
Plug-ins install
Stepping into disassembled code

Includes:
/usr/arm-none-eabi/include
/usr/lib/gcc/arm-none-eabi/6.2.1/include
/home/andreas/src/TivaWare

Symbols:
PART_TM4C123GH6PM
or
PART_LM4F120H5QR

Debugging Libraries

GNU Libs with Debug Information: Rebuilding the GNU ARM Libraries
Semihosting with GNU ARM Embedded (LaunchPad) and GNU ARM Eclipse Debug Plugins

Serial Wire Output (SWO) / Retargeting

Semihosting on ARM Cortex-M with open-source tools
Cannot Get Semihosting To Print (STM32F4, OpenOCD)
STM32F2 + SWO + JLINK + JLINK server + GDB client elf console
Semihosting on STM32 with GNU ARM Embedded, Eclipse and JLink GDB Server
J-Link SWO Viewer
Semihosting with GNU ARM Embedded (LaunchPad) and GNU ARM Eclipse Debug Plugins
Linker error on a C project using eclipse
Getting NewLib and printf to work with the STM32 and Code Sourcery Lite eabi
Linker error, undefined reference to `_sbrk’
Cannot Get Semihosting To Print (STM32F4, OpenOCD)
Printing “Hello World” to terminal from some board including nrf51822.
printf-gcc: An example of how printf can be redirected to UART with nRF51822
STM32 – printf
Printf() – debugging
Supply own _sbrk with libnosys/rdimon

undefined reference to `_sbrk’

arm-none-eabi-gcc -ggdb -Wall -Tstm32_flash.ld  -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Iinc -Ilib -Ilib/inc  -Ilib/inc/core -Ilib/inc/peripherals  src/main.c src/stm32f4xx_it.c src/system_stm32f4xx.c lib/startup_stm32f4xx.s -o main.elf -Llib -lstm32f4
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x10): undefined reference to `_write'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x10): undefined reference to `_lseek'
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/libg.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text._read_r+0x10): undefined reference to `_read'
collect2: error: ld returned 1 exit status
make: *** [main.elf] Error 1

Use rdimon:

--specs=rdimon.specs

undefined reference to `__end__’

arm-none-eabi-gcc -ggdb -Wall -Tstm32_flash.ld  -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Iinc -Ilib -Ilib/inc  -Ilib/inc/core -Ilib/inc/peripherals  src/main.c src/stm32f4xx_it.c src/system_stm32f4xx.c lib/startup_stm32f4xx.s --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group -o main.elf -Llib -lstm32f4
/usr/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu/rdimon-crt0.o: In function `_start':
(.text+0xd8): undefined reference to `__end__'
collect2: error: ld returned 1 exit status
make: *** [main.elf] Error 1

No __end__ identifier. Create one in stm32_flash.ld

  .bss :
  {
    [...]
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM

  ._user_heap_stack :
  {
  .bss :
  {
    [...]
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM

PROVIDE(__end__ = _ebss);
  ._user_heap_stack :
  {

Debug Register: Decimal -> Hex

Window->Preferences->C/C++->Debug
In “Opened view default settings” change “Default register format:” to “Hexadecimal”.

Questions

Busted JLink
J-Link v8 with FW newer than May 20, 2010?
OpenOCD and Segger J-Link
OpenOCD 0.5.0 not working with JLink 4.50m FW
J-Link v8 with FW newer than May 20, 2010?
Clearing lockup after double fault
J-Link v8 with FW newer than May 20, 2010?

$ sudo JLinkExe 
SEGGER J-Link Commander V4.78a ('?' for help)
Compiled Oct  8 2013 13:36:53
Can not connect to J-Link via USB.
$ cat JLink.log 
TB75126C0 000:000 SEGGER J-Link V4.78a Log File (0000ms, 0000ms total)
TB75126C0 000:000 DLL Compiled: Oct  8 2013 13:36:49 (0000ms, 0000ms total)
TB75126C0 000:000 Logging started @ 2013-10-11 22:18 (0000ms, 0000ms total)
TB75126C0 000:000 JLINK_SetWarnOutHandler(...) (0000ms, 0000ms total)
TB75126C0 000:000 JLINK_SelectUSB(Port = 0)  returns 0x00 (0001ms, 0001ms total)
TB75126C0 000:001 JLINK_OpenEx(...)
  ***** Error: Can not connect to J-Link via USB.  returns "Can not connect to J-Link via USB." (0136ms, 0137ms total)
TB75126C0 224:915 JLINK_ClrError() (0000ms, 0137ms total)
TB75126C0 224:915 JLINK_Close() (0000ms, 0137ms total)
TB75126C0 224:915  (0000ms, 0137ms total)
TB75126C0 224:915 Closed (0000ms, 0137ms total)
BUS!="usb", ACTION!="add", SUBSYSTEM!=="usb_device", GOTO="jlink_rules_end"
#
# Give all users read and write access.
#
ATTRS{idProduct}=="0101", ATTRS{idVendor}=="1366", MODE="666"
ATTRS{idProduct}=="0102", ATTRS{idVendor}=="1366", MODE="666"
ATTRS{idProduct}=="0103", ATTRS{idVendor}=="1366", MODE="666"
ATTRS{idProduct}=="0104", ATTRS{idVendor}=="1366", MODE="666"
#
# J-Link OBs with CDC functionality
#
ATTRS{idProduct}=="0105", ATTRS{idVendor}=="1366", MODE="666"
LABEL="jlink_rules_end"

Breaking JLink Clone-Detection
Anyone used JLink on Linux
J-Link Commander without sudo

Local GDB rather than Segger GDB

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
[...]
This GDB was configured as "x86_64-linux-gnu".
[...]
warning: Cannot convert floating-point register value to non-floating-point type.

JLink DLL / SO

$ file /opt/SEGGER/JLink_V512f/libjlinkarm.so.5.12.6
ELF 64-bit LSB shared object,
x86-64,
version 1 (SYSV),
dynamically linked,
BuildID[sha1]=93c09423c37da8451fa0a37bbbf613b8352a6948, stripped

$ objdump -f /opt/SEGGER/JLink_V512f/libjlinkarm.so.5.12.6
/opt/SEGGER/JLink_V512f/libjlinkarm.so.5.12.6:     file format elf64-x86-64
architecture: i386:x86-64, flags 0x00000150:
HAS_SYMS, DYNAMIC, D_PAGED
start address 0x00000000000ff510

$ readelf -S /opt/SEGGER/JLink_V512f/libjlinkarm.so.5.12.6
There are 27 section headers, starting at offset 0x9f46a0:

Section Headers:
  [Nr] Name              Type             Address           Offset       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000     0000000000000000  0000000000000000           0     0     0
  [ 1] .note.gnu.build-i NOTE             0000000000000190  00000190     0000000000000024  0000000000000000   A       0     0     4
  [ 2] .gnu.hash         GNU_HASH         00000000000001b8  000001b8     0000000000001350  0000000000000000   A       3     0     8
  [ 3] .dynsym           DYNSYM           0000000000001508  00001508     00000000000040f8  0000000000000018   A       4     2     8
  [ 4] .dynstr           STRTAB           0000000000005600  00005600     0000000000003334  0000000000000000   A       0     0     1
  [ 5] .gnu.version      VERSYM           0000000000008934  00008934     000000000000056a  0000000000000002   A       3     0     2
  [ 6] .gnu.version_r    VERNEED          0000000000008ea0  00008ea0     00000000000000d0  0000000000000000   A       4     5     8
  [ 7] .rela.dyn         RELA             0000000000008f70  00008f70     00000000000f2bf8  0000000000000018   A       3     0     8
  [ 8] .rela.plt         RELA             00000000000fbb68  000fbb68     0000000000002280  0000000000000018   A       3    10     8
  [ 9] .init             PROGBITS         00000000000fdde8  000fdde8     0000000000000018  0000000000000000  AX       0     0     4
  [10] .plt              PROGBITS         00000000000fde00  000fde00     0000000000001710  0000000000000010  AX       0     0     4
  [11] .text             PROGBITS         00000000000ff510  000ff510     0000000000153f28  0000000000000000  AX       0     0     16
  [12] .fini             PROGBITS         0000000000253438  00253438     000000000000000e  0000000000000000  AX       0     0     4
  [13] .rodata           PROGBITS         0000000000253460  00253460     00000000001337fc  0000000000000000   A       0     0     32
  [14] .eh_frame_hdr     PROGBITS         0000000000386c5c  00386c5c     000000000000d924  0000000000000000   A       0     0     4
  [15] .eh_frame         PROGBITS         0000000000394580  00394580     0000000000038bd4  0000000000000000   A       0     0     8
  [16] .ctors            PROGBITS         00000000005cd158  003cd158     0000000000000028  0000000000000000  WA       0     0     8
  [17] .dtors            PROGBITS         00000000005cd180  003cd180     0000000000000020  0000000000000000  WA       0     0     8
  [18] .jcr              PROGBITS         00000000005cd1a0  003cd1a0     0000000000000008  0000000000000000  WA       0     0     8
  [19] .data.rel.ro      PROGBITS         00000000005cd1c0  003cd1c0     000000000008bfe0  0000000000000000  WA       0     0     32
  [20] .dynamic          DYNAMIC          00000000006591a0  004591a0     00000000000001d0  0000000000000010  WA       4     0     8
  [21] .got              PROGBITS         0000000000659370  00459370     0000000000000410  0000000000000008  WA       0     0     8
  [22] .got.plt          PROGBITS         0000000000659780  00459780     0000000000000b98  0000000000000008  WA       0     0     8
  [23] .data             PROGBITS         000000000065a320  0045a320     000000000059a240  0000000000000000  WA       0     0     32
  [24] .bss              NOBITS           0000000000bf4560  009f4560     000000000020c8f0  0000000000000000  WA       0     0     32
  [25] .comment          PROGBITS         0000000000000000  009f4560     0000000000000059  0000000000000001  MS       0     0     1
  [26] .shstrtab         STRTAB           0000000000000000  009f45b9     00000000000000e5  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

$ objdump -s -j .rodata /opt/SEGGER/JLink_V512f/libjlinkarm.so.5.12.6
[...]
 328ed0 31473137 00415439 3153414d 44323147  1G17.AT91SAMD21G
 328ee0 31374100 41543931 53414d44 32314731  17A.AT91SAMD21G1
 328ef0 37415500 41543931 53414d44 32314731  7AU.AT91SAMD21G1
 328f00 38004154 53414d44 32314731 38004154  8.ATSAMD21G18.AT
 328f10 39315341 4d443231 47313841 00415453  91SAMD21G18A.ATS
 328f20 414d4432 31473138 41550041 54393153  AMD21G18AU.AT91S
 328f30 414d4432 314a3134 00415453 414d4432  AMD21J14.ATSAMD2
 328f40 314a3134 00415439 3153414d 4432314a  1J14.AT91SAMD21J
[...]

Start Debugging in Assembler

Command Line Programming and Debugging with GDB
Registers
Source and Machine Code
Examining Source Files

$ /opt/SEGGER/JLink_V512f/JLinkGDBServer -device ATSAMD21G18 -if SWD -speed 4000

$ arm-none-eabi-gdb 
(gdb) target remote :2331
(gdb) monitor reset
(gdb) si
(gdb) info registers
r0             0x2000005c	536871004
r1             0x0	0
r2             0x20000000	536870912
r3             0x42001400	1107301376
r4             0x200003e8	536871912
r5             0x20	32
r6             0xc	12
r7             0x20000400	536871936
r8             0xffd9befd	4292460285
r9             0x75fdf1ff	1979576831
r10            0xbfe7fffb	3219652603
r11            0xd373ffff	3547594751
r12            0x0	0
sp             0x20007fec	0x20007fec
lr             0x4fc5	20421
pc             0x652	0x652
xpsr           0x41000000	1090519040
MSP            0x20007fec	536903660
PSP            0x7f6fefec	2138042348
PRIMASK        0x0	0
BASEPRI        0x0	0
FAULTMASK      0x0	0
CONTROL        0x0	0

(gdb) p/x $pc
$1 = 0x652

(gdb) x/5i $pc
=> 0x64e:	ldr	r0, [pc, #60]	; (0x68c)
   0x650:	push	{r3, r4, r5, lr}
   0x652:	cmp	r2, r0
   0x654:	bne.n	0x660
   0x656:	ldr	r2, [pc, #56]	; (0x690)

Linux AT91SAM7

[  857.978356] usb 1-1: new full-speed USB device number 7 using xhci_hcd
[  858.127257] usb 1-1: New USB device found, idVendor=03eb, idProduct=6124
[  858.127264] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[  858.128567] cdc_acm 1-1:1.0: ttyACM1: USB ACM device

Linux udev


$ file /opt/SEGGER/JLink_V634h/libjlinkarm.so.6.34.8 
/opt/SEGGER/JLink_V634h/libjlinkarm.so.6.34.8: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=2a5fdec57f4c7979a8f453bc87e3f4f11a2b58c9, stripped

$ file /opt/SEGGER/JLink_V634h/libjlinkarm_x86.so.6.34.8 
/opt/SEGGER/JLink_V634h/libjlinkarm_x86.so.6.34.8: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=87bcf47d3d0a1ce17be9b08d935ca62451c68658, stripped

$ /opt/SEGGER/JLink_V634h/JLinkExe 
SEGGER J-Link Commander V6.34h (Compiled Oct  9 2018 15:50:29)
DLL version V6.34h, compiled Oct  9 2018 15:50:01

Connecting to J-Link via USB...Updating firmware:  J-Link
Replacing firmware: J-Link
FAILED: Failed to activate bootloader (timeout)
J-Link>

$ sudo udevadm control --reload-rules

$ udevadm monitor --udev
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing

UDEV  [3055.482775] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [3055.489309] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV  [3055.489419] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [3055.493441] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV  [3055.496803] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM1 (tty)
UDEV  [3055.500750] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.2 (usb)
UDEV  [3055.506381] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [3055.511787] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)

UDEV  [3078.917122] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV  [3078.919208] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.2 (usb)
UDEV  [3078.921420] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV  [3078.922618] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM1 (tty)
UDEV  [3078.928998] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [3078.931900] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV  [3078.936413] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [3078.941354] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)

lwIP internals

lwipopts.h
Native API (RAW) (see also lwip-1.4.1/doc/rawapi.txt)
IPv6
LwIP IPv4/IPv6 stacks
Available device drivers
LwIP with or without an operating system
Porting For Bare Metal
Porting for an OS

Nios updated port and demo for NiosEDS 11.0 and up (includes LwIP)
Using Lightweight IP with the Nios II Processor Tutorial

Commiter

http://git.savannah.gnu.org/cgit/lwip.git
http://git.savannah.gnu.org/cgit/lwip/lwip-contrib.git

The Big eCommerce Conference: Simon Goldschmidt
Facebook: Simon Goldschmidt

VLAN

Mailinglist

Add support for outgoing VLAN tags?
from Simon Goldschmidt
from address@hidden
from Dance, Brian
from web

Tasks

task #9033 Support IEEE 802.1q tagged frame (VLAN) (Commited: 21.012.2009, Done: 25.08.2009)
task #11620 Add outgoing VLAN PCP support for Ethernet level QoS (Commited: 01.12.2011, Done: 01.12.2011)

Patches

patch #7993 Add support for transmitting packets with VLAN headers (Commited: 03.04.2013, Done: 20.02.2014)

Code

/**
 * ETHARP_SUPPORT_VLAN==1: support receiving and sending ethernet packets with
 * VLAN header. See the description of LWIP_HOOK_VLAN_CHECK and
 * LWIP_HOOK_VLAN_SET hooks to check/set VLAN headers.
 * Additionally, you can define ETHARP_VLAN_CHECK to an u16_t VLAN ID to check.
 * If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted.
 * If ETHARP_VLAN_CHECK is not defined, all traffic is accepted.
 * Alternatively, define a function/define ETHARP_VLAN_CHECK_FN(eth_hdr, vlan)
 * that returns 1 to accept a packet or 0 to drop a packet.
 */
#ifndef ETHARP_SUPPORT_VLAN
#define ETHARP_SUPPORT_VLAN             1
#endif

/**
 * LWIP_HOOK_VLAN_CHECK(netif, eth_hdr, vlan_hdr):
 * - called from ethernet_input() if VLAN support is enabled
 * - netif: struct netif on which the packet has been received
 * - eth_hdr: struct eth_hdr of the packet
 * - vlan_hdr: struct eth_vlan_hdr of the packet
 * Return values:
 * - 0: Packet must be dropped.
 * - != 0: Packet must be accepted.
 */

/**
 * LWIP_HOOK_VLAN_SET(netif, eth_hdr, vlan_hdr):
 * - called from etharp_raw() and etharp_send_ip() if VLAN support is enabled
 * - netif: struct netif that the packet will be sent through
 * - eth_hdr: struct eth_hdr of the packet
 * - vlan_hdr: struct eth_vlan_hdr of the packet
 * Return values:
 * - 0: Packet shall not contain VLAN header.
 * - != 0: Packet shall contain VLAN header.
 * Hook can be used to set prio_vid field of vlan_hdr.
 */
#if ETHARP_SUPPORT_VLAN
  if (type == PP_HTONS(ETHTYPE_VLAN)) {
    struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
    if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {
      /* a packet with only an ethernet/vlan header (or less) is not valid for us */
      ETHARP_STATS_INC(etharp.proterr);
      ETHARP_STATS_INC(etharp.drop);
      goto free_and_return;
    }
#if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */
#ifdef LWIP_HOOK_VLAN_CHECK
    if (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) {
#elif defined(ETHARP_VLAN_CHECK_FN)
    if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) {
#elif defined(ETHARP_VLAN_CHECK)
    if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
#endif
      /* silently ignore this packet: not for our VLAN */
      pbuf_free(p);
      return ERR_OK;
    }
#endif /* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */
    type = vlan->tpid;
    ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
  }
#endif /* ETHARP_SUPPORT_VLAN */

STM32 & FreeRTOS

lwIP replies ping request with reply of an earlier request (packages out off sync)

Nios II

Nios II + LWIP_RAND() + IGMP

IPv4 (deprecated)

IPv4

/* This is the aligned version of ip_addr_t,
   used as local variable, on the stack, etc. */
struct ip_addr {
  u32_t addr;
};

/* This is the packed version of ip_addr_t,
   used in network headers that are itself packed */
struct ip_addr_packed {
  PACK_STRUCT_FIELD(u32_t addr);
};

/*
 * struct ipaddr2 is used in the definition of the ARP packet format in
 * order to support compilers that don't have structure packing.
 */
struct ip_addr2 {
  PACK_STRUCT_FIELD(u16_t addrw[2]);
};

/** ip_addr_t uses a struct for convenience only, so that the same defines can
 * operate both on ip_addr_t as well as on ip_addr_p_t. */
typedef struct ip_addr ip_addr_t;
typedef struct ip_addr_packed ip_addr_p_t;
/**
 * This function is called by the network interface device driver when
 * an IP packet is received. The function does the basic checks of the
 * IP header such as packet size being at least larger than the header
 * size etc. If the packet was not destined for us, the packet is
 * forwarded (using ip_forward). The IP checksum is always checked.
 *
 * Finally, the packet is sent to the upper layer protocol input function.
 * 
 * @param p the received IP packet (p->payload points to IP header)
 * @param inp the netif on which this packet was received
 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
 *         processed, but currently always returns ERR_OK)
 */
err_t
ip_input(struct pbuf *p, struct netif *inp)
{
    [...]
}

IPv6

Neighbor Discovery Protocol (NDP)

/** Router advertisement message header. */
struct ra_header {
  PACK_STRUCT_FIELD(u8_t         type);
  PACK_STRUCT_FIELD(u8_t         code);
  PACK_STRUCT_FIELD(u16_t        chksum);
  PACK_STRUCT_FIELD(u8_t         current_hop_limit);
  PACK_STRUCT_FIELD(u8_t         flags);
  PACK_STRUCT_FIELD(u16_t        router_lifetime);
  PACK_STRUCT_FIELD(u32_t        reachable_time);
  PACK_STRUCT_FIELD(u32_t        retrans_timer);
  /* Options follow. */
} PACK_STRUCT_STRUCT;

#define ND6_OPTION_TYPE_SOURCE_LLADDR <--
#define ND6_OPTION_TYPE_MTU
#define ND6_OPTION_TYPE_PREFIX_INFO   <--
#define ND6_OPTION_TYPE_ROUTE_INFO

#define ND6_PREFIX_FLAG_ON_LINK        (0x80)
#define ND6_PREFIX_FLAG_AUTONOMOUS     (0x40)
#define ND6_PREFIX_FLAG_ROUTER_ADDRESS (0x20)
#define ND6_PREFIX_FLAG_SITE_PREFIX    (0x10)

struct prefix_option {
  PACK_STRUCT_FIELD(u8_t         type);
  PACK_STRUCT_FIELD(u8_t         length);
  PACK_STRUCT_FIELD(u8_t         prefix_length);
  PACK_STRUCT_FIELD(u8_t         flags);
  PACK_STRUCT_FIELD(u32_t        valid_lifetime);
  PACK_STRUCT_FIELD(u32_t        preferred_lifetime);
  PACK_STRUCT_FIELD(u8_t         reserved2[3]);
  PACK_STRUCT_FIELD(u8_t         site_prefix_length);
  PACK_STRUCT_FIELD(ip6_addr_p_t prefix);
} PACK_STRUCT_STRUCT;

struct lladdr_option {
  PACK_STRUCT_FIELD(u8_t         type);
  PACK_STRUCT_FIELD(u8_t         length);
  PACK_STRUCT_FIELD(u8_t         addr[NETIF_MAX_HWADDR_LEN]);
} PACK_STRUCT_STRUCT;

enum nd6_neighbor_cache_entry_state {
  ND6_NO_ENTRY = 0,
  ND6_INCOMPLETE,
  ND6_REACHABLE,
  ND6_STALE,
  ND6_DELAY,
  ND6_PROBE
};

/* Struct for tables. */
struct nd6_neighbor_cache_entry {
  ip6_addr_t           next_hop_address;
  struct netif        *netif;
  u8_t                 lladdr[NETIF_MAX_HWADDR_LEN];
  /** Pointer to queue of pending outgoing packets on this entry. */
  struct nd6_q_entry  *q;
  u8_t                 state;
  u8_t                 isrouter;
  union {
    u32_t              reachable_time;
    u32_t              delay_time;
    u32_t              probes_sent;
    u32_t              stale_time;
  }                    counter;
};

default_router_list[i];
prefix_list[i];
neighbor_cache[i];
neighbor_cache[neighbor_index];

static void nd6_send_q(s8_t i)
Thread [1] (Suspended)	
	12 nd6_new_onlink_prefix() nd6.c:1401 0x100277c8	
	11 nd6_input() nd6.c:466 0x10024570
           [...]

Thread [1] (Suspended)	
	12 nd6_new_router() nd6.c:1333 0x10027464	
	11 nd6_input() nd6.c:386 0x1002413c	
	10 icmp6_input() icmp6.c:116 0x1001dd40	
	9 ip6_input() ip6.c:689 0x1001f9b0	
	8 ethernet_input() etharp.c:1465 0x1003da38	
	7 lwip_enet_input() lwip_enet.c:74 0x10015d48	
	6 ptp2_port_receive() ptp2_port.c:315 0x10011f90	
	5 ptp2_port_task_rx() ptp2_port.c:239 0x10011bf8	
	4 ptp2_master_mainloop() ptp2_master.c:142 0x1000d6b4	
	3 main() main.c:73 0x1003dbfc	
	2 alt_main() alt_main.c:154 0x1004f77c	
	1 _start() crt0.S:437 0x10000204	

Ping / ICMP

ICMP
Is there and ‘ping’ function?
ICMP ping send/receive

ARP

ARP
RFC 826: An Ethernet Address Resolution Protocol

/**
 * LWIP_ARP==1: Enable ARP functionality.
 */
#ifndef LWIP_ARP
#define LWIP_ARP                        1
#endif

/**
 * ARP_TABLE_SIZE: Number of active MAC-IP address pairs cached.
 */
#ifndef ARP_TABLE_SIZE
#define ARP_TABLE_SIZE                  10
#endif

/**
 * ARP_QUEUEING==1: Multiple outgoing packets are queued during hardware address
 * resolution. By default, only the most recent packet is queued per IP address.
 * This is sufficient for most protocols and mainly reduces TCP connection
 * startup time. Set this to 1 if you know your application sends more than one
 * packet in a row to an IP address that is not in the ARP cache.
 */
#ifndef ARP_QUEUEING
#define ARP_QUEUEING                    1
#endif

/**
 * ETHARP_TRUST_IP_MAC==1: Incoming IP packets cause the ARP table to be
 * updated with the source MAC and IP addresses supplied in the packet.
 * You may want to disable this if you do not trust LAN peers to have the
 * correct addresses, or as a limited approach to attempt to handle
 * spoofing. If disabled, lwIP will need to make a new ARP request if
 * the peer is not already in the ARP table, adding a little latency.
 * The peer *is* in the ARP table if it requested our address before.
 * Also notice that this slows down input processing of every IP packet!
 */
#ifndef ETHARP_TRUST_IP_MAC
#define ETHARP_TRUST_IP_MAC             1
#endif

/**
 * ETHARP_SUPPORT_VLAN==1: support receiving ethernet packets with VLAN header.
 * Additionally, you can define ETHARP_VLAN_CHECK to an u16_t VLAN ID to check.
 * If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted.
 * If ETHARP_VLAN_CHECK is not defined, all traffic is accepted.
 * Alternatively, define a function/define ETHARP_VLAN_CHECK_FN(eth_hdr, vlan)
 * that returns 1 to accept a packet or 0 to drop a packet.
 */
#ifndef ETHARP_SUPPORT_VLAN
#define ETHARP_SUPPORT_VLAN             0
#endif
/** the ARP message, see RFC 826 ("Packet format") */
struct etharp_hdr {
  PACK_STRUCT_FIELD(u16_t hwtype);
  PACK_STRUCT_FIELD(u16_t proto);
  PACK_STRUCT_FIELD(u8_t  hwlen);
  PACK_STRUCT_FIELD(u8_t  protolen);
  PACK_STRUCT_FIELD(u16_t opcode);
  PACK_STRUCT_FIELD(struct eth_addr shwaddr);
  PACK_STRUCT_FIELD(struct ip_addr2 sipaddr);
  PACK_STRUCT_FIELD(struct eth_addr dhwaddr);
  PACK_STRUCT_FIELD(struct ip_addr2 dipaddr);
} PACK_STRUCT_STRUCT;
/**
 * Update (or insert) a IP/MAC address pair in the ARP cache.
 *
 * If a pending entry is resolved, any queued packets will be sent
 * at this point.
 * 
 * @param netif netif related to this entry (used for NETIF_ADDRHINT)
 * @param ipaddr IP address of the inserted ARP entry.
 * @param ethaddr Ethernet address of the inserted ARP entry.
 * @param flags @see definition of ETHARP_FLAG_*
 *
 * @return
 * - ERR_OK Succesfully updated ARP cache.
 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
 *
 * @see pbuf_free()
 */
static err_t
etharp_update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
{
    [...]
}
  /* ARP message directed to us?
      -> add IP address in ARP cache; assume requester wants to talk to us,
         can result in directly sending the queued packets for this host.
     ARP message not directed to us?
      ->  update the source IP address in the cache, if present */
  etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
                   for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
Thread [1] (Suspended)	
	10 lwip_enet_low_level_output() lwip_enet.c:131 0x1002e640 [via netif->linkoutput()]
	9 etharp_arp_input() etharp.c:807 0x1002d374	
	8 ethernet_input() etharp.c:1372 0x1002e38c [via netif->input()]
	7 lwip_enet_input() lwip_enet.c:74 0x1002e4d0	
	6 ptp2_port_receive() ptp2_port.c:301 0x10011f48	
	5 ptp2_port_task_rx() ptp2_port.c:214 0x10011bac	
	4 ptp2_master_mainloop() ptp2_master.c:142 0x1000d6f8	
	3 main() main.c:73 0x1002e92c	
	2 alt_main() alt_main.c:154 0x10040340	
	1 _start() crt0.S:437 0x10000204	

Memory Pools – Dynamic pool memory manager

lwIP has dedicated pools for many structures (netconn, protocol control blocks, packet buffers, …). All these pools are managed here.

/**
 * MEM_LIBC_MALLOC==1: Use malloc/free/realloc provided by your C-library
 * instead of the lwip internal allocator. Can save code size if you
 * already use it.
 */
#ifndef MEM_LIBC_MALLOC
#define MEM_LIBC_MALLOC                 0
#endif

/**
* MEMP_MEM_MALLOC==1: Use mem_malloc/mem_free instead of the lwip pool allocator.
* Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution
* speed and usage from interrupts!
*/
#ifndef MEMP_MEM_MALLOC
#define MEMP_MEM_MALLOC                 0
#endif

/**
 * MEMP_SEPARATE_POOLS: if defined to 1, each pool is placed in its own array.
 * This can be used to individually change the location of each pool.
 * Default is one big array for all pools
 */
#ifndef MEMP_SEPARATE_POOLS
#define MEMP_SEPARATE_POOLS             0
#endif
/**
 * MEM_USE_POOLS==1: Use an alternative to malloc() by allocating from a set
 * of memory pools of various sizes. When mem_malloc is called, an element of
 * the smallest pool that can provide the length needed is returned.
 * To use this, MEMP_USE_CUSTOM_POOLS also has to be enabled.
 */
#ifndef MEM_USE_POOLS
#define MEM_USE_POOLS                   0
#endif

On the fly enum memp_t { MEMP_ARP_QUEUE, […] }


/* Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */
typedef enum {
#define LWIP_MEMPOOL(name,num,size,desc)  MEMP_##name,
#include "lwip/memp_std.h"
  MEMP_MAX
} memp_t;

/* it not defines macros, but uses it */
LWIP_MEMPOOL(RAW_PCB,        MEMP_NUM_RAW_PCB,         sizeof(struct raw_pcb),        "RAW_PCB")
LWIP_MEMPOOL(UDP_PCB,        MEMP_NUM_UDP_PCB,         sizeof(struct udp_pcb),        "UDP_PCB")
LWIP_MEMPOOL(TCP_PCB,        MEMP_NUM_TCP_PCB,         sizeof(struct tcp_pcb),        "TCP_PCB")
LWIP_MEMPOOL(TCP_PCB_LISTEN, MEMP_NUM_TCP_PCB_LISTEN,  sizeof(struct tcp_pcb_listen), "TCP_PCB_LISTEN")
LWIP_MEMPOOL(TCP_SEG,        MEMP_NUM_TCP_SEG,         sizeof(struct tcp_seg),        "TCP_SEG")
LWIP_MEMPOOL(REASSDATA,      MEMP_NUM_REASSDATA,       sizeof(struct ip_reassdata),   "REASSDATA")
LWIP_MEMPOOL(ARP_QUEUE,      MEMP_NUM_ARP_QUEUE,       sizeof(struct etharp_q_entry), "ARP_QUEUE")
LWIP_MEMPOOL(SYS_TIMEOUT,    MEMP_NUM_SYS_TIMEOUT,     sizeof(struct sys_timeo),      "SYS_TIMEOUT")
/*
 * A list of pools of pbuf's used by LWIP.
 *
 * LWIP_PBUF_MEMPOOL(pool_name, number_elements, pbuf_payload_size, pool_description)
 *     creates a pool name MEMP_pool_name. description is used in stats.c
 *     This allocates enough space for the pbuf struct and a payload.
 *     (Example: pbuf_payload_size=0 allocates only size for the struct)
 */
LWIP_PBUF_MEMPOOL(PBUF,      MEMP_NUM_PBUF,            0,                             "PBUF_REF/ROM")
LWIP_PBUF_MEMPOOL(PBUF_POOL, PBUF_POOL_SIZE,           PBUF_POOL_BUFSIZE,             "PBUF_POOL")

#if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */

struct memp {
  struct memp *next;
};

/*====== re-defines LWIP_MEMPOOL each time  ======*/

/** This array holds the first free element of each pool.
 *  Elements form a linked list. */
static struct memp *memp_tab[MEMP_MAX];

/** This array holds the element sizes of each pool. */
static const u16_t memp_sizes[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEM_ALIGN_SIZE(size),
#include "lwip/memp_std.h"
};

/** This array holds the number of elements in each pool. */
static const u16_t memp_num[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc)  (num),
#include "lwip/memp_std.h"
};

/** This is the actual memory used by the pools (all pools in one big block). */
static u8_t memp_memory[MEM_ALIGNMENT - 1 
#define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
#include "lwip/memp_std.h"
];

/**
 * Get an element from a specific pool.
 *
 * @param type the pool to get an element from
 *
 * @return a pointer to the allocated memory or a NULL pointer on error
 */
void *
memp_malloc(memp_t type)
{
    [...]
}

Mutexes and Semaphores

/**
 * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
 * critical regions during buffer allocation, deallocation and memory
 * allocation and deallocation.
 */
#ifndef SYS_LIGHTWEIGHT_PROT
#define SYS_LIGHTWEIGHT_PROT            0
#endif
#if SYS_LIGHTWEIGHT_PROT

/** SYS_ARCH_DECL_PROTECT
 * declare a protection variable. This macro will default to defining a variable of
 * type sys_prot_t. If a particular port needs a different implementation, then
 * this macro may be defined in sys_arch.h.
 */
#define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
/** SYS_ARCH_PROTECT
 * Perform a "fast" protect. This could be implemented by
 * disabling interrupts for an embedded system or by using a semaphore or
 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
 * already protected. The old protection level is returned in the variable
 * "lev". This macro will default to calling the sys_arch_protect() function
 * which should be implemented in sys_arch.c. If a particular port needs a
 * different implementation, then this macro may be defined in sys_arch.h
 */
#define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
/** SYS_ARCH_UNPROTECT
 * Perform a "fast" set of the protection level to "lev". This could be
 * implemented by setting the interrupt level to "lev" within the MACRO or by
 * using a semaphore or mutex.  This macro will default to calling the
 * sys_arch_unprotect() function which should be implemented in
 * sys_arch.c. If a particular port needs a different implementation, then
 * this macro may be defined in sys_arch.h
 */
#define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)

#else

#define SYS_ARCH_DECL_PROTECT(lev)
#define SYS_ARCH_PROTECT(lev)
#define SYS_ARCH_UNPROTECT(lev)

#endif /* SYS_LIGHTWEIGHT_PROT */