2013-12-21 18:17:45 +00:00
|
|
|
#include <stdio.h>
|
2013-10-18 23:44:05 +01:00
|
|
|
#include <stm32f4xx.h>
|
|
|
|
#include <stm32f4xx_gpio.h>
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "obj.h"
|
2013-10-18 23:44:05 +01:00
|
|
|
#include "led.h"
|
|
|
|
|
|
|
|
#define PYB_LED_R_PORT (GPIOA)
|
|
|
|
#define PYB_LED_R1_PIN (GPIO_Pin_8)
|
|
|
|
#define PYB_LED_R2_PIN (GPIO_Pin_10)
|
|
|
|
#define PYB_LED_G_PORT (GPIOC)
|
|
|
|
#define PYB_LED_G1_PIN (GPIO_Pin_4)
|
|
|
|
#define PYB_LED_G2_PIN (GPIO_Pin_5)
|
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
void led_init(void) {
|
2013-10-18 23:44:05 +01:00
|
|
|
// set the output high (so LED is off)
|
|
|
|
PYB_LED_R_PORT->BSRRL = PYB_LED_R1_PIN;
|
|
|
|
PYB_LED_R_PORT->BSRRL = PYB_LED_R2_PIN;
|
|
|
|
PYB_LED_G_PORT->BSRRL = PYB_LED_G1_PIN;
|
|
|
|
PYB_LED_G_PORT->BSRRL = PYB_LED_G2_PIN;
|
|
|
|
// make them open drain outputs
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = PYB_LED_R1_PIN | PYB_LED_R2_PIN;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
GPIO_Init(PYB_LED_R_PORT, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = PYB_LED_G1_PIN | PYB_LED_G2_PIN;
|
|
|
|
GPIO_Init(PYB_LED_G_PORT, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
|
|
|
|
void led_state(pyb_led_t led, int state) {
|
|
|
|
GPIO_TypeDef *port;
|
|
|
|
uint32_t pin;
|
|
|
|
switch (led) {
|
|
|
|
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break;
|
|
|
|
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break;
|
|
|
|
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break;
|
|
|
|
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
if (state == 0) {
|
2013-10-19 14:40:54 +01:00
|
|
|
// turn LED off (output is high)
|
2013-10-18 23:44:05 +01:00
|
|
|
port->BSRRL = pin;
|
|
|
|
} else {
|
2013-10-19 14:40:54 +01:00
|
|
|
// turn LED on (output is low)
|
|
|
|
port->BSRRH = pin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void led_toggle(pyb_led_t led) {
|
|
|
|
GPIO_TypeDef *port;
|
|
|
|
uint32_t pin;
|
|
|
|
switch (led) {
|
|
|
|
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break;
|
|
|
|
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break;
|
|
|
|
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break;
|
|
|
|
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
if (!(port->ODR & pin)) {
|
|
|
|
// turn LED off (output high)
|
|
|
|
port->BSRRL = pin;
|
|
|
|
} else {
|
|
|
|
// turn LED on (output low)
|
2013-10-18 23:44:05 +01:00
|
|
|
port->BSRRH = pin;
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
|
|
|
|
typedef struct _pyb_led_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
uint led_id;
|
|
|
|
} pyb_led_obj_t;
|
|
|
|
|
|
|
|
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
|
|
|
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;
|
|
|
|
switch (self->led_id) {
|
|
|
|
case 1: led_state(PYB_LED_G1, 1); break;
|
|
|
|
case 2: led_state(PYB_LED_G2, 1); break;
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t led_obj_off(mp_obj_t self_in) {
|
|
|
|
pyb_led_obj_t *self = self_in;
|
|
|
|
switch (self->led_id) {
|
|
|
|
case 1: led_state(PYB_LED_G1, 0); break;
|
|
|
|
case 2: led_state(PYB_LED_G2, 0); break;
|
|
|
|
}
|
|
|
|
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 const mp_obj_type_t led_obj_type = {
|
|
|
|
{ &mp_const_type },
|
|
|
|
"Led",
|
|
|
|
led_obj_print, // print
|
2014-01-04 20:21:15 +00:00
|
|
|
NULL, // make_new
|
2013-12-21 18:17:45 +00:00
|
|
|
NULL, // call_n
|
|
|
|
NULL, // unary_op
|
|
|
|
NULL, // binary_op
|
|
|
|
NULL, // getiter
|
|
|
|
NULL, // iternext
|
|
|
|
{ // method list
|
|
|
|
{ "on", &led_obj_on_obj },
|
|
|
|
{ "off", &led_obj_off_obj },
|
|
|
|
{ NULL, NULL },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_obj_t pyb_Led(mp_obj_t led_id) {
|
|
|
|
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;
|
|
|
|
}
|