2021-07-03 20:06:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Manivannan Sadhasivam
|
|
|
|
* Copyright (c) 2020 Andreas Sandberg
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/gpio.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2021-07-03 20:06:31 +02:00
|
|
|
|
|
|
|
#include "sx126x_common.h"
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2021-07-03 20:06:31 +02:00
|
|
|
LOG_MODULE_DECLARE(sx126x, CONFIG_LORA_LOG_LEVEL);
|
|
|
|
|
2021-07-12 21:31:08 +02:00
|
|
|
static const struct gpio_dt_spec sx126x_gpio_reset = GPIO_DT_SPEC_INST_GET(
|
|
|
|
0, reset_gpios);
|
|
|
|
static const struct gpio_dt_spec sx126x_gpio_busy = GPIO_DT_SPEC_INST_GET(
|
|
|
|
0, busy_gpios);
|
|
|
|
static const struct gpio_dt_spec sx126x_gpio_dio1 = GPIO_DT_SPEC_INST_GET(
|
|
|
|
0, dio1_gpios);
|
2021-07-03 20:06:31 +02:00
|
|
|
|
|
|
|
void sx126x_reset(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-07-12 21:31:08 +02:00
|
|
|
gpio_pin_set_dt(&sx126x_gpio_reset, 1);
|
2021-07-03 20:06:31 +02:00
|
|
|
k_sleep(K_MSEC(20));
|
2021-07-12 21:31:08 +02:00
|
|
|
gpio_pin_set_dt(&sx126x_gpio_reset, 0);
|
2021-07-03 20:06:31 +02:00
|
|
|
k_sleep(K_MSEC(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sx126x_is_busy(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-07-12 21:31:08 +02:00
|
|
|
return gpio_pin_get_dt(&sx126x_gpio_busy);
|
2021-07-03 20:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sx126x_get_dio1_pin_state(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-07-12 21:31:08 +02:00
|
|
|
return gpio_pin_get_dt(&sx126x_gpio_dio1) > 0 ? 1U : 0U;
|
2021-07-03 20:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sx126x_dio1_irq_enable(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-07-12 21:31:08 +02:00
|
|
|
gpio_pin_interrupt_configure_dt(&sx126x_gpio_dio1,
|
2021-07-03 20:06:31 +02:00
|
|
|
GPIO_INT_EDGE_TO_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sx126x_dio1_irq_disable(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-07-12 21:31:08 +02:00
|
|
|
gpio_pin_interrupt_configure_dt(&sx126x_gpio_dio1,
|
2021-07-03 20:06:31 +02:00
|
|
|
GPIO_INT_DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sx126x_dio1_irq_callback(const struct device *dev,
|
|
|
|
struct gpio_callback *cb, uint32_t pins)
|
|
|
|
{
|
2021-08-14 21:14:00 +02:00
|
|
|
struct sx126x_data *dev_data = CONTAINER_OF(cb, struct sx126x_data,
|
|
|
|
dio1_irq_callback);
|
2021-07-03 20:06:31 +02:00
|
|
|
|
2021-07-12 21:31:08 +02:00
|
|
|
if (pins & BIT(sx126x_gpio_dio1.pin)) {
|
2021-07-03 20:06:31 +02:00
|
|
|
k_work_submit(&dev_data->dio1_irq_work);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 01:47:06 +02:00
|
|
|
void sx126x_set_tx_params(int8_t power, RadioRampTimes_t ramp_time)
|
|
|
|
{
|
|
|
|
SX126xSetTxParams(power, ramp_time);
|
|
|
|
}
|
|
|
|
|
2021-07-03 20:06:31 +02:00
|
|
|
int sx126x_variant_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
struct sx126x_data *dev_data = dev->data;
|
|
|
|
|
2021-07-12 21:31:08 +02:00
|
|
|
if (gpio_pin_configure_dt(&sx126x_gpio_reset, GPIO_OUTPUT_ACTIVE) ||
|
|
|
|
gpio_pin_configure_dt(&sx126x_gpio_busy, GPIO_INPUT) ||
|
2021-10-27 14:52:31 +02:00
|
|
|
gpio_pin_configure_dt(&sx126x_gpio_dio1, GPIO_INPUT)) {
|
2021-07-12 21:31:08 +02:00
|
|
|
LOG_ERR("GPIO configuration failed.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-07-03 20:06:31 +02:00
|
|
|
gpio_init_callback(&dev_data->dio1_irq_callback,
|
2021-07-12 21:31:08 +02:00
|
|
|
sx126x_dio1_irq_callback, BIT(sx126x_gpio_dio1.pin));
|
|
|
|
if (gpio_add_callback(sx126x_gpio_dio1.port,
|
|
|
|
&dev_data->dio1_irq_callback) < 0) {
|
2021-07-03 20:06:31 +02:00
|
|
|
LOG_ERR("Could not set GPIO callback for DIO1 interrupt.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|