From 2a9174915707b544aec646681ddd8808dc6dbdc7 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 23 Jul 2022 22:41:16 +0530 Subject: [PATCH 01/38] Add new functions to libnmath * Arithmetic - isPrime * Complex Math - new datatypes --- .../{nmath.c => arithmetic/arithmetic.c} | 35 +++++++++++++++++- src/lib/libnmath/arithmetic/build.mk | 13 +++++++ src/lib/libnmath/build.mk | 1 + src/lib/libnmath/include/cmath.h | 37 +++++++++++++++++++ src/lib/libnmath/include/nmath.h | 3 +- 5 files changed, 87 insertions(+), 2 deletions(-) rename src/lib/libnmath/{nmath.c => arithmetic/arithmetic.c} (82%) create mode 100644 src/lib/libnmath/arithmetic/build.mk create mode 100644 src/lib/libnmath/include/cmath.h diff --git a/src/lib/libnmath/nmath.c b/src/lib/libnmath/arithmetic/arithmetic.c similarity index 82% rename from src/lib/libnmath/nmath.c rename to src/lib/libnmath/arithmetic/arithmetic.c index c3ae8243..1d1efa46 100644 --- a/src/lib/libnmath/nmath.c +++ b/src/lib/libnmath/arithmetic/arithmetic.c @@ -2,12 +2,13 @@ * CYANCORE LICENSE * Copyrights (C) 2019, Cyancore Team * - * File Name : nmath.c + * File Name : arithmetic.c * Description : This file contains sources of neo-math function * Primary Author : Akash Kollipara [akashkollipara@gmail.com] * Organisation : Cyancore Core-Team */ +#include #include /** @@ -132,7 +133,39 @@ long multiplicative_inverse(long base, long subject) return mod(t1, base); } +/** + * abs + * + * @brief This function returns absolute of input. + * + * @param[in] x: Input number + * @return ABS: returns absolute value + */ int abs(int x) { return (x < 0) ? -x : x; } + +/** + * isPrime + * + * @brief This function checks if the input number is prime or not + * + * @param [in] x: Input number + * + * @return : True/False + */ +bool isPrime(long x) +{ + if(x < 2) + return false; + else if(x <= 3) + return true; + else if((x % 2) || (x % 3)) + return false; + else + for(long i = 5; (i * i) <= x; i += 2) + if(!(x % i)) + return false; + return true; +} diff --git a/src/lib/libnmath/arithmetic/build.mk b/src/lib/libnmath/arithmetic/build.mk new file mode 100644 index 00000000..b090326d --- /dev/null +++ b/src/lib/libnmath/arithmetic/build.mk @@ -0,0 +1,13 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Descrption : This script accumulates sources and build +# neo-math library +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +DIR := $(GET_PATH) +include mk/lobj.mk diff --git a/src/lib/libnmath/build.mk b/src/lib/libnmath/build.mk index 360ddba9..afadb4ad 100644 --- a/src/lib/libnmath/build.mk +++ b/src/lib/libnmath/build.mk @@ -16,6 +16,7 @@ LIB := libnmath.a LIB_INCLUDE += $(NMATH_PATH)/include/ DEP_LIBS_ARG += -lnmath +include $(NMATH_PATH)/arithmetic/build.mk include $(NMATH_PATH)/dsp/build.mk DIR := $(NMATH_PATH) diff --git a/src/lib/libnmath/include/cmath.h b/src/lib/libnmath/include/cmath.h new file mode 100644 index 00000000..05ab4a89 --- /dev/null +++ b/src/lib/libnmath/include/cmath.h @@ -0,0 +1,37 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cmath.h + * Description : This file consists of complex math prototypes + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#include + +#define CREATE_COMPLEX_DATATYPE(type) \ + typedef struct complex_##type \ + { \ + type x; \ + type y; \ + } c##type + +#define COMPLEX(a, b) {.x=a, .y=b} + +/* int types */ +CREATE_COMPLEX_DATATYPE(uint8_t); +CREATE_COMPLEX_DATATYPE(int8_t); +CREATE_COMPLEX_DATATYPE(uint16_t); +CREATE_COMPLEX_DATATYPE(int16_t); +CREATE_COMPLEX_DATATYPE(uint32_t); +CREATE_COMPLEX_DATATYPE(int32_t); +CREATE_COMPLEX_DATATYPE(uint64_t); +CREATE_COMPLEX_DATATYPE(int64_t); + +/* generic types */ +CREATE_COMPLEX_DATATYPE(int); +CREATE_COMPLEX_DATATYPE(long); +CREATE_COMPLEX_DATATYPE(float); +CREATE_COMPLEX_DATATYPE(double); diff --git a/src/lib/libnmath/include/nmath.h b/src/lib/libnmath/include/nmath.h index 6e6b8ccf..4db5ee7e 100644 --- a/src/lib/libnmath/include/nmath.h +++ b/src/lib/libnmath/include/nmath.h @@ -9,7 +9,7 @@ */ #pragma once - +#include unsigned int clog2(unsigned long num); double ceiling(double num); double floor(double num); @@ -18,3 +18,4 @@ unsigned long lcd(unsigned int *a, unsigned int n); long mod(long a, long b); long multiplicative_inverse(long base, long subject); int abs(int x); +bool isPrime(long); From ec81f17929b9bee201e821710571852e1fe5eee9 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 21 Sep 2022 08:04:53 +0530 Subject: [PATCH 02/38] Added arch suspend/resume signal api These apis are to be used when entering and exiting suspend state. While entering cpu suspend state, programmer is intended to call and store the state, so that upon resume, certain checks for boot api needs be performed based on which they need to be executed. i.e.; when cpu enters simple wfi sleep, upon resume cpu need not perform full platform boot, (assuming cpu is reset; not applicable for AVR core, but these are generic apis) Issue: #169 --- src/arch/avr/8/common_5x_6/terravisor/arch.c | 46 +++++++++++++++++++ .../8/common_5x_6/terravisor/include/arch.h | 7 +++ src/lib/libresource/include/dp/dp_system.h | 1 + 3 files changed, 54 insertions(+) diff --git a/src/arch/avr/8/common_5x_6/terravisor/arch.c b/src/arch/avr/8/common_5x_6/terravisor/arch.c index f11ee6ae..f4cc7975 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/arch.c +++ b/src/arch/avr/8/common_5x_6/terravisor/arch.c @@ -10,6 +10,8 @@ */ #include +#include +#include #include #include #include @@ -97,3 +99,47 @@ void _NORETURN arch_panic_handler_callback() panic: while(1) arch_wfi(); } + +static cpu_sleep_t sleep_flag; + +/** + * arch_suspended_state_was + * + * @brief This function checks for the suspended state + * and returns true or false based on arg. + * + * @param[in] state: suspended state + * @return bool: True/False + */ +bool arch_suspended_state_was(cpu_sleep_t state) +{ + assert(state != resume); + if(!sleep_flag) + return false; + return (sleep_flag == state); +} + +/** + * arch_signal_suspend + * + * @brief This function is intended to be called before + * cpu enters suspend state. By passing the state, we store + * and use to check while exiting resume routine + * + * @param[in] state: Suspend state of cpu + */ +void arch_signal_suspend(cpu_sleep_t state) +{ + sleep_flag = state; +} + +/** + * arch_signal_resume + * + * @brief This function signals resume of cpu. It is intended + * to be called after exiting resume routine. + */ +void arch_signal_resume(void) +{ + sleep_flag = resume; +} diff --git a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h index 8a6b9704..8d03bbbc 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h +++ b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h @@ -15,6 +15,7 @@ #include #include #include +#include #include /** @@ -112,3 +113,9 @@ static inline void arch_nop() #define arch_isb() arch_nop() #define arch_dsb() arch_nop() #define arch_dmb() arch_nop() + +#ifdef _STDBOOL_H_ +bool arch_suspended_state_was(cpu_sleep_t); +void arch_signal_suspend(cpu_sleep_t); +void arch_signal_resume(void); +#endif diff --git a/src/lib/libresource/include/dp/dp_system.h b/src/lib/libresource/include/dp/dp_system.h index fee61f31..9cb931af 100644 --- a/src/lib/libresource/include/dp/dp_system.h +++ b/src/lib/libresource/include/dp/dp_system.h @@ -22,6 +22,7 @@ typedef enum cpu_sleep sleep_cluster_fclock = 0x201, sleep_cluster_off = 0x2f0, sleep_power_off = 0xaaa, + resume = 0xffff, } cpu_sleep_t; /* === Intrrupts structures === */ From b6b850ebbf799d6d3a5c30146e99996bd16c2d16 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 24 Sep 2022 17:46:22 +0530 Subject: [PATCH 03/38] Added arch suspend/resume signal apis Issue: #169 --- src/arch/riscv/32/i/terravisor/arch.c | 45 +++++++++++++++++++ src/arch/riscv/32/i/terravisor/include/arch.h | 6 +++ 2 files changed, 51 insertions(+) diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 068e43cc..a0becfe5 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -95,6 +95,51 @@ void arch_ei_restore_state(istate_t *istate) asm volatile("csrs mie, %0" : : "r" (*istate)); } +static cpu_sleep_t sleep_flag[N_CORES]; + +/** + * arch_suspended_state_was + * + * @brief This function checks per core suspend state + * and returns true/false based arg. + * + * @param[in] state: suspended state + * @return bool: True/False + */ +bool arch_suspended_state_was(cpu_sleep_t state) +{ + cpu_sleep_t temp = sleep_flag[arch_core_index()]; + if(!temp) + return false; + return (temp == state); +} + +/** + * arch_signal_suspend + * + * @brief This function is intended to be called before + * cpu enters suspend state. By passing the state, we store + * and use the value to check while executing resume routine. + * + * @param[in] state: Suspend state of cpu + */ +void arch_signal_suspend(cpu_sleep_t state) +{ + sleep_flag[arch_core_index()] = state; +} + +/** + * arch_signal_resume + * + * @brief This function is intended ot be called after + * execution of resume routine so that state of cpu is + * updated to resume. + */ +void arch_signal_resume(void) +{ + sleep_flag[arch_core_index()] = resume; +} + _WEAK void arch_panic_handler() { const context_frame_t *frame = get_context_frame(); diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index cae3b34c..ad957d51 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -11,6 +11,8 @@ #pragma once #define _ARCH_H_ +#include +#include #include #include @@ -175,3 +177,7 @@ static inline void arch_dmb() { fence(rw, rw); } + +bool arch_suspended_state_was(cpu_sleep_t); +void arch_signal_suspend(cpu_sleep_t); +void arch_signal_resume(void); From 76cdf7e8ac49d4690bdde3d9e2de0137990dc526 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 24 Sep 2022 17:59:07 +0530 Subject: [PATCH 04/38] Added stack pointer update api This function to be used only where there is not critical context. Upon calling this function the old context will be permanently lost and stack will be initialised to newer location without ensuring context management. Note: It is recommended to use this function only in naked function, the one which don't person cxt-mgnt. Issue: #169 --- src/arch/riscv/32/i/terravisor/include/riscv.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h index 81988af2..c88be24e 100644 --- a/src/arch/riscv/32/i/terravisor/include/riscv.h +++ b/src/arch/riscv/32/i/terravisor/include/riscv.h @@ -56,6 +56,11 @@ static inline unsigned int arch_core_impid() return ret; } +static inline void arch_update_sp(uint32_t *p) +{ + asm volatile("mov sp, %0" : : "r"(p)); +} + void riscv_update_vector(); context_frame_t *get_context_frame(); bool in_isr(void); From 78d9048bbba0db9982b9c8dae11e837dd1c04054 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 2 Oct 2022 23:06:57 +0530 Subject: [PATCH 05/38] Added stack pointer update api Issue: #169 --- src/arch/avr/8/common_5x_6/terravisor/include/avr.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/arch/avr/8/common_5x_6/terravisor/include/avr.h b/src/arch/avr/8/common_5x_6/terravisor/include/avr.h index d2486d80..364f4622 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/include/avr.h +++ b/src/arch/avr/8/common_5x_6/terravisor/include/avr.h @@ -30,3 +30,10 @@ typedef uint8_t istate_t; context_frame_t *get_context_frame(); void arch_panic_handler_callback(); bool in_isr(void); + +static inline void arch_update_sp(uint16_t *p) +{ + uint16_t temp = (uint16_t) p; + MMIO8(SPL) = (uint8_t)(temp & 0xff); + MMIO8(SPH) = (uint8_t)((temp >> 8) & 0xff); +} From 6eb7c0dc1abc77f81773e064c89168c893952a65 Mon Sep 17 00:00:00 2001 From: Pranjal Date: Tue, 23 Aug 2022 09:19:59 +0530 Subject: [PATCH 06/38] Proto complete --- projects/demo_avr/config.mk | 1 + src/include/visor/terravisor/cc_os/cc_os.h | 92 ++++++++++++++++++- .../visor/terravisor/cc_os/cc_os_config.h | 12 ++- src/include/visor/terravisor/cc_os/cc_shed.h | 31 +++++-- src/visor/terravisor/services/kernel/cc_os.c | 36 ++++++-- .../terravisor/services/kernel/cc_os_shed.c | 7 +- 6 files changed, 156 insertions(+), 23 deletions(-) diff --git a/projects/demo_avr/config.mk b/projects/demo_avr/config.mk index 8ca18de0..9fcc3484 100644 --- a/projects/demo_avr/config.mk +++ b/projects/demo_avr/config.mk @@ -17,3 +17,4 @@ BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 OBRDLED_ENABLE := 1 +TERRAKERN := 1 diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index fbed0837..26d13b71 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -1,29 +1,117 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #ifndef __CC_OS__ #define __CC_OS__ #include "status.h" #include "stdint.h" +#if ccosconfig_CC_OS_USE_DYNAMIC == 0 +#define CC_DYNAMIC 0 +#endif + #define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg typedef status_t cc_os_err_t; typedef void (* task_fn)(void * args); +typedef const char c_char; +/** + * @brief TASK infrastructure structure + * + */ typedef struct cc_os_task { task_fn task_fn; - char * name; + c_char * name; size_t priority; //>> For waited tasks - void * stack_ptr; + size_t * stack_ptr; size_t stack_len; }cc_os_task_t; +/** + * @brief Function to declare a static task with a dedicated stack for the task + * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN){ \ +static size_t _NAME##_stack[STACK_LEN]; \ +static const cc_os_task_t _NAME##_task = { \ + .task_fn = _fn, \ + .name = #_NAME, \ + .priority = _PRI, \ + .stack_ptr = _NAME##_stack, \ + .stack_len = STACK_LEN \ + }; \ +} + +/** + * @brief Function to get the instance using its name of already declared task. + * @brief Usage: cc_os_task_t * task = &(CC_GET_TASK_INST(TASK_Name)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_GET_TASK_INST(_NAME) _NAME##_task + +/** + * @brief A Function to add a task to the scheduler + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task + * @return cc_os_err_t + */ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task); + +/** + * @brief A function to delete a task from the scheduler + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_del_task (const char *name); + +/** + * @brief A Function to pause the task until call resume explicitly + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_pause_task (const char *name); + +/** + * + * @brief A Function to resume paused task. + * @note Calling this function for already non-waiting task has no effect. + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_resume_task (const char *name); + +/** + * @brief A Function to put the task to a waiting state and yield + * + * @param ticks Number of CC_OS Ticks + * @return cc_os_err_t + */ cc_os_err_t cc_os_wait_task (const size_t ticks); + +/** + * @brief A Function to invoke the kernel + * + * @return cc_os_err_t + */ cc_os_err_t cc_os_run (void); #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index cbc05a51..c82d92ba 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -1,9 +1,13 @@ #pragma once -#ifndef CC_OS_MAX_THREAD -#define CC_OS_MAX_THREAD 10 +#ifndef ccosconfig_CC_OS_USE_DYNAMIC +#define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif -#ifndef CC_OS_TASK_NAME_LEN -#define CC_OS_TASK_NAME_LEN 16 +#ifndef ccosconfig_CC_OS_MAX_THREAD +#define ccosconfig_CC_OS_MAX_THREAD 10 +#endif + +#ifndef ccosconfig_CC_OS_TASK_NAME_LEN +#define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif diff --git a/src/include/visor/terravisor/cc_os/cc_shed.h b/src/include/visor/terravisor/cc_os/cc_shed.h index 27096a76..1a6dde15 100644 --- a/src/include/visor/terravisor/cc_os/cc_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_shed.h @@ -1,3 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_shed.h + * Description : CC OS Kernel scheduler declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #pragma once #include "stdint.h" @@ -6,18 +16,19 @@ typedef struct cc_shed_tcb cc_shed_tcb_t; typedef enum { - cc_shed_task_terminated, - cc_shed_task_ready, - cc_shed_task_running, - cc_shed_task_paused, + cc_shed_task_terminated, ///> Initial State + cc_shed_task_ready, ///> Task Ready to despatch + cc_shed_task_running, ///> Task currently running + cc_shed_task_wait, ///> Task in wait state } cc_shed_task_status_t; struct cc_shed_tcb { - char name [CC_OS_TASK_NAME_LEN]; - size_t priority; - void * stack_ptr; - cc_shed_tcb_t * prev_shed_tcb; - cc_shed_tcb_t * next_shed_tcb; - cc_shed_task_status_t task_status; + char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + size_t priority; ///> Priority of the task + void * stack_ptr; ///> Stack Pointer + size_t task_delay_ticks; ///> Time delay in ticks + cc_shed_tcb_t * prev_shed_tcb; ///> Previous task pointer + cc_shed_tcb_t * next_shed_tcb; ///> Next task pointer + cc_shed_task_status_t task_status; ///> Current state of the task }; diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index a666abc5..b7b4c45c 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -5,7 +5,12 @@ #include extern cc_shed_tcb_t * g_list_head; + +#if CC_DYNAMIC == 0 extern cc_shed_tcb_t g_cc_os_tcb_list[]; +#else +extern cc_shed_tcb_t *g_cc_os_tcb_list; +#endif extern cc_shed_tcb_t * g_curr_task; cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) @@ -18,6 +23,9 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_shed_tcb_t * ptr = g_list_head; +#if CC_DYNAMIC == 0 + + /* Static Task Allocation */ if ((ptr->next_shed_tcb == NULL )&& (ptr->prev_shed_tcb == NULL)) { ptr->next_shed_tcb = ptr->prev_shed_tcb = g_list_head; @@ -25,7 +33,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) else { - for (size_t i = 0; i < CC_OS_MAX_THREAD; i++) + for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ if (g_cc_os_tcb_list[i].task_status == cc_shed_task_terminated) @@ -48,18 +56,23 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } } +#else + /* Dynamic Task Declaration */ +#endif /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, CC_OS_TASK_NAME_LEN); + memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; ptr->task_status = cc_shed_task_ready; - return success; } cc_os_err_t cc_os_del_task (const char * name) { cc_shed_tcb_t * ptr = g_curr_task; + +#if !CC_DYNAMIC + int name_found = 0; if (name != NULL) { @@ -96,7 +109,9 @@ cc_os_err_t cc_os_del_task (const char * name) } } - +#else + /* Dynamic Task Deletion */ +#endif ptr->next_shed_tcb->prev_shed_tcb = ptr->prev_shed_tcb; ptr->prev_shed_tcb->next_shed_tcb = ptr->next_shed_tcb; ptr->task_status = cc_shed_task_terminated; @@ -128,7 +143,7 @@ cc_os_err_t cc_os_pause_task (const char * name) } } - ptr->task_status = cc_shed_task_paused; + ptr->task_status = cc_shed_task_wait; return success; } @@ -159,8 +174,17 @@ cc_os_err_t cc_os_resume_task (const char * name) return success; } -cc_os_err_t cc_os_wait_task (const size_t ticks _UNUSED) +cc_os_err_t cc_os_wait_task (const size_t ticks) { + ASSERT_IF_FALSE(ticks > 0); + + cc_shed_tcb_t * ptr = g_list_head; + + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_shed_task_wait; + + // Yield + return success; } diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 56d06e6e..9dc1247b 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,6 +1,11 @@ #include #include -cc_shed_tcb_t g_cc_os_tcb_list [CC_OS_MAX_THREAD]; +#if !ccosconfig_CC_OS_USE_DYNAMIC + cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +#else + cc_shed_tcb_t *g_cc_os_tcb_list; +#endif + cc_shed_tcb_t * g_list_head = &(g_cc_os_tcb_list[0]); cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); From 249c2ab55201502161e13d4ce9b186ecdec1fa6f Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 21 Dec 2022 19:34:59 +0530 Subject: [PATCH 07/38] add cc_os dynaic task and cc_os_malloc proto. --- src/include/visor/terravisor/cc_os/cc_os.h | 54 ++++-- .../cc_os/{cc_shed.h => cc_os_shed.h} | 4 +- .../visor/terravisor/cc_os/utils/cc_os_heap.h | 7 + src/visor/terravisor/services/kernel/cc_os.c | 156 +++++++++++++----- .../terravisor/services/kernel/cc_os_heap.c | 10 ++ .../terravisor/services/kernel/cc_os_shed.c | 15 +- 6 files changed, 187 insertions(+), 59 deletions(-) rename src/include/visor/terravisor/cc_os/{cc_shed.h => cc_os_shed.h} (88%) create mode 100644 src/include/visor/terravisor/cc_os/utils/cc_os_heap.h create mode 100644 src/visor/terravisor/services/kernel/cc_os_heap.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 26d13b71..d5d7611b 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -14,9 +14,7 @@ #include "status.h" #include "stdint.h" -#if ccosconfig_CC_OS_USE_DYNAMIC == 0 -#define CC_DYNAMIC 0 -#endif +#define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC #define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg @@ -71,33 +69,63 @@ static const cc_os_task_t _NAME##_task = { \ * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task * @return cc_os_err_t */ -cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task); +cc_os_err_t cc_os_add_task(cc_os_task_t * cc_os_task); /** - * @brief A function to delete a task from the scheduler + * @brief A function to delete a task from the scheduler by instance * - * @param name + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_del_task(cc_os_task_t * cc_os_task); + +/** + * @brief A Function to pause the task until call resume explicitly using its instance + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task * @return cc_os_err_t */ -cc_os_err_t cc_os_del_task (const char *name); +cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task); + +/** + * + * @brief A Function to resume paused task using its instance + * @note Calling this function for already non-waiting task has no effect. + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task); + +/** + * @brief A function to delete a task from the scheduler by its name + * + * @param name Name of the task to be terminated; Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_del_task_by_name(const char * name); + /** - * @brief A Function to pause the task until call resume explicitly + * @brief A Function to pause the task until call resume explicitly by its name * * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_pause_task (const char *name); +cc_os_err_t cc_os_pause_task_by_name(const char *name); /** * - * @brief A Function to resume paused task. + * @brief A Function to resume paused task by its name * @note Calling this function for already non-waiting task has no effect. * * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_resume_task (const char *name); +cc_os_err_t cc_os_resume_task_by_name(const char *name); /** * @brief A Function to put the task to a waiting state and yield @@ -105,13 +133,13 @@ cc_os_err_t cc_os_resume_task (const char *name); * @param ticks Number of CC_OS Ticks * @return cc_os_err_t */ -cc_os_err_t cc_os_wait_task (const size_t ticks); +cc_os_err_t cc_os_wait_task(const size_t ticks); /** * @brief A Function to invoke the kernel * * @return cc_os_err_t */ -cc_os_err_t cc_os_run (void); +cc_os_err_t cc_os_run(void); #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h similarity index 88% rename from src/include/visor/terravisor/cc_os/cc_shed.h rename to src/include/visor/terravisor/cc_os/cc_os_shed.h index 1a6dde15..66360975 100644 --- a/src/include/visor/terravisor/cc_os/cc_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_os_shed.h @@ -28,7 +28,7 @@ struct cc_shed_tcb size_t priority; ///> Priority of the task void * stack_ptr; ///> Stack Pointer size_t task_delay_ticks; ///> Time delay in ticks - cc_shed_tcb_t * prev_shed_tcb; ///> Previous task pointer - cc_shed_tcb_t * next_shed_tcb; ///> Next task pointer + cc_shed_tcb_t * prev_ready_tcb; ///> Previous task pointer + cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer cc_shed_task_status_t task_status; ///> Current state of the task }; diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h new file mode 100644 index 00000000..1d192e95 --- /dev/null +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -0,0 +1,7 @@ +#include +#include +#include +#include + +void * cc_os_malloc(size_t size); +void * cc_os_free(void *addr); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index b7b4c45c..b2deec90 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -1,10 +1,11 @@ #include #include +#include #include #include -#include +#include -extern cc_shed_tcb_t * g_list_head; +extern cc_shed_tcb_t * g_ready_list_head; #if CC_DYNAMIC == 0 extern cc_shed_tcb_t g_cc_os_tcb_list[]; @@ -21,18 +22,28 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); ASSERT_IF_FALSE(cc_os_task->stack_len != 0); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; -#if CC_DYNAMIC == 0 - - /* Static Task Allocation */ - if ((ptr->next_shed_tcb == NULL )&& (ptr->prev_shed_tcb == NULL)) +#if CC_DYNAMIC == 1 + if (ptr == NULL) + { + /* First Dynamic task */ + ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + if (ptr == NULL) + { + return error_os_task_overfow; + } + } +#endif + if ((ptr->next_ready_tcb == NULL )&& (ptr->prev_ready_tcb == NULL)) { - ptr->next_shed_tcb = ptr->prev_shed_tcb = g_list_head; + ptr->next_ready_tcb = ptr->prev_ready_tcb = g_ready_list_head; } else { +#if CC_DYNAMIC == 0 + /* Static Task Allocation */ for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ @@ -42,23 +53,25 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) break; } } - if (ptr != g_list_head) + if (ptr != g_ready_list_head) + { +#else + /* Dynamic Task Declaration */ + ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + if (ptr != NULL) { +#endif /* Insert node at last */ - ptr->next_shed_tcb = g_list_head; - g_list_head->prev_shed_tcb = ptr; - ptr->prev_shed_tcb = g_list_head->prev_shed_tcb; - g_list_head->prev_shed_tcb->next_shed_tcb = ptr; + ptr->next_ready_tcb = g_ready_list_head; + g_ready_list_head->prev_ready_tcb = ptr; + ptr->prev_ready_tcb = g_ready_list_head->prev_ready_tcb; + g_ready_list_head->prev_ready_tcb->next_ready_tcb = ptr; } else { return error_os_task_overfow; } - } -#else - /* Dynamic Task Declaration */ -#endif /* Fill tcb details */ memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; @@ -67,18 +80,76 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_del_task (const char * name) +cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { cc_shed_tcb_t * ptr = g_curr_task; + if (cc_os_task != NULL) + { + ptr = cc_os_task; + } + /* Code to handle first node */ + if (ptr == g_ready_list_head) + { + if (ptr->next_ready_tcb != g_ready_list_head) + { + /* code for more than one node */ + g_ready_list_head = ptr->next_ready_tcb; + } + else + { + /* code for only one node */ + memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_ready_list_head = &(g_cc_os_tcb_list[0]); + return success; + } -#if !CC_DYNAMIC + } + ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; + ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; +#if CC_DYNAMIC == 0 + /* Static task Reset */ + ptr->task_status = cc_shed_task_terminated; + ptr->next_ready_tcb = NULL; + ptr->prev_ready_tcb = NULL; +#else + /* Dynamic Task Deletion */ + /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running */ +#endif + + return success; +} + +cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) +{ + cc_shed_tcb_t * ptr = g_curr_task; + if (cc_os_task != NULL) + { + ptr = cc_os_task; + } + ptr->task_status = cc_shed_task_wait; + + return success; +} + +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) +{ + ASSERT_IF_FALSE(cc_os_task != NULL); + + cc_os_task->task_status = cc_shed_task_ready; + + return success; +} + +cc_os_err_t cc_os_del_task_by_name (const char * name) +{ + cc_shed_tcb_t * ptr = g_curr_task; int name_found = 0; if (name != NULL) { /* code to delete node equal to name */ - ptr = g_list_head; - while(ptr->next_shed_tcb != g_list_head) + ptr = g_ready_list_head; + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -91,45 +162,52 @@ cc_os_err_t cc_os_del_task (const char * name) return error_func_inval_arg; } } - + else + { + ptr = g_curr_task; + } /* Code to handle first node */ - if (ptr == g_list_head) + if (ptr == g_ready_list_head) { - if (ptr->next_shed_tcb != g_list_head) + if (ptr->next_ready_tcb != g_ready_list_head) { /* code for more than one node */ - g_list_head = ptr->next_shed_tcb; + g_ready_list_head = ptr->next_ready_tcb; } else { /* code for only one node */ - memset(g_list_head, 0, sizeof(cc_shed_tcb_t)); - g_list_head = &(g_cc_os_tcb_list[0]); + memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_ready_list_head = &(g_cc_os_tcb_list[0]); return success; } } + ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; + ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; + +#if CC_DYNAMIC == 0 + /* Static task Reset */ + ptr->task_status = cc_shed_task_terminated; + ptr->next_ready_tcb = NULL; + ptr->prev_ready_tcb = NULL; #else /* Dynamic Task Deletion */ + /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ #endif - ptr->next_shed_tcb->prev_shed_tcb = ptr->prev_shed_tcb; - ptr->prev_shed_tcb->next_shed_tcb = ptr->next_shed_tcb; - ptr->task_status = cc_shed_task_terminated; - ptr->next_shed_tcb = NULL; - ptr->prev_shed_tcb = NULL; return success; } -cc_os_err_t cc_os_pause_task (const char * name) +cc_os_err_t cc_os_pause_task_by_name (const char * name) { cc_shed_tcb_t * ptr = g_curr_task; int name_found = 0; if (name != NULL) { /* code to pause node equal to name */ - ptr = g_list_head; - while(ptr->next_shed_tcb != g_list_head) + ptr = g_ready_list_head; + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -148,15 +226,15 @@ cc_os_err_t cc_os_pause_task (const char * name) return success; } -cc_os_err_t cc_os_resume_task (const char * name) +cc_os_err_t cc_os_resume_task_by_name (const char * name) { ASSERT_IF_FALSE(name != NULL); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; int name_found = 0; /* code to resume node equal to name */ - while(ptr->next_shed_tcb != g_list_head) + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -178,7 +256,7 @@ cc_os_err_t cc_os_wait_task (const size_t ticks) { ASSERT_IF_FALSE(ticks > 0); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; ptr->task_delay_ticks = ticks; ptr->task_status = cc_shed_task_wait; diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c new file mode 100644 index 00000000..b48838e2 --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -0,0 +1,10 @@ +#include + +void * cc_os_malloc(size_t size) +{ + +} +void * cc_os_free(void *addr) +{ + +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 9dc1247b..5a15b2fd 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,11 +1,16 @@ #include -#include +#include #if !ccosconfig_CC_OS_USE_DYNAMIC cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; + + cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_wait_list_head = NULL; #else - cc_shed_tcb_t *g_cc_os_tcb_list; -#endif + cc_shed_tcb_t * g_cc_os_tcb_list = NULL; -cc_shed_tcb_t * g_list_head = &(g_cc_os_tcb_list[0]); -cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_ready_list_head = NULL; + cc_shed_tcb_t * g_curr_task = NULL; + cc_shed_tcb_t * g_wait_list_head = NULL; +#endif From 24f567827be85d549c9811748bc2e6233102a3e7 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Thu, 22 Dec 2022 17:44:58 +0530 Subject: [PATCH 08/38] + scheduler basic prototype. + Code enhancements --- .vscode/c_cpp_properties.json | 7 ++- src/include/visor/terravisor/cc_os/cc_os.h | 14 +++-- .../visor/terravisor/cc_os/cc_os_config.h | 8 +++ .../visor/terravisor/cc_os/cc_os_shed.h | 25 +++++++- src/visor/terravisor/services/kernel/cc_os.c | 60 +++++++++++++++---- .../terravisor/services/kernel/cc_os_heap.c | 8 +-- .../terravisor/services/kernel/cc_os_idle.c | 9 +++ .../terravisor/services/kernel/cc_os_shed.c | 28 ++++++--- 8 files changed, 125 insertions(+), 34 deletions(-) create mode 100644 src/visor/terravisor/services/kernel/cc_os_idle.c diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d72ce463..496ed4ce 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,10 +6,11 @@ "${workspaceFolder}/src/**" ], "intelliSenseMode": "${default}", - "compilerPath": "", + "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", - "cppStandard": "gnu++14" + "cppStandard": "gnu++14", + "configurationProvider": "ms-vscode.makefile-tools" } ], "version": 4 -} +} \ No newline at end of file diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index d5d7611b..6fc540c2 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -13,6 +13,8 @@ #include "status.h" #include "stdint.h" +#include +#include #define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC @@ -31,9 +33,10 @@ typedef struct cc_os_task { task_fn task_fn; c_char * name; - size_t priority; //>> For waited tasks + size_t priority; ///>> For waited tasks size_t * stack_ptr; size_t stack_len; + cc_shed_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; /** @@ -43,16 +46,15 @@ typedef struct cc_os_task * @note DO NOT use space in place of TASK_Name as it would result build errors. * */ -#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN){ \ +#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN) \ static size_t _NAME##_stack[STACK_LEN]; \ -static const cc_os_task_t _NAME##_task = { \ +static cc_os_task_t _NAME##_task = { \ .task_fn = _fn, \ .name = #_NAME, \ .priority = _PRI, \ .stack_ptr = _NAME##_stack, \ .stack_len = STACK_LEN \ - }; \ -} + } \ /** * @brief Function to get the instance using its name of already declared task. @@ -133,7 +135,7 @@ cc_os_err_t cc_os_resume_task_by_name(const char *name); * @param ticks Number of CC_OS Ticks * @return cc_os_err_t */ -cc_os_err_t cc_os_wait_task(const size_t ticks); +cc_os_err_t cc_os_task_wait(const size_t ticks); /** * @brief A Function to invoke the kernel diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index c82d92ba..6c20eafa 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -11,3 +11,11 @@ #ifndef ccosconfig_CC_OS_TASK_NAME_LEN #define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif + +#ifndef ccosconfig_CC_OS_TASK_PRIORITY +#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#endif + +#ifndef ccosconfig_CC_OS_TASK_STACK_LEN +#define ccosconfig_CC_OS_TASK_STACK_LEN 255 +#endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h index 66360975..a709a8c7 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_os_shed.h @@ -13,13 +13,15 @@ #include "stdint.h" typedef struct cc_shed_tcb cc_shed_tcb_t; +typedef struct cc_shed cc_sched_t; typedef enum { - cc_shed_task_terminated, ///> Initial State + cc_shed_task_terminated = 0x00, ///> Initial State cc_shed_task_ready, ///> Task Ready to despatch cc_shed_task_running, ///> Task currently running cc_shed_task_wait, ///> Task in wait state + cc_shed_task_status_max = 0xff, ///> Do Nt Use } cc_shed_task_status_t; struct cc_shed_tcb @@ -32,3 +34,24 @@ struct cc_shed_tcb cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer cc_shed_task_status_t task_status; ///> Current state of the task }; + +/** + * @brief Prototype of scheduler algorithm function +*/ +typedef void (* algo_fn)(cc_shed_tcb_t * cc_os_tcb_list); + +typedef enum +{ + cc_shed_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm + cc_shed_algo_priority_driven, ///> Priority driven Scheduling + cc_shed_algo_max = 0xff +}cc_shed_algo_t; + +typedef struct cc_shed +{ + cc_shed_algo_t cc_selected_algo; ///> Selected Algorithm ID + algo_fn algo_function; ///> Pointer to algorithm function +}cc_shed_t; + + +void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index b2deec90..31736714 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -12,8 +12,11 @@ extern cc_shed_tcb_t g_cc_os_tcb_list[]; #else extern cc_shed_tcb_t *g_cc_os_tcb_list; #endif +extern cc_shed_tcb_t * g_task_max_prio; extern cc_shed_tcb_t * g_curr_task; +extern void CC_OS_IDLE_TASK(void * args); + cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); @@ -76,6 +79,19 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; + if (g_task_max_prio == NULL) + { + g_task_max_prio = ptr; + } + else + { + if(g_task_max_prio->priority < ptr->priority) + { + g_task_max_prio = ptr; + } + } + + cc_os_task->task_tcb_ptr = ptr; ptr->task_status = cc_shed_task_ready; return success; } @@ -83,9 +99,12 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { cc_shed_tcb_t * ptr = g_curr_task; + + ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); + if (cc_os_task != NULL) { - ptr = cc_os_task; + ptr = cc_os_task->task_tcb_ptr; } /* Code to handle first node */ if (ptr == g_ready_list_head) @@ -99,23 +118,33 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { /* code for only one node */ memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_task_max_prio = NULL; +#if CC_DYNAMIC == 0 g_ready_list_head = &(g_cc_os_tcb_list[0]); +#else + g_ready_list_head = NULL; +#endif return success; } } + ptr->task_status = cc_shed_task_terminated; + +/** TODO: By the scheduler for safe termination of current task ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; #if CC_DYNAMIC == 0 - /* Static task Reset */ - ptr->task_status = cc_shed_task_terminated; ptr->next_ready_tcb = NULL; ptr->prev_ready_tcb = NULL; #else - /* Dynamic Task Deletion */ - /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running */ + free(ptr); #endif +*/ + if (ptr == g_curr_task) + { + /* Yeild */ + } return success; } @@ -125,7 +154,7 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) cc_shed_tcb_t * ptr = g_curr_task; if (cc_os_task != NULL) { - ptr = cc_os_task; + ptr = cc_os_task->task_tcb_ptr; } ptr->task_status = cc_shed_task_wait; @@ -136,7 +165,7 @@ cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); - cc_os_task->task_status = cc_shed_task_ready; + cc_os_task->task_tcb_ptr->task_status = cc_shed_task_ready; return success; } @@ -252,7 +281,7 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) return success; } -cc_os_err_t cc_os_wait_task (const size_t ticks) +cc_os_err_t cc_os_task_wait (const size_t ticks) { ASSERT_IF_FALSE(ticks > 0); @@ -266,13 +295,20 @@ cc_os_err_t cc_os_wait_task (const size_t ticks) return success; } + cc_os_err_t cc_os_run (void) { /* OS Init code */ - while (1) - { - /* code to process OS */ - } + /* Initialise scheduler */ + + /* Initialise IDLE Task */ + CC_TASK_DEF( + cc_os_idle, /* Name of the instance */ + CC_OS_IDLE_TASK, /* Function pointer */ + ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ + ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ + ); + cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); return success; } diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index b48838e2..5d13112e 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -1,10 +1,10 @@ #include -void * cc_os_malloc(size_t size) +void * cc_os_malloc(size_t size _UNUSED) { - + return NULL; } -void * cc_os_free(void *addr) +void * cc_os_free(void *addr _UNUSED ) { - + return NULL; } diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c new file mode 100644 index 00000000..fbf3443a --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -0,0 +1,9 @@ +#include + +void CC_OS_IDLE_TASK(void * args _UNUSED) +{ + while (1) + { + cc_os_task_wait(1); + } +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 5a15b2fd..9d9ffe71 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,16 +1,28 @@ +#include #include #include #if !ccosconfig_CC_OS_USE_DYNAMIC - cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; - cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); - cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); - cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); +cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); #else - cc_shed_tcb_t * g_cc_os_tcb_list = NULL; +cc_shed_tcb_t * g_cc_os_tcb_list = NULL; - cc_shed_tcb_t * g_ready_list_head = NULL; - cc_shed_tcb_t * g_curr_task = NULL; - cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_ready_list_head = NULL; +cc_shed_tcb_t * g_curr_task = NULL; #endif +cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_task_max_prio = NULL; + +cc_shed_t g_cc_shed = { + .cc_selected_algo = cc_shed_algo_round_robin, + .algo_function = cc_shed_algo_round_robin_fn +}; + +void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list) +{ + (void)(cc_os_tcb_list); + return ; +} From 528506b112b741acd7443ced9f17cd0926971a62 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sat, 24 Dec 2022 17:24:46 +0530 Subject: [PATCH 09/38] Algo updates + Round Robin Algo + IDLE Task Heap Garbage collection + CC_OS code enhancements --- src/include/visor/terravisor/cc_os/cc_os.h | 34 +-- .../visor/terravisor/cc_os/cc_os_config.h | 42 +++- .../visor/terravisor/cc_os/cc_os_sched.h | 69 +++++++ .../visor/terravisor/cc_os/cc_os_shed.h | 57 ------ src/visor/terravisor/services/kernel/cc_os.c | 193 +++++++++++------- .../terravisor/services/kernel/cc_os_heap.c | 28 +++ .../terravisor/services/kernel/cc_os_idle.c | 41 +++- .../terravisor/services/kernel/cc_os_sched.c | 140 +++++++++++++ .../terravisor/services/kernel/cc_os_shed.c | 28 --- 9 files changed, 455 insertions(+), 177 deletions(-) create mode 100644 src/include/visor/terravisor/cc_os/cc_os_sched.h delete mode 100644 src/include/visor/terravisor/cc_os/cc_os_shed.h create mode 100644 src/visor/terravisor/services/kernel/cc_os_sched.c delete mode 100644 src/visor/terravisor/services/kernel/cc_os_shed.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 6fc540c2..b0cf3f07 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -12,9 +12,10 @@ #define __CC_OS__ #include "status.h" +#include "stdlib.h" #include "stdint.h" #include -#include +#include #define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC @@ -32,11 +33,12 @@ typedef const char c_char; typedef struct cc_os_task { task_fn task_fn; + void * args; ///>> Task Args ptr c_char * name; size_t priority; ///>> For waited tasks size_t * stack_ptr; size_t stack_len; - cc_shed_tcb_t * task_tcb_ptr; ///>> For internal use only + cc_sched_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; /** @@ -46,15 +48,16 @@ typedef struct cc_os_task * @note DO NOT use space in place of TASK_Name as it would result build errors. * */ -#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN) \ -static size_t _NAME##_stack[STACK_LEN]; \ -static cc_os_task_t _NAME##_task = { \ - .task_fn = _fn, \ - .name = #_NAME, \ - .priority = _PRI, \ - .stack_ptr = _NAME##_stack, \ - .stack_len = STACK_LEN \ - } \ +#define CC_TASK_DEF(_NAME, _fn, _args, _PRI, STACK_LEN) \ +static size_t _NAME##_stack[STACK_LEN]; \ +static cc_os_task_t _NAME##_task = { \ + .args = _args, \ + .task_fn = _fn, \ + .name = #_NAME, \ + .priority = _PRI, \ + .stack_ptr = _NAME##_stack, \ + .stack_len = STACK_LEN \ + } \ /** * @brief Function to get the instance using its name of already declared task. @@ -130,7 +133,7 @@ cc_os_err_t cc_os_pause_task_by_name(const char *name); cc_os_err_t cc_os_resume_task_by_name(const char *name); /** - * @brief A Function to put the task to a waiting state and yield + * @brief A Function to put the current task to a waiting state and yield * * @param ticks Number of CC_OS Ticks * @return cc_os_err_t @@ -144,4 +147,11 @@ cc_os_err_t cc_os_task_wait(const size_t ticks); */ cc_os_err_t cc_os_run(void); +/** + * @brief A function to set CC OS scheduler algorithm + * + * @return cc_os_err_t + */ +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); + #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index 6c20eafa..a0a92943 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -1,21 +1,53 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_config.h + * Description : CC OS Kernel configurations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #pragma once +/** + * @brief Shall CC_OS use dynamic resource allocation + * @note Possible values : 1/0 + */ #ifndef ccosconfig_CC_OS_USE_DYNAMIC -#define ccosconfig_CC_OS_USE_DYNAMIC 0 +#define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif +/** + * @brief Maximum number of threads allowed in static resource allocation + * @note The number of allowed threads are dependent of available RAM space. + * User may have to keep it optimised so as to use as low BSS section + * to be used as possible for the application being created. + */ #ifndef ccosconfig_CC_OS_MAX_THREAD -#define ccosconfig_CC_OS_MAX_THREAD 10 +#define ccosconfig_CC_OS_MAX_THREAD 10 #endif +/** + * @brief Max number of characters allowed to be used for task name + */ #ifndef ccosconfig_CC_OS_TASK_NAME_LEN -#define ccosconfig_CC_OS_TASK_NAME_LEN 16 +#define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif +/** + * @brief Task priority of IDLE task + * @note Possible values : 1 -> 255 + */ #ifndef ccosconfig_CC_OS_TASK_PRIORITY -#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#define ccosconfig_CC_OS_TASK_PRIORITY 1 #endif +/** + * @brief Stack size used by IDLE task + * @note The stack size is either allocated statically or dynamically as + * per the setting of ccosconfig_CC_OS_USE_DYNAMIC + */ #ifndef ccosconfig_CC_OS_TASK_STACK_LEN -#define ccosconfig_CC_OS_TASK_STACK_LEN 255 +#define ccosconfig_CC_OS_TASK_STACK_LEN 255 #endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/cc_os_sched.h new file mode 100644 index 00000000..b94bb30e --- /dev/null +++ b/src/include/visor/terravisor/cc_os/cc_os_sched.h @@ -0,0 +1,69 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_sched.h + * Description : CC OS Kernel scheduler declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#include "stdint.h" + +typedef struct cc_sched_tcb cc_sched_tcb_t; +typedef struct cc_sched cc_sched_t; + +typedef enum +{ + cc_sched_task_status_exit = 0x00, ///> Initial State + cc_sched_task_status_running, ///> Task currently running + cc_sched_task_status_ready, ///> Task Ready to despatch + cc_sched_task_status_wait, ///> Task in wait state + cc_sched_task_status_max = 0xff, ///> Do Nt Use +} cc_sched_task_status_t; + +typedef struct link +{ + cc_sched_tcb_t * prev; + cc_sched_tcb_t * next; +}link_t; + +struct cc_sched_tcb +{ + char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + size_t priority; ///> Priority of the task + void * stack_ptr; ///> Stack Pointer + size_t task_delay_ticks; ///> Time delay in ticks + link_t ready_link; ///> Ready Linked List Pointers + link_t wait_link; ///> Wait Linked List Pointers + cc_sched_task_status_t task_status; ///> Current state of the task +}; + +typedef struct cc_sched_ctrl +{ + cc_sched_tcb_t * ready_list_head; + cc_sched_tcb_t * curr_task; + cc_sched_tcb_t * wait_list_head; + cc_sched_tcb_t * task_max_prio; + cc_sched_t * selected_sched; +}cc_sched_ctrl_t; + +/** + * @brief Prototype of scheduler algorithm function + */ +typedef void (* algo_fn)(cc_sched_ctrl_t * sched_ctrl); + +typedef enum +{ + cc_sched_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm + cc_sched_algo_priority_driven, ///> Priority driven Scheduling + cc_sched_algo_max = 0xff +}cc_sched_algo_t; + +typedef struct cc_sched +{ + cc_sched_algo_t cc_selected_algo; ///> Selected Algorithm ID + algo_fn algo_function; ///> Pointer to algorithm function +}cc_sched_t; diff --git a/src/include/visor/terravisor/cc_os/cc_os_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h deleted file mode 100644 index a709a8c7..00000000 --- a/src/include/visor/terravisor/cc_os/cc_os_shed.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2022, Cyancore Team - * - * File Name : cc_shed.h - * Description : CC OS Kernel scheduler declaration - * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once - -#include "stdint.h" - -typedef struct cc_shed_tcb cc_shed_tcb_t; -typedef struct cc_shed cc_sched_t; - -typedef enum -{ - cc_shed_task_terminated = 0x00, ///> Initial State - cc_shed_task_ready, ///> Task Ready to despatch - cc_shed_task_running, ///> Task currently running - cc_shed_task_wait, ///> Task in wait state - cc_shed_task_status_max = 0xff, ///> Do Nt Use -} cc_shed_task_status_t; - -struct cc_shed_tcb -{ - char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task - size_t priority; ///> Priority of the task - void * stack_ptr; ///> Stack Pointer - size_t task_delay_ticks; ///> Time delay in ticks - cc_shed_tcb_t * prev_ready_tcb; ///> Previous task pointer - cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer - cc_shed_task_status_t task_status; ///> Current state of the task -}; - -/** - * @brief Prototype of scheduler algorithm function -*/ -typedef void (* algo_fn)(cc_shed_tcb_t * cc_os_tcb_list); - -typedef enum -{ - cc_shed_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm - cc_shed_algo_priority_driven, ///> Priority driven Scheduling - cc_shed_algo_max = 0xff -}cc_shed_algo_t; - -typedef struct cc_shed -{ - cc_shed_algo_t cc_selected_algo; ///> Selected Algorithm ID - algo_fn algo_function; ///> Pointer to algorithm function -}cc_shed_t; - - -void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index 31736714..c57ccf93 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -1,22 +1,54 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include #include #include #include #include -#include - -extern cc_shed_tcb_t * g_ready_list_head; +#include +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ #if CC_DYNAMIC == 0 -extern cc_shed_tcb_t g_cc_os_tcb_list[]; +extern cc_sched_tcb_t g_cc_os_tcb_list[]; #else -extern cc_shed_tcb_t *g_cc_os_tcb_list; +extern cc_sched_tcb_t *g_cc_os_tcb_list; #endif -extern cc_shed_tcb_t * g_task_max_prio; -extern cc_shed_tcb_t * g_curr_task; +/***************************************************** + * GLOBAL EXTERNS FUNCTIONS + *****************************************************/ extern void CC_OS_IDLE_TASK(void * args); +/***************************************************** + * GLOBAL EXTERNS VARIABLES + *****************************************************/ +extern cc_sched_t *g_cc_sched; +extern cc_sched_t g_cc_sched_list []; +extern cc_sched_ctrl_t g_sched_ctrl; + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ +static void __cc_os_task_yeild() +{ + return; +} +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); @@ -25,22 +57,22 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); ASSERT_IF_FALSE(cc_os_task->stack_len != 0); - cc_shed_tcb_t * ptr = g_ready_list_head; + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; #if CC_DYNAMIC == 1 if (ptr == NULL) { /* First Dynamic task */ - ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr == NULL) { return error_os_task_overfow; } } #endif - if ((ptr->next_ready_tcb == NULL )&& (ptr->prev_ready_tcb == NULL)) + if ((ptr->ready_link.next == NULL )&& (ptr->ready_link.prev == NULL)) { - ptr->next_ready_tcb = ptr->prev_ready_tcb = g_ready_list_head; + ptr->ready_link.next = ptr->ready_link.prev = g_sched_ctrl.ready_list_head; } else @@ -50,25 +82,25 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ - if (g_cc_os_tcb_list[i].task_status == cc_shed_task_terminated) + if (g_cc_os_tcb_list[i].task_status == cc_sched_task_status_exit) { ptr = &(g_cc_os_tcb_list[i]); break; } } - if (ptr != g_ready_list_head) + if (ptr != g_sched_ctrl.ready_list_head) { #else /* Dynamic Task Declaration */ - ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr != NULL) { #endif /* Insert node at last */ - ptr->next_ready_tcb = g_ready_list_head; - g_ready_list_head->prev_ready_tcb = ptr; - ptr->prev_ready_tcb = g_ready_list_head->prev_ready_tcb; - g_ready_list_head->prev_ready_tcb->next_ready_tcb = ptr; + ptr->ready_link.next = g_sched_ctrl.ready_list_head; + g_sched_ctrl.ready_list_head->ready_link.prev = ptr; + ptr->ready_link.prev = g_sched_ctrl.ready_list_head->ready_link.prev; + g_sched_ctrl.ready_list_head->ready_link.prev->ready_link.next = ptr; } else { @@ -79,26 +111,26 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; - if (g_task_max_prio == NULL) + if (g_sched_ctrl.task_max_prio == NULL) { - g_task_max_prio = ptr; + g_sched_ctrl.task_max_prio = ptr; } else { - if(g_task_max_prio->priority < ptr->priority) + if(g_sched_ctrl.task_max_prio->priority < ptr->priority) { - g_task_max_prio = ptr; + g_sched_ctrl.task_max_prio = ptr; } } cc_os_task->task_tcb_ptr = ptr; - ptr->task_status = cc_shed_task_ready; + ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); @@ -107,41 +139,30 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) ptr = cc_os_task->task_tcb_ptr; } /* Code to handle first node */ - if (ptr == g_ready_list_head) + if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->next_ready_tcb != g_ready_list_head) + if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) { /* code for more than one node */ - g_ready_list_head = ptr->next_ready_tcb; + g_sched_ctrl.ready_list_head = ptr->ready_link.next; } else { /* code for only one node */ - memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); - g_task_max_prio = NULL; + memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); + g_sched_ctrl.task_max_prio = NULL; #if CC_DYNAMIC == 0 - g_ready_list_head = &(g_cc_os_tcb_list[0]); + g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); #else - g_ready_list_head = NULL; + g_sched_ctrl.ready_list_head = NULL; #endif return success; } } - ptr->task_status = cc_shed_task_terminated; + ptr->task_status = cc_sched_task_status_exit; -/** TODO: By the scheduler for safe termination of current task - ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; - ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; - -#if CC_DYNAMIC == 0 - ptr->next_ready_tcb = NULL; - ptr->prev_ready_tcb = NULL; -#else - free(ptr); -#endif -*/ - if (ptr == g_curr_task) + if (ptr == g_sched_ctrl.curr_task) { /* Yeild */ } @@ -151,12 +172,12 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; if (cc_os_task != NULL) { ptr = cc_os_task->task_tcb_ptr; } - ptr->task_status = cc_shed_task_wait; + ptr->task_status = cc_sched_task_status_wait; return success; } @@ -165,20 +186,20 @@ cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); - cc_os_task->task_tcb_ptr->task_status = cc_shed_task_ready; + cc_os_task->task_tcb_ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_del_task_by_name (const char * name) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; int name_found = 0; if (name != NULL) { /* code to delete node equal to name */ - ptr = g_ready_list_head; - while(ptr->next_ready_tcb != g_ready_list_head) + ptr = g_sched_ctrl.ready_list_head; + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -193,33 +214,33 @@ cc_os_err_t cc_os_del_task_by_name (const char * name) } else { - ptr = g_curr_task; + ptr = g_sched_ctrl.curr_task; } /* Code to handle first node */ - if (ptr == g_ready_list_head) + if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->next_ready_tcb != g_ready_list_head) + if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) { /* code for more than one node */ - g_ready_list_head = ptr->next_ready_tcb; + g_sched_ctrl.ready_list_head = ptr->ready_link.next; } else { /* code for only one node */ - memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); - g_ready_list_head = &(g_cc_os_tcb_list[0]); + memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); + g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); return success; } } - ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; - ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; + ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; + ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; #if CC_DYNAMIC == 0 /* Static task Reset */ - ptr->task_status = cc_shed_task_terminated; - ptr->next_ready_tcb = NULL; - ptr->prev_ready_tcb = NULL; + ptr->task_status = cc_sched_task_status_exit; + ptr->ready_link.next = NULL; + ptr->ready_link.prev = NULL; #else /* Dynamic Task Deletion */ /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ @@ -230,13 +251,13 @@ cc_os_err_t cc_os_del_task_by_name (const char * name) cc_os_err_t cc_os_pause_task_by_name (const char * name) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; int name_found = 0; if (name != NULL) { /* code to pause node equal to name */ - ptr = g_ready_list_head; - while(ptr->next_ready_tcb != g_ready_list_head) + ptr = g_sched_ctrl.ready_list_head; + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -250,7 +271,7 @@ cc_os_err_t cc_os_pause_task_by_name (const char * name) } } - ptr->task_status = cc_shed_task_wait; + ptr->task_status = cc_sched_task_status_wait; return success; } @@ -259,11 +280,11 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) { ASSERT_IF_FALSE(name != NULL); - cc_shed_tcb_t * ptr = g_ready_list_head; + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; int name_found = 0; /* code to resume node equal to name */ - while(ptr->next_ready_tcb != g_ready_list_head) + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -276,25 +297,50 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) return error_func_inval_arg; } - ptr->task_status = cc_shed_task_ready; + ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_task_wait (const size_t ticks) { - ASSERT_IF_FALSE(ticks > 0); + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - cc_shed_tcb_t * ptr = g_ready_list_head; + if (ticks > 0) + { + /* Put into wait state only if wait count is more than 0 */ + if (g_sched_ctrl.wait_list_head == NULL) + { + /* First Wait list node */ + g_sched_ctrl.wait_list_head = ptr; + ptr->wait_link.next = ptr; + ptr->wait_link.prev = ptr; + } + else + { + ptr->wait_link.next = g_sched_ctrl.wait_list_head; + ptr->wait_link.prev = g_sched_ctrl.wait_list_head->wait_link.prev; + g_sched_ctrl.wait_list_head->wait_link.prev->wait_link.next = ptr; + g_sched_ctrl.wait_list_head->wait_link.prev = ptr; + } - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_shed_task_wait; + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; + } - // Yield + __cc_os_task_yeild(); return success; } +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +{ + ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); + + g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); + + return success; +} cc_os_err_t cc_os_run (void) { @@ -305,6 +351,7 @@ cc_os_err_t cc_os_run (void) CC_TASK_DEF( cc_os_idle, /* Name of the instance */ CC_OS_IDLE_TASK, /* Function pointer */ + &g_sched_ctrl, /* Task Args*/ ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ ); diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index 5d13112e..45f37b25 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -1,5 +1,33 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ + +/***************************************************** + * GLOBAL EXTERNS + *****************************************************/ + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ + +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ void * cc_os_malloc(size_t size _UNUSED) { return NULL; diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index fbf3443a..82ceff1b 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -1,9 +1,46 @@ #include -void CC_OS_IDLE_TASK(void * args _UNUSED) +/***************************************************** + * GLOBAL EXTERNS VARIABLES + *****************************************************/ +/** + * @brief This function cleans up the terminated task form the TCB list + * + * @param ptr[in] Pointer to the TCB being cleaned + * @return cc_sched_tcb_t * Pointer to the next TCB + */ +static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) { + cc_sched_tcb_t * next_ptr = ptr->ready_link.next; + if (ptr->ready_link.next->task_status == cc_sched_task_status_exit) + { + ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; + ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; + +#if ccosconfig_CC_OS_USE_DYNAMIC == 0 + ptr->ready_link.next = NULL; + ptr->ready_link.prev = NULL; +#else + cc_os_free(ptr); +#endif + } + + return next_ptr; +} + +void CC_OS_IDLE_TASK(void * args) +{ + cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; + static cc_sched_tcb_t * ptr = NULL; + ptr = g_sched_ctrl->ready_list_head; while (1) { - cc_os_task_wait(1); + /* Clean up task if terminated */ + ptr = __free_terminated_task(ptr); + + /* Power Save code */ + + /* Yield for nect available task */ + cc_os_task_wait(0); } } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c new file mode 100644 index 00000000..5d5d9a5b --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -0,0 +1,140 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_sched.c + * Description : CC OS Kernel scheduler definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ +#include +#include +#include + +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_SCHED_ALGO(_id, _fn) { \ + .cc_selected_algo = _id, \ + .algo_function = _fn} + +/***************************************************** + * STATIC FUNCTION DECLARATION + *****************************************************/ +static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl); +static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ +#if !ccosconfig_CC_OS_USE_DYNAMIC +cc_sched_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +#else +cc_sched_tcb_t * g_cc_os_tcb_list = NULL; +#endif + +cc_sched_t g_cc_sched_list [] = +{ + CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), + CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), +}; + +cc_sched_ctrl_t g_sched_ctrl = +{ +#if !ccosconfig_CC_OS_USE_DYNAMIC + .ready_list_head = &(g_cc_os_tcb_list[0]), + .curr_task = &(g_cc_os_tcb_list[0]), +#else + .ready_list_head = NULL, + .curr_task = NULL, +#endif + .wait_list_head = NULL, + .task_max_prio = NULL, + .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) +}; + + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ +static void __cc_sched_context_switch(cc_sched_tcb_t * next_task) +{ + next_task->task_status = cc_sched_task_status_running; +} + +static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) +{ + cc_sched_tcb_t * ptr; + if(sched_ctrl->wait_list_head != NULL) + { + ptr = sched_ctrl->wait_list_head; + /* Tasks present in wait list */ + while(1) + { + ptr->task_delay_ticks--; /* Tick caliberations required */ + if(ptr->task_delay_ticks == 0) + { + if (ptr == sched_ctrl->wait_list_head) + { + /* First in the list */ + sched_ctrl->wait_list_head = ptr->wait_link.next; + if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) + { + /* Last Wait task left */ + sched_ctrl->wait_list_head = NULL; + } + } + ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; + ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = NULL; + ptr->wait_link.next = NULL; + ptr->task_status = cc_sched_task_status_ready; + } + if (ptr->wait_link.next == sched_ctrl->wait_list_head) + { + break; + } + else + { + ptr = ptr->wait_link.next; + } + } + } +} + +/***************************************************** + * SCHEDULER ALGORITHMS + *****************************************************/ +static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) +{ + /* do waitlist adjustment */ + cc_sched_tcb_t * ptr = sched_ctrl->curr_task; + + __cc_sched_wait_list_adjustment(sched_ctrl); + + /* Context switch to next task */ + if (ptr->ready_link.next->task_status == cc_sched_task_status_ready) + { + __cc_sched_context_switch(ptr->ready_link.next); + } +} + +static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) +{ + /* do waitlist adjustment */ + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head; + + __cc_sched_wait_list_adjustment(sched_ctrl); + while (1) + { + if (ptr == sched_ctrl->ready_list_head) + { + break; + } + + } + +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c deleted file mode 100644 index 9d9ffe71..00000000 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -#if !ccosconfig_CC_OS_USE_DYNAMIC -cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; - -cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); -cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); -#else -cc_shed_tcb_t * g_cc_os_tcb_list = NULL; - -cc_shed_tcb_t * g_ready_list_head = NULL; -cc_shed_tcb_t * g_curr_task = NULL; -#endif -cc_shed_tcb_t * g_wait_list_head = NULL; -cc_shed_tcb_t * g_task_max_prio = NULL; - -cc_shed_t g_cc_shed = { - .cc_selected_algo = cc_shed_algo_round_robin, - .algo_function = cc_shed_algo_round_robin_fn -}; - -void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list) -{ - (void)(cc_os_tcb_list); - return ; -} From d276e8af479316fa2519ea53c17a2a571758d403 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 25 Dec 2022 17:47:58 +0530 Subject: [PATCH 10/38] Priority driven algorithm --- src/include/status.h | 1 + src/include/visor/terravisor/cc_os/cc_os.h | 72 +++-- .../visor/terravisor/cc_os/cc_os_config.h | 12 +- src/visor/terravisor/services/kernel/cc_os.c | 279 +++++++----------- .../terravisor/services/kernel/cc_os_heap.c | 3 +- .../terravisor/services/kernel/cc_os_idle.c | 24 +- .../terravisor/services/kernel/cc_os_sched.c | 163 ++++++++-- 7 files changed, 323 insertions(+), 231 deletions(-) diff --git a/src/include/status.h b/src/include/status.h index 62e06982..0264eb68 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -76,6 +76,7 @@ typedef enum status error_os = -0x0f00, error_os_task_overfow = -0x0f01, error_os_deadlock = -0x0f02, + error_os_invalid_op = -0x0f03, /* Mesg related error */ error_mesg = -0x1000, error_mesg_long = -0x1001, diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index b0cf3f07..0e231e4d 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -11,19 +11,33 @@ #ifndef __CC_OS__ #define __CC_OS__ +/***************************************************** + * INCLUDES + *****************************************************/ #include "status.h" #include "stdlib.h" #include "stdint.h" #include #include -#define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_FALSE 0 +#define CC_OS_TRUE 1 +#define CC_OS_DELAY_MAX ((size_t) -1) -#define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg +#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC +#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg + +/***************************************************** + * TYPEDEFS + *****************************************************/ typedef status_t cc_os_err_t; -typedef void (* task_fn)(void * args); +typedef void * os_args; +typedef void (* task_fn)(os_args args); typedef const char c_char; /** @@ -32,15 +46,18 @@ typedef const char c_char; */ typedef struct cc_os_task { - task_fn task_fn; - void * args; ///>> Task Args ptr - c_char * name; + task_fn task_fn; ///>> Task funcion + os_args args; ///>> Task Args ptr + c_char * name; ///>> String name of the task size_t priority; ///>> For waited tasks - size_t * stack_ptr; - size_t stack_len; + size_t * stack_ptr; ///>> Stack pointer of the task + size_t stack_len; ///>> Stack lengths of the task cc_sched_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; +/***************************************************** + * MACROS + *****************************************************/ /** * @brief Function to declare a static task with a dedicated stack for the task * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); @@ -68,6 +85,9 @@ static cc_os_task_t _NAME##_task = { \ */ #define CC_GET_TASK_INST(_NAME) _NAME##_task +/***************************************************** + * USER FUNCTION DECLARATIONS + *****************************************************/ /** * @brief A Function to add a task to the scheduler * @@ -106,52 +126,50 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task); cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task); /** - * @brief A function to delete a task from the scheduler by its name + * @brief A Function to pause all the tasks except the current and the IDLE Task + * @note To resume all please use cc_os_resume_all_task() call * - * @param name Name of the task to be terminated; Pass NULL to point to current task * @return cc_os_err_t */ -cc_os_err_t cc_os_del_task_by_name(const char * name); - +cc_os_err_t cc_os_pause_all_task (void); /** - * @brief A Function to pause the task until call resume explicitly by its name + * @brief A Function to resume all the tasks * - * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_pause_task_by_name(const char *name); +cc_os_err_t cc_os_resume_all_task (void); /** + * @brief A function to set CC OS scheduler algorithm * - * @brief A Function to resume paused task by its name - * @note Calling this function for already non-waiting task has no effect. - * - * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_resume_task_by_name(const char *name); +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); /** * @brief A Function to put the current task to a waiting state and yield + * @note To just Yeild set ticks to 0 * * @param ticks Number of CC_OS Ticks - * @return cc_os_err_t + * @return None */ -cc_os_err_t cc_os_task_wait(const size_t ticks); +void cc_os_task_wait(const size_t ticks); /** - * @brief A Function to invoke the kernel + * @brief A Function to switch to next available task * - * @return cc_os_err_t + * @param ticks Number of CC_OS Ticks + * @return None */ -cc_os_err_t cc_os_run(void); +void cc_os_task_yield(); /** - * @brief A function to set CC OS scheduler algorithm + * @brief A Function to invoke the kernel * * @return cc_os_err_t */ -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); +void cc_os_run(void); + #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index a0a92943..729b34a6 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -18,6 +18,14 @@ #define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif +/** + * @brief Heap size to be allocated to CC_OS + * @note Possible values : Depends on available RAM + */ +#ifndef ccosconfig_CC_OS_HEAP_SIZE +#define ccosconfig_CC_OS_HEAP_SIZE 1024 +#endif + /** * @brief Maximum number of threads allowed in static resource allocation * @note The number of allowed threads are dependent of available RAM space. @@ -39,8 +47,8 @@ * @brief Task priority of IDLE task * @note Possible values : 1 -> 255 */ -#ifndef ccosconfig_CC_OS_TASK_PRIORITY -#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#ifndef ccosconfig_CC_OS_IDLE_TASK_PRIORITY +#define ccosconfig_CC_OS_IDLE_TASK_PRIORITY 0x01 #endif /** diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index c57ccf93..c2eb579f 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -14,23 +14,33 @@ #include #include #include +#include #include #include #include +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_PRIORITY_MAX 255 + /***************************************************** * GLOBAL DECLARATIONS *****************************************************/ -#if CC_DYNAMIC == 0 +#if CC_OS_DYNAMIC == CC_OS_FALSE extern cc_sched_tcb_t g_cc_os_tcb_list[]; #else extern cc_sched_tcb_t *g_cc_os_tcb_list; #endif /***************************************************** - * GLOBAL EXTERNS FUNCTIONS + * INTERNAL EXTERNS FUNCTIONS *****************************************************/ -extern void CC_OS_IDLE_TASK(void * args); +extern void _cc_os_idle_task_fn (void * args); +extern void _insert_after (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_before (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); +extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); /***************************************************** * GLOBAL EXTERNS VARIABLES @@ -42,30 +52,32 @@ extern cc_sched_ctrl_t g_sched_ctrl; /***************************************************** * STATIC FUNCTION DEFINATIONS *****************************************************/ -static void __cc_os_task_yeild() -{ - return; -} + /***************************************************** * USER FUNCTION DEFINATIONS *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { - ASSERT_IF_FALSE(cc_os_task != NULL); - ASSERT_IF_FALSE(cc_os_task->name != NULL); - ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); - ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); - ASSERT_IF_FALSE(cc_os_task->stack_len != 0); + CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->name != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_len != CC_OS_FALSE); + CC_OS_ASSERT_IF_FALSE(cc_os_task->priority >= ccosconfig_CC_OS_IDLE_TASK_PRIORITY); + CC_OS_ASSERT_IF_FALSE(cc_os_task->priority < CC_OS_PRIORITY_MAX); + + cc_os_pause_all_task(); cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; -#if CC_DYNAMIC == 1 +#if CC_OS_DYNAMIC == CC_OS_TRUE if (ptr == NULL) { /* First Dynamic task */ ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr == NULL) { + cc_os_resume_all_task(); return error_os_task_overfow; } } @@ -77,9 +89,9 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) else { -#if CC_DYNAMIC == 0 +#if CC_OS_DYNAMIC == CC_OS_FALSE /* Static Task Allocation */ - for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) + for (size_t i = CC_OS_FALSE; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ if (g_cc_os_tcb_list[i].task_status == cc_sched_task_status_exit) @@ -96,43 +108,56 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) if (ptr != NULL) { #endif - /* Insert node at last */ - ptr->ready_link.next = g_sched_ctrl.ready_list_head; - g_sched_ctrl.ready_list_head->ready_link.prev = ptr; - ptr->ready_link.prev = g_sched_ctrl.ready_list_head->ready_link.prev; - g_sched_ctrl.ready_list_head->ready_link.prev->ready_link.next = ptr; + /* Fill tcb details */ + memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); + ptr->stack_ptr = cc_os_task->stack_ptr; + ptr->priority = cc_os_task->priority; } else { + cc_os_resume_all_task(); return error_os_task_overfow; } } - /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); - ptr->stack_ptr = cc_os_task->stack_ptr; - ptr->priority = cc_os_task->priority; + /* Insert Tasks in assending order of its priority */ if (g_sched_ctrl.task_max_prio == NULL) { g_sched_ctrl.task_max_prio = ptr; } + else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) + { + _insert_after(g_sched_ctrl.task_max_prio, ptr, CC_OS_FALSE); + g_sched_ctrl.task_max_prio = ptr; + } else { - if(g_sched_ctrl.task_max_prio->priority < ptr->priority) + cc_sched_tcb_t * comp_ptr = g_sched_ctrl.task_max_prio->ready_link.next; + while (1) { - g_sched_ctrl.task_max_prio = ptr; + if (comp_ptr->priority <= ptr->priority) + { + _insert_after(comp_ptr, ptr, CC_OS_FALSE); + break; + } + else + { + comp_ptr = comp_ptr->ready_link.next; + } } } cc_os_task->task_tcb_ptr = ptr; ptr->task_status = cc_sched_task_status_ready; + + cc_os_resume_all_task(); return success; } cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != _cc_os_idle_task_fn); - ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; if (cc_os_task != NULL) { @@ -141,30 +166,14 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) /* Code to handle first node */ if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - /* code for more than one node */ - g_sched_ctrl.ready_list_head = ptr->ready_link.next; - } - else - { - /* code for only one node */ - memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); - g_sched_ctrl.task_max_prio = NULL; -#if CC_DYNAMIC == 0 - g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); -#else - g_sched_ctrl.ready_list_head = NULL; -#endif - return success; - } - + /* IDLE Task can not be deleted */ + return error_os_invalid_op; } ptr->task_status = cc_sched_task_status_exit; if (ptr == g_sched_ctrl.curr_task) { - /* Yeild */ + cc_os_task_yield(); /* Yeild */ } return success; @@ -177,185 +186,105 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { ptr = cc_os_task->task_tcb_ptr; } - ptr->task_status = cc_sched_task_status_wait; - return success; -} - -cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) -{ - ASSERT_IF_FALSE(cc_os_task != NULL); - - cc_os_task->task_tcb_ptr->task_status = cc_sched_task_status_ready; + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, CC_OS_DELAY_MAX); return success; } -cc_os_err_t cc_os_del_task_by_name (const char * name) +cc_os_err_t cc_os_pause_all_task () { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - int name_found = 0; - if (name != NULL) - { - /* code to delete node equal to name */ - ptr = g_sched_ctrl.ready_list_head; - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) - { - return error_func_inval_arg; - } - } - else - { - ptr = g_sched_ctrl.curr_task; - } - /* Code to handle first node */ - if (ptr == g_sched_ctrl.ready_list_head) + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; + + while (ptr != g_sched_ctrl.ready_list_head) { - if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - /* code for more than one node */ - g_sched_ctrl.ready_list_head = ptr->ready_link.next; - } - else + if (ptr == g_sched_ctrl.curr_task) { - /* code for only one node */ - memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); - g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); - return success; + continue; } + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, CC_OS_DELAY_MAX); + ptr = ptr->ready_link.next; } - ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; - ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; - -#if CC_DYNAMIC == 0 - /* Static task Reset */ - ptr->task_status = cc_sched_task_status_exit; - ptr->ready_link.next = NULL; - ptr->ready_link.prev = NULL; -#else - /* Dynamic Task Deletion */ - /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ -#endif return success; } -cc_os_err_t cc_os_pause_task_by_name (const char * name) +cc_os_err_t cc_os_resume_all_task () { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - int name_found = 0; - if (name != NULL) + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; + + while (ptr != g_sched_ctrl.ready_list_head) { - /* code to pause node equal to name */ - ptr = g_sched_ctrl.ready_list_head; - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) + if (ptr == g_sched_ctrl.curr_task) { - return error_func_inval_arg; + continue; } - } - ptr->task_status = cc_sched_task_status_wait; + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + ptr = ptr->ready_link.next; + } return success; } -cc_os_err_t cc_os_resume_task_by_name (const char * name) +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { - ASSERT_IF_FALSE(name != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); - cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; - int name_found = 0; + cc_sched_tcb_t * ptr = cc_os_task->task_tcb_ptr; - /* code to resume node equal to name */ - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) - { - return error_func_inval_arg; - } + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - ptr->task_status = cc_sched_task_status_ready; + return success; +} + +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +{ + CC_OS_ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); + + g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); return success; } -cc_os_err_t cc_os_task_wait (const size_t ticks) +void cc_os_task_wait (const size_t ticks) { cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (ticks > 0) + if (ticks > CC_OS_FALSE) { - /* Put into wait state only if wait count is more than 0 */ - if (g_sched_ctrl.wait_list_head == NULL) - { - /* First Wait list node */ - g_sched_ctrl.wait_list_head = ptr; - ptr->wait_link.next = ptr; - ptr->wait_link.prev = ptr; - } - else - { - ptr->wait_link.next = g_sched_ctrl.wait_list_head; - ptr->wait_link.prev = g_sched_ctrl.wait_list_head->wait_link.prev; - g_sched_ctrl.wait_list_head->wait_link.prev->wait_link.next = ptr; - g_sched_ctrl.wait_list_head->wait_link.prev = ptr; - } - - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_sched_task_status_wait; + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, ticks); } - __cc_os_task_yeild(); - - return success; + cc_os_task_yield(); } -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +void cc_os_task_yield() { - ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); - - g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); - - return success; + TODO("cc_os_task_yield"); + return; } -cc_os_err_t cc_os_run (void) +void cc_os_run (void) { /* OS Init code */ - /* Initialise scheduler */ /* Initialise IDLE Task */ CC_TASK_DEF( cc_os_idle, /* Name of the instance */ - CC_OS_IDLE_TASK, /* Function pointer */ + _cc_os_idle_task_fn, /* Function pointer */ &g_sched_ctrl, /* Task Args*/ - ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ + ccosconfig_CC_OS_IDLE_TASK_PRIORITY, /* Task Priority */ ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ - ); + ); cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); - return success; + /* Initialise scheduler */ + cc_os_task_yield(); /* Yeild */ + while (1) + { + /* Code shall not reach here */ + arch_wfi(); + } } diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index 45f37b25..f75a30d3 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -14,8 +14,9 @@ #include /***************************************************** - * GLOBAL DECLARATIONS + * GLOBAL/STATIC VARIABLE DECLARATIONS *****************************************************/ +// static unsigned char g_cc_os_heap[ccosconfig_CC_OS_HEAP_SIZE]; /***************************************************** * GLOBAL EXTERNS diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 82ceff1b..0d413f52 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -1,7 +1,20 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_idle.c + * Description : CC OS IDLE Task definitions + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include /***************************************************** - * GLOBAL EXTERNS VARIABLES + * STATIC FUNCTION DECLARATION *****************************************************/ /** * @brief This function cleans up the terminated task form the TCB list @@ -28,7 +41,10 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) return next_ptr; } -void CC_OS_IDLE_TASK(void * args) +/***************************************************** + * USER FUNCTION DEFINATION + *****************************************************/ +void _cc_os_idle_task_fn(os_args args) { cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; static cc_sched_tcb_t * ptr = NULL; @@ -40,7 +56,7 @@ void CC_OS_IDLE_TASK(void * args) /* Power Save code */ - /* Yield for nect available task */ - cc_os_task_wait(0); + /* Yield for next available task */ + cc_os_task_yield(); } } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index 5d5d9a5b..c46dd285 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -27,6 +27,7 @@ *****************************************************/ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl); static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); + /***************************************************** * GLOBAL DECLARATIONS *****************************************************/ @@ -38,8 +39,8 @@ cc_sched_tcb_t * g_cc_os_tcb_list = NULL; cc_sched_t g_cc_sched_list [] = { - CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), - CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), + CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), + CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), }; cc_sched_ctrl_t g_sched_ctrl = @@ -56,6 +57,123 @@ cc_sched_ctrl_t g_sched_ctrl = .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) }; +/***************************************************** + * INTERNAL USED FUNCTIONS (NON-STATIC) + *****************************************************/ +void _insert_after(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +{ + switch (link_type) + { + case 0: + /* Ready Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->ready_link.next = new_node; + new_node->ready_link.prev = new_node; + } + else + { + new_node->ready_link.next = ptr->ready_link.next; + new_node->ready_link.prev = ptr; + new_node->ready_link.next->ready_link.prev = new_node; + ptr->ready_link.next = new_node; + } + break; + case 1: + /* Wait Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->wait_link.next = new_node; + new_node->wait_link.prev = new_node; + } + { + new_node->wait_link.next = ptr->wait_link.next; + new_node->wait_link.prev = ptr; + new_node->wait_link.next->wait_link.prev = new_node; + ptr->wait_link.next = new_node; + } + break; + default: + return; + } +} +void _insert_before(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +{ + switch (link_type) + { + case 0: + /* Ready Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->ready_link.next = new_node; + new_node->ready_link.prev = new_node; + } + { + new_node->ready_link.next = ptr; + new_node->ready_link.prev = ptr->ready_link.prev; + ptr->ready_link.prev = new_node; + new_node->ready_link.prev->ready_link.next = new_node; + } + break; + case 1: + /* Wait Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->wait_link.next = new_node; + new_node->wait_link.prev = new_node; + } + { + new_node->wait_link.next = ptr; + new_node->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = new_node; + new_node->wait_link.prev->wait_link.next = new_node; + } + break; + default: + return; + } +} + +void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks) +{ + if (ptr->task_status == cc_sched_task_status_wait) + { + return; + } + _insert_before(sched_ctrl->wait_list_head, ptr, 1); + + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; +} + +void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr) +{ + if (ptr->task_status == cc_sched_task_status_ready) + { + return; + } + + if (ptr == sched_ctrl->wait_list_head) + { + /* First in the list */ + sched_ctrl->wait_list_head = ptr->wait_link.next; + if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) + { + /* Last Wait task left */ + sched_ctrl->wait_list_head = NULL; + } + } + ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; + ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = NULL; + ptr->wait_link.next = NULL; + ptr->task_delay_ticks = 0; + ptr->task_status = cc_sched_task_status_ready; +} /***************************************************** * STATIC FUNCTION DEFINATIONS @@ -77,21 +195,7 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) ptr->task_delay_ticks--; /* Tick caliberations required */ if(ptr->task_delay_ticks == 0) { - if (ptr == sched_ctrl->wait_list_head) - { - /* First in the list */ - sched_ctrl->wait_list_head = ptr->wait_link.next; - if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) - { - /* Last Wait task left */ - sched_ctrl->wait_list_head = NULL; - } - } - ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; - ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = NULL; - ptr->wait_link.next = NULL; - ptr->task_status = cc_sched_task_status_ready; + _cc_sched_send_to_resume(sched_ctrl, ptr); } if (ptr->wait_link.next == sched_ctrl->wait_list_head) { @@ -111,12 +215,17 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->curr_task; + cc_sched_tcb_t * ptr = sched_ctrl->curr_task->ready_link.next; __cc_sched_wait_list_adjustment(sched_ctrl); + if (ptr == sched_ctrl->ready_list_head) + { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + } /* Context switch to next task */ - if (ptr->ready_link.next->task_status == cc_sched_task_status_ready) + if (ptr->task_status == cc_sched_task_status_ready) { __cc_sched_context_switch(ptr->ready_link.next); } @@ -125,16 +234,26 @@ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head; + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; __cc_sched_wait_list_adjustment(sched_ctrl); while (1) { + if (ptr->task_status == cc_sched_task_status_ready) + { + break; + } + else + { + /* Iterate Backwards */ + ptr = ptr->wait_link.prev; + } if (ptr == sched_ctrl->ready_list_head) { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); break; } - } - + __cc_sched_context_switch(ptr); } From 089983537b37123696824f38ed5c0f5c9220fa9f Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 25 Dec 2022 18:09:14 +0530 Subject: [PATCH 11/38] Sonar Cloud Bug fixes --- .vscode/c_cpp_properties.json | 5 +- .../visor/terravisor/cc_os/cc_os_sched.h | 10 +- .../visor/terravisor/cc_os/utils/cc_os_heap.h | 2 +- src/visor/terravisor/services/kernel/cc_os.c | 42 ++++--- .../terravisor/services/kernel/cc_os_heap.c | 3 +- .../terravisor/services/kernel/cc_os_sched.c | 108 +++++++++--------- 6 files changed, 85 insertions(+), 85 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 496ed4ce..e1140042 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,9 +8,8 @@ "intelliSenseMode": "${default}", "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", - "cppStandard": "gnu++14", - "configurationProvider": "ms-vscode.makefile-tools" + "cppStandard": "gnu++14" } ], "version": 4 -} \ No newline at end of file +} diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/cc_os_sched.h index b94bb30e..4b40b49a 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sched.h @@ -18,9 +18,9 @@ typedef struct cc_sched cc_sched_t; typedef enum { cc_sched_task_status_exit = 0x00, ///> Initial State - cc_sched_task_status_running, ///> Task currently running - cc_sched_task_status_ready, ///> Task Ready to despatch - cc_sched_task_status_wait, ///> Task in wait state + cc_sched_task_status_running = 0x01, ///> Task currently running + cc_sched_task_status_ready = 0x02, ///> Task Ready to despatch + cc_sched_task_status_wait = 0x03, ///> Task in wait state cc_sched_task_status_max = 0xff, ///> Do Nt Use } cc_sched_task_status_t; @@ -32,7 +32,7 @@ typedef struct link struct cc_sched_tcb { - char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + char name [ccosconfig_CC_OS_TASK_NAME_LEN + 1]; ///> Name of the Current Task size_t priority; ///> Priority of the task void * stack_ptr; ///> Stack Pointer size_t task_delay_ticks; ///> Time delay in ticks @@ -58,7 +58,7 @@ typedef void (* algo_fn)(cc_sched_ctrl_t * sched_ctrl); typedef enum { cc_sched_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm - cc_sched_algo_priority_driven, ///> Priority driven Scheduling + cc_sched_algo_priority_driven = 0x01, ///> Priority driven Scheduling cc_sched_algo_max = 0xff }cc_sched_algo_t; diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h index 1d192e95..099efc94 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -4,4 +4,4 @@ #include void * cc_os_malloc(size_t size); -void * cc_os_free(void *addr); +void * cc_os_free(const void *addr); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index c2eb579f..9619e458 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -37,8 +37,8 @@ extern cc_sched_tcb_t *g_cc_os_tcb_list; * INTERNAL EXTERNS FUNCTIONS *****************************************************/ extern void _cc_os_idle_task_fn (void * args); -extern void _insert_after (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); -extern void _insert_before (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); @@ -52,7 +52,10 @@ extern cc_sched_ctrl_t g_sched_ctrl; /***************************************************** * STATIC FUNCTION DEFINATIONS *****************************************************/ - +void __cc_init_scheduler() +{ + return; +} /***************************************************** * USER FUNCTION DEFINATIONS *****************************************************/ @@ -104,12 +107,12 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { #else /* Dynamic Task Declaration */ - ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr != NULL) + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); { #endif /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); + strlcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; } @@ -126,7 +129,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) { - _insert_after(g_sched_ctrl.task_max_prio, ptr, CC_OS_FALSE); + _insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE); g_sched_ctrl.task_max_prio = ptr; } else @@ -136,7 +139,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { if (comp_ptr->priority <= ptr->priority) { - _insert_after(comp_ptr, ptr, CC_OS_FALSE); + _insert_after(&comp_ptr, ptr, CC_OS_FALSE); break; } else @@ -192,7 +195,7 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_pause_all_task () +cc_os_err_t cc_os_pause_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; @@ -210,19 +213,24 @@ cc_os_err_t cc_os_pause_all_task () return success; } -cc_os_err_t cc_os_resume_all_task () +cc_os_err_t cc_os_resume_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; - - while (ptr != g_sched_ctrl.ready_list_head) + if (ptr != NULL) { - if (ptr == g_sched_ctrl.curr_task) + while (ptr != g_sched_ctrl.ready_list_head) { - continue; + if (ptr == g_sched_ctrl.curr_task) + { + continue; + } + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + ptr = ptr->ready_link.next; } - - _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - ptr = ptr->ready_link.next; + } + else + { + return error_os_invalid_op; } return success; @@ -262,7 +270,6 @@ void cc_os_task_wait (const size_t ticks) void cc_os_task_yield() { - TODO("cc_os_task_yield"); return; } @@ -281,6 +288,7 @@ void cc_os_run (void) cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); /* Initialise scheduler */ + __cc_init_scheduler(); cc_os_task_yield(); /* Yeild */ while (1) { diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index f75a30d3..afc9cb52 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -16,7 +16,6 @@ /***************************************************** * GLOBAL/STATIC VARIABLE DECLARATIONS *****************************************************/ -// static unsigned char g_cc_os_heap[ccosconfig_CC_OS_HEAP_SIZE]; /***************************************************** * GLOBAL EXTERNS @@ -33,7 +32,7 @@ void * cc_os_malloc(size_t size _UNUSED) { return NULL; } -void * cc_os_free(void *addr _UNUSED ) +void * cc_os_free(const void *addr _UNUSED ) { return NULL; } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index c46dd285..951f4d13 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -60,76 +60,79 @@ cc_sched_ctrl_t g_sched_ctrl = /***************************************************** * INTERNAL USED FUNCTIONS (NON-STATIC) *****************************************************/ -void _insert_after(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { switch (link_type) { case 0: /* Ready Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->ready_link.next = new_node; new_node->ready_link.prev = new_node; } else { - new_node->ready_link.next = ptr->ready_link.next; - new_node->ready_link.prev = ptr; + new_node->ready_link.next = (*ptr)->ready_link.next; + new_node->ready_link.prev = *ptr; new_node->ready_link.next->ready_link.prev = new_node; - ptr->ready_link.next = new_node; + (*ptr)->ready_link.next = new_node; } break; case 1: /* Wait Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->wait_link.next = new_node; new_node->wait_link.prev = new_node; } + else { - new_node->wait_link.next = ptr->wait_link.next; - new_node->wait_link.prev = ptr; + new_node->wait_link.next = (*ptr)->wait_link.next; + new_node->wait_link.prev = *ptr; new_node->wait_link.next->wait_link.prev = new_node; - ptr->wait_link.next = new_node; + (*ptr)->wait_link.next = new_node; } break; default: return; } } -void _insert_before(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { switch (link_type) { case 0: /* Ready Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->ready_link.next = new_node; new_node->ready_link.prev = new_node; } + else { - new_node->ready_link.next = ptr; - new_node->ready_link.prev = ptr->ready_link.prev; - ptr->ready_link.prev = new_node; + new_node->ready_link.next = *ptr; + new_node->ready_link.prev = (*ptr)->ready_link.prev; + (*ptr)->ready_link.prev = new_node; new_node->ready_link.prev->ready_link.next = new_node; } break; case 1: /* Wait Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->wait_link.next = new_node; new_node->wait_link.prev = new_node; } + else { - new_node->wait_link.next = ptr; - new_node->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = new_node; + new_node->wait_link.next = *ptr; + new_node->wait_link.prev = (*ptr)->wait_link.prev; + (*ptr)->wait_link.prev = new_node; new_node->wait_link.prev->wait_link.next = new_node; } break; @@ -144,7 +147,7 @@ void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, { return; } - _insert_before(sched_ctrl->wait_list_head, ptr, 1); + _insert_before(&(sched_ctrl->wait_list_head), ptr, 1); ptr->task_delay_ticks = ticks; ptr->task_status = cc_sched_task_status_wait; @@ -185,26 +188,22 @@ static void __cc_sched_context_switch(cc_sched_tcb_t * next_task) static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) { - cc_sched_tcb_t * ptr; - if(sched_ctrl->wait_list_head != NULL) + cc_sched_tcb_t * ptr = sched_ctrl->wait_list_head; + + while(ptr != NULL) { - ptr = sched_ctrl->wait_list_head; - /* Tasks present in wait list */ - while(1) + ptr->task_delay_ticks--; /* Tick caliberations required */ + if(ptr->task_delay_ticks == 0) { - ptr->task_delay_ticks--; /* Tick caliberations required */ - if(ptr->task_delay_ticks == 0) - { - _cc_sched_send_to_resume(sched_ctrl, ptr); - } - if (ptr->wait_link.next == sched_ctrl->wait_list_head) - { - break; - } - else - { - ptr = ptr->wait_link.next; - } + _cc_sched_send_to_resume(sched_ctrl, ptr); + } + if (ptr->wait_link.next == sched_ctrl->wait_list_head) + { + break; + } + else + { + ptr = ptr->wait_link.next; } } } @@ -234,26 +233,21 @@ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; - __cc_sched_wait_list_adjustment(sched_ctrl); - while (1) + + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; + if(ptr != NULL) { - if (ptr->task_status == cc_sched_task_status_ready) - { - break; - } - else + while (ptr->task_status != cc_sched_task_status_ready) { - /* Iterate Backwards */ ptr = ptr->wait_link.prev; + if (ptr == sched_ctrl->ready_list_head) + { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + break; + } } - if (ptr == sched_ctrl->ready_list_head) - { - /* IDLE Task */ - _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - break; - } + __cc_sched_context_switch(ptr); } - __cc_sched_context_switch(ptr); } From 84611173d6a5dc44b5cb496b09b32ab7dacd3734 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Mon, 26 Dec 2022 11:08:15 +0530 Subject: [PATCH 12/38] cc_os_sem proto added --- src/include/visor/terravisor/cc_os/cc_os.h | 13 +--- .../visor/terravisor/cc_os/cc_os_sem.h | 32 +++++++++ .../visor/terravisor/cc_os/utils/cc_os_heap.h | 2 +- .../cc_os/{ => utils}/cc_os_sched.h | 22 +++++- src/visor/terravisor/services/kernel/cc_os.c | 45 ++++++------ .../terravisor/services/kernel/cc_os_heap.c | 4 +- .../terravisor/services/kernel/cc_os_idle.c | 10 +-- .../terravisor/services/kernel/cc_os_sched.c | 68 +++++++++--------- .../terravisor/services/kernel/cc_os_sem.c | 69 +++++++++++++++++++ 9 files changed, 192 insertions(+), 73 deletions(-) create mode 100644 src/include/visor/terravisor/cc_os/cc_os_sem.h rename src/include/visor/terravisor/cc_os/{ => utils}/cc_os_sched.h (74%) create mode 100644 src/visor/terravisor/services/kernel/cc_os_sem.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 0e231e4d..deb4fb3d 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -18,18 +18,7 @@ #include "stdlib.h" #include "stdint.h" #include -#include - -/***************************************************** - * DEFINES - *****************************************************/ -#define CC_OS_FALSE 0 -#define CC_OS_TRUE 1 -#define CC_OS_DELAY_MAX ((size_t) -1) - -#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC - -#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg +#include /***************************************************** * TYPEDEFS diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h new file mode 100644 index 00000000..4e359e2d --- /dev/null +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -0,0 +1,32 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS semaphore declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +/***************************************************** + * DEFINES + *****************************************************/ +#include +/***************************************************** + * TYPEDEFS + *****************************************************/ +typedef struct sem +{ + uint8_t sem_init; + size_t sem_val; +}sem_t; + +/***************************************************** + * USER FUNCTION DECLARATIONS + *****************************************************/ +cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val); +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val); diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h index 099efc94..87c025d7 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -1,7 +1,7 @@ #include #include #include -#include +#include void * cc_os_malloc(size_t size); void * cc_os_free(const void *addr); diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h similarity index 74% rename from src/include/visor/terravisor/cc_os/cc_os_sched.h rename to src/include/visor/terravisor/cc_os/utils/cc_os_sched.h index 4b40b49a..d686ee72 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h @@ -10,8 +10,28 @@ #pragma once -#include "stdint.h" +/***************************************************** + * INCLUDES + *****************************************************/ +#include +#include +#include +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_FALSE 0 +#define CC_OS_TRUE 1 +#define CC_OS_NULL_PTR NULL +#define CC_OS_DELAY_MAX ((size_t) -1) + +#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC + +#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg + +/***************************************************** + * TYPEDEFS + *****************************************************/ typedef struct cc_sched_tcb cc_sched_tcb_t; typedef struct cc_sched cc_sched_t; diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index 9619e458..d8cfb8b3 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -17,7 +17,6 @@ #include #include #include -#include /***************************************************** * DEFINES @@ -37,8 +36,8 @@ extern cc_sched_tcb_t *g_cc_os_tcb_list; * INTERNAL EXTERNS FUNCTIONS *****************************************************/ extern void _cc_os_idle_task_fn (void * args); -extern void _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); -extern void _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern status_t _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern status_t _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); @@ -61,10 +60,10 @@ void __cc_init_scheduler() *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { - CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->name != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->name != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_NULL_PTR); CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_len != CC_OS_FALSE); CC_OS_ASSERT_IF_FALSE(cc_os_task->priority >= ccosconfig_CC_OS_IDLE_TASK_PRIORITY); CC_OS_ASSERT_IF_FALSE(cc_os_task->priority < CC_OS_PRIORITY_MAX); @@ -74,18 +73,18 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; #if CC_OS_DYNAMIC == CC_OS_TRUE - if (ptr == NULL) + if (ptr == CC_OS_NULL_PTR) { /* First Dynamic task */ ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); - if (ptr == NULL) + if (ptr == CC_OS_NULL_PTR) { cc_os_resume_all_task(); return error_os_task_overfow; } } #endif - if ((ptr->ready_link.next == NULL )&& (ptr->ready_link.prev == NULL)) + if ((ptr->ready_link.next == CC_OS_NULL_PTR )&& (ptr->ready_link.prev == CC_OS_NULL_PTR)) { ptr->ready_link.next = ptr->ready_link.prev = g_sched_ctrl.ready_list_head; } @@ -107,7 +106,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { #else /* Dynamic Task Declaration */ - if (ptr != NULL) + if (ptr != CC_OS_NULL_PTR) ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); { #endif @@ -123,23 +122,29 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } } /* Insert Tasks in assending order of its priority */ - if (g_sched_ctrl.task_max_prio == NULL) + if (g_sched_ctrl.task_max_prio == CC_OS_NULL_PTR) { g_sched_ctrl.task_max_prio = ptr; } else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) { - _insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE); - g_sched_ctrl.task_max_prio = ptr; + if(_insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE) == success) + { + g_sched_ctrl.task_max_prio = ptr; + } + else + { + cc_os_resume_all_task(); + return error_os_task_overfow; + } } else { cc_sched_tcb_t * comp_ptr = g_sched_ctrl.task_max_prio->ready_link.next; while (1) { - if (comp_ptr->priority <= ptr->priority) + if (comp_ptr->priority <= ptr->priority && (_insert_after(&comp_ptr, ptr, CC_OS_FALSE) == success)) { - _insert_after(&comp_ptr, ptr, CC_OS_FALSE); break; } else @@ -162,7 +167,7 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (cc_os_task != NULL) + if (cc_os_task != CC_OS_NULL_PTR) { ptr = cc_os_task->task_tcb_ptr; } @@ -185,7 +190,7 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (cc_os_task != NULL) + if (cc_os_task != CC_OS_NULL_PTR) { ptr = cc_os_task->task_tcb_ptr; } @@ -216,7 +221,7 @@ cc_os_err_t cc_os_pause_all_task (void) cc_os_err_t cc_os_resume_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; - if (ptr != NULL) + if (ptr != CC_OS_NULL_PTR) { while (ptr != g_sched_ctrl.ready_list_head) { @@ -238,7 +243,7 @@ cc_os_err_t cc_os_resume_all_task (void) cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { - CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); cc_sched_tcb_t * ptr = cc_os_task->task_tcb_ptr; diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index afc9cb52..c063d2f7 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -30,9 +30,9 @@ *****************************************************/ void * cc_os_malloc(size_t size _UNUSED) { - return NULL; + return CC_OS_NULL_PTR; } void * cc_os_free(const void *addr _UNUSED ) { - return NULL; + return CC_OS_NULL_PTR; } diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 0d413f52..9d837e6f 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -30,9 +30,9 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; -#if ccosconfig_CC_OS_USE_DYNAMIC == 0 - ptr->ready_link.next = NULL; - ptr->ready_link.prev = NULL; +#if CC_OS_DYNAMIC == CC_OS_FALSE + ptr->ready_link.next = CC_OS_NULL_PTR; + ptr->ready_link.prev = CC_OS_NULL_PTR; #else cc_os_free(ptr); #endif @@ -47,9 +47,9 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) void _cc_os_idle_task_fn(os_args args) { cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; - static cc_sched_tcb_t * ptr = NULL; + static cc_sched_tcb_t * ptr = CC_OS_NULL_PTR; ptr = g_sched_ctrl->ready_list_head; - while (1) + while (CC_OS_TRUE) { /* Clean up task if terminated */ ptr = __free_terminated_task(ptr); diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index 951f4d13..a6629c1a 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -11,9 +11,8 @@ /***************************************************** * INCLUDES *****************************************************/ -#include #include -#include +#include /***************************************************** * DEFINES @@ -34,7 +33,7 @@ static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); #if !ccosconfig_CC_OS_USE_DYNAMIC cc_sched_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; #else -cc_sched_tcb_t * g_cc_os_tcb_list = NULL; +cc_sched_tcb_t * g_cc_os_tcb_list = CC_OS_NULL_PTR; #endif cc_sched_t g_cc_sched_list [] = @@ -46,27 +45,28 @@ cc_sched_t g_cc_sched_list [] = cc_sched_ctrl_t g_sched_ctrl = { #if !ccosconfig_CC_OS_USE_DYNAMIC - .ready_list_head = &(g_cc_os_tcb_list[0]), - .curr_task = &(g_cc_os_tcb_list[0]), + .ready_list_head = &(g_cc_os_tcb_list[CC_OS_FALSE]), + .curr_task = &(g_cc_os_tcb_list[CC_OS_FALSE]), #else - .ready_list_head = NULL, - .curr_task = NULL, + .ready_list_head = CC_OS_NULL_PTR, + .curr_task = CC_OS_NULL_PTR, #endif - .wait_list_head = NULL, - .task_max_prio = NULL, + .wait_list_head = CC_OS_NULL_PTR, + .task_max_prio = CC_OS_NULL_PTR, .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) }; /***************************************************** * INTERNAL USED FUNCTIONS (NON-STATIC) *****************************************************/ -void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +status_t _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { + CC_OS_ASSERT_IF_FALSE(new_node != CC_OS_NULL_PTR); switch (link_type) { - case 0: + case CC_OS_FALSE: /* Ready Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->ready_link.next = new_node; @@ -80,9 +80,9 @@ void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t lin (*ptr)->ready_link.next = new_node; } break; - case 1: + case CC_OS_TRUE: /* Wait Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->wait_link.next = new_node; @@ -97,16 +97,18 @@ void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t lin } break; default: - return; + return error_os_invalid_op; } + return success; } -void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +status_t _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { + CC_OS_ASSERT_IF_FALSE(new_node != CC_OS_NULL_PTR); switch (link_type) { - case 0: + case CC_OS_FALSE: /* Ready Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->ready_link.next = new_node; @@ -120,9 +122,9 @@ void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t li new_node->ready_link.prev->ready_link.next = new_node; } break; - case 1: + case CC_OS_TRUE: /* Wait Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->wait_link.next = new_node; @@ -137,8 +139,9 @@ void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t li } break; default: - return; + return error_os_invalid_op; } + return success; } void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks) @@ -147,10 +150,11 @@ void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, { return; } - _insert_before(&(sched_ctrl->wait_list_head), ptr, 1); - - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_sched_task_status_wait; + if(_insert_before(&(sched_ctrl->wait_list_head), ptr, CC_OS_TRUE) == success) + { + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; + } } void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr) @@ -167,14 +171,14 @@ void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) { /* Last Wait task left */ - sched_ctrl->wait_list_head = NULL; + sched_ctrl->wait_list_head = CC_OS_NULL_PTR; } } ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = NULL; - ptr->wait_link.next = NULL; - ptr->task_delay_ticks = 0; + ptr->wait_link.prev = CC_OS_NULL_PTR; + ptr->wait_link.next = CC_OS_NULL_PTR; + ptr->task_delay_ticks = CC_OS_FALSE; ptr->task_status = cc_sched_task_status_ready; } @@ -190,10 +194,10 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) { cc_sched_tcb_t * ptr = sched_ctrl->wait_list_head; - while(ptr != NULL) + while(ptr != CC_OS_NULL_PTR) { ptr->task_delay_ticks--; /* Tick caliberations required */ - if(ptr->task_delay_ticks == 0) + if(ptr->task_delay_ticks == CC_OS_FALSE) { _cc_sched_send_to_resume(sched_ctrl, ptr); } @@ -236,7 +240,7 @@ static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) __cc_sched_wait_list_adjustment(sched_ctrl); cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; - if(ptr != NULL) + if(ptr != CC_OS_NULL_PTR) { while (ptr->task_status != cc_sched_task_status_ready) { diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c new file mode 100644 index 00000000..eb38be51 --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -0,0 +1,69 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2CC_OS_FALSE22, Cyancore Team + * + * File Name : cc_os_sem.h + * Description : CC OS Semaphore function definations + * Primary Author : Pranjal Chanda [pranjalchandaCC_OS_FALSE8@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ +#include + +/***************************************************** + * GLOBAL/STATIC VARIABLE DECLARATIONS + *****************************************************/ + +/***************************************************** + * GLOBAL EXTERNS + *****************************************************/ + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ + +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ + +cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init == CC_OS_FALSE)); + + sem_ptr->sem_init = CC_OS_TRUE; + sem_ptr->sem_val = init_val; + + return success; +} +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + return success; +} +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + return success; +} +cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + sem_ptr->sem_init = CC_OS_FALSE; + + return success; +} +cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + CC_OS_ASSERT_IF_FALSE(val != CC_OS_NULL_PTR); + + *val = sem_ptr->sem_val; + + return success; +} From 4b50c32bcb3de033810a9457d788f8f07d9c541a Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Tue, 27 Dec 2022 09:32:32 +0530 Subject: [PATCH 13/38] Semaphore complete !Need to add semaphore in ISR logic yet --- src/include/status.h | 1 + .../visor/terravisor/cc_os/cc_os_config.h | 8 +++ .../visor/terravisor/cc_os/cc_os_sem.h | 22 ++++++-- .../terravisor/services/kernel/cc_os_idle.c | 6 +- .../terravisor/services/kernel/cc_os_sem.c | 56 +++++++++++++++---- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/include/status.h b/src/include/status.h index 0264eb68..5b31e428 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -77,6 +77,7 @@ typedef enum status error_os_task_overfow = -0x0f01, error_os_deadlock = -0x0f02, error_os_invalid_op = -0x0f03, + error_os_sem_get = -0x0f04, /* Mesg related error */ error_mesg = -0x1000, error_mesg_long = -0x1001, diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index 729b34a6..3ffb4fd6 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -59,3 +59,11 @@ #ifndef ccosconfig_CC_OS_TASK_STACK_LEN #define ccosconfig_CC_OS_TASK_STACK_LEN 255 #endif + +/** + * @brief If the Kernel shall enter poer save mode during IDLE + * + */ +#ifndef ccosconfig_CC_OS_POWER_SAVE_EN +#define ccosconfig_CC_OS_POWER_SAVE_EN 0 +#endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h index 4e359e2d..a0099c82 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sem.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -21,12 +21,26 @@ typedef struct sem uint8_t sem_init; size_t sem_val; }sem_t; +/***************************************************** + * USER MACROS + *****************************************************/ +#define CC_SEM_INST(_Name) _Name##_sem_inst +#if CC_OS_DYNAMIC == CC_OS_FALSE +#define CC_SEM_DEF(_Name) \ +static sem_t _Name##_sem = { \ + .sem_init = 0, \ + .sem_val = 0 \ +}; \ +static sem_t * _Name##_sem_inst = &_Name##_sem +#else +static sem_t * _Name##_sem_inst = CC_OS_NULL_PTR +#endif /***************************************************** * USER FUNCTION DECLARATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val); -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val); +cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr); cc_os_err_t cc_os_sem_give (sem_t * sem_ptr); -cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr); -cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val); +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks); +cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val); diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 9d837e6f..0d47ee88 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -46,16 +46,18 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) *****************************************************/ void _cc_os_idle_task_fn(os_args args) { - cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; + cc_sched_ctrl_t * sched_ctrl = (cc_sched_ctrl_t *) args; static cc_sched_tcb_t * ptr = CC_OS_NULL_PTR; - ptr = g_sched_ctrl->ready_list_head; + ptr = sched_ctrl->ready_list_head; while (CC_OS_TRUE) { /* Clean up task if terminated */ ptr = __free_terminated_task(ptr); +#if ccosconfig_CC_OS_POWER_SAVE_EN /* Power Save code */ +#endif /* Yield for next available task */ cc_os_task_yield(); } diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c index eb38be51..49690df7 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sem.c +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -12,6 +12,7 @@ * INCLUDES *****************************************************/ #include +#include /***************************************************** * GLOBAL/STATIC VARIABLE DECLARATIONS @@ -29,36 +30,71 @@ * USER FUNCTION DEFINATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val) +cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val) { - CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init == CC_OS_FALSE)); +#if CC_OS_DYNAMIC == CC_OS_FALSE + CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init == CC_OS_FALSE)); - sem_ptr->sem_init = CC_OS_TRUE; - sem_ptr->sem_val = init_val; + (*sem_ptr)->sem_init = CC_OS_TRUE; + (*sem_ptr)->sem_val = init_val; +#else + CC_OS_ASSERT_IF_FALSE(*sem_ptr == CC_OS_NULL_PTR); + *sem_ptr = cc_os_malloc(sizeof(sem_t)); + if (*sem_ptr == CC_OS_NULL_PTR) + { + return error_memory_low; + } + else + { + (*sem_ptr)->sem_val = init_val; + (*sem_ptr)->sem_init = CC_OS_TRUE; + } +#endif return success; } -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + if (sem_ptr->sem_val == CC_OS_FALSE) + { + if (wait_ticks == CC_OS_FALSE) /* ||_IS_ISR */ + { + return error_os_sem_get; + } + else + { + cc_os_task_wait(wait_ticks); + } + } + else + { + sem_ptr->sem_val--; + } return success; } -cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + sem_ptr->sem_val++; + return success; } -cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr) { - CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init != CC_OS_FALSE)); + + (*sem_ptr)->sem_init = CC_OS_FALSE; - sem_ptr->sem_init = CC_OS_FALSE; +#if CC_OS_DYNAMIC == CC_OS_TRUE + cc_os_free(*sem_ptr); +#endif return success; } -cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val) +cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); CC_OS_ASSERT_IF_FALSE(val != CC_OS_NULL_PTR); From fdd0150131127611842b588031aaa6e49283e47c Mon Sep 17 00:00:00 2001 From: Pranjal Date: Tue, 23 Aug 2022 09:19:59 +0530 Subject: [PATCH 14/38] Proto complete --- projects/demo_avr/config.mk | 1 + src/include/visor/terravisor/cc_os/cc_os.h | 92 ++++++++++++++++++- .../visor/terravisor/cc_os/cc_os_config.h | 12 ++- src/include/visor/terravisor/cc_os/cc_shed.h | 31 +++++-- src/visor/terravisor/services/kernel/cc_os.c | 36 ++++++-- .../terravisor/services/kernel/cc_os_shed.c | 7 +- 6 files changed, 156 insertions(+), 23 deletions(-) diff --git a/projects/demo_avr/config.mk b/projects/demo_avr/config.mk index 8ca18de0..9fcc3484 100644 --- a/projects/demo_avr/config.mk +++ b/projects/demo_avr/config.mk @@ -17,3 +17,4 @@ BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 OBRDLED_ENABLE := 1 +TERRAKERN := 1 diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index fbed0837..26d13b71 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -1,29 +1,117 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #ifndef __CC_OS__ #define __CC_OS__ #include "status.h" #include "stdint.h" +#if ccosconfig_CC_OS_USE_DYNAMIC == 0 +#define CC_DYNAMIC 0 +#endif + #define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg typedef status_t cc_os_err_t; typedef void (* task_fn)(void * args); +typedef const char c_char; +/** + * @brief TASK infrastructure structure + * + */ typedef struct cc_os_task { task_fn task_fn; - char * name; + c_char * name; size_t priority; //>> For waited tasks - void * stack_ptr; + size_t * stack_ptr; size_t stack_len; }cc_os_task_t; +/** + * @brief Function to declare a static task with a dedicated stack for the task + * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN){ \ +static size_t _NAME##_stack[STACK_LEN]; \ +static const cc_os_task_t _NAME##_task = { \ + .task_fn = _fn, \ + .name = #_NAME, \ + .priority = _PRI, \ + .stack_ptr = _NAME##_stack, \ + .stack_len = STACK_LEN \ + }; \ +} + +/** + * @brief Function to get the instance using its name of already declared task. + * @brief Usage: cc_os_task_t * task = &(CC_GET_TASK_INST(TASK_Name)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_GET_TASK_INST(_NAME) _NAME##_task + +/** + * @brief A Function to add a task to the scheduler + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task + * @return cc_os_err_t + */ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task); + +/** + * @brief A function to delete a task from the scheduler + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_del_task (const char *name); + +/** + * @brief A Function to pause the task until call resume explicitly + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_pause_task (const char *name); + +/** + * + * @brief A Function to resume paused task. + * @note Calling this function for already non-waiting task has no effect. + * + * @param name + * @return cc_os_err_t + */ cc_os_err_t cc_os_resume_task (const char *name); + +/** + * @brief A Function to put the task to a waiting state and yield + * + * @param ticks Number of CC_OS Ticks + * @return cc_os_err_t + */ cc_os_err_t cc_os_wait_task (const size_t ticks); + +/** + * @brief A Function to invoke the kernel + * + * @return cc_os_err_t + */ cc_os_err_t cc_os_run (void); #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index cbc05a51..c82d92ba 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -1,9 +1,13 @@ #pragma once -#ifndef CC_OS_MAX_THREAD -#define CC_OS_MAX_THREAD 10 +#ifndef ccosconfig_CC_OS_USE_DYNAMIC +#define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif -#ifndef CC_OS_TASK_NAME_LEN -#define CC_OS_TASK_NAME_LEN 16 +#ifndef ccosconfig_CC_OS_MAX_THREAD +#define ccosconfig_CC_OS_MAX_THREAD 10 +#endif + +#ifndef ccosconfig_CC_OS_TASK_NAME_LEN +#define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif diff --git a/src/include/visor/terravisor/cc_os/cc_shed.h b/src/include/visor/terravisor/cc_os/cc_shed.h index 27096a76..1a6dde15 100644 --- a/src/include/visor/terravisor/cc_os/cc_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_shed.h @@ -1,3 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_shed.h + * Description : CC OS Kernel scheduler declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #pragma once #include "stdint.h" @@ -6,18 +16,19 @@ typedef struct cc_shed_tcb cc_shed_tcb_t; typedef enum { - cc_shed_task_terminated, - cc_shed_task_ready, - cc_shed_task_running, - cc_shed_task_paused, + cc_shed_task_terminated, ///> Initial State + cc_shed_task_ready, ///> Task Ready to despatch + cc_shed_task_running, ///> Task currently running + cc_shed_task_wait, ///> Task in wait state } cc_shed_task_status_t; struct cc_shed_tcb { - char name [CC_OS_TASK_NAME_LEN]; - size_t priority; - void * stack_ptr; - cc_shed_tcb_t * prev_shed_tcb; - cc_shed_tcb_t * next_shed_tcb; - cc_shed_task_status_t task_status; + char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + size_t priority; ///> Priority of the task + void * stack_ptr; ///> Stack Pointer + size_t task_delay_ticks; ///> Time delay in ticks + cc_shed_tcb_t * prev_shed_tcb; ///> Previous task pointer + cc_shed_tcb_t * next_shed_tcb; ///> Next task pointer + cc_shed_task_status_t task_status; ///> Current state of the task }; diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index a666abc5..b7b4c45c 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -5,7 +5,12 @@ #include extern cc_shed_tcb_t * g_list_head; + +#if CC_DYNAMIC == 0 extern cc_shed_tcb_t g_cc_os_tcb_list[]; +#else +extern cc_shed_tcb_t *g_cc_os_tcb_list; +#endif extern cc_shed_tcb_t * g_curr_task; cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) @@ -18,6 +23,9 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_shed_tcb_t * ptr = g_list_head; +#if CC_DYNAMIC == 0 + + /* Static Task Allocation */ if ((ptr->next_shed_tcb == NULL )&& (ptr->prev_shed_tcb == NULL)) { ptr->next_shed_tcb = ptr->prev_shed_tcb = g_list_head; @@ -25,7 +33,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) else { - for (size_t i = 0; i < CC_OS_MAX_THREAD; i++) + for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ if (g_cc_os_tcb_list[i].task_status == cc_shed_task_terminated) @@ -48,18 +56,23 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } } +#else + /* Dynamic Task Declaration */ +#endif /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, CC_OS_TASK_NAME_LEN); + memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; ptr->task_status = cc_shed_task_ready; - return success; } cc_os_err_t cc_os_del_task (const char * name) { cc_shed_tcb_t * ptr = g_curr_task; + +#if !CC_DYNAMIC + int name_found = 0; if (name != NULL) { @@ -96,7 +109,9 @@ cc_os_err_t cc_os_del_task (const char * name) } } - +#else + /* Dynamic Task Deletion */ +#endif ptr->next_shed_tcb->prev_shed_tcb = ptr->prev_shed_tcb; ptr->prev_shed_tcb->next_shed_tcb = ptr->next_shed_tcb; ptr->task_status = cc_shed_task_terminated; @@ -128,7 +143,7 @@ cc_os_err_t cc_os_pause_task (const char * name) } } - ptr->task_status = cc_shed_task_paused; + ptr->task_status = cc_shed_task_wait; return success; } @@ -159,8 +174,17 @@ cc_os_err_t cc_os_resume_task (const char * name) return success; } -cc_os_err_t cc_os_wait_task (const size_t ticks _UNUSED) +cc_os_err_t cc_os_wait_task (const size_t ticks) { + ASSERT_IF_FALSE(ticks > 0); + + cc_shed_tcb_t * ptr = g_list_head; + + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_shed_task_wait; + + // Yield + return success; } diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 56d06e6e..9dc1247b 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,6 +1,11 @@ #include #include -cc_shed_tcb_t g_cc_os_tcb_list [CC_OS_MAX_THREAD]; +#if !ccosconfig_CC_OS_USE_DYNAMIC + cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +#else + cc_shed_tcb_t *g_cc_os_tcb_list; +#endif + cc_shed_tcb_t * g_list_head = &(g_cc_os_tcb_list[0]); cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); From dcfda833d0fd111282ec655cdc3333be012f3548 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 21 Dec 2022 19:34:59 +0530 Subject: [PATCH 15/38] add cc_os dynaic task and cc_os_malloc proto. --- src/include/visor/terravisor/cc_os/cc_os.h | 54 ++++-- .../cc_os/{cc_shed.h => cc_os_shed.h} | 4 +- .../visor/terravisor/cc_os/utils/cc_os_heap.h | 7 + src/visor/terravisor/services/kernel/cc_os.c | 156 +++++++++++++----- .../terravisor/services/kernel/cc_os_heap.c | 10 ++ .../terravisor/services/kernel/cc_os_shed.c | 15 +- 6 files changed, 187 insertions(+), 59 deletions(-) rename src/include/visor/terravisor/cc_os/{cc_shed.h => cc_os_shed.h} (88%) create mode 100644 src/include/visor/terravisor/cc_os/utils/cc_os_heap.h create mode 100644 src/visor/terravisor/services/kernel/cc_os_heap.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 26d13b71..d5d7611b 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -14,9 +14,7 @@ #include "status.h" #include "stdint.h" -#if ccosconfig_CC_OS_USE_DYNAMIC == 0 -#define CC_DYNAMIC 0 -#endif +#define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC #define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg @@ -71,33 +69,63 @@ static const cc_os_task_t _NAME##_task = { \ * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task * @return cc_os_err_t */ -cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task); +cc_os_err_t cc_os_add_task(cc_os_task_t * cc_os_task); /** - * @brief A function to delete a task from the scheduler + * @brief A function to delete a task from the scheduler by instance * - * @param name + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_del_task(cc_os_task_t * cc_os_task); + +/** + * @brief A Function to pause the task until call resume explicitly using its instance + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task * @return cc_os_err_t */ -cc_os_err_t cc_os_del_task (const char *name); +cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task); + +/** + * + * @brief A Function to resume paused task using its instance + * @note Calling this function for already non-waiting task has no effect. + * + * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task); + +/** + * @brief A function to delete a task from the scheduler by its name + * + * @param name Name of the task to be terminated; Pass NULL to point to current task + * @return cc_os_err_t + */ +cc_os_err_t cc_os_del_task_by_name(const char * name); + /** - * @brief A Function to pause the task until call resume explicitly + * @brief A Function to pause the task until call resume explicitly by its name * * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_pause_task (const char *name); +cc_os_err_t cc_os_pause_task_by_name(const char *name); /** * - * @brief A Function to resume paused task. + * @brief A Function to resume paused task by its name * @note Calling this function for already non-waiting task has no effect. * * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_resume_task (const char *name); +cc_os_err_t cc_os_resume_task_by_name(const char *name); /** * @brief A Function to put the task to a waiting state and yield @@ -105,13 +133,13 @@ cc_os_err_t cc_os_resume_task (const char *name); * @param ticks Number of CC_OS Ticks * @return cc_os_err_t */ -cc_os_err_t cc_os_wait_task (const size_t ticks); +cc_os_err_t cc_os_wait_task(const size_t ticks); /** * @brief A Function to invoke the kernel * * @return cc_os_err_t */ -cc_os_err_t cc_os_run (void); +cc_os_err_t cc_os_run(void); #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h similarity index 88% rename from src/include/visor/terravisor/cc_os/cc_shed.h rename to src/include/visor/terravisor/cc_os/cc_os_shed.h index 1a6dde15..66360975 100644 --- a/src/include/visor/terravisor/cc_os/cc_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_os_shed.h @@ -28,7 +28,7 @@ struct cc_shed_tcb size_t priority; ///> Priority of the task void * stack_ptr; ///> Stack Pointer size_t task_delay_ticks; ///> Time delay in ticks - cc_shed_tcb_t * prev_shed_tcb; ///> Previous task pointer - cc_shed_tcb_t * next_shed_tcb; ///> Next task pointer + cc_shed_tcb_t * prev_ready_tcb; ///> Previous task pointer + cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer cc_shed_task_status_t task_status; ///> Current state of the task }; diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h new file mode 100644 index 00000000..1d192e95 --- /dev/null +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -0,0 +1,7 @@ +#include +#include +#include +#include + +void * cc_os_malloc(size_t size); +void * cc_os_free(void *addr); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index b7b4c45c..b2deec90 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -1,10 +1,11 @@ #include #include +#include #include #include -#include +#include -extern cc_shed_tcb_t * g_list_head; +extern cc_shed_tcb_t * g_ready_list_head; #if CC_DYNAMIC == 0 extern cc_shed_tcb_t g_cc_os_tcb_list[]; @@ -21,18 +22,28 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); ASSERT_IF_FALSE(cc_os_task->stack_len != 0); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; -#if CC_DYNAMIC == 0 - - /* Static Task Allocation */ - if ((ptr->next_shed_tcb == NULL )&& (ptr->prev_shed_tcb == NULL)) +#if CC_DYNAMIC == 1 + if (ptr == NULL) + { + /* First Dynamic task */ + ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + if (ptr == NULL) + { + return error_os_task_overfow; + } + } +#endif + if ((ptr->next_ready_tcb == NULL )&& (ptr->prev_ready_tcb == NULL)) { - ptr->next_shed_tcb = ptr->prev_shed_tcb = g_list_head; + ptr->next_ready_tcb = ptr->prev_ready_tcb = g_ready_list_head; } else { +#if CC_DYNAMIC == 0 + /* Static Task Allocation */ for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ @@ -42,23 +53,25 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) break; } } - if (ptr != g_list_head) + if (ptr != g_ready_list_head) + { +#else + /* Dynamic Task Declaration */ + ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + if (ptr != NULL) { +#endif /* Insert node at last */ - ptr->next_shed_tcb = g_list_head; - g_list_head->prev_shed_tcb = ptr; - ptr->prev_shed_tcb = g_list_head->prev_shed_tcb; - g_list_head->prev_shed_tcb->next_shed_tcb = ptr; + ptr->next_ready_tcb = g_ready_list_head; + g_ready_list_head->prev_ready_tcb = ptr; + ptr->prev_ready_tcb = g_ready_list_head->prev_ready_tcb; + g_ready_list_head->prev_ready_tcb->next_ready_tcb = ptr; } else { return error_os_task_overfow; } - } -#else - /* Dynamic Task Declaration */ -#endif /* Fill tcb details */ memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; @@ -67,18 +80,76 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_del_task (const char * name) +cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { cc_shed_tcb_t * ptr = g_curr_task; + if (cc_os_task != NULL) + { + ptr = cc_os_task; + } + /* Code to handle first node */ + if (ptr == g_ready_list_head) + { + if (ptr->next_ready_tcb != g_ready_list_head) + { + /* code for more than one node */ + g_ready_list_head = ptr->next_ready_tcb; + } + else + { + /* code for only one node */ + memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_ready_list_head = &(g_cc_os_tcb_list[0]); + return success; + } -#if !CC_DYNAMIC + } + ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; + ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; +#if CC_DYNAMIC == 0 + /* Static task Reset */ + ptr->task_status = cc_shed_task_terminated; + ptr->next_ready_tcb = NULL; + ptr->prev_ready_tcb = NULL; +#else + /* Dynamic Task Deletion */ + /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running */ +#endif + + return success; +} + +cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) +{ + cc_shed_tcb_t * ptr = g_curr_task; + if (cc_os_task != NULL) + { + ptr = cc_os_task; + } + ptr->task_status = cc_shed_task_wait; + + return success; +} + +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) +{ + ASSERT_IF_FALSE(cc_os_task != NULL); + + cc_os_task->task_status = cc_shed_task_ready; + + return success; +} + +cc_os_err_t cc_os_del_task_by_name (const char * name) +{ + cc_shed_tcb_t * ptr = g_curr_task; int name_found = 0; if (name != NULL) { /* code to delete node equal to name */ - ptr = g_list_head; - while(ptr->next_shed_tcb != g_list_head) + ptr = g_ready_list_head; + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -91,45 +162,52 @@ cc_os_err_t cc_os_del_task (const char * name) return error_func_inval_arg; } } - + else + { + ptr = g_curr_task; + } /* Code to handle first node */ - if (ptr == g_list_head) + if (ptr == g_ready_list_head) { - if (ptr->next_shed_tcb != g_list_head) + if (ptr->next_ready_tcb != g_ready_list_head) { /* code for more than one node */ - g_list_head = ptr->next_shed_tcb; + g_ready_list_head = ptr->next_ready_tcb; } else { /* code for only one node */ - memset(g_list_head, 0, sizeof(cc_shed_tcb_t)); - g_list_head = &(g_cc_os_tcb_list[0]); + memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_ready_list_head = &(g_cc_os_tcb_list[0]); return success; } } + ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; + ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; + +#if CC_DYNAMIC == 0 + /* Static task Reset */ + ptr->task_status = cc_shed_task_terminated; + ptr->next_ready_tcb = NULL; + ptr->prev_ready_tcb = NULL; #else /* Dynamic Task Deletion */ + /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ #endif - ptr->next_shed_tcb->prev_shed_tcb = ptr->prev_shed_tcb; - ptr->prev_shed_tcb->next_shed_tcb = ptr->next_shed_tcb; - ptr->task_status = cc_shed_task_terminated; - ptr->next_shed_tcb = NULL; - ptr->prev_shed_tcb = NULL; return success; } -cc_os_err_t cc_os_pause_task (const char * name) +cc_os_err_t cc_os_pause_task_by_name (const char * name) { cc_shed_tcb_t * ptr = g_curr_task; int name_found = 0; if (name != NULL) { /* code to pause node equal to name */ - ptr = g_list_head; - while(ptr->next_shed_tcb != g_list_head) + ptr = g_ready_list_head; + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -148,15 +226,15 @@ cc_os_err_t cc_os_pause_task (const char * name) return success; } -cc_os_err_t cc_os_resume_task (const char * name) +cc_os_err_t cc_os_resume_task_by_name (const char * name) { ASSERT_IF_FALSE(name != NULL); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; int name_found = 0; /* code to resume node equal to name */ - while(ptr->next_shed_tcb != g_list_head) + while(ptr->next_ready_tcb != g_ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -178,7 +256,7 @@ cc_os_err_t cc_os_wait_task (const size_t ticks) { ASSERT_IF_FALSE(ticks > 0); - cc_shed_tcb_t * ptr = g_list_head; + cc_shed_tcb_t * ptr = g_ready_list_head; ptr->task_delay_ticks = ticks; ptr->task_status = cc_shed_task_wait; diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c new file mode 100644 index 00000000..b48838e2 --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -0,0 +1,10 @@ +#include + +void * cc_os_malloc(size_t size) +{ + +} +void * cc_os_free(void *addr) +{ + +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 9dc1247b..5a15b2fd 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,11 +1,16 @@ #include -#include +#include #if !ccosconfig_CC_OS_USE_DYNAMIC cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; + + cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_wait_list_head = NULL; #else - cc_shed_tcb_t *g_cc_os_tcb_list; -#endif + cc_shed_tcb_t * g_cc_os_tcb_list = NULL; -cc_shed_tcb_t * g_list_head = &(g_cc_os_tcb_list[0]); -cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); + cc_shed_tcb_t * g_ready_list_head = NULL; + cc_shed_tcb_t * g_curr_task = NULL; + cc_shed_tcb_t * g_wait_list_head = NULL; +#endif From d37d707f3f62f1dca209e0e9234802d8bfd92545 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Thu, 22 Dec 2022 17:44:58 +0530 Subject: [PATCH 16/38] + scheduler basic prototype. + Code enhancements --- .vscode/c_cpp_properties.json | 7 ++- src/include/visor/terravisor/cc_os/cc_os.h | 14 +++-- .../visor/terravisor/cc_os/cc_os_config.h | 8 +++ .../visor/terravisor/cc_os/cc_os_shed.h | 25 +++++++- src/visor/terravisor/services/kernel/cc_os.c | 60 +++++++++++++++---- .../terravisor/services/kernel/cc_os_heap.c | 8 +-- .../terravisor/services/kernel/cc_os_idle.c | 9 +++ .../terravisor/services/kernel/cc_os_shed.c | 28 ++++++--- 8 files changed, 125 insertions(+), 34 deletions(-) create mode 100644 src/visor/terravisor/services/kernel/cc_os_idle.c diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d72ce463..496ed4ce 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,10 +6,11 @@ "${workspaceFolder}/src/**" ], "intelliSenseMode": "${default}", - "compilerPath": "", + "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", - "cppStandard": "gnu++14" + "cppStandard": "gnu++14", + "configurationProvider": "ms-vscode.makefile-tools" } ], "version": 4 -} +} \ No newline at end of file diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index d5d7611b..6fc540c2 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -13,6 +13,8 @@ #include "status.h" #include "stdint.h" +#include +#include #define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC @@ -31,9 +33,10 @@ typedef struct cc_os_task { task_fn task_fn; c_char * name; - size_t priority; //>> For waited tasks + size_t priority; ///>> For waited tasks size_t * stack_ptr; size_t stack_len; + cc_shed_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; /** @@ -43,16 +46,15 @@ typedef struct cc_os_task * @note DO NOT use space in place of TASK_Name as it would result build errors. * */ -#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN){ \ +#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN) \ static size_t _NAME##_stack[STACK_LEN]; \ -static const cc_os_task_t _NAME##_task = { \ +static cc_os_task_t _NAME##_task = { \ .task_fn = _fn, \ .name = #_NAME, \ .priority = _PRI, \ .stack_ptr = _NAME##_stack, \ .stack_len = STACK_LEN \ - }; \ -} + } \ /** * @brief Function to get the instance using its name of already declared task. @@ -133,7 +135,7 @@ cc_os_err_t cc_os_resume_task_by_name(const char *name); * @param ticks Number of CC_OS Ticks * @return cc_os_err_t */ -cc_os_err_t cc_os_wait_task(const size_t ticks); +cc_os_err_t cc_os_task_wait(const size_t ticks); /** * @brief A Function to invoke the kernel diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index c82d92ba..6c20eafa 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -11,3 +11,11 @@ #ifndef ccosconfig_CC_OS_TASK_NAME_LEN #define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif + +#ifndef ccosconfig_CC_OS_TASK_PRIORITY +#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#endif + +#ifndef ccosconfig_CC_OS_TASK_STACK_LEN +#define ccosconfig_CC_OS_TASK_STACK_LEN 255 +#endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h index 66360975..a709a8c7 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_shed.h +++ b/src/include/visor/terravisor/cc_os/cc_os_shed.h @@ -13,13 +13,15 @@ #include "stdint.h" typedef struct cc_shed_tcb cc_shed_tcb_t; +typedef struct cc_shed cc_sched_t; typedef enum { - cc_shed_task_terminated, ///> Initial State + cc_shed_task_terminated = 0x00, ///> Initial State cc_shed_task_ready, ///> Task Ready to despatch cc_shed_task_running, ///> Task currently running cc_shed_task_wait, ///> Task in wait state + cc_shed_task_status_max = 0xff, ///> Do Nt Use } cc_shed_task_status_t; struct cc_shed_tcb @@ -32,3 +34,24 @@ struct cc_shed_tcb cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer cc_shed_task_status_t task_status; ///> Current state of the task }; + +/** + * @brief Prototype of scheduler algorithm function +*/ +typedef void (* algo_fn)(cc_shed_tcb_t * cc_os_tcb_list); + +typedef enum +{ + cc_shed_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm + cc_shed_algo_priority_driven, ///> Priority driven Scheduling + cc_shed_algo_max = 0xff +}cc_shed_algo_t; + +typedef struct cc_shed +{ + cc_shed_algo_t cc_selected_algo; ///> Selected Algorithm ID + algo_fn algo_function; ///> Pointer to algorithm function +}cc_shed_t; + + +void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index b2deec90..31736714 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -12,8 +12,11 @@ extern cc_shed_tcb_t g_cc_os_tcb_list[]; #else extern cc_shed_tcb_t *g_cc_os_tcb_list; #endif +extern cc_shed_tcb_t * g_task_max_prio; extern cc_shed_tcb_t * g_curr_task; +extern void CC_OS_IDLE_TASK(void * args); + cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); @@ -76,6 +79,19 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; + if (g_task_max_prio == NULL) + { + g_task_max_prio = ptr; + } + else + { + if(g_task_max_prio->priority < ptr->priority) + { + g_task_max_prio = ptr; + } + } + + cc_os_task->task_tcb_ptr = ptr; ptr->task_status = cc_shed_task_ready; return success; } @@ -83,9 +99,12 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { cc_shed_tcb_t * ptr = g_curr_task; + + ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); + if (cc_os_task != NULL) { - ptr = cc_os_task; + ptr = cc_os_task->task_tcb_ptr; } /* Code to handle first node */ if (ptr == g_ready_list_head) @@ -99,23 +118,33 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { /* code for only one node */ memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); + g_task_max_prio = NULL; +#if CC_DYNAMIC == 0 g_ready_list_head = &(g_cc_os_tcb_list[0]); +#else + g_ready_list_head = NULL; +#endif return success; } } + ptr->task_status = cc_shed_task_terminated; + +/** TODO: By the scheduler for safe termination of current task ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; #if CC_DYNAMIC == 0 - /* Static task Reset */ - ptr->task_status = cc_shed_task_terminated; ptr->next_ready_tcb = NULL; ptr->prev_ready_tcb = NULL; #else - /* Dynamic Task Deletion */ - /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running */ + free(ptr); #endif +*/ + if (ptr == g_curr_task) + { + /* Yeild */ + } return success; } @@ -125,7 +154,7 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) cc_shed_tcb_t * ptr = g_curr_task; if (cc_os_task != NULL) { - ptr = cc_os_task; + ptr = cc_os_task->task_tcb_ptr; } ptr->task_status = cc_shed_task_wait; @@ -136,7 +165,7 @@ cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); - cc_os_task->task_status = cc_shed_task_ready; + cc_os_task->task_tcb_ptr->task_status = cc_shed_task_ready; return success; } @@ -252,7 +281,7 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) return success; } -cc_os_err_t cc_os_wait_task (const size_t ticks) +cc_os_err_t cc_os_task_wait (const size_t ticks) { ASSERT_IF_FALSE(ticks > 0); @@ -266,13 +295,20 @@ cc_os_err_t cc_os_wait_task (const size_t ticks) return success; } + cc_os_err_t cc_os_run (void) { /* OS Init code */ - while (1) - { - /* code to process OS */ - } + /* Initialise scheduler */ + + /* Initialise IDLE Task */ + CC_TASK_DEF( + cc_os_idle, /* Name of the instance */ + CC_OS_IDLE_TASK, /* Function pointer */ + ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ + ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ + ); + cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); return success; } diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index b48838e2..5d13112e 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -1,10 +1,10 @@ #include -void * cc_os_malloc(size_t size) +void * cc_os_malloc(size_t size _UNUSED) { - + return NULL; } -void * cc_os_free(void *addr) +void * cc_os_free(void *addr _UNUSED ) { - + return NULL; } diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c new file mode 100644 index 00000000..fbf3443a --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -0,0 +1,9 @@ +#include + +void CC_OS_IDLE_TASK(void * args _UNUSED) +{ + while (1) + { + cc_os_task_wait(1); + } +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c index 5a15b2fd..9d9ffe71 100644 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ b/src/visor/terravisor/services/kernel/cc_os_shed.c @@ -1,16 +1,28 @@ +#include #include #include #if !ccosconfig_CC_OS_USE_DYNAMIC - cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; - cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); - cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); - cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); +cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); #else - cc_shed_tcb_t * g_cc_os_tcb_list = NULL; +cc_shed_tcb_t * g_cc_os_tcb_list = NULL; - cc_shed_tcb_t * g_ready_list_head = NULL; - cc_shed_tcb_t * g_curr_task = NULL; - cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_ready_list_head = NULL; +cc_shed_tcb_t * g_curr_task = NULL; #endif +cc_shed_tcb_t * g_wait_list_head = NULL; +cc_shed_tcb_t * g_task_max_prio = NULL; + +cc_shed_t g_cc_shed = { + .cc_selected_algo = cc_shed_algo_round_robin, + .algo_function = cc_shed_algo_round_robin_fn +}; + +void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list) +{ + (void)(cc_os_tcb_list); + return ; +} From ec13ac4c7d3a1484ea14265423f4a7e29a8461ba Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sat, 24 Dec 2022 17:24:46 +0530 Subject: [PATCH 17/38] Algo updates + Round Robin Algo + IDLE Task Heap Garbage collection + CC_OS code enhancements --- src/include/visor/terravisor/cc_os/cc_os.h | 34 +-- .../visor/terravisor/cc_os/cc_os_config.h | 42 +++- .../visor/terravisor/cc_os/cc_os_sched.h | 69 +++++++ .../visor/terravisor/cc_os/cc_os_shed.h | 57 ------ src/visor/terravisor/services/kernel/cc_os.c | 193 +++++++++++------- .../terravisor/services/kernel/cc_os_heap.c | 28 +++ .../terravisor/services/kernel/cc_os_idle.c | 41 +++- .../terravisor/services/kernel/cc_os_sched.c | 140 +++++++++++++ .../terravisor/services/kernel/cc_os_shed.c | 28 --- 9 files changed, 455 insertions(+), 177 deletions(-) create mode 100644 src/include/visor/terravisor/cc_os/cc_os_sched.h delete mode 100644 src/include/visor/terravisor/cc_os/cc_os_shed.h create mode 100644 src/visor/terravisor/services/kernel/cc_os_sched.c delete mode 100644 src/visor/terravisor/services/kernel/cc_os_shed.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 6fc540c2..b0cf3f07 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -12,9 +12,10 @@ #define __CC_OS__ #include "status.h" +#include "stdlib.h" #include "stdint.h" #include -#include +#include #define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC @@ -32,11 +33,12 @@ typedef const char c_char; typedef struct cc_os_task { task_fn task_fn; + void * args; ///>> Task Args ptr c_char * name; size_t priority; ///>> For waited tasks size_t * stack_ptr; size_t stack_len; - cc_shed_tcb_t * task_tcb_ptr; ///>> For internal use only + cc_sched_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; /** @@ -46,15 +48,16 @@ typedef struct cc_os_task * @note DO NOT use space in place of TASK_Name as it would result build errors. * */ -#define CC_TASK_DEF(_NAME, _fn, _PRI, STACK_LEN) \ -static size_t _NAME##_stack[STACK_LEN]; \ -static cc_os_task_t _NAME##_task = { \ - .task_fn = _fn, \ - .name = #_NAME, \ - .priority = _PRI, \ - .stack_ptr = _NAME##_stack, \ - .stack_len = STACK_LEN \ - } \ +#define CC_TASK_DEF(_NAME, _fn, _args, _PRI, STACK_LEN) \ +static size_t _NAME##_stack[STACK_LEN]; \ +static cc_os_task_t _NAME##_task = { \ + .args = _args, \ + .task_fn = _fn, \ + .name = #_NAME, \ + .priority = _PRI, \ + .stack_ptr = _NAME##_stack, \ + .stack_len = STACK_LEN \ + } \ /** * @brief Function to get the instance using its name of already declared task. @@ -130,7 +133,7 @@ cc_os_err_t cc_os_pause_task_by_name(const char *name); cc_os_err_t cc_os_resume_task_by_name(const char *name); /** - * @brief A Function to put the task to a waiting state and yield + * @brief A Function to put the current task to a waiting state and yield * * @param ticks Number of CC_OS Ticks * @return cc_os_err_t @@ -144,4 +147,11 @@ cc_os_err_t cc_os_task_wait(const size_t ticks); */ cc_os_err_t cc_os_run(void); +/** + * @brief A function to set CC OS scheduler algorithm + * + * @return cc_os_err_t + */ +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); + #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index 6c20eafa..a0a92943 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -1,21 +1,53 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_config.h + * Description : CC OS Kernel configurations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #pragma once +/** + * @brief Shall CC_OS use dynamic resource allocation + * @note Possible values : 1/0 + */ #ifndef ccosconfig_CC_OS_USE_DYNAMIC -#define ccosconfig_CC_OS_USE_DYNAMIC 0 +#define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif +/** + * @brief Maximum number of threads allowed in static resource allocation + * @note The number of allowed threads are dependent of available RAM space. + * User may have to keep it optimised so as to use as low BSS section + * to be used as possible for the application being created. + */ #ifndef ccosconfig_CC_OS_MAX_THREAD -#define ccosconfig_CC_OS_MAX_THREAD 10 +#define ccosconfig_CC_OS_MAX_THREAD 10 #endif +/** + * @brief Max number of characters allowed to be used for task name + */ #ifndef ccosconfig_CC_OS_TASK_NAME_LEN -#define ccosconfig_CC_OS_TASK_NAME_LEN 16 +#define ccosconfig_CC_OS_TASK_NAME_LEN 16 #endif +/** + * @brief Task priority of IDLE task + * @note Possible values : 1 -> 255 + */ #ifndef ccosconfig_CC_OS_TASK_PRIORITY -#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#define ccosconfig_CC_OS_TASK_PRIORITY 1 #endif +/** + * @brief Stack size used by IDLE task + * @note The stack size is either allocated statically or dynamically as + * per the setting of ccosconfig_CC_OS_USE_DYNAMIC + */ #ifndef ccosconfig_CC_OS_TASK_STACK_LEN -#define ccosconfig_CC_OS_TASK_STACK_LEN 255 +#define ccosconfig_CC_OS_TASK_STACK_LEN 255 #endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/cc_os_sched.h new file mode 100644 index 00000000..b94bb30e --- /dev/null +++ b/src/include/visor/terravisor/cc_os/cc_os_sched.h @@ -0,0 +1,69 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_sched.h + * Description : CC OS Kernel scheduler declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#include "stdint.h" + +typedef struct cc_sched_tcb cc_sched_tcb_t; +typedef struct cc_sched cc_sched_t; + +typedef enum +{ + cc_sched_task_status_exit = 0x00, ///> Initial State + cc_sched_task_status_running, ///> Task currently running + cc_sched_task_status_ready, ///> Task Ready to despatch + cc_sched_task_status_wait, ///> Task in wait state + cc_sched_task_status_max = 0xff, ///> Do Nt Use +} cc_sched_task_status_t; + +typedef struct link +{ + cc_sched_tcb_t * prev; + cc_sched_tcb_t * next; +}link_t; + +struct cc_sched_tcb +{ + char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + size_t priority; ///> Priority of the task + void * stack_ptr; ///> Stack Pointer + size_t task_delay_ticks; ///> Time delay in ticks + link_t ready_link; ///> Ready Linked List Pointers + link_t wait_link; ///> Wait Linked List Pointers + cc_sched_task_status_t task_status; ///> Current state of the task +}; + +typedef struct cc_sched_ctrl +{ + cc_sched_tcb_t * ready_list_head; + cc_sched_tcb_t * curr_task; + cc_sched_tcb_t * wait_list_head; + cc_sched_tcb_t * task_max_prio; + cc_sched_t * selected_sched; +}cc_sched_ctrl_t; + +/** + * @brief Prototype of scheduler algorithm function + */ +typedef void (* algo_fn)(cc_sched_ctrl_t * sched_ctrl); + +typedef enum +{ + cc_sched_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm + cc_sched_algo_priority_driven, ///> Priority driven Scheduling + cc_sched_algo_max = 0xff +}cc_sched_algo_t; + +typedef struct cc_sched +{ + cc_sched_algo_t cc_selected_algo; ///> Selected Algorithm ID + algo_fn algo_function; ///> Pointer to algorithm function +}cc_sched_t; diff --git a/src/include/visor/terravisor/cc_os/cc_os_shed.h b/src/include/visor/terravisor/cc_os/cc_os_shed.h deleted file mode 100644 index a709a8c7..00000000 --- a/src/include/visor/terravisor/cc_os/cc_os_shed.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2022, Cyancore Team - * - * File Name : cc_shed.h - * Description : CC OS Kernel scheduler declaration - * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once - -#include "stdint.h" - -typedef struct cc_shed_tcb cc_shed_tcb_t; -typedef struct cc_shed cc_sched_t; - -typedef enum -{ - cc_shed_task_terminated = 0x00, ///> Initial State - cc_shed_task_ready, ///> Task Ready to despatch - cc_shed_task_running, ///> Task currently running - cc_shed_task_wait, ///> Task in wait state - cc_shed_task_status_max = 0xff, ///> Do Nt Use -} cc_shed_task_status_t; - -struct cc_shed_tcb -{ - char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task - size_t priority; ///> Priority of the task - void * stack_ptr; ///> Stack Pointer - size_t task_delay_ticks; ///> Time delay in ticks - cc_shed_tcb_t * prev_ready_tcb; ///> Previous task pointer - cc_shed_tcb_t * next_ready_tcb; ///> Next task pointer - cc_shed_task_status_t task_status; ///> Current state of the task -}; - -/** - * @brief Prototype of scheduler algorithm function -*/ -typedef void (* algo_fn)(cc_shed_tcb_t * cc_os_tcb_list); - -typedef enum -{ - cc_shed_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm - cc_shed_algo_priority_driven, ///> Priority driven Scheduling - cc_shed_algo_max = 0xff -}cc_shed_algo_t; - -typedef struct cc_shed -{ - cc_shed_algo_t cc_selected_algo; ///> Selected Algorithm ID - algo_fn algo_function; ///> Pointer to algorithm function -}cc_shed_t; - - -void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index 31736714..c57ccf93 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -1,22 +1,54 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include #include #include #include #include -#include - -extern cc_shed_tcb_t * g_ready_list_head; +#include +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ #if CC_DYNAMIC == 0 -extern cc_shed_tcb_t g_cc_os_tcb_list[]; +extern cc_sched_tcb_t g_cc_os_tcb_list[]; #else -extern cc_shed_tcb_t *g_cc_os_tcb_list; +extern cc_sched_tcb_t *g_cc_os_tcb_list; #endif -extern cc_shed_tcb_t * g_task_max_prio; -extern cc_shed_tcb_t * g_curr_task; +/***************************************************** + * GLOBAL EXTERNS FUNCTIONS + *****************************************************/ extern void CC_OS_IDLE_TASK(void * args); +/***************************************************** + * GLOBAL EXTERNS VARIABLES + *****************************************************/ +extern cc_sched_t *g_cc_sched; +extern cc_sched_t g_cc_sched_list []; +extern cc_sched_ctrl_t g_sched_ctrl; + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ +static void __cc_os_task_yeild() +{ + return; +} +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); @@ -25,22 +57,22 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); ASSERT_IF_FALSE(cc_os_task->stack_len != 0); - cc_shed_tcb_t * ptr = g_ready_list_head; + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; #if CC_DYNAMIC == 1 if (ptr == NULL) { /* First Dynamic task */ - ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr == NULL) { return error_os_task_overfow; } } #endif - if ((ptr->next_ready_tcb == NULL )&& (ptr->prev_ready_tcb == NULL)) + if ((ptr->ready_link.next == NULL )&& (ptr->ready_link.prev == NULL)) { - ptr->next_ready_tcb = ptr->prev_ready_tcb = g_ready_list_head; + ptr->ready_link.next = ptr->ready_link.prev = g_sched_ctrl.ready_list_head; } else @@ -50,25 +82,25 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ - if (g_cc_os_tcb_list[i].task_status == cc_shed_task_terminated) + if (g_cc_os_tcb_list[i].task_status == cc_sched_task_status_exit) { ptr = &(g_cc_os_tcb_list[i]); break; } } - if (ptr != g_ready_list_head) + if (ptr != g_sched_ctrl.ready_list_head) { #else /* Dynamic Task Declaration */ - ptr = (cc_shed_tcb_t *)cc_os_malloc(sizeof(cc_shed_tcb_t)); + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr != NULL) { #endif /* Insert node at last */ - ptr->next_ready_tcb = g_ready_list_head; - g_ready_list_head->prev_ready_tcb = ptr; - ptr->prev_ready_tcb = g_ready_list_head->prev_ready_tcb; - g_ready_list_head->prev_ready_tcb->next_ready_tcb = ptr; + ptr->ready_link.next = g_sched_ctrl.ready_list_head; + g_sched_ctrl.ready_list_head->ready_link.prev = ptr; + ptr->ready_link.prev = g_sched_ctrl.ready_list_head->ready_link.prev; + g_sched_ctrl.ready_list_head->ready_link.prev->ready_link.next = ptr; } else { @@ -79,26 +111,26 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; - if (g_task_max_prio == NULL) + if (g_sched_ctrl.task_max_prio == NULL) { - g_task_max_prio = ptr; + g_sched_ctrl.task_max_prio = ptr; } else { - if(g_task_max_prio->priority < ptr->priority) + if(g_sched_ctrl.task_max_prio->priority < ptr->priority) { - g_task_max_prio = ptr; + g_sched_ctrl.task_max_prio = ptr; } } cc_os_task->task_tcb_ptr = ptr; - ptr->task_status = cc_shed_task_ready; + ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); @@ -107,41 +139,30 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) ptr = cc_os_task->task_tcb_ptr; } /* Code to handle first node */ - if (ptr == g_ready_list_head) + if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->next_ready_tcb != g_ready_list_head) + if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) { /* code for more than one node */ - g_ready_list_head = ptr->next_ready_tcb; + g_sched_ctrl.ready_list_head = ptr->ready_link.next; } else { /* code for only one node */ - memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); - g_task_max_prio = NULL; + memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); + g_sched_ctrl.task_max_prio = NULL; #if CC_DYNAMIC == 0 - g_ready_list_head = &(g_cc_os_tcb_list[0]); + g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); #else - g_ready_list_head = NULL; + g_sched_ctrl.ready_list_head = NULL; #endif return success; } } - ptr->task_status = cc_shed_task_terminated; + ptr->task_status = cc_sched_task_status_exit; -/** TODO: By the scheduler for safe termination of current task - ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; - ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; - -#if CC_DYNAMIC == 0 - ptr->next_ready_tcb = NULL; - ptr->prev_ready_tcb = NULL; -#else - free(ptr); -#endif -*/ - if (ptr == g_curr_task) + if (ptr == g_sched_ctrl.curr_task) { /* Yeild */ } @@ -151,12 +172,12 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; if (cc_os_task != NULL) { ptr = cc_os_task->task_tcb_ptr; } - ptr->task_status = cc_shed_task_wait; + ptr->task_status = cc_sched_task_status_wait; return success; } @@ -165,20 +186,20 @@ cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { ASSERT_IF_FALSE(cc_os_task != NULL); - cc_os_task->task_tcb_ptr->task_status = cc_shed_task_ready; + cc_os_task->task_tcb_ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_del_task_by_name (const char * name) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; int name_found = 0; if (name != NULL) { /* code to delete node equal to name */ - ptr = g_ready_list_head; - while(ptr->next_ready_tcb != g_ready_list_head) + ptr = g_sched_ctrl.ready_list_head; + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -193,33 +214,33 @@ cc_os_err_t cc_os_del_task_by_name (const char * name) } else { - ptr = g_curr_task; + ptr = g_sched_ctrl.curr_task; } /* Code to handle first node */ - if (ptr == g_ready_list_head) + if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->next_ready_tcb != g_ready_list_head) + if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) { /* code for more than one node */ - g_ready_list_head = ptr->next_ready_tcb; + g_sched_ctrl.ready_list_head = ptr->ready_link.next; } else { /* code for only one node */ - memset(g_ready_list_head, 0, sizeof(cc_shed_tcb_t)); - g_ready_list_head = &(g_cc_os_tcb_list[0]); + memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); + g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); return success; } } - ptr->next_ready_tcb->prev_ready_tcb = ptr->prev_ready_tcb; - ptr->prev_ready_tcb->next_ready_tcb = ptr->next_ready_tcb; + ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; + ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; #if CC_DYNAMIC == 0 /* Static task Reset */ - ptr->task_status = cc_shed_task_terminated; - ptr->next_ready_tcb = NULL; - ptr->prev_ready_tcb = NULL; + ptr->task_status = cc_sched_task_status_exit; + ptr->ready_link.next = NULL; + ptr->ready_link.prev = NULL; #else /* Dynamic Task Deletion */ /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ @@ -230,13 +251,13 @@ cc_os_err_t cc_os_del_task_by_name (const char * name) cc_os_err_t cc_os_pause_task_by_name (const char * name) { - cc_shed_tcb_t * ptr = g_curr_task; + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; int name_found = 0; if (name != NULL) { /* code to pause node equal to name */ - ptr = g_ready_list_head; - while(ptr->next_ready_tcb != g_ready_list_head) + ptr = g_sched_ctrl.ready_list_head; + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -250,7 +271,7 @@ cc_os_err_t cc_os_pause_task_by_name (const char * name) } } - ptr->task_status = cc_shed_task_wait; + ptr->task_status = cc_sched_task_status_wait; return success; } @@ -259,11 +280,11 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) { ASSERT_IF_FALSE(name != NULL); - cc_shed_tcb_t * ptr = g_ready_list_head; + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; int name_found = 0; /* code to resume node equal to name */ - while(ptr->next_ready_tcb != g_ready_list_head) + while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) { if(strcmp(ptr->name, name) == 0) { @@ -276,25 +297,50 @@ cc_os_err_t cc_os_resume_task_by_name (const char * name) return error_func_inval_arg; } - ptr->task_status = cc_shed_task_ready; + ptr->task_status = cc_sched_task_status_ready; return success; } cc_os_err_t cc_os_task_wait (const size_t ticks) { - ASSERT_IF_FALSE(ticks > 0); + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - cc_shed_tcb_t * ptr = g_ready_list_head; + if (ticks > 0) + { + /* Put into wait state only if wait count is more than 0 */ + if (g_sched_ctrl.wait_list_head == NULL) + { + /* First Wait list node */ + g_sched_ctrl.wait_list_head = ptr; + ptr->wait_link.next = ptr; + ptr->wait_link.prev = ptr; + } + else + { + ptr->wait_link.next = g_sched_ctrl.wait_list_head; + ptr->wait_link.prev = g_sched_ctrl.wait_list_head->wait_link.prev; + g_sched_ctrl.wait_list_head->wait_link.prev->wait_link.next = ptr; + g_sched_ctrl.wait_list_head->wait_link.prev = ptr; + } - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_shed_task_wait; + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; + } - // Yield + __cc_os_task_yeild(); return success; } +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +{ + ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); + + g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); + + return success; +} cc_os_err_t cc_os_run (void) { @@ -305,6 +351,7 @@ cc_os_err_t cc_os_run (void) CC_TASK_DEF( cc_os_idle, /* Name of the instance */ CC_OS_IDLE_TASK, /* Function pointer */ + &g_sched_ctrl, /* Task Args*/ ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ ); diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index 5d13112e..45f37b25 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -1,5 +1,33 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS Kernel definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ + +/***************************************************** + * GLOBAL EXTERNS + *****************************************************/ + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ + +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ void * cc_os_malloc(size_t size _UNUSED) { return NULL; diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index fbf3443a..82ceff1b 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -1,9 +1,46 @@ #include -void CC_OS_IDLE_TASK(void * args _UNUSED) +/***************************************************** + * GLOBAL EXTERNS VARIABLES + *****************************************************/ +/** + * @brief This function cleans up the terminated task form the TCB list + * + * @param ptr[in] Pointer to the TCB being cleaned + * @return cc_sched_tcb_t * Pointer to the next TCB + */ +static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) { + cc_sched_tcb_t * next_ptr = ptr->ready_link.next; + if (ptr->ready_link.next->task_status == cc_sched_task_status_exit) + { + ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; + ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; + +#if ccosconfig_CC_OS_USE_DYNAMIC == 0 + ptr->ready_link.next = NULL; + ptr->ready_link.prev = NULL; +#else + cc_os_free(ptr); +#endif + } + + return next_ptr; +} + +void CC_OS_IDLE_TASK(void * args) +{ + cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; + static cc_sched_tcb_t * ptr = NULL; + ptr = g_sched_ctrl->ready_list_head; while (1) { - cc_os_task_wait(1); + /* Clean up task if terminated */ + ptr = __free_terminated_task(ptr); + + /* Power Save code */ + + /* Yield for nect available task */ + cc_os_task_wait(0); } } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c new file mode 100644 index 00000000..5d5d9a5b --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -0,0 +1,140 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_sched.c + * Description : CC OS Kernel scheduler definations + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ +#include +#include +#include + +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_SCHED_ALGO(_id, _fn) { \ + .cc_selected_algo = _id, \ + .algo_function = _fn} + +/***************************************************** + * STATIC FUNCTION DECLARATION + *****************************************************/ +static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl); +static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); +/***************************************************** + * GLOBAL DECLARATIONS + *****************************************************/ +#if !ccosconfig_CC_OS_USE_DYNAMIC +cc_sched_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; +#else +cc_sched_tcb_t * g_cc_os_tcb_list = NULL; +#endif + +cc_sched_t g_cc_sched_list [] = +{ + CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), + CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), +}; + +cc_sched_ctrl_t g_sched_ctrl = +{ +#if !ccosconfig_CC_OS_USE_DYNAMIC + .ready_list_head = &(g_cc_os_tcb_list[0]), + .curr_task = &(g_cc_os_tcb_list[0]), +#else + .ready_list_head = NULL, + .curr_task = NULL, +#endif + .wait_list_head = NULL, + .task_max_prio = NULL, + .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) +}; + + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ +static void __cc_sched_context_switch(cc_sched_tcb_t * next_task) +{ + next_task->task_status = cc_sched_task_status_running; +} + +static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) +{ + cc_sched_tcb_t * ptr; + if(sched_ctrl->wait_list_head != NULL) + { + ptr = sched_ctrl->wait_list_head; + /* Tasks present in wait list */ + while(1) + { + ptr->task_delay_ticks--; /* Tick caliberations required */ + if(ptr->task_delay_ticks == 0) + { + if (ptr == sched_ctrl->wait_list_head) + { + /* First in the list */ + sched_ctrl->wait_list_head = ptr->wait_link.next; + if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) + { + /* Last Wait task left */ + sched_ctrl->wait_list_head = NULL; + } + } + ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; + ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = NULL; + ptr->wait_link.next = NULL; + ptr->task_status = cc_sched_task_status_ready; + } + if (ptr->wait_link.next == sched_ctrl->wait_list_head) + { + break; + } + else + { + ptr = ptr->wait_link.next; + } + } + } +} + +/***************************************************** + * SCHEDULER ALGORITHMS + *****************************************************/ +static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) +{ + /* do waitlist adjustment */ + cc_sched_tcb_t * ptr = sched_ctrl->curr_task; + + __cc_sched_wait_list_adjustment(sched_ctrl); + + /* Context switch to next task */ + if (ptr->ready_link.next->task_status == cc_sched_task_status_ready) + { + __cc_sched_context_switch(ptr->ready_link.next); + } +} + +static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) +{ + /* do waitlist adjustment */ + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head; + + __cc_sched_wait_list_adjustment(sched_ctrl); + while (1) + { + if (ptr == sched_ctrl->ready_list_head) + { + break; + } + + } + +} diff --git a/src/visor/terravisor/services/kernel/cc_os_shed.c b/src/visor/terravisor/services/kernel/cc_os_shed.c deleted file mode 100644 index 9d9ffe71..00000000 --- a/src/visor/terravisor/services/kernel/cc_os_shed.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -#if !ccosconfig_CC_OS_USE_DYNAMIC -cc_shed_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; - -cc_shed_tcb_t * g_ready_list_head = &(g_cc_os_tcb_list[0]); -cc_shed_tcb_t * g_curr_task = &(g_cc_os_tcb_list[0]); -#else -cc_shed_tcb_t * g_cc_os_tcb_list = NULL; - -cc_shed_tcb_t * g_ready_list_head = NULL; -cc_shed_tcb_t * g_curr_task = NULL; -#endif -cc_shed_tcb_t * g_wait_list_head = NULL; -cc_shed_tcb_t * g_task_max_prio = NULL; - -cc_shed_t g_cc_shed = { - .cc_selected_algo = cc_shed_algo_round_robin, - .algo_function = cc_shed_algo_round_robin_fn -}; - -void cc_shed_algo_round_robin_fn(cc_shed_tcb_t * cc_os_tcb_list) -{ - (void)(cc_os_tcb_list); - return ; -} From 42025d4d348b1588208a6edcc3bbb405a97b0858 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 25 Dec 2022 17:47:58 +0530 Subject: [PATCH 18/38] Priority driven algorithm --- src/include/status.h | 1 + src/include/visor/terravisor/cc_os/cc_os.h | 72 +++-- .../visor/terravisor/cc_os/cc_os_config.h | 12 +- src/visor/terravisor/services/kernel/cc_os.c | 279 +++++++----------- .../terravisor/services/kernel/cc_os_heap.c | 3 +- .../terravisor/services/kernel/cc_os_idle.c | 24 +- .../terravisor/services/kernel/cc_os_sched.c | 163 ++++++++-- 7 files changed, 323 insertions(+), 231 deletions(-) diff --git a/src/include/status.h b/src/include/status.h index 62e06982..0264eb68 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -76,6 +76,7 @@ typedef enum status error_os = -0x0f00, error_os_task_overfow = -0x0f01, error_os_deadlock = -0x0f02, + error_os_invalid_op = -0x0f03, /* Mesg related error */ error_mesg = -0x1000, error_mesg_long = -0x1001, diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index b0cf3f07..0e231e4d 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -11,19 +11,33 @@ #ifndef __CC_OS__ #define __CC_OS__ +/***************************************************** + * INCLUDES + *****************************************************/ #include "status.h" #include "stdlib.h" #include "stdint.h" #include #include -#define CC_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_FALSE 0 +#define CC_OS_TRUE 1 +#define CC_OS_DELAY_MAX ((size_t) -1) -#define ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg +#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC +#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg + +/***************************************************** + * TYPEDEFS + *****************************************************/ typedef status_t cc_os_err_t; -typedef void (* task_fn)(void * args); +typedef void * os_args; +typedef void (* task_fn)(os_args args); typedef const char c_char; /** @@ -32,15 +46,18 @@ typedef const char c_char; */ typedef struct cc_os_task { - task_fn task_fn; - void * args; ///>> Task Args ptr - c_char * name; + task_fn task_fn; ///>> Task funcion + os_args args; ///>> Task Args ptr + c_char * name; ///>> String name of the task size_t priority; ///>> For waited tasks - size_t * stack_ptr; - size_t stack_len; + size_t * stack_ptr; ///>> Stack pointer of the task + size_t stack_len; ///>> Stack lengths of the task cc_sched_tcb_t * task_tcb_ptr; ///>> For internal use only }cc_os_task_t; +/***************************************************** + * MACROS + *****************************************************/ /** * @brief Function to declare a static task with a dedicated stack for the task * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); @@ -68,6 +85,9 @@ static cc_os_task_t _NAME##_task = { \ */ #define CC_GET_TASK_INST(_NAME) _NAME##_task +/***************************************************** + * USER FUNCTION DECLARATIONS + *****************************************************/ /** * @brief A Function to add a task to the scheduler * @@ -106,52 +126,50 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task); cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task); /** - * @brief A function to delete a task from the scheduler by its name + * @brief A Function to pause all the tasks except the current and the IDLE Task + * @note To resume all please use cc_os_resume_all_task() call * - * @param name Name of the task to be terminated; Pass NULL to point to current task * @return cc_os_err_t */ -cc_os_err_t cc_os_del_task_by_name(const char * name); - +cc_os_err_t cc_os_pause_all_task (void); /** - * @brief A Function to pause the task until call resume explicitly by its name + * @brief A Function to resume all the tasks * - * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_pause_task_by_name(const char *name); +cc_os_err_t cc_os_resume_all_task (void); /** + * @brief A function to set CC OS scheduler algorithm * - * @brief A Function to resume paused task by its name - * @note Calling this function for already non-waiting task has no effect. - * - * @param name * @return cc_os_err_t */ -cc_os_err_t cc_os_resume_task_by_name(const char *name); +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); /** * @brief A Function to put the current task to a waiting state and yield + * @note To just Yeild set ticks to 0 * * @param ticks Number of CC_OS Ticks - * @return cc_os_err_t + * @return None */ -cc_os_err_t cc_os_task_wait(const size_t ticks); +void cc_os_task_wait(const size_t ticks); /** - * @brief A Function to invoke the kernel + * @brief A Function to switch to next available task * - * @return cc_os_err_t + * @param ticks Number of CC_OS Ticks + * @return None */ -cc_os_err_t cc_os_run(void); +void cc_os_task_yield(); /** - * @brief A function to set CC OS scheduler algorithm + * @brief A Function to invoke the kernel * * @return cc_os_err_t */ -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); +void cc_os_run(void); + #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index a0a92943..729b34a6 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -18,6 +18,14 @@ #define ccosconfig_CC_OS_USE_DYNAMIC 0 #endif +/** + * @brief Heap size to be allocated to CC_OS + * @note Possible values : Depends on available RAM + */ +#ifndef ccosconfig_CC_OS_HEAP_SIZE +#define ccosconfig_CC_OS_HEAP_SIZE 1024 +#endif + /** * @brief Maximum number of threads allowed in static resource allocation * @note The number of allowed threads are dependent of available RAM space. @@ -39,8 +47,8 @@ * @brief Task priority of IDLE task * @note Possible values : 1 -> 255 */ -#ifndef ccosconfig_CC_OS_TASK_PRIORITY -#define ccosconfig_CC_OS_TASK_PRIORITY 1 +#ifndef ccosconfig_CC_OS_IDLE_TASK_PRIORITY +#define ccosconfig_CC_OS_IDLE_TASK_PRIORITY 0x01 #endif /** diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index c57ccf93..c2eb579f 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -14,23 +14,33 @@ #include #include #include +#include #include #include #include +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_PRIORITY_MAX 255 + /***************************************************** * GLOBAL DECLARATIONS *****************************************************/ -#if CC_DYNAMIC == 0 +#if CC_OS_DYNAMIC == CC_OS_FALSE extern cc_sched_tcb_t g_cc_os_tcb_list[]; #else extern cc_sched_tcb_t *g_cc_os_tcb_list; #endif /***************************************************** - * GLOBAL EXTERNS FUNCTIONS + * INTERNAL EXTERNS FUNCTIONS *****************************************************/ -extern void CC_OS_IDLE_TASK(void * args); +extern void _cc_os_idle_task_fn (void * args); +extern void _insert_after (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_before (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); +extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); /***************************************************** * GLOBAL EXTERNS VARIABLES @@ -42,30 +52,32 @@ extern cc_sched_ctrl_t g_sched_ctrl; /***************************************************** * STATIC FUNCTION DEFINATIONS *****************************************************/ -static void __cc_os_task_yeild() -{ - return; -} + /***************************************************** * USER FUNCTION DEFINATIONS *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { - ASSERT_IF_FALSE(cc_os_task != NULL); - ASSERT_IF_FALSE(cc_os_task->name != NULL); - ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); - ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); - ASSERT_IF_FALSE(cc_os_task->stack_len != 0); + CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->name != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_len != CC_OS_FALSE); + CC_OS_ASSERT_IF_FALSE(cc_os_task->priority >= ccosconfig_CC_OS_IDLE_TASK_PRIORITY); + CC_OS_ASSERT_IF_FALSE(cc_os_task->priority < CC_OS_PRIORITY_MAX); + + cc_os_pause_all_task(); cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; -#if CC_DYNAMIC == 1 +#if CC_OS_DYNAMIC == CC_OS_TRUE if (ptr == NULL) { /* First Dynamic task */ ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr == NULL) { + cc_os_resume_all_task(); return error_os_task_overfow; } } @@ -77,9 +89,9 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) else { -#if CC_DYNAMIC == 0 +#if CC_OS_DYNAMIC == CC_OS_FALSE /* Static Task Allocation */ - for (size_t i = 0; i < ccosconfig_CC_OS_MAX_THREAD; i++) + for (size_t i = CC_OS_FALSE; i < ccosconfig_CC_OS_MAX_THREAD; i++) { /* Get an available node from global tcb list */ if (g_cc_os_tcb_list[i].task_status == cc_sched_task_status_exit) @@ -96,43 +108,56 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) if (ptr != NULL) { #endif - /* Insert node at last */ - ptr->ready_link.next = g_sched_ctrl.ready_list_head; - g_sched_ctrl.ready_list_head->ready_link.prev = ptr; - ptr->ready_link.prev = g_sched_ctrl.ready_list_head->ready_link.prev; - g_sched_ctrl.ready_list_head->ready_link.prev->ready_link.next = ptr; + /* Fill tcb details */ + memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); + ptr->stack_ptr = cc_os_task->stack_ptr; + ptr->priority = cc_os_task->priority; } else { + cc_os_resume_all_task(); return error_os_task_overfow; } } - /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); - ptr->stack_ptr = cc_os_task->stack_ptr; - ptr->priority = cc_os_task->priority; + /* Insert Tasks in assending order of its priority */ if (g_sched_ctrl.task_max_prio == NULL) { g_sched_ctrl.task_max_prio = ptr; } + else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) + { + _insert_after(g_sched_ctrl.task_max_prio, ptr, CC_OS_FALSE); + g_sched_ctrl.task_max_prio = ptr; + } else { - if(g_sched_ctrl.task_max_prio->priority < ptr->priority) + cc_sched_tcb_t * comp_ptr = g_sched_ctrl.task_max_prio->ready_link.next; + while (1) { - g_sched_ctrl.task_max_prio = ptr; + if (comp_ptr->priority <= ptr->priority) + { + _insert_after(comp_ptr, ptr, CC_OS_FALSE); + break; + } + else + { + comp_ptr = comp_ptr->ready_link.next; + } } } cc_os_task->task_tcb_ptr = ptr; ptr->task_status = cc_sched_task_status_ready; + + cc_os_resume_all_task(); return success; } cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != _cc_os_idle_task_fn); - ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_IDLE_TASK); + cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; if (cc_os_task != NULL) { @@ -141,30 +166,14 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) /* Code to handle first node */ if (ptr == g_sched_ctrl.ready_list_head) { - if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - /* code for more than one node */ - g_sched_ctrl.ready_list_head = ptr->ready_link.next; - } - else - { - /* code for only one node */ - memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); - g_sched_ctrl.task_max_prio = NULL; -#if CC_DYNAMIC == 0 - g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); -#else - g_sched_ctrl.ready_list_head = NULL; -#endif - return success; - } - + /* IDLE Task can not be deleted */ + return error_os_invalid_op; } ptr->task_status = cc_sched_task_status_exit; if (ptr == g_sched_ctrl.curr_task) { - /* Yeild */ + cc_os_task_yield(); /* Yeild */ } return success; @@ -177,185 +186,105 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { ptr = cc_os_task->task_tcb_ptr; } - ptr->task_status = cc_sched_task_status_wait; - return success; -} - -cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) -{ - ASSERT_IF_FALSE(cc_os_task != NULL); - - cc_os_task->task_tcb_ptr->task_status = cc_sched_task_status_ready; + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, CC_OS_DELAY_MAX); return success; } -cc_os_err_t cc_os_del_task_by_name (const char * name) +cc_os_err_t cc_os_pause_all_task () { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - int name_found = 0; - if (name != NULL) - { - /* code to delete node equal to name */ - ptr = g_sched_ctrl.ready_list_head; - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) - { - return error_func_inval_arg; - } - } - else - { - ptr = g_sched_ctrl.curr_task; - } - /* Code to handle first node */ - if (ptr == g_sched_ctrl.ready_list_head) + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; + + while (ptr != g_sched_ctrl.ready_list_head) { - if (ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - /* code for more than one node */ - g_sched_ctrl.ready_list_head = ptr->ready_link.next; - } - else + if (ptr == g_sched_ctrl.curr_task) { - /* code for only one node */ - memset(g_sched_ctrl.ready_list_head, 0, sizeof(cc_sched_tcb_t)); - g_sched_ctrl.ready_list_head = &(g_cc_os_tcb_list[0]); - return success; + continue; } + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, CC_OS_DELAY_MAX); + ptr = ptr->ready_link.next; } - ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; - ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; - -#if CC_DYNAMIC == 0 - /* Static task Reset */ - ptr->task_status = cc_sched_task_status_exit; - ptr->ready_link.next = NULL; - ptr->ready_link.prev = NULL; -#else - /* Dynamic Task Deletion */ - /* TODO: Need to push to Terminated Queue so that the scheduler can free the task when not running*/ -#endif return success; } -cc_os_err_t cc_os_pause_task_by_name (const char * name) +cc_os_err_t cc_os_resume_all_task () { - cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - int name_found = 0; - if (name != NULL) + cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; + + while (ptr != g_sched_ctrl.ready_list_head) { - /* code to pause node equal to name */ - ptr = g_sched_ctrl.ready_list_head; - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) + if (ptr == g_sched_ctrl.curr_task) { - return error_func_inval_arg; + continue; } - } - ptr->task_status = cc_sched_task_status_wait; + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + ptr = ptr->ready_link.next; + } return success; } -cc_os_err_t cc_os_resume_task_by_name (const char * name) +cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { - ASSERT_IF_FALSE(name != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); - cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; - int name_found = 0; + cc_sched_tcb_t * ptr = cc_os_task->task_tcb_ptr; - /* code to resume node equal to name */ - while(ptr->ready_link.next != g_sched_ctrl.ready_list_head) - { - if(strcmp(ptr->name, name) == 0) - { - name_found = 1; - break; - } - } - if (!name_found) - { - return error_func_inval_arg; - } + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - ptr->task_status = cc_sched_task_status_ready; + return success; +} + +cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +{ + CC_OS_ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); + + g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); return success; } -cc_os_err_t cc_os_task_wait (const size_t ticks) +void cc_os_task_wait (const size_t ticks) { cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (ticks > 0) + if (ticks > CC_OS_FALSE) { - /* Put into wait state only if wait count is more than 0 */ - if (g_sched_ctrl.wait_list_head == NULL) - { - /* First Wait list node */ - g_sched_ctrl.wait_list_head = ptr; - ptr->wait_link.next = ptr; - ptr->wait_link.prev = ptr; - } - else - { - ptr->wait_link.next = g_sched_ctrl.wait_list_head; - ptr->wait_link.prev = g_sched_ctrl.wait_list_head->wait_link.prev; - g_sched_ctrl.wait_list_head->wait_link.prev->wait_link.next = ptr; - g_sched_ctrl.wait_list_head->wait_link.prev = ptr; - } - - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_sched_task_status_wait; + _cc_sched_send_to_wait(&g_sched_ctrl, ptr, ticks); } - __cc_os_task_yeild(); - - return success; + cc_os_task_yield(); } -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +void cc_os_task_yield() { - ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); - - g_sched_ctrl.selected_sched = &(g_cc_sched_list[sched_algo]); - - return success; + TODO("cc_os_task_yield"); + return; } -cc_os_err_t cc_os_run (void) +void cc_os_run (void) { /* OS Init code */ - /* Initialise scheduler */ /* Initialise IDLE Task */ CC_TASK_DEF( cc_os_idle, /* Name of the instance */ - CC_OS_IDLE_TASK, /* Function pointer */ + _cc_os_idle_task_fn, /* Function pointer */ &g_sched_ctrl, /* Task Args*/ - ccosconfig_CC_OS_TASK_PRIORITY, /* Task Priority */ + ccosconfig_CC_OS_IDLE_TASK_PRIORITY, /* Task Priority */ ccosconfig_CC_OS_TASK_STACK_LEN /* Stack Length of IDLE Task */ - ); + ); cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); - return success; + /* Initialise scheduler */ + cc_os_task_yield(); /* Yeild */ + while (1) + { + /* Code shall not reach here */ + arch_wfi(); + } } diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index 45f37b25..f75a30d3 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -14,8 +14,9 @@ #include /***************************************************** - * GLOBAL DECLARATIONS + * GLOBAL/STATIC VARIABLE DECLARATIONS *****************************************************/ +// static unsigned char g_cc_os_heap[ccosconfig_CC_OS_HEAP_SIZE]; /***************************************************** * GLOBAL EXTERNS diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 82ceff1b..0d413f52 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -1,7 +1,20 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_idle.c + * Description : CC OS IDLE Task definitions + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ #include /***************************************************** - * GLOBAL EXTERNS VARIABLES + * STATIC FUNCTION DECLARATION *****************************************************/ /** * @brief This function cleans up the terminated task form the TCB list @@ -28,7 +41,10 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) return next_ptr; } -void CC_OS_IDLE_TASK(void * args) +/***************************************************** + * USER FUNCTION DEFINATION + *****************************************************/ +void _cc_os_idle_task_fn(os_args args) { cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; static cc_sched_tcb_t * ptr = NULL; @@ -40,7 +56,7 @@ void CC_OS_IDLE_TASK(void * args) /* Power Save code */ - /* Yield for nect available task */ - cc_os_task_wait(0); + /* Yield for next available task */ + cc_os_task_yield(); } } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index 5d5d9a5b..c46dd285 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -27,6 +27,7 @@ *****************************************************/ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl); static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); + /***************************************************** * GLOBAL DECLARATIONS *****************************************************/ @@ -38,8 +39,8 @@ cc_sched_tcb_t * g_cc_os_tcb_list = NULL; cc_sched_t g_cc_sched_list [] = { - CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), - CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), + CC_SCHED_ALGO(cc_sched_algo_round_robin, __cc_sched_algo_round_robin_fn), + CC_SCHED_ALGO(cc_sched_algo_priority_driven, __cc_sched_algo_priority_driven_fn), }; cc_sched_ctrl_t g_sched_ctrl = @@ -56,6 +57,123 @@ cc_sched_ctrl_t g_sched_ctrl = .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) }; +/***************************************************** + * INTERNAL USED FUNCTIONS (NON-STATIC) + *****************************************************/ +void _insert_after(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +{ + switch (link_type) + { + case 0: + /* Ready Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->ready_link.next = new_node; + new_node->ready_link.prev = new_node; + } + else + { + new_node->ready_link.next = ptr->ready_link.next; + new_node->ready_link.prev = ptr; + new_node->ready_link.next->ready_link.prev = new_node; + ptr->ready_link.next = new_node; + } + break; + case 1: + /* Wait Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->wait_link.next = new_node; + new_node->wait_link.prev = new_node; + } + { + new_node->wait_link.next = ptr->wait_link.next; + new_node->wait_link.prev = ptr; + new_node->wait_link.next->wait_link.prev = new_node; + ptr->wait_link.next = new_node; + } + break; + default: + return; + } +} +void _insert_before(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +{ + switch (link_type) + { + case 0: + /* Ready Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->ready_link.next = new_node; + new_node->ready_link.prev = new_node; + } + { + new_node->ready_link.next = ptr; + new_node->ready_link.prev = ptr->ready_link.prev; + ptr->ready_link.prev = new_node; + new_node->ready_link.prev->ready_link.next = new_node; + } + break; + case 1: + /* Wait Link */ + if (ptr == NULL) + { + ptr = new_node; + new_node->wait_link.next = new_node; + new_node->wait_link.prev = new_node; + } + { + new_node->wait_link.next = ptr; + new_node->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = new_node; + new_node->wait_link.prev->wait_link.next = new_node; + } + break; + default: + return; + } +} + +void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks) +{ + if (ptr->task_status == cc_sched_task_status_wait) + { + return; + } + _insert_before(sched_ctrl->wait_list_head, ptr, 1); + + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; +} + +void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr) +{ + if (ptr->task_status == cc_sched_task_status_ready) + { + return; + } + + if (ptr == sched_ctrl->wait_list_head) + { + /* First in the list */ + sched_ctrl->wait_list_head = ptr->wait_link.next; + if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) + { + /* Last Wait task left */ + sched_ctrl->wait_list_head = NULL; + } + } + ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; + ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; + ptr->wait_link.prev = NULL; + ptr->wait_link.next = NULL; + ptr->task_delay_ticks = 0; + ptr->task_status = cc_sched_task_status_ready; +} /***************************************************** * STATIC FUNCTION DEFINATIONS @@ -77,21 +195,7 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) ptr->task_delay_ticks--; /* Tick caliberations required */ if(ptr->task_delay_ticks == 0) { - if (ptr == sched_ctrl->wait_list_head) - { - /* First in the list */ - sched_ctrl->wait_list_head = ptr->wait_link.next; - if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) - { - /* Last Wait task left */ - sched_ctrl->wait_list_head = NULL; - } - } - ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; - ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = NULL; - ptr->wait_link.next = NULL; - ptr->task_status = cc_sched_task_status_ready; + _cc_sched_send_to_resume(sched_ctrl, ptr); } if (ptr->wait_link.next == sched_ctrl->wait_list_head) { @@ -111,12 +215,17 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->curr_task; + cc_sched_tcb_t * ptr = sched_ctrl->curr_task->ready_link.next; __cc_sched_wait_list_adjustment(sched_ctrl); + if (ptr == sched_ctrl->ready_list_head) + { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + } /* Context switch to next task */ - if (ptr->ready_link.next->task_status == cc_sched_task_status_ready) + if (ptr->task_status == cc_sched_task_status_ready) { __cc_sched_context_switch(ptr->ready_link.next); } @@ -125,16 +234,26 @@ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head; + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; __cc_sched_wait_list_adjustment(sched_ctrl); while (1) { + if (ptr->task_status == cc_sched_task_status_ready) + { + break; + } + else + { + /* Iterate Backwards */ + ptr = ptr->wait_link.prev; + } if (ptr == sched_ctrl->ready_list_head) { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); break; } - } - + __cc_sched_context_switch(ptr); } From 9c909cb42a2076590a357bc132f605663a241daa Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 25 Dec 2022 18:09:14 +0530 Subject: [PATCH 19/38] Sonar Cloud Bug fixes --- .vscode/c_cpp_properties.json | 5 +- .../visor/terravisor/cc_os/cc_os_sched.h | 10 +- .../visor/terravisor/cc_os/utils/cc_os_heap.h | 2 +- src/visor/terravisor/services/kernel/cc_os.c | 42 ++++--- .../terravisor/services/kernel/cc_os_heap.c | 3 +- .../terravisor/services/kernel/cc_os_sched.c | 108 +++++++++--------- 6 files changed, 85 insertions(+), 85 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 496ed4ce..e1140042 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,9 +8,8 @@ "intelliSenseMode": "${default}", "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", - "cppStandard": "gnu++14", - "configurationProvider": "ms-vscode.makefile-tools" + "cppStandard": "gnu++14" } ], "version": 4 -} \ No newline at end of file +} diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/cc_os_sched.h index b94bb30e..4b40b49a 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sched.h @@ -18,9 +18,9 @@ typedef struct cc_sched cc_sched_t; typedef enum { cc_sched_task_status_exit = 0x00, ///> Initial State - cc_sched_task_status_running, ///> Task currently running - cc_sched_task_status_ready, ///> Task Ready to despatch - cc_sched_task_status_wait, ///> Task in wait state + cc_sched_task_status_running = 0x01, ///> Task currently running + cc_sched_task_status_ready = 0x02, ///> Task Ready to despatch + cc_sched_task_status_wait = 0x03, ///> Task in wait state cc_sched_task_status_max = 0xff, ///> Do Nt Use } cc_sched_task_status_t; @@ -32,7 +32,7 @@ typedef struct link struct cc_sched_tcb { - char name [ccosconfig_CC_OS_TASK_NAME_LEN]; ///> Name of the Current Task + char name [ccosconfig_CC_OS_TASK_NAME_LEN + 1]; ///> Name of the Current Task size_t priority; ///> Priority of the task void * stack_ptr; ///> Stack Pointer size_t task_delay_ticks; ///> Time delay in ticks @@ -58,7 +58,7 @@ typedef void (* algo_fn)(cc_sched_ctrl_t * sched_ctrl); typedef enum { cc_sched_algo_round_robin = 0x00, ///> Round Robin scheduling algorithm - cc_sched_algo_priority_driven, ///> Priority driven Scheduling + cc_sched_algo_priority_driven = 0x01, ///> Priority driven Scheduling cc_sched_algo_max = 0xff }cc_sched_algo_t; diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h index 1d192e95..099efc94 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -4,4 +4,4 @@ #include void * cc_os_malloc(size_t size); -void * cc_os_free(void *addr); +void * cc_os_free(const void *addr); diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index c2eb579f..9619e458 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -37,8 +37,8 @@ extern cc_sched_tcb_t *g_cc_os_tcb_list; * INTERNAL EXTERNS FUNCTIONS *****************************************************/ extern void _cc_os_idle_task_fn (void * args); -extern void _insert_after (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); -extern void _insert_before (cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern void _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); @@ -52,7 +52,10 @@ extern cc_sched_ctrl_t g_sched_ctrl; /***************************************************** * STATIC FUNCTION DEFINATIONS *****************************************************/ - +void __cc_init_scheduler() +{ + return; +} /***************************************************** * USER FUNCTION DEFINATIONS *****************************************************/ @@ -104,12 +107,12 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { #else /* Dynamic Task Declaration */ - ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); if (ptr != NULL) + ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); { #endif /* Fill tcb details */ - memcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); + strlcpy(ptr->name, cc_os_task->name, ccosconfig_CC_OS_TASK_NAME_LEN); ptr->stack_ptr = cc_os_task->stack_ptr; ptr->priority = cc_os_task->priority; } @@ -126,7 +129,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) { - _insert_after(g_sched_ctrl.task_max_prio, ptr, CC_OS_FALSE); + _insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE); g_sched_ctrl.task_max_prio = ptr; } else @@ -136,7 +139,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { if (comp_ptr->priority <= ptr->priority) { - _insert_after(comp_ptr, ptr, CC_OS_FALSE); + _insert_after(&comp_ptr, ptr, CC_OS_FALSE); break; } else @@ -192,7 +195,7 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_pause_all_task () +cc_os_err_t cc_os_pause_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; @@ -210,19 +213,24 @@ cc_os_err_t cc_os_pause_all_task () return success; } -cc_os_err_t cc_os_resume_all_task () +cc_os_err_t cc_os_resume_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; - - while (ptr != g_sched_ctrl.ready_list_head) + if (ptr != NULL) { - if (ptr == g_sched_ctrl.curr_task) + while (ptr != g_sched_ctrl.ready_list_head) { - continue; + if (ptr == g_sched_ctrl.curr_task) + { + continue; + } + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + ptr = ptr->ready_link.next; } - - _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - ptr = ptr->ready_link.next; + } + else + { + return error_os_invalid_op; } return success; @@ -262,7 +270,6 @@ void cc_os_task_wait (const size_t ticks) void cc_os_task_yield() { - TODO("cc_os_task_yield"); return; } @@ -281,6 +288,7 @@ void cc_os_run (void) cc_os_add_task(&CC_GET_TASK_INST(cc_os_idle)); /* Initialise scheduler */ + __cc_init_scheduler(); cc_os_task_yield(); /* Yeild */ while (1) { diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index f75a30d3..afc9cb52 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -16,7 +16,6 @@ /***************************************************** * GLOBAL/STATIC VARIABLE DECLARATIONS *****************************************************/ -// static unsigned char g_cc_os_heap[ccosconfig_CC_OS_HEAP_SIZE]; /***************************************************** * GLOBAL EXTERNS @@ -33,7 +32,7 @@ void * cc_os_malloc(size_t size _UNUSED) { return NULL; } -void * cc_os_free(void *addr _UNUSED ) +void * cc_os_free(const void *addr _UNUSED ) { return NULL; } diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index c46dd285..951f4d13 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -60,76 +60,79 @@ cc_sched_ctrl_t g_sched_ctrl = /***************************************************** * INTERNAL USED FUNCTIONS (NON-STATIC) *****************************************************/ -void _insert_after(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { switch (link_type) { case 0: /* Ready Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->ready_link.next = new_node; new_node->ready_link.prev = new_node; } else { - new_node->ready_link.next = ptr->ready_link.next; - new_node->ready_link.prev = ptr; + new_node->ready_link.next = (*ptr)->ready_link.next; + new_node->ready_link.prev = *ptr; new_node->ready_link.next->ready_link.prev = new_node; - ptr->ready_link.next = new_node; + (*ptr)->ready_link.next = new_node; } break; case 1: /* Wait Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->wait_link.next = new_node; new_node->wait_link.prev = new_node; } + else { - new_node->wait_link.next = ptr->wait_link.next; - new_node->wait_link.prev = ptr; + new_node->wait_link.next = (*ptr)->wait_link.next; + new_node->wait_link.prev = *ptr; new_node->wait_link.next->wait_link.prev = new_node; - ptr->wait_link.next = new_node; + (*ptr)->wait_link.next = new_node; } break; default: return; } } -void _insert_before(cc_sched_tcb_t * ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { switch (link_type) { case 0: /* Ready Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->ready_link.next = new_node; new_node->ready_link.prev = new_node; } + else { - new_node->ready_link.next = ptr; - new_node->ready_link.prev = ptr->ready_link.prev; - ptr->ready_link.prev = new_node; + new_node->ready_link.next = *ptr; + new_node->ready_link.prev = (*ptr)->ready_link.prev; + (*ptr)->ready_link.prev = new_node; new_node->ready_link.prev->ready_link.next = new_node; } break; case 1: /* Wait Link */ - if (ptr == NULL) + if (*ptr == NULL) { - ptr = new_node; + *ptr = new_node; new_node->wait_link.next = new_node; new_node->wait_link.prev = new_node; } + else { - new_node->wait_link.next = ptr; - new_node->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = new_node; + new_node->wait_link.next = *ptr; + new_node->wait_link.prev = (*ptr)->wait_link.prev; + (*ptr)->wait_link.prev = new_node; new_node->wait_link.prev->wait_link.next = new_node; } break; @@ -144,7 +147,7 @@ void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, { return; } - _insert_before(sched_ctrl->wait_list_head, ptr, 1); + _insert_before(&(sched_ctrl->wait_list_head), ptr, 1); ptr->task_delay_ticks = ticks; ptr->task_status = cc_sched_task_status_wait; @@ -185,26 +188,22 @@ static void __cc_sched_context_switch(cc_sched_tcb_t * next_task) static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) { - cc_sched_tcb_t * ptr; - if(sched_ctrl->wait_list_head != NULL) + cc_sched_tcb_t * ptr = sched_ctrl->wait_list_head; + + while(ptr != NULL) { - ptr = sched_ctrl->wait_list_head; - /* Tasks present in wait list */ - while(1) + ptr->task_delay_ticks--; /* Tick caliberations required */ + if(ptr->task_delay_ticks == 0) { - ptr->task_delay_ticks--; /* Tick caliberations required */ - if(ptr->task_delay_ticks == 0) - { - _cc_sched_send_to_resume(sched_ctrl, ptr); - } - if (ptr->wait_link.next == sched_ctrl->wait_list_head) - { - break; - } - else - { - ptr = ptr->wait_link.next; - } + _cc_sched_send_to_resume(sched_ctrl, ptr); + } + if (ptr->wait_link.next == sched_ctrl->wait_list_head) + { + break; + } + else + { + ptr = ptr->wait_link.next; } } } @@ -234,26 +233,21 @@ static void __cc_sched_algo_round_robin_fn(cc_sched_ctrl_t * sched_ctrl) static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) { /* do waitlist adjustment */ - cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; - __cc_sched_wait_list_adjustment(sched_ctrl); - while (1) + + cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; + if(ptr != NULL) { - if (ptr->task_status == cc_sched_task_status_ready) - { - break; - } - else + while (ptr->task_status != cc_sched_task_status_ready) { - /* Iterate Backwards */ ptr = ptr->wait_link.prev; + if (ptr == sched_ctrl->ready_list_head) + { + /* IDLE Task */ + _cc_sched_send_to_resume(&g_sched_ctrl, ptr); + break; + } } - if (ptr == sched_ctrl->ready_list_head) - { - /* IDLE Task */ - _cc_sched_send_to_resume(&g_sched_ctrl, ptr); - break; - } + __cc_sched_context_switch(ptr); } - __cc_sched_context_switch(ptr); } From 24ca94738a085751e1872fb96b2de4ec4a915a3a Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Mon, 26 Dec 2022 11:08:15 +0530 Subject: [PATCH 20/38] cc_os_sem proto added --- src/include/visor/terravisor/cc_os/cc_os.h | 13 +--- .../visor/terravisor/cc_os/cc_os_sem.h | 32 +++++++++ .../visor/terravisor/cc_os/utils/cc_os_heap.h | 2 +- .../cc_os/{ => utils}/cc_os_sched.h | 22 +++++- src/visor/terravisor/services/kernel/cc_os.c | 45 ++++++------ .../terravisor/services/kernel/cc_os_heap.c | 4 +- .../terravisor/services/kernel/cc_os_idle.c | 10 +-- .../terravisor/services/kernel/cc_os_sched.c | 68 +++++++++--------- .../terravisor/services/kernel/cc_os_sem.c | 69 +++++++++++++++++++ 9 files changed, 192 insertions(+), 73 deletions(-) create mode 100644 src/include/visor/terravisor/cc_os/cc_os_sem.h rename src/include/visor/terravisor/cc_os/{ => utils}/cc_os_sched.h (74%) create mode 100644 src/visor/terravisor/services/kernel/cc_os_sem.c diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 0e231e4d..deb4fb3d 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -18,18 +18,7 @@ #include "stdlib.h" #include "stdint.h" #include -#include - -/***************************************************** - * DEFINES - *****************************************************/ -#define CC_OS_FALSE 0 -#define CC_OS_TRUE 1 -#define CC_OS_DELAY_MAX ((size_t) -1) - -#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC - -#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg +#include /***************************************************** * TYPEDEFS diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h new file mode 100644 index 00000000..4e359e2d --- /dev/null +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -0,0 +1,32 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os.h + * Description : CC OS semaphore declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +/***************************************************** + * DEFINES + *****************************************************/ +#include +/***************************************************** + * TYPEDEFS + *****************************************************/ +typedef struct sem +{ + uint8_t sem_init; + size_t sem_val; +}sem_t; + +/***************************************************** + * USER FUNCTION DECLARATIONS + *****************************************************/ +cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val); +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val); diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h index 099efc94..87c025d7 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -1,7 +1,7 @@ #include #include #include -#include +#include void * cc_os_malloc(size_t size); void * cc_os_free(const void *addr); diff --git a/src/include/visor/terravisor/cc_os/cc_os_sched.h b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h similarity index 74% rename from src/include/visor/terravisor/cc_os/cc_os_sched.h rename to src/include/visor/terravisor/cc_os/utils/cc_os_sched.h index 4b40b49a..d686ee72 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h @@ -10,8 +10,28 @@ #pragma once -#include "stdint.h" +/***************************************************** + * INCLUDES + *****************************************************/ +#include +#include +#include +/***************************************************** + * DEFINES + *****************************************************/ +#define CC_OS_FALSE 0 +#define CC_OS_TRUE 1 +#define CC_OS_NULL_PTR NULL +#define CC_OS_DELAY_MAX ((size_t) -1) + +#define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC + +#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg + +/***************************************************** + * TYPEDEFS + *****************************************************/ typedef struct cc_sched_tcb cc_sched_tcb_t; typedef struct cc_sched cc_sched_t; diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os.c index 9619e458..d8cfb8b3 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os.c @@ -17,7 +17,6 @@ #include #include #include -#include /***************************************************** * DEFINES @@ -37,8 +36,8 @@ extern cc_sched_tcb_t *g_cc_os_tcb_list; * INTERNAL EXTERNS FUNCTIONS *****************************************************/ extern void _cc_os_idle_task_fn (void * args); -extern void _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); -extern void _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern status_t _insert_after (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); +extern status_t _insert_before (cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type); extern void _cc_sched_send_to_wait (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks); extern void _cc_sched_send_to_resume (cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr); @@ -61,10 +60,10 @@ void __cc_init_scheduler() *****************************************************/ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { - CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->name != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != NULL); - CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->name != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_ptr != CC_OS_NULL_PTR); + CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != CC_OS_NULL_PTR); CC_OS_ASSERT_IF_FALSE(cc_os_task->stack_len != CC_OS_FALSE); CC_OS_ASSERT_IF_FALSE(cc_os_task->priority >= ccosconfig_CC_OS_IDLE_TASK_PRIORITY); CC_OS_ASSERT_IF_FALSE(cc_os_task->priority < CC_OS_PRIORITY_MAX); @@ -74,18 +73,18 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head; #if CC_OS_DYNAMIC == CC_OS_TRUE - if (ptr == NULL) + if (ptr == CC_OS_NULL_PTR) { /* First Dynamic task */ ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); - if (ptr == NULL) + if (ptr == CC_OS_NULL_PTR) { cc_os_resume_all_task(); return error_os_task_overfow; } } #endif - if ((ptr->ready_link.next == NULL )&& (ptr->ready_link.prev == NULL)) + if ((ptr->ready_link.next == CC_OS_NULL_PTR )&& (ptr->ready_link.prev == CC_OS_NULL_PTR)) { ptr->ready_link.next = ptr->ready_link.prev = g_sched_ctrl.ready_list_head; } @@ -107,7 +106,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) { #else /* Dynamic Task Declaration */ - if (ptr != NULL) + if (ptr != CC_OS_NULL_PTR) ptr = (cc_sched_tcb_t *)cc_os_malloc(sizeof(cc_sched_tcb_t)); { #endif @@ -123,23 +122,29 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) } } /* Insert Tasks in assending order of its priority */ - if (g_sched_ctrl.task_max_prio == NULL) + if (g_sched_ctrl.task_max_prio == CC_OS_NULL_PTR) { g_sched_ctrl.task_max_prio = ptr; } else if(g_sched_ctrl.task_max_prio->priority <= ptr->priority) { - _insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE); - g_sched_ctrl.task_max_prio = ptr; + if(_insert_after(&(g_sched_ctrl.task_max_prio), ptr, CC_OS_FALSE) == success) + { + g_sched_ctrl.task_max_prio = ptr; + } + else + { + cc_os_resume_all_task(); + return error_os_task_overfow; + } } else { cc_sched_tcb_t * comp_ptr = g_sched_ctrl.task_max_prio->ready_link.next; while (1) { - if (comp_ptr->priority <= ptr->priority) + if (comp_ptr->priority <= ptr->priority && (_insert_after(&comp_ptr, ptr, CC_OS_FALSE) == success)) { - _insert_after(&comp_ptr, ptr, CC_OS_FALSE); break; } else @@ -162,7 +167,7 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (cc_os_task != NULL) + if (cc_os_task != CC_OS_NULL_PTR) { ptr = cc_os_task->task_tcb_ptr; } @@ -185,7 +190,7 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) { cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; - if (cc_os_task != NULL) + if (cc_os_task != CC_OS_NULL_PTR) { ptr = cc_os_task->task_tcb_ptr; } @@ -216,7 +221,7 @@ cc_os_err_t cc_os_pause_all_task (void) cc_os_err_t cc_os_resume_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; - if (ptr != NULL) + if (ptr != CC_OS_NULL_PTR) { while (ptr != g_sched_ctrl.ready_list_head) { @@ -238,7 +243,7 @@ cc_os_err_t cc_os_resume_all_task (void) cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) { - CC_OS_ASSERT_IF_FALSE(cc_os_task != NULL); + CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); cc_sched_tcb_t * ptr = cc_os_task->task_tcb_ptr; diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index afc9cb52..c063d2f7 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -30,9 +30,9 @@ *****************************************************/ void * cc_os_malloc(size_t size _UNUSED) { - return NULL; + return CC_OS_NULL_PTR; } void * cc_os_free(const void *addr _UNUSED ) { - return NULL; + return CC_OS_NULL_PTR; } diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 0d413f52..9d837e6f 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -30,9 +30,9 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) ptr->ready_link.prev->ready_link.next = ptr->ready_link.next; ptr->ready_link.next->ready_link.prev = ptr->ready_link.prev; -#if ccosconfig_CC_OS_USE_DYNAMIC == 0 - ptr->ready_link.next = NULL; - ptr->ready_link.prev = NULL; +#if CC_OS_DYNAMIC == CC_OS_FALSE + ptr->ready_link.next = CC_OS_NULL_PTR; + ptr->ready_link.prev = CC_OS_NULL_PTR; #else cc_os_free(ptr); #endif @@ -47,9 +47,9 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) void _cc_os_idle_task_fn(os_args args) { cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; - static cc_sched_tcb_t * ptr = NULL; + static cc_sched_tcb_t * ptr = CC_OS_NULL_PTR; ptr = g_sched_ctrl->ready_list_head; - while (1) + while (CC_OS_TRUE) { /* Clean up task if terminated */ ptr = __free_terminated_task(ptr); diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index 951f4d13..a6629c1a 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -11,9 +11,8 @@ /***************************************************** * INCLUDES *****************************************************/ -#include #include -#include +#include /***************************************************** * DEFINES @@ -34,7 +33,7 @@ static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl); #if !ccosconfig_CC_OS_USE_DYNAMIC cc_sched_tcb_t g_cc_os_tcb_list [ccosconfig_CC_OS_MAX_THREAD]; #else -cc_sched_tcb_t * g_cc_os_tcb_list = NULL; +cc_sched_tcb_t * g_cc_os_tcb_list = CC_OS_NULL_PTR; #endif cc_sched_t g_cc_sched_list [] = @@ -46,27 +45,28 @@ cc_sched_t g_cc_sched_list [] = cc_sched_ctrl_t g_sched_ctrl = { #if !ccosconfig_CC_OS_USE_DYNAMIC - .ready_list_head = &(g_cc_os_tcb_list[0]), - .curr_task = &(g_cc_os_tcb_list[0]), + .ready_list_head = &(g_cc_os_tcb_list[CC_OS_FALSE]), + .curr_task = &(g_cc_os_tcb_list[CC_OS_FALSE]), #else - .ready_list_head = NULL, - .curr_task = NULL, + .ready_list_head = CC_OS_NULL_PTR, + .curr_task = CC_OS_NULL_PTR, #endif - .wait_list_head = NULL, - .task_max_prio = NULL, + .wait_list_head = CC_OS_NULL_PTR, + .task_max_prio = CC_OS_NULL_PTR, .selected_sched = &(g_cc_sched_list[cc_sched_algo_round_robin]) }; /***************************************************** * INTERNAL USED FUNCTIONS (NON-STATIC) *****************************************************/ -void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +status_t _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { + CC_OS_ASSERT_IF_FALSE(new_node != CC_OS_NULL_PTR); switch (link_type) { - case 0: + case CC_OS_FALSE: /* Ready Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->ready_link.next = new_node; @@ -80,9 +80,9 @@ void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t lin (*ptr)->ready_link.next = new_node; } break; - case 1: + case CC_OS_TRUE: /* Wait Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->wait_link.next = new_node; @@ -97,16 +97,18 @@ void _insert_after(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t lin } break; default: - return; + return error_os_invalid_op; } + return success; } -void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) +status_t _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t link_type) { + CC_OS_ASSERT_IF_FALSE(new_node != CC_OS_NULL_PTR); switch (link_type) { - case 0: + case CC_OS_FALSE: /* Ready Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->ready_link.next = new_node; @@ -120,9 +122,9 @@ void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t li new_node->ready_link.prev->ready_link.next = new_node; } break; - case 1: + case CC_OS_TRUE: /* Wait Link */ - if (*ptr == NULL) + if (*ptr == CC_OS_NULL_PTR) { *ptr = new_node; new_node->wait_link.next = new_node; @@ -137,8 +139,9 @@ void _insert_before(cc_sched_tcb_t ** ptr, cc_sched_tcb_t * new_node, uint8_t li } break; default: - return; + return error_os_invalid_op; } + return success; } void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, const size_t ticks) @@ -147,10 +150,11 @@ void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, { return; } - _insert_before(&(sched_ctrl->wait_list_head), ptr, 1); - - ptr->task_delay_ticks = ticks; - ptr->task_status = cc_sched_task_status_wait; + if(_insert_before(&(sched_ctrl->wait_list_head), ptr, CC_OS_TRUE) == success) + { + ptr->task_delay_ticks = ticks; + ptr->task_status = cc_sched_task_status_wait; + } } void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr) @@ -167,14 +171,14 @@ void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr if (ptr->wait_link.next == ptr && ptr->wait_link.prev == ptr) { /* Last Wait task left */ - sched_ctrl->wait_list_head = NULL; + sched_ctrl->wait_list_head = CC_OS_NULL_PTR; } } ptr->wait_link.prev->wait_link.next = ptr->wait_link.next; ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; - ptr->wait_link.prev = NULL; - ptr->wait_link.next = NULL; - ptr->task_delay_ticks = 0; + ptr->wait_link.prev = CC_OS_NULL_PTR; + ptr->wait_link.next = CC_OS_NULL_PTR; + ptr->task_delay_ticks = CC_OS_FALSE; ptr->task_status = cc_sched_task_status_ready; } @@ -190,10 +194,10 @@ static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) { cc_sched_tcb_t * ptr = sched_ctrl->wait_list_head; - while(ptr != NULL) + while(ptr != CC_OS_NULL_PTR) { ptr->task_delay_ticks--; /* Tick caliberations required */ - if(ptr->task_delay_ticks == 0) + if(ptr->task_delay_ticks == CC_OS_FALSE) { _cc_sched_send_to_resume(sched_ctrl, ptr); } @@ -236,7 +240,7 @@ static void __cc_sched_algo_priority_driven_fn(cc_sched_ctrl_t * sched_ctrl) __cc_sched_wait_list_adjustment(sched_ctrl); cc_sched_tcb_t * ptr = sched_ctrl->ready_list_head->ready_link.prev; - if(ptr != NULL) + if(ptr != CC_OS_NULL_PTR) { while (ptr->task_status != cc_sched_task_status_ready) { diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c new file mode 100644 index 00000000..eb38be51 --- /dev/null +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -0,0 +1,69 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2CC_OS_FALSE22, Cyancore Team + * + * File Name : cc_os_sem.h + * Description : CC OS Semaphore function definations + * Primary Author : Pranjal Chanda [pranjalchandaCC_OS_FALSE8@gmail.com] + * Organisation : Cyancore Core-Team + */ + +/***************************************************** + * INCLUDES + *****************************************************/ +#include + +/***************************************************** + * GLOBAL/STATIC VARIABLE DECLARATIONS + *****************************************************/ + +/***************************************************** + * GLOBAL EXTERNS + *****************************************************/ + +/***************************************************** + * STATIC FUNCTION DEFINATIONS + *****************************************************/ + +/***************************************************** + * USER FUNCTION DEFINATIONS + *****************************************************/ + +cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init == CC_OS_FALSE)); + + sem_ptr->sem_init = CC_OS_TRUE; + sem_ptr->sem_val = init_val; + + return success; +} +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + return success; +} +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + return success; +} +cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + + sem_ptr->sem_init = CC_OS_FALSE; + + return success; +} +cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val) +{ + CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + CC_OS_ASSERT_IF_FALSE(val != CC_OS_NULL_PTR); + + *val = sem_ptr->sem_val; + + return success; +} From 0b8020f740d4d64c591dda788d4173664a3cae22 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Tue, 27 Dec 2022 09:32:32 +0530 Subject: [PATCH 21/38] Semaphore complete !Need to add semaphore in ISR logic yet --- src/include/status.h | 1 + .../visor/terravisor/cc_os/cc_os_config.h | 8 +++ .../visor/terravisor/cc_os/cc_os_sem.h | 22 ++++++-- .../terravisor/services/kernel/cc_os_idle.c | 6 +- .../terravisor/services/kernel/cc_os_sem.c | 56 +++++++++++++++---- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/include/status.h b/src/include/status.h index 0264eb68..5b31e428 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -77,6 +77,7 @@ typedef enum status error_os_task_overfow = -0x0f01, error_os_deadlock = -0x0f02, error_os_invalid_op = -0x0f03, + error_os_sem_get = -0x0f04, /* Mesg related error */ error_mesg = -0x1000, error_mesg_long = -0x1001, diff --git a/src/include/visor/terravisor/cc_os/cc_os_config.h b/src/include/visor/terravisor/cc_os/cc_os_config.h index 729b34a6..3ffb4fd6 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_config.h +++ b/src/include/visor/terravisor/cc_os/cc_os_config.h @@ -59,3 +59,11 @@ #ifndef ccosconfig_CC_OS_TASK_STACK_LEN #define ccosconfig_CC_OS_TASK_STACK_LEN 255 #endif + +/** + * @brief If the Kernel shall enter poer save mode during IDLE + * + */ +#ifndef ccosconfig_CC_OS_POWER_SAVE_EN +#define ccosconfig_CC_OS_POWER_SAVE_EN 0 +#endif diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h index 4e359e2d..a0099c82 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sem.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -21,12 +21,26 @@ typedef struct sem uint8_t sem_init; size_t sem_val; }sem_t; +/***************************************************** + * USER MACROS + *****************************************************/ +#define CC_SEM_INST(_Name) _Name##_sem_inst +#if CC_OS_DYNAMIC == CC_OS_FALSE +#define CC_SEM_DEF(_Name) \ +static sem_t _Name##_sem = { \ + .sem_init = 0, \ + .sem_val = 0 \ +}; \ +static sem_t * _Name##_sem_inst = &_Name##_sem +#else +static sem_t * _Name##_sem_inst = CC_OS_NULL_PTR +#endif /***************************************************** * USER FUNCTION DECLARATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val); -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr); +cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val); +cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr); cc_os_err_t cc_os_sem_give (sem_t * sem_ptr); -cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr); -cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val); +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks); +cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val); diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_idle.c index 9d837e6f..0d47ee88 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_idle.c @@ -46,16 +46,18 @@ static cc_sched_tcb_t * __free_terminated_task(cc_sched_tcb_t * ptr) *****************************************************/ void _cc_os_idle_task_fn(os_args args) { - cc_sched_ctrl_t * g_sched_ctrl = (cc_sched_ctrl_t *) args; + cc_sched_ctrl_t * sched_ctrl = (cc_sched_ctrl_t *) args; static cc_sched_tcb_t * ptr = CC_OS_NULL_PTR; - ptr = g_sched_ctrl->ready_list_head; + ptr = sched_ctrl->ready_list_head; while (CC_OS_TRUE) { /* Clean up task if terminated */ ptr = __free_terminated_task(ptr); +#if ccosconfig_CC_OS_POWER_SAVE_EN /* Power Save code */ +#endif /* Yield for next available task */ cc_os_task_yield(); } diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c index eb38be51..49690df7 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sem.c +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -12,6 +12,7 @@ * INCLUDES *****************************************************/ #include +#include /***************************************************** * GLOBAL/STATIC VARIABLE DECLARATIONS @@ -29,36 +30,71 @@ * USER FUNCTION DEFINATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t * sem_ptr, size_t init_val) +cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val) { - CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init == CC_OS_FALSE)); +#if CC_OS_DYNAMIC == CC_OS_FALSE + CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init == CC_OS_FALSE)); - sem_ptr->sem_init = CC_OS_TRUE; - sem_ptr->sem_val = init_val; + (*sem_ptr)->sem_init = CC_OS_TRUE; + (*sem_ptr)->sem_val = init_val; +#else + CC_OS_ASSERT_IF_FALSE(*sem_ptr == CC_OS_NULL_PTR); + *sem_ptr = cc_os_malloc(sizeof(sem_t)); + if (*sem_ptr == CC_OS_NULL_PTR) + { + return error_memory_low; + } + else + { + (*sem_ptr)->sem_val = init_val; + (*sem_ptr)->sem_init = CC_OS_TRUE; + } +#endif return success; } -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + if (sem_ptr->sem_val == CC_OS_FALSE) + { + if (wait_ticks == CC_OS_FALSE) /* ||_IS_ISR */ + { + return error_os_sem_get; + } + else + { + cc_os_task_wait(wait_ticks); + } + } + else + { + sem_ptr->sem_val--; + } return success; } -cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + sem_ptr->sem_val++; + return success; } -cc_os_err_t cc_os_sem_delete (sem_t * sem_ptr) +cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr) { - CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); + CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init != CC_OS_FALSE)); + + (*sem_ptr)->sem_init = CC_OS_FALSE; - sem_ptr->sem_init = CC_OS_FALSE; +#if CC_OS_DYNAMIC == CC_OS_TRUE + cc_os_free(*sem_ptr); +#endif return success; } -cc_os_err_t cc_os_sem_get_val (sem_t * sem_ptr, size_t * val) +cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); CC_OS_ASSERT_IF_FALSE(val != CC_OS_NULL_PTR); From 37a6697f7495268db9045b50b6e8c689c0ab2790 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 8 Nov 2022 17:16:37 +0530 Subject: [PATCH 22/38] Updated asm file --- src/arch/avr/8/common_5x_6/terravisor/asm.S | 109 +++----------------- 1 file changed, 17 insertions(+), 92 deletions(-) diff --git a/src/arch/avr/8/common_5x_6/terravisor/asm.S b/src/arch/avr/8/common_5x_6/terravisor/asm.S index 10154c09..e1e3ce39 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/asm.S +++ b/src/arch/avr/8/common_5x_6/terravisor/asm.S @@ -21,39 +21,17 @@ * As per the abi spec of AVR5-8, r1 is always supposed to be 0. * Hence, r1 is reset to 0 and other are moved with r1. */ +.altmacro +.macro clear_reg n + eor r\n, r\n +.endm + function zero_reg - eor r1, r1 - mov r0, r1 - mov r2, r1 - mov r3, r1 - mov r4, r1 - mov r5, r1 - mov r6, r1 - mov r7, r1 - mov r8, r1 - mov r9, r1 - mov r10, r1 - mov r11, r1 - mov r12, r1 - mov r13, r1 - mov r14, r1 - mov r15, r1 - mov r16, r1 - mov r17, r1 - mov r18, r1 - mov r19, r1 - mov r20, r1 - mov r21, r1 - mov r22, r1 - mov r23, r1 - mov r24, r1 - mov r25, r1 - mov r26, r1 - mov r27, r1 - mov r28, r1 - mov r29, r1 - mov r30, r1 - mov r31, r1 +.set i, 0 +.rept 32 + clear_reg %i +.set i, i+1 +.endr ret function arch_panic_handler @@ -144,6 +122,7 @@ function arch_panic_handler * * @param id - ID of the interrupt vector */ +.altmacro .macro INT id /** * int_(id) - Interrupt router function @@ -180,63 +159,9 @@ function isr * functions will be retained and others are cleaned up. */ /*==========< Interrupt router functions >==========*/ -INT 1 -INT 2 -INT 3 -INT 4 -INT 5 -INT 6 -INT 7 -INT 8 -INT 9 -INT 10 -INT 11 -INT 12 -INT 13 -INT 14 -INT 15 -INT 16 -INT 17 -INT 18 -INT 19 -INT 20 -INT 21 -INT 22 -INT 23 -INT 24 -INT 25 -INT 26 -INT 27 -INT 28 -INT 29 -INT 30 -INT 31 -INT 32 -INT 33 -INT 34 -INT 35 -INT 36 -INT 37 -INT 38 -INT 39 -INT 40 -INT 41 -INT 42 -INT 43 -INT 44 -INT 45 -INT 46 -INT 47 -INT 48 -INT 49 -INT 50 -INT 51 -INT 52 -INT 53 -INT 54 -INT 55 -INT 56 -INT 57 -INT 58 -INT 59 -INT 60 + +.set i, 1 +.rept 60 + INT %i +.set i, i+1 +.endr From 0bb4f0b3908231b4adbe45a1b484c5efdf677644 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 5 Dec 2022 21:15:06 +0530 Subject: [PATCH 23/38] Add HAL for AON_PMU - Added new error status - Added new apis for aon_pmu module - Updated documentation Issue: #171 --- src/include/status.h | 1 + src/include/utils.h | 1 + .../sifive/common_fe310/hal/aon/aon_private.h | 17 ++- .../sifive/common_fe310/hal/aon/pmu.c | 134 +++++++++++++++++- .../sifive/common_fe310/include/aon.h | 6 +- 5 files changed, 149 insertions(+), 10 deletions(-) diff --git a/src/include/status.h b/src/include/status.h index 62e06982..2c1c1cce 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -65,6 +65,7 @@ typedef enum status error_system_irq_link_fail = -0x0b01, error_system_irq_unlink_fail = -0x0b02, error_system_clk_caliberation = -0x0b03, + error_system_prog_fail = -0x0b04, /* Network related error */ error_net = -0x0c00, error_net_con_timeout = -0x0c01, diff --git a/src/include/utils.h b/src/include/utils.h index 115dc2ea..f06ebe90 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -10,4 +10,5 @@ #pragma once +#define RET_ON_FAIL(condition, status) if(!condition) return status #define ROUNDUP_ALIGN(x, align) x += align - (x % align) diff --git a/src/platform/sifive/common_fe310/hal/aon/aon_private.h b/src/platform/sifive/common_fe310/hal/aon/aon_private.h index eec4b3f0..1b0fd675 100644 --- a/src/platform/sifive/common_fe310/hal/aon/aon_private.h +++ b/src/platform/sifive/common_fe310/hal/aon/aon_private.h @@ -1,6 +1,6 @@ /* * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team + * Copyrights (C) 2019-2022, Cyancore Team * * File Name : aon_private.h * Description : This file contains macors used by AON HAL @@ -15,20 +15,25 @@ #define WDOGS_OFFSET 0x010 #define WDOGFEED_OFFSET 0x018 #define WDOGKEY_OFFSET 0x01c -#define WDOGCMP_OFFSET(X) 0x020 + (0 * X) +/* As per fe310 family doc, there is only 1 reg, but keeping the offset generic */ +#define WDOGCMP_OFFSET(X) (0x020 + (4 * X)) #define RTCCFG_OFFSET 0x040 #define RTCCOUNTLO_OFFSET 0x048 #define RTCCOUNTHI_OFFSET 0x04c #define RTCS_OFFSET 0x050 -#define RTCCMP_OFFSET(X) 0x060 + (0 * X) +/* As per fe310 family doc, there is only 1 reg, but keeping the offset generic*/ +#define RTCCMP_OFFSET(X) (0x060 + (4 * X)) #define LFROSCCFG_OFFSET 0x070 #define LFCLKMUX_OFFSET 0x07c -#define BACKUP_OFFSET(X) 0x080 + (4 * X) -#define PMUWAKEUPI_OFFSET(X) 0x100 + (4 * X) -#define PMUSLEEPI_OFFSET(X) 0x120 + (4 * X) +#define BACKUP_OFFSET(X) (0x080 + (4 * X)) +#define PMUWAKEUPI_OFFSET(X) (0x100 + (4 * X)) +#define PMUSLEEPI_OFFSET(X) (0x120 + (4 * X)) #define PMUIE_OFFSET 0x140 #define PMUCAUSE_OFFSET 0x144 #define PMUSLEEP_OFFSET 0x148 #define PMUKEY_OFFSET 0x14c #define SIFIVEBG_OFFSET 0x210 #define AONCFG_OFFSET 0x300 + +#define SIFIVE_PMU_SECRET 0x51f15e +#define PMU_PROGRAM_SIZE 8 diff --git a/src/platform/sifive/common_fe310/hal/aon/pmu.c b/src/platform/sifive/common_fe310/hal/aon/pmu.c index e306c1f0..48dc1cc1 100644 --- a/src/platform/sifive/common_fe310/hal/aon/pmu.c +++ b/src/platform/sifive/common_fe310/hal/aon/pmu.c @@ -11,16 +11,144 @@ #include #include #include +#include #include #include #include #include #include "aon_private.h" -status_t aon_pmucause(aon_port_t *port, uint32_t *reset_val) +/** + * aon_pmu_unlock - Unlocks write access to PMU registers + * + * @brief This function writes SIFIVE SECRET to key register + * which unlocks write access to any PMU Registers. + * This function needs to be called before performing writes + * PMU registers. Upon successful write, the PMU register + * gets locked and again this function needs to be called + * for further writes. + * + * @param[in] *port - HAL driver port pointer + */ +static void aon_pmu_unlock(const aon_port_t *port) +{ + /* Writing SECRET to key register unlocks write access */ + MMIO32(port->baddr + PMUKEY_OFFSET) = SIFIVE_PMU_SECRET; + arch_dsb(); +} + +/** + * aon_pmucause - Returns cause for PMU wakeup event + * + * @brief This function reads and returns PMU event value. + * + * @param[in] *port - HAL driver port pointer + * @param[out] *reset_val - Pointer to store the result + * @return status: function status + */ +status_t aon_pmucause(const aon_port_t *port, uint32_t *reset_val) { - if(!port) - return error_func_inval_arg; + RET_ON_FAIL(port, error_func_inval_arg); *reset_val = MMIO32(port->baddr + PMUCAUSE_OFFSET); return success; } + +/** + * aon_pmusleep_program - api to program sleep routine + * + * @brief This api needs to be invoked to program the sleep + * instructions into the PMU module. Input array should be + * PMU_PROGRAM_SIZE * 16 Bytes wide. + * + * @param[in] *port - HAL driver port pointer + * @param[in] *arr - Array consisting of sleep instructions + * @return status: function status + */ +status_t aon_pmusleep_program(const aon_port_t *port, const uint16_t *arr) +{ + RET_ON_FAIL(port, error_func_inval_arg); + sysdbg4("Programming sleep sequence...\n"); + for(size_t i = 0; i < PMU_PROGRAM_SIZE; i++) + { + /* Unlock writes to PMU Register */ + aon_pmu_unlock(port); + MMIO32(port->baddr + PMUSLEEPI_OFFSET(i)) = arr[i]; + /* Make sure the instructions are stored */ + arch_dsb(); + } + + sysdbg4("Verifying sleep sequence...\n"); + for(size_t i = 0; i < PMU_PROGRAM_SIZE; i++) + if(MMIO32(port->baddr + PMUSLEEPI_OFFSET(i)) != arr[i]) + return error_system_prog_fail; + return success; +} + +/** + * aon_pmuwake_program - api to program wake up routine + * + * @brief This api needs to be invoked to program the wake up + * instructions into the PMU module. Input array should be + * PMU_PROGRAM_SIZE * 16 Bytes wide. + * + * @param[in] *port - HAL driver port pointer + * @param[in] *arr - Array consisting of wake up instructions + * @return status: function status + */ +status_t aon_pmuwake_program(const aon_port_t *port, const uint16_t *arr) +{ + RET_ON_FAIL(port, error_func_inval_arg); + sysdbg4("Programming wake up sequence...\n"); + for(size_t i = 0; i < PMU_PROGRAM_SIZE; i++) + { + /* Unlock writes to PMU Register */ + aon_pmu_unlock(port); + MMIO32(port->baddr + PMUWAKEUPI_OFFSET(i)) = arr[i]; + /* Make sure the instructions are stored */ + arch_dsb(); + } + + sysdbg4("Verifying wake up sequence...\n"); + for(size_t i = 0; i < PMU_PROGRAM_SIZE; i++) + if(MMIO32(port->baddr + PMUWAKEUPI_OFFSET(i)) != arr[i]) + return error_system_prog_fail; + return success; +} + +/** + * aon_pmu_ie - api to enable pmu interrupt + * + * @brief This api needs to be invoked when higher level + * driver needs to enable wake up trigger before executing + * sleep routine. + * + * @param[in] *port - HAL driver port pointer + * @param[in] index - Index of wake up source + * @return status: function status + */ +status_t aon_pmu_ie(const aon_port_t *port, uint32_t index) +{ + RET_ON_FAIL(port, error_func_inval_arg); + aon_pmu_unlock(port); + MMIO32(port->baddr + PMUIE_OFFSET) |= (1 << index); + arch_dsb(); + return success; +} + +/** + * aon_pmusleep_trigger - api to trigger execution of sleep program + * + * @brief This api is used to execute sleep program and put + * platform to sleep. + * + * @param[in] *port - HAL driver port pointer + * @return status: function status + */ +status_t aon_pmusleep_trigger(const aon_port_t *port) +{ + RET_ON_FAIL(port, error_func_inval_arg); + aon_pmu_unlock(port); + MMIO32(port->baddr + PMUSLEEP_OFFSET) = 1; + arch_dsb(); + return success; +} diff --git a/src/platform/sifive/common_fe310/include/aon.h b/src/platform/sifive/common_fe310/include/aon.h index a602fd73..054aadfb 100644 --- a/src/platform/sifive/common_fe310/include/aon.h +++ b/src/platform/sifive/common_fe310/include/aon.h @@ -17,4 +17,8 @@ typedef struct aon_port } aon_port_t; -status_t aon_pmucause(aon_port_t *, uint32_t *); +status_t aon_pmucause(const aon_port_t *, uint32_t *); +status_t aon_pmusleep_program(const aon_port_t *, const uint16_t *); +status_t aon_pmuwake_program(const aon_port_t *, const uint16_t *); +status_t aon_pmu_ie(const aon_port_t *, uint32_t); +status_t aon_pmusleep_trigger(const aon_port_t *); From 7f1452b8640352f52c8502bd450b15149bb1ad1c Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 28 Dec 2022 11:08:35 +0530 Subject: [PATCH 24/38] Review Comments Resolved --- projects/demo_avr/config.mk | 2 +- projects/demo_riscv/config.mk | 1 + projects/hifive1b_bl/config.mk | 1 + projects/project.template/config.mk | 1 + src/include/visor/terravisor/cc_os/cc_os.h | 150 +---------------- .../visor/terravisor/cc_os/cc_os_sem.h | 56 ++++++- .../visor/terravisor/cc_os/cc_os_tasks.h | 157 ++++++++++++++++++ .../visor/terravisor/cc_os/utils/cc_os_heap.h | 10 ++ .../terravisor/cc_os/utils/cc_os_sched.h | 2 +- .../terravisor/services/kernel/cc_os_heap.c | 2 +- .../terravisor/services/kernel/cc_os_sched.c | 2 +- .../terravisor/services/kernel/cc_os_sem.c | 10 +- .../{cc_os_idle.c => cc_os_task_idle.c} | 2 +- .../kernel/{cc_os.c => cc_os_tasks.c} | 18 +- 14 files changed, 242 insertions(+), 172 deletions(-) create mode 100644 src/include/visor/terravisor/cc_os/cc_os_tasks.h rename src/visor/terravisor/services/kernel/{cc_os_idle.c => cc_os_task_idle.c} (98%) rename src/visor/terravisor/services/kernel/{cc_os.c => cc_os_tasks.c} (94%) diff --git a/projects/demo_avr/config.mk b/projects/demo_avr/config.mk index 9fcc3484..8e768966 100644 --- a/projects/demo_avr/config.mk +++ b/projects/demo_avr/config.mk @@ -17,4 +17,4 @@ BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 OBRDLED_ENABLE := 1 -TERRAKERN := 1 +TERRAKERN := 0 diff --git a/projects/demo_riscv/config.mk b/projects/demo_riscv/config.mk index 945e71d4..086686f1 100644 --- a/projects/demo_riscv/config.mk +++ b/projects/demo_riscv/config.mk @@ -17,3 +17,4 @@ BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 OBRDLED_ENABLE := 1 +TERRAKERN := 0 diff --git a/projects/hifive1b_bl/config.mk b/projects/hifive1b_bl/config.mk index 2fecd625..158073c4 100644 --- a/projects/hifive1b_bl/config.mk +++ b/projects/hifive1b_bl/config.mk @@ -17,3 +17,4 @@ BOOTMSGS := 1 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 0 OBRDLED_ENABLE := 1 +TERRAKERN := 0 diff --git a/projects/project.template/config.mk b/projects/project.template/config.mk index f3de5783..f0d4c172 100644 --- a/projects/project.template/config.mk +++ b/projects/project.template/config.mk @@ -15,3 +15,4 @@ PLATFORM := SYSLOG_MEMBUF := EARLYCON_SERIAL := CONSOLE_SERIAL := +TERRAKERN := diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index deb4fb3d..89a89ce0 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -3,7 +3,7 @@ * Copyrights (C) 2022, Cyancore Team * * File Name : cc_os.h - * Description : CC OS Kernel declaration + * Description : CC OS Kernel * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team */ @@ -14,151 +14,7 @@ /***************************************************** * INCLUDES *****************************************************/ -#include "status.h" -#include "stdlib.h" -#include "stdint.h" -#include -#include - -/***************************************************** - * TYPEDEFS - *****************************************************/ -typedef status_t cc_os_err_t; - -typedef void * os_args; -typedef void (* task_fn)(os_args args); -typedef const char c_char; - -/** - * @brief TASK infrastructure structure - * - */ -typedef struct cc_os_task -{ - task_fn task_fn; ///>> Task funcion - os_args args; ///>> Task Args ptr - c_char * name; ///>> String name of the task - size_t priority; ///>> For waited tasks - size_t * stack_ptr; ///>> Stack pointer of the task - size_t stack_len; ///>> Stack lengths of the task - cc_sched_tcb_t * task_tcb_ptr; ///>> For internal use only -}cc_os_task_t; - -/***************************************************** - * MACROS - *****************************************************/ -/** - * @brief Function to declare a static task with a dedicated stack for the task - * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); - * - * @note DO NOT use space in place of TASK_Name as it would result build errors. - * - */ -#define CC_TASK_DEF(_NAME, _fn, _args, _PRI, STACK_LEN) \ -static size_t _NAME##_stack[STACK_LEN]; \ -static cc_os_task_t _NAME##_task = { \ - .args = _args, \ - .task_fn = _fn, \ - .name = #_NAME, \ - .priority = _PRI, \ - .stack_ptr = _NAME##_stack, \ - .stack_len = STACK_LEN \ - } \ - -/** - * @brief Function to get the instance using its name of already declared task. - * @brief Usage: cc_os_task_t * task = &(CC_GET_TASK_INST(TASK_Name)); - * - * @note DO NOT use space in place of TASK_Name as it would result build errors. - * - */ -#define CC_GET_TASK_INST(_NAME) _NAME##_task - -/***************************************************** - * USER FUNCTION DECLARATIONS - *****************************************************/ -/** - * @brief A Function to add a task to the scheduler - * - * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task - * @return cc_os_err_t - */ -cc_os_err_t cc_os_add_task(cc_os_task_t * cc_os_task); - -/** - * @brief A function to delete a task from the scheduler by instance - * - * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; - * Pass NULL to point to current task - * @return cc_os_err_t - */ -cc_os_err_t cc_os_del_task(cc_os_task_t * cc_os_task); - -/** - * @brief A Function to pause the task until call resume explicitly using its instance - * - * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; - * Pass NULL to point to current task - * @return cc_os_err_t - */ -cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task); - -/** - * - * @brief A Function to resume paused task using its instance - * @note Calling this function for already non-waiting task has no effect. - * - * @param cc_os_task pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; - * Pass NULL to point to current task - * @return cc_os_err_t - */ -cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task); - -/** - * @brief A Function to pause all the tasks except the current and the IDLE Task - * @note To resume all please use cc_os_resume_all_task() call - * - * @return cc_os_err_t - */ -cc_os_err_t cc_os_pause_all_task (void); - -/** - * @brief A Function to resume all the tasks - * - * @return cc_os_err_t - */ -cc_os_err_t cc_os_resume_all_task (void); - -/** - * @brief A function to set CC OS scheduler algorithm - * - * @return cc_os_err_t - */ -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); - -/** - * @brief A Function to put the current task to a waiting state and yield - * @note To just Yeild set ticks to 0 - * - * @param ticks Number of CC_OS Ticks - * @return None - */ -void cc_os_task_wait(const size_t ticks); - -/** - * @brief A Function to switch to next available task - * - * @param ticks Number of CC_OS Ticks - * @return None - */ -void cc_os_task_yield(); - -/** - * @brief A Function to invoke the kernel - * - * @return cc_os_err_t - */ -void cc_os_run(void); - +#include +#include #endif /* __CC_OS__ */ diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h index a0099c82..8eb8526c 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sem.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_os.h + * File Name : cc_os_sem.h * Description : CC OS semaphore declaration * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team @@ -39,8 +39,52 @@ static sem_t * _Name##_sem_inst = CC_OS_NULL_PTR /***************************************************** * USER FUNCTION DECLARATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val); -cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr); -cc_os_err_t cc_os_sem_give (sem_t * sem_ptr); -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks); -cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val); +/** + * @brief Create a semaphore and initialise it + * @note The instance needs to be provided using CC_SEM_DEF macro + * + * @param sem_ptr[in_out] Instance pointer + * @param init_val[in] Initial value + * + * @return status_t + */ +status_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val); + +/** + * @brief Delete a semaphore and de-initialise it + * + * @param sem_ptr[in_out] Instance pointer + * + * @return status_t + */ +status_t cc_os_sem_delete (sem_t ** sem_ptr); + +/** + * @brief Decrement a semaphore value + * + * @param sem_ptr[in] Instance pointer + * + * @return status_t + */ +status_t cc_os_sem_give (sem_t * sem_ptr); + +/** + * @brief Increment a semaphore value + * + * @param sem_ptr[in] Instance pointer + * @param wait_ticks[in] Timeout Wait ticks + * + * @return status_t + */ +status_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks); + +/** + * @brief Get current semaphore value + * + * @param sem_ptr[in] Instance pointer + * @param val[out] Value return + * + * @return status_t + */ + +status_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val); diff --git a/src/include/visor/terravisor/cc_os/cc_os_tasks.h b/src/include/visor/terravisor/cc_os/cc_os_tasks.h new file mode 100644 index 00000000..74938dca --- /dev/null +++ b/src/include/visor/terravisor/cc_os/cc_os_tasks.h @@ -0,0 +1,157 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_task.h + * Description : CC OS Kernel Tasks declaration + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +/***************************************************** + * INCLUDES + *****************************************************/ + +#include "status.h" +#include "stdlib.h" +#include "stdint.h" +#include +#include + +/***************************************************** + * TYPEDEFS + *****************************************************/ +typedef void *os_args; +typedef void (*task_fn)(os_args args); +typedef const char c_char; + +/** + * @brief TASK infrastructure structure + * + */ +typedef struct cc_os_task +{ + task_fn task_fn; ///>> Task funcion + os_args args; ///>> Task Args ptr + c_char *name; ///>> String name of the task + size_t priority; ///>> For waited tasks + size_t *stack_ptr; ///>> Stack pointer of the task + size_t stack_len; ///>> Stack lengths of the task + cc_sched_tcb_t *task_tcb_ptr; ///>> For internal use only +} cc_os_task_t; + +/***************************************************** + * MACROS + *****************************************************/ +/** + * @brief Function to declare a static task with a dedicated stack for the task + * @brief Usage: CC_TASK_DEF(TASK_Name, task_func_pointer, priority(int), stack_len(int)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_TASK_DEF(_NAME, _fn, _args, _PRI, STACK_LEN) \ + static size_t _NAME##_stack[STACK_LEN]; \ + static cc_os_task_t _NAME##_task = { \ + .args = _args, \ + .task_fn = _fn, \ + .name = #_NAME, \ + .priority = _PRI, \ + .stack_ptr = _NAME##_stack, \ + .stack_len = STACK_LEN} + +/** + * @brief Function to get the instance using its name of already declared task. + * @brief Usage: cc_os_task_t * task = &(CC_GET_TASK_INST(TASK_Name)); + * + * @note DO NOT use space in place of TASK_Name as it would result build errors. + * + */ +#define CC_GET_TASK_INST(_NAME) _NAME##_task + +/***************************************************** + * USER FUNCTION DECLARATIONS + *****************************************************/ +/** + * @brief A Function to add a task to the scheduler + * + * @param cc_os_task[in_out] pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task + * @return status_t + */ +status_t cc_os_add_task(cc_os_task_t *cc_os_task); + +/** + * @brief A function to delete a task from the scheduler by instance + * + * @param cc_os_task[in] pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return status_t + */ +status_t cc_os_del_task(cc_os_task_t *cc_os_task); + +/** + * @brief A Function to pause the task until call resume explicitly using its instance + * + * @param cc_os_task[in] pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return status_t + */ +status_t cc_os_pause_task(cc_os_task_t *cc_os_task); + +/** + * + * @brief A Function to resume paused task using its instance + * @note Calling this function for already non-waiting task has no effect. + * + * @param cc_os_task[in] pointer to the TASK_instance; use CC_GET_TASK_INST(Name) to get the Defined Task; + * Pass NULL to point to current task + * @return status_t + */ +status_t cc_os_resume_task(cc_os_task_t *cc_os_task); + +/** + * @brief A Function to pause all the tasks except the current and the IDLE Task + * @note To resume all please use cc_os_resume_all_task() call + * + * @return status_t + */ +status_t cc_os_pause_all_task(void); + +/** + * @brief A Function to resume all the tasks + * + * @return status_t + */ +status_t cc_os_resume_all_task(void); + +/** + * @brief A function to set CC OS scheduler algorithm + * + * @param sched_algo[in] The algorithm that needs to be selected to + * @return status_t + */ +status_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo); + +/** + * @brief A Function to put the current task to a waiting state and yield + * @note To just Yeild set ticks to 0 + * + * @param ticks[in] Number of CC_OS Ticks + * @return None + */ +void cc_os_task_wait(const size_t ticks); + +/** + * @brief A Function to switch to next available task + * + * @return None + */ +void cc_os_task_yield(void); + +/** + * @brief A Function to invoke the kernel + * + * @return status_t + */ +void cc_os_run(void); diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h index 87c025d7..7e0cbb97 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_heap.h @@ -1,3 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : cc_os_heap.h + * Description : CC OS Heap declaration (Placeholder) + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #include #include #include diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h index d686ee72..539c3d8e 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_sched.h + * File Name : cc_os_sched.h * Description : CC OS Kernel scheduler declaration * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team diff --git a/src/visor/terravisor/services/kernel/cc_os_heap.c b/src/visor/terravisor/services/kernel/cc_os_heap.c index c063d2f7..a4683201 100644 --- a/src/visor/terravisor/services/kernel/cc_os_heap.c +++ b/src/visor/terravisor/services/kernel/cc_os_heap.c @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_os.h + * File Name : cc_os_heap.c * Description : CC OS Kernel definations * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index a6629c1a..e7cf7586 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_sched.c + * File Name : cc_os_sched.c * Description : CC OS Kernel scheduler definations * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c index 49690df7..d3055df9 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sem.c +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -30,7 +30,7 @@ * USER FUNCTION DEFINATIONS *****************************************************/ -cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val) +status_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val) { #if CC_OS_DYNAMIC == CC_OS_FALSE CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init == CC_OS_FALSE)); @@ -53,7 +53,7 @@ cc_os_err_t cc_os_sem_create (sem_t ** sem_ptr, size_t init_val) #endif return success; } -cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) +status_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); @@ -74,7 +74,7 @@ cc_os_err_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) } return success; } -cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) +status_t cc_os_sem_give (sem_t * sem_ptr) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); @@ -82,7 +82,7 @@ cc_os_err_t cc_os_sem_give (sem_t * sem_ptr) return success; } -cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr) +status_t cc_os_sem_delete (sem_t ** sem_ptr) { CC_OS_ASSERT_IF_FALSE((*sem_ptr != CC_OS_NULL_PTR && (*sem_ptr)->sem_init != CC_OS_FALSE)); @@ -94,7 +94,7 @@ cc_os_err_t cc_os_sem_delete (sem_t ** sem_ptr) return success; } -cc_os_err_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val) +status_t cc_os_sem_get_val (const sem_t * sem_ptr, size_t * val) { CC_OS_ASSERT_IF_FALSE((sem_ptr != CC_OS_NULL_PTR && sem_ptr->sem_init != CC_OS_FALSE)); CC_OS_ASSERT_IF_FALSE(val != CC_OS_NULL_PTR); diff --git a/src/visor/terravisor/services/kernel/cc_os_idle.c b/src/visor/terravisor/services/kernel/cc_os_task_idle.c similarity index 98% rename from src/visor/terravisor/services/kernel/cc_os_idle.c rename to src/visor/terravisor/services/kernel/cc_os_task_idle.c index 0d47ee88..fe416d28 100644 --- a/src/visor/terravisor/services/kernel/cc_os_idle.c +++ b/src/visor/terravisor/services/kernel/cc_os_task_idle.c @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_os_idle.c + * File Name : cc_os_task_idle.c * Description : CC OS IDLE Task definitions * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team diff --git a/src/visor/terravisor/services/kernel/cc_os.c b/src/visor/terravisor/services/kernel/cc_os_tasks.c similarity index 94% rename from src/visor/terravisor/services/kernel/cc_os.c rename to src/visor/terravisor/services/kernel/cc_os_tasks.c index d8cfb8b3..aa31b04c 100644 --- a/src/visor/terravisor/services/kernel/cc_os.c +++ b/src/visor/terravisor/services/kernel/cc_os_tasks.c @@ -2,8 +2,8 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : cc_os.h - * Description : CC OS Kernel definations + * File Name : cc_os_tasks.c + * Description : CC OS Kernel tasks related definations definations * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team */ @@ -58,7 +58,7 @@ void __cc_init_scheduler() /***************************************************** * USER FUNCTION DEFINATIONS *****************************************************/ -cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) +status_t cc_os_add_task (cc_os_task_t * cc_os_task) { CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); CC_OS_ASSERT_IF_FALSE(cc_os_task->name != CC_OS_NULL_PTR); @@ -161,7 +161,7 @@ cc_os_err_t cc_os_add_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) +status_t cc_os_del_task (cc_os_task_t * cc_os_task) { CC_OS_ASSERT_IF_FALSE(cc_os_task->task_fn != _cc_os_idle_task_fn); @@ -187,7 +187,7 @@ cc_os_err_t cc_os_del_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) +status_t cc_os_pause_task (cc_os_task_t * cc_os_task) { cc_sched_tcb_t * ptr = g_sched_ctrl.curr_task; if (cc_os_task != CC_OS_NULL_PTR) @@ -200,7 +200,7 @@ cc_os_err_t cc_os_pause_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t cc_os_pause_all_task (void) +status_t cc_os_pause_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; @@ -218,7 +218,7 @@ cc_os_err_t cc_os_pause_all_task (void) return success; } -cc_os_err_t cc_os_resume_all_task (void) +status_t cc_os_resume_all_task (void) { cc_sched_tcb_t * ptr = g_sched_ctrl.ready_list_head->ready_link.next; if (ptr != CC_OS_NULL_PTR) @@ -241,7 +241,7 @@ cc_os_err_t cc_os_resume_all_task (void) return success; } -cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) +status_t cc_os_resume_task (cc_os_task_t * cc_os_task) { CC_OS_ASSERT_IF_FALSE(cc_os_task != CC_OS_NULL_PTR); @@ -252,7 +252,7 @@ cc_os_err_t cc_os_resume_task (cc_os_task_t * cc_os_task) return success; } -cc_os_err_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) +status_t set_cc_os_sched_algo(cc_sched_algo_t sched_algo) { CC_OS_ASSERT_IF_FALSE(sched_algo != cc_sched_algo_max); From 26f44295421f22794f129d38cdf7fa4781a51ca4 Mon Sep 17 00:00:00 2001 From: Akash Kollipara <24636040+akashkollipara@users.noreply.github.com> Date: Wed, 28 Dec 2022 17:26:26 +0530 Subject: [PATCH 25/38] Adding code owners list. --- .github/CODEOWNERS | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 00000000..b8981574 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,22 @@ +# Cyancore root +/* @visorfolks/cc-arch-board + +# Akash Kollipara owns +/mk/* @akashkollipara +/src/arch/avr/* @akashkollipara +/src/arch/riscv/* @akashkollipara +/src/platform/mega_avr/* @akashkollipara +/src/platform/sifive/* @akashkollipara +/src/driver/* @akashkollipara +/src/lib/* @akashkollipara +/src/inlcude/* @akashkollipara +/src/visor/* @akashkollipara + +# Mayuri Lokhande owns +/src/arch/arm/* @MayuriLokhande +/src/platform/pico/* @MayuriLokhande +/src/visor/supervisor/* @MayuriLokhande + +# Pranjal Chanda owns +/src/visor/* @pranjalchanda08 +/src/lib/libposix/* @pranjalchanda08 From d9543fbf84bde54ebfc921dc81aa8fee19789202 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 3 Jun 2022 13:14:06 +0530 Subject: [PATCH 26/38] Add dir structure to cc arch - Created common for v6 and v7 - m0, m0p (mo+), m4 cpu placeholders --- src/arch/arm/32/common_v6/supervisor/build.mk | 0 src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S | 0 src/arch/arm/32/common_v6_v7/supervisor/asm.S | 0 src/arch/arm/32/common_v6_v7/supervisor/build.mk | 0 src/arch/arm/32/common_v6_v7/supervisor/entry.S | 0 src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c | 0 src/arch/arm/32/common_v6_v7/supervisor/init.c | 0 src/arch/arm/32/common_v6_v7/supervisor/systick.c | 0 src/arch/arm/32/common_v7/supervisor/build.mk | 0 src/arch/arm/32/m0/supervisor/build.mk | 0 src/arch/arm/32/m0p/supervisor/build.mk | 0 src/arch/arm/32/m4/supervisor/build.mk | 0 12 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/arch/arm/32/common_v6/supervisor/build.mk create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/asm.S create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/build.mk create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/entry.S create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/init.c create mode 100644 src/arch/arm/32/common_v6_v7/supervisor/systick.c create mode 100644 src/arch/arm/32/common_v7/supervisor/build.mk create mode 100644 src/arch/arm/32/m0/supervisor/build.mk create mode 100644 src/arch/arm/32/m0p/supervisor/build.mk create mode 100644 src/arch/arm/32/m4/supervisor/build.mk diff --git a/src/arch/arm/32/common_v6/supervisor/build.mk b/src/arch/arm/32/common_v6/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S b/src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/asm.S b/src/arch/arm/32/common_v6_v7/supervisor/asm.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/build.mk b/src/arch/arm/32/common_v6_v7/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/entry.S b/src/arch/arm/32/common_v6_v7/supervisor/entry.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c b/src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/init.c b/src/arch/arm/32/common_v6_v7/supervisor/init.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v6_v7/supervisor/systick.c b/src/arch/arm/32/common_v6_v7/supervisor/systick.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/common_v7/supervisor/build.mk b/src/arch/arm/32/common_v7/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/m0/supervisor/build.mk b/src/arch/arm/32/m0/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/m0p/supervisor/build.mk b/src/arch/arm/32/m0p/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32/m4/supervisor/build.mk b/src/arch/arm/32/m4/supervisor/build.mk new file mode 100644 index 00000000..e69de29b From 1eebb600f7fabe252f6b887633883fa8acb2c924 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 6 Jun 2022 20:22:10 +0530 Subject: [PATCH 27/38] Updated dir readmes --- src/arch/README.md | 6 ++++-- src/arch/riscv/32/README.md | 7 +++++++ src/arch/riscv/32/a/README.md | 6 ++++++ src/arch/riscv/32/i/README.md | 7 +++++++ src/arch/riscv/32/i/terravisor/README.md | 10 ++++++++++ src/arch/riscv/32/imac/README.md | 10 ++++++++++ src/arch/riscv/32/imac/terravisor/README.md | 7 +++++++ src/arch/riscv/README.md | 4 ++++ 8 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/arch/riscv/32/README.md create mode 100644 src/arch/riscv/32/a/README.md create mode 100644 src/arch/riscv/32/i/README.md create mode 100644 src/arch/riscv/32/i/terravisor/README.md create mode 100644 src/arch/riscv/32/imac/README.md create mode 100644 src/arch/riscv/32/imac/terravisor/README.md create mode 100644 src/arch/riscv/README.md diff --git a/src/arch/README.md b/src/arch/README.md index 492dc9e8..7c062598 100644 --- a/src/arch/README.md +++ b/src/arch/README.md @@ -2,7 +2,9 @@ Home directory for CPU architecture specific sources. +> Note : The folder structure is intentionally kept with the current resolution. **Do not modify it!** + #### Supported Architectures * AVR -* ARM [Coming soon] -* RISCV [Coming soon] +* ARM +* RISCV diff --git a/src/arch/riscv/32/README.md b/src/arch/riscv/32/README.md new file mode 100644 index 00000000..84d5e823 --- /dev/null +++ b/src/arch/riscv/32/README.md @@ -0,0 +1,7 @@ +# RV32 + +Home directory for RV32 CPU + +#### Supported Variants +* RV32I +* RV32IMAC diff --git a/src/arch/riscv/32/a/README.md b/src/arch/riscv/32/a/README.md new file mode 100644 index 00000000..a5def478 --- /dev/null +++ b/src/arch/riscv/32/a/README.md @@ -0,0 +1,6 @@ +# RV32 A-Extension +_RISC-V Atomic extension support_ + +* This is a generic atomic extension layer. +* This directory consists of sources that involve use of atomic instructions. +* If any other directory uses same function or has same intended funcationality, make sure to use "weak" function definitions. diff --git a/src/arch/riscv/32/i/README.md b/src/arch/riscv/32/i/README.md new file mode 100644 index 00000000..4522fde5 --- /dev/null +++ b/src/arch/riscv/32/i/README.md @@ -0,0 +1,7 @@ +# RV32 I-Extension (baseline) + +* Supports machine, supervisor and undermode as of the commit date. +* Hypervisor aka background supervisor spec is still under development. + +#### Supported execution levels +* Terravisor (machine layer) diff --git a/src/arch/riscv/32/i/terravisor/README.md b/src/arch/riscv/32/i/terravisor/README.md new file mode 100644 index 00000000..96d3a604 --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/README.md @@ -0,0 +1,10 @@ +# RV32 I [Terravisor] + +* Home directory for RV32 Terravisor sources. +* I being baseline for RV cores, this is a common directory. +* For terravisor documentation please read [Terravisor README](../../../../../visor/terravisor/README.md) + +#### Highlights +* Bootstrap routine is mostly in C +* Reduced use of ASM +* Exception handling is unified by updated "mtvec" reg with handler address diff --git a/src/arch/riscv/32/imac/README.md b/src/arch/riscv/32/imac/README.md new file mode 100644 index 00000000..dc7a4e99 --- /dev/null +++ b/src/arch/riscv/32/imac/README.md @@ -0,0 +1,10 @@ +# RV32 IMAC-Extension + +#### Supported Extensions +* RV32I baseline +* M - Hardware multiplication +* A - Hardware atomic +* C - Compressed instructions + +#### Supported execution level +* Terravisor diff --git a/src/arch/riscv/32/imac/terravisor/README.md b/src/arch/riscv/32/imac/terravisor/README.md new file mode 100644 index 00000000..34a1b42f --- /dev/null +++ b/src/arch/riscv/32/imac/terravisor/README.md @@ -0,0 +1,7 @@ +# RV32 IMAC [Terravisor] + +Home directory for RV32imac Terravisor + +For terravisor documentation please read [Terravisor README](../../../../../visor/terravisor/README.md) + +Please refer [README.md](../../i/terravisor/README.md) in i/terravisor directory. diff --git a/src/arch/riscv/README.md b/src/arch/riscv/README.md new file mode 100644 index 00000000..6feade22 --- /dev/null +++ b/src/arch/riscv/README.md @@ -0,0 +1,4 @@ +# RISC-V Core + +#### Supported RISC-V Cores +* 32Bit From 74a46ccc3ddcab0e0936e349ed4d652c99803d4a Mon Sep 17 00:00:00 2001 From: Mayuri Lokhande Date: Tue, 25 Jan 2022 23:02:15 +0530 Subject: [PATCH 28/38] Add initial port for RPi pico --- src/platform/pico/r2040/build.mk | 26 ++++++++++++++++++++++++++ src/projects/demo_rpipico/build.mk | 25 +++++++++++++++++++++++++ src/projects/demo_rpipico/config.mk | 15 +++++++++++++++ src/projects/demo_rpipico/project.c | 21 +++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 src/platform/pico/r2040/build.mk create mode 100644 src/projects/demo_rpipico/build.mk create mode 100644 src/projects/demo_rpipico/config.mk create mode 100644 src/projects/demo_rpipico/project.c diff --git a/src/platform/pico/r2040/build.mk b/src/platform/pico/r2040/build.mk new file mode 100644 index 00000000..095f6096 --- /dev/null +++ b/src/platform/pico/r2040/build.mk @@ -0,0 +1,26 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates the build scripts from +# all other directories that have RPi-pico R2040 +# board support sources +# Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] +# Organisation : Cyancore Core-Team +# + +R2040_DIR := $(GET_PATH) + +ARCH := arm-m +BIT := 32 +ARCH_VARIANT := v6 +TARGET_FLAGS += +PLAT_INCLUDE += $(R2040_DIR)/include +OUTPUT_FORMAT := + +#include $(R2040_DIR)/config.mk +#include $(R2040_DIR)/arch/build.mk +#include $(R2040_DIR)/platform/build.mk +#include $(R2040_DIR)/resources/build.mk +#include $(R2040_DIR)/../common_fe310/build.mk diff --git a/src/projects/demo_rpipico/build.mk b/src/projects/demo_rpipico/build.mk new file mode 100644 index 00000000..ba788310 --- /dev/null +++ b/src/projects/demo_rpipico/build.mk @@ -0,0 +1,25 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file build project sources and specifies +# project properties +# Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] +# Organisation : Cyancore Core-Team +# + +PROJECT_DIR := $(GET_PATH) + +OPTIMIZATION := s + +EXE_MODE := supervisor + +include $(PROJECT_DIR)/config.mk + +# You can add another folder with necessary files to build the project +# The below path should be valid +# include $(PROJECT_DIR)//build.mk + +DIR := $(PROJECT_DIR) +include mk/obj.mk diff --git a/src/projects/demo_rpipico/config.mk b/src/projects/demo_rpipico/config.mk new file mode 100644 index 00000000..508e124a --- /dev/null +++ b/src/projects/demo_rpipico/config.mk @@ -0,0 +1,15 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : config.mk +# Description : This file consists of project config +# Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] +# Organisation : Cyancore Core-Team +# + +COMPILER := gcc +TC_VER := 7.3.1 +FAMILY := pico +PLATFORM := r2040 +EARLYCON_MEMBUF := 0 diff --git a/src/projects/demo_rpipico/project.c b/src/projects/demo_rpipico/project.c new file mode 100644 index 00000000..39573e74 --- /dev/null +++ b/src/projects/demo_rpipico/project.c @@ -0,0 +1,21 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : project.c + * Description : This file consists of project srouces + * Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] + * Organisation : Cyancore Core-Team + */ + +void plug() +{ + /* < ! > Plug in one time setup code */ + return; +} + +void play() +{ + /* < ! > Play looping code here*/ + return; +} From 9ffb8a0c020eda37d8d83c4beabcca197b5a71ca Mon Sep 17 00:00:00 2001 From: Mayuri Lokhande Date: Fri, 16 Sep 2022 20:24:57 +0530 Subject: [PATCH 29/38] [R2040] Add initial support for platform --- src/platform/pico/common/sections.ld.sx | 97 ++++++++++++++++++++++ src/platform/pico/r2040/include/plat_mem.h | 30 +++++++ 2 files changed, 127 insertions(+) create mode 100644 src/platform/pico/common/sections.ld.sx create mode 100644 src/platform/pico/r2040/include/plat_mem.h diff --git a/src/platform/pico/common/sections.ld.sx b/src/platform/pico/common/sections.ld.sx new file mode 100644 index 00000000..7097b805 --- /dev/null +++ b/src/platform/pico/common/sections.ld.sx @@ -0,0 +1,97 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : sections.ld.sx + * Description : This file contains memory layout for the + * cyancore framework + * Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] + * Organisation : Cyancore Core-Team + */ + + #include + + + #ifndef L_MEM_START + #define L_MEM_START + #endif + + MEMORY + { + /* VM Addresses */ + vma_mem : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH + + /* LM Addresses */ + lma_mem : ORIGIN = L_MEM_START, LENGTH = IMEM_LENGTH + + /* Stack Addresses */ + v_smem : ORIGIN = V_SMEM_START, LENGTH = SMEM_LENGTH + + } + + SECTIONS + { + .text : + { + . = ALIGN(ALIGN_BOUND); + *(.archvectors) + KEEP(*(.archvectors)) + *(.platvectors) + KEEP(*(.platvectors)) + . = ALIGN(ALIGN_BOUND); + (*.text) + (*.text.*) + KEEP(*(.text)) + } > vma_mem AT > lma_mem + + .bss : + { + . = ALIGN(ALIGN_BOUND) + *(.bss) + *(.bss.*) + KEEP(*(.bss)) + *(COMMON) + } > vma_mem + + .data : + { + . = ALIGN(ALIGN_BOUND) + *(.version) + KEEP(*(.version)) + *(.rodata) + *(.rodata.*) + KEEP(*(.rodata)) + *(.data) + *(.data.*) + KEEP(*(.data)) + } > vma_mem + + .driver_table : {} > vma_mem AT > lma_mem + .mcall_table : {} > vma_mem AT > lma_mem + + .stack : + { + . = ALIGN(ALIGN_BOUND); + *(.stack) + KEEP(*(.stack)) + . = . + STACK_SIZE; + } > vma_mem + + PROVIDE(_data_start = LOADADDR(.data)); + PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.driver_table) + SIZEOF(.mcall_table)); + PROVIDE(_data_vstart = ADDR(.data)); + PROVIDE(_data_vend = _data_vstart + _data_size); + + PROVIDE(_bss_start = ADDR(.bss)); + PROVIDE(_bss_size = SIZEOF(.bss)); + PROVIDE(_flash_size = _data_size + SIZEOF(.text)); + PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); + + ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") + ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") + + /DISCARD/ : { *(.comment .trampolines) } +} + + + } diff --git a/src/platform/pico/r2040/include/plat_mem.h b/src/platform/pico/r2040/include/plat_mem.h new file mode 100644 index 00000000..3b8d287f --- /dev/null +++ b/src/platform/pico/r2040/include/plat_mem.h @@ -0,0 +1,30 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : plat_mem.h + * Description : This file contains memory config of fe310-g002 + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#define FLASH_SIZE 16M +#define RAM_SIZE 264K + +#define V_IMEM_START 0x10000000 +#define V_DMEM_START 0x20000000 +#define V_SMEM_START 0x20040000 + +#define L_MEM_START 0x10000000 +#define L_MEM_LENGTH 16M + +#define DMEM_LENGTH 256K +#define SMEM_LENGTH 8K +#define IMEM_LENGTH 16M + +#define ALIGN_BOUND 4 + +#define STACK_SIZE 0xc00 +#define STACK_SIZE_PCPU 4K From fe8689c8ccfde307ce7410fb5cb0359c1e23aa99 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 15 Oct 2022 16:45:34 +0530 Subject: [PATCH 30/38] Directory refactored - There are many difference between M and A profile hence refactoring the directory For Example: Stack pointer/location is present as part of vector table in ARM-M where as SP needs to be assigned during boot in ARM-A --- src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S | 0 src/arch/arm/32m/common_v6_v7/supervisor/asm.S | 0 src/arch/arm/32m/common_v6_v7/supervisor/build.mk | 0 src/arch/arm/32m/common_v6_v7/supervisor/entry.S | 0 src/arch/arm/32m/common_v6_v7/supervisor/exception_handler.c | 0 src/arch/arm/32m/common_v6_v7/supervisor/init.c | 0 src/arch/arm/32m/common_v6_v7/supervisor/systick.c | 0 src/arch/arm/32m/common_v7/supervisor/build.mk | 0 src/arch/arm/32m/m0/supervisor/build.mk | 0 src/arch/arm/32m/m0p/supervisor/build.mk | 0 src/arch/arm/32m/m4/supervisor/build.mk | 0 11 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/asm.S create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/build.mk create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/entry.S create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/exception_handler.c create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/init.c create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/systick.c create mode 100644 src/arch/arm/32m/common_v7/supervisor/build.mk create mode 100644 src/arch/arm/32m/m0/supervisor/build.mk create mode 100644 src/arch/arm/32m/m0p/supervisor/build.mk create mode 100644 src/arch/arm/32m/m4/supervisor/build.mk diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S b/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/asm.S b/src/arch/arm/32m/common_v6_v7/supervisor/asm.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/build.mk b/src/arch/arm/32m/common_v6_v7/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/entry.S b/src/arch/arm/32m/common_v6_v7/supervisor/entry.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/exception_handler.c b/src/arch/arm/32m/common_v6_v7/supervisor/exception_handler.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/init.c b/src/arch/arm/32m/common_v6_v7/supervisor/init.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/systick.c b/src/arch/arm/32m/common_v6_v7/supervisor/systick.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/common_v7/supervisor/build.mk b/src/arch/arm/32m/common_v7/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/m0/supervisor/build.mk b/src/arch/arm/32m/m0/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/m0p/supervisor/build.mk b/src/arch/arm/32m/m0p/supervisor/build.mk new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/arm/32m/m4/supervisor/build.mk b/src/arch/arm/32m/m4/supervisor/build.mk new file mode 100644 index 00000000..e69de29b From a4b447f4ec8668a0c8b133408f9097e92ffca2d5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 18 Oct 2022 23:37:35 +0530 Subject: [PATCH 31/38] Added arch vectors for v6, v7 - Updated arch vectors for v6 and v7 Cortex M CPUs - Removed old and redundant files --- .../common_v6_v7/supervisor/arch_vectors.S | 0 .../common_v6_v7/supervisor/arch_vectors.c | 61 +++++++++++++++++++ .../arm/32m/common_v6_v7/supervisor/entry.S | 0 3 files changed, 61 insertions(+) delete mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S create mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.c delete mode 100644 src/arch/arm/32m/common_v6_v7/supervisor/entry.S diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S b/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.c b/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.c new file mode 100644 index 00000000..f02f765c --- /dev/null +++ b/src/arch/arm/32m/common_v6_v7/supervisor/arch_vectors.c @@ -0,0 +1,61 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : arch_verctors.c + * Description : This file consists of array of vector table + * specific to arm-m proile. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include + +extern uint8_t _start_start; +extern void init(void); +#define proto_irq_func(x) extern void int_##x(void) +proto_irq_func(1); +proto_irq_func(2); +proto_irq_func(3); +proto_irq_func(4); +proto_irq_func(5); +proto_irq_func(6); +proto_irq_func(7); +proto_irq_func(8); +proto_irq_func(9); +proto_irq_func(10); +proto_irq_func(11); +proto_irq_func(12); +proto_irq_func(13); +proto_irq_func(14); + +/** + * arch_vectors - Interrupt vector table defined as per arm-m spec + * @brief This is the array of interrupt/exception vectors defined + * by arm-v6/v7 spec. This is generic implementation and upon any + * irq/excep, these function will call common handler which will + * deref based on irq id and call respective handler. + */ + +const void (*arch_vectors[N_IRQ+1](void)) _SECTION(".archvectors") = +[ + &_stack_start, // Stack start value has higher address of stack + // with as assumption that stack grows towards + // lower address + + &init, // CPU entry address + &int_1, // IRQ 1 -> NMI + &int_2, // IRQ 2 -> HardFault + &int_3, // IRQ 3 -> M-Manage + &int_4, // IRQ 4 -> BusFault + &int_5, // IRQ 5 -> UsageFault + &int_6, // IRQ 6 -> N/A + &int_7, // IRQ 7 -> DBG info + &int_8, // IRQ 8 -> Debugger handler + &int_9, // IRQ 9 -> N/A + &int_10, // IRQ 10 -> SVC + &int_11, // IRQ 11 -> Debug Monitor + &int_12, // IRQ 12 -> N/A + &int_13, // IRQ 13 -> PendSV + &int_14, // IRQ 14 -> SysTick +]; diff --git a/src/arch/arm/32m/common_v6_v7/supervisor/entry.S b/src/arch/arm/32m/common_v6_v7/supervisor/entry.S deleted file mode 100644 index e69de29b..00000000 From f667c7ea65b3f8a59a5a29f0ec57a307e2f5080d Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 8 Nov 2022 17:05:30 +0530 Subject: [PATCH 32/38] Removing redundant files --- src/arch/arm/32/common_v6/supervisor/build.mk | 0 src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S | 0 src/arch/arm/32/common_v6_v7/supervisor/asm.S | 0 src/arch/arm/32/common_v6_v7/supervisor/build.mk | 0 src/arch/arm/32/common_v6_v7/supervisor/entry.S | 0 src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c | 0 src/arch/arm/32/common_v6_v7/supervisor/init.c | 0 src/arch/arm/32/common_v6_v7/supervisor/systick.c | 0 src/arch/arm/32/common_v7/supervisor/build.mk | 0 src/arch/arm/32/m0/supervisor/build.mk | 0 src/arch/arm/32/m0p/supervisor/build.mk | 0 src/arch/arm/32/m4/supervisor/build.mk | 0 12 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/arch/arm/32/common_v6/supervisor/build.mk delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/asm.S delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/build.mk delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/entry.S delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/init.c delete mode 100644 src/arch/arm/32/common_v6_v7/supervisor/systick.c delete mode 100644 src/arch/arm/32/common_v7/supervisor/build.mk delete mode 100644 src/arch/arm/32/m0/supervisor/build.mk delete mode 100644 src/arch/arm/32/m0p/supervisor/build.mk delete mode 100644 src/arch/arm/32/m4/supervisor/build.mk diff --git a/src/arch/arm/32/common_v6/supervisor/build.mk b/src/arch/arm/32/common_v6/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S b/src/arch/arm/32/common_v6_v7/supervisor/arch_vectors.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/asm.S b/src/arch/arm/32/common_v6_v7/supervisor/asm.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/build.mk b/src/arch/arm/32/common_v6_v7/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/entry.S b/src/arch/arm/32/common_v6_v7/supervisor/entry.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c b/src/arch/arm/32/common_v6_v7/supervisor/exception_handler.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/init.c b/src/arch/arm/32/common_v6_v7/supervisor/init.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v6_v7/supervisor/systick.c b/src/arch/arm/32/common_v6_v7/supervisor/systick.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/common_v7/supervisor/build.mk b/src/arch/arm/32/common_v7/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/m0/supervisor/build.mk b/src/arch/arm/32/m0/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/m0p/supervisor/build.mk b/src/arch/arm/32/m0p/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/arm/32/m4/supervisor/build.mk b/src/arch/arm/32/m4/supervisor/build.mk deleted file mode 100644 index e69de29b..00000000 From 9f509bf85661e7b5b3e95f1988fee7f0f9807cce Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 28 Dec 2022 17:29:41 +0530 Subject: [PATCH 33/38] Removing projects --- src/projects/demo_rpipico/build.mk | 25 ------------------------- src/projects/demo_rpipico/config.mk | 15 --------------- src/projects/demo_rpipico/project.c | 21 --------------------- 3 files changed, 61 deletions(-) delete mode 100644 src/projects/demo_rpipico/build.mk delete mode 100644 src/projects/demo_rpipico/config.mk delete mode 100644 src/projects/demo_rpipico/project.c diff --git a/src/projects/demo_rpipico/build.mk b/src/projects/demo_rpipico/build.mk deleted file mode 100644 index ba788310..00000000 --- a/src/projects/demo_rpipico/build.mk +++ /dev/null @@ -1,25 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file build project sources and specifies -# project properties -# Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] -# Organisation : Cyancore Core-Team -# - -PROJECT_DIR := $(GET_PATH) - -OPTIMIZATION := s - -EXE_MODE := supervisor - -include $(PROJECT_DIR)/config.mk - -# You can add another folder with necessary files to build the project -# The below path should be valid -# include $(PROJECT_DIR)//build.mk - -DIR := $(PROJECT_DIR) -include mk/obj.mk diff --git a/src/projects/demo_rpipico/config.mk b/src/projects/demo_rpipico/config.mk deleted file mode 100644 index 508e124a..00000000 --- a/src/projects/demo_rpipico/config.mk +++ /dev/null @@ -1,15 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : config.mk -# Description : This file consists of project config -# Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] -# Organisation : Cyancore Core-Team -# - -COMPILER := gcc -TC_VER := 7.3.1 -FAMILY := pico -PLATFORM := r2040 -EARLYCON_MEMBUF := 0 diff --git a/src/projects/demo_rpipico/project.c b/src/projects/demo_rpipico/project.c deleted file mode 100644 index 39573e74..00000000 --- a/src/projects/demo_rpipico/project.c +++ /dev/null @@ -1,21 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : project.c - * Description : This file consists of project srouces - * Primary Author : Mayuri Lokhande [mayurilokhande01@gmail.com] - * Organisation : Cyancore Core-Team - */ - -void plug() -{ - /* < ! > Plug in one time setup code */ - return; -} - -void play() -{ - /* < ! > Play looping code here*/ - return; -} From b630fc64cab6074902dc830b809d5bddfdee0ee2 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 28 Dec 2022 17:42:17 +0530 Subject: [PATCH 34/38] Updated readme --- src/arch/README.md | 2 +- src/arch/arm/32m/README.md | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/arch/arm/32m/README.md diff --git a/src/arch/README.md b/src/arch/README.md index 7c062598..b6d84521 100644 --- a/src/arch/README.md +++ b/src/arch/README.md @@ -6,5 +6,5 @@ Home directory for CPU architecture specific sources. #### Supported Architectures * AVR -* ARM * RISCV +* ARM [Coming Soon] diff --git a/src/arch/arm/32m/README.md b/src/arch/arm/32m/README.md new file mode 100644 index 00000000..057c95b7 --- /dev/null +++ b/src/arch/arm/32m/README.md @@ -0,0 +1,8 @@ +# ARM - 32M + +Home directory for ARM-32M CPU + +#### Supported Variants +* M0 +* M0+ +* M4 From 0480d8131cbe240685b2dfd577c6ae04c18becc4 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Thu, 29 Dec 2022 10:34:34 +0530 Subject: [PATCH 35/38] Bug #176 fixed --- src/include/utils.h | 2 +- src/include/visor/terravisor/cc_os/cc_os.h | 1 + src/include/visor/terravisor/cc_os/cc_os_sem.h | 2 +- .../visor/terravisor/cc_os/utils/cc_os_sched.h | 10 ++++++++-- .../terravisor/services/kernel/cc_os_sched.c | 17 ++++++++++++----- .../terravisor/services/kernel/cc_os_sem.c | 7 +++---- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/include/utils.h b/src/include/utils.h index f06ebe90..3aaaf2b6 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -10,5 +10,5 @@ #pragma once -#define RET_ON_FAIL(condition, status) if(!condition) return status +#define RET_ON_FAIL(condition, status) if(!(condition)) return status #define ROUNDUP_ALIGN(x, align) x += align - (x % align) diff --git a/src/include/visor/terravisor/cc_os/cc_os.h b/src/include/visor/terravisor/cc_os/cc_os.h index 89a89ce0..aaac3656 100644 --- a/src/include/visor/terravisor/cc_os/cc_os.h +++ b/src/include/visor/terravisor/cc_os/cc_os.h @@ -14,6 +14,7 @@ /***************************************************** * INCLUDES *****************************************************/ +#include #include #include diff --git a/src/include/visor/terravisor/cc_os/cc_os_sem.h b/src/include/visor/terravisor/cc_os/cc_os_sem.h index 8eb8526c..e7a3d1e6 100644 --- a/src/include/visor/terravisor/cc_os/cc_os_sem.h +++ b/src/include/visor/terravisor/cc_os/cc_os_sem.h @@ -18,8 +18,8 @@ *****************************************************/ typedef struct sem { - uint8_t sem_init; size_t sem_val; + uint8_t sem_init; }sem_t; /***************************************************** * USER MACROS diff --git a/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h index 539c3d8e..29330d09 100644 --- a/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h +++ b/src/include/visor/terravisor/cc_os/utils/cc_os_sched.h @@ -27,7 +27,7 @@ #define CC_OS_DYNAMIC ccosconfig_CC_OS_USE_DYNAMIC -#define CC_OS_ASSERT_IF_FALSE(con) if(!(con)) return error_func_inval_arg +#define CC_OS_ASSERT_IF_FALSE(con) RET_ON_FAIL(con, error_func_inval_arg) /***************************************************** * TYPEDEFS @@ -50,12 +50,18 @@ typedef struct link cc_sched_tcb_t * next; }link_t; +typedef struct wres +{ + uintptr_t wait_on_resource; ///> Resource on hich the task is waiting on + size_t task_delay_ticks; ///> Time delay in ticks +}wres_t; + struct cc_sched_tcb { char name [ccosconfig_CC_OS_TASK_NAME_LEN + 1]; ///> Name of the Current Task size_t priority; ///> Priority of the task void * stack_ptr; ///> Stack Pointer - size_t task_delay_ticks; ///> Time delay in ticks + wres_t wait_res; ///> Wait Task resource link_t ready_link; ///> Ready Linked List Pointers link_t wait_link; ///> Wait Linked List Pointers cc_sched_task_status_t task_status; ///> Current state of the task diff --git a/src/visor/terravisor/services/kernel/cc_os_sched.c b/src/visor/terravisor/services/kernel/cc_os_sched.c index e7cf7586..9513288c 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sched.c +++ b/src/visor/terravisor/services/kernel/cc_os_sched.c @@ -152,7 +152,7 @@ void _cc_sched_send_to_wait(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr, } if(_insert_before(&(sched_ctrl->wait_list_head), ptr, CC_OS_TRUE) == success) { - ptr->task_delay_ticks = ticks; + ptr->wait_res.task_delay_ticks = ticks; ptr->task_status = cc_sched_task_status_wait; } } @@ -178,7 +178,7 @@ void _cc_sched_send_to_resume(cc_sched_ctrl_t * sched_ctrl, cc_sched_tcb_t * ptr ptr->wait_link.next->wait_link.prev = ptr->wait_link.prev; ptr->wait_link.prev = CC_OS_NULL_PTR; ptr->wait_link.next = CC_OS_NULL_PTR; - ptr->task_delay_ticks = CC_OS_FALSE; + ptr->wait_res.task_delay_ticks = CC_OS_FALSE; ptr->task_status = cc_sched_task_status_ready; } @@ -193,11 +193,18 @@ static void __cc_sched_context_switch(cc_sched_tcb_t * next_task) static void __cc_sched_wait_list_adjustment(cc_sched_ctrl_t * sched_ctrl) { cc_sched_tcb_t * ptr = sched_ctrl->wait_list_head; - + size_t * wait_res = ptr->wait_res.wait_on_resource; while(ptr != CC_OS_NULL_PTR) { - ptr->task_delay_ticks--; /* Tick caliberations required */ - if(ptr->task_delay_ticks == CC_OS_FALSE) + ptr->wait_res.task_delay_ticks--; /* Tick caliberations required */ + + if ((wait_res != CC_OS_NULL_PTR) && (*wait_res == CC_OS_FALSE)) + { + /* The resource is available can can go to ready state */ + ptr->wait_res.task_delay_ticks = CC_OS_FALSE; + ptr->wait_res.wait_on_resource = CC_OS_NULL_PTR; + } + if(ptr->wait_res.task_delay_ticks == CC_OS_FALSE) { _cc_sched_send_to_resume(sched_ctrl, ptr); } diff --git a/src/visor/terravisor/services/kernel/cc_os_sem.c b/src/visor/terravisor/services/kernel/cc_os_sem.c index d3055df9..759fb1b1 100644 --- a/src/visor/terravisor/services/kernel/cc_os_sem.c +++ b/src/visor/terravisor/services/kernel/cc_os_sem.c @@ -11,8 +11,7 @@ /***************************************************** * INCLUDES *****************************************************/ -#include -#include +#include /***************************************************** * GLOBAL/STATIC VARIABLE DECLARATIONS @@ -21,7 +20,7 @@ /***************************************************** * GLOBAL EXTERNS *****************************************************/ - +extern cc_sched_ctrl_t g_sched_ctrl; /***************************************************** * STATIC FUNCTION DEFINATIONS *****************************************************/ @@ -64,7 +63,7 @@ status_t cc_os_sem_take (sem_t * sem_ptr, size_t wait_ticks) return error_os_sem_get; } else - { + { g_sched_ctrl.curr_task->wait_res.wait_on_resource = (uintptr_t) sem_ptr; cc_os_task_wait(wait_ticks); } } From 0428ef8853cd66f4190eb2c50e986a9fd3c58c7e Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 29 Dec 2022 11:53:53 +0530 Subject: [PATCH 36/38] <1.3.2> Updated version --- README.md | 2 +- src/engine/banner.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b948497d..9b63560c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ -> **Version (arch:2 | major:4 | minor:2): 1.3.1** +> **Version (arch:2 | major:4 | minor:2): 1.3.2** [![GitHub CI](https://github.com/VisorFolks/cyancore/actions/workflows/github_ci.yml/badge.svg)](https://github.com/VisorFolks/cyancore/actions/workflows/github_ci.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=VisorFolks_cyancore&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=VisorFolks_cyancore) diff --git a/src/engine/banner.mk b/src/engine/banner.mk index c6b17aed..eb642065 100644 --- a/src/engine/banner.mk +++ b/src/engine/banner.mk @@ -10,7 +10,7 @@ .PHONY: version NAME = Lithium -VERSION = 0x01000301 +VERSION = 0x01000302 $(eval $(call add_define,VERSION)) From 619087294af7e3dbe4bfb1e3efcbf83df65583b5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 29 Dec 2022 12:37:15 +0530 Subject: [PATCH 37/38] Updated owners' rules --- .github/CODEOWNERS | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index b8981574..3b5f7e41 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,22 +1,22 @@ # Cyancore root -/* @visorfolks/cc-arch-board +* @visorfolks/cc-arch-board # Akash Kollipara owns -/mk/* @akashkollipara -/src/arch/avr/* @akashkollipara -/src/arch/riscv/* @akashkollipara -/src/platform/mega_avr/* @akashkollipara -/src/platform/sifive/* @akashkollipara -/src/driver/* @akashkollipara -/src/lib/* @akashkollipara -/src/inlcude/* @akashkollipara -/src/visor/* @akashkollipara +/mk/ @akashkollipara +/src/arch/avr/ @akashkollipara +/src/arch/riscv/ @akashkollipara +/src/platform/mega_avr/ @akashkollipara +/src/platform/sifive/ @akashkollipara +/src/driver/ @akashkollipara +/src/lib/ @akashkollipara +/src/inlcude/ @akashkollipara +/src/visor/ @akashkollipara # Mayuri Lokhande owns -/src/arch/arm/* @MayuriLokhande -/src/platform/pico/* @MayuriLokhande -/src/visor/supervisor/* @MayuriLokhande +/src/arch/arm/ @MayuriLokhande +/src/platform/pico/ @MayuriLokhande +/src/visor/supervisor/ @MayuriLokhande # Pranjal Chanda owns -/src/visor/* @pranjalchanda08 -/src/lib/libposix/* @pranjalchanda08 +/src/visor/ @pranjalchanda08 +/src/lib/libposix/ @pranjalchanda08 From 0525f0f1db809dea395e2e0316280cff77d8752e Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 29 Dec 2022 13:20:50 +0530 Subject: [PATCH 38/38] <1.3.2> Updated codename --- src/engine/banner.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/banner.mk b/src/engine/banner.mk index eb642065..37e4d109 100644 --- a/src/engine/banner.mk +++ b/src/engine/banner.mk @@ -9,7 +9,7 @@ # .PHONY: version -NAME = Lithium +NAME = Beryllium VERSION = 0x01000302 $(eval $(call add_define,VERSION))