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"
|
2013-10-18 18:44:05 -04:00
|
|
|
#include "led.h"
|
|
|
|
|
2014-01-05 12:38:41 -05:00
|
|
|
/* LED numbers, used internally */
|
2014-01-06 17:17:37 -05:00
|
|
|
#define PYB_LED_1 (1)
|
|
|
|
#define PYB_LED_2 (2)
|
|
|
|
#define PYB_LED_3 (3)
|
|
|
|
#define PYB_LED_4 (4)
|
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;
|
|
|
|
|
|
|
|
/* Turn off LEDs */
|
|
|
|
PYB_LED_OFF(PYB_LED1_PORT, PYB_LED1_PIN);
|
|
|
|
PYB_LED_OFF(PYB_LED2_PORT, PYB_LED2_PIN);
|
2014-01-05 23:41:56 -05:00
|
|
|
PYB_LED_OFF(PYB_LED3_PORT, PYB_LED3_PIN);
|
|
|
|
PYB_LED_OFF(PYB_LED4_PORT, PYB_LED4_PIN);
|
2014-01-05 12:38:41 -05:00
|
|
|
|
|
|
|
/* 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);
|
2013-10-18 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void led_state(pyb_led_t led, int state) {
|
|
|
|
GPIO_TypeDef *port;
|
|
|
|
uint32_t pin;
|
2014-01-05 12:38:41 -05:00
|
|
|
|
2013-10-18 18:44:05 -04:00
|
|
|
switch (led) {
|
2014-01-05 12:38:41 -05:00
|
|
|
case PYB_LED_1:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED1_PIN;
|
|
|
|
port = PYB_LED1_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_2:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED2_PIN;
|
|
|
|
port = PYB_LED2_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_3:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED3_PIN;
|
|
|
|
port = PYB_LED3_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_4:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED4_PIN;
|
|
|
|
port = PYB_LED4_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
2014-01-05 18:49:34 -05:00
|
|
|
default:
|
2014-01-05 12:38:41 -05:00
|
|
|
return;
|
2013-10-18 18:44:05 -04:00
|
|
|
}
|
2014-01-05 12:38:41 -05:00
|
|
|
|
2013-10-18 18:44:05 -04:00
|
|
|
if (state == 0) {
|
2014-01-05 12:38:41 -05:00
|
|
|
// turn LED off
|
|
|
|
PYB_LED_OFF(port, pin);
|
2013-10-18 18:44:05 -04:00
|
|
|
} else {
|
2014-01-05 12:38:41 -05:00
|
|
|
// turn LED on
|
|
|
|
PYB_LED_ON(port, pin);
|
2013-10-19 09:40:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void led_toggle(pyb_led_t led) {
|
|
|
|
GPIO_TypeDef *port;
|
|
|
|
uint32_t pin;
|
2014-01-05 12:38:41 -05:00
|
|
|
|
2013-10-19 09:40:54 -04:00
|
|
|
switch (led) {
|
2014-01-05 12:38:41 -05:00
|
|
|
case PYB_LED_1:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED1_PIN;
|
|
|
|
port = PYB_LED1_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_2:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED2_PIN;
|
|
|
|
port = PYB_LED2_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_3:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED3_PIN;
|
|
|
|
port = PYB_LED3_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
|
|
|
case PYB_LED_4:
|
2014-01-05 18:49:34 -05:00
|
|
|
pin = PYB_LED4_PIN;
|
|
|
|
port = PYB_LED4_PORT;
|
2014-01-05 12:38:41 -05:00
|
|
|
break;
|
2014-01-05 18:49:34 -05:00
|
|
|
default:
|
2014-01-05 12:38:41 -05:00
|
|
|
return;
|
2013-10-19 09:40:54 -04:00
|
|
|
}
|
2014-01-05 12:38:41 -05:00
|
|
|
|
2014-02-01 11:04:34 -05:00
|
|
|
// XXX this assumes LED is driven by a low MCU output (true for PYBv3, false for PYBv4)
|
2013-10-19 09:40:54 -04:00
|
|
|
if (!(port->ODR & pin)) {
|
2014-01-05 18:49:34 -05:00
|
|
|
// turn LED off
|
2014-01-05 12:38:41 -05:00
|
|
|
PYB_LED_OFF(port, pin);
|
2013-10-19 09:40:54 -04:00
|
|
|
} else {
|
|
|
|
// turn LED on (output low)
|
2014-01-05 12:38:41 -05:00
|
|
|
PYB_LED_ON(port, pin);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-07 10:58:30 -05:00
|
|
|
static const mp_method_t led_methods[] = {
|
|
|
|
{ "on", &led_obj_on_obj },
|
|
|
|
{ "off", &led_obj_off_obj },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
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-01-07 10:58:30 -05:00
|
|
|
.methods = led_methods,
|
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);
|