Month: September 2017

Converting GPS Latitude and Longitude to 32 bits

 * Convert Latitude in DMS (Degrees Minutes Seconds) to 32-bit integer
 * between -162000000 and 162000000

#include < stdio.h>

int main(void)
    int degree;
    int min;
    double sec;
    double frac_sec;

    printf("Inform the latitude in this format XXº YY\' ZZ.ABCD\"\n");

    printf("Type the value of the Degree (XX): ");
    scanf("%d", &degree);

    if (degree < -90 || degree > 90)
          printf("The Degree value needs to be between -90 and 90!\n");
          return -1;

    printf("Type the value of the Minute (YY): ");
    scanf("%d", &min);

    if (min < 0 || min > 59)
          printf("The Minute value needs to be between 0 and 59!\n");
          return -1;

    printf("Type the value of the Seconds (ZZ.ABCD): ");
    scanf("%lf", &sec);

    if (sec < 0 || sec >= 60)
          printf("The Seconds value needs to be between 0 e 59.9999!\n");
          return -1;

    printf("%02dº %02d\' %02f\"\n", degree, min, sec);

    /* Convert everything to seconds and add fraction of seconds */

    frac_sec  = degree * 60 * 60;
    frac_sec += (min * 60);
    frac_sec += sec;

    /* Multiply by 500 because the unit is in 1/500 seconds */

    frac_sec = frac_sec * 500;

    printf("Value converted = %d => 0x%08X!\n", (int) frac_sec, (int) frac_sec);

    return 0;



$ ./dmsto32b 
Inform the latitude in this format XXº YY' ZZ.ABCD"
Type the value of the Degree (XX): 30
Type the value of the Minute (YY): 15
Type the value of the Seconds (ZZ.ABCD): 57.1926
30º 15' 57.192600"
Value converted = 54478596 => 0x033F4704!

We can convert this value to Decimal Degrees dividing by 30000 (500 * 60) and then dividing by 60:

54478596 / 30000 = 1815,9532

1815,9532 / 60 = 30,265886667

Normally GPS position are represented on Decimal Degrees (as used by Google Maps), but these are the three most common options:

-27.587300, -48.437300 (DD)

-27° 35.238’, -48° 26.238’ (graus° minutos.Decimal’)

27° 35′ 14.28” S, 48° 26′ 14.28” W (DMS)

Testing RedBoard Duo board

Today I received my RedBoard Duo board! This board can communicate with the cloud and in fact it came with its firmware.

The board is just a little bit bigger than Particle Photon board. I missed a label near the SETUP and RESET button, it could make our life easier!

I started with the “Duo: Out-of-Box Experience” reading:

It suggest to install the DuoApp, then I did it:

$ adb install DuoApp_0.2.0.apk

After installing I didn’t find it in the Android application (I was expecting a RED BEAR), then I decided to list all installed applications using this command:

$ adb shell 'pm list packages -f'

Ok, it listed many packages, including the package:


Then I started it manually:

$ adb shell monkey -p net.redbear.redbearduo -c android.intent.category.LAUNCHER 1

The application expects you are connected to WiFi of your RedBear board (i.e. Duo-xxxx) then it scans for you router WiFi and ask your password. Finally it configures your RedBear automatically and connects it to Particle cloud, the LED starts glowing in Cyan. If configuration over WiFi fails you can enable the Bluetooth and it will try to configure it using BLE.

When the configuration is done it will show some informations, like the IP that RedBear got from your WiFi router. You can access this IP in the browser to turn-on or turn-off the Blue LED of your board.

Programming Smoothieboard V2 Mini

I’m testing the Smoothieboard V2 Mini

The JP10 (BOOT2) is in the position 0 (tied to ground) because I will program it using SWD instead using the LPCScript DFU/bootloader.

First we need to clone the repositories:

$ git clone
$ git clone

Enter inside “smoothie-nuttx” and compile the nuttx for smoothie v2 mini alpha:

$ cd smoothie-nuttx
$ cd nuttx
$ cd tools
$ ./ smoothiev2-mini-alpha/smoothiedev
$ cd ..
$ make export
CC:  lpc43_ostest.c
CC:  lpc43_autoleds.c
CC:  lpc43_timer.c
AR:   lpc43_boot.o lpc43_appinit.o lpc43_mmcsd.o lpc43_highpri.o lpc43_ostest.o lpc43_autoleds.o lpc43_timer.o 
make[2]: Leaving directory '/smoothie-nuttx/nuttx/configs/smoothiev2-mini-alpha/src'
make[1]: Leaving directory '/smoothie-nuttx/nuttx/arch/arm/src'

Just confirm the the file “” was created correctly:

$ ls -l 
-rw-rw-r-- 1 alan alan 2932905 Set  3 20:00

We need to copy this file to smoothie-v2 directory to compile the final firmware:

$ cp ../../smoothie-v2/Firmware/

Then go to there and decompress the nuttx-export:

$ cd ../../smoothie-v2/Firmware/
$ unzip
  inflating: nuttx-export/include/machine/_types.h  
  inflating: nuttx-export/include/machine/ieeefp.h  
  inflating: nuttx-export/include/math.h

Case you are using a version of gcc crosscompiler different from 6.3.1:

$ arm-none-eabi-gcc -v
Thread model: single
gcc version 6.3.1 20170620 (release) [ARM/embedded-6-branch revision 249437] (GNU Tools for ARM Embedded Processors 6-2017-q2-update) 

Then you need to export the variable the “ARMVERSION” with your version, see the Rakefile for more info.

We can compile the Unit Tests just executing:

$ rake testing=1
Compiling /tmp/smoothie-v2/Firmware/TestUnits/TEST_timer1.cpp for Bambino
Compiling /tmp/smoothie-v2/Firmware/TestUnits/main.cpp for Bambino
Linking for Bambino
   text	   data	    bss	    dec	    hex	filename
 394553	   1128	  11800	 407481	  637b9	smoothiev2_Bambino/smoothiev2.elf

Finally enter inside “smoothiev2_Bambino” and flash the firmware.

I’m using a STLink-v2 clone (low cost: about U$ 2.00) with only three pins: SWDIO, SWCLK and GND connected to JTAG header of the board:

$ cd smoothiev2_Bambino
$ sudo openocd -f interface/stlink-v2.cfg -f target/lpc4330.cfg -c init -c "reset halt" -c "flash write_image erase smoothiev2.bin 0x14000000"
Open On-Chip Debugger 0.10.0+dev-00172-g7719e96 (2017-08-24-16:57)
Licensed under GNU GPL v2
For bug reports, read
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.270400
Info : lpc4350.m4: hardware has 6 breakpoints, 4 watchpoints
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)
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x10000154 msp: 0x10087ff0
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x10000198 msp: 0x10087ff0
wrote 458752 bytes from file smoothiev2.bin in 12.150717s (36.870 KiB/s)

Connect the USB/Serial 3.3V adapter to UART0 header pin and you will get:

Starting tests...
There are 35 registered tests...
[(enable, false)]
elapsed time 20000 us
             total       used       free    largest
Mem:        191152      41840     149312     115232
/home/alan/nuttxspace/SmoothieProject/smoothie-v2/Firmware/TestUnits/TEST_sdcard.cpp:26:SDCardTest-mount:FAIL: Exp1

20 Tests 1 Failures 0 Ignored 

Keeping your bitbucket repo in sync with upstream

I imported the NuttX repository to my bitbucket account with name nuttx_nyx (for NyX board). Then I want to keep it in sync with mainline/upstream without accessing the bitbucket page to click on “Sync” button.

So you just need to add the upstream remote:

$ git remote add upstream

Then we have two remotes now:

$ git remote -v

origin (fetch)
origin (push)

upstream (fetch)
upstream (push)

Then when you fetch from upstream it will be putted on you origin/master:

$ git fetch upstream
   afe137f..fe7d8c9  master     -> origin/master

Fine, now just do the default way:

$ git push origin master

You can create your local commit and rebase it with be on top of upstream and saved to your repository.