default_if_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "ssd13x6.h"
  14. #include "ssd13x6_default_if.h"
  15. static int I2CPortNumber;
  16. static const int SSD13x6_I2C_COMMAND_MODE = 0x80;
  17. static const int SSD13x6_I2C_DATA_MODE = 0x40;
  18. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength );
  19. static bool I2CDefaultWriteCommand( struct SSD13x6_Device* Display, SSDCmd Command );
  20. static bool I2CDefaultWriteData( struct SSD13x6_Device* Display, const uint8_t* Data, size_t DataLength );
  21. static bool I2CDefaultReset( struct SSD13x6_Device* Display );
  22. /*
  23. * Initializes the i2c master with the parameters specified
  24. * in the component configuration in sdkconfig.h.
  25. *
  26. * Returns true on successful init of the i2c bus.
  27. */
  28. bool SSD13x6_I2CMasterInitDefault( int PortNumber, int SDA, int SCL ) {
  29. I2CPortNumber = PortNumber;
  30. if (SDA != -1 && SCL != -1) {
  31. i2c_config_t Config = { 0 };
  32. Config.mode = I2C_MODE_MASTER;
  33. Config.sda_io_num = SDA;
  34. Config.sda_pullup_en = GPIO_PULLUP_ENABLE;
  35. Config.scl_io_num = SCL;
  36. Config.scl_pullup_en = GPIO_PULLUP_ENABLE;
  37. Config.master.clk_speed = 250000;
  38. ESP_ERROR_CHECK_NONFATAL( i2c_param_config( I2CPortNumber, &Config ), return false );
  39. ESP_ERROR_CHECK_NONFATAL( i2c_driver_install( I2CPortNumber, Config.mode, 0, 0, 0 ), return false );
  40. }
  41. return true;
  42. }
  43. /*
  44. * Attaches a display to the I2C bus using default communication functions.
  45. *
  46. * Params:
  47. * DisplayHandle: Pointer to your SSD13x6_Device object
  48. * Width: Width of display
  49. * Height: Height of display
  50. * I2CAddress: Address of your display
  51. * RSTPin: Optional GPIO pin to use for hardware reset, if none pass -1 for this parameter.
  52. *
  53. * Returns true on successful init of display.
  54. */
  55. bool SSD13x6_I2CMasterAttachDisplayDefault( struct SSD13x6_Device* DeviceHandle, int Model, int Width, int Height, int I2CAddress, int RSTPin ) {
  56. NullCheck( DeviceHandle, return false );
  57. if ( RSTPin >= 0 ) {
  58. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  59. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 1 ), return false );
  60. }
  61. memset( DeviceHandle, 0, sizeof( struct SSD13x6_Device ) );
  62. DeviceHandle->Model = Model;
  63. return SSD13x6_Init_I2C( DeviceHandle,
  64. Width,
  65. Height,
  66. I2CAddress,
  67. RSTPin,
  68. I2CDefaultWriteCommand,
  69. I2CDefaultWriteData,
  70. I2CDefaultReset
  71. );
  72. }
  73. static bool I2CDefaultWriteBytes( int Address, bool IsCommand, const uint8_t* Data, size_t DataLength ) {
  74. i2c_cmd_handle_t* CommandHandle = NULL;
  75. static uint8_t ModeByte = 0;
  76. NullCheck( Data, return false );
  77. if ( ( CommandHandle = i2c_cmd_link_create( ) ) != NULL ) {
  78. ModeByte = ( IsCommand == true ) ? SSD13x6_I2C_COMMAND_MODE: SSD13x6_I2C_DATA_MODE;
  79. ESP_ERROR_CHECK_NONFATAL( i2c_master_start( CommandHandle ), goto error );
  80. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ( Address << 1 ) | I2C_MASTER_WRITE, true ), goto error );
  81. ESP_ERROR_CHECK_NONFATAL( i2c_master_write_byte( CommandHandle, ModeByte, true ), goto error );
  82. ESP_ERROR_CHECK_NONFATAL( i2c_master_write( CommandHandle, ( uint8_t* ) Data, DataLength, true ), goto error );
  83. ESP_ERROR_CHECK_NONFATAL( i2c_master_stop( CommandHandle ), goto error );
  84. ESP_ERROR_CHECK_NONFATAL( i2c_master_cmd_begin( I2CPortNumber, CommandHandle, pdMS_TO_TICKS( 1000 ) ), goto error );
  85. i2c_cmd_link_delete( CommandHandle );
  86. }
  87. return true;
  88. error:
  89. if (CommandHandle) i2c_cmd_link_delete( CommandHandle );
  90. return false;
  91. }
  92. static bool I2CDefaultWriteCommand( struct SSD13x6_Device* Display, SSDCmd Command ) {
  93. uint8_t CommandByte = ( uint8_t ) Command;
  94. NullCheck( Display, return false );
  95. return I2CDefaultWriteBytes( Display->Address, true, ( const uint8_t* ) &CommandByte, 1 );
  96. }
  97. static bool I2CDefaultWriteData( struct SSD13x6_Device* Display, const uint8_t* Data, size_t DataLength ) {
  98. NullCheck( Display, return false );
  99. NullCheck( Data, return false );
  100. return I2CDefaultWriteBytes( Display->Address, false, Data, DataLength );
  101. }
  102. static bool I2CDefaultReset( struct SSD13x6_Device* Display ) {
  103. NullCheck( Display, return false );
  104. if ( Display->RSTPin >= 0 ) {
  105. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( Display->RSTPin, 0 ), return true );
  106. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  107. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( Display->RSTPin, 1 ), return true );
  108. }
  109. return true;
  110. }