2013-06-01 16:03:55 +02:00
|
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
|
* drivers/wireless/nrf24l01.c
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
2021-03-31 14:42:54 +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
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
2021-03-31 14:42:54 +02:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
2021-03-31 14:42:54 +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.
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/* Features:
|
|
|
|
|
* - Fixed length and dynamically sized payloads (1 - 32 bytes)
|
|
|
|
|
* - Management of the 6 receiver pipes
|
2019-05-11 21:46:38 +02:00
|
|
|
|
* - Configuration of each pipe: address, packet length, auto-acknowledge,
|
|
|
|
|
* etc.
|
2013-06-01 16:03:55 +02:00
|
|
|
|
* - Use a FIFO buffer to store the received packets
|
|
|
|
|
*
|
|
|
|
|
* Todo:
|
|
|
|
|
* - Add support for payloads in ACK packets (?)
|
|
|
|
|
* - Add compatibility with nRF24L01 (not +) hardware (?)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <debug.h>
|
2019-05-11 21:14:54 +02:00
|
|
|
|
#include <fcntl.h>
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
|
#include <nuttx/mutex.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
|
#include <nuttx/semaphore.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
|
#include <nuttx/signal.h>
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
# include <nuttx/wqueue.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <nuttx/wireless/nrf24l01.h>
|
|
|
|
|
#include "nrf24l01.h"
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#ifndef CONFIG_WL_NRF24L01_DFLT_ADDR_WIDTH
|
|
|
|
|
# define CONFIG_WL_NRF24L01_DFLT_ADDR_WIDTH 5
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_WL_NRF24L01_RXFIFO_LEN
|
|
|
|
|
# define CONFIG_WL_NRF24L01_RXFIFO_LEN 128
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
#if defined(CONFIG_WL_NRF24L01_RXSUPPORT) && !defined(CONFIG_SCHED_HPWORK)
|
|
|
|
|
# error RX support requires CONFIG_SCHED_HPWORK
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_CHECK_PARAMS
|
2014-04-12 21:09:48 +02:00
|
|
|
|
# define CHECK_ARGS(cond) do { if (!(cond)) return -EINVAL; } while (0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#else
|
|
|
|
|
# define CHECK_ARGS(cond)
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* NRF24L01 Definitions *****************************************************/
|
|
|
|
|
|
|
|
|
|
/* Default SPI bus frequency (in Hz).
|
|
|
|
|
* Can go up to 10 Mbs according to datasheet.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define NRF24L01_SPIFREQ 9000000
|
|
|
|
|
|
|
|
|
|
/* power-down -> standby transition timing (in us). Note: this value is
|
|
|
|
|
* probably larger than required.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#define NRF24L01_TPD2STBY_DELAY 4500
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Max time to wait for TX irq (in ms) */
|
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
#define NRF24L01_MAX_TX_IRQ_WAIT 2000
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#define FIFO_PKTLEN_MASK 0x1F /* 5 ls bits used to store packet length */
|
|
|
|
|
#define FIFO_PKTLEN_SHIFT 0
|
|
|
|
|
#define FIFO_PIPENO_MASK 0xE0 /* 3 ms bits used to store pipe # */
|
2019-06-29 18:56:57 +02:00
|
|
|
|
#define FIFO_PIPENO_SHIFT 5
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 15:57:34 +01:00
|
|
|
|
#define FIFO_PKTLEN(dev) \
|
|
|
|
|
(((dev->rx_fifo[dev->nxt_read] & FIFO_PKTLEN_MASK) >> FIFO_PKTLEN_SHIFT) + 1)
|
|
|
|
|
#define FIFO_PIPENO(dev) \
|
|
|
|
|
(((dev->rx_fifo[dev->nxt_read] & FIFO_PIPENO_MASK) >> FIFO_PIPENO_SHIFT))
|
|
|
|
|
#define FIFO_HEADER(pktlen,pipeno) \
|
|
|
|
|
((pktlen - 1) | (pipeno << FIFO_PIPENO_SHIFT))
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 15:57:34 +01:00
|
|
|
|
#define DEV_NAME "/dev/nrf24l01"
|
|
|
|
|
#define FL_AA_ENABLED (1 << 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Data Types
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
MODE_READ,
|
|
|
|
|
MODE_WRITE
|
|
|
|
|
} nrf24l01_access_mode_t;
|
|
|
|
|
|
|
|
|
|
struct nrf24l01_dev_s
|
|
|
|
|
{
|
|
|
|
|
FAR struct spi_dev_s *spi; /* Reference to SPI bus device */
|
|
|
|
|
FAR struct nrf24l01_config_s *config; /* Board specific GPIO functions */
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_state_t state; /* Current state of the nRF24L01 */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:14:54 +02:00
|
|
|
|
bool tx_payload_noack; /* TX without waiting for ACK */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
uint8_t en_aa; /* Cache EN_AA register value */
|
|
|
|
|
uint8_t en_pipes; /* Cache EN_RXADDR register value */
|
|
|
|
|
bool ce_enabled; /* Cache the value of CE pin */
|
|
|
|
|
uint8_t lastxmitcount; /* Retransmit count of the last succeeded AA transmission */
|
|
|
|
|
uint8_t addrlen; /* Address width (3-5) */
|
|
|
|
|
uint8_t pipedatalen[NRF24L01_PIPE_COUNT];
|
|
|
|
|
|
|
|
|
|
uint8_t pipe0addr[NRF24L01_MAX_ADDR_LEN]; /* Configured address on pipe 0 */
|
|
|
|
|
|
|
|
|
|
uint8_t last_recvpipeno;
|
|
|
|
|
sem_t sem_tx;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
bool tx_pending; /* Is userspace waiting for TX IRQ? - accessor
|
|
|
|
|
* needs to hold lock on SPI bus */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
uint8_t *rx_fifo; /* Circular RX buffer. [pipe# / pkt_len] [packet data...] */
|
|
|
|
|
uint16_t fifo_len; /* Number of bytes stored in fifo */
|
|
|
|
|
uint16_t nxt_read; /* Next read index */
|
|
|
|
|
uint16_t nxt_write; /* Next write index */
|
2022-09-06 08:18:45 +02:00
|
|
|
|
mutex_t lock_fifo; /* Protect access to rx fifo */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
sem_t sem_rx; /* Wait for availability of received data */
|
|
|
|
|
|
|
|
|
|
struct work_s irq_work; /* Interrupt handling "bottom half" */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uint8_t nopens; /* Number of times the device has been opened */
|
2022-09-06 08:18:45 +02:00
|
|
|
|
mutex_t devlock; /* Ensures exclusive access to this structure */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
FAR struct pollfd *pfd; /* Polled file descr (or NULL if any) */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Function Prototypes
|
|
|
|
|
****************************************************************************/
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
/* Low-level SPI helpers */
|
|
|
|
|
|
|
|
|
|
static inline void nrf24l01_configspi(FAR struct spi_dev_s *spi);
|
|
|
|
|
static void nrf24l01_lock(FAR struct spi_dev_s *spi);
|
|
|
|
|
static void nrf24l01_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
|
|
|
|
|
|
static uint8_t nrf24l01_access(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
nrf24l01_access_mode_t mode, uint8_t cmd,
|
|
|
|
|
FAR uint8_t *buf, int length);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static uint8_t nrf24l01_flush_rx(FAR struct nrf24l01_dev_s *dev);
|
|
|
|
|
static uint8_t nrf24l01_flush_tx(FAR struct nrf24l01_dev_s *dev);
|
|
|
|
|
|
|
|
|
|
/* Read register from nrf24 */
|
|
|
|
|
|
|
|
|
|
static uint8_t nrf24l01_readreg(FAR struct nrf24l01_dev_s *dev, uint8_t reg,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
FAR uint8_t *value, int len);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Read single byte value from a register of nrf24 */
|
|
|
|
|
|
|
|
|
|
static uint8_t nrf24l01_readregbyte(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
uint8_t reg);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static void nrf24l01_writeregbyte(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
uint8_t reg, uint8_t value);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static uint8_t nrf24l01_setregbit(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
uint8_t reg, uint8_t value, bool set);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static void nrf24l01_tostate(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
nrf24l01_state_t state);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static int nrf24l01_irqhandler(FAR int irq, FAR void *context,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
FAR void *arg);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static inline int nrf24l01_attachirq(FAR struct nrf24l01_dev_s *dev,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
xcpt_t isr, FAR void *arg);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static int dosend(FAR struct nrf24l01_dev_s *dev, FAR const uint8_t *data,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
size_t datalen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static int nrf24l01_unregister(FAR struct nrf24l01_dev_s *dev);
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static void fifoput(FAR struct nrf24l01_dev_s *dev, uint8_t pipeno,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
FAR uint8_t *buffer, uint8_t buflen);
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static uint8_t fifoget(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
uint8_t buflen, FAR uint8_t *pipeno);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static void nrf24l01_worker(FAR void *arg);
|
2017-03-11 00:29:58 +01:00
|
|
|
|
#endif
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#ifdef CONFIG_DEBUG_WIRELESS
|
2023-03-05 15:34:33 +01:00
|
|
|
|
static void binarycvt(FAR char *deststr, size_t destlen,
|
|
|
|
|
FAR const uint8_t *srcbin, size_t srclen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* POSIX API */
|
|
|
|
|
|
|
|
|
|
static int nrf24l01_open(FAR struct file *filep);
|
|
|
|
|
static int nrf24l01_close(FAR struct file *filep);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static ssize_t nrf24l01_read(FAR struct file *filep, FAR char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
size_t buflen);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static ssize_t nrf24l01_write(FAR struct file *filep,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
FAR const char *buffer, size_t buflen);
|
2017-02-27 19:24:34 +01:00
|
|
|
|
static int nrf24l01_ioctl(FAR struct file *filep, int cmd,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
unsigned long arg);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static int nrf24l01_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-04-12 12:16:13 +02:00
|
|
|
|
bool setup);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
|
static const struct file_operations g_nrf24l01_fops =
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2015-01-31 19:29:21 +01:00
|
|
|
|
nrf24l01_open, /* open */
|
|
|
|
|
nrf24l01_close, /* close */
|
|
|
|
|
nrf24l01_read, /* read */
|
|
|
|
|
nrf24l01_write, /* write */
|
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
|
nrf24l01_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
|
nrf24l01_poll /* poll */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_lock
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static void nrf24l01_lock(FAR struct spi_dev_s *spi)
|
|
|
|
|
{
|
|
|
|
|
/* Lock the SPI bus because there are multiple devices competing for the
|
|
|
|
|
* SPI bus
|
|
|
|
|
*/
|
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
|
SPI_LOCK(spi, true);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* We have the lock. Now make sure that the SPI bus is configured for the
|
|
|
|
|
* NRF24L01 (it might have gotten configured for a different device while
|
|
|
|
|
* unlocked)
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(spi, SPIDEV_WIRELESS(0), true);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE0);
|
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
|
SPI_SETFREQUENCY(spi, NRF24L01_SPIFREQ);
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(spi, SPIDEV_WIRELESS(0), false);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
|
* Name: nrf24l01_unlock
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
|
|
|
|
* Description:
|
2016-01-24 01:54:36 +01:00
|
|
|
|
* Un-lock the SPI bus after each transfer, possibly losing the current
|
|
|
|
|
* configuration if we are sharing the SPI bus with other devices.
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
|
* Input Parameters:
|
2013-06-01 16:03:55 +02:00
|
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* None
|
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void nrf24l01_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
|
{
|
|
|
|
|
/* Relinquish the SPI bus. */
|
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
|
SPI_LOCK(spi, false);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
|
* Name: nrf24l01_configspi
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
|
|
|
|
* Description:
|
2016-01-24 01:54:36 +01:00
|
|
|
|
* Configure the SPI for use with the NRF24L01.
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
|
* Input Parameters:
|
2013-06-01 16:03:55 +02:00
|
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* None
|
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline void nrf24l01_configspi(FAR struct spi_dev_s *spi)
|
|
|
|
|
{
|
2016-01-24 01:54:36 +01:00
|
|
|
|
/* Configure SPI for the NRF24L01 module. */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(spi, SPIDEV_WIRELESS(0), true); /* Useful ? */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE0);
|
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
|
SPI_SETFREQUENCY(spi, NRF24L01_SPIFREQ);
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(spi, SPIDEV_WIRELESS(0), false);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_select
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
static inline void nrf24l01_select(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(dev->spi, SPIDEV_WIRELESS(0), true);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_deselect
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
static inline void nrf24l01_deselect(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-04-29 20:26:52 +02:00
|
|
|
|
SPI_SELECT(dev->spi, SPIDEV_WIRELESS(0), false);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_access
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static uint8_t nrf24l01_access(FAR struct nrf24l01_dev_s *dev,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_access_mode_t mode, uint8_t cmd,
|
|
|
|
|
FAR uint8_t *buf, int length)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
|
|
/* Prepare SPI */
|
|
|
|
|
|
|
|
|
|
nrf24l01_select(dev);
|
|
|
|
|
|
|
|
|
|
/* Transfer */
|
|
|
|
|
|
|
|
|
|
status = SPI_SEND(dev->spi, cmd);
|
|
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
|
{
|
|
|
|
|
case MODE_WRITE:
|
|
|
|
|
if (length > 0)
|
|
|
|
|
{
|
|
|
|
|
SPI_SNDBLOCK(dev->spi, buf, length);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MODE_READ:
|
|
|
|
|
SPI_RECVBLOCK(dev->spi, buf, length);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_deselect(dev);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_flush_rx
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static inline uint8_t nrf24l01_flush_rx(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
return nrf24l01_access(dev, MODE_WRITE, NRF24L01_FLUSH_RX, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_flush_tx
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static inline uint8_t nrf24l01_flush_tx(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
return nrf24l01_access(dev, MODE_WRITE, NRF24L01_FLUSH_TX, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_readreg
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Read register from nrf24l01
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static inline uint8_t nrf24l01_readreg(FAR struct nrf24l01_dev_s *dev,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
uint8_t reg, FAR uint8_t *value,
|
|
|
|
|
int len)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
return nrf24l01_access(dev, MODE_READ, reg | NRF24L01_R_REGISTER,
|
|
|
|
|
value, len);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_readregbyte
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Read single byte value from a register of nrf24l01
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static inline uint8_t nrf24l01_readregbyte(FAR struct nrf24l01_dev_s *dev,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
uint8_t reg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t val;
|
|
|
|
|
nrf24l01_readreg(dev, reg, &val, 1);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_writereg
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Write value to a register of nrf24l01
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
static inline int nrf24l01_writereg(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
uint8_t reg, FAR const uint8_t *value,
|
|
|
|
|
int len)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
return nrf24l01_access(dev, MODE_WRITE, reg | NRF24L01_W_REGISTER,
|
|
|
|
|
(FAR uint8_t *)value, len);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_writeregbyte
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Write single byte value to a register of nrf24l01
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
static inline void nrf24l01_writeregbyte(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
uint8_t reg, uint8_t value)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
nrf24l01_writereg(dev, reg, &value, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setregbit
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static uint8_t nrf24l01_setregbit(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
uint8_t reg, uint8_t value, bool set)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
|
|
nrf24l01_readreg(dev, reg, &val, 1);
|
|
|
|
|
if (set)
|
|
|
|
|
{
|
|
|
|
|
val |= value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
val &= ~value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_writereg(dev, reg, &val, 1);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: fifoput
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* RX fifo mgt
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
static void fifoput(FAR struct nrf24l01_dev_s *dev, uint8_t pipeno,
|
|
|
|
|
FAR uint8_t *buffer, uint8_t buflen)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_lock(&dev->lock_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
while (dev->fifo_len + buflen + 1 > CONFIG_WL_NRF24L01_RXFIFO_LEN)
|
|
|
|
|
{
|
|
|
|
|
/* TODO: Set fifo overrun flag ! */
|
|
|
|
|
|
|
|
|
|
int skiplen = FIFO_PKTLEN(dev) + 1;
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
dev->nxt_read = (dev->nxt_read + skiplen) %
|
|
|
|
|
CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
dev->fifo_len -= skiplen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->rx_fifo[dev->nxt_write] = FIFO_HEADER(buflen, pipeno);
|
|
|
|
|
dev->nxt_write = (dev->nxt_write + 1) % CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
|
|
|
|
|
|
|
|
|
/* Adjust fifo bytes count */
|
|
|
|
|
|
|
|
|
|
dev->fifo_len += (buflen + 1);
|
|
|
|
|
while (buflen--)
|
|
|
|
|
{
|
|
|
|
|
dev->rx_fifo[dev->nxt_write] = *(buffer++);
|
|
|
|
|
dev->nxt_write = (dev->nxt_write + 1) % CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->lock_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: fifoget
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static uint8_t fifoget(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *buffer,
|
2019-05-11 21:46:38 +02:00
|
|
|
|
uint8_t buflen, FAR uint8_t *pipeno)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t pktlen;
|
|
|
|
|
uint8_t i;
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_lock(&dev->lock_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* sem_rx contains count of inserted packets in FIFO, but FIFO can
|
|
|
|
|
* overflow - fail smart.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (dev->fifo_len == 0)
|
2017-03-10 23:21:49 +01:00
|
|
|
|
{
|
|
|
|
|
pktlen = 0;
|
|
|
|
|
goto no_data;
|
|
|
|
|
}
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
pktlen = FIFO_PKTLEN(dev);
|
|
|
|
|
if (NULL != pipeno)
|
|
|
|
|
{
|
|
|
|
|
*pipeno = FIFO_PIPENO(dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->nxt_read = (dev->nxt_read + 1) % CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < pktlen && i < buflen; i++)
|
|
|
|
|
{
|
|
|
|
|
*(buffer++) = dev->rx_fifo[dev->nxt_read];
|
2019-05-11 21:46:38 +02:00
|
|
|
|
dev->nxt_read = (dev->nxt_read + 1) %
|
|
|
|
|
CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i < pktlen)
|
|
|
|
|
{
|
2019-05-11 21:46:38 +02:00
|
|
|
|
dev->nxt_read = (dev->nxt_read + pktlen - i) %
|
|
|
|
|
CONFIG_WL_NRF24L01_RXFIFO_LEN;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Adjust fifo bytes count */
|
|
|
|
|
|
|
|
|
|
dev->fifo_len -= (pktlen + 1);
|
|
|
|
|
|
2022-09-26 06:56:25 +02:00
|
|
|
|
no_data:
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->lock_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
return pktlen;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_irqhandler
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-02-27 18:41:48 +01:00
|
|
|
|
static int nrf24l01_irqhandler(int irq, FAR void *context, FAR void *arg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-02-27 18:41:48 +01:00
|
|
|
|
FAR struct nrf24l01_dev_s *dev = (FAR struct nrf24l01_dev_s *)arg;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("*IRQ*\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
/* If RX is enabled we delegate the actual work to bottom-half handler */
|
|
|
|
|
|
2017-02-27 18:58:20 +01:00
|
|
|
|
work_queue(HPWORK, &dev->irq_work, nrf24l01_worker, dev, 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#else
|
|
|
|
|
/* Otherwise we simply wake up the send function */
|
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
|
nxsem_post(&dev->sem_tx); /* Wake up the send function */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_attachirq
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Configure IRQ pin (falling edge)
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static inline int nrf24l01_attachirq(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
xcpt_t isr, FAR void *arg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-02-27 19:11:35 +01:00
|
|
|
|
return dev->config->irqattach(isr, arg);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_chipenable
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static inline bool nrf24l01_chipenable(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
bool enable)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
if (dev->ce_enabled != enable)
|
|
|
|
|
{
|
|
|
|
|
dev->config->chipenable(enable);
|
|
|
|
|
dev->ce_enabled = enable;
|
|
|
|
|
return !enable;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return enable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_worker
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static void nrf24l01_worker(FAR void *arg)
|
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
|
FAR struct nrf24l01_dev_s *dev = (FAR struct nrf24l01_dev_s *)arg;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
uint8_t status;
|
|
|
|
|
uint8_t fifo_status;
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
status = nrf24l01_readregbyte(dev, NRF24L01_STATUS);
|
|
|
|
|
|
|
|
|
|
if (status & NRF24L01_RX_DR)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Put CE low */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
bool ce = nrf24l01_chipenable(dev, false);
|
2017-03-11 00:29:58 +01:00
|
|
|
|
bool has_data = false;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("RX_DR is set!\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Read and store all received payloads */
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
uint8_t pipeno;
|
|
|
|
|
uint8_t pktlen;
|
|
|
|
|
uint8_t buf[NRF24L01_MAX_PAYLOAD_LEN];
|
|
|
|
|
|
|
|
|
|
/* For each packet:
|
|
|
|
|
* - Get pipe #
|
|
|
|
|
* - Get payload length (either static or dynamic)
|
|
|
|
|
* - Read payload content
|
|
|
|
|
*/
|
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
|
pipeno = (status & NRF24L01_RX_P_NO_MASK) >>
|
|
|
|
|
NRF24L01_RX_P_NO_SHIFT;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
if (pipeno >= NRF24L01_PIPE_COUNT) /* 6=invalid 7=fifo empty */
|
2017-03-10 23:21:49 +01:00
|
|
|
|
{
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlerr("invalid pipe rx: %d\n", (int)pipeno);
|
2017-03-10 23:21:49 +01:00
|
|
|
|
nrf24l01_flush_rx(dev);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
pktlen = dev->pipedatalen[pipeno];
|
|
|
|
|
if (NRF24L01_DYN_LENGTH == pktlen)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* If dynamic length payload need to use R_RX_PL_WID command
|
|
|
|
|
* to get actual length.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
nrf24l01_access(dev, MODE_READ, NRF24L01_R_RX_PL_WID, &pktlen,
|
|
|
|
|
1);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
if (pktlen > NRF24L01_MAX_PAYLOAD_LEN) /* bad length */
|
2017-03-10 23:21:49 +01:00
|
|
|
|
{
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlerr("invalid length in rx: %d\n", (int)pktlen);
|
2017-03-10 23:21:49 +01:00
|
|
|
|
nrf24l01_flush_rx(dev);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
/* Get payload content */
|
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
|
nrf24l01_access(dev, MODE_READ,
|
|
|
|
|
NRF24L01_R_RX_PAYLOAD, buf, pktlen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
fifoput(dev, pipeno, buf, pktlen);
|
2017-03-10 23:21:49 +01:00
|
|
|
|
has_data = true;
|
2017-10-03 23:35:24 +02:00
|
|
|
|
nxsem_post(&dev->sem_rx); /* Wake-up any thread waiting in recv */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
status = nrf24l01_readreg(dev, NRF24L01_FIFO_STATUS, &fifo_status,
|
|
|
|
|
1);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("FIFO_STATUS=%02x\n", fifo_status);
|
|
|
|
|
wlinfo("STATUS=%02x\n", status);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-11 00:29:58 +01:00
|
|
|
|
while ((fifo_status & NRF24L01_RX_EMPTY) == 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
if (dev->pfd && has_data)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
|
poll_notify(&dev->pfd, 1, POLLIN);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
|
|
|
|
/* Clear interrupt sources */
|
|
|
|
|
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_STATUS, NRF24L01_RX_DR);
|
|
|
|
|
|
|
|
|
|
/* Restore CE */
|
|
|
|
|
|
|
|
|
|
nrf24l01_chipenable(dev, ce);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status & (NRF24L01_TX_DS | NRF24L01_MAX_RT))
|
|
|
|
|
{
|
2019-12-05 18:49:12 +01:00
|
|
|
|
/* Confirm send */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-12-05 18:49:12 +01:00
|
|
|
|
nrf24l01_chipenable(dev, false);
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2019-12-05 18:49:12 +01:00
|
|
|
|
if (dev->tx_pending)
|
|
|
|
|
{
|
|
|
|
|
/* The actual work is done in the send function */
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2019-12-05 18:49:12 +01:00
|
|
|
|
nxsem_post(&dev->sem_tx);
|
|
|
|
|
}
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dev->state == ST_RX)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Re-enable CE (to go back to RX mode state) */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_chipenable(dev, true);
|
|
|
|
|
}
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_tostate
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
static void nrf24l01_tostate(FAR struct nrf24l01_dev_s *dev,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_state_t state)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
nrf24l01_state_t oldstate = dev->state;
|
|
|
|
|
|
|
|
|
|
if (oldstate == state)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (oldstate == ST_POWER_DOWN)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Leaving power down (note: new state cannot be power down here) */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_CONFIG, NRF24L01_PWR_UP, true);
|
2017-10-06 18:15:01 +02:00
|
|
|
|
nxsig_usleep(NRF24L01_TPD2STBY_DELAY);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Entering new state */
|
|
|
|
|
|
2015-10-10 19:51:32 +02:00
|
|
|
|
switch (state)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
case ST_UNKNOWN:
|
2019-12-05 18:49:12 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
/* Power down the module here... */
|
2017-03-11 15:57:34 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
case ST_POWER_DOWN:
|
|
|
|
|
nrf24l01_chipenable(dev, false);
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_CONFIG, NRF24L01_PWR_UP, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ST_STANDBY:
|
|
|
|
|
nrf24l01_chipenable(dev, false);
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_CONFIG, NRF24L01_PRIM_RX, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
case ST_RX:
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_CONFIG, NRF24L01_PRIM_RX, true);
|
|
|
|
|
nrf24l01_chipenable(dev, true);
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->state = state;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: dosend
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static int dosend(FAR struct nrf24l01_dev_s *dev, FAR const uint8_t *data,
|
|
|
|
|
size_t datalen)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t status;
|
|
|
|
|
uint8_t obsvalue;
|
2019-05-11 21:46:38 +02:00
|
|
|
|
uint8_t cmd;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Store the current lifecycle state in order to restore it after transmit
|
|
|
|
|
* done.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_state_t prevstate = dev->state;
|
|
|
|
|
|
|
|
|
|
nrf24l01_tostate(dev, ST_STANDBY);
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Flush old - can't harm */
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
|
|
|
|
nrf24l01_flush_tx(dev);
|
|
|
|
|
|
2019-05-11 21:14:54 +02:00
|
|
|
|
/* Write payload - use different command depending on ACK setting */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
cmd = dev->tx_payload_noack ? NRF24L01_W_TX_PAYLOAD_NOACK :
|
|
|
|
|
NRF24L01_W_TX_PAYLOAD;
|
|
|
|
|
nrf24l01_access(dev, MODE_WRITE, cmd, (FAR uint8_t *)data, datalen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
dev->tx_pending = true;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Free the SPI bus during the IRQ wait */
|
|
|
|
|
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Cause rising CE edge to start transmission */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
nrf24l01_chipenable(dev, true);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
/* Wait for IRQ (TX_DS or MAX_RT) - but don't hang on lost IRQ */
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2022-05-10 02:33:09 +02:00
|
|
|
|
ret = nxsem_tickwait(&dev->sem_tx, MSEC2TICK(NRF24L01_MAX_TX_IRQ_WAIT));
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Re-acquire the SPI bus */
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
dev->tx_pending = false;
|
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2017-03-11 00:29:58 +01:00
|
|
|
|
{
|
2019-12-05 18:49:12 +01:00
|
|
|
|
wlerr("wait for irq failed\n");
|
|
|
|
|
nrf24l01_flush_tx(dev);
|
|
|
|
|
goto out;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
}
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
status = nrf24l01_readreg(dev, NRF24L01_OBSERVE_TX, &obsvalue, 1);
|
|
|
|
|
if (status & NRF24L01_TX_DS)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Transmit OK */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = OK;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
dev->lastxmitcount = (obsvalue & NRF24L01_ARC_CNT_MASK)
|
|
|
|
|
>> NRF24L01_ARC_CNT_SHIFT;
|
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Transmission OK (lastxmitcount=%d)\n", dev->lastxmitcount);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
else if (status & NRF24L01_MAX_RT)
|
|
|
|
|
{
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("MAX_RT! (lastxmitcount=%d)\n", dev->lastxmitcount);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -ECOMM;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
dev->lastxmitcount = NRF24L01_XMIT_MAXRT;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* If no ACK packet is received the payload remains in TX fifo. We
|
|
|
|
|
* need to flush it.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_flush_tx(dev);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Unexpected... */
|
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlerr("ERROR: No TX_DS nor MAX_RT bit set in STATUS reg!\n");
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -EIO;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
out:
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
/* Clear interrupt sources */
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_STATUS, NRF24L01_TX_DS |
|
|
|
|
|
NRF24L01_MAX_RT);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
/* Clear fifo */
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2017-03-10 23:21:49 +01:00
|
|
|
|
nrf24l01_flush_tx(dev);
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
/* Restore state */
|
|
|
|
|
|
|
|
|
|
nrf24l01_tostate(dev, prevstate);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: binarycvt
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#ifdef CONFIG_DEBUG_WIRELESS
|
2023-03-05 15:34:33 +01:00
|
|
|
|
static void binarycvt(FAR char *deststr, size_t destlen,
|
|
|
|
|
FAR const uint8_t *srcbin, size_t srclen)
|
2017-03-11 00:29:58 +01:00
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
2023-03-05 15:34:33 +01:00
|
|
|
|
while (i < srclen && 2 * (i + 1) < destlen)
|
2017-03-11 00:29:58 +01:00
|
|
|
|
{
|
2023-03-05 15:34:33 +01:00
|
|
|
|
snprintf(deststr + i * 2, destlen - i * 2, "%02x", srcbin[i]);
|
2017-03-11 00:29:58 +01:00
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
*(deststr + i * 2) = '\0';
|
2017-03-11 00:29:58 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* POSIX API
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_open
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
static int nrf24l01_open(FAR struct file *filep)
|
|
|
|
|
{
|
|
|
|
|
FAR struct inode *inode;
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Opening nRF24L01 dev\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if device is not already used */
|
|
|
|
|
|
|
|
|
|
if (dev->nopens > 0)
|
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -EBUSY;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = nrf24l01_init(dev);
|
|
|
|
|
if (!ret)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
dev->nopens++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_close
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static int nrf24l01_close(FAR struct file *filep)
|
|
|
|
|
{
|
|
|
|
|
FAR struct inode *inode;
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Closing nRF24L01 dev\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_changestate(dev, ST_POWER_DOWN);
|
|
|
|
|
dev->nopens--;
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_read
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static ssize_t nrf24l01_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
|
size_t buflen)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
#ifndef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
return -ENOSYS;
|
|
|
|
|
#else
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
|
|
|
|
FAR struct inode *inode;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 21:14:54 +02:00
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
|
{
|
|
|
|
|
int packet_count;
|
2019-05-11 21:46:38 +02:00
|
|
|
|
|
|
|
|
|
/* Test if data is ready */
|
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
|
ret = nxsem_get_value(&dev->sem_rx, &packet_count);
|
2019-05-11 21:14:54 +02:00
|
|
|
|
if (ret)
|
|
|
|
|
{
|
|
|
|
|
goto errout; /* getvalue failed */
|
|
|
|
|
}
|
2019-05-11 21:46:38 +02:00
|
|
|
|
|
|
|
|
|
if (!packet_count)
|
2019-05-11 21:14:54 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -EWOULDBLOCK; /* don't wait for packets */
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nrf24l01_recv(dev, (FAR uint8_t *)buffer, buflen,
|
|
|
|
|
&dev->last_recvpipeno);
|
2019-05-11 21:46:38 +02:00
|
|
|
|
|
2019-05-11 21:14:54 +02:00
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_write
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static ssize_t nrf24l01_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
|
size_t buflen)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
|
|
|
|
FAR struct inode *inode;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nrf24l01_send(dev, (FAR const uint8_t *)buffer, buflen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_ioctl
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static int nrf24l01_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
|
{
|
|
|
|
|
FAR struct inode *inode;
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("cmd: %d arg: %ld\n", cmd, arg);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Process the IOCTL by command */
|
|
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case WLIOC_SETRADIOFREQ: /* Set radio frequency. Arg: Pointer to
|
|
|
|
|
* uint32_t frequency value */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
|
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
|
|
|
|
|
nrf24l01_setradiofreq(dev, *ptr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case WLIOC_GETRADIOFREQ: /* Get current radio frequency. arg: Pointer
|
|
|
|
|
* to uint32_t frequency value */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
|
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
*ptr = nrf24l01_getradiofreq(dev);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case NRF24L01IOC_SETTXADDR: /* Set current TX addr. arg: Pointer to
|
|
|
|
|
* uint8_t array defining the address */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR const uint8_t *addr = (FAR const uint8_t *)(arg);
|
|
|
|
|
DEBUGASSERT(addr != NULL);
|
|
|
|
|
nrf24l01_settxaddr(dev, addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case NRF24L01IOC_GETTXADDR: /* Get current TX addr. arg: Pointer to
|
|
|
|
|
* uint8_t array defining the address */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR uint8_t *addr = (FAR uint8_t *)(arg);
|
|
|
|
|
DEBUGASSERT(addr != NULL);
|
|
|
|
|
nrf24l01_gettxaddr(dev, addr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case WLIOC_SETTXPOWER: /* Set current radio frequency. arg: Pointer
|
|
|
|
|
* to int32_t, output power */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR int32_t *ptr = (FAR int32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
nrf24l01_settxpower(dev, *ptr);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case WLIOC_GETTXPOWER: /* Get current radio frequency. arg: Pointer
|
|
|
|
|
* to int32_t, output power */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR int32_t *ptr = (FAR int32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
*ptr = nrf24l01_gettxpower(dev);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case NRF24L01IOC_SETRETRCFG: /* Set retransmit params. arg: Pointer
|
|
|
|
|
* to nrf24l01_retrcfg_t */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR nrf24l01_retrcfg_t *ptr = (FAR nrf24l01_retrcfg_t *)(arg);
|
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
nrf24l01_setretransmit(dev, ptr->delay, ptr->count);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
case NRF24L01IOC_GETRETRCFG: /* Get retransmit params. arg: Pointer
|
|
|
|
|
* to nrf24l01_retrcfg_t */
|
2020-05-04 16:15:10 +02:00
|
|
|
|
ret = -ENOSYS; /* TODO */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_SETPIPESCFG:
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2019-05-11 21:46:38 +02:00
|
|
|
|
FAR nrf24l01_pipecfg_t **cfg_array =
|
|
|
|
|
(FAR nrf24l01_pipecfg_t **)(arg);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
DEBUGASSERT(cfg_array != NULL);
|
|
|
|
|
for (i = 0; i < NRF24L01_PIPE_COUNT; i++)
|
|
|
|
|
{
|
|
|
|
|
if (cfg_array[i])
|
|
|
|
|
{
|
|
|
|
|
nrf24l01_setpipeconfig(dev, i, cfg_array[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETPIPESCFG:
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2019-05-11 21:46:38 +02:00
|
|
|
|
FAR nrf24l01_pipecfg_t **cfg_array =
|
|
|
|
|
(FAR nrf24l01_pipecfg_t **)(arg);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
DEBUGASSERT(cfg_array != NULL);
|
|
|
|
|
for (i = 0; i < NRF24L01_PIPE_COUNT; i++)
|
|
|
|
|
{
|
|
|
|
|
if (cfg_array[i])
|
|
|
|
|
{
|
|
|
|
|
nrf24l01_getpipeconfig(dev, i, cfg_array[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_SETPIPESENABLED:
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
uint8_t en_pipes;
|
|
|
|
|
|
|
|
|
|
FAR uint8_t *en_pipesp = (FAR uint8_t *)(arg);
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(en_pipesp != NULL);
|
|
|
|
|
en_pipes = *en_pipesp;
|
|
|
|
|
for (i = 0; i < NRF24L01_PIPE_COUNT; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((dev->en_pipes & (1 << i)) != (en_pipes & (1 << i)))
|
|
|
|
|
{
|
|
|
|
|
nrf24l01_enablepipe(dev, i, en_pipes & (1 << i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETPIPESENABLED:
|
|
|
|
|
{
|
|
|
|
|
FAR uint8_t *en_pipesp = (FAR uint8_t *)(arg);
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(en_pipesp != NULL);
|
|
|
|
|
*en_pipesp = dev->en_pipes;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_SETDATARATE:
|
|
|
|
|
{
|
|
|
|
|
FAR nrf24l01_datarate_t *drp = (FAR nrf24l01_datarate_t *)(arg);
|
|
|
|
|
DEBUGASSERT(drp != NULL);
|
|
|
|
|
|
|
|
|
|
nrf24l01_setdatarate(dev, *drp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETDATARATE:
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -ENOSYS; /* TODO */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_SETADDRWIDTH:
|
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *widthp = (FAR uint32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(widthp != NULL);
|
|
|
|
|
|
|
|
|
|
nrf24l01_setaddrwidth(dev, *widthp);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETADDRWIDTH:
|
|
|
|
|
{
|
|
|
|
|
FAR int *widthp = (FAR int *)(arg);
|
|
|
|
|
DEBUGASSERT(widthp != NULL);
|
|
|
|
|
|
|
|
|
|
*widthp = (int)dev->addrlen;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_SETSTATE:
|
|
|
|
|
{
|
|
|
|
|
FAR nrf24l01_state_t *statep = (FAR nrf24l01_state_t *)(arg);
|
|
|
|
|
DEBUGASSERT(statep != NULL);
|
|
|
|
|
|
|
|
|
|
nrf24l01_changestate(dev, *statep);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETSTATE:
|
|
|
|
|
{
|
|
|
|
|
FAR nrf24l01_state_t *statep = (FAR nrf24l01_state_t *)(arg);
|
|
|
|
|
DEBUGASSERT(statep != NULL);
|
|
|
|
|
|
|
|
|
|
*statep = dev->state;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETLASTXMITCOUNT:
|
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *xmitcntp = (FAR uint32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(xmitcntp != NULL);
|
|
|
|
|
|
|
|
|
|
*xmitcntp = dev->lastxmitcount;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETLASTPIPENO:
|
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *lastpipep = (FAR uint32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(lastpipep != NULL);
|
|
|
|
|
|
|
|
|
|
*lastpipep = dev->last_recvpipeno;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 21:14:54 +02:00
|
|
|
|
case NRF24L01IOC_SETTXPAYLOADNOACK:
|
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *tx_payload_noack = (FAR uint32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(tx_payload_noack != NULL);
|
|
|
|
|
|
|
|
|
|
dev->tx_payload_noack = (*tx_payload_noack) > 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NRF24L01IOC_GETTXPAYLOADNOACK:
|
|
|
|
|
{
|
|
|
|
|
FAR uint32_t *tx_payload_noack = (FAR uint32_t *)(arg);
|
|
|
|
|
DEBUGASSERT(tx_payload_noack != NULL);
|
|
|
|
|
|
|
|
|
|
*tx_payload_noack = dev->tx_payload_noack ? 1 : 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
default:
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -ENOTTY;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_poll
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
static int nrf24l01_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
bool setup)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
#ifndef CONFIG_WL_NRF24L01_RXSUPPORT
|
|
|
|
|
/* Polling is currently implemented for data input only */
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
return -ENOSYS;
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
FAR struct inode *inode;
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("setup: %d\n", (int)setup);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
DEBUGASSERT(filep && fds);
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
dev = (FAR struct nrf24l01_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
|
|
/* Exclusive access */
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&dev->devlock);
|
2017-10-07 17:40:02 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
|
{
|
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) == 0)
|
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -EDEADLK;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if we can accept this poll.
|
2017-03-11 00:29:58 +01:00
|
|
|
|
* For now, only one thread can poll the device at any time
|
|
|
|
|
* (shorter / simpler code)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (dev->pfd)
|
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -EBUSY;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->pfd = fds;
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Is there is already data in the fifo? then trigger POLLIN now -
|
|
|
|
|
* don't wait for RX.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_lock(&dev->lock_fifo);
|
2017-03-11 00:29:58 +01:00
|
|
|
|
if (dev->fifo_len > 0)
|
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
|
poll_notify(&dev->pfd, 1, POLLIN);
|
2017-03-11 00:29:58 +01:00
|
|
|
|
}
|
2017-03-10 23:21:49 +01:00
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->lock_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-11 00:29:58 +01:00
|
|
|
|
else /* Tear it down */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
dev->pfd = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&dev->devlock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_unregister
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
static int nrf24l01_unregister(FAR struct nrf24l01_dev_s *dev)
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARGS(dev);
|
|
|
|
|
|
|
|
|
|
/* Release IRQ */
|
|
|
|
|
|
2017-02-27 19:11:35 +01:00
|
|
|
|
nrf24l01_attachirq(dev, NULL, NULL);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Free memory */
|
2017-02-27 18:41:48 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2022-10-16 18:50:59 +02:00
|
|
|
|
nxmutex_destroy(&dev->lock_fifo);
|
|
|
|
|
nxsem_destroy(&dev->sem_rx);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
kmm_free(dev->rx_fifo);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-16 18:50:59 +02:00
|
|
|
|
nxmutex_destroy(&dev->devlock);
|
|
|
|
|
nxsem_destroy(&dev->sem_tx);
|
|
|
|
|
kmm_free(dev);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_register
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_register(FAR struct spi_dev_s *spi,
|
|
|
|
|
FAR struct nrf24l01_config_s *cfg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
FAR struct nrf24l01_dev_s *dev;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret = OK;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2019-05-11 21:46:38 +02:00
|
|
|
|
FAR uint8_t *rx_fifo;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2018-08-24 14:58:30 +02:00
|
|
|
|
DEBUGASSERT((spi != NULL) & (cfg != NULL));
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-06 17:09:19 +02:00
|
|
|
|
if ((dev = kmm_zalloc(sizeof(struct nrf24l01_dev_s))) == NULL)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
dev->spi = spi;
|
|
|
|
|
dev->config = cfg;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
dev->state = ST_UNKNOWN;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
dev->ce_enabled = false;
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_init(&dev->devlock);
|
2017-10-03 20:51:15 +02:00
|
|
|
|
nxsem_init(&dev->sem_tx, 0, 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2014-09-01 01:26:36 +02:00
|
|
|
|
if ((rx_fifo = kmm_malloc(CONFIG_WL_NRF24L01_RXFIFO_LEN)) == NULL)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2022-10-16 18:50:59 +02:00
|
|
|
|
nxmutex_destroy(&dev->devlock);
|
|
|
|
|
nxsem_destroy(&dev->sem_tx);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
kmm_free(dev);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 18:50:59 +02:00
|
|
|
|
dev->rx_fifo = rx_fifo;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_init(&dev->lock_fifo);
|
2022-10-16 18:50:59 +02:00
|
|
|
|
nxsem_init(&dev->sem_rx, 0, 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Configure IRQ pin (falling edge) */
|
|
|
|
|
|
2017-02-27 18:41:48 +01:00
|
|
|
|
nrf24l01_attachirq(dev, nrf24l01_irqhandler, dev);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Register the device as an input device */
|
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Registering " DEV_NAME "\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
|
ret = register_driver(DEV_NAME, &g_nrf24l01_fops, 0666, dev);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
wlerr("ERROR: register_driver() failed: %d\n", ret);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
nrf24l01_unregister(dev);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_init
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* (Re)set the device in a default initial state
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
int nrf24l01_init(FAR struct nrf24l01_dev_s *dev)
|
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret = OK;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
uint8_t features;
|
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev);
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
2016-01-24 15:21:55 +01:00
|
|
|
|
/* Configure the SPI parameters before communicating */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_configspi(dev->spi);
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
/* Enable features in hardware: dynamic payload length + sending without
|
|
|
|
|
* expecting ACK
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_FEATURE, NRF24L01_EN_DPL |
|
|
|
|
|
NRF24L01_EN_DYN_ACK);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
features = nrf24l01_readregbyte(dev, NRF24L01_FEATURE);
|
|
|
|
|
if (0 == features)
|
|
|
|
|
{
|
|
|
|
|
/* The ACTIVATE instruction is not documented in the nRF24L01+ docs.
|
|
|
|
|
* However it is referenced / described by many sources on Internet,
|
|
|
|
|
*
|
|
|
|
|
* Is it for nRF24L01 (not +) hardware ?
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
uint8_t v = 0x73;
|
|
|
|
|
nrf24l01_access(dev, MODE_WRITE, NRF24L01_ACTIVATE, &v, 1);
|
|
|
|
|
|
|
|
|
|
features = nrf24l01_readregbyte(dev, NRF24L01_FEATURE);
|
|
|
|
|
if (0 == features)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* If FEATURES reg is still unset here, consider there is no
|
|
|
|
|
* actual hardware.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = -ENODEV;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set initial state */
|
|
|
|
|
|
|
|
|
|
nrf24l01_tostate(dev, ST_POWER_DOWN);
|
|
|
|
|
|
|
|
|
|
/* Disable all pipes */
|
|
|
|
|
|
|
|
|
|
dev->en_pipes = 0;
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_EN_RXADDR, 0);
|
|
|
|
|
|
|
|
|
|
/* Set addr width to default */
|
|
|
|
|
|
|
|
|
|
dev->addrlen = CONFIG_WL_NRF24L01_DFLT_ADDR_WIDTH;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_SETUP_AW,
|
|
|
|
|
CONFIG_WL_NRF24L01_DFLT_ADDR_WIDTH - 2);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Get pipe #0 addr */
|
|
|
|
|
|
|
|
|
|
nrf24l01_readreg(dev, NRF24L01_RX_ADDR_P0, dev->pipe0addr, dev->addrlen);
|
|
|
|
|
|
|
|
|
|
dev->en_aa = nrf24l01_readregbyte(dev, NRF24L01_EN_AA);
|
|
|
|
|
|
|
|
|
|
/* Flush HW fifo */
|
|
|
|
|
|
|
|
|
|
nrf24l01_flush_rx(dev);
|
|
|
|
|
nrf24l01_flush_tx(dev);
|
|
|
|
|
|
|
|
|
|
/* Clear interrupt sources (useful ?) */
|
|
|
|
|
|
2015-10-10 19:51:32 +02:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_STATUS,
|
|
|
|
|
NRF24L01_RX_DR | NRF24L01_TX_DS | NRF24L01_MAX_RT);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setpipeconfig
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_setpipeconfig(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
unsigned int pipeno,
|
|
|
|
|
FAR const nrf24l01_pipecfg_t *pipecfg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
bool dynlength;
|
|
|
|
|
bool en_aa;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
int addrlen;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev && pipecfg && pipeno < NRF24L01_PIPE_COUNT);
|
|
|
|
|
|
|
|
|
|
dynlength = (pipecfg->payload_length == NRF24L01_DYN_LENGTH);
|
|
|
|
|
|
|
|
|
|
/* Need to enable AA to enable dynamic length payload */
|
|
|
|
|
|
|
|
|
|
en_aa = dynlength || pipecfg->en_aa;
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Set addr
|
|
|
|
|
* Pipe 0 & 1 are the only ones to have a full length address.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
addrlen = (pipeno <= 1) ? dev->addrlen : 1;
|
|
|
|
|
nrf24l01_writereg(dev, NRF24L01_RX_ADDR_P0 + pipeno, pipecfg->rx_addr,
|
|
|
|
|
addrlen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Auto ack */
|
|
|
|
|
|
|
|
|
|
if (en_aa)
|
|
|
|
|
{
|
|
|
|
|
dev->en_aa |= 1 << pipeno;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dev->en_aa &= ~(1 << pipeno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_EN_AA, 1 << pipeno, en_aa);
|
|
|
|
|
|
|
|
|
|
/* Payload config */
|
|
|
|
|
|
|
|
|
|
nrf24l01_setregbit(dev, NRF24L01_DYNPD, 1 << pipeno, dynlength);
|
|
|
|
|
if (!dynlength)
|
|
|
|
|
{
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_RX_PW_P0 + pipeno,
|
|
|
|
|
pipecfg->payload_length);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-11 00:29:58 +01:00
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
|
|
|
|
dev->pipedatalen[pipeno] = pipecfg->payload_length;
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_getpipeconfig
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_getpipeconfig(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
unsigned int pipeno,
|
|
|
|
|
FAR nrf24l01_pipecfg_t *pipecfg)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
bool dynlength;
|
2017-03-11 00:29:58 +01:00
|
|
|
|
int addrlen;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev && pipecfg && pipeno < NRF24L01_PIPE_COUNT);
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* Get pipe address.
|
|
|
|
|
* Pipe 0 & 1 are the only ones to have a full length address.
|
|
|
|
|
*/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
addrlen = (pipeno <= 1) ? dev->addrlen : 1;
|
|
|
|
|
nrf24l01_readreg(dev, NRF24L01_RX_ADDR_P0 + pipeno, pipecfg->rx_addr,
|
|
|
|
|
addrlen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Auto ack */
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
pipecfg->en_aa =
|
|
|
|
|
((nrf24l01_readregbyte(dev, NRF24L01_EN_AA) & (1 << pipeno)) != 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
/* Payload config */
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
dynlength =
|
|
|
|
|
((nrf24l01_readregbyte(dev, NRF24L01_DYNPD) & (1 << pipeno)) != 0);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
if (dynlength)
|
|
|
|
|
{
|
|
|
|
|
pipecfg->payload_length = NRF24L01_DYN_LENGTH;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-11 21:46:38 +02:00
|
|
|
|
pipecfg->payload_length =
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P0 + pipeno);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_enablepipe
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_enablepipe(FAR struct nrf24l01_dev_s *dev, unsigned int pipeno,
|
|
|
|
|
bool enable)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
CHECK_ARGS(dev && pipeno < NRF24L01_PIPE_COUNT);
|
|
|
|
|
|
|
|
|
|
uint8_t rxaddrval;
|
|
|
|
|
uint8_t pipemask = 1 << pipeno;
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
/* Enable pipe on nRF24L01 */
|
|
|
|
|
|
|
|
|
|
rxaddrval = nrf24l01_readregbyte(dev, NRF24L01_EN_RXADDR);
|
|
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
rxaddrval |= pipemask;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rxaddrval &= ~pipemask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_EN_RXADDR, rxaddrval);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
|
|
|
|
/* Update cached value */
|
|
|
|
|
|
|
|
|
|
dev->en_pipes = rxaddrval;
|
|
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_settxaddr
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_settxaddr(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
FAR const uint8_t *txaddr)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
CHECK_ARGS(dev && txaddr);
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
nrf24l01_writereg(dev, NRF24L01_TX_ADDR, txaddr, dev->addrlen);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_gettxaddr
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_gettxaddr(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *txaddr)
|
|
|
|
|
{
|
|
|
|
|
CHECK_ARGS(dev && txaddr);
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
nrf24l01_readreg(dev, NRF24L01_TX_ADDR, txaddr, dev->addrlen);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setretransmit
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_setretransmit(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
nrf24l01_retransmit_delay_t retrdelay,
|
|
|
|
|
uint8_t retrcount)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev && retrcount <= NRF24L01_MAX_XMIT_RETR);
|
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
|
val = (retrdelay << NRF24L01_ARD_SHIFT) |
|
|
|
|
|
(retrcount << NRF24L01_ARC_SHIFT);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_SETUP_RETR, val);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_settxpower
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_settxpower(FAR struct nrf24l01_dev_s *dev, int outpower)
|
|
|
|
|
{
|
|
|
|
|
uint8_t value;
|
|
|
|
|
uint8_t hwpow;
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/* RF_PWR value <-> Output power in dBm
|
2013-06-01 16:03:55 +02:00
|
|
|
|
*
|
|
|
|
|
* '00' – -18dBm
|
|
|
|
|
* '01' – -12dBm
|
|
|
|
|
* '10' – -6dBm
|
|
|
|
|
* '11' – 0dBm
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-10 19:51:32 +02:00
|
|
|
|
switch (outpower)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
hwpow = 3 << NRF24L01_RF_PWR_SHIFT;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -6:
|
|
|
|
|
hwpow = 2 << NRF24L01_RF_PWR_SHIFT;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -12:
|
|
|
|
|
hwpow = 1 << NRF24L01_RF_PWR_SHIFT;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -18:
|
|
|
|
|
hwpow = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
value = nrf24l01_readregbyte(dev, NRF24L01_RF_SETUP);
|
|
|
|
|
|
|
|
|
|
value &= ~(NRF24L01_RF_PWR_MASK);
|
|
|
|
|
value |= hwpow;
|
|
|
|
|
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_RF_SETUP, value);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_gettxpower
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_gettxpower(FAR struct nrf24l01_dev_s *dev)
|
|
|
|
|
{
|
|
|
|
|
uint8_t value;
|
2019-12-05 18:49:12 +01:00
|
|
|
|
int powers[] =
|
|
|
|
|
{
|
|
|
|
|
-18, -12, -6, 0
|
|
|
|
|
};
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
value = nrf24l01_readregbyte(dev, NRF24L01_RF_SETUP);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
|
|
|
|
value = (value & NRF24L01_RF_PWR_MASK) >> NRF24L01_RF_PWR_SHIFT;
|
|
|
|
|
return powers[value];
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setdatarate
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_setdatarate(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
nrf24l01_datarate_t datarate)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t value;
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
value = nrf24l01_readregbyte(dev, NRF24L01_RF_SETUP);
|
|
|
|
|
value &= ~(NRF24L01_RF_DR_HIGH | NRF24L01_RF_DR_LOW);
|
|
|
|
|
|
|
|
|
|
switch (datarate)
|
|
|
|
|
{
|
|
|
|
|
case RATE_1Mbps:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RATE_2Mbps:
|
|
|
|
|
value |= NRF24L01_RF_DR_HIGH;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RATE_250kbps:
|
|
|
|
|
value |= NRF24L01_RF_DR_LOW;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_RF_SETUP, value);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setradiofreq
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_setradiofreq(FAR struct nrf24l01_dev_s *dev, uint32_t freq)
|
|
|
|
|
{
|
|
|
|
|
uint8_t value;
|
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev && freq >= NRF24L01_MIN_FREQ && freq <= NRF24L01_MAX_FREQ);
|
|
|
|
|
|
2016-04-15 15:07:22 +02:00
|
|
|
|
value = freq - NRF24L01_MIN_FREQ;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_RF_CH, value);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_getradiofreq
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
uint32_t nrf24l01_getradiofreq(FAR struct nrf24l01_dev_s *dev)
|
|
|
|
|
{
|
|
|
|
|
int rffreq;
|
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev);
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
rffreq = (int)nrf24l01_readregbyte(dev, NRF24L01_RF_CH);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
|
|
|
|
|
return rffreq + NRF24L01_MIN_FREQ;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_setaddrwidth
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_setaddrwidth(FAR struct nrf24l01_dev_s *dev, uint32_t width)
|
|
|
|
|
{
|
2017-03-11 15:57:34 +01:00
|
|
|
|
CHECK_ARGS(dev && width <= NRF24L01_MAX_ADDR_LEN &&
|
|
|
|
|
width >= NRF24L01_MIN_ADDR_LEN);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
2019-05-11 21:46:38 +02:00
|
|
|
|
nrf24l01_writeregbyte(dev, NRF24L01_SETUP_AW, width - 2);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
dev->addrlen = width;
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_changestate
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2019-05-11 21:46:38 +02:00
|
|
|
|
int nrf24l01_changestate(FAR struct nrf24l01_dev_s *dev,
|
|
|
|
|
nrf24l01_state_t state)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
nrf24l01_tostate(dev, state);
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_send
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int nrf24l01_send(FAR struct nrf24l01_dev_s *dev, FAR const uint8_t *data,
|
|
|
|
|
size_t datalen)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
CHECK_ARGS(dev && data && datalen <= NRF24L01_MAX_PAYLOAD_LEN);
|
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = dosend(dev, data, datalen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_sendto
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_sendto(FAR struct nrf24l01_dev_s *dev, FAR const uint8_t *data,
|
2017-03-11 00:29:58 +01:00
|
|
|
|
size_t datalen, FAR const uint8_t *destaddr)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
bool pipeaddrchg = false;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_lock(dev->spi);
|
|
|
|
|
|
|
|
|
|
/* If AA is enabled (pipe 0 is active and its AA flag is set) and the dest
|
|
|
|
|
* addr is not the current pipe 0 addr we need to change pipe 0 addr in
|
|
|
|
|
* order to receive the ACK packet.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if ((dev->en_aa & 1) && (memcmp(destaddr, dev->pipe0addr, dev->addrlen)))
|
|
|
|
|
{
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Change pipe #0 addr to dest addr\n");
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_writereg(dev, NRF24L01_RX_ADDR_P0, destaddr,
|
|
|
|
|
NRF24L01_MAX_ADDR_LEN);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
pipeaddrchg = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
|
ret = dosend(dev, data, datalen);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
if (pipeaddrchg)
|
|
|
|
|
{
|
|
|
|
|
/* Restore pipe #0 addr */
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
nrf24l01_writereg(dev, NRF24L01_RX_ADDR_P0, dev->pipe0addr,
|
|
|
|
|
NRF24L01_MAX_ADDR_LEN);
|
2017-03-15 21:30:24 +01:00
|
|
|
|
wlinfo("Pipe #0 default addr restored\n");
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nrf24l01_unlock(dev->spi);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_lastxmitcount
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2013-06-01 16:03:55 +02:00
|
|
|
|
int nrf24l01_lastxmitcount(FAR struct nrf24l01_dev_s *dev)
|
|
|
|
|
{
|
|
|
|
|
return dev->lastxmitcount;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_recv
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
#ifdef CONFIG_WL_NRF24L01_RXSUPPORT
|
2019-05-11 21:46:38 +02:00
|
|
|
|
ssize_t nrf24l01_recv(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *buffer,
|
|
|
|
|
size_t buflen, FAR uint8_t *recvpipe)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
int ret = nxsem_wait(&dev->sem_rx);
|
|
|
|
|
if (ret < 0)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
|
return ret;
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fifoget(dev, buffer, buflen, recvpipe);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_dumpregs
|
|
|
|
|
****************************************************************************/
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#ifdef CONFIG_DEBUG_WIRELESS
|
2019-05-11 21:46:38 +02:00
|
|
|
|
void nrf24l01_dumpregs(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
|
|
|
|
uint8_t addr[NRF24L01_MAX_ADDR_LEN];
|
|
|
|
|
char addrstr[NRF24L01_MAX_ADDR_LEN * 2 +1];
|
|
|
|
|
|
2014-10-08 18:18:58 +02:00
|
|
|
|
syslog(LOG_INFO, "CONFIG: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_CONFIG));
|
|
|
|
|
syslog(LOG_INFO, "EN_AA: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_EN_AA));
|
|
|
|
|
syslog(LOG_INFO, "EN_RXADDR: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_EN_RXADDR));
|
|
|
|
|
syslog(LOG_INFO, "SETUP_AW: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_SETUP_AW));
|
|
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "SETUP_RETR:%02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_SETUP_RETR));
|
|
|
|
|
syslog(LOG_INFO, "RF_CH: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RF_CH));
|
|
|
|
|
syslog(LOG_INFO, "RF_SETUP: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RF_SETUP));
|
|
|
|
|
syslog(LOG_INFO, "STATUS: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_STATUS));
|
|
|
|
|
syslog(LOG_INFO, "OBS_TX: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_OBSERVE_TX));
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
|
|
|
|
nrf24l01_readreg(dev, NRF24L01_TX_ADDR, addr, dev->addrlen);
|
2023-03-05 15:34:33 +01:00
|
|
|
|
binarycvt(addrstr, sizeof(addrstr), addr, dev->addrlen);
|
2014-10-08 18:18:58 +02:00
|
|
|
|
syslog(LOG_INFO, "TX_ADDR: %s\n", addrstr);
|
|
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "CD: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_CD));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P0: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P0));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P1: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P1));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P2: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P2));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P3: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P3));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P4: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P4));
|
|
|
|
|
syslog(LOG_INFO, "RX_PW_P5: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_RX_PW_P5));
|
|
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "FIFO_STAT: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_FIFO_STATUS));
|
|
|
|
|
syslog(LOG_INFO, "DYNPD: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_DYNPD));
|
|
|
|
|
syslog(LOG_INFO, "FEATURE: %02x\n",
|
|
|
|
|
nrf24l01_readregbyte(dev, NRF24L01_FEATURE));
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#endif /* CONFIG_DEBUG_WIRELESS */
|
2013-06-01 16:03:55 +02:00
|
|
|
|
|
2017-03-11 00:29:58 +01:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: nrf24l01_dumprxfifo
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#if defined(CONFIG_DEBUG_WIRELESS) && defined(CONFIG_WL_NRF24L01_RXSUPPORT)
|
2019-05-11 21:46:38 +02:00
|
|
|
|
void nrf24l01_dumprxfifo(FAR struct nrf24l01_dev_s *dev)
|
2013-06-01 16:03:55 +02:00
|
|
|
|
{
|
2014-10-08 18:18:58 +02:00
|
|
|
|
syslog(LOG_INFO, "bytes count: %d\n", dev->fifo_len);
|
|
|
|
|
syslog(LOG_INFO, "next read: %d, next write: %d\n",
|
2022-04-12 12:16:13 +02:00
|
|
|
|
dev->nxt_read, dev->nxt_write);
|
2013-06-01 16:03:55 +02:00
|
|
|
|
}
|
2017-03-15 21:30:24 +01:00
|
|
|
|
#endif /* CONFIG_DEBUG_WIRELESS && CONFIG_WL_NRF24L01_RXSUPPORT */
|