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



##########
File path: arch/risc-v/src/bl602/bl602_os_hal.h
##########
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * arch/risc-v/src/bl602/bl602_os_hal.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_BL602_OS_HAL_H
+#define __ARCH_RISCV_SRC_BL602_OS_HAL_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#include <assert.h>

Review comment:
       bl602_os_hal.h  is shared by the binary library and the open source 
code, it's better to move all header file inclusion to .c except stdint.h
   

##########
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:
       kernel should use wdog or work with delay

##########
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;
+  struct sigevent evp = {
+    .sigev_notify_function = (sigev_notify_function_t)func,
+    .sigev_notify = SIGEV_THREAD,
+    .sigev_value.sival_ptr = argv
+  };
+
+  timer_create(CLOCK_REALTIME, &evp, &timerid);
+
+  return timerid;
+}
+
+/****************************************************************************
+ * Name: bl_os_timer_delete
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_timer_delete(void *timerid)
+{
+  return timer_delete(timerid);
+}
+
+/****************************************************************************
+ * Name: os_timer_start_once
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec)
+{
+  struct itimerspec value = {
+    .it_interval = {
+      .tv_sec = 0,
+      .tv_nsec = 0},
+    .it_value = {
+      .tv_sec = t_sec,
+      .tv_nsec = t_nsec}
+  };
+
+  return timer_settime(timerid, 0, &value, NULL);
+}
+
+/****************************************************************************
+ * Name: os_timer_start_periodic
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec)
+{
+  struct itimerspec value = {
+    .it_interval = {
+      .tv_sec = t_sec,
+      .tv_nsec = t_nsec
+      },
+    .it_value = {
+      .tv_sec = t_sec,
+      .tv_nsec = t_nsec
+      }
+  };
+
+  return timer_settime(timerid, 0, &value, NULL);
+}
+
+/****************************************************************************
+ * Name: bl_os_workqueue_submit_upwork
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int bl_os_workqueue_submit_upwork(void *worker, void *argv, long tick)
+{
+  return work_queue(OS_HPWORK, &g_wifi_work, (worker_t)worker, argv, tick);
+}
+
+/****************************************************************************
+ * Name: bl_os_clock_gettime_ms
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+uint64_t bl_os_clock_gettime_ms(void)
+{
+  struct timespec ts;
+  clock_gettime(CLOCK_REALTIME, &ts);
+  return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+}
+
+/****************************************************************************
+ * Name: bl_os_isr_adpt_cb
+ *
+ * Description:
+ *   Wi-Fi interrupt adapter callback function
+ *
+ * Input Parameters:
+ *   arg - interrupt adapter private data
+ *
+ * Returned Value:
+ *   0 on success
+ *
+ ****************************************************************************/
+
+static int bl_os_isr_adpt_cb(int irq, void *context, FAR void *arg)
+{
+  struct irq_adpt *adapter = (struct irq_adpt *)arg;
+
+  adapter->func(adapter->arg);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: bl_os_irq_attach
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+void bl_os_irq_attach(int32_t n, void *f, void *arg)
+{
+  int ret;
+  struct irq_adpt *adapter;
+
+  wlinfo("INFO: n=%ld f=%p arg=%p\n", n, f, arg);
+
+  adapter = (struct irq_adpt *)kmm_malloc(sizeof(struct irq_adpt));
+
+  if (!adapter)
+    {
+      DEBUGASSERT(0);
+    }
+
+  adapter->func = f;
+  adapter->arg  = arg;
+
+  ret = irq_attach(n, bl_os_isr_adpt_cb, (void *)adapter);
+
+  if (ret != OK)
+    {
+      DEBUGASSERT(0);
+    }
+}
+
+/****************************************************************************
+ * Name: bl_os_mutex_create
+ *
+ * Description:
+ *   Create mutex
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   Mutex data pointer
+ *
+ ****************************************************************************/
+
+void *bl_os_mutex_create(void)
+{
+  int ret;
+  pthread_mutex_t *mutex;
+  int tmp;
+
+  tmp = sizeof(pthread_mutex_t);
+  mutex = (pthread_mutex_t *)kmm_malloc(tmp);
+  if (!mutex)
+    {
+      wlerr("ERROR: Failed to alloc %d memory\n", tmp);
+      return NULL;
+    }
+
+  ret = pthread_mutex_init(mutex, NULL);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to initialize mutex error=%d\n", ret);
+      kmm_free(mutex);
+      return NULL;
+    }
+
+  return mutex;
+}
+
+/****************************************************************************
+ * Name: bl_os_mutex_delete
+ *
+ * Description:
+ *   Delete mutex
+ *
+ * Input Parameters:
+ *   mutex_data - mutex data pointer
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void bl_os_mutex_delete(void *mutex_data)
+{
+  pthread_mutex_t *mutex = (pthread_mutex_t *)mutex_data;
+
+  pthread_mutex_destroy(mutex);
+  kmm_free(mutex);
+}
+
+/****************************************************************************
+ * Name: bl_os_mutex_lock
+ *
+ * Description:
+ *   Lock mutex
+ *
+ * Input Parameters:
+ *   mutex_data - mutex data pointer
+ *
+ * Returned Value:
+ *   True if success or false if fail
+ *
+ ****************************************************************************/
+
+int32_t bl_os_mutex_lock(void *mutex_data)
+{
+  int ret;
+  pthread_mutex_t *mutex = (pthread_mutex_t *)mutex_data;
+
+  ret = pthread_mutex_lock(mutex);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to lock mutex error=%d\n", ret);
+    }
+
+  return bl_os_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: bl_os_mutex_unlock
+ *
+ * Description:
+ *   Lock mutex
+ *
+ * Input Parameters:
+ *   mutex_data - mutex data pointer
+ *
+ * Returned Value:
+ *   True if success or false if fail
+ *
+ ****************************************************************************/
+
+int32_t bl_os_mutex_unlock(void *mutex_data)
+{
+  int ret;
+  pthread_mutex_t *mutex = (pthread_mutex_t *)mutex_data;

Review comment:
       switch to semaphore

##########
File path: arch/risc-v/src/bl602/bl602_os_hal.h
##########
@@ -0,0 +1,144 @@
+/****************************************************************************
+ * arch/risc-v/src/bl602/bl602_os_hal.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_BL602_OS_HAL_H
+#define __ARCH_RISCV_SRC_BL602_OS_HAL_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <nuttx/config.h>
+#include <nuttx/irq.h>
+#include <nuttx/mqueue.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+#include <pthread.h>
+
+#include <syslog.h>
+#include <nuttx/net/netdev.h>
+
+#ifdef CONFIG_BL602_WIRELESS
+#include "wifi_manager/bl_os_adapter.h"
+#endif
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Definition
+ ****************************************************************************/
+
+#define OS_HPWORK HPWORK
+#define OS_LPWORK LPWORK 
+
+extern void *__attribute__((weak)) _wifi_log_flag;
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+void bl_os_printf(const char *__fmt, ...);
+
+void *bl_os_malloc(unsigned int size);
+
+void bl_os_free(void *ptr);
+
+void *bl_os_zalloc(unsigned int size);
+
+void *bl_os_mq_creat(uint32_t queue_len, uint32_t item_size);
+
+void bl_os_mq_delete(void *mq);
+
+int32_t bl_os_mq_send(void *queue, void *item, size_t len, uint32_t ticks);
+
+int bl_os_mq_recv(void *queue, void *item, uint32_t ticks);
+
+void *bl_os_timer_create(void *func, void *argv);
+
+int bl_os_timer_delete(void *timerid);
+
+int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec);
+
+int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec);
+
+int bl_os_workqueue_submit_upwork(void *worker, void *argv, long tick);
+
+uint64_t bl_os_clock_gettime_ms(void);
+
+void bl_os_irq_attach(int32_t n, void *f, void *arg);
+
+void *bl_os_mutex_create(void);
+
+void bl_os_mutex_delete(void *mutex_data);
+
+int32_t bl_os_mutex_lock(void *mutex_data);
+
+int32_t bl_os_mutex_unlock(void *mutex_data);
+
+void *bl_os_sem_create(uint32_t init);
+
+int32_t bl_os_sem_take(void *semphr, uint32_t ticks);
+
+int32_t bl_os_sem_give(void *semphr);
+
+void bl_os_sem_delete(void *semphr);
+
+int bl_os_api_init(void);
+
+int bl_os_event_notify(int evt, int val);
+
+void bl_os_lock_giant(void);
+
+void bl_os_unlock_giant(void);
+
+int bl_os_msleep(long msec);
+
+int bl_os_sleep(unsigned int seconds);
+
+uint32_t bl_os_enter_critical(void);
+
+void bl_os_exit_critical(uint32_t level);
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __ASSEMBLY__ */
+#endif /* __ARCH_RISCV_SRC_BL602_OS_HAL_H*/

Review comment:
       add blank line




-- 
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