summaryrefslogtreecommitdiff
path: root/drivers/painter/comms
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/painter/comms')
-rw-r--r--drivers/painter/comms/qp_comms_dummy.c34
-rw-r--r--drivers/painter/comms/qp_comms_dummy.h11
-rw-r--r--drivers/painter/comms/qp_comms_i2c.c94
-rw-r--r--drivers/painter/comms/qp_comms_i2c.h28
-rw-r--r--drivers/painter/comms/qp_comms_spi.c10
-rw-r--r--drivers/painter/comms/qp_comms_spi.h2
6 files changed, 177 insertions, 2 deletions
diff --git a/drivers/painter/comms/qp_comms_dummy.c b/drivers/painter/comms/qp_comms_dummy.c
new file mode 100644
index 0000000000..2ed49d2232
--- /dev/null
+++ b/drivers/painter/comms/qp_comms_dummy.c
@@ -0,0 +1,34 @@
+// Copyright 2023 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#ifdef QUANTUM_PAINTER_DUMMY_COMMS_ENABLE
+
+# include "qp_comms_dummy.h"
+
+static bool dummy_comms_init(painter_device_t device) {
+ // No-op.
+ return true;
+}
+
+static bool dummy_comms_start(painter_device_t device) {
+ // No-op.
+ return true;
+}
+
+static void dummy_comms_stop(painter_device_t device) {
+ // No-op.
+}
+
+uint32_t dummy_comms_send(painter_device_t device, const void *data, uint32_t byte_count) {
+ // No-op.
+ return byte_count;
+}
+
+painter_comms_vtable_t dummy_comms_vtable = {
+ // These are all effective no-op's because they're not actually needed.
+ .comms_init = dummy_comms_init,
+ .comms_start = dummy_comms_start,
+ .comms_stop = dummy_comms_stop,
+ .comms_send = dummy_comms_send};
+
+#endif // QUANTUM_PAINTER_DUMMY_COMMS_ENABLE
diff --git a/drivers/painter/comms/qp_comms_dummy.h b/drivers/painter/comms/qp_comms_dummy.h
new file mode 100644
index 0000000000..b2d5d6eea5
--- /dev/null
+++ b/drivers/painter/comms/qp_comms_dummy.h
@@ -0,0 +1,11 @@
+// Copyright 2023 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+#pragma once
+
+#ifdef QUANTUM_PAINTER_DUMMY_COMMS_ENABLE
+
+# include "qp_internal.h"
+
+extern painter_comms_vtable_t dummy_comms_vtable;
+
+#endif // QUANTUM_PAINTER_DUMMY_COMMS_ENABLE
diff --git a/drivers/painter/comms/qp_comms_i2c.c b/drivers/painter/comms/qp_comms_i2c.c
new file mode 100644
index 0000000000..ec45ddfb3b
--- /dev/null
+++ b/drivers/painter/comms/qp_comms_i2c.c
@@ -0,0 +1,94 @@
+// Copyright 2022 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#ifdef QUANTUM_PAINTER_I2C_ENABLE
+
+# include "i2c_master.h"
+# include "qp_comms_i2c.h"
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Helpers
+
+static uint32_t qp_comms_i2c_send_raw(painter_device_t device, const void *data, uint32_t byte_count) {
+ painter_driver_t * driver = (painter_driver_t *)device;
+ qp_comms_i2c_config_t *comms_config = (qp_comms_i2c_config_t *)driver->comms_config;
+ i2c_status_t res = i2c_transmit(comms_config->chip_address << 1, data, byte_count, I2C_TIMEOUT);
+ if (res < 0) {
+ return 0;
+ }
+ return byte_count;
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Base I2C support
+
+bool qp_comms_i2c_init(painter_device_t device) {
+ i2c_init();
+ return true;
+}
+
+bool qp_comms_i2c_start(painter_device_t device) {
+ painter_driver_t * driver = (painter_driver_t *)device;
+ qp_comms_i2c_config_t *comms_config = (qp_comms_i2c_config_t *)driver->comms_config;
+ return i2c_start(comms_config->chip_address << 1) == I2C_STATUS_SUCCESS;
+}
+
+uint32_t qp_comms_i2c_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
+ return qp_comms_i2c_send_raw(device, data, byte_count);
+}
+
+void qp_comms_i2c_stop(painter_device_t device) {
+ i2c_stop();
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Command+Data I2C support
+
+static const uint8_t cmd_byte = 0x00;
+static const uint8_t data_byte = 0x40;
+
+void qp_comms_i2c_cmddata_send_command(painter_device_t device, uint8_t cmd) {
+ uint8_t buf[2] = {cmd_byte, cmd};
+ qp_comms_i2c_send_raw(device, &buf, 2);
+}
+
+uint32_t qp_comms_i2c_cmddata_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
+ uint8_t buf[1 + byte_count];
+ buf[0] = data_byte;
+ memcpy(&buf[1], data, byte_count);
+ if (qp_comms_i2c_send_raw(device, buf, sizeof(buf)) != sizeof(buf)) {
+ return 0;
+ }
+ return byte_count;
+}
+
+void qp_comms_i2c_bulk_command_sequence(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
+ uint8_t buf[32];
+ for (size_t i = 0; i < sequence_len;) {
+ uint8_t command = sequence[i];
+ uint8_t delay = sequence[i + 1];
+ uint8_t num_bytes = sequence[i + 2];
+ buf[0] = cmd_byte;
+ buf[1] = command;
+ memcpy(&buf[2], &sequence[i + 3], num_bytes);
+ qp_comms_i2c_send_raw(device, buf, num_bytes + 2);
+ if (delay > 0) {
+ wait_ms(delay);
+ }
+ i += (3 + num_bytes);
+ }
+}
+
+const painter_comms_with_command_vtable_t i2c_comms_cmddata_vtable = {
+ .base =
+ {
+ .comms_init = qp_comms_i2c_init,
+ .comms_start = qp_comms_i2c_start,
+ .comms_send = qp_comms_i2c_cmddata_send_data,
+ .comms_stop = qp_comms_i2c_stop,
+ },
+ .send_command = qp_comms_i2c_cmddata_send_command,
+ .bulk_command_sequence = qp_comms_i2c_bulk_command_sequence,
+};
+
+#endif // QUANTUM_PAINTER_I2C_ENABLE
diff --git a/drivers/painter/comms/qp_comms_i2c.h b/drivers/painter/comms/qp_comms_i2c.h
new file mode 100644
index 0000000000..70083d6526
--- /dev/null
+++ b/drivers/painter/comms/qp_comms_i2c.h
@@ -0,0 +1,28 @@
+// Copyright 2022 Nick Brassel (@tzarc)
+// SPDX-License-Identifier: GPL-2.0-or-later
+#pragma once
+
+#ifdef QUANTUM_PAINTER_I2C_ENABLE
+
+# include <stdint.h>
+
+# include "gpio.h"
+# include "qp_internal.h"
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Base I2C support
+
+typedef struct qp_comms_i2c_config_t {
+ uint8_t chip_address;
+} qp_comms_i2c_config_t;
+
+bool qp_comms_i2c_init(painter_device_t device);
+bool qp_comms_i2c_start(painter_device_t device);
+uint32_t qp_comms_i2c_send_data(painter_device_t device, const void* data, uint32_t byte_count);
+void qp_comms_i2c_stop(painter_device_t device);
+
+extern const painter_comms_with_command_vtable_t i2c_comms_cmddata_vtable;
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#endif // QUANTUM_PAINTER_I2C_ENABLE
diff --git a/drivers/painter/comms/qp_comms_spi.c b/drivers/painter/comms/qp_comms_spi.c
index 7534e844d8..9f52bc7d1f 100644
--- a/drivers/painter/comms/qp_comms_spi.c
+++ b/drivers/painter/comms/qp_comms_spi.c
@@ -105,13 +105,21 @@ void qp_comms_spi_dc_reset_send_command(painter_device_t device, uint8_t cmd) {
}
void qp_comms_spi_dc_reset_bulk_command_sequence(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
+ painter_driver_t * driver = (painter_driver_t *)device;
+ qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
for (size_t i = 0; i < sequence_len;) {
uint8_t command = sequence[i];
uint8_t delay = sequence[i + 1];
uint8_t num_bytes = sequence[i + 2];
qp_comms_spi_dc_reset_send_command(device, command);
if (num_bytes > 0) {
- qp_comms_spi_dc_reset_send_data(device, &sequence[i + 3], num_bytes);
+ if (comms_config->command_params_uses_command_pin) {
+ for (uint8_t j = 0; j < num_bytes; j++) {
+ qp_comms_spi_dc_reset_send_command(device, sequence[i + 3 + j]);
+ }
+ } else {
+ qp_comms_spi_dc_reset_send_data(device, &sequence[i + 3], num_bytes);
+ }
}
if (delay > 0) {
wait_ms(delay);
diff --git a/drivers/painter/comms/qp_comms_spi.h b/drivers/painter/comms/qp_comms_spi.h
index b3da86d573..ff323c3c10 100644
--- a/drivers/painter/comms/qp_comms_spi.h
+++ b/drivers/painter/comms/qp_comms_spi.h
@@ -1,6 +1,5 @@
// Copyright 2021 Nick Brassel (@tzarc)
// SPDX-License-Identifier: GPL-2.0-or-later
-
#pragma once
#ifdef QUANTUM_PAINTER_SPI_ENABLE
@@ -36,6 +35,7 @@ typedef struct qp_comms_spi_dc_reset_config_t {
qp_comms_spi_config_t spi_config;
pin_t dc_pin;
pin_t reset_pin;
+ bool command_params_uses_command_pin; // keep D/C held low when sending command sequences for data bytes
} qp_comms_spi_dc_reset_config_t;
void qp_comms_spi_dc_reset_send_command(painter_device_t device, uint8_t cmd);