SPRESENSE / NuttX Tutorial Walk-Through

## Overview

this is just a quick walk-through to bring up [NuttX](https://nuttx.apache.org/) on [SPRESENSE](https://developer.sony.com/develop/spresense/) with some add-on boards and sample applications. it does not include NuttX internals and features description, hardware or device drivers.

the followings are in scope in this note.

- hardware equipment and setup
- development environment and tools
- hello world sample
- IMU sample (gyro and accel)
- Wi-Fi access setup (W.I.P)

<img src="https://www.aps-web.jp/wp-data/wp-content/uploads/2020/04/acd-amp-13-fig00-1024x576.jpg" width="500">

## Hardware

### Devices / Sensors

In this note, the following physical hardware devices are required.

- [SPRESENSE main board](https://www.switch-science.com/catalog/3900/)
- [SPRESENSE ext board](https://www.switch-science.com/catalog/3901/)
- [IMU BMI160](https://www.switch-science.com/catalog/5258/)
- [Wi-Fi add-on board](https://idy-design.com/product/is110b.html)

### Setup

we can go along with [introduction](https://developer.sony.com/develop/spresense/developer-tools/introduction/) to set up main and extension board to activate. other device specifications are included above device pages, connect PINs accordingly to extension board.

## Development

### SPRESENSE SDK

refer to https://developer.sony.com/develop/spresense/docs/sdk_set_up_en.html, we can config and build nuttx with hello-world sample w/o any problems.

```nsh
Welcome to minicom 2.7.1

OPTIONS: I18n 
Compiled on Dec 23 2019, 02:06:26.
Port /dev/ttyUSB0, 21:05:52

Press CTRL-A Z for help on special keys


NuttShell (NSH) NuttX-8.2
nsh> hello
Hello, World!!
```

if you want to use `minicom` instead of `screen` command, here is the rc-script for `minicom`.

```bash
>cat .minirc.spresense 
pu port		/dev/ttyUSB0
pu baudrate	115200
pu bits		8
pu parity	No
pu stopbits	1
pu rtscts	No
pu xonxoff	No

>minicom spresense
```

### IMU sample application

there is already `defconfig` (predefined configuration) available for IMU BMI160 sample.

```bash
>cd spresense/sdk
>tools/config.py -l | grep sixaxis
	examples/sixaxis
>tools/config.py examples/sixaxis
>make
...<snip>
>tools/flash.sh -c /dev/ttyUSB0 nuttx.spk
>>> Install files ...
install -b 115200
Install nuttx.spk
|0%-----------------------------50%------------------------------100%|
######################################################################

150464 bytes loaded.
Package validation is OK.
Saving package to "nuttx"
updater# sync
updater# Restarting the board ...
reboot
>minicom spresense

NuttShell (NSH) NuttX-8.2
nsh> sixaxis
...
[3503] -2, -10, -7 / -260, 12, 16230
[3504] -4, -9, -7 / -265, 22, 16256
[3505] -3, -8, -8 / -233, 38, 16282
[3506] -2, -8, -6 / -277, 8, 16251
[3507] -2, -8, -6 / -300, 43, 16258
[3508] -4, -9, -6 / -224, 75, 16287
...
```

the application code is [sixaxis](https://github.com/sonydevworld/spresense-nuttx-apps/blob/10b7b8c4a97b8999912a1948e365c6aab6d6123c/examples/bmi160/sixaxis_main.c), apply following patch and rebuild & run.

```patch
diff --git a/examples/bmi160/sixaxis_main.c b/examples/bmi160/sixaxis_main.c
index 66a2ab79..8f36f00e 100644
--- a/examples/bmi160/sixaxis_main.c
+++ b/examples/bmi160/sixaxis_main.c
@@ -40,6 +40,7 @@
 #include <nuttx/config.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <unistd.h>
 
 #include <nuttx/sensors/bmi160.h>
 
@@ -79,6 +80,7 @@ int sixaxis_main(int argc, char *argv[])
     {
       int ret;
 
+      usleep(100000);
       ret = read(fd, &data, sizeof(struct accel_gyro_st_s));
       if (ret != sizeof(struct accel_gyro_st_s))
         {
@@ -90,7 +92,7 @@ int sixaxis_main(int argc, char *argv[])
 
       if (prev != data.sensor_time)
         {
-          printf("[%d] %d, %d, %d / %d, %d, %d\n",
+          printf("[TIME: %d] gyro: %8d, %8d, %8d / accel: %8d, %8d, %8d\n",
                  data.sensor_time,
                  data.gyro.x, data.gyro.y, data.gyro.z,
                  data.accel.x, data.accel.y, data.accel.z);
```

### Wi-Fi add-on

as far as i see, there is no defconfig preset, so that we need to configure NuttX and application manually.
luckily, there seems to be a [gs2200m sample application](https://github.com/sonydevworld/spresense-nuttx-apps/tree/10b7b8c4a97b8999912a1948e365c6aab6d6123c/wireless/gs2200m) ready.

currently at this moment, there is a problem for device configuration.
details: https://github.com/apache/incubator-nuttx/issues/3537

1st, we need to import https://github.com/Barry-Xu-2018/spresense-nuttx/commit/12135640469360c128456492a19efc887c2b2e23

then,

```bash
>tools/config.py feature/wifi
>tools/config.py -m (configure your device type ver1.0A or B or C)
>make
```

you should be able to enable wireless network as following.

```bash
nsh> gs2200m <SSID> <PASSWORD> &
gs2200m [9:50]

nsh> ifconfig
eth0    Link encap:Ethernet HWaddr 3c:a0:67:59:3c:81 at UP
        inet addr:192.168.100.129 DRaddr:192.168.100.1 Mask:255.255.255.0
```

## Reference

- [Apache NuttX](https://nuttx.apache.org/)
- [NuttX mainline source code](https://bitbucket.org/patacongo/nuttx/src/master/)
- [Sony SPRESENSE](https://developer.sony.com/develop/spresense/)
- [Sony Developer Site](https://developer.sony.com/develop/spresense/docs/sdk_set_up_en.html)