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
1753void 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
3583void 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
54117void 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
86164mp_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
95170mp_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
0 commit comments