ble_dtm.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /**
  2. * Copyright (c) 2012 - 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(BLE_DTM)
  42. #include "ble_dtm.h"
  43. #include "ble_dtm_hw.h"
  44. #include <stdlib.h>
  45. #include <stdbool.h>
  46. #include <string.h>
  47. #include "nrf.h"
  48. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  49. #include "nrf21540.h"
  50. #endif
  51. #if NRF_RADIO_ANTENNA_COUNT > DTM_MAX_ANTENNA_CNT
  52. #error "Antena count must be smaller or equal 19"
  53. #endif
  54. #if defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  55. #define DIRECTION_FINDING_SUPPORTED 1
  56. #else
  57. #define DIRECTION_FINDING_SUPPORTED 0
  58. #endif // defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  59. #define DTM_HEADER_OFFSET 0 /**< Index where the header of the pdu is located. */
  60. #define DTM_HEADER_SIZE 2 /**< Size of PDU header. */
  61. #define DTM_HEADER_WITH_CTE_SIZE 3 /**< Size of PDU header with CTEInfo field. */
  62. #define DTM_HEADER_CTEINFO_OFFSET 2 /**< CTEInfo field offset in payload. */
  63. #define DTM_PAYLOAD_MAX_SIZE 255 /**< Maximum payload size allowed during dtm execution. */
  64. #define DTM_LENGTH_OFFSET (DTM_HEADER_OFFSET + 1) /**< Index where the length of the payload is encoded. */
  65. #define DTM_PDU_MAX_MEMORY_SIZE (DTM_HEADER_WITH_CTE_SIZE + DTM_PAYLOAD_MAX_SIZE) /**< Maximum PDU size allowed during dtm execution. */
  66. #define DTM_ON_AIR_OVERHEAD_SIZE 10 /**< Size of the packet on air without the payload (preamble + sync word + type + RFU + length + CRC). */
  67. #define DTM_CTE_REF_SAMPLE_CNT 8 /**< CTE Reference period sample count. */
  68. #define DTM_RESPONSE_EVENT_SHIFT 0x01 /**< Response event data shift. */
  69. #define NRF_MAX_PAYLOAD_OCTETS 0x00FF /**< Maximum number of payload octets that the local Controller supports for transmission of a single Link Layer Data Physical Channel PDU. */
  70. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  71. #define NRF_MAX_RX_TX_TIME 0x2148 /**< Maximum transmit or receive time, in microseconds, that the local Controller supports for transmission of a single Link Layer Data Physical Channel PDU, divided by 2. */
  72. #else
  73. #define NRF_MAX_RX_TX_TIME 0x424
  74. #endif // defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  75. #define NRF_CTE_MAX_LENGTH 0x14 /**< Maximum length of the Constant Tone Extension that the local Controller supports for transmission in a Link Layer packet, in 8 us units. */
  76. #define NRF_CTE_TIME_IN_US 0x08 /**< CTE time unit in us. CTE length is expressed in 8us unit. */
  77. #define RX_MODE true /**< Constant defining RX mode for radio during dtm test. */
  78. #define TX_MODE false /**< Constant defining TX mode for radio during dtm test. */
  79. #define PHYS_CH_MAX 39 /**< Maximum number of valid channels in BLE. */
  80. // Values that for now are "constants" - they could be configured by a function setting them,
  81. // but most of these are set by the BLE DTM standard, so changing them is not relevant.
  82. #define RFPHY_TEST_0X0F_REF_PATTERN 0x0f /**< RF-PHY test packet patterns, for the repeated octet packets. */
  83. #define RFPHY_TEST_0X55_REF_PATTERN 0x55 /**< RF-PHY test packet patterns, for the repeated octet packets. */
  84. #define RFPHY_TEST_0XFF_REF_PATTERN 0xFF /**< RF-PHY test packet patterns, for the repeated octet packets. */
  85. #define PRBS9_CONTENT {0xFF, 0xC1, 0xFB, 0xE8, 0x4C, 0x90, 0x72, 0x8B, \
  86. 0xE7, 0xB3, 0x51, 0x89, 0x63, 0xAB, 0x23, 0x23, \
  87. 0x02, 0x84, 0x18, 0x72, 0xAA, 0x61, 0x2F, 0x3B, \
  88. 0x51, 0xA8, 0xE5, 0x37, 0x49, 0xFB, 0xC9, 0xCA, \
  89. 0x0C, 0x18, 0x53, 0x2C, 0xFD, 0x45, 0xE3, 0x9A, \
  90. 0xE6, 0xF1, 0x5D, 0xB0, 0xB6, 0x1B, 0xB4, 0xBE, \
  91. 0x2A, 0x50, 0xEA, 0xE9, 0x0E, 0x9C, 0x4B, 0x5E, \
  92. 0x57, 0x24, 0xCC, 0xA1, 0xB7, 0x59, 0xB8, 0x87, \
  93. 0xFF, 0xE0, 0x7D, 0x74, 0x26, 0x48, 0xB9, 0xC5, \
  94. 0xF3, 0xD9, 0xA8, 0xC4, 0xB1, 0xD5, 0x91, 0x11, \
  95. 0x01, 0x42, 0x0C, 0x39, 0xD5, 0xB0, 0x97, 0x9D, \
  96. 0x28, 0xD4, 0xF2, 0x9B, 0xA4, 0xFD, 0x64, 0x65, \
  97. 0x06, 0x8C, 0x29, 0x96, 0xFE, 0xA2, 0x71, 0x4D, \
  98. 0xF3, 0xF8, 0x2E, 0x58, 0xDB, 0x0D, 0x5A, 0x5F, \
  99. 0x15, 0x28, 0xF5, 0x74, 0x07, 0xCE, 0x25, 0xAF, \
  100. 0x2B, 0x12, 0xE6, 0xD0, 0xDB, 0x2C, 0xDC, 0xC3, \
  101. 0x7F, 0xF0, 0x3E, 0x3A, 0x13, 0xA4, 0xDC, 0xE2, \
  102. 0xF9, 0x6C, 0x54, 0xE2, 0xD8, 0xEA, 0xC8, 0x88, \
  103. 0x00, 0x21, 0x86, 0x9C, 0x6A, 0xD8, 0xCB, 0x4E, \
  104. 0x14, 0x6A, 0xF9, 0x4D, 0xD2, 0x7E, 0xB2, 0x32, \
  105. 0x03, 0xC6, 0x14, 0x4B, 0x7F, 0xD1, 0xB8, 0xA6, \
  106. 0x79, 0x7C, 0x17, 0xAC, 0xED, 0x06, 0xAD, 0xAF, \
  107. 0x0A, 0x94, 0x7A, 0xBA, 0x03, 0xE7, 0x92, 0xD7, \
  108. 0x15, 0x09, 0x73, 0xE8, 0x6D, 0x16, 0xEE, 0xE1, \
  109. 0x3F, 0x78, 0x1F, 0x9D, 0x09, 0x52, 0x6E, 0xF1, \
  110. 0x7C, 0x36, 0x2A, 0x71, 0x6C, 0x75, 0x64, 0x44, \
  111. 0x80, 0x10, 0x43, 0x4E, 0x35, 0xEC, 0x65, 0x27, \
  112. 0x0A, 0xB5, 0xFC, 0x26, 0x69, 0x3F, 0x59, 0x99, \
  113. 0x01, 0x63, 0x8A, 0xA5, 0xBF, 0x68, 0x5C, 0xD3, \
  114. 0x3C, 0xBE, 0x0B, 0xD6, 0x76, 0x83, 0xD6, 0x57, \
  115. 0x05, 0x4A, 0x3D, 0xDD, 0x81, 0x73, 0xC9, 0xEB, \
  116. 0x8A, 0x84, 0x39, 0xF4, 0x36, 0x0B, 0xF7} /**< The PRBS9 sequence used as packet payload.
  117. The bytes in the sequence is in the right order, but the bits of each byte in the array is reverse.
  118. of that found by running the PRBS9 algorithm. This is because of the endianess of the nRF5 radio. */
  119. #if defined(NRF52832_XXAA) || defined(NRF52810_XXAA)
  120. #define DTM_SUPPORTED_FEATURE (DTM_LE_DATA_PACKET_LEN_EXTENSION | DTM_LE_2M_PHY)
  121. #elif defined(NRF52840_XXAA)
  122. #define DTM_SUPPORTED_FEATURE (DTM_LE_DATA_PACKET_LEN_EXTENSION | DTM_LE_2M_PHY | DTM_LE_CODED_PHY)
  123. #elif defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  124. #define DTM_SUPPORTED_FEATURE (DTM_LE_DATA_PACKET_LEN_EXTENSION | DTM_LE_2M_PHY | DTM_LE_CODED_PHY | \
  125. DTM_LE_CONSTANT_TONE_EXTENSION | DTM_LE_ANTENNA_SWITCH | \
  126. DTM_LE_AOD_1US_TANSMISSION | DTM_LE_AOD_1US_RECEPTION | \
  127. DTM_LE_AOA_1US_RECEPTION)
  128. #endif
  129. /**@brief Structure holding the PDU used for transmitting/receiving a PDU.
  130. */
  131. typedef struct
  132. {
  133. uint8_t content[DTM_HEADER_WITH_CTE_SIZE + DTM_PAYLOAD_MAX_SIZE]; /**< PDU packet content. */
  134. } pdu_type_t;
  135. /**@brief States used for the DTM test implementation.
  136. */
  137. typedef enum
  138. {
  139. STATE_UNINITIALIZED, /**< The DTM is uninitialized. */
  140. STATE_IDLE, /**< State when system has just initialized, or current test has completed. */
  141. STATE_TRANSMITTER_TEST, /**< State used when a DTM Transmission test is running. */
  142. STATE_CARRIER_TEST, /**< State used when a DTM Carrier test is running (Vendor specific test). */
  143. STATE_RECEIVER_TEST /**< State used when a DTM Receive test is running. */
  144. } state_t;
  145. /**@brief Constant Tone Extension mode.
  146. */
  147. typedef enum
  148. {
  149. CTE_MODE_OFF = 0x00, /**< Do not use the Constant Tone Extension. */
  150. CTE_MODE_AOD = 0x02, /**< Constant Tone Extension: Use Angle-of-Departure. */
  151. CTE_MODE_AOA = 0x03 /**< Constant Tone Extension: Use Angle-of-Arrival. */
  152. } cte_mode_t;
  153. /** Constatnt Tone Extension slot.
  154. */
  155. typedef enum
  156. {
  157. CTE_SLOT_2US = 0x01, /**< Constant Tone Extension: Sample with 1 us slot. */
  158. CTE_SLOT_1US = 0x02, /**< Constant Tone Extension: Sample with 2 us slot. */
  159. } cte_slot_t;
  160. /** Constatnt Tone Extension antenna switch pattern.
  161. */
  162. typedef enum
  163. {
  164. ANTENNA_PATTERN_123N123N = 0x00, /**< Constant Tone Extension: Antenna switch pattern 1, 2, 3 ...N. */
  165. ANTENNA_PATTERN_123N2123 = 0x01 /**< Constant Tone Extension: Antenna switch pattern 1, 2, 3 ...N, N - 1, N - 2, ..., 1, ... */
  166. } antenna_pattern_t;
  167. // Internal variables set as side effects of commands or events.
  168. static state_t m_state = STATE_UNINITIALIZED; /**< Current machine state. */
  169. static uint16_t m_rx_pkt_count; /**< Number of valid packets received. */
  170. static pdu_type_t m_pdu; /**< PDU to be sent. */
  171. static uint16_t m_event; /**< current command status - initially "ok", may be set if error detected, or to packet count. */
  172. static bool m_new_event; /**< Command has been processed - number of not yet reported event bytes. */
  173. static uint32_t m_packet_length; /**< Payload length of transmitted PDU, bits 2:7 of 16-bit dtm command. */
  174. static dtm_pkt_type_t m_packet_type; /**< Bits 0..1 of 16-bit transmit command, or 0xFFFFFFFF. */
  175. static dtm_freq_t m_phys_ch; /**< 0..39 physical channel number (base 2402 MHz, Interval 2 MHz), bits 8:13 of 16-bit dtm command. */
  176. static uint32_t m_current_time = 0; /**< Counter for interrupts from timer to ensure that the 2 bytes forming a DTM command are received within the time window. */
  177. #if DIRECTION_FINDING_SUPPORTED
  178. static cte_mode_t m_cte_mode = CTE_MODE_OFF; /**< Constant Tone Extension mode. */
  179. static cte_slot_t m_cte_slot = CTE_SLOT_2US; /**< Constant Tone Extension sample slot */
  180. static uint8_t m_cte_time = 0; /**< Constant Tone Extension length in 8us unit. */
  181. static antenna_pattern_t m_antenna_pattern = ANTENNA_PATTERN_123N123N; /**< Antenna switch pattern. */
  182. static uint8_t m_antenna_number = 0; /**< Number of antenna in the antenna array. */
  183. static uint8_t m_cte_info = 0; /**< CTEInfo. */
  184. #endif // DIRECTION_FINDING_SUPPORTED
  185. // Nordic specific configuration values (not defined by BLE standard).
  186. // Definition of initial values found in ble_dtm.h
  187. static uint32_t m_tx_power = DEFAULT_TX_POWER; /**< TX power for transmission test, default 0 dBm. */
  188. static NRF_TIMER_Type * mp_timer = DEFAULT_TIMER; /**< Timer to be used. */
  189. static IRQn_Type m_timer_irq = DEFAULT_TIMER_IRQn; /**< which interrupt line to clear on every timeout */
  190. static uint8_t const m_prbs_content[] = PRBS9_CONTENT; /**< Pseudo-random bit sequence defined by the BLE standard. */
  191. static uint8_t m_packetHeaderLFlen = 8; /**< Length of length field in packet Header (in bits). */
  192. static uint8_t m_packetHeaderS0len = 1; /**< Length of S0 field in packet Header (in bytes). */
  193. static uint8_t m_packetHeaderS1len = 0; /**< Length of S1 field in packet Header (in bits). */
  194. static uint8_t m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit; /**< Length of the preamble. */
  195. static uint8_t m_crcConfSkipAddr = 1; /**< Leave packet address field out of CRC calculation. */
  196. static uint8_t m_static_length = 0; /**< Number of bytes sent in addition to the var.length payload. */
  197. static uint32_t m_balen = 3; /**< Base address length in bytes. */
  198. static uint32_t m_endian = RADIO_PCNF1_ENDIAN_Little; /**< On air endianess of packet, this applies to the S0, LENGTH, S1 and the PAYLOAD fields. */
  199. static uint32_t m_whitening = RADIO_PCNF1_WHITEEN_Disabled; /**< Whitening disabled. */
  200. static uint8_t m_crcLength = RADIO_CRCCNF_LEN_Three; /**< CRC Length (in bytes). */
  201. static uint32_t m_address = 0x71764129; /**< Address. */
  202. static uint32_t m_crc_poly = 0x0000065B; /**< CRC polynomial. */
  203. static uint32_t m_crc_init = 0x00555555; /**< Initial value for CRC calculation. */
  204. static uint8_t m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit; /**< nRF51 specific radio mode value. */
  205. static uint32_t m_txIntervaluS = 2500; /**< Time between start of Tx packets (in uS). */
  206. // The variables and defines below are related to the workaround for nRF52840 anomaly 172
  207. static bool anomaly_172_wa_enabled = false; /**< Enable or disable the workaround for Errata 172. */
  208. static uint8_t m_strict_mode = 0; /**< Enable or disable strict mode to workaround Errata 172. */
  209. #define BLOCKER_FIX_RSSI_THRESHOLD 95 /**< The RSSI threshold at which to toggle strict mode. */
  210. #define BLOCKER_FIX_WAIT_DEFAULT 1250 /**< 1250 * 8 = 10000 us = 10 ms. */
  211. #define BLOCKER_FIX_WAIT_END 63 /**< 63 * 8 = ~500us. */
  212. #define BLOCKER_FIX_CNTDETECTTHR 15 /**< Threshold used to determine necessary strict mode status changes. */
  213. #define BLOCKER_FIX_CNTADDRTHR 2 /**< Threshold used to determine necessary strict mode status changes. */
  214. const uint32_t nrf_power_value[] = {
  215. RADIO_TXPOWER_TXPOWER_Neg40dBm,
  216. RADIO_TXPOWER_TXPOWER_Neg30dBm,
  217. RADIO_TXPOWER_TXPOWER_Neg20dBm,
  218. RADIO_TXPOWER_TXPOWER_Neg16dBm,
  219. RADIO_TXPOWER_TXPOWER_Neg12dBm,
  220. RADIO_TXPOWER_TXPOWER_Neg8dBm,
  221. RADIO_TXPOWER_TXPOWER_Neg4dBm,
  222. RADIO_TXPOWER_TXPOWER_0dBm,
  223. #if defined(RADIO_TXPOWER_TXPOWER_Pos2dBm)
  224. RADIO_TXPOWER_TXPOWER_Pos2dBm,
  225. #endif // defined(RADIO_TXPOWER_TXPOWER_Pos2dBm)
  226. RADIO_TXPOWER_TXPOWER_Pos3dBm,
  227. RADIO_TXPOWER_TXPOWER_Pos4dBm,
  228. #if defined(RADIO_TXPOWER_TXPOWER_Pos5dBm)
  229. RADIO_TXPOWER_TXPOWER_Pos5dBm,
  230. #endif // defined(RADIO_TXPOWER_TXPOWER_Pos5dBm)
  231. #if defined(RADIO_TXPOWER_TXPOWER_Pos6dBm)
  232. RADIO_TXPOWER_TXPOWER_Pos6dBm,
  233. #endif // defined(RADIO_TXPOWER_TXPOWER_Pos6dBm)
  234. #if defined(RADIO_TXPOWER_TXPOWER_Pos7dBm)
  235. RADIO_TXPOWER_TXPOWER_Pos7dBm,
  236. #endif // defined(RADIO_TXPOWER_TXPOWER_Pos7dBm)
  237. #if defined(RADIO_TXPOWER_TXPOWER_Pos8dBm)
  238. RADIO_TXPOWER_TXPOWER_Pos8dBm
  239. #endif // defined(RADIO_TXPOWER_TXPOWER_Pos8dBm)
  240. };
  241. #if DIRECTION_FINDING_SUPPORTED
  242. /**@brief Antenna pin array.
  243. */
  244. static const uint32_t m_antenna_pin[] = {
  245. NRF_RADIO_ANTENNA_PIN_1,
  246. NRF_RADIO_ANTENNA_PIN_2,
  247. NRF_RADIO_ANTENNA_PIN_3,
  248. NRF_RADIO_ANTENNA_PIN_4,
  249. NRF_RADIO_ANTENNA_PIN_5,
  250. NRF_RADIO_ANTENNA_PIN_6,
  251. NRF_RADIO_ANTENNA_PIN_7,
  252. NRF_RADIO_ANTENNA_PIN_8
  253. };
  254. /**@brief Received CTE IQ sample data.
  255. */
  256. static uint32_t m_cte_data[128];
  257. static void radio_gpio_pattern_clear(void)
  258. {
  259. NRF_RADIO->CLEARPATTERN = 1;
  260. }
  261. static void antenna_radio_pin_config(void)
  262. {
  263. for (uint8_t i = 0; i < ARRAY_SIZE(m_antenna_pin); i++)
  264. {
  265. NRF_RADIO->PSEL.DFEGPIO[i] = m_antenna_pin[i];
  266. }
  267. }
  268. static void switch_pattern_set(void)
  269. {
  270. // Set antenna for the guard period and for the reference period.
  271. NRF_RADIO->SWITCHPATTERN = 1;
  272. NRF_RADIO->SWITCHPATTERN = 1;
  273. switch (m_antenna_pattern)
  274. {
  275. case ANTENNA_PATTERN_123N123N:
  276. for (uint16_t i = 1; i <= m_antenna_number; i++)
  277. {
  278. NRF_RADIO->SWITCHPATTERN = i;
  279. }
  280. break;
  281. case ANTENNA_PATTERN_123N2123:
  282. for (uint16_t i = 1; i <= m_antenna_number; i++)
  283. {
  284. NRF_RADIO->SWITCHPATTERN = i;
  285. }
  286. for (uint16_t i = m_antenna_number - 1; i > 0; i--)
  287. {
  288. NRF_RADIO->SWITCHPATTERN = i;
  289. }
  290. break;
  291. }
  292. }
  293. static void radio_cte_prepare(bool rx)
  294. {
  295. if ((rx && (m_cte_mode == CTE_MODE_AOA)) || ((!rx) && (m_cte_mode == CTE_MODE_AOD)))
  296. {
  297. antenna_radio_pin_config();
  298. switch_pattern_set();
  299. // Set antenna switch spacing.
  300. NRF_RADIO->DFECTRL1 &= ~ RADIO_DFECTRL1_TSWITCHSPACING_Msk;
  301. NRF_RADIO->DFECTRL1 |= (m_cte_slot << RADIO_DFECTRL1_TSWITCHSPACING_Pos);
  302. }
  303. NRF_RADIO->DFEMODE = m_cte_mode;
  304. NRF_RADIO->PCNF0 |= (8 << RADIO_PCNF0_S1LEN_Pos);
  305. if (rx)
  306. {
  307. // Enable parsing CTEInfo from received packet.
  308. NRF_RADIO->CTEINLINECONF |= RADIO_CTEINLINECONF_CTEINLINECTRLEN_Enabled;
  309. NRF_RADIO->CTEINLINECONF |= (RADIO_CTEINLINECONF_CTEINFOINS1_InS1 << RADIO_CTEINLINECONF_CTEINFOINS1_Pos);
  310. // Set S0 Mask and Configuration to check if CP bit is set in received PDU.
  311. NRF_RADIO->CTEINLINECONF |= (0x20 << RADIO_CTEINLINECONF_S0CONF_Pos) |
  312. (0x20 << RADIO_CTEINLINECONF_S0MASK_Pos);
  313. NRF_RADIO->DFEPACKET.PTR = (uint32_t)m_cte_data;
  314. NRF_RADIO->DFEPACKET.MAXCNT = (uint16_t)sizeof(m_cte_data);
  315. }
  316. else
  317. {
  318. // Disable parsing CTEInfo from received packet.
  319. NRF_RADIO->CTEINLINECONF &= ~RADIO_CTEINLINECONF_CTEINLINECTRLEN_Enabled;
  320. NRF_RADIO->DFECTRL1 &= ~RADIO_DFECTRL1_NUMBEROF8US_Msk;
  321. NRF_RADIO->DFECTRL1 |= m_cte_time;
  322. }
  323. }
  324. #endif // DIRECTION_FINDING_SUPPORTED
  325. /**@brief Function for verifying that a received PDU has the expected structure and content.
  326. */
  327. static bool check_pdu(void)
  328. {
  329. uint8_t k; // Byte pointer for running through PDU payload
  330. uint8_t pattern; // Repeating octet value in payload
  331. dtm_pkt_type_t pdu_packet_type; // Note: PDU packet type is a 4-bit field in HCI, but 2 bits in BLE DTM
  332. uint32_t length = 0;
  333. uint8_t header_len;
  334. pdu_packet_type = (dtm_pkt_type_t)(m_pdu.content[DTM_HEADER_OFFSET] & 0x0F);
  335. length = m_pdu.content[DTM_LENGTH_OFFSET];
  336. #if DIRECTION_FINDING_SUPPORTED
  337. header_len = (m_cte_mode != CTE_MODE_OFF) ? DTM_HEADER_WITH_CTE_SIZE : DTM_HEADER_SIZE;
  338. #else
  339. header_len = DTM_HEADER_SIZE;
  340. #endif // DIRECTION_FINDING_SUPPORTED
  341. // Check that the length is valid.
  342. if (length > DTM_PAYLOAD_MAX_SIZE)
  343. {
  344. return false;
  345. }
  346. // If the 1Mbit or 2Mbit radio mode is active, check that one of the three valid uncoded DTM packet types are selected.
  347. if ((m_radio_mode == RADIO_MODE_MODE_Ble_1Mbit || m_radio_mode == RADIO_MODE_MODE_Ble_2Mbit) && (pdu_packet_type > (dtm_pkt_type_t)DTM_PKT_0X55))
  348. {
  349. return false;
  350. }
  351. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  352. // If a long range radio mode is active, check that one of the four valid coded DTM packet types are selected.
  353. if ((m_radio_mode == RADIO_MODE_MODE_Ble_LR500Kbit || m_radio_mode == RADIO_MODE_MODE_Ble_LR125Kbit) && (pdu_packet_type > (dtm_pkt_type_t)DTM_PKT_0XFF))
  354. {
  355. return false;
  356. }
  357. #endif //defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  358. if (pdu_packet_type == DTM_PKT_PRBS9)
  359. {
  360. // Payload does not consist of one repeated octet; must compare ir with entire block into
  361. return (memcmp(m_pdu.content + header_len, m_prbs_content, length) == 0);
  362. }
  363. if (pdu_packet_type == DTM_PKT_0X0F)
  364. {
  365. pattern = RFPHY_TEST_0X0F_REF_PATTERN;
  366. }
  367. else if (pdu_packet_type == DTM_PKT_0X55)
  368. {
  369. pattern = RFPHY_TEST_0X55_REF_PATTERN;
  370. }
  371. else if (pdu_packet_type == DTM_PKT_0XFF)
  372. {
  373. pattern = RFPHY_TEST_0XFF_REF_PATTERN;
  374. }
  375. else
  376. {
  377. // No valid packet type set.
  378. return false;
  379. }
  380. for (k = 0; k < length; k++)
  381. {
  382. // Check repeated pattern filling the PDU payload
  383. if (m_pdu.content[k + header_len] != pattern)
  384. {
  385. return false;
  386. }
  387. }
  388. #if DIRECTION_FINDING_SUPPORTED
  389. // Check CTEInfo and IQ sample cnt
  390. if (m_cte_mode != CTE_MODE_OFF)
  391. {
  392. uint8_t cte_info;
  393. uint8_t cte_sample_cnt;
  394. uint8_t expected_sample_cnt;
  395. cte_info = m_pdu.content[DTM_HEADER_CTEINFO_OFFSET];
  396. expected_sample_cnt = DTM_CTE_REF_SAMPLE_CNT + ((m_cte_time * 8)) /
  397. ((m_cte_slot == CTE_SLOT_1US) ? 2 : 4);
  398. cte_sample_cnt = NRF_RADIO->DFEPACKET.AMOUNT;
  399. memset(m_cte_data, 0, sizeof(m_cte_data));
  400. if ((cte_info != m_cte_info) || (expected_sample_cnt != cte_sample_cnt))
  401. {
  402. return false;
  403. }
  404. }
  405. #endif // DIRECTION_FINDING_SUPPORTED
  406. return true;
  407. }
  408. /**@brief Function for turning off the radio after a test.
  409. * Also called after test done, to be ready for next test.
  410. */
  411. static void radio_reset(void)
  412. {
  413. #if !defined(NRF21540_DRIVER_ENABLE) || (NRF21540_DRIVER_ENABLE == 0)
  414. NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk | PPI_CHENCLR_CH1_Msk;
  415. NRF_RADIO->SHORTS = 0;
  416. NRF_RADIO->EVENTS_DISABLED = 0;
  417. NRF_RADIO->TASKS_DISABLE = 1;
  418. while (NRF_RADIO->EVENTS_DISABLED == 0)
  419. {
  420. // Do nothing
  421. }
  422. NRF_RADIO->EVENTS_DISABLED = 0;
  423. NRF_RADIO->TASKS_RXEN = 0;
  424. NRF_RADIO->TASKS_TXEN = 0;
  425. #endif
  426. m_rx_pkt_count = 0;
  427. NRF_RADIO->PCNF0 &= ~RADIO_PCNF0_S1LEN_Msk;
  428. }
  429. /**@brief Function for initializing the radio for DTM.
  430. */
  431. static uint32_t radio_init(void)
  432. {
  433. if (dtm_radio_validate(m_tx_power, m_radio_mode) != DTM_SUCCESS)
  434. {
  435. m_event = LE_TEST_STATUS_EVENT_ERROR;
  436. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  437. }
  438. // Turn off radio before configuring it
  439. radio_reset();
  440. NRF_RADIO->TXPOWER = m_tx_power;
  441. NRF_RADIO->MODE = m_radio_mode << RADIO_MODE_MODE_Pos;
  442. // Set the access address, address0/prefix0 used for both Rx and Tx address
  443. NRF_RADIO->PREFIX0 &= ~RADIO_PREFIX0_AP0_Msk;
  444. NRF_RADIO->PREFIX0 |= (m_address >> 24) & RADIO_PREFIX0_AP0_Msk;
  445. NRF_RADIO->BASE0 = m_address << 8;
  446. NRF_RADIO->RXADDRESSES = RADIO_RXADDRESSES_ADDR0_Enabled << RADIO_RXADDRESSES_ADDR0_Pos;
  447. NRF_RADIO->TXADDRESS = (0x00 << RADIO_TXADDRESS_TXADDRESS_Pos) & RADIO_TXADDRESS_TXADDRESS_Msk;
  448. // Configure CRC calculation
  449. NRF_RADIO->CRCCNF = (m_crcConfSkipAddr << RADIO_CRCCNF_SKIP_ADDR_Pos) |
  450. (m_crcLength << RADIO_CRCCNF_LEN_Pos);
  451. if (m_radio_mode == RADIO_MODE_MODE_Ble_1Mbit || m_radio_mode == RADIO_MODE_MODE_Ble_2Mbit)
  452. {
  453. // Non-coded PHY
  454. NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
  455. (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
  456. (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
  457. (m_packetHeaderPlen << RADIO_PCNF0_PLEN_Pos);
  458. }
  459. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  460. else
  461. {
  462. // Coded PHY (Long range)
  463. NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
  464. (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
  465. (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
  466. (3 << RADIO_PCNF0_TERMLEN_Pos) |
  467. (2 << RADIO_PCNF0_CILEN_Pos) |
  468. (m_packetHeaderPlen << RADIO_PCNF0_PLEN_Pos);
  469. }
  470. #endif //defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  471. NRF_RADIO->PCNF1 = (m_whitening << RADIO_PCNF1_WHITEEN_Pos) |
  472. (m_endian << RADIO_PCNF1_ENDIAN_Pos) |
  473. (m_balen << RADIO_PCNF1_BALEN_Pos) |
  474. (m_static_length << RADIO_PCNF1_STATLEN_Pos) |
  475. (DTM_PAYLOAD_MAX_SIZE << RADIO_PCNF1_MAXLEN_Pos);
  476. return DTM_SUCCESS;
  477. }
  478. // Strict mode setting will be used only by devices affected by nRF52840 anomaly 172
  479. void set_strict_mode (bool enable)
  480. {
  481. uint8_t dbcCorrTh;
  482. uint8_t dsssMinPeakCount;
  483. if (enable == true)
  484. {
  485. dbcCorrTh = 0x7d;
  486. dsssMinPeakCount = 6;
  487. *(volatile uint32_t *) 0x4000173c = ((*((volatile uint32_t *) 0x4000173c)) & 0x7FFFFF00) | 0x80000000 | (((uint32_t)(dbcCorrTh)) << 0);
  488. *(volatile uint32_t *) 0x4000177c = ((*((volatile uint32_t *) 0x4000177c)) & 0x7FFFFF8F) | 0x80000000 | ((((uint32_t)dsssMinPeakCount) & 0x00000007) << 4);
  489. }
  490. else
  491. {
  492. *(volatile uint32_t *) 0x4000173c = 0x40003034;
  493. *(volatile uint32_t *) 0x4000177c = ((*((volatile uint32_t *) 0x4000177c)) & 0x7FFFFFFF); // Unset override of dsssMinPeakCount
  494. }
  495. m_strict_mode = enable;
  496. }
  497. // Radio configuration used as a workaround for nRF52840 anomaly 172
  498. void anomaly_172_radio_operation(void)
  499. {
  500. *(volatile uint32_t *) 0x40001040 = 1;
  501. *(volatile uint32_t *) 0x40001038 = 1;
  502. }
  503. // Function to gather RSSI data and set strict mode accordingly. Used as part of the workaround for nRF52840 anomaly 172
  504. uint8_t anomaly_172_rssi_check(void)
  505. {
  506. NRF_RADIO->EVENTS_RSSIEND = 0;
  507. NRF_RADIO->TASKS_RSSISTART = 1;
  508. while (NRF_RADIO->EVENTS_RSSIEND == 0);
  509. uint8_t rssi = NRF_RADIO->RSSISAMPLE;
  510. return rssi;
  511. }
  512. /**@brief Function for preparing the radio. At start of each test: Turn off RF, clear interrupt flags of RF, initialize the radio
  513. * at given RF channel.
  514. *
  515. *@param[in] rx boolean indicating if radio should be prepared in rx mode (true) or tx mode.
  516. */
  517. static void radio_prepare(bool rx)
  518. {
  519. dtm_turn_off_test();
  520. #if DIRECTION_FINDING_SUPPORTED
  521. if (m_cte_mode != CTE_MODE_OFF)
  522. {
  523. radio_cte_prepare(rx);
  524. }
  525. #endif // DIRECTION_FINDING_SUPPORTED
  526. NRF_RADIO->CRCPOLY = m_crc_poly;
  527. NRF_RADIO->CRCINIT = m_crc_init;
  528. NRF_RADIO->FREQUENCY = (m_phys_ch << 1) + 2; // Actual frequency (MHz): 2400 + register value
  529. NRF_RADIO->PACKETPTR = (uint32_t)&m_pdu; // Setting packet pointer will start the radio
  530. NRF_RADIO->EVENTS_READY = 0;
  531. #if !defined(NRF21540_DRIVER_ENABLE) || (NRF21540_DRIVER_ENABLE == 0)
  532. NRF_RADIO->SHORTS = (1 << RADIO_SHORTS_READY_START_Pos); // Shortcut between READY event and START task
  533. #if DIRECTION_FINDING_SUPPORTED
  534. if (m_cte_mode != CTE_MODE_OFF)
  535. {
  536. NRF_RADIO->SHORTS |= (1 << RADIO_SHORTS_PHYEND_DISABLE_Pos); // Shortcut between PHY_END event and DISABLE task
  537. }
  538. else
  539. {
  540. NRF_RADIO->SHORTS |= (1 << RADIO_SHORTS_END_DISABLE_Pos); // Shortcut between END event and DISABLE task
  541. }
  542. #else
  543. NRF_RADIO->SHORTS |= (1 << RADIO_SHORTS_END_DISABLE_Pos); // Shortcut between END event and DISABLE task
  544. #endif // DIRECTION_FINDING_SUPPORTED
  545. #endif // !defined(NRF21540_DRIVER_ENABLE) || (NRF21540_DRIVER_ENABLE == 0)
  546. if (rx)
  547. {
  548. // Enable strict mode if running on a device affected by nRF52840 anomaly 172
  549. if (anomaly_172_wa_enabled)
  550. {
  551. set_strict_mode(1);
  552. }
  553. NRF_RADIO->EVENTS_END = 0;
  554. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  555. (void)nrf21540_rx_set(NRF21540_EXECUTE_NOW, NRF21540_EXEC_MODE_NON_BLOCKING);
  556. #else
  557. NRF_RADIO->TASKS_RXEN = 1; // shorts will start radio in RX mode when it is ready
  558. #endif
  559. }
  560. else // tx
  561. {
  562. NRF_RADIO->TXPOWER = m_tx_power & RADIO_TXPOWER_TXPOWER_Msk;
  563. // Stop the timer used by nRF52840 anomaly 172 if running on an affected device.
  564. if (anomaly_172_wa_enabled)
  565. {
  566. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  567. ANOMALY_172_TIMER->TASKS_STOP = 1;
  568. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  569. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  570. }
  571. }
  572. }
  573. /**@brief Function for terminating the ongoing test (if any) and closing down the radio.
  574. */
  575. static void dtm_test_done(void)
  576. {
  577. dtm_turn_off_test();
  578. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  579. (void)nrf21540_power_down(NRF21540_EXECUTE_NOW, NRF21540_EXEC_MODE_BLOCKING);
  580. #else
  581. NRF_PPI->CHENCLR = 0x01;
  582. NRF_PPI->CH[0].EEP = 0; // Break connection from timer to radio to stop transmit loop
  583. NRF_PPI->CH[0].TEP = 0;
  584. #endif
  585. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  586. radio_reset();
  587. m_state = STATE_IDLE;
  588. }
  589. /**@brief Function for configuring the timer for 625us cycle time.
  590. */
  591. static uint32_t timer_init(void)
  592. {
  593. // Use 16MHz from external crystal
  594. // This could be customized for RC/Xtal, or even to use a 32 kHz crystal
  595. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  596. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  597. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  598. {
  599. // Do nothing while waiting for the clock to start
  600. }
  601. mp_timer->TASKS_STOP = 1; // Stop timer, if it was running
  602. mp_timer->TASKS_CLEAR = 1;
  603. mp_timer->MODE = TIMER_MODE_MODE_Timer; // Timer mode (not counter)
  604. mp_timer->EVENTS_COMPARE[0] = 0; // clean up possible old events
  605. mp_timer->EVENTS_COMPARE[1] = 0;
  606. mp_timer->EVENTS_COMPARE[2] = 0;
  607. mp_timer->EVENTS_COMPARE[3] = 0;
  608. // Timer is polled, but enable the compare0 interrupt in order to wakeup from CPU sleep
  609. mp_timer->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
  610. mp_timer->SHORTS = 1 << TIMER_SHORTS_COMPARE0_CLEAR_Pos; // Clear the count every time timer reaches the CCREG0 count
  611. mp_timer->PRESCALER = 4; // Input clock is 16MHz, timer clock = 2 ^ prescale -> interval 1us
  612. mp_timer->CC[0] = m_txIntervaluS; // 625uS with 1MHz clock to the timer
  613. mp_timer->CC[1] = UART_POLL_CYCLE; // Depends on the baud rate of the UART. Default baud rate of 19200 will result in a 260uS time with 1MHz clock to the timer
  614. mp_timer->TASKS_START = 1; // Start the timer - it will be running continuously
  615. m_current_time = 0;
  616. // Enable the timer used by nRF52840 anomaly 172 if running on an affected device.
  617. if (true)
  618. {
  619. ANOMALY_172_TIMER->TASKS_STOP = 1; // Stop timer, if it was running
  620. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  621. ANOMALY_172_TIMER->MODE = TIMER_MODE_MODE_Timer; // Timer mode (not counter)
  622. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0; // clean up possible old events
  623. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  624. ANOMALY_172_TIMER->EVENTS_COMPARE[2] = 0;
  625. ANOMALY_172_TIMER->EVENTS_COMPARE[3] = 0;
  626. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  627. ANOMALY_172_TIMER->CC[1] = 0;
  628. NVIC_ClearPendingIRQ(ANOMALY_172_TIMER_IRQn);
  629. ANOMALY_172_TIMER->PRESCALER = 7; // Input clock is 16MHz, timer clock = 2 ^ prescale -> interval 1us
  630. }
  631. return DTM_SUCCESS;
  632. }
  633. /**@brief Function for handling vendor specific commands.
  634. * Used when packet type is set to Vendor specific.
  635. * The length field is used for encoding vendor specific command.
  636. * The frequency field is used for encoding vendor specific options to the command.
  637. *
  638. * @param[in] vendor_cmd Vendor specific command to be executed.
  639. * @param[in] vendor_option Vendor specific option to the vendor command.
  640. *
  641. * @return DTM_SUCCESS or one of the DTM_ERROR_ values
  642. */
  643. static uint32_t dtm_vendor_specific_pkt(uint32_t vendor_cmd, dtm_freq_t vendor_option)
  644. {
  645. switch (vendor_cmd)
  646. {
  647. // nRFgo Studio uses CARRIER_TEST_STUDIO to indicate a continuous carrier without
  648. // a modulated signal.
  649. case CARRIER_TEST:
  650. case CARRIER_TEST_STUDIO:
  651. // Not a packet type, but used to indicate that a continuous carrier signal
  652. // should be transmitted by the radio.
  653. radio_prepare(TX_MODE);
  654. dtm_constant_carrier();
  655. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  656. (void)nrf21540_tx_set(NRF21540_EXECUTE_NOW, NRF21540_EXEC_MODE_NON_BLOCKING);
  657. #else
  658. // Shortcut between READY event and START task
  659. NRF_RADIO->SHORTS = 1 << RADIO_SHORTS_READY_START_Pos;
  660. // Shortcut will start radio in Tx mode when it is ready
  661. NRF_RADIO->TASKS_TXEN = 1;
  662. #endif // defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  663. m_state = STATE_CARRIER_TEST;
  664. break;
  665. case SET_TX_POWER:
  666. if (!dtm_set_txpower(vendor_option))
  667. {
  668. m_event = LE_TEST_STATUS_EVENT_ERROR;
  669. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  670. }
  671. break;
  672. case SELECT_TIMER:
  673. if (!dtm_set_timer(vendor_option))
  674. {
  675. m_event = LE_TEST_STATUS_EVENT_ERROR;
  676. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  677. }
  678. break;
  679. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  680. case SET_NRF21540_TX_POWER:
  681. if(!dtm_set_nrf21450_power_mode((dtm_nrf21540_power_mode_t)vendor_option))
  682. {
  683. m_event = LE_TEST_STATUS_EVENT_ERROR;
  684. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  685. }
  686. break;
  687. #endif // defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  688. }
  689. // Event code is unchanged, successful
  690. return DTM_SUCCESS;
  691. }
  692. static uint32_t dtm_packet_interval_calculate(uint32_t test_payload_length, uint32_t mode)
  693. {
  694. uint32_t test_packet_length = 0; // [us] NOTE: bits are us at 1Mbit
  695. uint32_t packet_interval = 0; // us
  696. uint32_t overhead_bits = 0; // bits
  697. /* packet overhead
  698. * see BLE [Vol 6, Part F] page 213
  699. * 4.1 LE TEST PACKET FORMAT */
  700. if (mode == RADIO_MODE_MODE_Ble_2Mbit)
  701. {
  702. // 16 preamble
  703. // 32 sync word
  704. // 8 PDU header, actually packetHeaderS0len * 8
  705. // 8 PDU length, actually packetHeaderLFlen
  706. // 24 CRC
  707. overhead_bits = 88; // 11 bytes
  708. }
  709. else if (mode == RADIO_MODE_MODE_Ble_1Mbit)
  710. {
  711. // 8 preamble
  712. // 32 sync word
  713. // 8 PDU header, actually packetHeaderS0len * 8
  714. // 8 PDU length, actually packetHeaderLFlen
  715. // 24 CRC
  716. overhead_bits = 80; // 10 bytes
  717. }
  718. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  719. else if (mode == RADIO_MODE_MODE_Ble_LR125Kbit)
  720. {
  721. // 80 preamble
  722. // 32 * 8 sync word coding=8
  723. // 2 * 8 Coding indicator, coding=8
  724. // 3 * 8 TERM1 coding=8
  725. // 8 * 8 PDU header, actually packetHeaderS0len * 8 coding=8
  726. // 8 * 8 PDU length, actually packetHeaderLFlen coding=8
  727. // 24 * 8 CRC coding=8
  728. // 3 * 8 TERM2 coding=8
  729. overhead_bits = 720; // 90 bytes
  730. }
  731. else if (mode == RADIO_MODE_MODE_Ble_LR500Kbit)
  732. {
  733. // 80 preamble
  734. // 32 * 8 sync word coding=8
  735. // 2 * 8 Coding indicator, coding=8
  736. // 3 * 8 TERM 1 coding=8
  737. // 8 * 2 PDU header, actually packetHeaderS0len * 8 coding=2
  738. // 8 * 2 PDU length, actually packetHeaderLFlen coding=2
  739. // 24 * 2 CRC coding=2
  740. // 3 * 2 TERM2 coding=2
  741. // NOTE: this makes us clock out 46 bits for CI + TERM1 + TERM2
  742. // assumption the radio will handle this
  743. overhead_bits = 462; // 57.75 bytes
  744. }
  745. #endif //defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  746. /* add PDU payload test_payload length */
  747. test_packet_length = (test_payload_length * 8); // in bits
  748. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  749. // account for the encoding of PDU
  750. if (mode == RADIO_MODE_MODE_Ble_LR125Kbit)
  751. {
  752. test_packet_length *= 8; // 1 to 8 encoding
  753. }
  754. if (mode == RADIO_MODE_MODE_Ble_LR500Kbit)
  755. {
  756. test_packet_length *= 2; // 1 to 2 encoding
  757. }
  758. #endif //defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  759. // add overhead calculated above
  760. test_packet_length += overhead_bits;
  761. // we remember this bits are us in 1Mbit
  762. if (mode == RADIO_MODE_MODE_Ble_2Mbit)
  763. {
  764. test_packet_length /= 2; // double speed
  765. }
  766. #if DIRECTION_FINDING_SUPPORTED
  767. if (m_cte_mode != CTE_MODE_OFF)
  768. {
  769. // Add 8 - bit S1 field with CTEInfo.
  770. ((test_packet_length += mode) == RADIO_MODE_MODE_Ble_1Mbit) ? 8 : 4;
  771. // Add CTE length in us to test packet length.
  772. test_packet_length += m_cte_time * NRF_CTE_TIME_IN_US;
  773. }
  774. #endif // DIRECTION_FINDING_SUPPORTED
  775. /*
  776. * packet_interval = ceil((test_packet_length+249)/625)*625
  777. * NOTE: To avoid floating point an equivalent calculation is used.
  778. */
  779. uint32_t i = 0;
  780. uint32_t timeout = 0;
  781. do
  782. {
  783. i++;
  784. timeout = i * 625;
  785. } while (test_packet_length + 249 > timeout);
  786. packet_interval = i * 625;
  787. return packet_interval;
  788. }
  789. static uint32_t phy_set(uint8_t phy)
  790. {
  791. if ((phy >= LE_PHY_1M_MIN_RANGE) && (phy <= LE_PHY_1M_MAX_RANGE))
  792. {
  793. m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit;
  794. m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit;
  795. #ifdef NRF52840_XXAA
  796. // Workaround for Errata ID 191
  797. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  798. #endif
  799. // Disable the workaround for nRF52840 anomaly 172.
  800. set_strict_mode(0);
  801. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  802. anomaly_172_wa_enabled = false;
  803. return radio_init();
  804. }
  805. else if ((phy >= LE_PHY_2M_MIN_RANGE) && (phy <= LE_PHY_2M_MAX_RANGE))
  806. {
  807. m_radio_mode = RADIO_MODE_MODE_Ble_2Mbit;
  808. m_packetHeaderPlen = RADIO_PCNF0_PLEN_16bit;
  809. #ifdef NRF52840_XXAA
  810. // Workaround for Errata ID 191
  811. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  812. #endif
  813. // Disable the workaround for nRF52840 anomaly 172.
  814. set_strict_mode(0);
  815. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  816. anomaly_172_wa_enabled = false;
  817. return radio_init();
  818. }
  819. else if ((phy >= LE_PHY_LE_CODED_S8_MIN_RANGE) && (phy <= LE_PHY_LE_CODED_S8_MAX_RANGE))
  820. {
  821. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  822. m_radio_mode = RADIO_MODE_MODE_Ble_LR125Kbit;
  823. m_packetHeaderPlen = RADIO_PCNF0_PLEN_LongRange;
  824. #ifdef NRF52840_XXAA
  825. // Workaround for Errata ID 191
  826. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFF00FF) | 0x80000000 | (((uint32_t)(196)) << 8);
  827. // Enable the workaround for nRF52840 anomaly 172 on affected devices.
  828. if ((*(volatile uint32_t *)0x40001788) == 0)
  829. {
  830. anomaly_172_wa_enabled = true;
  831. }
  832. #endif //NRF52840_XXAA
  833. return radio_init();
  834. #else
  835. m_event = LE_TEST_STATUS_EVENT_ERROR;
  836. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  837. #endif //defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  838. }
  839. else if ((phy >= LE_PHY_LE_CODED_S2_MIN_RANGE) && (phy <= LE_PHY_LE_CODED_S2_MAX_RANGE))
  840. {
  841. #if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(NRF52811_XXAA) || defined(NRF52820_XXAA)
  842. m_radio_mode = RADIO_MODE_MODE_Ble_LR500Kbit;
  843. m_packetHeaderPlen = RADIO_PCNF0_PLEN_LongRange;
  844. #ifdef NRF52840_XXAA
  845. // Workaround for Errata ID 191
  846. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFF00FF) | 0x80000000 | (((uint32_t)(196)) << 8);
  847. // Enable the workaround for nRF52840 anomaly 172 on affected devices.
  848. if ((*(volatile uint32_t *)0x40001788) == 0)
  849. {
  850. anomaly_172_wa_enabled = true;
  851. }
  852. #endif //NRF52840_XXAA
  853. return radio_init();
  854. #else
  855. m_event = LE_TEST_STATUS_EVENT_ERROR;
  856. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  857. #endif
  858. }
  859. else
  860. {
  861. m_event = LE_TEST_STATUS_EVENT_ERROR;
  862. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  863. }
  864. }
  865. static uint32_t modulation_set(uint8_t modulation)
  866. {
  867. // Only standard modulation is supported.
  868. if (modulation > LE_MODULATION_INDEX_STANDARD_MAX_RANGE)
  869. {
  870. m_event = LE_TEST_STATUS_EVENT_ERROR;
  871. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  872. }
  873. return DTM_SUCCESS;
  874. }
  875. static uint32_t feature_read(uint8_t cmd)
  876. {
  877. if (cmd > LE_TEST_FEATURE_READ_MAX_RANGE)
  878. {
  879. m_event = LE_TEST_STATUS_EVENT_ERROR;
  880. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  881. }
  882. m_event = DTM_SUPPORTED_FEATURE;
  883. return DTM_SUCCESS;
  884. }
  885. static uint32_t maximum_supported_value_read(uint8_t parameter)
  886. {
  887. // Read supportedMaxTxOctets
  888. if (parameter <= LE_TEST_SUPPORTED_TX_OCTETS_MAX_RANGE)
  889. {
  890. m_event = NRF_MAX_PAYLOAD_OCTETS << DTM_RESPONSE_EVENT_SHIFT;
  891. }
  892. // Read supportedMaxTxTime
  893. else if ((parameter >= LE_TEST_SUPPORTED_TX_TIME_MIN_RANGE) &&
  894. (parameter <= LE_TEST_SUPPORTED_TX_TIME_MAX_RANGE))
  895. {
  896. m_event = NRF_MAX_RX_TX_TIME << DTM_RESPONSE_EVENT_SHIFT;
  897. }
  898. // Read supportedMaxRxOctets
  899. else if ((parameter >= LE_TEST_SUPPORTED_RX_OCTETS_MIN_RANGE) &&
  900. (parameter <= LE_TEST_SUPPORTED_RX_OCTETS_MAX_RANGE))
  901. {
  902. m_event = NRF_MAX_PAYLOAD_OCTETS << DTM_RESPONSE_EVENT_SHIFT;
  903. }
  904. // Read supportedMaxRxTime
  905. else if ((parameter >= LE_TEST_SUPPORTED_RX_TIME_MIN_RANGE) &&
  906. (parameter <= LE_TEST_SUPPORTED_RX_TIME_MAX_RANGE))
  907. {
  908. m_event = NRF_MAX_RX_TX_TIME << DTM_RESPONSE_EVENT_SHIFT;
  909. }
  910. #if DIRECTION_FINDING_SUPPORTED
  911. // Read maximum length of Constant Tone Extension
  912. else if (parameter == LE_TEST_SUPPORTED_CTE_LENGTH)
  913. {
  914. m_event = NRF_CTE_MAX_LENGTH << DTM_RESPONSE_EVENT_SHIFT;
  915. }
  916. #endif // DIRECTION_FINDING_SUPPORTED
  917. else
  918. {
  919. m_event = LE_TEST_STATUS_EVENT_ERROR;
  920. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  921. }
  922. return DTM_SUCCESS;
  923. }
  924. static uint32_t transmit_power_set(int8_t parameter)
  925. {
  926. if (parameter == LE_TRANSMIT_POWER_LVL_SET_MIN)
  927. {
  928. m_tx_power = nrf_power_value[0];
  929. m_event = ((m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  930. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK) |
  931. LE_TRANSMIT_POWER_MIN_LVL_BIT;
  932. return DTM_SUCCESS;
  933. }
  934. if (parameter == LE_TRANSMIT_POWER_LVL_SET_MAX)
  935. {
  936. m_tx_power = nrf_power_value[ARRAY_SIZE(nrf_power_value) - 1];
  937. m_event = ((m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  938. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK) |
  939. LE_TRANSMIT_POWER_MAX_LVL_BIT;
  940. return DTM_SUCCESS;
  941. }
  942. if (parameter < LE_TRANSMIT_POWER_LVL_MIN || parameter > LE_TRANSMIT_POWER_LVL_MAX)
  943. {
  944. m_event = ((m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  945. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK) |
  946. LE_TEST_STATUS_EVENT_ERROR;
  947. if (m_tx_power == nrf_power_value[0])
  948. {
  949. m_event |= LE_TRANSMIT_POWER_MIN_LVL_BIT;
  950. }
  951. else if (m_tx_power == nrf_power_value[ARRAY_SIZE(nrf_power_value) - 1])
  952. {
  953. m_event |= LE_TRANSMIT_POWER_MAX_LVL_BIT;
  954. }
  955. else
  956. {
  957. // Do nothing.
  958. }
  959. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  960. }
  961. if (parameter <= ((int8_t) nrf_power_value[0]))
  962. {
  963. m_tx_power = nrf_power_value[0];
  964. m_event = ((m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  965. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK) |
  966. LE_TRANSMIT_POWER_MIN_LVL_BIT;
  967. return DTM_SUCCESS;
  968. }
  969. if (parameter >= ((int8_t) nrf_power_value[ARRAY_SIZE(nrf_power_value) - 1]))
  970. {
  971. m_tx_power = nrf_power_value[ARRAY_SIZE(nrf_power_value) - 1];
  972. m_event = ((m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  973. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK) |
  974. LE_TRANSMIT_POWER_MAX_LVL_BIT;
  975. return DTM_SUCCESS;
  976. }
  977. // Look for the nearest tansmit power level and set it.
  978. for (uint8_t i = 1; i < ARRAY_SIZE(nrf_power_value); i++)
  979. {
  980. if (((int8_t) nrf_power_value[i]) > parameter)
  981. {
  982. int8_t diff = abs((int8_t) nrf_power_value[i] - parameter);
  983. if (diff < abs((int8_t) nrf_power_value[i - 1] - parameter))
  984. {
  985. m_tx_power = nrf_power_value[i];
  986. }
  987. else
  988. {
  989. m_tx_power = nrf_power_value[i - 1];
  990. }
  991. break;
  992. }
  993. }
  994. m_event = (m_tx_power << LE_TRANSMIT_POWER_RESPONSE_LVL_POS) &
  995. LE_TRANSMIT_POWER_RESPONSE_LVL_MASK;
  996. return DTM_SUCCESS;
  997. }
  998. #if DIRECTION_FINDING_SUPPORTED
  999. static uint32_t constant_tone_setup(uint8_t cte_info)
  1000. {
  1001. uint8_t type = (cte_info >> LE_CTE_TYPE_POS) & LE_CTE_TYPE_MASK;
  1002. m_cte_time = cte_info & LE_CTE_CTETIME_MASK;
  1003. m_cte_info = cte_info;
  1004. if ((m_cte_time < LE_CTE_LENGTH_MIN) || (m_cte_time > LE_CTE_LENGTH_MAX))
  1005. {
  1006. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1007. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1008. }
  1009. switch (type)
  1010. {
  1011. case LE_CTE_TYPE_AOA:
  1012. m_cte_mode = CTE_MODE_AOA;
  1013. break;
  1014. case LE_CTE_TYPE_AOD_1US:
  1015. m_cte_mode = CTE_MODE_AOD;
  1016. m_cte_slot = CTE_SLOT_1US;
  1017. break;
  1018. case LE_CTE_TYPE_AOD_2US:
  1019. m_cte_mode = CTE_MODE_AOD;
  1020. m_cte_slot = CTE_SLOT_2US;
  1021. break;
  1022. default:
  1023. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1024. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1025. }
  1026. return DTM_SUCCESS;
  1027. }
  1028. #else
  1029. static uint32_t constant_tone_setup(uint8_t cte_info)
  1030. {
  1031. UNUSED_PARAMETER(cte_info);
  1032. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1033. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1034. }
  1035. #endif // DIRECTION_FINDING_SUPPORTED
  1036. #if DIRECTION_FINDING_SUPPORTED
  1037. static uint32_t constant_tone_slot_set(uint8_t cte_slot)
  1038. {
  1039. if (cte_slot == LE_CTE_TYPE_AOD_1US)
  1040. {
  1041. m_cte_slot = CTE_SLOT_1US;
  1042. return DTM_SUCCESS;
  1043. }
  1044. if (cte_slot == LE_CTE_TYPE_AOD_2US)
  1045. {
  1046. m_cte_slot = CTE_SLOT_2US;
  1047. return DTM_SUCCESS;
  1048. }
  1049. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1050. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1051. }
  1052. #else
  1053. static uint32_t constant_tone_slot_set(uint8_t cte_slot)
  1054. {
  1055. UNUSED_PARAMETER(cte_slot);
  1056. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1057. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1058. }
  1059. #endif // DIRECTION_FINDING_SUPPORTED
  1060. #if DIRECTION_FINDING_SUPPORTED
  1061. static uint32_t antenna_set(uint8_t antenna)
  1062. {
  1063. m_antenna_number = antenna & LE_ANTENNA_NUMBER_MASK;
  1064. m_antenna_pattern = (antenna_pattern_t)(antenna & LE_ANTENA_SWITCH_PATTERN_MASK);
  1065. if ((m_antenna_number < LE_TEST_ANTENNA_NUMBER_MIN) ||
  1066. (m_antenna_number > LE_TEST_ANTENNA_NUMBER_MAX) ||
  1067. (m_antenna_number > NRF_RADIO_ANTENNA_COUNT))
  1068. {
  1069. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1070. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1071. }
  1072. return DTM_SUCCESS;
  1073. }
  1074. #else
  1075. static uint32_t antenna_set(uint8_t antenna)
  1076. {
  1077. UNUSED_PARAMETER(antenna);
  1078. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1079. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1080. }
  1081. #endif // DIRECTION_FINDING_SUPPORTED
  1082. static uint32_t on_test_setup_cmd(uint8_t control, uint8_t parameter)
  1083. {
  1084. // Note that timer will continue running after a reset
  1085. dtm_test_done();
  1086. switch (control)
  1087. {
  1088. case LE_TEST_SETUP_RESET:
  1089. if (parameter > LE_RESET_MAX_RANGE)
  1090. {
  1091. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1092. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1093. }
  1094. // Reset the packet length upper bits.
  1095. m_packet_length = 0;
  1096. // Reset the selected PHY to 1Mbit
  1097. m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit;
  1098. m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit;
  1099. #if DIRECTION_FINDING_SUPPORTED
  1100. m_cte_mode = CTE_MODE_OFF;
  1101. radio_gpio_pattern_clear();
  1102. #endif // DIRECTION_FINDING_SUPPORTED
  1103. #ifdef NRF52840_XXAA
  1104. // Workaround for Errata ID 191
  1105. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  1106. #endif
  1107. break;
  1108. case LE_TEST_SETUP_SET_UPPER:
  1109. if (parameter > LE_SET_UPER_BITS_MAX_RANGE)
  1110. {
  1111. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1112. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1113. }
  1114. m_packet_length = (parameter && LE_UPPER_BITS_MASK) << LE_UPPER_BITS_POS;
  1115. break;
  1116. case LE_TEST_SETUP_SET_PHY:
  1117. return phy_set(parameter);
  1118. case LE_TEST_SETUP_SELECT_MODULATION:
  1119. return modulation_set(parameter);
  1120. case LE_TEST_SETUP_READ_SUPPORTED:
  1121. return feature_read(parameter);
  1122. case LE_TEST_SETUP_READ_MAX:
  1123. return maximum_supported_value_read(parameter);
  1124. case LE_TEST_SETUP_TRANSMIT_POWER:
  1125. return transmit_power_set(parameter);
  1126. case LE_TEST_SETUP_CONSTANT_TONE:
  1127. return constant_tone_setup(parameter);
  1128. case LE_TEST_SETUP_CONSTANT_TONE_SLOT:
  1129. return constant_tone_slot_set(parameter);
  1130. case LE_TEST_SETUP_ANTENNA_ARRAY:
  1131. return antenna_set(parameter);
  1132. default:
  1133. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1134. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1135. }
  1136. return DTM_SUCCESS;
  1137. }
  1138. static uint32_t on_test_end_cmd(void)
  1139. {
  1140. if (m_state == STATE_IDLE)
  1141. {
  1142. // Sequencing error - only rx or tx test may be ended!
  1143. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1144. return DTM_ERROR_INVALID_STATE;
  1145. }
  1146. m_event = LE_PACKET_REPORTING_EVENT | m_rx_pkt_count;
  1147. dtm_test_done();
  1148. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1149. (void) nrf21540_power_down(NRF21540_EXECUTE_NOW, NRF21540_EXEC_MODE_BLOCKING);
  1150. #endif
  1151. return DTM_SUCCESS;
  1152. }
  1153. static uint32_t on_test_transmit_cmd(uint32_t length, dtm_freq_t freq)
  1154. {
  1155. uint8_t header_len;
  1156. // Check for illegal values of m_packet_length. Skip the check if the packet is vendor spesific.
  1157. if (m_packet_type != DTM_PKT_TYPE_VENDORSPECIFIC && m_packet_length > DTM_PAYLOAD_MAX_SIZE)
  1158. {
  1159. // Parameter error
  1160. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1161. return DTM_ERROR_ILLEGAL_LENGTH;
  1162. }
  1163. #if DIRECTION_FINDING_SUPPORTED
  1164. header_len = (m_cte_mode != CTE_MODE_OFF) ? DTM_HEADER_WITH_CTE_SIZE : DTM_HEADER_SIZE;
  1165. #else
  1166. header_len = DTM_HEADER_SIZE;
  1167. #endif // DIRECTION_FINDING_SUPPORTED
  1168. m_pdu.content[DTM_LENGTH_OFFSET] = m_packet_length;
  1169. // Note that PDU uses 4 bits even though BLE DTM uses only 2 (the HCI SDU uses all 4)
  1170. switch (m_packet_type)
  1171. {
  1172. case DTM_PKT_PRBS9:
  1173. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_PRBS9;
  1174. // Non-repeated, must copy entire pattern to PDU
  1175. memcpy(m_pdu.content + header_len, m_prbs_content, m_packet_length);
  1176. break;
  1177. case DTM_PKT_0X0F:
  1178. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0X0F;
  1179. // Bit pattern 00001111 repeated
  1180. memset(m_pdu.content + header_len, RFPHY_TEST_0X0F_REF_PATTERN, m_packet_length);
  1181. break;
  1182. case DTM_PKT_0X55:
  1183. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0X55;
  1184. // Bit pattern 01010101 repeated
  1185. memset(m_pdu.content + header_len, RFPHY_TEST_0X55_REF_PATTERN, m_packet_length);
  1186. break;
  1187. case DTM_PKT_0XFF:
  1188. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0XFF;
  1189. // Bit pattern 11111111 repeated. Only available in coded PHY (Long range).
  1190. memset(m_pdu.content + header_len, RFPHY_TEST_0XFF_REF_PATTERN, m_packet_length);
  1191. break;
  1192. case DTM_PKT_TYPE_VENDORSPECIFIC:
  1193. // The length field is for indicating the vendor specific command to execute.
  1194. // The frequency field is used for vendor specific options to the command.
  1195. return dtm_vendor_specific_pkt(length, freq);
  1196. default:
  1197. // Parameter error
  1198. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1199. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1200. }
  1201. #if DIRECTION_FINDING_SUPPORTED
  1202. if (m_cte_mode != CTE_MODE_OFF)
  1203. {
  1204. m_pdu.content[DTM_HEADER_OFFSET] |= DTM_PKT_CP_BIT;
  1205. m_pdu.content[DTM_HEADER_CTEINFO_OFFSET] = m_cte_info;
  1206. }
  1207. #endif // DIRECTION_FINDING_SUPPORTED
  1208. // Initialize CRC value, set channel:
  1209. radio_prepare(TX_MODE);
  1210. // Set the timer to the correct period. The delay between each packet is described in the
  1211. // Bluetooth Core Specification version 4.2 Vol. 6 Part F Section 4.1.6.
  1212. mp_timer->CC[0] = dtm_packet_interval_calculate(m_packet_length, m_radio_mode);
  1213. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1214. (void)nrf21540_tx_set((uint32_t)&mp_timer->EVENTS_COMPARE[0],
  1215. NRF21540_EXEC_MODE_NON_BLOCKING);
  1216. #else
  1217. // Configure PPI so that timer will activate radio every 625 us
  1218. NRF_PPI->CH[0].EEP = (uint32_t)&mp_timer->EVENTS_COMPARE[0];
  1219. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_RADIO->TASKS_TXEN;
  1220. NRF_PPI->CHENSET = 0x01;
  1221. #endif
  1222. m_state = STATE_TRANSMITTER_TEST;
  1223. return DTM_SUCCESS;
  1224. }
  1225. static uint32_t on_test_receive_cmd(void)
  1226. {
  1227. // Zero fill all pdu fields to avoid stray data from earlier test run
  1228. memset(&m_pdu, 0, DTM_PDU_MAX_MEMORY_SIZE);
  1229. // Reinitialize "everything"; RF interrupts OFF
  1230. radio_prepare(RX_MODE);
  1231. m_state = STATE_RECEIVER_TEST;
  1232. return DTM_SUCCESS;
  1233. }
  1234. uint32_t dtm_init(void)
  1235. {
  1236. if ((timer_init() != DTM_SUCCESS) || (radio_init() != DTM_SUCCESS))
  1237. {
  1238. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  1239. }
  1240. m_new_event = false;
  1241. m_state = STATE_IDLE;
  1242. m_packet_length = 0;
  1243. #if defined(NRF52832_XXAA) || defined(NRF52840_XXAA) || defined(NRF52833_XXAA)
  1244. // Enable cache
  1245. NRF_NVMC->ICACHECNF = (NVMC_ICACHECNF_CACHEEN_Enabled << NVMC_ICACHECNF_CACHEEN_Pos) & NVMC_ICACHECNF_CACHEEN_Msk;
  1246. #endif
  1247. return DTM_SUCCESS;
  1248. }
  1249. uint32_t dtm_wait(void)
  1250. {
  1251. for (;;)
  1252. {
  1253. if (m_state == STATE_RECEIVER_TEST && NRF_RADIO->EVENTS_ADDRESS == 1)
  1254. {
  1255. NRF_RADIO->EVENTS_ADDRESS = 0;
  1256. if (anomaly_172_wa_enabled)
  1257. {
  1258. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  1259. }
  1260. }
  1261. // Event may be the reception of a packet -
  1262. // handle radio first, to give it highest priority:
  1263. if (NRF_RADIO->EVENTS_END != 0)
  1264. {
  1265. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1266. if (m_state != STATE_CARRIER_TEST)
  1267. {
  1268. (void)nrf21540_power_down(NRF21540_EXECUTE_NOW, NRF21540_EXEC_MODE_BLOCKING);
  1269. }
  1270. #endif
  1271. NRF_RADIO->EVENTS_END = 0;
  1272. #if !defined(NRF21540_DRIVER_ENABLE) || (NRF21540_DRIVER_ENABLE == 0) || \
  1273. (defined(NRF21540_DO_NOT_USE_NATIVE_RADIO_IRQ_HANDLER) && \
  1274. (NRF21540_DO_NOT_USE_NATIVE_RADIO_IRQ_HANDLER == 1))
  1275. NVIC_ClearPendingIRQ(RADIO_IRQn);
  1276. #endif
  1277. if (m_state == STATE_RECEIVER_TEST)
  1278. {
  1279. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1280. (void) nrf21540_rx_set(NRF21540_EXECUTE_NOW,
  1281. NRF21540_EXEC_MODE_BLOCKING);
  1282. #else
  1283. NRF_RADIO->TASKS_RXEN = 1;
  1284. #endif
  1285. if (anomaly_172_wa_enabled)
  1286. {
  1287. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  1288. ANOMALY_172_TIMER->CC[1] = BLOCKER_FIX_WAIT_END;
  1289. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  1290. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  1291. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  1292. ANOMALY_172_TIMER->TASKS_START = 1;
  1293. }
  1294. if ((NRF_RADIO->CRCSTATUS == 1) && check_pdu())
  1295. {
  1296. // Count the number of successfully received packets
  1297. m_rx_pkt_count++;
  1298. }
  1299. // Note that failing packets are simply ignored (CRC or contents error).
  1300. // Zero fill all pdu fields to avoid stray data
  1301. memset(&m_pdu, 0, DTM_PDU_MAX_MEMORY_SIZE);
  1302. }
  1303. // If no RECEIVER_TEST is running, ignore incoming packets (but do clear IRQ!)
  1304. }
  1305. if (m_state == STATE_RECEIVER_TEST && NRF_RADIO->EVENTS_READY == 1)
  1306. {
  1307. NRF_RADIO->EVENTS_READY = 0;
  1308. if (anomaly_172_wa_enabled)
  1309. {
  1310. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  1311. ANOMALY_172_TIMER->TASKS_START = 1;
  1312. }
  1313. }
  1314. // Check for timeouts:
  1315. if (mp_timer->EVENTS_COMPARE[0] != 0)
  1316. {
  1317. mp_timer->EVENTS_COMPARE[0] = 0;
  1318. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1319. if (m_state == STATE_TRANSMITTER_TEST)
  1320. {
  1321. (void) nrf21540_tx_set((uint32_t) &mp_timer->EVENTS_COMPARE[0],
  1322. NRF21540_EXEC_MODE_NON_BLOCKING);
  1323. }
  1324. #endif
  1325. }
  1326. else if (mp_timer->EVENTS_COMPARE[1] != 0)
  1327. {
  1328. // Reset timeout event flag for next iteration.
  1329. mp_timer->EVENTS_COMPARE[1] = 0;
  1330. NVIC_ClearPendingIRQ(m_timer_irq);
  1331. return ++m_current_time;
  1332. }
  1333. if (ANOMALY_172_TIMER->EVENTS_COMPARE[0] == 1) {
  1334. uint8_t rssi = anomaly_172_rssi_check();
  1335. if (m_strict_mode) {
  1336. if (rssi > BLOCKER_FIX_RSSI_THRESHOLD) {
  1337. set_strict_mode(0);
  1338. }
  1339. }
  1340. else
  1341. {
  1342. bool too_many_detects = false;
  1343. uint32_t packetcnt2 = *(volatile uint32_t *) 0x40001574;
  1344. uint32_t detect_cnt = packetcnt2 & 0xffff;
  1345. uint32_t addr_cnt = (packetcnt2 >> 16) & 0xffff;
  1346. if ((detect_cnt > BLOCKER_FIX_CNTDETECTTHR) && (addr_cnt < BLOCKER_FIX_CNTADDRTHR)) {
  1347. too_many_detects = true;
  1348. }
  1349. if ((rssi < BLOCKER_FIX_RSSI_THRESHOLD) || too_many_detects) {
  1350. set_strict_mode(1);
  1351. }
  1352. }
  1353. anomaly_172_radio_operation();
  1354. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  1355. ANOMALY_172_TIMER->TASKS_STOP = 1;
  1356. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  1357. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  1358. ANOMALY_172_TIMER->TASKS_START = 1;
  1359. NVIC_ClearPendingIRQ(ANOMALY_172_TIMER_IRQn);
  1360. }
  1361. if (ANOMALY_172_TIMER->EVENTS_COMPARE[1] != 0) {
  1362. uint8_t rssi = anomaly_172_rssi_check();
  1363. if (rssi >= BLOCKER_FIX_RSSI_THRESHOLD) {
  1364. set_strict_mode(0);
  1365. }
  1366. else
  1367. {
  1368. set_strict_mode(1);
  1369. }
  1370. anomaly_172_radio_operation();
  1371. // Disable this event.
  1372. ANOMALY_172_TIMER->CC[1] = 0;
  1373. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  1374. NVIC_ClearPendingIRQ(ANOMALY_172_TIMER_IRQn);
  1375. }
  1376. // Other events: No processing
  1377. }
  1378. }
  1379. uint32_t dtm_cmd(uint16_t cmd)
  1380. {
  1381. uint8_t command = (cmd >> 14) & 0x03;
  1382. // Clean out any non-retrieved event that might linger from an earlier test
  1383. m_new_event = true;
  1384. // Set default event; any error will set it to LE_TEST_STATUS_EVENT_ERROR
  1385. m_event = LE_TEST_STATUS_EVENT_SUCCESS;
  1386. if (m_state == STATE_UNINITIALIZED)
  1387. {
  1388. // Application has not explicitly initialized DTM,
  1389. return DTM_ERROR_UNINITIALIZED;
  1390. }
  1391. if (command == LE_TEST_SETUP)
  1392. {
  1393. uint8_t control = (cmd >> 8) & 0x3F;
  1394. uint8_t parameter = cmd;
  1395. return on_test_setup_cmd(control, parameter);
  1396. }
  1397. if (command == LE_TEST_END)
  1398. {
  1399. return on_test_end_cmd();
  1400. }
  1401. if (m_state != STATE_IDLE)
  1402. {
  1403. // Sequencing error - only TEST_END/RESET are legal while test is running
  1404. // Note: State is unchanged; ongoing test not affected
  1405. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1406. return DTM_ERROR_INVALID_STATE;
  1407. }
  1408. uint8_t length = (cmd >> 2) & 0x3F;
  1409. uint8_t freq = (cmd >> 8) & 0x3F;
  1410. uint8_t payload = cmd & 0x03;
  1411. // Save specified packet in static variable for tx/rx functions to use.
  1412. // Note that BLE conformance testers always use full length packets.
  1413. m_packet_length = (m_packet_length & 0xC0) | ((uint8_t)length & 0x3F);
  1414. m_packet_type = payload;
  1415. m_phys_ch = freq;
  1416. // If 1 Mbit or 2 Mbit radio mode is in use check for Vendor Specific payload.
  1417. if ((m_radio_mode == RADIO_MODE_MODE_Ble_1Mbit || m_radio_mode == RADIO_MODE_MODE_Ble_2Mbit) && payload == DTM_PKT_VENDORSPECIFIC)
  1418. {
  1419. /* Note that in a HCI adaption layer, as well as in the DTM PDU format,
  1420. the value 0x03 is a distinct bit pattern (PRBS15). Even though BLE does not
  1421. support PRBS15, this implementation re-maps 0x03 to DTM_PKT_VENDORSPECIFIC,
  1422. to avoid the risk of confusion, should the code be extended to greater coverage.
  1423. */
  1424. m_packet_type = DTM_PKT_TYPE_VENDORSPECIFIC;
  1425. }
  1426. // Check for illegal values of m_phys_ch. Skip the check if the packet is vendor spesific.
  1427. if (payload != DTM_PKT_VENDORSPECIFIC && m_phys_ch > PHYS_CH_MAX)
  1428. {
  1429. // Parameter error
  1430. // Note: State is unchanged; ongoing test not affected
  1431. m_event = LE_TEST_STATUS_EVENT_ERROR;
  1432. return DTM_ERROR_ILLEGAL_CHANNEL;
  1433. }
  1434. m_rx_pkt_count = 0;
  1435. if (command == LE_RECEIVER_TEST)
  1436. {
  1437. return on_test_receive_cmd();
  1438. }
  1439. if (command == LE_TRANSMITTER_TEST)
  1440. {
  1441. return on_test_transmit_cmd(length, freq);
  1442. }
  1443. return DTM_SUCCESS;
  1444. }
  1445. bool dtm_event_get(dtm_event_t *p_dtm_event)
  1446. {
  1447. bool was_new = m_new_event;
  1448. // mark the current event as retrieved
  1449. m_new_event = false;
  1450. *p_dtm_event = m_event;
  1451. // return value indicates whether this value was already retrieved.
  1452. return was_new;
  1453. }
  1454. /**@brief Function for configuring the output power for transmitter test.
  1455. This function may be called directly, or through dtm_cmd() specifying
  1456. DTM_PKT_VENDORSPECIFIC as payload, SET_TX_POWER as length, and the dBm value as frequency.
  1457. */
  1458. bool dtm_set_txpower(uint32_t new_tx_power)
  1459. {
  1460. // radio->TXPOWER register is 32 bits, low octet a tx power value, upper 24 bits zeroed
  1461. uint8_t new_power8 = (uint8_t)(new_tx_power & 0xFF);
  1462. // The two most significant bits are not sent in the 6 bit field of the DTM command.
  1463. // These two bits are 1's if and only if the tx_power is a negative number.
  1464. // All valid negative values have a non zero bit in among the two most significant
  1465. // of the 6-bit value.
  1466. // By checking these bits, the two most significant bits can be determined.
  1467. new_power8 = (new_power8 & 0x30) != 0 ? (new_power8 | 0xC0) : new_power8;
  1468. if (m_state > STATE_IDLE)
  1469. {
  1470. // radio must be idle to change the tx power
  1471. return false;
  1472. }
  1473. if (dtm_radio_validate(new_power8, m_radio_mode) != DTM_SUCCESS)
  1474. {
  1475. return false;
  1476. }
  1477. m_tx_power = new_power8;
  1478. return true;
  1479. }
  1480. // =================================================================================================
  1481. // Configuration functions (only for parameters not definitely determined by the BLE DTM standard).
  1482. // These functions return true if successful, false if value could not be set
  1483. /**@brief Function for selecting a timer resource.
  1484. * This function may be called directly, or through dtm_cmd() specifying
  1485. * DTM_PKT_VENDORSPECIFIC as payload, SELECT_TIMER as length, and the timer as freq
  1486. *
  1487. * @param[in] new_timer Timer id for the timer to use: 0, 1, or 2.
  1488. *
  1489. * @return true if the timer was successfully changed, false otherwise.
  1490. */
  1491. bool dtm_set_timer(uint32_t new_timer)
  1492. {
  1493. if (m_state > STATE_IDLE)
  1494. {
  1495. return false;
  1496. }
  1497. return dtm_hw_set_timer(&mp_timer, &m_timer_irq, new_timer);
  1498. }
  1499. #if defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1500. bool dtm_set_nrf21450_power_mode(dtm_nrf21540_power_mode_t power_mode)
  1501. {
  1502. nrf21540_pwr_mode_t pwr_mode;
  1503. if (m_state > STATE_IDLE)
  1504. {
  1505. return false;
  1506. }
  1507. switch (power_mode)
  1508. {
  1509. case NRF21540_POWER_MODE_A:
  1510. pwr_mode = NRF21540_PWR_MODE_A;
  1511. break;
  1512. case NRF21540_POWER_MODE_B:
  1513. pwr_mode = NRF21540_PWR_MODE_B;
  1514. break;
  1515. default:
  1516. return false;
  1517. }
  1518. if (nrf21540_pwr_mode_set(pwr_mode) == NRF_SUCCESS)
  1519. {
  1520. return true;
  1521. }
  1522. return false;
  1523. }
  1524. #endif // defined(NRF21540_DRIVER_ENABLE) && (NRF21540_DRIVER_ENABLE == 1)
  1525. /// @}
  1526. #endif // NRF_MODULE_ENABLED(BLE_DTM)