Ayke van Laethem 9fdf0d657d delay: add new package for cycle-accurate delays
This is implemented in inline assembly using machine.CPUFrequency() to
know how long a single CPU cycle takes. As long as it is called with a
constant duration, it should be fully inlined and all values can be
const-propagated resulting in very tight inline assembly.

For example, when I convert i2csoft to use this delay function, the
entire delay function compiles to something like this:

    8784:   movs    r6, #100
    8786:   mov     r0, r6
    8788:   nop
    878a:   nop
    878c:   nop
    878e:   nop
    8790:   nop
    8792:   subs    r0, #1
    8794:   bne     0x8788

That means that all the math to calculate the number of cycles is
entirely optimized away (in this case, to 100 loops).

I ran the example on a few boards to see how well it works:

| board                 | 100ms wait | CPU core
|-----------------------|------------|------
| microbit              | 121.6ms    | Cortex-M0 so it has 12% overhead
| circuitplay-express   | 100.1ms    | Cortex-M0+ so it is cycle accurate
| pico                  | 100.2ms    | Cortex-M0+
| pyportal              | 100.3ms    | Cortex-M4
| circuitplay-bluefruit | 125.8ms    | Cortex-M4
| esp8266               | 125.1ms    |

This shows that there is some loop overhead because of conservative
estimates, but note that even though there may be a 25% overhead, the
actual overhead per `delay.Sleep()` call is very small. It should be
good enough for software I2C at least, and can potentially be improved
in the future.
2023-05-14 19:35:29 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2021-05-12 09:21:52 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-12-20 11:13:43 +01:00
2021-05-30 22:11:27 +02:00
2021-01-08 11:32:49 +01:00
2022-12-19 22:01:39 +01:00
2022-12-21 17:47:14 -05:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2021-03-26 18:10:39 +01:00
2022-04-08 15:41:46 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-05-24 19:16:34 +02:00
2022-09-25 13:23:16 +02:00
2021-05-12 15:29:18 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2022-09-25 13:23:16 +02:00
2021-12-14 09:14:10 +09:00
2021-03-28 12:23:16 +02:00
2023-02-12 22:30:48 +01:00
2022-09-25 13:23:16 +02:00
2023-05-07 12:30:44 +02:00
2023-05-07 12:30:44 +02:00
2023-04-30 01:11:18 +02:00
2023-05-07 12:30:44 +02:00
2023-05-07 12:30:44 +02:00
2023-05-07 12:30:44 +02:00
2021-04-02 17:31:28 +02:00
2023-02-12 22:30:48 +01:00

TinyGo Drivers

PkgGoDev Build

This package provides a collection of hardware drivers for devices such as sensors and displays that can be used together with TinyGo.

Installing

go get tinygo.org/x/drivers

How to use

Here is an example in TinyGo that uses the BMP180 digital barometer:

package main

import (
    "time"

    "machine"

    "tinygo.org/x/drivers/bmp180"
)

func main() {
    machine.I2C0.Configure(machine.I2CConfig{})
    sensor := bmp180.New(machine.I2C0)
    sensor.Configure()

    connected := sensor.Connected()
    if !connected {
        println("BMP180 not detected")
        return
    }
    println("BMP180 detected")

    for {
        temp, _ := sensor.ReadTemperature()
        println("Temperature:", float32(temp)/1000, "°C")

        pressure, _ := sensor.ReadPressure()
        println("Pressure", float32(pressure)/100000, "hPa")

        time.Sleep(2 * time.Second)
    }
}

Supported devices

There are currently 94 devices supported. For the complete list, please see: https://tinygo.org/docs/reference/devices/

Contributing

Your contributions are welcome!

Please take a look at our CONTRIBUTING.md document for details.

License

This project is licensed under the BSD 3-clause license, just like the Go project itself.

S
Description
TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
Readme BSD-3-Clause 26 MiB
Latest
2026-04-21 08:44:44 +00:00
Languages
Go 99.1%
Shell 0.7%