gpio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * \file
  3. *
  4. * \brief SAM GPIO Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef GPIO_H_INCLUDED
  47. #define GPIO_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_samb_gpio_group SAM GPIO Driver (GPIO)
  50. *
  51. * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
  52. * configuration and management of the device's General Purpose Input/Output
  53. * (GPIO) pin functionality, for manual pin state reading and writing.
  54. *
  55. * The following peripherals are used by this module:
  56. * - GPIO (GPIO Management)
  57. *
  58. * The following devices can use this module:
  59. * - Atmel | SMART SAM B11
  60. *
  61. * The outline of this documentation is as follows:
  62. * - \ref asfdoc_samb_gpio_prerequisites
  63. * - \ref asfdoc_samb_gpio_module_overview
  64. * - \ref asfdoc_samb_gpio_special_considerations
  65. * - \ref asfdoc_samb_gpio_extra_info
  66. * - \ref asfdoc_samb_gpio_examples
  67. * - \ref asfdoc_samb_gpio_api_overview
  68. *
  69. *
  70. * \section asfdoc_samb_gpio_prerequisites Prerequisites
  71. *
  72. * There are no prerequisites for this module.
  73. *
  74. *
  75. * \section asfdoc_samb_gpio_module_overview Module Overview
  76. *
  77. * The device GPIO module provides an interface between the user application
  78. * logic and external hardware peripherals, when general pin state manipulation
  79. * is required. This driver provides an easy-to-use interface to the physical
  80. * pin input samplers and output drivers, so that pins can be read from or
  81. * written to for general purpose external hardware control.
  82. *
  83. * There are the different peripheral functions that are Software selectable
  84. * on a per pin basis. This allows for maximum flexibility of mapping desired
  85. * interfaces on GPIO pins. MUX1 option allows for any MEGAMUX option to be
  86. * assigned to a GPIO.
  87. *
  88. * An example is to illustrate the available options for pin LP_GPIO_3, depending
  89. * on the pin-MUX option selected:
  90. * - MUX0: the pin will function as bit 3 of the GPIO bus and is controlled by
  91. * the GPIO controller in the ARM subsystem
  92. * - MUX1: any option from the MEGAMUX table can be selected, for example it
  93. * can be a quad_dec, pwm, or any of the other functions listed in the
  94. * MEGAMUX table
  95. * - MUX2: the pin will function as UART1 TXD; this can be also achieved with
  96. * the MUX1 option via MEGAMUX, but the MUX2 option allows a shortcut
  97. * for the recommended pinout
  98. * - MUX3: this option is not used and thus defaults to the GPIO option (same
  99. * as MUX0)
  100. * - MUX4: the pin will function as SPI1 MOSI (this option is not available
  101. * through MEGAMUX)
  102. * - MUX5: the pin will function as SPI0 MOSI (this option is not available
  103. * through MEGAMUX)
  104. * - MUX6: the pin will function as SPI FLASH SCK (this option is not available
  105. * through MEGAMUX)
  106. * - MUX7: the pin will function as bit 3 of the test output bus, giving access
  107. * to various debug signals
  108. *
  109. * \section asfdoc_samb_gpio_special_considerations Special Considerations
  110. *
  111. * There are no special considerations for this module.
  112. *
  113. * \section asfdoc_samb_gpio_extra_info Extra Information
  114. *
  115. * For extra information, see \ref asfdoc_samb_gpio_extra. This includes:
  116. * - \ref asfdoc_samb_gpio_extra_acronyms
  117. * - \ref asfdoc_samb_gpio_extra_dependencies
  118. * - \ref asfdoc_samb_gpio_extra_errata
  119. * - \ref asfdoc_samb_gpio_extra_history
  120. *
  121. *
  122. * \section asfdoc_samb_gpio_examples Examples
  123. *
  124. * For a list of examples related to this driver, see
  125. * \ref asfdoc_samb_gpio_exqsg.
  126. *
  127. *
  128. * \section asfdoc_samb_gpio_api_overview API Overview
  129. * @{
  130. */
  131. #include <compiler.h>
  132. #include <system_sam_b.h>
  133. #ifdef __cplusplus
  134. extern "C" {
  135. #endif
  136. /**
  137. * \brief GPIO pin direction configuration enum.
  138. *
  139. * Enum for the possible pin direction settings of the gpio pin configuration
  140. * structure, to indicate the direction the pin should use.
  141. */
  142. enum gpio_pin_dir {
  143. /** The pin's input buffer should be enabled, so that the pin state can
  144. * be read. */
  145. GPIO_PIN_DIR_INPUT,
  146. /** The pin's output buffer should be enabled, so that the pin state can
  147. * be set. */
  148. GPIO_PIN_DIR_OUTPUT,
  149. };
  150. /**
  151. * \brief GPIO pin input pull configuration enum.
  152. *
  153. * Enum for the possible pin pull settings of the GPIO pin configuration
  154. * structure, to indicate the type of logic level pull the pin should use.
  155. */
  156. enum gpio_pin_pull {
  157. /** No logical pull should be applied to the pin */
  158. GPIO_PIN_PULL_NONE,
  159. /** Pin should be pulled up when idle */
  160. GPIO_PIN_PULL_UP ,
  161. /** Pin should be pulled down when idle */
  162. GPIO_PIN_PULL_DOWN,
  163. };
  164. /**
  165. * \brief GPIO pinmux selection enum.
  166. *
  167. * Enum for the pinmux settings of the GPIO pin configuration.
  168. */
  169. enum gpio_pinmux_sel {
  170. /** PINMUX selection 0 */
  171. GPIO_PINMUX_SEL_0 = 0,
  172. /** PINMUX selection 1 */
  173. GPIO_PINMUX_SEL_1,
  174. /** PINMUX selection 2 */
  175. GPIO_PINMUX_SEL_2,
  176. /** PINMUX selection 3 */
  177. GPIO_PINMUX_SEL_3,
  178. /** PINMUX selection 4 */
  179. GPIO_PINMUX_SEL_4,
  180. /** PINMUX selection 5 */
  181. GPIO_PINMUX_SEL_5,
  182. /** PINMUX selection 6 */
  183. GPIO_PINMUX_SEL_6,
  184. /** PINMUX selection 7 */
  185. GPIO_PINMUX_SEL_7,
  186. };
  187. /**
  188. * \brief GPIO module instance
  189. *
  190. * Forward Declaration for the device instance.
  191. */
  192. struct gpio_module;
  193. /**
  194. * \brief GPIO callback type
  195. *
  196. * Type of the callback functions.
  197. */
  198. typedef void (*gpio_callback_t)(void);
  199. /**
  200. * \brief GPIO Callback enum
  201. *
  202. * Callbacks for the GPIO driver.
  203. */
  204. enum gpio_callback {
  205. /** Callback for low level */
  206. GPIO_CALLBACK_LOW,
  207. /** Callback for high level */
  208. GPIO_CALLBACK_HIGH,
  209. /** Callback for rising edge */
  210. GPIO_CALLBACK_RISING,
  211. /** Callback for falling edge */
  212. GPIO_CALLBACK_FALLING,
  213. /** Number of available callbacks */
  214. GPIO_CALLBACK_N,
  215. };
  216. /**
  217. * \brief GPIO pin configuration structure.
  218. *
  219. * Configuration structure for a GPIO pin instance. This structure should be
  220. * initialized by the \ref GPIO_get_config_defaults() function before being
  221. * modified by the user application.
  222. */
  223. struct gpio_config {
  224. /** GPIO buffer input/output direction */
  225. enum gpio_pin_dir direction;
  226. /** GPIO pull-up/pull-down for input pins */
  227. enum gpio_pin_pull input_pull;
  228. /** Enable lowest possible powerstate on the pin
  229. *
  230. * \note All other configurations will be ignored, the pin will be disabled
  231. */
  232. bool powersave;
  233. /** Enable AON_GPIOs to wakeup MCU from ULP mode
  234. *
  235. * \note Only AON_GPIO_0, AON_GPIO_1, and AON_GPIO_2 could enable this feature
  236. */
  237. bool aon_wakeup;
  238. };
  239. /**
  240. * \brief GPIO driver software device instance structure.
  241. *
  242. * GPIO driver software instance structure, used to retain software
  243. * state information of an associated hardware module instance.
  244. *
  245. * \note The fields of this structure should not be altered by the user
  246. * application; they are reserved for module-internal use only.
  247. */
  248. struct gpio_module {
  249. #if !defined(__DOXYGEN__)
  250. /** Pointer to the hardware instance */
  251. Gpio *hw;
  252. /** Array to store callback function pointers in */
  253. gpio_callback_t callback[16];
  254. /** Bit mask for callbacks registered */
  255. uint16_t callback_reg_mask;
  256. /** Bit mask for callbacks enabled */
  257. uint16_t callback_enable_mask;
  258. #endif
  259. };
  260. /** \name Configuration and initialization
  261. * @{
  262. */
  263. void gpio_get_config_defaults(struct gpio_config *const config);
  264. enum status_code gpio_pin_set_config(const uint8_t gpio_pin,
  265. const struct gpio_config *config);
  266. /** @} */
  267. /** \name State reading/writing (logical pin orientated)
  268. * @{
  269. */
  270. bool gpio_pin_get_input_level(const uint8_t gpio_pin);
  271. bool gpio_pin_get_output_level(const uint8_t gpio_pin);
  272. void gpio_pin_set_output_level(const uint8_t gpio_pin, const bool level);
  273. void gpio_pin_toggle_output_level(const uint8_t gpio_pin);
  274. /** @} */
  275. /** \name PINMUX selection configuration
  276. * @{
  277. */
  278. void gpio_pinmux_cofiguration(const uint8_t gpio_pin, uint16_t pinmux_sel);
  279. /** @}*/
  280. /** \name GPIO callback config
  281. * @{
  282. */
  283. void gpio_register_callback(uint8_t gpio_pin, gpio_callback_t callback_func,
  284. enum gpio_callback callback_type);
  285. void gpio_unregister_callback(uint8_t gpio_pin,
  286. enum gpio_callback callback_type);
  287. void gpio_enable_callback(uint8_t gpio_pin);
  288. void gpio_disable_callback(uint8_t gpio_pin);
  289. void gpio_init(void);
  290. /** @}*/
  291. /** @}*/
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. /**
  296. * \page asfdoc_samb_gpio_extra Extra Information for GPIO Driver
  297. *
  298. * \section asfdoc_samb_gpio_extra_acronyms Acronyms
  299. * Below is a table listing the acronyms used in this module, along with their
  300. * intended meanings.
  301. *
  302. * <table>
  303. * <tr>
  304. * <th>Acronym</th>
  305. * <th>Description</th>
  306. * </tr>
  307. * <tr>
  308. * <td>GPIO</td>
  309. * <td>General Purpose Input/Output</td>
  310. * </tr>
  311. * </table>
  312. *
  313. *
  314. * \section asfdoc_samb_gpio_extra_dependencies Dependencies
  315. * There are no dependencies related to this driver.
  316. *
  317. *
  318. * \section asfdoc_samb_gpio_extra_errata Errata
  319. * There are no errata related to this driver.
  320. *
  321. *
  322. * \section asfdoc_samb_gpio_extra_history Module History
  323. * An overview of the module history is presented in the table below, with
  324. * details on the enhancements and fixes made to the module since its first
  325. * release. The current version of this corresponds to the newest version in
  326. * the table.
  327. *
  328. * <table>
  329. * <tr>
  330. * <th>Changelog</th>
  331. * </tr>
  332. * <tr>
  333. * <td>Initial Release</td>
  334. * </tr>
  335. * </table>
  336. */
  337. /**
  338. * \page asfdoc_samb_gpio_exqsg Examples for GPIO Driver
  339. *
  340. * This is a list of the available Quick Start guides (QSGs) and example
  341. * applications for \ref asfdoc_samb_gpio_group. QSGs are simple examples with
  342. * step-by-step instructions to configure and use this driver in a selection of
  343. * use cases. Note that QSGs can be compiled as a standalone application or be
  344. * added to the user application.
  345. *
  346. * - \subpage asfdoc_samb_gpio_basic_use_case
  347. *
  348. * \page asfdoc_samb_gpio_document_revision_history Document Revision History
  349. *
  350. * <table>
  351. * <tr>
  352. * <th>Doc. Rev.</td>
  353. * <th>Date</td>
  354. * <th>Comments</td>
  355. * </tr>
  356. * <tr>
  357. * <td>A</td>
  358. * <td>09/2015</td>
  359. * <td>Initial release</td>
  360. * </tr>
  361. * </table>
  362. */
  363. #endif