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

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ out/
go.sum
.git/
*.swp
.vscode

View File

@ -1,6 +1,7 @@
package display
import (
"context"
"fmt"
"image"
"io"
@ -11,6 +12,8 @@ type Display interface {
Flush(img *image.Gray) error
GetBounds() image.Rectangle
Test(ctx context.Context) error
io.Closer
}

View File

@ -5,27 +5,19 @@ import (
"fmt"
"image"
"log"
"math/rand"
"time"
"gitea.unprism.ru/yotia/display-test/pkg/mt12232a"
"github.com/stianeikeland/go-rpio/v4"
)
var _ = rand.Int() // TMP for import to exist
type displayMt12232a struct {
logger *log.Logger
// 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) {
@ -44,28 +36,28 @@ func newMt12232a(logger *log.Logger) (Display, error) {
// }
// Setup submit goroutine
ctx, cancel := context.WithCancel(context.Background())
d := displayMt12232a{
logger: logger,
dev: dev,
submitCtx: ctx,
submitCtxCancel: cancel,
logger: logger,
dev: dev,
}
defer d.submitLoop()
// Temp debug draw
d.status("start")
//if st0, st1 := d.dev.ReadStatus(0), d.dev.ReadStatus(1); false { //st0&0x20 == 0 && st1&0x20 == 0 {
// d.logger.Println("Display is already on")
//} else {
d.status("Setup start")
if err := d.dev.Reset(); err != nil {
return nil, fmt.Errorf("reset: %w", err)
}
d.status("After reset")
time.Sleep(200 * time.Millisecond)
if st0, st1 := d.dev.ReadStatus(0), d.dev.ReadStatus(1); st0 == 0 && st1 == 0 {
return nil, fmt.Errorf("No status response from dysplay")
}
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")
d.status("Setup end")
return &d, nil
}
@ -74,63 +66,55 @@ func (d *displayMt12232a) status(label string) {
}
func (d *displayMt12232a) powerOn() error {
d.logger.Println("Power on")
if err := d.dev.WriteCode(0, 0xE2); err != nil { // Reset
// Reset
if err := d.dev.WriteCode(0, 0xE2); err != nil {
return fmt.Errorf("reset: %w", err)
}
d.status("1L")
if err := d.dev.WriteCode(1, 0xE2); err != nil { // Reset
if err := d.dev.WriteCode(1, 0xE2); err != nil {
return fmt.Errorf("reset: %w", err)
}
d.status("1R")
if err := d.dev.WriteCode(0, 0xEE); err != nil { // ReadModifyWrite off
// ReadModifyWrite off
if err := d.dev.WriteCode(0, 0xEE); err != nil {
return fmt.Errorf("RMW off: %w", err)
}
d.status("2L")
if err := d.dev.WriteCode(1, 0xEE); err != nil { // ReadModifyWrite off
if err := d.dev.WriteCode(1, 0xEE); err != nil {
return fmt.Errorf("RMW off: %w", err)
}
d.status("2R")
if err := d.dev.WriteCode(0, 0xA4); err != nil { // Turn on common mode
// Turn on common mode
if err := d.dev.WriteCode(0, 0xA4); err != nil {
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
if err := d.dev.WriteCode(1, 0xA4); err != nil {
return fmt.Errorf("turn on common mode: %w", err)
}
d.status("3R")
if err := d.dev.WriteCode(0, 0xA9); err != nil { // Multiplex 1/32
// Multiplex 1/32
if err := d.dev.WriteCode(0, 0xA9); err != nil {
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4L")
if err := d.dev.WriteCode(1, 0xA9); err != nil { // Multiplex 1/32
if err := d.dev.WriteCode(1, 0xA9); err != nil {
return fmt.Errorf("multiplex 1/32: %w", err)
}
d.status("4R")
if err := d.dev.WriteCode(0, 0xC0); err != nil { // Top line to 0
// Top line to 0
if err := d.dev.WriteCode(0, 0xC0); err != nil {
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
if err := d.dev.WriteCode(1, 0xC0); err != nil {
return fmt.Errorf("top line to 0: %w", err)
}
d.status("5R")
if err := d.dev.WriteCode(0, 0xA0); err != nil { // Invert scan RAM
// Invert scan RAM
if err := d.dev.WriteCode(0, 0xA1); err != nil {
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6L")
if err := d.dev.WriteCode(1, 0xA0); err != nil { // Invert scan RAM
if err := d.dev.WriteCode(1, 0xA0); err != nil {
return fmt.Errorf("inver scan RAM: %w", err)
}
d.status("6R")
if err := d.dev.WriteCode(0, 0xAF); err != nil { // Display on
// Display on
if err := d.dev.WriteCode(0, 0xAF); err != nil {
return fmt.Errorf("display on: %w", err)
}
d.status("7L")
if err := d.dev.WriteCode(1, 0xAF); err != nil { // Display on
if err := d.dev.WriteCode(1, 0xAF); err != nil {
return fmt.Errorf("display on: %w", err)
}
d.status("7R")
//time.Sleep(time.Second)
@ -140,25 +124,91 @@ func (d *displayMt12232a) powerOn() error {
if d.dev.ReadStatus(0)&0x20 != 0 {
return fmt.Errorf("Left cristal is off")
}
if d.dev.ReadStatus(1)&0x20 != 0 {
return fmt.Errorf("Right cristal is off")
}
return nil
}
func (d *displayMt12232a) test() {
d.logger.Println("Write")
func (d *displayMt12232a) Test(ctx context.Context) error {
//d.logger.Println("Write")
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.dev.WriteData(0, 47)
d.logger.Println("Read")
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x00)
d.logger.Println(d.dev.ReadData(0))
d.logger.Println(d.dev.ReadData(0))
d.dev.WriteCode(0, 0x13)
for range 61 {
d.dev.WriteData(0, 0xFF)
}
for c := range 256 {
select {
case <-ctx.Done():
return nil
default:
if c == 0xA0 {
continue
}
// Write
d.dev.WriteCode(0, 0xB8)
d.dev.WriteCode(0, 0x13)
d.dev.WriteData(0, byte(c)) // 0x13
d.dev.WriteData(0, byte(c)) // 0x14
//d.logger.Printf("--- %x ---\n", c)
// Check status
if d.dev.ReadStatus(0) != 0 {
d.logger.Printf("error status on %x status: %08b\n", c, d.dev.ReadStatus(0))
if err := d.dev.Reset(); err != nil {
return fmt.Errorf("reset: %w", err)
}
if err := d.powerOn(); err != nil {
return fmt.Errorf("power on: %w", err)
}
continue
}
// Read
d.dev.WriteCode(0, 0x13)
if _, err := d.dev.ReadData(0); err != nil { // Empty
return fmt.Errorf("read data 0: %w", err)
}
rc0, err := d.dev.ReadData(0) // 0x13
if err != nil {
return fmt.Errorf("read data 1: %w", err)
}
rc1, err := d.dev.ReadData(0) // 0x13
if err != nil {
return fmt.Errorf("read data 1: %w", err)
}
if rc0 != byte(c) {
d.logger.Println("Not write 0x13", c)
}
if rc1 != byte(c) {
d.logger.Println("Not write 0x14", c)
}
time.Sleep(20 * time.Millisecond)
}
}
//d.dev.WriteData(0, 0xAE)
return nil
//d.logger.Println("Read")
//d.dev.WriteCode(0, 0x14)
//d.logger.Println(d.dev.ReadData(0))
//d.logger.Println(d.dev.ReadData(0))
//d.logger.Println(d.dev.ReadData(0))
//d.logger.Println(d.dev.ReadData(0))
//d.logger.Println(d.dev.ReadData(0))
//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}
//color := []byte{0, 0, 0, 0, 0, 0, 0, 0}
//color := byte(3)
//return
start := time.Now()
//d.dev.WriteCodeL(0xB8)
//d.dev.WriteCodeL(0x13)
@ -183,26 +233,30 @@ 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")
d.status("4")
for p := byte(0); p < 4; p++ {
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.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.dev.WriteData(0, byte(0xF0^(0xFF*(c%2))))
d.dev.WriteData(0, byte(rand.Int())>>1)
//d.dev.WriteData(0, 0xF2)
//d.dev.WriteData(0, 0xF0)
//time.Sleep(2 * time.Millisecond)
}
d.dev.WriteCode(1, p|0xB8)
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.WriteData(1, byte(rand.Int()))
//d.dev.WriteData(1, byte(4*int(p)+c))
//d.dev.WriteData(1, 0xF0+byte(rand.Int()%2))
d.dev.WriteData(1, byte(rand.Int())>>1)
//time.Sleep(2 * time.Millisecond)
}
////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")
d.status("9")
end := time.Now()
d.logger.Println(end.Sub(start))
// for p := byte(0); p < 8; p++ {
@ -219,6 +273,7 @@ func (d *displayMt12232a) test() {
// }
//d.dev.WriteCodeL(0xAF) // Display on
//d.dev.WriteCodeR(0xAF) // Display on
return nil
}
@ -261,23 +316,6 @@ 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
//
// }
// }
return d.dev.Close()
}

View File

@ -1,6 +1,7 @@
package display
import (
"context"
"fmt"
"image"
"log"
@ -39,6 +40,10 @@ func newMt12864a(logger *log.Logger) (Display, error) {
return &d, nil
}
func (d *displayMt12864a) Test(ctx context.Context) error {
return nil
}
func (d *displayMt12864a) test() {
//d.SetPixel(4, 10, true)
Logo128 := [][]byte{[]byte{0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,

View File

@ -1,6 +1,7 @@
package display
import (
"context"
"fmt"
"image"
"log"
@ -52,6 +53,10 @@ func (d *displaySsd1306) GetBounds() image.Rectangle {
return d.dev.Bounds()
}
func (d *displaySsd1306) Test(ctx context.Context) error {
return nil
}
func (d *displaySsd1306) Flush(img *image.Gray) error {
return d.dev.Draw(img.Bounds(), d.img, image.Point{})
}

View File

@ -88,7 +88,6 @@ 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
@ -170,6 +169,9 @@ func mainE(ctx context.Context) error {
if err := Init(ctx); err != nil {
return err
}
if err := dev.Test(ctx); err != nil {
return err
}
defer Close()
// if err := MainLoop(ctx); err != nil {
// return err

View File

@ -39,7 +39,7 @@ func New(logger *log.Logger) (Device, error) {
logger: logger,
dev: parallel8bit.New(logger, parallel8bit.DevicePins{
PinA0: rpio.Pin(19),
PinA0: rpio.Pin(1), // 19
PinRW: rpio.Pin(13),
PinE: rpio.Pin(12),
@ -66,6 +66,20 @@ func New(logger *log.Logger) (Device, error) {
}
func (d *mt12232a) Close() error {
d.pinCS.Low()
//d.pinRES.Low() // TMP
d.dev.Pins().PinA0.Low()
d.dev.Pins().PinE.Low()
d.dev.Pins().PinRW.Low()
d.dev.Pins().PinDB0.Low()
d.dev.Pins().PinDB1.Low()
d.dev.Pins().PinDB2.Low()
d.dev.Pins().PinDB3.Low()
d.dev.Pins().PinDB4.Low()
d.dev.Pins().PinDB5.Low()
d.dev.Pins().PinDB6.Low()
d.dev.Pins().PinDB7.Low()
return rpio.Close()
}
@ -74,9 +88,6 @@ func (d *mt12232a) status() {
}
func (d *mt12232a) Reset() error {
d.status()
d.logger.Println("Reset")
d.status()
d.pinRES.Low()
time.Sleep(10 * time.Microsecond)
d.pinRES.High()
@ -147,5 +158,5 @@ func (d *mt12232a) waitReady(cs rpio.State) error {
func (d *mt12232a) ReadStatus(cs rpio.State) byte {
d.pinCS.Write(cs) // Select cristals
return d.dev.ReadByte(1)
return d.dev.ReadByte(0)
}

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