Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 8c8d7f3

Browse files
author
Daniel Campora
committed
cc3200: Clean up pyb.Pin
Remove unused and unneeded functions, also create Pin.get_config() that returns the whole configuration of the pin. This reduces code size by ~500 bytes.
1 parent e4c899a commit 8c8d7f3

File tree

8 files changed

+40
-244
lines changed

8 files changed

+40
-244
lines changed

cc3200/application.mk

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ APP_MISC_SRC_C = $(addprefix misc/,\
8080
mperror.c \
8181
mpexception.c \
8282
mpsystick.c \
83-
pin_defs_cc3200.c \
8483
)
8584

8685
APP_MODS_SRC_C = $(addprefix mods/,\

cc3200/misc/pin_defs_cc3200.c

-55
This file was deleted.

cc3200/misc/pin_defs_cc3200.h

-41
This file was deleted.

cc3200/misc/pin_named_pins.c

-21
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@
3737
#include "pybpin.h"
3838
#include MICROPY_HAL_H
3939

40-
STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
41-
pin_named_pins_obj_t *self = self_in;
42-
mp_printf(print, "<Pin.%q>", self->name);
43-
}
44-
45-
const mp_obj_type_t pin_cpu_pins_obj_type = {
46-
{ &mp_type_type },
47-
.name = MP_QSTR_cpu,
48-
.print = pin_named_pins_obj_print,
49-
.locals_dict = (mp_obj_t)&pin_cpu_pins_locals_dict,
50-
};
5140

5241
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
5342
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
@@ -58,16 +47,6 @@ pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
5847
return NULL;
5948
}
6049

61-
pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num) {
62-
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
63-
for (uint i = 0; i < named_map->used; i++) {
64-
if (((pin_obj_t *)named_map->table[i].value)->pin_num == pin_num) {
65-
return named_map->table[i].value;
66-
}
67-
}
68-
return NULL;
69-
}
70-
7150
pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit) {
7251
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
7352
for (uint i = 0; i < named_map->used; i++) {

cc3200/mods/pybpin.c

+29-109
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,15 @@
6161
///
6262
/// Usage Model:
6363
///
64-
/// All CPU Pins are predefined as pyb.Pin.cpu.Name
65-
///
66-
/// GPIO9_pin = pyb.Pin.cpu.GPIO9
67-
///
68-
/// g = pyb.Pin(pyb.Pin.cpu.GPIO9, 0, pyb.Pin.IN)
69-
///
70-
/// CPU pins which correspond to the board pins are available
71-
/// as `pyb.cpu.Name`.
72-
///
73-
/// You can also use strings:
74-
///
75-
/// g = pyb.Pin('GPIO9', 0)
76-
///
77-
/// And finally, you can also pass a pin number directly:
78-
///
79-
/// g = pyb.Pin(64, 0)
80-
///
81-
/// To summarise, the following order determines how things get mapped into
82-
/// an ordinal pin number:
83-
///
84-
/// 1. Directly specify a Pin object
85-
/// 2. Supply a string which matches a CPU pin name
86-
/// 3. Provide a pin number
64+
/// g = pyb.Pin('GPIO9', af=0, mode=pyb.Pin.IN, type=pyb.Pin.STD, strength=pyb.Pin.S2MA)
8765
///
8866
/// \Interrupts:
8967
//// You can also configure the Pin to generate interrupts
9068
///
9169
/// Example callback:
9270
///
9371
/// def pincb(pin):
94-
/// print(pin.pin())
72+
/// print(pin.get_config().name)
9573
///
9674
/// extint = pyb.Pin('GPIO10', 0, pyb.Pin.INT_RISING, pyb.GPIO.STD_PD, pyb.S2MA)
9775
/// extint.callback (intmode=pyb.Pin.INT_RISING, handler=pincb)
@@ -166,28 +144,18 @@ void pin_init0(void) {
166144
pin_obj_t *pin_find(mp_obj_t user_obj) {
167145
pin_obj_t *pin_obj;
168146

169-
// If a pin was provided, then use it
147+
// if a pin was provided, use it
170148
if (MP_OBJ_IS_TYPE(user_obj, &pin_type)) {
171149
pin_obj = user_obj;
172150
return pin_obj;
173151
}
174152

175-
// See if the pin name matches a cpu pin
153+
// otherwise see if the pin name matches a cpu pin
176154
pin_obj = pin_find_named_pin(&pin_cpu_pins_locals_dict, user_obj);
177155
if (pin_obj) {
178156
return pin_obj;
179157
}
180158

181-
// See if the pin number matches a cpu pin
182-
mp_int_t pin_num;
183-
if (mp_obj_get_int_maybe(user_obj, &pin_num)) {
184-
// The Pins dictionary has pin indexes, so we must substract one from the value passed
185-
pin_obj = pin_find_pin(&pin_cpu_pins_locals_dict, (pin_num - 1));
186-
if (pin_obj) {
187-
return pin_obj;
188-
}
189-
}
190-
191159
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
192160
}
193161

@@ -387,7 +355,7 @@ STATIC const mp_arg_t pin_init_args[] = {
387355
{ MP_QSTR_af, MP_ARG_REQUIRED | MP_ARG_INT },
388356
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = GPIO_DIR_MODE_OUT} },
389357
{ MP_QSTR_type, MP_ARG_INT, {.u_int = PIN_TYPE_STD} },
390-
{ MP_QSTR_str, MP_ARG_INT, {.u_int = PIN_STRENGTH_4MA} },
358+
{ MP_QSTR_strength, MP_ARG_INT, {.u_int = PIN_STRENGTH_4MA} },
391359
};
392360
#define pin_INIT_NUM_ARGS MP_ARRAY_SIZE(pin_init_args)
393361

@@ -431,17 +399,18 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
431399
/// Return a string describing the pin object.
432400
STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
433401
pin_obj_t *self = self_in;
434-
uint32_t af = MAP_PinModeGet(self->pin_num);
435-
uint32_t type = pin_get_type(self);
436-
uint32_t strength = pin_get_strenght(self);
402+
uint32_t af = self->af;
403+
uint32_t type = self->type;
404+
uint32_t strength = self->strength;
437405

438406
// pin name
439407
mp_printf(print, "<Pin.cpu.%q, af=%u", self->name, af);
440408

409+
// pin mode
441410
if (af == PIN_MODE_0) {
442411
// IO mode
443412
qstr mode_qst;
444-
uint32_t mode = pin_get_mode(self);
413+
uint32_t mode = self->mode;
445414
if (mode == GPIO_DIR_MODE_IN) {
446415
mode_qst = MP_QSTR_IN;
447416
} else {
@@ -465,9 +434,9 @@ STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
465434
} else {
466435
type_qst = MP_QSTR_OD_PD;
467436
}
468-
mp_printf(print, ", pull=Pin.%q", type_qst);
437+
mp_printf(print, ", type=Pin.%q", type_qst);
469438

470-
// Strength
439+
// pin strength
471440
qstr str_qst;
472441
if (strength == PIN_STRENGTH_2MA) {
473442
str_qst = MP_QSTR_S2MA;
@@ -554,65 +523,25 @@ STATIC mp_obj_t pin_toggle(mp_obj_t self_in) {
554523
}
555524
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_toggle_obj, pin_toggle);
556525

557-
/// \method name()
558-
/// Get the pin name.
559-
STATIC mp_obj_t pin_name(mp_obj_t self_in) {
560-
pin_obj_t *self = self_in;
561-
return MP_OBJ_NEW_QSTR(self->name);
562-
}
563-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_name_obj, pin_name);
564-
565-
/// \method port()
566-
/// Get the pin port.
567-
STATIC mp_obj_t pin_port(mp_obj_t self_in) {
568-
pin_obj_t *self = self_in;
569-
return mp_obj_new_int(self->port);
570-
}
571-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_port_obj, pin_port);
572-
573-
/// \method pin()
574-
/// Get the pin number.
575-
STATIC mp_obj_t pin_pin(mp_obj_t self_in) {
576-
pin_obj_t *self = self_in;
577-
return MP_OBJ_NEW_SMALL_INT(self->pin_num);
578-
}
579-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pin_obj, pin_pin);
580-
581-
/// \method mode()
582-
/// Returns the currently configured mode of the gpio pin. The integer returned
583-
/// will match one of the allowed constants for the mode argument to the init
584-
/// function.
585-
STATIC mp_obj_t pin_mode(mp_obj_t self_in) {
586-
return MP_OBJ_NEW_SMALL_INT(pin_get_mode(self_in));
587-
}
588-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_mode_obj, pin_mode);
589-
590-
/// \method type()
591-
/// Returns the currently configured type of the pin. The integer returned
592-
/// will match one of the allowed constants for the type argument to the init
593-
/// function.
594-
STATIC mp_obj_t pin_type_get(mp_obj_t self_in) {
595-
return MP_OBJ_NEW_SMALL_INT(pin_get_type(self_in));
596-
}
597-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_type_obj, pin_type_get);
598-
599-
/// \method strength()
600-
/// Returns the currently configured drive strength of the pin. The integer returned
601-
/// will match one of the allowed constants for the strength argument to the init
602-
/// function.
603-
STATIC mp_obj_t pin_strength(mp_obj_t self_in) {
604-
return MP_OBJ_NEW_SMALL_INT(pin_get_strenght(self_in));
605-
}
606-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_strenght_obj, pin_strength);
526+
/// \method get_config()
527+
/// Returns a named tupple with the current configuration of the gpio pin
528+
STATIC mp_obj_t pin_get_config(mp_obj_t self_in) {
529+
STATIC const qstr pin_config_fields[] = {
530+
MP_QSTR_name, MP_QSTR_af, MP_QSTR_mode,
531+
MP_QSTR_type, MP_QSTR_strength
532+
};
607533

608-
/// \method af()
609-
/// Returns the currently configured alternate function of the gpio pin. The integer returned
610-
/// will match one of the allowed constants for the af argument to the init function.
611-
STATIC mp_obj_t pin_af(mp_obj_t self_in) {
612534
pin_obj_t *self = self_in;
613-
return MP_OBJ_NEW_SMALL_INT(MAP_PinModeGet(self->pin_num));
535+
mp_obj_t pin_config[5];
536+
pin_config[0] = MP_OBJ_NEW_QSTR(self->name);
537+
pin_config[1] = mp_obj_new_int(self->af);
538+
pin_config[2] = mp_obj_new_int(self->mode);
539+
pin_config[3] = mp_obj_new_int(self->type);
540+
pin_config[4] = mp_obj_new_int(self->strength);
541+
542+
return mp_obj_new_attrtuple(pin_config_fields, 5, pin_config);
614543
}
615-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
544+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_get_config_obj, pin_get_config);
616545

617546
/// \method callback(method, intmode, priority, pwrmode)
618547
/// Creates a callback object associated to a pin
@@ -750,18 +679,9 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
750679
{ MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pin_low_obj },
751680
{ MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pin_high_obj },
752681
{ MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&pin_toggle_obj },
753-
{ MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&pin_name_obj },
754-
{ MP_OBJ_NEW_QSTR(MP_QSTR_port), (mp_obj_t)&pin_port_obj },
755-
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin), (mp_obj_t)&pin_pin_obj },
756-
{ MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&pin_mode_obj },
757-
{ MP_OBJ_NEW_QSTR(MP_QSTR_type), (mp_obj_t)&pin_type_obj },
758-
{ MP_OBJ_NEW_QSTR(MP_QSTR_strength), (mp_obj_t)&pin_strenght_obj },
759-
{ MP_OBJ_NEW_QSTR(MP_QSTR_af), (mp_obj_t)&pin_af_obj },
682+
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_config), (mp_obj_t)&pin_get_config_obj },
760683
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pin_callback_obj },
761684

762-
// class attributes
763-
{ MP_OBJ_NEW_QSTR(MP_QSTR_cpu), (mp_obj_t)&pin_cpu_pins_obj_type },
764-
765685
// class constants
766686
/// \constant IN - set the pin to input mode
767687
/// \constant OUT - set the pin to output mode

cc3200/mods/pybpin.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#ifndef PYBPIN_H_
2929
#define PYBPIN_H_
3030

31-
// This file requires pin_defs_xxx.h (which has port specific enums and
32-
// defines, so we include it here. It should never be included directly
33-
34-
#include MICROPY_PIN_DEFS_PORT_H
35-
3631
#define PYBPIN_ANALOG_TYPE 0xFF
3732

33+
enum {
34+
PORT_A0 = GPIOA0_BASE,
35+
PORT_A1 = GPIOA1_BASE,
36+
PORT_A2 = GPIOA2_BASE,
37+
PORT_A3 = GPIOA3_BASE
38+
};
39+
3840
typedef struct {
3941
const mp_obj_base_t base;
4042
const qstr name;
@@ -70,7 +72,6 @@ void pin_config(pin_obj_t *self, uint af, uint mode, uint type, uint strength);
7072
void pin_extint_register(pin_obj_t *self, uint32_t intmode, uint32_t priority);
7173
pin_obj_t *pin_find(mp_obj_t user_obj);
7274
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name);
73-
pin_obj_t *pin_find_pin(const mp_obj_dict_t *named_pins, uint pin_num);
7475
pin_obj_t *pin_find_pin_by_port_bit (const mp_obj_dict_t *named_pins, uint port, uint bit);
7576
uint32_t pin_get_mode(const pin_obj_t *self);
7677
uint32_t pin_get_type(const pin_obj_t *self);

cc3200/mpconfigport.h

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len);
171171
#include "mpconfigboard.h"
172172

173173
#define MICROPY_HAL_H "cc3200_hal.h"
174-
#define MICROPY_PIN_DEFS_PORT_H "pin_defs_cc3200.h"
175174
#define MICROPY_PORT_HAS_TELNET (1)
176175
#define MICROPY_PORT_HAS_FTP (1)
177176
#define MICROPY_PY_SYS_PLATFORM "WiPy"

0 commit comments

Comments
 (0)