ble_dtm.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. * Copyright (c) 2012 - 2018, 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 <stdbool.h>
  45. #include <string.h>
  46. #include "nrf.h"
  47. #define DTM_HEADER_OFFSET 0 /**< Index where the header of the pdu is located. */
  48. #define DTM_HEADER_SIZE 2 /**< Size of PDU header. */
  49. #define DTM_PAYLOAD_MAX_SIZE 255 /**< Maximum payload size allowed during dtm execution. */
  50. #define DTM_LENGTH_OFFSET (DTM_HEADER_OFFSET + 1) /**< Index where the length of the payload is encoded. */
  51. #define DTM_PDU_MAX_MEMORY_SIZE (DTM_HEADER_SIZE + DTM_PAYLOAD_MAX_SIZE) /**< Maximum PDU size allowed during dtm execution. */
  52. #define DTM_ON_AIR_OVERHEAD_SIZE 10 /**< Size of the packet on air without the payload (preamble + sync word + type + RFU + length + CRC). */
  53. #define RX_MODE true /**< Constant defining RX mode for radio during dtm test. */
  54. #define TX_MODE false /**< Constant defining TX mode for radio during dtm test. */
  55. #define PHYS_CH_MAX 39 /**< Maximum number of valid channels in BLE. */
  56. // Values that for now are "constants" - they could be configured by a function setting them,
  57. // but most of these are set by the BLE DTM standard, so changing them is not relevant.
  58. #define RFPHY_TEST_0X0F_REF_PATTERN 0x0f /**< RF-PHY test packet patterns, for the repeated octet packets. */
  59. #define RFPHY_TEST_0X55_REF_PATTERN 0x55 /**< RF-PHY test packet patterns, for the repeated octet packets. */
  60. #define RFPHY_TEST_0XFF_REF_PATTERN 0xFF /**< RF-PHY test packet patterns, for the repeated octet packets. */
  61. #define PRBS9_CONTENT {0xFF, 0xC1, 0xFB, 0xE8, 0x4C, 0x90, 0x72, 0x8B, \
  62. 0xE7, 0xB3, 0x51, 0x89, 0x63, 0xAB, 0x23, 0x23, \
  63. 0x02, 0x84, 0x18, 0x72, 0xAA, 0x61, 0x2F, 0x3B, \
  64. 0x51, 0xA8, 0xE5, 0x37, 0x49, 0xFB, 0xC9, 0xCA, \
  65. 0x0C, 0x18, 0x53, 0x2C, 0xFD, 0x45, 0xE3, 0x9A, \
  66. 0xE6, 0xF1, 0x5D, 0xB0, 0xB6, 0x1B, 0xB4, 0xBE, \
  67. 0x2A, 0x50, 0xEA, 0xE9, 0x0E, 0x9C, 0x4B, 0x5E, \
  68. 0x57, 0x24, 0xCC, 0xA1, 0xB7, 0x59, 0xB8, 0x87, \
  69. 0xFF, 0xE0, 0x7D, 0x74, 0x26, 0x48, 0xB9, 0xC5, \
  70. 0xF3, 0xD9, 0xA8, 0xC4, 0xB1, 0xD5, 0x91, 0x11, \
  71. 0x01, 0x42, 0x0C, 0x39, 0xD5, 0xB0, 0x97, 0x9D, \
  72. 0x28, 0xD4, 0xF2, 0x9B, 0xA4, 0xFD, 0x64, 0x65, \
  73. 0x06, 0x8C, 0x29, 0x96, 0xFE, 0xA2, 0x71, 0x4D, \
  74. 0xF3, 0xF8, 0x2E, 0x58, 0xDB, 0x0D, 0x5A, 0x5F, \
  75. 0x15, 0x28, 0xF5, 0x74, 0x07, 0xCE, 0x25, 0xAF, \
  76. 0x2B, 0x12, 0xE6, 0xD0, 0xDB, 0x2C, 0xDC, 0xC3, \
  77. 0x7F, 0xF0, 0x3E, 0x3A, 0x13, 0xA4, 0xDC, 0xE2, \
  78. 0xF9, 0x6C, 0x54, 0xE2, 0xD8, 0xEA, 0xC8, 0x88, \
  79. 0x00, 0x21, 0x86, 0x9C, 0x6A, 0xD8, 0xCB, 0x4E, \
  80. 0x14, 0x6A, 0xF9, 0x4D, 0xD2, 0x7E, 0xB2, 0x32, \
  81. 0x03, 0xC6, 0x14, 0x4B, 0x7F, 0xD1, 0xB8, 0xA6, \
  82. 0x79, 0x7C, 0x17, 0xAC, 0xED, 0x06, 0xAD, 0xAF, \
  83. 0x0A, 0x94, 0x7A, 0xBA, 0x03, 0xE7, 0x92, 0xD7, \
  84. 0x15, 0x09, 0x73, 0xE8, 0x6D, 0x16, 0xEE, 0xE1, \
  85. 0x3F, 0x78, 0x1F, 0x9D, 0x09, 0x52, 0x6E, 0xF1, \
  86. 0x7C, 0x36, 0x2A, 0x71, 0x6C, 0x75, 0x64, 0x44, \
  87. 0x80, 0x10, 0x43, 0x4E, 0x35, 0xEC, 0x65, 0x27, \
  88. 0x0A, 0xB5, 0xFC, 0x26, 0x69, 0x3F, 0x59, 0x99, \
  89. 0x01, 0x63, 0x8A, 0xA5, 0xBF, 0x68, 0x5C, 0xD3, \
  90. 0x3C, 0xBE, 0x0B, 0xD6, 0x76, 0x83, 0xD6, 0x57, \
  91. 0x05, 0x4A, 0x3D, 0xDD, 0x81, 0x73, 0xC9, 0xEB, \
  92. 0x8A, 0x84, 0x39, 0xF4, 0x36, 0x0B, 0xF7} /**< The PRBS9 sequence used as packet payload.
  93. The bytes in the sequence is in the right order, but the bits of each byte in the array is reverse.
  94. of that found by running the PRBS9 algorithm. This is because of the endianess of the nRF5 radio. */
  95. /**@brief Structure holding the PDU used for transmitting/receiving a PDU.
  96. */
  97. typedef struct
  98. {
  99. uint8_t content[DTM_HEADER_SIZE + DTM_PAYLOAD_MAX_SIZE]; /**< PDU packet content. */
  100. } pdu_type_t;
  101. /**@brief States used for the DTM test implementation.
  102. */
  103. typedef enum
  104. {
  105. STATE_UNINITIALIZED, /**< The DTM is uninitialized. */
  106. STATE_IDLE, /**< State when system has just initialized, or current test has completed. */
  107. STATE_TRANSMITTER_TEST, /**< State used when a DTM Transmission test is running. */
  108. STATE_CARRIER_TEST, /**< State used when a DTM Carrier test is running (Vendor specific test). */
  109. STATE_RECEIVER_TEST /**< State used when a DTM Receive test is running. */
  110. } state_t;
  111. // Internal variables set as side effects of commands or events.
  112. static state_t m_state = STATE_UNINITIALIZED; /**< Current machine state. */
  113. static uint16_t m_rx_pkt_count; /**< Number of valid packets received. */
  114. static pdu_type_t m_pdu; /**< PDU to be sent. */
  115. static uint16_t m_event; /**< current command status - initially "ok", may be set if error detected, or to packet count. */
  116. static bool m_new_event; /**< Command has been processed - number of not yet reported event bytes. */
  117. static uint32_t m_packet_length; /**< Payload length of transmitted PDU, bits 2:7 of 16-bit dtm command. */
  118. static dtm_pkt_type_t m_packet_type; /**< Bits 0..1 of 16-bit transmit command, or 0xFFFFFFFF. */
  119. 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. */
  120. 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. */
  121. // Nordic specific configuration values (not defined by BLE standard).
  122. // Definition of initial values found in ble_dtm.h
  123. static int32_t m_tx_power = DEFAULT_TX_POWER; /**< TX power for transmission test, default 0 dBm. */
  124. static NRF_TIMER_Type * mp_timer = DEFAULT_TIMER; /**< Timer to be used. */
  125. static IRQn_Type m_timer_irq = DEFAULT_TIMER_IRQn; /**< which interrupt line to clear on every timeout */
  126. static uint8_t const m_prbs_content[] = PRBS9_CONTENT; /**< Pseudo-random bit sequence defined by the BLE standard. */
  127. static uint8_t m_packetHeaderLFlen = 8; /**< Length of length field in packet Header (in bits). */
  128. static uint8_t m_packetHeaderS0len = 1; /**< Length of S0 field in packet Header (in bytes). */
  129. static uint8_t m_packetHeaderS1len = 0; /**< Length of S1 field in packet Header (in bits). */
  130. static uint8_t m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit; /**< Length of the preamble. */
  131. static uint8_t m_crcConfSkipAddr = 1; /**< Leave packet address field out of CRC calculation. */
  132. static uint8_t m_static_length = 0; /**< Number of bytes sent in addition to the var.length payload. */
  133. static uint32_t m_balen = 3; /**< Base address length in bytes. */
  134. 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. */
  135. static uint32_t m_whitening = RADIO_PCNF1_WHITEEN_Disabled; /**< Whitening disabled. */
  136. static uint8_t m_crcLength = RADIO_CRCCNF_LEN_Three; /**< CRC Length (in bytes). */
  137. static uint32_t m_address = 0x71764129; /**< Address. */
  138. static uint32_t m_crc_poly = 0x0000065B; /**< CRC polynomial. */
  139. static uint32_t m_crc_init = 0x00555555; /**< Initial value for CRC calculation. */
  140. static uint8_t m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit; /**< nRF51 specific radio mode value. */
  141. static uint32_t m_txIntervaluS = 2500; /**< Time between start of Tx packets (in uS). */
  142. // nRF52840 anomaly 172
  143. static bool anomaly_172_wa_enabled = false; /**< Enable or disable the workaround for Errata 172. */
  144. static uint8_t m_strict_mode = 0; /**< Enable or disable strict mode to workaround Errata 172. */
  145. #define BLOCKER_FIX_RSSI_THRESHOLD 95
  146. #define BLOCKER_FIX_WAIT_DEFAULT 10000 // 10 ms
  147. #define BLOCKER_FIX_WAIT_END 500 // 500 us
  148. #define BLOCKER_FIX_CNTDETECTTHR 15
  149. #define BLOCKER_FIX_CNTADDRTHR 2
  150. /**@brief Function for verifying that a received PDU has the expected structure and content.
  151. */
  152. static bool check_pdu(void)
  153. {
  154. uint8_t k; // Byte pointer for running through PDU payload
  155. uint8_t pattern; // Repeating octet value in payload
  156. dtm_pkt_type_t pdu_packet_type; // Note: PDU packet type is a 4-bit field in HCI, but 2 bits in BLE DTM
  157. uint32_t length = 0;
  158. pdu_packet_type = (dtm_pkt_type_t)(m_pdu.content[DTM_HEADER_OFFSET] & 0x0F);
  159. length = m_pdu.content[DTM_LENGTH_OFFSET];
  160. // Check that the length is valid.
  161. if (length > DTM_PAYLOAD_MAX_SIZE)
  162. {
  163. return false;
  164. }
  165. // If the 1Mbit or 2Mbit radio mode is active, check that one of the three valid uncoded DTM packet types are selected.
  166. 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))
  167. {
  168. return false;
  169. }
  170. #ifdef NRF52840_XXAA
  171. // If a long range radio mode is active, check that one of the four valid coded DTM packet types are selected.
  172. 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))
  173. {
  174. return false;
  175. }
  176. #endif
  177. if (pdu_packet_type == DTM_PKT_PRBS9)
  178. {
  179. // Payload does not consist of one repeated octet; must compare ir with entire block into
  180. return (memcmp(m_pdu.content + DTM_HEADER_SIZE, m_prbs_content, length) == 0);
  181. }
  182. if (pdu_packet_type == DTM_PKT_0X0F)
  183. {
  184. pattern = RFPHY_TEST_0X0F_REF_PATTERN;
  185. }
  186. else if (pdu_packet_type == DTM_PKT_0X55)
  187. {
  188. pattern = RFPHY_TEST_0X55_REF_PATTERN;
  189. }
  190. else if (pdu_packet_type == DTM_PKT_0XFF)
  191. {
  192. pattern = RFPHY_TEST_0XFF_REF_PATTERN;
  193. }
  194. else
  195. {
  196. // No valid packet type set.
  197. return false;
  198. }
  199. for (k = 0; k < length; k++)
  200. {
  201. // Check repeated pattern filling the PDU payload
  202. if (m_pdu.content[k + 2] != pattern)
  203. {
  204. return false;
  205. }
  206. }
  207. return true;
  208. }
  209. /**@brief Function for turning off the radio after a test.
  210. * Also called after test done, to be ready for next test.
  211. */
  212. static void radio_reset(void)
  213. {
  214. NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk | PPI_CHENCLR_CH1_Msk;
  215. NRF_RADIO->SHORTS = 0;
  216. NRF_RADIO->EVENTS_DISABLED = 0;
  217. NRF_RADIO->TASKS_DISABLE = 1;
  218. while (NRF_RADIO->EVENTS_DISABLED == 0)
  219. {
  220. // Do nothing
  221. }
  222. NRF_RADIO->EVENTS_DISABLED = 0;
  223. NRF_RADIO->TASKS_RXEN = 0;
  224. NRF_RADIO->TASKS_TXEN = 0;
  225. m_rx_pkt_count = 0;
  226. }
  227. /**@brief Function for initializing the radio for DTM.
  228. */
  229. static uint32_t radio_init(void)
  230. {
  231. if (dtm_radio_validate(m_tx_power, m_radio_mode) != DTM_SUCCESS)
  232. {
  233. m_event = LE_TEST_STATUS_EVENT_ERROR;
  234. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  235. }
  236. // Turn off radio before configuring it
  237. radio_reset();
  238. NRF_RADIO->TXPOWER = m_tx_power & RADIO_TXPOWER_TXPOWER_Msk;
  239. NRF_RADIO->MODE = m_radio_mode << RADIO_MODE_MODE_Pos;
  240. // Set the access address, address0/prefix0 used for both Rx and Tx address
  241. NRF_RADIO->PREFIX0 &= ~RADIO_PREFIX0_AP0_Msk;
  242. NRF_RADIO->PREFIX0 |= (m_address >> 24) & RADIO_PREFIX0_AP0_Msk;
  243. NRF_RADIO->BASE0 = m_address << 8;
  244. NRF_RADIO->RXADDRESSES = RADIO_RXADDRESSES_ADDR0_Enabled << RADIO_RXADDRESSES_ADDR0_Pos;
  245. NRF_RADIO->TXADDRESS = (0x00 << RADIO_TXADDRESS_TXADDRESS_Pos) & RADIO_TXADDRESS_TXADDRESS_Msk;
  246. // Configure CRC calculation
  247. NRF_RADIO->CRCCNF = (m_crcConfSkipAddr << RADIO_CRCCNF_SKIP_ADDR_Pos) |
  248. (m_crcLength << RADIO_CRCCNF_LEN_Pos);
  249. if (m_radio_mode == RADIO_MODE_MODE_Ble_1Mbit || m_radio_mode == RADIO_MODE_MODE_Ble_2Mbit)
  250. {
  251. // Non-coded PHY
  252. NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
  253. (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
  254. (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
  255. (m_packetHeaderPlen << RADIO_PCNF0_PLEN_Pos);
  256. }
  257. #ifdef NRF52840_XXAA
  258. else
  259. {
  260. // Coded PHY (Long range)
  261. NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
  262. (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
  263. (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
  264. (3 << RADIO_PCNF0_TERMLEN_Pos) |
  265. (2 << RADIO_PCNF0_CILEN_Pos) |
  266. (m_packetHeaderPlen << RADIO_PCNF0_PLEN_Pos);
  267. }
  268. #endif
  269. NRF_RADIO->PCNF1 = (m_whitening << RADIO_PCNF1_WHITEEN_Pos) |
  270. (m_endian << RADIO_PCNF1_ENDIAN_Pos) |
  271. (m_balen << RADIO_PCNF1_BALEN_Pos) |
  272. (m_static_length << RADIO_PCNF1_STATLEN_Pos) |
  273. (DTM_PAYLOAD_MAX_SIZE << RADIO_PCNF1_MAXLEN_Pos);
  274. return DTM_SUCCESS;
  275. }
  276. // Strict mode setting will be used only by devices affected by nRF52840 anomaly 172
  277. void set_strict_mode (bool enable)
  278. {
  279. uint8_t dbcCorrTh;
  280. uint8_t dsssMinPeakCount;
  281. if (enable == true)
  282. {
  283. dbcCorrTh = 0x7d;
  284. dsssMinPeakCount = 6;
  285. *(volatile uint32_t *) 0x4000173c = ((*((volatile uint32_t *) 0x4000173c)) & 0x7FFFFF00) | 0x80000000 | (((uint32_t)(dbcCorrTh)) << 0);
  286. *(volatile uint32_t *) 0x4000177c = ((*((volatile uint32_t *) 0x4000177c)) & 0x7FFFFF8F) | 0x80000000 | ((((uint32_t)dsssMinPeakCount) & 0x00000007) << 4);
  287. }
  288. else
  289. {
  290. *(volatile uint32_t *) 0x4000173c = ((*((volatile uint32_t *) 0x4000173c)) & 0x7FFFFFFF); // Unset override of dbcCorrTh
  291. *(volatile uint32_t *) 0x4000177c = ((*((volatile uint32_t *) 0x4000177c)) & 0x7FFFFFFF); // Unset override of dsssMinPeakCount
  292. }
  293. m_strict_mode = enable;
  294. }
  295. // Radio configuration used as a workaround for nRF52840 anomaly 172
  296. void anomaly_172_radio_operation(void)
  297. {
  298. *(volatile uint32_t *) 0x40001040 = 1;
  299. *(volatile uint32_t *) 0x40001038 = 1;
  300. }
  301. // Function to gather RSSI data and set strict mode accordingly. Used as part of the workaround for nRF52840 anomaly 172
  302. uint8_t anomaly_172_rssi_check(void)
  303. {
  304. NRF_RADIO->EVENTS_RSSIEND = 0;
  305. NRF_RADIO->TASKS_RSSISTART = 1;
  306. while (NRF_RADIO->EVENTS_RSSIEND == 0);
  307. uint8_t rssi = NRF_RADIO->RSSISAMPLE;
  308. return rssi;
  309. }
  310. // Used only by devices affected by nRF52840 anomaly 172
  311. void ANOMALY_172_TIMER_IRQHandler(void)
  312. {
  313. if (ANOMALY_172_TIMER->EVENTS_COMPARE[0]) {
  314. uint8_t rssi = anomaly_172_rssi_check();
  315. if (m_strict_mode) {
  316. if (rssi > BLOCKER_FIX_RSSI_THRESHOLD) {
  317. set_strict_mode(0);
  318. }
  319. }
  320. else
  321. {
  322. uint8_t too_many_detects = 0;
  323. uint32_t packetcnt2 = *(volatile uint32_t *) 0x40001574;
  324. uint32_t detect_cnt = packetcnt2 & 0xffff;
  325. uint32_t addr_cnt = (packetcnt2 >> 16) & 0xffff;
  326. if ((detect_cnt > BLOCKER_FIX_CNTDETECTTHR) && (addr_cnt < BLOCKER_FIX_CNTADDRTHR)) {
  327. too_many_detects = 1;
  328. }
  329. if ((rssi < BLOCKER_FIX_RSSI_THRESHOLD) || too_many_detects) {
  330. set_strict_mode(1);
  331. }
  332. }
  333. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  334. ANOMALY_172_TIMER->TASKS_STOP = 1;
  335. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  336. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  337. ANOMALY_172_TIMER->TASKS_START = 1;
  338. }
  339. if (ANOMALY_172_TIMER->EVENTS_COMPARE[1]) {
  340. uint8_t rssi = anomaly_172_rssi_check();
  341. if (rssi > BLOCKER_FIX_RSSI_THRESHOLD) {
  342. set_strict_mode(0);
  343. }
  344. else
  345. {
  346. set_strict_mode(1);
  347. }
  348. // Disable this event.
  349. ANOMALY_172_TIMER->CC[1] = 0;
  350. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  351. }
  352. anomaly_172_radio_operation();
  353. }
  354. /**@brief Function for preparing the radio. At start of each test: Turn off RF, clear interrupt flags of RF, initialize the radio
  355. * at given RF channel.
  356. *
  357. *@param[in] rx boolean indicating if radio should be prepared in rx mode (true) or tx mode.
  358. */
  359. static void radio_prepare(bool rx)
  360. {
  361. dtm_turn_off_test();
  362. NRF_RADIO->CRCPOLY = m_crc_poly;
  363. NRF_RADIO->CRCINIT = m_crc_init;
  364. NRF_RADIO->FREQUENCY = (m_phys_ch << 1) + 2; // Actual frequency (MHz): 2400 + register value
  365. NRF_RADIO->PACKETPTR = (uint32_t)&m_pdu; // Setting packet pointer will start the radio
  366. NRF_RADIO->EVENTS_READY = 0;
  367. NRF_RADIO->SHORTS = (1 << RADIO_SHORTS_READY_START_Pos) | // Shortcut between READY event and START task
  368. (1 << RADIO_SHORTS_END_DISABLE_Pos); // Shortcut between END event and DISABLE task
  369. if (rx)
  370. {
  371. // Enable strict mode if running on a device affected by nRF52840 anomaly 172
  372. if (anomaly_172_wa_enabled)
  373. {
  374. set_strict_mode(1);
  375. }
  376. NRF_RADIO->EVENTS_END = 0;
  377. NRF_RADIO->TASKS_RXEN = 1; // shorts will start radio in RX mode when it is ready
  378. }
  379. else // tx
  380. {
  381. NRF_RADIO->TXPOWER = m_tx_power & RADIO_TXPOWER_TXPOWER_Msk;
  382. // Stop the timer used by nRF52840 anomaly 172 if running on an affected device.
  383. if (anomaly_172_wa_enabled)
  384. {
  385. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  386. ANOMALY_172_TIMER->TASKS_STOP = 1;
  387. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  388. }
  389. }
  390. }
  391. /**@brief Function for terminating the ongoing test (if any) and closing down the radio.
  392. */
  393. static void dtm_test_done(void)
  394. {
  395. dtm_turn_off_test();
  396. NRF_PPI->CHENCLR = 0x01;
  397. NRF_PPI->CH[0].EEP = 0; // Break connection from timer to radio to stop transmit loop
  398. NRF_PPI->CH[0].TEP = 0;
  399. radio_reset();
  400. m_state = STATE_IDLE;
  401. // Enable the timer used by nRF52840 anomaly 172 if running on an affected device.
  402. if (anomaly_172_wa_enabled)
  403. {
  404. NVIC_EnableIRQ(ANOMALY_172_TIMER_IRQn);
  405. NVIC_ClearPendingIRQ(ANOMALY_172_TIMER_IRQn);
  406. }
  407. }
  408. /**@brief Function for configuring the timer for 625us cycle time.
  409. */
  410. static uint32_t timer_init(void)
  411. {
  412. // Use 16MHz from external crystal
  413. // This could be customized for RC/Xtal, or even to use a 32 kHz crystal
  414. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  415. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  416. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  417. {
  418. // Do nothing while waiting for the clock to start
  419. }
  420. mp_timer->TASKS_STOP = 1; // Stop timer, if it was running
  421. mp_timer->TASKS_CLEAR = 1;
  422. mp_timer->MODE = TIMER_MODE_MODE_Timer; // Timer mode (not counter)
  423. mp_timer->EVENTS_COMPARE[0] = 0; // clean up possible old events
  424. mp_timer->EVENTS_COMPARE[1] = 0;
  425. mp_timer->EVENTS_COMPARE[2] = 0;
  426. mp_timer->EVENTS_COMPARE[3] = 0;
  427. // Timer is polled, but enable the compare0 interrupt in order to wakeup from CPU sleep
  428. mp_timer->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
  429. mp_timer->SHORTS = 1 << TIMER_SHORTS_COMPARE0_CLEAR_Pos; // Clear the count every time timer reaches the CCREG0 count
  430. mp_timer->PRESCALER = 4; // Input clock is 16MHz, timer clock = 2 ^ prescale -> interval 1us
  431. mp_timer->CC[0] = m_txIntervaluS; // 625uS with 1MHz clock to the timer
  432. 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
  433. mp_timer->TASKS_START = 1; // Start the timer - it will be running continuously
  434. m_current_time = 0;
  435. return DTM_SUCCESS;
  436. }
  437. /**@brief Function for handling vendor specific commands.
  438. * Used when packet type is set to Vendor specific.
  439. * The length field is used for encoding vendor specific command.
  440. * The frequency field is used for encoding vendor specific options to the command.
  441. *
  442. * @param[in] vendor_cmd Vendor specific command to be executed.
  443. * @param[in] vendor_option Vendor specific option to the vendor command.
  444. *
  445. * @return DTM_SUCCESS or one of the DTM_ERROR_ values
  446. */
  447. static uint32_t dtm_vendor_specific_pkt(uint32_t vendor_cmd, dtm_freq_t vendor_option)
  448. {
  449. switch (vendor_cmd)
  450. {
  451. // nRFgo Studio uses CARRIER_TEST_STUDIO to indicate a continuous carrier without
  452. // a modulated signal.
  453. case CARRIER_TEST:
  454. case CARRIER_TEST_STUDIO:
  455. // Not a packet type, but used to indicate that a continuous carrier signal
  456. // should be transmitted by the radio.
  457. radio_prepare(TX_MODE);
  458. dtm_constant_carrier();
  459. // Shortcut between READY event and START task
  460. NRF_RADIO->SHORTS = 1 << RADIO_SHORTS_READY_START_Pos;
  461. // Shortcut will start radio in Tx mode when it is ready
  462. NRF_RADIO->TASKS_TXEN = 1;
  463. m_state = STATE_CARRIER_TEST;
  464. break;
  465. case SET_TX_POWER:
  466. if (!dtm_set_txpower(vendor_option))
  467. {
  468. m_event = LE_TEST_STATUS_EVENT_ERROR;
  469. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  470. }
  471. break;
  472. case SELECT_TIMER:
  473. if (!dtm_set_timer(vendor_option))
  474. {
  475. m_event = LE_TEST_STATUS_EVENT_ERROR;
  476. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  477. }
  478. break;
  479. }
  480. // Event code is unchanged, successful
  481. return DTM_SUCCESS;
  482. }
  483. static uint32_t dtm_packet_interval_calculate(uint32_t test_payload_length, uint32_t mode)
  484. {
  485. uint32_t test_packet_length = 0; // [us] NOTE: bits are us at 1Mbit
  486. uint32_t packet_interval = 0; // us
  487. uint32_t overhead_bits = 0; // bits
  488. /* packet overhead
  489. * see BLE [Vol 6, Part F] page 213
  490. * 4.1 LE TEST PACKET FORMAT */
  491. if (mode == RADIO_MODE_MODE_Ble_2Mbit)
  492. {
  493. // 16 preamble
  494. // 32 sync word
  495. // 8 PDU header, actually packetHeaderS0len * 8
  496. // 8 PDU length, actually packetHeaderLFlen
  497. // 24 CRC
  498. overhead_bits = 88; // 11 bytes
  499. }
  500. else if (mode == RADIO_MODE_MODE_Ble_1Mbit)
  501. {
  502. // 8 preamble
  503. // 32 sync word
  504. // 8 PDU header, actually packetHeaderS0len * 8
  505. // 8 PDU length, actually packetHeaderLFlen
  506. // 24 CRC
  507. overhead_bits = 80; // 10 bytes
  508. }
  509. #ifdef NRF52840_XXAA
  510. else if (mode == RADIO_MODE_MODE_Ble_LR125Kbit)
  511. {
  512. // 80 preamble
  513. // 32 * 8 sync word coding=8
  514. // 2 * 8 Coding indicator, coding=8
  515. // 3 * 8 TERM1 coding=8
  516. // 8 * 8 PDU header, actually packetHeaderS0len * 8 coding=8
  517. // 8 * 8 PDU length, actually packetHeaderLFlen coding=8
  518. // 24 * 8 CRC coding=8
  519. // 3 * 8 TERM2 coding=8
  520. overhead_bits = 720; // 90 bytes
  521. }
  522. else if (mode == RADIO_MODE_MODE_Ble_LR500Kbit)
  523. {
  524. // 80 preamble
  525. // 32 * 8 sync word coding=8
  526. // 2 * 8 Coding indicator, coding=8
  527. // 3 * 8 TERM 1 coding=8
  528. // 8 * 2 PDU header, actually packetHeaderS0len * 8 coding=2
  529. // 8 * 2 PDU length, actually packetHeaderLFlen coding=2
  530. // 24 * 2 CRC coding=2
  531. // 3 * 2 TERM2 coding=2
  532. // NOTE: this makes us clock out 46 bits for CI + TERM1 + TERM2
  533. // assumption the radio will handle this
  534. overhead_bits = 462; // 57.75 bytes
  535. }
  536. #endif
  537. /* add PDU payload test_payload length */
  538. test_packet_length = (test_payload_length * 8); // in bits
  539. #ifdef NRF52840_XXAA
  540. // account for the encoding of PDU
  541. if (mode == RADIO_MODE_MODE_Ble_LR125Kbit)
  542. {
  543. test_packet_length *= 8; // 1 to 8 encoding
  544. }
  545. if (mode == RADIO_MODE_MODE_Ble_LR500Kbit)
  546. {
  547. test_packet_length *= 2; // 1 to 2 encoding
  548. }
  549. #endif
  550. // add overhead calculated above
  551. test_packet_length += overhead_bits;
  552. // we remember this bits are us in 1Mbit
  553. if (mode == RADIO_MODE_MODE_Ble_2Mbit)
  554. {
  555. test_packet_length /= 2; // double speed
  556. }
  557. /*
  558. * packet_interval = ceil((test_packet_length+249)/625)*625
  559. * NOTE: To avoid floating point an equivalent calculation is used.
  560. */
  561. uint32_t i = 0;
  562. uint32_t timeout = 0;
  563. do
  564. {
  565. i++;
  566. timeout = i * 625;
  567. } while (test_packet_length + 249 > timeout);
  568. packet_interval = i * 625;
  569. return packet_interval;
  570. }
  571. uint32_t dtm_init(void)
  572. {
  573. if ((timer_init() != DTM_SUCCESS) || (radio_init() != DTM_SUCCESS))
  574. {
  575. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  576. }
  577. m_new_event = false;
  578. m_state = STATE_IDLE;
  579. m_packet_length = 0;
  580. // Enable wake-up on event
  581. SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
  582. return DTM_SUCCESS;
  583. }
  584. uint32_t dtm_wait(void)
  585. {
  586. // Enable wake-up on event
  587. SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
  588. for (;;)
  589. {
  590. // Event may be the reception of a packet -
  591. // handle radio first, to give it highest priority:
  592. if (NRF_RADIO->EVENTS_END != 0)
  593. {
  594. NRF_RADIO->EVENTS_END = 0;
  595. NVIC_ClearPendingIRQ(RADIO_IRQn);
  596. if (m_state == STATE_RECEIVER_TEST)
  597. {
  598. NRF_RADIO->TASKS_RXEN = 1;
  599. if (anomaly_172_wa_enabled)
  600. {
  601. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  602. ANOMALY_172_TIMER->CC[1] = BLOCKER_FIX_WAIT_END;
  603. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  604. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  605. ANOMALY_172_TIMER->EVENTS_COMPARE[1] = 0;
  606. ANOMALY_172_TIMER->TASKS_START = 1;
  607. }
  608. if ((NRF_RADIO->CRCSTATUS == 1) && check_pdu())
  609. {
  610. // Count the number of successfully received packets
  611. m_rx_pkt_count++;
  612. }
  613. // Note that failing packets are simply ignored (CRC or contents error).
  614. // Zero fill all pdu fields to avoid stray data
  615. memset(&m_pdu, 0, DTM_PDU_MAX_MEMORY_SIZE);
  616. }
  617. // If no RECEIVER_TEST is running, ignore incoming packets (but do clear IRQ!)
  618. }
  619. if (m_state == STATE_RECEIVER_TEST && NRF_RADIO->EVENTS_ADDRESS != 0)
  620. {
  621. if (anomaly_172_wa_enabled)
  622. {
  623. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  624. }
  625. }
  626. if (m_state == STATE_RECEIVER_TEST && NRF_RADIO->EVENTS_READY != 0)
  627. {
  628. if (anomaly_172_wa_enabled)
  629. {
  630. // Check if strict mode is necessary
  631. uint8_t rssi = anomaly_172_rssi_check();
  632. if (rssi > BLOCKER_FIX_RSSI_THRESHOLD) {
  633. set_strict_mode(0);
  634. }
  635. // Start timer to regularly check if strict mode is necessary
  636. ANOMALY_172_TIMER->CC[0] = BLOCKER_FIX_WAIT_DEFAULT;
  637. ANOMALY_172_TIMER->TASKS_CLEAR = 1;
  638. ANOMALY_172_TIMER->EVENTS_COMPARE[0] = 0;
  639. ANOMALY_172_TIMER->TASKS_START = 1;
  640. anomaly_172_radio_operation();
  641. }
  642. }
  643. // Check for timeouts:
  644. if (mp_timer->EVENTS_COMPARE[0] != 0)
  645. {
  646. mp_timer->EVENTS_COMPARE[0] = 0;
  647. }
  648. else if (mp_timer->EVENTS_COMPARE[1] != 0)
  649. {
  650. // Reset timeout event flag for next iteration.
  651. mp_timer->EVENTS_COMPARE[1] = 0;
  652. NVIC_ClearPendingIRQ(m_timer_irq);
  653. return ++m_current_time;
  654. }
  655. // Other events: No processing
  656. }
  657. }
  658. uint32_t dtm_cmd(dtm_cmd_t cmd, dtm_freq_t freq, uint32_t length, dtm_pkt_type_t payload)
  659. {
  660. // Save specified packet in static variable for tx/rx functions to use.
  661. // Note that BLE conformance testers always use full length packets.
  662. m_packet_length = (m_packet_length & 0xC0) | ((uint8_t)length & 0x3F);
  663. m_packet_type = payload;
  664. m_phys_ch = freq;
  665. // If 1 Mbit or 2 Mbit radio mode is in use check for Vendor Specific payload.
  666. if ((m_radio_mode == RADIO_MODE_MODE_Ble_1Mbit || m_radio_mode == RADIO_MODE_MODE_Ble_2Mbit) && payload == DTM_PKT_VENDORSPECIFIC)
  667. {
  668. /* Note that in a HCI adaption layer, as well as in the DTM PDU format,
  669. the value 0x03 is a distinct bit pattern (PRBS15). Even though BLE does not
  670. support PRBS15, this implementation re-maps 0x03 to DTM_PKT_VENDORSPECIFIC,
  671. to avoid the risk of confusion, should the code be extended to greater coverage.
  672. */
  673. m_packet_type = DTM_PKT_TYPE_VENDORSPECIFIC;
  674. }
  675. // Clean out any non-retrieved event that might linger from an earlier test
  676. m_new_event = true;
  677. // Set default event; any error will set it to LE_TEST_STATUS_EVENT_ERROR
  678. m_event = LE_TEST_STATUS_EVENT_SUCCESS;
  679. if (m_state == STATE_UNINITIALIZED)
  680. {
  681. // Application has not explicitly initialized DTM,
  682. return DTM_ERROR_UNINITIALIZED;
  683. }
  684. if (cmd == LE_TEST_SETUP)
  685. {
  686. // Note that timer will continue running after a reset
  687. dtm_test_done();
  688. if (freq == LE_TEST_SETUP_RESET)
  689. {
  690. if (length != 0x00)
  691. {
  692. m_event = LE_TEST_STATUS_EVENT_ERROR;
  693. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  694. }
  695. // Reset the packet length upper bits.
  696. m_packet_length = 0;
  697. // Reset the selected PHY to 1Mbit
  698. m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit;
  699. m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit;
  700. #ifdef NRF52840_XXAA
  701. // Workaround for Errata ID 164
  702. *(volatile uint32_t *)0x4000173C &= ~0x80000000;
  703. // Workaround for Errata ID 191
  704. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  705. #endif
  706. }
  707. else if (freq == LE_TEST_SETUP_SET_UPPER)
  708. {
  709. if (length > 0x03)
  710. {
  711. m_event = LE_TEST_STATUS_EVENT_ERROR;
  712. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  713. }
  714. m_packet_length = length << 6;
  715. }
  716. else if (freq == LE_TEST_SETUP_SET_PHY)
  717. {
  718. switch (length)
  719. {
  720. case LE_PHY_1M:
  721. m_radio_mode = RADIO_MODE_MODE_Ble_1Mbit;
  722. m_packetHeaderPlen = RADIO_PCNF0_PLEN_8bit;
  723. #ifdef NRF52840_XXAA
  724. // Workaround for Errata ID 164
  725. *(volatile uint32_t *)0x4000173C &= ~0x80000000;
  726. // Workaround for Errata ID 191
  727. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  728. #endif
  729. // Disable the workaround for nRF52840 anomaly 172.
  730. set_strict_mode(0);
  731. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  732. anomaly_172_wa_enabled = false;
  733. return radio_init();
  734. case LE_PHY_2M:
  735. m_radio_mode = RADIO_MODE_MODE_Ble_2Mbit;
  736. m_packetHeaderPlen = RADIO_PCNF0_PLEN_16bit;
  737. #ifdef NRF52840_XXAA
  738. // Workaround for Errata ID 164
  739. *(volatile uint32_t *)0x4000173C &= ~0x80000000;
  740. // Workaround for Errata ID 191
  741. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFFFFFF);
  742. #endif
  743. // Disable the workaround for nRF52840 anomaly 172.
  744. set_strict_mode(0);
  745. ANOMALY_172_TIMER->TASKS_SHUTDOWN = 1;
  746. anomaly_172_wa_enabled = false;
  747. return radio_init();
  748. case LE_PHY_LE_CODED_S8:
  749. #ifdef NRF52840_XXAA
  750. m_radio_mode = RADIO_MODE_MODE_Ble_LR125Kbit;
  751. m_packetHeaderPlen = RADIO_PCNF0_PLEN_LongRange;
  752. // Workaround for Errata ID 164
  753. *(volatile uint32_t *)0x4000173C |= 0x80000000;
  754. *(volatile uint32_t *)0x4000173C = ((*(volatile uint32_t *)0x4000173C & 0xFFFFFF00) | 0x5C);
  755. // Workaround for Errata ID 191
  756. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFF00FF) | 0x80000000 | (((uint32_t)(196)) << 8);
  757. // Enable the workaround for nRF52840 anomaly 172 on affected devices.
  758. if ((*(volatile uint32_t *)0x40001788) == 0)
  759. {
  760. anomaly_172_wa_enabled = true;
  761. }
  762. return radio_init();
  763. #else
  764. m_event = LE_TEST_STATUS_EVENT_ERROR;
  765. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  766. #endif // NRF52840_XXAA
  767. case LE_PHY_LE_CODED_S2:
  768. #ifdef NRF52840_XXAA
  769. m_radio_mode = RADIO_MODE_MODE_Ble_LR500Kbit;
  770. m_packetHeaderPlen = RADIO_PCNF0_PLEN_LongRange;
  771. // Workaround for Errata ID 164
  772. *(volatile uint32_t *)0x4000173C |= 0x80000000;
  773. *(volatile uint32_t *)0x4000173C = ((*(volatile uint32_t *)0x4000173C & 0xFFFFFF00) | 0x5C);
  774. // Workaround for Errata ID 191
  775. *(volatile uint32_t *) 0x40001740 = ((*((volatile uint32_t *) 0x40001740)) & 0x7FFF00FF) | 0x80000000 | (((uint32_t)(196)) << 8);
  776. // Enable the workaround for nRF52840 anomaly 172 on affected devices.
  777. if ((*(volatile uint32_t *)0x40001788) == 0)
  778. {
  779. anomaly_172_wa_enabled = true;
  780. }
  781. return radio_init();
  782. #else
  783. m_event = LE_TEST_STATUS_EVENT_ERROR;
  784. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  785. #endif
  786. default:
  787. m_event = LE_TEST_STATUS_EVENT_ERROR;
  788. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  789. }
  790. }
  791. else if (freq == LE_TEST_SETUP_SELECT_MODULATION)
  792. {
  793. if (length > 0x01)
  794. {
  795. m_event = LE_TEST_STATUS_EVENT_ERROR;
  796. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  797. }
  798. // Only standard modulation is supported.
  799. }
  800. else if (freq == LE_TEST_SETUP_READ_SUPPORTED)
  801. {
  802. if (length != 0x00)
  803. {
  804. m_event = LE_TEST_STATUS_EVENT_ERROR;
  805. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  806. }
  807. // 0XXXXXXXXXXX0110 indicate that 2Mbit and DLE is supported and stable modulation is not supported (No nRF5 device supports this).
  808. m_event = 0x0006;
  809. }
  810. else if (freq == LE_TEST_SETUP_READ_MAX)
  811. {
  812. // Read max supported value.
  813. switch (length)
  814. {
  815. case 0x00:
  816. // Read supportedMaxTxOctets
  817. m_event = 0x01FE;
  818. break;
  819. case 0x01:
  820. // Read supportedMaxTxTime
  821. m_event = 0x4290;
  822. break;
  823. case 0x02:
  824. // Read supportedMaxRxOctets
  825. m_event = 0x01FE;
  826. break;
  827. case 0x03:
  828. // Read supportedMaxRxTime
  829. m_event = 0x4290;
  830. break;
  831. default:
  832. m_event = LE_TEST_STATUS_EVENT_ERROR;
  833. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  834. }
  835. }
  836. else
  837. {
  838. m_event = LE_TEST_STATUS_EVENT_ERROR;
  839. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  840. }
  841. return DTM_SUCCESS;
  842. }
  843. if (cmd == LE_TEST_END)
  844. {
  845. if (m_state == STATE_IDLE)
  846. {
  847. // Sequencing error - only rx or tx test may be ended!
  848. m_event = LE_TEST_STATUS_EVENT_ERROR;
  849. return DTM_ERROR_INVALID_STATE;
  850. }
  851. m_event = LE_PACKET_REPORTING_EVENT | m_rx_pkt_count;
  852. dtm_test_done();
  853. return DTM_SUCCESS;
  854. }
  855. if (m_state != STATE_IDLE)
  856. {
  857. // Sequencing error - only TEST_END/RESET are legal while test is running
  858. // Note: State is unchanged; ongoing test not affected
  859. m_event = LE_TEST_STATUS_EVENT_ERROR;
  860. return DTM_ERROR_INVALID_STATE;
  861. }
  862. // Check for illegal values of m_phys_ch. Skip the check if the packet is vendor spesific.
  863. if (payload != DTM_PKT_VENDORSPECIFIC && m_phys_ch > PHYS_CH_MAX)
  864. {
  865. // Parameter error
  866. // Note: State is unchanged; ongoing test not affected
  867. m_event = LE_TEST_STATUS_EVENT_ERROR;
  868. return DTM_ERROR_ILLEGAL_CHANNEL;
  869. }
  870. m_rx_pkt_count = 0;
  871. if (cmd == LE_RECEIVER_TEST)
  872. {
  873. // Zero fill all pdu fields to avoid stray data from earlier test run
  874. memset(&m_pdu, 0, DTM_PDU_MAX_MEMORY_SIZE);
  875. radio_prepare(RX_MODE); // Reinitialize "everything"; RF interrupts OFF
  876. m_state = STATE_RECEIVER_TEST;
  877. return DTM_SUCCESS;
  878. }
  879. if (cmd == LE_TRANSMITTER_TEST)
  880. {
  881. // Check for illegal values of m_packet_length. Skip the check if the packet is vendor spesific.
  882. if (m_packet_type != DTM_PKT_TYPE_VENDORSPECIFIC && m_packet_length > DTM_PAYLOAD_MAX_SIZE)
  883. {
  884. // Parameter error
  885. m_event = LE_TEST_STATUS_EVENT_ERROR;
  886. return DTM_ERROR_ILLEGAL_LENGTH;
  887. }
  888. m_pdu.content[DTM_LENGTH_OFFSET] = m_packet_length;
  889. // Note that PDU uses 4 bits even though BLE DTM uses only 2 (the HCI SDU uses all 4)
  890. switch (m_packet_type)
  891. {
  892. case DTM_PKT_PRBS9:
  893. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_PRBS9;
  894. // Non-repeated, must copy entire pattern to PDU
  895. memcpy(m_pdu.content + DTM_HEADER_SIZE, m_prbs_content, m_packet_length);
  896. break;
  897. case DTM_PKT_0X0F:
  898. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0X0F;
  899. // Bit pattern 00001111 repeated
  900. memset(m_pdu.content + DTM_HEADER_SIZE, RFPHY_TEST_0X0F_REF_PATTERN, m_packet_length);
  901. break;
  902. case DTM_PKT_0X55:
  903. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0X55;
  904. // Bit pattern 01010101 repeated
  905. memset(m_pdu.content + DTM_HEADER_SIZE, RFPHY_TEST_0X55_REF_PATTERN, m_packet_length);
  906. break;
  907. case DTM_PKT_0XFF:
  908. m_pdu.content[DTM_HEADER_OFFSET] = DTM_PDU_TYPE_0XFF;
  909. // Bit pattern 11111111 repeated. Only available in coded PHY (Long range).
  910. memset(m_pdu.content + DTM_HEADER_SIZE, RFPHY_TEST_0XFF_REF_PATTERN, m_packet_length);
  911. break;
  912. case DTM_PKT_TYPE_VENDORSPECIFIC:
  913. // The length field is for indicating the vendor specific command to execute.
  914. // The frequency field is used for vendor specific options to the command.
  915. return dtm_vendor_specific_pkt(length, freq);
  916. default:
  917. // Parameter error
  918. m_event = LE_TEST_STATUS_EVENT_ERROR;
  919. return DTM_ERROR_ILLEGAL_CONFIGURATION;
  920. }
  921. // Initialize CRC value, set channel:
  922. radio_prepare(TX_MODE);
  923. // Set the timer to the correct period. The delay between each packet is described in the
  924. // Bluetooth Core Spsification version 4.2 Vol. 6 Part F Section 4.1.6.
  925. mp_timer->CC[0] = dtm_packet_interval_calculate(m_packet_length, m_radio_mode);
  926. // Configure PPI so that timer will activate radio every 625 us
  927. NRF_PPI->CH[0].EEP = (uint32_t)&mp_timer->EVENTS_COMPARE[0];
  928. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_RADIO->TASKS_TXEN;
  929. NRF_PPI->CHENSET = 0x01;
  930. m_state = STATE_TRANSMITTER_TEST;
  931. }
  932. return DTM_SUCCESS;
  933. }
  934. bool dtm_event_get(dtm_event_t *p_dtm_event)
  935. {
  936. bool was_new = m_new_event;
  937. // mark the current event as retrieved
  938. m_new_event = false;
  939. *p_dtm_event = m_event;
  940. // return value indicates whether this value was already retrieved.
  941. return was_new;
  942. }
  943. // =================================================================================================
  944. // Configuration functions (only for parameters not definitely determined by the BLE DTM standard).
  945. // These functions return true if successful, false if value could not be set
  946. /**@brief Function for configuring the output power for transmitter test.
  947. This function may be called directly, or through dtm_cmd() specifying
  948. DTM_PKT_VENDORSPECIFIC as payload, SET_TX_POWER as length, and the dBm value as frequency.
  949. */
  950. bool dtm_set_txpower(uint32_t new_tx_power)
  951. {
  952. // radio->TXPOWER register is 32 bits, low octet a signed value, upper 24 bits zeroed
  953. int8_t new_power8 = (int8_t)(new_tx_power & 0xFF);
  954. // The two most significant bits are not sent in the 6 bit field of the DTM command.
  955. // These two bits are 1's if and only if the tx_power is a negative number.
  956. // All valid negative values have a non zero bit in among the two most significant
  957. // of the 6-bit value.
  958. // By checking these bits, the two most significant bits can be determined.
  959. new_power8 = (new_power8 & 0x30) != 0 ? (new_power8 | 0xC0) : new_power8;
  960. if (m_state > STATE_IDLE)
  961. {
  962. // radio must be idle to change the tx power
  963. return false;
  964. }
  965. if (dtm_radio_validate(new_power8, m_radio_mode) != DTM_SUCCESS)
  966. {
  967. return false;
  968. }
  969. m_tx_power = new_power8;
  970. return true;
  971. }
  972. /**@brief Function for selecting a timer resource.
  973. * This function may be called directly, or through dtm_cmd() specifying
  974. * DTM_PKT_VENDORSPECIFIC as payload, SELECT_TIMER as length, and the timer as freq
  975. *
  976. * @param[in] new_timer Timer id for the timer to use: 0, 1, or 2.
  977. *
  978. * @return true if the timer was successfully changed, false otherwise.
  979. */
  980. bool dtm_set_timer(uint32_t new_timer)
  981. {
  982. if (m_state > STATE_IDLE)
  983. {
  984. return false;
  985. }
  986. return dtm_hw_set_timer(&mp_timer, &m_timer_irq, new_timer);
  987. }
  988. /// @}
  989. #endif // NRF_MODULE_ENABLED(BLE_DTM)