default_if_i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <string.h>
  11. #include <driver/i2c.h>
  12. #include <driver/gpio.h>
  13. #include "gds.h"
  14. #include "gds_err.h"
  15. #include "gds_private.h"
  16. #include "gds_default_if.h"
  17. static int I2CPortNumber;
  18. static int I2CWait;
  19. static const int GDS_I2C_COMMAND_MODE = 0x80;
  20. static const int GDS_I2C_DATA_MODE = 0x40;
  21. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength );
  22. static bool I2CDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command );
  23. static bool I2CDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength );
  24. /*
  25. * Initializes the i2c master with the parameters specified
  26. * in the component configuration in sdkconfig.h.
  27. *
  28. * Returns true on successful init of the i2c bus.
  29. */
  30. bool GDS_I2CInit( int PortNumber, int SDA, int SCL, int Speed ) {
  31. I2CPortNumber = PortNumber;
  32. I2CWait = pdMS_TO_TICKS( Speed ? (250 * 250000) / Speed : 250 );
  33. if (SDA != -1 && SCL != -1) {
  34. i2c_config_t Config = { 0 };
  35. Config.mode = I2C_MODE_MASTER;
  36. Config.sda_io_num = SDA;
  37. Config.sda_pullup_en = GPIO_PULLUP_ENABLE;
  38. Config.scl_io_num = SCL;
  39. Config.scl_pullup_en = GPIO_PULLUP_ENABLE;
  40. Config.master.clk_speed = Speed ? Speed : 400000;
  41. ESP_ERROR_CHECK_NONFATAL( i2c_param_config( I2CPortNumber, &Config ), return false );
  42. ESP_ERROR_CHECK_NONFATAL( i2c_driver_install( I2CPortNumber, Config.mode, 0, 0, 0 ), return false );
  43. }
  44. return true;
  45. }
  46. /*
  47. * Attaches a display to the I2C bus using default communication functions.
  48. *
  49. * Params:
  50. * Device: Pointer to your GDS_Device object
  51. * Width: Width of display
  52. * Height: Height of display
  53. * I2CAddress: Address of your display
  54. * RSTPin: Optional GPIO pin to use for hardware reset, if none pass -1 for this parameter.
  55. *
  56. * Returns true on successful init of display.
  57. */
  58. bool GDS_I2CAttachDevice( struct GDS_Device* Device, int Width, int Height, int I2CAddress, int RSTPin, int BacklightPin ) {
  59. NullCheck( Device, return false );
  60. Device->WriteCommand = I2CDefaultWriteCommand;
  61. Device->WriteData = I2CDefaultWriteData;
  62. Device->Address = I2CAddress;
  63. Device->RSTPin = RSTPin;
  64. Device->Backlight.Pin = BacklightPin;
  65. Device->IF = GDS_IF_I2C;
  66. Device->Width = Width;
  67. Device->Height = Height;
  68. if ( RSTPin >= 0 ) {
  69. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  70. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 1 ), return false );
  71. GDS_Reset( Device );
  72. }
  73. return GDS_Init( Device );
  74. }
  75. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength ) {
  76. i2c_cmd_handle_t* CommandHandle = NULL;
  77. static uint8_t ModeByte = 0;
  78. NullCheck( Data, return false );
  79. if ( ( CommandHandle = i2c_cmd_link_create( ) ) != NULL ) {
  80. ModeByte = ( IsCommand == true ) ? GDS_I2C_COMMAND_MODE: GDS_I2C_DATA_MODE;
  81. ESP_ERROR_CHECK_NONFATAL( i2c_master_start( CommandHandle ), goto error );
  82. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ( Address << 1 ) | I2C_MASTER_WRITE, true ), goto error );
  83. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ModeByte, true ), goto error );
  84. ESP_ERROR_CHECK_NONFATAL( i2c_master_write( CommandHandle, ( uint8_t* ) Data, DataLength, true ), goto error );
  85. ESP_ERROR_CHECK_NONFATAL( i2c_master_stop( CommandHandle ), goto error );
  86. ESP_ERROR_CHECK_NONFATAL( i2c_master_cmd_begin( I2CPortNumber, CommandHandle, I2CWait ), goto error );
  87. i2c_cmd_link_delete( CommandHandle );
  88. }
  89. return true;
  90. error:
  91. if (CommandHandle) i2c_cmd_link_delete( CommandHandle );
  92. return false;
  93. }
  94. static bool I2CDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command ) {
  95. uint8_t CommandByte = ( uint8_t ) Command;
  96. NullCheck( Device, return false );
  97. return I2CDefaultWriteBytes( Device->Address, true, ( const uint8_t* ) &CommandByte, 1 );
  98. }
  99. static bool I2CDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength ) {
  100. NullCheck( Device, return false );
  101. NullCheck( Data, return false );
  102. return I2CDefaultWriteBytes( Device->Address, false, Data, DataLength );
  103. }