mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-13 11:23:40 +00:00
b3af3f594e
Previously, a new implementation had to be written for each processor
speed. This is tedious and error-prone, therefore I've rewritten all of
the assembly code using a tool that will calculate exactly how many NOP
instructions are required.
I've also switched from build tags to a switch statement over all
processor speeds.
All in all, this means that:
- Chips with a supported CPU performance will automatically be
supported (no build tags necessary).
- Adding a new chip is as easy as adding the MHz count to the
generator script and to the switch statement.
Right now this is only for the Cortex-M, but it's certainly feasible to
extend this to other architectures such as the AVR.
35 lines
867 B
Go
35 lines
867 B
Go
// +build cortexm
|
|
|
|
package ws2812
|
|
|
|
// This file implements the WS2812 protocol for various Cortex-M
|
|
// microcontrollers. It is intended to work with various variants: M0, M0+, M3,
|
|
// and M4. Because machine.CPUFrequency() is usually a constant, the value will
|
|
// usually be constant-propagated and the switch below will be a direct
|
|
// (inlinable function) - thus there is usually no code size penalty over build
|
|
// tags per CPU speed.
|
|
|
|
import (
|
|
"machine"
|
|
)
|
|
|
|
// Send a single byte using the WS2812 protocol.
|
|
func (d Device) WriteByte(c byte) error {
|
|
switch machine.CPUFrequency() {
|
|
case 16_000_000: // 16MHz
|
|
d.writeByte16(c)
|
|
return nil
|
|
case 48_000_000: // 48MHz
|
|
d.writeByte48(c)
|
|
return nil
|
|
case 64_000_000: // 64MHz
|
|
d.writeByte64(c)
|
|
return nil
|
|
case 120_000_000: // 120MHz
|
|
d.writeByte120(c)
|
|
return nil
|
|
default:
|
|
return errUnknownClockSpeed
|
|
}
|
|
}
|