From 58f7ce498dfe3f6c1312b28c6cb3d7d2604f6732 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Sun, 12 Apr 2020 22:45:39 +0200 Subject: [PATCH 1/3] Comment the setup a bit --- Makefile | 4 ++++ main.c | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/Makefile b/Makefile index 8c4e287..4def1c0 100644 --- a/Makefile +++ b/Makefile @@ -12,3 +12,7 @@ CPU_FLAGS := -mthumb -mcpu=cortex-m3 -msoft-float -mfix-cortex-m3-ldrd %.bin: %.elf arm-none-eabi-objcopy -Obinary $*.elf $*.bin +flash: main.bin + st-flash write /dev/stlinkv1_4 main.bin 0x08000000 + +.PHONY: flash diff --git a/main.c b/main.c index 83f03a4..00aa67a 100644 --- a/main.c +++ b/main.c @@ -18,10 +18,18 @@ #define STM32F1 1 // Needed for libopencm3 void setup (void) { // All the blackbox code + + // Set up clock rcc_clock_setup_in_hse_8mhz_out_24mhz(); + + // Enable the two GPIOs rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_GPIOA); + + // GPIO C8 amd C9 are the two LEDs gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8 | GPIO9); + + // GPIO A0 is the push button gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); } From dc0ff5562c15cc2ef1637681df99abfbb1ec6620 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Mon, 13 Apr 2020 02:43:47 +0200 Subject: [PATCH 2/3] Implement IRQ handling of the button --- main.c | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/main.c b/main.c index 00aa67a..122b650 100644 --- a/main.c +++ b/main.c @@ -11,12 +11,17 @@ #include #include +#include +#include #include //#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk) // Too long!! -#define DELAY 400000 // This is bad -- it's a value that was found to be OK +#define DELAY 4000000 // This is bad -- it's a value that was found to be OK #define STM32F1 1 // Needed for libopencm3 +enum {FORWARD, BACKWARD} direction; +bool running = false; + void setup (void) { // All the blackbox code // Set up clock @@ -26,44 +31,59 @@ void setup (void) { // All the blackbox code rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_GPIOA); + // Enable AFIO (we need that to map GPIO to EXTI) + rcc_periph_clock_enable(RCC_AFIO); + + // Enable EXTI0 interrupt. + // The interrupts are a bit magic, as the Internet says (the datasheet for F100RBT6B does not mention the EXTI/NVIC interface) + // This one is for EXTI0 ~~ GPIOx0 pins + nvic_enable_irq(NVIC_EXTI0_IRQ); + // GPIO C8 amd C9 are the two LEDs gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8 | GPIO9); // GPIO A0 is the push button + // And we set it as a regular button and enable its interrupts in the next block gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); + + // Configure the EXTI0 interrupts + // 1. Which pin + exti_select_source(EXTI0, GPIOA); + // 2. Which edge + exti_set_trigger(EXTI0, EXTI_TRIGGER_RISING); + // 3. Enable the interrupts + exti_enable_request(EXTI0); } -void initial_wait (void) { - gpio_clear(GPIOC, GPIO8 | GPIO9); - while (gpio_get(GPIOA, GPIO0) == 0); - return; +// libopencm3 has a pre-declared interrupt handlers, so it needs to have this name +void exti0_isr (void) { + // Clear the interrupt + exti_reset_request(EXTI0); + // NB: For some reason it is at the begining in the examples + // It needs to be here (possibly because of reordering, when this was at the end, I usually got the interrupt twice). + + if (!running) { + direction = FORWARD; + } else { + direction = direction == FORWARD ? BACKWARD : FORWARD; + } + running = true; } + int main (void) { setup(); - initial_wait(); - // Array of what leds should be on uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8}; - enum {FORWARD, BACKWARD} direction = FORWARD; - bool pressed = true; - bool change = false; char state = 0; int i; while (true) { - change = false; for (i=0 ; i Date: Mon, 13 Apr 2020 03:51:07 +0200 Subject: [PATCH 3/3] Reimplement the initial wait --- main.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 122b650..0452b18 100644 --- a/main.c +++ b/main.c @@ -19,8 +19,8 @@ #define DELAY 4000000 // This is bad -- it's a value that was found to be OK #define STM32F1 1 // Needed for libopencm3 -enum {FORWARD, BACKWARD} direction; -bool running = false; +volatile enum {FORWARD, BACKWARD} direction; +volatile bool running = false; void setup (void) { // All the blackbox code @@ -79,6 +79,9 @@ int main (void) { char state = 0; int i; + // Initial wait + while (!running) ; + while (true) { for (i=0 ; i