bcm283x

package
v3.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2023 License: Apache-2.0 Imports: 19 Imported by: 10

Documentation

Overview

Package bcm283x exposes the BCM283x GPIO functionality.

This driver implements memory-mapped GPIO pin manipulation and leverages sysfs-gpio for edge detection.

If you are looking for the actual implementation, open doc.go for further implementation details.

GPIOs

Aliases for GPCLK0, GPCLK1, GPCLK2 are created for corresponding CLKn pins. Same for PWM0_OUT and PWM1_OUT, which point respectively to PWM0 and PWM1.

Datasheet

https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

Its crowd-sourced errata: http://elinux.org/BCM2835_datasheet_errata

BCM2836: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf

Another doc about PCM and PWM: https://scribd.com/doc/127599939/BCM2835-Audio-clocks

GPIO pad control: https://scribd.com/doc/101830961/GPIO-Pads-Control2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Nanospin

func Nanospin(t time.Duration)

Nanospin spins the CPU without calling into the kernel code if possible.

func PinsClear0To31

func PinsClear0To31(mask uint32)

PinsClear0To31 clears the value of GPIO0 to GPIO31 pin for the bit set at their corresponding bit as a single write operation.

This function is extremely fast and does no error checking.

Example
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
	log.Fatal(err)
}

// Simultaneously clears GPIO4 and GPIO16 to gpio.Low.
bcm283x.PinsClear0To31(1<<16 | 1<<4)
Output:

func PinsClear32To46

func PinsClear32To46(mask uint32)

PinsClear32To46 clears the value of GPIO31 to GPIO46 pin for the bit set at their corresponding 'bit minus 32' as a single write operation.

This function is extremely fast and does no error checking.

Bits above 15 are ignored.

This function is not recommended on Raspberry Pis as these GPIOs are not easily accessible.

func PinsRead0To31

func PinsRead0To31() uint32

PinsRead0To31 returns the value of all GPIO0 to GPIO31 at their corresponding bit as a single read operation.

This function is extremely fast and does no error checking.

The returned bits are valid for both inputs and outputs.

Example
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
	log.Fatal(err)
}

// Print out the state of 32 GPIOs with a single read that reads all these
// pins all at once.
bits := bcm283x.PinsRead0To31()
fmt.Printf("bits: %#x\n", bits)
suffixes := []string{"   ", "\n"}
for i := uint(0); i < 32; i++ {
	fmt.Printf("GPIO%-2d: %d%s", i, (bits>>i)&1, suffixes[(i%4)/3])
}
Output:

bits: 0x80011010
GPIO0 : 0   GPIO1 : 0   GPIO2 : 0   GPIO3 : 0
GPIO4 : 1   GPIO5 : 0   GPIO6 : 0   GPIO7 : 0
GPIO8 : 0   GPIO9 : 0   GPIO10: 0   GPIO11: 0
GPIO12: 1   GPIO13: 0   GPIO14: 0   GPIO15: 0
GPIO16: 1   GPIO17: 0   GPIO18: 0   GPIO19: 0
GPIO20: 0   GPIO21: 0   GPIO22: 0   GPIO23: 0
GPIO24: 0   GPIO25: 0   GPIO26: 0   GPIO27: 0
GPIO28: 0   GPIO29: 0   GPIO30: 0   GPIO31: 1

func PinsRead32To46

func PinsRead32To46() uint32

PinsRead32To46 returns the value of all GPIO32 to GPIO46 at their corresponding 'bit minus 32' as a single read operation.

This function is extremely fast and does no error checking.

The returned bits are valid for both inputs and outputs.

Bits above 15 are guaranteed to be 0.

This function is not recommended on Raspberry Pis as these GPIOs are not easily accessible.

func PinsSet0To31

func PinsSet0To31(mask uint32)

PinsSet0To31 sets the value of GPIO0 to GPIO31 pin for the bit set at their corresponding bit as a single write operation.

Example
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
	log.Fatal(err)
}

// Simultaneously sets GPIO4 and GPIO16 to gpio.High.
bcm283x.PinsClear0To31(1<<16 | 1<<4)
Output:

func PinsSet32To46

func PinsSet32To46(mask uint32)

PinsSet32To46 sets the value of GPIO31 to GPIO46 pin for the bit set at their corresponding 'bit minus 32' as a single write operation.

This function is extremely fast and does no error checking.

Bits above 15 are ignored.

This function is not recommended on Raspberry Pis as these GPIOs are not easily accessible.

func PinsSetup0To27

func PinsSetup0To27(drive physic.ElectricCurrent, slewLimit, hysteresis bool) error

PinsSetup0To27 sets the output current drive strength, output slew limiting and input hysteresis for GPIO 0 to 27.

Default drive is 8mA, slew unlimited and hysteresis enabled.

Can only be used if driver bcm283x-dma was loaded.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/conn/v3/physic"
	"periph.io/x/host/v3/bcm283x"
)

func main() {
	if err := bcm283x.PinsSetup0To27(16*physic.MilliAmpere, true, true); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("drive:      %s", bcm283x.GPIO0.Drive())
	fmt.Printf("slew:       %t", bcm283x.GPIO0.SlewLimit())
	fmt.Printf("hysteresis: %t", bcm283x.GPIO0.Hysteresis())
}
Output:

func PinsSetup28To45

func PinsSetup28To45(drive physic.ElectricCurrent, slewLimit, hysteresis bool) error

PinsSetup28To45 sets the output current drive strength, output slew limiting and input hysteresis for GPIO 28 to 45.

Default drive is 16mA, slew unlimited and hysteresis enabled.

Can only be used if driver bcm283x-dma was loaded.

This function is not recommended on Raspberry Pis as these GPIOs are not easily accessible.

func Present

func Present() bool

Present returns true if running on a Broadcom bcm283x based CPU.

func ReadTime

func ReadTime() time.Duration

ReadTime returns the time on a monotonic 1Mhz clock (1µs resolution).

It only works if bcm283x-dma successfully loaded. Otherwise it returns 0.

Types

type Pin

type Pin struct {
	// contains filtered or unexported fields
}

Pin is a GPIO number (GPIOnn) on BCM238(5|6|7).

Pin implements gpio.PinIO.

var (
	GPIO0  *Pin // I2C0_SDA
	GPIO1  *Pin // I2C0_SCL
	GPIO2  *Pin // I2C1_SDA
	GPIO3  *Pin // I2C1_SCL
	GPIO4  *Pin // CLK0
	GPIO5  *Pin // CLK1
	GPIO6  *Pin // CLK2
	GPIO7  *Pin // SPI0_CS1
	GPIO8  *Pin // SPI0_CS0
	GPIO9  *Pin // SPI0_MISO
	GPIO10 *Pin // SPI0_MOSI
	GPIO11 *Pin // SPI0_CLK
	GPIO12 *Pin // PWM0
	GPIO13 *Pin // PWM1
	GPIO14 *Pin // UART0_TX, UART1_TX
	GPIO15 *Pin // UART0_RX, UART1_RX
	GPIO16 *Pin // UART0_CTS, SPI1_CS2, UART1_CTS
	GPIO17 *Pin // UART0_RTS, SPI1_CS1, UART1_RTS
	GPIO18 *Pin // I2S_SCK, SPI1_CS0, PWM0
	GPIO19 *Pin // I2S_WS, SPI1_MISO, PWM1
	GPIO20 *Pin // I2S_DIN, SPI1_MOSI, CLK0
	GPIO21 *Pin // I2S_DOUT, SPI1_CLK, CLK1
	GPIO22 *Pin //
	GPIO23 *Pin //
	GPIO24 *Pin //
	GPIO25 *Pin //
	GPIO26 *Pin //
	GPIO27 *Pin //
	GPIO28 *Pin // I2C0_SDA, I2S_SCK
	GPIO29 *Pin // I2C0_SCL, I2S_WS
	GPIO30 *Pin // I2S_DIN, UART0_CTS, UART1_CTS
	GPIO31 *Pin // I2S_DOUT, UART0_RTS, UART1_RTS
	GPIO32 *Pin // CLK0, UART0_TX, UART1_TX
	GPIO33 *Pin // UART0_RX, UART1_RX
	GPIO34 *Pin // CLK0
	GPIO35 *Pin // SPI0_CS1
	GPIO36 *Pin // SPI0_CS0, UART0_TX
	GPIO37 *Pin // SPI0_MISO, UART0_RX
	GPIO38 *Pin // SPI0_MOSI, UART0_RTS
	GPIO39 *Pin // SPI0_CLK, UART0_CTS
	GPIO40 *Pin // PWM0, SPI2_MISO, UART1_TX
	GPIO41 *Pin // PWM1, SPI2_MOSI, UART1_RX
	GPIO42 *Pin // CLK1, SPI2_CLK, UART1_RTS
	GPIO43 *Pin // CLK2, SPI2_CS0, UART1_CTS
	GPIO44 *Pin // CLK1, I2C0_SDA, I2C1_SDA, SPI2_CS1
	GPIO45 *Pin // PWM1, I2C0_SCL, I2C1_SCL, SPI2_CS2
	GPIO46 *Pin //

)

All the pins supported by the CPU.

func (*Pin) DefaultPull

func (p *Pin) DefaultPull() gpio.Pull

DefaultPull implements gpio.PinIn.

The CPU doesn't return the current pull.

func (*Pin) Drive

func (p *Pin) Drive() physic.ElectricCurrent

Drive returns the configured output current drive strength for this GPIO.

The current drive is configurable per GPIO groups: 0~27 and 28~45.

The default value for GPIOs 0~27 is 8mA and for GPIOs 28~45 is 16mA.

The value is a multiple 2mA between 2mA and 16mA.

Can only be used if driver bcm283x-dma was loaded. Otherwise returns 0.

func (*Pin) FastOut

func (p *Pin) FastOut(l gpio.Level)

FastOut sets a pin output level with Absolutely No error checking.

Out() Must be called once first before calling FastOut(), otherwise the behavior is undefined. Then FastOut() can be used for minimal CPU overhead to reach Mhz scale bit banging.

func (*Pin) FastRead

func (p *Pin) FastRead() gpio.Level

FastRead return the current pin level without any error checking.

This function is very fast. It works even if the pin is set as output.

func (*Pin) Func

func (p *Pin) Func() pin.Func

Func implements pin.PinFunc.

func (*Pin) Function

func (p *Pin) Function() string

Function implements pin.Pin.

func (*Pin) Halt

func (p *Pin) Halt() error

Halt implements conn.Resource.

If the pin is running a clock, PWM or waiting for edges, it is halted.

In the case of clock or PWM, all pins with this clock source are also disabled.

func (*Pin) Hysteresis

func (p *Pin) Hysteresis() bool

Hysteresis returns true if the input hysteresis via a Schmitt trigger is enabled.

The hysteresis is configurable per GPIO groups: 0~27 and 28~45.

The default is true.

Can only be used if driver bcm283x-dma was loaded. Otherwise returns true (the default value).

func (*Pin) In

func (p *Pin) In(pull gpio.Pull, edge gpio.Edge) error

In implements gpio.PinIn.

Specifying a value for pull other than gpio.PullNoChange causes this function to be slightly slower (about 1ms).

For pull down, the resistor is 50KOhm~60kOhm For pull up, the resistor is 50kOhm~65kOhm

The pull resistor stays set even after the processor shuts down. It is not possible to 'read back' what value was specified for each pin.

Will fail if requesting to change a pin that is set to special functionality.

Using edge detection requires opening a gpio sysfs file handle. On Raspbian, make sure the user is member of group 'gpio'. The pin will be exported at /sys/class/gpio/gpio*/. Note that the pin will not be unexported at shutdown.

For edge detection, the processor samples the input at its CPU clock rate and looks for '011' to rising and '100' for falling detection to avoid glitches. Because gpio sysfs is used, the latency is unpredictable.

func (*Pin) Name

func (p *Pin) Name() string

Name implements pin.Pin.

func (*Pin) Number

func (p *Pin) Number() int

Number implements pin.Pin.

This is the GPIO number, not the pin number on a header.

func (*Pin) Out

func (p *Pin) Out(l gpio.Level) error

Out implements gpio.PinOut.

Fails if requesting to change a pin that is set to special functionality.

func (*Pin) PWM

func (p *Pin) PWM(duty gpio.Duty, freq physic.Frequency) error

PWM implements gpio.PinOut.

It outputs a periodic signal on supported pins without CPU usage.

PWM pins

PWM0 is exposed on pins 12, 18 and 40. However, PWM0 is used for generating clock for DMA and unavailable for PWM.

PWM1 is exposed on pins 13, 19, 41 and 45.

PWM1 uses 25Mhz clock source. The frequency must be a divisor of 25Mhz.

DMA driven PWM is available for all pins except PWM1 pins, its resolution is 200KHz which is down-sampled from 25MHz clock above. The number of DMA driven PWM is limited.

Furthermore, these can only be used if the drive "bcm283x-dma" was loaded. It can only be loaded if the process has root level access.

The user must call either Halt(), In(), Out(), PWM(0,..) or PWM(gpio.DutyMax,..) to stop the clock source and DMA engine before exiting the program.

func (*Pin) Pull

func (p *Pin) Pull() gpio.Pull

Pull implements gpio.PinIn.

bcm2711/bcm2838 support querying the pull resistor of all GPIO pins. Prior to it, bcm283x doesn't support querying the pull resistor of any GPIO pin.

func (*Pin) Read

func (p *Pin) Read() gpio.Level

Read implements gpio.PinIn.

This function is fast. It works even if the pin is set as output.

func (*Pin) SetFunc

func (p *Pin) SetFunc(f pin.Func) error

SetFunc implements pin.PinFunc.

func (*Pin) SlewLimit

func (p *Pin) SlewLimit() bool

SlewLimit returns true if the output slew is limited to reduce interference.

The slew is configurable per GPIO groups: 0~27 and 28~45.

The default is true.

Can only be used if driver bcm283x-dma was loaded. Otherwise returns false (the default value).

func (*Pin) StreamIn

func (p *Pin) StreamIn(pull gpio.Pull, s gpiostream.Stream) error

StreamIn implements gpiostream.PinIn.

DMA driven StreamOut is available for GPIO0 to GPIO31 pin and the maximum resolution is 200kHz.

func (*Pin) StreamOut

func (p *Pin) StreamOut(s gpiostream.Stream) error

StreamOut implements gpiostream.PinOut.

I2S/PCM driven StreamOut is available for GPIO21 pin. The resolution is up to 250MHz.

For GPIO0 to GPIO31 except GPIO21 pin, DMA driven StreamOut is available and the maximum resolution is 200kHz.

func (*Pin) String

func (p *Pin) String() string

String implements conn.Resource.

func (*Pin) SupportedFuncs

func (p *Pin) SupportedFuncs() []pin.Func

SupportedFuncs implements pin.PinFunc.

func (*Pin) WaitForEdge

func (p *Pin) WaitForEdge(timeout time.Duration) bool

WaitForEdge implements gpio.PinIn.

Notes

Bugs

  • PWM(): There is no conflict verification when multiple pins are used simultaneously. The last call to PWM() will affect all pins of the same type (CLK0, CLK2, PWM0 or PWM1).

Directories

Path Synopsis
Package bcm283xsmoketest verifies that bcm283x specific functionality work.
Package bcm283xsmoketest verifies that bcm283x specific functionality work.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL