diff options
-rw-r--r-- | .gitmodules | 3 | ||||
m--------- | ext/libopencm3 | 0 | ||||
-rw-r--r-- | include/arch/stm32f446re-nucleo/driver/gpio.h | 63 | ||||
-rw-r--r-- | include/arch/stm32f446re-nucleo/driver/stdout.h | 19 | ||||
-rw-r--r-- | src/arch/stm32f446re-nucleo/Makefile.inc | 116 | ||||
-rw-r--r-- | src/arch/stm32f446re-nucleo/arch.cc | 54 | ||||
-rw-r--r-- | src/arch/stm32f446re-nucleo/driver/gpio.cc | 3 | ||||
-rw-r--r-- | src/arch/stm32f446re-nucleo/driver/stdout.cc | 31 | ||||
-rw-r--r-- | src/arch/stm32f446re-nucleo/stm32f446.ld | 7 |
9 files changed, 296 insertions, 0 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..01bc084 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libopencm3"] + path = ext/libopencm3 + url = https://github.com/libopencm3/libopencm3.git diff --git a/ext/libopencm3 b/ext/libopencm3 new file mode 160000 +Subproject 24bef9c49eda109e92e926e065b246a71d454f2 diff --git a/include/arch/stm32f446re-nucleo/driver/gpio.h b/include/arch/stm32f446re-nucleo/driver/gpio.h new file mode 100644 index 0000000..378a84f --- /dev/null +++ b/include/arch/stm32f446re-nucleo/driver/gpio.h @@ -0,0 +1,63 @@ +#ifndef GPIO_H +#define GPIO_H + +#include <libopencm3/stm32/rcc.h> +#include <libopencm3/stm32/gpio.h> + +class GPIO { + private: + GPIO(const GPIO ©); + + public: + GPIO () {} + + enum Pin : unsigned char { + pa_0 = 0, pa_1, pa_2, pa_3, pa_4, pa_5, pa_6, pa_7, + pa_8, pa_9, pa_10, pa_11, pa_12, pa_13, pa_14, pa_15, + pb_0, pb_1, pb_2, pb_3, pb_4, pb_5, pb_6, pb_7, + pb_8, pb_9, pb_10, pb_11, pb_12, pb_13, pb_14, pb_15, + pc_0, pc_1, pc_2, pc_3, pc_4, pc_5, pc_6, pc_7, + pc_8, pc_9, pc_10, pc_11, pc_12, pc_13, pc_14, pc_15, + PIN_INVALID + }; + + inline void setup() { + rcc_periph_clock_enable(RCC_GPIOA); + rcc_periph_clock_enable(RCC_GPIOB); + rcc_periph_clock_enable(RCC_GPIOC); + + // Set LED as output + gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5); + } +#pragma GCC diagnostic ignored "-Wunused-parameter" + inline void led_on(unsigned char id) { + gpio_set(GPIOA, GPIO5); + } + inline void led_off(unsigned char id) { + gpio_clear(GPIOA, GPIO5); + } + inline void led_toggle(unsigned char id) { + gpio_toggle(GPIOA, GPIO5); + } +#pragma GCC diagnostic pop + /* + inline void input(unsigned char const pin) { + } + inline void input(unsigned char const pin, unsigned char const pull) { + } + inline void output(unsigned char const pin) { + } + inline void output(unsigned char const pin, unsigned char const value) { + } + inline unsigned char read(unsigned char const pin) { + } + inline void write(unsigned char const pin, unsigned char value) { + } + inline void write_mask(unsigned char const pin_base, unsigned char set_mask, unsigned char clear_mask) { + } + */ +}; + +extern GPIO gpio; + +#endif diff --git a/include/arch/stm32f446re-nucleo/driver/stdout.h b/include/arch/stm32f446re-nucleo/driver/stdout.h new file mode 100644 index 0000000..2eb669d --- /dev/null +++ b/include/arch/stm32f446re-nucleo/driver/stdout.h @@ -0,0 +1,19 @@ +#ifndef STANDARDOUTPUT_H +#define STANDARDOUTPUT_H + +#include "object/outputstream.h" + +class StandardOutput : public OutputStream { + private: + StandardOutput(const StandardOutput ©); + + public: + StandardOutput () {} + void setup(); + + virtual void put(char c) override; +}; + +extern StandardOutput kout; + +#endif diff --git a/src/arch/stm32f446re-nucleo/Makefile.inc b/src/arch/stm32f446re-nucleo/Makefile.inc new file mode 100644 index 0000000..54792d3 --- /dev/null +++ b/src/arch/stm32f446re-nucleo/Makefile.inc @@ -0,0 +1,116 @@ +# vim:ft=make + +SERIAL_PORT ?= ttyACM0 + +INCLUDES += -Iext/libopencm3/include + +COMMON_FLAGS += --static -nostartfiles -g3 -Os -fno-common +COMMON_FLAGS += -ffunction-sections -fdata-sections +COMMON_FLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -DSTM32F4 + +CC = arm-none-eabi-gcc +CXX = arm-none-eabi-g++ +OBJCOPY = arm-none-eabi-objcopy +OBJDUMP = arm-none-eabi-objdump + +CXX_TARGETS += src/arch/stm32f446re-nucleo/arch.cc + +ifeq (${aspectc}, 1) + CXX = ag++ -r build/repo.acp -v 0 --c_compiler arm-none-eabi-g++ -p . --Xcompiler +endif + +ifneq ($(findstring adc,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/adc.cc +endif + +CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/gpio.cc +CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/stdout.cc +#CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/uptime.cc + +ifneq ($(findstring stdin,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/stdin.cc +endif + +ifneq ($(findstring softi2c,${drivers}), ) +else ifneq ($(findstring i2c,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/i2c.cc + COMMON_FLAGS += -DDRIVER_I2C +endif + +ifneq ($(findstring spi_a1,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/spi_a1.cc +endif + +ifneq ($(findstring spi_b,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/spi_b.cc +endif + +ifneq ($(findstring timer,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/timer.cc +endif + +ifneq ($(findstring counter,${arch_drivers}), ) + CXX_TARGETS += src/arch/stm32f446re-nucleo/driver/counter.cc +endif + + +OBJECTS = ${CXX_TARGETS:.cc=.o} ${C_TARGETS:.c=.o} ${ASM_TARGETS:.S=.o} + +.cc.o: + ${QUIET}${CXX} ${INCLUDES} ${COMMON_FLAGS} ${CXXFLAGS} -c -o $@ ${@:.o=.cc} + +.c.o: + ${QUIET}${CC} ${INCLUDES} ${COMMON_FLAGS} ${CFLAGS} -c -o $@ ${@:.o=.c} + +.S.o: + ${QUIET}${CC} ${INCLUDES} ${COMMON_FLAGS} -c -o $@ ${@:.o=.S} + +.s.o: + ${QUIET}${CC} ${INCLUDES} ${COMMON_FLAGS} -c -o $@ ${@:.o=.S} + +# deliberately no ${MAKE} here -- multipass relies on make -B, but we don't +# want to re-make libopencm3 all the time +ext/libopencm3/lib/libopencm3_stm32f4.a: ext/libopencm3/Makefile + git submodule update --init + ${MAKE} -C ext/libopencm3 + +build/system.elf: ${OBJECTS} ext/libopencm3/lib/libopencm3_stm32f4.a + ${QUIET}mkdir -p build + ${QUIET}${CXX} ${INCLUDES} ${COMMON_FLAGS} ${CXXFLAGS} \ + -Lext/libopencm3/lib -Wl,--gc-sections \ + -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \ + -T src/arch/stm32f446re-nucleo/stm32f446.ld \ + ${OBJECTS} -lopencm3_stm32f4 \ + -o $@ + +program: build/system.elf + openocd -c 'source [find interface/stlink-v2-1.cfg]' \ + -c 'transport select hla_swd' -c 'source [find target/stm32f4x.cfg]' \ + -c 'reset_config srst_only' -c '$$_TARGETNAME configure -rtos auto' \ + -c 'reset_config connect_assert_srst' -c init -c targets \ + -c 'reset halt' -c 'flash write_image erase "build/system.elf" 0' \ + 'verify_image "build/system.elf" 0' -c 'reset run' -c 'shutdown' + +arch_clean: + ${QUIET}rm -f ${OBJECTS} + +monitor: + ${QUIET}screen /dev/${SERIAL_PORT} 115200 + +arch_help: + @echo "stm32f446re-nucleo specific flags:" + @echo " DEBUG_PORT = ${DEBUG_PORT}" + @echo " SERIAL_PORT = ${SERIAL_PORT}" + @echo " cpu_freq = ${cpu_freq} (desired CPU frequency in Hz)" + @echo " supported frequencies: 1 / 4 / 8 / 16 MHz" + @echo " MSP430_FLASHER_DIR = /home/derf/var/projects/msp430/MSP430Flasher_1.3.7" + @echo " (required for flashing, must contain libmsp430.so and MSP430Flasher)" + +arch_info: + @echo "CPU Freq: ${cpu_freq} Hz" + @echo "Timer Freq: ${timer_freq} Hz -> $(shell src/arch/stm32f446re-nucleo/model.py f_timer "${cpu_freq}" "${timer_freq}")" + @echo "I2C Freq: ${i2c_freq} Hz" + @echo "Counter Overflow: 65536/255" + @echo "Monitor: /dev/${SERIAL_PORT} 115200" + +.PHONY: arch_clean arch_help arch_info monitor program diff --git a/src/arch/stm32f446re-nucleo/arch.cc b/src/arch/stm32f446re-nucleo/arch.cc new file mode 100644 index 0000000..12a612a --- /dev/null +++ b/src/arch/stm32f446re-nucleo/arch.cc @@ -0,0 +1,54 @@ +#include "arch.h" + +#ifdef __acweaving +#define __delay_cycles(x) +#endif + +void Arch::setup(void) +{ +} + +#ifdef WITH_WAKEUP +extern void wakeup(); +#endif + +#if defined(WITH_LOOP) +extern void loop(); +volatile char run_loop = 0; +#endif + +void Arch::delay_us(unsigned int const us) +{ + volatile int x = us * 2; + while (x--) { + __asm("nop"); + } +} +void Arch::delay_ms(unsigned int const ms) +{ + for (unsigned int i = 0; i < ms; i++) { + volatile int x = 2000; + while (x--) { + __asm("nop"); + } + } +} + +void Arch::idle_loop(void) +{ + while (1) { + delay_ms(1000); +#ifdef WITH_LOOP + loop(); +#endif + } +} + +void Arch::idle(void) +{ +#ifdef WITH_WAKEUP + wakeup(); +#endif +} + +Arch arch; diff --git a/src/arch/stm32f446re-nucleo/driver/gpio.cc b/src/arch/stm32f446re-nucleo/driver/gpio.cc new file mode 100644 index 0000000..1403aed --- /dev/null +++ b/src/arch/stm32f446re-nucleo/driver/gpio.cc @@ -0,0 +1,3 @@ +#include "driver/gpio.h" + +GPIO gpio; diff --git a/src/arch/stm32f446re-nucleo/driver/stdout.cc b/src/arch/stm32f446re-nucleo/driver/stdout.cc new file mode 100644 index 0000000..2c20158 --- /dev/null +++ b/src/arch/stm32f446re-nucleo/driver/stdout.cc @@ -0,0 +1,31 @@ +#include "driver/stdout.h" + +#include <libopencm3/stm32/rcc.h> +#include <libopencm3/stm32/gpio.h> +#include <libopencm3/stm32/usart.h> + +void StandardOutput::setup() +{ + rcc_periph_clock_enable(RCC_USART2); + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); + + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + usart_enable(USART2); +} + +void StandardOutput::put(char c) +{ + usart_send_blocking(USART2, c); + if (c == '\n') { + put('\r'); + } +} + +StandardOutput kout; diff --git a/src/arch/stm32f446re-nucleo/stm32f446.ld b/src/arch/stm32f446re-nucleo/stm32f446.ld new file mode 100644 index 0000000..d53b56e --- /dev/null +++ b/src/arch/stm32f446re-nucleo/stm32f446.ld @@ -0,0 +1,7 @@ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K +} + +INCLUDE ext/libopencm3/lib/cortex-m-generic.ld |