Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hal/stm32f401/include/fault_indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

void delay(volatile uint32_t count);
void warning_light_init(void);
void warning_light_mutex(void);
void warning_light_scheduler(void);

#endif
2 changes: 1 addition & 1 deletion hal/stm32f401/src/fault_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void warning_light_init(void) {
}


void warning_light_mutex(void) {
void warning_light_scheduler(void) {
while (1) {
for (int i = 0; i < 6; i++) {
gpio_toggle_pc13();
Expand Down
3 changes: 1 addition & 2 deletions hal/stm32f401/src/fpu.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stm32f401xc.h"
#include "fpu.h"

#include "stm32f401xc.h"

/* Enables FPU hardware
https://deepbluembedded.com/stm32-fpu-floating-point-unit-enable-disable/ */
Expand Down
2 changes: 1 addition & 1 deletion hal/stm32f401/src/gpio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gpio.h"
#include "stm32f401xc.h"
#include <stdint.h>
#include "gpio.h"

/* Page 158 of Reference manual
Moder configs whether pin is input, output, ect.
Expand Down
5 changes: 3 additions & 2 deletions hal/stm32f401/src/rcc.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* Page 91 of https://www.st.com/resource/en/reference_manual/dm00096844-stm32f401xb-c-and-stm32f401xd-e-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf*/

#include "rcc.h"
#include "gpio.h"
#include "stm32f401xc.h"
#include <stdint.h>
#include <gpio.h>
#include <stm32f401xc.h>

void rcc_init(void) {
/* Turn on HSI */
Expand Down
1 change: 0 additions & 1 deletion kernel/include/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ extern Task *current_task;
void scheduler_init(void);
void scheduler_start(void);
void scheduler_tick(void);
Task *scheduler_get_current_task(void);
void scheduler_select_next_task(void);

#endif
63 changes: 53 additions & 10 deletions kernel/src/scheduler.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
#include "scheduler.h"
#include "systick.h"
#include "stddef.h"
#include "fault_indicator.h"
#include "port.h"
#include "nvic.h"
#include <stddef.h>

/* Only scheduler.c needs tick count */
//static uint32_t tick_count = 0;
static uint32_t tick_count = 0;
Task *current_task = NULL;

void scheduler_start(void) {
scheduler_init();

port_start_first_task();
}

void scheduler_init(void) {
/* Turn on SysTick */
systick_init();
/* Set its priority */
nvic_init();
}

// void scheduler_tick(void) {
/* Called every 1ms to unblock
sleeping tasks, setup by
SysTick_Handler */
void scheduler_tick(void) {
tick_count += 1;

// }
for (int i = 0; i < MAX_TASKS; i++) {
Task *cur_task = &task_pool[i];

// Task *scheduler_get_current_task(void) {

// }
if (cur_task->state == BLOCKED && tick_count >= cur_task->delay_until) {
cur_task->state = READY;
}
}

port_trigger_context_switch();
}

// void scheduler_select_next_task(void) {
/* Used in PendSV after saving the
cur task context, PendSV Handler
will then select next task */
void scheduler_select_next_task(void) {
Task *next_task = NULL;
for (int i = 0; i < MAX_TASKS; i++) {
Task *cur_task = &task_pool[i];
if (cur_task->state == READY) {
if (next_task == NULL) {
next_task = cur_task;
} else if (cur_task->priority > next_task->priority) {
next_task = cur_task;
}
}
}

// }
/* Should always have a READY Task,
so shouldn't have to worry about this */
if (next_task == NULL) {
return;
}

/* Mark current task as READY and
next task as RUNNING */
if (current_task != NULL && current_task->state == RUNNING) {
current_task->state = READY;
}

next_task->state = RUNNING;
current_task = next_task;
}
3 changes: 1 addition & 2 deletions kernel/src/task.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "task.h"
#include "port.h"

#include "systick.h"
#include "fault_indicator.h"
#include "systick.h"

Task task_pool[MAX_TASKS];
uint32_t stack_pool[MAX_TASKS][TASK_STACK_SIZE];
Expand Down
1 change: 1 addition & 0 deletions port/cortex_m4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(port_cortex_m4 STATIC
src/port.c
src/port.s
src/nvic.c
src/systick.c
)

target_link_libraries(port_cortex_m4 PUBLIC
Expand Down
2 changes: 0 additions & 2 deletions port/cortex_m4/include/nvic.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef NVIC_H
#define NVIC_H

#include "stm32f401xc.h"

void nvic_init(void);

#endif
9 changes: 6 additions & 3 deletions port/cortex_m4/include/port.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#ifndef PORT_H
#define PORT_H

#include <stm32f401xc.h>
#include <stdint.h>
#include "rtos_config.h"
#include "task.h"

#define PENDSVSET (1U << 28)

void port_init_task(Task *task, uint32_t priority, uint32_t slot, const char* name);
/* In port.c */
void port_init_task(Task *task, uint8_t priority, uint32_t slot, const char* name);
void port_init_task_stack(Task *task, void (*function)(void));
void port_trigger_context_switch(void);

/* In port.s */
void PendSV_Handler(void);
void port_start_first_task(void);

#endif
3 changes: 0 additions & 3 deletions port/cortex_m4/include/systick.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef SYSTICK_H
#define SYSTICK_H

#include "stm32f401xc.h"
#include "scheduler.h"

void systick_init(void);
void SysTick_Handler(void);

Expand Down
1 change: 1 addition & 0 deletions port/cortex_m4/src/nvic.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nvic.h"
#include "stm32f401xc.h"

/* NVICs main role is to handle interrupts
https://microcontrollerslab.com/nested-vectored-interrupt-controller-nvic-arm-cortex-m/ */
Expand Down
7 changes: 6 additions & 1 deletion port/cortex_m4/src/port.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "port.h"
#include "rtos_config.h"
#include "stm32f401xc.h"

void port_init_task(Task *task, uint32_t priority, uint32_t slot, const char *name) {
void port_init_task(Task *task, uint8_t priority, uint32_t slot, const char *name) {
task->stack_base = stack_pool[slot];
*task->stack_base = 0xDEADBEEF;

Expand Down Expand Up @@ -55,6 +57,9 @@ void port_init_task_stack(Task *task, void (*function)(void)) {
task->stack_ptr = sp;
}

/* Turn on PendSV exception, so
we can save the current task's
context to memory */
void port_trigger_context_switch(void) {
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}
4 changes: 3 additions & 1 deletion port/cortex_m4/src/port.s
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PendSV_Handler:
PendSV_Handler:

port_start_first_task:
6 changes: 4 additions & 2 deletions port/cortex_m4/src/systick.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "systick.h"
#include "scheduler.h"
#include "stm32f401xc.h"

void systick_init(void) {
/* Page 247 in cortex manual*/
Expand All @@ -19,11 +21,11 @@ void systick_init(void) {
/* We get 84,000 cycles / 1 ms */
/* Standard according to Claude is 1ms */
/* Subtract 1 for LOAD - 83,999*/
SysTick->LOAD |= 83999;
SysTick->LOAD = 83999;

/* Page 249 of cortex*/
/* Just clear it */
SysTick->VAL |= 0x00;
SysTick->VAL = 0x00;

}

Expand Down
Loading