1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* @brief Max supported CTE length in 8us units */
8 #define LLL_DF_MAX_CTE_LEN 20
9 /* @brief Min supported CTE length in 8us units */
10 #define LLL_DF_MIN_CTE_LEN 2
11 
12 /* @brief Min supported length of antenna switching pattern */
13 #define LLL_DF_MIN_ANT_PATTERN_LEN 3
14 
15 /* @brief Macros to name constants informing where CTEInfo may be found within a PDU depending on
16  * a PDU type.
17  */
18 #define CTE_INFO_IN_S1_BYTE true
19 #define CTE_INFO_IN_PAYLOAD false
20 
21 /* @brief Macro to convert length of CTE to [us] */
22 #define CTE_LEN_US(n) ((n) * 8U)
23 
24 #if defined(CONFIG_BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN)
25 #define BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN \
26 	CONFIG_BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN
27 #else
28 #define BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN 0
29 #endif
30 
31 #if defined(CONFIG_BT_CTLR_DF_PER_ADV_CTE_NUM_MAX)
32 #define BT_CTLR_DF_PER_ADV_CTE_NUM_MAX CONFIG_BT_CTLR_DF_PER_ADV_CTE_NUM_MAX
33 #else
34 #define BT_CTLR_DF_PER_ADV_CTE_NUM_MAX 0
35 #endif
36 
37 /* @brief Configuration of Constant Tone Extension for connectionless
38  * transmission.
39  */
40 struct lll_df_adv_cfg {
41 	uint8_t is_enabled:1;
42 	uint8_t is_started:1;
43 	uint8_t cte_length:6;   /* Length of CTE in 8us units */
44 	uint8_t cte_type:2;
45 	uint8_t cte_count:6;
46 	uint8_t ant_sw_len:6;
47 	uint8_t ant_ids[BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN];
48 };
49 
50 /* BT 5.1 Vol 6, Part B, Section 2.5.4 reference period is sampled with 1us
51  * spacing. Thus we may have 8 IQ samples from reference period.
52  */
53 #define IQ_SAMPLE_REF_CNT 8
54 /* BT 5.1 Vol 6, Part B, Section 2.5.4
55  * If 1us sampling slots are supported maximum number of IQ samples in single CTE
56  * is 74 (sample spacing is 2us). If it is not supported maximum number of IQ
57  * samples is 37 (sample spacing is 4us).
58  */
59 #if defined(CONFIG_BT_CTLR_DF_CTE_RX_SAMPLE_1US)
60 #define IQ_SAMPLE_SWITCH_CNT 74
61 #else
62 #define IQ_SAMPLE_SWITCH_CNT 37
63 #endif
64 
65 #define IQ_SAMPLE_TOTAL_CNT ((IQ_SAMPLE_REF_CNT) + (IQ_SAMPLE_SWITCH_CNT))
66 #define IQ_SAMPLE_CNT (PDU_DC_LL_HEADER_SIZE + LL_LENGTH_OCTETS_RX_MAX)
67 
68 #define RSSI_DBM_TO_DECI_DBM(x) (-(x) * 10)
69 /* Macro that represents out of range IQ sample (saturated). Value provided by Radio specifications.
70  * It is not defined by Bluetooth Core specification. This is the vendor specific value.
71  *
72  * Nordic Semiconductor Radio peripheral provides 16 bit wide IQ samples.
73  * BT 5.3 Core specification Vol 4, Part E sections 7.7.65.21 and 7.7.65.22 limit size of
74  * IQ samples to 8 bits.
75  * To mitigate the limited accuracy and losing information about saturated IQ samples a 0x80 value
76  * is selected to serve the purpose.
77  */
78 #define IQ_SAMPLE_SATURATED_16_BIT ((int16_t)0x8000)
79 #define IQ_SAMPLE_SATURATED_8_BIT ((int8_t)0x80)
80 
81 #if defined(CONFIG_BT_CTLR_DF_IQ_SAMPLES_CONVERT_4_BITS_SHIFT)
82 #define IQ_SAMPLE_CONVERT_12_TO_8_BIT(x) ((int16_t)((x) >> 4))
83 #elif defined(CONFIG_BT_CTLR_DF_IQ_SAMPLES_CONVERT_2_BITS_SHIFT)
84 #define IQ_SAMPLE_CONVERT_12_TO_8_BIT(x) ((int16_t)((x) >> 2))
85 #elif defined(CONFIG_BT_CTLR_DF_IQ_SAMPLES_CONVERT_USE_8_LSB)
86 #define IQ_SAMPLE_CONVERT_12_TO_8_BIT(x) ((int8_t)(x))
87 #elif defined(CODNFIG_BT_CTLR_DF)
88 #error "Unsupported IQ samples conversion"
89 #endif
90 
91 /* Structure to store an single IQ sample */
92 struct iq_sample {
93 	int16_t i;
94 	int16_t q;
95 };
96 
97 /* Receive node aimed to report collected IQ samples during CTE receive */
98 struct node_rx_iq_report {
99 	/* hdr member must be a first member of the structure. It can't be moved because
100 	 * it is expected to be in the beginning of a node memory for common handling of
101 	 * all node_rx_xxx types.
102 	 */
103 	struct node_rx_hdr hdr;
104 	uint8_t sample_count;
105 	struct pdu_cte_info cte_info;
106 	uint8_t local_slot_durations;
107 	uint8_t packet_status;
108 	uint8_t rssi_ant_id;
109 	uint8_t chan_idx;
110 	uint16_t event_counter;
111 	union {
112 		uint8_t pdu[0] __aligned(4);
113 		struct iq_sample sample[0];
114 	};
115 };
116 
117 /* @brief Configuration of Constant Tone Extension for connectionless
118  * reception.
119  */
120 struct lll_df_sync_cfg {
121 	uint8_t is_enabled:1;
122 	uint8_t slot_durations:2; /* Bit field where: BIT(0) is 1us, BIT(1) is 2us. */
123 	uint8_t max_cte_count:5;  /* Max number of received CTEs. */
124 	uint8_t cte_count:5;      /* Received CTEs count. */
125 	uint8_t ant_sw_len:7;
126 	uint8_t ant_ids[BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN];
127 };
128 
129 /* Double buffer to store DF sync configuration */
130 struct lll_df_sync {
131 	uint8_t volatile first;
132 	uint8_t last;
133 	struct lll_df_sync_cfg cfg[DOUBLE_BUFFER_SIZE];
134 };
135 
136 /* Parameters for reception of Constant Tone Extension in connected mode */
137 struct lll_df_conn_rx_params {
138 	uint8_t is_enabled:1;
139 	uint8_t ant_sw_len:7;
140 	uint8_t ant_ids[BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN];
141 	uint8_t slot_durations:2; /* Bit field where: BIT(0) is 1us, BIT(1) is 2us. */
142 };
143 
144 /* Double buffer to store receive and sampling configuration for connected mode */
145 struct lll_df_conn_rx_cfg {
146 	/* Stores information if the RX configuration was set at least once.
147 	 * It is required for handling HCI_LE_Connection_CTE_Request_Enable HCI command.
148 	 * See BT 5.3 Core specification Vol 4, Part E, sec. 7.8.85.
149 	 */
150 	uint8_t is_initialized:1;
151 	/* Channel is set only once for a connection vent. The information will be used during CTE
152 	 * RX configuration by ISR handlers.
153 	 */
154 	uint8_t chan:6;
155 	/* Double buffer header must be placed just before memory for the buffer. */
156 	struct dbuf_hdr hdr;
157 	struct lll_df_conn_rx_params params[DOUBLE_BUFFER_SIZE];
158 };
159 
160 /* @brief Structure to store data required to prepare LE Connection IQ Report event or LE
161  * Connectionless IQ Report event.
162  */
163 struct cte_conn_iq_report {
164 	struct pdu_cte_info cte_info;
165 	uint8_t local_slot_durations;
166 	uint8_t packet_status;
167 	uint8_t sample_count;
168 	uint8_t rssi_ant_id;
169 	union {
170 		uint8_t pdu[0] __aligned(4);
171 		struct iq_sample sample[0];
172 	};
173 };
174 
175 /* Configuration for transmission of Constant Tone Extension in connected mode */
176 struct lll_df_conn_tx_cfg {
177 	/* Stores information if the TX configuration was set at least once.
178 	 * It is required for handling HCI_LE_Connection_CTE_Response_Enable HCI command.
179 	 * See BT 5.3 Core specification Vol 4, Part E, sec. 7.8.86.
180 	 */
181 	uint8_t is_initialized:1;
182 	uint8_t ant_sw_len:7;
183 	uint8_t cte_rsp_en:1; /* CTE response is enabled */
184 	uint8_t cte_types_allowed:3; /* Bitfield with allowed CTE types */
185 	uint8_t ant_ids[BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN];
186 };
187