Skip to content

Commit cd3c1ee

Browse files
committed
Merge pull request adafruit#82 from iabdalkader/master
Move user switch code into a separate module
2 parents 1703597 + de7fcc0 commit cd3c1ee

4 files changed

Lines changed: 78 additions & 54 deletions

File tree

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SRC_C = \
3636
sdio.c \
3737
pybwlan.c \
3838
i2c.c \
39+
usrsw.c \
3940

4041
SRC_S = \
4142
startup_stm32f40xx.s \

stm/main.c

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "audio.h"
4040
#include "pybwlan.h"
4141
#include "i2c.h"
42+
#include "usrsw.h"
4243

4344
int errno;
4445

@@ -73,52 +74,6 @@ static void impl02_c_version(void) {
7374
}
7475
}
7576

76-
#define PYB_USRSW_PORT (GPIOA)
77-
#define PYB_USRSW_PIN (GPIO_Pin_13)
78-
79-
void sw_init(void) {
80-
// make it an input with pull-up
81-
GPIO_InitTypeDef GPIO_InitStructure;
82-
GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
83-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
84-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
85-
GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
86-
87-
// the rest does the EXTI interrupt
88-
89-
/* Enable SYSCFG clock */
90-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
91-
92-
/* Connect EXTI Line13 to PA13 pin */
93-
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource13);
94-
95-
/* Configure EXTI Line13, rising edge */
96-
EXTI_InitTypeDef EXTI_InitStructure;
97-
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
98-
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
99-
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
100-
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
101-
EXTI_Init(&EXTI_InitStructure);
102-
103-
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
104-
NVIC_InitTypeDef NVIC_InitStructure;
105-
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
106-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
107-
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
108-
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
109-
NVIC_Init(&NVIC_InitStructure);
110-
}
111-
112-
int sw_get(void) {
113-
if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
114-
// pulled high, so switch is not pressed
115-
return 0;
116-
} else {
117-
// pulled low, so switch is pressed
118-
return 1;
119-
}
120-
}
121-
12277
void __fatal_error(const char *msg) {
12378
lcd_print_strn("\nFATAL ERROR:\n", 14);
12479
lcd_print_strn(msg, strlen(msg));
@@ -156,14 +111,6 @@ mp_obj_t pyb_led(mp_obj_t state) {
156111
return state;
157112
}
158113

159-
mp_obj_t pyb_sw(void) {
160-
if (sw_get()) {
161-
return mp_const_true;
162-
} else {
163-
return mp_const_false;
164-
}
165-
}
166-
167114
/*
168115
void g(uint i) {
169116
printf("g:%d\n", i);

stm/usrsw.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stm_misc.h>
2+
#include <stm32f4xx_gpio.h>
3+
#include <stm32f4xx_exti.h>
4+
#include <stm32f4xx_syscfg.h>
5+
#include <stm32f4xx_rcc.h>
6+
7+
#include "misc.h"
8+
#include "mpconfig.h"
9+
#include "obj.h"
10+
#include "usrsw.h"
11+
12+
#define PYB_USRSW_PORT (GPIOA)
13+
#define PYB_USRSW_PIN (GPIO_Pin_13)
14+
15+
void sw_init(void) {
16+
// make it an input with pull-up
17+
GPIO_InitTypeDef GPIO_InitStructure;
18+
GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
19+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
20+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
21+
GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
22+
23+
// the rest does the EXTI interrupt
24+
25+
/* Enable SYSCFG clock */
26+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
27+
28+
/* Connect EXTI Line13 to PA13 pin */
29+
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource13);
30+
31+
/* Configure EXTI Line13, rising edge */
32+
EXTI_InitTypeDef EXTI_InitStructure;
33+
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
34+
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
35+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
36+
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
37+
EXTI_Init(&EXTI_InitStructure);
38+
39+
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
40+
NVIC_InitTypeDef NVIC_InitStructure;
41+
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
42+
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
43+
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
44+
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
45+
NVIC_Init(&NVIC_InitStructure);
46+
}
47+
48+
int sw_get(void) {
49+
if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
50+
// pulled high, so switch is not pressed
51+
return 0;
52+
} else {
53+
// pulled low, so switch is pressed
54+
return 1;
55+
}
56+
}
57+
58+
/******************************************************************************/
59+
/* Micro Python bindings */
60+
61+
mp_obj_t pyb_sw(void) {
62+
if (sw_get()) {
63+
return mp_const_true;
64+
} else {
65+
return mp_const_false;
66+
}
67+
}
68+
69+

stm/usrsw.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __USRSW_H__
2+
#define __USRSW_H__
3+
void sw_init(void);
4+
int sw_get(void);
5+
6+
mp_obj_t pyb_sw(void);
7+
#endif //__USRSW_H__

0 commit comments

Comments
 (0)