Merge remote-tracking branch 'upstream/master' into list_reverse

This commit is contained in:
John R. Lenton 2014-01-06 00:08:21 +00:00
commit a10dba7509
33 changed files with 272 additions and 209 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Compiled Sources
###################
*.o
*.a
*.elf
*.bin
*.map
*.hex
*.dis
# Packages
############
# Logs and Databases
######################
*.log
# VIM Swap Files
######################
*.swp

View File

@ -41,7 +41,7 @@ const mp_obj_type_t bool_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
static const mp_obj_bool_t false_obj = {{&bool_type}, false}; static const mp_obj_bool_t false_obj = {{&bool_type}, false};

View File

@ -43,7 +43,7 @@ const mp_obj_type_t bound_meth_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) { mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) {

View File

@ -33,7 +33,7 @@ const mp_obj_type_t cell_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_cell(mp_obj_t obj) { mp_obj_t mp_obj_new_cell(mp_obj_t obj) {

View File

@ -70,7 +70,7 @@ const mp_obj_type_t class_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_class(mp_map_t *class_locals) { mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {

View File

@ -42,7 +42,7 @@ const mp_obj_type_t closure_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) { mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) {

View File

@ -125,7 +125,7 @@ const mp_obj_type_t complex_type = {
complex_binary_op, // binary_op complex_binary_op, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ { NULL, NULL }, }, // method list .methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {

View File

@ -63,14 +63,11 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
const mp_obj_type_t dict_type = { const mp_obj_type_t dict_type = {
{ &mp_const_type }, { &mp_const_type },
"dict", "dict",
dict_print, // print .print = dict_print,
dict_make_new, // make_new .make_new = dict_make_new,
NULL, // call_n .binary_op = dict_binary_op,
NULL, // unary_op .getiter = NULL,
dict_binary_op, // binary_op .methods = {{NULL, NULL},},
NULL, // getiter
NULL, // iternext
{{NULL, NULL},}, // method list
}; };
mp_obj_t mp_obj_new_dict(int n_args) { mp_obj_t mp_obj_new_dict(int n_args) {

View File

@ -45,7 +45,7 @@ const mp_obj_type_t exception_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_exception(qstr id) { mp_obj_t mp_obj_new_exception(qstr id) {

View File

@ -79,14 +79,11 @@ static mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
const mp_obj_type_t float_type = { const mp_obj_type_t float_type = {
{ &mp_const_type }, { &mp_const_type },
"float", "float",
float_print, .print = float_print,
float_make_new, // make_new .make_new = float_make_new,
NULL, // call_n .unary_op = float_unary_op,
float_unary_op, .binary_op = float_binary_op,
float_binary_op, .methods = { { NULL, NULL }, },
NULL, // getiter
NULL, // iternext
{ { NULL, NULL }, }, // method list
}; };
mp_obj_t mp_obj_new_float(mp_float_t value) { mp_obj_t mp_obj_new_float(mp_float_t value) {

View File

@ -79,7 +79,7 @@ const mp_obj_type_t fun_native_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ // method list .methods = {
{NULL, NULL}, // end-of-list sentinel {NULL, NULL}, // end-of-list sentinel
}, },
}; };
@ -181,7 +181,7 @@ const mp_obj_type_t fun_bc_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ // method list .methods = {
{NULL, NULL}, // end-of-list sentinel {NULL, NULL}, // end-of-list sentinel
}, },
}; };
@ -295,7 +295,7 @@ static const mp_obj_type_t fun_asm_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ // method list .methods = {
{NULL, NULL}, // end-of-list sentinel {NULL, NULL}, // end-of-list sentinel
}, },
}; };

View File

@ -46,7 +46,7 @@ const mp_obj_type_t gen_wrap_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) { mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) {
@ -101,7 +101,7 @@ const mp_obj_type_t gen_instance_type = {
NULL, // binary_op NULL, // binary_op
gen_instance_getiter, // getiter gen_instance_getiter, // getiter
gen_instance_iternext, // iternext gen_instance_iternext, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
// args are in reverse order in the array // args are in reverse order in the array

View File

@ -99,7 +99,7 @@ const mp_obj_type_t instance_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_instance(mp_obj_t class) { mp_obj_t mp_obj_new_instance(mp_obj_t class) {

View File

@ -33,14 +33,8 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
const mp_obj_type_t int_type = { const mp_obj_type_t int_type = {
{ &mp_const_type }, { &mp_const_type },
"int", "int",
NULL, .make_new = int_make_new,
int_make_new, // make_new .methods = { { NULL, NULL }, },
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ { NULL, NULL }, }, // method list
}; };
mp_obj_t mp_obj_new_int(machine_int_t value) { mp_obj_t mp_obj_new_int(machine_int_t value) {

View File

@ -182,13 +182,17 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
assert(MP_OBJ_IS_TYPE(args[0], &list_type)); assert(MP_OBJ_IS_TYPE(args[0], &list_type));
mp_obj_list_t *self = args[0]; mp_obj_list_t *self = args[0];
mp_obj_t *value = args[1]; mp_obj_t *value = args[1];
uint start = 0;
uint stop = self->len;
uint start = mp_get_index(self->base.type, self->len, if (n_args >= 3) {
n_args >= 3 ? args[2] : mp_obj_new_int(0)); start = mp_get_index(self->base.type, self->len, args[2]);
uint stop = mp_get_index(self->base.type, self->len, if (n_args >= 4) {
n_args >= 4 ? args[3] : mp_obj_new_int(-1)); stop = mp_get_index(self->base.type, self->len, args[3]);
}
}
for (uint i = start; i <= stop; i++) { for (uint i = start; i < stop; i++) {
if (mp_obj_equal(self->items[i], value)) { if (mp_obj_equal(self->items[i], value)) {
return mp_obj_new_int(i); return mp_obj_new_int(i);
} }
@ -259,14 +263,12 @@ static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
const mp_obj_type_t list_type = { const mp_obj_type_t list_type = {
{ &mp_const_type }, { &mp_const_type },
"list", "list",
list_print, // print .print = list_print,
list_make_new, // make_new .make_new = list_make_new,
NULL, // call_n .unary_op = NULL,
NULL, // unary_op .binary_op = list_binary_op,
list_binary_op, // binary_op .getiter = list_getiter,
list_getiter, // getiter .methods = {
NULL, // iternext
{ // method list
{ "append", &list_append_obj }, { "append", &list_append_obj },
{ "clear", &list_clear_obj }, { "clear", &list_clear_obj },
{ "copy", &list_copy_obj }, { "copy", &list_copy_obj },
@ -341,14 +343,8 @@ mp_obj_t list_it_iternext(mp_obj_t self_in) {
static const mp_obj_type_t list_it_type = { static const mp_obj_type_t list_it_type = {
{ &mp_const_type }, { &mp_const_type },
"list_iterator", "list_iterator",
NULL, // print .iternext = list_it_iternext,
NULL, // make_new .methods = { { NULL, NULL }, },
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
list_it_iternext, // iternext
{ { NULL, NULL }, }, // method list
}; };
mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) { mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {

View File

@ -31,7 +31,7 @@ const mp_obj_type_t module_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_module(qstr module_name) { mp_obj_t mp_obj_new_module(qstr module_name) {

View File

@ -17,14 +17,8 @@ void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ob
const mp_obj_type_t none_type = { const mp_obj_type_t none_type = {
{ &mp_const_type }, { &mp_const_type },
"NoneType", "NoneType",
none_print, // print .print = none_print,
NULL, // make_new .methods = {{NULL, NULL},},
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{{NULL, NULL},}, // method list
}; };
static const mp_obj_none_t none_obj = {{&none_type}}; static const mp_obj_none_t none_obj = {{&none_type}};

View File

@ -32,7 +32,7 @@ static const mp_obj_type_t range_type = {
NULL, // binary_op NULL, // binary_op
range_getiter, range_getiter,
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
// range is a class and instances are immutable sequence objects // range is a class and instances are immutable sequence objects
@ -77,7 +77,7 @@ static const mp_obj_type_t range_it_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
range_it_iternext, range_it_iternext,
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) { mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) {

View File

@ -64,7 +64,7 @@ const mp_obj_type_t set_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ { NULL, NULL }, }, // method list .methods = { { NULL, NULL }, },
}; };
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {

View File

@ -30,7 +30,7 @@ const mp_obj_type_t ellipsis_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{{NULL, NULL},}, // method list .methods = {{NULL, NULL},},
}; };
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
@ -57,14 +57,8 @@ void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_o
const mp_obj_type_t slice_type = { const mp_obj_type_t slice_type = {
{ &mp_const_type }, { &mp_const_type },
"slice", "slice",
slice_print, .print = slice_print,
NULL, // call_n .methods = { { NULL, NULL }, },
NULL, // make_new
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ { NULL, NULL }, }, // method list
}; };
// TODO: Make sure to handle "empty" values, which are signified by None in CPython // TODO: Make sure to handle "empty" values, which are signified by None in CPython

View File

@ -187,14 +187,10 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
const mp_obj_type_t str_type = { const mp_obj_type_t str_type = {
{ &mp_const_type }, { &mp_const_type },
"str", "str",
str_print, // print .print = str_print,
NULL, // make_new .binary_op = str_binary_op,
NULL, // call_n .getiter = str_getiter,
NULL, // unary_op .methods = {
str_binary_op, // binary_op
str_getiter, // getiter
NULL, // iternext
{ // method list
{ "join", &str_join_obj }, { "join", &str_join_obj },
{ "format", &str_format_obj }, { "format", &str_format_obj },
{ NULL, NULL }, // end-of-list sentinel { NULL, NULL }, // end-of-list sentinel
@ -238,14 +234,8 @@ mp_obj_t str_it_iternext(mp_obj_t self_in) {
static const mp_obj_type_t str_it_type = { static const mp_obj_type_t str_it_type = {
{ &mp_const_type }, { &mp_const_type },
"str_iterator", "str_iterator",
NULL, // print .iternext = str_it_iternext,
NULL, // make_new .methods = { { NULL, NULL }, },
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
str_it_iternext, // iternext
{ { NULL, NULL }, }, // method str
}; };
mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) { mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) {

View File

@ -99,14 +99,11 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
const mp_obj_type_t tuple_type = { const mp_obj_type_t tuple_type = {
{ &mp_const_type }, { &mp_const_type },
"tuple", "tuple",
tuple_print, // print .print = tuple_print,
tuple_make_new, // make_new .make_new = tuple_make_new,
NULL, // call_n .binary_op = tuple_binary_op,
NULL, // unary_op .getiter = tuple_getiter,
tuple_binary_op, // binary_op .methods = {{NULL, NULL},},
tuple_getiter, // getiter
NULL, // iternext
{{NULL, NULL},}, // method list
}; };
// the zero-length tuple // the zero-length tuple
@ -168,14 +165,8 @@ static mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
static const mp_obj_type_t tuple_it_type = { static const mp_obj_type_t tuple_it_type = {
{ &mp_const_type }, { &mp_const_type },
"tuple_iterator", "tuple_iterator",
NULL, // print .iternext = tuple_it_iternext,
NULL, // make_new .methods = {{NULL, NULL},},
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
tuple_it_iternext,
{{NULL, NULL},}, // method list
}; };
static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) { static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) {

View File

@ -25,12 +25,7 @@ static mp_obj_t type_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args)
const mp_obj_type_t mp_const_type = { const mp_obj_type_t mp_const_type = {
{ &mp_const_type }, { &mp_const_type },
"type", "type",
type_print, // print .print = type_print,
NULL, // make_new .call_n = type_call_n,
type_call_n, // call_n .methods = {{NULL, NULL},},
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{{NULL, NULL},}, // method list
}; };

View File

@ -36,10 +36,10 @@ const static qstr_pool_t const_pool = {
}, },
}; };
static qstr_pool_t *last_pool = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left static qstr_pool_t *last_pool;
void qstr_init(void) { void qstr_init(void) {
// nothing to do! last_pool = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left
} }
static qstr qstr_add(const char *str) { static qstr qstr_add(const char *str) {

View File

@ -5,12 +5,13 @@ CC3KSRC=cc3k
PYSRC=../py PYSRC=../py
BUILD=build BUILD=build
DFU=../tools/dfu.py DFU=../tools/dfu.py
TARGET=PYBOARD
AS = arm-none-eabi-as AS = arm-none-eabi-as
CC = arm-none-eabi-gcc CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld LD = arm-none-eabi-ld
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000 CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000
CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
#CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE #CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE
LDFLAGS = --nostdlib -T stm32f405.ld LDFLAGS = --nostdlib -T stm32f405.ld

View File

@ -329,14 +329,8 @@ static MP_DEFINE_CONST_FUN_OBJ_1(i2c_obj_stop_obj, i2c_obj_stop);
static const mp_obj_type_t i2c_obj_type = { static const mp_obj_type_t i2c_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"I2C", "I2C",
i2c_obj_print, // print .print = i2c_obj_print,
NULL, // make_new .methods = {
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ // method list
{ "start", &i2c_obj_start_obj }, { "start", &i2c_obj_start_obj },
{ "write", &i2c_obj_write_obj }, { "write", &i2c_obj_write_obj },
{ "read", &i2c_obj_read_obj }, { "read", &i2c_obj_read_obj },

166
stm/led.c
View File

@ -7,66 +7,144 @@
#include "obj.h" #include "obj.h"
#include "led.h" #include "led.h"
#define PYB_LED_R_PORT (GPIOA) /* LED numbers, used internally */
#define PYB_LED_R1_PIN (GPIO_Pin_8) #define PYB_LED_1 (0)
#define PYB_LED_R2_PIN (GPIO_Pin_10) #define PYB_LED_2 (1)
#define PYB_LED_G_PORT (GPIOC) #define PYB_LED_3 (2)
#define PYB_LED_G1_PIN (GPIO_Pin_4) #define PYB_LED_4 (3)
#define PYB_LED_G2_PIN (GPIO_Pin_5)
#if defined(PYBOARD)
#define PYB_LED1_PORT (GPIOA)
#define PYB_LED1_PIN (GPIO_Pin_8)
#define PYB_LED2_PORT (GPIOA)
#define PYB_LED2_PIN (GPIO_Pin_10)
#define PYB_LED3_PORT (GPIOC)
#define PYB_LED3_PIN (GPIO_Pin_4)
#define PYB_LED4_PORT (GPIOC)
#define PYB_LED4_PIN (GPIO_Pin_5)
#define PYB_OTYPE (GPIO_OType_OD)
#define PYB_LED_ON(port, pin) (port->BSRRH = pin)
#define PYB_LED_OFF(port, pin) (port->BSRRL = pin)
#elif defined(STM32F4DISC)
#define PYB_LED1_PORT (GPIOD)
#define PYB_LED1_PIN (GPIO_Pin_14)
#define PYB_LED2_PORT (GPIOD)
#define PYB_LED2_PIN (GPIO_Pin_12)
#define PYB_LED3_PORT (GPIOD)
#define PYB_LED3_PIN (GPIO_Pin_15)
#define PYB_LED4_PORT (GPIOD)
#define PYB_LED4_PIN (GPIO_Pin_13)
#define PYB_OTYPE (GPIO_OType_PP)
#define PYB_LED_ON(port, pin) (port->BSRRL = pin)
#define PYB_LED_OFF(port, pin) (port->BSRRH = pin)
#endif
void led_init(void) { void led_init(void) {
// set the output high (so LED is off) /* GPIO structure */
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_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = PYB_LED_R1_PIN | PYB_LED_R2_PIN;
/* Configure I/O speed, mode, output type and pull */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(PYB_LED_R_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_OType = PYB_OTYPE;
GPIO_InitStructure.GPIO_Pin = PYB_LED_G1_PIN | PYB_LED_G2_PIN;
GPIO_Init(PYB_LED_G_PORT, &GPIO_InitStructure); /* Turn off LEDs */
PYB_LED_OFF(PYB_LED1_PORT, PYB_LED1_PIN);
PYB_LED_OFF(PYB_LED2_PORT, PYB_LED2_PIN);
PYB_LED_OFF(PYB_LED3_PORT, PYB_LED1_PIN);
PYB_LED_OFF(PYB_LED4_PORT, PYB_LED2_PIN);
/* 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);
} }
void led_state(pyb_led_t led, int state) { void led_state(pyb_led_t led, int state) {
GPIO_TypeDef *port; GPIO_TypeDef *port;
uint32_t pin; uint32_t pin;
switch (led) { switch (led) {
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break; case PYB_LED_1:
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break; pin = PYB_LED1_PIN;
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break; port = PYB_LED1_PORT;
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break; break;
default: return; case PYB_LED_2:
pin = PYB_LED2_PIN;
port = PYB_LED2_PORT;
break;
case PYB_LED_3:
pin = PYB_LED3_PIN;
port = PYB_LED3_PORT;
break;
case PYB_LED_4:
pin = PYB_LED4_PIN;
port = PYB_LED4_PORT;
break;
default:
return;
} }
if (state == 0) { if (state == 0) {
// turn LED off (output is high) // turn LED off
port->BSRRL = pin; PYB_LED_OFF(port, pin);
} else { } else {
// turn LED on (output is low) // turn LED on
port->BSRRH = pin; PYB_LED_ON(port, pin);
} }
} }
void led_toggle(pyb_led_t led) { void led_toggle(pyb_led_t led) {
GPIO_TypeDef *port; GPIO_TypeDef *port;
uint32_t pin; uint32_t pin;
switch (led) { switch (led) {
case PYB_LED_R1: port = PYB_LED_R_PORT; pin = PYB_LED_R1_PIN; break; case PYB_LED_1:
case PYB_LED_R2: port = PYB_LED_R_PORT; pin = PYB_LED_R2_PIN; break; pin = PYB_LED1_PIN;
case PYB_LED_G1: port = PYB_LED_G_PORT; pin = PYB_LED_G1_PIN; break; port = PYB_LED1_PORT;
case PYB_LED_G2: port = PYB_LED_G_PORT; pin = PYB_LED_G2_PIN; break; break;
default: return; case PYB_LED_2:
pin = PYB_LED2_PIN;
port = PYB_LED2_PORT;
break;
case PYB_LED_3:
pin = PYB_LED3_PIN;
port = PYB_LED3_PORT;
break;
case PYB_LED_4:
pin = PYB_LED4_PIN;
port = PYB_LED4_PORT;
break;
default:
return;
} }
if (!(port->ODR & pin)) { if (!(port->ODR & pin)) {
// turn LED off (output high) // turn LED off
port->BSRRL = pin; PYB_LED_OFF(port, pin);
} else { } else {
// turn LED on (output low) // turn LED on (output low)
port->BSRRH = pin; PYB_LED_ON(port, pin);
} }
} }
@ -85,19 +163,13 @@ void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp
mp_obj_t led_obj_on(mp_obj_t self_in) { mp_obj_t led_obj_on(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in; pyb_led_obj_t *self = self_in;
switch (self->led_id) { led_state(self->led_id, 1);
case 1: led_state(PYB_LED_G1, 1); break;
case 2: led_state(PYB_LED_G2, 1); break;
}
return mp_const_none; return mp_const_none;
} }
mp_obj_t led_obj_off(mp_obj_t self_in) { mp_obj_t led_obj_off(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in; pyb_led_obj_t *self = self_in;
switch (self->led_id) { led_state(self->led_id, 0);
case 1: led_state(PYB_LED_G1, 0); break;
case 2: led_state(PYB_LED_G2, 0); break;
}
return mp_const_none; return mp_const_none;
} }
@ -107,14 +179,8 @@ static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
static const mp_obj_type_t led_obj_type = { static const mp_obj_type_t led_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"Led", "Led",
led_obj_print, // print .print = led_obj_print,
NULL, // make_new .methods = {
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ // method list
{ "on", &led_obj_on_obj }, { "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj }, { "off", &led_obj_off_obj },
{ NULL, NULL }, { NULL, NULL },

View File

@ -3,6 +3,11 @@ typedef enum {
PYB_LED_R2 = 1, PYB_LED_R2 = 1,
PYB_LED_G1 = 2, PYB_LED_G1 = 2,
PYB_LED_G2 = 3, PYB_LED_G2 = 3,
//STM32F4DISC
PYB_LED_R = 0,
PYB_LED_G = 1,
PYB_LED_B = 2,
PYB_LED_O = 3,
} pyb_led_t; } pyb_led_t;
void led_init(void); void led_init(void);

View File

@ -699,7 +699,7 @@ static const mp_obj_type_t file_obj_type = {
NULL, // binary_op NULL, // binary_op
NULL, // getiter NULL, // getiter
NULL, // iternext NULL, // iternext
{ // method list .methods = {
{ "read", &file_obj_read_obj }, { "read", &file_obj_read_obj },
{ "write", &file_obj_write_obj }, { "write", &file_obj_write_obj },
{ "close", &file_obj_close_obj }, { "close", &file_obj_close_obj },

View File

@ -140,14 +140,8 @@ static MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
static const mp_obj_type_t servo_obj_type = { static const mp_obj_type_t servo_obj_type = {
{ &mp_const_type }, { &mp_const_type },
"Servo", "Servo",
servo_obj_print, // print .print = servo_obj_print,
NULL, // make_new .methods = {
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ // method list
{ "angle", &servo_obj_angle_obj }, { "angle", &servo_obj_angle_obj },
{ NULL, NULL }, { NULL, NULL },
} }

View File

@ -298,4 +298,10 @@ void EXTI15_10_IRQHandler(void) {
} }
} }
#if defined(STM32F4DISC)
void EXTI0_IRQHandler(void) {
// clear pending interrupt bit
EXTI_ClearITPendingBit(EXTI_Line0);
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -9,36 +9,53 @@
#include "obj.h" #include "obj.h"
#include "usrsw.h" #include "usrsw.h"
#define PYB_USRSW_PORT (GPIOA) #if defined (PYBOARD)
#define PYB_USRSW_PIN (GPIO_Pin_13) #define USRSW_PORT (GPIOA)
#define USRSW_PIN (GPIO_Pin_13)
#define USRSW_PUPD (GPIO_PuPd_UP)
#define USRSW_EXTI_PIN (EXTI_PinSource13)
#define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
#define USRSW_EXTI_LINE (EXTI_Line13)
#define USRSW_EXTI_IRQN (EXTI15_10_IRQn)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Rising)
#elif defined (STM32F4DISC)
#define USRSW_PORT (GPIOA)
#define USRSW_PIN (GPIO_Pin_0)
#define USRSW_PUPD (GPIO_PuPd_NOPULL)
#define USRSW_EXTI_PIN (EXTI_PinSource0)
#define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA)
#define USRSW_EXTI_LINE (EXTI_Line0)
#define USRSW_EXTI_IRQN (EXTI0_IRQn)
#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling)
#endif
void switch_init(void) { void switch_init(void) {
// make it an input with pull-up // make it an input with pull-up
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN; GPIO_InitStructure.GPIO_Pin = USRSW_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_PuPd = USRSW_PUPD;
GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure); GPIO_Init(USRSW_PORT, &GPIO_InitStructure);
// the rest does the EXTI interrupt // the rest does the EXTI interrupt
/* Enable SYSCFG clock */ /* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Connect EXTI Line13 to PA13 pin */ /* Connect EXTI Line to GPIO pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource13); SYSCFG_EXTILineConfig(USRSW_EXTI_PORT, USRSW_EXTI_PIN);
/* Configure EXTI Line13, rising edge */ /* Configure EXTI Line */
EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line13; EXTI_InitStructure.EXTI_Line = USRSW_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_Trigger = USRSW_EXTI_EDGE;
EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI15_10 Interrupt to the lowest priority */ /* Enable and set EXTI15_10 Interrupt to the lowest priority */
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; NVIC_InitStructure.NVIC_IRQChannel = USRSW_EXTI_IRQN;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
@ -46,13 +63,24 @@ void switch_init(void) {
} }
int switch_get(void) { int switch_get(void) {
if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) { #if defined (PYBOARD)
if (USRSW_PORT->IDR & USRSW_PIN) {
// pulled high, so switch is not pressed // pulled high, so switch is not pressed
return 0; return 0;
} else { } else {
// pulled low, so switch is pressed // pulled low, so switch is pressed
return 1; return 1;
} }
#elif defined (STM32F4DISC)
/* switch pulled down */
if (USRSW_PORT->IDR & USRSW_PIN) {
// pulled high, so switch is pressed
return 1;
} else {
// pulled low, so switch is not pressed
return 0;
}
#endif
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -191,14 +191,14 @@ static MP_DEFINE_CONST_FUN_OBJ_2(test_set_obj, test_set);
static const mp_obj_type_t test_type = { static const mp_obj_type_t test_type = {
{ &mp_const_type }, { &mp_const_type },
"Test", "Test",
test_print, // print .print = test_print,
NULL, // make_new .make_new = NULL,
NULL, // call_n .call_n = NULL,
NULL, // unary_op .unary_op = NULL,
NULL, // binary_op .binary_op = NULL,
NULL, // getiter .getiter = NULL,
NULL, // iternext .iternext = NULL,
{ // method list .methods = {
{ "get", &test_get_obj }, { "get", &test_get_obj },
{ "set", &test_set_obj }, { "set", &test_set_obj },
{ NULL, NULL }, { NULL, NULL },