fix: error handling, timeouts

This commit is contained in:
Andrey Egorov 2024-09-09 10:31:36 +03:00
parent 7e41196dce
commit 8889e959ee
4 changed files with 209 additions and 159 deletions

View File

@ -1,10 +1,10 @@
package display
import (
"context"
"fmt"
"image"
"log"
"math/rand"
"time"
"gitea.unprism.ru/yotia/display-test/pkg/mt12232a"
@ -16,6 +16,16 @@ type displayMt12232a struct {
// GPIO pins
dev mt12232a.Device
submitCtx context.Context // Context for submit goroutine
submitCtxCancel context.CancelFunc
submitQueue chan uint16 // Rectangle for submit
// From highter to lower:
// 1 bit - cristal
// 2 pit - page
// 6 bit - x in coords of crystal
// 6 bit - len
// Sum - 15 bit
}
func newMt12232a(logger *log.Logger) (Display, error) {
@ -33,30 +43,116 @@ func newMt12232a(logger *log.Logger) (Display, error) {
// return nil, fmt.Errorf("power on: %w", err)
// }
// Setup submit goroutine
ctx, cancel := context.WithCancel(context.Background())
d := displayMt12232a{
logger: logger,
dev: dev,
submitCtx: ctx,
submitCtxCancel: cancel,
}
defer d.submitLoop()
// Temp debug draw
d.logger.Println(d.dev.ReadStatus(0)&0xF0, d.dev.ReadStatus(1)&0xF0)
//d.logger.Println("Draw...")
//d.test()
//d.logger.Println("Draw finished")
d.status("start")
if err := d.dev.Reset(); err != nil {
return nil, fmt.Errorf("reset: %w", err)
}
if err := d.powerOn(); err != nil {
return nil, fmt.Errorf("power on: %w", err)
}
d.status("end")
d.status("test start")
d.test()
d.status("test end")
return &d, nil
}
func (d *displayMt12232a) status(label string) {
d.logger.Printf("STATUS %s -- L: %08b R: %08b\n", label, d.dev.ReadStatus(0)&0xFF, d.dev.ReadStatus(1)&0xFF)
}
func (d *displayMt12232a) powerOn() error {
d.logger.Println("Power on")
if err := d.dev.WriteCode(0, 0xE2); err != nil { // Reset
return fmt.Errorf("reset: %w", err)
}
d.status("1L")
if err := d.dev.WriteCode(1, 0xE2); err != nil { // Reset
return fmt.Errorf("reset: %w", err)
}
d.status("1R")
if err := d.dev.WriteCode(0, 0xEE); err != nil { // ReadModifyWrite off
return fmt.Errorf("RMW off: %w", err)
}
d.status("2L")
if err := d.dev.WriteCode(1, 0xEE); err != nil { // ReadModifyWrite off
return fmt.Errorf("RMW off: %w", err)
}
d.status("2R")
if err := d.dev.WriteCode(0, 0xA4); err != nil { // Turn on common mode
return fmt.Errorf("turn on common mode: %w", err)
}
d.status("3L")
if err := d.dev.WriteCode(1, 0xA4); err != nil { // Turn on common mode
return fmt.Errorf("turn on common mode: %w", err)
}
d.status("3R")
if err := d.dev.WriteCode(0, 0xA9); err != nil { // Multiplex 1/32
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4L")
if err := d.dev.WriteCode(1, 0xA9); err != nil { // Multiplex 1/32
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4R")
if err := d.dev.WriteCode(0, 0xC0); err != nil { // Top line to 0
return fmt.Errorf("top line to 0: %w", err)
}
d.status("5L")
if err := d.dev.WriteCode(1, 0xC0); err != nil { // Top line to 0
return fmt.Errorf("top line to 0: %w", err)
}
d.status("5R")
if err := d.dev.WriteCode(0, 0xA0); err != nil { // Invert scan RAM
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6L")
if err := d.dev.WriteCode(1, 0xA0); err != nil { // Invert scan RAM
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6R")
if err := d.dev.WriteCode(0, 0xAF); err != nil { // Display on
return fmt.Errorf("display on: %w", err)
}
d.status("7L")
if err := d.dev.WriteCode(1, 0xAF); err != nil { // Display on
return fmt.Errorf("display on: %w", err)
}
d.status("7R")
//time.Sleep(time.Second)
// Check that crystals are turned on
// The same but with error
if d.dev.ReadStatus(0)&0x20 != 0 {
return fmt.Errorf("Left cristal is off")
}
return nil
}
func (d *displayMt12232a) test() {
d.logger.Println("Write")
d.dev.WriteCodeL(0xB8)
d.dev.WriteCodeL(0x13)
d.dev.WriteDataL(47)
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.dev.WriteData(0, 47)
d.logger.Println("Read")
// d.dev.WriteCodeL(0xB8)
d.dev.WriteCodeL(0x13)
d.logger.Println(d.dev.ReadDataL())
d.logger.Println(d.dev.ReadDataL())
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.logger.Println(d.dev.ReadData(0))
d.logger.Println(d.dev.ReadData(0))
//color := []byte{1, 0, 1, 0, 1, 0, 1, 0}
//color := []byte{1, 0, 1, 0, 1, 0, 0, 0}
@ -87,18 +183,26 @@ func (d *displayMt12232a) test() {
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 0, 0})
//d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 1, 0})
//d.status("4")
for p := byte(0); p < 4; p++ {
d.dev.WriteCodeL(p | 0xB8)
d.dev.WriteCodeL(0x13)
d.dev.WriteCode(0, p|0xB8)
// d.status("5")
d.dev.WriteCode(0, 0x13)
//d.status("6")
for c := 0; c < 61; c++ {
d.dev.WriteDatasL([]byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
}
d.dev.WriteCodeR(p | 0xB8)
d.dev.WriteCodeR(0x00)
for c := 0; c < 61; c++ {
d.dev.WriteDatasR([]byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
//d.dev.WriteDatas(0, []byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
d.dev.WriteDatas(0, []byte{0, 0, 0, 0, 0, 0, 0, 0})
}
////d.status("7")
//d.dev.WriteCode(1, p|0xB8)
////d.status("8")
//d.dev.WriteCode(1, 0x00)
//for c := 0; c < 61; c++ {
// d.dev.WriteDatas(1, []byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)})
// //d.dev.WriteDatas(1, []byte{0, 0, 0, 1, 0, 0, 0, 0})
//}
}
//d.status("9")
end := time.Now()
d.logger.Println(end.Sub(start))
// for p := byte(0); p < 8; p++ {
@ -124,10 +228,10 @@ func (d *displayMt12232a) GetBounds() image.Rectangle {
func (d *displayMt12232a) Flush(img *image.Gray) error {
for p := byte(0); p < 4; p++ {
d.dev.WriteCodeL((3 - p) | 0xB8)
d.dev.WriteCodeL(0x13)
d.dev.WriteCode(0, (3-p)|0xB8)
d.dev.WriteCode(0, 0x13)
for c := 0; c < 61; c++ {
d.dev.WriteDatasL([]byte{
d.dev.WriteDatas(0, []byte{
img.Pix[int(p<<3+7)*122+c],
img.Pix[int(p<<3+6)*122+c],
img.Pix[int(p<<3+5)*122+c],
@ -138,10 +242,10 @@ func (d *displayMt12232a) Flush(img *image.Gray) error {
img.Pix[int(p<<3+0)*122+c],
})
}
d.dev.WriteCodeR((3 - p) | 0xB8)
d.dev.WriteCodeR(0x00)
d.dev.WriteCode(1, (3-p)|0xB8)
d.dev.WriteCode(1, 0x00)
for c := 61; c < 122; c++ {
d.dev.WriteDatasR([]byte{
d.dev.WriteDatas(1, []byte{
img.Pix[int(p<<3+7)*122+c],
img.Pix[int(p<<3+6)*122+c],
img.Pix[int(p<<3+5)*122+c],
@ -157,5 +261,23 @@ func (d *displayMt12232a) Flush(img *image.Gray) error {
}
func (d *displayMt12232a) Close() error {
if d.submitCtxCancel != nil {
d.submitCtxCancel()
}
return rpio.Close()
}
func (d *displayMt12232a) submitLoop() {
// for {
// select {
// case <-d.submitCtx.Done():
// return
// case msg := <-d.submitQueue:
// crystal := (msg >> 14) & 0x01
// page := (msg >> 12) & 0x04
// x := (msg >> 6) & 0x4F
// len := (msg >> 0) & 0x4F
//
// }
// }
}

17
main.go
View File

@ -88,16 +88,17 @@ func Init(ctx context.Context) error {
if err != nil {
return fmt.Errorf("new display: %w", err)
}
time.Sleep(time.Second)
logger.Println("Display inited")
// Create drawer
d := drawer.New(dev)
d.Clear()
//d.FillBar(0, 0, 10, 10, 1)
d.PutText(0, 10, "CGSG")
d.PutText(0, 24, "forever!!!")
d.Flush()
//// Create drawer
//d := drawer.New(dev)
//
//d.Clear()
////d.FillBar(0, 0, 10, 10, 1)
//d.PutText(0, 10, "CGSG")
//d.PutText(0, 24, "forever!!!")
//d.Flush()
// Modem init
//mInitCtx, mInitCtxCancel := context.WithCancel(ctx)

View File

@ -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

View File

@ -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