2021-07-04 02:06:31 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Fabio Baltieri
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
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 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2021-07-04 02:06:31 +08:00
|
|
|
|
|
|
|
#include "sx126x_common.h"
|
|
|
|
|
|
|
|
#include <stm32wlxx_ll_exti.h>
|
|
|
|
#include <stm32wlxx_ll_pwr.h>
|
|
|
|
#include <stm32wlxx_ll_rcc.h>
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2022-10-17 16:24:11 +08:00
|
|
|
#include <zephyr/irq.h>
|
2021-07-04 02:06:31 +08:00
|
|
|
LOG_MODULE_DECLARE(sx126x, CONFIG_LORA_LOG_LEVEL);
|
|
|
|
|
2023-04-14 07:47:06 +08:00
|
|
|
static const enum {
|
|
|
|
RFO_LP,
|
|
|
|
RFO_HP,
|
|
|
|
} pa_output = DT_INST_STRING_UPPER_TOKEN(0, power_amplifier_output);
|
|
|
|
|
2021-07-04 02:06:31 +08:00
|
|
|
void sx126x_reset(struct sx126x_data *dev_data)
|
|
|
|
{
|
|
|
|
LL_RCC_RF_EnableReset();
|
|
|
|
k_sleep(K_MSEC(20));
|
|
|
|
LL_RCC_RF_DisableReset();
|
|
|
|
k_sleep(K_MSEC(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sx126x_is_busy(struct sx126x_data *dev_data)
|
|
|
|
{
|
|
|
|
return LL_PWR_IsActiveFlag_RFBUSYS();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sx126x_get_dio1_pin_state(struct sx126x_data *dev_data)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sx126x_dio1_irq_enable(struct sx126x_data *dev_data)
|
|
|
|
{
|
2021-09-23 06:07:14 +08:00
|
|
|
NVIC_ClearPendingIRQ(DT_INST_IRQN(0));
|
2021-07-04 02:06:31 +08:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void sx126x_dio1_irq_disable(struct sx126x_data *dev_data)
|
|
|
|
{
|
|
|
|
irq_disable(DT_INST_IRQN(0));
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:47:06 +08:00
|
|
|
void sx126x_set_tx_params(int8_t power, RadioRampTimes_t ramp_time)
|
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
|
|
|
if (pa_output == RFO_LP) {
|
|
|
|
const int8_t max_power = DT_INST_PROP(0, rfo_lp_max_power);
|
|
|
|
|
|
|
|
if (power > max_power) {
|
|
|
|
power = max_power;
|
|
|
|
}
|
|
|
|
if (max_power == 15) {
|
|
|
|
SX126xSetPaConfig(0x07, 0x00, 0x01, 0x01);
|
|
|
|
power = 14 - (max_power - power);
|
|
|
|
} else if (max_power == 10) {
|
|
|
|
SX126xSetPaConfig(0x01, 0x00, 0x01, 0x01);
|
|
|
|
power = 13 - (max_power - power);
|
|
|
|
} else { /* default +14 dBm */
|
|
|
|
SX126xSetPaConfig(0x04, 0x00, 0x01, 0x01);
|
|
|
|
power = 14 - (max_power - power);
|
|
|
|
}
|
|
|
|
if (power < -17) {
|
|
|
|
power = -17;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PA overcurrent protection limit 60 mA */
|
|
|
|
SX126xWriteRegister(REG_OCP, 0x18);
|
|
|
|
} else { /* RFO_HP */
|
|
|
|
/* Better Resistance of the RFO High Power Tx to Antenna
|
|
|
|
* Mismatch, see STM32WL Erratasheet
|
|
|
|
*/
|
|
|
|
SX126xWriteRegister(REG_TX_CLAMP_CFG,
|
|
|
|
SX126xReadRegister(REG_TX_CLAMP_CFG)
|
|
|
|
| (0x0F << 1));
|
|
|
|
|
|
|
|
const int8_t max_power = DT_INST_PROP(0, rfo_hp_max_power);
|
|
|
|
|
|
|
|
if (power > max_power) {
|
|
|
|
power = max_power;
|
|
|
|
}
|
|
|
|
if (max_power == 20) {
|
|
|
|
SX126xSetPaConfig(0x03, 0x05, 0x00, 0x01);
|
|
|
|
power = 22 - (max_power - power);
|
|
|
|
} else if (max_power == 17) {
|
|
|
|
SX126xSetPaConfig(0x02, 0x03, 0x00, 0x01);
|
|
|
|
power = 22 - (max_power - power);
|
|
|
|
} else if (max_power == 14) {
|
|
|
|
SX126xSetPaConfig(0x02, 0x02, 0x00, 0x01);
|
|
|
|
power = 14 - (max_power - power);
|
|
|
|
} else { /* default +22 dBm */
|
|
|
|
SX126xSetPaConfig(0x04, 0x07, 0x00, 0x01);
|
|
|
|
power = 22 - (max_power - power);
|
|
|
|
}
|
|
|
|
if (power < -9) {
|
|
|
|
power = -9;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PA overcurrent protection limit 140 mA */
|
|
|
|
SX126xWriteRegister(REG_OCP, 0x38);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = power;
|
|
|
|
buf[1] = (uint8_t)ramp_time;
|
|
|
|
SX126xWriteCommand(RADIO_SET_TXPARAMS, buf, 2);
|
|
|
|
}
|
|
|
|
|
2021-07-04 02:06:31 +08:00
|
|
|
static void radio_isr(const struct device *dev)
|
|
|
|
{
|
|
|
|
struct sx126x_data *dev_data = dev->data;
|
|
|
|
|
|
|
|
irq_disable(DT_INST_IRQN(0));
|
|
|
|
k_work_submit(&dev_data->dio1_irq_work);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sx126x_variant_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
DT_INST_IRQ(0, priority),
|
|
|
|
radio_isr, DEVICE_DT_INST_GET(0), 0);
|
|
|
|
LL_EXTI_EnableIT_32_63(LL_EXTI_LINE_44);
|
|
|
|
irq_enable(DT_INST_IRQN(0));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|