usrsw.c 2.3 KB
Newer Older
1
2
3
4
5
6
7
8
#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"
9
#include "qstr.h"
10
11
12
#include "obj.h"
#include "usrsw.h"

13
void switch_init(void) {
14
15
    // make it an input with pull-up
    GPIO_InitTypeDef GPIO_InitStructure;
16
    GPIO_InitStructure.GPIO_Pin  = USRSW_PIN;
17
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
18
    GPIO_InitStructure.GPIO_PuPd = USRSW_PUPD;
19
    GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
20
21
22
23
24
25

    // the rest does the EXTI interrupt

    /* Enable SYSCFG clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

26
27
    /* Connect EXTI Line to GPIO pin */
    SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
28

29
    /* Configure EXTI Line */
30
    EXTI_InitTypeDef EXTI_InitStructure;
31
    EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
32
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
33
    EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
34
35
36
37
38
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* Enable and set EXTI15_10 Interrupt to the lowest priority */
    NVIC_InitTypeDef NVIC_InitStructure;
39
    NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
40
41
42
43
44
45
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

46
int switch_get(void) {
47
#if defined (PYBOARD3) || defined (PYBOARD4)
48
    if (USRSW_PORT->IDR & USRSW_PIN) {
49
50
51
52
53
54
        // pulled high, so switch is not pressed
        return 0;
    } else {
        // pulled low, so switch is pressed
        return 1;
    }
Dave Hylands's avatar
Dave Hylands committed
55
#elif defined (STM32F4DISC) || defined(NETDUINO_PLUS_2)
56
57
58
59
60
61
62
63
64
    /* 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
65
66
67
68
69
}

/******************************************************************************/
/* Micro Python bindings                                                      */

70
71
static mp_obj_t pyb_switch(void) {
    if (switch_get()) {
72
73
74
75
76
77
        return mp_const_true;
    } else {
        return mp_const_false;
    }
}

78
MP_DEFINE_CONST_FUN_OBJ_0(pyb_switch_obj, pyb_switch);