summaryrefslogtreecommitdiffstatshomepage
path: root/stm/usrsw.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm/usrsw.c')
-rw-r--r--stm/usrsw.c51
1 files changed, 38 insertions, 13 deletions
diff --git a/stm/usrsw.c b/stm/usrsw.c
index 385547843f..1feb271733 100644
--- a/stm/usrsw.c
+++ b/stm/usrsw.c
@@ -9,36 +9,50 @@
#include "obj.h"
#include "usrsw.h"
-#define PYB_USRSW_PORT (GPIOA)
-#define PYB_USRSW_PIN (GPIO_Pin_13)
-
+#if defined (PYBOARD)
+ #define USRSW_PORT (GPIOA)
+ #define USRSW_PIN (GPIO_Pin_13)
+ #define USRSW_EXTI_PIN (EXTI_PinSource13)
+ #define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
+ #define USRSW_EXTI_LINE (EXTI_Line13)
+ #define USRSW_EXTI_IRQN (EXTI15_10_IRQn)
+ #define USRSW_EXTI_EDGE (EXTI_Trigger_Rising)
+#elif defined (STM32F4DISC)
+ #define USRSW_PORT (GPIOA)
+ #define USRSW_PIN (GPIO_Pin_0)
+ #define USRSW_EXTI_PIN (EXTI_PinSource0)
+ #define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
+ #define USRSW_EXTI_LINE (EXTI_Line0)
+ #define USRSW_EXTI_IRQN (EXTI0_IRQn)
+ #define USRSW_EXTI_EDGE (EXTI_Trigger_Falling)
+#endif
void switch_init(void) {
// make it an input with pull-up
GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
+ GPIO_InitStructure.GPIO_Pin = USRSW_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* allow external pull up/down */
+ GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
// the rest does the EXTI interrupt
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- /* Connect EXTI Line13 to PA13 pin */
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource13);
+ /* Connect EXTI Line to GPIO pin */
+ SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
- /* Configure EXTI Line13, rising edge */
+ /* Configure EXTI Line */
EXTI_InitTypeDef EXTI_InitStructure;
- EXTI_InitStructure.EXTI_Line = EXTI_Line13;
+ EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
+ EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
+ NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
@@ -46,13 +60,24 @@ void switch_init(void) {
}
int switch_get(void) {
- if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
+#if defined (PYBOARD)
+ if (USRSW_PORT->IDR & USRSW_PIN) {
// pulled high, so switch is not pressed
return 0;
} else {
// pulled low, so switch is pressed
return 1;
}
+#elif defined (STM32F4DISC)
+ /* switch pulled down */
+ if (USRSW_PORT->IDR & USRSW_PIN) {
+ // pulled high, so switch is pressed
+ return 1;
+ } else {
+ // pulled low, so switch is not pressed
+ return 0;
+ }
+#endif
}
/******************************************************************************/