SunJun8 commented on a change in pull request #4335:
URL: https://github.com/apache/incubator-nuttx/pull/4335#discussion_r690172513



##########
File path: arch/risc-v/src/bl602/bl602_os_hal.c
##########
@@ -0,0 +1,1035 @@
+/****************************************************************************
+ * arch/risc-v/src/bl602/bl602_os_hal.c
+ *
+ * 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 <debug.h>
+#include <clock/clock.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/pthread.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/signal.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/net/netdev.h>
+#include <nuttx/net/net.h>
+#include <syslog.h>
+#include <timer/timer.h>
+
+#include <bl602_netdev.h>
+#include <bl602_os_hal.h>
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_BL602_WIRELESS
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct mq_adpt
+{
+  struct file mq;           /* Message queue handle */
+  uint32_t    msgsize;      /* Message size */
+  char        name[16];     /* Message queue name */
+};
+
+struct irq_adpt
+{
+  void (*func)(void *arg);  /* Interrupt callback function */
+  void *arg;                /* Interrupt private data */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct work_s g_wifi_work;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+bl_ops_funcs_t g_bl_ops_funcs =
+{
+  ._version = BL_OS_ADAPTER_VERSION,
+  ._printf = bl_os_printf,
+  ._init = bl_os_api_init,
+  ._enter_critical = bl_os_enter_critical,
+  ._exit_critical = bl_os_exit_critical,
+  ._msleep = bl_os_msleep,
+  ._sleep = bl_os_sleep,
+  ._event_notify = bl_os_event_notify,
+  ._lock_giant = bl_os_lock_giant,
+  ._unlock_gaint = bl_os_unlock_giant,
+  ._irq_attach = bl_os_irq_attach,
+  ._workqueue_submit = bl_os_workqueue_submit_upwork,
+  ._timer_create = bl_os_timer_create,
+  ._timer_delete = bl_os_timer_delete,
+  ._timer_start_once = bl_os_timer_start_once,
+  ._timer_start_periodic = bl_os_timer_start_periodic,
+  ._sem_create = bl_os_sem_create,
+  ._sem_delete = bl_os_sem_delete,
+  ._sem_take = bl_os_sem_take,
+  ._sem_give = bl_os_sem_give,
+  ._mutex_create = bl_os_mutex_create,
+  ._mutex_delete = bl_os_mutex_delete,
+  ._mutex_lock = bl_os_mutex_lock,
+  ._mutex_unlock = bl_os_mutex_unlock,
+  . _queue_create = bl_os_mq_creat,
+  . _queue_delete = bl_os_mq_delete,
+  . _queue_send = bl_os_mq_send,
+  . _queue_recv = bl_os_mq_recv,
+  . _malloc = bl_os_malloc,
+  . _free = bl_os_free,
+  ._zalloc = bl_os_zalloc,
+  ._get_time_ms = bl_os_clock_gettime_ms
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bl_os_event_notify
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_event_notify(int evt, int val)
+{
+  bl602_net_event(evt, val);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: bl_os_api_init
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_api_init(void)
+{
+  return 0;
+}
+
+/****************************************************************************
+ * Name: bl_os_lock_giant
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void bl_os_lock_giant(void)
+{
+}
+
+/****************************************************************************
+ * Name: bl_os_unlock_giant
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void bl_os_unlock_giant(void)
+{
+}
+
+/****************************************************************************
+ * Name: bl_os_enter_critical
+ *
+ * Description:
+ *   Enter critical state
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   CPU PS value
+ *
+ ****************************************************************************/
+
+uint32_t bl_os_enter_critical(void)
+{
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+
+  return flags;
+}
+
+/****************************************************************************
+ * Name: bl_os_exit_critical
+ *
+ * Description:
+ *   Exit from critical state
+ *
+ * Input Parameters:
+ *   level - CPU PS value
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void bl_os_exit_critical(uint32_t level)
+{
+  leave_critical_section(level);
+}
+
+/****************************************************************************
+ * Name: bl_os_msleep
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_msleep(long msec)
+{
+  useconds_t usec = msec * 1000;
+
+  return nxsig_usleep(usec);
+}
+
+/****************************************************************************
+ * Name: bl_os_sleep
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_sleep(unsigned int seconds)
+{
+  return nxsig_sleep(seconds);
+}
+
+/****************************************************************************
+ * Name: bl_os_printf
+ *
+ * Description:
+ *   Output format string and its arguments
+ *
+ * Input Parameters:
+ *   format - format string
+ *
+ * Returned Value:
+ *   0
+ *
+ ****************************************************************************/
+
+void bl_os_printf(const char *__fmt, ...)
+{
+  if (&_wifi_log_flag)
+    {
+      va_list arg;
+
+      va_start(arg, __fmt);
+      syslog(LOG_INFO, __fmt, arg);
+      va_end(arg);
+    }
+}
+
+/****************************************************************************
+ * Name: bl_os_malloc
+ *
+ * Description:
+ *   Allocate a block of memory
+ *
+ * Input Parameters:
+ *   size - memory size
+ *
+ * Returned Value:
+ *   Memory pointer
+ *
+ ****************************************************************************/
+
+void *bl_os_malloc(unsigned int size)
+{
+  return kmm_malloc(size);
+}
+
+/****************************************************************************
+ * Name: bl_os_free
+ *
+ * Description:
+ *   Free a block of memory
+ *
+ * Input Parameters:
+ *   ptr - memory block
+ *
+ * Returned Value:
+ *   No
+ *
+ ****************************************************************************/
+
+void bl_os_free(void *ptr)
+{
+  kmm_free(ptr);
+}
+
+/****************************************************************************
+ * Name: bl_os_zalloc
+ *
+ * Description:
+ *   Allocate a block of memory
+ *
+ * Input Parameters:
+ *   size - memory size
+ *
+ * Returned Value:
+ *   Memory pointer
+ *
+ ****************************************************************************/
+
+void *bl_os_zalloc(unsigned int size)
+{
+  return kmm_zalloc(size);
+}
+
+/****************************************************************************
+ * Name: bl_os_update_time
+ *
+ * Description:
+ *   Transform ticks to time and add this time to timespec value
+ *
+ * Input Parameters:
+ *   timespec - Input timespec data pointer
+ *   ticks    - System ticks
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void bl_os_update_time(struct timespec *timespec, uint32_t ticks)
+{
+  uint32_t tmp;
+
+  tmp = TICK2SEC(ticks);
+  timespec->tv_sec += tmp;
+
+  ticks -= SEC2TICK(tmp);
+  tmp = TICK2NSEC(ticks);
+
+  timespec->tv_nsec += tmp;
+}
+
+/****************************************************************************
+ * Name: bl_os_errno_trans
+ *
+ * Description:
+ *   Transform from nuttx Os error code to Wi-Fi adapter error code
+ *
+ * Input Parameters:
+ *   ret - NuttX error code
+ *
+ * Returned Value:
+ *   Wi-Fi adapter error code
+ *
+ ****************************************************************************/
+
+static inline int32_t bl_os_errno_trans(int ret)
+{
+  if (!ret)
+    {
+      return true;
+    }
+  else
+    {
+      return false;
+    }
+}
+
+/****************************************************************************
+ * Name: bl_os_mq_creat
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void *bl_os_mq_creat(uint32_t queue_len, uint32_t item_size)
+{
+  struct mq_attr attr;
+  struct mq_adpt *mq_adpt;
+  int ret;
+
+  mq_adpt = (struct mq_adpt *)kmm_malloc(sizeof(struct mq_adpt));
+
+  if (!mq_adpt)
+    {
+      wlerr("ERROR: Failed to kmm_malloc\n");
+      return NULL;
+    }
+
+  snprintf(mq_adpt->name, sizeof(mq_adpt->name),
+           "/tmp/%p", mq_adpt);
+
+  attr.mq_maxmsg  = queue_len;
+  attr.mq_msgsize = item_size;
+  attr.mq_curmsgs = 0;
+  attr.mq_flags   = 0;
+
+  ret = file_mq_open(&mq_adpt->mq, mq_adpt->name,
+                     O_RDWR | O_CREAT, 0644, &attr);
+
+  if (ret < 0)
+    {
+      wlerr("ERROR: Failed to create mqueue\n");
+      kmm_free(mq_adpt);
+      return NULL;
+    }
+
+  mq_adpt->msgsize = item_size;
+
+  return (void *)mq_adpt;
+}
+
+/****************************************************************************
+ * Name: bl_os_mq_delete
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void bl_os_mq_delete(void *mq)
+{
+  struct mq_adpt *mq_adpt = (struct mq_adpt *)mq;
+
+  file_mq_close(&mq_adpt->mq);
+  file_mq_unlink(mq_adpt->name);
+  kmm_free(mq_adpt);
+}
+
+/****************************************************************************
+ * Name: bl_os_mq_send_generic
+ *
+ * Description:
+ *   Generic send message to queue within a certain period of time
+ *
+ * Input Parameters:
+ *   queue - Message queue data pointer
+ *   item  - Message data pointer
+ *   ticks - Wait ticks
+ *   prio  - Message priority
+ *
+ * Returned Value:
+ *   True if success or false if fail
+ *
+ ****************************************************************************/
+
+static int32_t bl_os_mq_send_generic(void *queue, void *item, size_t len,
+                                     uint32_t ticks, int prio)
+{
+  int ret;
+  struct timespec timeout;
+  struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+  if (ticks == 0)
+    {
+      /* Wi-Fi interrupt function will call this adapter function to send
+       * message to message queue, so here we should call kernel API
+       * instead of application API
+       */
+
+      ret = file_mq_send(&mq_adpt->mq, (const char *)item,
+                         len, prio);
+      if (ret < 0)
+        {
+          wlerr("ERROR: Failed to send message to mqueue error=%d\n",
+                ret);
+        }
+    }
+  else
+    {
+      ret = clock_gettime(CLOCK_REALTIME, &timeout);
+      if (ret < 0)
+        {
+          wlerr("ERROR: Failed to get time\n");
+          return false;
+        }
+
+      if (ticks)
+        {
+          bl_os_update_time(&timeout, ticks);
+        }
+
+      ret = file_mq_timedsend(&mq_adpt->mq, (const char *)item,
+                              len, prio, &timeout);
+      if (ret < 0)
+        {
+          wlerr("ERROR: Failed to timedsend message to mqueue error=%d\n",
+                ret);
+        }
+    }
+
+  return bl_os_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: bl_os_mq_send
+ *
+ * Description:
+ *   Send message of low priority to queue within a certain period of time
+ *
+ * Input Parameters:
+ *   queue - Message queue data pointer
+ *   item  - Message data pointer
+ *   ticks - Wait ticks
+ *
+ * Returned Value:
+ *   True if success or false if fail
+ *
+ ****************************************************************************/
+
+int32_t bl_os_mq_send(void *queue, void *item, size_t len, uint32_t ticks)
+{
+  return bl_os_mq_send_generic(queue, item, len, ticks, 0);
+}
+
+/****************************************************************************
+ * Name: bl_os_mq_recv
+ *
+ * Description:
+ *   Receive message from queue within a certain period of time
+ *
+ * Input Parameters:
+ *   queue - Message queue data pointer
+ *   item  - Message data pointer
+ *   ticks - Wait ticks
+ *
+ * Returned Value:
+ *   True if success or false if fail
+ *
+ ****************************************************************************/
+
+int bl_os_mq_recv(void *queue, void *item, uint32_t ticks)
+{
+  ssize_t ret;
+  struct timespec timeout;
+  unsigned int prio;
+  struct mq_adpt *mq_adpt = (struct mq_adpt *)queue;
+
+  ret = clock_gettime(CLOCK_REALTIME, &timeout);
+
+  if (ret < 0)
+    {
+      wlerr("ERROR: Failed to get time\n");
+      return false;
+    }
+
+  if (ticks)
+    {
+      bl_os_update_time(&timeout, ticks);
+    }
+
+  ret = file_mq_timedreceive(&mq_adpt->mq,
+                             (char *)item,
+                             mq_adpt->msgsize,
+                             &prio,
+                             &timeout);
+  if (ret < 0)
+    {
+      wlerr("ERROR: Failed to timedreceive from mqueue error=%d\n", ret);
+    }
+
+  return ret > 0 ? true : false;
+}
+
+/****************************************************************************
+ * Name: bl_os_timer_create
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void *bl_os_timer_create(void *func, void *argv)
+{
+  void *timerid;

Review comment:
       Is timer api only used in userspace?or for other reasons




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to