From 5119e23740b57c7e684a5feb46eb1d66c2633f2c Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 13 May 2022 23:22:52 +0530 Subject: [PATCH 01/21] Added picotool to build engine of CC - This patch support generation of picotool from sources - Added new folder "misc_bins" for storing all misc binaries used by CC - Added new targets get_picotool, install_pt_dep, pt_clean --- mk/path.mk | 1 + mk/picotool.mk | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ mk/project.mk | 1 + 3 files changed, 52 insertions(+) create mode 100644 mk/picotool.mk diff --git a/mk/path.mk b/mk/path.mk index f6a44b5a..24318b40 100644 --- a/mk/path.mk +++ b/mk/path.mk @@ -15,6 +15,7 @@ CC_ROOT := . TOOLS_ROOT ?= $(abspath $(CC_ROOT))/tools +MISC_TOOLS := $(TOOLS_ROOT)/misc_bins SRC := $(CC_ROOT)/src OUT_PATH ?= $(CC_ROOT)/out diff --git a/mk/picotool.mk b/mk/picotool.mk new file mode 100644 index 00000000..7f47e21d --- /dev/null +++ b/mk/picotool.mk @@ -0,0 +1,50 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2022, Cyancore Team +# +# File Name : picotool.mk +# Description : This is a build script to generate picotool +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +PICO_SDK_GIT := git@github.com:raspberrypi/pico-sdk.git +PICO_TOOL_GIT := git@github.com:raspberrypi/picotool.git +PICO_SDK_PATH := $(TOOLS_ROOT)/pico-sdk/ +PICO_TOOL_PATH := $(TOOLS_ROOT)/picotool/ +PICO_TOOL := $(MISC_TOOLS)/picotool +PICO_TOOL_BUILD := $(PICO_TOOL_PATH)/build/picotool + +ifneq ($(V),1) +SILENT_LOGS := > cbuild.log 2> /dev/null +endif + +T_ALLOWLIST += get_picotool pt_clean install_pt_dep + +get_picotool: $(PICO_TOOL) + +$(PICO_TOOL): $(PICO_TOOL_BUILD) + mkdir -p $(dir $@) + cp $(PICO_TOOL_PATH)/build/picotool $@ + +$(PICO_TOOL_BUILD): $(PICO_SDK_PATH) $(PICO_TOOL_PATH) + @echo "Building picotool ..." + cd $(PICO_TOOL_PATH) && mkdir -p build && \ + cd build && export PICO_SDK_PATH=$(PICO_SDK_PATH) && \ + cmake ../ $(SILENT_LOGS) \ + && make $(SILENT_LOGS) + +$(PICO_SDK_PATH): + @echo "Fetching PICO SDK ..." + git clone $(PICO_SDK_GIT) --quiet $@ + +$(PICO_TOOL_PATH): + @echo "Fetching PICO Tool ..." + git clone $(PICO_TOOL_GIT) --quiet $@ + +install_pt_dep: + @echo "Installing Dependencies ..." + @sudo apt-get install build-essential pkg-config libusb-1.0-0-dev -y -qq > /dev/null + +pt_clean: + rm -rf $(PICO_SDK_PATH) $(PICO_TOOL_PATH) $(PICO_TOOL) diff --git a/mk/project.mk b/mk/project.mk index 34d20db7..402bc36a 100644 --- a/mk/project.mk +++ b/mk/project.mk @@ -16,6 +16,7 @@ include mk/path.mk include mk/mk_helper.mk include mk/qemu.mk +include mk/picotool.mk P_TARGETS += default cyancore check version copy_to_remote clean_remote T_ALLOWLIST += help list clean all_projects From f77e165e7d999b7d99c7bd915718965a98d5be3d Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 14 May 2022 18:05:27 +0530 Subject: [PATCH 02/21] Added support to generate uf2 img --- mk/picotool.mk | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mk/picotool.mk b/mk/picotool.mk index 7f47e21d..813c33c3 100644 --- a/mk/picotool.mk +++ b/mk/picotool.mk @@ -14,14 +14,16 @@ PICO_SDK_PATH := $(TOOLS_ROOT)/pico-sdk/ PICO_TOOL_PATH := $(TOOLS_ROOT)/picotool/ PICO_TOOL := $(MISC_TOOLS)/picotool PICO_TOOL_BUILD := $(PICO_TOOL_PATH)/build/picotool +ELF2UF2 := $(MISC_TOOLS)/elf2uf2 ifneq ($(V),1) SILENT_LOGS := > cbuild.log 2> /dev/null endif +P_TARGETS += elf2uf2 T_ALLOWLIST += get_picotool pt_clean install_pt_dep -get_picotool: $(PICO_TOOL) +get_picotool: $(PICO_TOOL) $(ELF2UF2) $(PICO_TOOL): $(PICO_TOOL_BUILD) mkdir -p $(dir $@) @@ -34,6 +36,19 @@ $(PICO_TOOL_BUILD): $(PICO_SDK_PATH) $(PICO_TOOL_PATH) cmake ../ $(SILENT_LOGS) \ && make $(SILENT_LOGS) +$(ELF2UF2): $(PICO_SDK_PATH) + @echo "Building elf2uf2 ..." + mkdir -p $(MISC_TOOLS)/temp + cd $(MISC_TOOLS)/temp; \ + cmake $(PICO_SDK_PATH)/tools/elf2uf2 2>/dev/null >/dev/null; \ + make >/dev/null 2>/dev/null + cp $(MISC_TOOLS)/temp/elf2uf2 $@ + rm -rf $(MISC_TOOLS)/temp + +elf2uf2: $(ELF2UF2) elf + @echo "Generating $(notdir $(ELF:.elf=.uf2)) ..." + $(ELF2UF2) $(ELF) $(ELF:.elf=.uf2) + $(PICO_SDK_PATH): @echo "Fetching PICO SDK ..." git clone $(PICO_SDK_GIT) --quiet $@ @@ -47,4 +62,4 @@ install_pt_dep: @sudo apt-get install build-essential pkg-config libusb-1.0-0-dev -y -qq > /dev/null pt_clean: - rm -rf $(PICO_SDK_PATH) $(PICO_TOOL_PATH) $(PICO_TOOL) + rm -rf $(PICO_SDK_PATH) $(PICO_TOOL_PATH) $(PICO_TOOL) $(ELF2UF2) From 5313a32092af285ed318324f628304aa430d1617 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 3 Jun 2022 13:34:14 +0530 Subject: [PATCH 03/21] Updated vscode include path --- .vscode/c_cpp_properties.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index bb5c230e..d72ce463 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -3,7 +3,7 @@ { "name": "Cyancore VSCode extension", "includePath": [ - "${workspaceFolder}/**" + "${workspaceFolder}/src/**" ], "intelliSenseMode": "${default}", "compilerPath": "", From 6f13af9caa8459c1edd7ffd765877ed5003a4aec Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 25 Jul 2022 14:42:49 +0530 Subject: [PATCH 04/21] Added ctype functions --- src/lib/libc/ctype.c | 91 ++++++++++++++++++++++++++++++++++++ src/lib/libc/include/ctype.h | 29 ++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/lib/libc/ctype.c create mode 100644 src/lib/libc/include/ctype.h diff --git a/src/lib/libc/ctype.c b/src/lib/libc/ctype.c new file mode 100644 index 00000000..91c8f65c --- /dev/null +++ b/src/lib/libc/ctype.c @@ -0,0 +1,91 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : ctype.h + * Description : This file contains sources of libc-ctypes + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include + +bool isWhitespace(int c) +{ + return ((c == ' ') || (c == '\t')); +} + +bool isSpace(int c) +{ + return (isWhitespace(c) || (c == '\f') || (c == '\n') || + (c == '\r') || (c == '\v')); +} + +bool isLowercase(int c) +{ + return ((c >= 'a') && (c <= 'z')); +} + +bool isUppercase(int c) +{ + return ((c >= 'A') && (c <= 'Z')); +} + +bool isDigit(int c) +{ + return ((c >= '0') && (c <= '9')); +} + +bool isAlpha(int c) +{ + return (isLowercase(c) || isUppercase(c)); +} + +bool isAlphaNumeric(int c) +{ + return (isAlpha(c) || isDigit(c)); +} + +bool isHexDigit(int c) +{ + return (isDigit(c) || ((c >= 'a') && (c <= 'f')) || + ((c >= 'A') && (c <= 'F'))); +} + +bool isGraph(int c) +{ + return ((c > ' ') && (c < 0x7f)); +} + +bool isControl(int c) +{ + return ((c < ' ') || (c == 0x7f)); +} + +bool isPrintable(int c) +{ + return ((c >= 0x20) && (c < 0x7f)); +} +bool isPunct(int c) +{ + return (isGraph(c) && !isAlphaNumeric(c)); +} + +bool isASCII(int c) +{ + return (c < 0x80); +} + +int toLowercase(int c) +{ + if(isUppercase(c)) + return c + ('a' - 'A'); + return c; +} + +int toUppercase(int c) +{ + if(isLowercase(c)) + return c - ('a' - 'A'); + return c; +} diff --git a/src/lib/libc/include/ctype.h b/src/lib/libc/include/ctype.h new file mode 100644 index 00000000..2e013ddb --- /dev/null +++ b/src/lib/libc/include/ctype.h @@ -0,0 +1,29 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : ctype.h + * Description : This file contains prototypes of libc-ctypes + * functions + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#include + +bool isWhitespace(int); +bool isSpace(int); +bool isLowercase(int); +bool isUppercase(int); +bool isDigit(int); +bool isAlpha(int); +bool isAlphaNumeric(int); +bool isHexDigit(int); +bool isGraph(int); +bool isControl(int); +bool isPrintable(int); +bool isPunct(int); +bool isASCII(int); +int toLowercase(int); +int toUppercase(int); From be5804ecf80dbacc7f3c63e895f1852663ae6f26 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 1 Jun 2022 22:37:28 +0530 Subject: [PATCH 05/21] Add support for timer driver in CC - Porting timer driver for FE310 and MegaAVR with this commit. - Adding uinfied interface for all timer calls. - Fixed avr platform panic handler. rjmp instruction is replaced with jmp. --- projects/demo_avr/project.c | 10 +- projects/demo_riscv/config.mk | 2 +- projects/demo_riscv/project.c | 26 +--- src/include/hal/timer.h | 8 ++ src/include/visor/terravisor/timer.h | 26 ++++ src/lib/libc/include/time.h | 6 + src/lib/libc/time.c | 61 ++++++++ src/lib/libresource/include/sp/sp_device.h | 5 +- .../mega_avr/atmega328p/resources/dp.c | 1 + .../mega_avr/atmega328p/resources/sp.c | 10 +- src/platform/mega_avr/common/arch/panic.S | 2 +- .../mega_avr/common/platform/platform_timer.c | 133 ++++++++++++++++++ src/platform/sifive/common_fe310/build.mk | 2 + .../sifive/common_fe310/hal/clint/clint.c | 2 +- .../sifive/common_fe310/platform/plat_timer.c | 129 +++++++++++++++++ src/platform/sifive/fe310g002/resources/dp.c | 8 ++ src/platform/sifive/fe310g002/resources/sp.c | 10 +- src/visor/terravisor/services/build.mk | 3 +- src/visor/terravisor/services/driver/build.mk | 15 ++ src/visor/terravisor/services/driver/timer.c | 96 +++++++++++++ 20 files changed, 518 insertions(+), 37 deletions(-) create mode 100644 src/include/visor/terravisor/timer.h create mode 100644 src/lib/libc/time.c create mode 100644 src/platform/mega_avr/common/platform/platform_timer.c create mode 100644 src/platform/sifive/common_fe310/platform/plat_timer.c create mode 100644 src/visor/terravisor/services/driver/build.mk create mode 100644 src/visor/terravisor/services/driver/timer.c diff --git a/projects/demo_avr/project.c b/projects/demo_avr/project.c index 86123194..0624f86f 100644 --- a/projects/demo_avr/project.c +++ b/projects/demo_avr/project.c @@ -16,6 +16,7 @@ #include #include #include +#include gpio_port_t onboad_led; @@ -33,13 +34,6 @@ void plug() char progress[] = "-\\|/"; -void delay(unsigned long d) -{ - unsigned long c; - for(c = 0; c < d; c++) - asm volatile("nop"); -} - void play() { static unsigned int i = 0; @@ -47,7 +41,7 @@ void play() gpio_pin_toggle(&onboad_led); printf("%c]", progress[(i++) % strlen(progress)]); wdog_hush(); - delay(500000); + mdelay(500); printf("\b\b"); return; } diff --git a/projects/demo_riscv/config.mk b/projects/demo_riscv/config.mk index 398e7798..d8231176 100644 --- a/projects/demo_riscv/config.mk +++ b/projects/demo_riscv/config.mk @@ -9,7 +9,7 @@ # COMPILER := gcc -TC_VER := 8.2.0 +TC_VER := 10.2.0 FAMILY := sifive PLATFORM := fe310g002 STDLOG_MEMBUF := 0 diff --git a/projects/demo_riscv/project.c b/projects/demo_riscv/project.c index 453852c8..1b5b2879 100644 --- a/projects/demo_riscv/project.c +++ b/projects/demo_riscv/project.c @@ -17,19 +17,8 @@ #include #include #include -#include #include - -static unsigned long long t; -static unsigned int ticks = 16384; - -static void test() -{ - arch_di_mtime(); - t = clint_read_time(); - clint_config_tcmp(0, (t + ticks)); - arch_ei_mtime(); -} +#include static gpio_port_t gled, bled; @@ -38,7 +27,6 @@ void plug() bootstrap(); driver_setup_all(); platform_print_cpu_info(); - link_interrupt(int_local, 7, test); gpio_pin_alloc(&gled, PORTA, 19); gpio_pin_alloc(&bled, PORTA, 21); @@ -47,23 +35,23 @@ void plug() gpio_pin_set(&gled); gpio_pin_set(&bled); - t = clint_read_time(); - clint_config_tcmp(0, (t + ticks)); - arch_ei_mtime(); printf("Demo Program!\n"); printf("< ! > Running Blinky ... ["); return; } -char progress[] = "-\\|/"; void play() { - static unsigned int i = 0; + char progress[] = "-\\|/"; + static unsigned char i = 0; printf("%c]", progress[(i++) % strlen(progress)]); + gpio_pin_toggle(&gled); gpio_pin_toggle(&bled); - arch_wfi(); + + mdelay(500); + printf("\b\b"); return; } diff --git a/src/include/hal/timer.h b/src/include/hal/timer.h index e8b08b3d..7631c9c9 100644 --- a/src/include/hal/timer.h +++ b/src/include/hal/timer.h @@ -27,6 +27,14 @@ typedef struct timer_port void (*tmr_handler)(void); } timer_port_t; +typedef enum timer_mode +{ + __timer_mode_null = 0, + oneshot, + periodic, + timer_mode_other = 0xf000 +} timer_mode_t; + status_t timer_setup(const timer_port_t *port, unsigned int mode, unsigned int ps); status_t timer_shutdown(const timer_port_t *port); status_t timer_read(const timer_port_t *port, size_t *value); diff --git a/src/include/visor/terravisor/timer.h b/src/include/visor/terravisor/timer.h new file mode 100644 index 00000000..6ba4dca1 --- /dev/null +++ b/src/include/visor/terravisor/timer.h @@ -0,0 +1,26 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : timer.h + * Description : This file consists of prototypes for terravisor + * timer drivers + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#define _TVISOR_TIMER_T_ + +typedef struct tvisor_timer +{ + uint64_t (*read_ticks)(void); + uint64_t (*read_time)(void); + void (*set_period)(unsigned int); + void (*reg_cb)(void *); +} tvisor_timer_t; + +status_t timer_attach_device(status_t, tvisor_timer_t *); +status_t timer_release_device(); +status_t timer_link_callback(unsigned int, void *); diff --git a/src/lib/libc/include/time.h b/src/lib/libc/include/time.h index 0ef598e9..4b30559b 100644 --- a/src/lib/libc/include/time.h +++ b/src/lib/libc/include/time.h @@ -12,6 +12,7 @@ typedef struct time { + uint8_t cs; uint8_t s; uint8_t m; uint8_t h; @@ -20,3 +21,8 @@ typedef struct time uint8_t yy; uint8_t c; } time_t; + +status_t get_timestamp(uint64_t *); +status_t get_timeticks(uint64_t *); +status_t udelay(uint16_t); +status_t mdelay(uint16_t); diff --git a/src/lib/libc/time.c b/src/lib/libc/time.c new file mode 100644 index 00000000..69712200 --- /dev/null +++ b/src/lib/libc/time.c @@ -0,0 +1,61 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : time.c + * Description : This file contains sources of libc-time + * functions + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include + +status_t udelay(uint16_t d) +{ + status_t ret; + uint64_t tstamp; + uint64_t delay; + + if(d > 2000) + return error_func_inval_arg; + else if(d == 0) + delay = 1; + else + delay = d; + + ret = get_timestamp(&tstamp); + delay += tstamp; + do + { + arch_wfi(); + ret = get_timestamp(&tstamp); + } while(delay >= tstamp); + return ret; +} + +status_t mdelay(uint16_t d) +{ + status_t ret; + uint64_t tstamp; + uint64_t delay; + + if(d > 10000) + return error_func_inval_arg; + else if(d == 0) + delay = 1000; + else + delay = (uint64_t)d * 1000U; + + ret = get_timestamp(&tstamp); + delay += tstamp; + do + { + arch_wfi(); + ret = get_timestamp(&tstamp); + } while(delay >= tstamp); + return ret; +} diff --git a/src/lib/libresource/include/sp/sp_device.h b/src/lib/libresource/include/sp/sp_device.h index 974a30fe..be946313 100644 --- a/src/lib/libresource/include/sp/sp_device.h +++ b/src/lib/libresource/include/sp/sp_device.h @@ -13,8 +13,9 @@ typedef enum { - console_uart = 0x100, - bt_uart = 0x101, + console_uart = 0x100, + bt_uart = 0x101, + sched_timer = 0x200, } sw_devid_t; diff --git a/src/platform/mega_avr/atmega328p/resources/dp.c b/src/platform/mega_avr/atmega328p/resources/dp.c index 39fb623c..485dee9d 100644 --- a/src/platform/mega_avr/atmega328p/resources/dp.c +++ b/src/platform/mega_avr/atmega328p/resources/dp.c @@ -77,6 +77,7 @@ module_t timer0 = .id = timer | 0x00, .baddr = 0x44, .stride = 5, + .clk = 16000000, .interrupt[0] = {int_arch, 14, int_level}, .interrupt[1] = {int_arch, 15, int_level}, .clk_id = 5, diff --git a/src/platform/mega_avr/atmega328p/resources/sp.c b/src/platform/mega_avr/atmega328p/resources/sp.c index 5f9cbc87..64d84527 100644 --- a/src/platform/mega_avr/atmega328p/resources/sp.c +++ b/src/platform/mega_avr/atmega328p/resources/sp.c @@ -18,16 +18,22 @@ swdev_t consoleUart = .hwdev_id = uart }; +swdev_t schedTimer = +{ + .swdev_id = sched_timer, + .hwdev_id = timer | 0, +}; + sw_devid_t terra_devs[] = { - console_uart, + console_uart, sched_timer, }; visor_t terravisor = add_visor_devs(terra_devs); swdev_t *sw_devs[] = { - &consoleUart, + &consoleUart, &schedTimer }; sp_t software_prop = diff --git a/src/platform/mega_avr/common/arch/panic.S b/src/platform/mega_avr/common/arch/panic.S index d2ff423f..2d7b5b06 100644 --- a/src/platform/mega_avr/common/arch/panic.S +++ b/src/platform/mega_avr/common/arch/panic.S @@ -13,4 +13,4 @@ function plat_panic_handler push r24 ldi r24, 254 - rjmp isr + jmp isr diff --git a/src/platform/mega_avr/common/platform/platform_timer.c b/src/platform/mega_avr/common/platform/platform_timer.c new file mode 100644 index 00000000..b38f6789 --- /dev/null +++ b/src/platform/mega_avr/common/platform/platform_timer.c @@ -0,0 +1,133 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : platform_timer.c + * Description : This file contains sources for platform timer apis + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PS 4 +#define PSVALUE 256 + +static void (*tmr_cb)(void); +static timer_port_t tport; +static uint64_t ticks; +static uint64_t cntr; +static module_t *tm; + +static void plat_tmr_isr(void) +{ + cntr += ticks; + if(tmr_cb != NULL) + tmr_cb(); +} + +static void plat_timer_reg_cb(void *cb) +{ + tmr_cb = cb; +} + +static status_t plat_get_timer_prop(void) +{ + mret_t mres; + swdev_t *sp; + hw_devid_t devid; + + arch_machine_call(fetch_sp, sched_timer, 0, 0, &mres); + if(mres.status != success) + { + sysdbg3("%p - sp node could not be found!\n", sched_timer); + return mres.status; + } + sp = (swdev_t *) mres.p; + devid = sp->hwdev_id; + + arch_machine_call(fetch_dp, (devid & 0xff00), (devid & 0xff), 0, &mres); + if(mres.status != success) + { + sysdbg3("Timer Device %d not found!\n", devid); + return mres.status; + } + tm = (module_t *) mres.p; + return success; +} + +static uint64_t plat_get_timer_ticks_msec(uint64_t freq) +{ + uint64_t nt = freq / (2 * PSVALUE); + nt /= 1000; + return nt; +} + +static void plat_timer_set_period(unsigned int p) +{ + uint64_t nt = plat_get_timer_ticks_msec(tm->clk); + ticks = (nt * p) - 1; + tport.value = ticks; + timer_setup(&tport, 2, PS); +} + +static uint64_t plat_read_ticks(void) +{ + return cntr; +} + +static uint64_t plat_read_time(void) +{ + uint64_t stamp = cntr; + stamp <<= 10U; + stamp /= ((tport.value + 1) << 1); + return stamp; +} + +static tvisor_timer_t plat_timer_port = +{ + .read_ticks = &plat_read_ticks, + .read_time = &plat_read_time, + .set_period = &plat_timer_set_period, + .reg_cb = &plat_timer_reg_cb, +}; + +static status_t plat_timer_setup() +{ + status_t ret = success; + ret |= plat_get_timer_prop(); + + tport.port_id = tm->id; + tport.clk_id = tm->clk_id; + tport.baddr = tm->baddr; + tport.stride = tm->stride; + tport.tmr_irq = tm->interrupt[0].id; + tport.tmr_handler = plat_tmr_isr; + + ret |= timer_setup(&tport, 2, PS); + plat_timer_set_period(1); + ret |= timer_attach_device(ret, &plat_timer_port); + + return ret; +} + +static status_t plat_timer_exit(void) +{ + status_t ret = timer_shutdown(&tport); + ticks = 0; + tmr_cb = (void *) 0; + memset(&tport, 0, sizeof(timer_port_t)); + ret |= timer_release_device(); + return ret; +} + +INCLUDE_DRIVER(plat_timer, plat_timer_setup, plat_timer_exit, 0, 1, 1); diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index 9b4d3518..a66c97f0 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -17,6 +17,8 @@ PLAT_INCLUDE += $(FE310_COMMON_DIR)/include LINT_FLAGS += --platform=unix32 +LD_FLAGS += --no-relax + USE_DEFAULT_RESOURCES ?= 1 LOCAL_INTERRUPT_DEVICE ?= 1 PLAT_INTERRUPT_DEVICE ?= 1 diff --git a/src/platform/sifive/common_fe310/hal/clint/clint.c b/src/platform/sifive/common_fe310/hal/clint/clint.c index 5b55fe89..ffaf2bdc 100644 --- a/src/platform/sifive/common_fe310/hal/clint/clint.c +++ b/src/platform/sifive/common_fe310/hal/clint/clint.c @@ -79,4 +79,4 @@ uint64_t clint_read_time() return time_stamp; } -INCLUDE_DRIVER(plat_clint_pcpu, clint_setup, clint_exit, 1, 0, 0); +INCLUDE_DRIVER(plat_clint, clint_setup, clint_exit, 0, 0, 0); diff --git a/src/platform/sifive/common_fe310/platform/plat_timer.c b/src/platform/sifive/common_fe310/platform/plat_timer.c new file mode 100644 index 00000000..ebbf8397 --- /dev/null +++ b/src/platform/sifive/common_fe310/platform/plat_timer.c @@ -0,0 +1,129 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : plat_timer.c + * Description : This file contains sources for platform + * timer + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void (*tmr_cb)(void); +static uint64_t ticks; +static module_t *tm; + +static void plat_tmr_isr(void) +{ + arch_di_mtime(); + uint64_t t = clint_read_time(); + clint_config_tcmp(arch_core_index(), (t + ticks)); + arch_ei_mtime(); + + if(tmr_cb != NULL) + tmr_cb(); +} + +static void plat_timer_reg_cb(void *cb) +{ + arch_di_mtime(); + tmr_cb = cb; + arch_ei_mtime(); +} + +static status_t plat_get_timer_prop(void) +{ + mret_t mres; + swdev_t *sp; + hw_devid_t devid; + + arch_machine_call(fetch_sp, sched_timer, 0, 0, &mres); + if(mres.status != success) + { + sysdbg3("%p - sp node could not be found!\n", sched_timer); + return mres.status; + } + sp = (swdev_t *) mres.p; + devid = sp->hwdev_id; + + arch_machine_call(fetch_dp, (devid & 0xff00), (devid & 0xff), 0, &mres); + if(mres.status != success) + { + sysdbg3("Timer Device %d not found!\n", devid); + return mres.status; + } + tm = (module_t *) mres.p; + return success; +} + +static uint64_t plat_get_timer_ticks_msec(uint64_t freq) +{ + /* Compute ticks needed for 1 msec */ + return (uint64_t)(freq/1000); +} + +static void plat_timer_set_period(unsigned int p) +{ + uint64_t nt; + arch_di_mtime(); + ticks = plat_get_timer_ticks_msec(tm->clk); + ticks *= p; + nt = ticks + clint_read_time(); + clint_config_tcmp(arch_core_index(), nt); + arch_ei_mtime(); +} + +static uint64_t plat_read_time(void) +{ + uint64_t stamp = clint_read_time(); + stamp *= 1000000U; + return (uint64_t)(stamp / tm->clk); +} + +static tvisor_timer_t plat_timer_port = +{ + .read_ticks = &clint_read_time, + .read_time = &plat_read_time, + .set_period = &plat_timer_set_period, + .reg_cb = &plat_timer_reg_cb, +}; + +static status_t plat_timer_setup(void) +{ + status_t ret = success; + irqs_t *irq; + arch_di_mtime(); + + /* This funcition fetches device properties */ + ret |= plat_get_timer_prop(); + + irq = &tm->interrupt[0]; + + /* Link timer isr handle */ + ret |= link_interrupt(irq->module, irq->id, &plat_tmr_isr); + ret |= timer_attach_device(ret, &plat_timer_port); + plat_timer_set_period(1); + return ret; +} + +static status_t plat_timer_exit(void) +{ + arch_di_mtime(); + tmr_cb = (void *) 0; + ticks = 0; + return timer_release_device(); +} + +INCLUDE_DRIVER(plat_timer, plat_timer_setup, plat_timer_exit, 0, 1, 1); diff --git a/src/platform/sifive/fe310g002/resources/dp.c b/src/platform/sifive/fe310g002/resources/dp.c index 2064cd7e..0e918148 100644 --- a/src/platform/sifive/fe310g002/resources/dp.c +++ b/src/platform/sifive/fe310g002/resources/dp.c @@ -85,6 +85,13 @@ module_t aon0 = .stride = 0x1000, }; +module_t timer_core0 = +{ + .id = timer | 0, + .clk = 32768, + .interrupt[0] = {int_local, 7, int_level}, +}; + gpio_module_t *port_list[] = { &port0, @@ -93,6 +100,7 @@ gpio_module_t *port_list[] = module_t *mod_list[] = { &plic0, &uart0, &prci0, &clint0, &aon0, &uart1, + &timer_core0, }; dp_t device_prop = diff --git a/src/platform/sifive/fe310g002/resources/sp.c b/src/platform/sifive/fe310g002/resources/sp.c index e0579d7a..5bef38d3 100644 --- a/src/platform/sifive/fe310g002/resources/sp.c +++ b/src/platform/sifive/fe310g002/resources/sp.c @@ -22,16 +22,22 @@ swdev_t consoleUart = .pmux = &uart0 }; +swdev_t schedTimer = +{ + .swdev_id = sched_timer, + .hwdev_id = timer | 0, +}; + sw_devid_t terra_devs[] = { - console_uart, + console_uart, sched_timer, }; visor_t terravisor = add_visor_devs(terra_devs); swdev_t *sw_devs[] = { - &consoleUart, + &consoleUart, &schedTimer, }; sp_t software_prop = diff --git a/src/visor/terravisor/services/build.mk b/src/visor/terravisor/services/build.mk index 2b7bd18c..c8fd2473 100644 --- a/src/visor/terravisor/services/build.mk +++ b/src/visor/terravisor/services/build.mk @@ -1,6 +1,6 @@ # # CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team +# Copyrights (C) 2022, Cyancore Team # # File Name : build.mk # Description : This file accumulates the build scripts @@ -12,6 +12,7 @@ T_SERVICES := $(GET_PATH) include $(T_SERVICES)/bootstrap/build.mk +include $(T_SERVICES)/driver/build.mk ifeq ($(TERRAKERN),1) include $(T_SERVICES)/kernel/build.mk diff --git a/src/visor/terravisor/services/driver/build.mk b/src/visor/terravisor/services/driver/build.mk new file mode 100644 index 00000000..7d4538c2 --- /dev/null +++ b/src/visor/terravisor/services/driver/build.mk @@ -0,0 +1,15 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2022, Cyancore Team +# +# File Name : build.mk +# Description : This file builds all the drivers associated +# with terravisor +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +T_DRIVER_DIR := $(GET_PATH) + +DIR := $(T_DRIVER_DIR) +include mk/obj.mk diff --git a/src/visor/terravisor/services/driver/timer.c b/src/visor/terravisor/services/driver/timer.c new file mode 100644 index 00000000..f845c0c2 --- /dev/null +++ b/src/visor/terravisor/services/driver/timer.c @@ -0,0 +1,96 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : timer.c + * Description : This file contains sources of terravisor timer + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static tvisor_timer_t *port[N_CORES]; +static bool timer_attached[N_CORES] = {[0 ... N_CORES-1] = false}; +static lock_t tlock[N_CORES]; + +status_t timer_attach_device(status_t dev_status, tvisor_timer_t *ptmr) +{ + status_t ret; + size_t cpu_index = arch_core_index(); + lock_t *lock = &tlock[cpu_index]; + + lock_acquire(lock); + /* Link the driver instance */ + port[cpu_index] = ptmr; + + if(port[cpu_index] != NULL) + { + ret = dev_status; + timer_attached[cpu_index] = true; + } + else + ret = error_device_inval; + lock_release(lock); + return ret; +} + +status_t timer_release_device() +{ + size_t cpu_index = arch_core_index(); + lock_t *lock = &tlock[cpu_index]; + lock_acquire(lock); + port[cpu_index] = NULL; + timer_attached[cpu_index] = false; + lock_release(lock); + return success; +} + +status_t timer_link_callback(unsigned int p, void *cb) +{ + size_t cpu_index = arch_core_index(); + lock_t *lock = &tlock[cpu_index]; + tvisor_timer_t *ptr = port[cpu_index]; + if(!timer_attached[cpu_index]) + return error_driver_init_failed; + lock_acquire(lock); + ptr->set_period(p); + ptr->reg_cb(cb); + lock_release(lock); + return success; +} + +/* This function's prototype is located in libc */ +status_t get_timestamp(uint64_t *t) +{ + size_t cpu_index = arch_core_index(); + lock_t *lock = &tlock[cpu_index]; + tvisor_timer_t *ptr = port[cpu_index]; + if(!timer_attached[cpu_index]) + return error_driver_init_failed; + lock_acquire(lock); + *t = ptr->read_time(); + lock_release(lock); + return success; +} + +/* This function's prototype is located in libc */ +status_t get_timeticks(uint64_t *t) +{ + size_t cpu_index = arch_core_index(); + lock_t *lock = &tlock[cpu_index]; + tvisor_timer_t *ptr = port[cpu_index]; + if(!timer_attached[cpu_index]) + return error_driver_init_failed; + lock_acquire(lock); + *t = ptr->read_ticks(); + lock_release(lock); + return success; +} From aff0d0d0ce93f8643344edf39f07b41d018bc13f Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 20:48:19 +0530 Subject: [PATCH 06/21] Adding sources for implementing heap memory - Adding heap memory allocator. Uses best-fit algorithm. - This is a lite verion and needs to be replaced with full fledged implementation of memory allocator. - This change is being pushed to faciltate drivers with dynamic driver instances. Note: This change is temporary and needs better implementation of the same. --- src/include/visor/terravisor/platform.h | 1 + src/lib/libc/include/stdlib.h | 4 + src/lib/libc/malloc_lite.c | 93 +++++++++++++++++++ .../mega_avr/atmega2560/include/plat_mem.h | 2 + .../mega_avr/atmega328p/include/plat_mem.h | 2 + .../mega_avr/common/platform/platform.c | 1 + src/platform/mega_avr/common/sections.ld.sx | 12 ++- .../sifive/common_fe310/platform/platform.c | 2 +- .../sifive/common_fe310/sections.ld.sx | 12 ++- .../sifive/fe310g002-bl/include/plat_mem.h | 2 + .../sifive/fe310g002/include/plat_mem.h | 2 + 11 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 src/lib/libc/malloc_lite.c diff --git a/src/include/visor/terravisor/platform.h b/src/include/visor/terravisor/platform.h index 889fc077..6719c301 100644 --- a/src/include/visor/terravisor/platform.h +++ b/src/include/visor/terravisor/platform.h @@ -18,3 +18,4 @@ void platform_reset_handler(); void platform_early_setup(); void platform_setup(); void platform_cpu_setup(); +status_t platform_init_heap(); diff --git a/src/lib/libc/include/stdlib.h b/src/lib/libc/include/stdlib.h index f3da8d5d..f6f9368e 100644 --- a/src/lib/libc/include/stdlib.h +++ b/src/lib/libc/include/stdlib.h @@ -11,7 +11,9 @@ #pragma once #define _STDLIB_H_ +#include #include + #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 #define ATEXIT_MAX 1 @@ -19,3 +21,5 @@ void abort(void); int atexit(void (*func)(int)); void exit(int status) _NORETURN; +void *malloc(size_t); +void free(void *); diff --git a/src/lib/libc/malloc_lite.c b/src/lib/libc/malloc_lite.c new file mode 100644 index 00000000..6c1ffb62 --- /dev/null +++ b/src/lib/libc/malloc_lite.c @@ -0,0 +1,93 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : time.c + * Description : This file contains sources of libc-malloc + * functions + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include + +typedef struct chunk +{ + size_t size; + int free; + struct chunk *next; +} chunk_t; + +extern uint8_t _heap_start, _heap_end; + +static uint8_t *arena = &_heap_start; +static chunk_t *freeList; + +static void split(chunk_t *fit_slot, size_t size) +{ + chunk_t *new = (void *)((void *) fit_slot + size + sizeof(chunk_t)); + new->size = fit_slot->size - size - sizeof(chunk_t); + new->free = 1; + new->next = fit_slot->next; + fit_slot->size = size; + fit_slot->free = 0; + fit_slot->next = new; + return; +} + +static void merge() +{ + chunk_t *cur; + cur = freeList; + while(cur->next != NULL) + { + if(cur->free && cur->next->free) + { + cur->size += cur->next->size + sizeof(chunk_t); + cur->next = cur->next->next; + } + cur = cur->next; + } +} + +status_t platform_init_heap() +{ + memset(arena, 0, (size_t)(&_heap_end - &_heap_start)); + freeList = (void *)arena; + freeList->size = (size_t)(&_heap_end - &_heap_start) - sizeof(chunk_t); + freeList->free = 1; + freeList->next = NULL; + return success; +} + +void *malloc(size_t n_bytes) +{ + chunk_t *cur; + + if(!freeList->size) + return NULL; + cur = freeList; + while(cur->size < n_bytes || (!cur->free && cur->next)) + cur = cur->next; + if(cur->size == n_bytes) + cur->free = 0; + else if(cur->size > (n_bytes + sizeof(chunk_t))) + split(cur, n_bytes); + else + return NULL; + return (void *)(++cur); +} + +void free(void *ptr) +{ + if((void *)&_heap_start <= ptr && ptr <= (void *)&_heap_end) + { + chunk_t *cur = ptr; + cur->free = 1; + merge(); + } + return; +} diff --git a/src/platform/mega_avr/atmega2560/include/plat_mem.h b/src/platform/mega_avr/atmega2560/include/plat_mem.h index 314bc884..9f04c458 100644 --- a/src/platform/mega_avr/atmega2560/include/plat_mem.h +++ b/src/platform/mega_avr/atmega2560/include/plat_mem.h @@ -24,3 +24,5 @@ #define ALIGN_BOUND 2 #define STACK_SIZE 1024 + +#define HEAP_SIZE 128 diff --git a/src/platform/mega_avr/atmega328p/include/plat_mem.h b/src/platform/mega_avr/atmega328p/include/plat_mem.h index 63c2e765..4d9fb218 100644 --- a/src/platform/mega_avr/atmega328p/include/plat_mem.h +++ b/src/platform/mega_avr/atmega328p/include/plat_mem.h @@ -24,3 +24,5 @@ #define ALIGN_BOUND 2 #define STACK_SIZE 0x0200 + +#define HEAP_SIZE 128 diff --git a/src/platform/mega_avr/common/platform/platform.c b/src/platform/mega_avr/common/platform/platform.c index b2fa2a2d..7013f991 100644 --- a/src/platform/mega_avr/common/platform/platform.c +++ b/src/platform/mega_avr/common/platform/platform.c @@ -47,6 +47,7 @@ void platform_early_setup() /* Setup platform memories */ ret |= platform_copy_data(); ret |= platform_bss_clear(); + ret |= platform_init_heap(); ret |= platform_resources_setup(); /* Setup memory syslogger */ diff --git a/src/platform/mega_avr/common/sections.ld.sx b/src/platform/mega_avr/common/sections.ld.sx index 422c798f..eb9f55e1 100644 --- a/src/platform/mega_avr/common/sections.ld.sx +++ b/src/platform/mega_avr/common/sections.ld.sx @@ -52,6 +52,14 @@ SECTIONS *(COMMON) } > vma_dmem + .heap : + { + . = ALIGN(8); + *(.heap) + KEEP(*(.heap)) + . = . + HEAP_SIZE; + } > vma_dmem + .data : { . = ALIGN(ALIGN_BOUND); @@ -82,8 +90,10 @@ SECTIONS PROVIDE(_bss_start = ADDR(.bss)); PROVIDE(_bss_size = SIZEOF(.bss)); + PROVIDE(_heap_start = ADDR(.heap)); + PROVIDE(_heap_end = _heap_start + SIZEOF(.heap)); PROVIDE(_flash_size = _data_size + SIZEOF(.text)); - PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); + PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack) + SIZEOF(.heap)); ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c index 5f4b72fe..f8d584bb 100644 --- a/src/platform/sifive/common_fe310/platform/platform.c +++ b/src/platform/sifive/common_fe310/platform/platform.c @@ -5,7 +5,6 @@ #include #include #include -//#include #include #include #include @@ -19,6 +18,7 @@ void platform_early_setup() ret |= platform_copy_data(); ret |= platform_copy_itim(); ret |= platform_bss_clear(); + ret |= platform_init_heap(); ret |= platform_resources_setup(); syslog_stdout_disable(); driver_setup("mslog"); diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index 7dff48b7..4d3a05b2 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -115,6 +115,13 @@ SECTIONS . = . + STACK_SIZE; } > vma_dmem + .heap : ALIGN(8) + { + *(.heap) + KEEP(*(.heap)) + . = . + HEAP_SIZE; + } > vma_dmem + PROVIDE(_data_start = LOADADDR(.static_data_global)); PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.tdata) + SIZEOF(.static_data_global) +\ SIZEOF(.driver_table) + SIZEOF(.mcall_table)); @@ -128,13 +135,16 @@ SECTIONS PROVIDE(_bss_start = ADDR(.bss)); PROVIDE(_bss_size = SIZEOF(.bss) + SIZEOF(.static_bss_global) + 4); + PROVIDE(_heap_start = ADDR(.heap)); + PROVIDE(_heap_end = _heap_start + SIZEOF(.heap)); + PROVIDE(_itim_start = LOADADDR(.itim)); PROVIDE(_itim_size = SIZEOF(.itim)); PROVIDE(_itim_vstart = ADDR(.itim)); PROVIDE(_itim_vsize = _itim_vstart + _itim_size); PROVIDE(_flash_size = _data_size + SIZEOF(.text) + _itim_size + SIZEOF(.rodata)); - PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); + PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack) + SIZEOF(.heap)); ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") diff --git a/src/platform/sifive/fe310g002-bl/include/plat_mem.h b/src/platform/sifive/fe310g002-bl/include/plat_mem.h index fbbc4c6a..d05ac8eb 100644 --- a/src/platform/sifive/fe310g002-bl/include/plat_mem.h +++ b/src/platform/sifive/fe310g002-bl/include/plat_mem.h @@ -28,3 +28,5 @@ #define STACK_SIZE 0xc00 #define STACK_SIZE_PCPU 0xc00 + +#define HEAP_SIZE 512 diff --git a/src/platform/sifive/fe310g002/include/plat_mem.h b/src/platform/sifive/fe310g002/include/plat_mem.h index 2403e73c..31719ae7 100644 --- a/src/platform/sifive/fe310g002/include/plat_mem.h +++ b/src/platform/sifive/fe310g002/include/plat_mem.h @@ -28,3 +28,5 @@ #define STACK_SIZE 0xc00 #define STACK_SIZE_PCPU 0xc00 + +#define HEAP_SIZE 512 From 0bf74c437cfa44bf766d08d1fd981f8b1f04adab Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 22:40:22 +0530 Subject: [PATCH 07/21] Add support for onboard led driver --- src/driver/build.mk | 1 + src/driver/onboardled/build.mk | 14 +++ src/driver/onboardled/onboardled.c | 105 +++++++++++++++++++++ src/include/driver/onboardled.h | 17 ++++ src/lib/libresource/include/sp/sp_device.h | 1 + 5 files changed, 138 insertions(+) create mode 100644 src/driver/onboardled/build.mk create mode 100644 src/driver/onboardled/onboardled.c create mode 100644 src/include/driver/onboardled.h diff --git a/src/driver/build.mk b/src/driver/build.mk index 69b8c0c4..d0f47761 100644 --- a/src/driver/build.mk +++ b/src/driver/build.mk @@ -20,6 +20,7 @@ include $(DRIVER_PATH)/console/build.mk #==========< User Config Drivers >==========# $(eval $(call check_and_include,SYSCLK_ENABLE,$(DRIVER_PATH)/sysclk/build.mk)) +$(eval $(call check_and_include,OBRDLED_ENABLE,$(DRIVER_PATH)/onboardled/build.mk)) DIR :=$(DRIVER_PATH) diff --git a/src/driver/onboardled/build.mk b/src/driver/onboardled/build.mk new file mode 100644 index 00000000..15c0f509 --- /dev/null +++ b/src/driver/onboardled/build.mk @@ -0,0 +1,14 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2022, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates sources of onboard LED driver +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +OBRDLED_PATH := $(GET_PATH) + +DIR := $(OBRDLED_PATH) +include mk/obj.mk diff --git a/src/driver/onboardled/onboardled.c b/src/driver/onboardled/onboardled.c new file mode 100644 index 00000000..48c5ac1a --- /dev/null +++ b/src/driver/onboardled/onboardled.c @@ -0,0 +1,105 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : onboardled.c + * Description : This file contains sources of onboardled driver + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static gpio_port_t *obledPort; +static swdev_t *obled_sp; +static lock_t obledlock; + +status_t onboardled_toggle(void) +{ + status_t ret = success; + lock_acquire(&obledlock); + if(!obled_sp) + return error_driver_init_failed; + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) + ret |= gpio_pin_toggle(&obledPort[i]); + lock_release(&obledlock); + return ret; +} + +status_t onboardled_on(void) +{ + status_t ret = success; + lock_acquire(&obledlock); + if(!obled_sp) + return error_driver_init_failed; + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) + ret |= gpio_pin_set(&obledPort[i]); + lock_release(&obledlock); + return ret; +} + +status_t onboardled_off(void) +{ + status_t ret = success; + lock_acquire(&obledlock); + if(!obled_sp) + return error_driver_init_failed; + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) + ret |= gpio_pin_clear(&obledPort[i]); + lock_release(&obledlock); + return ret; +} + +static status_t onboardled_setup(void) +{ + mret_t mres; + status_t ret; + + lock_acquire(&obledlock); + arch_machine_call(fetch_sp, onboard_led, 0, 0, &mres); + if(mres.status != success) + { + sysdbg3("%p - sp node could not be found!\n", onboard_led); + return mres.status; + } + obled_sp = (swdev_t *) mres.p; + ret = mres.status; + + obledPort = (gpio_port_t *)malloc(sizeof(gpio_port_t)); + + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) + { + ret |= gpio_pin_alloc(&obledPort[i], obled_sp->pmux->port, obled_sp->pmux->pins[i]); + ret |= gpio_pin_mode(&obledPort[i], out); + } + lock_release(&obledlock); + + return ret; +} + +static status_t onboardled_exit(void) +{ + status_t ret = success; + lock_acquire(&obledlock); + if(!obled_sp) + return error_driver_init_failed; + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) + { + ret |= gpio_pin_free(&obledPort[i]); + free(&obledPort[i]); + } + obled_sp = NULL; + lock_release(&obledlock); + return ret; +} + +INCLUDE_DRIVER(OBrdLED, onboardled_setup, onboardled_exit, 0, 255, 255); diff --git a/src/include/driver/onboardled.h b/src/include/driver/onboardled.h new file mode 100644 index 00000000..3f59972f --- /dev/null +++ b/src/include/driver/onboardled.h @@ -0,0 +1,17 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : onboardled.h + * Description : This file consists of onboardled + * driver prototypes + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#define _ONBOARDLED_H_ + +status_t onboardled_toggle(void); +status_t onboardled_on(void); +status_t onboardled_off(void); diff --git a/src/lib/libresource/include/sp/sp_device.h b/src/lib/libresource/include/sp/sp_device.h index be946313..dccdb73c 100644 --- a/src/lib/libresource/include/sp/sp_device.h +++ b/src/lib/libresource/include/sp/sp_device.h @@ -16,6 +16,7 @@ typedef enum console_uart = 0x100, bt_uart = 0x101, sched_timer = 0x200, + onboard_led = 0x300, } sw_devid_t; From a76f03ff219a5bdb9ab20a91138c752aacb536c0 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 22:41:06 +0530 Subject: [PATCH 08/21] Updated platform SP for onboard led --- .../mega_avr/atmega328p/resources/sp.c | 12 ++++++++-- src/platform/sifive/fe310g002/resources/sp.c | 22 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/platform/mega_avr/atmega328p/resources/sp.c b/src/platform/mega_avr/atmega328p/resources/sp.c index 64d84527..10a270ea 100644 --- a/src/platform/mega_avr/atmega328p/resources/sp.c +++ b/src/platform/mega_avr/atmega328p/resources/sp.c @@ -24,16 +24,24 @@ swdev_t schedTimer = .hwdev_id = timer | 0, }; +static uint8_t led0pins[] = {5}; +static pinmux_t obled0 = addpins(1, led0pins, 0); +swdev_t onBoardLED0 = +{ + .swdev_id = onboard_led | 0, + .pmux = &obled0 +}; + sw_devid_t terra_devs[] = { - console_uart, sched_timer, + console_uart, sched_timer, (onboard_led | 0), }; visor_t terravisor = add_visor_devs(terra_devs); swdev_t *sw_devs[] = { - &consoleUart, &schedTimer + &consoleUart, &schedTimer, &onBoardLED0, }; sp_t software_prop = diff --git a/src/platform/sifive/fe310g002/resources/sp.c b/src/platform/sifive/fe310g002/resources/sp.c index 5bef38d3..23e2be42 100644 --- a/src/platform/sifive/fe310g002/resources/sp.c +++ b/src/platform/sifive/fe310g002/resources/sp.c @@ -28,16 +28,34 @@ swdev_t schedTimer = .hwdev_id = timer | 0, }; +static uint8_t led0pins[] = {19, 21}; +static pinmux_t obled0 = addpins(0, led0pins, 0); +swdev_t onBoardLED0 = +{ + .swdev_id = onboard_led | 0, + .pmux = &obled0 +}; + +static uint8_t led1pins[] = {20}; +static pinmux_t obled1 = addpins(0, led1pins, 0); +swdev_t onBoardLED1 = +{ + .swdev_id = onboard_led | 1, + .pmux = &obled1 +}; + sw_devid_t terra_devs[] = { - console_uart, sched_timer, + console_uart, sched_timer, (onboard_led | 0), + (onboard_led | 1), }; visor_t terravisor = add_visor_devs(terra_devs); swdev_t *sw_devs[] = { - &consoleUart, &schedTimer, + &consoleUart, &schedTimer, &onBoardLED0, + &onBoardLED1, }; sp_t software_prop = From 5726d537eee45d6ebba5142778a81e0918cd7a34 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 22:42:14 +0530 Subject: [PATCH 09/21] Enabled project unification feature - This commit enables developer to port 1 project like "name.src" to any target/platform using config.mk - Updated make to list only project and not sources - Updated config files to enable onboardled driver --- mk/project.mk | 2 +- projects/demo_avr/build.mk | 10 ++---- projects/demo_avr/config.mk | 1 + projects/demo_avr/project.c | 47 ----------------------------- projects/demo_riscv/build.mk | 8 ++--- projects/demo_riscv/config.mk | 1 + projects/demo_riscv/project.c | 57 ----------------------------------- 7 files changed, 8 insertions(+), 118 deletions(-) delete mode 100644 projects/demo_avr/project.c delete mode 100644 projects/demo_riscv/project.c diff --git a/mk/project.mk b/mk/project.mk index 402bc36a..24243b98 100644 --- a/mk/project.mk +++ b/mk/project.mk @@ -20,7 +20,7 @@ include mk/picotool.mk P_TARGETS += default cyancore check version copy_to_remote clean_remote T_ALLOWLIST += help list clean all_projects -PROJECT_LIST := $(shell ls projects/ -I *.template) +PROJECT_LIST := $(shell ls projects/ -I *.template -I *.src) .PHONY: aux_target diff --git a/projects/demo_avr/build.mk b/projects/demo_avr/build.mk index 7e0fe7b4..9c06d3cd 100644 --- a/projects/demo_avr/build.mk +++ b/projects/demo_avr/build.mk @@ -1,20 +1,16 @@ # # CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team +# Copyrights (C) 2019-2022, Cyancore Team # # File Name : build.mk -# Description : This file build project sources and specifies -# project properties +# Description : This file build and gathers project properties # Primary Author : Akash Kollipara [akashkollipara@gmail.com] # Organisation : Cyancore Core-Team # PROJECT_DIR := $(GET_PATH) -OPTIMIZATION := s - -EXE_MODE := terravisor - +include $(PROJECT_DIR)/../demo.src/build.mk include $(PROJECT_DIR)/config.mk DIR := $(PROJECT_DIR) diff --git a/projects/demo_avr/config.mk b/projects/demo_avr/config.mk index 9946b127..d6468d1d 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 TERRAKERN := 0 +OBRDLED_ENABLE := 1 diff --git a/projects/demo_avr/project.c b/projects/demo_avr/project.c deleted file mode 100644 index 0624f86f..00000000 --- a/projects/demo_avr/project.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : project.c - * Description : This file consists of project srouces - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -gpio_port_t onboad_led; - -void plug() -{ - bootstrap(); - driver_setup_all(); - gpio_pin_alloc(&onboad_led, PORTB, 5); - gpio_pin_mode(&onboad_led, out); - gpio_pin_clear(&onboad_led); - printf("Demo Program!\n"); - printf("< ! > Running Blinky ... ["); - return; -} - -char progress[] = "-\\|/"; - -void play() -{ - static unsigned int i = 0; - wdog_guard(WDT_64MS, true, NULL); - gpio_pin_toggle(&onboad_led); - printf("%c]", progress[(i++) % strlen(progress)]); - wdog_hush(); - mdelay(500); - printf("\b\b"); - return; -} diff --git a/projects/demo_riscv/build.mk b/projects/demo_riscv/build.mk index 7e0fe7b4..f4b55570 100644 --- a/projects/demo_riscv/build.mk +++ b/projects/demo_riscv/build.mk @@ -3,18 +3,14 @@ # Copyrights (C) 2019, Cyancore Team # # File Name : build.mk -# Description : This file build project sources and specifies -# project properties +# Description : This file builds and gathers project properties # Primary Author : Akash Kollipara [akashkollipara@gmail.com] # Organisation : Cyancore Core-Team # PROJECT_DIR := $(GET_PATH) -OPTIMIZATION := s - -EXE_MODE := terravisor - +include $(PROJECT_DIR)/../demo.src/build.mk include $(PROJECT_DIR)/config.mk DIR := $(PROJECT_DIR) diff --git a/projects/demo_riscv/config.mk b/projects/demo_riscv/config.mk index d8231176..945e71d4 100644 --- a/projects/demo_riscv/config.mk +++ b/projects/demo_riscv/config.mk @@ -16,3 +16,4 @@ STDLOG_MEMBUF := 0 BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 +OBRDLED_ENABLE := 1 diff --git a/projects/demo_riscv/project.c b/projects/demo_riscv/project.c deleted file mode 100644 index 1b5b2879..00000000 --- a/projects/demo_riscv/project.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : project.c - * Description : This file consists of project srouces - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static gpio_port_t gled, bled; - -void plug() -{ - bootstrap(); - driver_setup_all(); - platform_print_cpu_info(); - - gpio_pin_alloc(&gled, PORTA, 19); - gpio_pin_alloc(&bled, PORTA, 21); - gpio_pin_mode(&gled, out); - gpio_pin_mode(&bled, out); - gpio_pin_set(&gled); - gpio_pin_set(&bled); - - printf("Demo Program!\n"); - printf("< ! > Running Blinky ... ["); - return; -} - - -void play() -{ - char progress[] = "-\\|/"; - static unsigned char i = 0; - printf("%c]", progress[(i++) % strlen(progress)]); - - gpio_pin_toggle(&gled); - gpio_pin_toggle(&bled); - - mdelay(500); - - printf("\b\b"); - return; -} From aa72c7d2c7b8210e65f1a9c7879253b700f4f366 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 22:53:21 +0530 Subject: [PATCH 10/21] Added unified demo blinky code --- projects/demo.src/build.mk | 18 ++++++++++++++++ projects/demo.src/project.c | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 projects/demo.src/build.mk create mode 100644 projects/demo.src/project.c diff --git a/projects/demo.src/build.mk b/projects/demo.src/build.mk new file mode 100644 index 00000000..b6b7f7f1 --- /dev/null +++ b/projects/demo.src/build.mk @@ -0,0 +1,18 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2022, Cyancore Team +# +# File Name : build.mk +# Description : This file build project sources +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +DEMO_DIR := $(GET_PATH) + +OPTIMIZATION := s + +EXE_MODE := terravisor + +DIR := $(DEMO_DIR) +include mk/obj.mk diff --git a/projects/demo.src/project.c b/projects/demo.src/project.c new file mode 100644 index 00000000..dd8dba15 --- /dev/null +++ b/projects/demo.src/project.c @@ -0,0 +1,42 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : project.c + * Description : This file consists of project srouces + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include + +void plug() +{ + bootstrap(); + driver_setup_all(); + + printf("Demo Program!\n"); + printf("< ! > Running Blinky ... ["); + return; +} + + +void play() +{ + char progress[] = "-\\|/"; + static unsigned char i = 0; + printf("%c]", progress[(i++) % strlen(progress)]); + + onboardled_toggle(); + + mdelay(500); + + printf("\b\b"); + return; +} From 6d4b11a312cbefbbf347d2bfac46b8103503340c Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 28 Jul 2022 23:52:49 +0530 Subject: [PATCH 11/21] Fixed file header for malloc and minor changes --- src/lib/libc/malloc_lite.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib/libc/malloc_lite.c b/src/lib/libc/malloc_lite.c index 6c1ffb62..cae2aeca 100644 --- a/src/lib/libc/malloc_lite.c +++ b/src/lib/libc/malloc_lite.c @@ -2,7 +2,7 @@ * CYANCORE LICENSE * Copyrights (C) 2022, Cyancore Team * - * File Name : time.c + * File Name : malloc_lite.c * Description : This file contains sources of libc-malloc * functions * Primary Author : Akash Kollipara [akashkollipara@gmail.com] @@ -22,8 +22,6 @@ typedef struct chunk } chunk_t; extern uint8_t _heap_start, _heap_end; - -static uint8_t *arena = &_heap_start; static chunk_t *freeList; static void split(chunk_t *fit_slot, size_t size) @@ -55,8 +53,8 @@ static void merge() status_t platform_init_heap() { - memset(arena, 0, (size_t)(&_heap_end - &_heap_start)); - freeList = (void *)arena; + memset(&_heap_start, 0, (size_t)(&_heap_end - &_heap_start)); + freeList = (void *)&_heap_start; freeList->size = (size_t)(&_heap_end - &_heap_start) - sizeof(chunk_t); freeList->free = 1; freeList->next = NULL; From e6e47c75187ec260f2876eeddf4752acff1bd700 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 29 Jul 2022 12:59:37 +0530 Subject: [PATCH 12/21] Updated for libc and libsyslog - Printf can now print 64b variables - Added timestamping to syslog --- src/lib/libc/printf.c | 6 ++++-- src/lib/libsyslog/syslog.c | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/libc/printf.c b/src/lib/libc/printf.c index a7e486d6..0b1a4fdc 100644 --- a/src/lib/libc/printf.c +++ b/src/lib/libc/printf.c @@ -21,11 +21,13 @@ #define get_num_va_args(_args, _lcount) \ - (((_lcount) >= 1) ? va_arg(_args, long) : \ + (((_lcount) >= 2) ? va_arg(_args, int64_t) : \ + ((_lcount) == 1) ? va_arg(_args, long) : \ va_arg(_args, int)) #define get_unum_va_args(_args, _lcount) \ - (((_lcount) >= 1) ? va_arg(_args, unsigned long) : \ + (((_lcount) >= 2) ? va_arg(_args, uint64_t) : \ + ((_lcount) == 1) ? va_arg(_args, unsigned long) : \ va_arg(_args, unsigned int)) static int __fputc(const FILE *dev, bool en_stdout, const char c) diff --git a/src/lib/libsyslog/syslog.c b/src/lib/libsyslog/syslog.c index 943b4e26..e04dbc03 100644 --- a/src/lib/libsyslog/syslog.c +++ b/src/lib/libsyslog/syslog.c @@ -17,6 +17,7 @@ #include #include #include +#include static lock_t syslog_lock; static bool flag_enable_stdout; @@ -26,9 +27,12 @@ int syslog(logtype_t t, const char *c, ...) { int ret; va_list va; + uint64_t time; char logsign[] = {'/', 'i', '!', 'x', '$'}; va_start(va, c); - ret = fprintf(stdlog, flag_enable_stdout,"< %c > ", logsign[t]); + get_timestamp(&time); + time /= 1000U; + ret = fprintf(stdlog, flag_enable_stdout,"[%08llu] < %c > ", time, logsign[t]); ret += vprintf(stdlog, flag_enable_stdout, c, va); va_end(va); return ret; From 301aae0d387165b46d003df4eab4e7f5b5291ab8 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 29 Jul 2022 13:01:28 +0530 Subject: [PATCH 13/21] Updated blinky project to print time --- projects/demo.src/project.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/projects/demo.src/project.c b/projects/demo.src/project.c index dd8dba15..ce3caee7 100644 --- a/projects/demo.src/project.c +++ b/projects/demo.src/project.c @@ -22,21 +22,25 @@ void plug() driver_setup_all(); printf("Demo Program!\n"); - printf("< ! > Running Blinky ... ["); return; } void play() { - char progress[] = "-\\|/"; static unsigned char i = 0; - printf("%c]", progress[(i++) % strlen(progress)]); + char progress[] = "-\\|/"; + uint64_t time; + char c = progress[(i++) % strlen(progress)]; + get_timestamp(&time); + time /= 1000U; + + printf("[%012llu] Running Blinky ... [%c]", time, c); onboardled_toggle(); mdelay(500); - printf("\b\b"); + printf("\r"); return; } From 7c79befc400d92e41e6be3bc77b6f8cf5ae73c5a Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 29 Jul 2022 13:02:13 +0530 Subject: [PATCH 14/21] Updated code documentation --- .../mega_avr/common/platform/platform_timer.c | 66 ++++++++++++++++ .../sifive/common_fe310/platform/plat_timer.c | 56 +++++++++++++ src/visor/terravisor/services/driver/timer.c | 79 ++++++++++++++++++- 3 files changed, 199 insertions(+), 2 deletions(-) diff --git a/src/platform/mega_avr/common/platform/platform_timer.c b/src/platform/mega_avr/common/platform/platform_timer.c index b38f6789..92554891 100644 --- a/src/platform/mega_avr/common/platform/platform_timer.c +++ b/src/platform/mega_avr/common/platform/platform_timer.c @@ -22,12 +22,39 @@ #define PS 4 #define PSVALUE 256 +/** + * tmr_cb - Timer event call back + */ static void (*tmr_cb)(void); + +/** + * tport - Timer HAL device instance + */ static timer_port_t tport; + +/** + * ticks - Driver variable for keeping track of timer ticks + * for event + */ static uint64_t ticks; + +/** + * cntr - Timer counter + */ static uint64_t cntr; + +/** + * tm - timer device module + */ static module_t *tm; +/** + * plat_tmr_isr - platform timer isr handler + * + * @brief This function is timer event handler. It updated timer counter. + * Also, this function executes registered timer callback additionally + * which will be used by scheduler. + */ static void plat_tmr_isr(void) { cntr += ticks; @@ -35,11 +62,17 @@ static void plat_tmr_isr(void) tmr_cb(); } +/** + * plat_timer_reg_cb - Funtion to register call back + */ static void plat_timer_reg_cb(void *cb) { tmr_cb = cb; } +/** + * plat_get_timer_prop - Helper function to fetch timer properties + */ static status_t plat_get_timer_prop(void) { mret_t mres; @@ -65,6 +98,9 @@ static status_t plat_get_timer_prop(void) return success; } +/** + * plat_get_timer_ticks_msec - Helper function to get tick/msec + */ static uint64_t plat_get_timer_ticks_msec(uint64_t freq) { uint64_t nt = freq / (2 * PSVALUE); @@ -72,6 +108,14 @@ static uint64_t plat_get_timer_ticks_msec(uint64_t freq) return nt; } +/** + * plat_timer_set_period - Sets period for the timer events + * + * @brief This function configures timer to raise event after the + * programmed value. + * + * @param[in] p: Period of events in milli seconds + */ static void plat_timer_set_period(unsigned int p) { uint64_t nt = plat_get_timer_ticks_msec(tm->clk); @@ -80,11 +124,22 @@ static void plat_timer_set_period(unsigned int p) timer_setup(&tport, 2, PS); } +/** + * plat_read_ticks - Returns timer ticks + */ static uint64_t plat_read_ticks(void) { return cntr; } +/** + * plat_read_time - This function returns time + * + * @brief This function returns time based on timer events. + * The value might not be accurate to wall clock. + * + * @return time in microseconds + */ static uint64_t plat_read_time(void) { uint64_t stamp = cntr; @@ -93,6 +148,9 @@ static uint64_t plat_read_time(void) return stamp; } +/** + * Driver ops for linking timer + */ static tvisor_timer_t plat_timer_port = { .read_ticks = &plat_read_ticks, @@ -101,6 +159,10 @@ static tvisor_timer_t plat_timer_port = .reg_cb = &plat_timer_reg_cb, }; +/** + * plat_timer_setup - Timer driver setup function + * To be exported to driver table. + */ static status_t plat_timer_setup() { status_t ret = success; @@ -120,6 +182,10 @@ static status_t plat_timer_setup() return ret; } +/** + * plat_timer_exit - Timer driver shutdown function + * To be exported to driver table. + */ static status_t plat_timer_exit(void) { status_t ret = timer_shutdown(&tport); diff --git a/src/platform/sifive/common_fe310/platform/plat_timer.c b/src/platform/sifive/common_fe310/platform/plat_timer.c index ebbf8397..7d0b4f4d 100644 --- a/src/platform/sifive/common_fe310/platform/plat_timer.c +++ b/src/platform/sifive/common_fe310/platform/plat_timer.c @@ -21,10 +21,30 @@ #include #include +/** + * tmr_cb - Timer event call back + */ static void (*tmr_cb)(void); + +/** + * ticks - Driver variable for keeping track of timer ticks + * for event + */ static uint64_t ticks; + +/** + * tm - timer device module + */ static module_t *tm; +/** + * plat_tmr_isr - platform timer isr handler + * + * @brief This function is timer event handler. This function + * updated timercmp reg to raise event after 'ticks' have elapsed. + * Also, this function executes registered timer callback additionally + * which will be used by scheduler. + */ static void plat_tmr_isr(void) { arch_di_mtime(); @@ -36,6 +56,9 @@ static void plat_tmr_isr(void) tmr_cb(); } +/** + * plat_timer_reg_cb - Funtion to register call back + */ static void plat_timer_reg_cb(void *cb) { arch_di_mtime(); @@ -43,6 +66,9 @@ static void plat_timer_reg_cb(void *cb) arch_ei_mtime(); } +/** + * plat_get_timer_prop - Helper function to fetch timer properties + */ static status_t plat_get_timer_prop(void) { mret_t mres; @@ -68,12 +94,23 @@ static status_t plat_get_timer_prop(void) return success; } +/** + * plat_get_timer_ticks_msec - Helper function to get tick/msec + */ static uint64_t plat_get_timer_ticks_msec(uint64_t freq) { /* Compute ticks needed for 1 msec */ return (uint64_t)(freq/1000); } +/** + * plat_timer_set_period - Sets period for the timer events + * + * @brief This function configures timer to raise event after the + * programmed value. + * + * @param[in] p: Period of events in milli seconds + */ static void plat_timer_set_period(unsigned int p) { uint64_t nt; @@ -85,6 +122,14 @@ static void plat_timer_set_period(unsigned int p) arch_ei_mtime(); } +/** + * plat_read_time - This function returns time + * + * @brief This function returns time based on timer events. + * The value might not be accurate to wall clock. + * + * @return time in microseconds + */ static uint64_t plat_read_time(void) { uint64_t stamp = clint_read_time(); @@ -92,6 +137,9 @@ static uint64_t plat_read_time(void) return (uint64_t)(stamp / tm->clk); } +/** + * Driver ops for linking timer + */ static tvisor_timer_t plat_timer_port = { .read_ticks = &clint_read_time, @@ -100,6 +148,10 @@ static tvisor_timer_t plat_timer_port = .reg_cb = &plat_timer_reg_cb, }; +/** + * plat_timer_setup - Timer driver setup function + * To be exported to driver table. + */ static status_t plat_timer_setup(void) { status_t ret = success; @@ -118,6 +170,10 @@ static status_t plat_timer_setup(void) return ret; } +/** + * plat_timer_exit - Timer driver shutdown function + * To be exported to driver table. + */ static status_t plat_timer_exit(void) { arch_di_mtime(); diff --git a/src/visor/terravisor/services/driver/timer.c b/src/visor/terravisor/services/driver/timer.c index f845c0c2..77bdbd0c 100644 --- a/src/visor/terravisor/services/driver/timer.c +++ b/src/visor/terravisor/services/driver/timer.c @@ -17,10 +17,36 @@ #include #include +/** + * *port - Timer driver pointer + * + * This variable is updated by the timer_attach_device + * when the device driver is initialised + */ static tvisor_timer_t *port[N_CORES]; + +/** + * timer_attached - Flag to indicate the initialization status + */ static bool timer_attached[N_CORES] = {[0 ... N_CORES-1] = false}; + +/** + * tlock - Locks for sync in multi thread env + */ static lock_t tlock[N_CORES]; +/** + * timer_attach_device - This function links hardware driver + * and device driver. + * + * @brief This function links the hardware driver and timer device. + * On successful linking, it updates the status flag so that + * other functions know timer is attached. + * + * @param[in] dev_status: device init status + * @param[in] *ptmr: device timer port + * @return status: status of device/hardware driver + */ status_t timer_attach_device(status_t dev_status, tvisor_timer_t *ptmr) { status_t ret; @@ -42,6 +68,15 @@ status_t timer_attach_device(status_t dev_status, tvisor_timer_t *ptmr) return ret; } +/** + * timer_release_device - This function delinks hardware driver + * and device driver. + * + * @brief This function simply clear hardware driver pointer and updates + * timer status flag + * + * @return status: status of device/hardware driver + */ status_t timer_release_device() { size_t cpu_index = arch_core_index(); @@ -53,6 +88,18 @@ status_t timer_release_device() return success; } +/** + * timer_link_callback - This is a helper function which lets other + * programs to link timer event call back functions. + * + * @brief This is a helper function which lets other programs to link + * timer event call back functions. It allows to link onlt 1 callback + * which will be exeuted as part of timer ISR handler. + * + * @param[in] p: period of timer irq + * @param[in] cb: call back function pointer + * @return status + */ status_t timer_link_callback(unsigned int p, void *cb) { size_t cpu_index = arch_core_index(); @@ -67,28 +114,56 @@ status_t timer_link_callback(unsigned int p, void *cb) return success; } -/* This function's prototype is located in libc */ +/** + * get_timestamp - This funtion reads timestamp + * + * @brief This function reads time from lower level timer + * functions and updates the input potiner. If the driver + * is not initialised, then pointer is updated to 0. + * + * This function's prototype is located in libc. + * + * @param[in] *t: pointer to store time + * @return status + */ status_t get_timestamp(uint64_t *t) { size_t cpu_index = arch_core_index(); lock_t *lock = &tlock[cpu_index]; tvisor_timer_t *ptr = port[cpu_index]; if(!timer_attached[cpu_index]) + { + *t = 0; return error_driver_init_failed; + } lock_acquire(lock); *t = ptr->read_time(); lock_release(lock); return success; } -/* This function's prototype is located in libc */ +/** + * get_timeticks - This funtion reads timer ticks + * + * @brief This function reads ticks from lower level timer + * functions and updates the input potiner. If the driver + * is not initialised, then pointer is updated to 0. + * + * This function's prototype is located in libc. + * + * @param[in] *t: pointer to store ticks + * @return status + */ status_t get_timeticks(uint64_t *t) { size_t cpu_index = arch_core_index(); lock_t *lock = &tlock[cpu_index]; tvisor_timer_t *ptr = port[cpu_index]; if(!timer_attached[cpu_index]) + { + *t = 0; return error_driver_init_failed; + } lock_acquire(lock); *t = ptr->read_ticks(); lock_release(lock); From 84257f5edc049967fc4337f1b8fae403d547d090 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 29 Jul 2022 16:29:08 +0530 Subject: [PATCH 15/21]