Month: June 2013

Snippet code for read manufacturer and device ID of W25X32 SPI NOR Flash on NuttX

This simple snippet of code demonstrate how to read the manufacturer and the device ID of a W25X32 SPI Nor flash using NuttX RTOS :

int spi_nor_flash(void)
{
        uint8_t tx[6], rx[6];

        struct spi_dev_s *spi = up_spiinitialize(0);

        SPI_SETBITS(spi, 8);

        tx[0] = 0x90;
        tx[1] = 0;
        tx[2] = 0;
        tx[3] = 0;
        tx[4] = 0;
        tx[5] = 0;

        kl_gpiowrite(PIN_SPI0_CS, false);
        SPI_EXCHANGE(spi, &tx, &rx, 6);
        kl_gpiowrite(PIN_SPI0_CS, true);

        printf("Manuf. = 0x%02X | Device = 0x%02X\n", rx[4], rx[5]);

        return 0;
}

This is output I got in the serial (DEBUG_SPI enable):

spi_setfrequency: Frequency 400000->428571
spi_exchange: txbuffer=1ffff9dc rxbuffer=1ffff9e4 nwords=6
Received = 0xFF 0xFF 0xFF 0xFF 0xEF 0x15

Where 0xEF is the Manufacturer ID and 0x15 is the Device ID returned.

Note: You need to define the pins PIN_SPI0_SCK, PIN_SPI0_MISO, PIN_SPI0_MOSI and PIN_SPI0_CS in the file ‘configs/freedom-kl25z/include/board.h’.

Como rastrear encomendas que o muambator nao rastreia?

Muitas vezes você recebe uma encomenda com o codigo “LN000000000US” ou LC000000000US mas que o Muambator mostra como “Tracking Indisponível”.

Normalmente estas encomendas são USPS e podem ser rastreadas pelo site:

https://tools.usps.com/go/TrackConfirmAction!input.action

Se a encomenda é “RC000000000HK” ele foi enviada via Hongkong Post e pode ser rastreada via:

http://app3.hongkongpost.com/CGI/mt/enquiry.jsp

Se “FH000000000GB” é Royal Mail e pode ser rastreada via:

http://track.royalmail.com

Se “LK000000000CN é Express Mail Service (EMS) e pode ser rastreada via:

http://www.ems.com.cn/ems/logistics/searchE

Se “RF000000000SG” é Singapore Post e pode ser rastreada via (pede data de envio):
http://singpost.com/contact-us.html?view=booking&flag=register

Felizmente existe um site que funciona para rastrear todas os tipos de encomendas listadas anteriormente:

http://www.17track.net/index_en.shtml

Outros links interessantes:
http://www.parceltrack.co.za
http://www.approachchina.com/shippinginfo.html

How to create a driver for NuttX?

If you want to create a simple driver for NuttX this tutorial is just for you.

I created a small board powered by kinetis KL25 and put NuttX to run on it, but this board has 4 LEDs for user interface, then I created a simple driver just to let an application to interact with them directly.

Here is my LED driver source code:

#include <nuttx/config.h>
#include <nuttx/arch.h>

#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>

#include "kl_gpio.h"

/****************************************************************************
 * HW access
 ****************************************************************************/

#define GPIO_LED_1 (GPIO_OUTPUT | GPIO_OUTPUT_ONE | PIN_PORTE | PIN2)
#define GPIO_LED_2 (GPIO_OUTPUT | GPIO_OUTPUT_ONE | PIN_PORTE | PIN3)
#define GPIO_LED_3 (GPIO_OUTPUT | GPIO_OUTPUT_ONE | PIN_PORTE | PIN4)
#define GPIO_LED_4 (GPIO_OUTPUT | GPIO_OUTPUT_ONE | PIN_PORTE | PIN5)


/****************************************************************************
 * LEDs: Fileops Prototypes and Structures
 ****************************************************************************/

typedef FAR struct file		file_t;

static int     leds_open(file_t *filep);
static int     leds_close(file_t *filep);
static ssize_t leds_read(file_t *filep, FAR char *buffer, size_t buflen);
static ssize_t leds_write(file_t *filep, FAR const char *buf, size_t buflen);

static const struct file_operations leds_ops = {
	leds_open,		/* open */
	leds_close,		/* close */
	leds_read,		/* read */
	leds_write,		/* write */
	0,			/* seek */
	0,			/* ioctl */
};

/****************************************************************************
 * LEDs: Fileops
 ****************************************************************************/

static int leds_open(file_t *filep)
{
	/* Nothing to do here, maybe I should increase a counter like for Linux driver? */

	return OK;
}

static int leds_close(file_t *filep)
{
	/* Nothing to do here, maybe I should decrease a counter like for Linux driver?*/

	return OK;
}

static ssize_t leds_read(file_t *filep, FAR char *buf, size_t buflen)
{
	register uint8_t reg;

	if(buf == NULL || buflen < 1)
		/* Well... nothing to do */
		return -EINVAL;

	/* These LEDs are actived by low signal (common anode), then invert signal we read*/
	reg = ~(kl_gpioread(GPIO_LED_4));
	reg = (reg << 1) | ~(kl_gpioread(GPIO_LED_3));
	reg = (reg << 1) | ~(kl_gpioread(GPIO_LED_2));
	reg = (reg << 1) | ~(kl_gpioread(GPIO_LED_1));
	reg = reg & 0x0F;

	*buf = (char) reg;

	return 1;
}

static ssize_t leds_write(file_t *filep, FAR const char *buf, size_t buflen)
{
	register uint8_t reg;

	if(buf == NULL || buflen < 1)
		/* Well... nothing to do */
		return -EINVAL;

	reg = (uint8_t) *buf;

	printf("Trying to write %d\n", reg);

	/* These LEDs are actived by low signal (common anode), invert the boolean value */
	kl_gpiowrite(GPIO_LED_1, !(reg & 0x01));
	kl_gpiowrite(GPIO_LED_2, !(reg & 0x02));
	kl_gpiowrite(GPIO_LED_3, !(reg & 0x04));
	kl_gpiowrite(GPIO_LED_4, !(reg & 0x08));

	return 1;
}


/****************************************************************************
 * Initialize device, add /dev/... nodes
 ****************************************************************************/

void up_leds(void)
{
	kl_configgpio(GPIO_LED_1);
	kl_configgpio(GPIO_LED_2);
	kl_configgpio(GPIO_LED_3);
	kl_configgpio(GPIO_LED_4);

	(void)register_driver("/dev/leds", &leds_ops, 0444, NULL);
}

I saved this driver file as configs/freedom-kl25z/src/leds_driver.c and added it to Makefile:

ifeq ($(CONFIG_ARCH_LEDS),y)
CSRCS += kl_led.c
CSRCS += leds_driver.c
endif

I’m reusing the same ARCH_LEDS config but you can create a specific config for your driver.

Unfortunately you cannot call up_leds() initialization function from kl_boardinitialize.c because this is called before the RTOS finished initialization. More information click here: http://comments.gmane.org/gmane.comp.embedded.nuttx/3158

I decided to use NSH_ARCHINIT to let nsh initialize my driver. First of all add this define to your config file:

CONFIG_NSH_ARCHINIT=y

Now create the configs/freedom-kl25z/src/up_nsh.c file and add it:

/****************************************************************************
* Name: nsh_archinitialize
*
* Description:
* Perform architecture specific initialization
*
****************************************************************************/

int nsh_archinitialize(void)
{
#ifdef CONFIG_ARCH_LEDS
up_leds();
#endif

return OK;
}

Just compile and flash the firmware inside your board.

Now my application can control the user LEDs.

Creating a video from a single image and an audio file

If you try to upload to youtube an audio file you recorded using your smartphone it will not accept because it is not a video. Then the solution is “converting” this audio in video, you can do it using a still image file as backgroung.

I used this command to do that:

$ ffmpeg -loop 1 -shortest -y -i imade.jpg -i audio.3gp -acodec copy -vcodec mjpeg result_video.avi

UPDATE: I was getting this error: Option shortest (finish encoding within shortest input) cannot be applied to input file audio.3gp — you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to. Error parsing options for input file audio.3gpp.
I fixed it moving “-shortest” the right, just before result_video.avi.
UPDATE 2: mjpeg generate big files, use “libx264” instead and the fps was higher (more than 150 fps) using “-r 1” fixed the issue, now the complete command is:

$ ffmpeg -loop 1 -y -i meeting.jpg -i audio.3gp -acodec copy -vcodec libx264 -r 1 -shortest result_video.avi

You can find more information about similar commands here:
http://ffmpeg.org/trac/ffmpeg/wiki/Create%20a%20video%20slideshow%20from%20images

How to put your application to be called from NuttX terminal

In my previous post I explained how to compile NuttX to Freedom board, then a guy asked me how to add his application on NuttX. This tutorial will explain step by step how to do it.

It will use the Freedom Board (FRDM-KL25Z) as reference, but it could be adapted to any other board.

First add support to BUILT-IN applications on configs/freedom-kl25z/nsh/defconfig :

CONFIG_BUILTIN=y
CONFIG_NSH_BUILTIN_APPS=y

Also make sure CONFIG_BINFMT_DISABLE is not defined or is defined as NOT :

CONFIG_BINFMT_DISABLE=n

To make our life easy just use original “hello” example as base:

$ cd ..
$ cp -a apps/examples/hello apps/examples/myapp
$ cd apps/examples/myapp
$ mv hello_main.c myapp.c

Edit Kconfig file changing it to reflect your application name:

config EXAMPLES_MYAPP
        bool "My First NuttX Application"
        default n
        ---help---
                Enable the \"MyAPP\" example

if EXAMPLES_MYAPP
endif

Edit Makefile replacing APPNAME and CSRCS to reflect our application name:

APPNAME         = myapp
CSRCS           = myapp.c

Also remember to edit your myapp.c replacing hello_main by myapp_main.

Edit apps/examples/Kconfig and add:

source "$APPSDIR/examples/myapp/Kconfig"

Edit apps/examples/Make.defs and add:

ifeq ($(CONFIG_EXAMPLES_MYAPP),y)
CONFIGURED_APPS += examples/myapp
endif

Now you should to compile the frontend to kconfig interface:

$ cd ../misc/tools/kconfig-frontends
$ ./configure --enable-mconf
$ make
$ sudo make install

Inside nuttx directory execute this command to clear apps directory:

$ cd ../../../nuttx
$ make apps_distclean

Now execute the menuconfig to select your application:

$ make menuconfig

Choose “myapp” following this path:

Application Configuration  --->
    Examples  --->
        [*] My First NuttX Application

Now just compile everything:

$ make

Copy the firmware to the board and open the serial console:

NuttShell (NSH)
nsh> 

nsh> help
help usage:  help [-v] []

  ?           exec        help        ls          mw          usleep      
  cat         exit        hexdump     mb          ps          xd          
  echo        free        kill        mh          sleep       

Builtin Apps:
  myapp

nsh> myapp
This is my first application!!

nsh>

Next time you execute “make distclean” the “myapp” will be remove from compilation.

If you want to keep it in the final Nuttx firmware you need to add into configs/freedom-kl25z/nsh/defconfig :

CONFIG_EXAMPLES_MYAPP=y

That is it!!! Now keep going and create your nice application to run on NuttX!