NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_phy.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef H_BLE_PHY_
21#define H_BLE_PHY_
22
23#include "nimble/nimble/include/nimble/hci_common.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/* Forward declarations */
30struct os_mbuf;
31
32/* Channel/Frequency defintions */
33#define BLE_PHY_NUM_CHANS (40)
34#define BLE_PHY_NUM_DATA_CHANS (37)
35#define BLE_PHY_CHAN0_FREQ_MHZ (2402)
36#define BLE_PHY_DATA_CHAN0_FREQ_MHZ (2404)
37#define BLE_PHY_CHAN_SPACING_MHZ (2)
38#define BLE_PHY_NUM_ADV_CHANS (3)
39#define BLE_PHY_ADV_CHAN_START (37)
40
41/* Power */
42#define BLE_PHY_MAX_PWR_DBM (10)
43
44/* Deviation */
45#define BLE_PHY_DEV_KHZ (185)
46#define BLE_PHY_BINARY_ZERO (-BLE_PHY_DEV)
47#define BLE_PHY_BINARY_ONE (BLE_PHY_DEV)
48
49/* Max. clock drift */
50#define BLE_PHY_MAX_DRIFT_PPM (50)
51
52/* Data rate */
53#define BLE_PHY_BIT_RATE_BPS (1000000)
54
55/* Macros */
56#define BLE_IS_ADV_CHAN(chan) (chan >= BLE_PHY_ADV_CHAN_START)
57#define BLE_IS_DATA_CHAN(chan) (chan < BLE_PHY_ADV_CHAN_START)
58
59/* PHY states */
60#define BLE_PHY_STATE_IDLE (0)
61#define BLE_PHY_STATE_RX (1)
62#define BLE_PHY_STATE_TX (2)
63
64/* BLE PHY transitions */
65#define BLE_PHY_TRANSITION_NONE (0)
66#define BLE_PHY_TRANSITION_RX_TX (1)
67#define BLE_PHY_TRANSITION_TX_RX (2)
68
69/* PHY error codes */
70#define BLE_PHY_ERR_RADIO_STATE (1)
71#define BLE_PHY_ERR_INIT (2)
72#define BLE_PHY_ERR_INV_PARAM (3)
73#define BLE_PHY_ERR_NO_BUFS (4)
74#define BLE_PHY_ERR_TX_LATE (5)
75#define BLE_PHY_ERR_RX_LATE (6)
76
77/* Maximun PDU length. Includes LL header of 2 bytes and 255 bytes payload. */
78#define BLE_PHY_MAX_PDU_LEN (257)
79
80/* Wait for response timer */
81typedef void (*ble_phy_tx_end_func)(void *arg);
82
83/* Initialize the PHY */
84int ble_phy_init(void);
85
86/* Set the PHY channel */
87int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
88
89/* Set transmit start time */
90int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
91
92/* Set receive start time */
93int ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
94
95/* Set the transmit end callback and argument */
96void ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg);
97
98typedef uint8_t (*ble_phy_tx_pducb_t)(uint8_t *dptr, void *pducb_arg,
99 uint8_t *hdr_byte);
100
101/* Place the PHY into transmit mode */
102int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans);
103
104/* Place the PHY into receive mode */
105int ble_phy_rx(void);
106
107/* Copies the received PHY buffer into the allocated pdu */
108void ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu);
109
110/* Set the transmit power */
111int ble_phy_txpwr_set(int dbm);
112
113/* Get highest allowed power from range */
114int ble_phy_txpower_round(int dbm);
115
116/* Get the transmit power */
117int ble_phy_txpwr_get(void);
118
119/* Set RX path power compensation value rounded to integer dB */
120void ble_phy_set_rx_pwr_compensation(int8_t compensation);
121
122/* Disable the PHY */
123void ble_phy_disable(void);
124
125#define BLE_PHY_WFR_ENABLE_RX (0)
126#define BLE_PHY_WFR_ENABLE_TXRX (1)
127
128void ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs);
129
130/* Starts rf clock */
131void ble_phy_rfclk_enable(void);
132
133/* Stops rf clock */
134void ble_phy_rfclk_disable(void);
135
136/*
137 * Used to restart reception on same channel after wfr timer expiration or
138 * frame received.
139 */
140void ble_phy_restart_rx(void);
141
142/* Gets the current state of the PHY */
143int ble_phy_state_get(void);
144
145/* Gets current state of transceiver */
146uint8_t ble_phy_xcvr_state_get(void);
147
148/* Returns 'true' if a reception has started */
149int ble_phy_rx_started(void);
150
151/*
152 * Returns the maximum supported tx/rx PDU payload size, in bytes, for data
153 * channel PDUs (this does not apply to advertising channel PDUs). Note
154 * that the data channel PDU is composed of a 2-byte header, the payload, and
155 * an optional MIC. The maximum payload is 251 bytes.
156 */
157uint8_t ble_phy_max_data_pdu_pyld(void);
158
159/* Gets the current access address */
160uint32_t ble_phy_access_addr_get(void);
161
162/* Enable encryption */
163void ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
164 uint8_t is_central);
165
166/* Disable encryption */
167void ble_phy_encrypt_disable(void);
168
169/* Set the packet counters and dir used by LE encyption */
170void ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir);
171
172/* Enable phy resolving list */
173void ble_phy_resolv_list_enable(void);
174
175/* Disable phy resolving list */
176void ble_phy_resolv_list_disable(void);
177
178/*
179 * PHY mode values for 1M, 2M and Coded S=8 are the same as corresponding values
180 * of PHY. This makes conversion between 'phy' and 'phy_mode' easier and it also
181 * means that default coding for Coded will be S=8, unless explicitly translated
182 * to S=2.
183 */
184#define BLE_PHY_MODE_CODED_500KBPS (0)
185#define BLE_PHY_MODE_1M (1)
186#define BLE_PHY_MODE_2M (2)
187#define BLE_PHY_MODE_CODED_125KBPS (3)
188
189/* The number of different modes */
190#define BLE_PHY_NUM_MODE (4)
191
192/* PHY numbers (compatible with HCI) */
193#define BLE_PHY_1M (BLE_HCI_LE_PHY_1M)
194#define BLE_PHY_2M (BLE_HCI_LE_PHY_2M)
195#define BLE_PHY_CODED (BLE_HCI_LE_PHY_CODED)
196
197/* PHY bitmasks (compatible with HCI) */
198#define BLE_PHY_MASK_1M (BLE_HCI_LE_PHY_1M_PREF_MASK)
199#define BLE_PHY_MASK_2M (BLE_HCI_LE_PHY_2M_PREF_MASK)
200#define BLE_PHY_MASK_CODED (BLE_HCI_LE_PHY_CODED_PREF_MASK)
201
202/* PHY indices (for a zero-based array) */
203#define BLE_PHY_IDX_1M (0)
204#define BLE_PHY_IDX_2M (1)
205#define BLE_PHY_IDX_CODED (2)
206
207#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY))
208uint32_t ble_phy_mode_pdu_start_off(int phy);
209void ble_phy_mode_set(uint8_t tx_phy_mode, uint8_t rx_phy_mode);
210#else
211#define ble_phy_mode_pdu_start_off(phy) (40)
212
213#endif
214
215int ble_phy_get_cur_phy(void);
216static inline int ble_ll_phy_to_phy_mode(int phy, int phy_options)
217{
218 int phy_mode;
219
220 /*
221 * 'phy' value can be used as 'phy_mode' value unless S=2 coding is explicitly
222 * required. By default we'll use S=2 for Coded.
223 */
224 phy_mode = phy;
225
226#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
227 if (phy == BLE_PHY_CODED && phy_options == BLE_HCI_LE_PHY_CODED_S2_PREF) {
228 phy_mode = BLE_PHY_MODE_CODED_500KBPS;
229 }
230#else
231 (void)phy_options;
232#endif
233
234 return phy_mode;
235}
236
237#if MYNEWT_VAL(BLE_LL_DTM)
238void ble_phy_enable_dtm(void);
239void ble_phy_disable_dtm(void);
240#endif
241
242#ifdef __cplusplus
243}
244#endif
245
246#endif /* H_BLE_PHY_ */
Definition os_mbuf.h:86