9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Raspberry Pi 2 GPIO
@ 2019-08-31  8:03 Олег Бахарев
  2019-09-07 13:30 ` Richard Miller
  0 siblings, 1 reply; 8+ messages in thread
From: Олег Бахарев @ 2019-08-31  8:03 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 35 bytes --]

How to get access to RPi 2 GPIO ?

[-- Attachment #2: Type: text/html, Size: 57 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-08-31  8:03 [9fans] Raspberry Pi 2 GPIO Олег Бахарев
@ 2019-09-07 13:30 ` Richard Miller
  2019-09-10 14:23   ` Олег Бахарев
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Miller @ 2019-09-07 13:30 UTC (permalink / raw)
  To: 9fans

> How to get access to RPi 2 GPIO ?

bind -a '#G' /dev

Reading from /dev/gpio gives a 16-character big-endian hex string
with current values of gpios 63-0.

Writing commands like 'function 17 out' and 'set 17 1' to /dev/gpio
will change functions and outputs.

Sorry there doesn't seem to be a man page, see the source
for details (/sys/src/9/bcm/devgpio.c)




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-07 13:30 ` Richard Miller
@ 2019-09-10 14:23   ` Олег Бахарев
  2019-09-10 15:42     ` Brian L. Stuart
  0 siblings, 1 reply; 8+ messages in thread
From: Олег Бахарев @ 2019-09-10 14:23 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 554 bytes --]

Need man page for this.
Can you get some example for reading/writing GPIO ?

сб, 7 сент. 2019 г., 16:46 Richard Miller <9fans@hamnavoe.com>:

> > How to get access to RPi 2 GPIO ?
>
> bind -a '#G' /dev
>
> Reading from /dev/gpio gives a 16-character big-endian hex string
> with current values of gpios 63-0.
>
> Writing commands like 'function 17 out' and 'set 17 1' to /dev/gpio
> will change functions and outputs.
>
> Sorry there doesn't seem to be a man page, see the source
> for details (/sys/src/9/bcm/devgpio.c)
>
>
>

[-- Attachment #2: Type: text/html, Size: 892 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-10 14:23   ` Олег Бахарев
@ 2019-09-10 15:42     ` Brian L. Stuart
  2019-09-10 16:20       ` Richard Miller
  0 siblings, 1 reply; 8+ messages in thread
From: Brian L. Stuart @ 2019-09-10 15:42 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 4164 bytes --]

 On Tuesday, September 10, 2019, 10:27:08 AM EDT, Олег Бахарев <disconnectix@gmail.com> wrote:
> Need man page for this.

My bad. I thought I had sent man pages along with the
code. I've included the man page for it at the bottom of
this message.

> Can you get some example for reading/writing GPIO ?

I've also included a short example that uses both
the GPIO and the SPI interface to talk to a little
Nokia LCD screen.

BLS

.TH GPIO 3
.SH NAME
gpio \- access to Raspberry Pi GPIO pins
.SH SYNOPSIS
.B bind -a #G /dev
.PP
.B /dev/gpio
.fi
.SH DESCRIPTION
.I Gpio
serves a single file that provides access to the GPIO pins on the
Raspberry Pi.
Reads from the file receive a 16-character hexadecimal string
representing a
.B vlong
giving the values of up to 64 GPIO lines.
(The processor on both the first and second generation Pis provides
54 actual GPIO lines.)
The proper method for sampling GPIO 27 is as follows:
.IP
.B read(gfd, buf, 16);
.br
.B buf[16] = 0;
.br
.B gvals = strtoull(buf, nil, 16);
.br
.B "pin27 = gvals & (1 << 27);"
.PP
Writes to
.B gpio
control the characteristics and output values of GPIO lines.
The exact operation is specified by one of the following commands:
.TP
.BI function " pin f"
Set the function of GPIO
.I pin
to
.I f.
The valid values for
.I f
are:
.BR in ,
.BR out ,
.BR alt0 ,
.BR alt1 ,
.BR alt2 ,
.BR alt3 ,
.BR alt4 ,
.BR atl5 ,
and
.BR pulse .
The functions
.B in
and
.B out
set the specified GPIO line to be a general purpose input
and output, respectively.
The various
.BI alt n
functions are as specified in the Broadcom documentation and
differ on a per-pin basis.
The
.B pulse
function is somewhat specialized.
It causes the pin to be set to an output for
2μS and then to be set to a input.
With the value of the line set to 0 and a pullup resistor on the
line, this operation provides a short low pulse suitable for bit-banging
a 1-wire interface.
.TP
.BI pullup " pin"
Enables the internal pullup resistor for the specified GPIO pin.
.TP
.BI pulldown " pin"
Enables the internal pulldown resistor for the specified GPIO pin.
.TP
.BI float " pin"
Disables both internal pullup and pulldown resistors for the specified
GPIO pin.
.TP
.BI set " pin value"
For GPIO pins set to the output function, this command sets the output
value for the pin.
The
.I value
should be either 0 or 1.
.SH NOTES
All pin number references are according to the SoC documentation.
These GPIO signal numbers do
.I not
match the pin numbers on the header on the Pi board.
Reads sample the external signal values.
As a result, the values read for output pins might not match
the value written to them if externally they are driven harder
than the SoC drives them.
.SH SOURCE
.B /sys/src/9/bcm/devgpio.c
.br
.B /sys/src/9/bcm/gpio.c


#include <u.h>
#include <libc.h>

void
main()
{
 int gfd, sfd, i, j;
 uchar buf[256];

 gfd = open("/dev/gpio", ORDWR);
 sfd = open("/dev/spi0", ORDWR);
 if(gfd < 0 || sfd < 0)
 print("open error: %r\n");

 fprint(gfd, "function 22 out");
 fprint(gfd, "set 22 0");
 sleep(1);
 fprint(gfd, "set 22 1");
 fprint(gfd, "function 27 out");

 fprint(gfd, "function 26 out");
 fprint(gfd, "set 26 1");
 sleep(10);
 fprint(gfd, "set 26 0");
 fprint(gfd, "set 27 0");
 sleep(1);
 buf[0] = 0x21;
 pwrite(sfd, buf, 1, 0);
 sleep(1);
 buf[0] = 0x14;
 pwrite(sfd, buf, 1, 0);
 sleep(1);
 buf[0] = 0xc0;
 pwrite(sfd, buf, 1, 0);
 sleep(1);
 buf[0] = 0x20;
 if(pwrite(sfd, buf, 1, 0) < 0)
 print("write error: %r\n");
 sleep(1);
 buf[1] = 0x0c;
 if(pwrite(sfd, buf+1, 1, 0) < 0)
 print("write error: %r\n");
 sleep(1);
 buf[2] = 0x40;
 if(pwrite(sfd, buf+2, 1, 0) < 0)
 print("write error: %r\n");
 sleep(1);
 buf[3] = 0x80;
 if(pwrite(sfd, buf+3, 1, 0) < 0)
 print("write error: %r\n");
// pwrite(sfd, buf, 4, 0);

 fprint(gfd, "set 27 1");
 for(j = 0; j < 256; ++j) {
 for(i = 0; i < 16; ++i)
 buf[i] = j ^ i;
 sleep(1);
 for(i = 0; i < 6 * 84 / 16; ++i)
 pwrite(sfd, buf, 16, 0);
 sleep(100);
 }
 fprint(gfd, "set 26 1");
}
  

[-- Attachment #2: Type: text/html, Size: 4884 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-10 15:42     ` Brian L. Stuart
@ 2019-09-10 16:20       ` Richard Miller
  2019-09-10 23:18         ` Brian L. Stuart
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Miller @ 2019-09-10 16:20 UTC (permalink / raw)
  To: 9fans

Thanks Brian, I'll add your man page to the 9pi image for the
next update.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-10 16:20       ` Richard Miller
@ 2019-09-10 23:18         ` Brian L. Stuart
  2019-09-12  9:39           ` Richard Miller
  0 siblings, 1 reply; 8+ messages in thread
From: Brian L. Stuart @ 2019-09-10 23:18 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 304 bytes --]

 On Tuesday, September 10, 2019, 12:21:32 PM EDT, Richard Miller <9fans@hamnavoe.com> wrote:
> Thanks Brian, I'll add your man page to the 9pi image for the
> next update.

You're welcome. Do you also need the man pages for i2c and
spi, or did I send those the first time around?

Thanks,
BLS
  

[-- Attachment #2: Type: text/html, Size: 391 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-10 23:18         ` Brian L. Stuart
@ 2019-09-12  9:39           ` Richard Miller
  2019-09-16 18:41             ` Brian L. Stuart
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Miller @ 2019-09-12  9:39 UTC (permalink / raw)
  To: 9fans

> Do you also need the man pages for i2c and
> spi, or did I send those the first time around?

If you sent them, they must have gone astray ... if you can
supply a new copy, I'll add them to the image too. Thanks!




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] Raspberry Pi 2 GPIO
  2019-09-12  9:39           ` Richard Miller
@ 2019-09-16 18:41             ` Brian L. Stuart
  0 siblings, 0 replies; 8+ messages in thread
From: Brian L. Stuart @ 2019-09-16 18:41 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 4516 bytes --]

 On Thursday, September 12, 2019, 5:43:13 AM EDT, Richard Miller <9fans@hamnavoe.com> wrote:
> > Do you also need the man pages for i2c and
> > spi, or did I send those the first time around?
>
> If you sent them, they must have gone astray ... if you can
> supply a new copy, I'll add them to the image too. Thanks!

Okay. I've appened at the end here both of them, i2c
first. I think they're up to date, but it's been a while since
I did much with them, and I can't vouch for how well my
memory works as the years go by.

BLS

.TH I2C 3
.SH NAME
i2c \- basic I2C interface
.SH SYNOPSIS
.B bind -a
.BI #J n
.B /dev
.PP
.BI /dev/i2c. n .ctl
. br
.BI /dev/i2c. n .data
.fi
.SH DESCRIPTION
.I I2c
serves a one-level directory with two files
that give access to the target device with address
.I n
(given in hexadecimal)
on the system's I2C bus.
.I N
is usually determined by the I2C device manufacturer.
I2C gives address 0 special meaning as the `general call' address.
See an I2C specification for details.
.PP
The control file
.BI i2c. n .ctl
accepts commands to set the valid address range and
subaddressing mode for the corresponding data file.
The following control messages can be written to it:
.TP
.B a10
Force 10-bit addressing instead of 7-bit addressing.
Otherwise 10-bit addressing is used only if the device address
.I n
is bigger than 255.
.TP
.BI size " nbytes"
.br
Set the logical size of the target device to
.IR nbytes .
(By default when opened, it is 256 bytes, enough for most small I2C devices.)
IO requests will be kept within this limit.
This value is also returned by
.B Sys->stat
as the length of the data file.
.TP
.BI subaddress " \fR[\fP n \fR]\fP"
.br
Cause subsequent reads and writes
on the data file to use I2C subaddressing
with
.I n
byte subaddresses (default: 1 byte).
.I N
must be no larger than 4.
The target device must support subaddressing.
By default, the device is not subaddressed.
Setting
.I n
to zero switches off subaddressing.
.PP
When read,
the control file displays the current settings.
.PP
The data file
.BI i2c. n .data
can be read or written to
exchange data with the slave device with address
.I n
(where
.I n
is given in hexadecimal).
Each write request transmits the given data
to the device.
Each read request sends a receive
request to the device and returns the resulting data.
If the I2C target is subaddressed, the current file offset
is used as the subaddress;
otherwise the file offset is ignored, and the device typically starts at 0 for each transfer request.
Read and write requests are trimmed to the declared
size of the device.
.SH SOURCE
.B /sys/src/9/bcm/devi2c.c
.br
.B /sys/src/9/bcm/i2c.c

.TH SPI 3
.SH NAME
spi \- access to the main Raspberry Pi SPI interface
.SH SYNOPSIS
.B bind -a #π /dev
.PP
.B /dev/spictl
.br
.B /dev/spi0
.br
.B /dev/spi1
.SH DESCRIPTION
The Broadcom SoC on the Raspberry Pi has three SPI interfaces:
the main SPI interface, designated SPI0, and two auxiliary SPI
interfaces, designated SPI1 and SPI2.
On the first generation Pis, only SPI0 was brought out to the
header on the board.
For the B+ and Pi2 models, SPI0 and SPI1 are available.
The driver described in this man page only supports SPI0.
.PP
Reads and writes to the files
.B spi0
and
.B spi1
transfer data over the SPI bus.
Accesses to
.B spi0
cause the transfers to take place with the CE0\_0 line asserted
low.
Similarly, transfers to
.B spi1
are carried out with CE1\_0 asserted low.
.PP
The
.B spictl
file is used to set various control parameters.
It accepts the following commands:
.TP
.BI clock " freq"
Set the frequency of the SPI clock.
The clock from which the SPI clock is derived runs at 250MHz,
and the Broadcom documentation specifies that the divisor
must be a power of 2.
The driver sets the divisor to the highest power of 2 that results
in a clock rate that is less than or equal to the
.I freq
parameter in MHz.
.TP
.BI mode " n"
Treats
.I n
as a two-bit number specifying the settings for the clock phase
and clock polarity.
The default value of 0 matches the polarity and phase requirements
of most peripheral devices.
.TP
.B lossi
Enable a bidirectional mode where the MOSI line is used for both
reads and writes.
.SH SOURCE
.B /sys/src/9/bcm/devspi.c
.br
.B /sys/src/9/bcm/spi.c
.SH BUGS
The various SPI modes are untested and the LoSSI support is
unimplemented.

  

[-- Attachment #2: Type: text/html, Size: 4998 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-09-16 18:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-31  8:03 [9fans] Raspberry Pi 2 GPIO Олег Бахарев
2019-09-07 13:30 ` Richard Miller
2019-09-10 14:23   ` Олег Бахарев
2019-09-10 15:42     ` Brian L. Stuart
2019-09-10 16:20       ` Richard Miller
2019-09-10 23:18         ` Brian L. Stuart
2019-09-12  9:39           ` Richard Miller
2019-09-16 18:41             ` Brian L. Stuart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).