Skip to content

Commit 50d5420

Browse files
committed
Add Initial Support for STM32F4DISCOVERY Board
* Add a TARGET definition to Makefile (default PYBOARD). * Add support for discovery LEDs in led module. * Add support for discovery user switch in usersw * Add EXTI interrupt handler for discovery user switch on (PA0). * Parameterize led and usrsw modules pins and port. * Issue adafruit#83
1 parent 12e2656 commit 50d5420

5 files changed

Lines changed: 165 additions & 56 deletions

File tree

stm/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ CC3KSRC=cc3k
55
PYSRC=../py
66
BUILD=build
77
DFU=../tools/dfu.py
8+
TARGET=PYBOARD
89

910
AS = arm-none-eabi-as
1011
CC = arm-none-eabi-gcc
1112
LD = arm-none-eabi-ld
1213
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000
13-
CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4)
14+
CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
1415
#CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE
1516
LDFLAGS = --nostdlib -T stm32f405.ld
1617

stm/led.c

Lines changed: 114 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,144 @@
77
#include "obj.h"
88
#include "led.h"
99

10-
#define PYB_LED_R_PORT (GPIOA)
11-
#define PYB_LED_R1_PIN (GPIO_Pin_8)
12-
#define PYB_LED_R2_PIN (GPIO_Pin_10)
13-
#define PYB_LED_G_PORT (GPIOC)
14-
#define PYB_LED_G1_PIN (GPIO_Pin_4)
15-
#define PYB_LED_G2_PIN (GPIO_Pin_5)
10+
/* LED numbers, used internally */
11+
#define PYB_LED_1 (1)
12+
#define PYB_LED_2 (2)
13+
#define PYB_LED_3 (3)
14+
#define PYB_LED_4 (4)
15+
16+
#if defined(PYBOARD)
17+
#define PYB_LED1_PORT (GPIOA)
18+
#define PYB_LED1_PIN (GPIO_Pin_8)
19+
20+
#define PYB_LED2_PORT (GPIOA)
21+
#define PYB_LED2_PIN (GPIO_Pin_10)
22+
23+
#define PYB_LED3_PORT (GPIOC)
24+
#define PYB_LED3_PIN (GPIO_Pin_4)
25+
26+
#define PYB_LED4_PORT (GPIOC)
27+
#define PYB_LED4_PIN (GPIO_Pin_5)
28+
29+
#define PYB_OTYPE (GPIO_OType_OD)
30+
31+
#define PYB_LED_ON(port, pin) (port->BSRRH = pin)
32+
#define PYB_LED_OFF(port, pin) (port->BSRRL = pin)
33+
34+
#elif defined(STM32F4DISC)
35+
#define PYB_LED1_PORT (GPIOD)
36+
#define PYB_LED1_PIN (GPIO_Pin_14)
37+
38+
#define PYB_LED2_PORT (GPIOD)
39+
#define PYB_LED2_PIN (GPIO_Pin_12)
40+
41+
#define PYB_LED3_PORT (GPIOD)
42+
#define PYB_LED3_PIN (GPIO_Pin_15)
43+
44+
#define PYB_LED4_PORT (GPIOD)
45+
#define PYB_LED4_PIN (GPIO_Pin_13)
46+
47+
#define PYB_OTYPE (GPIO_OType_PP)
48+
49+
#define PYB_LED_ON(port, pin) (port->BSRRL = pin)
50+
#define PYB_LED_OFF(port, pin) (port->BSRRH = pin)
51+
#endif
1652

1753
void led_init(void) {
18-
// set the output high (so LED is off)
19-
PYB_LED_R_PORT->BSRRL = PYB_LED_R1_PIN;
20-
PYB_LED_R_PORT->BSRRL = PYB_LED_R2_PIN;
21-
PYB_LED_G_PORT->BSRRL = PYB_LED_G1_PIN;
22-
PYB_LED_G_PORT->BSRRL = PYB_LED_G2_PIN;
23-
// make them open drain outputs
54+
/* GPIO structure */
2455
GPIO_InitTypeDef GPIO_InitStructure;
25-
GPIO_InitStructure.GPIO_Pin = PYB_LED_R1_PIN | PYB_LED_R2_PIN;
56+
57+
/* Configure I/O speed, mode, output type and pull */
2658
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
2759
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
28-
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
2960
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
30-
GPIO_Init(PYB_LED_R_PORT, &GPIO_InitStructure);
31-
GPIO_InitStructure.GPIO_Pin = PYB_LED_G1_PIN | PYB_LED_G2_PIN;
32-
GPIO_Init(PYB_LED_G_PORT, &GPIO_InitStructure);
61+
GPIO_InitStructure.GPIO_OType = PYB_OTYPE;
62+
63+
/* Turn off LEDs */
64+
PYB_LED_OFF(PYB_LED1_PORT, PYB_LED1_PIN);
65+
PYB_LED_OFF(PYB_LED2_PORT, PYB_LED2_PIN);
66+
PYB_LED_OFF(PYB_LED3_PORT, PYB_LED1_PIN);
67+
PYB_LED_OFF(PYB_LED4_PORT, PYB_LED2_PIN);
68+
69+
/* Initialize LEDs */
70+
GPIO_InitStructure.GPIO_Pin = PYB_LED1_PIN;
71+
GPIO_Init(PYB_LED1_PORT, &GPIO_InitStructure);
72+
73+
GPIO_InitStructure.GPIO_Pin = PYB_LED2_PIN;
74+
GPIO_Init(PYB_LED2_PORT, &GPIO_InitStructure);
75+
76+
GPIO_InitStructure.GPIO_Pin = PYB_LED3_PIN;
77+
GPIO_Init(PYB_LED3_PORT, &GPIO_InitStructure);
78+
79+
GPIO_InitStructure.GPIO_Pin = PYB_LED4_PIN;
80+
GPIO_Init(PYB_LED4_PORT, &GPIO_InitStructure);
3381
}
3482

3583
void led_state(pyb_led_t led, int state) {
3684
GPIO_TypeDef *port;
3785
uint32_t pin;
86+
3887
switch (led) {
39-
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break;
40-
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break;
41-
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break;
42-
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break;
43-
default: return;
88+
case PYB_LED_1:
89+
pin = PYB_LED1_PIN;
90+
port = PYB_LED1_PORT;
91+
break;
92+
case PYB_LED_2:
93+
pin = PYB_LED2_PIN;
94+
port = PYB_LED2_PORT;
95+
break;
96+
case PYB_LED_3:
97+
pin = PYB_LED3_PIN;
98+
port = PYB_LED3_PORT;
99+
break;
100+
case PYB_LED_4:
101+
pin = PYB_LED4_PIN;
102+
port = PYB_LED4_PORT;
103+
break;
104+
default:
105+
return;
44106
}
107+
45108
if (state == 0) {
46-
// turn LED off (output is high)
47-
port->BSRRL = pin;
109+
// turn LED off
110+
PYB_LED_OFF(port, pin);
48111
} else {
49-
// turn LED on (output is low)
50-
port->BSRRH = pin;
112+
// turn LED on
113+
PYB_LED_ON(port, pin);
51114
}
52115
}
53116

54117
void led_toggle(pyb_led_t led) {
55118
GPIO_TypeDef *port;
56119
uint32_t pin;
120+
57121
switch (led) {
58-
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break;
59-
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break;
60-
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break;
61-
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break;
62-
default: return;
122+
case PYB_LED_1:
123+
pin = PYB_LED1_PIN;
124+
port = PYB_LED1_PORT;
125+
break;
126+
case PYB_LED_2:
127+
pin = PYB_LED2_PIN;
128+
port = PYB_LED2_PORT;
129+
break;
130+
case PYB_LED_3:
131+
pin = PYB_LED3_PIN;
132+
port = PYB_LED3_PORT;
133+
break;
134+
case PYB_LED_4:
135+
pin = PYB_LED4_PIN;
136+
port = PYB_LED4_PORT;
137+
break;
138+
default:
139+
return;
63140
}
141+
64142
if (!(port->ODR & pin)) {
65-
// turn LED off (output high)
66-
port->BSRRL = pin;
143+
// turn LED off
144+
PYB_LED_OFF(port, pin);
67145
} else {
68146
// turn LED on (output low)
69-
port->BSRRH = pin;
147+
PYB_LED_ON(port, pin);
70148
}
71149
}
72150

@@ -85,19 +163,13 @@ void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp
85163

86164
mp_obj_t led_obj_on(mp_obj_t self_in) {
87165
pyb_led_obj_t *self = self_in;
88-
switch (self->led_id) {
89-
case 1: led_state(PYB_LED_G1, 1); break;
90-
case 2: led_state(PYB_LED_G2, 1); break;
91-
}
166+
led_state(self->led_id, 1);
92167
return mp_const_none;
93168
}
94169

95170
mp_obj_t led_obj_off(mp_obj_t self_in) {
96171
pyb_led_obj_t *self = self_in;
97-
switch (self->led_id) {
98-
case 1: led_state(PYB_LED_G1, 0); break;
99-
case 2: led_state(PYB_LED_G2, 0); break;
100-
}
172+
led_state(self->led_id, 0);
101173
return mp_const_none;
102174
}
103175

stm/led.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ typedef enum {
33
PYB_LED_R2 = 1,
44
PYB_LED_G1 = 2,
55
PYB_LED_G2 = 3,
6+
//STM32F4DISC
7+
PYB_LED_R = 0,
8+
PYB_LED_G = 1,
9+
PYB_LED_B = 2,
10+
PYB_LED_O = 3,
611
} pyb_led_t;
712

813
void led_init(void);

stm/stm32fxxx_it.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,10 @@ void EXTI15_10_IRQHandler(void) {
298298
}
299299
}
300300

301+
#if defined(STM32F4DISC)
302+
void EXTI0_IRQHandler(void) {
303+
// clear pending interrupt bit
304+
EXTI_ClearITPendingBit(EXTI_Line0);
305+
}
306+
#endif
301307
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

stm/usrsw.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,75 @@
99
#include "obj.h"
1010
#include "usrsw.h"
1111

12-
#define PYB_USRSW_PORT (GPIOA)
13-
#define PYB_USRSW_PIN (GPIO_Pin_13)
14-
12+
#if defined (PYBOARD)
13+
#define USRSW_PORT (GPIOA)
14+
#define USRSW_PIN (GPIO_Pin_13)
15+
#define USRSW_EXTI_PIN (EXTI_PinSource13)
16+
#define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
17+
#define USRSW_EXTI_LINE (EXTI_Line13)
18+
#define USRSW_EXTI_IRQN (EXTI15_10_IRQn)
19+
#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising)
20+
#elif defined (STM32F4DISC)
21+
#define USRSW_PORT (GPIOA)
22+
#define USRSW_PIN (GPIO_Pin_0)
23+
#define USRSW_EXTI_PIN (EXTI_PinSource0)
24+
#define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
25+
#define USRSW_EXTI_LINE (EXTI_Line0)
26+
#define USRSW_EXTI_IRQN (EXTI0_IRQn)
27+
#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling)
28+
#endif
1529
void switch_init(void) {
1630
// make it an input with pull-up
1731
GPIO_InitTypeDef GPIO_InitStructure;
18-
GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
32+
GPIO_InitStructure.GPIO_Pin = USRSW_PIN;
1933
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
20-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
21-
GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
34+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* allow external pull up/down */
35+
GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
2236

2337
// the rest does the EXTI interrupt
2438

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

28-
/* Connect EXTI Line13 to PA13 pin */
29-
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource13);
42+
/* Connect EXTI Line to GPIO pin */
43+
SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
3044

31-
/* Configure EXTI Line13, rising edge */
45+
/* Configure EXTI Line */
3246
EXTI_InitTypeDef EXTI_InitStructure;
33-
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
47+
EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
3448
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
35-
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
49+
EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
3650
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
3751
EXTI_Init(&EXTI_InitStructure);
3852

3953
/* Enable and set EXTI15_10 Interrupt to the lowest priority */
4054
NVIC_InitTypeDef NVIC_InitStructure;
41-
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
55+
NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
4256
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
4357
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
4458
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
4559
NVIC_Init(&NVIC_InitStructure);
4660
}
4761

4862
int switch_get(void) {
49-
if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
63+
#if defined (PYBOARD)
64+
if (USRSW_PORT->IDR & USRSW_PIN) {
5065
// pulled high, so switch is not pressed
5166
return 0;
5267
} else {
5368
// pulled low, so switch is pressed
5469
return 1;
5570
}
71+
#elif defined (STM32F4DISC)
72+
/* switch pulled down */
73+
if (USRSW_PORT->IDR & USRSW_PIN) {
74+
// pulled high, so switch is pressed
75+
return 1;
76+
} else {
77+
// pulled low, so switch is not pressed
78+
return 0;
79+
}
80+
#endif
5681
}
5782

5883
/******************************************************************************/

0 commit comments

Comments
 (0)