logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qp_comms_i2c.c (3067B)


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