led.c 7.27 KB
Newer Older
1
2
3
#include <stdio.h>
#include <stm32f4xx_hal.h>

4
#include "nlr.h"
5
6
7
8
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
9
#include "runtime.h"
10
#include "timer.h"
11
12
#include "led.h"
#include "pin.h"
13
#include "genhdr/pins.h"
14

15
16
17
18
19
20
21
22
typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
    machine_uint_t led_id;
    const pin_obj_t *led_pin;
} pyb_led_obj_t;

STATIC const pyb_led_obj_t pyb_led_obj[] = {
    {{&pyb_led_type}, 1, &MICROPY_HW_LED1},
23
#if defined(MICROPY_HW_LED2)
24
    {{&pyb_led_type}, 2, &MICROPY_HW_LED2},
25
#if defined(MICROPY_HW_LED3)
26
    {{&pyb_led_type}, 3, &MICROPY_HW_LED3},
27
#if defined(MICROPY_HW_LED4)
28
    {{&pyb_led_type}, 4, &MICROPY_HW_LED4},
29
30
31
32
#endif
#endif
#endif
};
33
#define NUM_LEDS (sizeof(pyb_led_obj) / sizeof(pyb_led_obj[0]))
34
35
36
37
38
39
40

void led_init(void) {
    /* GPIO structure */
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Configure I/O speed, mode, output type and pull */
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
41
    GPIO_InitStructure.Mode = MICROPY_HW_LED_OTYPE;
42
43
44
45
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    /* Turn off LEDs and initialize */
    for (int led = 0; led < NUM_LEDS; led++) {
46
47
48
49
        const pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
        MICROPY_HW_LED_OFF(led_pin);
        GPIO_InitStructure.Pin = led_pin->pin_mask;
        HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);
50
    }
51

52
#if defined(PYBV4) || defined(PYBV10)
53
54
55
    // LED4 (blue) is on PB4 which is TIM3_CH1
    // we use PWM on this channel to fade the LED

56
57
    // LED3 (yellow) is on PA15 which has TIM2_CH1, so we could PWM that as well

58
    // GPIO configuration
59
    GPIO_InitStructure.Pin = MICROPY_HW_LED4.pin_mask;
60
61
62
63
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF2_TIM3;
64
    HAL_GPIO_Init(MICROPY_HW_LED4.gpio, &GPIO_InitStructure);
65
66
67
68
69
70
71
72
73
74
75

    // PWM mode configuration
    TIM_OC_InitTypeDef oc_init;
    oc_init.OCMode = TIM_OCMODE_PWM1;
    oc_init.Pulse = 0; // off
    oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
    oc_init.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &oc_init, TIM_CHANNEL_1);

    // start PWM
    TIM_CCxChannelCmd(TIM3, TIM_CHANNEL_1, TIM_CCx_ENABLE);
76
#endif
77
78
79
80
81
82
}

void led_state(pyb_led_t led, int state) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
83
#if defined(PYBV4) || defined(PYBV10)
84
85
86
87
88
89
90
91
    if (led == 4) {
        if (state) {
            TIM3->CCR1 = 0xffff;
        } else {
            TIM3->CCR1 = 0;
        }
        return;
    }
92
#endif
93
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
94
    //printf("led_state(%d,%d)\n", led, state);
95
96
    if (state == 0) {
        // turn LED off
97
        MICROPY_HW_LED_OFF(led_pin);
98
99
    } else {
        // turn LED on
100
        MICROPY_HW_LED_ON(led_pin);
101
102
103
104
105
106
107
    }
}

void led_toggle(pyb_led_t led) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
108

109
#if defined(PYBV4) || defined(PYBV10)
110
111
112
113
114
115
116
117
118
119
    if (led == 4) {
        if (TIM3->CCR1 == 0) {
            TIM3->CCR1 = 0xffff;
        } else {
            TIM3->CCR1 = 0;
        }
        return;
    }
#endif

120
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
121
122
123
124
125
126
127
128
129
130
131
132
133
    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;
    } else {
        // pin is low, make it high
        gpio->BSRRL = led_pin->pin_mask;
    }
}

134
int led_get_intensity(pyb_led_t led) {
135
136
137
    if (led < 1 || led > NUM_LEDS) {
        return 0;
    }
138

139
#if defined(PYBV4) || defined(PYBV10)
140
141
142
143
144
145
146
147
148
    if (led == 4) {
        machine_uint_t i = TIM3->CCR1 * 255 / ((USBD_CDC_POLLING_INTERVAL*1000) - 1);
        if (i > 255) {
            i = 255;
        }
        return i;
    }
#endif

149
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
150
151
152
153
154
    GPIO_TypeDef *gpio = led_pin->gpio;

    // TODO convert high/low to on/off depending on board
    if (gpio->ODR & led_pin->pin_mask) {
        // pin is high
155
        return 255;
156
157
158
159
160
161
    } else {
        // pin is low
        return 0;
    }
}

162
void led_set_intensity(pyb_led_t led, machine_int_t intensity) {
163
#if defined(PYBV4) || defined(PYBV10)
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    if (led == 4) {
        // set intensity using PWM pulse width
        if (intensity < 0) {
            intensity = 0;
        } else if (intensity >= 255) {
            intensity = 0xffff;
        } else {
            intensity = intensity * ((USBD_CDC_POLLING_INTERVAL*1000) - 1) / 255;
        }
        TIM3->CCR1 = intensity;
        return;
    }
#endif

    // intensity not supported for this LED; just turn it on/off
    led_state(led, intensity > 0);
}

182
183
184
185
186
187
188
189
void led_debug(int n, int delay) {
    led_state(1, n & 1);
    led_state(2, n & 2);
    led_state(3, n & 4);
    led_state(4, n & 8);
    HAL_Delay(delay);
}

190
191
192
193
194
195
196
197
/******************************************************************************/
/* Micro Python bindings                                                      */

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

198
199
STATIC mp_obj_t led_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
    // check arguments
Damien George's avatar
Damien George committed
200
    mp_check_nargs(n_args, 1, 1, n_kw, false);
201
202
203
204
205
206

    // get led number
    machine_int_t led_id = mp_obj_get_int(args[0]) - 1;

    // check led number
    if (!(0 <= led_id && led_id < NUM_LEDS)) {
207
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Led %d does not exist", led_id));
208
209
210
211
212
213
    }

    // return static led object
    return (mp_obj_t)&pyb_led_obj[led_id];
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
mp_obj_t led_obj_on(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_state(self->led_id, 1);
    return mp_const_none;
}

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

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;
}

232
mp_obj_t led_obj_intensity(uint n_args, const mp_obj_t *args) {
233
    pyb_led_obj_t *self = args[0];
234
235
    if (n_args == 1) {
        return mp_obj_new_int(led_get_intensity(self->led_id));
236
    } else {
237
        led_set_intensity(self->led_id, mp_obj_get_int(args[1]));
238
239
240
241
242
243
244
        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);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
245
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_intensity_obj, 1, 2, led_obj_intensity);
246

247
248
249
250
251
STATIC const mp_map_elem_t led_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&led_obj_on_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&led_obj_off_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&led_obj_toggle_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_intensity), (mp_obj_t)&led_obj_intensity_obj },
252
253
};

254
255
STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);

256
const mp_obj_type_t pyb_led_type = {
257
258
259
    { &mp_type_type },
    .name = MP_QSTR_Led,
    .print = led_obj_print,
260
    .make_new = led_obj_make_new,
261
    .locals_dict = (mp_obj_t)&led_locals_dict,
262
};