diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b028dd1fed81113bb8a80d96d31dec5aaddfd50f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/** +*.swp + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2bf669d06c46324674835ab76fca83390c3a6a61 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.12) + +include($ENV{PICO_SDK_PATH}/pico_sdk_init.cmake) +pico_sdk_init() + +project(comp2215-coursework C CXX ASM) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +add_subdirectory(rios) + diff --git a/README.md b/README.md index e901e8f5260fa672341024333f38ab8477d048db..8fafabb7c768c8a2984e50a31b81b3ef5930a223 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,3 @@ # COMP2215 Coursework +My solutions for Computer Systems 2 challenges, including the challenges I have created myself. - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://git.soton.ac.uk/wjg1g21/comp2215-coursework.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://git.soton.ac.uk/wjg1g21/comp2215-coursework/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/rios/CMakeLists.txt b/rios/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1b2f2a29985edc4738118347b73294883d26024b --- /dev/null +++ b/rios/CMakeLists.txt @@ -0,0 +1,18 @@ +if (TARGET tinyusb_device) + +add_library(rios STATIC rios.c rios.h) +target_link_libraries(rios hardware_irq) + +add_executable(rios_main main.c) +target_link_libraries(rios_main rios pico_stdlib) + +# TODO support uart +pico_enable_stdio_usb(rios_main 1) +pico_enable_stdio_uart(rios_main 0) + +pico_add_extra_outputs(rios_main) + +elseif(PICO_ON_DEVICE) + message(WARNING "cannot build rios because TinyUSB submodule is not initialised in the sdk") +endif() + diff --git a/rios/main.c b/rios/main.c new file mode 100644 index 0000000000000000000000000000000000000000..1aaaf1d46ae841a5a222d4f30a1854b94380ad8e --- /dev/null +++ b/rios/main.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include "pico/stdlib.h" +#include "rios.h" + +static uint32_t task1(uint32_t state) { + printf("[T1%d<", state); + sleep_ms(20); + printf(">T1%d]", state); + return ++state; +} + +static uint32_t task2(uint32_t state) { + printf("[T2%d<", state); + sleep_ms(600); + printf(">T2%d]", state); + return ++state; +} + +static uint32_t task3(uint32_t state) { + printf("[T3%d<", state); + sleep_ms(2000); + printf(">T3%d]", state); + return ++state; +} + +static inline void init(void) { + stdio_init_all(); + rios_init(); +} + +int main(void) { + init(); + + rios_add_task(29u, 0u, task1); + rios_add_task(77u, 0u, task2); + rios_add_task(162u, 0u, task3); + rios_start(); +} + diff --git a/rios/rios.c b/rios/rios.c new file mode 100644 index 0000000000000000000000000000000000000000..0d39456df4b42100bf8e96e1199840183f5dcd9f --- /dev/null +++ b/rios/rios.c @@ -0,0 +1,79 @@ +#include "rios.h" + +#include <stdint.h> +#include <stdbool.h> +#include "hardware/irq.h" + +typedef struct { + bool running; + uint32_t state; + uint32_t period; + uint32_t elapsed_time; + rios_task_t tick_func; +} task_data_t; + +static const uint32_t tick_ms = 400u; // Real time between ticks in ms +static const uint32_t tasks_period_GCD = 25u; // Timer tick rate + +static task_data_t tasks[RIOS_MAX_TASKS]; +static uint32_t tasks_index = 0u; + +static uint32_t running_tasks[RIOS_MAX_TASKS+1] = {UINT32_MAX}; +static uint32_t current_task = 0u; +static const uint32_t idle_task = UINT32_MAX; + +static const uint irq_num = 0u; + +static inline void enable_irq(void) { + irq_set_enabled(irq_num, true); +} + +static inline void disable_irq(void) { + irq_set_enabled(irq_num, false); +} + +static void scheduler_isr(void) { + for (uint32_t i = 0u; i < tasks_index; ++i) { + if ((tasks[i].elapsed_time >= tasks[i].period) + && (running_tasks[current_task] > i) + && (!tasks[i].running)) { + + disable_irq(); + tasks[i].elapsed_time = 0u; + tasks[i].running = true; + ++current_task; + running_tasks[current_task] = i; + enable_irq(); + + tasks[i].state = tasks[i].tick_func(tasks[i].state); // execute tick + + disable_irq(); + tasks[i].running = false; + running_tasks[current_task] = idle_task; + --current_task; + enable_irq(); + } + tasks[i].elapsed_time += tasks_period_GCD; + } +} + +void rios_init(void) { + irq_set_exclusive_handler(irq_num, scheduler_isr); + enable_irq(); +} + +void rios_add_task(uint32_t period, uint32_t initial_state, rios_task_t task) { + task_data_t task_data = { .running = false, + .state = initial_state, + .period = period, + .elapsed_time = period, + .tick_func = task }; + tasks[tasks_index] = task_data; + + ++tasks_index; +} + +void rios_start(void) { + enable_irq(); +} + diff --git a/rios/rios.h b/rios/rios.h new file mode 100644 index 0000000000000000000000000000000000000000..696435dba572c1a758ef0ea76f8d091f2c0a05d5 --- /dev/null +++ b/rios/rios.h @@ -0,0 +1,14 @@ +#pragma once + +#include <stdint.h> + +#define RIOS_MAX_TASKS (100) + +typedef uint32_t (*rios_task_t)(uint32_t state); + +void rios_init(void); + +void rios_add_task(uint32_t period, uint32_t initial_state, rios_task_t task); + +void rios_start(void); +