Tips to get Ethernet working on NuttX

I want to share here my experience to get Ethernet working on Bambino 200E board. In fact after you realize what needs to be done life become easy.

Almost all microcontroller with Ethernet MAC needs an external PHY chip to convert its signal to the physical media’s signal (in the twister pair cable). Normally this PHY communicate with the microcontroller using MII (Media-Independent Interface) or RMII (Reduced Media-Independent Interface) protocol.

So the first thing you need to find is the PHY Address used for your board’s PHY. The Bambino 200E uses the Micrel KSZ8031RNL and because there is not a Pull-UP resistor in the CRS_DV/PHYAD[1:0] the PHY Address will be 0, then you need to define it in your configuration:

CONFIG_LPC43_PHYADDR=0

And since the KSZ8031 support Auto Negotiation to detect the Ethernet mode and speed you need to define it:

CONFIG_LPC43_AUTONEG=y

Now comes the trick part, you need to setup the PHYSR (Status Register). The PHYSR is basically the address of the register where the PHY informs about the Auto Negotiation status.

So initially you could think it is the Basic Status register (at 1h), but the Basic Status is used to inform which modes and speeds your Ethernet router/hub/switch supports.

The right register in in fact the “PHY Control 1” (1Eh = 30). See the Operation Mode Indication field bits (1E.2:0) :

[000] = still in auto-negotiation
[001] = 10Base-T half-duplex
[010] = 100Base-TX half-duplex
[011] = reserved
[100] = reserved
[101] = 10Base-T full-duplex
[110] = 100Base-TX full-duplex
[111] = reserved

So, we need to define the following configuration to get Ethernet working:

CONFIG_LPC43_PHYSR=30
CONFIG_LPC43_PHYSR_ALTCONFIG=y
CONFIG_LPC43_PHYSR_ALTMODE=0x7
CONFIG_LPC43_PHYSR_10HD=0x1
CONFIG_LPC43_PHYSR_100HD=0x2
CONFIG_LPC43_PHYSR_10FD=0x5
CONFIG_LPC43_PHYSR_100FD=0x6
CONFIG_LPC43_RMII=y

I spent almost a working day to get Ethernet working, if I was aware of the above information it should matter of few minutes to get everything working.

CUDA compilation error

mkdir -p ./bin 
/usr/bin/nvcc -I/usr/include -O2 -Xcompiler -fPIC -o bin/out/ProgramCU.o src/pba/ProgramCU.cu -c
src/pba/ProgramCU.cu(131): warning: statement is unreachable

src/pba/ProgramCU.cu(131): warning: statement is unreachable

/usr/include/string.h: In function ‘void* __mempcpy_inline(void*, const void*, size_t)’:
/usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope
   return (char *) memcpy (__dest, __src, __n) + __n;
                                          ^
src/pba/ProgramCU.cu: In member function ‘void CuTexImage::BindTexture(textureReference&)’:
src/pba/ProgramCU.cu:41:80: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
     if(sz > MAX_TEXSIZE) fprintf(stderr, "cudaBindTexture: %d > %d\n", sz , MAX_TEXSIZE); 
                                                                                ^
src/pba/ProgramCU.cu: In function ‘bool ProgramCU::ShuffleCameraJacobian(CuTexImage&, CuTexImage&, CuTexImage&)’:
src/pba/ProgramCU.cu:2199:76: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
         fprintf(stderr, "datasize way too big %d, %d+...\n", szjc, (szjc)/ MAX_TEXSIZE); 
                                                                            ^
src/pba/ProgramCU.cu:2199:76: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
makefile:49: recipe for target 'bin/out/ProgramCU.o' failed
make: *** [bin/out/ProgramCU.o] Error 1

Fix: open the makefile and replace:

NVCC_FLAGS = -I$(CUDA_INC_PATH) -O2 -Xcompiler -fPIC

with:

NVCC_FLAGS = -I$(CUDA_INC_PATH) -O2 -D_FORCE_INLINES -Xcompiler -fPIC

Source: https://github.com/BVLC/caffe/issues/4046

error: template with C linkage

If you are facing this error then in some place in our code you have an ‘extern “C” {‘ including a C++ header, like in this example:

#include "someheader"
#include 

Then in your someheader.h you have:

extern "C" {
void function(void)
...

But it doesn’t have the final ‘}’ closing the ‘extern “C” {‘ block.

Reference: https://www.gidforums.com/t-9853.html

Discovering where a function “already declared in this scope” was declared

During the compilation you face this issue:

make[2]: Entering directory '/home/alan/nuttxspace/apps/examples/helloxx'
CXX:  helloxx_main.cxx
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^
/home/alan/nuttxspace/apps/Application.mk:85: recipe for target 'helloxx_main.o' failed
make[2]: *** [helloxx_main.o] Error 1
make[2]: Leaving directory '/home/alan/nuttxspace/apps/examples/helloxx'
Makefile:92: recipe for target 'examples/helloxx_all' failed
make[1]: *** [examples/helloxx_all] Error 2
make[1]: Leaving directory '/home/alan/nuttxspace/apps'
LibTargets.mk:174: recipe for target '../apps/libapps.a' failed
make: *** [../apps/libapps.a] Error 2

First thing to do is compile in verbose mode (make V=1) :

$ make V=1
...
make[2]: Entering directory '/home/alan/nuttxspace/apps/examples/helloxx'
CXX:  helloxx_main.cxx
arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^
/home/alan/nuttxspace/apps/Application.mk:85: recipe for target 'helloxx_main.o' failed
make[2]: *** [helloxx_main.o] Error 1
make[2]: Leaving directory '/home/alan/nuttxspace/apps/examples/helloxx'
Makefile:92: recipe for target 'examples/helloxx_all' failed
make[1]: *** [examples/helloxx_all] Error 2
make[1]: Leaving directory '/home/alan/nuttxspace/apps'
LibTargets.mk:174: recipe for target '../apps/libapps.a' failed
make: *** [../apps/libapps.a] Error 2

Copy the above g++ command line:

arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o

Enter inside the directory where the issue happened:

$ cd ../apps/examples/helloxx/

Paste the g++ line we copied and add “-save-temps” to it:

alan@pc:~/nuttxspace/apps/examples/helloxx$ arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o -save-temps
arm-none-eabi-g++: warning: -pipe ignored because -save-temps specified
In file included from /usr/include/newlib/c++/4.9.3/string:40:0,
                 from helloxx_main.cxx:42:
/usr/include/newlib/c++/4.9.3/bits/char_traits.h:63:20: error: 'mbstate_t' in namespace 'std' does not name a type
       typedef std::mbstate_t  state_type;
                    ^
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^

Confirm the .ii file was created:

$ ls *.ii
helloxx_main.ii

Open it:

$ vi helloxx_main.ii

Analyzing it we found:


# 42 "/usr/include/newlib/c++/4.9.3/type_traits" 3
namespace std
{
  typedef short unsigned int uint_least16_t;
  typedef long unsigned int uint_least32_t;
}


# 219 "/home/alan/nuttxspace/nuttx/include/stdint.h" 3 4
...
typedef _uint32_t uint_least32_t;


# 63 "/usr/include/newlib/machine/_default_types.h" 3 4
...
typedef long unsigned int __uint32_t;

Good, now we know the places where uint_least32_t was re-defined.

Thanks Greg Nutt for this tip!

Flash Micromint Bambino 200E board using OpenOCD

This is the way I flashed Bambino 200E board:

1) First create an OpenOCD “lpc4330.cfg” file:

$ sudo cp /usr/local/share/openocd/scripts/target/lpc4350.cfg /usr/local/share/openocd/scripts/target/lpc4330.cfg

Edit this file:

$ vi /usr/local/share/openocd/scripts/target/lpc4330.cfg

And add inside it:

flash bank SPIFI_FLASH lpcspifi 0x14000000 0 0 0 $_CHIPNAME.m4

Now just execute this command to flash “nuttx.bin” in sthe external SPI NOR Flash:

sudo openocd -f interface/stlink-v2.cfg -f target/lpc4330.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x14000000"

You should see this output:

Open On-Chip Debugger 0.10.0-dev-00371-g81631e4 (2016-09-26-12:35)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select '.
adapter speed: 500 kHz
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : clock speed 480 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.221508
Info : lpc4350.m4: hardware has 6 breakpoints, 4 watchpoints
lpc4350.m4: target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x10402c40 msp: 0x10087ff0
auto erase enabled
Info : Found flash device 'win w25q64cv' (ID 0x001740ef)
lpc4350.m4: target state: halted
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x10000154 msp: 0x10087ff0
lpc4350.m4: target state: halted
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x10000198 msp: 0x10087ff0
wrote 131072 bytes from file nuttx.bin in 3.775009s (33.907 KiB/s)

Source: https://sourceforge.net/p/openocd/mailman/message/31311198/