ser_app_hal_nrf5x.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2014 - 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 "app_util_platform.h"
  41. #include "ser_app_hal.h"
  42. #include "nrf.h"
  43. #include "nrf_gpio.h"
  44. #include "nrf_soc.h"
  45. #include "nrf_delay.h"
  46. #include "nrf_nvmc.h"
  47. #include "boards.h"
  48. #include "ser_phy.h"
  49. #include "ser_phy_config_app.h"
  50. #include "nrf_drv_clock.h"
  51. #define SOFTDEVICE_EVT_IRQ SD_EVT_IRQn /**< SoftDevice Event IRQ number. Used for both protocol events and SoC events. */
  52. #define FLASH_WRITE_MAX_LENGTH ((uint16_t)NRF_FICR->CODEPAGESIZE)
  53. #define BLE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE) /**< Size of one flash page. */
  54. static ser_app_hal_flash_op_done_handler_t m_flash_op_handler;
  55. uint32_t ser_app_hal_hw_init(ser_app_hal_flash_op_done_handler_t handler)
  56. {
  57. nrf_gpio_cfg_output(CONN_CHIP_RESET_PIN_NO);
  58. if (NRF_SUCCESS != nrf_drv_clock_init())
  59. {
  60. return NRF_ERROR_INTERNAL;
  61. }
  62. nrf_drv_clock_lfclk_request(NULL);
  63. nrf_drv_clock_hfclk_request(NULL);
  64. while (false == nrf_drv_clock_hfclk_is_running());
  65. while (false == nrf_drv_clock_lfclk_is_running());
  66. m_flash_op_handler = handler;
  67. return NRF_SUCCESS;
  68. }
  69. void ser_app_hal_delay(uint32_t ms)
  70. {
  71. nrf_delay_ms(ms);
  72. }
  73. void ser_app_hal_nrf_reset_pin_clear()
  74. {
  75. nrf_gpio_pin_clear(CONN_CHIP_RESET_PIN_NO);
  76. }
  77. void ser_app_hal_nrf_reset_pin_set()
  78. {
  79. nrf_gpio_pin_set(CONN_CHIP_RESET_PIN_NO);
  80. }
  81. void ser_app_hal_nrf_evt_irq_priority_set()
  82. {
  83. NVIC_SetPriority(SOFTDEVICE_EVT_IRQ, APP_IRQ_PRIORITY_LOWEST);
  84. }
  85. void ser_app_hal_nrf_evt_pending()
  86. {
  87. NVIC_SetPendingIRQ(SOFTDEVICE_EVT_IRQ);
  88. }
  89. uint32_t sd_ppi_channel_enable_get(uint32_t * p_channel_enable)
  90. {
  91. *p_channel_enable = NRF_PPI->CHEN;
  92. return NRF_SUCCESS;
  93. }
  94. uint32_t sd_ppi_channel_enable_set(uint32_t channel_enable_set_msk)
  95. {
  96. NRF_PPI->CHEN = channel_enable_set_msk;
  97. return NRF_SUCCESS;
  98. }
  99. uint32_t sd_ppi_channel_assign(uint8_t channel_num,
  100. const volatile void * evt_endpoint,
  101. const volatile void * task_endpoint)
  102. {
  103. NRF_PPI->CH[channel_num].TEP = (uint32_t)task_endpoint;
  104. NRF_PPI->CH[channel_num].EEP = (uint32_t)evt_endpoint;
  105. return NRF_SUCCESS;
  106. }
  107. /**
  108. * @brief Check if given address is in device FLASH range.
  109. *
  110. * @param[in] ptr Address to check.
  111. * @retval true Given address is located in FLASH.
  112. * @retval false Given address is not located in FLASH.
  113. */
  114. __STATIC_INLINE bool addr_is_in_FLASH(void const * const ptr)
  115. {
  116. return ((((uintptr_t)ptr) & 0xFF000000u) == 0x00000000u);
  117. }
  118. uint32_t sd_flash_page_erase(uint32_t page_number)
  119. {
  120. uint32_t * p_page = (uint32_t *)(BLE_FLASH_PAGE_SIZE * page_number);
  121. if (!addr_is_in_FLASH(p_page))
  122. {
  123. return NRF_ERROR_INVALID_ADDR;
  124. }
  125. nrf_nvmc_page_erase((uint32_t) p_page);
  126. m_flash_op_handler(true);
  127. return NRF_SUCCESS;
  128. }
  129. uint32_t sd_flash_write(uint32_t * const p_dst, uint32_t const * const p_src, uint32_t size)
  130. {
  131. if (size > FLASH_WRITE_MAX_LENGTH)
  132. {
  133. return NRF_ERROR_INVALID_LENGTH;
  134. }
  135. if (!addr_is_in_FLASH(p_dst))
  136. {
  137. return NRF_ERROR_INVALID_ADDR;
  138. }
  139. nrf_nvmc_write_words((uint32_t) p_dst, p_src, size);
  140. m_flash_op_handler(true);
  141. return NRF_SUCCESS;
  142. }