From c661006a46827cb5ac5a204743471f8a137068ac Mon Sep 17 00:00:00 2001 From: Iliass Mahjoub Date: Sat, 3 Aug 2024 15:21:39 +0200 Subject: [PATCH 1/2] added an OS and get blinky working --- Build/VS/UniversalBareMetal.vcxproj | 4 ++ Build/VS/UniversalBareMetal.vcxproj.filters | 18 +++++++ Src/App/C/App.c | 32 ++++-------- Src/App/C/OS/OS_Cfg.h | 24 +++++++++ Src/App/C/OS/OS_Task.c | 40 +++++++++++++++ .../stm32l432kc/Make/make_stm32l432kc.gmk | 2 + Src/Target/stm32l432kc/OS/OS.c | 51 +++++++++++++++++++ Src/Target/stm32l432kc/OS/OS.h | 16 ++++++ 8 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 Src/App/C/OS/OS_Cfg.h create mode 100644 Src/App/C/OS/OS_Task.c create mode 100644 Src/Target/stm32l432kc/OS/OS.c create mode 100644 Src/Target/stm32l432kc/OS/OS.h diff --git a/Build/VS/UniversalBareMetal.vcxproj b/Build/VS/UniversalBareMetal.vcxproj index 466bf0b..71bda9f 100644 --- a/Build/VS/UniversalBareMetal.vcxproj +++ b/Build/VS/UniversalBareMetal.vcxproj @@ -24,6 +24,7 @@ true true + true true @@ -90,6 +91,7 @@ true true + true true @@ -110,6 +112,7 @@ + true true @@ -186,6 +189,7 @@ true true + diff --git a/Build/VS/UniversalBareMetal.vcxproj.filters b/Build/VS/UniversalBareMetal.vcxproj.filters index 002de7c..f1a2a1e 100644 --- a/Build/VS/UniversalBareMetal.vcxproj.filters +++ b/Build/VS/UniversalBareMetal.vcxproj.filters @@ -59,6 +59,12 @@ {a45a3942-f76a-45ff-a692-7bb355eb8e53} + + {bd12dccf-bd29-4f88-8701-9be712fea8aa} + + + {da74b66b-3426-400e-a8b0-e21cea469bcc} + @@ -118,6 +124,12 @@ Source Files\Src\App\C + + Source Files\Src\App\C\OS + + + Source Files\Src\Target\stm32l432kc\OS + @@ -194,5 +206,11 @@ Source Files\Src\Util\Cpp + + Source Files\Src\App\C\OS + + + Source Files\Src\Target\stm32l432kc\OS + \ No newline at end of file diff --git a/Src/App/C/App.c b/Src/App/C/App.c index 8945d80..044953d 100644 --- a/Src/App/C/App.c +++ b/Src/App/C/App.c @@ -1,39 +1,25 @@ #include #include #include +#include +#include #include -void msDelay(volatile uint32_t count); - -// Simple delay function -void msDelay(volatile uint32_t count) -{ - while (count--) - { - __asm volatile ("nop"); // No operation (do nothing) - } -} - - int main(void) { /* Configure the System clock and flash */ SystemInit(); SetSysClock(); - /*. Enable GPIO Clock for GPIOB */ - RCC_AHB2ENR |= (1U << 1U); + /* Configure systick timer.*/ + SysTick_Init(); - /*. Configure PB3 as output */ - GPIOB_MODER |= (1U << 6U); - GPIOB_MODER &= ~(1U << 7U); + /* Initialize the OS. This calls the task init-functions one time only */ + OS_Init(); - for(;;) - { - /* Toggle PB3 to turn the LED on and off */ - GPIOB_ODR ^= (1U << 3U); - msDelay(1000000U); - } + /* Start the cooperative multitasking scheduler */ + OS_Start(); return 0; + } diff --git a/Src/App/C/OS/OS_Cfg.h b/Src/App/C/OS/OS_Cfg.h new file mode 100644 index 0000000..e9a1868 --- /dev/null +++ b/Src/App/C/OS/OS_Cfg.h @@ -0,0 +1,24 @@ +#ifndef OS_CFG_2023_08_23_H + #define OS_CFG_2023_08_23_H + + #if defined(__cplusplus) + extern "C" + { + #endif + + extern void Task01_Init(void); + + extern void Task01_Func(void); + + #define OS_CFG_TASK_LIST_ENTRY(init, func, t0, cycle) { (init), (func), (t0), (cycle) } + + #define OS_CFG_TASK_LIST_INIT \ + { \ + OS_CFG_TASK_LIST_ENTRY(Task01_Init, Task01_Func, 0U, 17U), \ + } + + #if defined(__cplusplus) + } + #endif + +#endif /* OS_CFG_2023_08_23_H */ diff --git a/Src/App/C/OS/OS_Task.c b/Src/App/C/OS/OS_Task.c new file mode 100644 index 0000000..1be49b1 --- /dev/null +++ b/Src/App/C/OS/OS_Task.c @@ -0,0 +1,40 @@ +#include +#include + +#include +#include +#include + +static uint64_t TaskTimer02; + +/************************* TASK1 *********************************/ +void Task01_Init(void); +void Task01_Func(void); + +void Task01_Init(void) +{ + /*. Enable GPIO Clock for GPIOB */ + RCC_AHB2ENR |= (1U << 1U); + + /*. Configure PB3 as output */ + GPIOB_MODER |= (1U << 6U); + GPIOB_MODER &= ~(1U << 7U); + + /* Toggle the LED pin */ + GPIOB_ODR |= (uint32_t) (1UL << 3U); + + /* Set the next timer timeout to be 1s later, */ + /* Toggling will be sequentially carried out in the task. */ + TaskTimer02 = TimerStart(1000U); +} + +void Task01_Func(void) +{ + if(TimerTimeout(TaskTimer02)) + { + TaskTimer02 = TimerStart(1000U); + + /* Toggle the LED pin */ + GPIOB_ODR ^= (uint32_t) (1UL << 3U); + } +} diff --git a/Src/Target/stm32l432kc/Make/make_stm32l432kc.gmk b/Src/Target/stm32l432kc/Make/make_stm32l432kc.gmk index 7f412db..d5b3bb4 100644 --- a/Src/Target/stm32l432kc/Make/make_stm32l432kc.gmk +++ b/Src/Target/stm32l432kc/Make/make_stm32l432kc.gmk @@ -140,9 +140,11 @@ LDFLAGS = $(CPPFLAGS) \ #------------------------------------------------------------------------------ SRC_FILES = $(PATH_SRC)/App/C/App \ + $(PATH_SRC)/App/C/OS/OS_Task \ $(PATH_TARGET)/Mcal/Gpt \ $(PATH_TARGET)/Mcal/Mcu \ $(PATH_TARGET)/Mcal/SysTick \ + $(PATH_TARGET)/OS/OS \ $(PATH_TARGET)/Startup/IntVect \ $(PATH_TARGET)/Startup/SysStartup diff --git a/Src/Target/stm32l432kc/OS/OS.c b/Src/Target/stm32l432kc/OS/OS.c new file mode 100644 index 0000000..1be8c86 --- /dev/null +++ b/Src/Target/stm32l432kc/OS/OS.c @@ -0,0 +1,51 @@ +#include +#include + +#include +#include +#include + +typedef struct TCB +{ + void(*pInit)(void); + void(*pFunc)(void); + Gpt_ValueType CallTimeNext; + const unsigned CallCycle; +} +TCB; + +static TCB TaskList[] = OS_CFG_TASK_LIST_INIT; + + +void OS_Init(void) +{ + for(size_t i = (size_t) UINT8_C(0); + i < (size_t) (sizeof(TaskList) / sizeof(TaskList[(size_t) UINT8_C(0)])); + ++i) + { + /* Call each task's init-function once at OS initialization.*/ + TaskList[i].pInit(); + } +} + +void OS_Start(void) +{ + /* Start the cooperative multitasking scheduler (and never return).*/ + for(;;) + { + for(size_t i = (size_t) UINT8_C(0); + i < (size_t) (sizeof(TaskList) / sizeof(TaskList[(size_t) UINT8_C(0)])); + ++i) + { + if(TimerTimeout(TaskList[i].CallTimeNext)) + { + TaskList[i].CallTimeNext = TimerStart(TaskList[i].CallCycle); + + TaskList[i].pFunc(); + + /* Implement an (optional) priority mechanism. */ + break; + } + } + } +} diff --git a/Src/Target/stm32l432kc/OS/OS.h b/Src/Target/stm32l432kc/OS/OS.h new file mode 100644 index 0000000..dc9781d --- /dev/null +++ b/Src/Target/stm32l432kc/OS/OS.h @@ -0,0 +1,16 @@ +#ifndef OS_2023_08_23_H + #define OS_2023_08_23_H + + #if defined(__cplusplus) + extern "C" + { + #endif + + void OS_Init (void); + void OS_Start(void); + + #if defined(__cplusplus) + } + #endif + +#endif /* OS_2023_08_23_H */ From 4b404e2fe542eb58095e29e3b0fd2734da4befb8 Mon Sep 17 00:00:00 2001 From: Iliass Mahjoub Date: Sat, 3 Aug 2024 15:23:37 +0200 Subject: [PATCH 2/2] exclude all files not needed for visual studio build-configs --- Build/VS/UniversalBareMetal.vcxproj | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Build/VS/UniversalBareMetal.vcxproj b/Build/VS/UniversalBareMetal.vcxproj index 71bda9f..c04ff0a 100644 --- a/Build/VS/UniversalBareMetal.vcxproj +++ b/Build/VS/UniversalBareMetal.vcxproj @@ -24,7 +24,12 @@ true true - + + true + true + true + true + true true @@ -91,7 +96,12 @@ true true - + + true + true + true + true + true true @@ -112,7 +122,12 @@ - + + true + true + true + true + true true @@ -189,7 +204,12 @@ true true - + + true + true + true + true +