Removed examples, they are moved into libopencm3-examples repository.
This commit is contained in:
parent
67903bfbfe
commit
3e2be1d4a4
12
Makefile
12
Makefile
|
@ -43,7 +43,7 @@ endif
|
|||
|
||||
all: build
|
||||
|
||||
build: lib examples
|
||||
build: lib
|
||||
|
||||
generatedheaders:
|
||||
@printf " UPDATING HEADERS\n"
|
||||
|
@ -65,14 +65,6 @@ $(LIB_DIRS): generatedheaders
|
|||
lib: $(LIB_DIRS)
|
||||
$(Q)true
|
||||
|
||||
EXAMPLE_DIRS:=$(sort $(dir $(wildcard $(addsuffix /*/*/Makefile,$(addprefix examples/,$(TARGETS))))))
|
||||
$(EXAMPLE_DIRS): lib
|
||||
@printf " BUILD $@\n";
|
||||
$(Q)$(MAKE) --directory=$@
|
||||
|
||||
examples: $(EXAMPLE_DIRS)
|
||||
$(Q)true
|
||||
|
||||
install: lib
|
||||
@printf " INSTALL headers\n"
|
||||
$(Q)$(INSTALL) -d $(INCDIR)/libopencm3
|
||||
|
@ -104,5 +96,5 @@ clean: cleanheaders
|
|||
@printf " CLEAN doxygen\n"
|
||||
$(Q)$(MAKE) -C doc clean
|
||||
|
||||
.PHONY: build lib $(LIB_DIRS) examples $(EXAMPLE_DIRS) install doc clean generatedheaders cleanheaders
|
||||
.PHONY: build lib $(LIB_DIRS) install doc clean generatedheaders cleanheaders
|
||||
|
||||
|
|
31
README
31
README
|
@ -66,34 +66,11 @@ For a more verbose build you can use
|
|||
Example projects
|
||||
----------------
|
||||
|
||||
The library ships with a few small example projects which illustrate how
|
||||
individual subsystems of the microcontrollers can be configured and used with
|
||||
libopencm3. The makefiles are generally useable for your own projects with
|
||||
only minimal changes for the libopencm3 install path (See Installation)
|
||||
|
||||
For flashing the 'miniblink' example (after you built libopencm3 and the
|
||||
examples by typing 'make' at the top-level directory) onto the Olimex
|
||||
STM32-H103 eval board (ST STM32F1 series microcontroller), you can execute:
|
||||
|
||||
$ cd examples/stm32/f1/stm32-h103/miniblink
|
||||
$ make flash
|
||||
|
||||
The Makefiles of the examples are configured to use a certain OpenOCD
|
||||
flash programmer, you might need to change some of the variables in the
|
||||
Makefile if you use a different one.
|
||||
|
||||
You can also flash manually like this:
|
||||
|
||||
$ openocd -f interface/jtagkey-tiny.cfg -f target/stm32f1x.cfg
|
||||
$ telnet localhost 4444
|
||||
> reset halt
|
||||
> flash write_image erase foobar.hex
|
||||
> reset
|
||||
|
||||
Replace the "jtagkey-tiny.cfg" with whatever JTAG device you are using, and/or
|
||||
replace "stm32f1x.cfg" with your respective config file. Replace "foobar.hex"
|
||||
with the file name of the image you want to flash.
|
||||
The libopencm3 community has written and is maintaining a huge collection of
|
||||
examples, displaying the capabilities and uses of the library. You can find all
|
||||
of them in the libopencm3-examples repository:
|
||||
|
||||
https://github.com/libopencm3/libopencm3-examples
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
|
|
@ -1,137 +0,0 @@
|
|||
##
|
||||
## 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>
|
||||
##
|
||||
## 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
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../..
|
||||
ifeq ($(wildcard ../../../../lib/libopencm3_lm3s.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 += -O0 -g3 \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include -fno-common \
|
||||
-mcpu=cortex-m3 -mthumb -MD -DLM3S
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= flossjtag
|
||||
OOCD_BOARD ?= olimex_stm32_h103
|
||||
# FIXME
|
||||
|
||||
# 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_lm3s.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lm3s
|
||||
|
||||
%.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
|
||||
|
||||
# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later.
|
||||
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
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for Luminary Micro (now TI) Stellaris LM3S3748-EVB. */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 128K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lm3s.ld
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../lm3s3748-evb.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the LuminaryMicro LM3S3748-EVB.
|
||||
It should blink the STATUS LED on the board.
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2011 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 <libopencm3/lm3s/systemcontrol.h>
|
||||
#include <libopencm3/lm3s/gpio.h>
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
SYSTEMCONTROL_RCGC2 |= 0x20; /* Enable GPIOF in run mode. */
|
||||
|
||||
GPIO_DIR(GPIOF) |= (1 << 0); /* Configure PF0 as output. */
|
||||
GPIO_DEN(GPIOF) |= (1 << 0); /* Enable digital function on PF0. */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Blink STATUS LED (PF0) on the board. */
|
||||
while (1) {
|
||||
gpio_set(GPIOF, GPIO0);
|
||||
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
gpio_clear(GPIOF, GPIO0);
|
||||
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,143 +0,0 @@
|
|||
##
|
||||
## 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) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
|
||||
##
|
||||
## This library is free software: you can redistribute it and/or modify
|
||||
## it undebipr 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
|
||||
CC = $(PREFIX)-gcc
|
||||
LD = $(PREFIX)-gcc
|
||||
OBJCOPY = $(PREFIX)-objcopy
|
||||
OBJDUMP = $(PREFIX)-objdump
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../..
|
||||
ifeq ($(wildcard ../../../../lib/libopencm3_lm4f.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
|
||||
|
||||
ARCH_FLAGS = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
CFLAGS += -O0 -g3 \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include \
|
||||
-fno-common $(ARCH_FLAGS) -MD -DLM4F
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \
|
||||
-L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \
|
||||
$(ARCH_FLAGS)
|
||||
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
# Support for ICDI is not in any openocd release as of yet
|
||||
# A patch to add support for ICDI is under review
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= ti-icdi
|
||||
# No official board.cfg for the stellaris launchpad
|
||||
OOCD_BOARD ?= stellaris_launchpad
|
||||
|
||||
# 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_lm4f.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_lm4f $(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
|
||||
|
||||
# FIXME: Replace STM32 stuff with proper Stellaris support.
|
||||
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
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for TI Stellaris EX-LM4F120XL evaluation board. */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 256K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 32K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lm4f.ld
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@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/>.
|
||||
##
|
||||
|
||||
BINARY = miniblink
|
||||
|
||||
LDSCRIPT = ../ek-lm4f120xl.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example demonstrates the following:
|
||||
* Configuriong GPIO pins
|
||||
* Toggling GPIO pins
|
||||
* Setting up and using GPIO interrupts
|
||||
* Unlocking protected GPIO pins
|
||||
* Controlling the system clock
|
||||
* Changing the system clock on the fly
|
||||
|
||||
Flashes the Red, Green and Blue diodes on the board, in order. The system clock
|
||||
starts at 80MHz.
|
||||
Pressing SW2 toggles the system clock between 80MHz, 57MHz, 40MHz ,20MHz, and
|
||||
16MHz by changing the PLL divisor.
|
||||
Pressing SW1 bypasses the PLL completely, and runs off the raw 16MHz clock
|
||||
provided by the external crystal oscillator.
|
||||
The LEDs will toggle at different speeds, depending on the system clock. The
|
||||
system clock changes are handled within the interrupt service routine.
|
||||
|
||||
|
|
@ -1,217 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
|
||||
* Copyright (C) 2012-2013 Alexandru Gagniuc <mr.nuke.me@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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \addtogroup Examples
|
||||
*
|
||||
* Flashes the Red, Green and Blue diodes on the board, in order.
|
||||
*
|
||||
* RED controlled by PF1
|
||||
* Green controlled by PF3
|
||||
* Blue controlled by PF2
|
||||
*/
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/lm4f/systemcontrol.h>
|
||||
#include <libopencm3/lm4f/rcc.h>
|
||||
#include <libopencm3/lm4f/gpio.h>
|
||||
#include <libopencm3/lm4f/nvic.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* This is how the RGB LED is connected on the stellaris launchpad */
|
||||
#define RGB_PORT GPIOF
|
||||
enum {
|
||||
LED_R = GPIO1,
|
||||
LED_G = GPIO3,
|
||||
LED_B = GPIO2,
|
||||
};
|
||||
|
||||
/* This is how the user switches are connected to GPIOF */
|
||||
enum {
|
||||
USR_SW1 = GPIO4,
|
||||
USR_SW2 = GPIO0,
|
||||
};
|
||||
|
||||
/* The divisors we loop through when the user presses SW2 */
|
||||
enum {
|
||||
PLL_DIV_80MHZ = 5,
|
||||
PLL_DIV_57MHZ = 7,
|
||||
PLL_DIV_40MHZ = 10,
|
||||
PLL_DIV_20MHZ = 20,
|
||||
PLL_DIV_16MHZ = 25,
|
||||
};
|
||||
|
||||
static const u8 plldiv[] = {
|
||||
PLL_DIV_80MHZ,
|
||||
PLL_DIV_57MHZ,
|
||||
PLL_DIV_40MHZ,
|
||||
PLL_DIV_20MHZ,
|
||||
PLL_DIV_16MHZ,
|
||||
0
|
||||
};
|
||||
/* The PLL divisor we are currently on */
|
||||
static size_t ipll = 0;
|
||||
/* Are we bypassing the PLL, or not? */
|
||||
static bool bypass = false;
|
||||
|
||||
/*
|
||||
* Clock setup:
|
||||
* Take the main crystal oscillator at 16MHz, run it through the PLL, and divide
|
||||
* the 400MHz PLL clock to get a system clock of 80MHz.
|
||||
*/
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_sysclk_config(OSCSRC_MOSC, XTAL_16M, PLL_DIV_80MHZ);
|
||||
}
|
||||
|
||||
/*
|
||||
* GPIO setup:
|
||||
* Enable the pins driving the RGB LED as outputs.
|
||||
*/
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/*
|
||||
* Configure GPIOF
|
||||
* This port is used to control the RGB LED
|
||||
*/
|
||||
periph_clock_enable(RCC_GPIOF);
|
||||
const u32 outpins = (LED_R | LED_G | LED_B);
|
||||
|
||||
GPIO_DIR(RGB_PORT) |= outpins; /* Configure outputs. */
|
||||
GPIO_DEN(RGB_PORT) |= outpins; /* Enable digital function on outputs. */
|
||||
|
||||
/*
|
||||
* Now take care of our buttons
|
||||
*/
|
||||
const u32 btnpins = USR_SW1 | USR_SW2;
|
||||
|
||||
/*
|
||||
* PF0 is locked by default. We need to unlock the GPIO_CR register,
|
||||
* then enable PF0 commit. After we do this, we can setup PF0. If we
|
||||
* don't do this, any configuration done to PF0 is lost, and we will not
|
||||
* have a PF0 interrupt.
|
||||
*/
|
||||
GPIO_LOCK(GPIOF) = 0x4C4F434B;
|
||||
GPIO_CR(GPIOF) |= USR_SW2;
|
||||
|
||||
/* Configure pins as inputs. */
|
||||
GPIO_DIR(GPIOF) &= ~btnpins;
|
||||
/* Enable digital function on the pins. */
|
||||
GPIO_DEN(GPIOF) |= btnpins;
|
||||
/* Pull-up the pins. We don't have an external pull-up */
|
||||
GPIO_PUR(GPIOF) |= btnpins;
|
||||
}
|
||||
|
||||
/*
|
||||
* IRQ setup:
|
||||
* Trigger an interrupt whenever a button is depressed.
|
||||
*/
|
||||
static void irq_setup(void)
|
||||
{
|
||||
const u32 btnpins = USR_SW1 | USR_SW2;
|
||||
/* Configure interrupt as edge-sensitive */
|
||||
GPIO_IS(GPIOF) &= ~btnpins;
|
||||
/* Interrupt only respond to rising or falling edge (single-edge) */
|
||||
GPIO_IBE(GPIOF) &= ~btnpins;
|
||||
/* Trigger interrupt on rising-edge (when button is depressed) */
|
||||
GPIO_IEV(GPIOF) |= btnpins;
|
||||
/* Finally, Enable interrupt */
|
||||
GPIO_IM(GPIOF) |= btnpins;
|
||||
/* Enable the interrupt in the NVIC as well */
|
||||
nvic_enable_irq(NVIC_GPIOF_IRQ);
|
||||
}
|
||||
|
||||
#define FLASH_DELAY 800000
|
||||
static void delay(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < FLASH_DELAY; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
irq_setup();
|
||||
|
||||
/* Blink each color of the RGB LED in order. */
|
||||
while (1) {
|
||||
/*
|
||||
* Flash the Red diode
|
||||
*/
|
||||
gpio_set(RGB_PORT, LED_R);
|
||||
delay(); /* Wait a bit. */
|
||||
gpio_clear(RGB_PORT, LED_R);
|
||||
delay(); /* Wait a bit. */
|
||||
|
||||
/*
|
||||
* Flash the Green diode
|
||||
*/
|
||||
gpio_set(RGB_PORT, LED_G);
|
||||
delay(); /* Wait a bit. */
|
||||
gpio_clear(RGB_PORT, LED_G);
|
||||
delay(); /* Wait a bit. */
|
||||
|
||||
/*
|
||||
* Flash the Blue diode
|
||||
*/
|
||||
gpio_set(RGB_PORT, LED_B);
|
||||
delay(); /* Wait a bit. */
|
||||
gpio_clear(RGB_PORT, LED_B);
|
||||
delay(); /* Wait a bit. */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gpiof_isr(void)
|
||||
{
|
||||
if (GPIO_RIS(GPIOF) & USR_SW1) {
|
||||
/* SW1 was just depressed */
|
||||
bypass = !bypass;
|
||||
if (bypass) {
|
||||
rcc_pll_bypass_enable();
|
||||
/*
|
||||
* The divisor is still applied to the raw clock.
|
||||
* Disable the divisor, or we'll divide the raw clock.
|
||||
*/
|
||||
SYSCTL_RCC &= ~SYSCTL_RCC_USESYSDIV;
|
||||
}
|
||||
else
|
||||
{
|
||||
rcc_change_pll_divisor(plldiv[ipll]);
|
||||
}
|
||||
/* Clear interrupt source */
|
||||
GPIO_ICR(GPIOF) = USR_SW1;
|
||||
}
|
||||
|
||||
if (GPIO_RIS(GPIOF) & USR_SW2) {
|
||||
/* SW2 was just depressed */
|
||||
if (!bypass) {
|
||||
if (plldiv[++ipll] == 0)
|
||||
ipll = 0;
|
||||
rcc_change_pll_divisor(plldiv[ipll]);
|
||||
}
|
||||
/* Clear interrupt source */
|
||||
GPIO_ICR(GPIOF) = USR_SW2;
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
##
|
||||
## 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>
|
||||
##
|
||||
## 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
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../..
|
||||
ifeq ($(wildcard ../../../../lib/libopencm3_lpc13xx.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-m3 -mthumb -MD -DLPC13XX
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= flossjtag
|
||||
OOCD_BOARD ?= olimex_stm32_h103
|
||||
# FIXME
|
||||
|
||||
# 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_lpc13xx.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc13xx
|
||||
|
||||
%.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
|
||||
|
||||
# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later.
|
||||
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
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for Olimex LPC-P1343 (LPC1343FBD48, 32K flash, 8K SRAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
||||
ram (rwx) : ORIGIN = 0x10000000, LENGTH = 8K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lpc13xx.ld
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../lpc-p1343.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the NXP LPC1343-based Olimex LPC-1343 eval board (see
|
||||
http://olimex.com/dev/lpc-p1343.html for details). It should blink
|
||||
a LED on the board.
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
// #include <libopencm3/lpc13xx/rcc.h>
|
||||
#include <libopencm3/lpc13xx/gpio.h>
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
GPIO3_DIR |= (1 << 0); /* Configure P3_0 as output. */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Blink LED0 (P3_0) on the board. */
|
||||
while (1) {
|
||||
/* Manually: */
|
||||
GPIO3_DATA |= (1 << 0); /* LED off */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
GPIO3_DATA &= ~(1 << 0); /* LED on */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
##
|
||||
## 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>
|
||||
##
|
||||
## 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
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../..
|
||||
ifeq ($(wildcard ../../../../lib/libopencm3_lpc17xx.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 += -O0 -g \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include -fno-common \
|
||||
-mcpu=cortex-m3 -mthumb -MD -DLPC17XX
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= flossjtag
|
||||
OOCD_BOARD ?= olimex_stm32_h103
|
||||
# FIXME
|
||||
|
||||
# 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_lpc17xx.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc17xx
|
||||
|
||||
%.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
|
||||
|
||||
# FIXME: Replace STM32 stuff with proper LPC13XX OpenOCD support later.
|
||||
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
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for Blueboard-LPC1768-H (LPC1768, 512K flash, 64K SRAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 512K
|
||||
ram (rwx) : ORIGIN = 0x10000000, LENGTH = 32K
|
||||
ram1 (rwx) : ORIGIN = 0x2007C000, LENGTH = 16K
|
||||
ram2 (rwx) : ORIGIN = 0x20080000, LENGTH = 16K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lpc17xx.ld
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../blueboard-lpc1768-h.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,9 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the NXP LPC1768-based NGX Blueboard-LPC1768-H eval board (see
|
||||
http://shop.ngxtechnologies.com/product_info.php?cPath=21&products_id=65). It should blink
|
||||
a LED on the board.
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/lpc17xx/gpio.h>
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
GPIO1_DIR |= (1 << 29); /* Configure P1_29 as output. */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Blink LED0 (P3_0) on the board. */
|
||||
while (1) {
|
||||
/* Manually: */
|
||||
//GPIO1_SET = (1 << 29); /* LED on */
|
||||
//for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
//GPIO1_CLR = (1 << 29); /* LED off */
|
||||
//for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
// __asm__("nop");
|
||||
|
||||
gpio_set(GPIO1, GPIOPIN29); /* LED on */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
gpio_clear(GPIO1, GPIOPIN29); /* LED off */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
##
|
||||
## 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) 2012 Michael Ossmann <mike@ossmann.com>
|
||||
## Copyright (C) 2012 Benjamin Vernoux <titanmkd@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
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../..
|
||||
ifeq ($(wildcard ../../../../lib/libopencm3_lpc43xx.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 += -O2 -g \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include -fno-common \
|
||||
-mcpu=cortex-m4 -mthumb -MD \
|
||||
-mfloat-abi=hard -mfpu=fpv4-sp-d16 -DLPC43XX
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += -L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections -Xlinker -Map=$(BINARY).map
|
||||
OBJS += $(BINARY).o
|
||||
|
||||
OOCD ?= openocd
|
||||
OOCD_INTERFACE ?= flossjtag
|
||||
OOCD_BOARD ?= olimex_stm32_h103
|
||||
|
||||
# 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_lpc43xx.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc43xx
|
||||
|
||||
%.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
|
||||
$(Q)rm -f *.map
|
||||
|
||||
# FIXME: Replace STM32 stuff with proper LPC43XX OpenOCD support later.
|
||||
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
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
|
@ -1,3 +0,0 @@
|
|||
These example programs are written for the Diolan LPC-4350-DB1:
|
||||
|
||||
http://www.diolan.com/lpc4350-features.html
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 64K SRAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
/* rom is really the shadow region that points to SPI flash or elsewhere */
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M
|
||||
ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K
|
||||
/* there are some additional RAM regions */
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lpc43xx.ld
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../diolan-lpc-4350-db1.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,11 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the Diolan LPC-4350-DB1:
|
||||
|
||||
http://www.diolan.com/lpc4350-features.html
|
||||
|
||||
It should blink D2 on the board.
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2012 Michael Ossmann <mike@ossmann.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/lpc43xx/gpio.h>
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
GPIO0_DIR |= (1 << 5); /* Configure GPIO0[5] (P6_6) as output. */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Blink D2 on the board. */
|
||||
while (1) {
|
||||
|
||||
gpio_set(GPIO0, GPIOPIN5); /* LED on */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
gpio_clear(GPIO0, GPIOPIN5); /* LED off */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
These example programs are written for the Jellybean development board from the
|
||||
HackRF project:
|
||||
|
||||
https://github.com/mossmann/hackrf
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = i2cdemo
|
||||
|
||||
LDSCRIPT = ../jellybean-lpc4330.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,15 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This program exercises the I2C peripheral on Jellybean's LPC43xx. You can
|
||||
scope SCL on P6 pin 3 and SDA on P6 pin 5. If Lemondrop is connected, LED1
|
||||
will illuminate if I2C communication to the Si5351C on Lemondrop is successful.
|
||||
|
||||
Required Lemondrop -> Jellybean connections:
|
||||
|
||||
SCL: Lemondrop P7 pin 3 -> Jellybean P6 pin 3
|
||||
SDA: Lemondrop P7 pin 5 -> Jellybean P6 pin 5
|
||||
VCC: Lemondrop P4 pin 2, 4, or 6 -> Jellybean P17 pin 2, 4, or 6
|
||||
1V8: Lemondrop P11 pin 2, 4, or 6 -> Jellybean P16 pin 2, 4, or 6
|
||||
GND: Lemondrop P5 -> Jellybean P13
|
|
@ -1,108 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2012 Michael Ossmann <mike@ossmann.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/lpc43xx/gpio.h>
|
||||
#include <libopencm3/lpc43xx/i2c.h>
|
||||
|
||||
#include "../jellybean_conf.h"
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Configure SCU Pin Mux as GPIO */
|
||||
scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST);
|
||||
|
||||
/* Configure all GPIO as Input (safe state) */
|
||||
GPIO0_DIR = 0;
|
||||
GPIO1_DIR = 0;
|
||||
GPIO2_DIR = 0;
|
||||
GPIO3_DIR = 0;
|
||||
GPIO4_DIR = 0;
|
||||
GPIO5_DIR = 0;
|
||||
GPIO6_DIR = 0;
|
||||
GPIO7_DIR = 0;
|
||||
|
||||
/* Configure GPIO as Output */
|
||||
GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */
|
||||
GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */
|
||||
}
|
||||
|
||||
#define SI5351C_I2C_ADDR (0x60 << 1)
|
||||
|
||||
/* write to single register */
|
||||
/* Not used!
|
||||
static void si5351c_write_reg(uint8_t reg, uint8_t val)
|
||||
{
|
||||
i2c0_tx_start();
|
||||
i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
|
||||
i2c0_tx_byte(reg);
|
||||
i2c0_tx_byte(val);
|
||||
i2c0_stop();
|
||||
}
|
||||
*/
|
||||
|
||||
/* read single register */
|
||||
static uint8_t si5351c_read_reg(uint8_t reg)
|
||||
{
|
||||
uint8_t val;
|
||||
|
||||
/* set register address with write */
|
||||
i2c0_tx_start();
|
||||
i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_WRITE);
|
||||
i2c0_tx_byte(reg);
|
||||
|
||||
/* read the value */
|
||||
i2c0_tx_start();
|
||||
i2c0_tx_byte(SI5351C_I2C_ADDR | I2C_READ);
|
||||
val = i2c0_rx_byte();
|
||||
i2c0_stop();
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
gpio_setup();
|
||||
i2c0_init();
|
||||
|
||||
gpio_set(PORT_EN1V8, PIN_EN1V8); /* 1V8 on */
|
||||
|
||||
while (1) {
|
||||
if (si5351c_read_reg(0) == 0x10)
|
||||
gpio_set(GPIO2, GPIOPIN1); /* LED on */
|
||||
else
|
||||
gpio_clear(GPIO2, GPIOPIN1); /* LED off */
|
||||
|
||||
for (i = 0; i < 1000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 264K SRAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
/* rom is really the shadow region that points to SPI flash or elsewhere */
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M
|
||||
ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K
|
||||
/* there are some additional RAM regions */
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lpc43xx.ld
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2012 Benjamin Vernoux <titanmkd@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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for HackRF Jellybean (LPC4330, 1M SPI flash, 264K SRAM). */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
/* Physical address in Flash used to copy Code from Flash to RAM */
|
||||
rom_flash (rx) : ORIGIN = 0x80000000, LENGTH = 1M
|
||||
/* rom is really the shadow region that points to SPI flash or elsewhere */
|
||||
rom (rx) : ORIGIN = 0x00000000, LENGTH = 1M
|
||||
ram (rwx) : ORIGIN = 0x10000000, LENGTH = 128K
|
||||
/* there are some additional RAM regions for data */
|
||||
ram_data (rw) : ORIGIN = 0x10080000, LENGTH = 72K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_lpc43xx_rom_to_ram.ld
|
|
@ -1,85 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2012 Benjamin Vernoux <titanmkd@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/>.
|
||||
*/
|
||||
|
||||
#ifndef __JELLYBEAN_CONF_H
|
||||
#define __JELLYBEAN_CONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <libopencm3/lpc43xx/scu.h>
|
||||
|
||||
/*
|
||||
* JellyBean SCU PinMux
|
||||
*/
|
||||
|
||||
/* GPIO Output PinMux */
|
||||
#define SCU_PINMUX_LED1 (P4_1) /* GPIO2[1] on P4_1 */
|
||||
#define SCU_PINMUX_LED2 (P4_2) /* GPIO2[2] on P4_2 */
|
||||
#define SCU_PINMUX_LED3 (P6_12) /* GPIO2[8] on P6_12 */
|
||||
|
||||
#define SCU_PINMUX_EN1V8 (P6_10) /* GPIO3[6] on P6_10 */
|
||||
|
||||
/* GPIO Input PinMux */
|
||||
#define SCU_PINMUX_BOOT0 (P1_1) /* GPIO0[8] on P1_1 */
|
||||
#define SCU_PINMUX_BOOT1 (P1_2) /* GPIO0[9] on P1_2 */
|
||||
#define SCU_PINMUX_BOOT2 (P2_8) /* GPIO5[7] on P2_8 */
|
||||
#define SCU_PINMUX_BOOT3 (P2_9) /* GPIO1[10] on P2_9 */
|
||||
|
||||
/* SSP1 Peripheral PinMux */
|
||||
#define SCU_SSP1_MISO (P1_3) /* P1_3 */
|
||||
#define SCU_SSP1_MOSI (P1_4) /* P1_4 */
|
||||
#define SCU_SSP1_SCK (P1_19) /* P1_19 */
|
||||
#define SCU_SSP1_SSEL (P1_20) /* P1_20 */
|
||||
|
||||
/* TODO add other Pins */
|
||||
|
||||
/*
|
||||
* JellyBean GPIO Pin
|
||||
*/
|
||||
/* GPIO Output */
|
||||
#define PIN_LED1 (BIT1) /* GPIO2[1] on P4_1 */
|
||||
#define PIN_LED2 (BIT2) /* GPIO2[2] on P4_2 */
|
||||
#define PIN_LED3 (BIT8) /* GPIO2[8] on P6_12 */
|
||||
#define PORT_LED1_3 (GPIO2) /* PORT for LED1, 2 & 3 */
|
||||
|
||||
#define PIN_EN1V8 (BIT6) /* GPIO3[6] on P6_10 */
|
||||
#define PORT_EN1V8 (GPIO3)
|
||||
|
||||
/* GPIO Input */
|
||||
#define PIN_BOOT0 (BIT8) /* GPIO0[8] on P1_1 */
|
||||
#define PIN_BOOT1 (BIT9) /* GPIO0[9] on P1_2 */
|
||||
#define PIN_BOOT2 (BIT7) /* GPIO5[7] on P2_8 */
|
||||
#define PIN_BOOT3 (BIT10) /* GPIO1[10] on P2_9 */
|
||||
|
||||
/* Read GPIO Pin */
|
||||
#define BOOT0_STATE ( (GPIO0_PIN & PIN_BOOT0)==PIN_BOOT0 )
|
||||
#define BOOT1_STATE ( (GPIO0_PIN & PIN_BOOT1)==PIN_BOOT1 )
|
||||
#define BOOT2_STATE ( (GPIO5_PIN & PIN_BOOT2)==PIN_BOOT2 )
|
||||
#define BOOT3_STATE ( (GPIO1_PIN & PIN_BOOT3)==PIN_BOOT3 )
|
||||
|
||||
/* TODO add other Pins */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../jellybean-lpc4330.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,11 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the Jellybean development board from the HackRF project:
|
||||
|
||||
https://github.com/mossmann/hackrf
|
||||
|
||||
It should blink LED1 on the board.
|
|
@ -1,82 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2012 Michael Ossmann <mike@ossmann.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/lpc43xx/gpio.h>
|
||||
#include <libopencm3/lpc43xx/scu.h>
|
||||
|
||||
#include "../jellybean_conf.h"
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Configure SCU Pin Mux as GPIO */
|
||||
scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST);
|
||||
|
||||
/* Configure all GPIO as Input (safe state) */
|
||||
GPIO0_DIR = 0;
|
||||
GPIO1_DIR = 0;
|
||||
GPIO2_DIR = 0;
|
||||
GPIO3_DIR = 0;
|
||||
GPIO4_DIR = 0;
|
||||
GPIO5_DIR = 0;
|
||||
GPIO6_DIR = 0;
|
||||
GPIO7_DIR = 0;
|
||||
|
||||
/* Configure GPIO as Output */
|
||||
GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */
|
||||
GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */
|
||||
}
|
||||
|
||||
u32 boot0, boot1, boot2, boot3;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
gpio_setup();
|
||||
|
||||
/* Set 1V8 */
|
||||
gpio_set(PORT_EN1V8, PIN_EN1V8);
|
||||
|
||||
/* Blink LED1/2/3 on the board and Read BOOT0/1/2/3 pins. */
|
||||
while (1)
|
||||
{
|
||||
boot0 = BOOT0_STATE;
|
||||
boot1 = BOOT1_STATE;
|
||||
boot2 = BOOT2_STATE;
|
||||
boot3 = BOOT3_STATE;
|
||||
|
||||
gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */
|
||||
for (i = 0; i < 2000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */
|
||||
for (i = 0; i < 2000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = ../jellybean-lpc4330_rom_to_ram.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,12 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is the smallest-possible example program using libopencm3.
|
||||
|
||||
It's intended for the Jellybean development board from the HackRF project:
|
||||
|
||||
https://github.com/mossmann/hackrf
|
||||
|
||||
It should blink LED1 on the board.
|
||||
This example copy the Code from ROM to RAM and execute code from RAM.
|
|
@ -1,82 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2012 Michael Ossmann <mike@ossmann.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/lpc43xx/gpio.h>
|
||||
#include <libopencm3/lpc43xx/scu.h>
|
||||
|
||||
#include "../jellybean_conf.h"
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Configure SCU Pin Mux as GPIO */
|
||||
scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST);
|
||||
|
||||
/* Configure all GPIO as Input (safe state) */
|
||||
GPIO0_DIR = 0;
|
||||
GPIO1_DIR = 0;
|
||||
GPIO2_DIR = 0;
|
||||
GPIO3_DIR = 0;
|
||||
GPIO4_DIR = 0;
|
||||
GPIO5_DIR = 0;
|
||||
GPIO6_DIR = 0;
|
||||
GPIO7_DIR = 0;
|
||||
|
||||
/* Configure GPIO as Output */
|
||||
GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */
|
||||
GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */
|
||||
}
|
||||
|
||||
u32 boot0, boot1, boot2, boot3;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
gpio_setup();
|
||||
|
||||
/* Set 1V8 */
|
||||
gpio_set(PORT_EN1V8, PIN_EN1V8);
|
||||
|
||||
/* Blink LED1/2/3 on the board and Read BOOT0/1/2/3 pins. */
|
||||
while (1)
|
||||
{
|
||||
boot0 = BOOT0_STATE;
|
||||
boot1 = BOOT1_STATE;
|
||||
boot2 = BOOT2_STATE;
|
||||
boot3 = BOOT3_STATE;
|
||||
|
||||
gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */
|
||||
for (i = 0; i < 2000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */
|
||||
for (i = 0; i < 2000000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2010 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 = sspdemo
|
||||
|
||||
LDSCRIPT = ../jellybean-lpc4330.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,48 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This program exercises the SSP1 peripheral on Jellybean's LPC43xx.
|
||||
|
||||
Jellybean (connector)
|
||||
P9 SPI
|
||||
|-----------------|
|
||||
| Pin2 Pin4 Pin6 |
|
||||
||------| |
|
||||
|| Pin1 |Pin3 Pin5 |
|
||||
||------|----------|
|
||||
|-------|
|
||||
|
||||
SSP1_MISO: Jellybean P9 SPI Pin6
|
||||
SSP1_MOSI: Jellybean P9 SPI Pin4
|
||||
SSP1_SCK: Jellybean P9 SPI Pin2
|
||||
SSP1_SSEL: Jellybean P9 SPI Pin3
|
||||
GND: Can be connected to P12 SD Pin1
|
||||
|
||||
PCLK clock source is PLL1 288MHz (from IRC 96MHz boot from SPIFI)
|
||||
Freq = PCLK / (CPSDVSR * [SCR+1]).
|
||||
|
||||
By default (CPSDVSR=0 => Means MAX Divisor)
|
||||
SSP1->CR0->SCR = 0x00 => CLK Freq 1.126MHz
|
||||
SSP1->CR0->SCR = 0x01 => MOSI Freq 566.9KHz
|
||||
...
|
||||
|
||||
Test Oscilloscpe:
|
||||
SCR=0, CPSDVSR=32 => CLK 9.025MHz
|
||||
SCR=1, CPSDVSR=2 => CLK 73MHz
|
||||
SCR=2, CPSDVSR=2 => CLK 49MHz
|
||||
SCR=4, CPSDVSR=2 => CLK 29MHz
|
||||
SCR=8, CPSDVSR=2 => CLK 16MHz
|
||||
SCR=16, CPSDVSR=2 => CLK 8.5MHz
|
||||
SCR=32, CPSDVSR=2 => CLK 4.386MHz
|
||||
SCR=64, CPSDVSR=2 => CLK 2.227MHz
|
||||
SCR=1, CPSDVSR=64 => CLK 2.262MHz
|
||||
|
||||
Theory:
|
||||
SCR=0, CPSDVSR=32 => 288MHz / (32*(0+1) = 9MHz
|
||||
SCR=1, CPSDVSR=2 => 288MHz / (2*(1+1) = 72MHz
|
||||
SCR=4, CPSDVSR=2 => 288MHz / (2*(4+1) = 28.8MHz
|
||||
SCR=32, CPSDVSR=2 => 288MHz / (2*(32+1) = 4.364MHz
|
||||
SCR=64, CPSDVSR=2 => 288MHz / (2*(64+1)) = 2.2154MHz
|
||||
SCR=128, CPSDVSR=2 => 288MHz / (2*(128+1)) = 1.116MHz
|
||||
SCR=1, CPSDVSR=64 => 288MHz / (64*(1+1)) = 2.25MHz
|
|
@ -1,102 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2012 Benjamin Vernoux <titanmkd@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/lpc43xx/gpio.h>
|
||||
#include <libopencm3/lpc43xx/scu.h>
|
||||
#include <libopencm3/lpc43xx/cgu.h>
|
||||
#include <libopencm3/lpc43xx/ssp.h>
|
||||
|
||||
#include "../jellybean_conf.h"
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Configure all GPIO as Input (safe state) */
|
||||
GPIO0_DIR = 0;
|
||||
GPIO1_DIR = 0;
|
||||
GPIO2_DIR = 0;
|
||||
GPIO3_DIR = 0;
|
||||
GPIO4_DIR = 0;
|
||||
GPIO5_DIR = 0;
|
||||
GPIO6_DIR = 0;
|
||||
GPIO7_DIR = 0;
|
||||
|
||||
/* Configure SCU Pin Mux as GPIO */
|
||||
scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST);
|
||||
|
||||
/* Configure SSP1 Peripheral (to be moved later in SSP driver) */
|
||||
scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
|
||||
scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
|
||||
scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
|
||||
scu_pinmux(SCU_SSP1_SSEL, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
|
||||
|
||||
/* Configure GPIO as Output */
|
||||
GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */
|
||||
GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
u8 ssp_val;
|
||||
u8 serial_clock_rate;
|
||||
u8 clock_prescale_rate;
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* Freq About 1.12MHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=288MHz */
|
||||
clock_prescale_rate = 2;
|
||||
serial_clock_rate = 128;
|
||||
|
||||
ssp_init(SSP1_NUM,
|
||||
SSP_DATA_8BITS,
|
||||
SSP_FRAME_SPI,
|
||||
SSP_CPOL_0_CPHA_0,
|
||||
serial_clock_rate,
|
||||
clock_prescale_rate,
|
||||
SSP_MODE_NORMAL,
|
||||
SSP_MASTER,
|
||||
SSP_SLAVE_OUT_ENABLE);
|
||||
|
||||
ssp_val = 0x0;
|
||||
|
||||
while (1) {
|
||||
|
||||
ssp_write(SSP1_NUM, (u16)ssp_val);
|
||||
|
||||
gpio_set(GPIO2, GPIOPIN1); /* LED on */
|
||||
|
||||
for (i = 0; i < 1000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
gpio_clear(GPIO2, GPIOPIN1); /* LED off */
|
||||
|
||||
ssp_val++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
##
|
||||
## This file is part of the libopencm3 project.
|
||||
##
|
||||
## Copyright (C) 2012 Benjamin Vernoux <titanmkd@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/>.
|
||||
##
|
||||
|
||||
BINARY = systickdemo
|
||||
|
||||
LDSCRIPT = ../jellybean-lpc4330.ld
|
||||
|
||||
include ../../Makefile.include
|
|
@ -1,8 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This program exercises the SysTick Interrupt of ARM CortexM4 on Jellybean's LPC43xx.
|
||||
It also enable Cycle Counter to be used for accurate delay independant from Clock Frequency.
|
||||
The Demo Use Cycle Counter and SysTick Interrupt to compute number of cycles executed per second.
|
||||
The result is LED1/2 & 3 Blink with an accurate 1s Period (using SysTick) (Checked visualy and with Oscilloscope).
|
|
@ -1,184 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2012 Benjamin Vernoux <titanmkd@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/lpc43xx/gpio.h>
|
||||
#include <libopencm3/lpc43xx/scu.h>
|
||||
#include <libopencm3/lpc43xx/cgu.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
#include <libopencm3/cm3/scs.h>
|
||||
|
||||
#include "../jellybean_conf.h"
|
||||
|
||||
/* Global counter incremented by SysTick Interrupt each millisecond */
|
||||
volatile u32 g_ulSysTickCount;
|
||||
u32 g_NbCyclePerSecond;
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Configure all GPIO as Input (safe state) */
|
||||
GPIO0_DIR = 0;
|
||||
GPIO1_DIR = 0;
|
||||
GPIO2_DIR = 0;
|
||||
GPIO3_DIR = 0;
|
||||
GPIO4_DIR = 0;
|
||||
GPIO5_DIR = 0;
|
||||
GPIO6_DIR = 0;
|
||||
GPIO7_DIR = 0;
|
||||
|
||||
/* Configure SCU Pin Mux as GPIO */
|
||||
scu_pinmux(SCU_PINMUX_LED1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_LED3, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_EN1V8, SCU_GPIO_FAST);
|
||||
|
||||
scu_pinmux(SCU_PINMUX_BOOT0, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT1, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT2, SCU_GPIO_FAST);
|
||||
scu_pinmux(SCU_PINMUX_BOOT3, SCU_GPIO_FAST);
|
||||
|
||||
/* Configure SSP1 Peripheral (to be moved later in SSP driver) */
|
||||
scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
|
||||
scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
|
||||
scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
|
||||
scu_pinmux(SCU_SSP1_SSEL, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
|
||||
|
||||
/* Configure GPIO as Output */
|
||||
GPIO2_DIR |= (PIN_LED1|PIN_LED2|PIN_LED3); /* Configure GPIO2[1/2/8] (P4_1/2 P6_12) as output. */
|
||||
GPIO3_DIR |= PIN_EN1V8; /* GPIO3[6] on P6_10 as output. */
|
||||
}
|
||||
|
||||
static void systick_setup(void)
|
||||
{
|
||||
u32 systick_reload_val;
|
||||
g_ulSysTickCount = 0;
|
||||
|
||||
/* Disable IRQ globally */
|
||||
asm volatile ("cpsid i");
|
||||
|
||||
/* Set processor Clock as Source Clock */
|
||||
systick_set_clocksource(STK_CTRL_CLKSOURCE);
|
||||
|
||||
/* Get SysTick calibration value to obtain by default 1 tick = 10ms */
|
||||
systick_reload_val = systick_get_calib();
|
||||
/*
|
||||
* Calibration seems wrong on LPC43xx(TBC) for default Freq it assume System Clock is 12MHz but it is 12*8=96MHz
|
||||
* Fix the Calibration value bu multiplication by 8
|
||||
*/
|
||||
systick_reload_val = (systick_reload_val*8);
|
||||
|
||||
/* To obtain 1ms per tick just divide by 10 the 10ms base tick and set the reload */
|
||||
systick_reload_val = systick_reload_val/10;
|
||||
systick_set_reload(systick_reload_val);
|
||||
|
||||
systick_interrupt_enable();
|
||||
|
||||
/* Start counting. */
|
||||
systick_counter_enable();
|
||||
|
||||
/* Set SysTick Priority to maximum */
|
||||
nvic_set_priority(NVIC_SYSTICK_IRQ, 0xFF);
|
||||
|
||||
/* Enable IRQ globally */
|
||||
asm volatile ("cpsie i");
|
||||
}
|
||||
|
||||
static void scs_dwt_cycle_counter_enabled(void)
|
||||
{
|
||||
SCS_DEMCR |= SCS_DEMCR_TRCENA;
|
||||
SCS_DWT_CTRL |= SCS_DWT_CTRL_CYCCNTENA;
|
||||
}
|
||||
|
||||
static u32 sys_tick_get_time_ms(void)
|
||||
{
|
||||
return g_ulSysTickCount;
|
||||
}
|
||||
|
||||
static u32 sys_tick_delta_time_ms(u32 start, u32 end)
|
||||
{
|
||||
#define MAX_T_U32 ((2^32)-1)
|
||||
u32 diff;
|
||||
|
||||
if(end > start)
|
||||
{
|
||||
diff=end-start;
|
||||
}else
|
||||
{
|
||||
diff=MAX_T_U32-(start-end)+1;
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
static void sys_tick_wait_time_ms(u32 wait_ms)
|
||||
{
|
||||
u32 start, end;
|
||||
u32 tickms;
|
||||
|
||||
start = sys_tick_get_time_ms();
|
||||
|
||||
do
|
||||
{
|
||||
end = sys_tick_get_time_ms();
|
||||
tickms = sys_tick_delta_time_ms(start, end);
|
||||
}while(tickms < wait_ms);
|
||||
}
|
||||
|
||||
/* Called each 1ms/1000Hz by interrupt
|
||||
1) Count the number of cycle per second.
|
||||
2) Increment g_ulSysTickCount counter.
|
||||
*/
|
||||
void sys_tick_handler(void)
|
||||
{
|
||||
if(g_ulSysTickCount==0)
|
||||
{
|
||||
/* Clear Cycle Counter*/
|
||||
SCS_DWT_CYCCNT = 0;
|
||||
}else if(g_ulSysTickCount==1000)
|
||||
{
|
||||
/* Capture number of cycle elapsed during 1 second */
|
||||
g_NbCyclePerSecond = SCS_DWT_CYCCNT;
|
||||
}
|
||||
|
||||
g_ulSysTickCount++;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
systick_setup();
|
||||
|
||||
gpio_setup();
|
||||
|
||||
/* SCS & Cycle Counter enabled (used to count number of cycles executed per second see g_NbCyclePerSecond */
|
||||
scs_dwt_cycle_counter_enabled();
|
||||
|
||||
while (1)
|
||||
{
|
||||
gpio_set(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LEDs on */
|
||||
|
||||
sys_tick_wait_time_ms(500);
|
||||
|
||||
gpio_clear(PORT_LED1_3, (PIN_LED1|PIN_LED2|PIN_LED3)); /* LED off */
|
||||
|
||||
sys_tick_wait_time_ms(500);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,172 +0,0 @@
|
|||
##
|
||||
## 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>
|
||||
##
|
||||
## 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
|
||||
|
||||
TOOLCHAIN_DIR ?= ../../../../..
|
||||
ifeq ($(wildcard ../../../../../lib/libopencm3_stm32f1.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
|
||||
|
||||
ARCH_FLAGS = -mthumb -mcpu=cortex-m3 -msoft-float
|
||||
CFLAGS += -Os -g \
|
||||
-Wall -Wextra -Wimplicit-function-declaration \
|
||||
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
|
||||
-Wundef -Wshadow \
|
||||
-I$(TOOLCHAIN_DIR)/include \
|
||||
-fno-common $(ARCH_FLAGS) -MD -DSTM32F1
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
LDFLAGS += --static -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group \
|
||||
-L$(TOOLCHAIN_DIR)/lib \
|
||||
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \
|
||||
$(ARCH_FLAGS) -mfix-cortex-m3-ldrd
|
||||
|
||||
ifneq ($(OPENCM3_DIR),)
|
||||
CFLAGS += -I$(OPENCM3_DIR)/include
|
||||
LDFLAGS += -L$(OPENCM3_DIR)/lib -L$(OPENCM3_DIR)/lib/stm32/f1
|
||||
SCRIPT_DIR = $(OPENCM3_DIR)/share
|
||||
else
|
||||
SCRIPT_DIR = $(shell dirname $(shell readlink -f $(shell which $(PREFIX)-gcc)))/../$(PREFIX)/share
|
||||
endif
|
||||
|
||||
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 ?=
|
||||
# texane/stlink can be used by uncommenting this...
|
||||
# or defining it in your own makefiles
|
||||
#STLINK_PORT ?= :4242
|
||||
|
||||
# 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_stm32f1.a
|
||||
@#printf " LD $(subst $(shell pwd)/,,$(@))\n"
|
||||
$(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f1 $(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
|
||||
|
||||
ifeq ($(STLINK_PORT),)
|
||||
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 "stm32f1x 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 "stm32f1x 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
|
||||
else
|
||||
%.flash: %.elf
|
||||
@echo " GDB $(*).elf (flash)"
|
||||
$(Q)$(GDB) --batch \
|
||||
-ex 'target extended-remote $(STLINK_PORT)' \
|
||||
-x $(SCRIPT_DIR)/libopencm3/scripts/stlink_flash.scr \
|
||||
$(*).elf
|
||||
endif
|
||||
|
||||
.PHONY: images clean
|
||||
|
||||
-include $(OBJS:.o=.d)
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## 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 = can
|
||||
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
This test sets up the CAN interface on Lisa/M and transmits 8 bites every
|
||||
100ms. The first byte is being incremented in each cycle. The demo also
|
||||
receives messages and is displaing the first 4 bits of the first byte on the
|
||||
board LEDs.
|
|
@ -1,237 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2010 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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
#include <libopencm3/stm32/can.h>
|
||||
|
||||
struct can_tx_msg {
|
||||
u32 std_id;
|
||||
u32 ext_id;
|
||||
u8 ide;
|
||||
u8 rtr;
|
||||
u8 dlc;
|
||||
u8 data[8];
|
||||
};
|
||||
|
||||
struct can_rx_msg {
|
||||
u32 std_id;
|
||||
u32 ext_id;
|
||||
u8 ide;
|
||||
u8 rtr;
|
||||
u8 dlc;
|
||||
u8 data[8];
|
||||
u8 fmi;
|
||||
};
|
||||
|
||||
struct can_tx_msg can_tx_msg;
|
||||
struct can_rx_msg can_rx_msg;
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable Alternate Function clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
||||
|
||||
/* Enable GPIOA clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
|
||||
/* Enable GPIOB clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
|
||||
|
||||
/* Enable GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Preconfigure LEDs. */
|
||||
gpio_set(GPIOA, GPIO8); /* LED0 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED1 off */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 off */
|
||||
gpio_set(GPIOC, GPIO2); /* LED3 off */
|
||||
gpio_set(GPIOC, GPIO5); /* LED4 off */
|
||||
|
||||
/* Configure LED GPIOOs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
|
||||
|
||||
/* Configure PB4 as GPIO. */
|
||||
AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;
|
||||
|
||||
}
|
||||
|
||||
static 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 interrupt every N clock pulses: set reload to N-1 */
|
||||
systick_set_reload(8999);
|
||||
|
||||
systick_interrupt_enable();
|
||||
|
||||
/* Start counting. */
|
||||
systick_counter_enable();
|
||||
}
|
||||
|
||||
static void can_setup(void)
|
||||
{
|
||||
/* Enable peripheral clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CANEN);
|
||||
|
||||
AFIO_MAPR |= AFIO_MAPR_CAN1_REMAP_PORTB;
|
||||
|
||||
/* Configure CAN pin: RX (input pull-up). */
|
||||
gpio_set_mode(GPIO_BANK_CAN1_PB_RX, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX);
|
||||
gpio_set(GPIOB, GPIO_CAN1_PB_RX);
|
||||
|
||||
/* Configure CAN pin: TX. */
|
||||
gpio_set_mode(GPIO_BANK_CAN1_PB_TX, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX);
|
||||
|
||||
/* NVIC setup. */
|
||||
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
|
||||
nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1);
|
||||
|
||||
/* Reset CAN. */
|
||||
can_reset(CAN1);
|
||||
|
||||
/* CAN cell init. */
|
||||
if (can_init(CAN1,
|
||||
false, /* TTCM: Time triggered comm mode? */
|
||||
true, /* ABOM: Automatic bus-off management? */
|
||||
false, /* AWUM: Automatic wakeup mode? */
|
||||
false, /* NART: No automatic retransmission? */
|
||||
false, /* RFLM: Receive FIFO locked mode? */
|
||||
false, /* TXFP: Transmit FIFO priority? */
|
||||
CAN_BTR_SJW_1TQ,
|
||||
CAN_BTR_TS1_3TQ,
|
||||
CAN_BTR_TS2_4TQ,
|
||||
12, /* BRP+1: Baud rate prescaler */
|
||||
false, /* loopback mode */
|
||||
false)) /* silent mode */
|
||||
{
|
||||
gpio_set(GPIOA, GPIO8); /* LED0 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED1 off */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 off */
|
||||
gpio_clear(GPIOC, GPIO2); /* LED3 on */
|
||||
gpio_set(GPIOC, GPIO5); /* LED4 off */
|
||||
|
||||
/* Die because we failed to initialize. */
|
||||
while (1)
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
/* CAN filter 0 init. */
|
||||
can_filter_id_mask_32bit_init(CAN1,
|
||||
0, /* Filter ID */
|
||||
0, /* CAN ID */
|
||||
0, /* CAN ID mask */
|
||||
0, /* FIFO assignment (here: FIFO0) */
|
||||
true); /* Enable the filter. */
|
||||
|
||||
/* Enable CAN RX interrupt. */
|
||||
can_enable_irq(CAN1, CAN_IER_FMPIE0);
|
||||
}
|
||||
|
||||
void sys_tick_handler(void)
|
||||
{
|
||||
static int temp32 = 0;
|
||||
static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0};
|
||||
|
||||
/* We call this handler every 1ms so 100ms = 1s
|
||||
* Resulting in 100Hz message frequency.
|
||||
*/
|
||||
if (++temp32 != 100)
|
||||
return;
|
||||
|
||||
temp32 = 0;
|
||||
|
||||
/* Transmit CAN frame. */
|
||||
data[0]++;
|
||||
if (can_transmit(CAN1,
|
||||
0, /* (EX/ST)ID: CAN ID */
|
||||
false, /* IDE: CAN ID extended? */
|
||||
false, /* RTR: Request transmit? */
|
||||
8, /* DLC: Data length */
|
||||
data) == -1)
|
||||
{
|
||||
gpio_set(GPIOA, GPIO8); /* LED0 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED1 off */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 off */
|
||||
gpio_set(GPIOC, GPIO2); /* LED3 off */
|
||||
gpio_clear(GPIOC, GPIO5); /* LED4 on */
|
||||
}
|
||||
}
|
||||
|
||||
void usb_lp_can_rx0_isr(void)
|
||||
{
|
||||
u32 id, fmi;
|
||||
bool ext, rtr;
|
||||
u8 length, data[8];
|
||||
|
||||
can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data);
|
||||
|
||||
if (data[0] & 1)
|
||||
gpio_clear(GPIOA, GPIO8);
|
||||
else
|
||||
gpio_set(GPIOA, GPIO8);
|
||||
|
||||
if (data[0] & 2)
|
||||
gpio_clear(GPIOB, GPIO4);
|
||||
else
|
||||
gpio_set(GPIOB, GPIO4);
|
||||
|
||||
if (data[0] & 4)
|
||||
gpio_clear(GPIOC, GPIO15);
|
||||
else
|
||||
gpio_set(GPIOC, GPIO15);
|
||||
|
||||
if (data[0] & 8)
|
||||
gpio_clear(GPIOC, GPIO2);
|
||||
else
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
|
||||
can_fifo_release(CAN1, 0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
can_setup();
|
||||
systick_setup();
|
||||
|
||||
while (1); /* Halt. */
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## 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 = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
/*
|
||||
* 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 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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
|
||||
/* Set STM32 to 72 MHz. */
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOB, GPIOC, and AFIO clocks. */
|
||||
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);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO13 (in GPIO port C) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
|
||||
|
||||
/* Set GPIO4 (in GPIO port B) to 'output push-pull'. */
|
||||
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 LEDs. */
|
||||
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 LEDs (PC13 and PB4) 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;
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for Lisa-M (STM32F103RBT6, 128K flash, 20K RAM). */
|
||||
/* TODO: Chip name and sizes correct? */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE libopencm3_stm32f1.ld
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## 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 = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example implements a USB CDC-ACM device (aka Virtual Serial Port)
|
||||
to demonstrate the use of the USB device stack.
|
||||
|
|
@ -1,261 +0,0 @@
|
|||
/*
|
||||
* 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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include <libopencm3/usb/cdc.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.
|
||||
*/
|
||||
char local_buf[10];
|
||||
struct usb_cdc_notification *notif = (void*)local_buf;
|
||||
|
||||
/* We echo signals back to host as notification. */
|
||||
notif->bmRequestType = 0xA1;
|
||||
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
|
||||
notif->wValue = 0;
|
||||
notif->wIndex = 0;
|
||||
notif->wLength = 2;
|
||||
local_buf[8] = req->wValue & 3;
|
||||
local_buf[9] = 0;
|
||||
// usbd_ep_write_packet(0x83, buf, 10);
|
||||
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)
|
||||
;
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
|
||||
usbd_device *usbd_dev;
|
||||
|
||||
rcc_clock_setup_in_hsi_out_48mhz();
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
|
||||
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
gpio_set(GPIOC, GPIO5);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
|
||||
|
||||
usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 3);
|
||||
usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);
|
||||
|
||||
for (i = 0; i < 0x800000; i++)
|
||||
__asm__("nop");
|
||||
gpio_clear(GPIOC, GPIO2);
|
||||
|
||||
while (1)
|
||||
usbd_poll(usbd_dev);
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
#! /usr/bin/env python
|
||||
#
|
||||
# This file is part of the libopencm3 project.
|
||||
#
|
||||
# Copyright (C) 2011 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/>.
|
||||
#
|
||||
|
||||
import serial
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
|
||||
def signal_handler(signal, frame):
|
||||
print 'You pressed Ctrl+C!'
|
||||
ser.close();
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler);
|
||||
|
||||
ser = serial.Serial('/dev/cu.usbmodemDEM1', 115200, timeout=0.05);
|
||||
|
||||
error = 0
|
||||
|
||||
tim = time.time();
|
||||
|
||||
cycles = 0
|
||||
|
||||
while 1==1:
|
||||
|
||||
ser.write("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(){}");
|
||||
|
||||
buf = ser.read(1024);
|
||||
|
||||
cycles += 1
|
||||
|
||||
if buf != "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(){}" :
|
||||
error+=1
|
||||
|
||||
print "received " + buf + " errors: " + str(error) + " cycles: " + str(cycles) + " runtime: " + str(time.time() - tim)
|
||||
|
||||
#time.sleep(0.1);
|
||||
|
||||
ser.close();
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## 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 = usbdfu
|
||||
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example implements a USB Device Firmware Upgrade (DFU) bootloader
|
||||
to demonstrate the use of the USB device stack.
|
||||
|
|
@ -1,272 +0,0 @@
|
|||
/*
|
||||
* 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 <string.h>
|
||||
#include <libopencm3/stm32/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/cm3/scb.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include <libopencm3/usb/dfu.h>
|
||||
|
||||
#define APP_ADDRESS 0x08002000
|
||||
|
||||
/* Commands sent with wBlockNum == 0 as per ST implementation. */
|
||||
#define CMD_SETADDR 0x21
|
||||
#define CMD_ERASE 0x41
|
||||
|
||||
/* We need a special large control buffer for this device: */
|
||||
u8 usbd_control_buffer[1024];
|
||||
|
||||
static enum dfu_state usbdfu_state = STATE_DFU_IDLE;
|
||||
|
||||
static struct {
|
||||
u8 buf[sizeof(usbd_control_buffer)];
|
||||
u16 len;
|
||||
u32 addr;
|
||||
u16 blocknum;
|
||||
} prog;
|
||||
|
||||
const struct usb_device_descriptor dev = {
|
||||
.bLength = USB_DT_DEVICE_SIZE,
|
||||
.bDescriptorType = USB_DT_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = 0,
|
||||
.bDeviceSubClass = 0,
|
||||
.bDeviceProtocol = 0,
|
||||
.bMaxPacketSize0 = 64,
|
||||
.idVendor = 0x0483,
|
||||
.idProduct = 0xDF11,
|
||||
.bcdDevice = 0x0200,
|
||||
.iManufacturer = 1,
|
||||
.iProduct = 2,
|
||||
.iSerialNumber = 3,
|
||||
.bNumConfigurations = 1,
|
||||
};
|
||||
|
||||
const struct usb_dfu_descriptor dfu_function = {
|
||||
.bLength = sizeof(struct usb_dfu_descriptor),
|
||||
.bDescriptorType = DFU_FUNCTIONAL,
|
||||
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
|
||||
.wDetachTimeout = 255,
|
||||
.wTransferSize = 1024,
|
||||
.bcdDFUVersion = 0x011A,
|
||||
};
|
||||
|
||||
const struct usb_interface_descriptor iface = {
|
||||
.bLength = USB_DT_INTERFACE_SIZE,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 0,
|
||||
.bInterfaceClass = 0xFE, /* Device Firmware Upgrade */
|
||||
.bInterfaceSubClass = 1,
|
||||
.bInterfaceProtocol = 2,
|
||||
|
||||
/* The ST Microelectronics DfuSe application needs this string.
|
||||
* The format isn't documented... */
|
||||
.iInterface = 4,
|
||||
|
||||
.extra = &dfu_function,
|
||||
.extralen = sizeof(dfu_function),
|
||||
};
|
||||
|
||||
const struct usb_interface ifaces[] = {{
|
||||
.num_altsetting = 1,
|
||||
.altsetting = &iface,
|
||||
}};
|
||||
|
||||
const struct usb_config_descriptor config = {
|
||||
.bLength = USB_DT_CONFIGURATION_SIZE,
|
||||
.bDescriptorType = USB_DT_CONFIGURATION,
|
||||
.wTotalLength = 0,
|
||||
.bNumInterfaces = 1,
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 0,
|
||||
.bmAttributes = 0xC0,
|
||||
.bMaxPower = 0x32,
|
||||
|
||||
.interface = ifaces,
|
||||
};
|
||||
|
||||
static const char *usb_strings[] = {
|
||||
"Black Sphere Technologies",
|
||||
"DFU Demo",
|
||||
"DEMO",
|
||||
/* This string is used by ST Microelectronics' DfuSe utility. */
|
||||
"@Internal Flash /0x08000000/8*001Ka,56*001Kg",
|
||||
};
|
||||
|
||||
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
||||
{
|
||||
switch (usbdfu_state) {
|
||||
case STATE_DFU_DNLOAD_SYNC:
|
||||
usbdfu_state = STATE_DFU_DNBUSY;
|
||||
*bwPollTimeout = 100;
|
||||
return DFU_STATUS_OK;
|
||||
case STATE_DFU_MANIFEST_SYNC:
|
||||
/* Device will reset when read is complete. */
|
||||
usbdfu_state = STATE_DFU_MANIFEST;
|
||||
return DFU_STATUS_OK;
|
||||
default:
|
||||
return DFU_STATUS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req)
|
||||
{
|
||||
int i;
|
||||
(void)req;
|
||||
(void)usbd_dev;
|
||||
|
||||
switch (usbdfu_state) {
|
||||
case STATE_DFU_DNBUSY:
|
||||
flash_unlock();
|
||||
if (prog.blocknum == 0) {
|
||||
switch (prog.buf[0]) {
|
||||
case CMD_ERASE:
|
||||
{
|
||||
u32 *dat = (u32 *)(prog.buf + 1);
|
||||
flash_erase_page(*dat);
|
||||
}
|
||||
case CMD_SETADDR:
|
||||
{
|
||||
u32 *dat = (u32 *)(prog.buf + 1);
|
||||
prog.addr = *dat;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
u32 baseaddr = prog.addr + ((prog.blocknum - 2) *
|
||||
dfu_function.wTransferSize);
|
||||
for (i = 0; i < prog.len; i += 2) {
|
||||
u16 *dat = (u16 *)(prog.buf + i);
|
||||
flash_program_half_word(baseaddr + i,
|
||||
*dat);
|
||||
}
|
||||
}
|
||||
flash_lock();
|
||||
|
||||
/* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */
|
||||
usbdfu_state = STATE_DFU_DNLOAD_IDLE;
|
||||
return;
|
||||
case STATE_DFU_MANIFEST:
|
||||
/* USB device must detach, we just reset... */
|
||||
scb_reset_system();
|
||||
return; /* Will never return. */
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static int usbdfu_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)usbd_dev;
|
||||
|
||||
if ((req->bmRequestType & 0x7F) != 0x21)
|
||||
return 0; /* Only accept class request. */
|
||||
|
||||
switch (req->bRequest) {
|
||||
case DFU_DNLOAD:
|
||||
if ((len == NULL) || (*len == 0)) {
|
||||
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
||||
return 1;
|
||||
} else {
|
||||
/* Copy download data for use on GET_STATUS. */
|
||||
prog.blocknum = req->wValue;
|
||||
prog.len = *len;
|
||||
memcpy(prog.buf, *buf, *len);
|
||||
usbdfu_state = STATE_DFU_DNLOAD_SYNC;
|
||||
return 1;
|
||||
}
|
||||
case DFU_CLRSTATUS:
|
||||
/* Clear error and return to dfuIDLE. */
|
||||
if (usbdfu_state == STATE_DFU_ERROR)
|
||||
usbdfu_state = STATE_DFU_IDLE;
|
||||
return 1;
|
||||
case DFU_ABORT:
|
||||
/* Abort returns to dfuIDLE state. */
|
||||
usbdfu_state = STATE_DFU_IDLE;
|
||||
return 1;
|
||||
case DFU_UPLOAD:
|
||||
/* Upload not supported for now. */
|
||||
return 0;
|
||||
case DFU_GETSTATUS: {
|
||||
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
||||
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
||||
(*buf)[1] = bwPollTimeout & 0xFF;
|
||||
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
||||
(*buf)[3] = (bwPollTimeout >> 16) & 0xFF;
|
||||
(*buf)[4] = usbdfu_state;
|
||||
(*buf)[5] = 0; /* iString not used here */
|
||||
*len = 6;
|
||||
*complete = usbdfu_getstatus_complete;
|
||||
return 1;
|
||||
}
|
||||
case DFU_GETSTATE:
|
||||
/* Return state with no state transision. */
|
||||
*buf[0] = usbdfu_state;
|
||||
*len = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
usbd_device *usbd_dev;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
|
||||
if (!gpio_get(GPIOA, GPIO10)) {
|
||||
/* Boot the application if it's valid. */
|
||||
if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
||||
/* Set vector table base address. */
|
||||
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
||||
/* Initialise master stack pointer. */
|
||||
asm volatile("msr msp, %0"::"g"
|
||||
(*(volatile u32 *)APP_ADDRESS));
|
||||
/* Jump to application. */
|
||||
(*(void (**)())(APP_ADDRESS + 4))();
|
||||
}
|
||||
}
|
||||
|
||||
rcc_clock_setup_in_hsi_out_48mhz();
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
|
||||
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
|
||||
usbd_dev = usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings, 4);
|
||||
usbd_set_control_buffer_size(usbd_dev, sizeof(usbd_control_buffer));
|
||||
usbd_register_control_callback(
|
||||
usbd_dev,
|
||||
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
|
||||
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
|
||||
usbdfu_control_request);
|
||||
|
||||
gpio_clear(GPIOC, GPIO2);
|
||||
|
||||
while (1)
|
||||
usbd_poll(usbd_dev);
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
##
|
||||
## 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 = usbhid
|
||||
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example implements a USB Human Interface Device (HID)
|
||||
to demonstrate the use of the USB device stack.
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2011 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __ADXL345_H
|
||||
#define __ADXL345_H
|
||||
|
||||
/* Register addresses */
|
||||
#define ADXL345_DEVID 0x00
|
||||
#define ADXL345_POWER_CTL 0x2D
|
||||
#define ADXL345_DATA_FORMAT 0x31
|
||||
#define ADXL345_DATAX0 0x32
|
||||
#define ADXL345_DATAX1 0x33
|
||||
#define ADXL345_DATAY0 0x34
|
||||
#define ADXL345_DATAY1 0x35
|
||||
#define ADXL345_DATAZ0 0x36
|
||||
#define ADXL345_DATAZ1 0x37
|
||||
|
||||
#define ADXL345_POWER_CTL_MEASURE (1 << 3)
|
||||
#define ADXL345_DATA_FORMAT_LALIGN (1 << 2)
|
||||
|
||||
#endif
|
||||
|
|
@ -1,369 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
|
||||
* Copyright (C) 2011 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 <stdlib.h>
|
||||
#include <libopencm3/stm32/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
#include <libopencm3/stm32/otg_fs.h>
|
||||
#include <libopencm3/stm32/f1/nvic.h>
|
||||
#include <libopencm3/usb/usbd.h>
|
||||
#include <libopencm3/usb/hid.h>
|
||||
#include "adxl345.h"
|
||||
|
||||
/* Define this to include the DFU APP interface. */
|
||||
#define INCLUDE_DFU_INTERFACE
|
||||
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
#include <libopencm3/cm3/scb.h>
|
||||
#include <libopencm3/usb/dfu.h>
|
||||
#endif
|
||||
|
||||
static usbd_device *usbd_dev;
|
||||
|
||||
const struct usb_device_descriptor dev_descr = {
|
||||
.bLength = USB_DT_DEVICE_SIZE,
|
||||
.bDescriptorType = USB_DT_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = 0,
|
||||
.bDeviceSubClass = 0,
|
||||
.bDeviceProtocol = 0,
|
||||
.bMaxPacketSize0 = 64,
|
||||
.idVendor = 0x0483,
|
||||
.idProduct = 0x5710,
|
||||
.bcdDevice = 0x0200,
|
||||
.iManufacturer = 1,
|
||||
.iProduct = 2,
|
||||
.iSerialNumber = 3,
|
||||
.bNumConfigurations = 1,
|
||||
};
|
||||
|
||||
/* I have no idea what this means. I haven't read the HID spec. */
|
||||
static const u8 hid_report_descriptor[] = {
|
||||
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01,
|
||||
0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
|
||||
0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
|
||||
0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
|
||||
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38,
|
||||
0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03,
|
||||
0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09,
|
||||
0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95,
|
||||
0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1,
|
||||
0x01, 0xc0,
|
||||
};
|
||||
|
||||
static const struct {
|
||||
struct usb_hid_descriptor hid_descriptor;
|
||||
struct {
|
||||
u8 bReportDescriptorType;
|
||||
u16 wDescriptorLength;
|
||||
} __attribute__((packed)) hid_report;
|
||||
} __attribute__((packed)) hid_function = {
|
||||
.hid_descriptor = {
|
||||
.bLength = sizeof(hid_function),
|
||||
.bDescriptorType = USB_DT_HID,
|
||||
.bcdHID = 0x0100,
|
||||
.bCountryCode = 0,
|
||||
.bNumDescriptors = 1,
|
||||
},
|
||||
.hid_report = {
|
||||
.bReportDescriptorType = USB_DT_REPORT,
|
||||
.wDescriptorLength = sizeof(hid_report_descriptor),
|
||||
}
|
||||
};
|
||||
|
||||
const struct usb_endpoint_descriptor hid_endpoint = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 0x81,
|
||||
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
|
||||
.wMaxPacketSize = 4,
|
||||
.bInterval = 0x02,
|
||||
};
|
||||
|
||||
const struct usb_interface_descriptor hid_iface = {
|
||||
.bLength = USB_DT_INTERFACE_SIZE,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 1,
|
||||
.bInterfaceClass = USB_CLASS_HID,
|
||||
.bInterfaceSubClass = 1, /* boot */
|
||||
.bInterfaceProtocol = 2, /* mouse */
|
||||
.iInterface = 0,
|
||||
|
||||
.endpoint = &hid_endpoint,
|
||||
|
||||
.extra = &hid_function,
|
||||
.extralen = sizeof(hid_function),
|
||||
};
|
||||
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
const struct usb_dfu_descriptor dfu_function = {
|
||||
.bLength = sizeof(struct usb_dfu_descriptor),
|
||||
.bDescriptorType = DFU_FUNCTIONAL,
|
||||
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
|
||||
.wDetachTimeout = 255,
|
||||
.wTransferSize = 1024,
|
||||
.bcdDFUVersion = 0x011A,
|
||||
};
|
||||
|
||||
const struct usb_interface_descriptor dfu_iface = {
|
||||
.bLength = USB_DT_INTERFACE_SIZE,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 1,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 0,
|
||||
.bInterfaceClass = 0xFE,
|
||||
.bInterfaceSubClass = 1,
|
||||
.bInterfaceProtocol = 1,
|
||||
.iInterface = 0,
|
||||
|
||||
.extra = &dfu_function,
|
||||
.extralen = sizeof(dfu_function),
|
||||
};
|
||||
#endif
|
||||
|
||||
const struct usb_interface ifaces[] = {{
|
||||
.num_altsetting = 1,
|
||||
.altsetting = &hid_iface,
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
}, {
|
||||
.num_altsetting = 1,
|
||||
.altsetting = &dfu_iface,
|
||||
#endif
|
||||
}};
|
||||
|
||||
const struct usb_config_descriptor config = {
|
||||
.bLength = USB_DT_CONFIGURATION_SIZE,
|
||||
.bDescriptorType = USB_DT_CONFIGURATION,
|
||||
.wTotalLength = 0,
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
.bNumInterfaces = 2,
|
||||
#else
|
||||
.bNumInterfaces = 1,
|
||||
#endif
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 0,
|
||||
.bmAttributes = 0xC0,
|
||||
.bMaxPower = 0x32,
|
||||
|
||||
.interface = ifaces,
|
||||
};
|
||||
|
||||
static const char *usb_strings[] = {
|
||||
"Black Sphere Technologies",
|
||||
"HID Demo",
|
||||
"DEMO",
|
||||
};
|
||||
|
||||
static int hid_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len,
|
||||
void (**complete)(usbd_device *dev, struct usb_setup_data *req))
|
||||
{
|
||||
(void)complete;
|
||||
(void)dev;
|
||||
|
||||
if((req->bmRequestType != 0x81) ||
|
||||
(req->bRequest != USB_REQ_GET_DESCRIPTOR) ||
|
||||
(req->wValue != 0x2200))
|
||||
return 0;
|
||||
|
||||
/* Handle the HID report descriptor. */
|
||||
*buf = (u8 *)hid_report_descriptor;
|
||||
*len = sizeof(hid_report_descriptor);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
|
||||
{
|
||||
(void)req;
|
||||
(void)dev;
|
||||
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15);
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
|
||||
gpio_set(GPIOA, GPIO10);
|
||||
scb_reset_core();
|
||||
}
|
||||
|
||||
static int dfu_control_request(usbd_device *dev, struct usb_setup_data *req, u8 **buf, u16 *len,
|
||||
void (**complete)(usbd_device *dev, struct usb_setup_data *req))
|
||||
{
|
||||
(void)buf;
|
||||
(void)len;
|
||||
(void)dev;
|
||||
|
||||
if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH))
|
||||
return 0; /* Only accept class request. */
|
||||
|
||||
*complete = dfu_detach_complete;
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void hid_set_config(usbd_device *dev, u16 wValue)
|
||||
{
|
||||
(void)wValue;
|
||||
|
||||
usbd_ep_setup(dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL);
|
||||
|
||||
usbd_register_control_callback(
|
||||
dev,
|
||||
USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE,
|
||||
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
|
||||
hid_control_request);
|
||||
#ifdef INCLUDE_DFU_INTERFACE
|
||||
usbd_register_control_callback(
|
||||
dev,
|
||||
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
|
||||
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
|
||||
dfu_control_request);
|
||||
#endif
|
||||
|
||||
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
|
||||
/* SysTick interrupt every N clock pulses: set reload to N-1 */
|
||||
systick_set_reload(99999);
|
||||
systick_interrupt_enable();
|
||||
systick_counter_enable();
|
||||
}
|
||||
|
||||
static u8 spi_readwrite(u32 spi, u8 data)
|
||||
{
|
||||
while (SPI_SR(spi) & SPI_SR_BSY)
|
||||
;
|
||||
SPI_DR(spi) = data;
|
||||
while (!(SPI_SR(spi) & SPI_SR_RXNE))
|
||||
;
|
||||
return SPI_DR(spi);
|
||||
}
|
||||
|
||||
static u8 accel_read(u8 addr)
|
||||
{
|
||||
u8 ret;
|
||||
gpio_clear(GPIOB, GPIO12);
|
||||
spi_readwrite(SPI2, addr | 0x80);
|
||||
ret = spi_readwrite(SPI2, 0);
|
||||
gpio_set(GPIOB, GPIO12);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void accel_write(u8 addr, u8 data)
|
||||
{
|
||||
gpio_clear(GPIOB, GPIO12);
|
||||
spi_readwrite(SPI2, addr);
|
||||
spi_readwrite(SPI2, data);
|
||||
gpio_set(GPIOB, GPIO12);
|
||||
}
|
||||
|
||||
static void accel_get(s16 *x, s16 *y, s16 *z)
|
||||
{
|
||||
if (x)
|
||||
*x = accel_read(ADXL345_DATAX0) |
|
||||
(accel_read(ADXL345_DATAX1) << 8);
|
||||
if (y)
|
||||
*y = accel_read(ADXL345_DATAY0) |
|
||||
(accel_read(ADXL345_DATAY1) << 8);
|
||||
if (z)
|
||||
*z = accel_read(ADXL345_DATAZ0) |
|
||||
(accel_read(ADXL345_DATAZ1) << 8);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_SPI2EN);
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
|
||||
|
||||
/* Configure SPI2: PB13(SCK), PB14(MISO), PB15(MOSI). */
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_10_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
|
||||
GPIO_SPI2_SCK | GPIO_SPI2_MOSI);
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
|
||||
GPIO_SPI2_MISO);
|
||||
/* Enable CS pin on PB12. */
|
||||
gpio_set(GPIOB, GPIO12);
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_10_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
|
||||
|
||||
/* Force to SPI mode. This should be default after reset! */
|
||||
SPI2_I2SCFGR = 0;
|
||||
spi_init_master(SPI2,
|
||||
SPI_CR1_BAUDRATE_FPCLK_DIV_256,
|
||||
SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2,
|
||||
SPI_CR1_DFF_8BIT,
|
||||
SPI_CR1_MSBFIRST);
|
||||
/* Ignore the stupid NSS pin. */
|
||||
spi_enable_software_slave_management(SPI2);
|
||||
spi_set_nss_high(SPI2);
|
||||
spi_enable(SPI2);
|
||||
|
||||
(void)accel_read(ADXL345_DEVID);
|
||||
accel_write(ADXL345_POWER_CTL, ADXL345_POWER_CTL_MEASURE);
|
||||
accel_write(ADXL345_DATA_FORMAT, ADXL345_DATA_FORMAT_LALIGN);
|
||||
|
||||
/* USB_DETECT as input. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8);
|
||||
|
||||
/* Green LED off, as output. */
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
|
||||
usbd_dev = usbd_init(&stm32f107_usb_driver, &dev_descr, &config, usb_strings, 3);
|
||||
usbd_register_set_config_callback(usbd_dev, hid_set_config);
|
||||
|
||||
/* Delay some seconds to show that pull-up switch works. */
|
||||
for (i = 0; i < 0x800000; i++)
|
||||
__asm__("nop");
|
||||
|
||||
/* Wait for USB Vbus. */
|
||||
while (gpio_get(GPIOA, GPIO8) == 0)
|
||||
__asm__("nop");
|
||||
|
||||
/* Green LED on, connect USB. */
|
||||
gpio_clear(GPIOC, GPIO2);
|
||||
// OTG_FS_GCCFG &= ~OTG_FS_GCCFG_VBUSBSEN;
|
||||
|
||||
while (1)
|
||||
usbd_poll(usbd_dev);
|
||||
}
|
||||
|
||||
void sys_tick_handler(void)
|
||||
{
|
||||
s16 x, y;
|
||||
u8 buf[4] = {0, 0, 0, 0};
|
||||
|
||||
accel_get(&x, &y, NULL);
|
||||
buf[1] = x >> 9;
|
||||
buf[2] = y >> 9;
|
||||
|
||||
usbd_ep_write_packet(usbd_dev, 0x81, buf, 4);
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = adc_injec
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is a simple polling example that sends the value read out from the
|
||||
temperature sensor ADC channel of the STM32 to the USART2.
|
||||
|
||||
This example polls injected channels.
|
||||
|
||||
The terminal settings for the receiving device/PC are 115200 8n1.
|
||||
|
|
@ -1,174 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2012 Stephen Dwyer <dwyer.sc@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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/adc.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 115200);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Setup the LEDs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
}
|
||||
|
||||
static void adc_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
|
||||
|
||||
/* Make sure the ADC doesn't run during config. */
|
||||
adc_off(ADC1);
|
||||
|
||||
/* We configure everything for one single injected conversion. */
|
||||
adc_disable_scan_mode(ADC1);
|
||||
adc_set_single_conversion_mode(ADC1);
|
||||
/* We can only use discontinuous mode on either the regular OR injected channels, not both */
|
||||
adc_disable_discontinuous_mode_regular(ADC1);
|
||||
adc_enable_discontinuous_mode_injected(ADC1);
|
||||
/* We want to start the injected conversion in software */
|
||||
adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_JSWSTART);
|
||||
adc_set_right_aligned(ADC1);
|
||||
/* We want to read the temperature sensor, so we have to enable it. */
|
||||
adc_enable_temperature_sensor(ADC1);
|
||||
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
|
||||
|
||||
adc_power_on(ADC1);
|
||||
|
||||
/* Wait for ADC starting up. */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
adc_reset_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); //added this check
|
||||
adc_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); //added this check
|
||||
}
|
||||
|
||||
static void my_usart_print_int(u32 usart, int value)
|
||||
{
|
||||
s8 i;
|
||||
u8 nr_digits = 0;
|
||||
char buffer[25];
|
||||
|
||||
if (value < 0) {
|
||||
usart_send_blocking(usart, '-');
|
||||
value = value * -1;
|
||||
}
|
||||
|
||||
while (value > 0) {
|
||||
buffer[nr_digits++] = "0123456789"[value % 10];
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
for (i = (nr_digits - 1); i >= 0; i--) {
|
||||
usart_send_blocking(usart, buffer[i]);
|
||||
}
|
||||
|
||||
usart_send_blocking(usart, '\r');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
u8 channel_array[16];
|
||||
u16 temperature = 0;
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
adc_setup();
|
||||
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 on */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 on */
|
||||
|
||||
/* Send a message on USART1. */
|
||||
usart_send_blocking(USART2, 's');
|
||||
usart_send_blocking(USART2, 't');
|
||||
usart_send_blocking(USART2, 'm');
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
|
||||
/* Select the channel we want to convert. 16=temperature_sensor. */
|
||||
channel_array[0] = 16;
|
||||
/* Set the injected sequence here, with number of channels */
|
||||
adc_set_injected_sequence(ADC1, 1, channel_array);
|
||||
|
||||
/* Continously convert and poll the temperature ADC. */
|
||||
while (1) {
|
||||
/*
|
||||
* If the ADC_CR2_ON bit is already set -> setting it another time
|
||||
* starts a regular conversion. Injected conversion is started
|
||||
* explicitly with the JSWSTART bit as an external trigger. It may
|
||||
* also work by setting no regular channels and setting JAUTO to
|
||||
* automatically convert the injected channels after the regular
|
||||
* channels (of which there would be none). (Not tested.)
|
||||
*/
|
||||
adc_start_conversion_injected(ADC1);
|
||||
|
||||
/* Wait for end of conversion. */
|
||||
while (!(adc_eoc_injected(ADC1)));
|
||||
ADC_SR(ADC2) &= ~ADC_SR_JEOC; //clear injected end of conversion
|
||||
|
||||
temperature = adc_read_injected(ADC1,1); //get the result from ADC_JDR1 on ADC1 (only bottom 16bits)
|
||||
|
||||
/*
|
||||
* That's actually not the real temperature - you have to compute it
|
||||
* as described in the datasheet.
|
||||
*/
|
||||
my_usart_print_int(USART2, temperature);
|
||||
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED2 on */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = adc_injec_timtrig
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is a simple example that sends the value read out from the
|
||||
temperature sensor ADC channel of the STM32 to the USART2.
|
||||
|
||||
This example uses a timer trigger to automatically sample the adc channel.
|
||||
|
||||
The terminal settings for the receiving device/PC are 115200 8n1.
|
||||
|
|
@ -1,195 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2012 Stephen Dwyer <dwyer.sc@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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/adc.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/timer.h>
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 115200);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Setup the LEDs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
}
|
||||
|
||||
static void timer_setup(void)
|
||||
{
|
||||
/* Set up the timer TIM2 for injected sampling */
|
||||
uint32_t timer;
|
||||
volatile uint32_t *rcc_apbenr;
|
||||
uint32_t rcc_apb;
|
||||
|
||||
timer = TIM2;
|
||||
rcc_apbenr = &RCC_APB1ENR;
|
||||
rcc_apb = RCC_APB1ENR_TIM2EN;
|
||||
|
||||
rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb);
|
||||
|
||||
/* Time Base configuration */
|
||||
timer_reset(timer);
|
||||
timer_set_mode(timer, TIM_CR1_CKD_CK_INT,
|
||||
TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
||||
timer_set_period(timer, 0xFF);
|
||||
timer_set_prescaler(timer, 0x8);
|
||||
timer_set_clock_division(timer, 0x0);
|
||||
/* Generate TRGO on every update. */
|
||||
timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE);
|
||||
timer_enable_counter(timer);
|
||||
}
|
||||
|
||||
static void adc_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
|
||||
|
||||
/* Make sure the ADC doesn't run during config. */
|
||||
adc_off(ADC1);
|
||||
|
||||
/* We configure everything for one single timer triggered injected conversion. */
|
||||
adc_disable_scan_mode(ADC1);
|
||||
adc_set_single_conversion_mode(ADC1);
|
||||
/* We can only use discontinuous mode on either the regular OR injected channels, not both */
|
||||
adc_disable_discontinuous_mode_regular(ADC1);
|
||||
adc_enable_discontinuous_mode_injected(ADC1);
|
||||
/* We want to start the injected conversion with the TIM2 TRGO */
|
||||
adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO);
|
||||
adc_set_right_aligned(ADC1);
|
||||
/* We want to read the temperature sensor, so we have to enable it. */
|
||||
adc_enable_temperature_sensor(ADC1);
|
||||
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
|
||||
|
||||
adc_power_on(ADC1);
|
||||
|
||||
/* Wait for ADC starting up. */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
adc_reset_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0);
|
||||
adc_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0);
|
||||
}
|
||||
|
||||
static void my_usart_print_int(u32 usart, int value)
|
||||
{
|
||||
s8 i;
|
||||
u8 nr_digits = 0;
|
||||
char buffer[25];
|
||||
|
||||
if (value < 0) {
|
||||
usart_send_blocking(usart, '-');
|
||||
value = value * -1;
|
||||
}
|
||||
|
||||
while (value > 0) {
|
||||
buffer[nr_digits++] = "0123456789"[value % 10];
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
for (i = (nr_digits - 1); i >= 0; i--) {
|
||||
usart_send_blocking(usart, buffer[i]);
|
||||
}
|
||||
|
||||
usart_send_blocking(usart, '\r');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
u8 channel_array[16];
|
||||
u16 temperature = 0;
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
timer_setup();
|
||||
adc_setup();
|
||||
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 on */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 on */
|
||||
|
||||
/* Send a message on USART1. */
|
||||
usart_send_blocking(USART2, 's');
|
||||
usart_send_blocking(USART2, 't');
|
||||
usart_send_blocking(USART2, 'm');
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
|
||||
/* Select the channel we want to convert. 16=temperature_sensor. */
|
||||
channel_array[0] = 16;
|
||||
/* Set the injected sequence here, with number of channels */
|
||||
adc_set_injected_sequence(ADC1, 1, channel_array);
|
||||
|
||||
/* Continously convert and poll the temperature ADC. */
|
||||
while (1) {
|
||||
/*
|
||||
* Since the injected sampling is triggered by the timer, it gets
|
||||
* updated automatically, we just need to periodically read out the value.
|
||||
* It would be better to check if the JEOC bit is set, and clear it following
|
||||
* so that you do not read the same value twice, especially for a slower
|
||||
* sampling rate.
|
||||
*/
|
||||
|
||||
temperature = adc_read_injected(ADC1,1); //get the result from ADC_JDR1 on ADC1 (only bottom 16bits)
|
||||
|
||||
/*
|
||||
* That's actually not the real temperature - you have to compute it
|
||||
* as described in the datasheet.
|
||||
*/
|
||||
my_usart_print_int(USART2, temperature);
|
||||
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED2 on */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = adc_injec_timtrig_irq
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is a simple example that sends the value read out from the
|
||||
temperature sensor ADC channel of the STM32 to the USART2.
|
||||
|
||||
This example uses a timer trigger to sample an injected adc channel and
|
||||
then uses an interrupt routine to retrieve the sample from the data register.
|
||||
|
||||
The terminal settings for the receiving device/PC are 115200 8n1.
|
||||
|
|
@ -1,211 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2012 Stephen Dwyer <dwyer.sc@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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/adc.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/timer.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
|
||||
volatile u16 temperature = 0;
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 115200);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Setup the LEDs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
}
|
||||
|
||||
static void timer_setup(void)
|
||||
{
|
||||
/* Set up the timer TIM2 for injected sampling */
|
||||
uint32_t timer;
|
||||
volatile uint32_t *rcc_apbenr;
|
||||
uint32_t rcc_apb;
|
||||
|
||||
timer = TIM2;
|
||||
rcc_apbenr = &RCC_APB1ENR;
|
||||
rcc_apb = RCC_APB1ENR_TIM2EN;
|
||||
|
||||
rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb);
|
||||
|
||||
/* Time Base configuration */
|
||||
timer_reset(timer);
|
||||
timer_set_mode(timer, TIM_CR1_CKD_CK_INT,
|
||||
TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
||||
timer_set_period(timer, 0xFF);
|
||||
timer_set_prescaler(timer, 0x8);
|
||||
timer_set_clock_division(timer, 0x0);
|
||||
/* Generate TRGO on every update. */
|
||||
timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE);
|
||||
timer_enable_counter(timer);
|
||||
}
|
||||
|
||||
static void irq_setup(void)
|
||||
{
|
||||
/* Enable the adc1_2_isr() routine */
|
||||
nvic_set_priority(NVIC_ADC1_2_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_ADC1_2_IRQ);
|
||||
}
|
||||
|
||||
static void adc_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
|
||||
|
||||
/* Make sure the ADC doesn't run during config. */
|
||||
adc_off(ADC1);
|
||||
|
||||
/* We configure everything for one single timer triggered injected conversion with interrupt generation. */
|
||||
/* While not needed for a single channel, try out scan mode which does all channels in one sweep and
|
||||
* generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one.
|
||||
*/
|
||||
adc_enable_scan_mode(ADC1);
|
||||
adc_set_single_conversion_mode(ADC1);
|
||||
/* We want to start the injected conversion with the TIM2 TRGO */
|
||||
adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO);
|
||||
/* Generate the ADC1_2_IRQ */
|
||||
adc_enable_eoc_interrupt_injected(ADC1);
|
||||
adc_set_right_aligned(ADC1);
|
||||
/* We want to read the temperature sensor, so we have to enable it. */
|
||||
adc_enable_temperature_sensor(ADC1);
|
||||
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
|
||||
|
||||
adc_power_on(ADC1);
|
||||
|
||||
/* Wait for ADC starting up. */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
adc_reset_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0);
|
||||
adc_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0);
|
||||
}
|
||||
|
||||
static void my_usart_print_int(u32 usart, int value)
|
||||
{
|
||||
s8 i;
|
||||
u8 nr_digits = 0;
|
||||
char buffer[25];
|
||||
|
||||
if (value < 0) {
|
||||
usart_send_blocking(usart, '-');
|
||||
value = value * -1;
|
||||
}
|
||||
|
||||
while (value > 0) {
|
||||
buffer[nr_digits++] = "0123456789"[value % 10];
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
for (i = (nr_digits - 1); i >= 0; i--) {
|
||||
usart_send_blocking(usart, buffer[i]);
|
||||
}
|
||||
|
||||
usart_send_blocking(usart, '\r');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
u8 channel_array[16];
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
timer_setup();
|
||||
irq_setup();
|
||||
adc_setup();
|
||||
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 on */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 on */
|
||||
|
||||
/* Send a message on USART1. */
|
||||
usart_send_blocking(USART2, 's');
|
||||
usart_send_blocking(USART2, 't');
|
||||
usart_send_blocking(USART2, 'm');
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
|
||||
/* Select the channel we want to convert. 16=temperature_sensor. */
|
||||
channel_array[0] = 16;
|
||||
/* Set the injected sequence here, with number of channels */
|
||||
adc_set_injected_sequence(ADC1, 1, channel_array);
|
||||
|
||||
/* Continously convert and poll the temperature ADC. */
|
||||
while (1) {
|
||||
/*
|
||||
* Since sampling is triggered by the timer and copying the value
|
||||
* out of the data register is handled by the interrupt routine,
|
||||
* we just need to print the value and toggle the LED. It may be useful
|
||||
* to buffer the adc values in some cases.
|
||||
*/
|
||||
|
||||
/*
|
||||
* That's actually not the real temperature - you have to compute it
|
||||
* as described in the datasheet.
|
||||
*/
|
||||
my_usart_print_int(USART2, temperature);
|
||||
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED2 on */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void adc1_2_isr(void)
|
||||
{
|
||||
/* Clear Injected End Of Conversion (JEOC) */
|
||||
ADC_SR(ADC1) &= ~ADC_SR_JEOC;
|
||||
temperature = adc_read_injected(ADC1,1);
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = adc_injec_timtrig_irq_4ch
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is a simple example that sends the values read out from four ADC
|
||||
channels of the STM32 to the USART2.
|
||||
|
||||
This example uses a timer trigger to sample the injected adc channels and
|
||||
then uses an interrupt routine to retrieve the samples from the data registers.
|
||||
|
||||
The terminal settings for the receiving device/PC are 115200 8n1.
|
||||
|
|
@ -1,230 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2012 Stephen Dwyer <dwyer.sc@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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/adc.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/timer.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
|
||||
volatile u16 temperature = 0;
|
||||
volatile u16 v_refint = 0;
|
||||
volatile u16 lisam_adc1 = 0;
|
||||
volatile u16 lisam_adc2 = 0;
|
||||
u8 channel_array[4]; /* for injected sampling, 4 channels max, for regular, 16 max */
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 115200);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Setup the LEDs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
|
||||
/* Setup Lisa/M v2 ADC1,2 on ANALOG1 connector */
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, \
|
||||
GPIO3 | GPIO0 );
|
||||
}
|
||||
|
||||
static void timer_setup(void)
|
||||
{
|
||||
/* Set up the timer TIM2 for injected sampling */
|
||||
uint32_t timer;
|
||||
volatile uint32_t *rcc_apbenr;
|
||||
uint32_t rcc_apb;
|
||||
|
||||
timer = TIM2;
|
||||
rcc_apbenr = &RCC_APB1ENR;
|
||||
rcc_apb = RCC_APB1ENR_TIM2EN;
|
||||
|
||||
rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb);
|
||||
|
||||
/* Time Base configuration */
|
||||
timer_reset(timer);
|
||||
timer_set_mode(timer, TIM_CR1_CKD_CK_INT,
|
||||
TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
||||
timer_set_period(timer, 0xFF);
|
||||
timer_set_prescaler(timer, 0x8);
|
||||
timer_set_clock_division(timer, 0x0);
|
||||
/* Generate TRGO on every update. */
|
||||
timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE);
|
||||
timer_enable_counter(timer);
|
||||
}
|
||||
|
||||
static void irq_setup(void)
|
||||
{
|
||||
/* Enable the adc1_2_isr() routine */
|
||||
nvic_set_priority(NVIC_ADC1_2_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_ADC1_2_IRQ);
|
||||
}
|
||||
|
||||
static void adc_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
|
||||
|
||||
/* Make sure the ADC doesn't run during config. */
|
||||
adc_off(ADC1);
|
||||
|
||||
/* We configure everything for one single timer triggered injected conversion with interrupt generation. */
|
||||
/* While not needed for a single channel, try out scan mode which does all channels in one sweep and
|
||||
* generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one.
|
||||
*/
|
||||
adc_enable_scan_mode(ADC1);
|
||||
adc_set_single_conversion_mode(ADC1);
|
||||
/* We want to start the injected conversion with the TIM2 TRGO */
|
||||
adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO);
|
||||
/* Generate the ADC1_2_IRQ */
|
||||
adc_enable_eoc_interrupt_injected(ADC1);
|
||||
adc_set_right_aligned(ADC1);
|
||||
/* We want to read the temperature sensor, so we have to enable it. */
|
||||
adc_enable_temperature_sensor(ADC1);
|
||||
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
|
||||
|
||||
/* Select the channels we want to convert.
|
||||
* 16=temperature_sensor, 17=Vrefint, 13=ADC1, 10=ADC2
|
||||
*/
|
||||
channel_array[0] = 16;
|
||||
channel_array[1] = 17;
|
||||
channel_array[2] = 13;
|
||||
channel_array[3] = 10;
|
||||
adc_set_injected_sequence(ADC1, 4, channel_array);
|
||||
|
||||
adc_power_on(ADC1);
|
||||
|
||||
/* Wait for ADC starting up. */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
adc_reset_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0); //added this check
|
||||
adc_calibration(ADC1);
|
||||
while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0); //added this check
|
||||
}
|
||||
|
||||
static void my_usart_print_int(u32 usart, int value)
|
||||
{
|
||||
s8 i;
|
||||
u8 nr_digits = 0;
|
||||
char buffer[25];
|
||||
|
||||
if (value < 0) {
|
||||
usart_send_blocking(usart, '-');
|
||||
value = value * -1;
|
||||
}
|
||||
|
||||
while (value > 0) {
|
||||
buffer[nr_digits++] = "0123456789"[value % 10];
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
for (i = (nr_digits - 1); i >= 0; i--) {
|
||||
usart_send_blocking(usart, buffer[i]);
|
||||
}
|
||||
|
||||
//usart_send_blocking(usart, '\r');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
timer_setup();
|
||||
irq_setup();
|
||||
adc_setup();
|
||||
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 off */
|
||||
gpio_set(GPIOC, GPIO15); /* LED5 off */
|
||||
|
||||
/* Send a message on USART1. */
|
||||
usart_send_blocking(USART2, 's');
|
||||
usart_send_blocking(USART2, 't');
|
||||
usart_send_blocking(USART2, 'm');
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
|
||||
/* Moved the channel selection and sequence init to adc_setup() */
|
||||
|
||||
/* Continously convert and poll the temperature ADC. */
|
||||
while (1) {
|
||||
/*
|
||||
* Since sampling is triggered by the timer and copying the values
|
||||
* out of the data registers is handled by the interrupt routine,
|
||||
* we just need to print the values and toggle the LED. It may be useful
|
||||
* to buffer the adc values in some cases.
|
||||
*/
|
||||
|
||||
my_usart_print_int(USART2, temperature);
|
||||
usart_send_blocking(USART2, ' ');
|
||||
my_usart_print_int(USART2, v_refint);
|
||||
usart_send_blocking(USART2, ' ');
|
||||
my_usart_print_int(USART2, lisam_adc1);
|
||||
usart_send_blocking(USART2, ' ');
|
||||
my_usart_print_int(USART2, lisam_adc2);
|
||||
usart_send_blocking(USART2, '\r');
|
||||
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED2 on */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void adc1_2_isr(void)
|
||||
{
|
||||
/* Clear Injected End Of Conversion (JEOC) */
|
||||
ADC_SR(ADC1) &= ~ADC_SR_JEOC;
|
||||
temperature = adc_read_injected(ADC1,1);
|
||||
v_refint = adc_read_injected(ADC1,2);
|
||||
lisam_adc1 = adc_read_injected(ADC1,3);
|
||||
lisam_adc2 = adc_read_injected(ADC1,4);
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = adc
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This is a simple polling example that sends the value read out from the
|
||||
temperature sensor ADC channel of the STM32 to the USART2.
|
||||
|
||||
The terminal settings for the receiving device/PC are 115200 8n1.
|
||||
|
|
@ -1,161 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
|
||||
* Copyright (C) 2012 Ken Sarkies <ksarkies@internode.on.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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/f1/adc.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 115200);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable GPIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Setup the LEDs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
}
|
||||
|
||||
static void adc_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
|
||||
|
||||
/* Make sure the ADC doesn't run during config. */
|
||||
adc_off(ADC1);
|
||||
|
||||
/* We configure everything for one single conversion. */
|
||||
adc_disable_scan_mode(ADC1);
|
||||
adc_set_single_conversion_mode(ADC1);
|
||||
adc_disable_external_trigger_regular(ADC1);
|
||||
adc_set_right_aligned(ADC1);
|
||||
/* We want to read the temperature sensor, so we have to enable it. */
|
||||
adc_enable_temperature_sensor(ADC1);
|
||||
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
|
||||
|
||||
adc_power_on(ADC1);
|
||||
|
||||
/* Wait for ADC starting up. */
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
|
||||
adc_reset_calibration(ADC1);
|
||||
adc_calibration(ADC1);
|
||||
}
|
||||
|
||||
static void my_usart_print_int(u32 usart, int value)
|
||||
{
|
||||
s8 i;
|
||||
u8 nr_digits = 0;
|
||||
char buffer[25];
|
||||
|
||||
if (value < 0) {
|
||||
usart_send_blocking(usart, '-');
|
||||
value = value * -1;
|
||||
}
|
||||
|
||||
while (value > 0) {
|
||||
buffer[nr_digits++] = "0123456789"[value % 10];
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
for (i = nr_digits; i >= 0; i--) {
|
||||
usart_send_blocking(usart, buffer[i]);
|
||||
}
|
||||
|
||||
usart_send_blocking(usart, '\r');
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
u8 channel_array[16];
|
||||
u16 temperature = 0;
|
||||
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
adc_setup();
|
||||
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 on */
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 off */
|
||||
|
||||
/* Send a message on USART1. */
|
||||
usart_send_blocking(USART2, 's');
|
||||
usart_send_blocking(USART2, 't');
|
||||
usart_send_blocking(USART2, 'm');
|
||||
usart_send_blocking(USART2, '\r');
|
||||
usart_send_blocking(USART2, '\n');
|
||||
|
||||
/* Select the channel we want to convert. 16=temperature_sensor. */
|
||||
channel_array[0] = 16;
|
||||
adc_set_regular_sequence(ADC1, 1, channel_array);
|
||||
|
||||
/* Continously convert and poll the temperature ADC. */
|
||||
while (1) {
|
||||
/*
|
||||
* Start the conversion directly (ie without a trigger).
|
||||
*/
|
||||
adc_start_conversion_direct(ADC1);
|
||||
|
||||
/* Wait for end of conversion. */
|
||||
while (!(adc_eoc(ADC1)));
|
||||
|
||||
temperature = adc_read_regular(ADC1);
|
||||
|
||||
/*
|
||||
* That's actually not the real temperature - you have to compute it
|
||||
* as described in the datasheet.
|
||||
*/
|
||||
my_usart_print_int(USART2, temperature);
|
||||
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED2 on */
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = can
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
This test sets up the CAN interface on Lisa/M and transmits 8 bites every
|
||||
100ms. The first byte is being incremented in each cycle. The demo also
|
||||
receives messages and is displaing the first 4 bits of the first byte on the
|
||||
board LEDs.
|
|
@ -1,237 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
|
||||
* Copyright (C) 2010-2011 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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/flash.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/cm3/systick.h>
|
||||
#include <libopencm3/stm32/can.h>
|
||||
|
||||
struct can_tx_msg {
|
||||
u32 std_id;
|
||||
u32 ext_id;
|
||||
u8 ide;
|
||||
u8 rtr;
|
||||
u8 dlc;
|
||||
u8 data[8];
|
||||
};
|
||||
|
||||
struct can_rx_msg {
|
||||
u32 std_id;
|
||||
u32 ext_id;
|
||||
u8 ide;
|
||||
u8 rtr;
|
||||
u8 dlc;
|
||||
u8 data[8];
|
||||
u8 fmi;
|
||||
};
|
||||
|
||||
struct can_tx_msg can_tx_msg;
|
||||
struct can_rx_msg can_rx_msg;
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Enable Alternate Function clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
||||
|
||||
/* Enable GPIOA clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
|
||||
/* Enable GPIOB clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
|
||||
|
||||
/* Enable GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Preconfigure LEDs. */
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED2 off */
|
||||
gpio_set(GPIOC, GPIO2); /* LED3 off */
|
||||
gpio_set(GPIOC, GPIO5); /* LED4 off */
|
||||
gpio_set(GPIOC, GPIO15); /* LED5 off */
|
||||
|
||||
/* Configure LED GPIOOs. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
|
||||
/* Configure PB4 as GPIO. */
|
||||
AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;
|
||||
|
||||
}
|
||||
|
||||
static 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 interrupt every N clock pulses: set reload to N-1 */
|
||||
systick_set_reload(8999);
|
||||
|
||||
systick_interrupt_enable();
|
||||
|
||||
/* Start counting. */
|
||||
systick_counter_enable();
|
||||
}
|
||||
|
||||
static void can_setup(void)
|
||||
{
|
||||
/* Enable peripheral clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CAN1EN);
|
||||
|
||||
AFIO_MAPR |= AFIO_MAPR_CAN1_REMAP_PORTB;
|
||||
|
||||
/* Configure CAN pin: RX (input pull-up). */
|
||||
gpio_set_mode(GPIO_BANK_CAN1_PB_RX, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_CAN1_PB_RX);
|
||||
gpio_set(GPIO_BANK_CAN1_PB_RX, GPIO_CAN1_PB_RX);
|
||||
|
||||
/* Configure CAN pin: TX. */
|
||||
gpio_set_mode(GPIO_BANK_CAN1_PB_TX, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_PB_TX);
|
||||
|
||||
/* NVIC setup. */
|
||||
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
|
||||
nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 1);
|
||||
|
||||
/* Reset CAN. */
|
||||
can_reset(CAN1);
|
||||
|
||||
/* CAN cell init. */
|
||||
if (can_init(CAN1,
|
||||
false, /* TTCM: Time triggered comm mode? */
|
||||
true, /* ABOM: Automatic bus-off management? */
|
||||
false, /* AWUM: Automatic wakeup mode? */
|
||||
false, /* NART: No automatic retransmission? */
|
||||
false, /* RFLM: Receive FIFO locked mode? */
|
||||
false, /* TXFP: Transmit FIFO priority? */
|
||||
CAN_BTR_SJW_1TQ,
|
||||
CAN_BTR_TS1_3TQ,
|
||||
CAN_BTR_TS2_4TQ,
|
||||
12,
|
||||
false,
|
||||
false)) /* BRP+1: Baud rate prescaler */
|
||||
{
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED2 off */
|
||||
gpio_set(GPIOC, GPIO2); /* LED3 off */
|
||||
gpio_clear(GPIOC, GPIO5); /* LED4 on */
|
||||
gpio_set(GPIOC, GPIO15); /* LED5 off */
|
||||
|
||||
/* Die because we failed to initialize. */
|
||||
while (1)
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
/* CAN filter 0 init. */
|
||||
can_filter_id_mask_32bit_init(CAN1,
|
||||
0, /* Filter ID */
|
||||
0, /* CAN ID */
|
||||
0, /* CAN ID mask */
|
||||
0, /* FIFO assignment (here: FIFO0) */
|
||||
true); /* Enable the filter. */
|
||||
|
||||
/* Enable CAN RX interrupt. */
|
||||
can_enable_irq(CAN1, CAN_IER_FMPIE0);
|
||||
}
|
||||
|
||||
void sys_tick_handler(void)
|
||||
{
|
||||
static int temp32 = 0;
|
||||
static u8 data[8] = {0, 1, 2, 0, 0, 0, 0, 0};
|
||||
|
||||
/* We call this handler every 1ms so every 100ms = 0.1s
|
||||
* resulting in 100Hz message rate.
|
||||
*/
|
||||
if (++temp32 != 100)
|
||||
return;
|
||||
|
||||
temp32 = 0;
|
||||
|
||||
/* Transmit CAN frame. */
|
||||
data[0]++;
|
||||
if (can_transmit(CAN1,
|
||||
0, /* (EX/ST)ID: CAN ID */
|
||||
false, /* IDE: CAN ID extended? */
|
||||
false, /* RTR: Request transmit? */
|
||||
8, /* DLC: Data length */
|
||||
data) == -1)
|
||||
{
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 off */
|
||||
gpio_set(GPIOB, GPIO4); /* LED2 off */
|
||||
gpio_set(GPIOC, GPIO2); /* LED3 off */
|
||||
gpio_set(GPIOC, GPIO5); /* LED4 off */
|
||||
gpio_clear(GPIOC, GPIO15); /* LED5 on */
|
||||
}
|
||||
}
|
||||
|
||||
void usb_lp_can_rx0_isr(void)
|
||||
{
|
||||
u32 id, fmi;
|
||||
bool ext, rtr;
|
||||
u8 length, data[8];
|
||||
|
||||
can_receive(CAN1, 0, false, &id, &ext, &rtr, &fmi, &length, data);
|
||||
|
||||
if (data[0] & 1)
|
||||
gpio_clear(GPIOA, GPIO8);
|
||||
else
|
||||
gpio_set(GPIOA, GPIO8);
|
||||
|
||||
if (data[0] & 2)
|
||||
gpio_clear(GPIOB, GPIO4);
|
||||
else
|
||||
gpio_set(GPIOB, GPIO4);
|
||||
|
||||
if (data[0] & 4)
|
||||
gpio_clear(GPIOC, GPIO2);
|
||||
else
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
|
||||
if (data[0] & 8)
|
||||
gpio_clear(GPIOC, GPIO5);
|
||||
else
|
||||
gpio_set(GPIOC, GPIO5);
|
||||
|
||||
can_fifo_release(CAN1, 0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
gpio_setup();
|
||||
can_setup();
|
||||
systick_setup();
|
||||
|
||||
while (1); /* Halt. */
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,148 +0,0 @@
|
|||
/*
|
||||
* 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 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/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
|
||||
/* Set STM32 to 72 MHz. */
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC, and AFIO clocks. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||
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);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* LED1 */
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
|
||||
/* LED2 */
|
||||
/* Set GPIO15 (in GPIO port C) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||
|
||||
/* JTAG_TRST */
|
||||
/* Set GPIO4 (in GPIO port B) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
|
||||
|
||||
AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;
|
||||
|
||||
/* ADC4 */
|
||||
/* Set GPIO5 (in GPIO port C) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
|
||||
|
||||
/* ADC6 */
|
||||
/* Set GPIO2 (in GPIO port C) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||
|
||||
/* Preconfigure the LEDs. */
|
||||
gpio_set(GPIOA, GPIO8);
|
||||
gpio_set(GPIOC, GPIO15);
|
||||
gpio_set(GPIOB, GPIO4);
|
||||
gpio_set(GPIOC, GPIO5);
|
||||
gpio_set(GPIOC, GPIO2);
|
||||
}
|
||||
|
||||
static void led_set(int id, int on)
|
||||
{
|
||||
if (on) {
|
||||
switch (id) {
|
||||
case 0:
|
||||
gpio_clear(GPIOA, GPIO8); /* LED1 On */
|
||||
break;
|
||||
case 1:
|
||||
gpio_clear(GPIOB, GPIO4); /* JTAG_TRST On */
|
||||
break;
|
||||
case 2:
|
||||
gpio_clear(GPIOC, GPIO2); /* ADC6 On */
|
||||
break;
|
||||
case 3:
|
||||
gpio_clear(GPIOC, GPIO5); /* ADC4 On */
|
||||
break;
|
||||
case 4:
|
||||
gpio_clear(GPIOC, GPIO15); /* LED2 On */
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (id) {
|
||||
case 0:
|
||||
gpio_set(GPIOA, GPIO8); /* LED1 On */
|
||||
break;
|
||||
case 1:
|
||||
gpio_set(GPIOB, GPIO4); /* JTAG_TRST On */
|
||||
break;
|
||||
case 2:
|
||||
gpio_set(GPIOC, GPIO2); /* ADC6 On */
|
||||
break;
|
||||
case 3:
|
||||
gpio_set(GPIOC, GPIO5); /* ADC4 On */
|
||||
break;
|
||||
case 4:
|
||||
gpio_set(GPIOC, GPIO15); /* LED2 On */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void led_advance(void)
|
||||
{
|
||||
static int state = 0;
|
||||
|
||||
if (state < 5) {
|
||||
led_set(state, 1);
|
||||
} else if (state < 10) {
|
||||
led_set(state - 5, 0);
|
||||
} else if (state < 15) {
|
||||
led_set(14 - state, 1);
|
||||
} else if (state < 20) {
|
||||
led_set(19 - state, 0);
|
||||
}
|
||||
|
||||
state++;
|
||||
if(state == 20) state = 0;
|
||||
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
|
||||
/* Blink the LEDs (PC13 and PB4) on the board. */
|
||||
while (1) {
|
||||
led_advance();
|
||||
for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Linker script for Lisa-M (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_stm32f1.ld
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = spi
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program demonstrates simple SPI transceive on Lisa/M 2.0 board
|
||||
(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details).
|
||||
|
||||
The terminal settings for the receiving device/PC are 9600 8n1.
|
||||
|
||||
The example expects a loopback connection between the MISO and MOSI pins on
|
||||
SPI1.
|
||||
|
||||
A counter increments and the spi sends this byte out, after which it should
|
||||
come back in to the rx buffer, and the result is reported on the uart.
|
|
@ -1,164 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2013 Stephen Dwyer <scdwyer@ualberta.ca>
|
||||
*
|
||||
* 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/rcc.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/dma.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
int _write(int file, char *ptr, int len);
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
|
||||
RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
|
||||
RCC_APB2ENR_AFIOEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Enable SPI1 Periph and gpio clocks */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_SPI1EN);
|
||||
|
||||
}
|
||||
|
||||
static void spi_setup(void) {
|
||||
|
||||
/* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7 */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO4 |
|
||||
GPIO5 |
|
||||
GPIO7 );
|
||||
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
|
||||
GPIO6);
|
||||
|
||||
/* Reset SPI, SPI_CR1 register cleared, SPI is disabled */
|
||||
spi_reset(SPI1);
|
||||
|
||||
/* Set up SPI in Master mode with:
|
||||
* Clock baud rate: 1/64 of peripheral clock frequency
|
||||
* Clock polarity: Idle High
|
||||
* Clock phase: Data valid on 2nd clock pulse
|
||||
* Data frame format: 8-bit
|
||||
* Frame format: MSB First
|
||||
*/
|
||||
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
|
||||
|
||||
/*
|
||||
* Set NSS management to software.
|
||||
*
|
||||
* Note:
|
||||
* Setting nss high is very important, even if we are controlling the GPIO
|
||||
* ourselves this bit needs to be at least set to 1, otherwise the spi
|
||||
* peripheral will not send any data out.
|
||||
*/
|
||||
spi_enable_software_slave_management(SPI1);
|
||||
spi_set_nss_high(SPI1);
|
||||
|
||||
/* Enable SPI1 periph. */
|
||||
spi_enable(SPI1);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 9600);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (file == 1) {
|
||||
for (i = 0; i < len; i++)
|
||||
usart_send_blocking(USART2, ptr[i]);
|
||||
return i;
|
||||
}
|
||||
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int counter = 0;
|
||||
u16 rx_value = 0x42;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
spi_setup();
|
||||
|
||||
|
||||
/* Blink the LED (PA8) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
/* LED on/off */
|
||||
gpio_toggle(GPIOA, GPIO8);
|
||||
|
||||
/* printf the value that SPI should send */
|
||||
printf("Counter: %i SPI Sent Byte: %i", counter, (uint8_t) counter);
|
||||
/* blocking send of the byte out SPI1 */
|
||||
spi_send(SPI1, (uint8_t) counter);
|
||||
/* Read the byte that just came in (use a loopback between MISO and MOSI
|
||||
* to get the same byte back)
|
||||
*/
|
||||
rx_value = spi_read(SPI1);
|
||||
/* printf the byte just received */
|
||||
printf(" SPI Received Byte: %i\r\n", rx_value);
|
||||
|
||||
counter++;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = spi_dma
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program demonstrates SPI transceive with DMA on Lisa/M 2.0 board
|
||||
(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details).
|
||||
|
||||
The terminal settings for the receiving device/PC are 9600 8n1.
|
||||
|
||||
The example expects a loopback connection between the MISO and MOSI pins on
|
||||
SPI1. The DRDY and SS pins (on the Lisa/M v2.0 SPI1 connector) are used as
|
||||
GPIO to time the tx and rx ISRs, respectively. Use a scope or logic analyzer.
|
||||
|
||||
The tx length is incremented, followed by the rx length, after which both tx
|
||||
and rx lengths are decremented together. The case where rx is longer than tx
|
||||
requires greater complexity to ensure all rx data is clocked in. See the adv
|
||||
example for this.
|
|
@ -1,439 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2013 Stephen Dwyer <scdwyer@ualberta.ca>
|
||||
*
|
||||
* 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/rcc.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/dma.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef USE_16BIT_TRANSFERS
|
||||
#define USE_16BIT_TRANSFERS 1
|
||||
#endif
|
||||
|
||||
/* This is for the counter state flag */
|
||||
typedef enum {
|
||||
TX_UP_RX_HOLD = 0,
|
||||
TX_HOLD_RX_UP,
|
||||
TX_DOWN_RX_DOWN
|
||||
} cnt_state;
|
||||
|
||||
/* This is a global spi state flag */
|
||||
typedef enum {
|
||||
NONE = 0,
|
||||
ONE,
|
||||
DONE
|
||||
} trans_status;
|
||||
|
||||
volatile trans_status transceive_status;
|
||||
|
||||
int _write(int file, char *ptr, int len);
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
|
||||
RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
|
||||
RCC_APB2ENR_AFIOEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Enable SPI1 Periph and gpio clocks */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_SPI1EN);
|
||||
|
||||
/* Enable DMA1 clock */
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN);
|
||||
}
|
||||
|
||||
static void spi_setup(void) {
|
||||
|
||||
/* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7
|
||||
* For now ignore the SS pin so we can use it to time the ISRs
|
||||
*/
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, /* GPIO4 | */
|
||||
GPIO5 |
|
||||
GPIO7 );
|
||||
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
|
||||
GPIO6);
|
||||
|
||||
/* Reset SPI, SPI_CR1 register cleared, SPI is disabled */
|
||||
spi_reset(SPI1);
|
||||
|
||||
/* Explicitly disable I2S in favour of SPI operation */
|
||||
SPI1_I2SCFGR = 0;
|
||||
|
||||
/* Set up SPI in Master mode with:
|
||||
* Clock baud rate: 1/64 of peripheral clock frequency
|
||||
* Clock polarity: Idle High
|
||||
* Clock phase: Data valid on 2nd clock pulse
|
||||
* Data frame format: 8-bit or 16-bit
|
||||
* Frame format: MSB First
|
||||
*/
|
||||
#if USE_16BIT_TRANSFERS
|
||||
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST);
|
||||
#else
|
||||
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Set NSS management to software.
|
||||
*
|
||||
* Note:
|
||||
* Setting nss high is very important, even if we are controlling the GPIO
|
||||
* ourselves this bit needs to be at least set to 1, otherwise the spi
|
||||
* peripheral will not send any data out.
|
||||
*/
|
||||
spi_enable_software_slave_management(SPI1);
|
||||
spi_set_nss_high(SPI1);
|
||||
|
||||
/* Enable SPI1 periph. */
|
||||
spi_enable(SPI1);
|
||||
}
|
||||
|
||||
static void dma_int_enable(void) {
|
||||
/* SPI1 RX on DMA1 Channel 2 */
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL2_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL2_IRQ);
|
||||
/* SPI1 TX on DMA1 Channel 3 */
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL3_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL3_IRQ);
|
||||
}
|
||||
|
||||
/* Not used in this example
|
||||
static void dma_int_disable(void) {
|
||||
nvic_disable_irq(NVIC_DMA1_CHANNEL2_IRQ);
|
||||
nvic_disable_irq(NVIC_DMA1_CHANNEL3_IRQ);
|
||||
}
|
||||
*/
|
||||
|
||||
static void dma_setup(void)
|
||||
{
|
||||
dma_int_enable();
|
||||
}
|
||||
|
||||
#if USE_16BIT_TRANSFERS
|
||||
static int spi_dma_transceive(u16 *tx_buf, int tx_len, u16 *rx_buf, int rx_len)
|
||||
#else
|
||||
static int spi_dma_transceive(u8 *tx_buf, int tx_len, u8 *rx_buf, int rx_len)
|
||||
#endif
|
||||
{
|
||||
/* Check for 0 length in both tx and rx */
|
||||
if ((rx_len < 1) && (tx_len < 1)) {
|
||||
/* return -1 as error */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reset DMA channels*/
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL2);
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* Reset SPI data and status registers.
|
||||
* Here we assume that the SPI peripheral is NOT
|
||||
* busy any longer, i.e. the last activity was verified
|
||||
* complete elsewhere in the program.
|
||||
*/
|
||||
volatile u8 temp_data __attribute__ ((unused));
|
||||
while (SPI_SR(SPI1) & (SPI_SR_RXNE | SPI_SR_OVR)) {
|
||||
temp_data = SPI_DR(SPI1);
|
||||
}
|
||||
|
||||
/* Reset status flag appropriately (both 0 case caught above) */
|
||||
transceive_status = NONE;
|
||||
if (rx_len < 1) {
|
||||
transceive_status = ONE;
|
||||
}
|
||||
if (tx_len < 1) {
|
||||
transceive_status = ONE;
|
||||
}
|
||||
|
||||
/* Set up rx dma, note it has higher priority to avoid overrun */
|
||||
if (rx_len > 0) {
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL2, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL2, (u32)rx_buf);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL2, rx_len);
|
||||
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL2);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL2);
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL2, DMA_CCR_PL_VERY_HIGH);
|
||||
}
|
||||
|
||||
/* Set up tx dma */
|
||||
if (tx_len > 0) {
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)tx_buf);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL3, tx_len);
|
||||
dma_set_read_from_memory(DMA1, DMA_CHANNEL3);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL3);
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH);
|
||||
}
|
||||
|
||||
/* Enable dma transfer complete interrupts */
|
||||
if (rx_len > 0) {
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2);
|
||||
}
|
||||
if (tx_len > 0) {
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3);
|
||||
}
|
||||
|
||||
/* Activate dma channels */
|
||||
if (rx_len > 0) {
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL2);
|
||||
}
|
||||
if (tx_len > 0) {
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL3);
|
||||
}
|
||||
|
||||
/* Enable the spi transfer via dma
|
||||
* This will immediately start the transmission,
|
||||
* after which when the receive is complete, the
|
||||
* receive dma will activate
|
||||
*/
|
||||
if (rx_len > 0) {
|
||||
spi_enable_rx_dma(SPI1);
|
||||
}
|
||||
if (tx_len > 0) {
|
||||
spi_enable_tx_dma(SPI1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* SPI receive completed with DMA */
|
||||
void dma1_channel2_isr(void)
|
||||
{
|
||||
gpio_set(GPIOA,GPIO4);
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF2) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF2;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2);
|
||||
|
||||
spi_disable_rx_dma(SPI1);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL2);
|
||||
|
||||
/* Increment the status to indicate one of the transfers is complete */
|
||||
transceive_status++;
|
||||
gpio_clear(GPIOA,GPIO4);
|
||||
}
|
||||
|
||||
/* SPI transmit completed with DMA */
|
||||
void dma1_channel3_isr(void)
|
||||
{
|
||||
gpio_set(GPIOB,GPIO1);
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF3) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF3;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3);
|
||||
|
||||
spi_disable_tx_dma(SPI1);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* Increment the status to indicate one of the transfers is complete */
|
||||
transceive_status++;
|
||||
gpio_clear(GPIOB,GPIO1);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 9600);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (file == 1) {
|
||||
for (i = 0; i < len; i++)
|
||||
usart_send_blocking(USART2, ptr[i]);
|
||||
return i;
|
||||
}
|
||||
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
|
||||
/* Use the extra pins to signal when the ISRs are running */
|
||||
/* First, SPI1 - SS pin on Lisa/M v2.0 */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
|
||||
/* Then, SPI1 - DRDY pin on Lisa/M v2.0 */
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
|
||||
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int counter_tx = 0;
|
||||
int counter_rx = 0;
|
||||
|
||||
cnt_state counter_state = TX_UP_RX_HOLD;
|
||||
|
||||
int i = 0;
|
||||
|
||||
/* Transmit and Receive packets, set transmit to index and receive to known unused value to aid in debugging */
|
||||
#if USE_16BIT_TRANSFERS
|
||||
u16 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
u16 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42};
|
||||
#else
|
||||
u8 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
u8 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42};
|
||||
#endif
|
||||
|
||||
transceive_status = DONE;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
spi_setup();
|
||||
dma_setup();
|
||||
|
||||
#if USE_16BIT_TRANSFERS
|
||||
printf("SPI with DMA Transfer Test using 16bit option (Use loopback)\r\n\r\n");
|
||||
#else
|
||||
printf("SPI with DMA Transfer Test using 8bit option (Use loopback)\r\n\r\n");
|
||||
#endif
|
||||
|
||||
/* Blink the LED (PA8) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
/* LED on/off */
|
||||
gpio_toggle(GPIOA, GPIO8);
|
||||
|
||||
/* Print what is going to be sent on the SPI bus */
|
||||
printf("Sending packet (tx len %02i):", counter_tx);
|
||||
for (i = 0; i < counter_tx; i++)
|
||||
{
|
||||
printf(" 0x%02x,", tx_packet[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
/* Start a transceive */
|
||||
if (spi_dma_transceive(tx_packet, counter_tx, rx_packet, counter_rx)) {
|
||||
printf("Attempted 0 length tx and rx packets\r\n");
|
||||
}
|
||||
|
||||
/* Wait until transceive complete.
|
||||
* This checks the state flag as well as follows the
|
||||
* procedure on the Reference Manual (RM0008 rev 14
|
||||
* Section 25.3.9 page 692, the note.)
|
||||
*/
|
||||
while (transceive_status != DONE)
|
||||
;
|
||||
while (!(SPI_SR(SPI1) & SPI_SR_TXE))
|
||||
;
|
||||
while (SPI_SR(SPI1) & SPI_SR_BSY)
|
||||
;
|
||||
|
||||
/* Print what was received on the SPI bus */
|
||||
printf("Received Packet (rx len %02i):", counter_rx);
|
||||
for (i = 0; i < 16; i++) {
|
||||
printf(" 0x%02x,", rx_packet[i]);
|
||||
}
|
||||
printf("\r\n\r\n");
|
||||
|
||||
/* Update counters
|
||||
* If we use the loopback method, we can not
|
||||
* have a rx length longer than the tx length.
|
||||
* Testing rx lengths longer than tx lengths
|
||||
* requires an actual slave device that will
|
||||
* return data.
|
||||
*/
|
||||
switch (counter_state) {
|
||||
case TX_UP_RX_HOLD:
|
||||
counter_tx++;
|
||||
if (counter_tx > 15) {
|
||||
counter_state = TX_HOLD_RX_UP;
|
||||
}
|
||||
break;
|
||||
case TX_HOLD_RX_UP:
|
||||
counter_rx++;
|
||||
if (counter_rx > 15) {
|
||||
counter_state = TX_DOWN_RX_DOWN;
|
||||
}
|
||||
break;
|
||||
case TX_DOWN_RX_DOWN:
|
||||
counter_tx--;
|
||||
counter_rx--;
|
||||
if (counter_tx < 1) {
|
||||
counter_state = TX_UP_RX_HOLD;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
/* Reset receive buffer for consistency */
|
||||
for (i = 0; i < 16; i++) {
|
||||
rx_packet[i] = 0x42;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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 = spi_dma_adv
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program demonstrates SPI transceive with DMA on Lisa/M 2.0 board
|
||||
(http://paparazzi.enac.fr/wiki/Lisa/M_v20 for details).
|
||||
|
||||
The terminal settings for the receiving device/PC are 9600 8n1.
|
||||
|
||||
The example expects a loopback connection between the MISO and MOSI pins on
|
||||
SPI1. The DRDY and SS pins (on the Lisa/M v2.0 SPI1 connector) are used as
|
||||
GPIO to time the tx and rx ISRs, respectively. Use a scope or logic analyzer.
|
||||
|
||||
The tx length is incremented, followed by the rx length, after which the tx is
|
||||
decremented, then the rx is decremented. This is repeated in a loop. In this
|
||||
example, rx lengths longer than tx lengths are handled by using two dma transmits
|
||||
one after the other, handled inside the tx dma ISR.
|
|
@ -1,506 +0,0 @@
|
|||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2013 Stephen Dwyer <scdwyer@ualberta.ca>
|
||||
*
|
||||
* 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/rcc.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/dma.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef USE_16BIT_TRANSFERS
|
||||
#define USE_16BIT_TRANSFERS 0
|
||||
#endif
|
||||
|
||||
/* This is for the counter state flag */
|
||||
typedef enum {
|
||||
TX_UP_RX_HOLD = 0,
|
||||
TX_HOLD_RX_UP,
|
||||
TX_DOWN_RX_HOLD,
|
||||
TX_HOLD_RX_DOWN
|
||||
} cnt_state;
|
||||
|
||||
/* This is a global spi state flag */
|
||||
typedef enum {
|
||||
NONE = 0,
|
||||
ONE,
|
||||
DONE
|
||||
} trans_status;
|
||||
|
||||
volatile trans_status transceive_status;
|
||||
|
||||
/* Global for dummy tx dma transfer */
|
||||
int rx_buf_remainder = 0;
|
||||
|
||||
#if USE_16BIT_TRANSFERS
|
||||
u16 dummy_tx_buf = 0xdd;
|
||||
#else
|
||||
u8 dummy_tx_buf = 0xdd;
|
||||
#endif
|
||||
|
||||
int _write(int file, char *ptr, int len);
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
|
||||
RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
|
||||
RCC_APB2ENR_AFIOEN);
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Enable SPI1 Periph and gpio clocks */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_SPI1EN);
|
||||
|
||||
/* Enable DMA1 clock */
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN);
|
||||
}
|
||||
|
||||
static void spi_setup(void) {
|
||||
|
||||
/* Configure GPIOs: SS=PA4, SCK=PA5, MISO=PA6 and MOSI=PA7
|
||||
* For now ignore the SS pin so we can use it to time the ISRs
|
||||
*/
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, /* GPIO4 | */
|
||||
GPIO5 |
|
||||
GPIO7 );
|
||||
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
|
||||
GPIO6);
|
||||
|
||||
/* Reset SPI, SPI_CR1 register cleared, SPI is disabled */
|
||||
spi_reset(SPI1);
|
||||
|
||||
/* Explicitly disable I2S in favour of SPI operation */
|
||||
SPI1_I2SCFGR = 0;
|
||||
|
||||
/* Set up SPI in Master mode with:
|
||||
* Clock baud rate: 1/64 of peripheral clock frequency
|
||||
* Clock polarity: Idle High
|
||||
* Clock phase: Data valid on 2nd clock pulse
|
||||
* Data frame format: 8-bit or 16-bit
|
||||
* Frame format: MSB First
|
||||
*/
|
||||
#if USE_16BIT_TRANSFERS
|
||||
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_MSBFIRST);
|
||||
#else
|
||||
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
|
||||
SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Set NSS management to software.
|
||||
*
|
||||
* Note:
|
||||
* Setting nss high is very important, even if we are controlling the GPIO
|
||||
* ourselves this bit needs to be at least set to 1, otherwise the spi
|
||||
* peripheral will not send any data out.
|
||||
*/
|
||||
spi_enable_software_slave_management(SPI1);
|
||||
spi_set_nss_high(SPI1);
|
||||
|
||||
/* Enable SPI1 periph. */
|
||||
spi_enable(SPI1);
|
||||
}
|
||||
|
||||
static void dma_int_enable(void) {
|
||||
/* SPI1 RX on DMA1 Channel 2 */
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL2_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL2_IRQ);
|
||||
/* SPI1 TX on DMA1 Channel 3 */
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL3_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL3_IRQ);
|
||||
}
|
||||
|
||||
/* Not used in this example
|
||||
static void dma_int_disable(void) {
|
||||
nvic_disable_irq(NVIC_DMA1_CHANNEL2_IRQ);
|
||||
nvic_disable_irq(NVIC_DMA1_CHANNEL3_IRQ);
|
||||
}
|
||||
*/
|
||||
|
||||
static void dma_setup(void)
|
||||
{
|
||||
dma_int_enable();
|
||||
}
|
||||
|
||||
#if USE_16BIT_TRANSFERS
|
||||
static int spi_dma_transceive(u16 *tx_buf, int tx_len, u16 *rx_buf, int rx_len)
|
||||
#else
|
||||
static int spi_dma_transceive(u8 *tx_buf, int tx_len, u8 *rx_buf, int rx_len)
|
||||
#endif
|
||||
{
|
||||
|
||||
/* Check for 0 length in both tx and rx */
|
||||
if ((rx_len < 1) && (tx_len < 1)) {
|
||||
/* return -1 as error */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reset DMA channels*/
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL2);
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* Reset SPI data and status registers.
|
||||
* Here we assume that the SPI peripheral is NOT
|
||||
* busy any longer, i.e. the last activity was verified
|
||||
* complete elsewhere in the program.
|
||||
*/
|
||||
volatile u8 temp_data __attribute__ ((unused));
|
||||
while (SPI_SR(SPI1) & (SPI_SR_RXNE | SPI_SR_OVR)) {
|
||||
temp_data = SPI_DR(SPI1);
|
||||
}
|
||||
|
||||
/* Reset status flag appropriately (both 0 case caught above) */
|
||||
transceive_status = NONE;
|
||||
if (rx_len < 1) {
|
||||
transceive_status = ONE;
|
||||
}
|
||||
|
||||
/* Determine tx length case to change behaviour
|
||||
* If tx_len >= rx_len, then normal case, run both DMAs with normal settings
|
||||
* If rx_len == 0, just don't run the rx DMA at all
|
||||
* If tx_len == 0, use a dummy buf and set the tx dma to transfer the same
|
||||
* amount as the rx_len, to ensure everything is clocked in
|
||||
* If 0 < tx_len < rx_len, first do a normal case, then on the tx finished
|
||||
* interrupt, set up a new dummyy buf tx dma transfer for the remaining
|
||||
* required clock cycles (handled in tx dma complete interrupt)
|
||||
*/
|
||||
if ((tx_len > 0) && (tx_len < rx_len)) {
|
||||
rx_buf_remainder = rx_len - tx_len;
|
||||
}
|
||||
|
||||
/* Set up rx dma, note it has higher priority to avoid overrun */
|
||||
if (rx_len > 0) {
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL2, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL2, (u32)rx_buf);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL2, rx_len);
|
||||
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL2);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL2);
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL2, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL2, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL2, DMA_CCR_PL_VERY_HIGH);
|
||||
}
|
||||
|
||||
/* Set up tx dma (must always run tx to get clock signal) */
|
||||
if (tx_len > 0) {
|
||||
/* Here we have a regular tx transfer */
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)tx_buf);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL3, tx_len);
|
||||
dma_set_read_from_memory(DMA1, DMA_CHANNEL3);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL3);
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH);
|
||||
} else {
|
||||
/* Here we aren't transmitting any real data, use the dummy buffer
|
||||
* and set the length to the rx_len to get all rx data in, while
|
||||
* not incrementing the memory pointer
|
||||
*/
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)(&dummy_tx_buf)); // Change here
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL3, rx_len); // Change here
|
||||
dma_set_read_from_memory(DMA1, DMA_CHANNEL3);
|
||||
dma_disable_memory_increment_mode(DMA1, DMA_CHANNEL3); // Change here
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH);
|
||||
}
|
||||
|
||||
/* Enable dma transfer complete interrupts */
|
||||
if (rx_len > 0) {
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2);
|
||||
}
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* Activate dma channels */
|
||||
if (rx_len > 0) {
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL2);
|
||||
}
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* Enable the spi transfer via dma
|
||||
* This will immediately start the transmission,
|
||||
* after which when the receive is complete, the
|
||||
* receive dma will activate
|
||||
*/
|
||||
if (rx_len > 0) {
|
||||
spi_enable_rx_dma(SPI1);
|
||||
}
|
||||
spi_enable_tx_dma(SPI1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* SPI receive completed with DMA */
|
||||
void dma1_channel2_isr(void)
|
||||
{
|
||||
gpio_set(GPIOA,GPIO4);
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF2) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF2;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL2);
|
||||
|
||||
spi_disable_rx_dma(SPI1);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL2);
|
||||
|
||||
/* Increment the status to indicate one of the transfers is complete */
|
||||
transceive_status++;
|
||||
gpio_clear(GPIOA,GPIO4);
|
||||
}
|
||||
|
||||
/* SPI transmit completed with DMA */
|
||||
void dma1_channel3_isr(void)
|
||||
{
|
||||
gpio_set(GPIOB,GPIO1);
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF3) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF3;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3);
|
||||
|
||||
spi_disable_tx_dma(SPI1);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL3);
|
||||
|
||||
/* If tx_len < rx_len, create a dummy transfer to clock in the remaining
|
||||
* rx data
|
||||
*/
|
||||
if (rx_buf_remainder > 0) {
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL3);
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL3, (u32)&SPI1_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL3, (u32)(&dummy_tx_buf)); // Change here
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL3, rx_buf_remainder); // Change here
|
||||
dma_set_read_from_memory(DMA1, DMA_CHANNEL3);
|
||||
dma_disable_memory_increment_mode(DMA1, DMA_CHANNEL3); // Change here
|
||||
#if USE_16BIT_TRANSFERS
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_16BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_16BIT);
|
||||
#else
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL3, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL3, DMA_CCR_MSIZE_8BIT);
|
||||
#endif
|
||||
dma_set_priority(DMA1, DMA_CHANNEL3, DMA_CCR_PL_HIGH);
|
||||
|
||||
rx_buf_remainder = 0; // Clear the buffer remainder to disable this section later
|
||||
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL3);
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL3);
|
||||
spi_enable_tx_dma(SPI1);
|
||||
} else {
|
||||
/* Increment the status to indicate one of the transfers is complete */
|
||||
transceive_status++;
|
||||
}
|
||||
|
||||
gpio_clear(GPIOB,GPIO1);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
|
||||
|
||||
/* Setup UART parameters. */
|
||||
usart_set_baudrate(USART2, 9600);
|
||||
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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
}
|
||||
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (file == 1) {
|
||||
for (i = 0; i < len; i++)
|
||||
usart_send_blocking(USART2, ptr[i]);
|
||||
return i;
|
||||
}
|
||||
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
|
||||
/* Use the extra pins to signal when the ISRs are running */
|
||||
/* First, SPI1 - SS pin on Lisa/M v2.0 */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
|
||||
/* Then, SPI1 - DRDY pin on Lisa/M v2.0 */
|
||||
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
|
||||
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int counter_tx = 0;
|
||||
int counter_rx = 0;
|
||||
|
||||
cnt_state counter_state = TX_UP_RX_HOLD;
|
||||
|
||||
int i = 0;
|
||||
|
||||
/* Transmit and Receive packets, set transmit to index and receive to known unused value to aid in debugging */
|
||||
#if USE_16BIT_TRANSFERS
|
||||
u16 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
u16 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42};
|
||||
#else
|
||||
u8 tx_packet[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
u8 rx_packet[16] = {0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42};
|
||||
#endif
|
||||
|
||||
transceive_status = DONE;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
spi_setup();
|
||||
dma_setup();
|
||||
|
||||
printf("SPI with DMA Transfer Test (Use loopback)\r\n\r\n");
|
||||
|
||||
/* Blink the LED (PA8) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
/* LED on/off */
|
||||
gpio_toggle(GPIOA, GPIO8);
|
||||
|
||||
/* Print what is going to be sent on the SPI bus */
|
||||
printf("Sending packet (tx len %02i):", counter_tx);
|
||||
for (i = 0; i < counter_tx; i++)
|
||||
{
|
||||
printf(" 0x%02x,", tx_packet[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
/* Start a transceive */
|
||||
if (spi_dma_transceive(tx_packet, counter_tx, rx_packet, counter_rx)) {
|
||||
printf("Attempted 0 length tx and rx packets\r\n");
|
||||
}
|
||||
|
||||
/* Wait until transceive complete.
|
||||
* This checks the state flag as well as follows the
|
||||
* procedure on the Reference Manual (RM0008 rev 14
|
||||
* Section 25.3.9 page 692, the note.)
|
||||
*/
|
||||
while (transceive_status != DONE)
|
||||
;
|
||||
while (!(SPI_SR(SPI1) & SPI_SR_TXE))
|
||||
;
|
||||
while (SPI_SR(SPI1) & SPI_SR_BSY)
|
||||
;
|
||||
|
||||
/* Print what was received on the SPI bus */
|
||||
printf("Received Packet (rx len %02i):", counter_rx);
|
||||
for (i = 0; i < 16; i++) {
|
||||
printf(" 0x%02x,", rx_packet[i]);
|
||||
}
|
||||
printf("\r\n\r\n");
|
||||
|
||||
/* Update counters
|
||||
* If we use the loopback method, we can not
|
||||
* have a rx length longer than the tx length.
|
||||
* Testing rx lengths longer than tx lengths
|
||||
* requires an actual slave device that will
|
||||
* return data.
|
||||
*/
|
||||
switch (counter_state) {
|
||||
case TX_UP_RX_HOLD:
|
||||
counter_tx++;
|
||||
if (counter_tx > 15) {
|
||||
counter_state = TX_HOLD_RX_UP;
|
||||
}
|
||||
break;
|
||||
case TX_HOLD_RX_UP:
|
||||
counter_rx++;
|
||||
if (counter_rx > 15) {
|
||||
counter_state = TX_DOWN_RX_HOLD;
|
||||
}
|
||||
break;
|
||||
case TX_DOWN_RX_HOLD:
|
||||
counter_tx--;
|
||||
if (counter_tx < 1) {
|
||||
counter_state = TX_HOLD_RX_DOWN;
|
||||
}
|
||||
break;
|
||||
case TX_HOLD_RX_DOWN:
|
||||
counter_rx--;
|
||||
if (counter_rx < 1) {
|
||||
counter_state = TX_UP_RX_HOLD;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
/* Reset receive buffer for consistency */
|
||||
for (i = 0; i < 16; i++) {
|
||||
#if USE_16BIT_TRANSFERS
|
||||
tx_packet[i] = (u16)i;
|
||||
#else
|
||||
tx_packet[i] = (u8)i;
|
||||
#endif
|
||||
rx_packet[i] = 0x42;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
------------------------------------------------------------------------------
|
||||
README
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
This example program sends some characters on USART2 on Lisa/M 2.0 board
|
||||
(see http://paparazzi.enac.fr/wiki/LisaM for details).
|
||||
|
||||
The terminal settings for the receiving device/PC are 38400 8n1.
|
||||
|
||||
The sending is done in a blocking way in the code, see the usart_irq example
|
||||
for a more elaborate USART example.
|
||||
|
|
@ -1,86 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
|
||||
RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR,
|
||||
RCC_APB1ENR_USART2EN);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO_USART2_TX. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
|
||||
/* Setup UART 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)
|
||||
{
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i, j = 0, c = 0;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
/* Blink the LED (PA8) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
gpio_toggle(GPIOA, GPIO8); /* 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 < 800000; i++) /* Wait a bit. */
|
||||
__asm__("nop");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
##
|
||||
## 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_dma
|
||||
|
||||
# Comment the following line if you _don't_ have luftboot flashed!
|
||||
LDFLAGS += -Wl,-Ttext=0x8002000
|
||||
CFLAGS += -std=c99
|
||||
LDSCRIPT = ../lisa-m.ld
|
||||
|
||||
include ../../Makefile.include
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <libopencm3/stm32/f1/rcc.h>
|
||||
#include <libopencm3/stm32/f1/gpio.h>
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/f1/dma.h>
|
||||
#include <libopencm3/cm3/nvic.h>
|
||||
|
||||
static void clock_setup(void)
|
||||
{
|
||||
rcc_clock_setup_in_hse_12mhz_out_72mhz();
|
||||
|
||||
/* Enable GPIOA, GPIOB, GPIOC clock. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB2ENR,
|
||||
RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
|
||||
RCC_APB2ENR_IOPCEN);
|
||||
|
||||
/* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */
|
||||
rcc_peripheral_enable_clock(&RCC_APB1ENR,
|
||||
RCC_APB1ENR_USART2EN);
|
||||
|
||||
/* Enable DMA1 clock */
|
||||
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN);
|
||||
}
|
||||
|
||||
static void usart_setup(void)
|
||||
{
|
||||
/* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
|
||||
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
|
||||
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
|
||||
|
||||
/* Setup UART 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);
|
||||
|
||||
/* Finally enable the USART. */
|
||||
usart_enable(USART2);
|
||||
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL7_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL7_IRQ);
|
||||
|
||||
nvic_set_priority(NVIC_DMA1_CHANNEL6_IRQ, 0);
|
||||
nvic_enable_irq(NVIC_DMA1_CHANNEL6_IRQ);
|
||||
|
||||
}
|
||||
|
||||
static void dma_write(char *data, int size)
|
||||
{
|
||||
/*
|
||||
* Using channel 7 for USART2_TX
|
||||
*/
|
||||
|
||||
/* Reset DMA channel*/
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL7);
|
||||
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL7, (u32)&USART2_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL7, (u32)data);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL7, size);
|
||||
dma_set_read_from_memory(DMA1, DMA_CHANNEL7);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL7);
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL7, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL7, DMA_CCR_MSIZE_8BIT);
|
||||
dma_set_priority(DMA1, DMA_CHANNEL7, DMA_CCR_PL_VERY_HIGH);
|
||||
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7);
|
||||
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL7);
|
||||
|
||||
usart_enable_tx_dma(USART2);
|
||||
}
|
||||
|
||||
volatile int transfered = 0;
|
||||
|
||||
void dma1_channel7_isr(void)
|
||||
{
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF7) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF7;
|
||||
|
||||
transfered = 1;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7);
|
||||
|
||||
usart_disable_tx_dma(USART2);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL7);
|
||||
}
|
||||
|
||||
static void dma_read(char *data, int size)
|
||||
{
|
||||
/*
|
||||
* Using channel 6 for USART2_RX
|
||||
*/
|
||||
|
||||
/* Reset DMA channel*/
|
||||
dma_channel_reset(DMA1, DMA_CHANNEL6);
|
||||
|
||||
dma_set_peripheral_address(DMA1, DMA_CHANNEL6, (u32)&USART2_DR);
|
||||
dma_set_memory_address(DMA1, DMA_CHANNEL6, (u32)data);
|
||||
dma_set_number_of_data(DMA1, DMA_CHANNEL6, size);
|
||||
dma_set_read_from_peripheral(DMA1, DMA_CHANNEL6);
|
||||
dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL6);
|
||||
dma_set_peripheral_size(DMA1, DMA_CHANNEL6, DMA_CCR_PSIZE_8BIT);
|
||||
dma_set_memory_size(DMA1, DMA_CHANNEL6, DMA_CCR_MSIZE_8BIT);
|
||||
dma_set_priority(DMA1, DMA_CHANNEL6, DMA_CCR_PL_HIGH);
|
||||
|
||||
dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6);
|
||||
|
||||
dma_enable_channel(DMA1, DMA_CHANNEL6);
|
||||
|
||||
usart_enable_rx_dma(USART2);
|
||||
}
|
||||
|
||||
volatile int received = 0;
|
||||
|
||||
void dma1_channel6_isr(void)
|
||||
{
|
||||
if ((DMA1_ISR &DMA_ISR_TCIF6) != 0) {
|
||||
DMA1_IFCR |= DMA_IFCR_CTCIF6;
|
||||
|
||||
received = 1;
|
||||
}
|
||||
|
||||
dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6);
|
||||
|
||||
usart_disable_rx_dma(USART2);
|
||||
|
||||
dma_disable_channel(DMA1, DMA_CHANNEL6);
|
||||
}
|
||||
|
||||
static void gpio_setup(void)
|
||||
{
|
||||
/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
|
||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char tx[10] = "abcdefg\r\n";
|
||||
int tx_len = 10;
|
||||
char rx[7] = "bcdefg";
|
||||
int rx_len = 6;
|
||||
|
||||
clock_setup();
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
|
||||
transfered = 0;
|
||||
dma_write(tx, tx_len);
|
||||
received = 0;
|
||||
dma_read(rx, rx_len);
|
||||
|
||||
/* Blink the LED (PA8) on the board with every transmitted byte. */
|
||||
while (1) {
|
||||
gpio_toggle(GPIOA, GPIO8); /* LED on/off */
|
||||
while ( transfered != 1) {
|
||||
if (received == 1) {
|
||||
tx[1] = rx[0];
|
||||
tx[2] = rx[1];
|
||||
tx[3] = rx[2];
|
||||
tx[4] = rx[3];
|
||||
tx[5] = rx[4];
|
||||
tx[6] = rx[5];
|
||||
received = 0;
|
||||
dma_read(rx, rx_len);
|
||||
}
|
||||
}
|
||||
tx[0]++;
|
||||
if (tx[0] > 'z') tx[0] = 'a';
|
||||
transfered = 0;
|
||||
dma_write(tx, tx_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue