From d97c1b04359e396d730c99f58ba2609444025ad9 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Mon, 1 May 2017 17:22:24 +0000 Subject: [PATCH] tests: gadget0: delay between calls to trigger races Attempt to be more brutal by delaying more often, instead of always promptly servicing the usb stack. This is implemented via using timer6 to do a known number of microseconds busy delay, and so only works on platforms that have reached at least core timer functionality, and provide the rcc_apb1_frequency variable. NOTE! This will _fail_ on devices using the st_usbfs drivers at present, but the code _should_ work, and the tests land to verify that the library fix, fixes the problem. (see subsequent commit) --- tests/gadget-zero/Makefile.stm32f072disco | 1 + tests/gadget-zero/Makefile.stm32f103-generic | 1 + tests/gadget-zero/Makefile.stm32f429i-disco | 1 + tests/gadget-zero/Makefile.stm32f4disco | 1 + tests/gadget-zero/Makefile.stm32l053disco | 1 + tests/gadget-zero/Makefile.stm32l1-generic | 1 + tests/gadget-zero/README.md | 5 ++ tests/gadget-zero/delay.c | 50 ++++++++++++++++++++ tests/gadget-zero/delay.h | 39 +++++++++++++++ tests/gadget-zero/main-stm32f072disco.c | 2 +- tests/gadget-zero/main-stm32f103-generic.c | 2 +- tests/gadget-zero/main-stm32f429i-disco.c | 2 +- tests/gadget-zero/main-stm32f4disco.c | 2 +- tests/gadget-zero/main-stm32l053disco.c | 2 +- tests/gadget-zero/main-stm32l1-generic.c | 2 +- tests/gadget-zero/usb-gadget0.c | 9 ++++ tests/gadget-zero/usb-gadget0.h | 7 +++ 17 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 tests/gadget-zero/delay.c create mode 100644 tests/gadget-zero/delay.h diff --git a/tests/gadget-zero/Makefile.stm32f072disco b/tests/gadget-zero/Makefile.stm32f072disco index b9f668bd..e155a523 100644 --- a/tests/gadget-zero/Makefile.stm32f072disco +++ b/tests/gadget-zero/Makefile.stm32f072disco @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/Makefile.stm32f103-generic b/tests/gadget-zero/Makefile.stm32f103-generic index 6ef881f0..a27dd607 100644 --- a/tests/gadget-zero/Makefile.stm32f103-generic +++ b/tests/gadget-zero/Makefile.stm32f103-generic @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c trace.c trace_stdio.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/Makefile.stm32f429i-disco b/tests/gadget-zero/Makefile.stm32f429i-disco index 4364778e..d1c432fd 100644 --- a/tests/gadget-zero/Makefile.stm32f429i-disco +++ b/tests/gadget-zero/Makefile.stm32f429i-disco @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c trace.c trace_stdio.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/Makefile.stm32f4disco b/tests/gadget-zero/Makefile.stm32f4disco index 23d777bb..ca737cb3 100644 --- a/tests/gadget-zero/Makefile.stm32f4disco +++ b/tests/gadget-zero/Makefile.stm32f4disco @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c trace.c trace_stdio.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/Makefile.stm32l053disco b/tests/gadget-zero/Makefile.stm32l053disco index 8af7f139..9c79127d 100644 --- a/tests/gadget-zero/Makefile.stm32l053disco +++ b/tests/gadget-zero/Makefile.stm32l053disco @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/Makefile.stm32l1-generic b/tests/gadget-zero/Makefile.stm32l1-generic index 7c0641d5..17496378 100644 --- a/tests/gadget-zero/Makefile.stm32l1-generic +++ b/tests/gadget-zero/Makefile.stm32l1-generic @@ -23,6 +23,7 @@ SHARED_DIR = ../shared CFILES = main-$(BOARD).c CFILES += usb-gadget0.c trace.c trace_stdio.c +CFILES += delay.c VPATH += $(SHARED_DIR) diff --git a/tests/gadget-zero/README.md b/tests/gadget-zero/README.md index b22cc627..de30b242 100644 --- a/tests/gadget-zero/README.md +++ b/tests/gadget-zero/README.md @@ -40,6 +40,11 @@ Ran 27 tests in 0.388s OK (skipped=2) ``` +To be even more brutal, run this in a shell loop. +``` +$ while true; do python test_gadget0.py stm32f072disco; done +``` + You can also run individual tests, or individual sets of tests, see the [unittest documentation](https://docs.python.org/3/library/unittest.html) for more information. Many development environments, such as [PyCharm](https://www.jetbrains.com/pycharm/) can diff --git a/tests/gadget-zero/delay.c b/tests/gadget-zero/delay.c new file mode 100644 index 00000000..616c4823 --- /dev/null +++ b/tests/gadget-zero/delay.c @@ -0,0 +1,50 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2017 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 . + */ + +/* + * This file implements some simple busy timers. They are designed to be + * portable, not performant. + * TIM6 is appropriated for usage. + */ +#include +#include +#include + +#include "delay.h" + +void delay_setup(void) +{ + /* set up a microsecond free running timer for ... things... */ + rcc_periph_clock_enable(RCC_TIM6); + /* microsecond counter */ + timer_set_prescaler(TIM6, rcc_apb1_frequency / 1e6 - 1); + timer_set_period(TIM6, 0xffff); + timer_one_shot_mode(TIM6); +} + +void delay_us(uint16_t us) +{ + TIM_ARR(TIM6) = us; + TIM_EGR(TIM6) = TIM_EGR_UG; + TIM_CR1(TIM6) |= TIM_CR1_CEN; + //timer_enable_counter(TIM6); + while (TIM_CR1(TIM6) & TIM_CR1_CEN); +} + + diff --git a/tests/gadget-zero/delay.h b/tests/gadget-zero/delay.h new file mode 100644 index 00000000..b0915547 --- /dev/null +++ b/tests/gadget-zero/delay.h @@ -0,0 +1,39 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2017 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 . + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * Initialize the timers used for delays. + */ + void delay_setup(void); + + /** + * busy wait for a number of usecs. + * @param us number of usecs to delay. + */ + void delay_us(uint16_t us); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/gadget-zero/main-stm32f072disco.c b/tests/gadget-zero/main-stm32f072disco.c index 02664668..4ee02b8d 100644 --- a/tests/gadget-zero/main-stm32f072disco.c +++ b/tests/gadget-zero/main-stm32f072disco.c @@ -59,7 +59,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); gpio_clear(GPIOC, GPIO7); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/main-stm32f103-generic.c b/tests/gadget-zero/main-stm32f103-generic.c index 3ebd5631..27148806 100644 --- a/tests/gadget-zero/main-stm32f103-generic.c +++ b/tests/gadget-zero/main-stm32f103-generic.c @@ -64,7 +64,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); gpio_clear(GPIOC, GPIO13); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/main-stm32f429i-disco.c b/tests/gadget-zero/main-stm32f429i-disco.c index 4b381c27..9109d927 100644 --- a/tests/gadget-zero/main-stm32f429i-disco.c +++ b/tests/gadget-zero/main-stm32f429i-disco.c @@ -52,7 +52,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/main-stm32f4disco.c b/tests/gadget-zero/main-stm32f4disco.c index 91aa7b77..aea0598c 100644 --- a/tests/gadget-zero/main-stm32f4disco.c +++ b/tests/gadget-zero/main-stm32f4disco.c @@ -52,7 +52,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/main-stm32l053disco.c b/tests/gadget-zero/main-stm32l053disco.c index 53046b5b..c90d739a 100644 --- a/tests/gadget-zero/main-stm32l053disco.c +++ b/tests/gadget-zero/main-stm32l053disco.c @@ -83,7 +83,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); gpio_clear(GPIOA, GPIO5); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/main-stm32l1-generic.c b/tests/gadget-zero/main-stm32l1-generic.c index 55b6286a..28d1c600 100644 --- a/tests/gadget-zero/main-stm32l1-generic.c +++ b/tests/gadget-zero/main-stm32l1-generic.c @@ -63,7 +63,7 @@ int main(void) ER_DPRINTF("bootup complete\n"); gpio_clear(GPIOB, GPIO1); while (1) { - usbd_poll(usbd_dev); + gadget0_run(usbd_dev); } } diff --git a/tests/gadget-zero/usb-gadget0.c b/tests/gadget-zero/usb-gadget0.c index b6649090..ad4d83b5 100644 --- a/tests/gadget-zero/usb-gadget0.c +++ b/tests/gadget-zero/usb-gadget0.c @@ -28,6 +28,7 @@ #include #include "trace.h" +#include "delay.h" #include "usb-gadget0.h" #define ER_DEBUG @@ -343,6 +344,14 @@ usbd_device *gadget0_init(const usbd_driver *driver, const char *userserial) usbd_control_buffer, sizeof(usbd_control_buffer)); usbd_register_set_config_callback(our_dev, gadget0_set_config); + delay_setup(); return our_dev; } + +void gadget0_run(usbd_device *usbd_dev) +{ + usbd_poll(usbd_dev); + /* This should be more than allowable! */ + delay_us(100); +} \ No newline at end of file diff --git a/tests/gadget-zero/usb-gadget0.h b/tests/gadget-zero/usb-gadget0.h index bb05bdf4..9ed332af 100644 --- a/tests/gadget-zero/usb-gadget0.h +++ b/tests/gadget-zero/usb-gadget0.h @@ -32,4 +32,11 @@ */ usbd_device *gadget0_init(const usbd_driver *driver, const char *userserial); +/** + * Call this forever. + * @param usbd_dev the object returned in _init. + * @sa gadget0_init + */ +void gadget0_run(usbd_device *usbd_dev); + #endif