LibDriver SX1262
Loading...
Searching...
No Matches
driver_sx1262_cad_test.c
Go to the documentation of this file.
1
36
38
39static sx1262_handle_t gs_handle;
40
49{
50 if (sx1262_irq_handler(&gs_handle) != 0)
51 {
52 return 1;
53 }
54 else
55 {
56 return 0;
57 }
58}
59
67uint8_t sx1262_cad_test(void)
68{
69 uint8_t res;
70 uint32_t reg;
71 uint8_t modulation;
72 uint8_t config;
73 sx1262_bool_t enable;
74
75 /* link interface function */
89
90 /* start cad test */
91 sx1262_interface_debug_print("sx1262: start cad test.\n");
92
93 /* init the sx1262 */
94 res = sx1262_init(&gs_handle);
95 if (res != 0)
96 {
97 sx1262_interface_debug_print("sx1262: init failed.\n");
98
99 return 1;
100 }
101
102 /* enter standby */
104 if (res != 0)
105 {
106 sx1262_interface_debug_print("sx1262: set standby failed.\n");
107 (void)sx1262_deinit(&gs_handle);
108
109 return 1;
110 }
111
112 /* disable stop timer on preamble */
114 if (res != 0)
115 {
116 sx1262_interface_debug_print("sx1262: stop timer on preamble failed.\n");
117 (void)sx1262_deinit(&gs_handle);
118
119 return 1;
120 }
121
122 /* set dc dc ldo */
124 if (res != 0)
125 {
126 sx1262_interface_debug_print("sx1262: set regulator mode failed.\n");
127 (void)sx1262_deinit(&gs_handle);
128
129 return 1;
130 }
131
132 /* set +17dBm power */
133 res = sx1262_set_pa_config(&gs_handle, 0x02, 0x03);
134 if (res != 0)
135 {
136 sx1262_interface_debug_print("sx1262: set pa config failed.\n");
137 (void)sx1262_deinit(&gs_handle);
138
139 return 1;
140 }
141
142 /* enter to stdby rc mode */
144 if (res != 0)
145 {
146 sx1262_interface_debug_print("sx1262: set rx tx fallback mode failed.\n");
147 (void)sx1262_deinit(&gs_handle);
148
149 return 1;
150 }
151
152 /* set dio irq */
153 res = sx1262_set_dio_irq_params(&gs_handle, 0x03FF, 0x03FF, 0x0000, 0x0000);
154 if (res != 0)
155 {
156 sx1262_interface_debug_print("sx1262: set dio irq params failed.\n");
157 (void)sx1262_deinit(&gs_handle);
158
159 return 1;
160 }
161
162 /* clear irq status */
163 res = sx1262_clear_irq_status(&gs_handle, 0x03FF);
164 if (res != 0)
165 {
166 sx1262_interface_debug_print("sx1262: clear irq status failed.\n");
167 (void)sx1262_deinit(&gs_handle);
168
169 return 1;
170 }
171
172 /* set lora mode */
174 if (res != 0)
175 {
176 sx1262_interface_debug_print("sx1262: set packet type failed.\n");
177 (void)sx1262_deinit(&gs_handle);
178
179 return 1;
180 }
181
182 /* +17dBm */
183 res = sx1262_set_tx_params(&gs_handle, 17, SX1262_RAMP_TIME_10US);
184 if (res != 0)
185 {
186 sx1262_interface_debug_print("sx1262: set tx params failed.\n");
187 (void)sx1262_deinit(&gs_handle);
188
189 return 1;
190 }
191
192 /* sf9, 125khz, cr4/5, disable low data rate optimize */
195 if (res != 0)
196 {
197 sx1262_interface_debug_print("sx1262: set lora modulation params failed.\n");
198 (void)sx1262_deinit(&gs_handle);
199
200 return 1;
201 }
202
203 /* convert the frequency */
204 res = sx1262_frequency_convert_to_register(&gs_handle, 480100000, (uint32_t *)&reg);
205 if (res != 0)
206 {
207 sx1262_interface_debug_print("sx1262: convert to register failed.\n");
208 (void)sx1262_deinit(&gs_handle);
209
210 return 1;
211 }
212
213 /* set the frequency */
214 res = sx1262_set_rf_frequency(&gs_handle, reg);
215 if (res != 0)
216 {
217 sx1262_interface_debug_print("sx1262: set rf frequency failed.\n");
218 (void)sx1262_deinit(&gs_handle);
219
220 return 1;
221 }
222
223 /* set base address */
224 res = sx1262_set_buffer_base_address(&gs_handle, 0x00, 0x00);
225 if (res != 0)
226 {
227 sx1262_interface_debug_print("sx1262: set buffer base address failed.\n");
228 (void)sx1262_deinit(&gs_handle);
229
230 return 1;
231 }
232
233 /* 1 lora symb num */
234 res = sx1262_set_lora_symb_num_timeout(&gs_handle, 0);
235 if (res != 0)
236 {
237 sx1262_interface_debug_print("sx1262: set lora symb num timeout failed.\n");
238 (void)sx1262_deinit(&gs_handle);
239
240 return 1;
241 }
242
243 /* reset stats */
244 res = sx1262_reset_stats(&gs_handle, 0x0000, 0x0000, 0x0000);
245 if (res != 0)
246 {
247 sx1262_interface_debug_print("sx1262: reset stats failed.\n");
248 (void)sx1262_deinit(&gs_handle);
249
250 return 1;
251 }
252
253 /* clear device errors */
254 res = sx1262_clear_device_errors(&gs_handle);
255 if (res != 0)
256 {
257 sx1262_interface_debug_print("sx1262: clear device errors failed.\n");
258 (void)sx1262_deinit(&gs_handle);
259
260 return 1;
261 }
262
263 /* set the lora sync word */
264 res = sx1262_set_lora_sync_word(&gs_handle, 0x1424);
265 if (res != 0)
266 {
267 sx1262_interface_debug_print("sx1262: set lora sync word failed.\n");
268 (void)sx1262_deinit(&gs_handle);
269
270 return 1;
271 }
272
273 /* get tx modulation */
274 res = sx1262_get_tx_modulation(&gs_handle, (uint8_t *)&modulation);
275 if (res != 0)
276 {
277 sx1262_interface_debug_print("sx1262: get tx modulation failed.\n");
278 (void)sx1262_deinit(&gs_handle);
279
280 return 1;
281 }
282 modulation |= 0x04;
283
284 /* set the tx modulation */
285 res = sx1262_set_tx_modulation(&gs_handle, modulation);
286 if (res != 0)
287 {
288 sx1262_interface_debug_print("sx1262: set tx modulation failed.\n");
289 (void)sx1262_deinit(&gs_handle);
290
291 return 1;
292 }
293
294 /* set the rx gain */
295 res = sx1262_set_rx_gain(&gs_handle, 0x94);
296 if (res != 0)
297 {
298 sx1262_interface_debug_print("sx1262: set rx gain failed.\n");
299 (void)sx1262_deinit(&gs_handle);
300
301 return 1;
302 }
303
304 /* set the ocp */
305 res = sx1262_set_ocp(&gs_handle, 0x38);
306 if (res != 0)
307 {
308 sx1262_interface_debug_print("sx1262: set ocp failed.\n");
309 (void)sx1262_deinit(&gs_handle);
310
311 return 1;
312 }
313
314 /* get the tx clamp config */
315 res = sx1262_get_tx_clamp_config(&gs_handle, (uint8_t *)&config);
316 if (res != 0)
317 {
318 sx1262_interface_debug_print("sx1262: get tx clamp config failed.\n");
319 (void)sx1262_deinit(&gs_handle);
320
321 return 1;
322 }
323 config |= 0x1E;
324
325 /* set the tx clamp config */
326 res = sx1262_set_tx_clamp_config(&gs_handle, config);
327 if (res != 0)
328 {
329 sx1262_interface_debug_print("sx1262: set tx clamp config failed.\n");
330 (void)sx1262_deinit(&gs_handle);
331
332 return 1;
333 }
334
335 /* set cad params */
338 0);
339 if (res != 0)
340 {
341 sx1262_interface_debug_print("sx1262: set cad params failed.\n");
342 (void)sx1262_deinit(&gs_handle);
343
344 return 1;
345 }
346
347 /* run the cad */
348 res = sx1262_lora_cad(&gs_handle, &enable);
349 if (res != 0)
350 {
351 sx1262_interface_debug_print("sx1262: lora cad failed.\n");
352 (void)sx1262_deinit(&gs_handle);
353
354 return 1;
355 }
356
357 /* output the result */
358 if (enable == SX1262_BOOL_TRUE)
359 {
360 sx1262_interface_debug_print("sx1262: cad detected.\n");
361 }
362 else
363 {
364 sx1262_interface_debug_print("sx1262: cad not detected.\n");
365 }
366
367 /* finish cad test */
368 sx1262_interface_debug_print("sx1262: finish cad test.\n");
369
370 /* deinit */
371 (void)sx1262_deinit(&gs_handle);
372
373 return 0;
374}
driver sx1262 cad test header file
uint8_t sx1262_set_lora_modulation_params(sx1262_handle_t *handle, sx1262_lora_sf_t sf, sx1262_lora_bandwidth_t bw, sx1262_lora_cr_t cr, sx1262_bool_t low_data_rate_optimize_enable)
set the modulation params in LoRa mode
sx1262_bool_t
sx1262 bool enumeration definition
uint8_t sx1262_set_buffer_base_address(sx1262_handle_t *handle, uint8_t tx_base_addr, uint8_t rx_base_addr)
set the buffer base address
uint8_t sx1262_clear_irq_status(sx1262_handle_t *handle, uint16_t clear_irq_param)
clear the irq status
uint8_t sx1262_set_rx_tx_fallback_mode(sx1262_handle_t *handle, sx1262_rx_tx_fallback_mode_t mode)
set the rx tx fallback mode
uint8_t sx1262_reset_stats(sx1262_handle_t *handle, uint16_t pkt_received, uint16_t pkt_crc_error, uint16_t pkt_length_header_error)
reset the stats
struct sx1262_handle_s sx1262_handle_t
sx1262 handle structure definition
uint8_t sx1262_set_standby(sx1262_handle_t *handle, sx1262_clock_source_t src)
enter to the standby mode
uint8_t sx1262_set_tx_params(sx1262_handle_t *handle, int8_t dbm, sx1262_ramp_time_t t)
set the tx params
uint8_t sx1262_set_regulator_mode(sx1262_handle_t *handle, sx1262_regulator_mode_t mode)
set the regulator_mode
uint8_t sx1262_set_lora_symb_num_timeout(sx1262_handle_t *handle, uint8_t symb_num)
set the lora symbol number timeout
uint8_t sx1262_irq_handler(sx1262_handle_t *handle)
irq handler
uint8_t sx1262_set_pa_config(sx1262_handle_t *handle, uint8_t pa_duty_cycle, uint8_t hp_max)
set the pa config
uint8_t sx1262_set_rf_frequency(sx1262_handle_t *handle, uint32_t reg)
set the rf frequency
uint8_t sx1262_set_packet_type(sx1262_handle_t *handle, sx1262_packet_type_t type)
set the packet type
uint8_t sx1262_set_stop_timer_on_preamble(sx1262_handle_t *handle, sx1262_bool_t enable)
stop timer on preamble
uint8_t sx1262_deinit(sx1262_handle_t *handle)
close the chip
uint8_t sx1262_set_dio_irq_params(sx1262_handle_t *handle, uint16_t irq_mask, uint16_t dio1_mask, uint16_t dio2_mask, uint16_t dio3_mask)
set the dio irq params
uint8_t sx1262_frequency_convert_to_register(sx1262_handle_t *handle, uint32_t freq, uint32_t *reg)
convert the frequency to the register raw data
uint8_t sx1262_set_cad_params(sx1262_handle_t *handle, sx1262_lora_cad_symbol_num_t num, uint8_t cad_det_peak, uint8_t cad_det_min, sx1262_lora_cad_exit_mode_t mode, uint32_t timeout)
set the cad params
uint8_t sx1262_clear_device_errors(sx1262_handle_t *handle)
clear the device errors
uint8_t sx1262_init(sx1262_handle_t *handle)
initialize the chip
uint8_t sx1262_lora_cad(sx1262_handle_t *handle, sx1262_bool_t *enable)
run the cad
@ SX1262_LORA_BANDWIDTH_125_KHZ
@ SX1262_BOOL_FALSE
@ SX1262_BOOL_TRUE
@ SX1262_LORA_CAD_EXIT_MODE_ONLY
@ SX1262_LORA_SF_9
@ SX1262_RAMP_TIME_10US
@ SX1262_RX_TX_FALLBACK_MODE_STDBY_XOSC
@ SX1262_PACKET_TYPE_LORA
@ SX1262_LORA_CAD_SYMBOL_NUM_2
@ SX1262_CLOCK_SOURCE_XTAL_32MHZ
@ SX1262_REGULATOR_MODE_DC_DC_LDO
@ SX1262_LORA_CR_4_5
uint8_t sx1262_interface_busy_gpio_deinit(void)
interface busy gpio deinit
uint8_t sx1262_interface_reset_gpio_init(void)
interface reset gpio init
uint8_t sx1262_interface_spi_deinit(void)
interface spi bus deinit
uint8_t sx1262_interface_spi_init(void)
interface spi bus init
uint8_t sx1262_interface_reset_gpio_deinit(void)
interface reset gpio deinit
uint8_t sx1262_interface_busy_gpio_init(void)
interface busy gpio init
void sx1262_interface_receive_callback(uint16_t type, uint8_t *buf, uint16_t len)
interface receive callback
uint8_t sx1262_interface_reset_gpio_write(uint8_t data)
interface reset gpio write
void sx1262_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t sx1262_interface_busy_gpio_read(uint8_t *value)
interface busy gpio read
uint8_t sx1262_interface_spi_write_read(uint8_t *in_buf, uint32_t in_len, uint8_t *out_buf, uint32_t out_len)
interface spi bus write read
void sx1262_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t sx1262_set_tx_clamp_config(sx1262_handle_t *handle, uint8_t config)
set the tx clamp config
uint8_t sx1262_set_lora_sync_word(sx1262_handle_t *handle, uint16_t sync_word)
set the lora sync word
uint8_t sx1262_set_rx_gain(sx1262_handle_t *handle, uint8_t gain)
set the rx gain
uint8_t sx1262_set_ocp(sx1262_handle_t *handle, uint8_t ocp)
set the ocp
uint8_t sx1262_set_tx_modulation(sx1262_handle_t *handle, uint8_t modulation)
set the tx modulation
uint8_t sx1262_get_tx_clamp_config(sx1262_handle_t *handle, uint8_t *config)
get the tx clamp config
uint8_t sx1262_get_tx_modulation(sx1262_handle_t *handle, uint8_t *modulation)
get the tx modulation
uint8_t sx1262_cad_test_irq_handler(void)
sx1262 cad test irq
uint8_t sx1262_cad_test(void)
send test