Plan 9 from Bell Labs’s /usr/web/sources/contrib/cross/devgpio.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/*
 * access to GPIO pins.
 */

#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

enum{
	Qrootdir = 0,
	Qgpiodir,
	Q17,
	Q18,
};

static Dirtab gpiorootdir[]={
	".",	{Qrootdir, 0, QTDIR},	0,	0555,
	"gpio",	{Qgpiodir, 0, QTDIR},	0,	0555,
};

static Dirtab gpiodir[]={
	".",	{Qgpiodir, 0, QTDIR},	0,	0555,
	"17",		{Q17, 0},	0,	0664,
	"18",		{Q18, 0},	0,	0664,
};

enum {
	Pin17 = 17,
	Pin18 = 18,
	Npins,

	Output = 0x01,

	Off = 0,
	On,
};

static int gpiostate[Npins];

extern void gpioout(uint pin, int set);
extern void gpiosel(uint pin, int func);

static void
gpioinit(void)
{
	gpiosel(Pin17, Output);
	gpiosel(Pin18, Output);
}

static int
gpiogen(Chan *c, char *name, Dirtab *, int, int s, Dir *dp)
{
	if ((ulong)c->qid.path == Qrootdir) {
		if (s == DEVDOTDOT){
			devdir(c, (Qid){Qrootdir, 0, QTDIR}, "#G", 0, eve, 0555, dp);
			return 1;
		}
		return devgen(c, name, gpiorootdir, nelem(gpiorootdir), s, dp);
	}
	if ((ulong)c->qid.path == Qgpiodir && s == DEVDOTDOT){
		devdir(c, (Qid){Qrootdir, 0, QTDIR}, "#G", 0, eve, 0555, dp);
		return 1;
	}
	return devgen(c, name, gpiodir, nelem(gpiodir), s, dp);
}

static long
gpioread(Chan *c, void *a, long n, vlong offset)
{
	int pin;
	char *status;

	if(c->qid.type & QTDIR)
		return devdirread(c, a, n, nil, 0, gpiogen);

	pin = 0;
	switch((ulong)c->qid.path){
	case Q17:
		pin = Pin17;
		break;
	case Q18:
		pin = Pin18;
		break;
	default:
		error(Ebadarg);
		break;
	}
	status = gpiostate[pin] ? "on" : "off";
	return readstr(offset, a, n, status);
}

static long
gpiowrite(Chan*c, void *a, long n, vlong)
{
	int state;
	char *ch, buf[20];

	if (n >= sizeof(buf))
		error(Ebadarg);
	strncpy(buf, a, sizeof(buf));
	buf[sizeof(buf)-1] = '\0';
	ch = strrchr(buf, '\n');
	if (ch != nil)
		*ch = '\0';
	state = (strcmp(buf, "on") == 0);
	switch((ulong)c->qid.path){
	case Q17:
		gpiostate[Pin17] = state;
		gpioout(Pin17, state);
		break;
	case Q18:
		gpiostate[Pin18] = state;
		gpioout(Pin18, state);
		break;
	default:
		error(Ebadarg);
		break;
	}
	return n;
}

static Chan*
gpioattach(char* spec)
{
	return devattach('G', spec);
}

static Walkqid*	 
gpiowalk(Chan* c, Chan *nc, char** name, int nname)
{
	return devwalk(c, nc, name, nname, nil, 0, gpiogen);
}

static int	 
gpiostat(Chan* c, uchar* dp, int n)
{
	return devstat(c, dp, n, nil, 0, gpiogen);
}

static Chan*
gpioopen(Chan* c, int omode)
{
	return devopen(c, omode, nil, 0, gpiogen);
}

static void	 
gpioclose(Chan*)
{
}

Dev gpiodevtab = {
	'G',
	"gpio",

	devreset,
	gpioinit,
	devshutdown,
	gpioattach,
	gpiowalk,
	gpiostat,
	gpioopen,
	devcreate,
	gpioclose,
	gpioread,
	devbread,
	gpiowrite,
	devbwrite,
	devremove,
	devwstat,
};

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.