finis week 1.6

This commit is contained in:
2025-10-07 17:56:04 +02:00
parent a31096e720
commit 0732a9d82c
3 changed files with 251 additions and 112 deletions

View File

@@ -16,3 +16,5 @@ auther:
![](/report-2/week_1.4.md) ![](/report-2/week_1.4.md)
![](/report-2/week_1.5.md) ![](/report-2/week_1.5.md)
![](/report-2/week_1.6.md)

View File

@@ -24,20 +24,20 @@ There already is an counter in the task struct. but no function that decrements
```c ```c
void decrement_sleeping_delay() void decrement_sleeping_delay()
{ {
for (usize_t i=0; i < MAX_TASKS; i++) for (usize_t i=0; i < MAX_TASKS; i++)
{ {
if (taskList[i].state == SLEEPING_DELAY) if (taskList[i].state == SLEEPING_DELAY)
{ {
if (taskList[i].counter == 0) if (taskList[i].counter == 0)
{ {
taskList[i].state = READY; taskList[i].state = READY;
} }
else else
{ {
taskList[i].counter--; taskList[i].counter--;
} }
} }
} }
} }
``` ```
@@ -48,13 +48,13 @@ The `SysTick_Handeler` now look like the following
```c ```c
void SysTick_Handler(void) void SysTick_Handler(void)
{ {
SysTick_flag = true; SysTick_flag = true;
//decrement counter for task in SLEEPING_DELAY //decrement counter for task in SLEEPING_DELAY
decrement_sleeping_delay(); decrement_sleeping_delay();
//select the next task //select the next task
taskToExecute = schedule(); taskToExecute = schedule();
//request context switch //request context switch
SCB->ICSR |= (1<<28); SCB->ICSR |= (1<<28);
} }
``` ```
@@ -65,9 +65,9 @@ As last the delay function itself.
```c ```c
void delay(uint32_t ticks) void delay(uint32_t ticks)
{ {
currentTask->state = SLEEPING_DELAY; currentTask->state = SLEEPING_DELAY;
currentTask->counter = ticks; currentTask->counter = ticks;
taskYield(); taskYield();
} }
``` ```
@@ -84,12 +84,12 @@ This implementation works, only the delays are a bit off. To make it a nice coun
```c ```c
void blocking_delay(unsigned int ticks) void blocking_delay(unsigned int ticks)
{ {
while (ticks != 0) { while (ticks != 0) {
extern bool SysTick_flag; extern bool SysTick_flag;
while (SysTick_flag == false); // busy wait while (SysTick_flag == false); // busy wait
SysTick_flag = false; SysTick_flag = false;
ticks--; ticks--;
} }
} }
``` ```
@@ -104,9 +104,9 @@ void toggleBlue(void)
{ {
while(1) while(1)
{ {
GPIOD->ODR ^= 1 << BLUE; GPIOD->ODR ^= 1 << BLUE;
delay(800-1); delay(800-1);
SysTick->LOAD = 2 * CLOCK_FREQ_IN_KHz - 1; SysTick->LOAD = 2 * CLOCK_FREQ_IN_KHz - 1;
} }
} }
``` ```
@@ -123,10 +123,10 @@ The control register is a special register to it need to be set and read with sp
.global toUnprivileged .global toUnprivileged
toUnprivileged: toUnprivileged:
mrs R0, CONTROL // copy control register to R0 mrs R0, CONTROL // copy control register to R0
ORR R0, R0, #1 // clear bit 0 (nPRIV) ORR R0, R0, #1 // clear bit 0 (nPRIV)
msr CONTROL, R0 // copy R0 to control register msr CONTROL, R0 // copy R0 to control register
bx lr bx lr
``` ```
And called this function in the `PendSV_Handeler` And called this function in the `PendSV_Handeler`
@@ -137,22 +137,22 @@ void toUnprivileged(void);
__attribute__((naked)) // No function entry and exit code __attribute__((naked)) // No function entry and exit code
void PendSV_Handler(void) void PendSV_Handler(void)
{ {
//Push {R4-R11} context to PSP //Push {R4-R11} context to PSP
pushRegistersToCurrentPSP(); pushRegistersToCurrentPSP();
//Save the new stack pointer after the push //Save the new stack pointer after the push
currentTask->stack = readPSP(); currentTask->stack = readPSP();
currentTask = taskToExecute; currentTask = taskToExecute;
//Load the new stack pointer from (new) currentTask //Load the new stack pointer from (new) currentTask
writePSP(currentTask->stack); writePSP(currentTask->stack);
//Pop {R4-R11} context from PSP //Pop {R4-R11} context from PSP
popRegistersFromCurrentPSP(); popRegistersFromCurrentPSP();
toUnprivileged(); // <--- this line was added toUnprivileged(); // <--- this line was added
returnToPSP(); returnToPSP();
} }
``` ```
@@ -192,11 +192,11 @@ void toggleBlue(void)
while(1) while(1)
{ {
GPIOD->ODR ^= 1 << BLUE; GPIOD->ODR ^= 1 << BLUE;
delay(800-1); delay(800-1);
setSysTick_10ms(); setSysTick_10ms();
GPIOD->ODR ^= 1 << BLUE; GPIOD->ODR ^= 1 << BLUE;
delay(800-1); delay(800-1);
setSysTick_1ms(); setSysTick_1ms();
} }
} }
``` ```
@@ -235,34 +235,34 @@ In the memory map I found a littleendien encoded address witch was very close to
```c ```c
void SVC_Handler(void) void SVC_Handler(void)
{ {
uint32_t* sp = readPSP(); uint32_t* sp = readPSP();
// get PC location // get PC location
sp = sp + 6; sp = sp + 6;
// get PC // get PC
uint16_t* pc = (uint16_t*)*sp; uint16_t* pc = (uint16_t*)*sp;
// get SVC intruction // get SVC intruction
uint16_t instruction = *(pc - 1); uint16_t instruction = *(pc - 1);
if ((instruction & 0xFF00) != 0xDF00) { if ((instruction & 0xFF00) != 0xDF00) {
return; return;
} }
uint8_t svc_id = instruction & 0x00FF; uint8_t svc_id = instruction & 0x00FF;
switch (svc_id) switch (svc_id)
{ {
case 1: // taskYield case 1: // taskYield
taskToExecute = schedule(); taskToExecute = schedule();
SCB->ICSR |= (1<<28); SCB->ICSR |= (1<<28);
break; break;
case 2: // setSysTick_10ms case 2: // setSysTick_10ms
SysTick->LOAD = 10 * CLOCK_FREQ_IN_KHz - 1; SysTick->LOAD = 10 * CLOCK_FREQ_IN_KHz - 1;
break; break;
case 3: // setSysTick_1ms case 3: // setSysTick_1ms
SysTick->LOAD = 1 * CLOCK_FREQ_IN_KHz - 1; SysTick->LOAD = 1 * CLOCK_FREQ_IN_KHz - 1;
break; break;
} }
} }
``` ```
@@ -347,49 +347,49 @@ I added a new pointer `nextTaskPtr` to store the highest priority task that is r
```c ```c
task * schedule() task * schedule()
{ {
task* nextTaskPtr = NULL; task* nextTaskPtr = NULL;
task* tempTaskPtr = currentTask; task* tempTaskPtr = currentTask;
task* idleTaskPtr = &taskList[IDLE_TASK]; task* idleTaskPtr = &taskList[IDLE_TASK];
int teller=0; int teller=0;
do do
{ {
tempTaskPtr++; tempTaskPtr++;
if( (tempTaskPtr-1) == idleTaskPtr || tempTaskPtr == idleTaskPtr) if( (tempTaskPtr-1) == idleTaskPtr || tempTaskPtr == idleTaskPtr)
{ {
//since idle task is the last in the list, we've reached the end //since idle task is the last in the list, we've reached the end
//and need to continue at the beginning //and need to continue at the beginning
tempTaskPtr = &taskList[0]; tempTaskPtr = &taskList[0];
} }
if (tempTaskPtr->state != READY) if (tempTaskPtr->state != READY)
{ {
continue; continue;
} }
if (nextTaskPtr == NULL) if (nextTaskPtr == NULL)
{ {
nextTaskPtr = tempTaskPtr; nextTaskPtr = tempTaskPtr;
continue; continue;
} }
if (tempTaskPtr->priority < nextTaskPtr->priority) if (tempTaskPtr->priority < nextTaskPtr->priority)
{ {
nextTaskPtr = tempTaskPtr; nextTaskPtr = tempTaskPtr;
continue; continue;
} }
} while (teller++ < MAX_TASKS); } while (teller++ < MAX_TASKS);
//if no task was found //if no task was found
if(nextTaskPtr == NULL) if(nextTaskPtr == NULL)
{ {
//idle task //idle task
nextTaskPtr = idleTaskPtr; nextTaskPtr = idleTaskPtr;
} }
return nextTaskPtr; return nextTaskPtr;
} }
``` ```

137
report-2/week_1.6.md Normal file
View File

@@ -0,0 +1,137 @@
# Week 1.6
## Assginment 6.1
> A program consists of four tasks, $T_1$ through $T_4$. These tasks do not use shared resources. In the table, $i$ represents the task number, $T_i$ represents the period of task , and $C_i$ represents the maximum execution time of task $i$. The deadline of each task is equal to it's period.
>
> | i | $T_i$ | $C_i$ |
> |---|---------|--------|
> | 1 | $100ms$ | $50ms$ |
> | 2 | $280ms$ | $45ms$ |
> | 3 | $200ms$ | $20ms$ |
> | 4 | $300ms$ | $40ms$ |
>
> A) Determine the schedulability of these tasks using the "Utilization based schedulability test" Provide the necessary calculations and draw your conclusions.
$$
U=\sum_{i=1}^N{\frac{C_i}{T_i}}\le N(2^{1/N}-1)
$$
$$
U=\frac{C_1}{T_1}+\frac{C_2}{T_2}+\frac{C_3}{T_3}+\frac{C_4}{T_4}\le 4\cdot(2^{1/4}-1)
$$
$$
U=\frac{0.05}{0.1}+\frac{0.045}{0.28}+\frac{0.02}{0.2}+\frac{0.04}{0.3}\le N(2^{1/4}-1)
$$
$$
U=0.894047619048\le0.756828460011=false
$$
This equetion cannot say if all the deadline are possible to make in this case.
> B) Determine the priorities $P_i$ of the various tasks when using FPS-RMPA (Fixed-priority Preemptive Scheduling Rate Monotonic Priority Assignment). The system has four different priorities (1 through 4), with 4 being the highest.
The task with the shortest deadline should get the highest priority. The deadline are equal to $T_i$, this result in the following priorities:
| i | $T_i$ | $C_i$ | $P_i$ |
| --- | ------- | ------ | ----- |
| 1 | $100ms$ | $50ms$ | 4 |
| 2 | $280ms$ | $45ms$ | 2 |
| 3 | $200ms$ | $20ms$ | 3 |
| 4 | $300ms$ | $40ms$ | 1 |
> C) Calculate for all tasks whether the deadline will be met, and if the deadline is met, provide the response time $R_i$.
$$
R_i=C_i+\sum_{j∈hp(i)}{\left\lceil \frac{R_i}{T_j} \right\rceil} C_j
$$
$$
R_1=C_1+\sum_{j∈hp(1)}{\left\lceil \frac{R_1}{T_j} \right\rceil} C_j=C_1=50ms
$$
$$
R_3=C_3+\sum_{j∈hp(3)}{\left\lceil \frac{R_3}{T_j} \right\rceil} C_j=C_3+\left\lceil \frac{R_3}{T_1} \right\rceil C_1
$$
$$
\Rightarrow w_{3,1}=C_3+\left\lceil \frac{C_3}{T_1} \right\rceil C_1=0.02+\left\lceil \frac{0.02}{0.1} \right\rceil 0.05=0.07
$$
$$
\rightarrow w_{3,2}=C_3+\left\lceil \frac{w_{3,1}}{T_1} \right\rceil C_1=0.02+\left\lceil \frac{0.02}{0.1} \right\rceil 0.05=0.07
$$
$$
R_3=70ms
$$
$w_{3,2}$ is the same as $w_{3,1}$ so $R_3$ is equal to both. This is lower then $T_3$ ($200ms$) so this is still possible.
$$
R_2=C_2+\sum_{j∈hp(2)}{\left\lceil \frac{R_2}{T_j} \right\rceil C_j}=C_2+\left\lceil \frac{R_2}{T_1} \right\rceil C_1+\left\lceil \frac{R_2}{T_3} \right\rceil C_3
$$
$$
\Rightarrow w_{2,1}=C_2+\left\lceil \frac{C_2}{T_1} \right\rceil C_1+\left\lceil \frac{C_2}{T_3} \right\rceil C_3=0.045+\left\lceil \frac{0.045}{0.1} \right\rceil 0.05+\left\lceil \frac{0.045}{0.2} \right\rceil 0.02=0.115
$$
$$
\Rightarrow w_{2,2}=C_2+\left\lceil \frac{w_{2,1}}{T_1} \right\rceil C_1+\left\lceil \frac{w_{2,1}}{T_3} \right\rceil C_3=0.045+\left\lceil \frac{0.115}{0.1} \right\rceil 0.05+\left\lceil \frac{0.115}{0.2} \right\rceil 0.02=0.165
$$
$$
\Rightarrow w_{2,3}=C_2+\left\lceil \frac{w_{2,2}}{T_1} \right\rceil C_1+\left\lceil \frac{w_{2,2}}{T_3} \right\rceil C_3
=0.045+\left\lceil \frac{0.165}{0.1} \right\rceil 0.05+\left\lceil \frac{0.165}{0.2} \right\rceil 0.02=0.165
$$
$$
R_2=165ms
$$
$w_{2,2}$ is the same as $w_{2,1}$ so $R_2$ is equal to both. This is lower then $T_2$ ($280ms$) so this is still possible.
$$
R_4=C_4+\sum_{j∈hp(4)}{\left\lceil \frac{R_4}{T_j} \right\rceil C_j}=C_4+\left\lceil \frac{R_4}{T_1} \right\rceil C_1+\left\lceil \frac{R_4}{T_3} \right\rceil C_3+\left\lceil \frac{R_4}{T_2} \right\rceil C_2
$$
$$
\Rightarrow w_{4,1}=
C_4+\left\lceil \frac{C_4}{T_1} \right\rceil C_1
+\left\lceil \frac{C_4}{T_3} \right\rceil C_3
+\left\lceil \frac{C_4}{T_2} \right\rceil C_2
=0.04+\left\lceil \frac{0.04}{0.1} \right\rceil 0.05
+\left\lceil \frac{0.04}{0.2} \right\rceil 0.02
+\left\lceil \frac{0.04}{0.28} \right\rceil 0.045
=0.155
$$
$$
\Rightarrow w_{4,2}=
C_4+\left\lceil \frac{w_{4,1}}{T_1} \right\rceil C_1
+\left\lceil \frac{w_{4,1}}{T_3} \right\rceil C_3
+\left\lceil \frac{w_{4,1}}{T_2} \right\rceil C_2
=0.04+\left\lceil \frac{0.155}{0.1} \right\rceil 0.05
+\left\lceil \frac{0.155}{0.2} \right\rceil 0.02
+\left\lceil \frac{0.155}{0.28} \right\rceil 0.045
=0.205
$$
$$
\Rightarrow w_{4,3}=
C_4+\left\lceil \frac{w_{4,2}}{T_1} \right\rceil C_1
+\left\lceil \frac{w_{4,2}}{T_3} \right\rceil C_3
+\left\lceil \frac{w_{4,2}}{T_2} \right\rceil C_2
=0.04+\left\lceil \frac{0.205}{0.1} \right\rceil 0.05
+\left\lceil \frac{0.205}{0.2} \right\rceil 0.02
+\left\lceil \frac{0.205}{0.28} \right\rceil 0.045
=0.275
$$
$$
\Rightarrow w_{4,4}=
C_4+\left\lceil \frac{w_{4,3}}{T_1} \right\rceil C_1
+\left\lceil \frac{w_{4,3}}{T_3} \right\rceil C_3
+\left\lceil \frac{w_{4,3}}{T_2} \right\rceil C_2
=0.04+\left\lceil \frac{0.275}{0.1} \right\rceil 0.05
+\left\lceil \frac{0.275}{0.2} \right\rceil 0.02
+\left\lceil \frac{0.275}{0.28} \right\rceil 0.045
=0.275
$$
$$
R_4=275ms
$$
$w_{4,3}$ is the same as $w_{4,4}$ so $R_4$ is equal to both. This is lower then $T_4$ ($300ms$) so FPS-RMPA will not miss any deadlines.