Skip to content

Commit d97433a

Browse files
committed
sch_event_management.c updated
Added the smiplest function of arbitraging the execution of processes
1 parent 03aba96 commit d97433a

8 files changed

Lines changed: 152 additions & 62 deletions

File tree

examples/stm32f4x/app/src/main.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "sch_event_management.h"
12
#include "stm32f4xx_hal.h"
23
#include "main.h"
34
#include "sch.h"
@@ -30,7 +31,7 @@ int main(void)
3031
MX_GPIO_Init();
3132
MX_USART2_UART_Init();
3233

33-
/*sch_task_t first_task;
34+
sch_task_t first_task;
3435
sch_task_t second_task;
3536
sch_task_t echo_data;
3637
sch_task_t another_system_task;
@@ -46,13 +47,15 @@ int main(void)
4647

4748
sch_task_create(&echo_data, -4, 3, echo_task_dispatch, echo_task_init);
4849
sch_task_activate(&echo_data);
49-
sch_task_run(&echo_data, NULL);*/
50+
sch_task_run(&echo_data, NULL);
51+
52+
sch_event_management();
5053

5154
/*char buf[40] = {0};
5255
snprintf(buf, sizeof(buf), "%d\r\n", sch_find_most_significant_task(sch_preempt_tasks_ready_set));
5356
HAL_UART_Transmit(&huart2, (uint8_t*)buf, strlen(buf), HAL_MAX_DELAY);*/
5457

55-
sch_context_change_interrupt_check = 1;
58+
/*sch_context_change_interrupt_check = 1;
5659
sch_context_change();
5760
if (sch_context_change_interrupt_check == 0)
5861
{
@@ -62,7 +65,7 @@ int main(void)
6265
{
6366
HAL_UART_Transmit(&huart2, (uint8_t*)"sch_context_change_interrupt_check != 0", \
6467
strlen("sch_context_change_interrupt_check != 0"), HAL_MAX_DELAY);
65-
}
68+
}*/
6669

6770

6871
while (1)

sch/inc/sch.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
extern "C" {
1515
#endif
1616

17-
#include <stdio.h>
17+
#include <stddef.h>
1818
#include <stdint.h>
1919

2020
#include "sch_config.h"
@@ -23,6 +23,14 @@ extern "C" {
2323
#include "sch_task_management.h"
2424
#include "sch_event_management.h"
2525

26+
/*--Functions----------------------------------------------------------------*/
27+
28+
/**
29+
* @brief Wrapper function for starting the arbitraging the processes
30+
*
31+
*/
32+
uint8_t sch_run(void);
33+
2634
#ifdef __cplusplus
2735
}
2836
#endif

sch/inc/sch_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef SCH_COMMON_H
1010
#define SCH_COMMON_H
1111

12-
#include <cstdint>
12+
#include <stdint.h>
1313
#ifdef __cplusplus
1414
extern "C" {
1515
#endif

sch/inc/sch_config.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@ extern "C" {
1616

1717
/**
1818
* @brief the SCH_MAX_QUANTITY_OF_TASKS macros
19-
* defines the max quantity of tasks
19+
* defines the max quantity of tasks
20+
*
2021
*/
2122
#define SCH_MAX_QUANTITY_OF_TASKS 3
2223

2324
/**
2425
* @brief mode of SCH arbitration. There're options:
25-
* Manual (SCH_ARBITRATION_MODE_MANUAL)
26-
* Systick (SCH_ARBITRATION_MODE_SYSTICK)
26+
* Manual (SCH_ARBITRATION_MODE_MANUAL)
27+
* Systick (SCH_ARBITRATION_MODE_SYSTICK)
2728
*
2829
*/
2930
#define SCH_ARBITRATION_MODE SCH_ARBITRATION_MODE_MANUAL
3031

32+
/**
33+
* @brief Turning on/off the events. There're options:
34+
* On (SCH_EVENTS_ON)
35+
* Off (SCH_EVENTS_OFF)
36+
*
37+
*/
38+
#define SCH_EVENTS_STATE SCH_EVENTS_OFF
39+
3140
#ifdef __cplusplus
3241
}
3342
#endif

sch/inc/sch_event_management.h

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* @file sch_event_management.h
3+
* @author Mike Strangewood (michael.a.strangewood@gmail.com)
4+
* @brief This file contains any solutions for implementing the communication
5+
* between processes (or between a process and something outside)
6+
* @version 0.1
7+
* @date 2026-04-07
8+
*
9+
*/
10+
11+
/**
12+
* @note: In commentaries I'll name communcation between processes by the name
13+
* "P2P", and communication between a process and environment by the "P2E" name
14+
*
15+
*/
16+
117
#ifndef SCH_EVENT_MANAGEMENT_H
218
#define SCH_EVENT_MANAGEMENT_H
319

@@ -7,20 +23,43 @@
723
extern "C" {
824
#endif
925

10-
typedef struct {
11-
uint16_t sig;
12-
} sch_event_header;
26+
#if SCH_EVENTS_STATE == SCH_EVENTS_ON
1327

14-
typedef struct {
15-
sch_event_header event_header;
16-
int16_t first_data;
17-
int16_t second_data;
18-
} sch_event;
28+
/*--Structures---------------------------------------------------------------*/
29+
30+
/**
31+
* @brief Structure for defining the header of packet (a particle) of
32+
* communication between processes
33+
*
34+
*/
35+
typedef struct {
36+
uint16_t sig;
37+
} sch_event_header;
1938

20-
void sch_event_management(sch_task_t* sch_task);
39+
/**
40+
* @brief Structure for defining for the package of communication intself
41+
*
42+
*/
43+
typedef struct {
44+
sch_event_header event_header;
45+
int16_t first_data;
46+
int16_t second_data;
47+
} sch_event;
48+
49+
/*--Functions----------------------------------------------------------------*/
50+
51+
/**
52+
* @brief Portable function. Defines the "process" of scheduling and arbitrating
53+
* the processes
54+
*
55+
* @param sch_task
56+
*/
57+
uint8_t sch_event_management(void);
58+
59+
#ifdef __cplusplus
60+
}
61+
#endif
2162

22-
#ifdef __cplusplus
23-
}
2463
#endif
2564

2665
#endif

sch/port/stm32f4xx/sch_context_change.s

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313
@ -----------------------------------------------------------------------------
1414

15-
PendSV_Handler:
16-
push {lr}
17-
ldr r0, =sch_system_tasks_ready_set
18-
ldr r0, [r0
19-
bl sch_find_most_significant_task
20-
cbz r0, .exit_pendsv
21-
sub r0, r0, #1
22-
bl sch_call_dispatch[cite: 17]
23-
.exit_pendsv:
24-
pop {pc}
15+
@ PendSV_Handler:
16+
@ push {lr}
17+
@ ldr r0, =sch_system_tasks_ready_set
18+
@ ldr r0, [r0
19+
@ bl sch_find_most_significant_task
20+
@ cbz r0, .exit_pendsv
21+
@ sub r0, r0, #1
22+
@ bl sch_call_dispatch[cite: 17]
23+
@.exit_pendsv:
24+
@ pop {pc}
2525

2626
@ -----------------------------------------------------------------------------
2727

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1+
/**
2+
* @file sch_event_management.c
3+
* @author Mike Strangewood (michael.a.strangewood@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-07
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
112
#include "sch.h"
213
#include "sch_config.h"
314
#include "sch_event_management.h"
415

5-
/*
6-
todo:
7-
i know you do read this hehe
8-
9-
*/
16+
/*--Functions----------------------------------------------------------------*/
1017

11-
void sch_event_management(sch_task_t* sch_task) {
12-
#if SCH_ARBITRATION_MODE == SCH_ARBITRATION_MODE_SYSTICK
18+
/**
19+
* @brief Portable function. Defines the "process" of scheduling and arbitrating
20+
* the processes
21+
*
22+
* @param sch_task
23+
*/
24+
uint8_t sch_event_management(void) {
25+
#if SCH_EVENTS_STATE == SCH_EVENTS_ON
26+
#if SCH_ARBITRATION_MODE == SCH_ARBITRATION_MODE_SYSTICK
1327

14-
#elif SCH_ARBITRATION_MODE == SCH_ARBITRATION_MODE_MANUAL
15-
switch (sch_task -> priority) {
16-
case 1:
17-
//other cases
18-
}
28+
#elif SCH_ARBITRATION_MODE == SCH_ARBITRATION_MODE_MANUAL
29+
uint8_t priority = sch_find_most_significant_task(sch_preempt_tasks_ready_set);
30+
if (priority > 0) {
31+
sch_task_t* task_to_run = sch_task_registry[priority - 1];
32+
if (task_to_run != NULL) {
33+
sch_task_run(task_to_run, NULL);
34+
}
35+
}
36+
#endif
37+
#elif SCH_EVENTS_STATE == SCH_EVENTS_OFF
38+
return 0;
1939
#endif
40+
return 0;
2041
}

sch/src/sch.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include "sch.h"
13+
#include "sch_event_management.h"
1314

1415
int32_t sch_system_tasks_ready_set = 0;
1516
int32_t sch_preempt_tasks_ready_set = 0;
@@ -26,14 +27,14 @@ sch_task_t* sch_task_registry[SCH_MAX_QUANTITY_OF_TASKS] = {0};
2627
* @param init
2728
*/
2829
void sch_task_create(sch_task_t* const task, sch_task_priority priority, sch_irq irq, sch_task_dispatch_fn dispatch, sch_task_presetting_fn presetting) {
29-
if (task != (sch_task_t* const)NULL) {
30-
task->irq = irq;
31-
task->priority = priority;
32-
task->task_dispatch = dispatch;
33-
task->task_presetting = presetting;
30+
if (task != (sch_task_t* const)NULL) {
31+
task->irq = irq;
32+
task->priority = priority;
33+
task->task_dispatch = dispatch;
34+
task->task_presetting = presetting;
3435

35-
task->task_presetting(task);
36-
}
36+
task->task_presetting(task);
37+
}
3738
}
3839

3940
/**
@@ -43,15 +44,15 @@ void sch_task_create(sch_task_t* const task, sch_task_priority priority, sch_irq
4344
* @param param
4445
*/
4546
void sch_task_run(sch_task_t* const task, void* param) {
46-
SCH_ASSERT((task->priority > 0) && (task->priority < SCH_MAX_QUANTITY_OF_TASKS));
47-
(*task->task_dispatch)(task);
47+
SCH_ASSERT((task->priority > 0) && (task->priority < SCH_MAX_QUANTITY_OF_TASKS));
48+
(*task->task_dispatch)(task);
4849

49-
//afterword
50-
if (task->priority < 0) {
51-
sch_system_tasks_ready_set &= ~(1U << (uint8_t)(-task->priority - 1));
52-
} else {
53-
sch_preempt_tasks_ready_set &= ~(1U << (uint8_t)(task->priority - 1));
54-
}
50+
//afterword
51+
if (task->priority < 0) {
52+
sch_system_tasks_ready_set &= ~(1U << (uint8_t)(-task->priority - 1));
53+
} else {
54+
sch_preempt_tasks_ready_set &= ~(1U << (uint8_t)(task->priority - 1));
55+
}
5556
}
5657

5758
/**
@@ -60,9 +61,18 @@ void sch_task_run(sch_task_t* const task, void* param) {
6061
* @param task
6162
*/
6263
void sch_task_activate(sch_task_t* const task) {
63-
if (task->priority < 0) {
64-
sch_system_tasks_ready_set |= (1U << (uint8_t)(-task->priority - 1));
65-
} else {
66-
sch_preempt_tasks_ready_set |= (1U << (uint8_t)(task->priority - 1));
67-
}
64+
if (task->priority < 0) {
65+
sch_system_tasks_ready_set |= (1U << (uint8_t)(-task->priority - 1));
66+
} else {
67+
sch_preempt_tasks_ready_set |= (1U << (uint8_t)(task->priority - 1));
68+
}
69+
}
70+
71+
/**
72+
* @brief Wrapper function for starting the arbitraging the processes
73+
*
74+
*/
75+
uint8_t sch_run(void) {
76+
sch_event_management();
77+
return 0;
6878
}

0 commit comments

Comments
 (0)