2
0

default_if_spi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/spi_master.h>
  12. #include <driver/gpio.h>
  13. #include <freertos/task.h>
  14. #include "ssd13x6.h"
  15. #include "ssd13x6_default_if.h"
  16. static const int SSD13x6_SPI_Command_Mode = 0;
  17. static const int SSD13x6_SPI_Data_Mode = 1;
  18. static spi_host_device_t SPIHost;
  19. static int DCPin;
  20. static bool SPIDefaultWriteBytes( spi_device_handle_t SPIHandle, int WriteMode, const uint8_t* Data, size_t DataLength );
  21. static bool SPIDefaultWriteCommand( struct SSD13x6_Device* DeviceHandle, SSDCmd Command );
  22. static bool SPIDefaultWriteData( struct SSD13x6_Device* DeviceHandle, const uint8_t* Data, size_t DataLength );
  23. static bool SPIDefaultReset( struct SSD13x6_Device* DeviceHandle );
  24. bool SSD13x6_SPIMasterInitDefault( int SPI, int DC ) {
  25. SPIHost = SPI;
  26. DCPin = DC;
  27. return true;
  28. }
  29. bool SSD13x6_SPIMasterAttachDisplayDefault( struct SSD13x6_Device* DeviceHandle, int Model, int Width, int Height, int CSPin, int RSTPin, int Speed ) {
  30. spi_device_interface_config_t SPIDeviceConfig;
  31. spi_device_handle_t SPIDeviceHandle;
  32. NullCheck( DeviceHandle, return false );
  33. if (CSPin >= 0) {
  34. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( CSPin, GPIO_MODE_OUTPUT ), return false );
  35. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( CSPin, 0 ), return false );
  36. }
  37. memset( &SPIDeviceConfig, 0, sizeof( spi_device_interface_config_t ) );
  38. SPIDeviceConfig.clock_speed_hz = Speed > 0 ? Speed : SPI_MASTER_FREQ_8M;
  39. SPIDeviceConfig.spics_io_num = CSPin;
  40. SPIDeviceConfig.queue_size = 1;
  41. if ( RSTPin >= 0 ) {
  42. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  43. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 0 ), return false );
  44. }
  45. ESP_ERROR_CHECK_NONFATAL( spi_bus_add_device( SPIHost, &SPIDeviceConfig, &SPIDeviceHandle ), return false );
  46. memset( DeviceHandle, 0, sizeof( struct SSD13x6_Device ) );
  47. DeviceHandle->Model = Model;
  48. return SSD13x6_Init_SPI( DeviceHandle,
  49. Width,
  50. Height,
  51. RSTPin,
  52. CSPin,
  53. SPIDeviceHandle,
  54. SPIDefaultWriteCommand,
  55. SPIDefaultWriteData,
  56. SPIDefaultReset
  57. );
  58. }
  59. static bool SPIDefaultWriteBytes( spi_device_handle_t SPIHandle, int WriteMode, const uint8_t* Data, size_t DataLength ) {
  60. spi_transaction_t SPITransaction = { 0 };
  61. NullCheck( SPIHandle, return false );
  62. NullCheck( Data, return false );
  63. if ( DataLength > 0 ) {
  64. gpio_set_level( DCPin, WriteMode );
  65. SPITransaction.length = DataLength * 8;
  66. SPITransaction.tx_buffer = Data;
  67. // only do polling as we don't have contention on SPI (otherwise DMA for transfers > 16 bytes)
  68. ESP_ERROR_CHECK_NONFATAL( spi_device_polling_transmit(SPIHandle, &SPITransaction), return false );
  69. }
  70. return true;
  71. }
  72. static bool SPIDefaultWriteCommand( struct SSD13x6_Device* DeviceHandle, SSDCmd Command ) {
  73. static uint8_t CommandByte = 0;
  74. NullCheck( DeviceHandle, return false );
  75. NullCheck( DeviceHandle->SPIHandle, return false );
  76. CommandByte = Command;
  77. return SPIDefaultWriteBytes( DeviceHandle->SPIHandle, SSD13x6_SPI_Command_Mode, &CommandByte, 1 );
  78. }
  79. static bool SPIDefaultWriteData( struct SSD13x6_Device* DeviceHandle, const uint8_t* Data, size_t DataLength ) {
  80. NullCheck( DeviceHandle, return false );
  81. NullCheck( DeviceHandle->SPIHandle, return false );
  82. return SPIDefaultWriteBytes( DeviceHandle->SPIHandle, SSD13x6_SPI_Data_Mode, Data, DataLength );
  83. }
  84. static bool SPIDefaultReset( struct SSD13x6_Device* DeviceHandle ) {
  85. NullCheck( DeviceHandle, return false );
  86. if ( DeviceHandle->RSTPin >= 0 ) {
  87. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( DeviceHandle->RSTPin, 0 ), return false );
  88. vTaskDelay( pdMS_TO_TICKS( 100 ) );
  89. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( DeviceHandle->RSTPin, 1 ), return false );
  90. }
  91. return true;
  92. }