Categorygithub.com/kidoman/embd
modulepackage
0.0.0-20170508013040-d3d8c0c5c68d
Repository: https://github.com/kidoman/embd.git
Documentation: pkg.go.dev

# README

embd Build Status GoDoc

embd is a hardware abstraction layer (HAL) for embedded systems.

It allows you to start your hardware hack on easily available hobby boards (like the Raspberry Pi, BeagleBone Black, C.H.I.P., etc.) by giving you straight-forward access to the board's capabilities as well as a plethora of sensors (like accelerometers, gyroscopes, thermometers, etc.) and controllers (PWM generators, digital-to-analog convertors) for which it includes drivers. If you move to custom designed boards you have to throw away your code: you carry forward the effort where the HAL abstraction of EMBD will save you precious time.

The overall strategy used in embd is to use Linux device drivers to access gpio pins, SPI and I2C buses, as well as interrupts. This makes it easy to port from one platform to another and it enables kernel code to handle the devices as efficiently as possible. What embd then adds is first a Golang library interface on top of the various Linux devices and then another layer of user-level drivers for specific sensors and controllers that are connected to gpio pins or one of the buses.

Development supported and sponsored by SoStronk and ThoughtWorks.

Also, you might be interested in: Why Golang?

Blog post introducing EMBD

Getting Started

Install Go version 1.6 or later to make compiling for ARM easy. The set up your GOPATH, and create your first .go file. We'll call it simpleblinker.go.

package main

import (
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/rpi" // This loads the RPi driver
)

func main() {
	for {
		embd.LEDToggle("LED0")
		time.Sleep(250 * time.Millisecond)
	}
}

Then install the EMBD package:

$ go get github.com/kidoman/embd

Build the binary for linux/ARM:

$ export GOOS=linux
$ export GOARCH=arm
$ go build simpleblinker.go

Copy the cross-compiled binary to your RaspberryPi*:

$ scp simpleblinker [email protected]:~

Then on the rPi run the program with sudo*:

$ sudo ./simpleblinker

You will now see the green LED (next to the always on power LED) blink every 1/4 sec.

* Notes

  • Assuming your RaspberryPi has an IP address of 192.168.2.2. Substitute as necessary
  • sudo (root) permission is required as we are controlling the hardware by writing to special files
  • This sample program is optimized for brevity and does not clean up after itself. Click here to see the full version

Getting Help

Join the slack channel

Platforms Supported

The command line tool

go get github.com/kidoman/embd/embd

will install a command line utility embd which will allow you to quickly get started with prototyping. The binary should be available in your $GOPATH/bin. However, to be able to run this on a ARM based device, you will need to build it with GOOS=linux and GOARCH=arm environment variables set.

For example, if you run embd detect on a BeagleBone Black:

root@beaglebone:~# embd detect

detected host BeagleBone Black (rev 0)

Run embd without any arguments to discover the various commands supported by the utility.

How to use the framework

Package embd provides a hardware abstraction layer for doing embedded programming on supported platforms like the Raspberry Pi and BeagleBone Black. Most of the examples below will work without change (i.e. the same binary) on all supported platforms. How cool is that?

Although samples are all present in the samples folder, we will show a few choice examples here.

Use the LED driver to toggle LEDs on the BBB:

import "github.com/kidoman/embd"
import _ "github.com/kidoman/embd/host/all"
...
embd.InitLED()
defer embd.CloseLED()
...
led, err := embd.NewLED(3)
...
led.Toggle()

Even shorter when quickly trying things out:

import "github.com/kidoman/embd"
import _ "github.com/kidoman/embd/host/all"
...
embd.InitLED()
defer embd.CloseLED()
...
embd.ToggleLED(3)

3 is the same as USR3 for all intents and purposes. The driver is smart enough to figure all this out.

BBB + PWM:

import "github.com/kidoman/embd"
import _ "github.com/kidoman/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pwm, _ := embd.NewPWMPin("P9_14")
defer pwm.Close()
...
pwm.SetDuty(1000)

Control GPIO pins on the RaspberryPi / BeagleBone Black:

import "github.com/kidoman/embd"
import _ "github.com/kidoman/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
embd.SetDirection(10, embd.Out)
embd.DigitalWrite(10, embd.High)

Could also do:

import "github.com/kidoman/embd"
import _ "github.com/kidoman/embd/host/all"
...
embd.InitGPIO()
defer embd.CloseGPIO()
...
pin, err := embd.NewDigitalPin(10)
...
pin.SetDirection(embd.Out)
pin.Write(embd.High)

Or read data from the Bosch BMP085 barometric sensor:

import "github.com/kidoman/embd"
import "github.com/kidoman/embd/sensor/bmp085"
import _ "github.com/kidoman/embd/host/all"
...
bus := embd.NewI2CBus(1)
...
baro := bmp085.New(bus)
...
temp, err := baro.Temperature()
altitude, err := baro.Altitude()

Even find out the heading from the LSM303 magnetometer:

import "github.com/kidoman/embd"
import "github.com/kidoman/embd/sensor/lsm303"
import _ "github.com/kidoman/embd/host/all"
...
bus := embd.NewI2CBus(1)
...
mag := lsm303.New(bus)
...
heading, err := mag.Heading()

The above two examples depend on I2C and therefore will work without change on almost all platforms.

Protocols Supported

Sensors Supported

Interfaces

Controllers

Convertors

  • MCP3008 8-channel, 10-bit ADC with SPI protocol, Datasheet

Contributing

Pull requests that follow the guidelines are very appreciated. If you find a problem but are not up to coding a fix please file an issue. Thank you!

About

EMBD is affectionately designed/developed by Karan Misra (kidoman), Kunal Powar (kunalpowar) and FRIENDS. We also have a list of CONTRIBUTORS.

# Packages

Package controller is a container for the various device controllers supported by EMBD.
Package convertors contains the various convertor modules for use on your platform.
Package host is a container for the various hosts supported by EMBD.
Package sensor contains the various sensors modules for use on your platform.
Package util contains utility functions.

# Functions

ActiveLow makes the pin active low.
AnalogWrite reads a value from the pin.
CloseGPIO releases resources associated with the GPIO driver.
CloseI2C releases resources associated with the I2C driver.
CloseLED releases resources associated with the LED driver.
CloseSPI releases resources associated with the SPI driver.
DescribeHost returns the detected host descriptor.
DetectHost returns the detected host and its revision number.
DigitalRead reads a value from the pin.
DigitalWrite writes val to the pin.
FindFirstMatchingFile finds the first glob match in the filesystem.
InitGPIO initializes the GPIO driver.
InitI2C initializes the I2C driver.
InitLED initializes the LED driver.
InitSPI initializes the SPI driver.
LEDOff switches the LED off.
LEDOn switches the LED on.
LEDToggle toggles the LED.
NewAnalogPin returns a AnalogPin interface which allows control over the analog GPIO pin.
NewDigitalPin returns a DigitalPin interface which allows control over the digital GPIO pin.
NewGPIODriver returns a GPIODriver interface which allows control over the GPIO subsystem.
NewI2CBus returns a I2CBus.
NewI2CDriver returns a I2CDriver interface which allows control over the I²C subsystem.
NewLED returns a LED interface which allows control over the LED.
NewLEDDriver returns a LEDDriver interface which allows control over the LED subsystem.
NewPWMPin returns a PWMPin interface which allows PWM signal generation over a the PWM pin.
NewSPIBus returns a SPIBus.
NewSPIDriver returns a SPIDriver interface which allows control over the SPI bus.
PullDown pulls the pin down.
PullUp pulls the pin up.
Register makes a host describer available by the provided host key.
SetDirection sets the direction of the pin (in/out).
SetHost overrides the host and revision no.

# Constants

CapAnalog represents pins with analog IO capability.
CapDigital represents the digital IO capability.
CapGPMS represents pins with the GPMC capability.
CapI2C represents pins with the I2C capability.
CapLCD represents pins used to carry LCD data.
CapPWM represents pins with PWM capability.
CapSPI represents pins with the SPI capability.
CapUART represents pins with the UART capability.
High represents 1.
HostBBB represents the BeagleBone Black.
HostCHIP represents the NextThing C.H.I.P.
HostCubieTruck represents the Cubie Truck.
HostGalileo represents the Intel Galileo board.
HostNull reprents a null host.
HostRadxa represents the Radxa board.
HostRPi represents the RaspberryPi.
In represents read mode.
Low represents 0.
Negative represents negative polarity.
Out represents write mode.
Positive represents (default) positive polarity.
SPIMode0 represents the mode0 operation (CPOL=0 CPHA=0) of spi.
SPIMode1 represents the mode0 operation (CPOL=0 CPHA=1) of spi.
SPIMode2 represents the mode0 operation (CPOL=1 CPHA=0) of spi.
SPIMode3 represents the mode0 operation (CPOL=1 CPHA=1) of spi.

# Variables

ErrFeatureNotImplemented is returned when a particular feature is supported by the host but not implemented yet.
ErrFeatureNotSupported is returned when the host does not support a particular feature.

# Structs

Descriptor represents a host descriptor.
PinDesc represents a pin descriptor.

# Interfaces

AnalogPin implements access to a analog IO capable GPIO pin.
DigitalPin implements access to a digital IO capable GPIO pin.
GPIODriver implements a generic GPIO driver.
I2CBus interface is used to interact with the I2C bus.
I2CDriver interface interacts with the host descriptors to allow us control of I2C communication.
InterruptPin implements access to an interrupt capable GPIO pin.
The LED interface is used to control a led on the prototyping board.
LEDDriver interface interacts with the host descriptors to allow us control of the LEDs.
PWMPin implements access to a pwm capable GPIO pin.
SPIBus interface allows interaction with the SPI bus.
SPIDriver interface interacts with the host descriptors to allow us control of SPI communication.

# Type aliases

The Describer type is a Descriptor provider.
The Direction type indicates the direction of a GPIO pin.
The Edge trigger for the GPIO Interrupt.
The Host type represents all the supported host types.
LEDMap type represents a LED mapping for a host.
PinMap type represents a collection of pin descriptors.
The Polarity type indicates the polarity of a pwm pin.