Skip to content

Commit

Permalink
bcm283x: increase test coverage to 55%
Browse files Browse the repository at this point in the history
Make Pin.StreamIn() not crash on gpiostream.BitStream where .Duration() == 0.
  • Loading branch information
maruel committed Aug 12, 2018
1 parent ad2e997 commit 1249f79
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 21 deletions.
3 changes: 3 additions & 0 deletions host/bcm283x/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ func (p *Pin) StreamIn(pull gpio.Pull, s gpiostream.Stream) error {
if !b.LSBF {
return errors.New("bcm283x: MSBF BitStream is not implemented yet")
}
if b.Duration() == 0 {
return errors.New("bcm283x: can't read to empty BitStream")
}
if drvGPIO.gpioMemory == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized"))
}
Expand Down
144 changes: 123 additions & 21 deletions host/bcm283x/gpio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpiostream"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/host/pmem"
"periph.io/x/periph/host/videocore"
Expand All @@ -18,27 +19,49 @@ func TestPresent(t *testing.T) {
Present()
}

func TestPin(t *testing.T) {
func TestPins(t *testing.T) {
defer reset()
if v := PinsRead0To31(); v != 0x80011010 {
t.Fatal(v)
}
PinsClear0To31(1)
PinsSet0To31(1)
if v := PinsRead32To46(); v != 0x4101 {
t.Fatal(v)
}
PinsClear32To46(1)
PinsSet32To46(1)
PinsSetup0To27(0, true, true)
PinsSetup28To45(0, true, true)
drvDMA.gpioPadMemory = nil
PinsSetup0To27(0, true, true)
PinsSetup28To45(0, true, true)
}

func TestPin_NoMem(t *testing.T) {
defer reset()
drvGPIO.gpioMemory = nil
drvDMA.gpioPadMemory = nil
// Using Pin without the driver being initialized doesn't crash.
p := Pin{name: "Foo", number: 42, defaultPull: gpio.PullDown}

// conn.Resource
if s := p.String(); s != "Foo" {
t.Fatal(s)
}

// pin.Pin
if s := p.Name(); s != "Foo" {
t.Fatal(s)
}
if n := p.Number(); n != 42 {
t.Fatal(n)
}
if d := p.DefaultPull(); d != gpio.PullDown {
t.Fatal(d)
}
if s := p.Function(); s != "ERR" {
t.Fatal(s)
}

// gpio.PinIn
if p.In(gpio.PullNoChange, gpio.NoEdge) == nil {
t.Fatal("not initialized")
}
Expand All @@ -48,14 +71,37 @@ func TestPin(t *testing.T) {
if d := p.Pull(); d != gpio.PullNoChange {
t.Fatal(d)
}
if d := p.DefaultPull(); d != gpio.PullDown {
t.Fatal(d)
}
if p.WaitForEdge(-1) {
t.Fatal("edge not initialized")
}

// gpio.PinOut
if p.Out(gpio.Low) == nil {
t.Fatal("not initialized")
}

setMemory()
if v := p.Drive(); v != 0 {
t.Fatal(v)
}
if !p.SlewLimit() {
t.Fatal("oops")
}
if !p.Hysteresis() {
t.Fatal("oops")
}
}

func TestPin(t *testing.T) {
p := Pin{name: "Foo", number: 42, defaultPull: gpio.PullDown}
// pin.Pin
if s := p.Function(); s != "In/Low" {
t.Fatal(s)
}

// gpio.PinIn
if err := p.In(gpio.PullDown, gpio.NoEdge); err != nil {
t.Fatal(err)
}
Expand All @@ -65,12 +111,11 @@ func TestPin(t *testing.T) {
if err := p.In(gpio.Float, gpio.NoEdge); err != nil {
t.Fatal(err)
}
if s := p.Function(); s != "In/Low" {
t.Fatal(s)
}
if d := p.Read(); d != gpio.Low {
t.Fatal(d)
}

// gpio.PinOut
if err := p.Out(gpio.Low); err != nil {
t.Fatal(err)
}
Expand All @@ -81,7 +126,27 @@ func TestPin(t *testing.T) {
t.Fatal(err)
}

p.number = 25
// Above 27.
for i := 0; i < 8; i++ {
drvDMA.gpioPadMemory.pads1 = pad(i)
if v := p.Drive(); v != physic.ElectricCurrent(2*(i+1))*physic.MilliAmpere {
t.Fatal(v)
}
}
drvDMA.gpioPadMemory.pads1 = 1
if v := p.Drive(); v != 4*physic.MilliAmpere {
t.Fatal(v)
}
if !p.SlewLimit() {
t.Fatal("oops")
}
if p.Hysteresis() {
t.Fatal("oops")
}
}

func TestPin_SetFunc_25(t *testing.T) {
p := Pin{name: "Foo", number: 25, defaultPull: gpio.PullDown}
p.setFunction(alt0)
if s := p.Function(); s != "<Alt0>" {
t.Fatal(s)
Expand All @@ -107,7 +172,32 @@ func TestPin(t *testing.T) {
t.Fatal(s)
}

p.number = 45
// Below 28.
if v := p.Drive(); v != 2*physic.MilliAmpere {
t.Fatal(v)
}
if !p.SlewLimit() {
t.Fatal("oops")
}
if p.Hysteresis() {
t.Fatal("oops")
}
}

func TestPin_SetFunc_33(t *testing.T) {
p := Pin{name: "Foo", number: 33, defaultPull: gpio.PullDown}
p.setFunction(alt3)
if s := p.Function(); s != "UART0_RX" {
t.Fatal(s)
}
p.setFunction(alt5)
if s := p.Function(); s != "UART1_RX" {
t.Fatal(s)
}
}

func TestPin_SetFunc_45(t *testing.T) {
p := Pin{name: "Foo", number: 45, defaultPull: gpio.PullDown}
p.setFunction(alt0)
if s := p.Function(); s != "PWM1" {
t.Fatal(s)
Expand All @@ -124,26 +214,16 @@ func TestPin(t *testing.T) {
if s := p.Function(); s != "SPI2_CS2" {
t.Fatal(s)
}

p.number = 33
p.setFunction(alt3)
if s := p.Function(); s != "UART0_RX" {
t.Fatal(s)
}
p.setFunction(alt5)
if s := p.Function(); s != "UART1_RX" {
t.Fatal(s)
}
}

func TestPinPWM(t *testing.T) {
defer reset()
// Necessary to zap out setRaw failing on non-working fake CPU memory map.
oldErrClockRegister := errClockRegister
errClockRegister = nil
defer func() {
errClockRegister = oldErrClockRegister
}()
defer reset()
setMemory()
p := Pin{name: "C1", number: 4, defaultPull: gpio.PullDown}
if err := p.PWM(gpio.DutyHalf, 2*physic.MegaHertz); err == nil || err.Error() != "bcm283x-gpio (C1): bcm283x-dma not initialized; try again as root?" {
Expand All @@ -168,6 +248,27 @@ func TestPinPWM(t *testing.T) {
}
}

func TestPinStreamIn(t *testing.T) {
defer reset()
p := Pin{name: "C1", number: 4, defaultPull: gpio.PullDown}
if err := p.StreamIn(gpio.PullDown, nil); err.Error() != "bcm283x: other Stream than BitStream are not implemented yet" {
t.Fatal(err)
}
if err := p.StreamIn(gpio.PullDown, &gpiostream.BitStream{}); err.Error() != "bcm283x: MSBF BitStream is not implemented yet" {
t.Fatal(err)
}
if err := p.StreamIn(gpio.PullDown, &gpiostream.BitStream{LSBF: true}); err.Error() != "bcm283x: can't read to empty BitStream" {
t.Fatal(err)
}
if err := p.StreamIn(gpio.PullDown, &gpiostream.BitStream{Bits: make([]byte, 1), Freq: physic.KiloHertz, LSBF: true}); err.Error() != "bcm283x-gpio (C1): frequency is too high(1kHz)" {
t.Fatal(err)
}
drvGPIO.gpioMemory = nil
if err := p.StreamIn(gpio.PullDown, &gpiostream.BitStream{Bits: make([]byte, 1), Freq: physic.KiloHertz, LSBF: true}); err.Error() != "bcm283x-gpio (C1): subsystem gpiomem not initialized" {
t.Fatal(err)
}
}

func TestDriver(t *testing.T) {
defer reset()
if s := drvGPIO.String(); s != "bcm283x-gpio" {
Expand Down Expand Up @@ -209,4 +310,5 @@ func setMemory() {
buf := make([]byte, s)
return &videocore.Mem{View: &pmem.View{Slice: buf}}, nil
}
drvDMA.gpioPadMemory = &gpioPadMap{}
}

0 comments on commit 1249f79

Please sign in to comment.