Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions make/smoketest.mk
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ smoketest-examples: | build/smoke
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=circuitplay-express examples/i2s
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=pca10040 examples/i2s-tone
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=pca10040 examples/mcp3008
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=pca10040 examples/memstats
Expand Down Expand Up @@ -142,6 +144,8 @@ smoketest-nrf: | build/smoke
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=microbit-v2 examples/microbit-blink
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=microbit-v2 examples/i2s-tone
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=microbit-v2-s113v7 examples/microbit-blink
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=microbit-v2-s140v7 examples/microbit-blink
Expand Down Expand Up @@ -237,6 +241,8 @@ smoketest-samd: | build/smoke
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=itsybitsy-nrf52840 examples/blinky1
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=itsybitsy-nrf52840 examples/i2s-tone
@$(MD5SUM) $(SMOKE_OUT).hex
$(TINYGO) build -size short -o $(SMOKE_OUT).hex -target=qtpy examples/machinetest
@$(MD5SUM) $(SMOKE_OUT).hex

Expand Down
11 changes: 11 additions & 0 deletions src/examples/i2s-tone/itsybitsy-nrf52840.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build itsybitsy_nrf52840

package main

import "machine"

const (
pinSCK = machine.D9
pinWS = machine.D7
pinSDO = machine.D5
)
48 changes: 48 additions & 0 deletions src/examples/i2s-tone/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Plays 440 Hz on the left channel and 880 Hz on the right channel on an I2S
// DAC such as the PCM5102.
package main

import (
"machine"
"math"
)

const sampleRate = 44100

var sine [256]int16

func main() {
for i := range sine {
sine[i] = int16(math.Sin(2*math.Pi*float64(i)/float64(len(sine))) * 8000)
}

err := machine.I2S0.Configure(machine.I2SConfig{
SCK: pinSCK,
WS: pinWS,
SDO: pinSDO,
SDI: machine.NoPin,
Mode: machine.I2SModeSource,
AudioFrequency: sampleRate,
Stereo: true,
})
if err != nil {
println("could not configure I2S:", err.Error())
return
}

// Phase steps in 8.24 fixed point, one table entry per 1<<24.
left := uint32(440 * uint64(len(sine)) << 24 / sampleRate)
right := 2 * left
var phaseL, phaseR uint32
buf := make([]uint32, 64)
for {
for i := range buf {
l := uint16(sine[(phaseL>>24)%uint32(len(sine))])
r := uint16(sine[(phaseR>>24)%uint32(len(sine))])
buf[i] = uint32(l) | uint32(r)<<16
phaseL += left
phaseR += right
}
machine.I2S0.WriteStereo(buf)
}
}
11 changes: 11 additions & 0 deletions src/examples/i2s-tone/nrf52.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build nrf52 || nrf52833 || (nrf52840 && !itsybitsy_nrf52840)

package main

import "machine"

const (
pinSCK = machine.P0_03
pinWS = machine.P0_04
pinSDO = machine.P0_28
)
2 changes: 1 addition & 1 deletion src/machine/i2s.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build sam && atsamd21
//go:build (sam && atsamd21) || nrf52 || nrf52833 || nrf52840

// This is the definition for I2S bus functions.
// Actual implementations if available for any given hardware
Expand Down
Loading
Loading