Merge pull request #512 from heyuanjie87/ForPullRequest

[CMSIS] 完善cmsis os接口
This commit is contained in:
Bernard Xiong 2015-07-05 08:34:02 +08:00
commit 9f84e429ef
2 changed files with 174 additions and 120 deletions

View File

@ -215,7 +215,7 @@ typedef struct rt_mailbox *osMailQId;
/// \note CAN BE CHANGED: \b os_thread_def is implementation specific in every CMSIS-RTOS. /// \note CAN BE CHANGED: \b os_thread_def is implementation specific in every CMSIS-RTOS.
typedef const struct os_thread_def { typedef const struct os_thread_def {
const char *name; const char *name;
void (*entry)(void *parameter); os_pthread entry;
rt_uint32_t stack_size; rt_uint32_t stack_size;
rt_uint8_t priority; rt_uint8_t priority;
rt_uint32_t tick; rt_uint32_t tick;
@ -225,7 +225,7 @@ typedef const struct os_thread_def {
/// \note CAN BE CHANGED: \b os_timer_def is implementation specific in every CMSIS-RTOS. /// \note CAN BE CHANGED: \b os_timer_def is implementation specific in every CMSIS-RTOS.
typedef const struct os_timer_def { typedef const struct os_timer_def {
const char *name; const char *name;
void (*timeout)(void *parameter); os_ptimer timeout;
void *parameter; void *parameter;
rt_tick_t time; rt_tick_t time;
rt_uint8_t flag; rt_uint8_t flag;
@ -257,8 +257,8 @@ typedef const struct os_pool_def {
/// \note CAN BE CHANGED: \b os_messageQ_def is implementation specific in every CMSIS-RTOS. /// \note CAN BE CHANGED: \b os_messageQ_def is implementation specific in every CMSIS-RTOS.
typedef const struct os_messageQ_def { typedef const struct os_messageQ_def {
const char *name; const char *name;
rt_size_t msg_size;
rt_size_t max_msgs; rt_size_t max_msgs;
rt_size_t msg_size;
rt_uint8_t flag; rt_uint8_t flag;
} osMessageQDef_t; } osMessageQDef_t;
@ -294,7 +294,7 @@ typedef struct {
/// \param[in] argument pointer that is passed to the thread function as start argument. /// \param[in] argument pointer that is passed to the thread function as start argument.
/// \return status code that indicates the execution status of the function. /// \return status code that indicates the execution status of the function.
/// \note MUST REMAIN UNCHANGED: \b osKernelStart shall be consistent in every CMSIS-RTOS. /// \note MUST REMAIN UNCHANGED: \b osKernelStart shall be consistent in every CMSIS-RTOS.
osStatus osKernelStart (osThreadDef_t *thread_def, void *argument); osStatus osKernelStart (void);
/// Check if the RTOS kernel is already started. /// Check if the RTOS kernel is already started.
/// \note MUST REMAIN UNCHANGED: \b osKernelRunning shall be consistent in every CMSIS-RTOS. /// \note MUST REMAIN UNCHANGED: \b osKernelRunning shall be consistent in every CMSIS-RTOS.
@ -498,6 +498,11 @@ osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec);
/// \note MUST REMAIN UNCHANGED: \b osMutexRelease shall be consistent in every CMSIS-RTOS. /// \note MUST REMAIN UNCHANGED: \b osMutexRelease shall be consistent in every CMSIS-RTOS.
osStatus osMutexRelease (osMutexId mutex_id); osStatus osMutexRelease (osMutexId mutex_id);
/// Delete a Mutex that was created by \ref osMutexCreate.
/// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
/// \return status code that indicates the execution status of the function.
/// \note MUST REMAIN UNCHANGED: \b osMutexDelete shall be consistent in every CMSIS-RTOS.
osStatus osMutexDelete (osMutexId mutex_id);
// ==== Semaphore Management Functions ==== // ==== Semaphore Management Functions ====
@ -560,7 +565,7 @@ extern osPoolDef_t os_pool_def_##name
#else // define the object #else // define the object
#define osPoolDef(name, no, type) \ #define osPoolDef(name, no, type) \
osPoolDef_t os_pool_def_##name = \ osPoolDef_t os_pool_def_##name = \
{ (no), sizeof(type), NULL } {"pool", (no), sizeof(type) }
#endif #endif
/// \brief Access a Memory Pool definition. /// \brief Access a Memory Pool definition.
@ -614,7 +619,7 @@ extern osMessageQDef_t os_messageQ_def_##name
#else // define the object #else // define the object
#define osMessageQDef(name, queue_sz, type) \ #define osMessageQDef(name, queue_sz, type) \
osMessageQDef_t os_messageQ_def_##name = \ osMessageQDef_t os_messageQ_def_##name = \
{ (queue_sz), sizeof (type) } {"msg", (queue_sz), 4 }
#endif #endif
/// \brief Access a Message Queue Definition. /// \brief Access a Message Queue Definition.

View File

@ -3,19 +3,17 @@
// Kernel Control Public API // Kernel Control Public API
/// Start the RTOS Kernel with executing the specified thread /// Start the RTOS Kernel with executing the specified thread
osStatus osKernelStart(osThreadDef_t *thread_def, void *argument) osStatus osKernelStart(void)
{ {
osThreadCreate(thread_def, argument); rt_system_scheduler_start();
rt_system_scheduler_start(); return osOK;
return osOK;
} }
/// Check if the RTOS kernel is already started /// Check if the RTOS kernel is already started
int32_t osKernelRunning(void) int32_t osKernelRunning(void)
{ {
return (rt_thread_self() != RT_NULL) ? 1 : 0; return (rt_thread_self() != RT_NULL) ? 1 : 0;
} }
// Thread Public API // Thread Public API
@ -23,76 +21,83 @@ int32_t osKernelRunning(void)
/// Create a thread and add it to Active Threads and set it to state READY /// Create a thread and add it to Active Threads and set it to state READY
osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument) osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument)
{ {
osThreadId thread; osThreadId thread;
int size;
thread = rt_thread_create(thread_def->name, thread_def->entry, argument, thread_def->stack_size, thread_def->priority, thread_def->tick); size = thread_def->stack_size;
if (thread != RT_NULL) if (size == 0)
{
size = 4096;
}
thread = rt_thread_create(thread_def->name, thread_def->entry, argument, size, thread_def->priority, thread_def->tick);
if (thread != RT_NULL)
rt_thread_startup(thread); rt_thread_startup(thread);
return thread; return thread;
} }
/// Return the thread ID of the current running thread /// Return the thread ID of the current running thread
osThreadId osThreadGetId(void) osThreadId osThreadGetId(void)
{ {
return rt_thread_self(); return rt_thread_self();
} }
/// Terminate execution of a thread and remove it from ActiveThreads /// Terminate execution of a thread and remove it from ActiveThreads
osStatus osThreadTerminate(osThreadId thread_id) osStatus osThreadTerminate(osThreadId thread_id)
{ {
rt_err_t result; rt_err_t result;
result = rt_thread_delete(thread_id); result = rt_thread_delete(thread_id);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Pass control to next thread that is in state READY /// Pass control to next thread that is in state READY
osStatus osThreadYield(void) osStatus osThreadYield(void)
{ {
rt_err_t result; rt_err_t result;
result = rt_thread_yield(); result = rt_thread_yield();
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Change prority of an active thread /// Change prority of an active thread
osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority) osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
{ {
rt_err_t result; rt_err_t result;
if (thread_id == RT_NULL) if (thread_id == RT_NULL)
return osErrorOS; return osErrorOS;
if (priority < osPriorityIdle || priority > osPriorityRealtime) if (priority < osPriorityIdle || priority > osPriorityRealtime)
return osErrorPriority; return osErrorPriority;
result = rt_thread_control(thread_id, RT_THREAD_CTRL_CHANGE_PRIORITY, &priority); result = rt_thread_control(thread_id, RT_THREAD_CTRL_CHANGE_PRIORITY, &priority);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Get current prority of an active thread /// Get current prority of an active thread
osPriority osThreadGetPriority(osThreadId thread_id) osPriority osThreadGetPriority(osThreadId thread_id)
{ {
if (thread_id == RT_NULL) if (thread_id == RT_NULL)
return osErrorOS; return osPriorityError;
if (thread_id->current_priority < osPriorityIdle || thread_id->current_priority > osPriorityRealtime) if ((osPriority)thread_id->current_priority < osPriorityIdle || (osPriority)thread_id->current_priority > osPriorityRealtime)
return osPriorityError; return osPriorityError;
return thread_id->current_priority; return thread_id->current_priority;
} }
// Generic Wait API // Generic Wait API
@ -100,31 +105,31 @@ osPriority osThreadGetPriority(osThreadId thread_id)
/// Wait for Timeout (Time Delay) /// Wait for Timeout (Time Delay)
osStatus osDelay(uint32_t millisec) osStatus osDelay(uint32_t millisec)
{ {
rt_err_t result; rt_err_t result;
rt_tick_t ticks; rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec); ticks = rt_tick_from_millisecond(millisec);
result = rt_thread_delay(ticks); result = rt_thread_delay(ticks);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Wait for Signal, Message, Mail, or Timeout /// Wait for Signal, Message, Mail, or Timeout
osEvent osWait(uint32_t millisec) osEvent osWait(uint32_t millisec)
{ {
rt_err_t result; rt_err_t result;
rt_tick_t ticks; rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec); ticks = rt_tick_from_millisecond(millisec);
result = rt_thread_delay(ticks); result = rt_thread_delay(ticks);
/* /*
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
*/ */
} }
@ -133,31 +138,41 @@ osEvent osWait(uint32_t millisec)
/// Create timer /// Create timer
osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument) osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument)
{ {
return rt_timer_create(timer_def->name, timer_def->timeout, timer_def->parameter, timer_def->time, timer_def->flag); uint8_t flag = RT_TIMER_FLAG_SOFT_TIMER;
if (type == osTimerPeriodic)
{
flag |= RT_TIMER_FLAG_PERIODIC;
}
return rt_timer_create(timer_def->name, timer_def->timeout, argument, timer_def->time, flag);
} }
/// Start or restart timer /// Start or restart timer
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec) osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
{ {
rt_err_t result; rt_err_t result;
rt_tick_t ticks;
result = rt_timer_start(timer_id); ticks = rt_tick_from_millisecond(millisec);
if (result == RT_EOK) rt_timer_control(timer_id, RT_TIMER_CTRL_SET_TIME, &ticks);
return osOK; result = rt_timer_start(timer_id);
else if (result == RT_EOK)
return osErrorOS; return osOK;
else
return osErrorOS;
} }
/// Stop timer /// Stop timer
osStatus osTimerStop(osTimerId timer_id) osStatus osTimerStop(osTimerId timer_id)
{ {
rt_err_t result; rt_err_t result;
result = rt_timer_stop(timer_id); result = rt_timer_stop(timer_id);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
// Mutex Public API // Mutex Public API
@ -165,35 +180,47 @@ osStatus osTimerStop(osTimerId timer_id)
/// Create and Initialize a Mutex object /// Create and Initialize a Mutex object
osMutexId osMutexCreate(osMutexDef_t *mutex_def) osMutexId osMutexCreate(osMutexDef_t *mutex_def)
{ {
return rt_mutex_create(mutex_def->name, mutex_def->flag); return rt_mutex_create(mutex_def->name, mutex_def->flag);
} }
/// Wait until a Mutex becomes available /// Wait until a Mutex becomes available
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec) osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec)
{ {
rt_err_t result; rt_err_t result;
rt_tick_t ticks; rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec); ticks = rt_tick_from_millisecond(millisec);
result = rt_mutex_take(mutex_id, ticks); result = rt_mutex_take(mutex_id, ticks);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Release a Mutex that was obtained with osMutexWait /// Release a Mutex that was obtained with osMutexWait
osStatus osMutexRelease(osMutexId mutex_id) osStatus osMutexRelease(osMutexId mutex_id)
{ {
rt_err_t result; rt_err_t result;
result = rt_mutex_release(mutex_id); result = rt_mutex_release(mutex_id);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
}
osStatus osMutexDelete (osMutexId mutex_id)
{
rt_err_t result;
result = rt_mutex_delete(mutex_id);
if (result == RT_EOK)
return osOK;
else
return osErrorOS;
} }
// Semaphore Public API // Semaphore Public API
@ -201,34 +228,34 @@ osStatus osMutexRelease(osMutexId mutex_id)
/// Create and Initialize a Semaphore object /// Create and Initialize a Semaphore object
osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count) osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count)
{ {
return rt_sem_create(semaphore_def->name, count, semaphore_def->flag); return rt_sem_create(semaphore_def->name, count, semaphore_def->flag);
} }
/// Wait until a Semaphore becomes available /// Wait until a Semaphore becomes available
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec) int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
{ {
rt_tick_t ticks; rt_tick_t ticks;
if (semaphore_id == RT_NULL) if (semaphore_id == RT_NULL)
return -1; return -1;
ticks = rt_tick_from_millisecond(millisec); ticks = rt_tick_from_millisecond(millisec);
rt_sem_take(semaphore_id, ticks); rt_sem_take(semaphore_id, ticks);
return semaphore_id->value; return semaphore_id->value;
} }
/// Release a Semaphore /// Release a Semaphore
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id) osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
{ {
rt_err_t result; rt_err_t result;
result = rt_sem_release(semaphore_id); result = rt_sem_release(semaphore_id);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
// Memory Management Public API // Memory Management Public API
@ -236,26 +263,27 @@ osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
/// Create and Initialize memory pool /// Create and Initialize memory pool
osPoolId osPoolCreate(osPoolDef_t *pool_def) osPoolId osPoolCreate(osPoolDef_t *pool_def)
{ {
return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size); return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size);
} }
/// Allocate a memory block from a memory pool /// Allocate a memory block from a memory pool
void *osPoolAlloc(osPoolId pool_id) void *osPoolAlloc(osPoolId pool_id)
{ {
return rt_mp_alloc(pool_id, 0); return rt_mp_alloc(pool_id, 0);
} }
/// Allocate a memory block from a memory pool and set memory block to zero /// Allocate a memory block from a memory pool and set memory block to zero
void *osPoolCAlloc(osPoolId pool_id) void *osPoolCAlloc(osPoolId pool_id)
{ {
return RT_NULL;
} }
/// Return an allocated memory block back to a specific memory pool /// Return an allocated memory block back to a specific memory pool
osStatus osPoolFree(osPoolId pool_id, void *block) osStatus osPoolFree(osPoolId pool_id, void *block)
{ {
rt_mp_free(block); rt_mp_free(block);
return osOK; return osOK;
} }
// Message Queue Management Public API // Message Queue Management Public API
@ -263,26 +291,42 @@ osStatus osPoolFree(osPoolId pool_id, void *block)
/// Create and Initialize Message Queue /// Create and Initialize Message Queue
osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id) osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id)
{ {
return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag); return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag);
} }
/// Put a Message to a Queue /// Put a Message to a Queue
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec) osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
{ {
rt_err_t result; rt_err_t result;
result = rt_mq_send(queue_id, &info, 1); result = rt_mq_send(queue_id, &info, 4);
if (result == RT_EOK) if (result == RT_EOK)
return osOK; return osOK;
else else
return osErrorOS; return osErrorOS;
} }
/// Get a Message or Wait for a Message from a Queue /// Get a Message or Wait for a Message from a Queue
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec) osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
{ {
osEvent event;
rt_err_t result;
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(millisec);
result = rt_mq_recv(queue_id, &event.value, 4, ticks);
if (result == RT_EOK)
{
event.status = osEventMessage;
}
else
{
event.status = osEventTimeout;
}
return event;
} }
// Mail Queue Management Public API // Mail Queue Management Public API
@ -290,26 +334,31 @@ osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
/// Create and Initialize mail queue /// Create and Initialize mail queue
osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id) osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id)
{ {
return RT_NULL;
} }
/// Allocate a memory block from a mail /// Allocate a memory block from a mail
void *osMailAlloc(osMailQId queue_id, uint32_t millisec) void *osMailAlloc(osMailQId queue_id, uint32_t millisec)
{ {
return RT_NULL;
} }
/// Allocate a memory block from a mail and set memory block to zero /// Allocate a memory block from a mail and set memory block to zero
void *osMailCAlloc(osMailQId queue_id, uint32_t millisec) void *osMailCAlloc(osMailQId queue_id, uint32_t millisec)
{ {
return RT_NULL;
} }
/// Free a memory block from a mail /// Free a memory block from a mail
osStatus osMailFree(osMailQId queue_id, void *mail) osStatus osMailFree(osMailQId queue_id, void *mail)
{ {
return osErrorOS;
} }
/// Put a mail to a queue /// Put a mail to a queue
osStatus osMailPut(osMailQId queue_id, void *mail) osStatus osMailPut(osMailQId queue_id, void *mail)
{ {
return osErrorOS;
} }
/// Get a mail from a queue /// Get a mail from a queue