diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 54a836cf18..647cf04044 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -586,7 +586,6 @@ msgstr "" msgid "Buffer incorrect size. Should be %d bytes." msgstr "" -#: shared-bindings/displayio/Display.c #: shared-bindings/framebufferio/FramebufferDisplay.c msgid "Buffer is not a bytearray." msgstr "" @@ -881,10 +880,6 @@ msgstr "" msgid "Data 0 pin must be byte aligned" msgstr "" -#: ports/espressif/common-hal/paralleldisplay/ParallelBus.c -msgid "Data 0 pin must be byte aligned." -msgstr "" - #: shared-module/audiocore/WaveFile.c msgid "Data chunk must follow fmt chunk" msgstr "" @@ -1238,6 +1233,10 @@ msgstr "" msgid "Internal define error" msgstr "" +#: ports/espressif/common-hal/paralleldisplay/ParallelBus.c +msgid "Internal error" +msgstr "" + #: shared-module/rgbmatrix/RGBMatrix.c #, c-format msgid "Internal error #%d" @@ -1716,6 +1715,11 @@ msgstr "" msgid "Not settable" msgstr "" +#: ports/espressif/common-hal/paralleldisplay/ParallelBus.c +#, c-format +msgid "Number of data_pins must be 8 or 16, not %d" +msgstr "" + #: shared-bindings/util.c msgid "" "Object has been deinitialized and can no longer be used. Create a new object." @@ -2110,6 +2114,10 @@ msgstr "" msgid "Source and destination buffers must be the same length" msgstr "" +#: shared-bindings/paralleldisplay/ParallelBus.c +msgid "Specify exactly one of data0 or data_pins" +msgstr "" + #: extmod/modure.c msgid "Splitting with sub-captures" msgstr "" @@ -2185,6 +2193,12 @@ msgstr "" msgid "The sample's signedness does not match the mixer's" msgstr "" +#: shared-module/paralleldisplay/ParallelBus.c +msgid "" +"This microcontroller only supports data0=, not data_pins=, because it " +"requires contiguous pins." +msgstr "" + #: shared-bindings/displayio/TileGrid.c msgid "Tile height must exactly divide bitmap height" msgstr "" diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index 4b058f7d47..c879726be6 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -183,6 +183,7 @@ CFLAGS += -DCFG_TUD_VENDOR_RX_BUFSIZE=128 -DCFG_TUD_VENDOR_TX_BUFSIZE=128 SRC_C += \ background.c \ cam.c \ + i2s_lcd_esp32s2_driver.c \ fatfs_port.c \ mphalport.c \ bindings/espidf/__init__.c \ @@ -196,6 +197,8 @@ SRC_C += \ peripherals/touch.c \ peripherals/$(IDF_TARGET)/pins.c +$(BUILD)/i2s_lcd_esp32s2_driver.o: CFLAGS += -Wno-sign-compare + ifneq ($(CIRCUITPY_USB),0) SRC_C += lib/tinyusb/src/portable/espressif/esp32sx/dcd_esp32sx.c endif diff --git a/ports/espressif/boards/espressif_hmi_devkit_1/pins.c b/ports/espressif/boards/espressif_hmi_devkit_1/pins.c index 7e201e163f..ad3b32e4e4 100644 --- a/ports/espressif/boards/espressif_hmi_devkit_1/pins.c +++ b/ports/espressif/boards/espressif_hmi_devkit_1/pins.c @@ -3,7 +3,7 @@ STATIC const mp_rom_obj_tuple_t lcd_data_tuple = { {&mp_type_tuple}, - 8, + 16, { MP_ROM_PTR(&pin_GPIO1), MP_ROM_PTR(&pin_GPIO10), diff --git a/ports/espressif/common-hal/paralleldisplay/ParallelBus.c b/ports/espressif/common-hal/paralleldisplay/ParallelBus.c index befd836247..05dcfcca54 100644 --- a/ports/espressif/common-hal/paralleldisplay/ParallelBus.c +++ b/ports/espressif/common-hal/paralleldisplay/ParallelBus.c @@ -24,138 +24,139 @@ * THE SOFTWARE. */ -#include "shared-bindings/paralleldisplay/ParallelBus.h" - #include +#include -#include "common-hal/microcontroller/Pin.h" -#include "py/runtime.h" +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/microcontroller/__init__.h" +#include "common-hal/audiobusio/__init__.h" +#include "common-hal/microcontroller/Pin.h" +#include "py/runtime.h" + +#include "i2s_lcd_driver.h" +#include "driver/gpio.h" /* * Current pin limitations for ESP32-S2 ParallelBus: * - data0 pin must be byte aligned */ -void common_hal_paralleldisplay_parallelbus_construct(paralleldisplay_parallelbus_obj_t *self, - const mcu_pin_obj_t *data0, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, +void common_hal_paralleldisplay_parallelbus_construct_nonsequential(paralleldisplay_parallelbus_obj_t *self, + uint8_t n_pins, mcu_pin_obj_t **data_pins, + const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset, uint32_t frequency) { - uint8_t data_pin = data0->number; - if (data_pin % 8 != 0) { - mp_raise_ValueError(translate("Data 0 pin must be byte aligned.")); + if (n_pins != 8 && n_pins != 16) { + mp_raise_ValueError_varg(translate("Number of data_pins must be 8 or 16, not %d"), n_pins); } - for (uint8_t i = 0; i < 8; i++) { - if (!pin_number_is_free(data_pin + i)) { + for (uint8_t i = 0; i < n_pins; i++) { + if (!common_hal_mcu_pin_is_free(data_pins[i])) { mp_raise_ValueError_varg(translate("Bus pin %d is already in use"), i); } } - gpio_dev_t *g = &GPIO; // this is the GPIO registers, see "extern gpio_dev_t GPIO" from file:gpio_struct.h + // This will throw if unsuccessful. Everything following is guaranteed to succeed. + port_i2s_allocate_i2s0(); - // Setup the pins as "Simple GPIO outputs" see section 19.3.3 of the ESP32-S2 Reference Manual - // Enable pins with "enable_w1ts" + i2s_lcd_config_t config = { + .data_width = n_pins, + .pin_num_cs = common_hal_mcu_pin_number(chip_select), + .pin_num_wr = common_hal_mcu_pin_number(write), // write strobe + .pin_num_rs = common_hal_mcu_pin_number(command), // The "register select" pin is called "command" by CircuitPython + .clk_freq = frequency, + .i2s_port = 0, + .swap_data = false, + .buffer_size = 512, + }; - for (uint8_t i = 0; i < 8; i++) { - g->enable_w1ts = (0x1 << (data_pin + i)); - g->func_out_sel_cfg[data_pin + i].val = 256; /* setup output pin for simple GPIO Output, (0x100 = 256) */ - - } - - /* From my understanding, there is a limitation of the ESP32-S2 that does not allow single-byte writes - * into the GPIO registers. See section 10.3.3 regarding "non-aligned writes" into the registers. - */ - - - if (data_pin < 31) { - self->bus = (uint32_t *)&g->out; // pointer to GPIO output register (for pins 0-31) - } else { - self->bus = (uint32_t *)&g->out1.val; // pointer to GPIO output register (for pins >= 32) - } - - /* SNIP - common setup of command, chip select, write and read pins, same as from SAMD and NRF ports */ - self->command.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->command, command); - common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL); - - self->chip_select.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); - common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); - - self->write.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->write, write); - common_hal_digitalio_digitalinout_switch_to_output(&self->write, true, DRIVE_MODE_PUSH_PULL); - - self->read.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->read, read); - common_hal_digitalio_digitalinout_switch_to_output(&self->read, true, DRIVE_MODE_PUSH_PULL); - - self->data0_pin = data_pin; - - if (write->number < 32) { - self->write_clear_register = (uint32_t *)&g->out_w1tc; - self->write_set_register = (uint32_t *)&g->out_w1ts; - } else { - self->write_clear_register = (uint32_t *)&g->out1_w1tc.val; - self->write_set_register = (uint32_t *)&g->out1_w1ts.val; - } - - // Check to see if the data and write pins are on the same register: - if ((((self->data0_pin < 32) && (write->number < 32))) || - (((self->data0_pin > 31) && (write->number > 31)))) { - self->data_write_same_register = true; // data pins and write pin are on the same register - } else { - self->data_write_same_register = false; // data pins and write pins are on different registers - } - - - self->write_mask = 1 << (write->number % 32); /* the write pin triggers the LCD to latch the data */ - - /* SNIP - common setup of the reset pin, same as from SAMD and NRF ports */ - self->reset.base.type = &mp_type_NoneType; if (reset != NULL) { - self->reset.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->reset, reset); - common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); - never_reset_pin_number(reset->number); - common_hal_paralleldisplay_parallelbus_reset(self); + common_hal_never_reset_pin(reset); + self->reset_pin_number = reset->number; + } else { + self->reset_pin_number = NO_PIN; } - never_reset_pin_number(command->number); - never_reset_pin_number(chip_select->number); - never_reset_pin_number(write->number); - never_reset_pin_number(read->number); - for (uint8_t i = 0; i < 8; i++) { - never_reset_pin_number(data_pin + i); + for (uint8_t i = 0; i < n_pins; i++) { + common_hal_never_reset_pin(data_pins[i]); + config.pin_data_num[i] = common_hal_mcu_pin_number(data_pins[i]); } + if (read != NULL) { + common_hal_never_reset_pin(read); + gpio_config_t read_config = { + .pin_bit_mask = 1ull << read->number, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&read_config); + self->read_pin_number = read->number; + gpio_set_level(read->number, true); + } + + common_hal_never_reset_pin(chip_select); + common_hal_never_reset_pin(command); + common_hal_never_reset_pin(read); + common_hal_never_reset_pin(reset); + common_hal_never_reset_pin(write); + + self->config = config; + self->handle = i2s_lcd_driver_init(&config); + + if (!self->handle) { + port_i2s_reset_instance(0); + mp_raise_RuntimeError(translate("Internal error")); + } } + +void common_hal_paralleldisplay_parallelbus_construct(paralleldisplay_parallelbus_obj_t *self, + const mcu_pin_obj_t *data0, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, + const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset, uint32_t frequency) { + char buf[7]; + mcu_pin_obj_t *data_pins[8]; + for (int i = 0; i < 8; i++) { + snprintf(buf, sizeof(buf), "GPIO%d", data0->number + i); + data_pins[i] = validate_obj_is_free_pin(mp_obj_dict_get(MP_OBJ_FROM_PTR(&mcu_pin_globals), mp_obj_new_str(buf, strlen(buf)))); + } + common_hal_paralleldisplay_parallelbus_construct_nonsequential(self, 8, data_pins, command, chip_select, write, read, reset, frequency); +} + + void common_hal_paralleldisplay_parallelbus_deinit(paralleldisplay_parallelbus_obj_t *self) { + if (!self->handle) { + return; + } + i2s_lcd_driver_deinit(self->handle); + self->handle = NULL; /* SNIP - same as from SAMD and NRF ports */ - for (uint8_t i = 0; i < 8; i++) { - reset_pin_number(self->data0_pin + i); + for (uint8_t i = 0; i < self->config.data_width; i++) { + reset_pin_number(self->config.pin_data_num[i]); } - reset_pin_number(self->command.pin->number); - reset_pin_number(self->chip_select.pin->number); - reset_pin_number(self->write.pin->number); - reset_pin_number(self->read.pin->number); - reset_pin_number(self->reset.pin->number); + reset_pin_number(self->config.pin_num_cs); + reset_pin_number(self->config.pin_num_wr); + reset_pin_number(self->read_pin_number); + reset_pin_number(self->config.pin_num_rs); + reset_pin_number(self->reset_pin_number); + + port_i2s_reset_instance(0); } bool common_hal_paralleldisplay_parallelbus_reset(mp_obj_t obj) { /* SNIP - same as from SAMD and NRF ports */ paralleldisplay_parallelbus_obj_t *self = MP_OBJ_TO_PTR(obj); - if (self->reset.base.type == &mp_type_NoneType) { + if (self->reset_pin_number == NO_PIN) { return false; } - common_hal_digitalio_digitalinout_set_value(&self->reset, false); + gpio_set_level(self->reset_pin_number, false); common_hal_mcu_delay_us(4); - common_hal_digitalio_digitalinout_set_value(&self->reset, true); + gpio_set_level(self->reset_pin_number, true); return true; } @@ -166,62 +167,25 @@ bool common_hal_paralleldisplay_parallelbus_bus_free(mp_obj_t obj) { } bool common_hal_paralleldisplay_parallelbus_begin_transaction(mp_obj_t obj) { - /* SNIP - same as from SAMD and NRF ports */ paralleldisplay_parallelbus_obj_t *self = MP_OBJ_TO_PTR(obj); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); - return true; + bool r = i2s_lcd_acquire_nonblocking(self->handle, 1); + if (r) { + gpio_set_level(self->config.pin_num_cs, false); + } + return r; } void common_hal_paralleldisplay_parallelbus_send(mp_obj_t obj, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) { paralleldisplay_parallelbus_obj_t *self = MP_OBJ_TO_PTR(obj); - common_hal_digitalio_digitalinout_set_value(&self->command, byte_type == DISPLAY_DATA); - - uint32_t *clear_write = self->write_clear_register; - uint32_t *set_write = self->write_set_register; - - const uint32_t mask = self->write_mask; - - /* Setup structures for data writing. The ESP32-S2 port differs from the SAMD and NRF ports - * because I have not found a way to write a single byte into the ESP32-S2 registers. - * For the ESP32-S2, I create a 32-bit data_buffer that is used to transfer the data bytes. - */ - - *clear_write = mask; // Clear the write pin to prepare the registers before storing the - // register value into data_buffer - - const uint32_t data_buffer = *self->bus; // store the initial output register values into the data output buffer - uint8_t *data_address = ((uint8_t *)&data_buffer) + (self->data0_pin / 8); /* address inside data_buffer where - * each data byte will be written to the data pin registers - */ - - - if (self->data_write_same_register) { // data and write pins are on the same register - for (uint32_t i = 0; i < data_length; i++) { - - /* Note: If the write pin and data pins are controlled by the same GPIO register, we can eliminate - * the "clear_write" step below, since the write pin is cleared when the data_buffer is written - * to the bus. - */ - - *(data_address) = data[i]; // stuff the data byte into the data_buffer at the correct offset byte location - *self->bus = data_buffer; // write the data to the output register - *set_write = mask; // set the write pin - } - } else { // data and write pins are on different registers - for (uint32_t i = 0; i < data_length; i++) { - *clear_write = mask; // clear the write pin (See comment above, this may not be necessary). - *(data_address) = data[i]; // stuff the data byte into the data_buffer at the correct offset byte location - *self->bus = data_buffer; // write the data to the output register - *set_write = mask; // set the write pin - - } + if (data_length) { + gpio_set_level(self->config.pin_num_rs, byte_type == DISPLAY_DATA); + i2s_lcd_write(self->handle, data, data_length); } - } void common_hal_paralleldisplay_parallelbus_end_transaction(mp_obj_t obj) { - /* SNIP - same as from SAMD and NRF ports */ paralleldisplay_parallelbus_obj_t *self = MP_OBJ_TO_PTR(obj); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + i2s_lcd_release(self->handle); + gpio_set_level(self->config.pin_num_cs, true); } diff --git a/ports/espressif/common-hal/paralleldisplay/ParallelBus.h b/ports/espressif/common-hal/paralleldisplay/ParallelBus.h index fc9b378274..1c84d9b421 100644 --- a/ports/espressif/common-hal/paralleldisplay/ParallelBus.h +++ b/ports/espressif/common-hal/paralleldisplay/ParallelBus.h @@ -28,20 +28,14 @@ #define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_PARALLELDISPLAY_PARALLELBUS_H #include "common-hal/digitalio/DigitalInOut.h" +#include "i2s_lcd_driver.h" typedef struct { mp_obj_base_t base; - uint32_t *bus; // pointer where 8 bits of data are written to the display - digitalio_digitalinout_obj_t command; - digitalio_digitalinout_obj_t chip_select; - digitalio_digitalinout_obj_t reset; - digitalio_digitalinout_obj_t write; - digitalio_digitalinout_obj_t read; - uint8_t data0_pin; // pin number for the lowest number data pin. Must be 8-bit aligned - bool data_write_same_register; // if data and write pins are in the sare - uint32_t *write_set_register; // pointer to the write group for setting the write bit to latch the data on the LCD - uint32_t *write_clear_register; // pointer to the write group for clearing the write bit to latch the data on the LCD - uint32_t write_mask; // bit mask for the single bit for the write pin register + uint8_t read_pin_number; + uint8_t reset_pin_number; + i2s_lcd_config_t config; + i2s_lcd_handle_t handle; } paralleldisplay_parallelbus_obj_t; #endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_PARALLELDISPLAY_PARALLELBUS_H diff --git a/ports/espressif/i2s_lcd_driver.h b/ports/espressif/i2s_lcd_driver.h new file mode 100644 index 0000000000..0002d6b564 --- /dev/null +++ b/ports/espressif/i2s_lcd_driver.h @@ -0,0 +1,135 @@ +/* *INDENT-OFF* */ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef __I2S_LCD_DRIVER_H__ +#define __I2S_LCD_DRIVER_H__ + +#include "driver/i2s.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define LCD_CMD_LEV (0) +#define LCD_DATA_LEV (1) + +typedef void *i2s_lcd_handle_t; /** Handle of i2s lcd driver */ + +/** + * @brief Configuration of i2s lcd mode + * + */ +typedef struct { + int8_t data_width; /*!< Parallel data width, 16bit or 8bit available */ + int8_t pin_data_num[16]; /*!< Parallel data output IO*/ + int8_t pin_num_cs; /*!< CS io num */ + int8_t pin_num_wr; /*!< Write clk io*/ + int8_t pin_num_rs; /*!< RS io num */ + int clk_freq; /*!< I2s clock frequency */ + i2s_port_t i2s_port; /*!< I2S port number */ + bool swap_data; /*!< Swap the 2 bytes of RGB565 color */ + uint32_t buffer_size; /*!< DMA buffer size */ +} i2s_lcd_config_t; + +/** + * @brief Initilize i2s lcd driver. + * + * @param config configuration of i2s + * + * @return A handle to the created i2s lcd driver, or NULL in case of error. + */ +i2s_lcd_handle_t i2s_lcd_driver_init(const i2s_lcd_config_t *config); + +/** + * @brief Deinit i2s lcd driver. + * + * @param handle i2s lcd driver handle to deinitilize + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG handle is invalid + */ +esp_err_t i2s_lcd_driver_deinit(i2s_lcd_handle_t handle); + +/** + * @brief Write a data to LCD + * + * @param handle i2s lcd driver handle + * @param data Data to write + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG handle is invalid + */ +esp_err_t i2s_lcd_write_data(i2s_lcd_handle_t handle, uint16_t data); + +/** + * @brief Write a command to LCD + * + * @param handle Handle of i2s lcd driver + * @param cmd command to write + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG handle is invalid + */ +esp_err_t i2s_lcd_write_cmd(i2s_lcd_handle_t handle, uint16_t cmd); + +/** + * @brief Write block data to LCD + * + * @param handle Handle of i2s lcd driver + * @param data Pointer of data + * @param length length of data + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG handle is invalid + */ +esp_err_t i2s_lcd_write(i2s_lcd_handle_t handle, const uint8_t *data, uint32_t length); + +/** + * @brief acquire a lock + * + * @param handle Handle of i2s lcd driver + * + * @return Always return ESP_OK + */ +esp_err_t i2s_lcd_acquire(i2s_lcd_handle_t handle); + +/** + * @brief acquire a lock, but only wait a certain period of time + * + * @param handle Handle of i2s lcd driver + * + * @return true if the lock was acquired, false otherwise + */ +bool i2s_lcd_acquire_nonblocking(i2s_lcd_handle_t handle, TickType_t ticks_to_wait); + +/** + * @brief release a lock + * + * @param handle Handle of i2s lcd driver + * + * @return Always return ESP_OK + */ +esp_err_t i2s_lcd_release(i2s_lcd_handle_t handle); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ports/espressif/i2s_lcd_esp32s2_driver.c b/ports/espressif/i2s_lcd_esp32s2_driver.c new file mode 100644 index 0000000000..3b74cc9eda --- /dev/null +++ b/ports/espressif/i2s_lcd_esp32s2_driver.c @@ -0,0 +1,489 @@ +/* *INDENT-OFF* */ +// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "sdkconfig.h" +#if CONFIG_IDF_TARGET_ESP32S2 + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "esp_log.h" +#include "driver/gpio.h" +#include "driver/i2s.h" +#include "esp_heap_caps.h" +#include "esp32s2/rom/lldesc.h" +#include "soc/system_reg.h" +#include "i2s_lcd_driver.h" + + +static const char *TAG = "ESP32S2_I2S_LCD"; + +#define I2S_CHECK(a, str, ret) if (!(a)) { \ + ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ + return ret; \ +} + +#define LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE (4000) // 4-byte aligned +#define LCD_DATA_MAX_WIDTH (24) /*!< Maximum width of LCD data bus */ + +typedef struct { + uint32_t dma_buffer_size; + uint32_t dma_half_buffer_size; + uint32_t dma_node_buffer_size; + uint32_t dma_node_cnt; + uint32_t dma_half_node_cnt; + lldesc_t *dma; + uint8_t *dma_buffer; + QueueHandle_t event_queue; + uint8_t width; + bool swap_data; +} lcd_obj_t; + +typedef struct { + lcd_obj_t lcd; + intr_handle_t lcd_cam_intr_handle; + i2s_dev_t *i2s_dev; +} lcd_cam_obj_t; + +typedef struct { + int rs_io_num; + lcd_cam_obj_t *lcd_cam_obj; + SemaphoreHandle_t mutex; +} i2s_lcd_driver_t; + +static void IRAM_ATTR i2s_isr(void *arg) { + BaseType_t HPTaskAwoken = pdFALSE; + lcd_cam_obj_t *lcd_cam_obj = (lcd_cam_obj_t *)arg; + i2s_dev_t *i2s_dev = lcd_cam_obj->i2s_dev; + + typeof(i2s_dev->int_st) status = i2s_dev->int_st; + i2s_dev->int_clr.val = status.val; + if (status.val == 0) { + return; + } + + if (status.out_eof) { + xQueueSendFromISR(lcd_cam_obj->lcd.event_queue, (void *)&status.val, &HPTaskAwoken); + } + + if (HPTaskAwoken == pdTRUE) { + portYIELD_FROM_ISR(); + } +} + + +static void lcd_dma_set_int(lcd_cam_obj_t *lcd_cam_obj) { + // Generate a data DMA linked list + for (int x = 0; x < lcd_cam_obj->lcd.dma_node_cnt; x++) { + lcd_cam_obj->lcd.dma[x].size = lcd_cam_obj->lcd.dma_node_buffer_size; + lcd_cam_obj->lcd.dma[x].length = lcd_cam_obj->lcd.dma_node_buffer_size; + lcd_cam_obj->lcd.dma[x].buf = (lcd_cam_obj->lcd.dma_buffer + lcd_cam_obj->lcd.dma_node_buffer_size * x); + lcd_cam_obj->lcd.dma[x].eof = !((x + 1) % lcd_cam_obj->lcd.dma_half_node_cnt); + lcd_cam_obj->lcd.dma[x].empty = (uint32_t)&lcd_cam_obj->lcd.dma[(x + 1) % lcd_cam_obj->lcd.dma_node_cnt]; + } + lcd_cam_obj->lcd.dma[lcd_cam_obj->lcd.dma_half_node_cnt - 1].empty = (uint32_t)NULL; + lcd_cam_obj->lcd.dma[lcd_cam_obj->lcd.dma_node_cnt - 1].empty = (uint32_t)NULL; +} + +static void lcd_dma_set_left(lcd_cam_obj_t *lcd_cam_obj, int pos, size_t len) { + int end_pos = 0, size = 0; + // Processing data length is an integer multiple of lcd_cam_obj->lcd.dma_node_buffer_size + if (len % lcd_cam_obj->lcd.dma_node_buffer_size) { + end_pos = (pos % 2) * lcd_cam_obj->lcd.dma_half_node_cnt + len / lcd_cam_obj->lcd.dma_node_buffer_size; + size = len % lcd_cam_obj->lcd.dma_node_buffer_size; + } else { + end_pos = (pos % 2) * lcd_cam_obj->lcd.dma_half_node_cnt + len / lcd_cam_obj->lcd.dma_node_buffer_size - 1; + size = lcd_cam_obj->lcd.dma_node_buffer_size; + } + // Process the tail node to make it a DMA tail + lcd_cam_obj->lcd.dma[end_pos].size = size; + lcd_cam_obj->lcd.dma[end_pos].length = size; + lcd_cam_obj->lcd.dma[end_pos].eof = 1; + lcd_cam_obj->lcd.dma[end_pos].empty = (uint32_t)NULL; +} + +static void lcd_i2s_start(i2s_dev_t *i2s_dev, uint32_t addr, size_t len) { + while (!i2s_dev->state.tx_idle) { + ; + } + i2s_dev->conf.tx_reset = 1; + i2s_dev->conf.tx_reset = 0; + i2s_dev->conf.tx_fifo_reset = 1; + i2s_dev->conf.tx_fifo_reset = 0; + i2s_dev->out_link.addr = addr; + i2s_dev->out_link.start = 1; + ets_delay_us(1); + i2s_dev->conf.tx_start = 1; +} + +static void i2s_write_data(lcd_cam_obj_t *lcd_cam_obj, uint8_t *data, size_t len) { + int event = 0; + int x = 0, y = 0, left = 0, cnt = 0; + if (len <= 0) { + ESP_LOGE(TAG, "wrong len!"); + return; + } + lcd_dma_set_int(lcd_cam_obj); + cnt = len / lcd_cam_obj->lcd.dma_half_buffer_size; + // Start signal + xQueueSend(lcd_cam_obj->lcd.event_queue, &event, 0); + // Process a complete piece of data, ping-pong operation + for (x = 0; x < cnt; x++) { + uint8_t *out = (uint8_t *)lcd_cam_obj->lcd.dma[(x % 2) * lcd_cam_obj->lcd.dma_half_node_cnt].buf; + uint8_t *in = data; + if (lcd_cam_obj->lcd.swap_data) { + for (y = 0; y < lcd_cam_obj->lcd.dma_half_buffer_size; y += 2) { + out[y + 1] = in[y + 0]; + out[y + 0] = in[y + 1]; + } + } else { + memcpy(out, in, lcd_cam_obj->lcd.dma_half_buffer_size); + } + data += lcd_cam_obj->lcd.dma_half_buffer_size; + xQueueReceive(lcd_cam_obj->lcd.event_queue, (void *)&event, portMAX_DELAY); + lcd_i2s_start(lcd_cam_obj->i2s_dev, ((uint32_t)&lcd_cam_obj->lcd.dma[(x % 2) * lcd_cam_obj->lcd.dma_half_node_cnt]) & 0xfffff, lcd_cam_obj->lcd.dma_half_buffer_size); + } + left = len % lcd_cam_obj->lcd.dma_half_buffer_size; + // Process remaining incomplete segment data + if (left) { + uint8_t *out = (uint8_t *)lcd_cam_obj->lcd.dma[(x % 2) * lcd_cam_obj->lcd.dma_half_node_cnt].buf; + uint8_t *in = data; + cnt = left - left % 2; + if (cnt) { + if (lcd_cam_obj->lcd.swap_data) { + for (y = 0; y < cnt; y += 2) { + out[y + 1] = in[y + 0]; + out[y + 0] = in[y + 1]; + } + } else { + memcpy(out, in, cnt); + } + } + + if (left % 2) { + out[cnt] = in[cnt]; + } + lcd_dma_set_left(lcd_cam_obj, x, left); + xQueueReceive(lcd_cam_obj->lcd.event_queue, (void *)&event, portMAX_DELAY); + lcd_i2s_start(lcd_cam_obj->i2s_dev, ((uint32_t)&lcd_cam_obj->lcd.dma[(x % 2) * lcd_cam_obj->lcd.dma_half_node_cnt]) & 0xfffff, left); + } + xQueueReceive(lcd_cam_obj->lcd.event_queue, (void *)&event, portMAX_DELAY); +} + + +static esp_err_t i2s_lcd_reg_config(i2s_dev_t *i2s_dev, uint16_t data_width, uint32_t clk_freq) { + // Configure the clock + i2s_dev->clkm_conf.clkm_div_num = 2; // 160MHz / 2 = 80MHz + i2s_dev->clkm_conf.clkm_div_b = 0; + i2s_dev->clkm_conf.clkm_div_a = 0; + i2s_dev->clkm_conf.clk_sel = 2; // PLL_160M_CLK + i2s_dev->clkm_conf.clk_en = 1; + + i2s_dev->conf.val = 0; + i2s_dev->fifo_conf.val = 0; + i2s_dev->fifo_conf.dscr_en = 1; + + i2s_dev->lc_conf.ahbm_fifo_rst = 1; + i2s_dev->lc_conf.ahbm_fifo_rst = 0; + i2s_dev->lc_conf.ahbm_rst = 1; + i2s_dev->lc_conf.ahbm_rst = 0; + i2s_dev->lc_conf.check_owner = 0; + + i2s_dev->timing.val = 0; + + i2s_dev->int_ena.val = 0; + i2s_dev->int_clr.val = ~0; + + i2s_dev->conf2.lcd_en = 1; + + // Configure sampling rate + // The datasheet states that Fws = Fbck / (W*2), but empirically storing + // 1 in the register gives the highest value of 20MHz, storing 2 gives + // 10MHz, (and storing 0 causes a freeze instead of acting as though 64 was + // specified). + int div_num = (20000000 + clk_freq - 1) / clk_freq; + if (div_num == 0) { + div_num = 1; + } + if (div_num > 63) { + div_num = 63; + } + i2s_dev->sample_rate_conf.tx_bck_div_num = div_num; + i2s_dev->sample_rate_conf.tx_bits_mod = data_width; + // Configuration data format + + i2s_dev->conf.tx_right_first = 1; + i2s_dev->conf.tx_msb_right = 1; + i2s_dev->conf.tx_dma_equal = 1; + + i2s_dev->conf1.tx_pcm_bypass = 1; + i2s_dev->conf1.tx_stop_en = 1; + + i2s_dev->conf2.lcd_en = 1; + + i2s_dev->conf_chan.tx_chan_mod = 1; + + i2s_dev->fifo_conf.tx_fifo_mod_force_en = 1; + i2s_dev->fifo_conf.tx_data_num = 32; + i2s_dev->fifo_conf.tx_fifo_mod = 2; + i2s_dev->fifo_conf.tx_24msb_en = 0; + + i2s_dev->lc_conf.out_rst = 1; + i2s_dev->lc_conf.out_rst = 0; + + i2s_dev->int_ena.out_eof = 1; + + + return ESP_OK; +} + +static esp_err_t lcd_set_pin(const i2s_lcd_config_t *config) { + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_num_wr], PIN_FUNC_GPIO); + gpio_set_direction(config->pin_num_wr, GPIO_MODE_OUTPUT); + gpio_set_pull_mode(config->pin_num_wr, GPIO_FLOATING); + gpio_matrix_out(config->pin_num_wr, I2S0O_WS_OUT_IDX, true, false); + + for (int i = 0; i < config->data_width; i++) { + PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_data_num[i]], PIN_FUNC_GPIO); + gpio_set_direction(config->pin_data_num[i], GPIO_MODE_OUTPUT); + gpio_set_pull_mode(config->pin_data_num[i], GPIO_FLOATING); + // High bit aligned, OUT23 is always the highest bit + gpio_matrix_out(config->pin_data_num[i], I2S0O_DATA_OUT0_IDX + (LCD_DATA_MAX_WIDTH - config->data_width) + i, false, false); + } + + return ESP_OK; +} + +static esp_err_t lcd_dma_config(lcd_cam_obj_t *lcd_cam_obj, uint32_t max_dma_buffer_size) { + int cnt = 0; + if (LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE % 2 != 0) { + ESP_LOGE(TAG, "ESP32 only supports 2-byte aligned data length"); + return ESP_FAIL; + } + if (max_dma_buffer_size >= LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE * 2) { + lcd_cam_obj->lcd.dma_node_buffer_size = LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE; + for (cnt = 0; cnt < max_dma_buffer_size - 8; cnt++) { // Find a buffer size that can divide dma_size + if ((max_dma_buffer_size - cnt) % (lcd_cam_obj->lcd.dma_node_buffer_size * 2) == 0) { + break; + } + } + lcd_cam_obj->lcd.dma_buffer_size = max_dma_buffer_size - cnt; + } else { + lcd_cam_obj->lcd.dma_node_buffer_size = max_dma_buffer_size / 2; + lcd_cam_obj->lcd.dma_buffer_size = lcd_cam_obj->lcd.dma_node_buffer_size * 2; + } + + lcd_cam_obj->lcd.dma_half_buffer_size = lcd_cam_obj->lcd.dma_buffer_size / 2; + lcd_cam_obj->lcd.dma_node_cnt = (lcd_cam_obj->lcd.dma_buffer_size) / lcd_cam_obj->lcd.dma_node_buffer_size; // Number of DMA nodes + lcd_cam_obj->lcd.dma_half_node_cnt = lcd_cam_obj->lcd.dma_node_cnt / 2; + + ESP_LOGI(TAG, "lcd_buffer_size: %d, lcd_dma_size: %d, lcd_dma_node_cnt: %d", lcd_cam_obj->lcd.dma_buffer_size, lcd_cam_obj->lcd.dma_node_buffer_size, lcd_cam_obj->lcd.dma_node_cnt); + + lcd_cam_obj->lcd.dma = (lldesc_t *)heap_caps_malloc(lcd_cam_obj->lcd.dma_node_cnt * sizeof(lldesc_t), MALLOC_CAP_DMA | MALLOC_CAP_8BIT); + lcd_cam_obj->lcd.dma_buffer = (uint8_t *)heap_caps_malloc(lcd_cam_obj->lcd.dma_buffer_size * sizeof(uint8_t), MALLOC_CAP_DMA | MALLOC_CAP_8BIT); + return ESP_OK; +} + +static esp_err_t lcd_cam_deinit(i2s_lcd_driver_t *drv) { + if (!drv->lcd_cam_obj) { + return ESP_FAIL; + } + + if (drv->lcd_cam_obj->lcd.event_queue) { + vQueueDelete(drv->lcd_cam_obj->lcd.event_queue); + } + if (drv->lcd_cam_obj->lcd.dma) { + heap_caps_free(drv->lcd_cam_obj->lcd.dma); + } + if (drv->lcd_cam_obj->lcd.dma_buffer) { + heap_caps_free(drv->lcd_cam_obj->lcd.dma_buffer); + } + + if (drv->lcd_cam_obj->lcd_cam_intr_handle) { + esp_intr_free(drv->lcd_cam_obj->lcd_cam_intr_handle); + } + + heap_caps_free(drv->lcd_cam_obj); + drv->lcd_cam_obj = NULL; + return ESP_OK; +} + +static esp_err_t lcd_cam_init(i2s_lcd_driver_t *drv, const i2s_lcd_config_t *config) { + esp_err_t ret = ESP_OK; + + lcd_cam_obj_t *lcd_cam_obj = (lcd_cam_obj_t *)heap_caps_calloc(1, sizeof(lcd_cam_obj_t), MALLOC_CAP_DMA); + if (lcd_cam_obj == NULL) { + ESP_LOGE(TAG, "lcd_cam object malloc error"); + return ESP_ERR_NO_MEM; + } + drv->lcd_cam_obj = lcd_cam_obj; + + if (I2S_NUM_0 == config->i2s_port) { + lcd_cam_obj->i2s_dev = &I2S0; + periph_module_enable(PERIPH_I2S0_MODULE); + } else { + ESP_LOGE(TAG, "Designated I2S peripheral not found"); + } + + ret |= i2s_lcd_reg_config(lcd_cam_obj->i2s_dev, config->data_width, config->clk_freq); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "lcd_cam config fail!"); + lcd_cam_deinit(drv); + return ESP_FAIL; + } + + ret |= lcd_set_pin(config); + ret |= lcd_dma_config(lcd_cam_obj, config->buffer_size); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "lcd config fail!"); + lcd_cam_deinit(drv); + return ESP_FAIL; + } + + lcd_cam_obj->lcd.event_queue = xQueueCreate(1, sizeof(int)); + lcd_cam_obj->lcd.width = config->data_width; + lcd_cam_obj->lcd.swap_data = config->swap_data; + + if (lcd_cam_obj->lcd.event_queue == NULL) { + ESP_LOGE(TAG, "lcd config fail!"); + lcd_cam_deinit(drv); + return ESP_FAIL; + } + + ret |= esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, i2s_isr, lcd_cam_obj, &lcd_cam_obj->lcd_cam_intr_handle); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "lcd_cam intr alloc fail!"); + lcd_cam_deinit(drv); + return ESP_FAIL; + } + + ESP_LOGI(TAG, "lcd init ok"); + + return ESP_OK; +} + + +/**< Public functions */ + +i2s_lcd_handle_t i2s_lcd_driver_init(const i2s_lcd_config_t *config) { + I2S_CHECK(NULL != config, "config pointer invalid", NULL); + I2S_CHECK(GPIO_IS_VALID_GPIO(config->pin_num_wr), "GPIO WR invalid", NULL); + I2S_CHECK(GPIO_IS_VALID_GPIO(config->pin_num_rs), "GPIO RS invalid", NULL); + I2S_CHECK(config->data_width > 0 && config->data_width <= 16, "Bit width out of range", NULL); + I2S_CHECK(0 == (config->data_width % 8), "Bit width must be a multiple of 8", NULL); + uint64_t pin_mask = 0; + for (size_t i = 0; i < config->data_width; i++) { + uint64_t mask = 1ULL << config->pin_data_num[i]; + I2S_CHECK(!(pin_mask & mask), "Data bus GPIO has a duplicate", NULL); + I2S_CHECK(GPIO_IS_VALID_GPIO(config->pin_data_num[i]), "Data bus gpio invalid", NULL); + pin_mask |= mask; + } + + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)heap_caps_malloc(sizeof(i2s_lcd_driver_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + I2S_CHECK(NULL != i2s_lcd_drv, "Error malloc handle of i2s lcd driver", NULL); + + esp_err_t ret = lcd_cam_init(i2s_lcd_drv, config); + if (ESP_OK != ret) { + ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, "i2s lcd driver initialize failed"); + heap_caps_free(i2s_lcd_drv); + return NULL; + } + + i2s_lcd_drv->mutex = xSemaphoreCreateMutex(); + if (i2s_lcd_drv->mutex == NULL) { + ESP_LOGE(TAG, "%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, "lcd create mutex failed"); + lcd_cam_deinit(i2s_lcd_drv); + heap_caps_free(i2s_lcd_drv); + return NULL; + } + + if (config->pin_num_cs >= 0) { + gpio_pad_select_gpio(config->pin_num_cs); + gpio_set_direction(config->pin_num_cs, GPIO_MODE_OUTPUT); + gpio_set_level(config->pin_num_cs, 0); + } + + gpio_pad_select_gpio(config->pin_num_rs); + gpio_set_direction(config->pin_num_rs, GPIO_MODE_OUTPUT); + i2s_lcd_drv->rs_io_num = config->pin_num_rs; + return (i2s_lcd_handle_t)i2s_lcd_drv; +} + +esp_err_t i2s_lcd_driver_deinit(i2s_lcd_handle_t handle) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + lcd_cam_deinit(i2s_lcd_drv); + vSemaphoreDelete(i2s_lcd_drv->mutex); + heap_caps_free(handle); + return ESP_OK; +} + +esp_err_t i2s_lcd_write_data(i2s_lcd_handle_t handle, uint16_t data) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + i2s_write_data(i2s_lcd_drv->lcd_cam_obj, (uint8_t *)&data, 2); + return ESP_OK; +} + +esp_err_t i2s_lcd_write_cmd(i2s_lcd_handle_t handle, uint16_t cmd) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + gpio_set_level(i2s_lcd_drv->rs_io_num, LCD_CMD_LEV); + i2s_write_data(i2s_lcd_drv->lcd_cam_obj, (uint8_t *)&cmd, 2); + gpio_set_level(i2s_lcd_drv->rs_io_num, LCD_DATA_LEV); + return ESP_OK; +} + +esp_err_t i2s_lcd_write(i2s_lcd_handle_t handle, const uint8_t *data, uint32_t length) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + i2s_write_data(i2s_lcd_drv->lcd_cam_obj, (uint8_t *)data, length); + + return ESP_OK; +} + +esp_err_t i2s_lcd_acquire(i2s_lcd_handle_t handle) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + BaseType_t ret = xSemaphoreTake(i2s_lcd_drv->mutex, portMAX_DELAY); + I2S_CHECK(pdTRUE == ret, "Take semaphore failed", ESP_FAIL); + return ESP_OK; +} + +bool i2s_lcd_acquire_nonblocking(i2s_lcd_handle_t handle, TickType_t ticks_to_wait) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + return xSemaphoreTake(i2s_lcd_drv->mutex, ticks_to_wait); +} + +esp_err_t i2s_lcd_release(i2s_lcd_handle_t handle) { + i2s_lcd_driver_t *i2s_lcd_drv = (i2s_lcd_driver_t *)handle; + I2S_CHECK(NULL != i2s_lcd_drv, "handle pointer invalid", ESP_ERR_INVALID_ARG); + i2s_dev_t *i2s_dev = &I2S0; +// at this point, the DMA is done but there could still be data in the FIFO. so we need +// to wait for I2S_TX_IDLE so that it's safe e.g., for calling code to deassert CS + while (!i2s_dev->state.tx_idle) { + ; + } + BaseType_t ret = xSemaphoreGive(i2s_lcd_drv->mutex); + I2S_CHECK(pdTRUE == ret, "Give semaphore failed", ESP_FAIL); + return ESP_OK; +} + +#endif // CONFIG_IDF_TARGET_ESP32S2 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 74290fe3d7..836f3c7af1 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -551,6 +551,7 @@ SRC_SHARED_MODULE_ALL = \ onewireio/__init__.c \ onewireio/OneWire.c \ os/__init__.c \ + paralleldisplay/ParallelBus.c \ qrio/__init__.c \ qrio/QRDecoder.c \ rainbowio/__init__.c \ diff --git a/shared-bindings/paralleldisplay/ParallelBus.c b/shared-bindings/paralleldisplay/ParallelBus.c index 804886233a..57bf0974bf 100644 --- a/shared-bindings/paralleldisplay/ParallelBus.c +++ b/shared-bindings/paralleldisplay/ParallelBus.c @@ -51,6 +51,7 @@ //| code is done.) So, the first time you initialize a display bus in code.py you should call //| :py:func:`displayio.release_displays` first, otherwise it will error after the first code.py run. //| +//| :param microcontroller.Pin data_pins: A list of data pins. Specify exactly one of ``data_pins`` or ``data0``. //| :param microcontroller.Pin data0: The first data pin. The rest are implied //| :param microcontroller.Pin command: Data or command pin //| :param microcontroller.Pin chip_select: Chip select pin @@ -61,9 +62,10 @@ //| ... //| STATIC mp_obj_t paralleldisplay_parallelbus_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_data0, ARG_command, ARG_chip_select, ARG_write, ARG_read, ARG_reset, ARG_frequency }; + enum { ARG_data0, ARG_data_pins, ARG_command, ARG_chip_select, ARG_write, ARG_read, ARG_reset, ARG_frequency }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_data0, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, + { MP_QSTR_data0, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } }, + { MP_QSTR_data_pins, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } }, { MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, { MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, { MP_QSTR_write, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, @@ -74,7 +76,6 @@ STATIC mp_obj_t paralleldisplay_parallelbus_make_new(const mp_obj_type_t *type, mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mcu_pin_obj_t *data0 = validate_obj_is_free_pin(args[ARG_data0].u_obj); mcu_pin_obj_t *command = validate_obj_is_free_pin(args[ARG_command].u_obj); mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj); mcu_pin_obj_t *write = validate_obj_is_free_pin(args[ARG_write].u_obj); @@ -84,7 +85,22 @@ STATIC mp_obj_t paralleldisplay_parallelbus_make_new(const mp_obj_type_t *type, paralleldisplay_parallelbus_obj_t *self = &allocate_display_bus_or_raise()->parallel_bus; self->base.type = ¶lleldisplay_parallelbus_type; - common_hal_paralleldisplay_parallelbus_construct(self, data0, command, chip_select, write, read, reset, args[ARG_frequency].u_int); + bool specified_data0 = args[ARG_data0].u_obj != mp_const_none; + bool specified_data_pins = args[ARG_data_pins].u_obj != mp_const_none; + + if (specified_data0 == specified_data_pins) { + mp_raise_ValueError(translate("Specify exactly one of data0 or data_pins")); + } + + if (specified_data0) { + mcu_pin_obj_t *data0 = validate_obj_is_free_pin(args[ARG_data0].u_obj); + common_hal_paralleldisplay_parallelbus_construct(self, data0, command, chip_select, write, read, reset, args[ARG_frequency].u_int); + } else { + uint8_t num_pins; + mcu_pin_obj_t *data_pins[16]; + validate_list_is_free_pins(MP_QSTR_data_pins, data_pins, (mp_int_t)MP_ARRAY_SIZE(data_pins), args[ARG_data_pins].u_obj, &num_pins); + common_hal_paralleldisplay_parallelbus_construct_nonsequential(self, num_pins, data_pins, command, chip_select, write, read, reset, args[ARG_frequency].u_int); + } return self; } diff --git a/shared-bindings/paralleldisplay/ParallelBus.h b/shared-bindings/paralleldisplay/ParallelBus.h index cd9e32f9be..5c6212a970 100644 --- a/shared-bindings/paralleldisplay/ParallelBus.h +++ b/shared-bindings/paralleldisplay/ParallelBus.h @@ -38,6 +38,10 @@ void common_hal_paralleldisplay_parallelbus_construct(paralleldisplay_parallelbu const mcu_pin_obj_t *data0, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset, uint32_t frequency); +void common_hal_paralleldisplay_parallelbus_construct_nonsequential(paralleldisplay_parallelbus_obj_t *self, + uint8_t n_pins, mcu_pin_obj_t **data_pins, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, + const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset, uint32_t frequency); + void common_hal_paralleldisplay_parallelbus_deinit(paralleldisplay_parallelbus_obj_t *self); bool common_hal_paralleldisplay_parallelbus_reset(mp_obj_t self); diff --git a/shared-module/paralleldisplay/ParallelBus.c b/shared-module/paralleldisplay/ParallelBus.c new file mode 100644 index 0000000000..6a53945e06 --- /dev/null +++ b/shared-module/paralleldisplay/ParallelBus.c @@ -0,0 +1,37 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#include "py/runtime.h" + +// If non-sequential pins aren't supported, then this default (weak) +// implementation will raise an exception for you. +__attribute__((weak)) +void common_hal_paralleldisplay_parallelbus_construct_nonsequential(paralleldisplay_parallelbus_obj_t *self, + uint8_t n_pins, mcu_pin_obj_t **data_pins, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, + const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset, uint32_t frequency) { + mp_raise_NotImplementedError(translate("This microcontroller only supports data0=, not data_pins=, because it requires contiguous pins.")); +}