wifinina: avoid busy wait

This commit is contained in:
Yurii Soldak
2021-10-20 00:08:20 +02:00
committed by Ron Evans
parent 114e24870e
commit 0f9b9d873b
3 changed files with 17 additions and 43 deletions
+6 -7
View File
@@ -87,7 +87,8 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
}
// FIXME: this 4 second timeout is simply mimicking the Arduino driver
for t := newTimer(4 * time.Second); !t.Expired(); {
start := time.Now()
for time.Since(start) < 4*time.Second {
connected, err := drv.IsConnected()
if err != nil {
return err
@@ -95,7 +96,7 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
if connected {
return nil
}
wait(1 * time.Millisecond)
time.Sleep(1 * time.Millisecond)
}
return ErrConnectionTimeout
@@ -272,15 +273,13 @@ func (drv *Driver) stop() error {
return nil
}
drv.dev.StopClient(drv.sock)
for t := newTimer(5 * time.Second); !t.Expired(); {
start := time.Now()
for time.Since(start) < 5*time.Second {
st, _ := drv.status()
if st == TCPStateClosed {
break
}
// FIXME: without the time.Sleep below this blocks until TCPStateClosed,
// however with it got goroutine stack overflows; not sure if this is still
// an issue so should investigate further
//time.Sleep(1 * time.Millisecond)
time.Sleep(1 * time.Millisecond)
}
drv.sock = NoSocketAvail
return nil
-28
View File
@@ -1,28 +0,0 @@
package wifinina
import "time"
func wait(duration time.Duration) {
newTimer(duration).WaitUntilExpired()
}
type timer struct {
start int64
interval int64
}
func newTimer(interval time.Duration) timer {
return timer{
start: time.Now().UnixNano(),
interval: int64(interval),
}
}
func (t timer) Expired() bool {
return time.Now().UnixNano() > (t.start + t.interval)
}
func (t timer) WaitUntilExpired() {
for !t.Expired() {
}
}
+11 -8
View File
@@ -379,7 +379,7 @@ func (d *Device) CheckDataSent(sock uint8) (bool, error) {
if sent > 0 {
return true, nil
}
wait(100 * time.Microsecond)
time.Sleep(100 * time.Microsecond)
}
return false, lastErr
}
@@ -1023,8 +1023,7 @@ func (d *Device) checkStartCmd() (bool, error) {
}
func (d *Device) waitForChipSelect() (err error) {
err = d.waitForChipReady()
if err == nil {
if err = d.waitForChipReady(); err == nil {
err = d.spiChipSelect()
}
return
@@ -1034,12 +1033,14 @@ func (d *Device) waitForChipReady() error {
if _debug {
println("waitForChipReady()\r")
}
for t := newTimer(10 * time.Second); !(d.ACK.Get() == false); {
if t.Expired() {
return ErrTimeoutChipReady
start := time.Now()
for time.Since(start) < 10*time.Second {
if !d.ACK.Get() {
return nil
}
time.Sleep(1 * time.Millisecond)
}
return nil
return ErrTimeoutChipReady
}
func (d *Device) spiChipSelect() error {
@@ -1047,10 +1048,12 @@ func (d *Device) spiChipSelect() error {
println("spiChipSelect()\r")
}
d.CS.Low()
for t := newTimer(5 * time.Millisecond); !t.Expired(); {
start := time.Now()
for time.Since(start) < 5*time.Millisecond {
if d.ACK.Get() {
return nil
}
time.Sleep(100 * time.Microsecond)
}
return ErrTimeoutChipSelect
}