Skip to content

Commit

Permalink
initial file ops and initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
keever50 committed Feb 11, 2025
1 parent 8583eeb commit 0e88488
Showing 1 changed file with 150 additions and 27 deletions.
177 changes: 150 additions & 27 deletions drivers/wireless/lpwan/sx126x/sx126x.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <debug.h>
#include <errno.h>
#include <nuttx/mutex.h>
#include <nuttx/spi/spi.h>
#include <sched.h>
#include <stdint.h>
Expand All @@ -43,12 +44,17 @@
* Private prototypes
****************************************************************************/

typedef FAR struct file file_t;
static int sx126x_open(file_t *filep);
static int sx126x_close(file_t *filep);
static ssize_t sx126x_read(file_t *filep, FAR char *buffer, size_t buflen);
static ssize_t sx126x_write(file_t *filep, FAR const char *buf,
static int sx126x_open(FAR struct file *filep);
static int sx126x_close(FAR struct file *filep);
static ssize_t sx126x_read(FAR struct file *filep,
FAR char *buffer,
size_t buflen);
static ssize_t sx126x_write(FAR struct file *filep,
FAR const char *buf,
size_t buflen);
static int sx126x_ioctl(FAR struct file *filep,
int cmd,
unsigned long arg);

/****************************************************************************
* Private data types
Expand All @@ -58,6 +64,8 @@ struct sx126x_dev_s
{
struct spi_dev_s *spi;
const struct sx126x_lower_s *lower;
uint8_t times_opened;
mutex_t lock; /* Only let one user in at a time */
};

enum sx126x_cmd_status
Expand All @@ -79,21 +87,24 @@ struct sx126x_status_s
enum sx126x_chip_mode mode;
};

/****************************************************************************
* Globals
****************************************************************************/

FAR struct sx126x_dev_s g_sx126x_devices[SX126X_MAX_DEVICES];
static const struct file_operations sx126x_ops =
{
sx126x_open,
sx126x_close,
sx126x_read,
sx126x_write,
0,
0
NULL,
sx126x_ioctl,
NULL,
NULL
};

/****************************************************************************
* Globals
****************************************************************************/

FAR struct sx126x_dev_s g_sx126x_devices[SX126X_MAX_DEVICES];

/****************************************************************************
* Private prototypes
****************************************************************************/
Expand Down Expand Up @@ -212,40 +223,104 @@ static void sx126x_set_syncword(FAR struct sx126x_dev_s *dev,
uint8_t *syncword,
uint8_t syncword_length);

/* Driver specific **********************************************************/

static int sx126x_init(FAR struct sx126x_dev_s *dev);

static int sx126x_deinit(FAR struct sx126x_dev_s *dev);

/****************************************************************************
* Private Functions
****************************************************************************/

/* File operations **********************************************************/

static int sx126x_open(file_t *filep)
static int sx126x_open(FAR struct file *filep)
{
struct sx126x_dev_s *dev;
int ret = 0;

/* Get device */

struct sx126x_dev_s *dev;
dev = filep->f_inode->i_private;
syslog(LOG_INFO, "Opening SX126x %d", dev->lower->port);

syslog(LOG_INFO, "Opening SX126x port %d\n", dev->lower->port);
/* Lock dev */

ret = nxmutex_lock(&dev->lock);
if (ret < 0)
{
return ret;
}

/* Only one can open this dev at a time */

if (dev->times_opened > 0)
{
ret = -EBUSY;
goto exit_err;
}

/* Initialize */

sx126x_spi_lock(dev);
sx126x_reset(dev);
usleep(100000);
sx126x_test(dev);
usleep(1000);
ret = sx126x_init(dev);
if (ret != 0)
{
goto exit_err;
}

/* Success */

return OK;
dev->times_opened++;
ret = OK;

exit_err:
nxmutex_unlock(&dev->lock);
return ret;
}

static int sx126x_close(file_t *filep)
static int sx126x_close(FAR struct file *filep)
{
struct sx126x_dev_s *dev;
int ret = 0;

/* Get device */

struct sx126x_dev_s *dev;
dev = filep->f_inode->i_private;
syslog(LOG_INFO, "Closing SX126x %d", dev->lower->port);

syslog(LOG_INFO, "Closing SX126x\n");
sx126x_spi_unlock(dev);
return OK;
/* Lock */

ret = nxmutex_lock(&dev->lock);
if (ret < 0)
{
goto exit_err;
}

/* De-init */

ret = sx126x_deinit(dev);
if (ret != 0)
{
goto exit_err;
}

/* Success */

if (dev->times_opened > 0)
{
dev->times_opened--; /* Do not let this wrap around. */
ret = OK;
}

exit_err:
nxmutex_unlock(&dev->lock);
return ret;
}

static ssize_t sx126x_read(file_t *filep,
static ssize_t sx126x_read(FAR struct file *filep,
FAR char *buf,
size_t buflen)
{
Expand All @@ -259,7 +334,7 @@ static ssize_t sx126x_read(file_t *filep,
return 1;
}

static ssize_t sx126x_write(file_t *filep,
static ssize_t sx126x_write(FAR struct file *filep,
FAR const char *buf,
size_t buflen)
{
Expand All @@ -273,6 +348,38 @@ static ssize_t sx126x_write(file_t *filep,
return 1;
}

static int sx126x_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
int ret = 0;

/* Get device */

struct sx126x_dev_s *dev;
dev = filep->f_inode->i_private;
syslog(LOG_INFO, "IOCTL cmd %d arg %ld SX126x %d",
cmd,
arg,
dev->lower->port);

/* Lock */

ret = nxmutex_lock(&dev->lock);
if (ret < 0)
{
goto exit_err;
}

/* Do thing */

/* Success */

ret = OK;

exit_err:
nxmutex_unlock(&dev->lock);
return ret;
}

/* Test *********************************************************************/

uint8_t sx126x_temp[0xff];
Expand Down Expand Up @@ -861,13 +968,29 @@ static void sx126x_set_syncword(FAR struct sx126x_dev_s *dev,
sx126x_write_register(dev, SX126X_REG_SYNCWORD, syncword, syncword_length);
}

/* Driver specific **********************************************************/

static int sx126x_init(FAR struct sx126x_dev_s *dev)
{
return 0;
}

static int sx126x_deinit(FAR struct sx126x_dev_s *dev)
{
return 0;
}

/****************************************************************************
* Public Functions
****************************************************************************/

void sx126x_register(FAR struct spi_dev_s *spi,
FAR const struct sx126x_lower_s *lower)
{
/* Register the dev using an unique port id,
* so multiple radios can be registered at once
*/

if (lower->port >= SX126X_MAX_DEVICES)
{
syslog(LOG_ERR,
Expand All @@ -880,6 +1003,6 @@ void sx126x_register(FAR struct spi_dev_s *spi,
g_sx126x_devices[lower->port].lower = lower;
g_sx126x_devices[lower->port].spi = spi;

(void)register_driver("/dev/sx126x", &sx126x_ops, 0444,
(void)register_driver("/dev/sx126x", &sx126x_ops, 0666,
&g_sx126x_devices[lower->port]);
}

0 comments on commit 0e88488

Please sign in to comment.