diff --git a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/test/TencentOS_tiny.uvprojx b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/test/TencentOS_tiny.uvprojx index f74c1969..775df288 100644 --- a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/test/TencentOS_tiny.uvprojx +++ b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/test/TencentOS_tiny.uvprojx @@ -10,14 +10,14 @@ TencentOS_tiny 0x4 ARM-ADS - 5060750::V5.06 update 6 (build 750)::ARMCC + 5060750::V5.06 update 6 (build 750)::.\ARMCC 0 STM32L431RCTx STMicroelectronics - Keil.STM32L4xx_DFP.2.2.0 - http://www.keil.com/pack + Keil.STM32L4xx_DFP.2.3.0 + https://www.keil.com/pack/ IRAM(0x20000000-0x2000FFFF) IROM(0x8000000-0x803FFFF) CLOCK(8000000) FPU2 CPUTYPE("Cortex-M4") @@ -185,6 +185,7 @@ 0 2 0 + 0 0 0 8 @@ -351,7 +352,7 @@ 0 0 0 - 0 + 4 @@ -879,4 +880,19 @@ + + + + <Project Info> + + + + + + 0 + 1 + + + + diff --git a/kernel/core/include/tos_kerr.h b/kernel/core/include/tos_kerr.h index b0a2954b..e0cefe2b 100644 --- a/kernel/core/include/tos_kerr.h +++ b/kernel/core/include/tos_kerr.h @@ -124,6 +124,10 @@ typedef enum k_err_en { K_ERR_TIMER_INVALID_OPT, K_ERR_TIMER_STOPPED, K_ERR_TIMER_RUNNING, + + K_ERR_SEM_OUT_OF_MEMORY = 2200u, + + K_ERR_MUTEX_OUT_OF_MEMORY = 2300u, } k_err_t; #endif /* _TOS_ERR_H_ */ diff --git a/kernel/core/include/tos_mutex.h b/kernel/core/include/tos_mutex.h index 60a8c65a..5ce2e341 100644 --- a/kernel/core/include/tos_mutex.h +++ b/kernel/core/include/tos_mutex.h @@ -47,6 +47,19 @@ typedef struct k_mutex_st { */ __API__ k_err_t tos_mutex_create(k_mutex_t *mutex); +/** + * @brief Create a dynamic mutex. + * create a dynamic mutex. + * + * @attention None + * + * @param[in] mutex pointer to the pointer of the mutex. + * + * @return errcode + * @retval #K_ERR_NONE return successfully. + */ +__API__ k_err_t tos_mutex_create_dyn(k_mutex_t **mutex); + /** * @brief Destroy a mutex. * destroy a mutex. diff --git a/kernel/core/include/tos_sem.h b/kernel/core/include/tos_sem.h index 4a94675b..664186dd 100644 --- a/kernel/core/include/tos_sem.h +++ b/kernel/core/include/tos_sem.h @@ -61,6 +61,35 @@ __API__ k_err_t tos_sem_create_max(k_sem_t *sem, k_sem_cnt_t init_count, k_sem_c */ __API__ k_err_t tos_sem_create(k_sem_t *sem, k_sem_cnt_t init_count); +/** + * @brief Create a dynamic semaphore with a limitation of maximum count. + * create a dynamic semaphore with a limitation of maximum count. + * + * @attention None + * + * @param[in] sem pointer to the pointer of the semaphore. + * @param[in] init_count initial count of the semaphore. + * @param[in] max_count maximum count of the semaphore. + * + * @return errcode + * @retval #K_ERR_NONE return successfully. + */ +__API__ k_err_t tos_sem_create_max_dyn(k_sem_t **sem, k_sem_cnt_t init_count, k_sem_cnt_t max_count); + +/** + * @brief Create a dynamic semaphore. + * create a dynamic semaphore. + * + * @attention None + * + * @param[in] sem pointer to the pointer of the semaphore. + * @param[in] init_count initial count of the semaphore. + * + * @return errcode + * @retval #K_ERR_NONE return successfully. + */ +__API__ k_err_t tos_sem_create_dyn(k_sem_t **sem, k_sem_cnt_t init_count); + /** * @brief Destroy a semaphore. * destroy a semaphore. diff --git a/kernel/core/tos_mutex.c b/kernel/core/tos_mutex.c index 73fe9cc1..e19085f0 100644 --- a/kernel/core/tos_mutex.c +++ b/kernel/core/tos_mutex.c @@ -83,6 +83,32 @@ __API__ k_err_t tos_mutex_create(k_mutex_t *mutex) return K_ERR_NONE; } +__API__ k_err_t tos_mutex_create_dyn(k_mutex_t **mutex) +{ + k_mutex_t *the_mutex; + + TOS_IN_IRQ_CHECK(); + TOS_PTR_SANITY_CHECK(mutex); + + the_mutex = tos_mmheap_calloc(1, sizeof(k_mutex_t)); + if (!the_mutex) { + return K_ERR_MUTEX_OUT_OF_MEMORY; + } + + pend_object_init(&the_mutex->pend_obj); + the_mutex->pend_nesting = (k_nesting_t)0u; + the_mutex->owner = K_NULL; + the_mutex->owner_orig_prio = K_TASK_PRIO_INVALID; + tos_list_init(&the_mutex->owner_anchor); + TOS_OBJ_INIT(the_mutex, KNL_OBJ_TYPE_MUTEX); + + knl_object_alloc_set_dynamic(&the_mutex->knl_obj); + + *mutex = the_mutex; + + return K_ERR_NONE; +} + __API__ k_err_t tos_mutex_destroy(k_mutex_t *mutex) { TOS_CPU_CPSR_ALLOC(); @@ -101,7 +127,12 @@ __API__ k_err_t tos_mutex_destroy(k_mutex_t *mutex) pend_object_deinit(&mutex->pend_obj); - TOS_OBJ_DEINIT(mutex); + if (knl_object_alloc_is_dynamic(&mutex->knl_obj)) { + TOS_OBJ_DEINIT(mutex); + tos_mmheap_free(mutex); + } else { + TOS_OBJ_DEINIT(mutex); + } TOS_CPU_INT_ENABLE(); knl_sched(); diff --git a/kernel/core/tos_sem.c b/kernel/core/tos_sem.c index c0ddbde1..56d8b426 100644 --- a/kernel/core/tos_sem.c +++ b/kernel/core/tos_sem.c @@ -41,6 +41,40 @@ __API__ k_err_t tos_sem_create(k_sem_t *sem, k_sem_cnt_t init_count) return tos_sem_create_max(sem, init_count, (k_sem_cnt_t)-1); } +__API__ k_err_t tos_sem_create_max_dyn(k_sem_t **sem, k_sem_cnt_t init_count, k_sem_cnt_t max_count) +{ + k_sem_t *the_sem; + + TOS_IN_IRQ_CHECK(); + TOS_PTR_SANITY_CHECK(sem); + + if (unlikely(init_count > max_count)) { + init_count = max_count; + } + + the_sem = tos_mmheap_calloc(1, sizeof(k_sem_t)); + if (!the_sem) { + return K_ERR_SEM_OUT_OF_MEMORY; + } + + the_sem->count = init_count; + the_sem->count_max = max_count; + + pend_object_init(&the_sem->pend_obj); + TOS_OBJ_INIT(the_sem, KNL_OBJ_TYPE_SEMAPHORE); + + knl_object_alloc_set_dynamic(&the_sem->knl_obj); + + *sem = the_sem; + + return K_ERR_NONE; +} + +__API__ k_err_t tos_sem_create_dyn(k_sem_t **sem, k_sem_cnt_t init_count) +{ + return tos_sem_create_max_dyn(sem, init_count, (k_sem_cnt_t)-1); +} + __API__ k_err_t tos_sem_destroy(k_sem_t *sem) { TOS_CPU_CPSR_ALLOC(); @@ -54,7 +88,12 @@ __API__ k_err_t tos_sem_destroy(k_sem_t *sem) pend_object_deinit(&sem->pend_obj); - TOS_OBJ_DEINIT(sem); + if (knl_object_alloc_is_dynamic(&sem->knl_obj)) { + TOS_OBJ_DEINIT(sem); + tos_mmheap_free(sem); + } else { + TOS_OBJ_DEINIT(sem); + } TOS_CPU_INT_ENABLE(); knl_sched(); diff --git a/test/suit_mutex.c b/test/suit_mutex.c index 39260946..197e2d21 100644 --- a/test/suit_mutex.c +++ b/test/suit_mutex.c @@ -10,6 +10,7 @@ SUITE(suit_mutex); k_mutex_t test_mutex_00; k_mutex_t test_mutex_01; k_mutex_t test_mutex_02; +k_mutex_t *test_mutex_dyn_00; static void test_mutex_pend_task_entry(void *arg) { @@ -127,6 +128,19 @@ TEST test_tos_mutex_create(void) PASS(); } +TEST test_tos_mutex_create_dyn(void) +{ + k_err_t err; + + err = tos_mutex_create_dyn(&test_mutex_dyn_00); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_mutex_destroy(test_mutex_dyn_00); + ASSERT_EQ(err, K_ERR_NONE); + + PASS(); +} + TEST test_tos_mutex_destroy(void) { k_err_t err; @@ -406,6 +420,7 @@ SUITE(suit_mutex) { RUN_TEST(test_tos_mutex_create); RUN_TEST(test_tos_mutex_destroy); + RUN_TEST(test_tos_mutex_create_dyn); RUN_TEST(test_tos_mutex_pend); RUN_TEST(test_tos_mutex_pend_timed); RUN_TEST(test_tos_mutex_post); diff --git a/test/suit_sem.c b/test/suit_sem.c index 58bd0ee6..a61869bb 100644 --- a/test/suit_sem.c +++ b/test/suit_sem.c @@ -10,7 +10,9 @@ SUITE(suit_sem); k_sem_t test_sem_00; k_sem_t test_sem_01; k_sem_t test_sem_02; - +k_sem_t *test_sem_dyn_00; +k_sem_t *test_sem_dyn_01; +k_sem_t *test_sem_dyn_02; static void test_sem_pend_task_entry(void *arg) { k_err_t err; @@ -81,6 +83,31 @@ TEST test_tos_sem_create(void) PASS(); } +TEST test_tos_sem_create_dyn(void) +{ + k_err_t err; + + err = tos_sem_create_dyn(&test_sem_dyn_00, (k_sem_cnt_t)0); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_sem_create_dyn(&test_sem_dyn_01, (k_sem_cnt_t)0xFF); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_sem_create_dyn(&test_sem_dyn_02, (k_sem_cnt_t)0xFFFF); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_sem_destroy(test_sem_dyn_00); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_sem_destroy(test_sem_dyn_01); + ASSERT_EQ(err, K_ERR_NONE); + + err = tos_sem_destroy(test_sem_dyn_02); + ASSERT_EQ(err, K_ERR_NONE); + + PASS(); +} + TEST test_tos_sem_destroy(void) { k_err_t err; @@ -251,6 +278,7 @@ TEST test_tos_sem_post_all(void) SUITE(suit_sem) { RUN_TEST(test_tos_sem_create); + RUN_TEST(test_tos_sem_create_dyn); RUN_TEST(test_tos_sem_destroy); RUN_TEST(test_tos_sem_pend); RUN_TEST(test_tos_sem_pend_timed);