Nrf52 imrpovements (#119)

* arch/arm/src/nrf52/hardware/nrf52_twi.h: fix typo

  * arch/arm/src/nrf52/nrf52_i2c.c: add interrupts support and some debug messages

  * boards/arm/nrf52/nrf52840-dk: add support for hts221 and lsm303agr; boards/arm/nrf52/nrf52840-dk: fix issues noted by nxstyle
This commit is contained in:
raiden00pl 2020-01-17 14:30:34 +00:00 committed by Ouss4
parent c600f6d6cf
commit e0cd10b7d5
11 changed files with 480 additions and 38 deletions

View File

@ -169,7 +169,7 @@
#define TWIM_FREQUENCY_100KBPS (0x01980000) /* 100 kbps */
#define TWIM_FREQUENCY_250KBPS (0x04000000) /* 250 kbps */
#define TWIM_FREQUENCY_400KBPS (0x64000000) /* 400 kbps */
#define TWIM_FREQUENCY_400KBPS (0x06400000) /* 400 kbps */
/* RXDMAXCNT Register */

View File

@ -59,9 +59,9 @@
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_I2C_POLLED
# error I2C irq not supported yet
#endif
/* I2C errors not functional yet */
#undef CONFIG_NRF52_I2C_ERRORS
/****************************************************************************
* Private Types
@ -76,6 +76,7 @@ struct nrf52_i2c_priv_s
uint32_t scl_pin; /* SCL pin configuration */
uint32_t sda_pin; /* SDA pin configuration */
int refs; /* Reference count */
int status; /* I2C transfer status */
#ifndef CONFIG_I2C_POLLED
uint32_t irq; /* TWI interrupt */
#endif
@ -231,6 +232,12 @@ static int nrf52_i2c_transfer(FAR struct i2c_master_s *dev,
priv->msgv = msgs;
priv->msgc = count;
/* Reset I2C transfer status */
priv->status = OK;
i2cinfo("I2C TRANSFER count=%d\n", count);
/* Do we need change I2C bus freqency ? */
if (priv->msgv->frequency != priv->freq)
@ -284,6 +291,9 @@ static int nrf52_i2c_transfer(FAR struct i2c_master_s *dev,
priv->flags = priv->msgv->flags;
priv->addr = priv->msgv->addr;
i2cinfo("ptr=%p dcnt=%d flags=%d addr=%d\n",
priv->ptr, priv->dcnt, priv->flags, priv->addr);
/* Write TWI address */
regval = priv->addr;
@ -314,21 +324,13 @@ static int nrf52_i2c_transfer(FAR struct i2c_master_s *dev,
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_LASTTX_OFFSET, 0);
#endif
#else
nxsem_wait(&priv->sem_isr);
/* TWIM stop */
nrf52_i2c_putreg(priv, NRF52_TWIM_TASKS_STOP_OFFSET, 1);
/* Wait for stop event */
#ifdef CONFIG_I2C_POLLED
while (nrf52_i2c_getreg(priv,
NRF52_TWIM_EVENTS_STOPPED_OFFSET) != 1);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_STOPPED_OFFSET, 0);
if (priv->status < 0)
{
goto errout;
}
#endif
}
else
@ -352,16 +354,17 @@ static int nrf52_i2c_transfer(FAR struct i2c_master_s *dev,
#ifdef CONFIG_I2C_POLLED
while (nrf52_i2c_getreg(priv,
NRF52_TWIM_EVENTS_LASTRX_OFFSET) != 1);
#endif
/* Stop TWIM */
nrf52_i2c_putreg(priv, NRF52_TWIM_TASKS_STOP_OFFSET, 1);
/* Clear event */
/* Wait for stop event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_LASTRX_OFFSET, 0);
#else
nxsem_wait(&priv->sem_isr);
#ifdef CONFIG_I2C_POLLED
while (nrf52_i2c_getreg(priv,
NRF52_TWIM_EVENTS_STOPPED_OFFSET) != 1);
if (priv->status < 0)
{
goto errout;
}
#endif
}
@ -372,6 +375,28 @@ static int nrf52_i2c_transfer(FAR struct i2c_master_s *dev,
}
while (priv->msgc > 0);
/* TWIM stop */
nrf52_i2c_putreg(priv, NRF52_TWIM_TASKS_STOP_OFFSET, 1);
/* Wait for stop event */
#ifdef CONFIG_I2C_POLLED
while (nrf52_i2c_getreg(priv,
NRF52_TWIM_EVENTS_STOPPED_OFFSET) != 1);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_STOPPED_OFFSET, 0);
#else
nxsem_wait(&priv->sem_isr);
if (priv->status < 0)
{
goto errout;
}
#endif
errout:
return ret;
}
@ -408,7 +433,80 @@ static int nrf52_i2c_reset(FAR struct i2c_master_s *dev)
#ifndef CONFIG_I2C_POLLED
static int nrf52_i2c_isr(int irq, void *context, FAR void *arg)
{
#error not implemented
FAR struct nrf52_i2c_priv_s *priv = (FAR struct nrf52_i2c_priv_s *)arg;
/* Reset I2C status */
priv->status = OK;
if ((priv->flags & I2C_M_READ) == 0)
{
if (nrf52_i2c_getreg(priv, NRF52_TWIM_EVENTS_LASTTX_OFFSET) == 1)
{
i2cinfo("I2C LASTTX\n");
/* TX done */
nxsem_post(&priv->sem_isr);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_LASTTX_OFFSET, 0);
return OK;
}
}
else
{
if (nrf52_i2c_getreg(priv, NRF52_TWIM_EVENTS_LASTRX_OFFSET) == 1)
{
i2cinfo("I2C LASTRX\n");
/* RX done */
nxsem_post(&priv->sem_isr);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_LASTRX_OFFSET, 0);
return OK;
}
}
if (nrf52_i2c_getreg(priv, NRF52_TWIM_EVENTS_STOPPED_OFFSET) == 1)
{
i2cinfo("I2C STOPPED\n");
/* STOPPED event */
nxsem_post(&priv->sem_isr);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_STOPPED_OFFSET, 0);
}
#ifdef CONFIG_NRF52_I2C_ERRORS
if (nrf52_i2c_getreg(priv, NRF52_TWIM_EVENTS_ERROR_OFFSET) == 1)
{
i2cerr("I2C ERROR\n");
/* Set ERROR status */
priv->status = ERROR;
/* ERROR event */
nxsem_post(&priv->sem_isr);
/* Clear event */
nrf52_i2c_putreg(priv, NRF52_TWIM_EVENTS_ERROR_OFFSET, 0);
}
#endif
return OK;
}
#endif
@ -454,6 +552,16 @@ static int nrf52_i2c_init(FAR struct nrf52_i2c_priv_s *priv)
nrf52_i2c_putreg(priv, NRF52_TWIS_ENABLE_OFFSET, TWIM_ENABLE_EN);
#ifndef CONFIG_I2C_POLLED
/* Enable I2C interrupts */
#ifdef CONFIG_NRF52_I2C_ERRORS
regval = (TWIM_INT_LASTRX | TWIM_INT_LASTTX | TWIM_INT_STOPPED |
TWIM_INT_ERROR);
#else
regval = (TWIM_INT_LASTRX | TWIM_INT_LASTTX | TWIM_INT_STOPPED);
#endif
nrf52_i2c_putreg(priv, NRF52_TWIM_INTEN_OFFSET, regval);
/* Attach error and event interrupts to the ISRs */
irq_attach(priv->irq, nrf52_i2c_isr, priv);
@ -560,6 +668,8 @@ FAR struct i2c_master_s *nrf52_i2cbus_initialize(int port)
FAR struct nrf52_i2c_priv_s *priv = NULL;
irqstate_t flags;
i2cinfo("I2C INITIALIZE port=%d\n", port);
/* Get interface */
switch (port)

View File

@ -60,6 +60,14 @@ ifeq ($(CONFIG_SENSORS_LSM6DSL),y)
CSRCS += nrf52_lsm6dsl.c
endif
ifeq ($(CONFIG_SENSORS_LSM303AGR),y)
CSRCS += nrf52_lsm303agr.c
endif
ifeq ($(CONFIG_SENSORS_HTS221),y)
CSRCS += nrf52_hts221.c
endif
ifeq ($(CONFIG_LPWAN_SX127X),y)
CSRCS += nrf52_sx127x.c
endif

View File

@ -128,10 +128,34 @@ void nrf52_spidev_initialize(void);
*
****************************************************************************/
#ifdef CONFIG_SENSORS_LSM303AGR
#ifdef CONFIG_SENSORS_LSM6DSL
int nrf52_lsm6dsl_initialize(char *devpath);
#endif
/*****************************************************************************
* Name: nrf52_lsm303agr_initialize
*
* Description:
* Initialize I2C-based LSM303AGR.
*
****************************************************************************/
#ifdef CONFIG_SENSORS_LSM303AGR
int nrf52_lsm303agr_initialize(char *devpath);
#endif
/*****************************************************************************
* Name: nrf52_hts221_initialize
*
* Description:
* Initialize I2C-based HTS221.
*
****************************************************************************/
#ifdef CONFIG_SENSORS_HTS221
int nrf52_hts221_initialize(char *devpath);
#endif
/*****************************************************************************
* Name: nrf52_lpwaninitialize
*

View File

@ -54,6 +54,10 @@
#ifdef CONFIG_ARCH_LEDS
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define LED_ON 0
#define LED_OFF 1

View File

@ -85,9 +85,9 @@ void nrf52_board_initialize(void)
* Description:
* If CONFIG_BOARD_LATE_INITIALIZE is selected, then an additional
* initialization call will be performed in the boot-up sequence to a
* function called board_late_initialize(). board_late_initialize() will be
* called immediately after up_initialize() is called and just before the
* initial application is started. This additional initialization phase
* function called board_late_initialize(). board_late_initialize() will
* be called immediately after up_initialize() is called and just before
* the initial application is started. This additional initialization phase
* may be used, for example, to initialize board-specific device drivers.
*
****************************************************************************/

View File

@ -144,6 +144,26 @@ int nrf52_bringup(void)
}
#endif /* CONFIG_SENSORS_LSM6DSL */
#ifdef CONFIG_SENSORS_LSM303AGR
ret = nrf52_lsm303agr_initialize("/dev/lsm303agr0");
if (ret < 0)
{
syslog(LOG_ERR,
"ERROR: Failed to initialize LSM303AGR driver: %d\n",
ret);
}
#endif /* CONFIG_SENSORS_LSM303AGR */
#ifdef CONFIG_SENSORS_HTS221
ret = nrf52_hts221_initialize("/dev/hts2210");
if (ret < 0)
{
syslog(LOG_ERR,
"ERROR: Failed to initialize HTS221 driver: %d\n",
ret);
}
#endif /* CONFIG_SENSORS_HTS221 */
#ifdef CONFIG_LPWAN_SX127X
ret = nrf52_lpwaninitialize();
if (ret < 0)

View File

@ -0,0 +1,175 @@
/****************************************************************************
* boards/arm/nrf52/nrf52840-dk/src/nrf52_hts221.c
*
* Copyright (C) 2020 Greg Nutt. All rights reserved.
* Author: Mateusz Szafoni <raiden00@railab.me>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include "nrf52_i2c.h"
#include "nrf52840-dk.h"
#include <nuttx/sensors/hts221.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NRF52_I2C0_MASTER
# error "HTS221 driver requires CONFIG_NRF52_I2C0_MASTER to be enabled"
#endif
/* HTS221 I2C address */
#define HTS221HUM_ADDR (0xbe >> 1) /* 7-bit */
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int nrf52_hts221_irq_attach(FAR struct hts221_config_s *state,
xcpt_t isr, FAR void *arg);
static void nrf52_hts221_irq_enable(FAR const struct hts221_config_s *state,
bool enable);
static void nrf52_hts221_irq_clear(FAR const struct hts221_config_s *state);
static int nrf52_hts221_set_power(FAR const struct hts221_config_s *state,
bool on);
/****************************************************************************
* Private Data
****************************************************************************/
static hts221_config_t g_hts221_config =
{
.irq_attach = nrf52_hts221_irq_attach,
.irq_enable = nrf52_hts221_irq_enable,
.irq_clear = nrf52_hts221_irq_clear,
.set_power = nrf52_hts221_set_power
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nrf52_hts221_irq_attach
****************************************************************************/
static int nrf52_hts221_irq_attach(FAR struct hts221_config_s *state,
xcpt_t isr, FAR void *arg)
{
sinfo("Attach HTS221 IRQ\n");
/* TODO: IRQ on rising edge */
/* nrf52_gpiosetevent(GPIO_HTS221_IRQ, true, false, false, isr, arg); */
return OK;
}
/****************************************************************************
* Name: nrf52_hts221_irq_enable
****************************************************************************/
static void nrf52_hts221_irq_enable(FAR const struct hts221_config_s *state,
bool enable)
{
return;
}
/****************************************************************************
* Name: nrf52_hts221_irq_clear
****************************************************************************/
static void nrf52_hts221_irq_clear(FAR const struct hts221_config_s *state)
{
return;
}
/****************************************************************************
* Name: nrf52_hts221_set_power
****************************************************************************/
static int nrf52_hts221_set_power(FAR const struct hts221_config_s *state,
bool on)
{
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nrf52_hts221_initialize
*
* Description:
* Initialize I2C-based HTS221.
*
****************************************************************************/
int nrf52_hts221_initialize(char *devpath)
{
FAR struct i2c_master_s *i2c;
int ret = OK;
sninfo("Initializing HTS221!\n");
#ifdef CONFIG_NRF52_I2C0_MASTER
i2c = nrf52_i2cbus_initialize(0);
if (i2c == NULL)
{
return -ENODEV;
}
sninfo("INFO: Initializing HTS221 hum-temp sensor over I2C%d\n", ret);
ret = hts221_register(devpath, i2c, HTS221HUM_ADDR, &g_hts221_config);
if (ret < 0)
{
snerr("ERROR: Failed to initialize HTS221 hum-temp driver %s\n",
devpath);
return -ENODEV;
}
sninfo("INFO: HTS221 sensor has been initialized successfully\n");
#endif
return ret;
}

View File

@ -0,0 +1,99 @@
/****************************************************************************
* boards/arm/nrf52/nrf52840-dk/src/nrf52_lsm303agr.c
*
* Copyright (C) 2020 Greg Nutt. All rights reserved.
* Author: Mateusz Szafoni <raiden00@railab.me>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include "nrf52_i2c.h"
#include "nrf52840-dk.h"
#include <nuttx/sensors/lsm303agr.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NRF52_I2C0_MASTER
# error "LSM303AGR driver requires CONFIG_NRF52_I2C0_MASTER to be enabled"
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nrf52_lsm303agr_initialize
*
* Description:
* Initialize I2C-based LSM303AGR.
*
****************************************************************************/
int nrf52_lsm303agr_initialize(char *devpath)
{
FAR struct i2c_master_s *i2c;
int ret = OK;
sninfo("Initializing LSM303AGR!\n");
#ifdef CONFIG_NRF52_I2C0_MASTER
i2c = nrf52_i2cbus_initialize(0);
if (i2c == NULL)
{
return -ENODEV;
}
sninfo("INFO: Initializing LSM303AGR accelero-magnet sensor over I2C%d\n",
ret);
ret = lsm303agr_sensor_register(devpath, i2c, LSM303AGRMAGNETO_ADDR);
if (ret < 0)
{
snerr("ERROR: Failed to initialize LSM303AGR acc-gyro driver %s\n",
devpath);
return -ENODEV;
}
sninfo("INFO: LSM303AGR sensor has been initialized successfully\n");
#endif
return ret;
}

View File

@ -1,4 +1,4 @@
/*****************************************************************************
/****************************************************************************
* boards/arm/nrf52/nrf52840-dk/src/nrf52_lsm6dsl.c
*
* Copyright (C) 2019 Greg Nutt. All rights reserved.
@ -32,7 +32,7 @@
* POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
/*****************************************************************************
/****************************************************************************
* Included Files
****************************************************************************/
@ -47,7 +47,7 @@
#include "nrf52840-dk.h"
#include <nuttx/sensors/lsm6dsl.h>
/*****************************************************************************
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -55,15 +55,16 @@
# error "LSM6DSL driver requires CONFIG_NRF52_I2C0_MASTER to be enabled"
#endif
/*****************************************************************************
/****************************************************************************
* Public Functions
****************************************************************************/
/*****************************************************************************
/****************************************************************************
* Name: nrf52_lsm6dsl_initialize
*
* Description:
* Initialize I2C-based LSM6DSL.
*
****************************************************************************/
int nrf52_lsm6dsl_initialize(char *devpath)
@ -80,7 +81,8 @@ int nrf52_lsm6dsl_initialize(char *devpath)
return -ENODEV;
}
sninfo("INFO: Initializing LMS6DSL accelero-gyro sensor over I2C%d\n", ret);
sninfo("INFO: Initializing LMS6DSL accelero-gyro sensor over I2C%d\n",
ret);
ret = lsm6dsl_sensor_register(devpath, i2c, LSM6DSLACCEL_ADDR1);
if (ret < 0)

View File

@ -99,8 +99,8 @@ void nrf52_spidev_initialize(void)
* functions in your board-specific logic. These functions will perform
* chip selection and status operations using GPIOs in the way your
* board is configured.
* 3. Add a calls to nrf52_spibus_initialize() in your low level application
* initialization logic
* 3. Add a calls to nrf52_spibus_initialize() in your low level
* application initialization logic
* 4. The handle returned by nrf52_spibus_initialize() may then be used to
* bind the SPI driver to higher level logic (e.g., calling
* mmcsd_spislotinitialize(), for example, will bind the SPI driver to