aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32
diff options
context:
space:
mode:
Diffstat (limited to 'examples/stm32')
-rw-r--r--examples/stm32/Makefile.include9
-rw-r--r--examples/stm32/lisa-m/Makefile8
-rw-r--r--examples/stm32/lisa-m/fancyblink/Makefile23
-rw-r--r--examples/stm32/lisa-m/fancyblink/fancyblink.c67
-rw-r--r--examples/stm32/lisa-m/fancyblink/fancyblink.ld31
-rw-r--r--examples/stm32/obldc/usart_irq/usart_irq.c6
-rw-r--r--examples/stm32/stm32-h103/Makefile26
-rw-r--r--examples/stm32/stm32-h103/timer/Makefile23
-rw-r--r--examples/stm32/stm32-h103/timer/timer.c183
-rw-r--r--examples/stm32/stm32-h103/timer/timer.ld31
-rw-r--r--examples/stm32/stm32-h103/usart_irq/Makefile23
-rw-r--r--examples/stm32/stm32-h103/usart_irq/usart_irq.c119
-rw-r--r--examples/stm32/stm32-h103/usart_irq/usart_irq.ld31
-rw-r--r--examples/stm32/stm32-h103/usart_irq_printf/Makefile23
-rw-r--r--examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.c272
-rw-r--r--examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.ld31
-rw-r--r--examples/stm32/stm32-h103/usart_printf/Makefile23
-rw-r--r--examples/stm32/stm32-h103/usart_printf/usart_printf.c108
-rw-r--r--examples/stm32/stm32-h103/usart_printf/usart_printf.ld31
19 files changed, 1058 insertions, 10 deletions
diff --git a/examples/stm32/Makefile.include b/examples/stm32/Makefile.include
index af6d02f..3eb3d35 100644
--- a/examples/stm32/Makefile.include
+++ b/examples/stm32/Makefile.include
@@ -28,10 +28,11 @@ OBJDUMP = $(PREFIX)-objdump
# TOOLCHAIN_DIR = `dirname \`which $(CC)\``/../$(PREFIX)
TOOLCHAIN_DIR = ../../../..
CFLAGS += -Os -g -Wall -Wextra -I$(TOOLCHAIN_DIR)/include \
- -fno-common -mcpu=cortex-m3 -mthumb
+ -fno-common -mcpu=cortex-m3 -mthumb -msoft-float
LDSCRIPT = $(BINARY).ld
-LDFLAGS += -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/stm32 \
- -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections
+LDFLAGS += -lc -lnosys -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/stm32 \
+ -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \
+ -mthumb -march=armv7 -mfix-cortex-m3-ldrd -msoft-float
OBJS += $(BINARY).o
OOCD ?= openocd
@@ -76,7 +77,7 @@ flash: $(BINARY).flash
%.elf: $(OBJS) $(LDSCRIPT)
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
- $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_stm32
+ $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32 $(LDFLAGS)
%.o: %.c Makefile
@#printf " CC $(subst $(shell pwd)/,,$(@))\n"
diff --git a/examples/stm32/lisa-m/Makefile b/examples/stm32/lisa-m/Makefile
index 877e66b..fa554bc 100644
--- a/examples/stm32/lisa-m/Makefile
+++ b/examples/stm32/lisa-m/Makefile
@@ -25,7 +25,11 @@ MAKEFLAGS += --no-print-directory
endif
# TODO: Add usb_dfu later, doesn't build at the moment.
-all: usb_hid
+all: fancyblink usb_hid
+
+fancyblink:
+ @printf " BUILD examples/stm32/lisa-m/fancyblink\n"
+ $(Q)$(MAKE) -C usb_hid
usb_hid:
@printf " BUILD examples/stm32/lisa-m/usb_hid\n"
@@ -35,5 +39,5 @@ clean:
@printf " CLEAN examples/stm32/lisa-m/usb_hid\n"
$(Q)$(MAKE) -C usb_hid clean
-.PHONY: usb_hid clean
+.PHONY: fancyblink usb_hid clean
diff --git a/examples/stm32/lisa-m/fancyblink/Makefile b/examples/stm32/lisa-m/fancyblink/Makefile
new file mode 100644
index 0000000..1baec4d
--- /dev/null
+++ b/examples/stm32/lisa-m/fancyblink/Makefile
@@ -0,0 +1,23 @@
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+BINARY = fancyblink
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/lisa-m/fancyblink/fancyblink.c b/examples/stm32/lisa-m/fancyblink/fancyblink.c
new file mode 100644
index 0000000..efada62
--- /dev/null
+++ b/examples/stm32/lisa-m/fancyblink/fancyblink.c
@@ -0,0 +1,67 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ * Copyright (C) 2011 Piotr Esden-Tempski <piotr@esden.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+
+/* Set STM32 to 72 MHz. */
+void clock_setup(void)
+{
+ rcc_clock_setup_in_hse_16mhz_out_72mhz();
+
+ /* Enable GPIOC clock. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
+
+}
+
+void gpio_setup(void)
+{
+ /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+ gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
+
+ AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;
+
+ /* preconfigure the led's */
+ gpio_set(GPIOB, GPIO4); /* switch off led */
+ gpio_clear(GPIOC, GPIO13); /* switch on led */
+}
+
+int main(void)
+{
+ int i;
+
+ clock_setup();
+ gpio_setup();
+
+ /* Blink the LED (PC12) on the board. */
+ while (1) {
+ gpio_toggle(GPIOC, GPIO13); /* LED on/off */
+ gpio_toggle(GPIOB, GPIO4); /* LED on/off */
+ for (i = 0; i < 800000; i++) /* Wait a bit. */
+ __asm__("nop");
+ }
+
+ return 0;
+}
diff --git a/examples/stm32/lisa-m/fancyblink/fancyblink.ld b/examples/stm32/lisa-m/fancyblink/fancyblink.ld
new file mode 100644
index 0000000..7ea2b92
--- /dev/null
+++ b/examples/stm32/lisa-m/fancyblink/fancyblink.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32.ld
+
diff --git a/examples/stm32/obldc/usart_irq/usart_irq.c b/examples/stm32/obldc/usart_irq/usart_irq.c
index 2d4dd06..7e5f971 100644
--- a/examples/stm32/obldc/usart_irq/usart_irq.c
+++ b/examples/stm32/obldc/usart_irq/usart_irq.c
@@ -80,7 +80,8 @@ void usart1_isr(void)
static u8 data = 'A';
/* Check if we were called because of RXNE. */
- if ((USART_SR(USART1) & USART_SR_RXNE) != 0) {
+ if (((USART_CR1(USART1) & USART_SR_RXNEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
/* Indicate that we got data. */
gpio_toggle(GPIOA, GPIO6);
@@ -92,7 +93,8 @@ void usart1_isr(void)
}
/* Check if we were called because of TXE. */
- if ((USART_SR(USART1) & USART_SR_TXE) != 0) {
+ if ((USART_CR1(USART1) & USART_SR_TXEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_TXE) != 0)) {
/* Indicate that we are sending out data. */
gpio_toggle(GPIOA, GPIO7);
diff --git a/examples/stm32/stm32-h103/Makefile b/examples/stm32/stm32-h103/Makefile
index a319da6..85d0c45 100644
--- a/examples/stm32/stm32-h103/Makefile
+++ b/examples/stm32/stm32-h103/Makefile
@@ -24,7 +24,7 @@ Q := @
MAKEFLAGS += --no-print-directory
endif
-all: miniblink fancyblink usart usb_cdcacm usb_hid button exti_both traceswo
+all: miniblink fancyblink usart usart_irq usart_printf usart_irq_printf timer usb_cdcacm usb_hid button exti_both traceswo
miniblink:
@printf " BUILD examples/stm32/stm32-h103/miniblink\n"
@@ -38,6 +38,22 @@ usart:
@printf " BUILD examples/stm32/stm32-h103/usart\n"
$(Q)$(MAKE) -C usart
+usart_irq:
+ @printf " BUILD examples/stm32/stm32-h103/usart_irq\n"
+ $(Q)$(MAKE) -C usart_irq
+
+usart_printf:
+ @printf " BUILD examples/stm32/stm32-h103/usart_printf\n"
+ $(Q)$(MAKE) -C usart_printf
+
+usart_irq_printf:
+ @printf " BUILD examples/stm32/stm32-h103/usart_irq_printf\n"
+ $(Q)$(MAKE) -C usart_irq_printf
+
+timer:
+ @printf " BUILD examples/stm32/stm32-h103/timer\n"
+ $(Q)$(MAKE) -C timer
+
spi:
@printf " BUILD examples/stm32/stm32-h103/spi\n"
$(Q)$(MAKE) -C spi
@@ -69,6 +85,12 @@ clean:
$(Q)$(MAKE) -C fancyblink clean
@printf " CLEAN examples/stm32/stm32-h103/usart\n"
$(Q)$(MAKE) -C usart clean
+ @printf " CLEAN examples/stm32/stm32-h103/usart_irq\n"
+ $(Q)$(MAKE) -C usart_irq clean
+ @printf " CLEAN examples/stm32/stm32-h103/usart_printf\n"
+ $(Q)$(MAKE) -C usart_printf clean
+ @printf " CLEAN examples/stm32/stm32-h103/usart_irq_printf\n"
+ $(Q)$(MAKE) -C usart_irq_printf clean
@printf " CLEAN examples/stm32/stm32-h103/spi\n"
$(Q)$(MAKE) -C spi clean
@printf " CLEAN examples/stm32/stm32-h103/usb_cdcacm\n"
@@ -82,5 +104,5 @@ clean:
@printf " CLEAN examples/stm32/stm32-h103/traceswo\n"
$(Q)$(MAKE) -C traceswo clean
-.PHONY: miniblink fancyblink usart spi usb_cdcacm usb_hid button exti_both traceswo clean
+.PHONY: miniblink fancyblink usart usart_irq usart_printf usart_irq_printf timer spi usb_cdcacm usb_hid button exti_both traceswo clean
diff --git a/examples/stm32/stm32-h103/timer/Makefile b/examples/stm32/stm32-h103/timer/Makefile
new file mode 100644
index 0000000..86aced4
--- /dev/null
+++ b/examples/stm32/stm32-h103/timer/Makefile
@@ -0,0 +1,23 @@
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+BINARY = timer
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/stm32-h103/timer/timer.c b/examples/stm32/stm32-h103/timer/timer.c
new file mode 100644
index 0000000..ea64c86
--- /dev/null
+++ b/examples/stm32/stm32-h103/timer/timer.c
@@ -0,0 +1,183 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2011 Piotr Esden-Tempski <piotr@esden.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/nvic.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
+#include <libopencm3/stm32/exti.h>
+
+u16 frequency_sequence[18] = {
+ 1000,
+ 500,
+ 1000,
+ 500,
+ 1000,
+ 500,
+ 2000,
+ 500,
+ 2000,
+ 500,
+ 2000,
+ 500,
+ 1000,
+ 500,
+ 1000,
+ 500,
+ 1000,
+ 5000
+};
+
+
+int frequency_sel = 0;
+
+u16 compare_time;
+u16 new_time;
+u16 frequency;
+int debug = 0;
+
+void clock_setup(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+
+}
+
+void gpio_setup(void)
+{
+ /* Enable GPIOC clock. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
+
+ /*
+ * Set GPIO12 (PORTC) (led) to
+ * 'output alternate function push-pull'.
+ */
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
+
+ gpio_set(GPIOC, GPIO12);
+
+}
+
+void tim_setup(void)
+{
+
+ /* Enable TIM2 clock. */
+ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
+
+
+ /* Enable TIM2 interrupt. */
+ nvic_enable_irq(NVIC_TIM2_IRQ);
+
+ /* Reset TIM2 peripheral */
+ timer_reset(TIM2);
+
+ /* Timer global mode:
+ * - No divider
+ * - alignment edge
+ * - direction up
+ */
+ timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT,
+ TIM_CR1_CMS_EDGE,
+ TIM_CR1_DIR_UP);
+
+ /* Reset prescaler value. */
+ timer_set_prescaler(TIM2, 36000);
+
+ /* Enable preload. */
+ timer_disable_preload(TIM2);
+
+ /* Continous mode. */
+ timer_continuous_mode(TIM2);
+
+ /* Period (36kHz) */
+ timer_set_period(TIM2, 65535);
+
+ /* Disable outputs. */
+ timer_disable_oc_output(TIM2, TIM_OC1);
+ timer_disable_oc_output(TIM2, TIM_OC2);
+ timer_disable_oc_output(TIM2, TIM_OC3);
+ timer_disable_oc_output(TIM2, TIM_OC4);
+
+ /* -- OC1 configuration -- */
+
+ /* Configure global mode of line 1. */
+ timer_disable_oc_clear(TIM2, TIM_OC1);
+ timer_disable_oc_preload(TIM2, TIM_OC1);
+ timer_set_oc_slow_mode(TIM2, TIM_OC1);
+ timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_FROZEN);
+
+ /* Set the capture compare value for OC1. */
+ timer_set_oc_value(TIM2, TIM_OC1, 1000);
+
+ /* ---- */
+ /* ARR reload enable */
+ timer_disable_preload(TIM2);
+
+ /* Counter enable */
+ timer_enable_counter(TIM2);
+
+ /* Enable commutation interrupt */
+ timer_enable_irq(TIM2, TIM_DIER_CC1IE);
+}
+
+void tim2_isr(void)
+{
+
+ if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {
+
+ /* Clear compare interrupt flag. */
+ timer_clear_flag(TIM2, TIM_SR_CC1IF);
+
+ /*
+ * Get current timer value to calculate next
+ * compare register value
+ */
+ compare_time = timer_get_counter(TIM2);
+
+ /*
+ * Calculate and set the next compare value.
+ */
+ frequency = frequency_sequence[frequency_sel++];
+ new_time = compare_time + frequency;
+
+ timer_set_oc_value(TIM2, TIM_OC1,
+ new_time);
+ if (frequency_sel == 18) {
+ frequency_sel = 0;
+ }
+
+ /* Toggle led to indicate compare event */
+ gpio_toggle(GPIOC, GPIO12);
+
+ }
+}
+
+int main(void)
+{
+
+ clock_setup();
+ gpio_setup();
+ tim_setup();
+
+ while (1) {
+ __asm("nop");
+ }
+
+ return 0;
+}
diff --git a/examples/stm32/stm32-h103/timer/timer.ld b/examples/stm32/stm32-h103/timer/timer.ld
new file mode 100644
index 0000000..7ea2b92
--- /dev/null
+++ b/examples/stm32/stm32-h103/timer/timer.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32.ld
+
diff --git a/examples/stm32/stm32-h103/usart_irq/Makefile b/examples/stm32/stm32-h103/usart_irq/Makefile
new file mode 100644
index 0000000..6ee93bd
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq/Makefile
@@ -0,0 +1,23 @@
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+BINARY = usart_irq
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/stm32-h103/usart_irq/usart_irq.c b/examples/stm32/stm32-h103/usart_irq/usart_irq.c
new file mode 100644
index 0000000..9b957dc
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq/usart_irq.c
@@ -0,0 +1,119 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
+#include <libopencm3/stm32/nvic.h>
+
+void clock_setup(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+
+ /* Enable GPIOA clock (for LED GPIOs). */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
+
+ /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
+ RCC_APB2ENR_AFIOEN |
+ RCC_APB2ENR_USART1EN);
+}
+
+void usart_setup(void)
+{
+ /* Enable the USART1 interrupt. */
+ nvic_enable_irq(NVIC_USART1_IRQ);
+
+ /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+
+ /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
+ gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
+
+ /* Setup UART parameters. */
+ usart_set_baudrate(USART1, 230400);
+ 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);
+
+ /* Enable USART1 Receive interrupt. */
+ USART_CR1(USART1) |= USART_CR1_RXNEIE;
+
+ /* Finally enable the USART. */
+ usart_enable(USART1);
+}
+
+void gpio_setup(void)
+{
+ gpio_set(GPIOC, GPIO12);
+
+ /* Setup GPIO6 and 7 (in GPIO port A) for led use. */
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
+}
+
+void usart1_isr(void)
+{
+ static u8 data = 'A';
+
+ /* Check if we were called because of RXNE. */
+ if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
+
+ /* Indicate that we got data. */
+ gpio_toggle(GPIOC, GPIO12);
+
+ /* Retrieve the data from the peripheral. */
+ data = usart_recv(USART1);
+
+ /* Enable transmit interrupt so it sends back the data. */
+ USART_CR1(USART1) |= USART_CR1_TXEIE;
+ }
+
+ /* Check if we were called because of TXE. */
+ if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_TXE) != 0)) {
+
+ /* Indicate that we are sending out data. */
+ //gpio_toggle(GPIOA, GPIO7);
+
+ /* Put data into the transmit register. */
+ usart_send(USART1, data);
+
+ /* Disable the TXE interrupt as we don't need it anymore. */
+ USART_CR1(USART1) &= ~USART_CR1_TXEIE;
+ }
+}
+
+int main(void)
+{
+ clock_setup();
+ gpio_setup();
+ usart_setup();
+
+ /* Wait forever and do nothing. */
+ while (1)
+ __asm__("nop");
+
+ return 0;
+}
diff --git a/examples/stm32/stm32-h103/usart_irq/usart_irq.ld b/examples/stm32/stm32-h103/usart_irq/usart_irq.ld
new file mode 100644
index 0000000..7899773
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq/usart_irq.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for Open-BLDC (STM32F103CBT6, 128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32.ld
+
diff --git a/examples/stm32/stm32-h103/usart_irq_printf/Makefile b/examples/stm32/stm32-h103/usart_irq_printf/Makefile
new file mode 100644
index 0000000..2917a7d
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq_printf/Makefile
@@ -0,0 +1,23 @@
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+BINARY = usart_irq_printf
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.c b/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.c
new file mode 100644
index 0000000..42a7472
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.c
@@ -0,0 +1,272 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
+ * 2011 Piotr Esden-Tempski <piotr@esden.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
+#include <libopencm3/stm32/nvic.h>
+#include <libopencm3/stm32/systick.h>
+
+#include <stdio.h>
+#include <errno.h>
+
+/******************************************************************************
+ * Simple ringbuffer implementation from open-bldc's libgovernor that
+ * you can find at:
+ * https://github.com/open-bldc/open-bldc/tree/master/source/libgovernor
+ ******************************************************************************/
+
+typedef s32 ring_size_t;
+
+struct ring {
+ u8 *data;
+ ring_size_t size;
+ u32 begin;
+ u32 end;
+};
+
+#define RING_SIZE(RING) ((RING)->size - 1)
+#define RING_DATA(RING) (RING)->data
+#define RING_EMPTY(RING) ((RING)->begin == (RING)->end)
+
+void ring_init(struct ring *ring, u8 * buf, ring_size_t size)
+{
+ ring->data = buf;
+ ring->size = size;
+ ring->begin = 0;
+ ring->end = 0;
+}
+
+s32 ring_write_ch(struct ring *ring, u8 ch)
+{
+ if (((ring->end + 1) % ring->size) != ring->begin) {
+ ring->data[ring->end++] = ch;
+ ring->end %= ring->size;
+ return (u32) ch;
+ }
+
+ return -1;
+}
+
+s32 ring_write(struct ring * ring, u8 * data, ring_size_t size)
+{
+ s32 i;
+
+ for (i = 0; i < size; i++) {
+ if (ring_write_ch(ring, data[i]) < 0) {
+ return -i;
+ }
+ }
+
+ return i;
+}
+
+s32 ring_read_ch(struct ring * ring, u8 * ch)
+{
+ s32 ret = -1;
+
+ if (ring->begin != ring->end) {
+ ret = ring->data[ring->begin++];
+ ring->begin %= ring->size;
+ if (ch)
+ *ch = ret;
+ }
+
+ return ret;
+}
+
+s32 ring_read(struct ring * ring, u8 * data, ring_size_t size)
+{
+ s32 i;
+
+ for (i = 0; i < size; i++) {
+ if (ring_read_ch(ring, data + i) < 0) {
+ return i;
+ }
+ }
+
+ return -i;
+}
+
+/******************************************************************************
+ * The example implementation
+ ******************************************************************************/
+
+#define BUFFER_SIZE 1024
+
+struct ring output_ring;
+u8 output_ring_buffer[BUFFER_SIZE];
+
+void clock_setup(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+
+ /* Enable GPIOA clock (for LED GPIOs). */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
+
+ /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
+ RCC_APB2ENR_AFIOEN |
+ RCC_APB2ENR_USART1EN);
+}
+
+void usart_setup(void)
+{
+
+ /* Initialize output ring buffer */
+ ring_init(&output_ring, output_ring_buffer, BUFFER_SIZE);
+
+ /* Enable the USART1 interrupt. */
+ nvic_enable_irq(NVIC_USART1_IRQ);
+
+ /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+
+ /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
+ gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
+
+ /* Setup UART parameters. */
+ usart_set_baudrate(USART1, 230400);
+ 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);
+
+ /* Enable USART1 Receive interrupt. */
+ USART_CR1(USART1) |= USART_CR1_RXNEIE;
+
+ /* Finally enable the USART. */
+ usart_enable(USART1);
+}
+
+void gpio_setup(void)
+{
+ gpio_set(GPIOC, GPIO12);
+
+ /* Setup GPIO6 and 7 (in GPIO port A) for led use. */
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
+}
+
+void usart1_isr(void)
+{
+ /* Check if we were called because of RXNE. */
+ if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
+
+ /* Indicate that we got data. */
+ gpio_toggle(GPIOC, GPIO12);
+
+ /* Retrieve the data from the peripheral. */
+ ring_write_ch(&output_ring, usart_recv(USART1));
+
+ /* Enable transmit interrupt so it sends back the data. */
+ USART_CR1(USART1) |= USART_CR1_TXEIE;
+ }
+
+ /* Check if we were called because of TXE. */
+ if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&
+ ((USART_SR(USART1) & USART_SR_TXE) != 0)) {
+
+ s32 data;
+
+ data = ring_read_ch(&output_ring, NULL);
+
+ if (data == -1) {
+ /* Disable the TXE interrupt as we don't need it anymore. */
+ USART_CR1(USART1) &= ~USART_CR1_TXEIE;
+ } else {
+ /* Put data into the transmit register. */
+ usart_send(USART1, data);
+ }
+ }
+}
+
+int _write (int file, char *ptr, int len)
+{
+ int ret;
+
+ if (file == 1) {
+ ret = ring_write(&output_ring, (u8 *)ptr, len);
+
+ if (ret < 0) ret = -ret;
+
+ USART_CR1(USART1) |= USART_CR1_TXEIE;
+
+ return ret;
+ }
+
+ errno = EIO;
+ return -1;
+}
+
+void systick_setup(void) {
+
+ /* 72MHz / 8 => 9000000 counts per second */
+ systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
+
+ /* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */
+ systick_set_reload(9000);
+
+ systick_interrupt_enable();
+
+ /* Start counting. */
+ systick_counter_enable();
+
+}
+
+void sys_tick_handler(void)
+{
+ static int counter = 0;
+ static float fcounter = 0.0;
+ static double dcounter = 0.0;
+
+ static u32 temp32 = 0;
+
+ temp32++;
+
+ /* We call this handler every 1ms so we are sending hello world every 10ms / 100Hz. */
+ if (temp32 == 10) {
+ printf("Hello World! %i %f %f\r\n", counter, fcounter, dcounter);
+ counter++;
+ fcounter+=0.01;
+ dcounter+=0.01;
+
+ temp32 = 0;
+ }
+}
+
+int main(void)
+{
+
+ clock_setup();
+ gpio_setup();
+ usart_setup();
+ systick_setup();
+
+ while (1) {
+ __asm__("nop");
+ }
+
+ return 0;
+}
diff --git a/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.ld b/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.ld
new file mode 100644
index 0000000..7899773
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_irq_printf/usart_irq_printf.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for Open-BLDC (STM32F103CBT6, 128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32.ld
+
diff --git a/examples/stm32/stm32-h103/usart_printf/Makefile b/examples/stm32/stm32-h103/usart_printf/Makefile
new file mode 100644
index 0000000..fb26208
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_printf/Makefile
@@ -0,0 +1,23 @@
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+
+BINARY = usart_printf
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/stm32-h103/usart_printf/usart_printf.c b/examples/stm32/stm32-h103/usart_printf/usart_printf.c
new file mode 100644
index 0000000..26fcee6
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_printf/usart_printf.c
@@ -0,0 +1,108 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
+ * 2011 Piotr Esden-Tempski <piotr@esden.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
+#include <libopencm3/stm32/nvic.h>
+
+#include <stdio.h>
+#include <errno.h>
+
+void clock_setup(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+
+ /* Enable GPIOA clock (for LED GPIOs). */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
+
+ /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
+ RCC_APB2ENR_AFIOEN |
+ RCC_APB2ENR_USART1EN);
+}
+
+void usart_setup(void)
+{
+ /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+
+ /* Setup UART parameters. */
+ usart_set_baudrate(USART1, 230400);
+ 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);
+
+ /* Finally enable the USART. */
+ usart_enable(USART1);
+}
+
+void gpio_setup(void)
+{
+ gpio_set(GPIOC, GPIO12);
+
+ /* Setup GPIO6 and 7 (in GPIO port A) for led use. */
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
+}
+
+int _write (int file, char *ptr, int len)
+{
+ int i;
+
+ if (file == 1) {
+ for (i = 0; i < len; i++){
+ usart_send_blocking(USART1, ptr[i]);
+ }
+
+ return i;
+ }
+
+ errno = EIO;
+ return -1;
+}
+
+int main(void)
+{
+ int counter = 0;
+ float fcounter = 0.0;
+ double dcounter = 0.0;
+
+ clock_setup();
+ gpio_setup();
+ usart_setup();
+
+ /*
+ * Write Hello World an integer, float and double all over
+ * again while incrementing the numbers.
+ */
+ while (1) {
+ gpio_toggle(GPIOC, GPIO12);
+ printf("Hello World! %i %f %f\r\n", counter, fcounter, dcounter);
+ counter++;
+ fcounter+=0.01;
+ dcounter+=0.01;
+ }
+
+ return 0;
+}
diff --git a/examples/stm32/stm32-h103/usart_printf/usart_printf.ld b/examples/stm32/stm32-h103/usart_printf/usart_printf.ld
new file mode 100644
index 0000000..7899773
--- /dev/null
+++ b/examples/stm32/stm32-h103/usart_printf/usart_printf.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Linker script for Open-BLDC (STM32F103CBT6, 128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script. */
+INCLUDE libopencm3_stm32.ld
+