Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad60936

Browse files
committed
Initial Sempahore project
- Cloned from Systick_Scheduler - Contains osKernel files
1 parent 461b18e commit ad60936

34 files changed

+29380
-0
lines changed

Semaphore/.clang-format

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveMacros: false
7+
AlignConsecutiveAssignments: false
8+
AlignConsecutiveDeclarations: false
9+
AlignEscapedNewlines: Right
10+
AlignOperands: true
11+
AlignTrailingComments: true
12+
AllowAllArgumentsOnNextLine: true
13+
AllowAllConstructorInitializersOnNextLine: true
14+
AllowAllParametersOfDeclarationOnNextLine: true
15+
AllowShortBlocksOnASingleLine: Never
16+
AllowShortCaseLabelsOnASingleLine: false
17+
AllowShortFunctionsOnASingleLine: All
18+
AllowShortLambdasOnASingleLine: All
19+
AllowShortIfStatementsOnASingleLine: Never
20+
AllowShortLoopsOnASingleLine: false
21+
AlwaysBreakAfterDefinitionReturnType: None
22+
AlwaysBreakAfterReturnType: None
23+
AlwaysBreakBeforeMultilineStrings: false
24+
AlwaysBreakTemplateDeclarations: MultiLine
25+
BinPackArguments: true
26+
BinPackParameters: true
27+
BraceWrapping:
28+
AfterCaseLabel: false
29+
AfterClass: false
30+
AfterControlStatement: false
31+
AfterEnum: false
32+
AfterFunction: false
33+
AfterNamespace: false
34+
AfterObjCDeclaration: false
35+
AfterStruct: false
36+
AfterUnion: false
37+
AfterExternBlock: false
38+
BeforeCatch: false
39+
BeforeElse: false
40+
IndentBraces: false
41+
SplitEmptyFunction: true
42+
SplitEmptyRecord: true
43+
SplitEmptyNamespace: true
44+
BreakBeforeBinaryOperators: None
45+
BreakBeforeBraces: Attach
46+
BreakBeforeInheritanceComma: false
47+
BreakInheritanceList: BeforeColon
48+
BreakBeforeTernaryOperators: true
49+
BreakConstructorInitializersBeforeComma: false
50+
BreakConstructorInitializers: BeforeColon
51+
BreakAfterJavaFieldAnnotations: false
52+
BreakStringLiterals: true
53+
ColumnLimit: 80
54+
CommentPragmas: '^ IWYU pragma:'
55+
CompactNamespaces: false
56+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
57+
ConstructorInitializerIndentWidth: 4
58+
ContinuationIndentWidth: 4
59+
Cpp11BracedListStyle: true
60+
DeriveLineEnding: true
61+
DerivePointerAlignment: false
62+
DisableFormat: false
63+
ExperimentalAutoDetectBinPacking: false
64+
FixNamespaceComments: true
65+
ForEachMacros:
66+
- foreach
67+
- Q_FOREACH
68+
- BOOST_FOREACH
69+
IncludeBlocks: Preserve
70+
IncludeCategories:
71+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
72+
Priority: 2
73+
SortPriority: 0
74+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
75+
Priority: 3
76+
SortPriority: 0
77+
- Regex: '.*'
78+
Priority: 1
79+
SortPriority: 0
80+
IncludeIsMainRegex: '(Test)?$'
81+
IncludeIsMainSourceRegex: ''
82+
IndentCaseLabels: false
83+
IndentGotoLabels: true
84+
IndentPPDirectives: None
85+
IndentWidth: 2
86+
IndentWrappedFunctionNames: false
87+
JavaScriptQuotes: Leave
88+
JavaScriptWrapImports: true
89+
KeepEmptyLinesAtTheStartOfBlocks: true
90+
MacroBlockBegin: ''
91+
MacroBlockEnd: ''
92+
MaxEmptyLinesToKeep: 1
93+
NamespaceIndentation: None
94+
ObjCBinPackProtocolList: Auto
95+
ObjCBlockIndentWidth: 2
96+
ObjCSpaceAfterProperty: false
97+
ObjCSpaceBeforeProtocolList: true
98+
PenaltyBreakAssignment: 2
99+
PenaltyBreakBeforeFirstCallParameter: 19
100+
PenaltyBreakComment: 300
101+
PenaltyBreakFirstLessLess: 120
102+
PenaltyBreakString: 1000
103+
PenaltyBreakTemplateDeclaration: 10
104+
PenaltyExcessCharacter: 1000000
105+
PenaltyReturnTypeOnItsOwnLine: 60
106+
PointerAlignment: Right
107+
ReflowComments: true
108+
SortIncludes: true
109+
SortUsingDeclarations: true
110+
SpaceAfterCStyleCast: false
111+
SpaceAfterLogicalNot: false
112+
SpaceAfterTemplateKeyword: true
113+
SpaceBeforeAssignmentOperators: true
114+
SpaceBeforeCpp11BracedList: false
115+
SpaceBeforeCtorInitializerColon: true
116+
SpaceBeforeInheritanceColon: true
117+
SpaceBeforeParens: ControlStatements
118+
SpaceBeforeRangeBasedForLoopColon: true
119+
SpaceInEmptyBlock: false
120+
SpaceInEmptyParentheses: false
121+
SpacesBeforeTrailingComments: 1
122+
SpacesInAngles: false
123+
SpacesInConditionalStatement: false
124+
SpacesInContainerLiterals: true
125+
SpacesInCStyleCastParentheses: false
126+
SpacesInParentheses: false
127+
SpacesInSquareBrackets: false
128+
SpaceBeforeSquareBrackets: false
129+
Standard: Latest
130+
StatementMacros:
131+
- Q_UNUSED
132+
- QT_REQUIRE_VERSION
133+
TabWidth: 8
134+
UseCRLF: false
135+
UseTab: Never
136+
...
137+

Semaphore/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Exclude these files from root level .gitignore
2+
build
3+
4+
.vscode/.cortex-debug*

Semaphore/.vscode/launch.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"configurations": [
3+
{
4+
"cwd": "${workspaceFolder}",
5+
"executable": "build/Semaphore.elf",
6+
"configFiles": [
7+
"stm32l4discovery.cfg"
8+
],
9+
"postLaunchCommands": [
10+
"monitor arm semihosting enable",
11+
],
12+
"name": "Debug Microcontroller",
13+
"request": "launch",
14+
"type": "cortex-debug",
15+
"servertype": "openocd"
16+
},
17+
{
18+
"cwd": "${workspaceFolder}",
19+
"executable": "build/Semaphore.elf",
20+
"configFiles": [
21+
"stm32l4discovery.cfg"
22+
],
23+
"postLaunchCommands": [
24+
"load",
25+
"monitor arm semihosting enable",
26+
],
27+
"name": "Load and Debug Microcontroller",
28+
"request": "launch",
29+
"type": "cortex-debug",
30+
"servertype": "openocd"
31+
}
32+
]
33+
}

Semaphore/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
3+
}

Semaphore/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
# Set your constants here
4+
set(PROJECT_NAME "Semaphore")
5+
set(CONTROLLER_NAME "STM32L475xx")
6+
7+
# Project start
8+
set(CMAKE_TOOLCHAIN_FILE "cmake/toolchain.cmake")
9+
project(${PROJECT_NAME}
10+
LANGUAGES C ASM
11+
)
12+
13+
# Get compiler and linker flags from here
14+
include(cmake/flags.cmake)
15+
16+
set(USER_PROJECT_TARGET "${PROJECT_NAME}.elf")
17+
add_executable(${USER_PROJECT_TARGET} application/main.c)
18+
19+
target_compile_definitions(${USER_PROJECT_TARGET} PRIVATE
20+
${CONTROLLER_NAME}
21+
)
22+
target_compile_options(${USER_PROJECT_TARGET} PRIVATE
23+
${COMMON_C_FLAGS}
24+
${USER_C_FLAGS}
25+
)
26+
target_link_options(${USER_PROJECT_TARGET} PRIVATE
27+
${COMMON_C_FLAGS}
28+
${USER_LINK_FLAGS}
29+
)
30+
31+
# TODO, Add more subdirectories here
32+
add_subdirectory(l0_lowlevel)
33+
add_subdirectory(modules)
34+
add_subdirectory(application)
35+
36+
# Tools and various custom targets
37+
include(cmake/targets.cmake)

Semaphore/application/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
target_sources(${USER_PROJECT_TARGET} PRIVATE
2+
osKernel.c
3+
osKernel.S
4+
)
5+
target_include_directories(${USER_PROJECT_TARGET} PRIVATE
6+
.
7+
)

Semaphore/application/osKernel.S

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.global currentPt
2+
3+
.text 32
4+
.align 4
5+
.syntax unified
6+
7+
.global SysTick_Handler
8+
.thumb_func
9+
SysTick_Handler:
10+
CPSID i
11+
PUSH {R4-R11}
12+
LDR R0,=currentPt @ R0 points to currentPt
13+
LDR R1,[R0] @ Load currentPt to R1
14+
STR SP,[R1] @ Store SP to address at current->stackPt
15+
LDR R1, [R1, #4] @ r1 = currentPt->next
16+
STR R1, [R0] @ currentPt = r1
17+
LDR SP, [R1] @ SP = currentPt->stackPt
18+
POP {R4-R11}
19+
CPSIE i
20+
BX LR
21+
22+
23+
.global osSchedulerLaunch
24+
.thumb_func
25+
osSchedulerLaunch:
26+
LDR R0,=currentPt
27+
LDR R2,[R0] @ R2 = currentPt
28+
LDR SP,[R2] @ SP = currentPt->stackPt
29+
POP {R4-R11}
30+
POP {R0-R3}
31+
POP {R12}
32+
ADD SP,SP,#4
33+
POP {LR}
34+
ADD SP,SP,#4
35+
CPSIE i
36+
BX LR

Semaphore/application/osKernel.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "osKernel.h"
2+
3+
#include "stm32l4xx.h"
4+
5+
// Constants
6+
#define NUM_OF_THREADS 3
7+
#define STACKSIZE 100
8+
struct tcb {
9+
uint32_t *stackPt;
10+
struct tcb *nextPt;
11+
};
12+
typedef struct tcb tcbType;
13+
14+
// Static functions
15+
static void osKernelStackInit(int thread_number);
16+
17+
// Calls the assembly function
18+
extern void osSchedulerLaunch(void);
19+
20+
// State variables
21+
static tcbType tcbs[NUM_OF_THREADS];
22+
static uint32_t TCB_STACK[NUM_OF_THREADS][STACKSIZE];
23+
24+
// * NOTE, Do not make this static since it is used by the osKernel.S file
25+
tcbType *currentPt;
26+
27+
uint8_t osKernelAddThreads(void (*thread0)(void), void (*thread1)(void),
28+
void (*thread2)(void)) {
29+
__disable_irq();
30+
31+
// Create a Circular Linked list here
32+
tcbs[0].nextPt = &tcbs[1];
33+
tcbs[1].nextPt = &tcbs[2];
34+
tcbs[2].nextPt = &tcbs[0];
35+
36+
osKernelStackInit(0);
37+
TCB_STACK[0][STACKSIZE - 2] = (uint32_t)thread0;
38+
39+
osKernelStackInit(1);
40+
TCB_STACK[1][STACKSIZE - 2] = (uint32_t)thread1;
41+
42+
osKernelStackInit(2);
43+
TCB_STACK[2][STACKSIZE - 2] = (uint32_t)thread2;
44+
45+
// Initialize the currentPt
46+
currentPt = &tcbs[0];
47+
__enable_irq();
48+
return 1;
49+
}
50+
51+
void osKernelLaunch(uint32_t quanta) {
52+
// Disable the SysTick
53+
SysTick->CTRL = 0;
54+
55+
// Reset the initial value
56+
SysTick->VAL = 0;
57+
58+
// Set the lowest priority (largest possible value)
59+
NVIC_SetPriority(SysTick_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
60+
61+
// Set the counter
62+
const uint32_t MILLIS_PRESCALER = SystemCoreClock / 1000;
63+
SysTick->LOAD = quanta * MILLIS_PRESCALER - 1;
64+
65+
// Enable the SysTick source
66+
// enable = 1, tickint = 1, CLKSOURCE=Processor Clock (SystemCoreClock)
67+
SysTick->CTRL = 0x07;
68+
osSchedulerLaunch();
69+
}
70+
71+
static const uint8_t PENDSTSET = 26;
72+
void osKernelYield(void) {
73+
// Clear SysTick VAL since it can contain arbritary values
74+
SysTick->VAL = 0;
75+
76+
// Raise the SysTick Interrupt
77+
SCB->ICSR = (1 << PENDSTSET);
78+
}
79+
80+
// Static functions
81+
static void osKernelStackInit(int thread_number) {
82+
// STACKSIZE = 100
83+
// Last value is 99
84+
// 100 - 16 => 84 uint32_t value to be stored
85+
tcbs[thread_number].stackPt = &TCB_STACK[thread_number][STACKSIZE - 16];
86+
87+
// xPSR value needs to be in the thumb state
88+
// xPSR, PC, LR, r12, r3, r2, r1, r0
89+
// r11, r10, r9, r8, r7, r6, r5, r4
90+
TCB_STACK[thread_number][STACKSIZE - 1] = 0x01000000;
91+
}

Semaphore/application/osKernel.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef OS_KERNEL_H_
2+
#define OS_KERNEL_H_
3+
4+
#include <stdint.h>
5+
6+
uint8_t osKernelAddThreads(void (*thread0)(void), void (*thread1)(void),
7+
void (*thread2)(void));
8+
9+
void osKernelLaunch(uint32_t quanta);
10+
11+
// For cooperative scheduling
12+
// Raise an interrupt to the SysTick Handler
13+
void osKernelYield(void);
14+
15+
#endif

0 commit comments

Comments
 (0)