From 58d5de26f36a5dad2db8f954ed431eb3b838d3b6 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Sat, 5 Sep 2015 21:16:17 +0000 Subject: [PATCH] tests: gadget0: add stm32f103 target There's no F1 discovery style board with usb device, so this is for a "generic" device. The USB portion should be safe, but there's a led used for bootup that is board specific, and of course the clock source is board specific. Related, the openocd config file is rather custom to my own setup, but shows what you need to customize for your test environment. Further, as the F1 usb core doesn't include support for soft disconnect, use the very hacky method of dragging the pin low to force reenumeration on reset. Very very useful for development purposes! --- tests/gadget-zero/Makefile.stm32f103-generic | 42 +++++++++++ tests/gadget-zero/main-stm32f103-generic.c | 69 +++++++++++++++++++ .../gadget-zero/openocd.stm32f103-generic.cfg | 15 ++++ tests/gadget-zero/test_gadget0.py | 1 + 4 files changed, 127 insertions(+) create mode 100644 tests/gadget-zero/Makefile.stm32f103-generic create mode 100644 tests/gadget-zero/main-stm32f103-generic.c create mode 100644 tests/gadget-zero/openocd.stm32f103-generic.cfg diff --git a/tests/gadget-zero/Makefile.stm32f103-generic b/tests/gadget-zero/Makefile.stm32f103-generic new file mode 100644 index 00000000..6ef881f0 --- /dev/null +++ b/tests/gadget-zero/Makefile.stm32f103-generic @@ -0,0 +1,42 @@ +## +## This file is part of the libopencm3 project. +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BOARD = stm32f103-generic +PROJECT = usb-gadget0-$(BOARD) +BUILD_DIR = bin-$(BOARD) + +SHARED_DIR = ../shared + +CFILES = main-$(BOARD).c +CFILES += usb-gadget0.c trace.c trace_stdio.c + +VPATH += $(SHARED_DIR) + +INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) + +OPENCM3_DIR=../../ + +### This section can go to an arch shared rules eventually... +LDSCRIPT = ../../lib/stm32/f1/stm32f103x8.ld +OPENCM3_LIB = opencm3_stm32f1 +OPENCM3_DEFS = -DSTM32F1 +ARCH_FLAGS = -mthumb -mcpu=cortex-m3 +#OOCD_INTERFACE = jlink +#OOCD_TARGET = stm32f1x +OOCD_FILE = openocd.stm32f103-generic.cfg + +include ../rules.mk diff --git a/tests/gadget-zero/main-stm32f103-generic.c b/tests/gadget-zero/main-stm32f103-generic.c new file mode 100644 index 00000000..10bc2730 --- /dev/null +++ b/tests/gadget-zero/main-stm32f103-generic.c @@ -0,0 +1,69 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2015 Karl Palsson + * + * 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 . + */ + +#include +#include +#include + +#include +#include "usb-gadget0.h" + +#define ER_DEBUG +#ifdef ER_DEBUG +#define ER_DPRINTF(fmt, ...) \ + do { printf(fmt, ## __VA_ARGS__); } while (0) +#else +#define ER_DPRINTF(fmt, ...) \ + do { } while (0) +#endif + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + /* LED to indicate boot process */ + rcc_periph_clock_enable(RCC_GPIOC); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + gpio_set(GPIOC, GPIO13); + + rcc_periph_clock_enable(RCC_GPIOA); + /* + * Vile hack to reenumerate, physically _drag_ d+ low. + * do NOT do this if you're board has proper usb pull up control! + * (need at least 2.5us to trigger usb disconnect) + */ + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); + gpio_clear(GPIOA, GPIO11); + for (unsigned int i = 0; i < 800000; i++) + __asm__("nop"); + + rcc_periph_clock_enable(RCC_OTGFS); + + + usbd_device *usbd_dev = gadget0_init(&stm32f103_usb_driver, "stm32f103-generic"); + + ER_DPRINTF("bootup complete\n"); + gpio_clear(GPIOC, GPIO13); + while (1) { + usbd_poll(usbd_dev); + } + +} + diff --git a/tests/gadget-zero/openocd.stm32f103-generic.cfg b/tests/gadget-zero/openocd.stm32f103-generic.cfg new file mode 100644 index 00000000..4a0d6d37 --- /dev/null +++ b/tests/gadget-zero/openocd.stm32f103-generic.cfg @@ -0,0 +1,15 @@ +# Unfortunately, with no f103 disco, we're currently +# using a separate disco board +source [find interface/stlink-v2.cfg] +set WORKAREASIZE 0x2000 +source [find target/stm32f1x.cfg] + +# Serial of my l1 disco used as stlink here. +hla_serial "S?l\x06H?WQ%\x10\x18?" + +tpiu config internal swodump.stm32f103-generic.log uart off 72000000 + +# Uncomment to reset on connect, for grabbing under WFI et al +reset_config srst_only srst_nogate +# reset_config srst_only srst_nogate connect_assert_srst + diff --git a/tests/gadget-zero/test_gadget0.py b/tests/gadget-zero/test_gadget0.py index 09db7596..226ecb8b 100644 --- a/tests/gadget-zero/test_gadget0.py +++ b/tests/gadget-zero/test_gadget0.py @@ -7,6 +7,7 @@ import logging import unittest DUT_SERIAL = "stm32f4disco" +#DUT_SERIAL = "stm32f103-generic" class find_by_serial(object): def __init__(self, serial):