nuttx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-nuttx] xiewenxiang commented on a change in pull request #3543: feat(esp32c3): Support esp32c3 ble function
Date Tue, 20 Apr 2021 08:42:08 GMT

xiewenxiang commented on a change in pull request #3543:
URL: https://github.com/apache/incubator-nuttx/pull/3543#discussion_r616473894



##########
File path: arch/risc-v/src/esp32c3/esp32c3_ble_adapter.c
##########
@@ -0,0 +1,2059 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_ble_adapter.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 <nuttx/config.h>
+
+#ifdef CONFIG_ESP32C3_BLE
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <clock/clock.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "esp32c3_ble_adapter.h"
+#include "hardware/esp32c3_syscon.h"
+#include "esp32c3.h"
+#include "esp32c3_attr.h"
+#include "esp32c3_irq.h"
+#include "esp32c3_rt_timer.h"
+
+#include "nuttx/kmalloc.h"
+#include <nuttx/mqueue.h>
+#include "nuttx/spinlock.h"
+#include <nuttx/irq.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/kthread.h>
+#include <nuttx/wdog.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/sched.h>
+#include <nuttx/signal.h>
+
+#include "espidf_wifi.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+#define OSI_FUNCS_TIME_BLOCKING  0xffffffff
+#define OSI_VERSION              0x00010006
+#define OSI_MAGIC_VALUE          0xfadebead
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* BLE message queue private data */
+
+struct mq_adpt
+{
+  struct file mq;           /* Message queue handle */
+  uint32_t    msgsize;      /* Message size */
+  char        name[16];     /* Message queue name */
+};
+
+/* BLE interrupt adapter private data */
+
+struct irq_adpt
+{
+  void (*func)(void *arg);  /* Interrupt callback function */
+  void *arg;                /* Interrupt private data */
+};
+
+/* BLE low power control struct */
+
+typedef union
+{
+    struct
+    {
+        uint32_t enable                  :  1; /* whether low power mode is required */
+        uint32_t lpclk_sel               :  2; /* low power clock source */
+        uint32_t mac_bb_pd               :  1; /* whether hardware(MAC, BB) force-power-down
is required during sleep */
+        uint32_t wakeup_timer_required   :  1; /* whether system timer is needed */
+        uint32_t no_light_sleep          :  1; /* do not allow system to enter light sleep
after bluetooth is enabled */
+        uint32_t reserved                : 26; /* reserved */
+    };
+    uint32_t val;
+} btdm_lpcntl_t;
+
+/* low power control status */
+
+typedef union
+{
+    struct
+    {
+        uint32_t pm_lock_released        :  1; /* whether power management lock is released
*/
+        uint32_t mac_bb_pd               :  1; /* whether hardware(MAC, BB) is powered down
*/
+        uint32_t phy_enabled             :  1; /* whether phy is switched on */
+        uint32_t wakeup_timer_started    :  1; /* whether wakeup timer is started */
+        uint32_t reserved                : 28; /* reserved */
+    };
+    uint32_t val;
+} btdm_lpstat_t;
+
+/* vendor dependent signals to be posted to controller task */
+
+typedef enum
+{
+    BTDM_VND_OL_SIG_WAKEUP_TMR = 0,
+    BTDM_VND_OL_SIG_NUM,
+} btdm_vnd_ol_sig_t;
+
+/* prototype of function to handle vendor dependent signals */
+
+typedef void (* btdm_vnd_ol_task_func_t)(void *param);
+
+/* VHCI function interface */
+
+typedef struct vhci_host_callback
+{
+    void (*notify_host_send_available)(void);               /* callback used to notify that
the host can send packet to controller */
+    int (*notify_host_recv)(uint8_t *data, uint16_t len);   /* callback used to notify that
the controller has a packet to send to the host */
+} vhci_host_callback_t;
+
+/* Dram region */
+
+typedef struct
+{
+    esp_bt_mode_t mode;
+    intptr_t start;
+    intptr_t end;
+} btdm_dram_available_region_t;
+
+typedef void (* osi_intr_handler)(void);
+
+/* BLE OS function */
+
+struct osi_funcs_t
+{
+    uint32_t _magic;
+    uint32_t _version;
+    void (*_interrupt_set)(int cpu_no, int intr_source,
+                            int interrupt_no, int interrpt_prio);
+    void (*_interrupt_clear)(int interrupt_source, int interrupt_no);
+    void (*_interrupt_handler_set)(int interrupt_no, void * fn, void *arg);
+    void (*_interrupt_disable)(void);
+    void (*_interrupt_restore)(void);
+    void (*_task_yield)(void);
+    void (*_task_yield_from_isr)(void);
+    void *(*_semphr_create)(uint32_t max, uint32_t init);
+    void (*_semphr_delete)(void *semphr);
+    int (*_semphr_take_from_isr)(void *semphr, void *hptw);
+    int (*_semphr_give_from_isr)(void *semphr, void *hptw);
+    int (*_semphr_take)(void *semphr, uint32_t block_time_ms);
+    int (*_semphr_give)(void *semphr);
+    void *(*_mutex_create)(void);
+    void (*_mutex_delete)(void *mutex);
+    int (*_mutex_lock)(void *mutex);
+    int (*_mutex_unlock)(void *mutex);
+    void *(* _queue_create)(uint32_t queue_len, uint32_t item_size);
+    void (* _queue_delete)(void *queue);
+    int (* _queue_send)(void *queue, void *item, uint32_t block_time_ms);
+    int (* _queue_send_from_isr)(void *queue, void *item, void *hptw);
+    int (* _queue_recv)(void *queue, void *item, uint32_t block_time_ms);
+    int (* _queue_recv_from_isr)(void *queue, void *item, void *hptw);
+    int (* _task_create)(void *task_func, const char *name,
+                            uint32_t stack_depth, void *param, uint32_t prio,
+                            void *task_handle, uint32_t core_id);
+    void (* _task_delete)(void *task_handle);
+    bool (* _is_in_isr)(void);
+    int (* _cause_sw_intr_to_core)(int core_id, int intr_no);
+    void *(* _malloc)(size_t size);
+    void *(* _malloc_internal)(size_t size);
+    void (* _free)(void *p);
+    int (* _read_efuse_mac)(uint8_t mac[6]);
+    void (* _srand)(unsigned int seed);
+    int (* _rand)(void);
+    uint32_t (* _btdm_lpcycles_2_hus)(uint32_t cycles, uint32_t *error_corr);
+    uint32_t (* _btdm_hus_2_lpcycles)(uint32_t us);
+    bool (* _btdm_sleep_check_duration)(int32_t *slot_cnt);
+    void (* _btdm_sleep_enter_phase1)(uint32_t lpcycles);  /* called when interrupt is disabled
*/
+    void (* _btdm_sleep_enter_phase2)(void);
+    void (* _btdm_sleep_exit_phase1)(void);  /* called from ISR */
+    void (* _btdm_sleep_exit_phase2)(void);  /* called from ISR */
+    void (* _btdm_sleep_exit_phase3)(void);  /* called from task */
+    void (* _coex_wifi_sleep_set)(bool sleep);
+    int (* _coex_core_ble_conn_dyn_prio_get)(bool *low, bool *high);
+    void (* _coex_schm_status_bit_set)(uint32_t type, uint32_t status);
+    void (* _coex_schm_status_bit_clear)(uint32_t type, uint32_t status);
+    void (* _interrupt_on)(int intr_num);
+    void (* _interrupt_off)(int intr_num);
+    void (* _esp_hw_power_down)(void);
+    void (* _esp_hw_power_up)(void);
+    void (* _ets_backup_dma_copy)(uint32_t reg,
+                    uint32_t mem_addr, uint32_t num, bool to_rem);
+};
+
+/****************************************************************************
+ * Private Function
+ ****************************************************************************/
+
+static void interrupt_set_wrapper(int cpu_no,
+                    int intr_source, int intr_num, int intr_prio);
+static void interrupt_clear_wrapper(int intr_source, int intr_num);
+static void interrupt_handler_set_wrapper(int n, void * fn, void *arg);
+static void IRAM_ATTR interrupt_disable(void);
+static void IRAM_ATTR interrupt_restore(void);
+static void IRAM_ATTR task_yield_from_isr(void);
+static void *semphr_create_wrapper(uint32_t max, uint32_t init);
+static void semphr_delete_wrapper(void *semphr);
+static int IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, void *hptw);
+static int IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw);
+static int  semphr_take_wrapper(void *semphr, uint32_t block_time_ms);
+static int  semphr_give_wrapper(void *semphr);
+static void *mutex_create_wrapper(void);
+static void mutex_delete_wrapper(void *mutex);
+static int mutex_lock_wrapper(void *mutex);
+static int mutex_unlock_wrapper(void *mutex);
+static int IRAM_ATTR queue_send_from_isr_wrapper(void *queue,
+                        void *item, void *hptw);
+int queue_recv_wrapper(void *queue, void *item, uint32_t block_time_ms);
+static int IRAM_ATTR queue_recv_from_isr_wrapper(void *queue,
+                        void *item, void *hptw);
+static int task_create_wrapper(void *task_func,
+                        const char *name, uint32_t stack_depth, void *param,
+                        uint32_t prio, void *task_handle, uint32_t core_id);
+static void task_delete_wrapper(void *task_handle);
+static bool IRAM_ATTR is_in_isr_wrapper(void);
+static void *malloc_wrapper(size_t size);
+static void *malloc_internal_wrapper(size_t size);
+static int IRAM_ATTR read_mac_wrapper(uint8_t mac[6]);
+static void IRAM_ATTR srand_wrapper(unsigned int seed);
+static int IRAM_ATTR rand_wrapper(void);
+static uint32_t IRAM_ATTR btdm_lpcycles_2_hus(uint32_t cycles,
+                        uint32_t *error_corr);
+static uint32_t IRAM_ATTR btdm_hus_2_lpcycles(uint32_t us);
+static void coex_wifi_sleep_set_hook(bool sleep);
+static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status);
+static void coex_schm_status_bit_clear_wrapper(uint32_t type,
+                        uint32_t status);
+static void interrupt_on_wrapper(int intr_num);
+static void interrupt_off_wrapper(int intr_num);
+
+/****************************************************************************
+ * Extern Functions declaration and value
+ ****************************************************************************/
+
+extern int btdm_osi_funcs_register(void *osi_funcs);
+
+/* Initialise and De-initialise */
+
+extern int btdm_controller_init(esp_bt_controller_config_t *config_opts);
+extern void btdm_controller_deinit(void);
+extern int btdm_controller_enable(esp_bt_mode_t mode);
+extern void btdm_controller_disable(void);
+extern uint8_t btdm_controller_get_mode(void);
+extern const char *btdm_controller_get_compile_version(void);
+extern void btdm_rf_bb_init_phase2(void); /* shall be called after PHY/RF is enabled */
+
+/* Sleep */
+
+extern void btdm_controller_enable_sleep(bool enable);
+extern uint8_t btdm_controller_get_sleep_mode(void);
+extern bool btdm_power_state_active(void);
+extern void btdm_wakeup_request(void);
+extern void btdm_in_wakeup_requesting_set(bool in_wakeup_requesting);
+
+/* vendor dependent tasks to be posted and handled by controller task */
+
+extern int btdm_vnd_offload_task_register(btdm_vnd_ol_sig_t sig,
+                        btdm_vnd_ol_task_func_t func);
+extern int btdm_vnd_offload_task_deregister(btdm_vnd_ol_sig_t sig);
+extern int btdm_vnd_offload_post_from_isr(btdm_vnd_ol_sig_t sig,
+                        void *param, bool need_yield);
+extern int btdm_vnd_offload_post(btdm_vnd_ol_sig_t sig, void *param);
+
+/* Low Power Clock */
+
+extern bool btdm_lpclk_select_src(uint32_t sel);
+extern bool btdm_lpclk_set_div(uint32_t div);
+extern int btdm_hci_tl_io_event_post(int event);
+
+/* VHCI */
+
+extern bool API_vhci_host_check_send_available(void); /* Functions in bt lib */
+extern void API_vhci_host_send_packet(uint8_t * data, uint16_t len);
+extern int API_vhci_host_register_callback(
+                          const vhci_host_callback_t * callback);
+
+/* TX power */
+
+extern int ble_txpwr_set(int power_type, int power_level);
+extern int ble_txpwr_get(int power_type);
+
+extern uint16_t l2c_ble_link_get_tx_buf_num(void);
+extern int coex_core_ble_conn_dyn_prio_get(bool *low, bool *high);
+
+extern bool btdm_deep_sleep_mem_init(void);
+extern void btdm_deep_sleep_mem_deinit(void);
+extern void btdm_ble_power_down_dma_copy(bool copy);
+extern uint8_t btdm_sleep_clock_sync(void);
+
+extern char _bss_start_btdm;
+extern char _bss_end_btdm;
+extern char _data_start_btdm;
+extern char _data_end_btdm;
+extern uint32_t _data_start_btdm_rom;
+extern uint32_t _data_end_btdm_rom;
+
+extern uint32_t _bt_bss_start;
+extern uint32_t _bt_bss_end;
+extern uint32_t _btdm_bss_start;
+extern uint32_t _btdm_bss_end;
+extern uint32_t _bt_data_start;
+extern uint32_t _bt_data_end;
+extern uint32_t _btdm_data_start;
+extern uint32_t _btdm_data_end;
+
+extern char _bt_tmp_bss_start;
+extern char _bt_tmp_bss_end;
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static DRAM_ATTR struct osi_funcs_t *g_osi_funcs_p;
+
+/* Static variable declare */
+
+static DRAM_ATTR esp_bt_controller_status_t btdm_controller_status =
+                        ESP_BT_CONTROLLER_STATUS_IDLE;
+
+/* low power control struct */
+
+static DRAM_ATTR btdm_lpcntl_t g_lp_cntl;
+
+/* low power status struct */
+
+static DRAM_ATTR btdm_lpstat_t g_lp_stat;
+
+/* measured average low power clock period in micro seconds */
+
+static DRAM_ATTR uint32_t g_btdm_lpcycle_us = 0;
+
+/* number of fractional bit for g_btdm_lpcycle_us */
+
+static DRAM_ATTR uint8_t g_btdm_lpcycle_us_frac = 0;
+
+/* BT interrupt private data */
+
+static bool g_ble_irq_bind;
+static   irqstate_t g_inter_flags;
+static uint32_t g_phy_clk_en_cnt;
+static int64_t g_phy_rf_en_ts;
+static uint8_t g_phy_access_ref;
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* BLE OS adapter data */
+
+static const struct osi_funcs_t g_osi_funcs_ro =
+{
+    ._magic = OSI_MAGIC_VALUE,
+    ._version = OSI_VERSION,
+    ._interrupt_set = interrupt_set_wrapper,
+    ._interrupt_clear = interrupt_clear_wrapper,
+    ._interrupt_handler_set = interrupt_handler_set_wrapper,
+    ._interrupt_disable = interrupt_disable,
+    ._interrupt_restore = interrupt_restore,
+    ._task_yield = task_yield_from_isr,
+    ._task_yield_from_isr = task_yield_from_isr,
+    ._semphr_create = semphr_create_wrapper,
+    ._semphr_delete = semphr_delete_wrapper,
+    ._semphr_take_from_isr = semphr_take_from_isr_wrapper,
+    ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
+    ._semphr_take = semphr_take_wrapper,
+    ._semphr_give = semphr_give_wrapper,
+    ._mutex_create = mutex_create_wrapper,
+    ._mutex_delete = mutex_delete_wrapper,
+    ._mutex_lock = mutex_lock_wrapper,
+    ._mutex_unlock = mutex_unlock_wrapper,
+    ._queue_create = queue_create_wrapper,
+    ._queue_delete = queue_delete_wrapper,
+    ._queue_send = queue_send_wrapper,
+    ._queue_send_from_isr = queue_send_from_isr_wrapper,
+    ._queue_recv = queue_recv_wrapper,
+    ._queue_recv_from_isr = queue_recv_from_isr_wrapper,
+    ._task_create = task_create_wrapper,
+    ._task_delete = task_delete_wrapper,
+    ._is_in_isr = is_in_isr_wrapper,
+    ._cause_sw_intr_to_core = NULL,
+    ._malloc = malloc_wrapper,
+    ._malloc_internal = malloc_internal_wrapper,
+    ._free = free,
+    ._read_efuse_mac = read_mac_wrapper,
+    ._srand = srand_wrapper,
+    ._rand = rand_wrapper,
+    ._btdm_lpcycles_2_hus = btdm_lpcycles_2_hus,
+    ._btdm_hus_2_lpcycles = btdm_hus_2_lpcycles,
+    ._btdm_sleep_check_duration = NULL,
+    ._btdm_sleep_enter_phase1 = NULL,
+    ._btdm_sleep_enter_phase2 = NULL,
+    ._btdm_sleep_exit_phase1 = NULL,
+    ._btdm_sleep_exit_phase2 = NULL,
+    ._btdm_sleep_exit_phase3 = NULL,
+    ._coex_wifi_sleep_set = coex_wifi_sleep_set_hook,
+    ._coex_core_ble_conn_dyn_prio_get = coex_core_ble_conn_dyn_prio_get,
+    ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
+    ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
+    ._interrupt_on = interrupt_on_wrapper,
+    ._interrupt_off = interrupt_off_wrapper,
+    ._esp_hw_power_down = NULL,
+    ._esp_hw_power_up = NULL,
+    ._ets_backup_dma_copy = NULL,

Review comment:
       OK, it's been modified.




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

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



Mime
View raw message