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
1 change: 1 addition & 0 deletions examples/blinky/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_executable(blinky main.c ${CMAKE_SOURCE_DIR}/src/startup/startup_stm32f401.c

target_link_libraries(blinky
hal_stm32f401
kernel
)

target_link_options(blinky PRIVATE
Expand Down
7 changes: 4 additions & 3 deletions examples/blinky/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "hal.h"
#include "stdint.h"
#include "fpu.h"
#include "rcc.h"
#include "gpio.h"
#include "fault_indicator.h"
#include <stdint.h>

int main(void) {
fpu_init();
rcc_init();
rcc_enable_gpioc();
gpio_init_pc13();
nvic_init();

while (1) {
gpio_toggle_pc13();
Expand Down
1 change: 1 addition & 0 deletions examples/rtos_demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_executable(rtos_demo main.c ${CMAKE_SOURCE_DIR}/src/startup/startup_stm32f40

target_link_libraries(rtos_demo
hal_stm32f401
kernel
)

target_link_options(rtos_demo PRIVATE
Expand Down
4 changes: 2 additions & 2 deletions examples/rtos_demo/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gpio.h"
#include "hal.h"
#include "task.h"
#include "rcc.h"
#include "fpu.h"
#include "scheduler.h"
#include "fault_indicator.h"
#include <stdint.h>
Expand Down
30 changes: 30 additions & 0 deletions include/task_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TASK_TYPES_H
#define TASK_TYPES_H

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

/* More Granular States for Debugging*/
typedef enum {
UNINITIALIZED,
READY,
RUNNING,
BLOCKED,
SUSPENDED
} Task_State;


typedef struct {
uint32_t *stack_ptr;
uint32_t *stack_base;
uint32_t stack_size;
uint32_t delay_until;
Task_State state;
uint8_t priority;
const char *name;
} Task;

extern Task task_pool[MAX_TASKS];
extern uint32_t stack_pool[MAX_TASKS][TASK_STACK_SIZE];

#endif
4 changes: 0 additions & 4 deletions src/hal/stm32f401/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ add_library(hal_stm32f401 STATIC
target_include_directories(hal_stm32f401 PUBLIC
include
${CMAKE_SOURCE_DIR}/config
)

target_link_libraries(hal_stm32f401 PUBLIC
port_cortex_m4
)
2 changes: 1 addition & 1 deletion src/hal/stm32f401/include/fault_indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>

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

#endif
9 changes: 0 additions & 9 deletions src/hal/stm32f401/include/hal.h

This file was deleted.

37 changes: 4 additions & 33 deletions src/hal/stm32f401/src/fault_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,17 @@ void delay(volatile uint32_t count) {
while (count--);
}

void warning_light_init(void) {
void warning_light(void) {
while (1) {
for (int i = 0; i < 6; i++) {
gpio_toggle_pc13();

delay(1000000);
}

gpio_toggle_pc13();
gpio_set_pc13();

delay(10000000);

for (int i = 0; i < 4; i++) {
gpio_toggle_pc13();

delay(1000000);
}
gpio_clear_pc13();

delay(10000000);
}
}


void warning_light_scheduler(void) {
while (1) {
for (int i = 0; i < 6; i++) {
gpio_toggle_pc13();

delay(1000000);
}

gpio_toggle_pc13();

delay(10000000);

for (int i = 0; i < 8; i++) {
gpio_toggle_pc13();

delay(1000000);
}
gpio_set_pc13();

delay(10000000);
}
Expand Down
3 changes: 2 additions & 1 deletion src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ add_library(kernel STATIC
)

target_link_libraries(kernel PUBLIC
hal_stm32f401
port_cortex_m4
)

target_include_directories(kernel PUBLIC
include
${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/include
)
2 changes: 1 addition & 1 deletion src/kernel/include/queue.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef QUEUE_H
#define QUEUE_H

#include "stdint.h"
#include "task.h"
#include <stdint.h>
typedef struct {
Task *enqueue_waiting;
Task *dequeue_waiting;
Expand Down
22 changes: 2 additions & 20 deletions src/kernel/include/task.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
#ifndef TASK_H
#define TASK_H

#include "rtos_config.h"
#include "task_types.h"
#include <stdint.h>
#include <stdbool.h>
#include "rtos_config.h"

/* More Granular States for Debugging*/
typedef enum {
UNINITIALIZED,
READY,
RUNNING,
BLOCKED,
SUSPENDED
} Task_State;

typedef struct {
uint32_t *stack_ptr;
uint32_t *stack_base;
uint32_t stack_size;
uint32_t delay_until;
Task_State state;
uint8_t priority;
const char *name;
} Task;

Task *task_create(void (*function)(void), uint8_t priority, const char *name);
bool task_state_overflow(Task task);
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/src/mutex.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "mutex.h"
#include "scheduler.h"
#include "port.h"
#include "fault_indicator.h"
#include <stddef.h>

void mutex_init(Mutex *mutex) {
Expand All @@ -13,7 +12,7 @@ void mutex_init(Mutex *mutex) {
void mutex_lock(Mutex *mutex) {
while (mutex->is_locked) {
if (mutex->waiting != NULL) {
warning_light_scheduler();
port_fault();
}
mutex->waiting = current_task;
current_task->state = BLOCKED;
Expand Down
5 changes: 2 additions & 3 deletions src/kernel/src/queue.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "queue.h"
#include "scheduler.h"
#include "port.h"
#include "fault_indicator.h"
#include <stddef.h>
#include <string.h>

Expand Down Expand Up @@ -43,7 +42,7 @@ void enqueue(Queue *queue, void *item) {
check queue is not full */
while (queue->count == queue->depth) {
if (queue->enqueue_waiting != NULL) {
warning_light_scheduler();
port_fault();
}
queue->enqueue_waiting = current_task;
current_task->state = BLOCKED;
Expand All @@ -68,7 +67,7 @@ void enqueue(Queue *queue, void *item) {
void dequeue(Queue *queue, void *out) {
while (queue->count == 0) {
if (queue->dequeue_waiting != NULL) {
warning_light_scheduler();
port_fault();
}
queue->dequeue_waiting = current_task;
current_task->state = BLOCKED;
Expand Down
12 changes: 3 additions & 9 deletions src/kernel/src/scheduler.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "scheduler.h"
#include "systick.h"
#include "fault_indicator.h"
#include "port.h"
#include "nvic.h"
#include "stm32f401xc.h"
#include <stddef.h>

/* Only scheduler.c needs tick count */
Expand All @@ -16,12 +14,8 @@ void scheduler_start(void) {
}

void idle_task(void) {
while (1) {
/* Special ARM Instr
to put CPU to sleep until
next interrupt fires */
__WFI();
}
/* This taks just sleeps */
port_idle();
}

void scheduler_init(void) {
Expand All @@ -43,7 +37,7 @@ void scheduler_tick(void) {
Task *cur_task = &task_pool[i];

if (cur_task->state != UNINITIALIZED && task_state_overflow(*cur_task)) {
warning_light_init();
port_fault();
}

if (cur_task->state == BLOCKED && tick_count >= cur_task->delay_until) {
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/src/task.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "task.h"
#include "port.h"
#include "fault_indicator.h"
#include "scheduler.h"

Task task_pool[MAX_TASKS];
Expand All @@ -17,7 +16,7 @@ Task *task_create(void (*function)(void), uint8_t priority, const char *name) {

/* Fail if not enough task slots available */
if (new_task_slot == -1) {
warning_light_init();
port_fault();
}

Task *new_task = &task_pool[new_task_slot];
Expand Down
4 changes: 2 additions & 2 deletions src/port/cortex_m4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ add_library(port_cortex_m4 STATIC
)

target_link_libraries(port_cortex_m4 PUBLIC
kernel
)
hal_stm32f401)

target_include_directories(port_cortex_m4 PUBLIC
include
${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/include
)
5 changes: 4 additions & 1 deletion src/port/cortex_m4/include/port.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef PORT_H
#define PORT_H

#include "task_types.h"
#include "fault_indicator.h"
#include <stdint.h>
#include "task.h"

#define PENDSVSET (1U << 28)

Expand All @@ -14,5 +15,7 @@ void port_trigger_context_switch(void);
/* In port.s */
void PendSV_Handler(void);
void port_start_first_task(void);
void port_fault(void);
void port_idle(void);

#endif
2 changes: 1 addition & 1 deletion src/port/cortex_m4/include/systick.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#define SYSTICK_H

void systick_init(void);
void SysTick_Handler(void);
void SysTick_Handler(void (*tick_callback)(void));

#endif
10 changes: 10 additions & 0 deletions src/port/cortex_m4/src/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ void port_init_task_stack(Task *task, void (*function)(void)) {
context to memory */
void port_trigger_context_switch(void) {
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}

void port_fault(void) {
warning_light();
}

void port_idle(void) {
while (1) {
__WFI();
}
}
5 changes: 2 additions & 3 deletions src/port/cortex_m4/src/systick.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "systick.h"
#include "scheduler.h"
#include "stm32f401xc.h"

void systick_init(void) {
Expand Down Expand Up @@ -28,6 +27,6 @@ void systick_init(void) {
SysTick->VAL = 0x00;
}

void SysTick_Handler(void) {
scheduler_tick();
void SysTick_Handler(void (*tick_callback)(void)) {
tick_callback();
}
Loading
Loading