2018-10-30 15:23:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel_structs.h>
|
|
|
|
#include "wrapper.h"
|
|
|
|
|
|
|
|
K_MEM_SLAB_DEFINE(cv2_semaphore_slab, sizeof(struct cv2_sem),
|
2019-02-06 16:03:32 +08:00
|
|
|
CONFIG_CMSIS_V2_SEMAPHORE_MAX_COUNT, 4);
|
2018-10-30 15:23:44 +08:00
|
|
|
|
|
|
|
static const osSemaphoreAttr_t init_sema_attrs = {
|
|
|
|
.name = "ZephyrSem",
|
|
|
|
.attr_bits = 0,
|
|
|
|
.cb_mem = NULL,
|
|
|
|
.cb_size = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and Initialize a semaphore object.
|
|
|
|
*/
|
|
|
|
osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count,
|
2019-02-06 16:03:32 +08:00
|
|
|
const osSemaphoreAttr_t *attr)
|
2018-10-30 15:23:44 +08:00
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore;
|
|
|
|
|
|
|
|
if (k_is_in_isr()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr == NULL) {
|
|
|
|
attr = &init_sema_attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k_mem_slab_alloc(&cv2_semaphore_slab,
|
2019-02-06 16:03:32 +08:00
|
|
|
(void **)&semaphore, 100) == 0) {
|
2018-10-30 15:23:44 +08:00
|
|
|
(void)memset(semaphore, 0, sizeof(struct cv2_sem));
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_sem_init(&semaphore->z_semaphore, initial_count, max_count);
|
2019-02-06 16:41:37 +08:00
|
|
|
|
|
|
|
if (attr->name == NULL) {
|
|
|
|
strncpy(semaphore->name, init_sema_attrs.name,
|
|
|
|
sizeof(semaphore->name) - 1);
|
|
|
|
} else {
|
|
|
|
strncpy(semaphore->name, attr->name,
|
|
|
|
sizeof(semaphore->name) - 1);
|
|
|
|
}
|
2018-10-30 15:23:44 +08:00
|
|
|
|
|
|
|
return (osSemaphoreId_t)semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Wait until a semaphore becomes available.
|
|
|
|
*/
|
|
|
|
osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
|
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore = (struct cv2_sem *) semaphore_id;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (semaphore_id == NULL) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can be called from ISRs only if timeout is set to 0 */
|
|
|
|
if (timeout > 0 && k_is_in_isr()) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == osWaitForever) {
|
|
|
|
status = k_sem_take(&semaphore->z_semaphore, K_FOREVER);
|
2019-03-27 09:57:45 +08:00
|
|
|
} else if (timeout == 0U) {
|
2018-10-30 15:23:44 +08:00
|
|
|
status = k_sem_take(&semaphore->z_semaphore, K_NO_WAIT);
|
|
|
|
} else {
|
|
|
|
status = k_sem_take(&semaphore->z_semaphore,
|
2019-02-06 16:03:32 +08:00
|
|
|
__ticks_to_ms(timeout));
|
2018-10-30 15:23:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status == -EBUSY) {
|
|
|
|
return osErrorResource;
|
|
|
|
} else if (status == -EAGAIN) {
|
|
|
|
return osErrorTimeout;
|
|
|
|
} else {
|
|
|
|
return osOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t osSemaphoreGetCount(osSemaphoreId_t semaphore_id)
|
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore = (struct cv2_sem *)semaphore_id;
|
|
|
|
|
|
|
|
if (semaphore_id == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return k_sem_count_get(&semaphore->z_semaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release a semaphore that was obtained by osSemaphoreWait.
|
|
|
|
*/
|
|
|
|
osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)
|
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore = (struct cv2_sem *) semaphore_id;
|
|
|
|
|
|
|
|
if (semaphore_id == NULL) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All tokens have already been released */
|
|
|
|
if (k_sem_count_get(&semaphore->z_semaphore) ==
|
2019-02-06 16:03:32 +08:00
|
|
|
semaphore->z_semaphore.limit) {
|
2018-10-30 15:23:44 +08:00
|
|
|
return osErrorResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_sem_give(&semaphore->z_semaphore);
|
|
|
|
|
|
|
|
return osOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete a semaphore that was created by osSemaphoreCreate.
|
|
|
|
*/
|
|
|
|
osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
|
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore = (struct cv2_sem *)semaphore_id;
|
|
|
|
|
|
|
|
if (semaphore_id == NULL) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k_is_in_isr()) {
|
|
|
|
return osErrorISR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The status code "osErrorResource" (the semaphore specified by
|
|
|
|
* parameter semaphore_id is in an invalid semaphore state) is not
|
|
|
|
* supported in Zephyr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
k_mem_slab_free(&cv2_semaphore_slab, (void *) &semaphore);
|
|
|
|
|
|
|
|
return osOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *osSemaphoreGetName(osSemaphoreId_t semaphore_id)
|
|
|
|
{
|
|
|
|
struct cv2_sem *semaphore = (struct cv2_sem *)semaphore_id;
|
|
|
|
|
|
|
|
if (!k_is_in_isr() && (semaphore_id != NULL)) {
|
|
|
|
return semaphore->name;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|