ble_dtm_hw_nrf51.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2016 - 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 "ble_dtm_hw.h"
  41. #include "ble_dtm.h"
  42. #include <stdbool.h>
  43. #include <string.h>
  44. #include "nrf.h"
  45. void dtm_turn_off_test()
  46. {
  47. NRF_RADIO->TEST = 0;
  48. }
  49. void dtm_constant_carrier()
  50. {
  51. NRF_RADIO->TEST = (RADIO_TEST_PLL_LOCK_Enabled << RADIO_TEST_PLL_LOCK_Pos) |
  52. (RADIO_TEST_CONST_CARRIER_Enabled << RADIO_TEST_CONST_CARRIER_Pos);
  53. }
  54. uint32_t dtm_radio_validate(int32_t m_tx_power, uint8_t m_radio_mode)
  55. {
  56. // Handle BLE Radio tuning parameters from production for DTM if required.
  57. // Only needed for DTM without SoftDevice, as the SoftDevice normally handles this.
  58. // PCN-083.
  59. if ( ((NRF_FICR->OVERRIDEEN) & FICR_OVERRIDEEN_BLE_1MBIT_Msk) == FICR_OVERRIDEEN_BLE_1MBIT_Override)
  60. {
  61. NRF_RADIO->OVERRIDE0 = NRF_FICR->BLE_1MBIT[0];
  62. NRF_RADIO->OVERRIDE1 = NRF_FICR->BLE_1MBIT[1];
  63. NRF_RADIO->OVERRIDE2 = NRF_FICR->BLE_1MBIT[2];
  64. NRF_RADIO->OVERRIDE3 = NRF_FICR->BLE_1MBIT[3];
  65. NRF_RADIO->OVERRIDE4 = NRF_FICR->BLE_1MBIT[4];
  66. }
  67. // Initializing code below is quite generic - for BLE, the values are fixed, and expressions
  68. // are constant. Non-constant values are essentially set in radio_prepare().
  69. if (!(m_tx_power == RADIO_TXPOWER_TXPOWER_0dBm ||
  70. m_tx_power == RADIO_TXPOWER_TXPOWER_Pos4dBm ||
  71. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg30dBm ||
  72. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg20dBm ||
  73. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg16dBm ||
  74. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg12dBm ||
  75. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg8dBm ||
  76. m_tx_power == RADIO_TXPOWER_TXPOWER_Neg4dBm
  77. ) ||
  78. (m_radio_mode > RADIO_MODE_MODE_Ble_1Mbit) // Values 0 - 2: Proprietary mode, 3 (last valid): BLE
  79. )
  80. {
  81. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  82. }
  83. return DTM_SUCCESS;
  84. }
  85. bool dtm_hw_set_timer(NRF_TIMER_Type ** mp_timer, IRQn_Type * m_timer_irq, uint32_t new_timer)
  86. {
  87. if (new_timer == 0)
  88. {
  89. *mp_timer = NRF_TIMER0;
  90. *m_timer_irq = TIMER0_IRQn;
  91. }
  92. else if (new_timer == 1)
  93. {
  94. *mp_timer = NRF_TIMER1;
  95. *m_timer_irq = TIMER1_IRQn;
  96. }
  97. else if (new_timer == 2)
  98. {
  99. *mp_timer = NRF_TIMER2;
  100. *m_timer_irq = TIMER2_IRQn;
  101. }
  102. else
  103. {
  104. // Parameter error: Only TIMER 0, 1, 2 provided by nRF51
  105. return false;
  106. }
  107. // New timer has been selected:
  108. return true;
  109. }