Merge nanoos

This commit is contained in:
2022-09-06 08:30:16 +02:00
parent 5947cd60cc
commit f3b0eb4a59
16 changed files with 814 additions and 0 deletions

7
nanoos/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
*.elf
*.bin
*.map
*.geany
*.o
*~
work

87
nanoos/Makefile Normal file
View File

@@ -0,0 +1,87 @@
#
# Copyright: Oleg Borodin <onborodin@gmail.com> 2018
#
.SECONDARY:
CFLAGS+= -I. -Os -DSTM32F4 -std=c99
#CFLAGS+= -mthumb
#CFLAGS+= -march=armv7e-m
CFLAGS+= -mfloat-abi=hard
#CFLAGS+= -mfpu=fpv4-sp-d16
CFLAGS+= -mcpu=cortex-m4
CFLAGS+= -fno-common -ffunction-sections -fdata-sections
CFLAGS+= -g -gdwarf-2
CFLAGS+= -Wall
LDFLAGS+= ${CFLAGS}
LDFLAGS+= --static
#LDFLAGS+= -nostartfiles
LDFLAGS+= -T main.ld
LDFLAGS+= -Wl,-Map=main.map
LDFLAGS+= -Wl,--cref -Wl,--gc-sections
LDFLAGS+= -lopencm3_stm32f4
LDFLAGS+= -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
TARGET= arm-eabi
all: main.bin
OBJS+= main.o
OBJS+= syscall.o
OBJS+= usartu.o
OBJS+= scheduler.o
OBJS+= atomic.o
OBJS+= semaphore.o
OBJS+= mutex.o
main.elf: $(OBJS)
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@
$(TARGET)-size --format=berkeley $@
%.o: %.c
$(TARGET)-gcc $(CFLAGS) -c -o $@ $<
%.o: %.S
$(TARGET)-gcc $(CFLAGS) -c -o $@ $<
%.bin: %.elf
$(TARGET)-objcopy -O binary $< $@
%.elf: %.o
$(TARGET)-gcc $(^F) $(LDFLAGS) -o $@
$(TARGET)-size --format=berkeley $@
clean:
rm -f *.i *.o *.elf *.bin *.map *~ *.hex *.d *.s
flash: main.bin
@openocd \
-c 'puts "--- START --------------------"' \
-f 'interface/stlink.cfg' \
-f 'target/stm32f4x.cfg' \
-c 'puts "--- INIT --------------------"' \
-c "init" \
-c "reset halt" \
-c 'puts "--- WRITE --------------------"' \
-c "flash write_image erase $< 0x08000000"\
-c 'puts "--- VERIFY --------------------"' \
-c "verify_image $<" \
-c 'puts "--- RESET --------------------"' \
-c "reset" \
-c 'puts "--- DONE --------------------"' \
-c "shutdown"
debug: main.bin
@openocd \
-c 'puts "--- START --------------------"' \
-f 'interface/stlink.cfg' \
-f 'target/stm32f4x.cfg' \
-c 'puts "--- INIT --------------------"' \
-c "init" \
-c "halt" \
-c "poll"
#EOF

65
nanoos/README.md Normal file
View File

@@ -0,0 +1,65 @@
## Minimal STM32 F4/F7 task sheduler
NanoOS: "Make less makes no sense" =)
### Register frame description
```
/*
* ABI: r0..r3 are caller-saved (scratch registers), R4..R12 are callee-saved.
* It is appropriate to use R12 for a system call opcode (saved by NVIC). The
* stack pointer points to the current extent of the stack -- it is decremented
* before being used as index in a store. The stack grows downwards, to lower
* addresses. When an interrupt is processed, 8 registers are stored. LR is set
* to a special value that makes an ordinary function return into a return from
* interrupt. The LR value indicates which stack is going to be used (process
* or main) and can be modified before return.
*
* ____________________
* Stack | |
* | |
* higher | R4 | <-- SP saved in TCB (64B context)
* addresses | R5 | ^
* | ^ | R6 | |
* | | | R7 | | 8 registers pushed by handler:
* | | | R8 | | R4..R11
* | | | R9 | | Full task context is now stored
* V | | R10 | |
* | | R11 | |
* direction | R0 | <-- SP when SVC handler gets control
* of growth | R1 | ^
* | R2 | |
* | R3 | | 8 registers are pushed by
* | R12 | | the NVIC hardware:
* | LR (R14) | | xPSR, PC, LR, R12, R3..R0
* | PC (R15) | |
* | xPSR | |
* | | <-- SP before SVC
* | (stuff) |
* Stack + | |
* StackSize |____________________|
*
*/
```
#### Ouput
```
task 2 1
task 3 14
task 4 27 1
task 1 44
task 2 6341
task 3 6362
task 4 6379 2
task 1 6400
task 2 12689
task 3 12710
task 4 12727 3
task 1 12752
task 2 19037
task 3 19058
task 4 19079 4
task 1 19104
```

43
nanoos/atomic.S Normal file
View File

@@ -0,0 +1,43 @@
.thumb
.syntax unified
.text
.globl atomic_inc32
.type atomic_inc32, %function
atomic_inc32:
1: ldrex r2, [r0]
add r2, r2, r1
strex r3, r2, [r0]
teq r3, #0
bne 1b
mov r0, r2
bx lr
.globl atomic_dec32
.type atomic_dec32, %function
atomic_dec32:
1: ldrex r2, [r0]
sub r2, r2, r1
strex r3, r2, [r0]
teq r3, #0
bne 1b
mov r0, r2
bx lr
.globl atomic_dec32le0
.type atomic_dec32le0, %function
atomic_dec32le0:
1: ldrex r2, [r0]
cmp r2, #0
ble 1b
sub r2, r2, r1
strex r3, r2, [r0]
teq r3, #0
bne 1b
mov r0, r2
bx lr

15
nanoos/atomic.h Normal file
View File

@@ -0,0 +1,15 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#ifndef SEMOPER_H_QWERTY
#define SEMOPER_H_QWERTY
#include <stdint.h>
int32_t atomic_inc32(volatile int32_t *addr, int32_t value);
int32_t atomic_dec32(volatile int32_t *addr, int32_t value);
int32_t atomic_dec32le0(volatile int32_t *addr, int32_t value);
#endif

125
nanoos/main.c Normal file
View File

@@ -0,0 +1,125 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "scheduler.h"
#include "usartu.h"
#include "atomic.h"
#include "semaphore.h"
static int g_uptime;
static sem_t g_sem;
static scheduler_t g_scheduler;
void delay(uint32_t n) {
for (int i = 0; i < n * 800; i++)
__asm__("nop");
}
static void clock_setup(void) {
rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_USART1);
}
static void usart_setup(void) {
usart_disable(USART1);
nvic_disable_irq(NVIC_USART1_IRQ);
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10);
gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10);
usart_set_baudrate(USART1, 115200);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
usart_set_mode(USART1, USART_MODE_TX_RX);
usart_disable_rx_interrupt(USART1);
usart_enable(USART1);
}
static void systick_setup(void) {
systick_set_frequency(10000, rcc_ahb_frequency);
systick_interrupt_enable();
systick_counter_enable();
}
void task1(void) {
while (true) {
sem_wait(&g_sem);
printf("task 1 %d\r\n", g_uptime);
sem_post(&g_sem);
delay(3000);
};
}
void task2(void) {
while (true) {
sem_wait(&g_sem);
printf("task 2 %d\r\n", g_uptime);
sem_post(&g_sem);
delay(3000);
};
}
void task3(void) {
while (true) {
sem_wait(&g_sem);
printf("task 3 %d\r\n", g_uptime);
sem_post(&g_sem);
delay(3000);
};
}
void task4(void) {
static volatile int32_t t4;
while (true) {
atomic_inc32(&t4, (int32_t)1);
sem_wait(&g_sem);
printf("task 4 %d %lu\r\n", g_uptime, t4);
sem_post(&g_sem);
delay(3000);
};
}
void sys_tick_handler(void) {
g_uptime++;
scheduler_yield();
}
int main(void) {
g_uptime = 0;
sem_init(&g_sem, 1);
clock_setup();
usart_setup();
scheduler_init(&g_scheduler);
scheduler_task(&g_scheduler, 0, task1);
scheduler_task(&g_scheduler, 1, task2);
scheduler_task(&g_scheduler, 2, task3);
scheduler_task(&g_scheduler, 3, task4);
systick_setup();
while (true);
}

71
nanoos/main.ld Normal file
View File

@@ -0,0 +1,71 @@
MEMORY {
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
EXTERN (vector_table)
ENTRY(reset_handler)
SECTIONS
{
.text : {
*(.vectors)
*(.text*)
. = ALIGN(4);
*(.rodata*)
. = ALIGN(4);
} >FLASH
.preinit_array : {
. = ALIGN(4);
__preinit_array_start = .;
KEEP (*(.preinit_array))
__preinit_array_end = .;
} >FLASH
.init_array : {
. = ALIGN(4);
__init_array_start = .;
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
__init_array_end = .;
} >FLASH
.fini_array : {
. = ALIGN(4);
__fini_array_start = .;
KEEP (*(.fini_array))
KEEP (*(SORT(.fini_array.*)))
__fini_array_end = .;
} >FLASH
. = ALIGN(4);
_etext = .;
.data : {
_data = .;
*(.data*)
. = ALIGN(4);
_edata = .;
} >SRAM AT >FLASH
_data_loadaddr = LOADADDR(.data);
.bss : {
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >SRAM
. = ALIGN(4);
_end = .;
}
PROVIDE(_stack = ORIGIN(SRAM) + LENGTH(SRAM));
PROVIDE(_heap = _end );
PROVIDE(_eheap = ORIGIN(SRAM) + LENGTH(SRAM));

23
nanoos/mutex.c Normal file
View File

@@ -0,0 +1,23 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include "mutex.h"
#include "atomic.h"
void mutex_init(mutex_t* mutex) {
mutex->value = 1;
}
int32_t mutex_lock(mutex_t* mutex) {
//while(mutex->value <= 0);
//mutex->value--;
//return mutex->value;
return atomic_dec32le0(&(mutex->value), (int32_t)1);
}
int32_t mutex_unlock(mutex_t* mutex) {
//mutex->value++;
//return mutex->value;
return atomic_inc32(&(mutex->value), (int32_t)1);
}

14
nanoos/mutex.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdint.h>
typedef struct {
int32_t value;
} mutex_t;
void mutex_init(mutex_t* mutex);
int32_t mutex_lock(mutex_t* mutex);
int32_t mutex_unlock(mutex_t* mutex);

129
nanoos/scheduler.c Normal file
View File

@@ -0,0 +1,129 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*
*/
#include <libopencm3/cm3/scb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include "scheduler.h"
static scheduler_t* m_scheduler;
#define TASK_COUNT_REG 8
typedef struct __attribute__((packed)) {
struct {
uint32_t registers[TASK_COUNT_REG];
} sw_frame;
struct {
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uint32_t lr;
uint32_t pc;
uint32_t psr;
} nvic_frame;
} stack_frame_t;
static void task_exit_func(void) {
while (true);
}
#define RETURN_PSR 0x21000000
#define STACK_FILL 0xA5
void scheduler_init(scheduler_t *scheduler) {
int i;
for (i = 0; i < SCHEDULER_NUM_TASKS; i++) {
task_t *task = &(scheduler->tasks[i]);
task->stack = NULL;
task->stack_size = 0;
task->sp = (uint32_t)NULL;
task->runnable = false;
}
scheduler->current_task = 0;
m_scheduler = scheduler;
}
void scheduler_task(scheduler_t *scheduler, int task_no, void (*entry)(void)) {
task_t *task = &(scheduler->tasks[task_no]);
task->stack = (void*)malloc(TASK_STACK_SIZE);
task->stack_size = TASK_STACK_SIZE;
memset(task->stack, STACK_FILL, task->stack_size);
task->sp = (uint32_t)(task->stack + task->stack_size);
task->sp -= sizeof(stack_frame_t);
stack_frame_t *frame = (stack_frame_t*)task->sp;
frame->nvic_frame.lr = (uint32_t)task_exit_func;
frame->nvic_frame.pc = (uint32_t)entry;
frame->nvic_frame.psr = RETURN_PSR;
task->runnable = true;
}
static void scheduler_switch(scheduler_t *scheduler) {
do {
scheduler->current_task = (scheduler->current_task + 1) % SCHEDULER_NUM_TASKS;
} while (!scheduler->tasks[scheduler->current_task].runnable);
}
#define EXC_RETURN_MODE_THREAD 0x00000004
#define RETURN_ON_PSP_THREAD 0xFFFFFFFD
void __attribute__((naked)) pend_sv_handler(void) {
const uint32_t RETURN_ON_PSP = RETURN_ON_PSP_THREAD;
uint32_t lr;
__asm__(
"cpsid if \n"
"mov %0, lr \n"
:"=r"(lr)
);
if (lr & EXC_RETURN_MODE_THREAD) {
uint32_t psp;
__asm__(
"mrs %0, psp \n"
"stmdb %0!, {r4-r11} \n"
"msr psp, %0 \n"
: "=r"(psp)
);
m_scheduler->tasks[m_scheduler->current_task].sp = psp;
} else {
__asm__(
"stmdb sp!, {r4-r11} \n"
);
}
scheduler_switch(m_scheduler);
uint32_t psp = (uint32_t)m_scheduler->tasks[m_scheduler->current_task].sp;
__asm__ (
"ldmfd %0!, {r4-r11} \n"
"msr psp, %0 \n"
"cpsie if \n"
"bx %1 \n"
:: "r"(psp), "r"(RETURN_ON_PSP)
);
}
void scheduler_yield(void) {
SCB_ICSR |= SCB_ICSR_PENDSVSET;
__asm__(
"nop \n"
"nop \n"
"nop \n"
"nop \n"
);
}

30
nanoos/scheduler.h Normal file
View File

@@ -0,0 +1,30 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*
*/
#ifndef SCHEDULER_H_QWERTY
#define SCHEDULER_H_QWERTY
#define SCHEDULER_NUM_TASKS 4
#define TASK_STACK_SIZE 2048
typedef struct {
void (*entry)(void);
void *stack;
unsigned stack_size;
uint32_t sp;
bool runnable;
} task_t;
typedef struct {
task_t tasks[SCHEDULER_NUM_TASKS];
uint8_t current_task;
} scheduler_t;
void scheduler_init(scheduler_t *scheduler);
void scheduler_task(scheduler_t *scheduler, int task_no, void (*entry)(void));
void scheduler_yield(void);
#endif

24
nanoos/semaphore.c Normal file
View File

@@ -0,0 +1,24 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include "semaphore.h"
#include "atomic.h"
void sem_init(sem_t* sem, int32_t value) {
sem->value = value;
}
int32_t sem_wait(sem_t* sem) {
//while(sem->value <= 0);
//sem->value--;
//return sem->value;
//while (atomic_dec32(&(sem->value), (int32_t)0) <= 0);
return atomic_dec32le0(&(sem->value), (int32_t)1);
}
int32_t sem_post(sem_t* sem) {
//sem->value++;
//return sem->value;
return atomic_inc32(&(sem->value), (int32_t)1);
}

14
nanoos/semaphore.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdint.h>
typedef struct {
int32_t value;
} sem_t;
void sem_init(sem_t* sem, int32_t value);
int32_t sem_wait(sem_t* sem);
int32_t sem_post(sem_t* sem);

141
nanoos/syscall.c Normal file
View File

@@ -0,0 +1,141 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <libopencm3/stm32/usart.h>
#include <stdlib.h>
#include <reent.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <errno.h>
#include <usartu.h>
#undef errno
extern int errno;
char *__env[1] = { 0 };
char **environ = __env;
int _execve(char *name, char **argv, char **env) {
errno = ENOMEM;
return -1;
}
int _fork(void) {
errno = EAGAIN;
return -1;
}
int _getpid(void) {
return 1;
}
int _kill(int pid, int sig) {
errno = EINVAL;
return -1;
}
int _exit() {
while (1);
}
int _isatty(int file) {
return 1;
}
int _fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new) {
errno = EMLINK;
return -1;
}
int _lseek(int file, int ptr, int dir) {
return 0;
}
int _open(const char *name, int flags, int mode) {
return -1;
}
#define STDIN 0
#define STDOUT 1
#define STDERR 3
int _read(int file, char *ptr, int len) {
int i = 0;
while (i < len) {
ptr[i++] = 0;
}
return i;
}
int _write(int file, char *ptr, int len) {
int i;
if ((file == STDOUT) || (file == STDERR)) {
for (i = 0; i < len; i++) {
usart_putc(USART1, ptr[i]);
}
return len;
}
return 0;
}
int _stat(char *file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _close(int file) {
return -1;
}
int _times(struct tms *buf) {
return -1;
}
int _unlink(char *name) {
errno = ENOENT;
return -1;
}
int _wait(int *status) {
errno = ECHILD;
return -1;
}
void *_sbrk(int incr) {
extern const void *_heap;
extern const void *_eheap;
void *prev_heap;
static void *heap = NULL;
if (heap == NULL) {
heap = (void *)&_heap;
}
void* next_heap = heap + incr;
if (next_heap >= (void*)&_eheap) {
errno = ENOMEM;
return NULL;
}
prev_heap = heap;
heap = next_heap;
return (void*)prev_heap;
}

19
nanoos/usartu.c Normal file
View File

@@ -0,0 +1,19 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <libopencm3/stm32/usart.h>
#include <stdlib.h>
#include <stdio.h>
void usart_puts(uint32_t usart, uint8_t * str) {
uint16_t i = 0;
while (str[i] != 0) {
usart_send_blocking(usart, str[i++]);
}
}
void usart_putc(uint32_t usart, uint8_t c) {
usart_send_blocking(usart, c);
}

7
nanoos/usartu.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef _USARTU_H_XYZ
#define _USARTU_H_XYZ
void usart_puts(uint32_t usart, uint8_t * str);
void usart_putc(uint32_t usart, uint8_t c);
#endif