The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
160 lines
3.9 KiB
C
160 lines
3.9 KiB
C
/*
|
|
* Copyright (c) 2019 Intel corporation
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/* Disable syscall tracing for all calls from this compilation unit to avoid
|
|
* undefined symbols as the macros are not expanded recursively
|
|
*/
|
|
#define DISABLE_SYSCALL_TRACING
|
|
|
|
#include <zephyr/init.h>
|
|
#include <string.h>
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/sys/util.h>
|
|
#include <zephyr/sys/atomic.h>
|
|
#include <tracing_core.h>
|
|
#include <tracing_buffer.h>
|
|
#include <tracing_backend.h>
|
|
|
|
#define TRACING_CMD_ENABLE "enable"
|
|
#define TRACING_CMD_DISABLE "disable"
|
|
|
|
#ifdef CONFIG_TRACING_BACKEND_UART
|
|
#define TRACING_BACKEND_NAME "tracing_backend_uart"
|
|
#elif defined CONFIG_TRACING_BACKEND_USB
|
|
#define TRACING_BACKEND_NAME "tracing_backend_usb"
|
|
#elif defined CONFIG_TRACING_BACKEND_POSIX
|
|
#define TRACING_BACKEND_NAME "tracing_backend_posix"
|
|
#elif defined CONFIG_TRACING_BACKEND_RAM
|
|
#define TRACING_BACKEND_NAME "tracing_backend_ram"
|
|
#else
|
|
#define TRACING_BACKEND_NAME ""
|
|
#endif
|
|
|
|
enum tracing_state {
|
|
TRACING_DISABLE = 0,
|
|
TRACING_ENABLE
|
|
};
|
|
|
|
static atomic_t tracing_state;
|
|
static atomic_t tracing_packet_drop_num;
|
|
static struct tracing_backend *working_backend;
|
|
|
|
#ifdef CONFIG_TRACING_ASYNC
|
|
#define TRACING_THREAD_NAME "tracing_thread"
|
|
|
|
static k_tid_t tracing_thread_tid;
|
|
static struct k_thread tracing_thread;
|
|
static struct k_timer tracing_thread_timer;
|
|
static K_SEM_DEFINE(tracing_thread_sem, 0, 1);
|
|
static K_THREAD_STACK_DEFINE(tracing_thread_stack,
|
|
CONFIG_TRACING_THREAD_STACK_SIZE);
|
|
|
|
static void tracing_thread_func(void *dummy1, void *dummy2, void *dummy3)
|
|
{
|
|
uint8_t *transferring_buf;
|
|
uint32_t transferring_length, tracing_buffer_max_length;
|
|
|
|
tracing_thread_tid = k_current_get();
|
|
|
|
tracing_buffer_max_length = tracing_buffer_capacity_get();
|
|
|
|
while (true) {
|
|
if (tracing_buffer_is_empty()) {
|
|
k_sem_take(&tracing_thread_sem, K_FOREVER);
|
|
} else {
|
|
transferring_length =
|
|
tracing_buffer_get_claim(
|
|
&transferring_buf,
|
|
tracing_buffer_max_length);
|
|
tracing_buffer_handle(transferring_buf,
|
|
transferring_length);
|
|
tracing_buffer_get_finish(transferring_length);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void tracing_thread_timer_expiry_fn(struct k_timer *timer)
|
|
{
|
|
k_sem_give(&tracing_thread_sem);
|
|
}
|
|
#endif
|
|
|
|
static void tracing_set_state(enum tracing_state state)
|
|
{
|
|
atomic_set(&tracing_state, state);
|
|
}
|
|
|
|
static int tracing_init(void)
|
|
{
|
|
|
|
tracing_buffer_init();
|
|
|
|
working_backend = tracing_backend_get(TRACING_BACKEND_NAME);
|
|
tracing_backend_init(working_backend);
|
|
|
|
atomic_set(&tracing_packet_drop_num, 0);
|
|
|
|
if (IS_ENABLED(CONFIG_TRACING_HANDLE_HOST_CMD)) {
|
|
tracing_set_state(TRACING_DISABLE);
|
|
} else {
|
|
tracing_set_state(TRACING_ENABLE);
|
|
}
|
|
|
|
#ifdef CONFIG_TRACING_ASYNC
|
|
k_timer_init(&tracing_thread_timer,
|
|
tracing_thread_timer_expiry_fn, NULL);
|
|
|
|
k_thread_create(&tracing_thread, tracing_thread_stack,
|
|
K_THREAD_STACK_SIZEOF(tracing_thread_stack),
|
|
tracing_thread_func, NULL, NULL, NULL,
|
|
K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT);
|
|
k_thread_name_set(&tracing_thread, TRACING_THREAD_NAME);
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
SYS_INIT(tracing_init, APPLICATION, 0);
|
|
|
|
#ifdef CONFIG_TRACING_ASYNC
|
|
void tracing_trigger_output(bool before_put_is_empty)
|
|
{
|
|
if (before_put_is_empty) {
|
|
k_timer_start(&tracing_thread_timer,
|
|
K_MSEC(CONFIG_TRACING_THREAD_WAIT_THRESHOLD),
|
|
K_NO_WAIT);
|
|
}
|
|
}
|
|
|
|
bool is_tracing_thread(void)
|
|
{
|
|
return (!k_is_in_isr() && (k_current_get() == tracing_thread_tid));
|
|
}
|
|
#endif
|
|
|
|
bool is_tracing_enabled(void)
|
|
{
|
|
return atomic_get(&tracing_state) == TRACING_ENABLE;
|
|
}
|
|
|
|
void tracing_cmd_handle(uint8_t *buf, uint32_t length)
|
|
{
|
|
if (strncmp(buf, TRACING_CMD_ENABLE, length) == 0) {
|
|
tracing_set_state(TRACING_ENABLE);
|
|
} else if (strncmp(buf, TRACING_CMD_DISABLE, length) == 0) {
|
|
tracing_set_state(TRACING_DISABLE);
|
|
}
|
|
}
|
|
|
|
void tracing_buffer_handle(uint8_t *data, uint32_t length)
|
|
{
|
|
tracing_backend_output(working_backend, data, length);
|
|
}
|
|
|
|
void tracing_packet_drop_handle(void)
|
|
{
|
|
atomic_inc(&tracing_packet_drop_num);
|
|
}
|