fix: error handling, timeouts
This commit is contained in:
@ -20,17 +20,13 @@ type mt12232a struct {
|
||||
}
|
||||
|
||||
type Device interface {
|
||||
PowerOn() error
|
||||
Reset() error
|
||||
|
||||
WriteCodeL(c byte) error
|
||||
WriteCodeR(c byte) error
|
||||
WriteDataL(b byte) error
|
||||
WriteDataR(b byte) error
|
||||
WriteDatasL(b []byte) error
|
||||
WriteDatasR(b []byte) error
|
||||
WriteCode(cs rpio.State, c byte) error
|
||||
WriteData(cs rpio.State, b byte) error
|
||||
WriteDatas(cs rpio.State, b []byte) error
|
||||
|
||||
ReadDataL() (byte, error)
|
||||
ReadDataR() (byte, error)
|
||||
ReadData(cs rpio.State) (byte, error)
|
||||
ReadStatus(cs rpio.State) byte
|
||||
|
||||
io.Closer
|
||||
@ -63,6 +59,8 @@ func New(logger *log.Logger) (Device, error) {
|
||||
d.pinCS.Output()
|
||||
d.pinRES.Output()
|
||||
d.dev.Reset()
|
||||
d.pinCS.Low()
|
||||
d.pinRES.High()
|
||||
|
||||
return &d, nil
|
||||
}
|
||||
@ -72,130 +70,41 @@ func (d *mt12232a) Close() error {
|
||||
}
|
||||
|
||||
func (d *mt12232a) status() {
|
||||
d.logger.Println("STATUS:", d.ReadStatus(0)&0xF0, d.ReadStatus(1)&0xF0)
|
||||
d.logger.Printf("ST L: %08b R: %08b\n", d.ReadStatus(0)&0xFF, d.ReadStatus(1)&0xFF)
|
||||
}
|
||||
|
||||
func (d *mt12232a) PowerOn() error {
|
||||
func (d *mt12232a) Reset() error {
|
||||
d.status()
|
||||
d.logger.Println(d.ReadStatus(0)) // Should be 0
|
||||
d.logger.Println("Reset")
|
||||
d.status()
|
||||
d.pinRES.Low()
|
||||
time.Sleep(time.Microsecond)
|
||||
d.logger.Println(d.ReadStatus(0)) // Should be 48 (power off and reset)
|
||||
time.Sleep(10 * time.Microsecond)
|
||||
d.pinRES.High()
|
||||
time.Sleep(4 * time.Millisecond)
|
||||
d.logger.Println(d.ReadStatus(0)) // Should be 32 (power off)
|
||||
|
||||
// Module is reset and should be turned off
|
||||
if d.ReadStatus(0) == 0 || d.ReadStatus(1) == 0 {
|
||||
return fmt.Errorf("no response from display(or it is possible that it is turned on but...)")
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
if ((d.ReadStatus(0) >> 4) & 1) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
d.status()
|
||||
d.status()
|
||||
d.status()
|
||||
|
||||
d.logger.Println("Power on")
|
||||
d.status()
|
||||
d.WriteCodeL(0xE2) // Reset
|
||||
d.WriteCodeR(0xE2) // Reset
|
||||
d.WriteCodeL(0xEE) // ReadModifyWrite off
|
||||
d.WriteCodeR(0xEE) // ReadModifyWrite off
|
||||
d.WriteCodeL(0xA4) // Turn on common mode
|
||||
d.WriteCodeR(0xA4) // Turn on common mode
|
||||
d.WriteCodeL(0xA9) // Multiplex 1/32
|
||||
d.WriteCodeR(0xA9) // Multiplex 1/32
|
||||
d.WriteCodeL(0xC0) // Top line to 0
|
||||
d.WriteCodeR(0xC0) // Top line to 0
|
||||
d.WriteCodeL(0xA1) // Invert scan RAM
|
||||
d.WriteCodeR(0xA0) // NonInvert scan RAM
|
||||
|
||||
d.status()
|
||||
d.logger.Println("Display on")
|
||||
d.WriteCodeL(0xAF) // Display on
|
||||
d.WriteCodeR(0xAF) // Display on
|
||||
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.status()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Check that crystals are turned on
|
||||
if ((d.ReadStatus(0) >> 5) & 1) == 1 {
|
||||
d.logger.Println("Left cristal is still off")
|
||||
}
|
||||
if ((d.ReadStatus(0) >> 5) & 1) == 1 {
|
||||
d.logger.Println("Right cristal is still off")
|
||||
}
|
||||
|
||||
// The same but with error
|
||||
if ((d.ReadStatus(0) >> 5) & 1) == 1 {
|
||||
return fmt.Errorf("Left cristal is still off")
|
||||
}
|
||||
if ((d.ReadStatus(0) >> 5) & 1) == 1 {
|
||||
return fmt.Errorf("Right cristal is still off")
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write codes
|
||||
|
||||
func (d *mt12232a) WriteCodeL(c byte) error {
|
||||
return d.writeByte(c, 0, 0)
|
||||
}
|
||||
|
||||
func (d *mt12232a) WriteCodeR(c byte) error {
|
||||
return d.writeByte(c, 0, 1)
|
||||
func (d *mt12232a) WriteCode(cs rpio.State, c byte) error {
|
||||
return d.writeByte(c, 0, cs)
|
||||
}
|
||||
|
||||
// Write data as byte
|
||||
|
||||
func (d *mt12232a) WriteDataL(b byte) error {
|
||||
return d.writeByte(b, 1, 0)
|
||||
func (d *mt12232a) WriteData(cs rpio.State, b byte) error {
|
||||
return d.writeByte(b, 1, cs)
|
||||
}
|
||||
|
||||
func (d *mt12232a) WriteDataR(b byte) error {
|
||||
return d.writeByte(b, 1, 1)
|
||||
}
|
||||
|
||||
func (d *mt12232a) WriteDatasL(b []byte) error {
|
||||
return d.writeBytes(b, 1, 0)
|
||||
}
|
||||
|
||||
func (d *mt12232a) WriteDatasR(b []byte) error {
|
||||
return d.writeBytes(b, 1, 1)
|
||||
func (d *mt12232a) WriteDatas(cs rpio.State, b []byte) error {
|
||||
return d.writeBytes(b, 1, cs)
|
||||
}
|
||||
|
||||
// Read data
|
||||
|
||||
func (d *mt12232a) ReadDataL() (byte, error) {
|
||||
return d.readByte(1, 0)
|
||||
}
|
||||
|
||||
func (d *mt12232a) ReadDataR() (byte, error) {
|
||||
return d.readByte(1, 1)
|
||||
func (d *mt12232a) ReadData(cs rpio.State) (byte, error) {
|
||||
return d.readByte(1, cs)
|
||||
}
|
||||
|
||||
// Low level functions
|
||||
|
@ -9,8 +9,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
adressWriteTimeout = 140 * time.Nanosecond
|
||||
dataStrobeTimeout = 250 * time.Nanosecond // (Data transfer)
|
||||
adressWriteTimeout = 40 * time.Nanosecond
|
||||
dataStrobeTimeout = 160 * time.Nanosecond // (Data transfer)
|
||||
dataReadTimeout = 300 * time.Nanosecond
|
||||
gapTimeout = 2000 * time.Nanosecond
|
||||
maxWaitCycles = 100
|
||||
)
|
||||
|
||||
@ -68,7 +70,20 @@ func (d *device) Reset() {
|
||||
d.PinE.Output()
|
||||
d.PinRES.Output()
|
||||
d.busOutput()
|
||||
d.PinA0.Low()
|
||||
d.PinRW.Low()
|
||||
d.PinE.Low()
|
||||
d.PinDB0.Low()
|
||||
d.PinDB1.Low()
|
||||
d.PinDB2.Low()
|
||||
d.PinDB3.Low()
|
||||
d.PinDB4.Low()
|
||||
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) {
|
||||
@ -78,6 +93,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.PinDB0.Write(rpio.State((b >> 0) & 1))
|
||||
d.PinDB1.Write(rpio.State((b >> 1) & 1))
|
||||
d.PinDB2.Write(rpio.State((b >> 2) & 1))
|
||||
@ -99,8 +115,8 @@ func (d *device) WriteByte(b byte, cd rpio.State) {
|
||||
func (d *device) WriteBytes(b []byte, cd rpio.State) {
|
||||
// d.logger.Println("Write byte", b, cd, l, r)
|
||||
d.busOutput()
|
||||
d.PinRW.Low() // We write
|
||||
d.PinA0.Write(cd)
|
||||
d.PinRW.Low() // We write
|
||||
|
||||
// Write bus
|
||||
d.PinDB0.Write(rpio.State(b[0]))
|
||||
@ -113,24 +129,26 @@ func (d *device) WriteBytes(b []byte, cd rpio.State) {
|
||||
d.PinDB7.Write(rpio.State(b[7]))
|
||||
|
||||
// Strobe start
|
||||
time.Sleep(adressWriteTimeout)
|
||||
d.PinE.High()
|
||||
time.Sleep(dataStrobeTimeout)
|
||||
|
||||
// Strobe end
|
||||
d.PinE.Low()
|
||||
time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout)
|
||||
time.Sleep(gapTimeout - dataStrobeTimeout - adressWriteTimeout)
|
||||
}
|
||||
|
||||
func (d *device) ReadByte(cd rpio.State) byte {
|
||||
// Setup
|
||||
var b byte
|
||||
d.busOutput()
|
||||
d.PinRW.High() // We write
|
||||
d.busInput()
|
||||
d.PinA0.Write(cd)
|
||||
d.PinRW.High() // We read
|
||||
|
||||
// Strobe start
|
||||
time.Sleep(adressWriteTimeout)
|
||||
d.PinE.High()
|
||||
time.Sleep(dataStrobeTimeout)
|
||||
time.Sleep(dataReadTimeout)
|
||||
|
||||
// Read
|
||||
b = uint8(d.PinDB0.Read()) |
|
||||
@ -144,26 +162,26 @@ func (d *device) ReadByte(cd rpio.State) byte {
|
||||
|
||||
// Strobe end
|
||||
d.PinE.Low()
|
||||
time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout)
|
||||
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
|
||||
return b
|
||||
}
|
||||
|
||||
func (d *device) WaitReady() error {
|
||||
d.busInput() // Set bus to input
|
||||
d.PinRW.High() // We read
|
||||
d.PinA0.Low() // Status
|
||||
d.PinRW.High() // We read
|
||||
time.Sleep(adressWriteTimeout)
|
||||
|
||||
// Strobe start
|
||||
d.PinE.High()
|
||||
time.Sleep(dataStrobeTimeout)
|
||||
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())
|
||||
//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
|
||||
}
|
||||
@ -174,12 +192,12 @@ func (d *device) WaitReady() error {
|
||||
|
||||
// Strobe end
|
||||
d.PinE.Low()
|
||||
time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout)
|
||||
time.Sleep(gapTimeout - dataReadTimeout - adressWriteTimeout)
|
||||
// d.logger.Println("Ready")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set bus pins to output
|
||||
// Set bus Pins to output
|
||||
func (d *device) busOutput() {
|
||||
// if d.isBusOutput {
|
||||
// return
|
||||
|
Reference in New Issue
Block a user