Skip to content

Commit

Permalink
ili9341: Add option to set SPI to full duplex
Browse files Browse the repository at this point in the history
fixes #80
  • Loading branch information
amirgon committed Aug 21, 2020
1 parent ea77fe6 commit 2875bba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions driver/esp32/espidf.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ enum {
ENUM_SPI_DEVICE_3WIRE = SPI_DEVICE_3WIRE,
ENUM_SPI_DEVICE_POSITIVE_CS = SPI_DEVICE_POSITIVE_CS,
ENUM_SPI_DEVICE_HALFDUPLEX = SPI_DEVICE_HALFDUPLEX,
ENUM_SPI_DEVICE_NO_DUMMY = SPI_DEVICE_NO_DUMMY,
ENUM_SPI_DEVICE_CLK_AS_CS = SPI_DEVICE_CLK_AS_CS,
};

Expand Down
9 changes: 7 additions & 2 deletions driver/esp32/ili9341.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ili9341:
def __init__(self,
miso=5, mosi=18, clk=19, cs=13, dc=12, rst=4, power=14, backlight=15, backlight_on=0, power_on=0,
spihost=esp.HSPI_HOST, mhz=40, factor=4, hybrid=True, width=240, height=320,
colormode=COLOR_MODE_BGR, rot=PORTRAIT, invert=False, double_buffer=True
colormode=COLOR_MODE_BGR, rot=PORTRAIT, invert=False, double_buffer=True, half_duplex=True
):

# Make sure Micropython was built such that color won't require processing before DMA
Expand Down Expand Up @@ -77,6 +77,7 @@ def __init__(self,
self.mhz = mhz
self.factor = factor
self.hybrid = hybrid
self.half_duplex = half_duplex

self.buf_size = (self.width * self.height * lv.color_t.SIZE) // factor

Expand Down Expand Up @@ -159,12 +160,16 @@ def disp_spi_init(self):
"max_transfer_sz": self.buf_size,
})

devcfg_flags = esp.SPI_DEVICE.NO_DUMMY
if self.half_duplex:
devcfg_flags |= esp.SPI_DEVICE.HALFDUPLEX

devcfg = esp.spi_device_interface_config_t({
"clock_speed_hz": self.mhz*1000*1000, # Clock out at DISP_SPI_MHZ MHz
"mode": 0, # SPI mode 0
"spics_io_num": self.cs, # CS pin
"queue_size": 2,
"flags": esp.SPI_DEVICE.HALFDUPLEX,
"flags": devcfg_flags,
"duty_cycle_pos": 128,
})

Expand Down
13 changes: 11 additions & 2 deletions driver/esp32/xpt2046.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class xpt2046:

MAX_RAW_COORD = const((1<<12) - 1)

def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25, spihost=esp.HSPI_HOST, mhz=5, max_cmds=16, cal_x0 = 3783, cal_y0 = 3948, cal_x1 = 242, cal_y1 = 423, transpose = True, samples = 3):
def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25,
spihost=esp.HSPI_HOST, half_duplex=True, mhz=5, max_cmds=16,
cal_x0 = 3783, cal_y0 = 3948, cal_x1 = 242, cal_y1 = 423,
transpose = True, samples = 3):

# Initializations

Expand All @@ -28,6 +31,7 @@ def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25, spihost=esp.HSPI_HOST, mhz=5
self.clk = clk
self.cs = cs
self.spihost = spihost
self.half_duplex = half_duplex
self.mhz = mhz
self.max_cmds = max_cmds
self.cal_x0 = cal_x0
Expand Down Expand Up @@ -64,13 +68,17 @@ def spi_init(self):
"max_transfer_sz": 4,
})

devcfg_flags = 0 # esp.SPI_DEVICE.NO_DUMMY
if self.half_duplex:
devcfg_flags |= esp.SPI_DEVICE.HALFDUPLEX

devcfg = esp.spi_device_interface_config_t({
"command_bits": 9, # Actually 8, but need another cycle before xpt starts transmitting response, see Figure 12 on the datasheet.
"clock_speed_hz": self.mhz*1000*1000,
"mode": 0, # SPI mode 0
"spics_io_num": self.cs, # CS pin
"queue_size": self.max_cmds,
"flags": esp.SPI_DEVICE.HALFDUPLEX,
"flags": devcfg_flags,
"duty_cycle_pos": 128,
})

Expand Down Expand Up @@ -110,6 +118,7 @@ def spi_init(self):

self.trans = [esp.spi_transaction_t({
'rx_buffer': bytearray(2),
'length': 0 if self.half_duplex else 16,
'rxlength': 16
}) for i in range(0, self.max_cmds)]

Expand Down

0 comments on commit 2875bba

Please sign in to comment.