fix: power on

This commit is contained in:
Andrey Egorov
2024-09-11 18:55:26 +03:00
parent 8889e959ee
commit 30359a0a22
8 changed files with 198 additions and 128 deletions

View File

@ -9,10 +9,10 @@ import (
)
const (
adressWriteTimeout = 40 * time.Nanosecond
dataStrobeTimeout = 160 * time.Nanosecond // (Data transfer)
dataReadTimeout = 300 * time.Nanosecond
gapTimeout = 2000 * time.Nanosecond
adressWriteTimeout = 5000 * time.Nanosecond // 40
dataStrobeTimeout = 2000 * time.Nanosecond // (Data transfer) 160
dataReadTimeout = 4000 * time.Nanosecond // 300
gapTimeout = 20000 * time.Nanosecond // TIM // 2000
maxWaitCycles = 100
)
@ -41,9 +41,6 @@ type DevicePins struct {
PinDB5 rpio.Pin
PinDB6 rpio.Pin
PinDB7 rpio.Pin
PinE1 rpio.Pin
PinE2 rpio.Pin
PinRES rpio.Pin
}
type device struct {
@ -68,11 +65,10 @@ func (d *device) Reset() {
d.PinA0.Output()
d.PinRW.Output()
d.PinE.Output()
d.PinRES.Output()
d.busOutput()
d.PinA0.Low()
d.PinRW.Low()
d.PinE.Low()
d.PinE.High()
d.PinDB0.Low()
d.PinDB1.Low()
d.PinDB2.Low()
@ -81,9 +77,6 @@ func (d *device) Reset() {
d.PinDB5.Low()
d.PinDB6.Low()
d.PinDB7.Low()
d.PinE1.Low()
d.PinE2.Low()
d.PinRES.High()
}
func (d *device) WriteByte(b byte, cd rpio.State) {
@ -93,7 +86,7 @@ func (d *device) WriteByte(b byte, cd rpio.State) {
d.PinA0.Write(cd)
// Write bus
d.logger.Printf("Write byte %x\n", b)
//d.logger.Printf("Write byte %x\n", b)
d.PinDB0.Write(rpio.State((b >> 0) & 1))
d.PinDB1.Write(rpio.State((b >> 1) & 1))
d.PinDB2.Write(rpio.State((b >> 2) & 1))
@ -103,17 +96,20 @@ func (d *device) WriteByte(b byte, cd rpio.State) {
d.PinDB6.Write(rpio.State((b >> 6) & 1))
d.PinDB7.Write(rpio.State((b >> 7) & 1))
// Strobe start
d.PinE.High()
time.Sleep(dataStrobeTimeout)
time.Sleep(adressWriteTimeout)
// Strobe end
d.PinE.Low()
time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout)
d.PinE.Low() // Strobe start
time.Sleep(dataStrobeTimeout)
d.PinE.High() // Strobe end
// time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout)
time.Sleep(time.Millisecond)
}
func (d *device) WriteBytes(b []byte, cd rpio.State) {
// d.logger.Println("Write byte", b, cd, l, r)
d.logger.Println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
d.busOutput()
d.PinA0.Write(cd)
d.PinRW.Low() // We write
@ -128,26 +124,26 @@ func (d *device) WriteBytes(b []byte, cd rpio.State) {
d.PinDB6.Write(rpio.State(b[6]))
d.PinDB7.Write(rpio.State(b[7]))
// Strobe start
time.Sleep(adressWriteTimeout)
d.PinE.High()
time.Sleep(dataStrobeTimeout)
// Strobe end
d.PinE.Low()
d.PinE.Low() // Strobe start
time.Sleep(dataStrobeTimeout)
d.PinE.High() // Strobe end
time.Sleep(gapTimeout - dataStrobeTimeout - adressWriteTimeout)
//time.Sleep(time.Millisecond)
}
func (d *device) ReadByte(cd rpio.State) byte {
// Setup
var b byte
d.busInput()
d.PinA0.Write(cd)
d.PinRW.High() // We read
d.busInput()
// Strobe start
time.Sleep(adressWriteTimeout)
d.PinE.High()
d.PinE.Low()
time.Sleep(dataReadTimeout)
// Read
@ -159,9 +155,18 @@ func (d *device) ReadByte(cd rpio.State) byte {
(uint8(d.PinDB5.Read()) << 5) |
(uint8(d.PinDB6.Read()) << 6) |
(uint8(d.PinDB7.Read()) << 7)
//d.logger.Printf("Read ---- %d%d%d%d%d%d%d%d\n",
// d.PinDB0.Read(),
// d.PinDB1.Read(),
// d.PinDB2.Read(),
// d.PinDB3.Read(),
// d.PinDB4.Read(),
// d.PinDB5.Read(),
// d.PinDB6.Read(),
// d.PinDB7.Read())
// Strobe end
d.PinE.Low()
d.PinE.High()
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
return b
}
@ -173,25 +178,25 @@ func (d *device) WaitReady() error {
time.Sleep(adressWriteTimeout)
// Strobe start
d.PinE.High()
d.PinE.Low()
time.Sleep(dataReadTimeout)
// Wait status flag drop
ok := false
d.busInput() // Set bus to input
for counter := 0; counter < maxWaitCycles; counter++ {
if d.PinDB7.Read() != rpio.High {
//d.logger.Printf("BUS:%d%d%d%d%d%d%d%d\n", d.PinDB0.Read(), d.PinDB1.Read(), d.PinDB2.Read(), d.PinDB3.Read(), d.PinDB4.Read(), d.PinDB5.Read(), d.PinDB6.Read(), d.PinDB7.Read())
ok = true
break
}
fmt.Print(".")
}
if !ok {
return fmt.Errorf("busy timeout")
}
// Strobe end
d.PinE.Low()
d.PinE.High()
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
// d.logger.Println("Ready")
return nil