163 lines
3.1 KiB
Go
163 lines
3.1 KiB
Go
package mt12232a
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"time"
|
|
|
|
"gitea.unprism.ru/yotia/display-test/pkg/parallel8bit"
|
|
"github.com/stianeikeland/go-rpio/v4"
|
|
)
|
|
|
|
type mt12232a struct {
|
|
logger *log.Logger
|
|
|
|
dev parallel8bit.Device
|
|
|
|
pinCS rpio.Pin
|
|
pinRES rpio.Pin
|
|
}
|
|
|
|
type Device interface {
|
|
Reset() error
|
|
|
|
WriteCode(cs rpio.State, c byte) error
|
|
WriteData(cs rpio.State, b byte) error
|
|
WriteDatas(cs rpio.State, b []byte) error
|
|
|
|
ReadData(cs rpio.State) (byte, error)
|
|
ReadStatus(cs rpio.State) byte
|
|
|
|
io.Closer
|
|
}
|
|
|
|
func New(logger *log.Logger) (Device, error) {
|
|
rpio.Open()
|
|
|
|
d := mt12232a{
|
|
logger: logger,
|
|
|
|
dev: parallel8bit.New(logger, parallel8bit.DevicePins{
|
|
PinA0: rpio.Pin(1), // 19
|
|
PinRW: rpio.Pin(13),
|
|
PinE: rpio.Pin(12),
|
|
|
|
PinDB0: rpio.Pin(22),
|
|
PinDB1: rpio.Pin(10),
|
|
PinDB2: rpio.Pin(9),
|
|
PinDB3: rpio.Pin(11),
|
|
PinDB4: rpio.Pin(21),
|
|
PinDB5: rpio.Pin(20),
|
|
PinDB6: rpio.Pin(26),
|
|
PinDB7: rpio.Pin(16),
|
|
}),
|
|
|
|
pinCS: rpio.Pin(17),
|
|
pinRES: rpio.Pin(27),
|
|
}
|
|
d.pinCS.Output()
|
|
d.pinRES.Output()
|
|
d.dev.Reset()
|
|
d.pinCS.Low()
|
|
d.pinRES.High()
|
|
|
|
return &d, nil
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func (d *mt12232a) status() {
|
|
d.logger.Printf("ST L: %08b R: %08b\n", d.ReadStatus(0)&0xFF, d.ReadStatus(1)&0xFF)
|
|
}
|
|
|
|
func (d *mt12232a) Reset() error {
|
|
d.pinRES.Low()
|
|
time.Sleep(10 * time.Microsecond)
|
|
d.pinRES.High()
|
|
time.Sleep(2 * time.Millisecond)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Write codes
|
|
|
|
func (d *mt12232a) WriteCode(cs rpio.State, c byte) error {
|
|
return d.writeByte(c, 0, cs)
|
|
}
|
|
|
|
// Write data as byte
|
|
|
|
func (d *mt12232a) WriteData(cs rpio.State, b byte) error {
|
|
return d.writeByte(b, 1, cs)
|
|
}
|
|
|
|
func (d *mt12232a) WriteDatas(cs rpio.State, b []byte) error {
|
|
return d.writeBytes(b, 1, cs)
|
|
}
|
|
|
|
// Read data
|
|
|
|
func (d *mt12232a) ReadData(cs rpio.State) (byte, error) {
|
|
return d.readByte(1, cs)
|
|
}
|
|
|
|
// Low level functions
|
|
|
|
func (d *mt12232a) writeByte(b byte, cd, cs rpio.State) error {
|
|
if err := d.waitReady(cs); err != nil {
|
|
return fmt.Errorf("wait ready: %w", err)
|
|
}
|
|
d.pinCS.Write(cs) // Select cristals
|
|
|
|
d.dev.WriteByte(b, cd)
|
|
return nil
|
|
}
|
|
|
|
func (d *mt12232a) writeBytes(b []byte, cd, cs rpio.State) error {
|
|
if err := d.waitReady(cs); err != nil {
|
|
return fmt.Errorf("wait ready: %w", err)
|
|
}
|
|
d.pinCS.Write(cs) // Select cristals
|
|
|
|
d.dev.WriteBytes(b, cd)
|
|
return nil
|
|
}
|
|
|
|
func (d *mt12232a) readByte(cd, cs rpio.State) (byte, error) {
|
|
// Setup
|
|
if err := d.waitReady(cs); err != nil {
|
|
return 0, fmt.Errorf("wait ready: %w", err)
|
|
}
|
|
d.pinCS.Write(cs) // Select cristals
|
|
|
|
return d.dev.ReadByte(cd), nil
|
|
}
|
|
|
|
// Wait, checking status byte
|
|
func (d *mt12232a) waitReady(cs rpio.State) error {
|
|
d.pinCS.Write(cs) // Select cristals
|
|
return d.dev.WaitReady()
|
|
}
|
|
|
|
func (d *mt12232a) ReadStatus(cs rpio.State) byte {
|
|
d.pinCS.Write(cs) // Select cristals
|
|
return d.dev.ReadByte(0)
|
|
}
|