qp_comms_i2c.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifdef QUANTUM_PAINTER_I2C_ENABLE
  4. # include "i2c_master.h"
  5. # include "qp_comms_i2c.h"
  6. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. // Helpers
  8. static uint32_t qp_comms_i2c_send_raw(painter_device_t device, const void *data, uint32_t byte_count) {
  9. painter_driver_t * driver = (painter_driver_t *)device;
  10. qp_comms_i2c_config_t *comms_config = (qp_comms_i2c_config_t *)driver->comms_config;
  11. i2c_status_t res = i2c_transmit(comms_config->chip_address << 1, data, byte_count, I2C_TIMEOUT);
  12. if (res < 0) {
  13. return 0;
  14. }
  15. return byte_count;
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Base I2C support
  19. bool qp_comms_i2c_init(painter_device_t device) {
  20. i2c_init();
  21. return true;
  22. }
  23. bool qp_comms_i2c_start(painter_device_t device) {
  24. painter_driver_t * driver = (painter_driver_t *)device;
  25. qp_comms_i2c_config_t *comms_config = (qp_comms_i2c_config_t *)driver->comms_config;
  26. return i2c_start(comms_config->chip_address << 1) == I2C_STATUS_SUCCESS;
  27. }
  28. uint32_t qp_comms_i2c_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
  29. return qp_comms_i2c_send_raw(device, data, byte_count);
  30. }
  31. void qp_comms_i2c_stop(painter_device_t device) {
  32. i2c_stop();
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. // Command+Data I2C support
  36. static const uint8_t cmd_byte = 0x00;
  37. static const uint8_t data_byte = 0x40;
  38. void qp_comms_i2c_cmddata_send_command(painter_device_t device, uint8_t cmd) {
  39. uint8_t buf[2] = {cmd_byte, cmd};
  40. qp_comms_i2c_send_raw(device, &buf, 2);
  41. }
  42. uint32_t qp_comms_i2c_cmddata_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
  43. uint8_t buf[1 + byte_count];
  44. buf[0] = data_byte;
  45. memcpy(&buf[1], data, byte_count);
  46. if (qp_comms_i2c_send_raw(device, buf, sizeof(buf)) != sizeof(buf)) {
  47. return 0;
  48. }
  49. return byte_count;
  50. }
  51. void qp_comms_i2c_bulk_command_sequence(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
  52. uint8_t buf[32];
  53. for (size_t i = 0; i < sequence_len;) {
  54. uint8_t command = sequence[i];
  55. uint8_t delay = sequence[i + 1];
  56. uint8_t num_bytes = sequence[i + 2];
  57. buf[0] = cmd_byte;
  58. buf[1] = command;
  59. memcpy(&buf[2], &sequence[i + 3], num_bytes);
  60. qp_comms_i2c_send_raw(device, buf, num_bytes + 2);
  61. if (delay > 0) {
  62. wait_ms(delay);
  63. }
  64. i += (3 + num_bytes);
  65. }
  66. }
  67. const painter_comms_with_command_vtable_t i2c_comms_cmddata_vtable = {
  68. .base =
  69. {
  70. .comms_init = qp_comms_i2c_init,
  71. .comms_start = qp_comms_i2c_start,
  72. .comms_send = qp_comms_i2c_cmddata_send_data,
  73. .comms_stop = qp_comms_i2c_stop,
  74. },
  75. .send_command = qp_comms_i2c_cmddata_send_command,
  76. .bulk_command_sequence = qp_comms_i2c_bulk_command_sequence,
  77. };
  78. #endif // QUANTUM_PAINTER_I2C_ENABLE