mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-13 19:33:39 +00:00
revision: modify proposed pin HAL.
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package pin
|
||||
|
||||
import "errors"
|
||||
|
||||
// OutputFunc is hardware abstraction for a pin which outputs a
|
||||
// digital signal (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.OutputFunc
|
||||
// led := machine.LED
|
||||
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
// var p pin.OutputFuncFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
|
||||
type OutputFunc func(level bool)
|
||||
|
||||
func (o OutputFunc) High() {
|
||||
o(true)
|
||||
}
|
||||
|
||||
func (o OutputFunc) Low() {
|
||||
o(false)
|
||||
}
|
||||
|
||||
// InputFunc is hardware abstraction for a pin which receives a
|
||||
// digital signal and reads it (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.InputFunc
|
||||
// input := machine.LED
|
||||
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput
|
||||
// var p pin.InputFunc = input.Get // Going from a machine.Pin to a drivers.PinInput
|
||||
type InputFunc func() (level bool)
|
||||
|
||||
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
// This is an wrapper to pin.OutputFunc abstraction which is a function type.
|
||||
//
|
||||
// func New(p1, p2, p3 pin.Output) *Device {
|
||||
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
|
||||
// }
|
||||
//
|
||||
// func NewWithPinfuncs(p1, p2, p3 pin.OutputFunc) *Device {
|
||||
// return &Device{p1:p1, p2:p2, p3:p3}
|
||||
// }
|
||||
//
|
||||
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
|
||||
type Output interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
|
||||
// more information on why this is "legacy".
|
||||
type Input interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
|
||||
// ConfigureOutput is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureOutput(po Output) {
|
||||
configureOutput(po)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPulldown(pi Input) {
|
||||
configureInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInput(pi Input) {
|
||||
configureInput(pi)
|
||||
}
|
||||
|
||||
// ConfigureInputPullup is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPullup(pi Input) {
|
||||
configureInputPullup(pi)
|
||||
}
|
||||
|
||||
// IsNotPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers should not require pin knowledge.
|
||||
func IsNotPin(pin any) bool {
|
||||
return isNotPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !baremetal
|
||||
|
||||
package pin
|
||||
|
||||
func configureOutput(p Output) {}
|
||||
func configureInput(p Input) {}
|
||||
func configureInputPulldown(p Input) {}
|
||||
func configureInputPullup(p Input) {}
|
||||
func isNotPin(a any) bool { return false }
|
||||
@@ -0,0 +1,10 @@
|
||||
//go:build baremetal && fe310
|
||||
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
const (
|
||||
pulldown = machine.PinInput
|
||||
pullup = machine.PinInput
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build baremetal && !fe310
|
||||
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
// If you are getting a build error here you then we missed adding
|
||||
// your CPU build tag to the list of CPUs that do not have pulldown/pullups.
|
||||
// Add it above and in pinhal_nopulls! You should also add a smoketest for it :)
|
||||
const (
|
||||
pulldown = machine.PinInputPulldown
|
||||
pullup = machine.PinInputPullup
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build baremetal
|
||||
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
func configureOutput(p Output) {
|
||||
configure(p, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configureInputPulldown(p Input) {
|
||||
configure(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configureInput(p Input) {
|
||||
configure(p, machine.PinInput)
|
||||
}
|
||||
|
||||
func configureInputPullup(p Input) {
|
||||
configure(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func isNotPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configure(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user