This commit is contained in:
刘可亮
2025-07-22 11:15:46 +08:00
parent d164b333ed
commit 11c97ef399
2870 changed files with 951307 additions and 26675 deletions

View File

@@ -0,0 +1,69 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2018-12-18 ChenYong first implementation
* 2021-08-19 WaterFishJ fix the assert bug
*/
#include <stddef.h>
#include <rtthread.h>
#include <rthw.h>
#include "syscfg/syscfg.h"
#include "nimble/nimble_port.h"
#if NIMBLE_CFG_CONTROLLER
static rt_thread_t ll_task_h;
#endif
extern void ble_ll_task(void *arg);
#ifdef RT_DEBUG
RT_WEAK void __aeabi_assert(const char *expr, const char *file, int line)
{
rt_assert_handler(expr, file, line);
}
#endif
int nimble_port_rtthread_init(void)
{
extern int realtek_bt_init(void);
realtek_bt_init();
nimble_port_init();
#if NIMBLE_CFG_CONTROLLER
/*
* Create task where NimBLE LL will run. This one is required as LL has its
* own event queue and should have highest priority. The task function is
* provided by NimBLE and in case of FreeRTOS it does not need to be wrapped
* since it has compatible prototype.
*/
ll_task_h = rt_thread_create("ll", ble_ll_task, NULL,
MYNEWT_VAL(BLE_CTLR_THREAD_STACK_SIZE),
MYNEWT_VAL(BLE_CTLR_THREAD_PRIORITY), 10);
if (ll_task_h != RT_NULL)
rt_thread_startup(ll_task_h);
#endif
return 0;
}
void ble_hs_thread_entry(void *parameter)
{
nimble_port_run();
}
void ble_hs_thread_startup(void)
{
rt_thread_t tid;
tid = rt_thread_create("host", ble_hs_thread_entry, NULL,
MYNEWT_VAL(BLE_HOST_THREAD_STACK_SIZE),
MYNEWT_VAL(BLE_HOST_THREAD_PRIORITY), 10);
if (tid != RT_NULL)
rt_thread_startup(tid);
}
INIT_COMPONENT_EXPORT(nimble_port_rtthread_init);

View File

@@ -0,0 +1,408 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2018-12-18 ZeroFree first implementation
*/
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "nimble/nimble_npl.h"
#include <rtthread.h>
#include <rthw.h>
static void npl_rtthread_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq, ble_npl_event_fn *ev_cb, void *ev_arg);
static ble_npl_error_t npl_rtthread_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks);
static ble_npl_time_t npl_rtthread_callout_remaining_ticks(struct ble_npl_callout *co, ble_npl_time_t now);
static ble_npl_error_t npl_rtthread_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks);
static ble_npl_error_t npl_rtthread_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms);
void *ble_npl_get_current_task_id(void)
{
return rt_thread_self();
}
int ble_npl_task_init(struct ble_npl_task *t, const char *name,
ble_npl_task_fn *func,void *arg, uint8_t prio,
uint32_t sanity_itvl, uint32_t *stack_bottom, uint16_t stack_size)
{
rt_thread_t tid;
tid = rt_thread_create(name, func, arg, stack_size, prio, 10);
if(tid)
{
t->t = tid;
rt_thread_startup(tid);
return RT_EOK;
}
return -RT_ERROR;
}
void ble_npl_eventq_init(struct ble_npl_eventq *evq)
{
evq->q = rt_mq_create("npl_evq", sizeof(struct ble_npl_eventq *), 32, RT_IPC_FLAG_FIFO);
}
struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
{
struct ble_npl_event *ev = NULL;
rt_mq_recv((struct rt_messagequeue *)evq->q, &ev, sizeof(struct ble_npl_eventq *), tmo);
if (ev)
{
ev->queued = false;
}
return ev;
}
#ifdef PKG_USING_BLUETRUM_SDK
RT_SECTION(".com_text")
#endif
void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
{
int ret __attribute__((unused));
if (ev->queued)
{
return;
}
ev->queued = true;
ret = rt_mq_send((struct rt_messagequeue *)evq->q, &ev, sizeof(struct ble_npl_eventq *));
RT_ASSERT(ret == RT_EOK);
}
void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
{
if (!ev->queued)
{
return;
}
rt_mq_control((struct rt_messagequeue *)evq->q, RT_IPC_CMD_RESET, RT_NULL);
ev->queued = false;
}
void ble_npl_event_run(struct ble_npl_event *ev)
{
ev->fn(ev);
}
bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
{
int count;
rt_base_t level;
level = rt_hw_interrupt_disable();
count = ((struct rt_messagequeue *)evq->q)->entry;
rt_hw_interrupt_enable(level);
return count ? true : false;
}
void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
void *arg)
{
memset(ev, 0, sizeof(*ev));
ev->fn = fn;
ev->arg = arg;
}
bool ble_npl_event_is_queued(struct ble_npl_event *ev)
{
return ev->queued;
}
void *ble_npl_event_get_arg(struct ble_npl_event *ev)
{
return ev->arg;
}
void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
{
ev->arg = arg;
}
ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
{
if (!mu)
{
return BLE_NPL_INVALID_PARAM;
}
mu->handle = rt_mutex_create("mutx", RT_IPC_FLAG_FIFO);
RT_ASSERT(mu->handle);
return BLE_NPL_OK;
}
ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
{
int ret;
if (!mu)
{
return BLE_NPL_INVALID_PARAM;
}
RT_ASSERT(mu->handle);
ret = rt_mutex_take((struct rt_mutex *)mu->handle, timeout);
return ret == RT_EOK ? BLE_NPL_OK : BLE_NPL_TIMEOUT;
}
ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
{
int ret;
if (!mu)
{
return BLE_NPL_INVALID_PARAM;
}
RT_ASSERT(mu->handle);
ret = rt_mutex_release((struct rt_mutex *)mu->handle);
return ret == RT_EOK ? BLE_NPL_OK : BLE_NPL_ERROR;
}
ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
{
if (!sem)
{
return BLE_NPL_INVALID_PARAM;
}
sem->handle = rt_sem_create("npl_sem", tokens, RT_IPC_FLAG_FIFO);
RT_ASSERT(sem->handle);
return BLE_NPL_OK;
}
ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
{
int ret;
if (!sem)
{
return BLE_NPL_INVALID_PARAM;
}
RT_ASSERT(sem->handle);
ret = rt_sem_take((struct rt_semaphore *)sem->handle, timeout);
return ret == RT_EOK ? BLE_NPL_OK : BLE_NPL_TIMEOUT;
}
ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
{
int ret;
if (!sem)
{
return BLE_NPL_INVALID_PARAM;
}
RT_ASSERT(sem->handle);
ret = rt_sem_release((struct rt_semaphore *) sem->handle);
return ret == RT_EOK ? BLE_NPL_OK : BLE_NPL_ERROR;
}
uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
{
int count;
rt_base_t level;
RT_ASSERT(sem->handle);
level = rt_hw_interrupt_disable();
count = ((struct rt_semaphore *)sem->handle)->value;
rt_hw_interrupt_enable(level);
return count;
}
void ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
npl_rtthread_callout_init(co, evq, ev_cb, ev_arg);
}
ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
{
return npl_rtthread_callout_reset(co, ticks);
}
void ble_npl_callout_stop(struct ble_npl_callout *co)
{
if (co->handle)
rt_timer_stop((struct rt_timer *)co->handle);
}
bool ble_npl_callout_is_active(struct ble_npl_callout *co)
{
return (((struct rt_timer *)co->handle)->parent.flag & RT_TIMER_FLAG_ACTIVATED) ? true : false;
}
ble_npl_time_t ble_npl_callout_get_ticks(struct ble_npl_callout *co)
{
return ((struct rt_timer *)co->handle)->timeout_tick;
}
ble_npl_time_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
ble_npl_time_t time)
{
return npl_rtthread_callout_remaining_ticks(co, time);
}
void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
{
co->ev.arg = arg;
}
ble_npl_time_t ble_npl_time_get(void)
{
return rt_tick_get();
}
ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
{
return npl_rtthread_time_ms_to_ticks(ms, out_ticks);
}
ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
{
return npl_rtthread_time_ticks_to_ms(ticks, out_ms);
}
ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
{
return ms * RT_TICK_PER_SECOND / 1000;
}
uint32_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
{
return ticks * 1000 / RT_TICK_PER_SECOND;
}
void ble_npl_time_delay(ble_npl_time_t ticks)
{
rt_thread_delay(ticks);
}
uint32_t ble_npl_hw_enter_critical(void)
{
rt_interrupt_enter();
return 0;
}
void ble_npl_hw_exit_critical(uint32_t ctx)
{
rt_interrupt_leave();
}
#ifdef PKG_USING_BLUETRUM_SDK
RT_SECTION(".com_text") __attribute__((noinline))
#endif
static void os_callout_timer_cb(void *parameter)
{
struct ble_npl_callout *co;
co = (struct ble_npl_callout *)parameter;
RT_ASSERT(co);
if (co->evq)
{
ble_npl_eventq_put(co->evq, &co->ev);
}
else
{
co->ev.fn(&co->ev);
}
}
static void npl_rtthread_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
memset(co, 0, sizeof(*co));
co->handle = rt_timer_create("co", os_callout_timer_cb, co, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
co->evq = evq;
ble_npl_event_init(&co->ev, ev_cb, ev_arg);
}
static ble_npl_error_t npl_rtthread_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
{
if (ticks == 0)
{
ticks = 1;
}
rt_timer_stop((struct rt_timer *)co->handle);
rt_timer_control((struct rt_timer *)co->handle, RT_TIMER_CTRL_SET_TIME, &ticks);
rt_timer_start((struct rt_timer *)co->handle);
return BLE_NPL_OK;
}
static ble_npl_time_t npl_rtthread_callout_remaining_ticks(struct ble_npl_callout *co,
ble_npl_time_t now)
{
ble_npl_time_t rt;
uint32_t exp;
rt_base_t level;
level = rt_hw_interrupt_disable();
exp = ((struct rt_timer *)co->handle)->timeout_tick;
rt_hw_interrupt_enable(level);
if (exp > now)
{
rt = exp - now;
}
else
{
rt = 0;
}
return rt;
}
static ble_npl_error_t npl_rtthread_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
{
uint64_t ticks;
ticks = rt_tick_from_millisecond(ms);
if (ticks > UINT32_MAX)
{
return BLE_NPL_EINVAL;
}
*out_ticks = ticks;
return BLE_NPL_OK;
}
static ble_npl_error_t npl_rtthread_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
{
uint64_t ms;
ms = ((uint64_t)ticks * 1000) / RT_TICK_PER_SECOND;
if (ms > UINT32_MAX)
{
return BLE_NPL_EINVAL;
}
*out_ms = ms;
return BLE_NPL_OK;
}

View File

@@ -0,0 +1,469 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2019-02-14 ZeroFree first implementation
*/
#include <string.h>
#include "nimble/nimble_npl.h"
#include "nimble/npl_shell.h"
#include <rtthread.h>
#define SHELL_PROMPT "shell"
#define SHELL_MAX_MODULES 3
#define SHELL_PROMPT_SUFFIX "> "
static const char *get_command_and_module(char *argv[], int *module);
static int set_default_module(const char *name);
static void print_prompt(void);
static int get_destination_module(const char *module_str, int len);
static int show_cmd_help(char *argv[]);
static struct shell_module shell_modules[SHELL_MAX_MODULES];
static size_t num_of_shell_entities;
static const char *prompt;
static int default_module = -1;
static shell_cmd_func_t app_cmd_handler;
static shell_prompt_function_t app_prompt_handler;
void console_write(const char *str, int cnt)
{
rt_device_write(rt_console_get_device(), 0, str, cnt);
}
int shell_register(const char *module_name, const struct shell_cmd *commands)
{
if (num_of_shell_entities >= SHELL_MAX_MODULES)
{
console_printf("Max number of modules reached\n");
return -1;
}
shell_modules[num_of_shell_entities].name = module_name;
shell_modules[num_of_shell_entities].commands = commands;
++num_of_shell_entities;
return 0;
}
void shell_register_default_module(const char *name)
{
int result = set_default_module(name);
if (result != -1)
{
console_printf("\n");
print_prompt();
}
}
static void print_modules(void)
{
int module;
for (module = 0; module < num_of_shell_entities; module++)
{
console_printf("%s\n", shell_modules[module].name);
}
}
static void print_module_commands(const int module)
{
const struct shell_module *shell_module = &shell_modules[module];
int i;
console_printf("help\n");
for (i = 0; shell_module->commands[i].sc_cmd; i++)
{
console_printf("%-30s", shell_module->commands[i].sc_cmd);
if (shell_module->commands[i].help &&
shell_module->commands[i].help->summary)
{
console_printf("%s", shell_module->commands[i].help->summary);
}
console_printf("\n");
}
}
static int show_help(int argc, char *argv[])
{
int module;
/* help per command */
if ((argc > 2) || ((default_module != -1) && (argc == 2)))
{
return show_cmd_help(&argv[1]);
}
/* help per module */
if ((argc == 2) || ((default_module != -1) && (argc == 1)))
{
if (default_module == -1)
{
module = get_destination_module(argv[1], -1);
if (module == -1)
{
console_printf("Illegal module %s\n", argv[1]);
return 0;
}
}
else
{
module = default_module;
}
print_module_commands(module);
}
else /* help for all entities */
{
console_printf("Available modules:\n");
print_modules();
console_printf("To select a module, enter 'select <module name>'.\n");
}
return 0;
}
static int set_default_module(const char *name)
{
int module;
module = get_destination_module(name, -1);
if (module == -1)
{
console_printf("Illegal module %s, default is not changed\n", name);
return -1;
}
default_module = module;
return 0;
}
static int select_module(int argc, char *argv[])
{
if (argc == 1)
{
default_module = -1;
}
else
{
set_default_module(argv[1]);
}
return 0;
}
static size_t line2argv(char *str, char *argv[], size_t size)
{
size_t argc = 0;
if (!strlen(str))
{
return 0;
}
while (*str && *str == ' ')
{
str++;
}
if (!*str)
{
return 0;
}
argv[argc++] = str;
while ((str = strchr(str, ' ')))
{
*str++ = '\0';
while (*str && *str == ' ')
{
str++;
}
if (!*str)
{
break;
}
argv[argc++] = str;
if (argc == size)
{
console_printf("Too many parameters (max %zu)\n", size - 1);
return 0;
}
}
/* keep it POSIX style where argv[argc] is required to be NULL */
argv[argc] = NULL;
return argc;
}
static shell_cmd_func_t get_cb(int argc, char *argv[])
{
const char *first_string = argv[0];
int module = -1;
const struct shell_module *shell_module;
const char *command;
int i;
if (!first_string || first_string[0] == '\0')
{
console_printf("Illegal parameter\n");
return NULL;
}
if (!strcmp(first_string, "help"))
{
return show_help;
}
if (!strcmp(first_string, "select"))
{
return select_module;
}
if ((argc == 1) && (default_module == -1))
{
console_printf("Missing parameter\n");
return NULL;
}
command = get_command_and_module(argv, &module);
if ((module == -1) || (command == NULL))
{
return NULL;
}
shell_module = &shell_modules[module];
for (i = 0; shell_module->commands[i].sc_cmd; i++)
{
if (!strcmp(command, shell_module->commands[i].sc_cmd))
{
return shell_module->commands[i].sc_cmd_func;
}
}
return NULL;
}
static const char *get_prompt(void)
{
const char *str;
if (app_prompt_handler)
{
str = app_prompt_handler();
if (str)
{
return str;
}
}
if (default_module != -1)
{
return shell_modules[default_module].name;
}
return prompt;
}
static void print_prompt(void)
{
console_printf("%s%s", get_prompt(), SHELL_PROMPT_SUFFIX);
}
static int get_destination_module(const char *module_str, int len)
{
int i;
for (i = 0; i < num_of_shell_entities; i++)
{
if (len < 0)
{
if (!strcmp(module_str, shell_modules[i].name))
{
return i;
}
}
else
{
if (!strncmp(module_str, shell_modules[i].name, len))
{
return i;
}
}
}
return -1;
}
/* For a specific command: argv[0] = module name, argv[1] = command name
* If a default module was selected: argv[0] = command name
*/
static const char *get_command_and_module(char *argv[], int *module)
{
*module = -1;
if (!argv[0])
{
console_printf("Unrecognized command\n");
return NULL;
}
if (default_module == -1)
{
if (!argv[1] || argv[1][0] == '\0')
{
console_printf("Unrecognized command: %s\n", argv[0]);
return NULL;
}
*module = get_destination_module(argv[0], -1);
if (*module == -1)
{
console_printf("Illegal module %s\n", argv[0]);
return NULL;
}
return argv[1];
}
*module = default_module;
return argv[0];
}
static void print_command_params(const int module, const int command)
{
const struct shell_module *shell_module = &shell_modules[module];
const struct shell_cmd *shell_cmd = &shell_module->commands[command];
int i;
if (!(shell_cmd->help && shell_cmd->help->params))
{
return;
}
for (i = 0; shell_cmd->help->params[i].param_name; i++)
{
console_printf("%-30s%s\n", shell_cmd->help->params[i].param_name,
shell_cmd->help->params[i].help);
}
}
static int show_cmd_help(char *argv[])
{
const char *command = NULL;
int module = -1;
const struct shell_module *shell_module = NULL;
const struct shell_cmd *cmd;
int i;
command = get_command_and_module(argv, &module);
if ((module == -1) || (command == NULL))
{
return 0;
}
shell_module = &shell_modules[module];
for (i = 0; shell_module->commands[i].sc_cmd; i++)
{
cmd = &shell_module->commands[i];
if (!strcmp(command, cmd->sc_cmd))
{
if (!cmd->help || (!cmd->help->summary &&
!cmd->help->usage &&
!cmd->help->params))
{
console_printf("(no help available)\n");
return 0;
}
if (cmd->help->summary)
{
console_printf("Summary:\n");
console_printf("%s\n", cmd->help->summary);
}
if (cmd->help->usage)
{
console_printf("Usage:\n");
console_printf("%s\n", cmd->help->usage);
}
if (cmd->help->params)
{
console_printf("Parameters:\n");
print_command_params(module, i);
}
return 0;
}
}
console_printf("Unrecognized command: %s\n", argv[0]);
return 0;
}
void shell_process_command(char *line)
{
char *argv[FINSH_CMD_SIZE + 1];
shell_cmd_func_t sc_cmd_func;
size_t argc_offset = 0;
size_t argc;
argc = line2argv(line, argv, FINSH_CMD_SIZE + 1);
if (!argc)
{
print_prompt();
return;
}
sc_cmd_func = get_cb(argc, argv);
if (!sc_cmd_func)
{
if (app_cmd_handler != NULL)
{
sc_cmd_func = app_cmd_handler;
}
else
{
console_printf("Unrecognized command: %s\n", argv[0]);
console_printf("Type 'help' for list of available commands\n");
print_prompt();
return;
}
}
/* Allow invoking a cmd with module name as a prefix; a command should
* not know how it was invoked (with or without prefix)
*/
if (default_module == -1 && sc_cmd_func != select_module &&
sc_cmd_func != show_help)
{
argc_offset = 1;
}
/* Execute callback with arguments */
if (sc_cmd_func(argc - argc_offset, &argv[argc_offset]) < 0)
{
show_cmd_help(argv);
}
print_prompt();
}

View File

@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2018-12-18 ZeroFree first implementation
*/
#include "nimble/nimble_npl.h"
#include "nrfx.h"
static void (*radio_isr_addr)(void);
static void (*rng_isr_addr)(void);
static void (*rtc0_isr_addr)(void);
void RADIO_IRQHandler(void)
{
radio_isr_addr();
}
void RNG_IRQHandler(void)
{
rng_isr_addr();
}
void RTC0_IRQHandler(void)
{
rtc0_isr_addr();
}
void ble_npl_hw_set_isr(int irqn, void (*addr)(void))
{
switch (irqn)
{
case RADIO_IRQn:
radio_isr_addr = addr;
break;
case RNG_IRQn:
rng_isr_addr = addr;
break;
case RTC0_IRQn:
rtc0_isr_addr = addr;
break;
}
}