NuttX driver to control a single LED

This is a simple LED driver for NuttX.

You can control the LED over /dev/led using these commands:

nsh> echo "1" > /dev/led
nsh> dd if=/dev/led of=/dev/console bs=2 count=1
1
nsh> echo "0" > /dev/led
nsh> dd if=/dev/led of=/dev/console bs=2 count=1
0
nsh>

This is the whole driver:

#include 
#include 

#include 
#include 
#include 
#include 

#include "stm32_gpio.h"
#include "mikroe-stm32f4-internal.h"

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

/****************************************************************************
 * 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 = stm32_gpioread(GPIO_LED1);

	buf[0] = (char) (reg + '0');
        buf[1] = '\n';

	return 2;
}

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;

        if (reg != '0' && reg != '1')
            return 1;

	stm32_gpiowrite(GPIO_LED1, (reg - '0'));

	return 1;
}


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

void up_leds(void)
{
	int ret;

	stm32_configgpio(GPIO_LED1);

	ret = register_driver("/dev/led", &leds_ops, 0444, NULL);
        if (ret < 0)
        {
           dbg("Failed to register driver: %d\n", ret);
        }
}

Note: you need to enable Arch Init (CONFIG_NSH_ARCHINIT=y) and call “up_leds();” from nsh_archinitialize() function normally in up_nsh.c.

Advertisements

2 thoughts on “NuttX driver to control a single LED

  1. Hi celem,
    You are always welcome! In fact NuttX is very interesting and very powerful, now that it has micropython support integrated then it is easy to create applications in a very fast way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s