led.c 8.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
28
29
#include <stdio.h>
#include <stm32f4xx_hal.h>

30
31
#include "py/nlr.h"
#include "py/runtime.h"
32
#include "timer.h"
33
34
#include "led.h"
#include "pin.h"
35
#include "genhdr/pins.h"
36

37
38
39
40
41
/// \moduleref pyb
/// \class LED - LED object
///
/// The LED object controls an individual LED (Light Emitting Diode).

42
43
typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
44
    mp_uint_t led_id;
45
46
47
48
49
    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},
50
#if defined(MICROPY_HW_LED2)
51
    {{&pyb_led_type}, 2, &MICROPY_HW_LED2},
52
#if defined(MICROPY_HW_LED3)
53
    {{&pyb_led_type}, 3, &MICROPY_HW_LED3},
54
#if defined(MICROPY_HW_LED4)
55
    {{&pyb_led_type}, 4, &MICROPY_HW_LED4},
56
57
58
59
#endif
#endif
#endif
};
60
#define NUM_LEDS MP_ARRAY_SIZE(pyb_led_obj)
61
62
63
64
65
66
67

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;
68
    GPIO_InitStructure.Mode = MICROPY_HW_LED_OTYPE;
69
70
71
72
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    /* Turn off LEDs and initialize */
    for (int led = 0; led < NUM_LEDS; led++) {
73
74
75
76
        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);
77
    }
78

79
#if defined(PYBV4) || defined(PYBV10)
80
81
82
    // LED4 (blue) is on PB4 which is TIM3_CH1
    // we use PWM on this channel to fade the LED

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

85
    // GPIO configuration
86
    GPIO_InitStructure.Pin = MICROPY_HW_LED4.pin_mask;
87
88
89
90
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF2_TIM3;
91
    HAL_GPIO_Init(MICROPY_HW_LED4.gpio, &GPIO_InitStructure);
92
93
94
95
96
97
98
99
100
101
102

    // 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);
103
#endif
104
105
106
107
108
109
}

void led_state(pyb_led_t led, int state) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
110
#if defined(PYBV4) || defined(PYBV10)
111
112
113
114
115
116
117
118
    if (led == 4) {
        if (state) {
            TIM3->CCR1 = 0xffff;
        } else {
            TIM3->CCR1 = 0;
        }
        return;
    }
119
#endif
120
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
121
    //printf("led_state(%d,%d)\n", led, state);
122
123
    if (state == 0) {
        // turn LED off
124
        MICROPY_HW_LED_OFF(led_pin);
125
126
    } else {
        // turn LED on
127
        MICROPY_HW_LED_ON(led_pin);
128
129
130
131
132
133
134
    }
}

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

136
#if defined(PYBV4) || defined(PYBV10)
137
138
139
140
141
142
143
144
145
146
    if (led == 4) {
        if (TIM3->CCR1 == 0) {
            TIM3->CCR1 = 0xffff;
        } else {
            TIM3->CCR1 = 0;
        }
        return;
    }
#endif

147
    // toggle the output data register to toggle the LED state
148
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
149
    led_pin->gpio->ODR ^= led_pin->pin_mask;
150
151
}

152
int led_get_intensity(pyb_led_t led) {
153
154
155
    if (led < 1 || led > NUM_LEDS) {
        return 0;
    }
156

157
#if defined(PYBV4) || defined(PYBV10)
158
    if (led == 4) {
159
        mp_uint_t i = (TIM3->CCR1 * 255 + (USBD_CDC_POLLING_INTERVAL*1000) - 2) / ((USBD_CDC_POLLING_INTERVAL*1000) - 1);
160
161
162
163
164
165
166
        if (i > 255) {
            i = 255;
        }
        return i;
    }
#endif

167
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
168
169
170
171
172
    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
173
        return 255;
174
175
176
177
178
179
    } else {
        // pin is low
        return 0;
    }
}

180
void led_set_intensity(pyb_led_t led, mp_int_t intensity) {
181
#if defined(PYBV4) || defined(PYBV10)
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    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);
}

200
201
202
203
204
205
206
207
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);
}

208
209
210
211
212
213
214
215
/******************************************************************************/
/* 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);
}

216
217
218
219
/// \classmethod \constructor(id)
/// Create an LED object associated with the given LED:
///
///   - `id` is the LED number, 1-4.
220
STATIC mp_obj_t led_obj_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
221
    // check arguments
Damien George's avatar
Damien George committed
222
    mp_arg_check_num(n_args, n_kw, 1, 1, false);
223
224

    // get led number
225
    mp_int_t led_id = mp_obj_get_int(args[0]);
226
227

    // check led number
228
    if (!(1 <= led_id && led_id <= NUM_LEDS)) {
229
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED %d does not exist", led_id));
230
231
232
    }

    // return static led object
233
    return (mp_obj_t)&pyb_led_obj[led_id - 1];
234
235
}

236
237
/// \method on()
/// Turn the LED on.
238
239
240
241
242
243
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;
}

244
245
/// \method off()
/// Turn the LED off.
246
247
248
249
250
251
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;
}

252
253
/// \method toggle()
/// Toggle the LED between on and off.
254
255
256
257
258
259
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;
}

260
261
262
263
/// \method intensity([value])
/// Get or set the LED intensity.  Intensity ranges between 0 (off) and 255 (full on).
/// If no argument is given, return the LED intensity.
/// If an argument is given, set the LED intensity and return `None`.
264
mp_obj_t led_obj_intensity(mp_uint_t n_args, const mp_obj_t *args) {
265
    pyb_led_obj_t *self = args[0];
266
267
    if (n_args == 1) {
        return mp_obj_new_int(led_get_intensity(self->led_id));
268
    } else {
269
        led_set_intensity(self->led_id, mp_obj_get_int(args[1]));
270
271
272
273
274
275
276
        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);
277
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_intensity_obj, 1, 2, led_obj_intensity);
278

279
280
281
282
283
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 },
284
285
};

286
287
STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);

288
const mp_obj_type_t pyb_led_type = {
289
    { &mp_type_type },
290
    .name = MP_QSTR_LED,
291
    .print = led_obj_print,
292
    .make_new = led_obj_make_new,
293
    .locals_dict = (mp_obj_t)&led_locals_dict,
294
};