2019-01-20 14:03:11 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* boards/arm/stm32f0l0g0/b-l072z-lrwan1/src/stm32_sx127x.c
|
2019-01-20 14:03:11 +01:00
|
|
|
*
|
2020-10-10 14:44:33 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-01-20 14:03:11 +01:00
|
|
|
*
|
2020-10-10 14:44:33 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-01-20 14:03:11 +01:00
|
|
|
*
|
2020-10-10 14:44:33 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-01-20 14:03:11 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/board.h>
|
2019-11-30 00:47:03 +01:00
|
|
|
#include <nuttx/signal.h>
|
2019-01-20 14:03:11 +01:00
|
|
|
#include <nuttx/wireless/lpwan/sx127x.h>
|
2019-11-30 00:47:03 +01:00
|
|
|
#include <arch/board/board.h>
|
2019-01-20 14:03:11 +01:00
|
|
|
|
|
|
|
#include "stm32_gpio.h"
|
|
|
|
#include "stm32_exti.h"
|
|
|
|
#include "stm32_spi.h"
|
|
|
|
|
|
|
|
#include "b-l072z-lrwan1.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* SX127X on SPI1 bus */
|
|
|
|
|
|
|
|
#define SX127X_SPI 1
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void sx127x_chip_reset(void);
|
2019-02-24 18:58:46 +01:00
|
|
|
static int sx127x_opmode_change(int opmode);
|
|
|
|
static int sx127x_freq_select(uint32_t freq);
|
|
|
|
static int sx127x_pa_select(bool enable);
|
2022-04-17 08:01:48 +02:00
|
|
|
static int sx127x_irq0_attach(xcpt_t isr, void *arg);
|
2019-01-20 14:03:11 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct sx127x_lower_s lower =
|
|
|
|
{
|
2019-02-24 18:58:46 +01:00
|
|
|
.irq0attach = sx127x_irq0_attach,
|
|
|
|
.reset = sx127x_chip_reset,
|
|
|
|
.opmode_change = sx127x_opmode_change,
|
|
|
|
.freq_select = sx127x_freq_select,
|
|
|
|
.pa_select = sx127x_pa_select,
|
|
|
|
.pa_force = false
|
2019-01-20 14:03:11 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 18:58:46 +01:00
|
|
|
static bool g_high_power_output = false;
|
|
|
|
|
2019-01-20 14:03:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 18:58:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sx127x_irq0_attach
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int sx127x_irq0_attach(xcpt_t isr, void *arg)
|
2019-01-20 14:03:11 +01:00
|
|
|
{
|
|
|
|
wlinfo("Attach DIO0 IRQ\n");
|
|
|
|
|
|
|
|
/* IRQ on rising edge */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
stm32_gpiosetevent(GPIO_SX127X_DIO0, true, false, false, isr, arg);
|
2019-01-20 14:03:11 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:58:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sx127x_chip_reset
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-20 14:03:11 +01:00
|
|
|
static void sx127x_chip_reset(void)
|
|
|
|
{
|
|
|
|
wlinfo("SX127X RESET\n");
|
|
|
|
|
|
|
|
/* Configure reset as output */
|
|
|
|
|
2019-01-20 14:38:55 +01:00
|
|
|
stm32_configgpio(GPIO_SX127X_RESET | GPIO_OUTPUT | GPIO_SPEED_HIGH |
|
2019-02-16 21:53:16 +01:00
|
|
|
GPIO_OUTPUT_CLEAR);
|
2019-01-20 14:03:11 +01:00
|
|
|
|
|
|
|
/* Set pin to zero */
|
|
|
|
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_RESET, false);
|
|
|
|
|
|
|
|
/* Wait 1 ms */
|
|
|
|
|
2019-11-30 00:47:03 +01:00
|
|
|
nxsig_usleep(1000);
|
2019-01-20 14:03:11 +01:00
|
|
|
|
|
|
|
/* Configure reset as input */
|
|
|
|
|
2019-01-20 14:38:55 +01:00
|
|
|
stm32_configgpio(GPIO_SX127X_RESET | GPIO_INPUT | GPIO_FLOAT);
|
2019-01-20 14:03:11 +01:00
|
|
|
|
|
|
|
/* Wait 10 ms */
|
|
|
|
|
2019-11-30 00:47:03 +01:00
|
|
|
nxsig_usleep(10000);
|
2019-01-20 14:03:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 18:58:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sx127x_opmode_change
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sx127x_opmode_change(int opmode)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Configure antena switch outputs in SLEEP mode */
|
|
|
|
|
|
|
|
if (opmode == SX127X_OPMODE_SLEEP)
|
|
|
|
{
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF1, false);
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF2, false);
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF3, false);
|
|
|
|
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF1 | GPIO_ANALOG);
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF2 | GPIO_ANALOG);
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF3 | GPIO_ANALOG);
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure antena switch outputs */
|
|
|
|
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF1 | GPIO_OUTPUT | GPIO_OUTPUT_CLEAR);
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF2 | GPIO_OUTPUT | GPIO_OUTPUT_CLEAR);
|
|
|
|
stm32_configgpio(GPIO_SX127X_CRF3 | GPIO_OUTPUT | GPIO_OUTPUT_CLEAR);
|
|
|
|
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF1, false);
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF2, false);
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF3, false);
|
|
|
|
|
|
|
|
switch (opmode)
|
|
|
|
{
|
|
|
|
case SX127X_OPMODE_STANDBY:
|
|
|
|
case SX127X_OPMODE_FSRX:
|
|
|
|
case SX127X_OPMODE_FSTX:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SX127X_OPMODE_TX:
|
|
|
|
{
|
|
|
|
/* Set TX RFO or TX BOOST */
|
|
|
|
|
|
|
|
if (g_high_power_output == true)
|
|
|
|
{
|
|
|
|
wlinfo("SET CRF3\n");
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF3, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wlinfo("SET CRF2\n");
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF2, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SX127X_OPMODE_RX:
|
|
|
|
case SX127X_OPMODE_RXSINGLE:
|
|
|
|
case SX127X_OPMODE_CAD:
|
|
|
|
{
|
|
|
|
/* Set antena RX */
|
|
|
|
|
|
|
|
wlinfo("SET CRF1\n");
|
|
|
|
stm32_gpiowrite(GPIO_SX127X_CRF1, true);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
wlerr("ERROR: invalid mode %d\n", opmode);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sx127x_freq_select
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sx127x_freq_select(uint32_t freq)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Only HF supported (BAND3 - 860-930 MHz) */
|
|
|
|
|
|
|
|
if (freq < SX127X_HFBAND_THR)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
wlerr("LF band not supported\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sx127x_pa_select
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sx127x_pa_select(bool enable)
|
|
|
|
{
|
|
|
|
g_high_power_output = enable;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:03:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int stm32_lpwaninitialize(void)
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
struct spi_dev_s *spidev;
|
2019-01-20 14:03:11 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
wlinfo("Register the sx127x module\n");
|
|
|
|
|
|
|
|
/* Setup DIO0 */
|
|
|
|
|
|
|
|
stm32_configgpio(GPIO_SX127X_DIO0);
|
|
|
|
|
|
|
|
/* Init SPI bus */
|
|
|
|
|
|
|
|
spidev = stm32_spibus_initialize(SX127X_SPI);
|
|
|
|
if (!spidev)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to initialize SPI %d bus\n", SX127X_SPI);
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize SX127X */
|
|
|
|
|
|
|
|
ret = sx127x_register(spidev, &lower);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
wlerr("ERROR: Failed to register sx127x\n");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|