Category: BrainDamage

Using coroutine on Lua

The Lua documentation about coroutine is not much clear and the example is not much useful. Fortunately I found an explanation how to use it and simplify the idea here:

sleep_awhile = function (period)
        os.execute("sleep "..period)

func_a = function()
        nnew = 0
        while true do
           nnew = nnew + 1
           print ("func_a ", nnew)
           print ("AGAIN ...")

func_b = function()
        count = 0
        while true do
           count = count + 1
           print ("func_b ", count)

-- ======= Main code =============

cor_a = coroutine.create(func_a)
cor_b = coroutine.create(func_b)

while true do
        itdidit,newcount = coroutine.resume(cor_a)
        print (newcount)
        itdidit = coroutine.resume(cor_b)

I hope it be useful for you too.


Running external application on NuttX

Normally NuttX supports internal (Built-In) applications embedded on its firmware. When you run: “nsh> hello” or your application, NuttX look for the application inside the internal flash of the microcontroller and run it.

But NuttX also supports running external applications. These external applications are ELF files. The ELF format is the default executable format used on Linux and almost all Unix OS.

Normally the OS needs to create a Global Symbol Table (AKA: symtab) where the ELF look at to translate the used functions of the ELF binary to memory addresses where these functions exist in the OS.

The “drawback” of this symtab is because it will waste a lot of space inside the flash (firmware) of the OS.

Fortunately NuttX also supports an alternative solution where this symtab is not needed. The ELF binary is created with the fixed memory position of each function address position. The drawback of this solution is the ELF binary is not relocatable, in other words: if you compile a new firmware for your board the existent external ELF binary could to fail because these functions positions are located at a different position now.

You can find the instructions for both options here:

I tested the “no-symtab” option loading an external “hello” from a USB Flash Drive:

NuttShell (NSH)
nsh> ls /dev
nsh> ?
help usage:  help [-v] []

  [           dirname     help        mh          set         unset       
  ?           dd          hexdump     mount       sh          usleep      
  basename    df          kill        mv          sleep       xd          
  break       echo        ls          mw          test        
  cat         exec        mb          ps          time        
  cd          exit        mkdir       pwd         true        
  cp          false       mkfatfs     rm          uname       
  cmp         free        mkrd        rmdir       umount      

Builtin Apps:
nsh> hello
Hello, World!!

nsh> mount -t vfat /dev/sda /bin
nsh> hello
Hello world from external binary!

As you can see initially the internal “hello” program was executed, but after I mounted the USB Flash Drive to /bin the external “hello” takes precedence over the internal.

These are the options I need to get it working:


Using BOOST-CC2564MODA with bluez on Linux

I’m trying to get the CC2564MODA working with NuttX, then I decided to test it with Linux’s bluez first to guarantee it is working.

I used a CP2101 USB/Serial dongle, connected RXD to BUTX1, TXD to BURX1, CTS to BURT1, RTS to BUCT1.

Unfortunately the CC2564B will not work correctly if you don’t load a patch to fix its internal firmware. I download the file from TI site extracted and copied it to:

$ sudo cp ~/CC2564/CC256XB_BT_SP/v1.6/initscripts-TIInit_6.7.16_ble_add-on.bts /lib/firmware/ti-connectivity/TIInit_6.7.16.bts

Next step is to use hciattach:

$ sudo hciattach -s 115200 /dev/ttyUSB0 texas 115200
Found a Texas Instruments' chip!
Firmware file : /lib/firmware/ti-connectivity/TIInit_6.7.16.bts
Loaded BTS script version 1
Device setup complete

Open a new linux terminal and run:

$  sudo btmon
Bluetooth monitor ver 5.37
= New Index: B0:B4:48:F4:A1:54 (BR/EDR,UART,hci1)               [hci1] 0.270465
= Open Index: B0:B4:48:F4:A1:54                                 [hci1] 0.270467
= Index Info: B0:B4:48:F4:A1:54 (Texas Instruments Inc.)        [hci1] 0.270467
= New Index: E0:06:E6:CF:FD:CE (BR/EDR,USB,hci0)                [hci0] 0.270468

Open another terminal and run:

$ sudo btmgmt --index 1

[hci1]# auto-power
Found controller with index 1

Note that I used “–index 1” instead “–index 0” because the BOOST-CC2564MODA was included as “hci1” (my laptop has Bluetooth internally).

Finally we can search for BLE devices:

[hci1]# find -l
Discovery started
hci1 type 6 discovering on
hci1 dev_found: BC:6A:29:AB:3F:46 type LE Public rssi -50 flags 0x0000 
AD flags 0x05 
eir_len 3
hci1 type 6 discovering off

How to install NuttX on Nordic nRF52832

Today I tested NuttX on Nordic nRF52832 and it worked fine!!!

I used this low cost nRF52832 module:

First you need to clone, compile and install a recent OpenOCD.

I used this mirror from github because I was facing issue with main repository:

$ git clone
$ cd openocd
$ ./configure --enable-aice --enable-amtjtagaccel --enable-armjtagew --enable-cmsis-dap --enable-dummy --enable-ftdi --enable-gw16012 --enable-jlink --enable-jtag_vpi --enable-opendous --enable-openjtag_ftdi --enable-osbdm --enable-legacy-ft2232_libftdi --enable-parport --disable-parport-ppdev --enable-parport-giveio --enable-presto_libftdi --enable-remote-bitbang --enable-rlink --enable-stlink --enable-ti-icdi --enable-ulink --enable-usb-blaster-2 --enable-usb_blaster_libftdi --enable-usbprog --enable-vsllink
$ make
$ sudo make install

Now we can clone, configure and compile NuttX on nRF52832:

$ git clone
$ git clone
$ cd nuttx
$ ./tools/ nrf52-pca10040/nsh
$ make menuconfig
$ make

$ ls -l nuttx.bin 
-rwxrwxr-x 1 alan alan 61016 Abr  3 20:07 nuttx.bin

$ arm-none-eabi-size nuttx
   text	   data	    bss	    dec	    hex	filename
  60793	    220	   1972	  62985	   f609	nuttx

Now we can flash nuttx.bin in the board.

To program this board I used a stlink-v2 clone. I connected the SWDIO (board pin 33), SWDCLK (board pin 32), GND (pin 9) and VCC (pin 10) to respective pins of ST-LINK V2 programmer. Note: I connected VCC to 3.3V.

$ sudo openocd -f interface/stlink-v2.cfg -f target/nrf52.cfg -c init -c "reset init" -c halt -c "nrf5 mass_erase" -c "program nuttx.bin verify" -c reset -c exit
Open On-Chip Debugger 0.10.0+dev-00376-g3d3b45a (2018-04-03-20:18)
Licensed under GNU GPL v2
For bug reports, read
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select '.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.225739
Info : nrf52.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x000009f4 msp: 0x20000400
Info : nRF52832-QFAA(build code: B0) 512kB Flash
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
auto erase enabled
Warn : using fast async flash loader. This is currently supported
Warn : only with ST-Link and CMSIS-DAP. If you have issues, add
Warn : "set WORKAREASIZE 0" before sourcing nrf51.cfg/nrf52.cfg to disable it
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000001e msp: 0xfffffffc
wrote 61440 bytes from file nuttx.bin in 1.536207s (39.057 KiB/s)
** Programming Finished **
** Verify Started **
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000002e msp: 0xfffffffc
verified 61016 bytes in 0.165886s (359.198 KiB/s)
** Verified OK **
Warn : Flash driver of nrf52.flash does not support free_driver_priv()
Warn : Flash driver of nrf52.uicr does not support free_driver_priv()

You can see the NuttX starting in the serial console configured to 115200 8n1, just use a USB/Serial of 3.3V connecting P0.24 (board pin 36) to RXD, the P0.23 (pin 35) to TXD and the GND (pin 9) to USB/Serial’s GND.