a bugfix for mail queue and printf clipping switch

1. add TOS_CFG_LIBC_PRINTF_EN
1: enable printf to implement tos_kprintf
when you call tos_kprintf(actually currently it's a MACRO rather than a function) , the libc will be invloved into the image, most of the time it means a larger image size.

0: disable printf, means tos_kprintf is defined as an empty MACRO
most of the time, means you will not get a log output from the uart(currently we donnot have a console yet) but a smaller image size.

2. fix a bug of tos_mail_q and tos_priority_mail_q
if we are post-ing a mail and there is[are] task[s] just pend-ing for the mail queue, the mail delivered to the pend-er will be a pointer, and probably it's a pointer to the local variable on the stack. Actually we should do a memcpy rather than a pointer assignment. for more details, see the patch.
This commit is contained in:
daishengdong
2019-11-25 21:04:37 +08:00
parent 7b7c806f2a
commit 96cf620708
20 changed files with 68 additions and 59 deletions

View File

@@ -19,8 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
#endif
return 0;
#endif
}

View File

@@ -19,7 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
return 0;
#endif
return 0;
}

View File

@@ -19,7 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
return 0;
#endif
return 0;
}

View File

@@ -19,7 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
return 0;
#endif
return 0;
}

View File

@@ -19,7 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
return 0;
#endif
return 0;
}

View File

@@ -19,7 +19,8 @@ int tos_bsp_tickless_setup(void)
tos_tickless_wkup_alarm_init(TOS_LOW_POWER_MODE_STANDBY);
tos_pm_cpu_lpwr_mode_set(TOS_LOW_POWER_MODE_SLEEP);
return 0;
#endif
return 0;
}

View File

@@ -32,7 +32,6 @@
#include <tos_fault.h>
#include <tos_klib.h>
#include <tos_list.h>
#include <tos_pm.h>
#include <tos_pend.h>
#include <tos_sys.h>
#include <tos_ring_queue.h>

View File

@@ -230,6 +230,10 @@
#define TOS_CFG_OBJECT_VERIFY_EN 0u
#endif
#ifndef TOS_CFG_LIBC_PRINTF_EN
#define TOS_CFG_LIBC_PRINTF_EN 1u // we enable this by default
#endif
#if (TOS_CFG_TIMER_AS_PROC == 0u) && !defined(TOS_CFG_TIMER_TASK_PRIO)
#define TOS_CFG_TIMER_TASK_PRIO (k_prio_t)(K_TASK_PRIO_IDLE - (k_prio_t)1u)
#endif

View File

@@ -31,14 +31,14 @@
#define TOS_PTR_SANITY_CHECK(ptr) \
do { \
if (unlikely((ptr) == K_NULL)) { \
if (unlikely(!(ptr))) { \
return K_ERR_OBJ_PTR_NULL; \
} \
} while(0)
#define TOS_PTR_SANITY_CHECK_RC(ptr, return_code) \
do { \
if (unlikely((ptr) == K_NULL)) { \
if (unlikely(!(ptr))) { \
return return_code; \
} \
} while(0)
@@ -70,12 +70,18 @@
#define TOS_OBJ_VERIFY_RC(obj, obj_type, return_code)
#endif
#if TOS_CFG_LIBC_PRINTF_EN > 0u
#define LIBC_PRINTF printf
#else
#define LIBC_PRINTF(...)
#endif
// currently we use default microlib supplied by mdk
#define tos_kprintf(...) printf(__VA_ARGS__);
#define tos_kprintf(...) LIBC_PRINTF(__VA_ARGS__);
#define tos_kprintln(...) \
printf(__VA_ARGS__); \
printf("\n");
LIBC_PRINTF(__VA_ARGS__); \
LIBC_PRINTF("\n");
#define TOS_ASSERT_AUX(exp, function, line) \
if (!(exp)) { \

View File

@@ -416,8 +416,9 @@ __KERNEL__ __STATIC_INLINE__ void task_state_set_sleeping(k_task_t *task)
__DEBUG__ __STATIC_INLINE__ void task_default_walker(k_task_t *task)
{
char *state_str;
char *state_str = "ABNORMAL";
state_str = state_str;
tos_kprintln("tsk name: %s", task->name);
if (task->state == K_TASK_STATE_PENDTIMEOUT_SUSPENDED) {

View File

@@ -32,7 +32,7 @@ __API__ void tos_tick_handler(void);
__KERNEL__ void tick_update(k_tick_t tick);
__KERNEL__ k_err_t tick_list_add(k_task_t *task, k_tick_t timeout);
__KERNEL__ void tick_list_add(k_task_t *task, k_tick_t timeout);
__KERNEL__ void tick_list_remove(k_task_t *task);

View File

@@ -21,9 +21,10 @@
__API__ k_err_t tos_mail_q_create(k_mail_q_t *mail_q, void *pool, size_t mail_cnt, size_t mail_size)
{
TOS_PTR_SANITY_CHECK(mail_q);
k_err_t err;
TOS_PTR_SANITY_CHECK(mail_q);
err = tos_ring_q_create(&mail_q->ring_q, pool, mail_cnt, mail_size);
if (err != K_ERR_NONE) {
return err;
@@ -86,9 +87,10 @@ __API__ k_err_t tos_mail_q_destroy(k_mail_q_t *mail_q)
__API__ k_err_t tos_mail_q_create_dyn(k_mail_q_t *mail_q, size_t mail_cnt, size_t mail_size)
{
TOS_PTR_SANITY_CHECK(mail_q);
k_err_t err;
TOS_PTR_SANITY_CHECK(mail_q);
err = tos_ring_q_create_dyn(&mail_q->ring_q, mail_cnt, mail_size);
if (err != K_ERR_NONE) {
return err;
@@ -178,15 +180,14 @@ __API__ k_err_t tos_mail_q_pend(k_mail_q_t *mail_q, void *mail_buf, size_t *mail
return K_ERR_PEND_SCHED_LOCKED;
}
k_curr_task->mail = mail_buf;
pend_task_block(k_curr_task, &mail_q->pend_obj, timeout);
TOS_CPU_INT_ENABLE();
knl_sched();
err = pend_state2errno(k_curr_task->pend_state);
if (err == K_ERR_NONE) {
memcpy(mail_buf, k_curr_task->mail, k_curr_task->mail_size);
*mail_size = k_curr_task->mail_size;
k_curr_task->mail = K_NULL;
k_curr_task->mail_size = 0;
@@ -197,7 +198,7 @@ __API__ k_err_t tos_mail_q_pend(k_mail_q_t *mail_q, void *mail_buf, size_t *mail
__STATIC__ void mail_task_recv(k_task_t *task, void *mail_buf, size_t mail_size)
{
task->mail = mail_buf;
memcpy(task->mail, mail_buf, mail_size);
task->mail_size = mail_size;
pend_task_wakeup(task, PEND_STATE_POST);
}

View File

@@ -187,7 +187,6 @@ __API__ k_err_t tos_msg_q_pend(k_msg_q_t *msg_q, void **msg_ptr, k_tick_t timeou
knl_sched();
err = pend_state2errno(k_curr_task->pend_state);
if (err == K_ERR_NONE) {
*msg_ptr = k_curr_task->msg;
k_curr_task->msg = K_NULL;

View File

@@ -69,9 +69,8 @@ __KERNEL__ void mutex_release(k_mutex_t *mutex)
__API__ k_err_t tos_mutex_create(k_mutex_t *mutex)
{
TOS_PTR_SANITY_CHECK(mutex);
TOS_IN_IRQ_CHECK();
TOS_PTR_SANITY_CHECK(mutex);
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_object_init(&mutex->knl_obj, KNL_OBJ_TYPE_MUTEX);
@@ -90,8 +89,8 @@ __API__ k_err_t tos_mutex_destroy(k_mutex_t *mutex)
{
TOS_CPU_CPSR_ALLOC();
TOS_PTR_SANITY_CHECK(mutex);
TOS_IN_IRQ_CHECK();
TOS_PTR_SANITY_CHECK(mutex);
TOS_OBJ_VERIFY(mutex, KNL_OBJ_TYPE_MUTEX);
TOS_CPU_INT_DISABLE();
@@ -120,8 +119,8 @@ __API__ k_err_t tos_mutex_pend_timed(k_mutex_t *mutex, k_tick_t timeout)
{
TOS_CPU_CPSR_ALLOC();
TOS_PTR_SANITY_CHECK(mutex);
TOS_IN_IRQ_CHECK();
TOS_PTR_SANITY_CHECK(mutex);
TOS_OBJ_VERIFY(mutex, KNL_OBJ_TYPE_MUTEX);
TOS_CPU_INT_DISABLE();
@@ -176,8 +175,8 @@ __API__ k_err_t tos_mutex_post(k_mutex_t *mutex)
TOS_CPU_CPSR_ALLOC();
k_task_t *pending_task;
TOS_PTR_SANITY_CHECK(mutex);
TOS_IN_IRQ_CHECK();
TOS_PTR_SANITY_CHECK(mutex);
TOS_OBJ_VERIFY(mutex, KNL_OBJ_TYPE_MUTEX);
TOS_CPU_INT_DISABLE();

View File

@@ -21,10 +21,11 @@
__API__ k_err_t tos_prio_mail_q_create(k_prio_mail_q_t *prio_mail_q, void *pool, size_t mail_cnt, size_t mail_size)
{
TOS_PTR_SANITY_CHECK(prio_mail_q);
k_err_t err;
void *prio_q_mgr_array = K_NULL;
TOS_PTR_SANITY_CHECK(prio_mail_q);
prio_q_mgr_array = tos_mmheap_alloc(TOS_PRIO_Q_MGR_ARRAY_SIZE(mail_cnt));
if (!prio_q_mgr_array) {
return K_ERR_OUT_OF_MEMORY;
@@ -89,9 +90,10 @@ __API__ k_err_t tos_prio_mail_q_destroy(k_prio_mail_q_t *prio_mail_q)
__API__ k_err_t tos_prio_mail_q_create_dyn(k_prio_mail_q_t *prio_mail_q, size_t mail_cnt, size_t mail_size)
{
TOS_PTR_SANITY_CHECK(prio_mail_q);
k_err_t err;
TOS_PTR_SANITY_CHECK(prio_mail_q);
err = tos_prio_q_create_dyn(&prio_mail_q->prio_q, mail_cnt, mail_size);
if (err != K_ERR_NONE) {
return err;
@@ -179,15 +181,14 @@ __API__ k_err_t tos_prio_mail_q_pend(k_prio_mail_q_t *prio_mail_q, void *mail_bu
return K_ERR_PEND_SCHED_LOCKED;
}
k_curr_task->mail = mail_buf;
pend_task_block(k_curr_task, &prio_mail_q->pend_obj, timeout);
TOS_CPU_INT_ENABLE();
knl_sched();
err = pend_state2errno(k_curr_task->pend_state);
if (err == K_ERR_NONE) {
memcpy(mail_buf, k_curr_task->mail, k_curr_task->mail_size);
*mail_size = k_curr_task->mail_size;
k_curr_task->mail = K_NULL;
k_curr_task->mail_size = 0;
@@ -198,7 +199,7 @@ __API__ k_err_t tos_prio_mail_q_pend(k_prio_mail_q_t *prio_mail_q, void *mail_bu
__STATIC__ void prio_mail_task_recv(k_task_t *task, void *mail_buf, size_t mail_size)
{
task->mail = mail_buf;
memcpy(task->mail, mail_buf, mail_size);
task->mail_size = mail_size;
pend_task_wakeup(task, PEND_STATE_POST);
}

View File

@@ -21,10 +21,11 @@
__API__ k_err_t tos_prio_msg_q_create(k_prio_msg_q_t *prio_msg_q, void *pool, size_t msg_cnt)
{
TOS_PTR_SANITY_CHECK(prio_msg_q);
k_err_t err;
void *prio_q_mgr_array = K_NULL;
TOS_PTR_SANITY_CHECK(prio_msg_q);
prio_q_mgr_array = tos_mmheap_alloc(TOS_PRIO_Q_MGR_ARRAY_SIZE(msg_cnt));
if (!prio_q_mgr_array) {
return K_ERR_OUT_OF_MEMORY;
@@ -89,9 +90,10 @@ __API__ k_err_t tos_prio_msg_q_destroy(k_prio_msg_q_t *prio_msg_q)
__API__ k_err_t tos_prio_msg_q_create_dyn(k_prio_msg_q_t *prio_msg_q, size_t msg_cnt)
{
TOS_PTR_SANITY_CHECK(prio_msg_q);
k_err_t err;
TOS_PTR_SANITY_CHECK(prio_msg_q);
err = tos_prio_q_create_dyn(&prio_msg_q->prio_q, msg_cnt, sizeof(void *));
if (err != K_ERR_NONE) {
return err;
@@ -188,7 +190,6 @@ __API__ k_err_t tos_prio_msg_q_pend(k_prio_msg_q_t *prio_msg_q, void **msg_ptr,
knl_sched();
err = pend_state2errno(k_curr_task->pend_state);
if (err == K_ERR_NONE) {
*msg_ptr = k_curr_task->msg;
k_curr_task->msg = K_NULL;

View File

@@ -250,6 +250,10 @@ __KERNEL__ void knl_sched(void)
{
TOS_CPU_CPSR_ALLOC();
if (unlikely(!tos_knl_is_running())) {
return;
}
if (knl_is_inirq()) {
return;
}

View File

@@ -488,14 +488,14 @@ __API__ k_err_t tos_task_delay(k_tick_t delay)
return K_ERR_NONE;
}
TOS_CPU_INT_DISABLE();
if (tick_list_add(k_curr_task, delay) != K_ERR_NONE) {
TOS_CPU_INT_ENABLE();
if (unlikely(delay == TOS_TIME_FOREVER)) {
// if you wanna delay your task forever, why don't just suspend?
return K_ERR_DELAY_FOREVER;
}
TOS_CPU_INT_DISABLE();
tick_list_add(k_curr_task, delay);
readyqueue_remove(k_curr_task);
TOS_CPU_INT_ENABLE();

View File

@@ -71,21 +71,10 @@ __STATIC__ void tick_task_takeoff(k_task_t *task)
TOS_CPU_INT_ENABLE();
}
__KERNEL__ k_err_t tick_list_add(k_task_t *task, k_tick_t timeout)
__KERNEL__ void tick_list_add(k_task_t *task, k_tick_t timeout)
{
if (timeout == TOS_TIME_NOWAIT) {
return K_ERR_DELAY_ZERO;
}
if (timeout == TOS_TIME_FOREVER) {
return K_ERR_DELAY_FOREVER;
}
task->tick_expires = timeout;
tick_task_place(task, timeout);
task_state_set_sleeping(task);
return K_ERR_NONE;
}
__KERNEL__ void tick_list_remove(k_task_t *task)

View File

@@ -305,7 +305,7 @@ __STATIC__ void at_parser(void *arg)
at_echo_buffer_copy(recv_cache, at_echo);
}
printf("--->%s\n", recv_cache->buffer);
tos_kprintln("--->%s", recv_cache->buffer);
}
}