Skip to content

Commit 334ed57

Browse files
ysoldakdeadprogram
authored andcommitted
wifinina: control nina pins, for example leds
1 parent 3637033 commit 334ed57

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

examples/wifinina/pins/main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//go:build nano_rp2040
2+
// +build nano_rp2040
3+
4+
// This examples shows how to control RGB LED connected to
5+
// NINA-W102 chip on Arduino Nano RP2040 Connect board
6+
// Built-in LED code added for API comparison
7+
8+
package main
9+
10+
import (
11+
"machine"
12+
"time"
13+
14+
"tinygo.org/x/drivers/wifinina"
15+
)
16+
17+
const (
18+
LED = machine.LED
19+
20+
// Arduino Nano RP2040 Connect board RGB LED pins
21+
// See https://docs.arduino.cc/static/3525d638b5c76a2d19588d6b41cd02a0/ABX00053-full-pinout.pdf
22+
LED_R wifinina.Pin = 27
23+
LED_G wifinina.Pin = 25
24+
LED_B wifinina.Pin = 26
25+
)
26+
27+
var (
28+
29+
// these are the default pins for the Arduino Nano-RP2040 Connect
30+
spi = machine.NINA_SPI
31+
32+
// this is the ESP chip that has the WIFININA firmware flashed on it
33+
device *wifinina.Device
34+
)
35+
36+
func setup() {
37+
38+
// Configure SPI for 8Mhz, Mode 0, MSB First
39+
spi.Configure(machine.SPIConfig{
40+
Frequency: 8 * 1e6,
41+
SDO: machine.NINA_SDO,
42+
SDI: machine.NINA_SDI,
43+
SCK: machine.NINA_SCK,
44+
})
45+
46+
device = wifinina.New(spi,
47+
machine.NINA_CS,
48+
machine.NINA_ACK,
49+
machine.NINA_GPIO0,
50+
machine.NINA_RESETN)
51+
device.Configure()
52+
53+
time.Sleep(time.Second)
54+
55+
LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
56+
LED_R.Configure(wifinina.PinConfig{Mode: wifinina.PinOutput})
57+
LED_G.Configure(wifinina.PinConfig{Mode: wifinina.PinOutput})
58+
LED_B.Configure(wifinina.PinConfig{Mode: wifinina.PinOutput})
59+
}
60+
61+
func main() {
62+
63+
setup()
64+
65+
LED.Low() // OFF
66+
LED_R.High() // OFF
67+
LED_G.High() // OFF
68+
LED_B.High() // OFF
69+
70+
go func() {
71+
for {
72+
LED.Low()
73+
time.Sleep(time.Second)
74+
LED.High()
75+
time.Sleep(time.Second)
76+
}
77+
}()
78+
79+
for {
80+
LED_R.Low() // ON
81+
time.Sleep(time.Second)
82+
LED_R.High() // OFF
83+
LED_G.Low() // ON
84+
time.Sleep(time.Second)
85+
LED_G.High() // OFF
86+
LED_B.Low() // ON
87+
time.Sleep(time.Second)
88+
LED_B.High() // OFF
89+
}
90+
91+
}

wifinina/pins.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package wifinina
2+
3+
import "errors"
4+
5+
// Mimics machine package's pin control
6+
//
7+
// NB! These are NINA chip pins, not main unit pins.
8+
//
9+
// Digital pin values and modes taken from
10+
// https://github.com/arduino/nina-fw/blob/master/arduino/cores/esp32/wiring_digital.h
11+
12+
type Pin uint8
13+
14+
const (
15+
PinLow uint8 = iota
16+
PinHigh
17+
)
18+
19+
type PinMode uint8
20+
21+
const (
22+
PinInput PinMode = iota
23+
PinOutput
24+
PinInputPullup
25+
)
26+
27+
type PinConfig struct {
28+
Mode PinMode
29+
}
30+
31+
var (
32+
ErrPinNoDevice = errors.New("wifinina pin: device not set")
33+
)
34+
35+
var pinDevice *Device
36+
37+
func pinUseDevice(d *Device) {
38+
pinDevice = d
39+
}
40+
41+
func (p Pin) Configure(config PinConfig) error {
42+
if pinDevice == nil {
43+
return ErrPinNoDevice
44+
}
45+
return pinDevice.PinMode(uint8(p), uint8(config.Mode))
46+
}
47+
48+
func (p Pin) Set(high bool) error {
49+
if pinDevice == nil {
50+
return ErrPinNoDevice
51+
}
52+
value := PinLow
53+
if high {
54+
value = PinHigh
55+
}
56+
return pinDevice.DigitalWrite(uint8(p), value)
57+
}
58+
59+
func (p Pin) High() error {
60+
return p.Set(true)
61+
}
62+
63+
func (p Pin) Low() error {
64+
return p.Set(false)
65+
}

wifinina/wifinina.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func New(bus drivers.SPI, csPin, ackPin, gpio0Pin, resetPin machine.Pin) *Device
297297
func (d *Device) Configure() {
298298

299299
net.UseDriver(d.NewDriver())
300+
pinUseDevice(d)
300301

301302
d.CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
302303
d.ACK.Configure(machine.PinConfig{Mode: machine.PinInput})
@@ -689,6 +690,23 @@ func (d *Device) StartScanNetworks() (uint8, error) {
689690
return d.getUint8(d.req0(CmdStartScanNetworks))
690691
}
691692

693+
func (d *Device) PinMode(pin uint8, mode uint8) error {
694+
_, err := d.req2Uint8(CmdSetPinMode, pin, mode)
695+
return err
696+
}
697+
698+
func (d *Device) DigitalWrite(pin uint8, value uint8) error {
699+
_, err := d.req2Uint8(CmdSetDigitalWrite, pin, value)
700+
return err
701+
}
702+
703+
func (d *Device) AnalogWrite(pin uint8, value uint8) error {
704+
_, err := d.req2Uint8(CmdSetAnalogWrite, pin, value)
705+
return err
706+
}
707+
708+
// ------------- End of public device interface ----------------------------
709+
692710
func (d *Device) getString(l uint8, err error) (string, error) {
693711
if err != nil {
694712
return "", err
@@ -773,6 +791,14 @@ func (d *Device) reqUint8(cmd uint8, data uint8) (l uint8, err error) {
773791
return d.waitRspCmd1(cmd)
774792
}
775793

794+
// req2Uint8 sends a command to the device with two uint8 parameters
795+
func (d *Device) req2Uint8(cmd, p1, p2 uint8) (l uint8, err error) {
796+
if err := d.sendCmdPadded2(cmd, p1, p2); err != nil {
797+
return 0, err
798+
}
799+
return d.waitRspCmd1(cmd)
800+
}
801+
776802
// reqStr sends a command to the device with a single string parameter
777803
func (d *Device) reqStr(cmd uint8, p1 string) (uint8, error) {
778804
if err := d.sendCmdStr(cmd, p1); err != nil {
@@ -834,6 +860,18 @@ func (d *Device) sendCmdPadded1(cmd uint8, data uint8) error {
834860
return nil
835861
}
836862

863+
func (d *Device) sendCmdPadded2(cmd, data1, data2 uint8) error {
864+
defer d.spiChipDeselect()
865+
if err := d.waitForChipSelect(); err != nil {
866+
return err
867+
}
868+
l := d.sendCmd(cmd, 1)
869+
l += d.sendParam8(data1, false)
870+
l += d.sendParam8(data2, true)
871+
d.SPI.Transfer(dummyData)
872+
return nil
873+
}
874+
837875
func (d *Device) sendCmdStr(cmd uint8, p1 string) (err error) {
838876
defer d.spiChipDeselect()
839877
if err := d.waitForChipSelect(); err != nil {

0 commit comments

Comments
 (0)