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