diff --git a/contrib/usart-stm32f103.c b/contrib/usart-stm32f103.c index c6bdc26..7bc5e33 100644 --- a/contrib/usart-stm32f103.c +++ b/contrib/usart-stm32f103.c @@ -116,8 +116,8 @@ struct usart { struct USART *USART; struct usart_stat *stat; struct chx_intr *intr; - struct rb *rb_h2a; struct rb *rb_a2h; + struct rb *rb_h2a; uint8_t *buf_a2h; uint8_t *buf_h2a; chopstx_poll_cond_t *app_write_event; diff --git a/contrib/usart-stm32l4.c b/contrib/usart-stm32l4.c index 6f781e6..d69f87f 100644 --- a/contrib/usart-stm32l4.c +++ b/contrib/usart-stm32l4.c @@ -85,8 +85,8 @@ struct usart { struct USART *USART; struct usart_stat *stat; struct chx_intr *intr; - struct rb *rb_h2a; struct rb *rb_a2h; + struct rb *rb_h2a; uint8_t *buf_a2h; uint8_t *buf_h2a; chopstx_poll_cond_t *app_write_event; diff --git a/example-usart/Makefile b/example-usart/Makefile new file mode 100644 index 0000000..c7d82a4 --- /dev/null +++ b/example-usart/Makefile @@ -0,0 +1,47 @@ +# Makefile for example application of Chopstx + +PROJECT = sample + +### Currently, it is for STM32 Nucleo L432 +### +### Please change lines started with '###' for Cortex-M3 board. +### +### Please change lines started with '###' for Cortex-M0+ board +### (STM32F0 Discovery). + +CHOPSTX = .. +LDSCRIPT= sample.ld.m4 +### LDSCRIPT= sample.ld +### LDSCRIPT= sample.ld.m3 +CSRC = sample.c + +CHIP=stm32l4 +USE_SYS = yes +USE_USART = yes + +################################### +CROSS = arm-none-eabi- +CC = $(CROSS)gcc +LD = $(CROSS)gcc +OBJCOPY = $(CROSS)objcopy + +### MCU = cortex-m3 +### MCU = cortex-m0 +MCU = cortex-m4 +CWARN = -Wall -Wextra -Wstrict-prototypes +DEFS = -DUSE_SYS3 -DFREE_STANDING -DMHZ=80 +### DEFS = -DFREE_STANDING -DUSE_SYS3 -DBUSY_LOOP -DCHX_FLAGS_MAIN=CHOPSTX_SCHED_RR +OPT = -O3 -Os -g +LIBS = + +#################### +include ../rules.mk + +board.h: + @echo Please make a symbolic link \'board.h\' to a file in ../board; + @exit 1 + +sys.c: board.h + +distclean: clean + rm -f board.h diff --git a/example-usart/board.h b/example-usart/board.h new file mode 120000 index 0000000..8075a6c --- /dev/null +++ b/example-usart/board.h @@ -0,0 +1 @@ +../board/board-st-nucleo-l432.h \ No newline at end of file diff --git a/example-usart/sample.c b/example-usart/sample.c new file mode 100644 index 0000000..8e517ff --- /dev/null +++ b/example-usart/sample.c @@ -0,0 +1,136 @@ +#include +#include +#include + +#include "board.h" +#include "sys.h" /* for set_led */ +#include + +static chopstx_mutex_t mtx; +static chopstx_cond_t cnd0; +static chopstx_cond_t cnd1; + +static uint8_t u, v; +static uint8_t m; /* 0..100 */ + +static void +wait_for (uint32_t usec) +{ +#if defined(BUSY_LOOP) + uint32_t count = usec * 6; + uint32_t i; + + for (i = 0; i < count; i++) + asm volatile ("" : : "r" (i) : "memory"); +#else + chopstx_usec_wait (usec); +#endif +} + +static void * +pwm (void *arg) +{ + (void)arg; + + chopstx_mutex_lock (&mtx); + chopstx_cond_wait (&cnd0, &mtx); + chopstx_mutex_unlock (&mtx); + + while (1) + { + set_led (u&v); + wait_for (m); + set_led (0); + wait_for (100-m); + } + + return NULL; +} + +static void * +blk (void *arg) +{ + (void)arg; + + chopstx_mutex_lock (&mtx); + chopstx_cond_wait (&cnd1, &mtx); + chopstx_mutex_unlock (&mtx); + + while (1) + { + v = 0; + wait_for (200*1000); + v = 1; + wait_for (200*1000); + } + + return NULL; +} + +#if defined(BUSY_LOOP) +#define PRIO_PWM (CHOPSTX_SCHED_RR|1) +#define PRIO_BLK (CHOPSTX_SCHED_RR|1) +#else +#define PRIO_PWM 3 +#define PRIO_BLK 2 +#endif + +#define STACK_MAIN +#define STACK_PROCESS_1 +#define STACK_PROCESS_2 +#define STACK_PROCESS_3 +#include "stack-def.h" + + +#define STACK_ADDR_PWM ((uint32_t)process1_base) +#define STACK_SIZE_PWM (sizeof process1_base) + +#define STACK_ADDR_BLK ((uint32_t)process2_base) +#define STACK_SIZE_BLK (sizeof process2_base) + +#define PRIO_USART 4 +#define STACK_ADDR_USART ((uint32_t)process3_base) +#define STACK_SIZE_USART (sizeof process3_base) + +static int +ss_notify (uint8_t dev_no, uint16_t state_bits) +{ + (void)dev_no; + (void)state_bits; + return 0; +} + +int +main (int argc, const char *argv[]) +{ + (void)argc; + (void)argv; + + chopstx_mutex_init (&mtx); + chopstx_cond_init (&cnd0); + chopstx_cond_init (&cnd1); + + m = 10; + + chopstx_create (PRIO_PWM, STACK_ADDR_PWM, STACK_SIZE_PWM, pwm, NULL); + chopstx_create (PRIO_BLK, STACK_ADDR_BLK, STACK_SIZE_BLK, blk, NULL); + + chopstx_usec_wait (200*1000); + + chopstx_mutex_lock (&mtx); + chopstx_cond_signal (&cnd0); + chopstx_cond_signal (&cnd1); + chopstx_mutex_unlock (&mtx); + + usart_init (PRIO_USART, STACK_ADDR_USART, STACK_SIZE_USART, ss_notify); + usart_config (2, B115200 | CS8 | STOP1B); + + while (1) + { + u ^= 1; + wait_for (200*1000*6); + usart_write (2, "Hello\r\n", 7); + } + + return 0; +} diff --git a/example-usart/sample.ld b/example-usart/sample.ld new file mode 100644 index 0000000..a9be67f --- /dev/null +++ b/example-usart/sample.ld @@ -0,0 +1,126 @@ +/* + * ST32F0 memory setup. + */ +MEMORY +{ + flash0 : org = 0x08000000, len = 4k + flash : org = 0x08000000+0x1000, len = 60k + ram : org = 0x20000000, len = 20k +} + +__ram_start__ = ORIGIN(ram); +__ram_size__ = 20k; +__ram_end__ = __ram_start__ + __ram_size__; + +SECTIONS +{ + . = 0; + + .sys : ALIGN(4) SUBALIGN(4) + { + _sys = .; + KEEP(*(.vectors)) + . = ALIGN(16); + KEEP(*(.sys.version)) + KEEP(*(.sys.board_id)) + KEEP(*(.sys.board_name)) + build/sys-*.o(.text) + build/sys-*.o(.text.*) + build/sys-*.o(.rodata) + build/sys-*.o(.rodata.*) + . = ALIGN(1024); +/* + *(.sys.0) + *(.sys.1) + *(.sys.2) +*/ + } > flash0 =0xffffffff + + _text = .; + + .startup : ALIGN(128) SUBALIGN(128) + { + KEEP(*(.startup.vectors)) + . = ALIGN (16); + } > flash =0xffffffff + + .text : ALIGN(16) SUBALIGN(16) + { + *(.text.startup.*) + *(.text) + *(.text.*) + *(.rodata) + *(.rodata.*) + *(.glue_7t) + *(.glue_7) + *(.gcc*) + . = ALIGN(8); + } > flash + + .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)} > flash + + .ARM.exidx : { + PROVIDE(__exidx_start = .); + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + PROVIDE(__exidx_end = .); + } > flash + + .eh_frame_hdr : {*(.eh_frame_hdr)} > flash + + .eh_frame : ONLY_IF_RO {*(.eh_frame)} > flash + + .textalign : ONLY_IF_RO { . = ALIGN(8); } > flash + + _etext = .; + _textdata = _etext; + + .vectors_in_ram : + { + . = ALIGN(8); + __vector_ram_addr__ = .; + KEEP(*(.bss.startup.*)) + } > ram + + .stacks (NOLOAD) : + { + . = ALIGN(8); + *(.main_stack) + *(.process_stack.0) + *(.process_stack.1) + *(.process_stack.2) + *(.process_stack.3) + . = ALIGN(8); + } > ram + + .data : + { + . = ALIGN(4); + PROVIDE(_data = .); + *(.data) + . = ALIGN(4); + *(.data.*) + . = ALIGN(4); + *(.ramtext) + . = ALIGN(4); + PROVIDE(_edata = .); + } > ram AT > flash + + .bss : + { + . = ALIGN(4); + PROVIDE(_bss_start = .); + *(.bss) + . = ALIGN(4); + *(.bss.*) + . = ALIGN(4); + *(COMMON) + . = ALIGN(4); + PROVIDE(_bss_end = .); + } > ram + + PROVIDE(end = .); + _end = .; +} + +__heap_base__ = _end; +__heap_end__ = __ram_end__; diff --git a/example-usart/sample.ld.m3 b/example-usart/sample.ld.m3 new file mode 100644 index 0000000..57ddb83 --- /dev/null +++ b/example-usart/sample.ld.m3 @@ -0,0 +1,116 @@ +/* + * ST32F103 memory setup. + */ +MEMORY +{ + flash0 : org = 0x08000000, len = 4k + flash : org = 0x08000000+0x1000, len = 60k + ram : org = 0x20000000, len = 20k +} + +__ram_start__ = ORIGIN(ram); +__ram_size__ = 20k; +__ram_end__ = __ram_start__ + __ram_size__; + +SECTIONS +{ + . = 0; + + .sys : ALIGN(4) SUBALIGN(4) + { + _sys = .; + KEEP(*(.vectors)) + . = ALIGN(16); + KEEP(*(.sys.version)) + KEEP(*(.sys.board_id)) + KEEP(*(.sys.board_name)) + build/sys-*.o(.text) + build/sys-*.o(.text.*) + build/sys-*.o(.rodata) + build/sys-*.o(.rodata.*) + . = ALIGN(1024); + *(.sys.0) + *(.sys.1) + *(.sys.2) + } > flash0 + + _text = .; + + .startup : ALIGN(128) SUBALIGN(128) + { + KEEP(*(.startup.vectors)) + . = ALIGN (16); + } > flash =0xffffffff + + .text : ALIGN(16) SUBALIGN(16) + { + *(.text.startup.*) + *(.text) + *(.text.*) + *(.rodata) + *(.rodata.*) + *(.glue_7t) + *(.glue_7) + *(.gcc*) + . = ALIGN(8); + } > flash + + .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)} > flash + + .ARM.exidx : { + PROVIDE(__exidx_start = .); + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + PROVIDE(__exidx_end = .); + } > flash + + .eh_frame_hdr : {*(.eh_frame_hdr)} > flash + + .eh_frame : ONLY_IF_RO {*(.eh_frame)} > flash + + .textalign : ONLY_IF_RO { . = ALIGN(8); } > flash + + _etext = .; + _textdata = _etext; + + .stacks (NOLOAD) : + { + *(.main_stack) + *(.process_stack.0) + *(.process_stack.1) + *(.process_stack.2) + *(.process_stack.3) + . = ALIGN(8); + } > ram + + .data : + { + . = ALIGN(4); + PROVIDE(_data = .); + *(.data) + . = ALIGN(4); + *(.data.*) + . = ALIGN(4); + *(.ramtext) + . = ALIGN(4); + PROVIDE(_edata = .); + } > ram AT > flash + + .bss : + { + . = ALIGN(4); + PROVIDE(_bss_start = .); + *(.bss) + . = ALIGN(4); + *(.bss.*) + . = ALIGN(4); + *(COMMON) + . = ALIGN(4); + PROVIDE(_bss_end = .); + } > ram + + PROVIDE(end = .); + _end = .; +} + +__heap_base__ = _end; +__heap_end__ = __ram_end__; diff --git a/example-usart/sample.ld.m4 b/example-usart/sample.ld.m4 new file mode 100644 index 0000000..e4b2548 --- /dev/null +++ b/example-usart/sample.ld.m4 @@ -0,0 +1,107 @@ +/* + * ST32L4 memory setup. + */ +MEMORY +{ + flash : org = 0x08000000, len = 256k + ram : org = 0x20000000, len = 48k +} + +__ram_start__ = ORIGIN(ram); +__ram_size__ = 20k; +__ram_end__ = __ram_start__ + __ram_size__; + +SECTIONS +{ + . = 0; + + _text = .; + + .startup : ALIGN(128) SUBALIGN(128) + { + KEEP(*(.startup.vectors)) + . = ALIGN(16); + _sys = .; + . = ALIGN(16); + KEEP(*(.sys.version)) + KEEP(*(.sys.board_id)) + KEEP(*(.sys.board_name)) + build/sys-*.o(.text) + build/sys-*.o(.text.*) + build/sys-*.o(.rodata) + build/sys-*.o(.rodata.*) + . = ALIGN(1024); + } > flash =0xffffffff + + .text : ALIGN(16) SUBALIGN(16) + { + *(.text.startup.*) + *(.text) + *(.text.*) + *(.rodata) + *(.rodata.*) + *(.glue_7t) + *(.glue_7) + *(.gcc*) + . = ALIGN(8); + } > flash + + .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)} > flash + + .ARM.exidx : { + PROVIDE(__exidx_start = .); + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + PROVIDE(__exidx_end = .); + } > flash + + .eh_frame_hdr : {*(.eh_frame_hdr)} > flash + + .eh_frame : ONLY_IF_RO {*(.eh_frame)} > flash + + .textalign : ONLY_IF_RO { . = ALIGN(8); } > flash + + _etext = .; + _textdata = _etext; + + .stacks (NOLOAD) : + { + *(.main_stack) + *(.process_stack.0) + *(.process_stack.1) + *(.process_stack.2) + *(.process_stack.3) + . = ALIGN(8); + } > ram + + .data : + { + . = ALIGN(4); + PROVIDE(_data = .); + *(.data) + . = ALIGN(4); + *(.data.*) + . = ALIGN(4); + *(.ramtext) + . = ALIGN(4); + PROVIDE(_edata = .); + } > ram AT > flash + + .bss : + { + . = ALIGN(4); + PROVIDE(_bss_start = .); + *(.bss) + . = ALIGN(4); + *(.bss.*) + . = ALIGN(4); + *(COMMON) + . = ALIGN(4); + PROVIDE(_bss_end = .); + } > ram + + PROVIDE(end = .); + _end = .; +} + +__heap_base__ = _end; +__heap_end__ = __ram_end__; diff --git a/example-usart/stack-def.h b/example-usart/stack-def.h new file mode 100644 index 0000000..fb9ace6 --- /dev/null +++ b/example-usart/stack-def.h @@ -0,0 +1,54 @@ +#define MAIN_SIZE 0x0080 /* Idle+Exception handlers */ +#define SIZE_0 0x0200 /* Main program */ +#define SIZE_1 0x0100 /* first thread program */ +#define SIZE_2 0x0100 /* second thread program */ +#define SIZE_3 0x0200 /* third thread program */ + +#if defined(STACK_MAIN) +/* + * The terminology of "main" is confusing in ARM architecture. + * Here, "main_base" is for exception handlers. + */ +/* Idle+Exception handlers */ +char __main_stack_end__[0] __attribute__ ((section(".main_stack"))); +char main_base[MAIN_SIZE] __attribute__ ((section(".main_stack"))); + +/* Main program */ +char __process0_stack_end__[0] __attribute__ ((section(".process_stack.0"))); +char process0_base[SIZE_0] __attribute__ ((section(".process_stack.0"))); +#endif + +/* First thread program */ +#if defined(STACK_PROCESS_1) +char process1_base[SIZE_1] __attribute__ ((section(".process_stack.1"))); +#endif + +/* Second thread program */ +#if defined(STACK_PROCESS_2) +char process2_base[SIZE_2] __attribute__ ((section(".process_stack.2"))); +#endif + +/* Third thread program */ +#if defined(STACK_PROCESS_3) +char process3_base[SIZE_3] __attribute__ ((section(".process_stack.3"))); +#endif + +/* Fourth thread program */ +#if defined(STACK_PROCESS_4) +char process4_base[SIZE_4] __attribute__ ((section(".process_stack.4"))); +#endif + +/* Fifth thread program */ +#if defined(STACK_PROCESS_5) +char process5_base[SIZE_5] __attribute__ ((section(".process_stack.5"))); +#endif + +/* Sixth thread program */ +#if defined(STACK_PROCESS_6) +char process6_base[SIZE_6] __attribute__ ((section(".process_stack.6"))); +#endif + +/* Seventh thread program */ +#if defined(STACK_PROCESS_7) +char process7_base[SIZE_7] __attribute__ ((section(".process_stack.7"))); +#endif