Update to work on real hardware
This commit is contained in:
+5
-4
@@ -39,11 +39,11 @@ static const char *m_types[] = {
|
||||
};
|
||||
|
||||
|
||||
static void *
|
||||
static void
|
||||
IRQ_onTimerInterrupt(void)
|
||||
{
|
||||
Timer_incFromISR();
|
||||
return Task_scheduleFromISR();
|
||||
Task_scheduleFromISR();
|
||||
}
|
||||
|
||||
void
|
||||
@@ -52,13 +52,15 @@ IRQ_init()
|
||||
AArch64_setReg32(ENABLE_IRQS_1, SYSTEM_TIMER_IRQ_1);
|
||||
}
|
||||
|
||||
void *
|
||||
void
|
||||
IRQ_onInterrupt(void)
|
||||
{
|
||||
unsigned int irq = AArch64_getReg32(IRQ_PENDING_1);
|
||||
|
||||
switch(irq) {
|
||||
#if CONFIG_ARM_TIMER == 1
|
||||
case LOCAL_TIMER_IRQ:
|
||||
#endif
|
||||
case SYSTEM_TIMER_IRQ_1:
|
||||
return IRQ_onTimerInterrupt();
|
||||
|
||||
@@ -67,7 +69,6 @@ IRQ_onInterrupt(void)
|
||||
Log_putU(irq, 16);
|
||||
Log_putS("\n");
|
||||
}
|
||||
return 0; /* do not change stack pointer */
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
void
|
||||
IRQ_init(void);
|
||||
|
||||
void *
|
||||
void
|
||||
IRQ_onInterrupt(void);
|
||||
|
||||
void
|
||||
|
||||
+26
-11
@@ -13,14 +13,18 @@
|
||||
* @see https://lowenware.com/
|
||||
*/
|
||||
|
||||
#include <leos/log.h>
|
||||
#include <leos/irq.h>
|
||||
#include <leos/leos.h>
|
||||
#include <leos/log.h>
|
||||
#include <leos/memory.h>
|
||||
#include <leos/task.h>
|
||||
#include <drivers/timer/timer.h>
|
||||
|
||||
void
|
||||
Leos_demoTask(void *arg);
|
||||
Leos_demoTask1(void *arg);
|
||||
|
||||
void
|
||||
Leos_demoTask2(void *arg);
|
||||
|
||||
|
||||
void
|
||||
@@ -36,27 +40,38 @@ Leos_run(void)
|
||||
|
||||
IRQ_init();
|
||||
Timer_init();
|
||||
IRQ_enable();
|
||||
Memory_init();
|
||||
|
||||
Task_initSheduler();
|
||||
if (Task_create(Leos_demoTask, "Task 1") == -1)
|
||||
|
||||
IRQ_enable();
|
||||
|
||||
if (Task_create(Leos_demoTask1, "T1") == -1)
|
||||
Log_putS("Task 1 was not created\r\n");
|
||||
|
||||
if (Task_create(Leos_demoTask2, "T2") == -1)
|
||||
Log_putS("Task 2 was not created\r\n");
|
||||
|
||||
|
||||
for (;;) {
|
||||
Log_putU(Timer_getTicks(), 10);
|
||||
Log_putS(". Idle Task\r\n");
|
||||
Log_putS("I");
|
||||
Task_yield();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Leos_demoTask(void *arg)
|
||||
Leos_demoTask2(void *arg)
|
||||
{
|
||||
for (;;) {
|
||||
Log_putU(Timer_getTicks(), 10);
|
||||
Log_putS(". ");
|
||||
Log_putS((const char *)arg);
|
||||
Log_putS("\r\n");
|
||||
Log_putS("B");
|
||||
Task_yield();
|
||||
}
|
||||
}
|
||||
void
|
||||
Leos_demoTask1(void *arg)
|
||||
{
|
||||
for (;;) {
|
||||
Log_putS("A");
|
||||
Task_yield();
|
||||
}
|
||||
}
|
||||
|
||||
+10
-3
@@ -15,19 +15,26 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <aarch64/aarch64.h>
|
||||
#include <drivers/soc/bcm2837/bcm2837.h>
|
||||
#include "memory.h"
|
||||
#include "log.h"
|
||||
|
||||
static unsigned long m_map[MEMORY_PAGE_COUNT / sizeof(unsigned long) / 8] = {0,};
|
||||
static int m_ix = 0;
|
||||
static unsigned long m_map[MEMORY_PAGE_COUNT / sizeof(unsigned long) / 8];
|
||||
static int m_ix;
|
||||
|
||||
void
|
||||
Memory_init(void)
|
||||
{
|
||||
m_ix = 0;
|
||||
AArch64_memzero(m_map, sizeof(m_map));
|
||||
}
|
||||
|
||||
void *
|
||||
Memory_getPage(void)
|
||||
{
|
||||
int i, j;
|
||||
unsigned long map;
|
||||
|
||||
|
||||
for (i = m_ix; i < sizeof(m_map) / sizeof(m_map[0]); i++) {
|
||||
map = m_map[i];
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
#ifndef MEMORY_H_C583E24E_55B0_49EF_99C9_5A36B04468AC
|
||||
#define MEMORY_H_C583E24E_55B0_49EF_99C9_5A36B04468AC
|
||||
|
||||
|
||||
void
|
||||
Memory_init(void);
|
||||
|
||||
void *
|
||||
Memory_getPage(void);
|
||||
|
||||
|
||||
+88
-44
@@ -16,11 +16,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <aarch64/aarch64.h>
|
||||
#include <drivers/soc/bcm2837/bcm2837.h>
|
||||
#include <drivers/timer/timer.h>
|
||||
#include "memory.h"
|
||||
#include "log.h"
|
||||
#include "task.h"
|
||||
|
||||
#define TASK_STATE_RUNNING 0
|
||||
#define TASK_STATE_HALFCYCLE (1 << 8)
|
||||
|
||||
#ifndef CONFIG_IDLE_TASK_STACK_SIZE
|
||||
#define CONFIG_IDLE_TASK_STACK_SIZE 0x4000
|
||||
@@ -46,21 +48,8 @@ struct __attribute__((packed)) TaskContext {
|
||||
uint64_t spsr_el1;
|
||||
};
|
||||
|
||||
static struct Task *m_currentTask = NULL
|
||||
, *m_lastTask = NULL
|
||||
, m_idleTask = {
|
||||
.sp = NULL
|
||||
, .stackStart = NULL
|
||||
, .stackSize = CONFIG_IDLE_TASK_STACK_SIZE
|
||||
, .pid = 1
|
||||
, .lock = 1
|
||||
, .counter = 1
|
||||
, .cycles = 1
|
||||
, .priority = 0
|
||||
, .state = TASK_STATE_RUNNING
|
||||
, .name = {'I', 'D', 'L', 'E', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||
, .next = NULL
|
||||
};
|
||||
static struct Task *m_currentTask, *m_lastTask, m_idleTask;
|
||||
|
||||
|
||||
static uint32_t m_pid = 1;
|
||||
|
||||
@@ -70,7 +59,9 @@ scheduleNext(void)
|
||||
struct Task *i, *next = NULL;
|
||||
int32_t priority = -1;
|
||||
|
||||
Log_putS("scheduleNext\r\n ");
|
||||
if (m_currentTask->counter)
|
||||
m_currentTask->counter--;
|
||||
|
||||
for (;;) {
|
||||
/* check urgent tasks */
|
||||
for (i = &m_idleTask; i != NULL; i = i->next) {
|
||||
@@ -81,10 +72,6 @@ scheduleNext(void)
|
||||
priority = i->priority;
|
||||
next = i;
|
||||
}
|
||||
if (!i->state && i->counter > priority) {
|
||||
priority = i->priority;
|
||||
next = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (next) {
|
||||
@@ -96,10 +83,6 @@ scheduleNext(void)
|
||||
}
|
||||
}
|
||||
|
||||
Log_putS("\t switch: ");
|
||||
Log_putU((uint64_t)next->pid, 10);
|
||||
Log_putS("\n");
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
@@ -107,6 +90,24 @@ void
|
||||
Task_initSheduler(void)
|
||||
{
|
||||
struct Task *idleTask = &m_idleTask;
|
||||
|
||||
m_pid = 1;
|
||||
m_idleTask.sp = NULL;
|
||||
m_idleTask.stackStart = NULL;
|
||||
m_idleTask.stackSize = CONFIG_IDLE_TASK_STACK_SIZE;
|
||||
m_idleTask.pid = 1;
|
||||
m_idleTask.lock = 1;
|
||||
m_idleTask.counter = 1;
|
||||
m_idleTask.cycles = 1;
|
||||
m_idleTask.priority = 0;
|
||||
m_idleTask.state = TASK_STATE_RUNNING;
|
||||
m_idleTask.name[0] = 'I';
|
||||
m_idleTask.name[1] = 'D';
|
||||
m_idleTask.name[2] = 'L';
|
||||
m_idleTask.name[3] = 'E';
|
||||
m_idleTask.name[4] = 0;
|
||||
m_idleTask.next = NULL;
|
||||
|
||||
m_currentTask = idleTask;
|
||||
m_lastTask = idleTask;
|
||||
Task_unlockScheduler();
|
||||
@@ -120,6 +121,12 @@ Task_create(TaskCallback callback, void *arg)
|
||||
|
||||
if (!task)
|
||||
return -1;
|
||||
|
||||
Log_putS("Task @0x");
|
||||
Log_putU((uint64_t) task, 16);
|
||||
Log_putS(" - ");
|
||||
Log_putU(((uint64_t) task) + MEMORY_PAGE_SIZE, 16);
|
||||
Log_putS("\r\n");
|
||||
|
||||
task->sp = (void *)task + MEMORY_PAGE_SIZE - 272;
|
||||
task->stackStart = task->sp;
|
||||
@@ -138,10 +145,11 @@ Task_create(TaskCallback callback, void *arg)
|
||||
task->next = 0;
|
||||
|
||||
ctx = (struct TaskContext *) task->sp;
|
||||
ctx->x[0] = (uint64_t) arg;
|
||||
ctx->x[1] = (uint64_t) callback;
|
||||
ctx->x[20] = (uint64_t) arg;
|
||||
ctx->x[21] = (uint64_t) callback;
|
||||
ctx->x[30] = (uint64_t) AArch64_startTask;
|
||||
ctx->elr_el1 = (uint64_t) AArch64_startTask;
|
||||
ctx->spsr_el1 = AArch64_getPState();
|
||||
|
||||
Task_lockScheduler();
|
||||
m_lastTask->next = task;
|
||||
@@ -154,25 +162,41 @@ Task_create(TaskCallback callback, void *arg)
|
||||
void
|
||||
Task_yield(void)
|
||||
{
|
||||
struct Task *next, *prev = m_currentTask;
|
||||
#if 1
|
||||
uint64_t ticks = Timer_getTicks();
|
||||
|
||||
Log_putS("Task_yield()\r\n");
|
||||
Task_lockScheduler();
|
||||
Log_putS("^");
|
||||
m_currentTask->counter = 0;
|
||||
Task_unlockScheduler();
|
||||
__asm__("WFE");
|
||||
|
||||
while (ticks == Timer_getTicks());
|
||||
#else
|
||||
/* Task yield should be solved differently */
|
||||
struct Task *next, *prev;
|
||||
|
||||
Task_lockScheduler();
|
||||
Log_putS("^");
|
||||
prev = m_currentTask;
|
||||
prev->counter = 0;
|
||||
|
||||
next = scheduleNext();
|
||||
|
||||
if (next != prev) {
|
||||
next->state |= TASK_STATE_HALFCYCLE;
|
||||
m_currentTask = next;
|
||||
Task_unlockScheduler();
|
||||
AArch64_switchContext(prev, next);
|
||||
/* newly created tasks never exit here, that is why in AArch64_startTask
|
||||
* there is own Task_unlockScheduler() call
|
||||
/* `next` tasks never exits here, but in AArch64_startTask,
|
||||
* that is why there is an own Task_unlockScheduler() call
|
||||
* */
|
||||
} else {
|
||||
__asm__("WFE");
|
||||
// __asm__("WFE");
|
||||
}
|
||||
|
||||
Task_unlockScheduler();
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@@ -184,35 +208,55 @@ Task_lockScheduler(void)
|
||||
void
|
||||
Task_unlockScheduler(void)
|
||||
{
|
||||
m_currentTask->lock--;
|
||||
if (m_currentTask->lock)
|
||||
m_currentTask->lock--;
|
||||
}
|
||||
|
||||
void *
|
||||
void
|
||||
Task_scheduleFromISR(void)
|
||||
{
|
||||
void *sp = NULL;
|
||||
|
||||
# if 0
|
||||
if (m_currentTask->counter)
|
||||
m_currentTask->counter--;
|
||||
|
||||
if (!m_currentTask->lock) {
|
||||
struct Task *next;
|
||||
Log_putS("Task_scheduleFromISR\r\n");
|
||||
|
||||
if (m_currentTask->state & TASK_STATE_HALFCYCLE) {
|
||||
m_currentTask->state &= ~TASK_STATE_HALFCYCLE;
|
||||
return;
|
||||
}
|
||||
|
||||
Log_putS("!");
|
||||
|
||||
Task_lockScheduler();
|
||||
|
||||
|
||||
next = scheduleNext();
|
||||
if (next != m_currentTask) {
|
||||
m_currentTask = next;
|
||||
sp = next->sp;
|
||||
}
|
||||
/* unlock call could be moved to aarch64.S interrupt handler in case of
|
||||
* issue
|
||||
* */
|
||||
Task_unlockScheduler();
|
||||
}
|
||||
#endif
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
void *
|
||||
Task_getStackPointer(void)
|
||||
{
|
||||
/*
|
||||
Log_putS("gSP:");
|
||||
Log_putU((uint64_t)m_currentTask->sp, 16);
|
||||
Log_putS("\r\n");
|
||||
*/
|
||||
return m_currentTask->sp;
|
||||
}
|
||||
|
||||
void
|
||||
Task_setStackPointer(void *sp)
|
||||
{
|
||||
/*
|
||||
Log_putS("sSP:");
|
||||
Log_putU((uint64_t)sp, 16);
|
||||
Log_putS("\r\n");
|
||||
*/
|
||||
m_currentTask->sp = sp;
|
||||
}
|
||||
|
||||
+7
-1
@@ -39,7 +39,13 @@ Task_lockScheduler(void);
|
||||
void
|
||||
Task_unlockScheduler(void);
|
||||
|
||||
void *
|
||||
void
|
||||
Task_scheduleFromISR(void);
|
||||
|
||||
void *
|
||||
Task_getStackPointer(void);
|
||||
|
||||
void
|
||||
Task_setStackPointer(void *sp);
|
||||
|
||||
#endif /* !TASK_H */
|
||||
|
||||
Reference in New Issue
Block a user