usrsw.c 3.48 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
14
15
#if defined (PYBOARD)
    #define USRSW_PORT          (GPIOA)
    #define USRSW_PIN           (GPIO_Pin_13)
16
    #define USRSW_PUPD          (GPIO_PuPd_UP)
17
18
19
20
21
    #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)
22
23
24
25
26
27
28
29
30
#elif defined (PYBOARD4)
    #define USRSW_PORT          (GPIOB)
    #define USRSW_PIN           (GPIO_Pin_3)
    #define USRSW_PUPD          (GPIO_PuPd_UP)
    #define USRSW_EXTI_PIN      (EXTI_PinSource3)
    #define USRSW_EXTI_PORT     (EXTI_PortSourceGPIOB)
    #define USRSW_EXTI_LINE     (EXTI_Line3)
    #define USRSW_EXTI_IRQN     (EXTI15_10_IRQn)
    #define USRSW_EXTI_EDGE     (EXTI_Trigger_Rising)
31
32
33
#elif defined (STM32F4DISC)
    #define USRSW_PORT          (GPIOA)
    #define USRSW_PIN           (GPIO_Pin_0)
34
    #define USRSW_PUPD          (GPIO_PuPd_NOPULL)
35
36
37
38
39
    #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)
40
41
#endif

42
void switch_init(void) {
43
44
    // make it an input with pull-up
    GPIO_InitTypeDef GPIO_InitStructure;
45
    GPIO_InitStructure.GPIO_Pin  = USRSW_PIN;
46
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
47
    GPIO_InitStructure.GPIO_PuPd = USRSW_PUPD;
48
    GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
49
50
51
52
53
54

    // the rest does the EXTI interrupt

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

55
56
    /* Connect EXTI Line to GPIO pin */
    SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
57

58
    /* Configure EXTI Line */
59
    EXTI_InitTypeDef EXTI_InitStructure;
60
    EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
61
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
62
    EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
63
64
65
66
67
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* Enable and set EXTI15_10 Interrupt to the lowest priority */
    NVIC_InitTypeDef NVIC_InitStructure;
68
    NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
69
70
71
72
73
74
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

75
int switch_get(void) {
76
#if defined (PYBOARD) || defined (PYBOARD4)
77
    if (USRSW_PORT->IDR & USRSW_PIN) {
78
79
80
81
82
83
        // pulled high, so switch is not pressed
        return 0;
    } else {
        // pulled low, so switch is pressed
        return 1;
    }
84
85
86
87
88
89
90
91
92
93
#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
94
95
96
97
98
}

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

99
100
static mp_obj_t pyb_switch(void) {
    if (switch_get()) {
101
102
103
104
105
106
        return mp_const_true;
    } else {
        return mp_const_false;
    }
}

107
MP_DEFINE_CONST_FUN_OBJ_0(pyb_switch_obj, pyb_switch);