keever50 commented on code in PR #15795:
URL: https://github.com/apache/nuttx/pull/15795#discussion_r1951536782


##########
drivers/wireless/lpwan/sx126x/sx126x.c:
##########
@@ -0,0 +1,1008 @@
+/****************************************************************************
+ * drivers/wireless/lpwan/sx126x/sx126x.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "sx126x.h"
+
+#include <nuttx/arch.h>
+#include <nuttx/config.h>
+
+#include <debug.h>
+#include <errno.h>
+#include <nuttx/mutex.h>
+#include <nuttx/spi/spi.h>
+#include <sched.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/endian.h>
+#include <syslog.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Private prototypes
+ ****************************************************************************/
+
+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
+ ****************************************************************************/
+
+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
+{
+  SX126X_STATUS_RESERVED, SX126X_STATUS_RFU, SX126X_STATUS_DATA_AVAILABLE,
+  SX126X_STATUS_TIMEOUT, SX126X_STATUS_ERROR, SX126X_STATUS_EXECUTE_FAIL,
+  SX126X_STATUS_TX_DONE
+};
+
+enum sx126x_chip_mode
+{
+  SX126X_MODE_UNUSED, SX126X_MODE_RFU, SX126X_MODE_STBY_RC,
+  SX126X_MODE_STBY_XOSC, SX126X_MODE_FS, SX126X_MODE_RX, SX126X_MODE_TX
+};
+
+struct sx126x_status_s
+{
+  enum sx126x_cmd_status cmd;
+  enum sx126x_chip_mode mode;
+};
+
+static const struct file_operations sx126x_ops =
+{
+  sx126x_open,
+  sx126x_close,
+  sx126x_read,
+  sx126x_write,
+  NULL,
+  sx126x_ioctl,
+  NULL,
+  NULL
+};
+
+/****************************************************************************
+ * Globals
+ ****************************************************************************/
+
+FAR struct sx126x_dev_s g_sx126x_devices[SX126X_MAX_DEVICES];
+
+/****************************************************************************
+ * Private prototypes
+ ****************************************************************************/
+
+/* SPI and control **********************************************************/
+
+static void sx126x_command(FAR struct sx126x_dev_s *dev,
+                           uint8_t cmd,
+                           FAR const uint8_t *params,
+                           size_t paramslen,
+                           FAR uint8_t *returns);
+
+static void sx126x_reset(FAR struct sx126x_dev_s *dev);
+
+static void sx126x_get_status(FAR struct sx126x_dev_s *dev,
+                              FAR struct sx126x_status_s *status);
+
+static void sx126x_test(FAR struct sx126x_dev_s *dev);
+
+static void sx126x_spi_lock(FAR struct sx126x_dev_s *dev);
+
+static void sx126x_spi_unlock(FAR struct sx126x_dev_s *dev);
+
+static void sx126x_write_register(FAR struct sx126x_dev_s *dev,
+                                  uint16_t address,
+                                  uint8_t *data,
+                                  size_t data_length);
+
+/* Operational modes functions **********************************************/
+
+static void sx126x_set_standby(FAR struct sx126x_dev_s *dev,
+                               enum sx126x_standby_mode_e mode);
+
+static void sx126x_set_tx(FAR struct sx126x_dev_s *dev,
+                          uint32_t timeout);
+
+static void sx126x_set_cad(struct sx126x_dev_s *dev);
+
+static void sx126x_set_tx_continuous_wave(FAR struct sx126x_dev_s *dev);
+
+static void sx126x_set_regulator_mode(FAR struct sx126x_dev_s *dev,
+                                      enum sx126x_regulator_mode_e mode);
+
+static void sx126x_set_pa_config(FAR struct sx126x_dev_s *dev,
+                                 enum sx126x_device_e model,
+                                 uint8_t hpmax,
+                                 uint8_t padutycycle);
+
+static void sx126x_set_tx_infinite_preamble(FAR struct sx126x_dev_s *dev);
+
+/* DIO and IRQ control functions ********************************************/
+
+static void sx126x_set_dio_irq_params(FAR struct sx126x_dev_s *dev,
+                                      uint16_t irq_mask,
+                                      uint16_t dio1_mask,
+                                      uint16_t dio2_mask,
+                                      uint16_t dio3_mask);
+
+static void sx126x_set_dio2_as_rf_switch(FAR struct sx126x_dev_s *dev,
+                                         bool enable);
+
+static void sx126x_set_dio3_as_tcxo(FAR struct sx126x_dev_s *dev,
+                                    enum sx126x_tcxo_voltage_e voltage,
+                                    uint32_t delay);
+
+/* RF Modulation and Packet-Related Functions *******************************/
+
+static void sx126x_set_packet_params_lora(FAR struct sx126x_dev_s *dev,
+                                          FAR struct
+                                          sx126x_packetparams_lora_s *
+                                          pktparams);
+
+static void sx126x_set_modulation_params_lora(FAR struct sx126x_dev_s *dev,
+                                              FAR struct
+                                              sx126x_modparams_lora_s *
+                                              modparams);
+
+static void sx126x_set_buffer_base_address(FAR struct sx126x_dev_s *dev,
+                                           uint8_t tx,
+                                           uint8_t rx);
+
+static void sx126x_set_tx_params(FAR struct sx126x_dev_s *dev, uint8_t power,
+                                 enum sx126x_ramp_time_e ramp_time);
+
+static void sx126x_set_packet_type(FAR struct sx126x_dev_s *dev,
+                                   enum sx126x_packet_type_e type);
+
+static void sx126x_get_rf_frequency(FAR struct sx126x_dev_s *dev,
+                                    uint32_t frequency_hz);
+
+/* Communication status information *****************************************/
+
+static void sx126x_get_rssi_inst(FAR struct sx126x_dev_s *dev,
+                                 FAR float *dbm);
+
+/* Registers and buffer *****************************************************/
+
+static void sx126x_write_register(FAR struct sx126x_dev_s *dev,
+                                  uint16_t address,
+                                  uint8_t *data,
+                                  size_t data_length);
+
+static void sx126x_write_buffer(FAR struct sx126x_dev_s *dev,
+                                uint8_t offset,
+                                FAR const uint8_t *payload,
+                                uint8_t len);
+
+static void sx126x_read_buffer(FAR struct sx126x_dev_s *dev,
+                               uint8_t offset,
+                               FAR uint8_t *payload,
+                               uint8_t len);
+
+/* Register settings ********************************************************/
+
+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(FAR struct file *filep)
+{
+  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);
+
+  /* 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_reset(dev);
+  usleep(1000);
+  ret = sx126x_init(dev);
+  if (ret != 0)
+    {
+      goto exit_err;
+    }
+
+  /* Success */
+
+  dev->times_opened++;
+  ret = OK;
+
+exit_err:
+  nxmutex_unlock(&dev->lock);
+  return ret;
+}
+
+static int sx126x_close(FAR struct file *filep)
+{
+  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);
+
+  /* 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(FAR struct file *filep,
+                           FAR char *buf,
+                          size_t buflen)
+{
+  if (buf == NULL || buflen < 1)
+    {
+      return -EINVAL;
+    }
+
+  printf("Reading\n");

Review Comment:
   Yeah good idea, also just placeholder for now. I am planning on replacing 
all of these to wldebug, wlerr, wlinfo, etc. We have one of these already, so 
might as well. I just partially integrated everything into Nuttx, such as 
public includes, Kconfig and make files. I am testing everything on a board as 
described before. Is it a good idea to just add this one into this PR later? 
That makes things a bit easier.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to