tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/ili9341/parallel_atsamd51.go (about)

     1  //go:build atsamd51
     2  
     3  package ili9341
     4  
     5  import (
     6  	"machine"
     7  	"runtime/volatile"
     8  	"unsafe"
     9  )
    10  
    11  type parallelDriver struct {
    12  	d0 machine.Pin
    13  	wr machine.Pin
    14  
    15  	setPort *uint8
    16  
    17  	clrPort *uint32
    18  	clrMask uint32
    19  
    20  	wrPortSet *uint32
    21  	wrMaskSet uint32
    22  
    23  	wrPortClr *uint32
    24  	wrMaskClr uint32
    25  }
    26  
    27  func NewParallel(d0, wr, dc, cs, rst, rd machine.Pin) *Device {
    28  	return &Device{
    29  		dc:  dc,
    30  		cs:  cs,
    31  		rd:  rd,
    32  		rst: rst,
    33  		driver: &parallelDriver{
    34  			d0: d0,
    35  			wr: wr,
    36  		},
    37  	}
    38  }
    39  
    40  func (pd *parallelDriver) configure(config *Config) {
    41  	output := machine.PinConfig{machine.PinOutput}
    42  	for pin := pd.d0; pin < pd.d0+8; pin++ {
    43  		pin.Configure(output)
    44  		pin.Low()
    45  	}
    46  	pd.wr.Configure(output)
    47  	pd.wr.High()
    48  
    49  	// Calculates the address of the OUT register from the OUTSET register and obtains an address that allows 8-bit access.
    50  	//   OUT    : offset = 0x10
    51  	//   OUTSET : offset = 0x18
    52  	setPort, _ := pd.d0.PortMaskSet()
    53  	setMask := uint32(pd.d0) & 0x1f
    54  	pd.setPort = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(setPort)) - uintptr(8) + uintptr(setMask/8)))
    55  
    56  	pd.clrPort, _ = (pd.d0).PortMaskClear()
    57  	pd.clrMask = 0xFF << uint32(pd.d0)
    58  
    59  	pd.wrPortSet, pd.wrMaskSet = pd.wr.PortMaskSet()
    60  	pd.wrPortClr, pd.wrMaskClr = pd.wr.PortMaskClear()
    61  }
    62  
    63  //go:inline
    64  func (pd *parallelDriver) write8(b byte) {
    65  	volatile.StoreUint8(pd.setPort, uint8(b))
    66  	pd.wrx()
    67  }
    68  
    69  //go:inline
    70  func (pd *parallelDriver) wrx() {
    71  	volatile.StoreUint32(pd.wrPortClr, pd.wrMaskClr)
    72  	volatile.StoreUint32(pd.wrPortSet, pd.wrMaskSet)
    73  }
    74  
    75  //go:inline
    76  func (pd *parallelDriver) write8n(b byte, n int) {
    77  	for i := 0; i < n; i++ {
    78  		pd.write8(b)
    79  	}
    80  }
    81  
    82  //go:inline
    83  func (pd *parallelDriver) write8sl(b []byte) {
    84  	for i := 0; i < len(b); i++ {
    85  		pd.write8(b[i])
    86  	}
    87  }
    88  
    89  //go:inline
    90  func (pd *parallelDriver) write16(data uint16) {
    91  	pd.write8(byte(data >> 8))
    92  	pd.write8(byte(data))
    93  }
    94  
    95  //go:inline
    96  func (pd *parallelDriver) write16n(data uint16, n int) {
    97  	for i := 0; i < n; i++ {
    98  		pd.write8(byte(data >> 8))
    99  		pd.write8(byte(data))
   100  	}
   101  }
   102  
   103  //go:inline
   104  func (pd *parallelDriver) write16sl(data []uint16) {
   105  	for i, c := 0, len(data); i < c; i++ {
   106  		pd.write8(byte(data[i] >> 8))
   107  		pd.write8(byte(data[i]))
   108  	}
   109  }