|
33 | 33 | #include "shared-bindings/digitalio/DigitalInOut.h"
|
34 | 34 | #include "shared-bindings/microcontroller/__init__.h"
|
35 | 35 |
|
| 36 | +/* |
| 37 | + * Current pin limitations for ESP32-S2 ParallelBus: |
| 38 | + * - data0 pin must be byte aligned |
| 39 | + */ |
| 40 | + |
36 | 41 | void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* self,
|
37 | 42 | const mcu_pin_obj_t* data0, const mcu_pin_obj_t* command, const mcu_pin_obj_t* chip_select,
|
38 | 43 | const mcu_pin_obj_t* write, const mcu_pin_obj_t* read, const mcu_pin_obj_t* reset) {
|
39 | 44 |
|
40 |
| - mp_raise_NotImplementedError(translate("ParallelBus not yet supported")); |
| 45 | + uint8_t data_pin = data0->number; |
| 46 | + if (data_pin % 8 != 0) { |
| 47 | + mp_raise_ValueError(translate("Data 0 pin must be byte aligned.")); |
| 48 | + } |
| 49 | + |
| 50 | + for (uint8_t i = 0; i < 8; i++) { |
| 51 | + if (!pin_number_is_free(data_pin + i)) { |
| 52 | + mp_raise_ValueError_varg(translate("Bus pin %d is already in use"), i); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + gpio_dev_t *g = &GPIO; // this is the GPIO registers, see "extern gpio_dev_t GPIO" from file:gpio_struct.h |
| 57 | + |
| 58 | + // Setup the pins as "Simple GPIO outputs" see section 19.3.3 of the ESP32-S2 Reference Manual |
| 59 | + // Enable pins with "enable_w1ts" |
| 60 | + |
| 61 | + for (uint8_t i = 0; i < 8; i++) { |
| 62 | + g->enable_w1ts = (0x1 << (data_pin + i)); |
| 63 | + g->func_out_sel_cfg[data_pin + i].val= 256; /* setup output pin for simple GPIO Output, (0x100 = 256) */ |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + /* From my understanding, there is a limitation of the ESP32-S2 that does not allow single-byte writes |
| 68 | + * into the GPIO registers. See section 10.3.3 regarding "non-aligned writes" into the registers. |
| 69 | + */ |
| 70 | + |
| 71 | + |
| 72 | + if (data_pin < 31) { |
| 73 | + self->bus = (uint32_t*) &g->out; //pointer to GPIO output register (for pins 0-31) |
| 74 | + } else { |
| 75 | + self->bus = (uint32_t*) &g->out1.val; //pointer to GPIO output register (for pins >= 32) |
| 76 | + } |
| 77 | + |
| 78 | + /* SNIP - common setup of command, chip select, write and read pins, same as from SAMD and NRF ports */ |
| 79 | + self->command.base.type = &digitalio_digitalinout_type; |
| 80 | + common_hal_digitalio_digitalinout_construct(&self->command, command); |
| 81 | + common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL); |
| 82 | + |
| 83 | + self->chip_select.base.type = &digitalio_digitalinout_type; |
| 84 | + common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); |
| 85 | + common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); |
| 86 | + |
| 87 | + self->write.base.type = &digitalio_digitalinout_type; |
| 88 | + common_hal_digitalio_digitalinout_construct(&self->write, write); |
| 89 | + common_hal_digitalio_digitalinout_switch_to_output(&self->write, true, DRIVE_MODE_PUSH_PULL); |
| 90 | + |
| 91 | + self->read.base.type = &digitalio_digitalinout_type; |
| 92 | + common_hal_digitalio_digitalinout_construct(&self->read, read); |
| 93 | + common_hal_digitalio_digitalinout_switch_to_output(&self->read, true, DRIVE_MODE_PUSH_PULL); |
| 94 | + |
| 95 | + self->data0_pin = data_pin; |
| 96 | + |
| 97 | + if (write->number < 32) { |
| 98 | + self->write_clear_register = (uint32_t*) &g->out_w1tc; |
| 99 | + self->write_set_register = (uint32_t*) &g->out_w1ts; |
| 100 | + } else { |
| 101 | + self->write_clear_register = (uint32_t*) &g->out1_w1tc.val; |
| 102 | + self->write_set_register = (uint32_t*) &g->out1_w1ts.val; |
| 103 | + } |
| 104 | + |
| 105 | + // Check to see if the data and write pins are on the same register: |
| 106 | + if ( ( ((self->data0_pin < 32) && (write->number < 32)) ) || |
| 107 | + ( ((self->data0_pin > 31) && (write->number > 31)) ) ) { |
| 108 | + self->data_write_same_register = true; // data pins and write pin are on the same register |
| 109 | + } else { |
| 110 | + self->data_write_same_register = false; // data pins and write pins are on different registers |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + self->write_mask = 1 << (write->number % 32); /* the write pin triggers the LCD to latch the data */ |
| 115 | + |
| 116 | + /* SNIP - common setup of the reset pin, same as from SAMD and NRF ports */ |
| 117 | + self->reset.base.type = &mp_type_NoneType; |
| 118 | + if (reset != NULL) { |
| 119 | + self->reset.base.type = &digitalio_digitalinout_type; |
| 120 | + common_hal_digitalio_digitalinout_construct(&self->reset, reset); |
| 121 | + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); |
| 122 | + never_reset_pin_number(reset->number); |
| 123 | + common_hal_displayio_parallelbus_reset(self); |
| 124 | + } |
| 125 | + |
| 126 | + never_reset_pin_number(command->number); |
| 127 | + never_reset_pin_number(chip_select->number); |
| 128 | + never_reset_pin_number(write->number); |
| 129 | + never_reset_pin_number(read->number); |
| 130 | + for (uint8_t i = 0; i < 8; i++) { |
| 131 | + never_reset_pin_number(data_pin + i); |
| 132 | + } |
| 133 | + |
41 | 134 | }
|
42 | 135 |
|
43 | 136 | void common_hal_displayio_parallelbus_deinit(displayio_parallelbus_obj_t* self) {
|
| 137 | + /* SNIP - same as from SAMD and NRF ports */ |
| 138 | + for (uint8_t i = 0; i < 8; i++) { |
| 139 | + reset_pin_number(self->data0_pin + i); |
| 140 | + } |
44 | 141 |
|
| 142 | + reset_pin_number(self->command.pin->number); |
| 143 | + reset_pin_number(self->chip_select.pin->number); |
| 144 | + reset_pin_number(self->write.pin->number); |
| 145 | + reset_pin_number(self->read.pin->number); |
| 146 | + reset_pin_number(self->reset.pin->number); |
45 | 147 | }
|
46 | 148 |
|
47 | 149 | bool common_hal_displayio_parallelbus_reset(mp_obj_t obj) {
|
48 |
| - return false; |
| 150 | + /* SNIP - same as from SAMD and NRF ports */ |
| 151 | + displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj); |
| 152 | + if (self->reset.base.type == &mp_type_NoneType) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + common_hal_digitalio_digitalinout_set_value(&self->reset, false); |
| 157 | + common_hal_mcu_delay_us(4); |
| 158 | + common_hal_digitalio_digitalinout_set_value(&self->reset, true); |
| 159 | + return true; |
| 160 | + |
49 | 161 | }
|
50 | 162 |
|
51 | 163 | bool common_hal_displayio_parallelbus_bus_free(mp_obj_t obj) {
|
52 |
| - return false; |
| 164 | + /* SNIP - same as from SAMD and NRF ports */ |
| 165 | + return true; |
53 | 166 | }
|
54 | 167 |
|
55 | 168 | bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t obj) {
|
56 |
| - |
57 |
| - return false; |
| 169 | + /* SNIP - same as from SAMD and NRF ports */ |
| 170 | + displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj); |
| 171 | + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); |
| 172 | + return true; |
58 | 173 | }
|
59 | 174 |
|
60 | 175 | void common_hal_displayio_parallelbus_send(mp_obj_t obj, display_byte_type_t byte_type,
|
61 |
| - display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) { |
| 176 | + display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) { |
| 177 | + displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj); |
| 178 | + common_hal_digitalio_digitalinout_set_value(&self->command, byte_type == DISPLAY_DATA); |
| 179 | + |
| 180 | + uint32_t* clear_write = self->write_clear_register; |
| 181 | + uint32_t* set_write = self->write_set_register; |
| 182 | + |
| 183 | + const uint32_t mask = self->write_mask; |
| 184 | + |
| 185 | + /* Setup structures for data writing. The ESP32-S2 port differs from the SAMD and NRF ports |
| 186 | + * because I have not found a way to write a single byte into the ESP32-S2 registers. |
| 187 | + * For the ESP32-S2, I create a 32-bit data_buffer that is used to transfer the data bytes. |
| 188 | + */ |
| 189 | + |
| 190 | + *clear_write = mask; // Clear the write pin to prepare the registers before storing the |
| 191 | + // register value into data_buffer |
| 192 | + |
| 193 | + const uint32_t data_buffer = *self->bus; // store the initial output register values into the data output buffer |
| 194 | + uint8_t* data_address = ((uint8_t*) &data_buffer) + (self->data0_pin / 8); /* address inside data_buffer where |
| 195 | + * each data byte will be written to the data pin registers |
| 196 | + */ |
| 197 | + |
| 198 | + |
| 199 | + if ( self->data_write_same_register ) { // data and write pins are on the same register |
| 200 | + for (uint32_t i = 0; i < data_length; i++) { |
| 201 | + |
| 202 | + /* Note: If the write pin and data pins are controlled by the same GPIO register, we can eliminate |
| 203 | + * the "clear_write" step below, since the write pin is cleared when the data_buffer is written |
| 204 | + * to the bus. |
| 205 | + */ |
| 206 | + |
| 207 | + *(data_address) = data[i]; // stuff the data byte into the data_buffer at the correct offset byte location |
| 208 | + *self->bus = data_buffer; // write the data to the output register |
| 209 | + *set_write = mask; // set the write pin |
| 210 | + } |
| 211 | + } |
| 212 | + else { // data and write pins are on different registers |
| 213 | + for (uint32_t i = 0; i < data_length; i++) { |
| 214 | + *clear_write = mask; // clear the write pin (See comment above, this may not be necessary). |
| 215 | + *(data_address) = data[i]; // stuff the data byte into the data_buffer at the correct offset byte location |
| 216 | + *self->bus = data_buffer; // write the data to the output register |
| 217 | + *set_write = mask; // set the write pin |
| 218 | + |
| 219 | + } |
| 220 | + } |
62 | 221 |
|
63 | 222 | }
|
64 | 223 |
|
65 | 224 | void common_hal_displayio_parallelbus_end_transaction(mp_obj_t obj) {
|
66 |
| - |
| 225 | + /* SNIP - same as from SAMD and NRF ports */ |
| 226 | + displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj); |
| 227 | + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); |
67 | 228 | }
|
0 commit comments