uwb  2.0.0.0
uwb.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef UWB_H
36 #define UWB_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 #include "spi_specifics.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define UWB_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define UWB_OK 0
78 #define UWB_ERROR -1
79 
85 #define UWB_SUB_NO 0x00
86 
92 #define UWB_REG_DEV_ID 0x00
93 #define UWB_REG_EU_ID 0x01
94 #define UWB_REG_PAN_ID 0x03
95 #define UWB_REG_SYS_CFG 0x04
96 #define UWB_REG_SYS_CNT 0x06
97 #define UWB_REG_SYS_TX_CTRL 0x08
98 #define UWB_REG_TX_DATA_BUF 0x09
99 #define UWB_REG_DX_TIME 0x0A
100 #define UWB_REG_RX_TIMEOUT 0x0C
101 #define UWB_REG_SYS_CTRL 0x0D
102 #define UWB_REG_SYS_EVENT_MASK 0x0E
103 #define UWB_REG_EVENT_STATUS 0x0F
104 #define UWB_REG_RX_INFO 0x10
105 #define UWB_REG_RX_BUF 0x11
106 #define UWB_REG_RX_QUALITY_INFO 0x12
107 #define UWB_REG_RX_TIME_INTERVAL 0x13
108 #define UWB_REG_RX_TIME_OFFSET 0x14
109 #define UWB_REG_RX_MESSAGE_TOA 0x15
110 #define UWB_REG_TX_MESSAGE_TOS 0x17
111 #define UWB_REG_TX_ANTD 0x18
112 #define UWB_REG_SYS_STATE 0x19
113 #define UWB_REG_ACK_RESPONSE_TIME 0x1A
114 #define UWB_REG_RX_SNIFF_CFG 0x1D
115 #define UWB_REG_TX_POWER 0x1E
116 #define UWB_REG_CHN_CTRL 0x1F
117 #define UWB_REG_SFD 0x21
118 #define UWB_REG_AUTO_GAIN_CFG 0x23
119 #define UWB_REG_EXTERNAL_SYNC 0x24
120 #define UWB_REG_READ_AA_DATA 0x25
121 #define UWB_REG_GPIO_CTRL 0x26
122 #define UWB_REG_DRX_CFG 0x27
123 #define UWB_REG_RF_CFG 0x28
124 #define UWB_REG_TX_CALIBRATION 0x2A
125 #define UWB_REG_FREQ_SYNTH_CTRL 0x2B
126 #define UWB_REG_ALWAYS_ON_REG 0x2C
127 #define UWB_REG_OTP_INTERFACE 0x2D
128 #define UWB_REG_LEAD_EDGE_DET_CTRL 0x2E
129 #define UWB_REG_DIGITAL_DIAG_IF 0x2F
130 #define UWB_REG_POWER_MANAGE_SYS_CTRL 0x36
131 
137 #define UWB_OTP_WRITE 0x00
138 #define UWB_OTP_ADR 0x04
139 #define UWB_OTP_CTRL 0x06
140 #define UWB_OTP_STATUS 0x08
141 #define UWB_OTP_RDAT 0x0A
142 #define UWB_OTP_SRDAT 0x0E
143 #define UWB_OTP_SF 0x12
144 
150 #define UWB_DATA_RATE_110KBSPS 0x00
151 #define UWB_DATA_RATE_850KBSPS 0x01
152 #define UWB_DATA_RATE_6800KBSPS 0x02
153 
159 #define UWB_PULSTE_FREQ_4MHZ 0x00
160 #define UWB_PULSTE_FREQ_16MHZ 0x01
161 #define UWB_PULSTE_FREQ_64MHZ 0x02
162 
168 #define UWB_PREAMBLE_LEN_64 0x01
169 #define UWB_PREAMBLE_LEN_128 0x05
170 #define UWB_PREAMBLE_LEN_256 0x09
171 #define UWB_PREAMBLE_LEN_512 0x0D
172 #define UWB_PREAMBLE_LEN_1024 0x02
173 #define UWB_PREAMBLE_LEN_1536 0x06
174 #define UWB_PREAMBLE_LEN_2048 0x0A
175 #define UWB_PREAMBLE_LEN_4096 0x03
176 
182 #define UWB_PAC_SIZE_8 8
183 #define UWB_PAC_SIZE_16 16
184 #define UWB_PAC_SIZE_32 32
185 #define UWB_PAC_SIZE_64 64
186 
192 #define UWB_CHANNEL_1 1
193 #define UWB_CHANNEL_2 2
194 #define UWB_CHANNEL_3 3
195 #define UWB_CHANNEL_4 4
196 #define UWB_CHANNEL_5 5
197 #define UWB_CHANNEL_7 7
198 
204 #define UWB_PREAMBLE_CODE_1 1
205 #define UWB_PREAMBLE_CODE_2 2
206 #define UWB_PREAMBLE_CODE_3 3
207 #define UWB_PREAMBLE_CODE_4 4
208 #define UWB_PREAMBLE_CODE_5 5
209 #define UWB_PREAMBLE_CODE_6 6
210 #define UWB_PREAMBLE_CODE_7 7
211 #define UWB_PREAMBLE_CODE_8 8
212 #define UWB_PREAMBLE_CODE_9 9
213 #define UWB_PREAMBLE_CODE_10 10
214 #define UWB_PREAMBLE_CODE_11 11
215 #define UWB_PREAMBLE_CODE_12 12
216 #define UWB_PREAMBLE_CODE_13 13
217 #define UWB_PREAMBLE_CODE_14 14
218 #define UWB_PREAMBLE_CODE_15 15
219 #define UWB_PREAMBLE_CODE_16 16
220 #define UWB_PREAMBLE_CODE_17 17
221 #define UWB_PREAMBLE_CODE_18 18
222 #define UWB_PREAMBLE_CODE_19 19
223 #define UWB_PREAMBLE_CODE_20 20
224 #define UWB_PREAMBLE_CODE_21 21
225 #define UWB_PREAMBLE_CODE_22 22
226 #define UWB_PREAMBLE_CODE_23 23
227 #define UWB_PREAMBLE_CODE_24 24
228 
234 #define UWB_FRAME_LEN_NORMAL 0x00
235 #define UWB_FRAME_LEN_EXTENDED 0x03
236 
242 #define UWB_HIGH 1
243 #define UWB_LOW 0
244 
250 #define UWB_DEV_ERROR 0xFF
251 #define UWB_DEV_OK 0x00
252 
258 #define UWB_MODE_IDLE 1
259 #define UWB_MODE_RX 2
260 #define UWB_MODE_TX 3
261 
267 #define UWB_TAG 0xDECA
268 
274 #define WRITE_MASK 0x80
275 #define SUB_MASK 0x40
276 #define READ_MASK 0x00
277 #define SUB_EXT_MASK 0x80
278 
284 #define AGC_TUNE1_SUB 0x04
285 #define AGC_TUNE2_SUB 0x0C
286 #define AGC_TUNE3_SUB 0x12
287 #define LEN_AGC_TUNE1 2
288 #define LEN_AGC_TUNE2 4
289 #define LEN_AGC_TUNE3 2
290 #define DRX_TUNE0b_SUB 0x02
291 #define DRX_TUNE1a_SUB 0x04
292 #define DRX_TUNE1b_SUB 0x06
293 #define DRX_TUNE2_SUB 0x08
294 #define DRX_TUNE4H_SUB 0x26
295 #define LEN_DRX_TUNE0b 2
296 #define LEN_DRX_TUNE1a 2
297 #define LEN_DRX_TUNE1b 2
298 #define LEN_DRX_TUNE2 4
299 #define LEN_DRX_TUNE4H 2
300 #define LDE_CFG1_SUB 0x0806
301 #define LDE_RXANTD_SUB 0x1804
302 #define LDE_CFG2_SUB 0x1806
303 #define LDE_REPC_SUB 0x2804
304 #define LEN_LDE_CFG1 1
305 #define LEN_LDE_CFG2 2
306 #define LEN_LDE_REPC 2
307 #define LEN_LDE_RXANTD 2
308 #define LEN_TX_POWER 4
309 #define RF_RXCTRLH_SUB 0x0B
310 #define RF_TXCTRL_SUB 0x0C
311 #define LEN_RF_RXCTRLH 1
312 #define LEN_RF_TXCTRL 4
313 #define TC_PGDELAY_SUB 0x0B
314 #define LEN_TC_PGDELAY 1
315 #define TC_SARC 0x00
316 #define TC_SARL 0x03
317 #define FS_PLLCFG_SUB 0x07
318 #define FS_PLLTUNE_SUB 0x0B
319 #define FS_XTALT_SUB 0x0E
320 #define LEN_FS_PLLCFG 4
321 #define LEN_FS_PLLTUNE 1
322 #define LEN_FS_XTALT 1
323 
329 #define DUMMY_BUFFER 1024
330 
340 #define UWB_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
341 #define UWB_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
342  // End group macro
343 // --------------------------------------------------------------- PUBLIC TYPES
352 typedef struct
353 {
354  // Output pins
355 
356  digital_out_t rst;
357  digital_out_t cs;
358 
359  // Input pins
360 
361  digital_in_t irq;
362 
363  // Modules
364 
365  spi_master_t spi;
366  pin_name_t chip_select;
367 
368  // Sub address offset
369 
370  uint16_t offset;
371 
372 } uwb_t;
373 
377 typedef struct
378 {
379  // Communication gpio pins
380 
381  pin_name_t miso;
382  pin_name_t mosi;
383  pin_name_t sck;
384  pin_name_t cs;
385 
386  // Additional gpio pins
387 
388  pin_name_t rst;
389  pin_name_t irq;
390 
391  // static variable
392 
393  uint32_t spi_speed;
394  spi_master_mode_t spi_mode;
395  spi_master_chip_select_polarity_t cs_polarity;
396 
397 } uwb_cfg_t;
398 
402 typedef struct
403 {
404  uint8_t dev_mode;
405  uint8_t extended_frame;
406  uint8_t pac_size;
407  uint8_t pulse_freq;
408  uint8_t data_rate;
409  uint8_t preamble_len;
410  uint8_t preamble_code;
411  uint8_t channel;
412  uint16_t antenna_delay;
413  uint8_t antenna_calib;
414  uint8_t smart_power;
415  uint8_t frame_check;
416  uint8_t permanent_rx;
418 
419 }uwb_dev_t;
420  // End types group
422 // ------------------------------------------------------------------ CONSTANTS
428 extern const uint8_t UWB_TMODE_LONGDATA_RANGE_LOWPOWER[ 3 ];
429 extern const uint8_t UWB_TMODE_LONGDATA_RANGE_ACCURACY[ 3 ];
430 extern const uint8_t UWB_TMODE_LONGDATA_FAST_LOWPOWER[ 3 ];
431 extern const uint8_t UWB_TMODE_LONGDATA_FAST_ACCURACY[ 3 ];
432 extern const uint8_t UWB_TMODE_SHORTDATA_FAST_LOWPOWER[ 3 ];
433 extern const uint8_t UWB_TMODE_SHORTDATA_FAST_ACCURACY[ 3 ];
434  // End constants group
436 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
437 
442 #ifdef __cplusplus
443 extern "C"{
444 #endif
445 
454 void uwb_cfg_setup ( uwb_cfg_t *cfg );
455 
464 err_t uwb_init ( uwb_t *ctx, uwb_cfg_t *cfg );
465 
476 void uwb_generic_write ( uwb_t *ctx, uint8_t reg_adr, uint8_t *tx_buf, uint16_t buf_len );
477 
488 void uwb_generic_read ( uwb_t *ctx, uint8_t reg_adr, uint8_t *rx_buf, uint16_t buf_len );
489 
498 void uwb_set_rst_pin_status ( uwb_t *ctx, uint8_t status );
499 
510 
520 void uwb_read_otp ( uwb_t *ctx, uint16_t otp_adr, uint8_t *rx_buf );
521 
532 void uwb_set_bit ( uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num, uint8_t bit_state );
533 
545 uint8_t uwb_get_bit ( uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num );
546 
557 uint16_t uwb_reg_data_len ( uint8_t reg_adr );
558 
568 void uwb_set_dev_adr_n_network_id ( uwb_t *ctx, uint16_t dev_adr, uint16_t net_id );
569 
580 uint8_t uwb_set_data_rate ( uwb_t *ctx, uint8_t rate );
581 
590 void uwb_set_pulse_freq ( uwb_t *ctx, uint8_t freq );
591 
600 void uwb_set_preamble_len ( uwb_t *ctx, uint8_t prealen );
601 
610 void uwb_set_preable_code ( uwb_t *ctx, uint8_t preamble );
611 
620 void uwb_set_channel ( uwb_t *ctx, uint8_t channel );
621 
630 void uwb_set_transmit_type ( uwb_t *ctx, const uint8_t *t_t );
631 
641 void uwb_get_transmit ( uwb_t *ctx, uint8_t *rx_buf, uint16_t len_buf );
642 
652 uint8_t uwb_get_transmit_len ( uwb_t *ctx );
653 
663 void uwb_set_transmit ( uwb_t *ctx, uint8_t *tx_buf, uint16_t len_buf );
664 
673 void uwb_use_smart_power ( uwb_t *ctx, uint8_t smart_power );
674 
683 void uwb_frame_check ( uint8_t fc_val );
684 
693 void uwb_frame_filter ( uwb_t *ctx, uint8_t ff_val );
694 
704 void uwb_set_mode ( uwb_t *ctx, uint8_t mode );
705 
715 
727 
736 void uwb_clear_status ( uwb_t *ctx );
737 
746 void uwb_int_mask_set ( uwb_t *ctx );
747 
757 void uwb_tune_config ( uwb_t *ctx );
758 
766 void uwb_dev_reset ( uwb_t *ctx );
767 
775 void uwb_enable ( uwb_t *ctx );
776 
777 #ifdef __cplusplus
778 }
779 #endif
780 #endif // _UWB_H_
781  // End public_function group
784 
785 // ------------------------------------------------------------------------- END
uwb_set_dev_adr_n_network_id
void uwb_set_dev_adr_n_network_id(uwb_t *ctx, uint16_t dev_adr, uint16_t net_id)
Function for setting device address and network ID.
UWB_TMODE_SHORTDATA_FAST_LOWPOWER
const uint8_t UWB_TMODE_SHORTDATA_FAST_LOWPOWER[3]
uwb_enable
void uwb_enable(uwb_t *ctx)
Function for enabling device.
uwb_t::spi
spi_master_t spi
Definition: uwb.h:365
uwb_dev_t::dev_mode
uint8_t dev_mode
Definition: uwb.h:404
uwb_get_bit
uint8_t uwb_get_bit(uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num)
Function for getting single bit state.
uwb_set_channel
void uwb_set_channel(uwb_t *ctx, uint8_t channel)
Function for setting channel.
uwb_dev_t::antenna_calib
uint8_t antenna_calib
Definition: uwb.h:413
uwb_dev_t::permanent_rx
uint8_t permanent_rx
Definition: uwb.h:416
uwb_dev_t::channel
uint8_t channel
Definition: uwb.h:411
uwb_cfg_t::sck
pin_name_t sck
Definition: uwb.h:383
UWB_TMODE_SHORTDATA_FAST_ACCURACY
const uint8_t UWB_TMODE_SHORTDATA_FAST_ACCURACY[3]
uwb_int_mask_set
void uwb_int_mask_set(uwb_t *ctx)
Function for setting interrupt mask.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
uwb_dev_t::debounce_clk_en
uint8_t debounce_clk_en
Definition: uwb.h:417
uwb_clear_status
void uwb_clear_status(uwb_t *ctx)
Function for clearing status.
uwb_cfg_setup
void uwb_cfg_setup(uwb_cfg_t *cfg)
Config Object Initialization function.
uwb_get_qint_pin_status
uint8_t uwb_get_qint_pin_status(uwb_t *ctx)
Function for getting irq pin status.
uwb_dev_t::preamble_code
uint8_t preamble_code
Definition: uwb.h:410
uwb_t
Click ctx object definition.
Definition: uwb.h:353
uwb_t::cs
digital_out_t cs
Definition: uwb.h:357
uwb_dev_reset
void uwb_dev_reset(uwb_t *ctx)
Function for reseting device.
uwb_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: uwb.h:394
uwb_generic_write
void uwb_generic_write(uwb_t *ctx, uint8_t reg_adr, uint8_t *tx_buf, uint16_t buf_len)
Function for writing data to register.
uwb_get_transmit_status
uint8_t uwb_get_transmit_status(uwb_t *ctx)
Function for getting transmit status.
uwb_dev_t::antenna_delay
uint16_t antenna_delay
Definition: uwb.h:412
uwb_use_smart_power
void uwb_use_smart_power(uwb_t *ctx, uint8_t smart_power)
Function for setting smart power option.
uwb_cfg_t::mosi
pin_name_t mosi
Definition: uwb.h:382
UWB_TMODE_LONGDATA_FAST_LOWPOWER
const uint8_t UWB_TMODE_LONGDATA_FAST_LOWPOWER[3]
uwb_dev_t::smart_power
uint8_t smart_power
Definition: uwb.h:414
uwb_t::irq
digital_in_t irq
Definition: uwb.h:361
uwb_set_preamble_len
void uwb_set_preamble_len(uwb_t *ctx, uint8_t prealen)
Function for setting preamble length.
uwb_dev_t::pac_size
uint8_t pac_size
Definition: uwb.h:406
uwb_cfg_t::rst
pin_name_t rst
Definition: uwb.h:388
uwb_read_otp
void uwb_read_otp(uwb_t *ctx, uint16_t otp_adr, uint8_t *rx_buf)
Function for reading OTM memory.
uwb_get_transmit
void uwb_get_transmit(uwb_t *ctx, uint8_t *rx_buf, uint16_t len_buf)
Function for getting transmit data.
uwb_start_transceiver
void uwb_start_transceiver(uwb_t *ctx)
Function for starting communication of device.
uwb_cfg_t
Click configuration structure definition.
Definition: uwb.h:378
uwb_set_preable_code
void uwb_set_preable_code(uwb_t *ctx, uint8_t preamble)
Function for setting preamble code.
uwb_dev_t::data_rate
uint8_t data_rate
Definition: uwb.h:408
UWB_TMODE_LONGDATA_RANGE_ACCURACY
const uint8_t UWB_TMODE_LONGDATA_RANGE_ACCURACY[3]
uwb_set_transmit_type
void uwb_set_transmit_type(uwb_t *ctx, const uint8_t *t_t)
Function for setting transmit type mode.
uwb_set_pulse_freq
void uwb_set_pulse_freq(uwb_t *ctx, uint8_t freq)
Function for setting pulse frequency.
uwb_t::rst
digital_out_t rst
Definition: uwb.h:356
uwb_init
err_t uwb_init(uwb_t *ctx, uwb_cfg_t *cfg)
Initialization function.
uwb_set_rst_pin_status
void uwb_set_rst_pin_status(uwb_t *ctx, uint8_t status)
Function for setting rst pin status.
uwb_dev_t::frame_check
uint8_t frame_check
Definition: uwb.h:415
uwb_t::offset
uint16_t offset
Definition: uwb.h:370
uwb_frame_check
void uwb_frame_check(uint8_t fc_val)
Function for setting frame check option.
uwb_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: uwb.h:395
uwb_set_data_rate
uint8_t uwb_set_data_rate(uwb_t *ctx, uint8_t rate)
Function for setting data rate.
uwb_dev_t::extended_frame
uint8_t extended_frame
Definition: uwb.h:405
uwb_cfg_t::spi_speed
uint32_t spi_speed
Definition: uwb.h:393
uwb_generic_read
void uwb_generic_read(uwb_t *ctx, uint8_t reg_adr, uint8_t *rx_buf, uint16_t buf_len)
Function for reading data from register.
uwb_dev_t::pulse_freq
uint8_t pulse_freq
Definition: uwb.h:407
uwb_dev_t::preamble_len
uint8_t preamble_len
Definition: uwb.h:409
uwb_cfg_t::cs
pin_name_t cs
Definition: uwb.h:384
uwb_get_transmit_len
uint8_t uwb_get_transmit_len(uwb_t *ctx)
Function for getting transmit length.
uwb_t::chip_select
pin_name_t chip_select
Definition: uwb.h:366
uwb_reg_data_len
uint16_t uwb_reg_data_len(uint8_t reg_adr)
Function for getting length of register data.
uwb_tune_config
void uwb_tune_config(uwb_t *ctx)
Function for tuneing configuration for set values.
UWB_TMODE_LONGDATA_RANGE_LOWPOWER
const uint8_t UWB_TMODE_LONGDATA_RANGE_LOWPOWER[3]
uwb_set_transmit
void uwb_set_transmit(uwb_t *ctx, uint8_t *tx_buf, uint16_t len_buf)
Function for setting transmit data.
uwb_set_mode
void uwb_set_mode(uwb_t *ctx, uint8_t mode)
Function for setting device working mode.
uwb_cfg_t::irq
pin_name_t irq
Definition: uwb.h:389
uwb_dev_t
Structure for device configuration.
Definition: uwb.h:403
uwb_cfg_t::miso
pin_name_t miso
Definition: uwb.h:381
uwb_frame_filter
void uwb_frame_filter(uwb_t *ctx, uint8_t ff_val)
Function for setting frame filter option.
uwb_set_bit
void uwb_set_bit(uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num, uint8_t bit_state)
Function for setting single bit state.
UWB_TMODE_LONGDATA_FAST_ACCURACY
const uint8_t UWB_TMODE_LONGDATA_FAST_ACCURACY[3]