user button on maple mini can be used

This commit is contained in:
King Kévin 2016-01-29 11:25:30 +01:00
parent aa30d0db52
commit 5b4df1bc8a
2 changed files with 42 additions and 8 deletions

View File

@ -14,6 +14,10 @@
*/
/* Copyright (c) 2016 King Kévin <kingkevin@cuvoodoo.info> */
#include <libopencm3/stm32/gpio.h> // GPIO defines
#include <libopencm3/cm3/nvic.h> // interrupt defines
#include <libopencm3/stm32/exti.h> // external interrupt defines
/* get the length of an array */
#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
@ -22,17 +26,28 @@
/* LED pin */
#ifdef SYSTEM_BOARD
/* for system board LED is on pin 11/PA1 */
/* on system board LED is on pin 11/PA1 */
#define LED_RCC RCC_GPIOA
#define LED_PORT GPIOA
#define LED_PIN GPIO1
#elif MAPLE_MINI
/* for maple mini LED is on pin 19/PB1 */
/* on maple mini LED is on pin 19/PB1 */
#define LED_RCC RCC_GPIOB
#define LED_PORT GPIOB
#define LED_PIN GPIO1
#endif
/* user button */
#ifdef MAPLE_MINI
/* on maple mini button is on 32/PB8 */
#define BUTTON_RCC RCC_GPIOB
#define BUTTON_PORT GPIOB
#define BUTTON_PIN GPIO8
#define BUTTON_EXTI EXTI8
#define BUTTON_IRQ NVIC_EXTI9_5_IRQ
#define BUTTON_ISR exti9_5_isr
#endif
/* switch on LED */
void led_on(void);
/* switch off LED */

31
main.c
View File

@ -26,6 +26,8 @@
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/cm3/scb.h> // vector table definition
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/cm3/nvic.h> // interrupt utilities
#include <libopencm3/stm32/exti.h> // external interrupt utilities
/* own libraries */
#include "global.h" // board definitions
@ -83,17 +85,26 @@ int main(void)
SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
usart_setup(); // setup USART (for printing)
cdcacm_setup(); // setup USB CDC ACM (for printing)
setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print
setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print
// setup LED
rcc_periph_clock_enable(LED_RCC); //enable clock for LED
rcc_periph_clock_enable(LED_RCC); // enable clock for LED
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
led_off(); // switch off LED to indicate setup started
usart_setup(); // setup USART (for printing)
cdcacm_setup(); // setup USB CDC ACM (for printing)
setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print
setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print
// setup button
#if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ)
rcc_periph_clock_enable(BUTTON_RCC); // enable clock for button
gpio_set_mode(BUTTON_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BUTTON_PIN); // set button pin to input
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_BOTH); // trigger on both edge
exti_enable_request(BUTTON_EXTI); // enable external interrupt
nvic_enable_irq(BUTTON_IRQ); // enable interrupt
#endif
printf("welcome to the STM32F1 CuVoodoo example code\n"); // print welcome message
led_on(); // switch on LED to indicate setup completed
@ -113,3 +124,11 @@ int main(void)
return 0;
}
#if defined(BUTTON_ISR) && defined(BUTTON_EXTI)
void BUTTON_ISR(void)
{
exti_reset_request(BUTTON_EXTI); // reset interrupt
led_toggle(); // toggle LED to show the button press has been detected
}
#endif