nrf21540_gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Copyright (c) 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf21540_gpio.h"
  41. #include "nrf_assert.h"
  42. #include "boards.h"
  43. #include "nrf21540_defs.h"
  44. #include "nrf_gpiote.h"
  45. #include "nrf_ppi.h"
  46. #include "nrf_timer.h"
  47. void nrf21540_gpio_init(void)
  48. {
  49. nrf_gpio_cfg_output(NRF21540_ANTSEL_PIN);
  50. #if NRF21540_USE_GPIO_MANAGEMENT
  51. nrf_gpio_cfg_output(NRF21540_MODE_PIN);
  52. //GPIOTE for TXEN pin configuration
  53. nrf_gpiote_task_configure(NRF21540_PA_GPIOTE_CHANNEL_NO,
  54. NRF21540_TXEN_PIN,
  55. (nrf_gpiote_polarity_t) GPIOTE_CONFIG_POLARITY_None,
  56. NRF_GPIOTE_INITIAL_VALUE_LOW);
  57. nrf_gpiote_task_enable(NRF21540_PA_GPIOTE_CHANNEL_NO);
  58. //GPIOTE for RXEN pin configuration
  59. nrf_gpiote_task_configure(NRF21540_LNA_GPIOTE_CHANNEL_NO,
  60. NRF21540_RXEN_PIN,
  61. (nrf_gpiote_polarity_t) GPIOTE_CONFIG_POLARITY_None,
  62. NRF_GPIOTE_INITIAL_VALUE_LOW);
  63. nrf_gpiote_task_enable(NRF21540_LNA_GPIOTE_CHANNEL_NO);
  64. #endif /*NRF21540_USE_GPIO_MANAGEMENT*/
  65. }
  66. ret_code_t nrf21540_gpio_ant_set(nrf21540_antenna_t antenna)
  67. {
  68. if (antenna == NRF21540_ANT1)
  69. {
  70. nrf_gpio_pin_clear(NRF21540_ANTSEL_PIN);
  71. }
  72. else if (antenna == NRF21540_ANT2)
  73. {
  74. nrf_gpio_pin_set(NRF21540_ANTSEL_PIN);
  75. }
  76. else
  77. {
  78. return NRF_ERROR_INVALID_PARAM;
  79. }
  80. return NRF_SUCCESS;
  81. }
  82. #if NRF21540_USE_GPIO_MANAGEMENT
  83. uint32_t nrf21540_gpio_trx_task_start_address_get(nrf21540_trx_t dir,
  84. nrf21540_bool_state_t required_state)
  85. {
  86. uint8_t gpiote_rx_tx_channel =
  87. dir == NRF21540_TX ?
  88. NRF21540_PA_GPIOTE_CHANNEL_NO :
  89. NRF21540_LNA_GPIOTE_CHANNEL_NO;
  90. return required_state == NRF21540_ENABLE ?
  91. nrf_gpiote_task_addr_get(nrf_gpiote_set_task_get(gpiote_rx_tx_channel)) :
  92. nrf_gpiote_task_addr_get(nrf_gpiote_clr_task_get(gpiote_rx_tx_channel));
  93. }
  94. void nrf21540_gpio_trx_enable(nrf21540_trx_t dir)
  95. {
  96. uint32_t gpiote_task_start = nrf21540_gpio_trx_task_start_address_get(dir, NRF21540_ENABLE);
  97. nrf_ppi_channel_endpoint_setup(NRF21540_TRX_PPI_CHANNEL,
  98. (uint32_t)nrf_timer_event_address_get(NRF21540_TIMER,
  99. NRF21540_TIMER_CC_PD_PG_EVENT),
  100. gpiote_task_start);
  101. nrf_ppi_channel_enable(NRF21540_TRX_PPI_CHANNEL);
  102. }
  103. ret_code_t nrf21540_gpio_pwr_mode_set(nrf21540_pwr_mode_t mode)
  104. {
  105. if (mode == NRF21540_PWR_MODE_A)
  106. {
  107. nrf_gpio_pin_clear(NRF21540_MODE_PIN);
  108. }
  109. else if (mode == NRF21540_PWR_MODE_B)
  110. {
  111. nrf_gpio_pin_set(NRF21540_MODE_PIN);
  112. }
  113. else
  114. {
  115. return NRF_ERROR_INVALID_PARAM;
  116. }
  117. return NRF_SUCCESS;
  118. }
  119. #endif /*NRF21540_USE_GPIO_MANAGEMENT*/