Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ILI9341 TFT driver (SPI) for ATSAMD2x #174

Merged
merged 3 commits into from
Aug 3, 2020
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ smoke-test:
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
@md5sum ./build/test.hex
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
Expand Down
29 changes: 29 additions & 0 deletions examples/ili9341/basic/atsamd21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build atsamd21

package main

import (
"machine"

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

var (
display = ili9341.NewSpi(
machine.SPI0,
machine.D0,
machine.D1,
machine.D2,
)

backlight = machine.D3
)

func init() {
machine.SPI0.Configure(machine.SPIConfig{
SCK: machine.SPI0_SCK_PIN,
SDO: machine.SPI0_SDO_PIN,
SDI: machine.SPI0_SDI_PIN,
Frequency: 24000000,
})
}
29 changes: 29 additions & 0 deletions examples/ili9341/scroll/atsamd21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build atsamd21

package main

import (
"machine"

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

var (
display = ili9341.NewSpi(
machine.SPI0,
machine.D0,
machine.D1,
machine.D2,
)

backlight = machine.D3
)

func init() {
machine.SPI0.Configure(machine.SPIConfig{
SCK: machine.SPI0_SCK_PIN,
SDO: machine.SPI0_SDO_PIN,
SDI: machine.SPI0_SDI_PIN,
Frequency: 24000000,
})
}
116 changes: 116 additions & 0 deletions ili9341/spi_atsamd21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// +build atsamd21

package ili9341

import (
"device/sam"
"machine"
)

type spiDriver struct {
bus machine.SPI
}

func NewSpi(bus machine.SPI, dc, cs, rst machine.Pin) *Device {
return &Device{
dc: dc,
cs: cs,
rst: rst,
rd: machine.NoPin,
driver: &spiDriver{
bus: bus,
},
}
}

func (pd *spiDriver) configure(config *Config) {
}

func (pd *spiDriver) write8(b byte) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(b))

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}

func (pd *spiDriver) write8n(b byte, n int) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for i, c := 0, n; i < c; i++ {
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(b))
}

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}

func (pd *spiDriver) write8sl(b []byte) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for i, c := 0, len(b); i < c; i++ {
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(b[i]))
}

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}

func (pd *spiDriver) write16(data uint16) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data >> 8)))
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data)))

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}

func (pd *spiDriver) write16n(data uint16, n int) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for i := 0; i < n; i++ {
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data >> 8)))
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data)))
}

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}

func (pd *spiDriver) write16sl(data []uint16) {
pd.bus.Bus.CTRLB.ClearBits(sam.SERCOM_SPI_CTRLB_RXEN)

for i, c := 0, len(data); i < c; i++ {
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data[i] >> 8)))
for !pd.bus.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
pd.bus.Bus.DATA.Set(uint32(uint8(data[i])))
}

pd.bus.Bus.CTRLB.SetBits(sam.SERCOM_SPI_CTRLB_RXEN)
for pd.bus.Bus.SYNCBUSY.HasBits(sam.SERCOM_SPI_SYNCBUSY_CTRLB) {
}
}