Skip to content

Commit

Permalink
Try minimalistic cooperative scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Jan 9, 2024
1 parent 9e97767 commit 2e5430c
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 76 deletions.
12 changes: 9 additions & 3 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,18 @@ $(PATH_BIN)/refapp.8xp :
@-$(ECHO)
@-$(ECHO) +++ assembling $(notdir $(PATH_SRC)/startup/crt0.s)
@-$(AS) $(AFLAGS) -c -o $(PATH_OBJ)/crt0.rel $(PATH_SRC)/startup/crt0.s
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_led_util.c)
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_acc.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/app_acc.rel $(PATH_SRC)/app/app_acc.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_led.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/app_led.rel $(PATH_SRC)/app/app_led.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/mcal/app/app_led_util.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/app_led_util.rel $(PATH_SRC)/app/app_led_util.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/mcal/mcal_gpt.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/mcal_gpt.rel $(PATH_SRC)/mcal/mcal_gpt.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_led.c) and linking/extracting to $(notdir $(PATH_BIN)/refapp.ihx)
@-$(CC) $(CFLAGS) -o $(PATH_BIN)/refapp.ihx $(PATH_SRC)/app/app_led.c $(PATH_OBJ)/app_led_util.rel $(PATH_OBJ)/crt0.rel $(PATH_OBJ)/mcal_gpt.rel
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/os/os.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/os.rel $(PATH_SRC)/os/os.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_main.c) and linking/extracting to $(notdir $(PATH_BIN)/refapp.ihx)
@-$(CC) $(CFLAGS) -o $(PATH_BIN)/refapp.ihx $(PATH_SRC)/app/app_main.c $(PATH_OBJ)/app_acc.rel $(PATH_OBJ)/app_led.rel $(PATH_OBJ)/app_led_util.rel $(PATH_OBJ)/crt0.rel $(PATH_OBJ)/mcal_gpt.rel $(PATH_OBJ)/os.rel
@-$(ECHO) +++ creating binary $(notdir $(PATH_BIN)/refapp.bin)
@-$(OBJCOPY) -Iihex -Obinary $(PATH_BIN)/refapp.ihx $(PATH_BIN)/refapp.bin
@-$(ECHO) +++ creating on-calc app $(notdir $(PATH_BIN)/refapp.8xp)
Expand Down
4 changes: 4 additions & 0 deletions build/ti84-ref_app.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
<None Include="Makefile" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\app\app_acc.c" />
<ClCompile Include="..\src\app\app_led.c" />
<ClCompile Include="..\src\app\app_led_util.c" />
<ClCompile Include="..\src\app\app_main.c" />
<ClCompile Include="..\src\mcal\mcal_gpt.c" />
<ClCompile Include="..\src\os\os.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\app\app_led_util.h" />
<ClInclude Include="..\src\mcal\mcal_gpt.h" />
<ClInclude Include="..\src\os\os.h" />
<ClInclude Include="..\src\startup\asm_util.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
15 changes: 15 additions & 0 deletions build/ti84-ref_app.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<Filter Include="src\mcal">
<UniqueIdentifier>{40bb87ba-a6ab-435a-aa98-6a436693e6ab}</UniqueIdentifier>
</Filter>
<Filter Include="src\os">
<UniqueIdentifier>{56938916-c213-486a-b3b1-22d39f753f45}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="Makefile">
Expand All @@ -50,6 +53,15 @@
<ClCompile Include="..\src\mcal\mcal_gpt.c">
<Filter>src\mcal</Filter>
</ClCompile>
<ClCompile Include="..\src\os\os.c">
<Filter>src\os</Filter>
</ClCompile>
<ClCompile Include="..\src\app\app_acc.c">
<Filter>src\app</Filter>
</ClCompile>
<ClCompile Include="..\src\app\app_main.c">
<Filter>src\app</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\app\app_led_util.h">
Expand All @@ -61,5 +73,8 @@
<ClInclude Include="..\src\mcal\mcal_gpt.h">
<Filter>src\mcal</Filter>
</ClInclude>
<ClInclude Include="..\src\os\os.h">
<Filter>src\os</Filter>
</ClInclude>
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions src/app/app_acc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2024.
// Distributed under The Unlicense
//

void app_acc_init(void)
{
}

void app_acc_task(void)
{
}
64 changes: 15 additions & 49 deletions src/app/app_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,40 @@
//

#include <app/app_led_util.h>
#include <mcal/mcal_gpt.h>

static void app_led_on (void);
static void app_led_off (void);
static void app_led_clr (void);
static void app_led_delay(void);
static void app_led_on (void);
static void app_led_off(void);
static void app_led_clr(void);

static uint8_t app_led_current_tick;
static bool app_led_exit_flag;
static bool app_led_is_on;

void main(void)
void app_led_init(void)
{
app_led_util_init();
}

mcal_gpt_init();

app_led_current_tick = mcal_gpt_get_time_elapsed();
void app_led_task(void)
{
app_led_clr();

for(;;)
if(!app_led_is_on)
{
app_led_clr();
app_led_on();
app_led_delay();

if(app_led_exit_flag)
{
break;
}

app_led_clr();
}
else
{
app_led_off();
app_led_delay();

if(app_led_exit_flag)
{
break;
}
}

mcal_gpt_de_init();

// Exit the Application and return to the home screen
// using the JForceCmdNoChar function. See also page 16
// in "TI-83 Plus System Routines", Third Release,
// (Jan. 25, 2002).

__asm__("rst 0x28\n" ".dw #0x4027\n");
app_led_is_on = (!app_led_is_on);
}

static void app_led_on(void)
{
app_led_util_home();
app_led_util_putc('O');
app_led_util_putc('N');
app_led_util_putc(' ');
}

static void app_led_off(void)
Expand All @@ -75,17 +55,3 @@ static void app_led_clr(void)
app_led_util_putc(' ');
app_led_util_putc(' ');
}

static void app_led_delay(void)
{
uint8_t next_tick;

do
{
next_tick = mcal_gpt_get_time_elapsed();
app_led_exit_flag = app_led_util_wants_exit();
}
while((next_tick == app_led_current_tick) && (!app_led_exit_flag));

app_led_current_tick = next_tick;
}
15 changes: 0 additions & 15 deletions src/app/app_led_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,3 @@ void app_led_util_init(void) ATTRIBUTE_NAKED
__asm__("rst 0x28\n" ".dw #0x4558\n");
__asm__("ret\n");
}

bool app_led_util_wants_exit(void) ATTRIBUTE_NAKED
{
__asm__("rst 0x28\n" ".dw #0x4018\n");
__asm__("ld h, #0x0\n");
__asm__("cp #0x9\n");
__asm__("jp nz, no_exit\n");
__asm__("ld h, #0x1\n");
__asm__("no_exit:\n");
__asm__("ret\n");

#if defined(_MSC_VER)
return false;
#endif
}
6 changes: 3 additions & 3 deletions src/app/app_led_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Distributed under The Unlicense
//

#ifndef APP_LED_UTIL_2024_01_7_H
#define APP_LED_UTIL_2024_01_7_H
#ifndef APP_LED_UTIL_2024_01_07_H
#define APP_LED_UTIL_2024_01_07_H

#include <stdbool.h>
#include <stdint.h>
Expand All @@ -29,4 +29,4 @@
void app_led_util_init (void) ATTRIBUTE_NAKED;
bool app_led_util_wants_exit(void) ATTRIBUTE_NAKED;

#endif // APP_UTIL_2024_01_7_H
#endif // APP_UTIL_2024_01_07_H
26 changes: 26 additions & 0 deletions src/app/app_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2024.
// Distributed under The Unlicense
//

#include <mcal/mcal_gpt.h>
#include <os/os.h>
#include <startup/asm_util.h>

void main(void)
{
mcal_gpt_init();

os_init();

os_schedule();

mcal_gpt_de_init();

// Exit the Application and return to the home screen
// using the JForceCmdNoChar function. See also page 16
// in "TI-83 Plus System Routines", Third Release,
// (Jan. 25, 2002).

__asm__("rst 0x28\n" ".dw #0x4027\n");
}
6 changes: 3 additions & 3 deletions src/mcal/mcal_gpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Distributed under The Unlicense
//

#ifndef MCAL_GPT_2024_01_7_H
#define MCAL_GPT_2024_01_7_H
#ifndef MCAL_GPT_2024_01_07_H
#define MCAL_GPT_2024_01_07_H

#include <stdint.h>

void mcal_gpt_init (void);
void mcal_gpt_de_init (void);
uint8_t mcal_gpt_get_time_elapsed(void);

#endif //CLOCK_2024_01_7_H
#endif // MCAL_GPT_2024_01_07_H
67 changes: 67 additions & 0 deletions src/os/os.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2024.
// Distributed under The Unlicense
//

#include <stdbool.h>
#include <stddef.h>

#include <mcal/mcal_gpt.h>
#include <os/os.h>
#include <startup/asm_util.h>

#define OS_COUNTOF(a) (size_t) (sizeof(a) / sizeof(a[(size_t) 0U]))

#define OS_TIMER_TIMEOUT(T) (bool) (((uint8_t) ((uint8_t) (mcal_gpt_get_time_elapsed() - (T)) & (uint8_t) UINT8_C(0x80)) == (uint8_t) UINT8_C(0)) ? true : false)

extern void app_led_init(void);
extern void app_led_task(void);
extern void app_acc_init(void);
extern void app_acc_task(void);

static bool os_wants_exit(void) ATTRIBUTE_NAKED;

static os_task_control_block os_task_list[2U] =
{
{ app_led_init, app_led_task, (uint8_t) UINT8_C(1), (uint8_t) UINT8_C(0) },
{ app_acc_init, app_acc_task, (uint8_t) UINT8_C(7), (uint8_t) UINT8_C(0) }
};

void os_init(void)
{
for(uint8_t task_index = (uint8_t) UINT8_C(0); task_index < (uint8_t) OS_COUNTOF(os_task_list); ++task_index)
{
os_task_list[task_index].p_init();
}
}

void os_schedule(void)
{
while(!os_wants_exit())
{
for(uint8_t task_index = (uint8_t) UINT8_C(0); task_index < (uint8_t) OS_COUNTOF(os_task_list); ++task_index)
{
if(OS_TIMER_TIMEOUT(os_task_list[task_index].timer))
{
os_task_list[task_index].p_func();

os_task_list[task_index].timer += os_task_list[task_index].cycle;
}
}
}
}

static bool os_wants_exit(void) ATTRIBUTE_NAKED
{
__asm__("rst 0x28\n" ".dw #0x4018\n");
__asm__("ld h, #0x0\n");
__asm__("cp #0x9\n");
__asm__("jp nz, no_exit\n");
__asm__("ld h, #0x1\n");
__asm__("no_exit:\n");
__asm__("ret\n");

#if defined(_MSC_VER)
return false;
#endif
}
25 changes: 25 additions & 0 deletions src/os/os.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2024.
// Distributed under The Unlicense
//

#ifndef OS_2024_01_09_H
#define OS_2024_01_09_H

#include <stdint.h>

typedef void(*os_function_type)(void);

typedef struct os_task_control_block
{
os_function_type p_init;
os_function_type p_func;
uint8_t cycle;
uint8_t timer;
}
os_task_control_block;

void os_init (void);
void os_schedule(void);

#endif // OS_2024_01_09_H
6 changes: 3 additions & 3 deletions src/startup/asm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Distributed under The Unlicense
//

#ifndef ASM_UTIL_2024_01_7_H
#define ASM_UTIL_2024_01_7_H
#ifndef ASM_UTIL_2024_01_07_H
#define ASM_UTIL_2024_01_07_H

#if defined(_MSC_VER)
#define __asm__(x)
Expand All @@ -16,4 +16,4 @@
#define ATTRIBUTE_NAKED __naked
#endif

#endif // ASM_UTIL_2024_01_7_H
#endif // ASM_UTIL_2024_01_07_H

0 comments on commit 2e5430c

Please sign in to comment.