2013-12-21 13:17:45 -05:00
|
|
|
#include <stdio.h>
|
2013-10-18 18:44:05 -04:00
|
|
|
#include <stm32f4xx.h>
|
|
|
|
#include <stm32f4xx_gpio.h>
|
2013-12-21 13:17:45 -05:00
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "obj.h"
|
2014-03-26 17:47:19 -04:00
|
|
|
#include "map.h"
|
2013-10-18 18:44:05 -04:00
|
|
|
#include "led.h"
|
2014-02-26 03:05:34 -05:00
|
|
|
#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]))
|
2014-01-05 12:38:41 -05:00
|
|
|
|
2013-10-23 15:39:20 -04:00
|
|
|
void led_init(void) {
|
2014-01-05 12:38:41 -05:00
|
|
|
/* GPIO structure */
|
2013-10-18 18:44:05 -04:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
2014-01-05 12:38:41 -05:00
|
|
|
|
|
|
|
/* Configure I/O speed, mode, output type and pull */
|
2013-10-18 18:44:05 -04:00
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
2014-01-05 12:38:41 -05:00
|
|
|
GPIO_InitStructure.GPIO_OType = PYB_OTYPE;
|
|
|
|
|
2014-02-26 03:05:34 -05:00
|
|
|
/* 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);
|
|
|
|
}
|
2013-10-18 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void led_state(pyb_led_t led, int state) {
|
2014-02-26 03:05:34 -05:00
|
|
|
if (led < 1 || led > NUM_LEDS) {
|
|
|
|
return;
|
2013-10-18 18:44:05 -04:00
|
|
|
}
|
2014-02-26 03:05:34 -05:00
|
|
|
const pin_obj_t *led_pin = gLed[led - 1];
|
2013-10-18 18:44:05 -04:00
|
|
|
if (state == 0) {
|
2014-01-05 12:38:41 -05:00
|
|
|
// turn LED off
|
2014-02-26 03:05:34 -05:00
|
|
|
PYB_LED_OFF(led_pin);
|
2013-10-18 18:44:05 -04:00
|
|
|
} else {
|
2014-01-05 12:38:41 -05:00
|
|
|
// turn LED on
|
2014-02-26 03:05:34 -05:00
|
|
|
PYB_LED_ON(led_pin);
|
2013-10-19 09:40:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void led_toggle(pyb_led_t led) {
|
2014-02-26 03:05:34 -05:00
|
|
|
if (led < 1 || led > NUM_LEDS) {
|
|
|
|
return;
|
2013-10-19 09:40:54 -04:00
|
|
|
}
|
2014-02-26 03:05:34 -05:00
|
|
|
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;
|
2013-10-19 09:40:54 -04:00
|
|
|
} else {
|
2014-02-26 03:05:34 -05:00
|
|
|
// pin is low, make it high
|
|
|
|
gpio->BSRRL = led_pin->pin_mask;
|
2013-10-18 18:44:05 -04:00
|
|
|
}
|
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
|
|
|
|
typedef struct _pyb_led_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
uint led_id;
|
|
|
|
} pyb_led_obj_t;
|
|
|
|
|
2014-01-15 18:02:53 -05:00
|
|
|
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2013-12-21 13:17:45 -05:00
|
|
|
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;
|
2014-01-05 12:38:41 -05:00
|
|
|
led_state(self->led_id, 1);
|
2013-12-21 13:17:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t led_obj_off(mp_obj_t self_in) {
|
|
|
|
pyb_led_obj_t *self = self_in;
|
2014-01-05 12:38:41 -05:00
|
|
|
led_state(self->led_id, 0);
|
2013-12-21 13:17:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-02-26 03:05:34 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
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);
|
2014-02-26 03:05:34 -05:00
|
|
|
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
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 },
|
2014-01-07 10:58:30 -05:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
static const mp_obj_type_t led_obj_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_Led,
|
2014-01-05 15:34:09 -05:00
|
|
|
.print = led_obj_print,
|
2014-03-26 17:47:19 -04:00
|
|
|
.locals_dict = (mp_obj_t)&led_locals_dict,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-01-21 18:28:03 -05:00
|
|
|
static mp_obj_t pyb_Led(mp_obj_t led_id) {
|
2013-12-21 13:17:45 -05:00
|
|
|
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;
|
|
|
|
}
|
2014-01-21 18:28:03 -05:00
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led);
|