1 /*
2  * Copyright (c) 2020-2024, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ti_drivers_RCL_commands_ble5_h__include
34 #define ti_drivers_RCL_commands_ble5_h__include
35 
36 typedef struct RCL_FL_ENTRY_t     RCL_FL_Entry;
37 typedef struct RCL_FILTER_LIST_t  RCL_FilterList;
38 
39 typedef struct RCL_ADDR_TYPE_t    RCL_AddrType;
40 typedef struct RCL_CONN_PARAMS_t  RCL_ConnParams;
41 
42 typedef struct RCL_CMD_BLE5_ADV_t          RCL_CmdBle5Advertiser;
43 typedef struct RCL_CMD_BLE5_AUX_ADV_t      RCL_CmdBle5AuxAdvertiser;
44 typedef struct RCL_CMD_BLE5_PER_ADV_t      RCL_CmdBle5PeriodicAdvertiser;
45 typedef struct RCL_CMD_BLE5_INITIATOR_t    RCL_CmdBle5Initiator;
46 typedef struct RCL_CMD_BLE5_SCANNER_t      RCL_CmdBle5Scanner;
47 typedef struct RCL_CMD_BLE5_PER_SCANNER_t  RCL_CmdBle5PeriodicScanner;
48 typedef struct RCL_CMD_BLE5_CONNECTION_t   RCL_CmdBle5Connection;
49 typedef struct RCL_CMD_BLE5_DTM_TX         RCL_CmdBle5DtmTx;
50 typedef struct RCL_CMD_BLE5_GENERIC_RX_t   RCL_CmdBle5GenericRx;
51 typedef struct RCL_CMD_BLE5_GENERIC_TX_t   RCL_CmdBle5GenericTx;
52 typedef struct RCL_CMD_BLE5_TX_TEST_t      RCL_CmdBle5TxTest;
53 
54 typedef struct RCL_CTX_ADVERTISER_t        RCL_CtxAdvertiser;
55 typedef struct RCL_CTX_PER_ADVERTISER_t    RCL_CtxPeriodicAdvertiser;
56 typedef struct RCL_CTX_SCAN_INIT_t         RCL_CtxScanInit;
57 typedef struct RCL_CTX_PER_SCANNER_t       RCL_CtxPeriodicScanner;
58 typedef struct RCL_CTX_CONNECTION_t        RCL_CtxConnection;
59 typedef struct RCL_CTX_GENERIC_RX_t        RCL_CtxGenericRx;
60 typedef struct RCL_CTX_GENERIC_TX_t        RCL_CtxGenericTx;
61 
62 typedef struct RCL_STATS_ADV_SCAN_INIT_t   RCL_StatsAdvScanInit;
63 typedef struct RCL_STATS_CONNECTION_t      RCL_StatsConnection;
64 typedef struct RCL_STATS_GENERIC_RX_t      RCL_StatsGenericRx;
65 
66 /**
67  * @brief Type for BLE channels
68  *
69  * 0-39:    BLE channel number, indicates standard BLE whitening
70  * 64-103:  Physical channel number; 64 is 2402 MHz, 65 is 2404 MHz, etc. Whitening disabled
71  * 128-255: Custom frequency programmed with %RCL_BLE5_setDefaultRfFrequency(); custom whitening.
72  *          See %BLE_CUSTOM_FREQ_BM.
73  */
74 typedef uint8_t RCL_Ble5Channel;
75 
76 /**
77  *  @brief PHY returned in status of received packets
78  *
79  */
80 typedef enum {
81     RCL_Ble5_RxPhy1Mbps = 0,
82     RCL_Ble5_RxPhy2Mbps = 1,
83     RCL_Ble5_RxPhyCodedS8 = 2,
84     RCL_Ble5_RxPhyCodedS2 = 3,
85 } RCL_Ble5_RxPhy;
86 
87 /**
88  *  @brief Appended packet status field from RX buffer
89  *
90  */
91 typedef union {
92     struct {
93         RCL_Ble5_RxPhy phy       :2;     /*!< Received PHY */
94         uint8_t crcError         :1;     /*!< True if packet had CRC error */
95         uint8_t ignored          :1;     /*!< True if packet was ignored */
96         uint8_t ignoredRpa       :1;     /*!< True if packet should have been ignored due to unknown RPA, but was kept due to rpaMode */
97         uint8_t ignoredSyncInfo  :1;     /*!< True if packet should have been ignored due to unknown RPA, but was kept due to periodicSyncEstablishment */
98         uint8_t reserved         :2;
99     };
100     uint8_t value;
101 } RCL_Ble5_RxPktStatus;
102 
103 /* Invalid packet status */
104 #define RCL_BLE5_RX_PKT_STATUS_INVALID 0xFF
105 
106 /* Do includes after typedefs, as the types are needed in ti/drivers/rcl/handlers/ble5.h */
107 #include <ti/drivers/rcl/RCL_Command.h>
108 #include <ti/drivers/rcl/handlers/ble5.h>
109 #include <ti/drivers/utils/List.h>
110 
111 #include <ti/devices/DeviceFamily.h>
112 #include DeviceFamily_constructPath(inc/pbe_ble5_ram_regs.h)
113 
114 /* Command IDs for BLE commands */
115 #define RCL_CMDID_BLE5_ADVERTISER              0x1001U
116 #define RCL_CMDID_BLE5_INITIATOR               0x1002U
117 #define RCL_CMDID_BLE5_SCANNER                 0x1003U
118 #define RCL_CMDID_BLE5_CONNECTION              0x1004U
119 #define RCL_CMDID_BLE5_DTM_TX                  0x1005U
120 #define RCL_CMDID_BLE5_GENERIC_RX              0x1006U
121 #define RCL_CMDID_BLE5_GENERIC_TX              0x1007U
122 #define RCL_CMDID_BLE5_TX_TEST                 0x1008U
123 #define RCL_CMDID_BLE5_AUX_ADV                 0x1009U
124 #define RCL_CMDID_BLE5_PERIODIC_ADV            0x100AU
125 #define RCL_CMDID_BLE5_PERIODIC_SCAN           0x100BU
126 
127 /**
128  * @brief Bit mask indicating the use of a custom frequency
129  *
130  * If a channel or chanMap parameter has this bit set, the RF frequency used must be programmed
131  * with %RCL_BLE5_setDefaultRfFrequency(), and the whitening is initialzed by bits 0-6 of channel
132  * or chanMap
133  */
134 #define BLE_CUSTOM_FREQ_BM        0x80U
135 
136 /**
137  *  @brief Filter list entry
138  *
139  *  Accept or reject filter entry
140  */
141 struct RCL_FL_ENTRY_t {
142     union
143     {
144         struct
145         {
146             uint16_t enabled      : 1; /*!< 1 if the entry is used; 0 otherwise */
147             uint16_t addType      : 1; /*!< Address type of this entry */
148             uint16_t duplicateIgn : 1; /*!< 1 to ignore the entry due to duplicate address filtering (scanners only) */
149             uint16_t privIgn      : 1; /*!< 1 to ignore the entry due to privacy (initiators only) */
150             uint16_t reserved     : 12;/*!< Reserved, set to 0 */
151         } ctl;
152         uint16_t ctlWord;
153     };
154     uint16_t address[3];               /*!< Address of entry */
155 };
156 
157 /**
158  *  @brief Filter list object
159  *
160  *  Accept or reject filter list
161  */
162 struct RCL_FILTER_LIST_t {
163     uint32_t     numEntries;
164     RCL_FL_Entry entries[16];
165 };
166 
167 /**
168  *  @brief Address type
169  *
170  *  Address type for own address and peer address
171  */
172 struct RCL_ADDR_TYPE_t {
173     uint8_t peer :1;     /*!< Address type for peer device (0: public. 1: random) */
174     uint8_t own :1;      /*!< Address type for this device (0: public. 1: random) */
175     uint8_t reserved: 6; /*!< Reserved, set to 0 */
176 };
177 
178 /**
179  *  @brief Connection parameters
180  *
181  *  Connection parameters for AUX_CONNECT_REQ based on received AuxPhy
182  */
183 struct RCL_CONN_PARAMS_t {
184     struct
185     {
186         uint16_t interval;
187         uint16_t latency;
188         uint16_t timeout;
189     } ble2M;
190     struct
191     {
192         uint16_t interval;
193         uint16_t latency;
194         uint16_t timeout;
195     } bleCoded;
196 };
197 
198 /**
199  *  @brief Advertiser command
200  *
201  *  Command to run BLE advertiser. The advertisement type is found from the packet type transmitted.
202  */
203 struct RCL_CMD_BLE5_ADV_t {
204     RCL_Command  common;
205     uint8_t chanMap;              /*!< Channel map. Bit positions 0-2 correspond to channels 37-39; a 1 means channel enabled */
206     RCL_Command_TxPower txPower;  /*!< Transmit power */
207     uint8_t order;                /*!< Order to run channels. 0: Run in increasing order. 1-5: Other order. Others: Reserved */
208     uint8_t highDuty;             /*!< High duty-cycle advertising (directed advertising only) 0: Disabled. 1 Enabled */
209     uint32_t connectPktTime;      /*!< Time of received CONNECT_IND or AUX_CONNECT_REQ packet is returned if connection is formed */
210     RCL_CtxAdvertiser *ctx;       /*!< Pointer to context structure */
211     RCL_StatsAdvScanInit *stats;  /*!< Pointer to statistics structure */
212 };
213 
214 #define RCL_CmdBle5Advertiser_Default()                         \
215 {                                                               \
216     .common = RCL_Command_Default(RCL_CMDID_BLE5_ADVERTISER,    \
217                                   RCL_Handler_BLE5_adv),        \
218     .chanMap = 0x7,                                             \
219     .txPower = {.dBm = 0, .fraction = 0},                       \
220     .order = 0,                                                 \
221     .highDuty = 0,                                              \
222     .connectPktTime = 0,                                        \
223     .ctx = NULL,                                                \
224     .stats = NULL,                                              \
225 }
226 #define RCL_CmdBle5Advertiser_DefaultRuntime() (RCL_CmdBle5Advertiser) RCL_CmdBle5Advertiser_Default()
227 
228 /**
229  *  @brief Secondary Channel Advertiser command
230  *
231  *  Command to run BLE advertiser on a secondary channel. The advertisement type is found from the packet type transmitted.
232  */
233 struct RCL_CMD_BLE5_AUX_ADV_t {
234     RCL_Command  common;
235     RCL_Ble5Channel channel;      /*!< Channel index */
236     RCL_Command_TxPower txPower;  /*!< Transmit power */
237     uint32_t connectPktTime;      /*!< Time of received CONNECT_IND packet is returned if connection is formed - Not supported in this release. */
238     RCL_CtxAdvertiser *ctx;       /*!< Pointer to context structure */
239     RCL_StatsAdvScanInit *stats;  /*!< Pointer to statistics structure */
240 };
241 
242 #define RCL_CmdBle5AuxAdvertiser_Default()                      \
243 {                                                               \
244     .common = RCL_Command_Default(RCL_CMDID_BLE5_AUX_ADV,       \
245                                   RCL_Handler_BLE5_aux_adv),    \
246     .channel = 0,                                               \
247     .txPower = {.dBm = 0, .fraction = 0},                       \
248     .ctx = NULL,                                                \
249     .stats = NULL,                                              \
250 }
251 #define RCL_CmdBle5AuxAdvertiser_DefaultRuntime() (RCL_CmdBle5AuxAdvertiser) RCL_CmdBle5AuxAdvertiser_Default()
252 
253 /**
254  *  @brief Advertiser context
255  *
256  *  Context for advertiser command
257  */
258 struct RCL_CTX_ADVERTISER_t {
259     RCL_FilterList *filterListConn;     /*!< Filter list for initiator packets */
260     RCL_FilterList *filterListScan;     /*!< Filter list for scanner packets */
261     List_List txBuffers;                /*!< Linked list of packets to transmit: Advertisement followed by scan response */
262     List_List rxBuffers;                /*!< Linked list of buffers for storing received packets */
263     uint16_t advA[3];                   /*!< Advertiser address of type %addrType.own */
264     uint16_t peerA[3];                  /*!< Directed advertising: Peer device address of type %addrType.peer */
265     RCL_AddrType addrType;              /*!< Address types */
266     uint8_t filterPolicy: 2;            /*!< Filter policy */
267     uint8_t privIgnMode: 1;             /*!< Privacy ignore mode. 0: Use filter list only when filter policy says. 1: Use filter list to ignore packets with privIgn bit set for all filter policies */
268     uint8_t rpaModePeer: 1;             /*!< RPA mode for peer address. 0: Treat RPA normally. 1: Report packets where advertiser address is an unknown RPA */
269     uint8_t acceptAllRpaConnectInd: 1;  /*!< CONNECT_IND RPA treatment. 0: Treat RPA in InitA normally. 1: Accept all RPA in InitA of CONNECT_IND. */
270 };
271 
272 #define RCL_CtxAdvertiser_Default() \
273 {                                   \
274     .filterListConn = NULL,         \
275     .filterListScan = NULL,         \
276     .txBuffers = { 0 },             \
277     .rxBuffers = { 0 },             \
278     .advA = { 0 },                  \
279     .peerA = { 0 },                 \
280     .addrType =  { 0 },             \
281     .filterPolicy = 0,              \
282     .privIgnMode = 0,               \
283     .rpaModePeer = 0,               \
284     .acceptAllRpaConnectInd = 0     \
285 }
286 #define RCL_CtxAdvertiser_DefaultRuntime() (RCL_CtxAdvertiser) RCL_CtxAdvertiser_Default()
287 
288 /**
289  *  @brief Periodic advertiser command
290  *
291  *  Command to run BLE periodic advertiser.
292  */
293 struct RCL_CMD_BLE5_PER_ADV_t {
294     RCL_Command  common;
295     RCL_Ble5Channel channel;         /*!< Channel index */
296     RCL_Command_TxPower txPower;     /*!< Transmit power */
297     RCL_CtxPeriodicAdvertiser *ctx;  /*!< Pointer to context structure */
298     RCL_StatsAdvScanInit *stats;     /*!< Pointer to statistics structure */
299 };
300 
301 #define RCL_CmdBle5PeriodicAdvertiser_Default()                         \
302 {                                                                       \
303     .common = RCL_Command_Default(RCL_CMDID_BLE5_PERIODIC_ADV,          \
304                                   RCL_Handler_BLE5_periodicAdv),        \
305     .channel = 0,                                                       \
306     .txPower = {.dBm = 0, .fraction = 0},                               \
307     .ctx = NULL,                                                        \
308     .stats = NULL,                                                      \
309 }
310 #define RCL_CmdBle5PeriodicAdvertiser_DefaultRuntime() (RCL_CmdBle5PeriodicAdvertiser) RCL_CmdBle5PeriodicAdvertiser_Default()
311 
312 /**
313  *  @brief Periodic Advertiser context
314  *
315  *  Context for periodic advertiser command
316  */
317 struct RCL_CTX_PER_ADVERTISER_t {
318     List_List txBuffers;       /*!< Linked list of packets to transmit. Only AUX_SYNC_IND and AUX_CHAIN_IND */
319     uint32_t accessAddress;    /*!< Access address */
320     uint32_t crcInit;          /*!< CRC initialization value (24 bits) */
321 };
322 
323 #define RCL_CtxPeriodicAdvertiser_Default() \
324 {                                           \
325     .txBuffers = { 0 },                     \
326     .accessAddress = 0,                     \
327     .crcInit = 0,                           \
328 }
329 #define RCL_CtxPeriodicAdvertiser_DefaultRuntime() (RCL_CtxPeriodicAdvertiser) RCL_CtxPeriodicAdvertiser_Default()
330 
331 /**
332  *  @brief Initiator command
333  *
334  *  Command to run BLE initiator.
335  */
336 struct RCL_CMD_BLE5_INITIATOR_t {
337     RCL_Command common;
338     RCL_Ble5Channel channel;      /*!< Channel index */
339     RCL_Command_TxPower txPower;  /*!< Transmit power */
340     uint16_t maxAuxPtrWaitTime;   /*!< Maximum time to wait for AuxPtr before ending command (1 us units). 0: No limit */
341     bool dynamicWinOffset;        /*!< Window offset processing. 0: Fixed. 1: Dynamic */
342     bool acceptLegacy : 1;        /*!< Accept legacy advertising. 0: Do not accept. 1: Accept */
343     bool acceptExtended : 1;      /*!< Accept extended advertising. 0: Do not accept. 1: Accept */
344     uint32_t connectTime;         /*!< For dynamic window offset, wanted connect time is given as input. In all cases, actual connect time is returned. */
345     RCL_CtxScanInit *ctx;         /*!< Pointer to context structure */
346     RCL_StatsAdvScanInit *stats;  /*!< Pointer to statistics structure */
347 };
348 
349 #define RCL_CmdInitiator_Default()                                  \
350 {                                                                   \
351     .common = RCL_Command_Default(RCL_CMDID_BLE5_INITIATOR,         \
352                                   RCL_Handler_BLE5_scan_init),      \
353     .channel = 37,                                                  \
354     .txPower = {.dBm = 0, .fraction = 0},                           \
355     .maxAuxPtrWaitTime = 30000,                                     \
356     .dynamicWinOffset = 0,                                          \
357     .acceptLegacy = 1,                                              \
358     .acceptExtended = 0,                                            \
359     .connectTime = 0,                                               \
360     .ctx = NULL,                                                    \
361     .stats = NULL,                                                  \
362 }
363 #define RCL_CmdInitiator_DefaultRuntime() (RCL_CmdBle5Initiator) RCL_CmdInitiator_Default()
364 
365 /**
366  *  @brief Scanner command
367  *
368  *  Command to run BLE scanner
369  */
370 struct RCL_CMD_BLE5_SCANNER_t {
371     RCL_Command common;
372     RCL_Ble5Channel channel;       /*!< Channel index */
373     RCL_Command_TxPower txPower;   /*!< Transmit power */
374     uint16_t maxAuxPtrWaitTime;    /*!< Maximum time to wait for AuxPtr before ending command (1 us units). 0: No limit */
375     bool activeScan;               /*!< Scan type. 0: Passive. 1: Active */
376     bool acceptLegacy : 1;         /*!< Accept legacy advertising. 0: Do not accept. 1: Accept */
377     bool acceptExtended : 1;       /*!< Accept extended advertising. 0: Do not accept. 1: Accept */
378     RCL_CtxScanInit *ctx;          /*!< Pointer to context structure */
379     RCL_StatsAdvScanInit *stats;   /*!< Pointer to statistics structure */
380 };
381 
382 #define RCL_CmdScanner_Default()                                    \
383 {                                                                   \
384     .common = RCL_Command_Default(RCL_CMDID_BLE5_SCANNER,           \
385                                   RCL_Handler_BLE5_scan_init),      \
386     .channel = 37,                                                  \
387     .txPower = {.dBm = 0, .fraction = 0},                           \
388     .maxAuxPtrWaitTime = 20000,                                     \
389     .activeScan = 0,                                                \
390     .acceptLegacy = 1,                                              \
391     .acceptExtended = 0,                                            \
392     .ctx = NULL,                                                    \
393     .stats = NULL,                                                  \
394 }
395 #define RCL_CmdScanner_DefaultRuntime() (RCL_CmdBle5Scanner) RCL_CmdScanner_Default()
396 
397 /**
398  *  @brief Scanner and initiator context
399  *
400  *  Context for scanner or initiator command
401  */
402 struct RCL_CTX_SCAN_INIT_t {
403     RCL_FilterList *filterList;           /*!< Filter list */
404     List_List txBuffers;                  /*!< Linked list of packets to transmit: Only CONNECT_IND and AUX_CONNECT_REQ */
405     List_List rxBuffers;                  /*!< Linked list of buffers for storing received packets */
406     uint16_t ownA[3];                     /*!< Own device address of type %addrType.own */
407     uint16_t peerA[3];                    /*!< Initiator: Peer device address of type %addrType.peer */
408     RCL_AddrType addrType;                /*!< Address types */
409     uint8_t filterPolicy : 1;             /*!< Filter policy */
410     uint8_t scanExtFilterPolicy: 1;       /*!< Extended filter policy for scanners */
411     uint8_t rpaModeOwn: 1;                /*!< RPA mode for own address. 0: Treat RPA normally. 1: Report packets where target address is an unknown RPA */
412     uint8_t rpaModePeer: 1;               /*!< RPA mode for peer address. 0: Treat RPA normally. 1: Report packets where advertiser address is an unknown RPA */
413     uint8_t acceptAllRpaConnectRsp: 1;    /*!< AUX_CONNECT_RSP RPA treatment. 0: Treat RPA in TargetA normally. 1: Accept all RPA in TargetA of AUX_CONNECT_RSP - Not supported in this release */
414     uint8_t periodicSyncEstablishment: 1; /*!< Synchronization to periodic advertisement. 0: Disabled. 1: Report all packets with SyncInfo present */
415     uint16_t initialBackoff;              /*!< Initial backoff value */
416     uint8_t backoffUpper;                 /*!< Backoff parameter */
417     struct
418     {
419         uint8_t backOffLastFail : 1;
420         uint8_t backOffLastSucceed : 1;
421         uint8_t reserved  : 6;
422     } backoffStatus;                      /*!< Backoff parameter */
423     uint16_t localClockAccuracy;          /*!< Maximum relative local clock error (in ppm) scaled by 2^26 */
424     RCL_ConnParams *connParams;           /*!< Pointer to connection parameters structure (LE 2M and LE Coded only). LE 1M parameters are provided in the default CONNECT_IND/AUX_CONNECT_REQ */
425 };
426 
427 #define RCL_CtxScanInit_Default()   \
428 {                                   \
429     .filterList = NULL,             \
430     .txBuffers = { 0 },             \
431     .rxBuffers = { 0 },             \
432     .ownA = { 0 },                  \
433     .peerA = { 0 },                 \
434     .addrType =  { 0 },             \
435     .filterPolicy = 0,              \
436     .scanExtFilterPolicy = 0,       \
437     .rpaModeOwn = 0,                \
438     .rpaModePeer = 0,               \
439     .acceptAllRpaConnectRsp = 0,    \
440     .periodicSyncEstablishment = 0, \
441     .initialBackoff = 1,            \
442     .backoffUpper = 0,              \
443     .backoffStatus = { 0 },         \
444     .localClockAccuracy = 3355,     \
445     .connParams = NULL,             \
446 }
447 #define RCL_CtxScanInit_DefaultRuntime() (RCL_CtxScanInit) RCL_CtxScanInit_Default()
448 
449 /**
450  *  @brief Periodic Scanner command
451  *
452  *  Command to run BLE periodic scanner
453  */
454 struct RCL_CMD_BLE5_PER_SCANNER_t {
455     RCL_Command common;
456     RCL_Ble5Channel channel;       /*!< Channel index */
457     RCL_Command_TxPower txPower;   /*!< Transmit power */
458     uint16_t maxAuxPtrWaitTime;    /*!< Maximum time to wait for AuxPtr before ending command (1 us units). 0: No limit */
459     RCL_CtxPeriodicScanner *ctx;   /*!< Pointer to context structure */
460     RCL_StatsAdvScanInit *stats;   /*!< Pointer to statistics structure */
461 };
462 
463 #define RCL_CmdPeriodicScanner_Default()                            \
464 {                                                                   \
465     .common = RCL_Command_Default(RCL_CMDID_BLE5_PERIODIC_SCAN,     \
466                                   RCL_Handler_BLE5_periodicScan),   \
467     .channel = 37,                                                  \
468     .txPower = {.dBm = 0, .fraction = 0},                           \
469     .maxAuxPtrWaitTime = 20000,                                     \
470     .ctx = NULL,                                                    \
471     .stats = NULL,                                                  \
472 }
473 #define RCL_CmdPeriodicScanner_DefaultRuntime() (RCL_CmdBle5PeriodicScanner) RCL_CmdPeriodicScanner_Default()
474 
475 /**
476  *  @brief Periodic scanner context
477  *
478  *  Context for periodic scanner command
479  */
480 struct RCL_CTX_PER_SCANNER_t {
481     List_List rxBuffers;                  /*!< Linked list of buffers for storing received packets */
482     uint16_t localClockAccuracy;          /*!< Maximum relative local clock error (in ppm) scaled by 2^26 */
483     uint32_t accessAddress;               /*!< Access address */
484     uint32_t crcInit;                     /*!< CRC initialization value (24 bits) */
485 };
486 
487 #define RCL_CtxPeriodicScanner_Default()   \
488 {                                          \
489     .rxBuffers = { 0 },                    \
490     .localClockAccuracy = 3355,            \
491     .accessAddress = 0,                    \
492     .crcInit = 0,                          \
493 }
494 #define RCL_CtxPeriodicScanner_DefaultRuntime() (RCL_CtxPeriodicScanner) RCL_CtxPeriodicScanner_Default()
495 
496 /**
497  *  @brief Statistics structure for advertiser, scanner and initiator
498  *
499  *  Statistics for advertiser, scanner or initiator command
500  */
501 struct RCL_STATS_ADV_SCAN_INIT_t {
502     struct
503     {
504         uint8_t accumulate : 1;      /*!< 0: Reset counters to 0 at start of command. 1: Add to incoming value of counters. */
505         uint8_t activeUpdate : 1;    /*!< 0: Update only at end of command. 1: Update after receiving or transmitting packets. */
506         uint8_t reserved : 6;        /*!< Reserved, set to 0 */
507     } config;                        /*!< Configuration provided to RCL */
508     uint8_t   timestampValid;        /*!< Returns 1 if %lastTimestamp is updated; 0 otherwise */
509     int8_t    lastRssi;              /*!< RSSI of last received packet */
510     uint32_t  lastTimestamp;         /*!< Timestamp of last successfully received packet */
511     uint16_t  nRxNok;                /*!< Number of packets received with CRC error */
512     uint16_t  nRxIgnored;            /*!< Number of packets to be ignored received */
513     uint16_t  nRxOk;                 /*!< Number of correctly received, accepted packets */
514     uint8_t   nRxFifoFull;           /*!< Number of packets received which could not be stored */
515     uint8_t   nTx;                   /*!< Number of packets transmitted */
516 };
517 
518 #define RCL_StatsAdvScanInit_Default()  \
519 {                                       \
520     .config = { 0 },                    \
521     .timestampValid = 0,                \
522     .lastRssi = LRF_RSSI_INVALID,       \
523 }
524 #define RCL_StatsAdvScanInit_DefaultRuntime() (RCL_StatsAdvScanInit) RCL_StatsAdvScanInit_Default()
525 
526 /**
527  *  @brief Connection command
528  *
529  *  Command to run connection event as central or peripheral
530  */
531 struct RCL_CMD_BLE5_CONNECTION_t {
532     RCL_Command common;
533     RCL_Ble5Channel channel;      /*!< Channel index */
534     RCL_Command_TxPower txPower;  /*!< Transmit power */
535     uint16_t relRxTimeoutTime;    /*!< Peripheral only: Time before timing out the first packet of the event (0.25 us units). 0: No timeout. */
536     RCL_CtxConnection *ctx;       /*!< Pointer to context structure */
537     RCL_StatsConnection *stats;   /*!< Pointer to statistics structure */
538 };
539 
540 #define RCL_CmdBle5Connection_Default()                             \
541 {                                                                   \
542     .common = RCL_Command_Default(RCL_CMDID_BLE5_CONNECTION,        \
543                                   RCL_Handler_BLE5_conn),           \
544     .channel = 0,                                                   \
545     .txPower = {.dBm = 0, .fraction = 0},                           \
546     .ctx = NULL,                                                    \
547     .stats = NULL,                                                  \
548 }
549 #define RCL_CmdBle5Connection_DefaultRuntime() (RCL_CmdBle5Connection) RCL_CmdBle5Connection_Default()
550 
551 /**
552  *  @brief Connection context
553  *
554  *  Context for a connection
555  */
556 struct RCL_CTX_CONNECTION_t {
557     List_List txBuffers;       /*!< Linked list of packets to transmit. RCL will pop packets that have been ACKed */
558     List_List rxBuffers;       /*!< Linked list of buffers for storing received packets */
559     bool      isPeripheral;    /*!< 0: Run as central. 1: Run as peripheral */
560     uint16_t  seqStat;         /*!< Sequencer status. Use default initialization for a new connection and keep unchanged between connection events on the same connection. */
561     uint32_t  accessAddress;   /*!< Access address */
562     uint32_t  crcInit;         /*!< CRC initialization value (24 bits) */
563 };
564 
565 #define _INIT_SEQSTAT (PBE_BLE5_RAM_SEQSTAT_LASTRXSN_M | \
566                        PBE_BLE5_RAM_SEQSTAT_LASTTXSN_M | \
567                        PBE_BLE5_RAM_SEQSTAT_FIRSTPKT_M )
568 
569 #define RCL_CtxConnection_Default() \
570 {                                   \
571     .txBuffers = { 0 },             \
572     .rxBuffers = { 0 },             \
573     .isPeripheral = 0,              \
574     .seqStat = _INIT_SEQSTAT,       \
575     .accessAddress = 0,             \
576     .crcInit = 0,                   \
577 }
578 #define RCL_CtxConnection_DefaultRuntime() (RCL_CtxConnection) RCL_CtxConnection_Default()
579 
580 /**
581  *  @brief Statistics structure for connection
582  *
583  *  Statistics for connection command
584  */
585 struct RCL_STATS_CONNECTION_t {
586     struct
587     {
588         uint8_t accumulate : 1;      /*!< 0: Reset counters to 0 at start of command. 1: Add to incoming value of counters. */
589         uint8_t activeUpdate : 1;    /*!< 0: Update only at end of command. 1: Update after receiving or transmitting packets. */
590         uint8_t reserved : 6;        /*!< Reserved, set to 0 */
591     } config;                        /*!< Configuration provided to RCL */
592     uint8_t   anchorValid;           /*!< Returns 1 if %anchorPoint is updated; 0 otherwise */
593     int8_t    lastRssi;              /*!< RSSI of last received packet */
594     uint32_t  anchorPoint;           /*!< Peripheral only: Timestamp of first received packet */
595     uint8_t   nTxDone;               /*!< Number of TX buffers finished because ACK is received */
596     uint8_t   nTxAck;                /*!< Number of ACKs received on transmitted packets */
597     uint8_t   nTxCtlAck;             /*!< Number of ACKs received on transmitted control packets */
598     uint8_t   nTxCtl;                /*!< Number of control packets transmitted */
599     uint8_t   nTxRetrans;            /*!< Number of packets retransmitted */
600     uint8_t   nRxNok;                /*!< Number of packets received with CRC error */
601     uint8_t   nRxIgnored;            /*!< Number of packets to be ignored received */
602     uint8_t   nRxEmpty;              /*!< Number of empty packets received */
603     uint8_t   nRxFifoFull;           /*!< Number of packets received which could not be stored */
604     uint8_t   nRxOk;                 /*!< Number of correctly received, accepted packets */
605     uint8_t   nTx;                   /*!< Number of packets transmitted */
606     uint8_t   nRxCtl;                /*!< Number of control packets received */
607     uint8_t   nRxCtlAck;             /*!< Number of ACKs transmitted on received control packets */
608 };
609 
610 #define RCL_StatsConnection_Default()   \
611 {                                       \
612     .config = { 0 },                    \
613     .anchorValid = 0,                   \
614     .lastRssi = LRF_RSSI_INVALID,       \
615 }
616 #define RCL_StatsConnection_DefaultRuntime() (RCL_StatsConnection) RCL_StatsConnection_Default()
617 
618 /**
619  *  @brief DTM TX command
620  *
621  *  Command to send BLE direct test mode packets
622  */
623 struct RCL_CMD_BLE5_DTM_TX {
624     RCL_Command common;
625     RCL_Ble5Channel channel;      /*!< Channel index */
626     RCL_Command_TxPower txPower;  /*!< Transmit power */
627     uint8_t   pduHeader;          /*!< PDU header to transmit; payload is given according to BLE DTM spec */
628     uint8_t   pduLength;          /*!< PDU length */
629     uint8_t   cteInfo;            /*!< CTE info; decides length of CTE. Ignored if bit #5 of %pduHeader is 0. Not supported in this version. */
630     uint16_t  periodUs;           /*!< Time between start of each packet in 1 us units */
631     uint16_t  numPackets;         /*!< Number of packets to transmit. 0: Transmit indefinitely until command times out or is stopped. */
632 };
633 
634 #define RCL_CmdBle5DtmTx_Default()                          \
635 {                                                           \
636     .common = RCL_Command_Default(RCL_CMDID_BLE5_DTM_TX,    \
637                                   RCL_Handler_BLE5_dtmTx),  \
638     .channel = 64,                                          \
639     .txPower = {.dBm = 0, .fraction = 0},                   \
640     .pduHeader = 0x00,                                      \
641     .pduLength = 37,                                        \
642     .cteInfo = 0x14,                                        \
643     .periodUs = 625,                                        \
644     .numPackets = 0,                                        \
645 }
646 #define RCL_CmdBle5DtmTx_DefaultRuntime() (RCL_CmdBle5DtmTx) RCL_CmdBle5DtmTx_Default()
647 
648 /**
649  *  @brief Generic RX command
650  *
651  *  Command to receive generic BLE packets
652  */
653 struct RCL_CMD_BLE5_GENERIC_RX_t {
654     RCL_Command common;
655     RCL_Ble5Channel channel;       /*!< Channel index */
656     RCL_CtxGenericRx *ctx;         /*!< Pointer to context structure */
657     RCL_StatsGenericRx *stats;     /*!< Pointer to statistics structure */
658 };
659 
660 #define RCL_CmdBle5GenericRx_Default()                              \
661 {                                                                   \
662     .common = RCL_Command_Default(RCL_CMDID_BLE5_GENERIC_RX,        \
663                                   RCL_Handler_BLE5_genericRx),      \
664     .channel = 64,                                                  \
665     .ctx = NULL,                                                    \
666     .stats = NULL,                                                  \
667 }
668 #define RCL_CmdBle5GenericRx_DefaultRuntime() (RCL_CmdBle5GenericRx) RCL_CmdBle5GenericRx_Default()
669 
670 /**
671  *  @brief Generic RX context
672  *
673  *  Context for generic RX command
674  */
675 struct RCL_CTX_GENERIC_RX_t {
676     List_List rxBuffers;       /*!< Linked list of buffers for storing received packets */
677     uint32_t  accessAddress;   /*!< Access address */
678     uint32_t  crcInit;         /*!< CRC initialization value (24 bits) */
679     uint8_t   maxPktLen;       /*!< Maximum payload length of received packets */
680     struct {
681         uint8_t  repeated: 1;         /*!< 0: End after receiving one packet. 1: Go back to sync search after receiving. */
682         uint8_t  disableSync: 1;      /*!< 0: Listen for sync 1: Do not listen for sync */
683         uint8_t  discardRxPackets: 1; /*!< 0: Store received packets in rxBuffers. 1: Do not store packets, useful for link tests where CRC result is enough */
684         uint8_t  reserved: 4;         /*!< Reserved, set to 0 */
685     } config;
686 };
687 
688 #define RCL_CtxGenericRx_Default()  \
689 {                                   \
690     .rxBuffers = { 0 },             \
691     .accessAddress = 0x71764129U,   \
692     .crcInit = 0x555555,            \
693     .config = {                     \
694         .repeated = 1,              \
695         .disableSync = 0,           \
696         .discardRxPackets = 1,      \
697         .reserved = 0,              \
698     },                              \
699 }
700 #define RCL_CtxGenericRx_DefaultRuntime() (RCL_CtxGenericRx) RCL_CtxGenericRx_Default()
701 
702 /**
703  *  @brief Statistics structure for generic RX
704  *
705  *  Statistics for generic RX command
706  */
707 struct RCL_STATS_GENERIC_RX_t {
708     struct
709     {
710         uint8_t accumulate : 1;      /*!< 0: Reset counters to 0 at start of command. 1: Add to incoming value of counters. */
711         uint8_t activeUpdate : 1;    /*!< 0: Update only at end of command. 1: Update after receiving or transmitting packets. */
712         uint8_t reserved : 6;        /*!< Reserved, set to 0 */
713     } config;                        /*!< Configuration provided to RCL */
714     uint8_t   timestampValid;        /*!< Returns 1 if %lastTimestamp is updated; 0 otherwise */
715     int8_t    lastRssi;              /*!< RSSI of last received packet */
716     uint32_t  lastTimestamp;         /*!< Timestamp of last successfully received packet */
717     uint16_t  nRxNok;                /*!< Number of packets received with CRC error */
718     uint16_t  nRxOk;                 /*!< Number of correctly received, accepted packets */
719     uint8_t   nRxFifoFull;           /*!< Number of packets received which could not be stored */
720 };
721 
722 #define RCL_StatsGenericRx_Default()    \
723 {                                       \
724     .config =  { 0 },                   \
725     .timestampValid = 0,                \
726     .lastRssi = LRF_RSSI_INVALID,       \
727 }
728 #define RCL_StatsGenericRx_DefaultRuntime() (RCL_StatsGenericRx) RCL_StatsGenericRx_Default()
729 
730 /**
731  *  @brief Generic TX command
732  *
733  *  Command to transmit a generic BLE packet with no restriction on packet contents
734  */
735 struct RCL_CMD_BLE5_GENERIC_TX_t {
736     RCL_Command common;
737     RCL_Ble5Channel channel;       /*!< Channel index */
738     RCL_Command_TxPower txPower;   /*!< Transmit power */
739     RCL_CtxGenericTx *ctx;         /*!< Pointer to context structure */
740 };
741 
742 #define RCL_CmdBle5GenericTx_Default()                              \
743 {                                                                   \
744     .common = RCL_Command_Default(RCL_CMDID_BLE5_GENERIC_TX,        \
745                                   RCL_Handler_BLE5_genericTx),      \
746     .channel = 64,                                                  \
747     .txPower = {.dBm = 0, .fraction = 0},                           \
748     .ctx = NULL,                                                    \
749 }
750 #define RCL_CmdBle5GenericTx_DefaultRuntime() (RCL_CmdBle5GenericTx) RCL_CmdBle5GenericTx_Default()
751 
752 /**
753  *  @brief Generic TX context
754  *
755  *  Context for generic TX command
756  */
757 struct RCL_CTX_GENERIC_TX_t {
758     List_List txBuffers;            /*!< Linked list of packets to transmit. First packet will be transmitted and consumed. */
759     uint32_t  accessAddress;        /*!< Access address */
760     uint32_t  crcInit;              /*!< CRC initialization value (24 bits) */
761 };
762 
763 #define RCL_CtxGenericTx_Default()  \
764 {                                   \
765     .txBuffers = { 0 },             \
766     .accessAddress = 0x71764129U,   \
767     .crcInit = 0x555555,            \
768 }
769 #define RCL_CtxGenericTx_DefaultRuntime() (RCL_CtxGenericTx) RCL_CtxGenericTx_Default()
770 
771 /**
772  *  @brief BLE5 transmitter test command
773  *
774  *  Command to transmit continuously, either a modulated signal or continuous wave
775  */
776 struct RCL_CMD_BLE5_TX_TEST_t {
777     RCL_Command  common;
778     uint8_t      channel;           /*!< Channel index */
779     RCL_Command_TxPower txPower;    /*!< Transmit power */
780     uint16_t     txWord;            /*!< Repeated word to transmit */
781     struct {
782         uint8_t  whitenMode: 2;     /*!< 0. No or default whitening. 1: PRBS-9. 2: PRBS-15. 3: PRBS-32 */
783         uint8_t  sendCw: 1;         /*!< 0: Send modulated signal. 1: Send CW */
784         uint8_t  reserved: 5;       /*!< Reserved, set to 0 */
785     } config;
786 };
787 
788 #define RCL_CmdBle5TxTest_Default()                             \
789 {                                                               \
790     .common = RCL_Command_Default(RCL_CMDID_BLE5_TX_TEST,       \
791                                   RCL_Handler_Ble5_txTest),     \
792     .channel = 64,                                              \
793     .txPower = {.dBm = 0, .fraction = 0},                       \
794     .txWord = 0,                                                \
795     .config = {                                                 \
796         .whitenMode = 2,                                        \
797         .sendCw = 0,                                            \
798         .reserved = 0,                                          \
799     },                                                          \
800 }
801 #define RCL_CmdBle5TxTest_DefaultRuntime() (RCL_CmdBle5TxTest) RCL_CmdBle5TxTest_Default()
802 
803 #define RCL_CMD_BLE5_WH_MODE_DEFAULT  0 /*!< config.whitenMode: Default (or no) whitening */
804 #define RCL_CMD_BLE5_WH_MODE_PRBS9    1 /*!< config.whitenMode: PRBS-9  */
805 #define RCL_CMD_BLE5_WH_MODE_PRBS15   2 /*!< config.whitenMode: PRBS-15 */
806 #define RCL_CMD_BLE5_WH_MODE_PRBS32   3 /*!< config.whitenMode: PRBS-32 */
807 
808 #endif
809