led.c 5.44 KB
Newer Older
1
#include <stdio.h>
2
3
#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
4
5
6

#include "misc.h"
#include "mpconfig.h"
7
#include "qstr.h"
8
#include "obj.h"
9
10
#include "led.h"

11
/* LED numbers, used internally */
Damien George's avatar
Damien George committed
12
13
14
15
#define PYB_LED_1   (1)
#define PYB_LED_2   (2)
#define PYB_LED_3   (3)
#define PYB_LED_4   (4)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#if defined(PYBOARD)
    #define PYB_LED1_PORT   (GPIOA)
    #define PYB_LED1_PIN    (GPIO_Pin_8)

    #define PYB_LED2_PORT   (GPIOA)
    #define PYB_LED2_PIN    (GPIO_Pin_10)

    #define PYB_LED3_PORT   (GPIOC)
    #define PYB_LED3_PIN    (GPIO_Pin_4)

    #define PYB_LED4_PORT   (GPIOC)
    #define PYB_LED4_PIN    (GPIO_Pin_5)

    #define PYB_OTYPE       (GPIO_OType_OD)

    #define PYB_LED_ON(port, pin)  (port->BSRRH = pin)
    #define PYB_LED_OFF(port, pin) (port->BSRRL = pin)

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#elif defined(PYBOARD4)
    #define PYB_LED1_PORT   (GPIOA)
    #define PYB_LED1_PIN    (GPIO_Pin_13)

    #define PYB_LED2_PORT   (GPIOA)
    #define PYB_LED2_PIN    (GPIO_Pin_14)

    #define PYB_LED3_PORT   (GPIOA)
    #define PYB_LED3_PIN    (GPIO_Pin_15)

    #define PYB_LED4_PORT   (GPIOB)
    #define PYB_LED4_PIN    (GPIO_Pin_4)

    #define PYB_OTYPE       (GPIO_OType_PP)

    #define PYB_LED_ON(port, pin)  (port->BSRRL = pin)
    #define PYB_LED_OFF(port, pin) (port->BSRRH = pin)

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#elif defined(STM32F4DISC)
    #define PYB_LED1_PORT   (GPIOD)
    #define PYB_LED1_PIN    (GPIO_Pin_14)

    #define PYB_LED2_PORT   (GPIOD)
    #define PYB_LED2_PIN    (GPIO_Pin_12)

    #define PYB_LED3_PORT   (GPIOD)
    #define PYB_LED3_PIN    (GPIO_Pin_15)

    #define PYB_LED4_PORT   (GPIOD)
    #define PYB_LED4_PIN    (GPIO_Pin_13)

    #define PYB_OTYPE       (GPIO_OType_PP)

    #define PYB_LED_ON(port, pin)  (port->BSRRL = pin)
    #define PYB_LED_OFF(port, pin) (port->BSRRH = pin)
#endif
71

72
void led_init(void) {
73
    /* GPIO structure */
74
    GPIO_InitTypeDef GPIO_InitStructure;
75
76

    /* Configure I/O speed, mode, output type and pull */
77
78
79
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
80
81
82
83
84
    GPIO_InitStructure.GPIO_OType = PYB_OTYPE;

    /* Turn off LEDs */
    PYB_LED_OFF(PYB_LED1_PORT, PYB_LED1_PIN);
    PYB_LED_OFF(PYB_LED2_PORT, PYB_LED2_PIN);
mux's avatar
mux committed
85
86
    PYB_LED_OFF(PYB_LED3_PORT, PYB_LED3_PIN);
    PYB_LED_OFF(PYB_LED4_PORT, PYB_LED4_PIN);
87
88
89
90
91
92
93
94
95
96
97
98
99

    /* Initialize LEDs */
    GPIO_InitStructure.GPIO_Pin = PYB_LED1_PIN;
    GPIO_Init(PYB_LED1_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED2_PIN;
    GPIO_Init(PYB_LED2_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED3_PIN;
    GPIO_Init(PYB_LED3_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED4_PIN;
    GPIO_Init(PYB_LED4_PORT, &GPIO_InitStructure);
100
101
102
103
104
}

void led_state(pyb_led_t led, int state) {
    GPIO_TypeDef *port;
    uint32_t pin;
105

106
    switch (led) {
107
        case PYB_LED_1:
108
109
            pin  = PYB_LED1_PIN;
            port = PYB_LED1_PORT;
110
111
            break;
        case PYB_LED_2:
112
113
            pin  = PYB_LED2_PIN;
            port = PYB_LED2_PORT;
114
115
            break;
        case PYB_LED_3:
116
117
            pin  = PYB_LED3_PIN;
            port = PYB_LED3_PORT;
118
119
            break;
        case PYB_LED_4:
120
121
            pin  = PYB_LED4_PIN;
            port = PYB_LED4_PORT;
122
            break;
123
        default:
124
            return;
125
    }
126

127
    if (state == 0) {
128
129
        // turn LED off
        PYB_LED_OFF(port, pin);
130
    } else {
131
132
        // turn LED on
        PYB_LED_ON(port, pin);
133
134
135
136
137
138
    }
}

void led_toggle(pyb_led_t led) {
    GPIO_TypeDef *port;
    uint32_t pin;
139

140
    switch (led) {
141
        case PYB_LED_1:
142
143
            pin  = PYB_LED1_PIN;
            port = PYB_LED1_PORT;
144
145
            break;
        case PYB_LED_2:
146
147
            pin  = PYB_LED2_PIN;
            port = PYB_LED2_PORT;
148
149
            break;
        case PYB_LED_3:
150
151
            pin  = PYB_LED3_PIN;
            port = PYB_LED3_PORT;
152
153
            break;
        case PYB_LED_4:
154
155
            pin  = PYB_LED4_PIN;
            port = PYB_LED4_PORT;
156
            break;
157
        default:
158
            return;
159
    }
160

161
    if (!(port->ODR & pin)) {
162
        // turn LED off
163
        PYB_LED_OFF(port, pin);
164
165
    } else {
        // turn LED on (output low)
166
        PYB_LED_ON(port, pin);
167
168
    }
}
169
170
171
172
173
174
175
176
177

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

typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
    uint led_id;
} pyb_led_obj_t;

178
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
179
180
181
182
183
184
    pyb_led_obj_t *self = self_in;
    print(env, "<LED %lu>", self->led_id);
}

mp_obj_t led_obj_on(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
185
    led_state(self->led_id, 1);
186
187
188
189
190
    return mp_const_none;
}

mp_obj_t led_obj_off(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
191
    led_state(self->led_id, 0);
192
193
194
195
196
197
    return mp_const_none;
}

static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);

198
199
200
201
202
203
static const mp_method_t led_methods[] = {
    { "on", &led_obj_on_obj },
    { "off", &led_obj_off_obj },
    { NULL, NULL },
};

204
205
206
static const mp_obj_type_t led_obj_type = {
    { &mp_const_type },
    "Led",
207
    .print = led_obj_print,
208
    .methods = led_methods,
209
210
211
212
213
214
215
216
};

mp_obj_t pyb_Led(mp_obj_t led_id) {
    pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t);
    o->base.type = &led_obj_type;
    o->led_id = mp_obj_get_int(led_id);
    return o;
}