ccrf 2.0.0.0
ccrf.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 CCRF_H
36#define CCRF_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
56// -------------------------------------------------------------- PUBLIC MACROS
67#define CCRF_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72 cfg.gd0 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
73 cfg.gd2 = MIKROBUS( mikrobus, MIKROBUS_PWM )
80#define CCRF_OK 0
81#define CCRF_ERROR -1
84// Configuration Registers
85#define CCRF_IOCFG2 0x00
86#define CCRF_IOCFG1 0x01
87#define CCRF_IOCFG0 0x02
88#define CCRF_FIFOTHR 0x03
89#define CCRF_SYNC1 0x04
90#define CCRF_SYNC0 0x05
91#define CCRF_PKTLEN 0x06
92#define CCRF_PKTCTRL1 0x07
93#define CCRF_PKTCTRL0 0x08
94#define CCRF_ADDR 0x09
95#define CCRF_CHANNR 0x0A
96#define CCRF_FSCTRL1 0x0B
97#define CCRF_FSCTRL0 0x0C
98#define CCRF_FREQ2 0x0D
99#define CCRF_FREQ1 0x0E
100#define CCRF_FREQ0 0x0F
101#define CCRF_MDMCFG4 0x10
102#define CCRF_MDMCFG3 0x11
103#define CCRF_MDMCFG2 0x12
104#define CCRF_MDMCFG1 0x13
105#define CCRF_MDMCFG0 0x14
106#define CCRF_DEVIATN 0x15
107#define CCRF_MCSM2 0x16
108#define CCRF_MCSM1 0x17
109#define CCRF_MCSM0 0x18
110#define CCRF_FOCCFG 0x19
111#define CCRF_BSCFG 0x1A
112#define CCRF_AGCCTRL2 0x1B
113#define CCRF_AGCCTRL1 0x1C
114#define CCRF_AGCCTRL0 0x1D
115#define CCRF_WOREVT1 0x1E
116#define CCRF_WOREVT0 0x1F
117#define CCRF_WORCTRL 0x20
118#define CCRF_FREND1 0x21
119#define CCRF_FREND0 0x22
120#define CCRF_FSCAL3 0x23
121#define CCRF_FSCAL2 0x24
122#define CCRF_FSCAL1 0x25
123#define CCRF_FSCAL0 0x26
124#define CCRF_RCCTRL1 0x27
125#define CCRF_RCCTRL0 0x28
126#define CCRF_FSTEST 0x29
127#define CCRF_PTEST 0x2A
128#define CCRF_AGCTEST 0x2B
129#define CCRF_TEST2 0x2C
130#define CCRF_TEST1 0x2D
131#define CCRF_TEST0 0x2E
132
133// Strobe commands
134#define CCRF_SRES 0x30
135#define CCRF_SFSTXON 0x31
136#define CCRF_SXOFF 0x32
137#define CCRF_SCAL 0x33
138#define CCRF_SRX 0x34
139#define CCRF_STX 0x35
140#define CCRF_SIDLE 0x36
141#define CCRF_SAFC 0x37
142#define CCRF_SWOR 0x38
143#define CCRF_SPWD 0x39
144#define CCRF_SFRX 0x3A
145#define CCRF_SFTX 0x3B
146#define CCRF_SWORRST 0x3C
147#define CCRF_SNOP 0x3D
148
149// Status registers
150#define CCRF_PARTNUM 0x30
151#define CCRF_VERSION 0x31
152#define CCRF_FREQEST 0x32
153#define CCRF_LQI 0x33
154#define CCRF_RSSI 0x34
155#define CCRF_MARCSTATE 0x35
156#define CCRF_WORTIME1 0x36
157#define CCRF_WORTIME0 0x37
158#define CCRF_PKTSTATUS 0x38
159#define CCRF_VCO_VC_DAC 0x39
160#define CCRF_TXBYTES 0x3A
161#define CCRF_RXBYTES 0x3B
162#define CCRF_RCCTRL1_STATUS 0x3C
163#define CCRF_RCCTRL0_STATUS 0x3D
164#define CCRF_NUM_RXBYTES 0x7F
165
166// Other memory locations
167#define CCRF_PATABLE 0x3E
168#define CCRF_TXFIFO 0x3F
169#define CCRF_RXFIFO 0x3F
170
171// Masks for appended status bytes
172#define CCRF_LQI_RX 0x01
173#define CCRF_CRC_OK 0x80
174
175// Definitions to support burst/single access:
176#define CCRF_WRITE_BURST 0x40
177#define CCRF_READ_SINGLE 0x80
178#define CCRF_READ_BURST 0xC0
179
180#define CCRF_RECEIVE_CRC_ERROR 0x00
181#define CCRF_RECEIVE_CRC_OK 0x80
182#define CCRF_BURST_IN_RXFIFO 0x7F
183
184#define CCRF_SINGLE_TXFIFO 0x3F
185#define CCRF_BURST_TXFIFO 0x7F
186#define CCRF_SINGLE_RXFIFO 0xBF
187#define CCRF_BURST_RXFIFO 0xFF
188
189#define CCRF_TX_MODE 0
190#define CCRF_RX_MODE 1
191#define CCRF_IDLE_MODE 2
192
193 // End group macro
194// --------------------------------------------------------------- PUBLIC TYPES
203typedef struct
204{
205 // Output pins
206 digital_out_t gd2;
207
208 // Input pins
209 digital_in_t gd0;
210
211 // Modules
212 spi_master_t spi;
213 pin_name_t chip_select;
214
215} ccrf_t;
216
220typedef struct
221{
222 // Communication gpio pins
223 pin_name_t miso;
224 pin_name_t mosi;
225 pin_name_t sck;
226 pin_name_t cs;
227
228 // Additional gpio pins
229 pin_name_t gd0;
230 pin_name_t gd2;
231
232 // static variable
233 uint32_t spi_speed;
234 spi_master_mode_t spi_mode;
235 spi_master_chip_select_polarity_t cs_polarity;
236
237} ccrf_cfg_t;
238
239 // End types group
240// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
241
246#ifdef __cplusplus
247extern "C"{
248#endif
249
259
268err_t ccrf_init ( ccrf_t *ctx, ccrf_cfg_t *cfg );
269
278
286uint8_t ccrf_get_start( ccrf_t *ctx );
287
298void ccrf_write_byte ( ccrf_t *ctx, uint8_t reg_address, uint8_t write_data );
299
311void ccrf_write_bytes ( ccrf_t *ctx, uint8_t reg_address, uint8_t *write_data, uint8_t n_bytes );
312
322void ccrf_write_strobe ( ccrf_t *ctx, uint8_t strobe );
323
335uint8_t ccrf_read_byte ( ccrf_t *ctx, uint8_t reg_address );
336
348uint8_t ccrf_read_byte_status ( ccrf_t *ctx, uint8_t reg_address );
349
361void ccrf_read_bytes( ccrf_t *ctx, uint8_t reg_address, uint8_t *read_data, uint8_t n_bytes );
362
371void ccrf_sw_reset ( ccrf_t *ctx );
372
383void ccrf_hw_reset ( ccrf_t *ctx );
384
403void ccrf_transmit_packet ( ccrf_t *ctx, uint8_t *tx_buffer, uint8_t n_bytes );
404
427uint8_t ccrf_receive_packet ( ccrf_t *ctx, uint8_t *rx_buffer, uint8_t *length_buff );
428
440uint8_t ccrf_transmit_packet_with_address ( ccrf_t *ctx, uint8_t *tx_buffer, uint8_t n_bytes, uint8_t *attempts );
441
452
463
474
485
498
509
539void ccrf_set_fifo_threshold ( ccrf_t *ctx, uint8_t threshold_rx_tx );
540
555void ccrf_set_max_length_packets ( ccrf_t *ctx, uint8_t max_length );
556
567
578
598void ccrf_format_rx_tx_data ( ccrf_t *ctx, uint8_t rx_tx_data_format );
599
610void ccrf_set_device_address ( ccrf_t *ctx, uint8_t dev_addr );
611
623void ccrf_set_channel_number ( ccrf_t *ctx, uint8_t ch_num );
624
635void ccrf_set_if_frequency ( ccrf_t *ctx, uint8_t if_freq );
636
647void ccrf_set_frequency_offset ( ccrf_t *ctx, uint8_t freq_offset );
648
661
672
690void ccrf_set_modulation ( ccrf_t *ctx, uint8_t rf_modulation );
691
702
713
735void ccrf_set_sync_word_qualifier_mode ( ccrf_t *ctx, uint8_t q_mode );
736
747
758
780void ccrf_set_min_num_preamble ( ccrf_t *ctx, uint8_t p_num );
781
799void ccrf_set_cca_mode ( ccrf_t *ctx, uint8_t cca_mode );
800
801
819void ccrf_set_state_packet_recived ( ccrf_t *ctx, uint8_t pr_state );
820
838void ccrf_set_state_packet_sent ( ccrf_t *ctx, uint8_t ps_state );
839
857void ccrf_set_auto_calibrate_mode ( ccrf_t *ctx, uint8_t ac_mode );
858
876void ccrf_set_freq_loop_gain_bs ( ccrf_t *ctx, uint8_t fcl_gain );
877
893void ccrf_set_freq_loop_gain_as ( ccrf_t *ctx, uint8_t fcl_gain );
894
929void ccrf_set_bit_sync_config ( ccrf_t *ctx, uint8_t bs_config );
930
948void ccrf_set_dvga_gain ( ccrf_t *ctx, uint8_t dvga_gain );
949
971void ccrf_set_lna_gain ( ccrf_t *ctx, uint8_t lna_gain );
972
994void ccrf_set_amplitude_ch_filter ( ccrf_t *ctx, uint8_t ach_filter );
995
1010void ccrf_set_lna_strategies ( ccrf_t *ctx, uint8_t lna_strat );
1011
1029void ccrf_set_relative_rssi_threshold ( ccrf_t *ctx, uint8_t rssi_threshold );
1030
1060void ccrf_set_absolute_rssi_threshold ( ccrf_t *ctx, uint8_t rssi_threshold );
1061
1079void ccrf_set_hysteresis_lvl ( ccrf_t *ctx, uint8_t hyst_lvl );
1080
1098void ccrf_set_num_ch_filter ( ccrf_t *ctx, uint8_t num_ch_filter );
1099
1116void ccrf_set_agc_gain ( ccrf_t *ctx, uint8_t agc_gain );
1117
1135void ccrf_set_avr_len_ampitude ( ccrf_t *ctx, uint8_t al_amp );
1136
1147
1169void ccrf_set_timeout_reg_block ( ccrf_t *ctx, uint8_t timeout );
1170
1181
1192
1210void ccrf_set_timeout_wor ( ccrf_t *ctx, uint8_t timeout );
1211
1223
1235
1246uint8_t ccrf_get_rssi ( ccrf_t *ctx );
1247
1288
1312
1325
1338
1351
1352#ifdef __cplusplus
1353}
1354#endif
1355#endif // CCRF_H_
1356
1357 // End public_function group
1359
1360// ------------------------------------------------------------------------- END
uint8_t ccrf_get_mrc_state(ccrf_t *ctx)
Get main radio control state machine state function.
void ccrf_set_frequency_offset(ccrf_t *ctx, uint8_t freq_offset)
Set frequency offset function.
void ccrf_disable_fec(ccrf_t *ctx)
Disable forward error correction function.
void ccrf_cfg_setup(ccrf_cfg_t *cfg)
Config Object Initialization function.
void ccrf_set_bit_sync_config(ccrf_t *ctx, uint8_t bs_config)
Set bit synchronization configuration function.
void ccrf_enable_manchester_encoding(ccrf_t *ctx)
Enable Manchester encoding function.
void ccrf_read_bytes(ccrf_t *ctx, uint8_t reg_address, uint8_t *read_data, uint8_t n_bytes)
Sequential read function.
void ccrf_set_gd2_active_low(ccrf_t *ctx)
GD2 select active low function.
void ccrf_set_lna_gain(ccrf_t *ctx, uint8_t lna_gain)
Set the maximum allowable LNA gain function.
uint8_t ccrf_receive_packet(ccrf_t *ctx, uint8_t *rx_buffer, uint8_t *length_buff)
Receive packet data function.
uint8_t ccrf_get_rc_osc_cal_result(ccrf_t *ctx)
Get RC oscillator calibration result function.
uint8_t ccrf_get_gd0_and_packet_state(ccrf_t *ctx)
Get current GDO status and packet status function.
uint8_t ccrf_get_chip_version_number(ccrf_t *ctx)
Get chip version number function.
void ccrf_enable_osc_rc(ccrf_t *ctx)
Enable the RC oscillator calibration function.
void ccrf_disable_osc_rc(ccrf_t *ctx)
Disables the RC oscillator calibration function.
void ccrf_set_auto_initial_calibration(ccrf_t *ctx)
Set automatic initial calibration function.
void ccrf_enable_fec(ccrf_t *ctx)
Enable forward error correction function.
void ccrf_enable_temperature_sensor(ccrf_t *ctx)
Enable analog temperature sensor function.
void ccrf_set_num_ch_filter(ccrf_t *ctx, uint8_t num_ch_filter)
Set the number of channel filter samples function.
void ccrf_set_max_length_packets(ccrf_t *ctx, uint8_t max_length)
Set maximum length packets allowed function.
void ccrf_format_rx_tx_data(ccrf_t *ctx, uint8_t rx_tx_data_format)
Set format of RX and TX data function.
void ccrf_default_cfg(ccrf_t *ctx)
Click Default Configuration function.
void ccrf_set_channel_number(ccrf_t *ctx, uint8_t ch_num)
Set channel number function.
void ccrf_set_gd0_active_high(ccrf_t *ctx)
GD0 select active high function.
void ccrf_disable_manchester_encoding(ccrf_t *ctx)
Disable Manchester encoding function.
void ccrf_set_state_packet_sent(ccrf_t *ctx, uint8_t ps_state)
Set state when a packet has been sent function.
void ccrf_set_dvga_gain(ccrf_t *ctx, uint8_t dvga_gain)
Set the maximum allowable DVGA gain function.
void ccrf_set_gd0_active_low(ccrf_t *ctx)
GD0 select active low function.
void ccrf_transmit_packet(ccrf_t *ctx, uint8_t *tx_buffer, uint8_t n_bytes)
Transmit packet data function.
void ccrf_write_strobe(ccrf_t *ctx, uint8_t strobe)
Write a strobe command function.
void ccrf_set_amplitude_ch_filter(ccrf_t *ctx, uint8_t ach_filter)
Set the target amplitude from channel filter function.
uint8_t ccrf_get_start(ccrf_t *ctx)
Get state of GD0 pin function.
void ccrf_set_state_packet_recived(ccrf_t *ctx, uint8_t pr_state)
Set state after finishing packet reception function.
void ccrf_set_cca_mode(ccrf_t *ctx, uint8_t cca_mode)
Set CCA mode function.
void ccrf_turn_data_whitening_off(ccrf_t *ctx)
Turn data whitening off function.
void ccrf_set_timeout_reg_block(ccrf_t *ctx, uint8_t timeout)
Set the averaging length for the amplitude from the channel filter function.
void ccrf_set_absolute_rssi_threshold(ccrf_t *ctx, uint8_t rssi_threshold)
Set the rabsolute RSSI threshold for asserting carrier sense function.
void ccrf_set_freq_loop_gain_as(ccrf_t *ctx, uint8_t fcl_gain)
Set frequency compensation loop gain after a sync function.
err_t ccrf_init(ccrf_t *ctx, ccrf_cfg_t *cfg)
Initialization function.
void ccrf_write_byte(ccrf_t *ctx, uint8_t reg_address, uint8_t write_data)
Write the byte of data to the 8-bit register address function.
void ccrf_set_agc_gain(ccrf_t *ctx, uint8_t agc_gain)
Set the AGC gain function.
void ccrf_set_if_frequency(ccrf_t *ctx, uint8_t if_freq)
Set desired IF frequency function.
void ccrf_sw_reset(ccrf_t *ctx)
Softwere reset function.
void ccrf_enable_dc_blocking_filter(ccrf_t *ctx)
Enable digital DC blocking filter function.
void ccrf_set_lna_strategies(ccrf_t *ctx, uint8_t lna_strat)
Set the LNA strategies function.
uint8_t ccrf_get_rssi(ccrf_t *ctx)
Get received signal strength indication function.
void ccrf_disable_dc_blocking_filter(ccrf_t *ctx)
Disable digital DC blocking filter function.
uint8_t ccrf_get_chip_part_number(ccrf_t *ctx)
Get chip part number function.
void ccrf_set_hysteresis_lvl(ccrf_t *ctx, uint8_t hyst_lvl)
Set the level of hysteresis on the magnitude deviation function.
uint8_t ccrf_transmit_packet_with_address(ccrf_t *ctx, uint8_t *tx_buffer, uint8_t n_bytes, uint8_t *attempts)
Transmit packet data with address function.
void ccrf_set_gd2_active_high(ccrf_t *ctx)
GD2 select active high function.
uint8_t ccrf_get_rx_fifo_num_bytes(ccrf_t *ctx)
Get number of bytes in RX FIFO function.
void ccrf_disable_temperature_sensor(ccrf_t *ctx)
Disable analog temperature sensor function.
void ccrf_set_min_num_preamble(ccrf_t *ctx, uint8_t p_num)
Set the minimum number of preamble bytes to be transmitted function.
void ccrf_set_modulation(ccrf_t *ctx, uint8_t rf_modulation)
Set the modulation format of the radio signal function.
void ccrf_set_avr_len_ampitude(ccrf_t *ctx, uint8_t al_amp)
Set the averaging length for the amplitude from the channel filter function.
void ccrf_set_sync_word_qualifier_mode(ccrf_t *ctx, uint8_t q_mode)
Set sync-word qualifier mode function.
uint8_t ccrf_read_byte_status(ccrf_t *ctx, uint8_t reg_address)
Read the byte of data from the 8-bit status register address function.
void ccrf_set_timeout_wor(ccrf_t *ctx, uint8_t timeout)
Set the maximum timeout of the WOR module function.
void ccrf_set_fifo_threshold(ccrf_t *ctx, uint8_t threshold_rx_tx)
Set the threshold for the TX FIFO and RX FIFO function.
void ccrf_hw_reset(ccrf_t *ctx)
Hardware reset function.
uint8_t ccrf_get_underflow_rx_fifo(ccrf_t *ctx)
Get underflow RX FIFO status function.
void ccrf_write_bytes(ccrf_t *ctx, uint8_t reg_address, uint8_t *write_data, uint8_t n_bytes)
Sequential write function.
void ccrf_turn_data_whitening_on(ccrf_t *ctx)
Turn data whitening on function.
void ccrf_set_auto_calibrate_mode(ccrf_t *ctx, uint8_t ac_mode)
Set automatically calibrate function.
void ccrf_set_device_address(ccrf_t *ctx, uint8_t dev_addr)
Set device address function.
void ccrf_set_relative_rssi_threshold(ccrf_t *ctx, uint8_t rssi_threshold)
Set the relative change threshold for asserting carrier sense function.
void ccrf_set_freq_loop_gain_bs(ccrf_t *ctx, uint8_t fcl_gain)
Set frequency compensation loop gain before a sync function.
uint8_t ccrf_read_byte(ccrf_t *ctx, uint8_t reg_address)
Read the byte of data from the 8-bit register address function.
Click configuration structure definition.
Definition ccrf.h:221
spi_master_chip_select_polarity_t cs_polarity
Definition ccrf.h:235
pin_name_t sck
Definition ccrf.h:225
pin_name_t gd2
Definition ccrf.h:230
spi_master_mode_t spi_mode
Definition ccrf.h:234
pin_name_t mosi
Definition ccrf.h:224
uint32_t spi_speed
Definition ccrf.h:233
pin_name_t miso
Definition ccrf.h:223
pin_name_t gd0
Definition ccrf.h:229
pin_name_t cs
Definition ccrf.h:226
Click ctx object definition.
Definition ccrf.h:204
spi_master_t spi
Definition ccrf.h:212
digital_in_t gd0
Definition ccrf.h:209
digital_out_t gd2
Definition ccrf.h:206
pin_name_t chip_select
Definition ccrf.h:213