gadget-chilipepper-plantmon.../button.c

68 lines
1.5 KiB
C
Raw Permalink Normal View History

2019-03-26 09:28:17 +01:00
/* (c) Daniele Lacamera 2019
* GPL
*/
#include <stdint.h>
#include <stdlib.h>
#include "system.h"
#include "button.h"
#include "unicore-mx/stm32/gpio.h"
#include "unicore-mx/stm32/exti.h"
#include "unicore-mx/stm32/rcc.h"
#include <unicore-mx/cm3/nvic.h>
volatile int button_press_pending = 0;
#define KEYA_PIN 13 // pc13
#define KEYB_PIN 2 // pb2
void button_setup(void)
{
int i;
uint32_t exti_irq;
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOC);
rcc_periph_clock_enable(RCC_AFIO);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, KEYB_PIN);
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, KEYA_PIN);
gpio_clear(GPIOB, KEYB_PIN);
gpio_clear(GPIOC, KEYA_PIN);
nvic_enable_irq(NVIC_EXTI15_10_IRQ);
nvic_enable_irq(NVIC_EXTI2_IRQ);
nvic_set_priority(NVIC_EXTI15_10_IRQ, 1);
nvic_set_priority(NVIC_EXTI2_IRQ, 1);
gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, KEYA_PIN);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, KEYB_PIN);
exti_select_source(KEYA_PIN, GPIOC);
exti_select_source(KEYB_PIN, GPIOB);
}
void button_start_read(void)
{
exti_set_trigger(KEYA_PIN, EXTI_TRIGGER_FALLING);
exti_set_trigger(KEYB_PIN, EXTI_TRIGGER_RISING);
exti_enable_request(KEYA_PIN);
}
void isr_exti15_10(void)
{
exti_reset_request(KEYA_PIN);
button_press_pending++;
}
void isr_exti2(void)
{
exti_reset_request(KEYB_PIN);
button_press_pending++;
}