qp_comms_i2c.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return true;
  25. }
  26. uint32_t qp_comms_i2c_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
  27. return qp_comms_i2c_send_raw(device, data, byte_count);
  28. }
  29. bool qp_comms_i2c_stop(painter_device_t device) {
  30. return true;
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. // Command+Data I2C support
  34. static const uint8_t cmd_byte = 0x00;
  35. static const uint8_t data_byte = 0x40;
  36. bool qp_comms_i2c_cmddata_send_command(painter_device_t device, uint8_t cmd) {
  37. uint8_t buf[2] = {cmd_byte, cmd};
  38. return qp_comms_i2c_send_raw(device, &buf, 2);
  39. }
  40. uint32_t qp_comms_i2c_cmddata_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
  41. uint8_t buf[1 + byte_count];
  42. buf[0] = data_byte;
  43. memcpy(&buf[1], data, byte_count);
  44. if (qp_comms_i2c_send_raw(device, buf, sizeof(buf)) != sizeof(buf)) {
  45. return 0;
  46. }
  47. return byte_count;
  48. }
  49. bool qp_comms_i2c_bulk_command_sequence(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
  50. uint8_t buf[32];
  51. for (size_t i = 0; i < sequence_len;) {
  52. uint8_t command = sequence[i];
  53. uint8_t delay = sequence[i + 1];
  54. uint8_t num_bytes = sequence[i + 2];
  55. buf[0] = cmd_byte;
  56. buf[1] = command;
  57. memcpy(&buf[2], &sequence[i + 3], num_bytes);
  58. if (!qp_comms_i2c_send_raw(device, buf, num_bytes + 2)) {
  59. return false;
  60. }
  61. if (delay > 0) {
  62. wait_ms(delay);
  63. }
  64. i += (3 + num_bytes);
  65. }
  66. return true;
  67. }
  68. const painter_comms_with_command_vtable_t i2c_comms_cmddata_vtable = {
  69. .base =
  70. {
  71. .comms_init = qp_comms_i2c_init,
  72. .comms_start = qp_comms_i2c_start,
  73. .comms_send = qp_comms_i2c_cmddata_send_data,
  74. .comms_stop = qp_comms_i2c_stop,
  75. },
  76. .send_command = qp_comms_i2c_cmddata_send_command,
  77. .bulk_command_sequence = qp_comms_i2c_bulk_command_sequence,
  78. };
  79. #endif // QUANTUM_PAINTER_I2C_ENABLE