gpio_cxx.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "system_cxx.hpp"
  10. namespace idf {
  11. /**
  12. * @brief Exception thrown for errors in the GPIO C++ API.
  13. */
  14. struct GPIOException : public ESPException {
  15. /**
  16. * @param error The IDF error representing the error class of the error to throw.
  17. */
  18. GPIOException(esp_err_t error);
  19. };
  20. /**
  21. * Check if the numeric pin number is valid on the current hardware.
  22. */
  23. esp_err_t check_gpio_pin_num(uint32_t pin_num) noexcept;
  24. /**
  25. * Check if the numeric value of a drive strength is valid on the current hardware.
  26. */
  27. esp_err_t check_gpio_drive_strength(uint32_t strength) noexcept;
  28. /**
  29. * This is a "Strong Value Type" class for GPIO. The GPIO pin number is checked during construction according to
  30. * the hardware capabilities. This means that any GPIONumBase object is guaranteed to contain a valid GPIO number.
  31. * See also the template class \c StrongValue.
  32. */
  33. template<typename GPIONumFinalType>
  34. class GPIONumBase final : public StrongValueComparable<uint32_t> {
  35. public:
  36. /**
  37. * @brief Create a numerical pin number representation and make sure it's correct.
  38. *
  39. * @throw GPIOException if the number does not reflect a valid GPIO number on the current hardware.
  40. */
  41. GPIONumBase(uint32_t pin) : StrongValueComparable<uint32_t>(pin)
  42. {
  43. esp_err_t pin_check_result = check_gpio_pin_num(pin);
  44. if (pin_check_result != ESP_OK) {
  45. throw GPIOException(pin_check_result);
  46. }
  47. }
  48. using StrongValueComparable<uint32_t>::operator==;
  49. using StrongValueComparable<uint32_t>::operator!=;
  50. /**
  51. * Retrieves the valid numerical representation of the GPIO number.
  52. */
  53. uint32_t get_num() const { return get_value(); };
  54. };
  55. /**
  56. * This is a TAG type whose sole purpose is to create a distinct type from GPIONumBase.
  57. */
  58. class GPIONumType;
  59. /**
  60. * A GPIO number type used for general GPIOs, in contrast to specific GPIO pins like e.g. SPI_SCLK.
  61. */
  62. using GPIONum = GPIONumBase<class GPIONumType>;
  63. /**
  64. * Level of an input GPIO.
  65. */
  66. enum class GPIOLevel {
  67. HIGH,
  68. LOW
  69. };
  70. /**
  71. * Represents a valid pull up configuration for GPIOs.
  72. * It is supposed to resemble an enum type, hence it has static creation methods and a private constructor.
  73. * This class is a "Strong Value Type", see also the template class \c StrongValue for more properties.
  74. */
  75. class GPIOPullMode final : public StrongValueComparable<uint32_t> {
  76. private:
  77. /**
  78. * Constructor is private since it should only be accessed by the static creation methods.
  79. *
  80. * @param pull_mode A valid numerical respresentation of the pull up configuration. Must be valid!
  81. */
  82. GPIOPullMode(uint32_t pull_mode) : StrongValueComparable<uint32_t>(pull_mode) { }
  83. public:
  84. /**
  85. * Create a representation of a floating pin configuration.
  86. * For more information, check the driver and HAL files.
  87. */
  88. static GPIOPullMode FLOATING();
  89. /**
  90. * Create a representation of a pullup configuration.
  91. * For more information, check the driver and HAL files.
  92. */
  93. static GPIOPullMode PULLUP();
  94. /**
  95. * Create a representation of a pulldown configuration.
  96. * For more information, check the driver and HAL files.
  97. */
  98. static GPIOPullMode PULLDOWN();
  99. using StrongValueComparable<uint32_t>::operator==;
  100. using StrongValueComparable<uint32_t>::operator!=;
  101. /**
  102. * Retrieves the valid numerical representation of the pull mode.
  103. */
  104. uint32_t get_pull_mode() const { return get_value(); };
  105. };
  106. /**
  107. * @brief Represents a valid wakup interrupt type for GPIO inputs.
  108. *
  109. * This class is a "Strong Value Type", see also the template class \c StrongValue for more properties.
  110. * It is supposed to resemble an enum type, hence it has static creation methods and a private constructor.
  111. * For a detailed mapping of interrupt types to numeric values, please refer to the driver types and implementation.
  112. */
  113. class GPIOWakeupIntrType final: public StrongValueComparable<uint32_t> {
  114. private:
  115. /**
  116. * Constructor is private since it should only be accessed by the static creation methods.
  117. *
  118. * @param pull_mode A valid numerical respresentation of a possible interrupt level to wake up. Must be valid!
  119. */
  120. GPIOWakeupIntrType(uint32_t interrupt_level) : StrongValueComparable<uint32_t>(interrupt_level) { }
  121. public:
  122. static GPIOWakeupIntrType LOW_LEVEL();
  123. static GPIOWakeupIntrType HIGH_LEVEL();
  124. /**
  125. * Retrieves the valid numerical representation of the pull mode.
  126. */
  127. uint32_t get_level() const noexcept { return get_value(); };
  128. };
  129. /**
  130. * Class representing a valid drive strength for GPIO outputs.
  131. * This class is a "Strong Value Type", see also the template class \c StrongValue for more properties.
  132. * For a detailed mapping for values to drive strengths, please refer to the datasheet of the chip you are using.
  133. * E.g. for ESP32, the values in general are the following:
  134. * - WEAK: 5mA
  135. * - STRONGER: 10mA
  136. * - DEFAULT/MEDIUM: 20mA
  137. * - STRONGEST: 40mA
  138. */
  139. class GPIODriveStrength final : public StrongValueComparable<uint32_t> {
  140. public:
  141. /**
  142. * @brief Create a drive strength representation and checks its validity.
  143. *
  144. * After construction, this class should have a guaranteed valid strength representation.
  145. *
  146. * @param strength the numeric value mapping for a particular strength. For possible ranges, look at the
  147. * static creation functions below.
  148. * @throws GPIOException if the supplied number is out of the hardware capable range.
  149. */
  150. GPIODriveStrength(uint32_t strength) : StrongValueComparable<uint32_t>(strength)
  151. {
  152. esp_err_t strength_check_result = check_gpio_drive_strength(strength);
  153. if (strength_check_result != ESP_OK) {
  154. throw GPIOException(strength_check_result);
  155. }
  156. }
  157. /**
  158. * Create a representation of the default drive strength.
  159. * For more information, check the datasheet and driver and HAL files.
  160. */
  161. static GPIODriveStrength DEFAULT();
  162. /**
  163. * Create a representation of the weak drive strength.
  164. * For more information, check the datasheet and driver and HAL files.
  165. */
  166. static GPIODriveStrength WEAK();
  167. /**
  168. * Create a representation of the less weak drive strength.
  169. * For more information, check the datasheet and driver and HAL files.
  170. */
  171. static GPIODriveStrength LESS_WEAK();
  172. /**
  173. * Create a representation of the medium drive strength.
  174. * For more information, check the datasheet and driver and HAL files.
  175. */
  176. static GPIODriveStrength MEDIUM();
  177. /**
  178. * Create a representation of the strong drive strength.
  179. */
  180. static GPIODriveStrength STRONGEST();
  181. using StrongValueComparable<uint32_t>::operator==;
  182. using StrongValueComparable<uint32_t>::operator!=;
  183. /**
  184. * Retrieves the valid numerical representation of the drive strength.
  185. */
  186. uint32_t get_strength() const { return get_value(); };
  187. };
  188. /**
  189. * @brief Implementations commonly used functionality for all GPIO configurations.
  190. *
  191. * Some functionality is only for specific configurations (set and get drive strength) but is necessary here
  192. * to avoid complicating the inheritance hierarchy of the GPIO classes.
  193. * Child classes implementing any GPIO configuration (output, input, etc.) are meant to intherit from this class
  194. * and possibly make some of the functionality publicly available.
  195. */
  196. class GPIOBase {
  197. protected:
  198. /**
  199. * @brief Construct a GPIO.
  200. *
  201. * This constructor will only reset the GPIO but leaves the actual configuration (input, output, etc.) to
  202. * the sub class.
  203. *
  204. * @param num GPIO pin number of the GPIO to be configured.
  205. *
  206. * @throws GPIOException
  207. * - if the underlying driver function fails
  208. */
  209. GPIOBase(GPIONum num);
  210. /**
  211. * @brief Enable gpio pad hold function.
  212. *
  213. * The gpio pad hold function works in both input and output modes, but must be output-capable gpios.
  214. * If pad hold enabled:
  215. * in output mode: the output level of the pad will be force locked and can not be changed.
  216. * in input mode: the input value read will not change, regardless the changes of input signal.
  217. *
  218. * @throws GPIOException if the underlying driver function fails.
  219. */
  220. void hold_en();
  221. /**
  222. * @brief Disable gpio pad hold function.
  223. *
  224. * @throws GPIOException if the underlying driver function fails.
  225. */
  226. void hold_dis();
  227. /**
  228. * @brief Configure the drive strength of the GPIO.
  229. *
  230. * @param strength The drive strength. Refer to \c GPIODriveStrength for more details.
  231. *
  232. * @throws GPIOException if the underlying driver function fails.
  233. */
  234. void set_drive_strength(GPIODriveStrength strength);
  235. /**
  236. * @brief Return the current drive strength of the GPIO.
  237. *
  238. * @return The currently configured drive strength. Refer to \c GPIODriveStrength for more details.
  239. *
  240. * @throws GPIOException if the underlying driver function fails.
  241. */
  242. GPIODriveStrength get_drive_strength();
  243. /**
  244. * @brief The number of the configured GPIO pin.
  245. */
  246. GPIONum gpio_num;
  247. };
  248. /**
  249. * @brief This class represents a GPIO which is configured as output.
  250. */
  251. class GPIO_Output : public GPIOBase {
  252. public:
  253. /**
  254. * @brief Construct and configure a GPIO as output.
  255. *
  256. * @param num GPIO pin number of the GPIO to be configured.
  257. *
  258. * @throws GPIOException
  259. * - if the underlying driver function fails
  260. */
  261. GPIO_Output(GPIONum num);
  262. /**
  263. * @brief Set GPIO to high level.
  264. *
  265. * @throws GPIOException if the underlying driver function fails.
  266. */
  267. void set_high();
  268. /**
  269. * @brief Set GPIO to low level.
  270. *
  271. * @throws GPIOException if the underlying driver function fails.
  272. */
  273. void set_low();
  274. using GPIOBase::set_drive_strength;
  275. using GPIOBase::get_drive_strength;
  276. };
  277. /**
  278. * @brief This class represents a GPIO which is configured as input.
  279. */
  280. class GPIOInput : public GPIOBase {
  281. public:
  282. /**
  283. * @brief Construct and configure a GPIO as input.
  284. *
  285. * @param num GPIO pin number of the GPIO to be configured.
  286. *
  287. * @throws GPIOException
  288. * - if the underlying driver function fails
  289. */
  290. GPIOInput(GPIONum num);
  291. /**
  292. * @brief Read the current level of the GPIO.
  293. *
  294. * @return The GPIO current level of the GPIO.
  295. */
  296. GPIOLevel get_level() const noexcept;
  297. /**
  298. * @brief Configure the internal pull-up and pull-down restors.
  299. *
  300. * @param mode The pull-up/pull-down configuration see \c GPIOPullMode.
  301. *
  302. * @throws GPIOException if the underlying driver function fails.
  303. */
  304. void set_pull_mode(GPIOPullMode mode);
  305. /**
  306. * @brief Configure the pin as wake up pin.
  307. *
  308. * @throws GPIOException if the underlying driver function fails.
  309. */
  310. void wakeup_enable(GPIOWakeupIntrType interrupt_type);
  311. /**
  312. * @brief Disable wake up functionality for this pin if it was enabled before.
  313. *
  314. * @throws GPIOException if the underlying driver function fails.
  315. */
  316. void wakeup_disable();
  317. };
  318. /**
  319. * @brief This class represents a GPIO which is configured as open drain output and input at the same time.
  320. *
  321. * This class facilitates bit-banging for single wire protocols.
  322. */
  323. class GPIO_OpenDrain : public GPIOInput {
  324. public:
  325. /**
  326. * @brief Construct and configure a GPIO as open drain output as well as input.
  327. *
  328. * @param num GPIO pin number of the GPIO to be configured.
  329. *
  330. * @throws GPIOException
  331. * - if the underlying driver function fails
  332. */
  333. GPIO_OpenDrain(GPIONum num);
  334. /**
  335. * @brief Set GPIO to floating level.
  336. *
  337. * @throws GPIOException if the underlying driver function fails.
  338. */
  339. void set_floating();
  340. /**
  341. * @brief Set GPIO to low level.
  342. *
  343. * @throws GPIOException if the underlying driver function fails.
  344. */
  345. void set_low();
  346. using GPIOBase::set_drive_strength;
  347. using GPIOBase::get_drive_strength;
  348. };
  349. }
  350. #endif