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 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 #include "spi_specifics.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define UWB_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
73  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
74  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
75 
81 #define UWB_OK 0
82 #define UWB_ERROR -1
83 
89 #define UWB_SUB_NO 0x00
90 
96 #define UWB_REG_DEV_ID 0x00
97 #define UWB_REG_EU_ID 0x01
98 #define UWB_REG_PAN_ID 0x03
99 #define UWB_REG_SYS_CFG 0x04
100 #define UWB_REG_SYS_CNT 0x06
101 #define UWB_REG_SYS_TX_CTRL 0x08
102 #define UWB_REG_TX_DATA_BUF 0x09
103 #define UWB_REG_DX_TIME 0x0A
104 #define UWB_REG_RX_TIMEOUT 0x0C
105 #define UWB_REG_SYS_CTRL 0x0D
106 #define UWB_REG_SYS_EVENT_MASK 0x0E
107 #define UWB_REG_EVENT_STATUS 0x0F
108 #define UWB_REG_RX_INFO 0x10
109 #define UWB_REG_RX_BUF 0x11
110 #define UWB_REG_RX_QUALITY_INFO 0x12
111 #define UWB_REG_RX_TIME_INTERVAL 0x13
112 #define UWB_REG_RX_TIME_OFFSET 0x14
113 #define UWB_REG_RX_MESSAGE_TOA 0x15
114 #define UWB_REG_TX_MESSAGE_TOS 0x17
115 #define UWB_REG_TX_ANTD 0x18
116 #define UWB_REG_SYS_STATE 0x19
117 #define UWB_REG_ACK_RESPONSE_TIME 0x1A
118 #define UWB_REG_RX_SNIFF_CFG 0x1D
119 #define UWB_REG_TX_POWER 0x1E
120 #define UWB_REG_CHN_CTRL 0x1F
121 #define UWB_REG_SFD 0x21
122 #define UWB_REG_AUTO_GAIN_CFG 0x23
123 #define UWB_REG_EXTERNAL_SYNC 0x24
124 #define UWB_REG_READ_AA_DATA 0x25
125 #define UWB_REG_GPIO_CTRL 0x26
126 #define UWB_REG_DRX_CFG 0x27
127 #define UWB_REG_RF_CFG 0x28
128 #define UWB_REG_TX_CALIBRATION 0x2A
129 #define UWB_REG_FREQ_SYNTH_CTRL 0x2B
130 #define UWB_REG_ALWAYS_ON_REG 0x2C
131 #define UWB_REG_OTP_INTERFACE 0x2D
132 #define UWB_REG_LEAD_EDGE_DET_CTRL 0x2E
133 #define UWB_REG_DIGITAL_DIAG_IF 0x2F
134 #define UWB_REG_POWER_MANAGE_SYS_CTRL 0x36
135 
141 #define UWB_OTP_WRITE 0x00
142 #define UWB_OTP_ADR 0x04
143 #define UWB_OTP_CTRL 0x06
144 #define UWB_OTP_STATUS 0x08
145 #define UWB_OTP_RDAT 0x0A
146 #define UWB_OTP_SRDAT 0x0E
147 #define UWB_OTP_SF 0x12
148 
154 #define UWB_DATA_RATE_110KBSPS 0x00
155 #define UWB_DATA_RATE_850KBSPS 0x01
156 #define UWB_DATA_RATE_6800KBSPS 0x02
157 
163 #define UWB_PULSTE_FREQ_4MHZ 0x00
164 #define UWB_PULSTE_FREQ_16MHZ 0x01
165 #define UWB_PULSTE_FREQ_64MHZ 0x02
166 
172 #define UWB_PREAMBLE_LEN_64 0x01
173 #define UWB_PREAMBLE_LEN_128 0x05
174 #define UWB_PREAMBLE_LEN_256 0x09
175 #define UWB_PREAMBLE_LEN_512 0x0D
176 #define UWB_PREAMBLE_LEN_1024 0x02
177 #define UWB_PREAMBLE_LEN_1536 0x06
178 #define UWB_PREAMBLE_LEN_2048 0x0A
179 #define UWB_PREAMBLE_LEN_4096 0x03
180 
186 #define UWB_PAC_SIZE_8 8
187 #define UWB_PAC_SIZE_16 16
188 #define UWB_PAC_SIZE_32 32
189 #define UWB_PAC_SIZE_64 64
190 
196 #define UWB_CHANNEL_1 1
197 #define UWB_CHANNEL_2 2
198 #define UWB_CHANNEL_3 3
199 #define UWB_CHANNEL_4 4
200 #define UWB_CHANNEL_5 5
201 #define UWB_CHANNEL_7 7
202 
208 #define UWB_PREAMBLE_CODE_1 1
209 #define UWB_PREAMBLE_CODE_2 2
210 #define UWB_PREAMBLE_CODE_3 3
211 #define UWB_PREAMBLE_CODE_4 4
212 #define UWB_PREAMBLE_CODE_5 5
213 #define UWB_PREAMBLE_CODE_6 6
214 #define UWB_PREAMBLE_CODE_7 7
215 #define UWB_PREAMBLE_CODE_8 8
216 #define UWB_PREAMBLE_CODE_9 9
217 #define UWB_PREAMBLE_CODE_10 10
218 #define UWB_PREAMBLE_CODE_11 11
219 #define UWB_PREAMBLE_CODE_12 12
220 #define UWB_PREAMBLE_CODE_13 13
221 #define UWB_PREAMBLE_CODE_14 14
222 #define UWB_PREAMBLE_CODE_15 15
223 #define UWB_PREAMBLE_CODE_16 16
224 #define UWB_PREAMBLE_CODE_17 17
225 #define UWB_PREAMBLE_CODE_18 18
226 #define UWB_PREAMBLE_CODE_19 19
227 #define UWB_PREAMBLE_CODE_20 20
228 #define UWB_PREAMBLE_CODE_21 21
229 #define UWB_PREAMBLE_CODE_22 22
230 #define UWB_PREAMBLE_CODE_23 23
231 #define UWB_PREAMBLE_CODE_24 24
232 
238 #define UWB_FRAME_LEN_NORMAL 0x00
239 #define UWB_FRAME_LEN_EXTENDED 0x03
240 
246 #define UWB_HIGH 1
247 #define UWB_LOW 0
248 
254 #define UWB_DEV_ERROR 0xFF
255 #define UWB_DEV_OK 0x00
256 
262 #define UWB_MODE_IDLE 1
263 #define UWB_MODE_RX 2
264 #define UWB_MODE_TX 3
265 
271 #define UWB_TAG 0xDECA
272 
278 #define WRITE_MASK 0x80
279 #define SUB_MASK 0x40
280 #define READ_MASK 0x00
281 #define SUB_EXT_MASK 0x80
282 
288 #define AGC_TUNE1_SUB 0x04
289 #define AGC_TUNE2_SUB 0x0C
290 #define AGC_TUNE3_SUB 0x12
291 #define LEN_AGC_TUNE1 2
292 #define LEN_AGC_TUNE2 4
293 #define LEN_AGC_TUNE3 2
294 #define DRX_TUNE0b_SUB 0x02
295 #define DRX_TUNE1a_SUB 0x04
296 #define DRX_TUNE1b_SUB 0x06
297 #define DRX_TUNE2_SUB 0x08
298 #define DRX_TUNE4H_SUB 0x26
299 #define LEN_DRX_TUNE0b 2
300 #define LEN_DRX_TUNE1a 2
301 #define LEN_DRX_TUNE1b 2
302 #define LEN_DRX_TUNE2 4
303 #define LEN_DRX_TUNE4H 2
304 #define LDE_CFG1_SUB 0x0806
305 #define LDE_RXANTD_SUB 0x1804
306 #define LDE_CFG2_SUB 0x1806
307 #define LDE_REPC_SUB 0x2804
308 #define LEN_LDE_CFG1 1
309 #define LEN_LDE_CFG2 2
310 #define LEN_LDE_REPC 2
311 #define LEN_LDE_RXANTD 2
312 #define LEN_TX_POWER 4
313 #define RF_RXCTRLH_SUB 0x0B
314 #define RF_TXCTRL_SUB 0x0C
315 #define LEN_RF_RXCTRLH 1
316 #define LEN_RF_TXCTRL 4
317 #define TC_PGDELAY_SUB 0x0B
318 #define LEN_TC_PGDELAY 1
319 #define TC_SARC 0x00
320 #define TC_SARL 0x03
321 #define FS_PLLCFG_SUB 0x07
322 #define FS_PLLTUNE_SUB 0x0B
323 #define FS_XTALT_SUB 0x0E
324 #define LEN_FS_PLLCFG 4
325 #define LEN_FS_PLLTUNE 1
326 #define LEN_FS_XTALT 1
327 
333 #define DUMMY_BUFFER 1024
334 
344 #define UWB_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
345 #define UWB_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
346  // End group macro
347 // --------------------------------------------------------------- PUBLIC TYPES
356 typedef struct
357 {
358  // Output pins
359 
360  digital_out_t rst;
361  digital_out_t cs;
362 
363  // Input pins
364 
365  digital_in_t irq;
366 
367  // Modules
368 
369  spi_master_t spi;
370  pin_name_t chip_select;
371 
372  // Sub address offset
373 
374  uint16_t offset;
375 
376 } uwb_t;
377 
381 typedef struct
382 {
383  // Communication gpio pins
384 
385  pin_name_t miso;
386  pin_name_t mosi;
387  pin_name_t sck;
388  pin_name_t cs;
389 
390  // Additional gpio pins
391 
392  pin_name_t rst;
393  pin_name_t irq;
394 
395  // static variable
396 
397  uint32_t spi_speed;
398  spi_master_mode_t spi_mode;
399  spi_master_chip_select_polarity_t cs_polarity;
400 
401 } uwb_cfg_t;
402 
406 typedef struct
407 {
408  uint8_t dev_mode;
409  uint8_t extended_frame;
410  uint8_t pac_size;
411  uint8_t pulse_freq;
412  uint8_t data_rate;
413  uint8_t preamble_len;
414  uint8_t preamble_code;
415  uint8_t channel;
416  uint16_t antenna_delay;
417  uint8_t antenna_calib;
418  uint8_t smart_power;
419  uint8_t frame_check;
420  uint8_t permanent_rx;
422 
423 }uwb_dev_t;
424  // End types group
426 // ------------------------------------------------------------------ CONSTANTS
432 extern const uint8_t UWB_TMODE_LONGDATA_RANGE_LOWPOWER[ 3 ];
433 extern const uint8_t UWB_TMODE_LONGDATA_RANGE_ACCURACY[ 3 ];
434 extern const uint8_t UWB_TMODE_LONGDATA_FAST_LOWPOWER[ 3 ];
435 extern const uint8_t UWB_TMODE_LONGDATA_FAST_ACCURACY[ 3 ];
436 extern const uint8_t UWB_TMODE_SHORTDATA_FAST_LOWPOWER[ 3 ];
437 extern const uint8_t UWB_TMODE_SHORTDATA_FAST_ACCURACY[ 3 ];
438  // End constants group
440 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
441 
446 #ifdef __cplusplus
447 extern "C"{
448 #endif
449 
458 void uwb_cfg_setup ( uwb_cfg_t *cfg );
459 
468 err_t uwb_init ( uwb_t *ctx, uwb_cfg_t *cfg );
469 
480 void uwb_generic_write ( uwb_t *ctx, uint8_t reg_adr, uint8_t *tx_buf, uint16_t buf_len );
481 
492 void uwb_generic_read ( uwb_t *ctx, uint8_t reg_adr, uint8_t *rx_buf, uint16_t buf_len );
493 
502 void uwb_set_rst_pin_status ( uwb_t *ctx, uint8_t status );
503 
514 
524 void uwb_read_otp ( uwb_t *ctx, uint16_t otp_adr, uint8_t *rx_buf );
525 
536 void uwb_set_bit ( uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num, uint8_t bit_state );
537 
549 uint8_t uwb_get_bit ( uwb_t *ctx, uint8_t reg_adr, uint16_t bit_num );
550 
561 uint16_t uwb_reg_data_len ( uint8_t reg_adr );
562 
572 void uwb_set_dev_adr_n_network_id ( uwb_t *ctx, uint16_t dev_adr, uint16_t net_id );
573 
584 uint8_t uwb_set_data_rate ( uwb_t *ctx, uint8_t rate );
585 
594 void uwb_set_pulse_freq ( uwb_t *ctx, uint8_t freq );
595 
604 void uwb_set_preamble_len ( uwb_t *ctx, uint8_t prealen );
605 
614 void uwb_set_preable_code ( uwb_t *ctx, uint8_t preamble );
615 
624 void uwb_set_channel ( uwb_t *ctx, uint8_t channel );
625 
634 void uwb_set_transmit_type ( uwb_t *ctx, const uint8_t *t_t );
635 
645 void uwb_get_transmit ( uwb_t *ctx, uint8_t *rx_buf, uint16_t len_buf );
646 
656 uint8_t uwb_get_transmit_len ( uwb_t *ctx );
657 
667 void uwb_set_transmit ( uwb_t *ctx, uint8_t *tx_buf, uint16_t len_buf );
668 
677 void uwb_use_smart_power ( uwb_t *ctx, uint8_t smart_power );
678 
687 void uwb_frame_check ( uint8_t fc_val );
688 
697 void uwb_frame_filter ( uwb_t *ctx, uint8_t ff_val );
698 
708 void uwb_set_mode ( uwb_t *ctx, uint8_t mode );
709 
719 
731 
740 void uwb_clear_status ( uwb_t *ctx );
741 
750 void uwb_int_mask_set ( uwb_t *ctx );
751 
761 void uwb_tune_config ( uwb_t *ctx );
762 
770 void uwb_dev_reset ( uwb_t *ctx );
771 
779 void uwb_enable ( uwb_t *ctx );
780 
781 #ifdef __cplusplus
782 }
783 #endif
784 #endif // _UWB_H_
785  // End public_function group
788 
789 // ------------------------------------------------------------------------- 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:369
uwb_dev_t::dev_mode
uint8_t dev_mode
Definition: uwb.h:408
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:417
uwb_dev_t::permanent_rx
uint8_t permanent_rx
Definition: uwb.h:420
uwb_dev_t::channel
uint8_t channel
Definition: uwb.h:415
uwb_cfg_t::sck
pin_name_t sck
Definition: uwb.h:387
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:421
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:414
uwb_t
Click ctx object definition.
Definition: uwb.h:357
uwb_t::cs
digital_out_t cs
Definition: uwb.h:361
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:398
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:416
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:386
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:418
uwb_t::irq
digital_in_t irq
Definition: uwb.h:365
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:410
uwb_cfg_t::rst
pin_name_t rst
Definition: uwb.h:392
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:382
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:412
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:360
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:419
uwb_t::offset
uint16_t offset
Definition: uwb.h:374
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:399
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:409
uwb_cfg_t::spi_speed
uint32_t spi_speed
Definition: uwb.h:397
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:411
uwb_dev_t::preamble_len
uint8_t preamble_len
Definition: uwb.h:413
uwb_cfg_t::cs
pin_name_t cs
Definition: uwb.h:388
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:370
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:393
uwb_dev_t
Structure for device configuration.
Definition: uwb.h:407
uwb_cfg_t::miso
pin_name_t miso
Definition: uwb.h:385
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]