zephyr/lib/cmsis_rtos_v2/event_flags.c

253 lines
5.4 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
headers: Refactor kernel and arch headers. This commit refactors kernel and arch headers to establish a boundary between private and public interface headers. The refactoring strategy used in this commit is detailed in the issue This commit introduces the following major changes: 1. Establish a clear boundary between private and public headers by removing "kernel/include" and "arch/*/include" from the global include paths. Ideally, only kernel/ and arch/*/ source files should reference the headers in these directories. If these headers must be used by a component, these include paths shall be manually added to the CMakeLists.txt file of the component. This is intended to discourage applications from including private kernel and arch headers either knowingly and unknowingly. - kernel/include/ (PRIVATE) This directory contains the private headers that provide private kernel definitions which should not be visible outside the kernel and arch source code. All public kernel definitions must be added to an appropriate header located under include/. - arch/*/include/ (PRIVATE) This directory contains the private headers that provide private architecture-specific definitions which should not be visible outside the arch and kernel source code. All public architecture- specific definitions must be added to an appropriate header located under include/arch/*/. - include/ AND include/sys/ (PUBLIC) This directory contains the public headers that provide public kernel definitions which can be referenced by both kernel and application code. - include/arch/*/ (PUBLIC) This directory contains the public headers that provide public architecture-specific definitions which can be referenced by both kernel and application code. 2. Split arch_interface.h into "kernel-to-arch interface" and "public arch interface" divisions. - kernel/include/kernel_arch_interface.h * provides private "kernel-to-arch interface" definition. * includes arch/*/include/kernel_arch_func.h to ensure that the interface function implementations are always available. * includes sys/arch_interface.h so that public arch interface definitions are automatically included when including this file. - arch/*/include/kernel_arch_func.h * provides architecture-specific "kernel-to-arch interface" implementation. * only the functions that will be used in kernel and arch source files are defined here. - include/sys/arch_interface.h * provides "public arch interface" definition. * includes include/arch/arch_inlines.h to ensure that the architecture-specific public inline interface function implementations are always available. - include/arch/arch_inlines.h * includes architecture-specific arch_inlines.h in include/arch/*/arch_inline.h. - include/arch/*/arch_inline.h * provides architecture-specific "public arch interface" inline function implementation. * supersedes include/sys/arch_inline.h. 3. Refactor kernel and the existing architecture implementations. - Remove circular dependency of kernel and arch headers. The following general rules should be observed: * Never include any private headers from public headers * Never include kernel_internal.h in kernel_arch_data.h * Always include kernel_arch_data.h from kernel_arch_func.h * Never include kernel.h from kernel_struct.h either directly or indirectly. Only add the kernel structures that must be referenced from public arch headers in this file. - Relocate syscall_handler.h to include/ so it can be used in the public code. This is necessary because many user-mode public codes reference the functions defined in this header. - Relocate kernel_arch_thread.h to include/arch/*/thread.h. This is necessary to provide architecture-specific thread definition for 'struct k_thread' in kernel.h. - Remove any private header dependencies from public headers using the following methods: * If dependency is not required, simply omit * If dependency is required, - Relocate a portion of the required dependencies from the private header to an appropriate public header OR - Relocate the required private header to make it public. This commit supersedes #20047, addresses #19666, and fixes #3056. Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2019-10-25 00:08:21 +09:00
#include <kernel.h>
#include <string.h>
#include "wrapper.h"
K_MEM_SLAB_DEFINE(cv2_event_flags_slab, sizeof(struct cv2_event_flags),
CONFIG_CMSIS_V2_EVT_FLAGS_MAX_COUNT, 4);
static const osEventFlagsAttr_t init_event_flags_attrs = {
.name = "ZephyrEvent",
.attr_bits = 0,
.cb_mem = NULL,
.cb_size = 0,
};
#define DONT_CARE (0)
#define NSEC_PER_MSEC (NSEC_PER_USEC * USEC_PER_MSEC)
/**
* @brief Create and Initialize an Event Flags object.
*/
osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
{
struct cv2_event_flags *events;
if (k_is_in_isr()) {
return NULL;
}
if (attr == NULL) {
attr = &init_event_flags_attrs;
}
if (k_mem_slab_alloc(&cv2_event_flags_slab, (void **)&events, K_MSEC(100))
== 0) {
memset(events, 0, sizeof(struct cv2_event_flags));
} else {
return NULL;
}
k_poll_signal_init(&events->poll_signal);
k_poll_event_init(&events->poll_event, K_POLL_TYPE_SIGNAL,
K_POLL_MODE_NOTIFY_ONLY, &events->poll_signal);
events->signal_results = 0U;
if (attr->name == NULL) {
strncpy(events->name, init_event_flags_attrs.name,
sizeof(events->name) - 1);
} else {
strncpy(events->name, attr->name, sizeof(events->name) - 1);
}
return (osEventFlagsId_t)events;
}
/**
* @brief Set the specified Event Flags.
*/
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
int key;
if ((ef_id == NULL) || (flags & 0x80000000)) {
return osFlagsErrorParameter;
}
key = irq_lock();
events->signal_results |= flags;
irq_unlock(key);
k_poll_signal_raise(&events->poll_signal, DONT_CARE);
return events->signal_results;
}
/**
* @brief Clear the specified Event Flags.
*/
uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
int key;
uint32_t sig;
if ((ef_id == NULL) || (flags & 0x80000000)) {
return osFlagsErrorParameter;
}
key = irq_lock();
sig = events->signal_results;
events->signal_results &= ~(flags);
irq_unlock(key);
return sig;
}
/**
* @brief Wait for one or more Event Flags to become signaled.
*/
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,
uint32_t options, uint32_t timeout)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
int retval, key;
uint32_t sig;
uint32_t time_delta_ms, timeout_ms = k_ticks_to_ms_floor64(timeout);
uint64_t time_stamp_start, hwclk_cycles_delta, time_delta_ns;
/* Can be called from ISRs only if timeout is set to 0 */
if (timeout > 0 && k_is_in_isr()) {
return osFlagsErrorUnknown;
}
if ((ef_id == NULL) || (flags & 0x80000000)) {
return osFlagsErrorParameter;
}
for (;;) {
time_stamp_start = (uint64_t)k_cycle_get_32();
switch (timeout) {
case 0:
retval = k_poll(&events->poll_event, 1, K_NO_WAIT);
break;
case osWaitForever:
retval = k_poll(&events->poll_event, 1, K_FOREVER);
break;
default:
retval = k_poll(&events->poll_event, 1,
K_MSEC(timeout_ms));
break;
}
switch (retval) {
case 0:
break;
case -EAGAIN:
return osFlagsErrorTimeout;
default:
return osFlagsErrorUnknown;
}
__ASSERT(events->poll_event.state == K_POLL_STATE_SIGNALED,
"event state not signalled!");
__ASSERT(events->poll_event.signal->signaled == 1U,
"event signaled is not 1");
/* Reset the states to facilitate the next trigger */
events->poll_event.signal->signaled = 0U;
events->poll_event.state = K_POLL_STATE_NOT_READY;
if (options & osFlagsWaitAll) {
/* Check if all events we are waiting on have
* been signalled
*/
if ((events->signal_results & flags) == flags) {
break;
}
/* If we need to wait on more signals, we need to
* adjust the timeout value accordingly based on
* the time that has already elapsed.
*/
hwclk_cycles_delta =
(uint64_t)k_cycle_get_32() - time_stamp_start;
time_delta_ns =
(uint32_t)k_cyc_to_ns_floor64(hwclk_cycles_delta);
time_delta_ms = (uint32_t)time_delta_ns / NSEC_PER_MSEC;
if (timeout_ms > time_delta_ms) {
timeout_ms -= time_delta_ms;
} else {
timeout_ms = 0U;
}
} else {
break;
}
}
sig = events->signal_results;
if (!(options & osFlagsNoClear)) {
/* Clear signal flags as the thread is ready now */
key = irq_lock();
events->signal_results &= ~(flags);
irq_unlock(key);
}
return sig;
}
/**
* @brief Get name of an Event Flags object.
*/
const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
if (!k_is_in_isr() && (ef_id != NULL)) {
return events->name;
} else {
return NULL;
}
}
/**
* @brief Get the current Event Flags.
*/
uint32_t osEventFlagsGet(osEventFlagsId_t ef_id)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
if (ef_id == NULL) {
return 0;
}
return events->signal_results;
}
/**
* @brief Delete an Event Flags object.
*/
osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id)
{
struct cv2_event_flags *events = (struct cv2_event_flags *)ef_id;
if (ef_id == NULL) {
return osErrorResource;
}
if (k_is_in_isr()) {
return osErrorISR;
}
/* The status code "osErrorParameter" (the value of the parameter
* ef_id is incorrect) is not supported in Zephyr.
*/
k_mem_slab_free(&cv2_event_flags_slab, (void *)&events);
return osOK;
}