led.c 3.4 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
#include "led.h"
Dave Hylands's avatar
Dave Hylands committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "pin.h"
#include "build/pins.h"

static const pin_obj_t *gLed[] = {
    &PYB_LED1,
#if defined(PYB_LED2)
    &PYB_LED2,
#if defined(PYB_LED3)
    &PYB_LED3,
#if defined(PYB_LED4)
    &PYB_LED4,
#endif
#endif
#endif
};
#define NUM_LEDS (sizeof(gLed) / sizeof(gLed[0]))
26

27
void led_init(void) {
28
    /* GPIO structure */
29
    GPIO_InitTypeDef GPIO_InitStructure;
30
31

    /* Configure I/O speed, mode, output type and pull */
32
33
34
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
35
36
    GPIO_InitStructure.GPIO_OType = PYB_OTYPE;

Dave Hylands's avatar
Dave Hylands committed
37
38
39
40
41
42
    /* Turn off LEDs and initialize */
    for (int led = 0; led < NUM_LEDS; led++) {
        PYB_LED_OFF(gLed[led]); 
        GPIO_InitStructure.GPIO_Pin = gLed[led]->pin_mask;
        GPIO_Init(gLed[led]->gpio, &GPIO_InitStructure);
    }
43
44
45
}

void led_state(pyb_led_t led, int state) {
Dave Hylands's avatar
Dave Hylands committed
46
47
    if (led < 1 || led > NUM_LEDS) {
        return;
48
    }
Dave Hylands's avatar
Dave Hylands committed
49
    const pin_obj_t *led_pin = gLed[led - 1];
50
    if (state == 0) {
51
        // turn LED off
Dave Hylands's avatar
Dave Hylands committed
52
        PYB_LED_OFF(led_pin);
53
    } else {
54
        // turn LED on
Dave Hylands's avatar
Dave Hylands committed
55
        PYB_LED_ON(led_pin);
56
57
58
59
    }
}

void led_toggle(pyb_led_t led) {
Dave Hylands's avatar
Dave Hylands committed
60
61
    if (led < 1 || led > NUM_LEDS) {
        return;
62
    }
Dave Hylands's avatar
Dave Hylands committed
63
64
65
66
67
68
69
70
    const pin_obj_t *led_pin = gLed[led - 1];
    GPIO_TypeDef *gpio = led_pin->gpio;

    // We don't know if we're turning the LED on or off, but we don't really
    // care. Just invert the state.
    if (gpio->ODR & led_pin->pin_mask) {
        // pin is high, make it low
        gpio->BSRRH = led_pin->pin_mask;
71
    } else {
Dave Hylands's avatar
Dave Hylands committed
72
73
        // pin is low, make it high
        gpio->BSRRL = led_pin->pin_mask;
74
75
    }
}
76
77
78
79
80
81
82
83
84

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

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

85
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
86
87
88
89
90
91
    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;
92
    led_state(self->led_id, 1);
93
94
95
96
97
    return mp_const_none;
}

mp_obj_t led_obj_off(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
98
    led_state(self->led_id, 0);
99
100
101
    return mp_const_none;
}

Dave Hylands's avatar
Dave Hylands committed
102
103
104
105
106
107
mp_obj_t led_obj_toggle(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_toggle(self->led_id);
    return mp_const_none;
}

108
109
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);
Dave Hylands's avatar
Dave Hylands committed
110
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
111

112
static const mp_method_t led_methods[] = {
113
114
115
116
    { MP_QSTR_on, &led_obj_on_obj },
    { MP_QSTR_off, &led_obj_off_obj },
    { MP_QSTR_toggle, &led_obj_toggle_obj },
    { MP_QSTR_NULL, NULL },
117
118
};

119
static const mp_obj_type_t led_obj_type = {
120
    { &mp_type_type },
121
    .name = MP_QSTR_Led,
122
    .print = led_obj_print,
123
    .methods = led_methods,
124
125
};

126
static mp_obj_t pyb_Led(mp_obj_t led_id) {
127
128
129
130
131
    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;
}
132
133

MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led);