1 /*
2  * Copyright (c) 2021 Fabio Baltieri
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr.h>
8 
9 #include "sx126x_common.h"
10 
11 #include <stm32wlxx_ll_exti.h>
12 #include <stm32wlxx_ll_pwr.h>
13 #include <stm32wlxx_ll_rcc.h>
14 
15 #include <logging/log.h>
16 LOG_MODULE_DECLARE(sx126x, CONFIG_LORA_LOG_LEVEL);
17 
sx126x_reset(struct sx126x_data * dev_data)18 void sx126x_reset(struct sx126x_data *dev_data)
19 {
20 	LL_RCC_RF_EnableReset();
21 	k_sleep(K_MSEC(20));
22 	LL_RCC_RF_DisableReset();
23 	k_sleep(K_MSEC(10));
24 }
25 
sx126x_is_busy(struct sx126x_data * dev_data)26 bool sx126x_is_busy(struct sx126x_data *dev_data)
27 {
28 	return LL_PWR_IsActiveFlag_RFBUSYS();
29 }
30 
sx126x_get_dio1_pin_state(struct sx126x_data * dev_data)31 uint32_t sx126x_get_dio1_pin_state(struct sx126x_data *dev_data)
32 {
33 	return 0;
34 }
35 
sx126x_dio1_irq_enable(struct sx126x_data * dev_data)36 void sx126x_dio1_irq_enable(struct sx126x_data *dev_data)
37 {
38 	NVIC_ClearPendingIRQ(DT_INST_IRQN(0));
39 	irq_enable(DT_INST_IRQN(0));
40 }
41 
sx126x_dio1_irq_disable(struct sx126x_data * dev_data)42 void sx126x_dio1_irq_disable(struct sx126x_data *dev_data)
43 {
44 	irq_disable(DT_INST_IRQN(0));
45 }
46 
radio_isr(const struct device * dev)47 static void radio_isr(const struct device *dev)
48 {
49 	struct sx126x_data *dev_data = dev->data;
50 
51 	irq_disable(DT_INST_IRQN(0));
52 	k_work_submit(&dev_data->dio1_irq_work);
53 }
54 
sx126x_variant_init(const struct device * dev)55 int sx126x_variant_init(const struct device *dev)
56 {
57 	IRQ_CONNECT(DT_INST_IRQN(0),
58 		    DT_INST_IRQ(0, priority),
59 		    radio_isr, DEVICE_DT_INST_GET(0), 0);
60 	LL_EXTI_EnableIT_32_63(LL_EXTI_LINE_44);
61 	irq_enable(DT_INST_IRQN(0));
62 
63 	return 0;
64 }
65