default_if_spi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "gds.h"
  15. #include "gds_err.h"
  16. #include "gds_private.h"
  17. #include "gds_default_if.h"
  18. static const int GDS_SPI_Command_Mode = 0;
  19. static const int GDS_SPI_Data_Mode = 1;
  20. static spi_host_device_t SPIHost;
  21. static int DCPin;
  22. static bool SPIDefaultWriteBytes( spi_device_handle_t SPIHandle, int WriteMode, const uint8_t* Data, size_t DataLength );
  23. static bool SPIDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command );
  24. static bool SPIDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength );
  25. bool GDS_SPIInit( int SPI, int DC ) {
  26. SPIHost = SPI;
  27. DCPin = DC;
  28. return true;
  29. }
  30. bool GDS_SPIAttachDevice( struct GDS_Device* Device, int Width, int Height, int CSPin, int RSTPin, int BackLightPin, int Speed ) {
  31. spi_device_interface_config_t SPIDeviceConfig;
  32. spi_device_handle_t SPIDevice;
  33. NullCheck( Device, return false );
  34. if (CSPin >= 0) {
  35. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( CSPin, GPIO_MODE_OUTPUT ), return false );
  36. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( CSPin, 0 ), return false );
  37. }
  38. memset( &SPIDeviceConfig, 0, sizeof( spi_device_interface_config_t ) );
  39. SPIDeviceConfig.clock_speed_hz = Speed > 0 ? Speed : SPI_MASTER_FREQ_8M;
  40. SPIDeviceConfig.spics_io_num = CSPin;
  41. SPIDeviceConfig.queue_size = 1;
  42. SPIDeviceConfig.flags = SPI_DEVICE_NO_DUMMY;
  43. SPIDeviceConfig.cs_ena_pretrans = Device->CS_pre;
  44. SPIDeviceConfig.cs_ena_posttrans = Device->CS_post;
  45. SPIDeviceConfig.mode = Device->SPI_mode;
  46. ESP_ERROR_CHECK_NONFATAL( spi_bus_add_device( SPIHost, &SPIDeviceConfig, &SPIDevice ), return false );
  47. Device->WriteCommand = SPIDefaultWriteCommand;
  48. Device->WriteData = SPIDefaultWriteData;
  49. Device->SPIHandle = SPIDevice;
  50. Device->RSTPin = RSTPin;
  51. Device->CSPin = CSPin;
  52. Device->Backlight.Pin = BackLightPin;
  53. Device->IF = GDS_IF_SPI;
  54. Device->Width = Width;
  55. Device->Height = Height;
  56. if ( RSTPin >= 0 ) {
  57. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  58. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 0 ), return false );
  59. GDS_Reset( Device );
  60. }
  61. return GDS_Init( Device );
  62. }
  63. static bool SPIDefaultWriteBytes( spi_device_handle_t SPIHandle, int WriteMode, const uint8_t* Data, size_t DataLength ) {
  64. spi_transaction_t SPITransaction = { };
  65. NullCheck( SPIHandle, return false );
  66. NullCheck( Data, return false );
  67. if ( DataLength > 0 ) {
  68. gpio_set_level( DCPin, WriteMode );
  69. SPITransaction.length = DataLength * 8;
  70. if (DataLength <= 4) {
  71. SPITransaction.flags = SPI_TRANS_USE_TXDATA;
  72. SPITransaction.tx_data[0] = *Data++; SPITransaction.tx_data[1] = *Data++;
  73. SPITransaction.tx_data[2] = *Data++; SPITransaction.tx_data[3] = *Data;
  74. } else {
  75. SPITransaction.tx_buffer = Data;
  76. }
  77. // only do polling as we don't have contention on SPI (otherwise DMA for transfers > 16 bytes)
  78. ESP_ERROR_CHECK_NONFATAL( spi_device_polling_transmit(SPIHandle, &SPITransaction), return false );
  79. }
  80. return true;
  81. }
  82. static bool SPIDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command ) {
  83. static uint8_t CommandByte = 0;
  84. NullCheck( Device, return false );
  85. NullCheck( Device->SPIHandle, return false );
  86. CommandByte = Command;
  87. return SPIDefaultWriteBytes( Device->SPIHandle, GDS_SPI_Command_Mode, &CommandByte, 1 );
  88. }
  89. static bool SPIDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength ) {
  90. NullCheck( Device, return false );
  91. NullCheck( Device->SPIHandle, return false );
  92. return SPIDefaultWriteBytes( Device->SPIHandle, GDS_SPI_Data_Mode, Data, DataLength );
  93. }