rel_1.6.0 init

This commit is contained in:
guocheng.kgc 2020-06-18 20:06:52 +08:00 committed by shengdong.dsd
commit 27b3e2883d
19359 changed files with 8093121 additions and 0 deletions

View file

@ -0,0 +1,11 @@
menu "ESPOS"
config ESPOS
bool "Enable Espressif RTOS abstract API"
default y
help
If this feature is enabled, system will use Espressif RTOS abstract APIs instead of RTOS real APIs.
This option is not currently used anywhere.
endmenu

View file

@ -0,0 +1,14 @@
NAME := espos
GLOBAL_INCLUDES += include
$(NAME)_SOURCES := \
platform/rhino/espos_misc.c \
platform/rhino/espos_mutex.c \
platform/rhino/espos_queue.c \
platform/rhino/espos_scheduler.c \
platform/rhino/espos_semaphore.c \
platform/rhino/espos_spinlock.c \
platform/rhino/espos_task.c \
platform/rhino/espos_time.c \
platform/rhino/espos_timer.c

View file

@ -0,0 +1,26 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_ARCH_H_
#define _ESPOS_ARCH_H_
#if defined(ESPOS_FOR_ESP32)
#include "espos_esp32.h"
#elif defined(ESPOS_FOR_ESP8266)
#include "espos_esp8266.h"
#else
#error "no espressif platform"
#endif
#endif

View file

@ -0,0 +1,110 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_ESP32_H_
#define _ESPOS_ESP32_H_
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef CONFIG_FREERTOS_UNICORE
#define ESPOS_PROCESSORS_NUM 2u
#define CONFIG_ESPOS_SMP
#else
#define ESPOS_PROCESSORS_NUM 1u
#endif
typedef struct espos_spinlock_arch {
int lock;
} espos_spinlock_arch_t;
#define ESPOS_SPINLOCK_ARCH_UNLOCK_INITIALIZER \
{ \
0, \
}
static inline int espos_get_core_id(void)
{
int id;
__asm__ __volatile__ (
" rsr.prid %0\n"
" extui %0, %0, 13, 1\n"
:"=r"(id));
return id;
}
static inline void espos_spinlock_arch_init(espos_spinlock_arch_t *lock)
{
lock->lock = 0;
}
static inline void espos_spinlock_arch_deinit(espos_spinlock_arch_t *lock)
{
lock->lock = 0;
}
static inline void espos_spinlock_arch_lock(espos_spinlock_arch_t *lock)
{
unsigned long tmp;
__asm__ __volatile__(
" movi %0, 0\n"
" wsr %0, scompare1\n"
"1: movi %0, 1\n"
" s32c1i %0, %1, 0\n"
" bnez %0, 1b\n"
: "=&a" (tmp)
: "a" (&lock->lock)
: "memory");
}
static inline int espos_spinlock_arch_trylock(espos_spinlock_arch_t *lock)
{
unsigned long tmp;
__asm__ __volatile__(
" movi %0, 0\n"
" wsr %0, scompare1\n"
" movi %0, 1\n"
" s32c1i %0, %1, 0\n"
: "=&a" (tmp)
: "a" (&lock->lock)
: "memory");
return tmp == 0 ? 0 : 1;
}
static inline void espos_spinlock_arch_unlock(espos_spinlock_arch_t *lock)
{
unsigned long tmp;
__asm__ __volatile__(
" movi %0, 0\n"
" s32ri %0, %1, 0\n"
: "=&a" (tmp)
: "a" (&lock->lock)
: "memory");
}
#ifdef __cplusplus
}
#endif
#endif /* _ESPOS_ESP32_H_ */

View file

@ -0,0 +1,57 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_ESP8266_H_
#define _ESPOS_ESP8266_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define ESPOS_PROCESSORS_NUM 1u
#define ESPOS_SPINLOCK_ARCH_UNLOCK_INITIALIZER \
{ \
0, \
}
typedef int32_t esp_err_t;
typedef struct espos_spinlock_arch {
int lock;
} espos_spinlock_arch_t;
static inline uintptr_t espos_suspend_interrupt(void)
{
uintptr_t state;
__asm__ volatile ("RSIL %0, 5" : "=a" (state) :: "memory");
return state;
}
static inline void espos_resume_interrupt(uintptr_t state)
{
__asm__ volatile ("WSR %0, ps" :: "a" (state) : "memory");
}
#define espos_get_core_id() 0
#ifdef __cplusplus
}
#endif
#endif /* _ESPOS_ESP8266_H_ */

View file

@ -0,0 +1,17 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef _ESPOS_ERRNO_H_
#define _ESPOS_ERRNO_H_
#include <sys/errno.h>
#if defined(ESPOS_FOR_ESP32)
#include "esp_err.h"
#elif defined(ESPOS_FOR_ESP8266)
#endif
#endif

View file

@ -0,0 +1,20 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef ESPOS_INCLUDE_H
#define ESPOS_INCLUDE_H
#include "espos_errno.h"
#include "espos_mutex.h"
#include "espos_queue.h"
#include "espos_scheduler.h"
#include "espos_semaphore.h"
#include "espos_spinlock.h"
#include "espos_task.h"
#include "espos_time.h"
#include "espos_timer.h"
#include "espos_types.h"
#endif /* ESPOS_INCLUDE_H */

View file

@ -0,0 +1,142 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_MUTEX_H_
#define _ESPOS_MUTEX_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_time.h"
#ifdef __cplusplus
extern "C" {
#endif
/* use default option to create mutex */
typedef enum espos_mutex_opt {
/* AT this mode, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock.
*
* If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked,
* undefined behavior results.
*/
ESPOS_MUTEX_NORMAL = 0,
ESPOS_MUTEX_RECURSIVE,
ESPOS_MUTEX_TYPE_MAX
} espos_mutex_type_t;
#define ESPOS_MUTEX_TYPE(type) (type & 0xFF)
/**
* @brief create a mutex
*
* @param mutex mutex handle point
* @param opt mutex option, if you don't know how to do, just use "ESPOS_MUTEX_NORMAL" here
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINTR : you do this at interrupt state, and it is not supported
* -EINVAL : input parameter error
*/
esp_err_t espos_mutex_create(espos_mutex_t *mutex, espos_opt_t opt);
/**
* @brief set a mutex name
*
* @param mutex mutex handle
* @param name mutex's name
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_mutex_set_name(espos_mutex_t mutex, const char *name);
/**
* @brief lock a mutex
*
* @param mutex mutex handle
* @param wait_ticks sleep for system ticks if the locked mutex is not unlocked. Otherwise if someone
* else unlock the locked mutex, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY"
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
* -ETIMEDOUT : timeout and you have not locked it
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
esp_err_t espos_mutex_lock(espos_mutex_t mutex, espos_tick_t wait_ticks);
/**
* @bref try to lock a mutex and it will return immediately without being blocked
*
* @param m mutex handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
*/
#define espos_mutex_trylock(m) espos_mutex_lock(m, ESPOS_NO_DELAY)
/**
* @brief unlock a mutex
*
* @param mutex mutex handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
* -EPERM : current task don't lock the mutex
*/
esp_err_t espos_mutex_unlock(espos_mutex_t mutex);
/**
* @brief get task handle which lock the mutex
*
* @param mutex mutex handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
espos_task_t espos_mutex_get_holder(espos_mutex_t mutex);
/**
* @brief delete the mutex
*
* @param mutex mutex handle
*
* @return the result
* 0 : successful
* -EINTR : you do this at interrupt state, and it is not supported
* -EINVAL : input parameter error
*
* @note if low-level module is YunOS, this function will awake all task blocked at the mutex
*/
esp_err_t espos_mutex_del(espos_mutex_t mutex);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,212 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_QUEUE_H_
#define _ESPOS_QUEUE_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_time.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum espos_queue_pos {
/* send message to the front of the queue */
ESPOS_QUEUE_SEND_FRONT = 0,
/* send message to the back of the queue */
ESPOS_QUEUE_SEND_BACK,
ESPOS_QUEUE_POS_MAX
} espos_queue_pos_t;
typedef enum espos_queue_send_opt {
/* send message with normal option */
ESPOS_QUEUE_SEND_OPT_NORMAL = 0,
ESPOS_QUEUE_SEND_OPT_MAX
} espos_queue_send_opt_t;
typedef enum espos_queue_recv_opt {
/* receive message with normal option */
ESPOS_QUEUE_RECV_OPT_NORMAL = 0,
ESPOS_QUEUE_RECV_OPT_MAX
} espos_queue_recv_opt_t;
/**
* @brief create a queue
*
* @param queue queue handle point
* @param msg_len queue internal message length (bytes)
* @param queue_len queue internal message maximum number
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINTR : you do this at interrupt state, and it is not supported
* -EINVAL : input parameter error
*
* @note all input and out message length is fixedly "msg_len"
*/
esp_err_t espos_queue_create(espos_queue_t *queue, espos_size_t msg_len, espos_size_t queue_len);
/**
* @brief set a queue name
*
* @param queue queue handle
* @param name queue's name
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_queue_set_name(espos_queue_t queue, const char *name);
/**
* @brief send a message to the queue, it is suggested that you had better not use "espos_queue_send" directly,
* please use "espos_queue_send_front" or "espos_queue_send_back"
*
* @param queue queue handle
* @param msg message point
* @param wait_ticks sleep for system ticks if the queue is full. Otherwise if queue is not full, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY"
* @param pos position where sending the message
* ESPOS_QUEUE_SEND_FRONT : send message to the front of the queue
* ESPOS_QUEUE_SEND_BACK : send message to the back of the queue
* @param opt sending option
* ESPOS_QUEUE_SEND_OPT_NORMAL : wake up blocked task
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and the queue is full
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
esp_err_t espos_queue_send_generic(espos_queue_t queue, void *msg, espos_tick_t wait_ticks, espos_pos_t pos, espos_opt_t opt);
/**
* @brief send a message to the front of the queue
*
* @param q queue handle
* @param m message point
* @param t sleep for system ticks if the queue is full. Otherwise if queue is not full, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY"
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and the queue is full
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
#define espos_queue_send_front(q, m, t) espos_queue_send_generic(q, m, t, ESPOS_QUEUE_SEND_FRONT, ESPOS_QUEUE_SEND_OPT_NORMAL)
/**
* @brief send a message to the back of the queue
*
* @param q queue handle
* @param m message point
* @param t sleep for system ticks if the queue is full. Otherwise if queue is not full, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY"
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and the queue is full
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
#define espos_queue_send(q, m, t) espos_queue_send_generic(q, m, t, ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL)
/**
* @brief receive a message of the queue
*
* @param queue queue handle
* @param msg message point
* @param wait_ticks sleep for system ticks if the queue is empty. Otherwise if queue is not empty, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY", at CPU ISR mode, it is forced to be 0
* @param opt queue sending option
* ESPOS_QUEUE_RECV_OPT_NORMAL : use wait_ticks to check if it is need be blocked
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and the queue is empty
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
esp_err_t espos_queue_recv_generic(espos_queue_t queue, void *msg, espos_tick_t wait_ticks, espos_opt_t opt);
/**
* @brief receive a message of the queue with normal option
*
* @param queue queue handle
* @param msg message point
* @param wait_ticks sleep for system ticks if the queue is empty. Otherwise if queue is not empty, you will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY", at CPU ISR mode, it is forced to be 0
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and the queue is empty
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
#define espos_queue_recv(q, m, t) espos_queue_recv_generic(q, m, t, ESPOS_QUEUE_RECV_OPT_NORMAL)
/**
* @brief get current message number of the queue
*
* @param queue queue handle
*
* @return current message number of the queue
*/
espos_size_t espos_queue_msg_waiting(espos_queue_t queue);
/**
* @brief reset the queue
*
* @param queue queue handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*
* @note if low-level module is YunOS, the function will not awake the tasks which is blocked at the queue
*/
esp_err_t espos_queue_flush(espos_queue_t queue);
/**
* @brief delete the queue
*
* @param queue queue handle
*
* @return the result
* 0 : successful
* -EINTR : you do this at interrupt state, and it is not supported
* -EINVAL : input parameter error
*
* @note if low-level module is YunOS, this function will awake all task blocked at the mutex
*/
esp_err_t espos_queue_del(espos_queue_t queue);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,245 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_SCHEDULER_H_
#define _ESPOS_SCHEDULER_H_
#include <stdbool.h>
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_spinlock.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* ESPOS state
*/
typedef enum espos_stat {
ESPOS_IS_NOT_STARTED = 0,
ESPOS_IS_RUNNING,
ESPOS_IS_SUSPENDED
} espos_stat_t;
/************************************ internal function ************************************/
/**
* @brief enter ESPOS system critical state
*
* @param spinlock spinlock handle point
*
* @return critical state temple variable(used by "espos_exit_critical")
*/
espos_critical_t _espos_enter_critical(espos_spinlock_t *spinlock);
/**
* @brief exit ESPOS system critical state
*
* @param spinlock spinlock handle point
* @param tmp critical state temple variable(created by "espos_enter_critical")
*
* @return none
*/
void _espos_exit_critical(espos_spinlock_t *spinlock, espos_critical_t tmp);
/*******************************************************************************************/
/**
* @brief initialize ESPOS system
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
*/
esp_err_t espos_init(void);
/**
* @brief start ESPOS system
*
* @return the result
* 0 : successful
* -EPERM : failed (it will never happen in a general way)
*/
esp_err_t espos_start(void);
/**
* @brief start ESPOS system CPU port
*
* @param port CPU port ID
*
* @return the result
* 0 : successful
* -EPERM : failed (it will never happen in a general way)
*/
esp_err_t espos_start_port(int port);
/**
* @brief get espos system state
*
* @return the state
* ESPOS_IS_NOT_STARTED : ESPOS is not started
* ESPOS_IS_RUNNING : ESPOS is running
* ESPOS_IS_SUSPENDED : ESPOS is suspended
*/
espos_stat_t espos_sched_state_get(void);
/**
* @brief declare espos critical temp data
*/
#define espos_declare_critical(t) espos_critical_t t
/**
* @brief enter ESPOS system critical state
*
* @param sl spinlock handle
* @param t critical state
*
* @return critical state temple variable(used by "espos_exit_critical")
*/
#define espos_enter_critical(t, sl) (t) = _espos_enter_critical(&(sl))
/**
* @brief exit ESPOS system critical state
*
* @param t critical state temple variable(created by "espos_enter_critical")
* @param sl spinlock handle point
*
* @return none
*/
#define espos_exit_critical(t, sl) _espos_exit_critical(&(sl), t)
/**
* @brief OS internal function enter ESPOS system critical state
*
* @param sl spinlock handle
* @param t critical state
*
* @return critical state temple variable(used by "espos_exit_critical")
*
* @note: ESPOS is application level OS API, so it means all APIs call internal
* real OS's function, so if OS's or its core hardware's functions want
* to call ESPOS, loop nesting will occur, so we should tell ESPOS it's
* at OS internal state now.
*/
#define espos_os_enter_critical(t, sl) \
espos_os_enter(); \
(t) = _espos_enter_critical(&(sl))
/**
* @brief OS internal function exit ESPOS system critical state
*
* @param t critical state temple variable(created by "espos_enter_critical")
* @param sl spinlock handle point
*
* @return none
*
* @note: ESPOS is application level OS API, so it means all APIs call internal
* real OS's function, so if OS's or its core hardware's functions want
* to call ESPOS, loop nesting will occur, so we should tell ESPOS it's
* at OS internal state now.
*/
#define espos_os_exit_critical(t, sl) \
_espos_exit_critical(&(sl), t); \
espos_os_exit()
/**
* @brief suspend the local CPU core preempt and the current task
* owned by local CPU core will not be preempted
*
* @return the result
* 0 : successful
* -EAGAIN : preempt suspending maximum number is reached, and fail to do it
* -EINTR : you do this at interrupt state, and it is not supported
*
* @note the function can be used nested
*/
esp_err_t espos_preempt_suspend_local(void);
/**
* @brief resume the local CPU core preempt
*
* @return the result
* 0 : successful
* -EAGAIN : preempt resuming maximum number is reached, and fail to do it
* -EINTR : you do this at interrupt state, and it is not supported
*
* @note the function can be used nested
*/
esp_err_t espos_preempt_resume_local(void);
/**
* @brief enter system interrupt server, the function must be used after entering hardware interrupt
*
* @return the result
* 0 : successful
* -EAGAIN : nested count is reached
*
* @note the function can be used nested
*/
esp_err_t espos_isr_enter (void);
/**
* @brief exit system interrupt server, the function must be used before exiting hardware interrupt
*
* @return none
*
* @note the function can be used nested
*/
void espos_isr_exit(void);
/**
* @brief check if the CPU is at ISR state
*
* @param none
*
* @return true if the CPU is at ISR state or false
*/
bool espos_in_isr(void);
/**
* @brief mark it enters real OS interal function
*
* @return the result
* 0 : successful
* -EAGAIN : nested count is reached
*
* @note the function can be used nested
*/
esp_err_t espos_os_enter(void);
/**
* @brief remove mark it enters real OS interal function
*
* @param none
*
* @return none
*/
void espos_os_exit(void);
/**
* @brief check if the function is at real OS internal fucntion
*
* @param none
*
* @return true if the CPU is at real OS internal fucntion or false
*/
bool espos_os_isr(void);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,119 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_SEMAPHORE_H_
#define _ESPOS_SEMAPHORE_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_time.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief create a semaphore
*
* @param sem semaphore handle point
* @param max_count semaphore maximum count
* @param init_count semaphore initialized count
*
* @return the result
* 0 : successful
* ENOMEM : no enough memory
* EINVAL : input parameter error
*
* @note if low-level module is YunOS, "max_count" does not work, and the reachable count is "0xFFFFFFFF"
*/
esp_err_t espos_sem_create(espos_sem_t *sem, espos_sem_count_t max_count, espos_sem_count_t init_count);
/**
* @brief set a semaphore name
*
* @param semaphore semaphore handle
* @param name semaphore's name
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_sem_set_name(espos_sem_t sem, const char *name);
/**
* @brief take semaphore
*
* @param sem semaphore handle
* @param wait_ticks sleep for system ticks if the semaphore is empty. Otherwise if semaphore is given,
* oldest blocked task will wake up.
* maximum time is "ESPOS_MAX_DELAY", no time is "ESPOS_NO_DELAY"
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : timeout and not take semaphore
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
esp_err_t espos_sem_take(espos_sem_t sem, espos_tick_t wait_ticks);
/**
* @brief try to take semaphore
*
* @param s semaphore handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ETIMEDOUT : not take semaphore
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
#define espos_sem_trytake(s) espos_sem_take(s, ESPOS_NO_DELAY)
/**
* @brief give up semaphore
*
* @param sem semaphore handle
* @param wait_ticks no meaning
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -EAGAIN : the maximum count is reached
*
* @note you can transform the millisecond to ticks by "espos_ms_to_ticks"
*/
esp_err_t espos_sem_give(espos_sem_t sem);
/**
* @brief delete the semaphore
*
* @param sem semaphore handle
*
* @return the result
* 0 : successful
* -EINTR : you do this at interrupt state, and it is not supported
* -EINVAL : input parameter error
*
* @note if low-level module is YunOS, this function will awake all task blocked here
*/
esp_err_t espos_sem_del(espos_sem_t sem);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,128 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_SPINLOCK_H_
#define _ESPOS_SPINLOCK_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_task.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct espos_spinlock {
espos_spinlock_arch_t lock;
espos_task_t holder;
} espos_spinlock_t;
#define ESPOS_SPINLOCK_UNLOCK_INITIALIZER \
{ \
ESPOS_SPINLOCK_ARCH_UNLOCK_INITIALIZER, \
ESPOS_OBJ_NONE, \
}
#define ESPOS_DEFINE_SPINLOCK(l) \
espos_spinlock_t l = ESPOS_SPINLOCK_UNLOCK_INITIALIZER
#define ESPOS_DEFINE_STATIC_SPINLOCK(l) \
static espos_spinlock_t l = ESPOS_SPINLOCK_UNLOCK_INITIALIZER
/**
* @brief create a spinlock
*
* @param spinlock spinlock handle point
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_create (espos_spinlock_t *spinlock);
/**
* @brief set a spinlock name
*
* @param spinlock spinlock handle
* @param name spinlock's name
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_set_name(espos_spinlock_t *spinlock, const char *name);
/**
* @brief spin to lock a spinlock until successfully
*
* @param spinlock spinlock handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_lock(espos_spinlock_t *spinlock);
/**
* @brief try to lock a spinlock
*
* @param spinlock spinlock handle
*
* @return the result
* 0 : successful
* -EAGAIN : no spinlock is valid
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_trylock(espos_spinlock_t *spinlock);
/**
* @brief get spinlock holder task handle
*
* @param spinlock spinlock handle
*
* @return holder task handle
*/
espos_task_t espos_spinlock_get_holder(espos_spinlock_t *spinlock);
/**
* @brief unlock a spinlock
*
* @param spinlock spinlock handle
*
* @return the result
* 0 : successful
* -EAGAIN : no spinlock is locked
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_unlock(espos_spinlock_t *spinlock);
/**
* @brief delete a spinlock
*
* @param spinlock spinlock handle
*
* @return the result
* 0 : successful
* -EACCES : failed to do it with some special reason
* -EINVAL : input parameter error
*/
esp_err_t espos_spinlock_del(espos_spinlock_t *spinlock);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,209 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_TASK_H_
#define _ESPOS_TASK_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_time.h"
#ifdef __cplusplus
extern "C" {
#endif
/* task will run at any CPU */
#define ESPOS_TASK_NO_AFFINITY -1
/* create task and start it automatically */
#define ESPOS_TASK_CREATE_NORMAL 0
/* just create task and don't start it */
#define ESPOS_TASK_CREATE_SILENCE 1
/* task delete itself */
#define ESPOS_TASK_DELETE_SELF 0
/*
* task priority numbers, we use 26 here because FreeRTOS uses 26 but YunOS uses 62,
* we should use minimum one of them to be compatible with the current application
*/
#ifndef ESPOS_TASK_PRIO_NUM
#define ESPOS_TASK_PRIO_NUM espos_task_prio_num()
#endif
/* task maximum priority number */
#define ESPOS_TASK_PRIO_MAX (ESPOS_TASK_PRIO_NUM - 1)
/**
* @brief create a task
*
* @param task task handle point
* @param name task name, if name is NULL, we will use "default_task" default
* @param arg task entry function inputting parameter
* @param prio task priority
* @param ticks task time slice
* @param stack_size task stack size, its unit is "byte"
* @param entry task entry function
* @param opt task option
* ESPOS_TASK_CREATE_NORMAL : the created task will be started automatically
* ESPOS_TASK_CREATE_SILENCE : the created task will not be started automatically
* @param cpu_id task CPU id
* natural number : the task only runs at the CPU of the number
* ESPOS_TASK_NO_AFFINITY : the task runs at any CPU
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
*/
esp_err_t espos_task_create_on_cpu(espos_task_t *task, const char *name, void *arg, espos_prio_t prio,
espos_tick_t ticks, espos_size_t stack_size, espos_task_entry_t entry, espos_opt_t opt, espos_cpu_t cpu_id);
/**
* @brief create a task and set its CPU id to be "ESPOS_TASK_NO_AFFINITY"
*
* @param task task handle point
* @param name task name, if name is NULL, we will use "default_task" default
* @param arg task entry function inputting parameter
* @param prio task priority
* @param ticks task time slice
* @param stack_size task stack size, its unit is "byte"
* @param entry task entry function
* @param opt task option
* ESPOS_TASK_CREATE_NORMAL : the created task will be started automatically
* ESPOS_TASK_CREATE_SILENCE : the created task will not be started automatically
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
*/
#define espos_task_create(task, name, arg, prio, ticks, stack_size, entry, opt) \
espos_task_create_on_cpu(task, name, arg, prio, ticks, stack_size, entry, opt, ESPOS_TASK_NO_AFFINITY)
/**
* @brief delete a task
*
* @param task task handle
* ESPOS_TASK_DELETE_SELF : task will delete itself
*
* @return the result
* 0 : successful
* EACCES : failed to do it with some special reason
*/
esp_err_t espos_task_del(espos_task_t task);
/**
* @brief let current task sleep for some ticks
*
* @param delay_ticks system ticks
*
* @return the result
* 0 : successful
* EINVAL : input parameter error
*/
esp_err_t espos_task_delay(const espos_tick_t delay_ticks);
/**
* @brief suspend target task
*
* @param task task handle
*
* @return the result
* 0 : successful
* EINVAL : input parameter error
*/
esp_err_t espos_task_suspend(espos_task_t task);
/**
* @brief resume target task
*
* @param task task handle
*
* @return the result
* 0 : successful
* EINVAL : input parameter error
*/
esp_err_t espos_task_resume(espos_task_t task);
/**
* @brief yield the cpu once
*
* @return the result
* 0 : successful
* EACCES : failed to do it with some special reason
*/
esp_err_t espos_task_yield(void);
/**
* @brief get current task handle
*
* @return current task handle
*/
espos_task_t espos_task_get_current(void);
/**
* @brief get current task name point
*
* @param task task handle
* @param pname pointing at name's point
*
* @return current task handle
*/
esp_err_t espos_task_get_name (espos_task_t task, char **pname);
/**
* @brief set task private data
*
* @param task task handle
* @param idx task data index
* @param info task private data
*
* @return the result
* 0 : successful
* EINVAL : input parameter error
*/
esp_err_t espos_task_set_private_data(espos_task_t task, int idx, void *info);
/**
* @brief get task private data
*
* @param task task handle
* @param idx task data index
* @param info task private data point
*
* @return the result
* 0 : successful
* EINVAL : input parameter error
*/
esp_err_t espos_task_get_private_data(espos_task_t task, int idx, void **info);
/**
* @brief get CPU affinity of task
*
* @return CPU affinity of task
*/
espos_cpu_t espos_task_get_affinity(espos_task_t task);
size_t espos_task_prio_num(void);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,69 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_TIME_H_
#define _ESPOS_TIME_H_
#include "espos_types.h"
#include "espos_errno.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* delay or wait for ticks, it is used by mutex, queue,
* semaphore, timer and so on
*/
/* no delay for ticks, function will return immediately */
#define ESPOS_NO_DELAY 0u
/* delay forever, function will never return until event triggers */
#define ESPOS_MAX_DELAY 0xffffffffu
espos_time_t espos_get_tick_per_ms(void);
/**
* @brief get current system ticks
*
* @return current ticks
*/
espos_tick_t espos_get_tick_count(void);
/**
* @brief transform milliseconds to system ticks
*
* @param ms milliseconds
*
* @return system ticks
*
* @note the function discards the shortage of digits, for example:
* 20ms -> 2 ticks ; 21ms -> 2 ticks; 29 -> 2 ticks
*/
espos_tick_t espos_ms_to_ticks(espos_time_t ms);
/**
* @brief transform system ticks to milliseconds
*
* @param ticks system ticks
*
* @return milliseconds
*/
espos_time_t espos_ticks_to_ms(espos_tick_t ticks);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,174 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#ifndef _ESPOS_TIMER_H_
#define _ESPOS_TIMER_H_
#include "espos_types.h"
#include "espos_errno.h"
#include "espos_time.h"
#ifdef __cplusplus
extern "C" {
#endif
/* timer just run once after starting it */
#define ESPOS_TIMER_NO_AUTO_RUN 0
/* timer run cycle after starting it */
#define ESPOS_TIMER_AUTO_RUN (1 << 0)
/* timer configuration command */
typedef enum espos_timer_cmd {
/* configure time's period */
ESPOS_TIMER_CHANGE_PERIOD = 0,
/* configure time to be "once" */
ESPOS_TIMER_CHANGE_ONCE,
/* configure time to be "auto" */
ESPOS_TIMER_CHANGE_AUTO,
ESPOS_TIMER_CMD_MAX
} espos_timer_cmd_t;
/**
* @brief create a timer
*
* @param timer timer handle point
* @param name timer name, if name is NULL, we will use "default_timer" default
* @param cb timer callback function
* @param arg timer entry function inputting parameter
* @param period_ticks timer period ticks
* @param opt timer option
* ESPOS_TIMER_NO_AUTO_RUN : created timer will run once, even if it is started,
* ESPOS_TIMER_AUTO_RUN : created timer will run automatically
*
* @return the result
* 0 : successful
* -ENOMEM : no enough memory
* -EINVAL : input parameter error
* -EINTR : you do this at interrupt state, and it is not supported
*
* @note after starting the created timer by "espos_timer_start", it will only run.
*/
esp_err_t espos_timer_create(espos_timer_t *timer, const char *name, espos_timer_cb_t cb, void *arg,
espos_tick_t period_ticks, espos_opt_t opt);
/**
* @brief start a timer
*
* @param timer timer handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*
* @note after starting the created timer by "espos_timer_start", it will only run
*/
esp_err_t espos_timer_start(espos_timer_t timer);
/**
* @brief stop a timer
*
* @param timer timer handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*
* @note the timer should be started again, if it is stopped
*/
esp_err_t espos_timer_stop(espos_timer_t timer);
/**
* @brief configure a timer
*
* @param timer timer handle
* @param opt timer option command
* @param arg timer parameter
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*/
esp_err_t espos_timer_change(espos_timer_t timer, espos_opt_t opt, void *arg);
/**
* @brief configure period of timer
*
* @param t timer handle
* @param a timer period
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*/
#define espos_timer_set_period(t, a) espos_timer_change(t, ESPOS_TIMER_CHANGE_PERIOD, (void *)a)
/**
* @brief configure timer to be "once"
*
* @param t timer handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*/
#define espos_timer_set_once(t) espos_timer_change(t, ESPOS_TIMER_CHANGE_ONCE, NULL)
/**
* @brief configure timer to be "auto"
*
* @param t timer handle
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
* -ECANCELED : failed for some reason depend on low-level OS
*/
#define espos_timer_set_auto(t) espos_timer_change(t, ESPOS_TIMER_CHANGE_AUTO, NULL)
/**
* @brief delete a timer
*
* @param t timer handle
*
* @return the result
* 0 : successful
* -EACCES : failed to do it with some special reason
* -EINTR : you do this at interrupt state, and it is not supported
*/
esp_err_t espos_timer_del(espos_timer_t timer);
/**
* @brief get current timer name point
*
* @param timer timer handle
* @param pname pointing at name's point
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_get_timer_name (espos_timer_t timer, char **pname);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef _ESPOS_TYPES_H_
#define _ESPOS_TYPES_H_
#include <stdint.h>
#include <stddef.h>
#include "arch/espos_arch.h"
#define ESPOS_OBJ_NONE 0
#ifdef ARCH64
typedef uint64_t espos_obj_t;
#else
typedef uint32_t espos_obj_t;
#endif
typedef espos_obj_t espos_task_t;
typedef espos_obj_t espos_queue_t;
typedef espos_obj_t espos_mutex_t;
typedef espos_obj_t espos_sem_t;
typedef espos_obj_t espos_timer_t;
typedef espos_obj_t espos_critical_t;
typedef size_t espos_size_t;
typedef size_t espos_pos_t;
typedef uint8_t espos_prio_t;
typedef uint32_t espos_opt_t;
typedef int32_t espos_cpu_t;
typedef espos_size_t espos_tick_t;
typedef espos_size_t espos_time_t;
typedef espos_size_t espos_sem_count_t;
typedef void (*espos_task_entry_t)(void *p);
typedef void (*espos_timer_cb_t)(espos_timer_t timer, void *arg);
#endif

View file

@ -0,0 +1,13 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef ESPOS_ERR_MAP_H
#define ESPOS_ERR_MAP_H
#define IRAM_ATTR __attribute__((section(".text")))
int espos_err_map (kstat_t err_code);
#endif

View file

@ -0,0 +1,22 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "k_api.h"
#include "espos_err.h"
int IRAM_ATTR espos_err_map (kstat_t err_code)
{
if (err_code == RHINO_SUCCESS) {
return 0;
}
return -1;
}
size_t espos_get_free_heap_size(void)
{
extern k_mm_head *g_kmm_head;
return g_kmm_head->free_size;
}

View file

@ -0,0 +1,111 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include <stdlib.h>
#include "k_api.h"
#include "espos_err.h"
#include "espos_mutex.h"
#include "espos_scheduler.h"
/**
* @brief create a mutex
*/
esp_err_t espos_mutex_create (
espos_mutex_t *mutex,
espos_opt_t opt
)
{
kstat_t ret;
kmutex_t **pmutex = (kmutex_t **)mutex;
ret = krhino_mutex_dyn_create(pmutex, "default_mutex");
return espos_err_map(ret);
}
/**
* @brief set a mutex name
*/
esp_err_t espos_mutex_set_name(espos_mutex_t mutex, const char *name)
{
return 0;
}
/**
* @brief lock a mutex
*/
esp_err_t espos_mutex_lock (
espos_mutex_t mutex,
espos_tick_t wait_ticks
)
{
kstat_t ret;
kmutex_t *pmutex = (kmutex_t *)mutex;
ret = krhino_mutex_lock(pmutex, wait_ticks);
if (ret == RHINO_MUTEX_OWNER_NESTED) {
return 0;
}
return espos_err_map(ret);
}
/**
* @brief unlock a mutex
*/
esp_err_t espos_mutex_unlock (
espos_mutex_t mutex
)
{
kstat_t ret;
kmutex_t *pmutex = (kmutex_t *)mutex;
ret = krhino_mutex_unlock(pmutex);
if (ret == RHINO_MUTEX_OWNER_NESTED) {
return 0;
}
return espos_err_map(ret);
}
/**
* @brief get task handle whick lock the mutex
*/
espos_task_t espos_mutex_get_holder (
espos_mutex_t mutex
)
{
espos_task_t tmp;
kmutex_t *pmutex = (kmutex_t *)mutex;
tmp = (espos_task_t)(pmutex->mutex_task);
return tmp;
}
/**
* @brief delete the mutex
*/
esp_err_t espos_mutex_del (
espos_mutex_t mutex
)
{
kstat_t ret;
kmutex_t *pmutex = (kmutex_t *)mutex;
ret = krhino_mutex_dyn_del(pmutex);
return espos_err_map(ret);
}

View file

@ -0,0 +1,138 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include "k_api.h"
#include "espos_err.h"
#include "espos_queue.h"
#include "espos_scheduler.h"
/**
* @brief create a queue
*/
esp_err_t espos_queue_create (
espos_queue_t *queue,
espos_size_t msg_len,
espos_size_t queue_len
)
{
kstat_t ret;
kbuf_queue_t **pqueue = (kbuf_queue_t **)queue;
ret = krhino_buf_queue_dyn_create(pqueue, "default_queue", queue_len * (msg_len + COMPRESS_LEN(msg_len)), msg_len);
return espos_err_map(ret);
}
/**
* @brief set a queue name
*
* @param queue queue handle
* @param name queue's name
*
* @return the result
* 0 : successful
* -EINVAL : input parameter error
*/
esp_err_t espos_queue_set_name(espos_queue_t queue, const char *name)
{
return 0;
}
/**
* @brief send a message to the queue
*/
esp_err_t IRAM_ATTR espos_queue_send_generic (
espos_queue_t queue,
void *msg,
espos_tick_t wait_ticks,
espos_pos_t pos,
espos_opt_t opt
)
{
kstat_t ret;
kbuf_queue_t *pqueue = (kbuf_queue_t *)queue;
ret = krhino_buf_queue_send(pqueue, msg, pqueue->max_msg_size);
return espos_err_map(ret);
}
/**
* @brief receive a message of the queue
*/
esp_err_t espos_queue_recv_generic (
espos_queue_t queue,
void *msg,
espos_tick_t wait_ticks,
espos_opt_t opt
)
{
kstat_t ret;
kbuf_queue_t *pqueue = (kbuf_queue_t *)queue;
size_t msg_len;
ret = krhino_buf_queue_recv(pqueue, wait_ticks, msg, &msg_len);
return espos_err_map(ret);
}
/**
* @brief get current message number of the queue
*/
espos_size_t espos_queue_msg_waiting(espos_queue_t queue)
{
CPSR_ALLOC();
espos_size_t item_size;
kbuf_queue_t *pqueue = (kbuf_queue_t *)queue;
RHINO_CRITICAL_ENTER();
//item_size = (pqueue->buf_size - pqueue->free_buf_size) / (pqueue->max_msg_size + HEADER_SIZE);
item_size = pqueue->cur_num;
RHINO_CRITICAL_EXIT();
return item_size;
}
/**
* @brief reset the queue
*/
esp_err_t espos_queue_flush (
espos_queue_t queue
)
{
kstat_t ret;
kbuf_queue_t *pqueue = (kbuf_queue_t *)queue;
ret = krhino_buf_queue_flush(pqueue);
return espos_err_map(ret);
}
/**
* @brief delete the queue
*/
esp_err_t espos_queue_del (
espos_queue_t queue
)
{
kstat_t ret;
kbuf_queue_t *pqueue = (kbuf_queue_t *)queue;
ret = krhino_buf_queue_dyn_del(pqueue);
return espos_err_map(ret);
}

View file

@ -0,0 +1,217 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include <stddef.h>
#include <stdint.h>
#include "k_api.h"
#include "espos_err.h"
#include "espos_scheduler.h"
static espos_size_t s_isr_nested_count[ESPOS_PROCESSORS_NUM];
static espos_size_t s_os_nested_count[ESPOS_PROCESSORS_NUM];
#if 1
static espos_size_t s_critial_count[ESPOS_PROCESSORS_NUM];
static espos_critical_t s_critial_status[ESPOS_PROCESSORS_NUM];
#endif
/**
* @brief initialize ESPOS system
*/
esp_err_t espos_init(void)
{
kstat_t ret;
ret = krhino_init();
return espos_err_map(ret);
}
/**
* @brief start ESPOS system
*/
esp_err_t espos_start(void)
{
kstat_t ret;
ret = krhino_start();
return espos_err_map(ret);
}
/**
* @brief start ESPOS system CPU port
*/
esp_err_t espos_start_port(int port)
{
return 0;
}
/**
* @brief get ESPOS system state
*/
espos_stat_t espos_sched_state_get(void)
{
if (g_sys_stat == RHINO_RUNNING) {
return ESPOS_IS_RUNNING;
}
return ESPOS_IS_NOT_STARTED;
}
/**
* @brief check if the CPU is at ISR state
*/
bool espos_in_isr(void)
{
return (s_isr_nested_count[0] > 0) ? true : false;
}
/**
* @brief check if the function is at real OS internal fucntion
*/
bool espos_os_isr(void)
{
return (s_os_nested_count[0] > 0) ? true : false;
}
static inline uint32_t __set_int(void)
{
uint32_t flags;
__asm__ __volatile__(
"rsil %0, 2\n"
: "=a"(flags)
:
: "memory"
);
return flags;
}
static inline void __set_ps(uint32_t flags)
{
__asm__ __volatile__(
"wsr %0, ps\n"
"esync"
:
: "a"(flags)
: "memory"
);
}
/**
* @brief enter ESPOS system critical state
*/
espos_critical_t IRAM_ATTR _espos_enter_critical(espos_spinlock_t *spinlock)
{
espos_critical_t tmp;
tmp = (espos_critical_t)__set_int();
if (!s_critial_count[espos_get_core_id()]) {
s_critial_status[espos_get_core_id()] = tmp;
}
s_critial_count[espos_get_core_id()]++;
return tmp;
}
/**
* @brief exit ESPOS system critical state
*/
void IRAM_ATTR _espos_exit_critical(espos_spinlock_t *spinlock, espos_critical_t tmp)
{
s_critial_count[espos_get_core_id()]--;
if (!s_critial_count[espos_get_core_id()]) {
tmp = s_critial_status[espos_get_core_id()];
__set_ps(tmp);
}
}
/**
* @brief suspend the preempt and the current task will not be preempted
*/
esp_err_t espos_preempt_suspend_local(void)
{
kstat_t ret;
if (g_sys_stat == RHINO_STOPPED) {
return 0;
}
ret = krhino_sched_disable();
return espos_err_map(ret);
}
/**
* @brief resume the preempt
*/
esp_err_t espos_preempt_resume_local(void)
{
kstat_t ret;
if (g_sys_stat == RHINO_STOPPED) {
return 0;
}
ret = krhino_sched_enable();
return espos_err_map(ret);
}
/**
* @brief enter system interrupt server, the function must be used after entering hardware interrupt
*/
esp_err_t espos_isr_enter (void)
{
kstat_t ret;
ret = krhino_intrpt_enter();
return espos_err_map(ret);
}
/**
* @brief exit system interrupt server, the function must be used before exiting hardware interrupt
*/
void espos_isr_exit(void)
{
krhino_intrpt_exit();
}
/**
* @brief mark it enters real OS interal function
*/
esp_err_t espos_os_enter (void)
{
s_os_nested_count[espos_get_core_id()]++;
return 0;
}
/**
* @brief remove mark it enters real OS interal function
*/
void espos_os_exit(void)
{
if (!s_os_nested_count[espos_get_core_id()]) {
return ;
}
s_os_nested_count[espos_get_core_id()]--;
return ;
}

View file

@ -0,0 +1,90 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include <k_api.h>
#include "espos_err.h"
#include "espos_semaphore.h"
#include "espos_scheduler.h"
/**
* @brief create a semaphore
*/
esp_err_t espos_sem_create (
espos_sem_t *sem,
espos_sem_count_t max_count,
espos_sem_count_t init_count
)
{
kstat_t ret;
ksem_t **psem = (ksem_t **)sem;
ret = krhino_sem_dyn_create(psem, "default_sem", init_count);
return espos_err_map(ret);
}
/**
* @brief set a semaphore name
*/
esp_err_t espos_sem_set_name(espos_sem_t sem, const char *name)
{
return 0;
}
/**
* @brief take semaphore
*/
esp_err_t espos_sem_take (
espos_sem_t sem,
espos_tick_t wait_ticks
)
{
kstat_t ret;
ksem_t *psem = (ksem_t *)sem;
ret = krhino_sem_take(psem, wait_ticks);
return espos_err_map(ret);
}
/**
* @brief give up semaphore
*/
esp_err_t espos_sem_give (
espos_sem_t sem
)
{
kstat_t ret;
ksem_t *psem = (ksem_t *)sem;
ret = krhino_sem_give(psem);
return espos_err_map(ret);
}
/**
* @brief delete the semaphore
*/
esp_err_t espos_sem_del (
espos_sem_t sem
)
{
kstat_t ret;
ksem_t *psem = (ksem_t *)sem;
ret = krhino_sem_dyn_del(psem);
return espos_err_map(ret);
}

View file

@ -0,0 +1,134 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include "espos_spinlock.h"
#include "espos_scheduler.h"
/**
* @brief create a spinlock
*/
esp_err_t espos_spinlock_create (
espos_spinlock_t *spinlock
)
{
if (!spinlock) {
return -EINVAL;
}
espos_spinlock_arch_init(&spinlock->lock);
return 0;
}
/**
* @brief set a spinlock name
*/
esp_err_t espos_spinlock_set_name(espos_spinlock_t *spinlock, const char *name)
{
return 0;
}
/**
* @brief spin to lock a spinlock until sucessfully
*/
esp_err_t espos_spinlock_lock (
espos_spinlock_t *spinlock
)
{
if (!spinlock) {
return -EINVAL;
}
espos_preempt_suspend_local();
espos_spinlock_arch_lock(&spinlock->lock);
spinlock->holder = espos_task_get_current();
return 0;
}
/**
* @brief try to lock a spinlock
*/
esp_err_t espos_spinlock_trylock (
espos_spinlock_t *spinlock
)
{
int ret;
if (!spinlock) {
return -EINVAL;
}
espos_preempt_suspend_local();
ret = espos_spinlock_arch_trylock(&spinlock->lock);
if (ret) {
ret = -EAGAIN;
espos_preempt_resume_local();
} else {
spinlock->holder = espos_task_get_current();
}
return ret;
}
/**
* @brief get spinlock holder task handle
*/
espos_task_t espos_spinlock_get_holder (
espos_spinlock_t *spinlock
)
{
return spinlock->holder;
}
/**
* @brief unlock a spinlock
*/
esp_err_t espos_spinlock_unlock (
espos_spinlock_t *spinlock
)
{
if (!spinlock) {
return -EINVAL;
}
spinlock->holder = ESPOS_OBJ_NONE;
espos_spinlock_arch_unlock(&spinlock->lock);
espos_preempt_resume_local();
return 0;
}
/**
* @brief delete a spinlock
*/
esp_err_t espos_spinlock_del (
espos_spinlock_t *spinlock
)
{
if (!spinlock) {
return -EINVAL;
}
espos_spinlock_arch_deinit(&spinlock->lock);
return 0;
}

View file

@ -0,0 +1,267 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include <assert.h>
#include "k_api.h"
#include "espos_err.h"
#include "espos_task.h"
#include "espos_scheduler.h"
/**
* @brief create a task
*/
esp_err_t espos_task_create_on_cpu (
espos_task_t *task,
const char *name,
void *arg,
espos_prio_t prio,
espos_tick_t ticks,
espos_size_t stack_size,
espos_task_entry_t entry,
espos_opt_t opt,
espos_cpu_t cpu_id
)
{
kstat_t ret;
ktask_t **ptask = (ktask_t **)task;
uint8_t auto_run;
extern void _cleanup_r(struct _reent* r);
cpu_stack_t os_task_size;
if (name == NULL) {
name = "default_task";
}
if (prio > ESPOS_TASK_PRIO_NUM)
return espos_err_map(RHINO_BEYOND_MAX_PRI);
if (ESPOS_TASK_CREATE_NORMAL == opt) {
auto_run = 1;
}
else {
auto_run = 0;
}
#if defined(ESPOS_FOR_ESP32)
os_task_size = stack_size / sizeof(cpu_stack_t);
#elif defined(ESPOS_FOR_ESP8266)
os_task_size = stack_size;
#endif
prio = RHINO_CONFIG_USER_PRI_MAX - prio;
assert(prio > 10);
if ( strcmp(name,"event_task") == 0 )
{
/* default event_task size is too small */
os_task_size += 64;
}
ret = krhino_task_dyn_create(ptask, name, arg, prio, ticks,
os_task_size, entry, auto_run);
/* The following code may open it later */
#if 0
if (ret == 0) {
struct _reent *r;
r = krhino_mm_alloc(sizeof(*r));
esp_reent_init(r);
ret = krhino_task_info_set(*ptask, 2, r);
}
#endif
return espos_err_map(ret);
}
/**
* @brief delete a task
*/
esp_err_t espos_task_del (
espos_task_t task
)
{
kstat_t ret;
ktask_t *ptask = (ktask_t *)task;
/* Be very careful here user_info[1] is used here */
#if 0
espos_sem_t *sem = ptask->user_info[1];
if (sem && *sem) {
espos_sem_del(*sem);
}
if (sem){
krhino_mm_free(sem);
}
#endif
ret = krhino_task_dyn_del(ptask);
return espos_err_map(ret);
}
/**
* @brief let current task sleep for some ticks
*/
esp_err_t espos_task_delay (
const espos_tick_t delay_ticks
)
{
kstat_t ret;
ret = krhino_task_sleep(delay_ticks);
return espos_err_map(ret);
}
/**
* @brief suspend target task
*/
esp_err_t espos_task_suspend (
espos_task_t task
)
{
kstat_t ret;
ktask_t *ptask = (ktask_t *)task;
ret = krhino_task_suspend(ptask);
return espos_err_map(ret);
}
/**
* @brief resume target task
*/
esp_err_t espos_task_resume (
espos_task_t task
)
{
kstat_t ret;
ktask_t *ptask = (ktask_t *)task;
ret = krhino_task_resume(ptask);
return espos_err_map(ret);
}
/**
* @brief yield the cpu once
*/
esp_err_t espos_task_yield(void)
{
kstat_t ret;
ret = krhino_task_yield();
return espos_err_map(ret);
}
/**
* @brief get current task handle
*/
espos_task_t espos_task_get_current(void)
{
return (espos_task_t)krhino_cur_task_get();
}
/**
* @brief get current task name point
*/
esp_err_t espos_task_get_name (
espos_task_t task,
char **pname
)
{
ktask_t *ptask = (ktask_t *)task;
*pname = (char *)ptask->task_name;
return 0;
}
/**
* @brief set task private data
*/
esp_err_t espos_task_set_private_data (
espos_task_t task,
int idx,
void *info
)
{
kstat_t ret;
ktask_t *ptask = (ktask_t *)task;;
ret = krhino_task_info_set(ptask, (size_t)idx, info);
return espos_err_map(ret);
}
/**
* @brief get task private data
*/
esp_err_t espos_task_get_private_data (
espos_task_t task,
int idx,
void **info
)
{
kstat_t ret;
ktask_t *ptask = (ktask_t *)task;;
ret = krhino_task_info_get(ptask, idx, info);
return espos_err_map(ret);
}
/**
* @brief get cpu affinity of task
*/
espos_cpu_t espos_task_get_affinity(espos_task_t task)
{
return 0;
}
size_t espos_task_prio_num(void)
{
return RHINO_CONFIG_USER_PRI_MAX + 1 - 10;
}
#if 0
struct _reent* __getreent()
{
void *info;
ktask_t *task = krhino_cur_task_get();
if (task == NULL) {
return _GLOBAL_REENT;
}
else {
krhino_task_info_get(task, 2, &info);
return info;
}
}
#endif
#if defined(ESPOS_FOR_ESP32)
struct _reent* __getreent()
{
ktask_t *task = krhino_cur_task_get();
if (task == NULL) {
return _GLOBAL_REENT;
}
else {
return _GLOBAL_REENT;;
}
}
#endif

View file

@ -0,0 +1,57 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include "k_api.h"
#include "espos_err.h"
#include "espos_time.h"
/**
* @brief get current system ticks
*/
espos_tick_t espos_get_tick_count(void)
{
return (espos_tick_t)krhino_sys_tick_get();
}
/**
* @brief transform milliseconds to system ticks
*/
espos_tick_t espos_ms_to_ticks(espos_time_t ms)
{
return (ms / (1000 / RHINO_CONFIG_TICKS_PER_SECOND));
}
/**
* @brief transform system ticks to milliseconds
*/
espos_time_t espos_ticks_to_ms(espos_tick_t ticks)
{
return (ticks * (1000 / RHINO_CONFIG_TICKS_PER_SECOND));
}
void espos_add_tick_count(size_t t)
{
g_tick_count += (sys_time_t)t;
}
size_t espos_get_expected_idle_time(void)
{
ktask_t *task = krhino_cur_task_get();
if (!strcmp(task->task_name, "idle_task")) {
return task->time_slice;
}
return 0;
}

View file

@ -0,0 +1,129 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
#include "k_api.h"
#include "espos_err.h"
#include "espos_timer.h"
#include "espos_scheduler.h"
/**
* @brief create a timer
*/
esp_err_t espos_timer_create (
espos_timer_t *timer,
const char *name,
espos_timer_cb_t cb,
void *arg,
espos_tick_t period_ticks,
espos_opt_t opt
)
{
kstat_t ret = RHINO_SUCCESS;
ktimer_t **ptimer = (ktimer_t **)timer;
tick_t round;
if (opt == ESPOS_TIMER_NO_AUTO_RUN)
round = 0;
else if (opt == ESPOS_TIMER_AUTO_RUN)
round = period_ticks;
else
return EINVAL;
ret = krhino_timer_dyn_create(ptimer, name, (timer_cb_t)cb, period_ticks,
round, arg, 0);
return espos_err_map(ret);
}
/**
* @brief start a timer
*/
esp_err_t espos_timer_start (espos_timer_t timer)
{
kstat_t ret;
ktimer_t *ptimer = (ktimer_t *)timer;
ret = krhino_timer_start(ptimer);
return espos_err_map(ret);
}
/**
* @brief stop a timer
*/
esp_err_t espos_timer_stop (
espos_timer_t timer
)
{
kstat_t ret;
ktimer_t *ptimer = (ktimer_t *)timer;
ret = krhino_timer_stop(ptimer);
return espos_err_map(ret);
}
/**
* @brief configure a timer
*/
esp_err_t espos_timer_change (
espos_timer_t timer,
espos_opt_t opt,
void *arg
)
{
kstat_t ret;
ktimer_t *ptimer = (ktimer_t *)timer;
if (ESPOS_TIMER_CHANGE_PERIOD == opt) {
espos_tick_t period = (espos_tick_t)arg;
ret = krhino_timer_change(ptimer, ptimer->init_count, period);
} else
return EINVAL;
return espos_err_map(ret);
}
/**
* @brief delete a timer
*/
esp_err_t espos_timer_del (
espos_timer_t timer
)
{
kstat_t ret;
ktimer_t *ptimer = (ktimer_t *)timer;
ret = krhino_timer_dyn_del(ptimer);
return espos_err_map(ret);
}
/**
* @brief get current timer name point
*/
esp_err_t espos_get_timer_name (
espos_timer_t timer,
char **tname
)
{
kstat_t ret;
ktimer_t *ptimer = (ktimer_t *)timer;
*tname = (char *)ptimer->name;
ret = RHINO_SUCCESS;
return espos_err_map(ret);
}

View file

@ -0,0 +1,5 @@
#
#Component Makefile
#
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View file

@ -0,0 +1,12 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
// No event group API yet

View file

@ -0,0 +1,126 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#if 0
#include "unity.h"
#include "espos_mem.h"
#include "espos_task.h"
#include "espos_semaphore.h"
#include "espos_time.h"
static espos_sem_t sync_semaphore;
TEST_CASE("ESPOS mm api test", "[espos]")
{
uint8_t *buf;
uint8_t non_zero = 0;
uint32_t i;
// do malloc
buf = espos_malloc(1024);
memset(buf, 0xFF, 1024);
TEST_ASSERT(buf != NULL);
espos_free(buf);
// do zalloc
buf = espos_zalloc(1024);
TEST_ASSERT(buf != NULL);
for (i = 0; i < 1024; i++) {
if (buf[i] != 0) {
non_zero = 1;
break;
}
}
TEST_ASSERT(non_zero == 0);
espos_free(buf);
}
static int tryAllocMem()
{
int **mem;
int i, noAllocated, j;
mem = malloc(sizeof(int) * 1024);
if (!mem) {
return 0;
}
for (i = 0; i < 1024; i++) {
mem[i] = malloc(1024);
if (mem[i] == NULL) {
break;
}
for (j = 0; j < 1024 / 4; j++) {
mem[i][j] = (0xdeadbeef);
}
}
noAllocated = i;
for (i = 0; i < noAllocated; i++) {
for (j = 0; j < 1024 / 4; j++) {
TEST_ASSERT(mem[i][j] == (0xdeadbeef));
}
free(mem[i]);
}
free(mem);
return noAllocated;
}
static void malloc_free_task(void *p)
{
uint32_t i;
uint8_t *buf;
for (i = 0; i < 0xFFFF; i++) {
buf = espos_malloc(1024);
espos_task_delay(1);
espos_free(buf);
}
espos_sem_give(sync_semaphore);
espos_task_del(0);
}
TEST_CASE("ESPOS concurrent use of malloc free test", "[espos]")
{
uint32_t m1, m2;
espos_task_t new_task_handle;
espos_sem_create(&sync_semaphore, NULL, 2, 0);
m1 = tryAllocMem();
espos_task_create(&new_task_handle, "malloc_free_task1", NULL, 1,
0, 1024, malloc_free_task, 0);
espos_task_create(&new_task_handle, "malloc_free_task2", NULL, 1,
0, 1024, malloc_free_task, 0);
// wait task exit
espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY);
espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY);
espos_sem_del(sync_semaphore);
m2 = tryAllocMem();
TEST_ASSERT(m1 == m2);
}
TEST_CASE("ESPOS mm API invalid parameter test", "[espos]")
{
uint8_t *buf;
buf = espos_malloc(0xFFFFFFFF);
TEST_ASSERT(buf == NULL);
buf = espos_malloc(0);
TEST_ASSERT(buf == NULL);
buf = espos_zalloc(0xFFFFFFFF);
TEST_ASSERT(buf == NULL);
buf = espos_zalloc(0);
TEST_ASSERT(buf == NULL);
// TODO: need to check if free invalid pointer will cause exception
espos_free(NULL);
espos_free((void *)0xFFFF);
}
#endif
// To add more test cases for specific RTOS, like fragment handling

View file

@ -0,0 +1,223 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_mutex.h"
#include "espos_semaphore.h"
#include "espos_task.h"
#include "espos_queue.h"
#include "test_espos_run_in_isr.h"
#define ESPOS_MUTEX_ADD_TEST_COUNT 0xFFFF
#define ESPOS_MUTEX_CREATE_INVALID ESPOS_MUTEX_NORMAL + 10
#define ESPOS_MUTEX_INVALID_HANDLE 0
static espos_sem_t sync_semaphore;
static espos_mutex_t global_mutex;
static espos_queue_t isr_queue;
static int count;
TEST_CASE("ESPOS mutex create delete test", "[espos]")
{
uint32_t i;
espos_queue_t mutex = 0;
for (i = 0; i < 1000; i++) {
if (espos_mutex_create(&mutex, ESPOS_MUTEX_NORMAL) != 0) {
TEST_ASSERT(0);
break;
}
if (espos_mutex_del(mutex) != 0) {
TEST_ASSERT(0);
break;
}
}
}
static void mutex_test_task(void *p)
{
uint32_t i;
for (i = 0; i < ESPOS_MUTEX_ADD_TEST_COUNT; i++) {
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_MAX_DELAY) == 0);
count++;
TEST_ASSERT(espos_mutex_unlock(global_mutex) == 0);
}
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS mutex lock unlock test", "[espos]")
{
espos_task_t new_task_handle;
TEST_ASSERT(espos_mutex_create(&global_mutex, ESPOS_MUTEX_NORMAL) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 2, 0) == 0);
count = 0;
TEST_ASSERT(espos_task_create(&new_task_handle, "mutex_test_task1", NULL, 1,
0, 2048, mutex_test_task, 0) == 0);
TEST_ASSERT(espos_task_create(&new_task_handle, "mutex_test_task2", NULL, 1,
0, 2048, mutex_test_task, 0) == 0);
// wait task exit
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
TEST_ASSERT(count == (ESPOS_MUTEX_ADD_TEST_COUNT << 1));
TEST_ASSERT(espos_mutex_del(global_mutex) == 0);
}
static void mutex_lock_test_task(void *p)
{
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_mutex_unlock(global_mutex) == 0);
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS delete mutex in use test", "[espos]")
{
// TODO: this case is RTOS dependent
espos_task_t new_task_handle;
TEST_ASSERT(espos_mutex_create(&global_mutex, ESPOS_MUTEX_NORMAL) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 1, 0) == 0);
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_task_create(&new_task_handle, "mutex_lock_test_task", NULL, ESPOS_TASK_PRIO_MAX,
0, 2048, mutex_lock_test_task, 0) == 0);
TEST_ASSERT(espos_mutex_unlock(global_mutex) == 0);
// wait task exit
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_mutex_del(global_mutex) == 0);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
}
static void mutex_unlock_test_task(void *p)
{
TEST_ASSERT(espos_mutex_unlock(global_mutex) == -EPERM);
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS mutex unlock by different task test", "[espos]")
{
espos_task_t new_task_handle;
TEST_ASSERT(espos_mutex_create(&global_mutex, ESPOS_MUTEX_NORMAL) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 1, 0) == 0);
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_task_create(&new_task_handle, "mutex_unlock_test_task", NULL, ESPOS_TASK_PRIO_MAX,
0, 1024, mutex_unlock_test_task, 0) == 0);
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_mutex_del(global_mutex) == 0);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
}
TEST_CASE("ESPOS mutex lock timeout test", "[espos]")
{
espos_queue_t mutex;
TEST_ASSERT(espos_mutex_create(&mutex, ESPOS_MUTEX_NORMAL) == 0);
TEST_ASSERT(espos_mutex_lock(mutex, ESPOS_NO_DELAY) == 0);
TEST_ASSERT(espos_mutex_lock(mutex, ESPOS_NO_DELAY) == -ETIMEDOUT);
TEST_ASSERT(espos_mutex_del(mutex) == 0);
}
static void mutex_lock_hold_test_task(void *p)
{
espos_sem_t sem = (espos_sem_t) p;
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_sem_take(sem, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_del(sem) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS mutex lock get holder test", "[espos]")
{
espos_task_t new_task_handle;
espos_sem_t sem;
TEST_ASSERT(espos_mutex_create(&global_mutex, ESPOS_MUTEX_NORMAL) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 1, 0) == 0);
TEST_ASSERT(espos_sem_create(&sem, 1, 0) == 0);
TEST_ASSERT(espos_mutex_lock(global_mutex, ESPOS_NO_DELAY) == 0);
TEST_ASSERT(espos_mutex_get_holder(global_mutex) == espos_task_get_current());
TEST_ASSERT(espos_mutex_unlock(global_mutex) == 0);
TEST_ASSERT(espos_task_create(&new_task_handle, "mutex_lock_hold_test_task", (void *)sem, ESPOS_TASK_PRIO_MAX,
0, 2048, mutex_lock_hold_test_task, 0) == 0);
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_mutex_get_holder(global_mutex) == new_task_handle);
TEST_ASSERT(espos_sem_give(sem) == 0);
TEST_ASSERT(espos_mutex_del(global_mutex) == 0);
}
TEST_CASE("ESPOS mutex recursive lock unlock test", "[espos]")
{
//TODO: to add this case if recursive lock is supported
}
static void IRAM_ATTR mutex_isr_test(void)
{
uint8_t msg = 0;
if (espos_mutex_lock(global_mutex, 0) != -EINTR
|| espos_mutex_unlock(global_mutex) != -EINTR) {
msg = 1;
}
TEST_ASSERT(espos_queue_send(isr_queue, &msg, ESPOS_MAX_DELAY) == 0);
}
TEST_CASE("ESPOS use mutex in ISR test", "[espos]")
{
uint8_t msg;
TEST_ASSERT(espos_queue_create(&isr_queue, 1, 1) == 0);
TEST_ASSERT(espos_mutex_create(&global_mutex, ESPOS_MUTEX_NORMAL) == 0);
espos_test_run_in_isr(mutex_isr_test);
TEST_ASSERT(espos_queue_recv(isr_queue, &msg, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(msg == 0);
TEST_ASSERT(espos_mutex_del(global_mutex) == 0);
TEST_ASSERT(espos_queue_del(isr_queue) == 0);
}
TEST_CASE("ESPOS mutex API invalid parameter test", "[espos]")
{
espos_queue_t mutex;
TEST_ASSERT(espos_mutex_create(NULL, ESPOS_MUTEX_NORMAL) == -EINVAL);
TEST_ASSERT(espos_mutex_create(&mutex, ESPOS_MUTEX_CREATE_INVALID) == -EINVAL);
TEST_ASSERT(espos_mutex_lock(ESPOS_MUTEX_INVALID_HANDLE, 0) == -EINVAL);
TEST_ASSERT(espos_mutex_unlock(ESPOS_MUTEX_INVALID_HANDLE) == -EINVAL);
TEST_ASSERT(espos_mutex_del(ESPOS_MUTEX_INVALID_HANDLE) == -EINVAL);
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_include.h"
uint32_t tick;
// memory management
TEST_CASE("ESPOS malloc free performance", "[espos]")
{
}
// scheduler
TEST_CASE("ESPOS task context switch performance", "[espos]")
{
}
TEST_CASE("ESPOS isr context switch performance", "[espos]")
{
}
TEST_CASE("ESPOS critical section performance", "[espos]")
{
}
// spin lock
TEST_CASE("ESPOS spin lock performance", "[espos]")
{
}
// queue
TEST_CASE("ESPOS queue send recv performance", "[espos]")
{
}
// semaphore
TEST_CASE("ESPOS semaphore performance", "[espos]")
{
}
// timer
TEST_CASE("ESPOS timer accuracy performance", "[espos]")
{
}

View file

@ -0,0 +1,248 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_queue.h"
#include "espos_task.h"
#include "espos_semaphore.h"
#include "espos_time.h"
#include "test_espos_run_in_isr.h"
#define ESPOS_QUEUE_INVALID_OPT (ESPOS_QUEUE_SEND_OPT_NORMAL+10)
#define ESPOS_QUEUE_INVALID_POS (ESPOS_QUEUE_SEND_BACK+10)
#define ESPOS_QUEUE_INVALID_HANDLE 0
static espos_sem_t sync_semaphore;
static espos_queue_t global_queue;
static espos_queue_t isr_queue;
TEST_CASE("ESPOS queue create delete", "[espos]")
{
uint32_t i;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
for (i = 0; i < 1000; i++) {
if (espos_queue_create(&queue, 16, 50) != 0) {
TEST_ASSERT(0);
break;
}
if (espos_queue_del(queue) != 0) {
TEST_ASSERT(0);
break;
}
}
}
static void queue_send_task(void *p)
{
uint32_t msg = 0xF1F2F3F4;
uint8_t *received = (uint8_t *) p;
TEST_ASSERT(espos_queue_send(global_queue, &msg, ESPOS_MAX_DELAY) == 0);
// should switch to recv task and then switch back
TEST_ASSERT(*received == 1);
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
static void queue_recv_task(void *p)
{
uint32_t msg;
uint8_t *received = (uint8_t *) p;
TEST_ASSERT(espos_queue_recv(global_queue, &msg, ESPOS_MAX_DELAY) == 0);
// check msg value
TEST_ASSERT(msg = 0xF1F2F3F4);
// set received flag
*received = 1;
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS queue send receive", "[espos]")
{
espos_task_t new_task_handle;
uint8_t received = 0;
TEST_ASSERT(espos_queue_create(&global_queue, 4, 10) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 2, 0) == 0);
// recv task with higher priority, create recv task first
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "queue_recv_task", &received, 2,
0, 1024, queue_recv_task, 0, 0) == 0);
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "queue_send_task", &received, 1,
0, 1024, queue_send_task, 0, 0) == 0);
// wait task exit
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
TEST_ASSERT(espos_queue_del(global_queue) == 0);
}
TEST_CASE("ESPOS queue flush", "[espos]")
{
uint32_t msg = 0;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 4, 10) == 0);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
TEST_ASSERT(espos_queue_flush(queue) == 0);
TEST_ASSERT(espos_queue_msg_waiting(queue) == 0);
TEST_ASSERT(espos_queue_flush(queue) == 0);
TEST_ASSERT(espos_queue_del(queue) == 0);
}
TEST_CASE("ESPOS queue send front/end", "[espos]")
{
uint32_t msg;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 4, 10) == 0);
msg = 0x01;
TEST_ASSERT(espos_queue_send_generic(queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
msg = 0x02;
TEST_ASSERT(espos_queue_send_generic(queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_FRONT, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
msg = 0x03;
TEST_ASSERT(espos_queue_send_generic(queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
TEST_ASSERT(espos_queue_recv(queue, &msg, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(msg == 0x02);
TEST_ASSERT(espos_queue_recv(queue, &msg, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(msg == 0x01);
TEST_ASSERT(espos_queue_recv(queue, &msg, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(msg == 0x03);
TEST_ASSERT(espos_queue_del(queue) == 0);
}
TEST_CASE("ESPOS queue timeout test", "[espos]")
{
uint32_t msg;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 4, 1) == 0);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, 0,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, 0,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == -ETIMEDOUT);
TEST_ASSERT(espos_queue_recv(queue, &msg, 0) == 0);
TEST_ASSERT(espos_queue_recv(queue, &msg, 0) == -ETIMEDOUT);
TEST_ASSERT(espos_queue_del(queue) == 0);
}
TEST_CASE("ESPOS queue create/delete API invalid param test", "[espos]")
{
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 0xFFFF, 0xFFFFFF) == -ENOMEM);
TEST_ASSERT(espos_queue_del(queue) == -EINVAL);
TEST_ASSERT(espos_queue_create(NULL, 4, 10) == -EINVAL);
TEST_ASSERT(espos_queue_create(&queue, 0, 10) == -EINVAL);
TEST_ASSERT(espos_queue_create(&queue, 4, 0) == -EINVAL);
TEST_ASSERT(espos_queue_del(ESPOS_QUEUE_INVALID_HANDLE) == -EINVAL)
}
TEST_CASE("ESPOS queue send API invalid param test", "[espos]")
{
uint32_t msg;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 4, 10) == 0);
TEST_ASSERT(espos_queue_send_generic(queue, NULL, 0,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == -EINVAL);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, 0,
ESPOS_QUEUE_INVALID_POS, ESPOS_QUEUE_SEND_OPT_NORMAL) == -EINVAL);
TEST_ASSERT(espos_queue_send_generic(queue, &msg, 0,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_INVALID_OPT) == -EINVAL);
TEST_ASSERT(espos_queue_send_generic(ESPOS_QUEUE_INVALID_HANDLE, &msg, 0,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_INVALID_OPT) == -EINVAL);
TEST_ASSERT(espos_queue_del(queue) == 0);
}
TEST_CASE("ESPOS queue recv/wait/flush API invalid param test", "[espos]")
{
uint32_t msg;
espos_queue_t queue = ESPOS_QUEUE_INVALID_HANDLE;
TEST_ASSERT(espos_queue_create(&queue, 4, 10) == 0);
TEST_ASSERT(espos_queue_recv(ESPOS_QUEUE_INVALID_HANDLE, &msg, 0) == -EINVAL);
TEST_ASSERT(espos_queue_recv(queue, NULL, 0) == -EINVAL);
//TODO: don't konw what could happen if passing invalid queue handle
TEST_ASSERT(espos_queue_msg_waiting(ESPOS_QUEUE_INVALID_HANDLE) == 0);
TEST_ASSERT(espos_queue_flush(ESPOS_QUEUE_INVALID_HANDLE) == -EINVAL);
TEST_ASSERT(espos_queue_del(queue) == 0);
}
TEST_CASE("ESPOS deleting a queue in use test", "[espos]")
{
// TODO: this case is RTOS dependent
espos_task_t new_task_handle;
uint8_t received = 0;
uint32_t msg = 0xF1F2F3F4;
TEST_ASSERT(espos_queue_create(&global_queue, 4, 1) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 1, 0) == 0);
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "queue_recv_task", &received, 1,
0, 1024, queue_recv_task, 0, 0) == 0);
TEST_ASSERT(espos_queue_send(global_queue, &msg, ESPOS_MAX_DELAY) == 0);
// wait task exit
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(received == 1);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
TEST_ASSERT(espos_queue_del(global_queue) == 0);
}
static void IRAM_ATTR queue_isr_test(void)
{
uint8_t msg = 0;
TEST_ASSERT(espos_queue_send_generic(isr_queue, &msg, ESPOS_MAX_DELAY,
ESPOS_QUEUE_SEND_BACK, ESPOS_QUEUE_SEND_OPT_NORMAL) == 0);
}
TEST_CASE("ESPOS send to queue in ISR test", "[espos]")
{
uint8_t msg;
espos_queue_create(&isr_queue, 1, 1);
espos_test_run_in_isr(queue_isr_test);
espos_queue_recv(isr_queue, &msg, ESPOS_MAX_DELAY);
TEST_ASSERT(msg == 0);
espos_queue_del(isr_queue);
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "esp_types.h"
#include "rom/ets_sys.h"
#include "espos_task.h"
#include "espos_scheduler.h"
#include "soc/timer_group_struct.h"
#include "driver/periph_ctrl.h"
#include "driver/timer.h"
#include "test_espos_run_in_isr.h"
#define TIMER_INTR_SEL TIMER_INTR_LEVEL /*!< Timer level interrupt */
#define TIMER_GROUP TIMER_GROUP_0 /*!< Test on timer group 0 */
#define TIMER_DIVIDER 16 /*!< Hardware timer clock divider */
static void IRAM_ATTR timer_isr(void *p)
{
espos_test_isr_routine routine = (espos_test_isr_routine) p;
espos_isr_enter();
TIMERG0.int_clr_timers.t0 = 1;
routine();
espos_isr_exit();
}
void espos_test_run_in_isr(espos_test_isr_routine routine)
{
int timer_group = TIMER_GROUP_0;
int timer_idx = TIMER_0;
timer_config_t config;
config.alarm_en = 1;
config.auto_reload = 0;
config.counter_dir = TIMER_COUNT_UP;
config.divider = TIMER_DIVIDER;
config.intr_type = TIMER_INTR_SEL;
config.counter_en = TIMER_PAUSE;
/*Configure timer*/
timer_init(timer_group, timer_idx, &config);
/*Stop timer counter*/
timer_pause(timer_group, timer_idx);
/*Load counter value */
timer_set_counter_value(timer_group, timer_idx, 0x00000000ULL);
/*Set alarm value*/
timer_set_alarm_value(timer_group, timer_idx, 1);
timer_set_auto_reload(timer_group, timer_idx, TIMER_AUTORELOAD_DIS);
/*Enable timer interrupt*/
timer_enable_intr(timer_group, timer_idx);
/*Set ISR handler*/
timer_isr_register(timer_group, timer_idx, timer_isr,
(void *) routine, ESP_INTR_FLAG_IRAM, NULL);
/*Start timer counter*/
timer_start(timer_group, timer_idx);
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef _TEST_ESPOS_RUN_IN_ISR_H_
#define _TEST_ESPOS_RUN_IN_ISR_H_
#include "espos_types.h"
#include "esp_attr.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*espos_test_isr_routine)(void);
/*
* @brief run handler in timer isr
*/
void espos_test_run_in_isr(espos_test_isr_routine routine);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_task.h"
#include "espos_scheduler.h"
#include "espos_spinlock.h"
#include "test_espos_run_in_isr.h"
#define ESPOS_SCHEDULER_INVALID_HANDLE -1
uint32_t count;
TEST_CASE("ESPOS scheduler critical section test", "[espos]")
{
//TODO: how to test critical section?
}
static void count_task(void *p)
{
count ++;
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS scheduler preempt suspend resume test", "[espos]")
{
espos_task_t new_task_handle;
count = 0;
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "count_task", NULL, 10,
0, 1024, count_task, 0, 0) == 0);
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "count_task", NULL, 10,
0, 1024, count_task, 0, 0) == 0);
TEST_ASSERT(count == 2);
count = 0;
TEST_ASSERT(espos_preempt_suspend_local() == 0);
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "count_task", NULL, 10,
0, 1024, count_task, 0, 0) == 0);
TEST_ASSERT(espos_task_create_on_cpu(&new_task_handle, "count_task", NULL, 10,
0, 1024, count_task, 0, 0) == 0);
TEST_ASSERT(count == 0);
TEST_ASSERT(espos_preempt_resume_local() == 0);
}

View file

@ -0,0 +1,174 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_semaphore.h"
#include "espos_queue.h"
#include "espos_task.h"
#include "espos_time.h"
#include "driver/timer.h"
#include "test_espos_run_in_isr.h"
#define ESPOS_SEM_INVALID_HANDLE 0
static espos_sem_t sync_semaphore;
static espos_sem_t global_sema;
static espos_queue_t isr_queue;
TEST_CASE("ESPOS semaphore create delete test", "[espos]")
{
uint32_t i;
espos_sem_t sem;
for (i = 0; i < 1000; i++) {
if (espos_sem_create(&sem, 1, 1) != 0) {
TEST_ASSERT(0);
break;
}
if (espos_sem_del(sem) != 0) {
TEST_ASSERT(0);
break;
}
}
}
static void sema_give_task(void *p)
{
uint8_t *taken = (uint8_t *) p;
TEST_ASSERT(espos_sem_give(global_sema) == 0);
// should switch to recv task and then switch back
TEST_ASSERT(*taken == 1);
espos_sem_give(sync_semaphore);
espos_task_del(0);
}
static void sema_take_task(void *p)
{
uint8_t *taken = (uint8_t *) p;
TEST_ASSERT(espos_sem_take(global_sema, ESPOS_MAX_DELAY) == 0);
// set taken flag
*taken = 1;
espos_sem_give(sync_semaphore);
espos_task_del(0);
}
TEST_CASE("ESPOS semaphore take give test", "[espos]")
{
espos_task_t new_task_handle;
uint8_t taken = 0;
TEST_ASSERT(espos_sem_create(&global_sema, 1, 0) == 0);
espos_sem_create(&sync_semaphore, 2, 0);
// recv task with higher priority, create recv task first
espos_task_create_on_cpu(&new_task_handle, "sema_take_task", &taken, 2,
0, 1024, sema_take_task, 0, 0);
espos_task_create_on_cpu(&new_task_handle, "sema_give_task", &taken, 1,
0, 1024, sema_give_task, 0, 0);
// wait task exit
espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY);
espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY);
espos_sem_del(sync_semaphore);
TEST_ASSERT(espos_sem_del(global_sema) == 0);
}
TEST_CASE("ESPOS semaphore count test", "[espos]")
{
uint8_t i;
espos_sem_t sem;
TEST_ASSERT(espos_sem_create(&sem, 5, 1) == 0);
TEST_ASSERT(espos_sem_take(sem, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_trytake(sem) == -ETIMEDOUT);
for (i = 0; i < 5; i++) {
TEST_ASSERT(espos_sem_give(sem) == 0);
}
TEST_ASSERT(espos_sem_give(sem) == -EAGAIN);
for (i = 0; i < 5; i++) {
TEST_ASSERT(espos_sem_trytake(sem) == 0);
}
TEST_ASSERT(espos_sem_del(sem) == 0);
}
static void IRAM_ATTR semaphore_isr_test(void)
{
uint8_t msg = 0;
espos_sem_t sem;
if (espos_sem_create(&sem, 2, 1) != -EINTR) {
msg = 1;
}
if (espos_sem_take(global_sema, 0) != 0
|| espos_sem_give(global_sema) != 0
|| espos_sem_del(global_sema) != -EINTR) {
msg = 1;
}
espos_queue_send(isr_queue, &msg, ESPOS_MAX_DELAY);
}
TEST_CASE("ESPOS use semaphore in ISR test", "[espos]")
{
uint8_t msg;
espos_queue_create(&isr_queue, 1, 1);
TEST_ASSERT(espos_sem_create(&global_sema, 2, 1) == 0);
espos_test_run_in_isr(semaphore_isr_test);
espos_queue_recv(isr_queue, &msg, ESPOS_MAX_DELAY);
TEST_ASSERT(msg == 0);
espos_sem_del(global_sema);
espos_queue_del(isr_queue);
}
TEST_CASE("ESPOS deleting a semaphore in use test", "[espos]")
{
// TODO: this case is RTOS dependent
espos_task_t new_task_handle;
uint8_t taken = 0;
TEST_ASSERT(espos_sem_create(&global_sema, 1, 0) == 0);
espos_sem_create(&sync_semaphore, 1, 0);
espos_task_create_on_cpu(&new_task_handle, "sema_take_task", &taken, ESPOS_TASK_PRIO_MAX,
0, 1024, sema_take_task, 0, 0);
espos_sem_give(global_sema);
// wait task exit
espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY);
TEST_ASSERT(taken == 1);
espos_sem_del(sync_semaphore);
TEST_ASSERT(espos_sem_del(global_sema) == 0);
}
TEST_CASE("ESPOS semaphore API invalid parameter test", "[espos]")
{
TEST_ASSERT(espos_sem_create(NULL, 5, 1) == -EINVAL);
TEST_ASSERT(espos_sem_take(ESPOS_SEM_INVALID_HANDLE, ESPOS_MAX_DELAY) == -EINVAL);
TEST_ASSERT(espos_sem_give(ESPOS_SEM_INVALID_HANDLE) == -EINVAL);
TEST_ASSERT(espos_sem_del(ESPOS_SEM_INVALID_HANDLE) == -EINVAL);
}

View file

@ -0,0 +1,119 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_spinlock.h"
#include "espos_task.h"
#include "espos_queue.h"
#include "espos_semaphore.h"
#include "test_espos_run_in_isr.h"
#define ESPOS_SPINLOCK_INVALID_HANDLE NULL
#define ESPOS_SPINLOCK_ADD_TEST_COUNT 0xFFFF
static espos_sem_t sync_semaphore;
static espos_queue_t isr_queue;
static espos_spinlock_t global_spinlock;
static int count;
TEST_CASE("ESPOS spinlock create delete test", "[espos]")
{
espos_spinlock_t spinlock;
for (int i = 0; i < 1000; i++) {
if (espos_spinlock_create(&spinlock) != 0) {
TEST_ASSERT(0);
}
if (espos_spinlock_del(&spinlock) != 0) {
TEST_ASSERT(0);
}
}
}
static void spinlock_test_task(void *p)
{
for (int i = 0; i < ESPOS_SPINLOCK_ADD_TEST_COUNT; i++) {
TEST_ASSERT(espos_spinlock_lock(&global_spinlock) == 0);
count++;
TEST_ASSERT(espos_spinlock_unlock(&global_spinlock) == 0);
}
TEST_ASSERT(espos_sem_give(sync_semaphore) == 0);
TEST_ASSERT(espos_task_del(0) == 0);
}
TEST_CASE("ESPOS spinlock lock unlock test", "[espos]")
{
espos_task_t new_task_handle;
TEST_ASSERT(espos_spinlock_create(&global_spinlock) == 0);
TEST_ASSERT(espos_sem_create(&sync_semaphore, 2, 0) == 0);
count = 0;
TEST_ASSERT(espos_task_create(&new_task_handle, "spinlock_test_task1", NULL, 1,
0, 1024, spinlock_test_task, 0) == 0);
TEST_ASSERT(espos_task_create(&new_task_handle, "spinlock_test_task2", NULL, 1,
0, 1024, spinlock_test_task, 0) == 0);
// wait task exit
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_take(sync_semaphore, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_del(sync_semaphore) == 0);
TEST_ASSERT(count == (ESPOS_SPINLOCK_ADD_TEST_COUNT << 1));
TEST_ASSERT(espos_spinlock_del(&global_spinlock) == 0);
}
TEST_CASE("ESPOS spinlock trylock test", "[espos]")
{
espos_spinlock_t spinlock;
TEST_ASSERT(espos_spinlock_create(&spinlock) == 0);
TEST_ASSERT(espos_spinlock_trylock(&spinlock) == 0);
TEST_ASSERT(espos_spinlock_trylock(&spinlock) == -EAGAIN);
TEST_ASSERT(espos_spinlock_unlock(&spinlock) == 0);
TEST_ASSERT(espos_spinlock_del(&spinlock) == 0);
}
static void IRAM_ATTR spinlock_isr_test(void)
{
uint8_t msg = 0;
if (espos_spinlock_lock(&global_spinlock) != 0
|| espos_spinlock_unlock(&global_spinlock) != 0) {
msg = 1;
}
TEST_ASSERT(espos_queue_send(isr_queue, &msg, ESPOS_MAX_DELAY) == 0);
}
TEST_CASE("ESPOS spinlock use in ISR test", "[espos]")
{
uint8_t msg;
TEST_ASSERT(espos_queue_create(&isr_queue, 1, 1) == 0);
TEST_ASSERT(espos_spinlock_create(&global_spinlock) == 0);
espos_test_run_in_isr(spinlock_isr_test);
TEST_ASSERT(espos_queue_recv(isr_queue, &msg, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(msg == 0);
TEST_ASSERT(espos_queue_del(isr_queue) == 0);
TEST_ASSERT(espos_spinlock_del(&global_spinlock) == 0);
}
TEST_CASE("ESPOS spinlock API invalid parameter test", "[espos]")
{
TEST_ASSERT(espos_spinlock_create(NULL) == -EINVAL);
TEST_ASSERT(espos_spinlock_lock(NULL) == -EINVAL);
TEST_ASSERT(espos_spinlock_trylock(NULL) == -EINVAL);
TEST_ASSERT(espos_spinlock_unlock(NULL) == -EINVAL);
TEST_ASSERT(espos_spinlock_del(ESPOS_SPINLOCK_INVALID_HANDLE) == -EINVAL);
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_task.h"
uint32_t count;
TEST_CASE("ESPOS task SMP create delete test", "[espos]")
{
}
TEST_CASE("ESPOS task multi-core test", "[espos]")
{
}
TEST_CASE("ESPOS task yield test", "[espos]")
{
}
TEST_CASE("ESPOS task exit test", "[espos]")
{
}
TEST_CASE("ESPOS task suspend resume test", "[espos]")
{
}
TEST_CASE("ESPOS task sleep test", "[espos]")
{
}
TEST_CASE("ESPOS task private data test", "[espos]")
{
}
TEST_CASE("ESPOS task priority test", "[espos]")
{
}
TEST_CASE("ESPOS task time slice test", "[espos]")
{
}
TEST_CASE("ESPOS task option test", "[espos]")
{
}
TEST_CASE("ESPOS task stack size test", "[espos]")
{
}
TEST_CASE("ESPOS task API misc param test", "[espos]")
{
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_time.h"
#include "espos_task.h"
#define _ESPOS_TIME_DELAY_TICK 100
#define _ESPOS_TIME_DELAY_TICK_TH 120
TEST_CASE("ESPOS get tick test", "[espos]")
{
espos_tick_t tick1, tick2;
tick1 = espos_get_tick_count();
espos_task_delay(_ESPOS_TIME_DELAY_TICK);
tick2 = espos_get_tick_count();
TEST_ASSERT(tick2 - tick1 >= _ESPOS_TIME_DELAY_TICK);
TEST_ASSERT(tick2 - tick1 < _ESPOS_TIME_DELAY_TICK_TH);
}
TEST_CASE("ESPOS tick ms convert test", "[espos]")
{
TEST_ASSERT(espos_ticks_to_ms(espos_ms_to_ticks(10)) == 10);
TEST_ASSERT(espos_ms_to_ticks(0xFFFFFFFF) == 0xFFFFFFFF / 10);
}

View file

@ -0,0 +1,162 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include "unity.h"
#include "espos_timer.h"
#include "espos_semaphore.h"
#define ESPOS_TIMER_INVALID_OPT 0xFF
#define ESPOS_TIMER_INVALID_HANDLE 0
#define ESPOS_TIMER_CHANGE_OPT_INVALID 5
static const char *TIMER_NAME = "timer_test";
static espos_sem_t sync_sema;
static espos_timer_t global_timer;
static int count;
static void espos_test_timer_dummy_cb(espos_timer_t timer, void *arg)
{
}
TEST_CASE("ESPOS timer create delete test", "[espos]")
{
uint32_t i;
espos_timer_t timer = ESPOS_TIMER_INVALID_HANDLE;
for (i = 0; i < 1000; i++) {
if (espos_timer_create(&timer, TIMER_NAME, espos_test_timer_dummy_cb, NULL,
1, ESPOS_TIMER_NO_AUTO_RUN) != 0) {
TEST_ASSERT(0);
break;
}
if (espos_timer_del(timer) != 0) {
TEST_ASSERT(0);
break;
}
}
}
static void espos_test_timer_cb(espos_timer_t timer, void *arg)
{
TEST_ASSERT(global_timer == timer);
TEST_ASSERT(arg == NULL);
count++;
TEST_ASSERT(espos_sem_give(sync_sema) == 0);
}
TEST_CASE("ESPOS timer no auto run test", "[espos]")
{
uint32_t i;
TEST_ASSERT(espos_sem_create(&sync_sema, 1, 0) == 0);
TEST_ASSERT(espos_timer_create(&global_timer, TIMER_NAME, espos_test_timer_cb, NULL,
espos_ms_to_ticks(20), ESPOS_TIMER_NO_AUTO_RUN) == 0);
count = 0;
for (i = 0; i < 100; i++) {
TEST_ASSERT(espos_timer_start(global_timer) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, ESPOS_MAX_DELAY) == 0);
}
TEST_ASSERT(count == 100);
TEST_ASSERT(espos_sem_del(sync_sema) == 0);
TEST_ASSERT(espos_timer_del(global_timer) == 0);
}
TEST_CASE("ESPOS timer auto run test", "[espos]")
{
uint32_t i;
espos_sem_create(&sync_sema, 1, 0);
TEST_ASSERT(espos_timer_create(&global_timer, TIMER_NAME, espos_test_timer_cb, NULL,
espos_ms_to_ticks(20), ESPOS_TIMER_AUTO_RUN) == 0);
count = 0;
TEST_ASSERT(espos_timer_start(global_timer) == 0);
for (i = 0; i < 100; i++) {
espos_sem_take(sync_sema, ESPOS_MAX_DELAY);
}
TEST_ASSERT(espos_timer_stop(global_timer) == 0);
TEST_ASSERT(count == 100);
TEST_ASSERT(espos_sem_del(sync_sema) == 0);
TEST_ASSERT(espos_timer_del(global_timer) == 0);
}
TEST_CASE("ESPOS timer auto run test", "[espos]")
{
espos_timer_t timer;
TEST_ASSERT(espos_sem_create(&sync_sema, 1, 0) == 0);
TEST_ASSERT(espos_timer_create(&timer, TIMER_NAME, espos_test_timer_cb, NULL,
20, ESPOS_TIMER_NO_AUTO_RUN) == 0);
TEST_ASSERT(espos_timer_start(timer) == 0);
TEST_ASSERT(espos_timer_start(timer) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, 200) == 0);
TEST_ASSERT(espos_timer_stop(timer) == 0);
TEST_ASSERT(espos_timer_stop(timer) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, 200) == -ETIMEDOUT);
TEST_ASSERT(espos_timer_del(timer) == 0);
}
TEST_CASE("ESPOS timer change test", "[espos]")
{
TEST_ASSERT(espos_sem_create(&sync_sema, 1, 0) == 0);
TEST_ASSERT(espos_timer_create(&global_timer, TIMER_NAME, espos_test_timer_cb, NULL,
20, ESPOS_TIMER_AUTO_RUN) == 0);
count = 0;
TEST_ASSERT(espos_timer_start(global_timer) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, ESPOS_MAX_DELAY) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, 200) == 0);
TEST_ASSERT(espos_sem_take(sync_sema, 200) == 0);
TEST_ASSERT(espos_timer_stop(global_timer) == 0);
TEST_ASSERT(count == 3);
TEST_ASSERT(espos_sem_del(sync_sema) == 0);
TEST_ASSERT(espos_timer_del(global_timer) == 0);
}
TEST_CASE("ESPOS timer API param test", "[espos]")
{
espos_timer_t timer;
TEST_ASSERT(espos_timer_create(&timer, NULL, NULL, espos_test_timer_cb,
20, ESPOS_TIMER_NO_AUTO_RUN) == -EINVAL);
TEST_ASSERT(espos_timer_create(NULL, NULL, espos_test_timer_cb, NULL,
20, ESPOS_TIMER_AUTO_RUN) == -EINVAL);
TEST_ASSERT(espos_timer_create(&timer, NULL, NULL, NULL,
20, ESPOS_TIMER_AUTO_RUN) == -EINVAL);
TEST_ASSERT(espos_timer_create(&timer, NULL, NULL, espos_test_timer_cb,
20, ESPOS_TIMER_INVALID_OPT) == -EINVAL);
TEST_ASSERT(espos_timer_start(ESPOS_TIMER_INVALID_HANDLE) == -EINVAL);
TEST_ASSERT(espos_timer_stop(ESPOS_TIMER_INVALID_HANDLE) == -EINVAL);
TEST_ASSERT(espos_timer_change(ESPOS_TIMER_INVALID_HANDLE,
ESPOS_TIMER_CHANGE_ONCE, NULL) == -EINVAL);
TEST_ASSERT(espos_timer_change(timer,
ESPOS_TIMER_CHANGE_OPT_INVALID, NULL) == -EINVAL);
TEST_ASSERT(espos_timer_del(ESPOS_TIMER_INVALID_HANDLE) == -EINVAL);
}

View file

@ -0,0 +1,14 @@
src = Split('''
platform/rhino/espos_misc.c
platform/rhino/espos_mutex.c
platform/rhino/espos_queue.c
platform/rhino/espos_scheduler.c
platform/rhino/espos_semaphore.c
platform/rhino/espos_spinlock.c
platform/rhino/espos_task.c
platform/rhino/espos_time.c
platform/rhino/espos_timer.c
''')
component = aos_component('espos', src)
component.add_global_includes('include')