default_if_i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 const int i2c_timeout_value=2000;
  22. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength );
  23. static bool I2CDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command );
  24. static bool I2CDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength );
  25. /*
  26. * Initializes the i2c master with the parameters specified
  27. * in the component configuration in sdkconfig.h.
  28. *
  29. * Returns true on successful init of the i2c bus.
  30. */
  31. bool GDS_I2CInit( int PortNumber, int SDA, int SCL, int Speed ) {
  32. I2CPortNumber = PortNumber;
  33. I2CWait = pdMS_TO_TICKS( Speed ? Speed / 4000 : 100 );
  34. if (SDA != -1 && SCL != -1) {
  35. i2c_config_t Config = { 0 };
  36. Config.mode = I2C_MODE_MASTER;
  37. Config.sda_io_num = SDA;
  38. Config.sda_pullup_en = GPIO_PULLUP_ENABLE;
  39. Config.scl_io_num = SCL;
  40. Config.scl_pullup_en = GPIO_PULLUP_ENABLE;
  41. Config.master.clk_speed = Speed ? Speed : 400000;
  42. ESP_ERROR_CHECK_NONFATAL( i2c_param_config( I2CPortNumber, &Config ), return false );
  43. ESP_ERROR_CHECK_NONFATAL( i2c_driver_install( I2CPortNumber, Config.mode, 0, 0, 0 ), return false );
  44. printf("Setting timeout value to %d",i2c_timeout_value);
  45. i2c_set_timeout(I2CPortNumber, (I2C_APB_CLK_FREQ /(2500000))* i2c_timeout_value);
  46. }
  47. return true;
  48. }
  49. /*
  50. * Attaches a display to the I2C bus using default communication functions.
  51. *
  52. * Params:
  53. * Device: Pointer to your GDS_Device object
  54. * Width: Width of display
  55. * Height: Height of display
  56. * I2CAddress: Address of your display
  57. * RSTPin: Optional GPIO pin to use for hardware reset, if none pass -1 for this parameter.
  58. *
  59. * Returns true on successful init of display.
  60. */
  61. bool GDS_I2CAttachDevice( struct GDS_Device* Device, int Width, int Height, int I2CAddress, int RSTPin ) {
  62. NullCheck( Device, return false );
  63. Device->WriteCommand = I2CDefaultWriteCommand;
  64. Device->WriteData = I2CDefaultWriteData;
  65. Device->Address = I2CAddress;
  66. Device->RSTPin = RSTPin;
  67. Device->IF = GDS_IF_I2C;
  68. Device->Width = Width;
  69. Device->Height = Height;
  70. if ( RSTPin >= 0 ) {
  71. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  72. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 1 ), return false );
  73. GDS_Reset( Device );
  74. }
  75. return GDS_Init( Device );
  76. }
  77. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength ) {
  78. i2c_cmd_handle_t* CommandHandle = NULL;
  79. static uint8_t ModeByte = 0;
  80. NullCheck( Data, return false );
  81. if ( ( CommandHandle = i2c_cmd_link_create( ) ) != NULL ) {
  82. ModeByte = ( IsCommand == true ) ? GDS_I2C_COMMAND_MODE: GDS_I2C_DATA_MODE;
  83. ESP_ERROR_CHECK_NONFATAL( i2c_master_start( CommandHandle ), goto error );
  84. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ( Address << 1 ) | I2C_MASTER_WRITE, true ), goto error );
  85. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ModeByte, true ), goto error );
  86. ESP_ERROR_CHECK_NONFATAL( i2c_master_write( CommandHandle, ( uint8_t* ) Data, DataLength, true ), goto error );
  87. ESP_ERROR_CHECK_NONFATAL( i2c_master_stop( CommandHandle ), goto error );
  88. ESP_ERROR_CHECK_NONFATAL( i2c_master_cmd_begin( I2CPortNumber, CommandHandle, I2CWait ), goto error );
  89. i2c_cmd_link_delete( CommandHandle );
  90. }
  91. return true;
  92. error:
  93. if (CommandHandle) i2c_cmd_link_delete( CommandHandle );
  94. return false;
  95. }
  96. static bool I2CDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command ) {
  97. uint8_t CommandByte = ( uint8_t ) Command;
  98. NullCheck( Device, return false );
  99. return I2CDefaultWriteBytes( Device->Address, true, ( const uint8_t* ) &CommandByte, 1 );
  100. }
  101. static bool I2CDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength ) {
  102. NullCheck( Device, return false );
  103. NullCheck( Data, return false );
  104. return I2CDefaultWriteBytes( Device->Address, false, Data, DataLength );
  105. }