Skip to content

Commit de7bf08

Browse files
committed
concepts: add page to describe how drivers work
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 355e528 commit de7bf08

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

content/docs/concepts/drivers.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "Drivers"
3+
linkTitle: "Drivers"
4+
type: "docs"
5+
weight: 35
6+
description: >
7+
How TinyGo drivers can be used to communicate with sensors and other devices and how this works.
8+
---
9+
10+
Drivers are packages to make it easier for TinyGo programs to use sensors, displays, and other external hardware that can be physically connected to a microcontroller.
11+
12+
Here is an example of a TinyGo program that uses a BMP180 digital temperature sensor by way of the [I2C interface](../peripherals/i2c):
13+
14+
```go
15+
package main
16+
17+
import (
18+
"time"
19+
20+
"machine"
21+
22+
"tinygo.org/x/drivers/bmp180"
23+
)
24+
25+
func main() {
26+
machine.I2C0.Configure(machine.I2CConfig{})
27+
sensor := bmp180.New(machine.I2C0)
28+
sensor.Configure()
29+
30+
connected := sensor.Connected()
31+
if !connected {
32+
println("BMP180 not detected")
33+
return
34+
}
35+
println("BMP180 detected")
36+
37+
for {
38+
temp, _ := sensor.ReadTemperature()
39+
println("Temperature:", float32(temp)/1000, "°C")
40+
41+
pressure, _ := sensor.ReadPressure()
42+
println("Pressure", float32(pressure)/100000, "hPa")
43+
44+
time.Sleep(2 * time.Second)
45+
}
46+
}
47+
```
48+
49+
You can find all of the TinyGo drivers in the repository located at [https://github.com/tinygo-org/drivers](https://github.com/tinygo-org/drivers).
50+
51+
## How Drivers Work
52+
53+
Most, if not all, microcontrollers have hardware peripheral blocks that provide low-level implementations of protocols like I2C, SPI, etc. But these blocks provide only very low level functions. They must be put together in order to actually do something functional on that microcontroller.
54+
55+
TinyGo defines Go language interfaces for I2C, SPI, and other peripherals, and then implements these interfaces by utilizing the microcontroller blocks and registers. This is what is in the TinyGo `machine` package.
56+
57+
Creating a TinyGo driver for a sensor, is to consume the interfaces defined by TinyGo for that peripheral such as I2C and then to use those to implement whatever communication is needed for a particular device to work.
58+
59+
Here is a diagram showing a specific example of this for an MPU6050 digital accelerometer:
60+
61+
```mermaid
62+
graph TD
63+
U[User code]
64+
U -- calls accelerometer API --> G(driver/mpu6050 package)
65+
G -- calls I2C interface --> M(machine)
66+
M -- provides I2C implementation --> D(device)
67+
D -- define I2C block/registers --> H[Hardware]
68+
```
69+
70+
For example, the MPU6050 has specific commands that it expects to be sent via I2C in order to return accelerometer data. The TinyGo driver provides functions like `ReadAccelerometer()` so you can use the sensor from a TinyGo program.
71+
72+
These functions call the I2C functions in TinyGo which are implemented by the `machine` package. The `machine` package then uses the `device` package for that microcontroller to call the low level MCU functions needed to get their work done.

0 commit comments

Comments
 (0)