2014-01-05 07:52:37 -05:00
|
|
|
#include <stm_misc.h>
|
|
|
|
#include <stm32f4xx_gpio.h>
|
|
|
|
#include <stm32f4xx_exti.h>
|
|
|
|
#include <stm32f4xx_syscfg.h>
|
|
|
|
#include <stm32f4xx_rcc.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-22 10:42:06 -05:00
|
|
|
#include "mpconfigport.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2014-01-05 07:52:37 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "usrsw.h"
|
|
|
|
|
2014-01-05 09:04:55 -05:00
|
|
|
void switch_init(void) {
|
2014-01-05 07:52:37 -05:00
|
|
|
// make it an input with pull-up
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
2014-01-05 12:38:41 -05:00
|
|
|
GPIO_InitStructure.GPIO_Pin = USRSW_PIN;
|
2014-01-05 07:52:37 -05:00
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
2014-01-05 18:49:34 -05:00
|
|
|
GPIO_InitStructure.GPIO_PuPd = USRSW_PUPD;
|
2014-01-05 12:38:41 -05:00
|
|
|
GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
|
2014-01-05 07:52:37 -05:00
|
|
|
|
|
|
|
// the rest does the EXTI interrupt
|
|
|
|
|
|
|
|
/* Enable SYSCFG clock */
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
|
|
|
|
2014-01-05 12:38:41 -05:00
|
|
|
/* Connect EXTI Line to GPIO pin */
|
|
|
|
SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
|
2014-01-05 07:52:37 -05:00
|
|
|
|
2014-01-05 12:38:41 -05:00
|
|
|
/* Configure EXTI Line */
|
2014-01-05 07:52:37 -05:00
|
|
|
EXTI_InitTypeDef EXTI_InitStructure;
|
2014-01-05 12:38:41 -05:00
|
|
|
EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
|
2014-01-05 07:52:37 -05:00
|
|
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
2014-01-05 12:38:41 -05:00
|
|
|
EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
|
2014-01-05 07:52:37 -05:00
|
|
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
|
|
|
EXTI_Init(&EXTI_InitStructure);
|
|
|
|
|
|
|
|
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
2014-01-05 12:38:41 -05:00
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
|
2014-01-05 07:52:37 -05:00
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
}
|
|
|
|
|
2014-01-05 09:04:55 -05:00
|
|
|
int switch_get(void) {
|
2014-01-21 17:20:50 -05:00
|
|
|
#if defined (PYBOARD) || defined (PYBOARD4)
|
2014-01-05 12:38:41 -05:00
|
|
|
if (USRSW_PORT->IDR & USRSW_PIN) {
|
2014-01-05 07:52:37 -05:00
|
|
|
// pulled high, so switch is not pressed
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// pulled low, so switch is pressed
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-05 12:38:41 -05:00
|
|
|
#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
|
2014-01-05 07:52:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
|
2014-01-05 09:04:55 -05:00
|
|
|
static mp_obj_t pyb_switch(void) {
|
|
|
|
if (switch_get()) {
|
2014-01-05 07:52:37 -05:00
|
|
|
return mp_const_true;
|
|
|
|
} else {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 09:04:55 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_switch_obj, pyb_switch);
|