spi_cxx.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #if __cpp_exceptions
  8. #include "esp_exception.hpp"
  9. #include "gpio_cxx.hpp"
  10. #include "system_cxx.hpp"
  11. namespace idf {
  12. /**
  13. * @brief Exception which is thrown in the context of SPI C++ classes.
  14. */
  15. struct SPIException : public ESPException {
  16. SPIException(esp_err_t error);
  17. };
  18. /**
  19. * @brief The maximum SPI transfer size in bytes.
  20. */
  21. class SPITransferSize : public StrongValueOrdered<size_t> {
  22. public:
  23. /**
  24. * @brief Create a valid SPI transfer size.
  25. *
  26. * @param transfer_size The raw transfer size in bytes.
  27. */
  28. explicit SPITransferSize(size_t transfer_size) noexcept : StrongValueOrdered<size_t>(transfer_size) { }
  29. static SPITransferSize default_size() {
  30. return SPITransferSize(0);
  31. }
  32. };
  33. /**
  34. * @brief Check if the raw uint32_t spi number is in the range according to the hardware.
  35. */
  36. esp_err_t check_spi_num(uint32_t spi_num) noexcept;
  37. /**
  38. * @brief Represents a valid SPI host number.
  39. *
  40. * ESP chips may have different independent SPI peripherals. This SPI number distinguishes between them.
  41. */
  42. class SPINum : public StrongValueComparable<uint32_t> {
  43. public:
  44. /**
  45. * @brief Create a valid SPI host number.
  46. *
  47. * @param host_id_raw The raw SPI host number.
  48. *
  49. * @throw SPIException if the passed SPI host number is incorrect.
  50. */
  51. explicit SPINum(uint32_t host_id_raw) : StrongValueComparable<uint32_t>(host_id_raw)
  52. {
  53. esp_err_t spi_num_check_result = check_spi_num(host_id_raw);
  54. if (spi_num_check_result != ESP_OK) {
  55. throw SPIException(spi_num_check_result);
  56. }
  57. }
  58. /**
  59. * @brief Return the raw value of the SPI host.
  60. *
  61. * This should only be used when calling driver and other interfaces which don't support the C++ class.
  62. *
  63. * @return the raw value of the SPI host.
  64. */
  65. uint32_t get_spi_num() const
  66. {
  67. return get_value();
  68. }
  69. };
  70. /**
  71. * @brief Represents a valid MOSI signal pin number.
  72. */
  73. class MOSI_type;
  74. using MOSI = GPIONumBase<class MOSI_type>;
  75. /**
  76. * @brief Represents a valid MISO signal pin number.
  77. */
  78. class MISO_type;
  79. using MISO = GPIONumBase<class MISO_type>;
  80. /**
  81. * @brief Represents a valid SCLK signal pin number.
  82. */
  83. class SCLK_type;
  84. using SCLK = GPIONumBase<class SCLK_type>;
  85. /**
  86. * @brief Represents a valid CS (chip select) signal pin number.
  87. */
  88. class CS_type;
  89. using CS = GPIONumBase<class CS_type>;
  90. /**
  91. * @brief Represents a valid QSPIWP signal pin number.
  92. */
  93. class QSPIWP_type;
  94. using QSPIWP = GPIONumBase<class QSPIWP_type>;
  95. /**
  96. * @brief Represents a valid QSPIHD signal pin number.
  97. */
  98. class QSPIHD_type;
  99. using QSPIHD = GPIONumBase<class QSPIHD_type>;
  100. /**
  101. * @brief Represents a valid SPI DMA configuration. Use it similar to an enum.
  102. */
  103. class SPI_DMAConfig : public StrongValueComparable<uint32_t> {
  104. /**
  105. * Constructor is hidden to enforce object invariants.
  106. * Use the static creation methods to create instances.
  107. */
  108. explicit SPI_DMAConfig(uint32_t channel_num) : StrongValueComparable<uint32_t>(channel_num) { }
  109. public:
  110. /**
  111. * @brief Create a configuration with DMA disabled.
  112. */
  113. static SPI_DMAConfig DISABLED();
  114. /**
  115. * @brief Create a configuration where the driver allocates DMA.
  116. */
  117. static SPI_DMAConfig AUTO();
  118. /**
  119. * @brief Return the raw value of the DMA configuration.
  120. *
  121. * This should only be used when calling driver and other interfaces which don't support the C++ class.
  122. *
  123. * @return the raw value of the DMA configuration.
  124. */
  125. uint32_t get_num() const {
  126. return get_value();
  127. }
  128. };
  129. }
  130. #endif