default_if_spi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/FreeRTOS.h"
  14. #include <freertos/task.h>
  15. #include "gds.h"
  16. #include "gds_err.h"
  17. #include "gds_private.h"
  18. #include "gds_default_if.h"
  19. static const int GDS_SPI_Command_Mode = 0;
  20. static const int GDS_SPI_Data_Mode = 1;
  21. static spi_host_device_t SPIHost;
  22. static int DCPin;
  23. static bool SPIDefaultWriteBytes( spi_device_handle_t SPIHandle, int WriteMode, const uint8_t* Data, size_t DataLength );
  24. static bool SPIDefaultWriteCommand( struct GDS_Device* Device, uint8_t Command );
  25. static bool SPIDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength );
  26. bool GDS_SPIInit( int SPI, int DC ) {
  27. SPIHost = SPI;
  28. DCPin = DC;
  29. return true;
  30. }
  31. bool GDS_SPIAttachDevice( struct GDS_Device* Device, int Width, int Height, int CSPin, int RSTPin, int Speed ) {
  32. spi_device_interface_config_t SPIDeviceConfig;
  33. spi_device_handle_t SPIDevice;
  34. NullCheck( Device, return false );
  35. if (CSPin >= 0) {
  36. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( CSPin, GPIO_MODE_OUTPUT ), return false );
  37. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( CSPin, 0 ), return false );
  38. }
  39. memset( &SPIDeviceConfig, 0, sizeof( spi_device_interface_config_t ) );
  40. SPIDeviceConfig.clock_speed_hz = Speed > 0 ? Speed : SPI_MASTER_FREQ_8M;
  41. SPIDeviceConfig.spics_io_num = CSPin;
  42. SPIDeviceConfig.queue_size = 1;
  43. ESP_ERROR_CHECK_NONFATAL( spi_bus_add_device( SPIHost, &SPIDeviceConfig, &SPIDevice ), return false );
  44. Device->WriteCommand = SPIDefaultWriteCommand;
  45. Device->WriteData = SPIDefaultWriteData;
  46. Device->SPIHandle = SPIDevice;
  47. Device->RSTPin = RSTPin;
  48. Device->CSPin = CSPin;
  49. Device->IF = GDS_IF_SPI;
  50. Device->Width = Width;
  51. Device->Height = Height;
  52. if ( RSTPin >= 0 ) {
  53. ESP_ERROR_CHECK_NONFATAL( gpio_set_direction( RSTPin, GPIO_MODE_OUTPUT ), return false );
  54. ESP_ERROR_CHECK_NONFATAL( gpio_set_level( RSTPin, 0 ), return false );
  55. GDS_Reset( Device );
  56. }
  57. return GDS_Init( Device );
  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 GDS_Device* Device, uint8_t Command ) {
  73. static uint8_t CommandByte = 0;
  74. NullCheck( Device, return false );
  75. NullCheck( Device->SPIHandle, return false );
  76. CommandByte = Command;
  77. return SPIDefaultWriteBytes( Device->SPIHandle, GDS_SPI_Command_Mode, &CommandByte, 1 );
  78. }
  79. static bool SPIDefaultWriteData( struct GDS_Device* Device, const uint8_t* Data, size_t DataLength ) {
  80. NullCheck( Device, return false );
  81. NullCheck( Device->SPIHandle, return false );
  82. return SPIDefaultWriteBytes( Device->SPIHandle, GDS_SPI_Data_Mode, Data, DataLength );
  83. }