Initial commit.
This commit is contained in:
158
examples/stm32/f4/Makefile.include
Normal file
158
examples/stm32/f4/Makefile.include
Normal file
@@ -0,0 +1,158 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
## Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
|
||||
## Copyright (C) 2011 Fergus Noble <fergusnoble@gmail.com>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
PREFIX ?= arm-none-eabi
|
||||
# PREFIX ?= arm-elf
|
||||
CC = $(PREFIX)-gcc
|
||||
LD = $(PREFIX)-gcc
|
||||
OBJCOPY = $(PREFIX)-objcopy
|
||||
OBJDUMP = $(PREFIX)-objdump
|
||||
GDB = $(PREFIX)-gdb
|
||||
FLASH = $(shell which st-flash)
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../../..
|
||||
ifeq ($(wildcard ../../../../../lib/libopencm3_stm32f4.a),)
|
||||
ifneq ($(strip $(shell which $(CC))),)
|
||||
TOOLCHAIN_DIR := $(shell dirname `which $(CC)`)/../$(PREFIX)
|
||||
endif
|
||||
else
|
||||
ifeq ($(V),1)
|
||||
$(info We seem to be building the example in the source directory. Using local library!)
|
||||
endif
|
||||
endif
|
||||
|
||||
CFLAGS += -Os -g \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include \
|
||||
-fno-common -mcpu=cortex-m4 -mthumb \
|
||||
-mfloat-abi=hard -mfpu=fpv4-sp-d16 -MD -DSTM32F4
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += --static -lc -lnosys -L$(TOOLCHAIN_DIR)/lib \
|
||||
-L$(TOOLCHAIN_DIR)/lib/stm32/f4 \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \
|
||||
-mthumb -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= flossjtag
|
||||
OOCD_BOARD ?= olimex_stm32_h103
|
||||
# Black magic probe specific variables
|
||||
# Set the BMP_PORT to a serial port and then BMP is used for flashing
|
||||
BMP_PORT ?=
|
||||
|
||||
# Be silent per default, but 'make V=1' will show all compiler calls.
|
||||
ifneq ($(V),1)
|
||||
Q := @
|
||||
NULL := 2>/dev/null
|
||||
else
|
||||
LDFLAGS += -Wl,--print-gc-sections
|
||||
endif
|
||||
|
||||
.SUFFIXES: .elf .bin .hex .srec .list .images
|
||||
.SECONDEXPANSION:
|
||||
.SECONDARY:
|
||||
|
||||
all: images
|
||||
|
||||
images: $(BINARY).images
|
||||
flash: $(BINARY).flash
|
||||
|
||||
%.images: %.bin %.hex %.srec %.list
|
||||
@#echo "*** $* images generated ***"
|
||||
|
||||
%.bin: %.elf
|
||||
@#printf " OBJCOPY $(*).bin\n"
|
||||
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
|
||||
|
||||
%.hex: %.elf
|
||||
@#printf " OBJCOPY $(*).hex\n"
|
||||
$(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex
|
||||
|
||||
%.srec: %.elf
|
||||
@#printf " OBJCOPY $(*).srec\n"
|
||||
$(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec
|
||||
|
||||
%.list: %.elf
|
||||
@#printf " OBJDUMP $(*).list\n"
|
||||
$(Q)$(OBJDUMP) -S $(*).elf > $(*).list
|
||||
|
||||
%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f4.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f4 $(LDFLAGS)
|
||||
|
||||
%.o: %.c Makefile
|
||||
@#printf " CC $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(CC) $(CFLAGS) -o $@ -c $<
|
||||
|
||||
clean:
|
||||
$(Q)rm -f *.o
|
||||
$(Q)rm -f *.d
|
||||
$(Q)rm -f *.elf
|
||||
$(Q)rm -f *.bin
|
||||
$(Q)rm -f *.hex
|
||||
$(Q)rm -f *.srec
|
||||
$(Q)rm -f *.list
|
||||
|
||||
%.stlink-flash: %.bin
|
||||
@printf " FLASH $<\n"
|
||||
$(Q)$(FLASH) write $(*).bin 0x8000000
|
||||
|
||||
|
||||
ifeq ($(BMP_PORT),)
|
||||
ifeq ($(OOCD_SERIAL),)
|
||||
%.flash: %.hex
|
||||
@printf " FLASH $<\n"
|
||||
@# IMPORTANT: Don't use "resume", only "reset" will work correctly!
|
||||
$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
|
||||
-f board/$(OOCD_BOARD).cfg \
|
||||
-c "init" -c "reset init" \
|
||||
-c "stm32x mass_erase 0" \
|
||||
-c "flash write_image $(*).hex" \
|
||||
-c "reset" \
|
||||
-c "shutdown" $(NULL)
|
||||
else
|
||||
%.flash: %.hex
|
||||
@printf " FLASH $<\n"
|
||||
@# IMPORTANT: Don't use "resume", only "reset" will work correctly!
|
||||
$(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
|
||||
-f board/$(OOCD_BOARD).cfg \
|
||||
-c "ft2232_serial $(OOCD_SERIAL)" \
|
||||
-c "init" -c "reset init" \
|
||||
-c "stm32x mass_erase 0" \
|
||||
-c "flash write_image $(*).hex" \
|
||||
-c "reset" \
|
||||
-c "shutdown" $(NULL)
|
||||
endif
|
||||
else
|
||||
%.flash: %.elf
|
||||
@echo " GDB $(*).elf (flash)"
|
||||
$(Q)$(GDB) --batch \
|
||||
-ex 'target extended-remote $(BMP_PORT)' \
|
||||
-x $(TOOLCHAIN_DIR)/scripts/black_magic_probe_flash.scr \
|
||||
$(*).elf
|
||||
endif
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
||||
25
examples/stm32/f4/stm32f4-discovery/button/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/button/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = button
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
8
examples/stm32/f4/stm32f4-discovery/button/README
Normal file
8
examples/stm32/f4/stm32f4-discovery/button/README
Normal file
@@ -0,0 +1,8 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example blinks the green LED on the ST STM32F4DISCOVERY eval board.
|
||||
|
||||
When you press the 'USER' button, the blinking is slower.
|
||||
|
||||
76
examples/stm32/f4/stm32f4-discovery/button/button.c
Normal file
76
examples/stm32/f4/stm32f4-discovery/button/button.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
|
||||
* Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
|
||||
u16 exti_line_state;
|
||||
|
||||
/* Set STM32 to 168 MHz. */
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIOD clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
|
||||
/* Set GPIO12 (in GPIO port D) to 'output push-pull'. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,
|
||||
GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
|
||||
}
|
||||
|
||||
static void button_setup(void)
|
||||
{
|
||||
/* Enable GPIOA clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
|
||||
/* Set GPIO0 (in GPIO port A) to 'input open-drain'. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
clock_setup();
|
||||
button_setup();
|
||||
gpio_setup();
|
||||
|
||||
/* Blink the LED (PD12) on the board. */
|
||||
while (1) {
|
||||
gpio_toggle(GPIOD, GPIO12);
|
||||
|
||||
/* Upon button press, blink more slowly. */
|
||||
exti_line_state = GPIOA_IDR;
|
||||
if ((exti_line_state & (1 << 0)) != 0) {
|
||||
for (i = 0; i < 3000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
for (i = 0; i < 3000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/fancyblink/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = fancyblink
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
9
examples/stm32/f4/stm32f4-discovery/fancyblink/README
Normal file
9
examples/stm32/f4/stm32f4-discovery/fancyblink/README
Normal file
@@ -0,0 +1,9 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the ST STM32F4DISCOVERY eval board. It should blink
|
||||
the LEDs on the board.
|
||||
|
||||
60
examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c
Normal file
60
examples/stm32/f4/stm32f4-discovery/fancyblink/fancyblink.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Damjan Marion <damjan.marion@gmail.com>
|
||||
* Copyright (C) 2011 Mark Panajotovic <marko@electrontube.org>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
|
||||
/* Set STM32 to 168 MHz. */
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
|
||||
|
||||
/* Enable GPIOD clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO12-15 (in GPIO port D) to 'output push-pull'. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,
|
||||
GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
|
||||
/* Set two LEDs for wigwag effect when toggling. */
|
||||
gpio_set(GPIOD, GPIO12 | GPIO14);
|
||||
|
||||
/* Blink the LEDs (PD12, PD13, PD14 and PD15) on the board. */
|
||||
while (1) {
|
||||
/* Toggle LEDs. */
|
||||
gpio_toggle(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);
|
||||
for (i = 0; i < 6000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/mandelbrot/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = mandel
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
12
examples/stm32/f4/stm32f4-discovery/mandelbrot/README
Normal file
12
examples/stm32/f4/stm32f4-discovery/mandelbrot/README
Normal file
@@ -0,0 +1,12 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program demonstrates the floating point coprocessor usage on
|
||||
the ST STM32F4DISCOVERY eval board.
|
||||
|
||||
A mandelbrot fractal is calculated and sent as "ascii-art" image through
|
||||
the USART2.
|
||||
|
||||
The terminal settings for the receiving device/PC are 38400 8n1.
|
||||
|
||||
127
examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c
Normal file
127
examples/stm32/f4/stm32f4-discovery/mandelbrot/mandel.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
* Copyright (C) 2012 Daniel Serpell <daniel.serpell@gmail.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
/* Enable high-speed clock at 120MHz */
|
||||
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]);
|
||||
|
||||
/* Enable GPIOD clock for LED & USARTs. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
|
||||
/* Enable clocks for USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup USART2 parameters. */
|
||||
usart_set_baudrate(USART2, 38400);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO12 on GPIO port D for LED. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
|
||||
|
||||
/* Setup GPIO pins for USART2 transmit. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
|
||||
|
||||
/* Setup USART2 TX pin as alternate function. */
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
|
||||
}
|
||||
|
||||
/* Maximum number of iterations for the escape-time calculation */
|
||||
#define maxIter 32
|
||||
/* This array converts the iteration count to a character representation. */
|
||||
static char color[maxIter+1] = " .:++xxXXX%%%%%%################";
|
||||
|
||||
/* Main mandelbrot calculation */
|
||||
static int iterate(float px, float py)
|
||||
{
|
||||
int it=0;
|
||||
float x=0,y=0;
|
||||
while(it<maxIter)
|
||||
{
|
||||
float nx = x*x;
|
||||
float ny = y*y;
|
||||
if( (nx + ny) > 4 )
|
||||
return it;
|
||||
// Zn+1 = Zn^2 + P
|
||||
y = 2*x*y + py;
|
||||
x = nx - ny + px;
|
||||
it++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mandel(float cX, float cY, float scale)
|
||||
{
|
||||
int x,y;
|
||||
for(x=-60;x<60;x++)
|
||||
{
|
||||
for(y=-50;y<50;y++)
|
||||
{
|
||||
int i = iterate(cX+x*scale, cY+y*scale);
|
||||
usart_send_blocking(USART2, color[i]);
|
||||
}
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
float scale = 0.25f, centerX = -0.5f, centerY = 0.0f;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
while (1) {
|
||||
/* Blink the LED (PD12) on the board with each fractal drawn. */
|
||||
gpio_toggle(GPIOD, GPIO12); /* LED on/off */
|
||||
mandel(centerX,centerY,scale); /* draw mandelbrot */
|
||||
|
||||
/* Change scale and center */
|
||||
centerX += 0.175f * scale;
|
||||
centerY += 0.522f * scale;
|
||||
scale *= 0.875f;
|
||||
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/miniblink/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/miniblink/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = miniblink
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
9
examples/stm32/f4/stm32f4-discovery/miniblink/README
Normal file
9
examples/stm32/f4/stm32f4-discovery/miniblink/README
Normal file
@@ -0,0 +1,9 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the ST STM32F4DISCOVERY eval board. It should blink
|
||||
the blue LED on the board.
|
||||
|
||||
71
examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c
Normal file
71
examples/stm32/f4/stm32f4-discovery/miniblink/miniblink.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIOD clock. */
|
||||
/* Manually: */
|
||||
// RCC_AHB1ENR |= RCC_AHB1ENR_IOPDEN;
|
||||
/* Using API functions: */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
|
||||
/* Set GPIO12 (in GPIO port D) to 'output push-pull'. */
|
||||
/* Manually: */
|
||||
// GPIOD_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((8 - 8) * 4) + 2));
|
||||
// GPIOD_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((8 - 8) * 4));
|
||||
/* Using API functions: */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Blink the LED (PC8) on the board. */
|
||||
while (1) {
|
||||
/* Manually: */
|
||||
// GPIOD_BSRR = GPIO12; /* LED off */
|
||||
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
// GPIOD_BRR = GPIO12; /* LED on */
|
||||
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
|
||||
/* Using API functions gpio_set()/gpio_clear(): */
|
||||
// gpio_set(GPIOD, GPIO12); /* LED off */
|
||||
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
// gpio_clear(GPIOD, GPIO12); /* LED on */
|
||||
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
|
||||
/* Using API function gpio_toggle(): */
|
||||
gpio_toggle(GPIOD, GPIO12); /* LED on/off */
|
||||
for (i = 0; i < 1000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/random/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/random/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = random
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
5
examples/stm32/f4/stm32f4-discovery/random/README
Normal file
5
examples/stm32/f4/stm32f4-discovery/random/README
Normal file
@@ -0,0 +1,5 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example randomly blinks the green LED on the ST STM32F4DISCOVERY eval board.
|
||||
88
examples/stm32/f4/stm32f4-discovery/random/random.c
Normal file
88
examples/stm32/f4/stm32f4-discovery/random/random.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <libopencm3/cm3/common.h>
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/stm32/f4/rng.h>
|
||||
|
||||
static void rcc_setup(void)
|
||||
{
|
||||
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]);
|
||||
|
||||
/* Enable GPIOD clock for onboard leds. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
|
||||
/* Enable rng clock */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_RNGEN);
|
||||
}
|
||||
|
||||
static void rng_setup(void)
|
||||
{
|
||||
/* Enable interupt */
|
||||
/* Set the IE bit in the RNG_CR register. */
|
||||
RNG_CR |= RNG_CR_IE;
|
||||
/* Enable the random number generation by setting the RNGEN bit in the RNG_CR
|
||||
register. This activates the analog part, the RNG_LFSR and the error detector.
|
||||
*/
|
||||
RNG_CR |= RNG_CR_RNGEN;
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Setup onboard led */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13);
|
||||
}
|
||||
|
||||
/* Tried to folow the guidelines in the stm32f4 user manual.*/
|
||||
static u32 random_int(void)
|
||||
{
|
||||
static u32 last_value=0;
|
||||
static u32 new_value=0;
|
||||
u32 error_bits = 0;
|
||||
error_bits = RNG_SR_SEIS | RNG_SR_CEIS;
|
||||
while (new_value==last_value) {
|
||||
/* Check for error flags and if data is ready. */
|
||||
if ( ((RNG_SR & error_bits) == 0) && ( (RNG_SR & RNG_SR_DRDY) == 1 ) )
|
||||
new_value=RNG_DR;
|
||||
}
|
||||
last_value=new_value;
|
||||
return new_value;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i,j;
|
||||
rcc_setup();
|
||||
gpio_setup();
|
||||
rng_setup();
|
||||
while(1){
|
||||
u32 rnd;
|
||||
rnd = random_int();
|
||||
for(i=0;i!=32;++i){
|
||||
if ( (rnd & (1 << i))!=0 )
|
||||
gpio_set(GPIOD, GPIO12);
|
||||
else
|
||||
gpio_clear(GPIOD, GPIO12);
|
||||
/* Delay */
|
||||
for(j=0;j!=5000000;++j)
|
||||
__asm__("nop");
|
||||
}
|
||||
}
|
||||
}
|
||||
32
examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld
Normal file
32
examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Linker script for ST STM32F4DISCOVERY (STM32F407VG, 1024K flash, 128K RAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_stm32f4.ld
|
||||
|
||||
25
examples/stm32/f4/stm32f4-discovery/usart/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/usart/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = usart
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
11
examples/stm32/f4/stm32f4-discovery/usart/README
Normal file
11
examples/stm32/f4/stm32f4-discovery/usart/README
Normal file
@@ -0,0 +1,11 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program sends some characters on USART2 on the
|
||||
ST STM32F4DISCOVERY eval board.
|
||||
|
||||
The terminal settings for the receiving device/PC are 38400 8n1.
|
||||
|
||||
The sending is done in a blocking way in the code.
|
||||
|
||||
83
examples/stm32/f4/stm32f4-discovery/usart/usart.c
Normal file
83
examples/stm32/f4/stm32f4-discovery/usart/usart.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
/* Enable GPIOD clock for LED & USARTs. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
|
||||
/* Enable clocks for USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup USART2 parameters. */
|
||||
usart_set_baudrate(USART2, 38400);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO12 on GPIO port D for LED. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
|
||||
|
||||
/* Setup GPIO pins for USART2 transmit. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
|
||||
|
||||
/* Setup USART2 TX pin as alternate function. */
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i, j = 0, c = 0;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
/* Blink the LED (PD12) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
gpio_toggle(GPIOD, GPIO12); /* LED on/off */
|
||||
usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */
|
||||
c = (c == 9) ? 0 : c + 1; /* Increment c. */
|
||||
if ((j++ % 80) == 0) { /* Newline after line full. */
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
}
|
||||
for (i = 0; i < 3000000; i++) /* Wait a bit. */
|
||||
__asm__("NOP");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/usart_irq/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = usart_irq
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
14
examples/stm32/f4/stm32f4-discovery/usart_irq/README
Normal file
14
examples/stm32/f4/stm32f4-discovery/usart_irq/README
Normal file
@@ -0,0 +1,14 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program echoes data sent in on USART2 on the
|
||||
ST STM32F4DISCOVERY eval board. Uses interrupts for that purpose.
|
||||
|
||||
Connect your TTL RX device line to PA2 on the discovery board.
|
||||
Connect your TTL TX device line to PA3 on the discovery board.
|
||||
|
||||
The terminal settings for the receiving device/PC are 38400 8n1.
|
||||
|
||||
The sending is done in a blocking way in the code.
|
||||
|
||||
115
examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c
Normal file
115
examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
* Copyright (C) 2013 Piotr Esden-Tempski <piotr@esden.net>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
/* Enable GPIOD clock for LED & USARTs. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
|
||||
/* Enable clocks for USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable the USART2 interrupt. */
|
||||
nvic_enable_irq(NVIC_USART2_IRQ);
|
||||
|
||||
/* Setup GPIO pins for USART2 transmit. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
|
||||
|
||||
/* Setup GPIO pins for USART2 receive. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
|
||||
gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3);
|
||||
|
||||
/* Setup USART2 TX and RX pin as alternate function. */
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO3);
|
||||
|
||||
/* Setup USART2 parameters. */
|
||||
usart_set_baudrate(USART2, 38400);
|
||||
usart_set_databits(USART2, 8);
|
||||
usart_set_stopbits(USART2, USART_STOPBITS_1);
|
||||
usart_set_mode(USART2, USART_MODE_TX_RX);
|
||||
usart_set_parity(USART2, USART_PARITY_NONE);
|
||||
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
|
||||
|
||||
/* Enable USART2 Receive interrupt. */
|
||||
usart_enable_rx_interrupt(USART2);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO12 on GPIO port D for LED. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
while (1) {
|
||||
__asm__("NOP");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usart2_isr(void)
|
||||
{
|
||||
static u8 data = 'A';
|
||||
|
||||
/* Check if we were called because of RXNE. */
|
||||
if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) &&
|
||||
((USART_SR(USART2) & USART_SR_RXNE) != 0)) {
|
||||
|
||||
/* Indicate that we got data. */
|
||||
gpio_toggle(GPIOD, GPIO12);
|
||||
|
||||
/* Retrieve the data from the peripheral. */
|
||||
data = usart_recv(USART2);
|
||||
|
||||
/* Enable transmit interrupt so it sends back the data. */
|
||||
usart_enable_tx_interrupt(USART2);
|
||||
}
|
||||
|
||||
/* Check if we were called because of TXE. */
|
||||
if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) &&
|
||||
((USART_SR(USART2) & USART_SR_TXE) != 0)) {
|
||||
|
||||
/* Put data into the transmit register. */
|
||||
usart_send(USART2, data);
|
||||
|
||||
/* Disable the TXE interrupt as we don't need it anymore. */
|
||||
usart_disable_tx_interrupt(USART2);
|
||||
}
|
||||
}
|
||||
93
examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~
Normal file
93
examples/stm32/f4/stm32f4-discovery/usart_irq/usart_irq.c~
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
void clock_setup(void)
|
||||
{
|
||||
/* Enable GPIOD clock for LED & USARTs. */
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
|
||||
/* Enable clocks for USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
}
|
||||
|
||||
void usart_setup(void)
|
||||
{
|
||||
/* Setup USART2 parameters. */
|
||||
usart_set_baudrate(USART2, 38400);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
void gpio_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO12 on GPIO port D for LED. */
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
|
||||
|
||||
/* Setup GPIO pins for USART2 transmit. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
|
||||
|
||||
|
||||
/* Setup GPIO pins for USART2 receive. */
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_OTYPE_OD, GPIO3);
|
||||
|
||||
/* Setup USART2 TX and RX pin as alternate function. */
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
|
||||
gpio_set_af(GPIOA, GPIO_AF7, GPIO3);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i, j = 0, c = 0;
|
||||
u16 in_ch = 0;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
/* Blink the LED (PD12) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
gpio_toggle(GPIOD, GPIO12); /* LED on/off */
|
||||
usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */
|
||||
c = (c == 9) ? 0 : c + 1; /* Increment c. */
|
||||
if ((j++ % 80) == 0) { /* Newline after line full. */
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
}
|
||||
if ((USART_SR(usart) & USART_SR_RXNE) != 0) {
|
||||
in_ch = usart_recv(USART2);
|
||||
usart_send_blocking(USART2, in_ch);
|
||||
}
|
||||
for (i = 0; i < 3000000; i++) /* Wait a bit. */
|
||||
__asm__("NOP");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile
Normal file
25
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU Lesser General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This library 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 Lesser General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU Lesser General Public License
|
||||
## along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
BINARY = cdcacm
|
||||
|
||||
LDSCRIPT = ../stm32f4-discovery.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
||||
7
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README
Normal file
7
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/README
Normal file
@@ -0,0 +1,7 @@
|
||||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example implements a USB CDC-ACM device (aka Virtual Serial Port)
|
||||
to demonstrate the use of the USB device stack.
|
||||
|
||||
240
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c
Normal file
240
examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <libopencm3/stm32/f4/rcc.h>
|
||||
#include <libopencm3/stm32/f4/gpio.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include <libopencm3/usb/cdc.h>
|
||||
#include <libopencm3/cm3/scb.h>
|
||||
|
||||
static const struct usb_device_descriptor dev = {
|
||||
.bLength = USB_DT_DEVICE_SIZE,
|
||||
.bDescriptorType = USB_DT_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = USB_CLASS_CDC,
|
||||
.bDeviceSubClass = 0,
|
||||
.bDeviceProtocol = 0,
|
||||
.bMaxPacketSize0 = 64,
|
||||
.idVendor = 0x0483,
|
||||
.idProduct = 0x5740,
|
||||
.bcdDevice = 0x0200,
|
||||
.iManufacturer = 1,
|
||||
.iProduct = 2,
|
||||
.iSerialNumber = 3,
|
||||
.bNumConfigurations = 1,
|
||||
};
|
||||
|
||||
/*
|
||||
* This notification endpoint isn't implemented. According to CDC spec it's
|
||||
* optional, but its absence causes a NULL pointer dereference in the
|
||||
* Linux cdc_acm driver.
|
||||
*/
|
||||
static const struct usb_endpoint_descriptor comm_endp[] = {{
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 0x83,
|
||||
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
|
||||
.wMaxPacketSize = 16,
|
||||
.bInterval = 255,
|
||||
}};
|
||||
|
||||
static const struct usb_endpoint_descriptor data_endp[] = {{
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 0x01,
|
||||
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 1,
|
||||
}, {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 0x82,
|
||||
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
|
||||
.wMaxPacketSize = 64,
|
||||
.bInterval = 1,
|
||||
}};
|
||||
|
||||
static const struct {
|
||||
struct usb_cdc_header_descriptor header;
|
||||
struct usb_cdc_call_management_descriptor call_mgmt;
|
||||
struct usb_cdc_acm_descriptor acm;
|
||||
struct usb_cdc_union_descriptor cdc_union;
|
||||
} __attribute__((packed)) cdcacm_functional_descriptors = {
|
||||
.header = {
|
||||
.bFunctionLength = sizeof(struct usb_cdc_header_descriptor),
|
||||
.bDescriptorType = CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_CDC_TYPE_HEADER,
|
||||
.bcdCDC = 0x0110,
|
||||
},
|
||||
.call_mgmt = {
|
||||
.bFunctionLength =
|
||||
sizeof(struct usb_cdc_call_management_descriptor),
|
||||
.bDescriptorType = CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT,
|
||||
.bmCapabilities = 0,
|
||||
.bDataInterface = 1,
|
||||
},
|
||||
.acm = {
|
||||
.bFunctionLength = sizeof(struct usb_cdc_acm_descriptor),
|
||||
.bDescriptorType = CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_CDC_TYPE_ACM,
|
||||
.bmCapabilities = 0,
|
||||
},
|
||||
.cdc_union = {
|
||||
.bFunctionLength = sizeof(struct usb_cdc_union_descriptor),
|
||||
.bDescriptorType = CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_CDC_TYPE_UNION,
|
||||
.bControlInterface = 0,
|
||||
.bSubordinateInterface0 = 1,
|
||||
}
|
||||
};
|
||||
|
||||
static const struct usb_interface_descriptor comm_iface[] = {{
|
||||
.bLength = USB_DT_INTERFACE_SIZE,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 1,
|
||||
.bInterfaceClass = USB_CLASS_CDC,
|
||||
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
|
||||
.bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
|
||||
.iInterface = 0,
|
||||
|
||||
.endpoint = comm_endp,
|
||||
|
||||
.extra = &cdcacm_functional_descriptors,
|
||||
.extralen = sizeof(cdcacm_functional_descriptors)
|
||||
}};
|
||||
|
||||
static const struct usb_interface_descriptor data_iface[] = {{
|
||||
.bLength = USB_DT_INTERFACE_SIZE,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 1,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 2,
|
||||
.bInterfaceClass = USB_CLASS_DATA,
|
||||
.bInterfaceSubClass = 0,
|
||||
.bInterfaceProtocol = 0,
|
||||
.iInterface = 0,
|
||||
|
||||
.endpoint = data_endp,
|
||||
}};
|
||||
|
||||
static const struct usb_interface ifaces[] = {{
|
||||
.num_altsetting = 1,
|
||||
.altsetting = comm_iface,
|
||||
}, {
|
||||
.num_altsetting = 1,
|
||||
.altsetting = data_iface,
|
||||
}};
|
||||
|
||||
static const struct usb_config_descriptor config = {
|
||||
.bLength = USB_DT_CONFIGURATION_SIZE,
|
||||
.bDescriptorType = USB_DT_CONFIGURATION,
|
||||
.wTotalLength = 0,
|
||||
.bNumInterfaces = 2,
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 0,
|
||||
.bmAttributes = 0x80,
|
||||
.bMaxPower = 0x32,
|
||||
|
||||
.interface = ifaces,
|
||||
};
|
||||
|
||||
static const char *usb_strings[] = {
|
||||
"Black Sphere Technologies",
|
||||
"CDC-ACM Demo",
|
||||
"DEMO",
|
||||
};
|
||||
|
||||
static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf,
|
||||
u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
|
||||
{
|
||||
(void)complete;
|
||||
(void)buf;
|
||||
(void)usbd_dev;
|
||||
|
||||
switch (req->bRequest) {
|
||||
case USB_CDC_REQ_SET_CONTROL_LINE_STATE: {
|
||||
/*
|
||||
* This Linux cdc_acm driver requires this to be implemented
|
||||
* even though it's optional in the CDC spec, and we don't
|
||||
* advertise it in the ACM functional descriptor.
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
case USB_CDC_REQ_SET_LINE_CODING:
|
||||
if (*len < sizeof(struct usb_cdc_line_coding))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void cdcacm_data_rx_cb(usbd_device *usbd_dev, u8 ep)
|
||||
{
|
||||
(void)ep;
|
||||
|
||||
char buf[64];
|
||||
int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64);
|
||||
|
||||
if (len) {
|
||||
while (usbd_ep_write_packet(usbd_dev, 0x82, buf, len) == 0)
|
||||
;
|
||||
}
|
||||
|
||||
gpio_toggle(GPIOC, GPIO5);
|
||||
}
|
||||
|
||||
static void cdcacm_set_config(usbd_device *usbd_dev, u16 wValue)
|
||||
{
|
||||
(void)wValue;
|
||||
|
||||
usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb);
|
||||
usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL);
|
||||
usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
|
||||
|
||||
usbd_register_control_callback(
|
||||
usbd_dev,
|
||||
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
|
||||
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
|
||||
cdcacm_control_request);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
usbd_device *usbd_dev;
|
||||
|
||||
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_120MHZ]);
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN);
|
||||
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,
|
||||
GPIO9 | GPIO11 | GPIO12);
|
||||
gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);
|
||||
|
||||
usbd_dev = usbd_init(&otgfs_usb_driver, &dev, &config, usb_strings, 3);
|
||||
usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);
|
||||
|
||||
while (1)
|
||||
usbd_poll(usbd_dev);
|
||||
}
|
||||
Reference in New Issue
Block a user