pal_gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file
  26. *
  27. * \brief This file implements the platform abstraction layer APIs for gpio.
  28. *
  29. * \addtogroup grPAL
  30. * @{
  31. */
  32. /**********************************************************************************************************************
  33. * HEADER FILES
  34. *********************************************************************************************************************/
  35. #include "optiga/pal/pal_gpio.h"
  36. #include "optiga/pal/pal_ifx_i2c_config.h"
  37. #include "nrf_gpio.h"
  38. #include "pal_pin_config.h"
  39. /**********************************************************************************************************************
  40. * MACROS
  41. *********************************************************************************************************************/
  42. /**********************************************************************************************************************
  43. * LOCAL DATA
  44. *********************************************************************************************************************/
  45. /**********************************************************************************************************************
  46. * LOCAL ROUTINES
  47. *********************************************************************************************************************/
  48. void setup_nrf_gpio(uint32_t pin)
  49. {
  50. // don't touch pin config for unused pins
  51. if (pin == OPTIGA_PIN_UNUSED) {
  52. return;
  53. }
  54. // remove our flags to allow nrf_gpio_* functions to work
  55. const uint32_t pin_nr = pin & ~OPTIGA_PIN_ALL_MASKS;
  56. // Init pin direction
  57. nrf_gpio_cfg_output(pin_nr);
  58. // Set pin to initial state
  59. nrf_gpio_pin_write(pin_nr, pin & OPTIGA_PIN_INITIAL_VAL_MASK);
  60. }
  61. void write_nrf_gpio(uint32_t pin, bool value)
  62. {
  63. // Skip pins marked for one time init or unused
  64. if ((pin == OPTIGA_PIN_UNUSED) || (pin & OPTIGA_PIN_ONE_TIME_INIT_MASK)) {
  65. return;
  66. }
  67. // remove our flags to allow nrf_gpio_* functions to work
  68. const uint32_t pin_nr = pin & ~OPTIGA_PIN_ALL_MASKS;
  69. nrf_gpio_pin_write(pin_nr, value);
  70. }
  71. /**********************************************************************************************************************
  72. * API IMPLEMENTATION
  73. *********************************************************************************************************************/
  74. pal_status_t pal_gpio_init(const pal_gpio_t * p_gpio_context)
  75. {
  76. const uint32_t vdd_pin = (uint32_t)(optiga_vdd_0.p_gpio_hw);
  77. const uint32_t rst_pin = (uint32_t)(optiga_reset_0.p_gpio_hw);
  78. setup_nrf_gpio(vdd_pin);
  79. setup_nrf_gpio(rst_pin);
  80. return PAL_STATUS_SUCCESS;
  81. }
  82. /**
  83. * Sets the gpio pin to high state
  84. *
  85. * <b>API Details:</b>
  86. * The API sets the pin high, only if the pin is assigned to a valid gpio context.<br>
  87. * Otherwise the API returns without any failure status.<br>
  88. *
  89. *\param[in] p_gpio_context Pointer to pal layer gpio context
  90. *
  91. *
  92. */
  93. void pal_gpio_set_high(const pal_gpio_t* p_gpio_context)
  94. {
  95. if (p_gpio_context != NULL)
  96. {
  97. write_nrf_gpio((uint32_t)(p_gpio_context->p_gpio_hw), true);
  98. }
  99. }
  100. /**
  101. * Sets the gpio pin to low state
  102. *
  103. * <b>API Details:</b>
  104. * The API set the pin low, only if the pin is assigned to a valid gpio context.<br>
  105. * Otherwise the API returns without any failure status.<br>
  106. *
  107. *\param[in] p_gpio_context Pointer to pal layer gpio context
  108. *
  109. */
  110. void pal_gpio_set_low(const pal_gpio_t* p_gpio_context)
  111. {
  112. if (p_gpio_context != NULL)
  113. {
  114. write_nrf_gpio((uint32_t)(p_gpio_context->p_gpio_hw), false);
  115. }
  116. }
  117. /**
  118. * @}
  119. */