2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 01:06:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2017-01-31 01:36:04 +00:00
|
|
|
#include "extmod/virtpin.h"
|
2015-01-01 21:13:30 +00:00
|
|
|
#include "pin.h"
|
2017-06-15 08:17:36 +01:00
|
|
|
#include "extint.h"
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class Pin - control I/O pins
|
|
|
|
///
|
|
|
|
/// A pin is the basic object to control I/O pins. It has methods to set
|
|
|
|
/// the mode of the pin (input, output, etc) and methods to get and set the
|
|
|
|
/// digital logic level. For analog control of a pin, see the ADC class.
|
|
|
|
///
|
|
|
|
/// Usage Model:
|
|
|
|
///
|
|
|
|
/// All Board Pins are predefined as pyb.Pin.board.Name
|
|
|
|
///
|
|
|
|
/// x1_pin = pyb.Pin.board.X1
|
|
|
|
///
|
|
|
|
/// g = pyb.Pin(pyb.Pin.board.X1, pyb.Pin.IN)
|
|
|
|
///
|
|
|
|
/// CPU pins which correspond to the board pins are available
|
|
|
|
/// as `pyb.cpu.Name`. For the CPU pins, the names are the port letter
|
|
|
|
/// followed by the pin number. On the PYBv1.0, `pyb.Pin.board.X1` and
|
|
|
|
/// `pyb.Pin.cpu.B6` are the same pin.
|
|
|
|
///
|
|
|
|
/// You can also use strings:
|
|
|
|
///
|
|
|
|
/// g = pyb.Pin('X1', pyb.Pin.OUT_PP)
|
|
|
|
///
|
|
|
|
/// Users can add their own names:
|
|
|
|
///
|
2014-07-22 15:57:36 +01:00
|
|
|
/// MyMapperDict = { 'LeftMotorDir' : pyb.Pin.cpu.C12 }
|
|
|
|
/// pyb.Pin.dict(MyMapperDict)
|
2014-04-29 22:55:34 +01:00
|
|
|
/// g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)
|
|
|
|
///
|
|
|
|
/// and can query mappings
|
|
|
|
///
|
|
|
|
/// pin = pyb.Pin("LeftMotorDir")
|
|
|
|
///
|
|
|
|
/// Users can also add their own mapping function:
|
|
|
|
///
|
|
|
|
/// def MyMapper(pin_name):
|
|
|
|
/// if pin_name == "LeftMotorDir":
|
|
|
|
/// return pyb.Pin.cpu.A0
|
|
|
|
///
|
|
|
|
/// pyb.Pin.mapper(MyMapper)
|
|
|
|
///
|
|
|
|
/// So, if you were to call: `pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP)`
|
|
|
|
/// then `"LeftMotorDir"` is passed directly to the mapper function.
|
|
|
|
///
|
|
|
|
/// To summarise, the following order determines how things get mapped into
|
|
|
|
/// an ordinal pin number:
|
|
|
|
///
|
|
|
|
/// 1. Directly specify a pin object
|
|
|
|
/// 2. User supplied mapping function
|
|
|
|
/// 3. User supplied mapping (object must be usable as a dictionary key)
|
|
|
|
/// 4. Supply a string which matches a board pin
|
|
|
|
/// 5. Supply a string which matches a CPU port/pin
|
|
|
|
///
|
|
|
|
/// You can set `pyb.Pin.debug(True)` to get some debug information about
|
|
|
|
/// how a particular object gets mapped to a pin.
|
2014-04-18 22:38:09 +01:00
|
|
|
|
|
|
|
// Pin class variables
|
|
|
|
STATIC bool pin_class_debug;
|
|
|
|
|
2014-08-04 11:09:51 +01:00
|
|
|
void pin_init0(void) {
|
2015-01-07 23:38:50 +00:00
|
|
|
MP_STATE_PORT(pin_class_mapper) = mp_const_none;
|
|
|
|
MP_STATE_PORT(pin_class_map_dict) = mp_const_none;
|
2014-04-18 22:38:09 +01:00
|
|
|
pin_class_debug = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C API used to convert a user-supplied pin name into an ordinal pin number.
|
|
|
|
const pin_obj_t *pin_find(mp_obj_t user_obj) {
|
|
|
|
const pin_obj_t *pin_obj;
|
|
|
|
|
|
|
|
// If a pin was provided, then use it
|
2019-01-30 11:05:48 +00:00
|
|
|
if (mp_obj_is_type(user_obj, &pin_type)) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj = MP_OBJ_TO_PTR(user_obj);
|
2014-04-18 22:38:09 +01:00
|
|
|
if (pin_class_debug) {
|
|
|
|
printf("Pin map passed pin ");
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR);
|
2014-04-18 22:38:09 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return pin_obj;
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
if (MP_STATE_PORT(pin_class_mapper) != mp_const_none) {
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_t o = mp_call_function_1(MP_STATE_PORT(pin_class_mapper), user_obj);
|
|
|
|
if (o != mp_const_none) {
|
2019-01-30 11:05:48 +00:00
|
|
|
if (!mp_obj_is_type(o, &pin_type)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("Pin.mapper didn't return a Pin object"));
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
if (pin_class_debug) {
|
|
|
|
printf("Pin.mapper maps ");
|
|
|
|
mp_obj_print(user_obj, PRINT_REPR);
|
|
|
|
printf(" to ");
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_print(o, PRINT_STR);
|
2014-04-18 22:38:09 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_TO_PTR(o);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
// The pin mapping function returned mp_const_none, fall through to
|
|
|
|
// other lookup methods.
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
if (MP_STATE_PORT(pin_class_map_dict) != mp_const_none) {
|
|
|
|
mp_map_t *pin_map_map = mp_obj_dict_get_map(MP_STATE_PORT(pin_class_map_dict));
|
2014-04-18 22:38:09 +01:00
|
|
|
mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP);
|
2018-07-08 14:25:11 +01:00
|
|
|
if (elem != NULL && elem->value != MP_OBJ_NULL) {
|
|
|
|
mp_obj_t o = elem->value;
|
2014-04-18 22:38:09 +01:00
|
|
|
if (pin_class_debug) {
|
|
|
|
printf("Pin.map_dict maps ");
|
|
|
|
mp_obj_print(user_obj, PRINT_REPR);
|
|
|
|
printf(" to ");
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_print(o, PRINT_STR);
|
2014-04-18 22:38:09 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_TO_PTR(o);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the pin name matches a board pin
|
2014-07-22 15:57:36 +01:00
|
|
|
pin_obj = pin_find_named_pin(&pin_board_pins_locals_dict, user_obj);
|
2014-04-18 22:38:09 +01:00
|
|
|
if (pin_obj) {
|
|
|
|
if (pin_class_debug) {
|
|
|
|
printf("Pin.board maps ");
|
|
|
|
mp_obj_print(user_obj, PRINT_REPR);
|
|
|
|
printf(" to ");
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR);
|
2014-04-18 22:38:09 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return pin_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the pin name matches a cpu pin
|
2014-07-22 15:57:36 +01:00
|
|
|
pin_obj = pin_find_named_pin(&pin_cpu_pins_locals_dict, user_obj);
|
2014-04-18 22:38:09 +01:00
|
|
|
if (pin_obj) {
|
|
|
|
if (pin_class_debug) {
|
|
|
|
printf("Pin.cpu maps ");
|
|
|
|
mp_obj_print(user_obj, PRINT_REPR);
|
|
|
|
printf(" to ");
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR);
|
2014-04-18 22:38:09 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return pin_obj;
|
|
|
|
}
|
|
|
|
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%s) doesn't exist"), mp_obj_str_get_str(user_obj));
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method __str__()
|
|
|
|
/// Return a string describing the pin object.
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
|
2014-08-09 09:19:37 +01:00
|
|
|
// pin name
|
2015-04-16 18:40:06 +01:00
|
|
|
mp_printf(print, "Pin(Pin.cpu.%q, mode=Pin.", self->name);
|
2014-07-22 15:57:36 +01:00
|
|
|
|
|
|
|
uint32_t mode = pin_get_mode(self);
|
2014-08-09 09:19:37 +01:00
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
if (mode == GPIO_MODE_ANALOG) {
|
2014-08-09 09:19:37 +01:00
|
|
|
// analog
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, "ANALOG)");
|
2014-08-09 09:19:37 +01:00
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
} else {
|
2014-08-09 09:19:37 +01:00
|
|
|
// IO mode
|
|
|
|
bool af = false;
|
|
|
|
qstr mode_qst;
|
2014-07-22 15:57:36 +01:00
|
|
|
if (mode == GPIO_MODE_INPUT) {
|
2014-08-09 09:19:37 +01:00
|
|
|
mode_qst = MP_QSTR_IN;
|
|
|
|
} else if (mode == GPIO_MODE_OUTPUT_PP) {
|
2016-08-23 07:22:04 +01:00
|
|
|
mode_qst = MP_QSTR_OUT;
|
2014-08-09 09:19:37 +01:00
|
|
|
} else if (mode == GPIO_MODE_OUTPUT_OD) {
|
2016-08-23 07:22:04 +01:00
|
|
|
mode_qst = MP_QSTR_OPEN_DRAIN;
|
2014-07-22 15:57:36 +01:00
|
|
|
} else {
|
2014-08-09 09:19:37 +01:00
|
|
|
af = true;
|
2014-07-22 15:57:36 +01:00
|
|
|
if (mode == GPIO_MODE_AF_PP) {
|
2016-08-23 07:22:04 +01:00
|
|
|
mode_qst = MP_QSTR_ALT;
|
2014-07-22 15:57:36 +01:00
|
|
|
} else {
|
2016-08-23 07:22:04 +01:00
|
|
|
mode_qst = MP_QSTR_ALT_OPEN_DRAIN;
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
2014-08-09 09:19:37 +01:00
|
|
|
}
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, qstr_str(mode_qst));
|
2014-08-09 09:19:37 +01:00
|
|
|
|
|
|
|
// pull mode
|
2019-09-25 06:53:30 +01:00
|
|
|
qstr pull_qst = MP_QSTRnull;
|
2014-08-09 09:19:37 +01:00
|
|
|
uint32_t pull = pin_get_pull(self);
|
|
|
|
if (pull == GPIO_PULLUP) {
|
|
|
|
pull_qst = MP_QSTR_PULL_UP;
|
|
|
|
} else if (pull == GPIO_PULLDOWN) {
|
|
|
|
pull_qst = MP_QSTR_PULL_DOWN;
|
|
|
|
}
|
2019-09-25 06:53:30 +01:00
|
|
|
if (pull_qst != MP_QSTRnull) {
|
2015-04-16 18:40:06 +01:00
|
|
|
mp_printf(print, ", pull=Pin.%q", pull_qst);
|
2014-08-09 09:19:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AF mode
|
|
|
|
if (af) {
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_uint_t af_idx = pin_get_af(self);
|
2014-08-09 09:19:37 +01:00
|
|
|
const pin_af_obj_t *af_obj = pin_find_af_by_index(self, af_idx);
|
|
|
|
if (af_obj == NULL) {
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, ", af=%d)", af_idx);
|
2014-07-22 15:57:36 +01:00
|
|
|
} else {
|
2015-04-16 18:40:06 +01:00
|
|
|
mp_printf(print, ", af=Pin.%q)", af_obj->name);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
2014-08-09 09:19:37 +01:00
|
|
|
} else {
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, ")");
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *pin, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args);
|
2014-04-19 02:17:57 +01:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod \constructor(id, ...)
|
|
|
|
/// Create a new Pin object associated with the id. If additional arguments are given,
|
|
|
|
/// they are used to initialise the pin. See `init`.
|
2017-04-11 04:12:54 +01:00
|
|
|
mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
2014-04-18 22:38:09 +01:00
|
|
|
|
|
|
|
// Run an argument through the mapper and return the result.
|
|
|
|
const pin_obj_t *pin = pin_find(args[0]);
|
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
if (n_args > 1 || n_kw > 0) {
|
2014-04-18 22:38:09 +01:00
|
|
|
// pin mode given, so configure this GPIO
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
|
|
|
pin_obj_init_helper(pin, n_args - 1, args + 1, &kw_args);
|
2021-09-02 04:02:49 +01:00
|
|
|
} else {
|
|
|
|
// enable the peripheral clock so pin reading at least works
|
|
|
|
mp_hal_gpio_clock_enable(pin->gpio);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_FROM_PTR(pin);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:48:21 +01:00
|
|
|
// fast method for getting/setting pin value
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-10-19 22:48:21 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-10-19 22:48:21 +01:00
|
|
|
if (n_args == 0) {
|
|
|
|
// get pin
|
2016-10-18 04:32:42 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self));
|
2015-10-19 22:48:21 +01:00
|
|
|
} else {
|
|
|
|
// set pin
|
2016-10-18 04:32:42 +01:00
|
|
|
mp_hal_pin_write(self, mp_obj_is_true(args[0]));
|
2015-10-19 22:48:21 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod mapper([fun])
|
|
|
|
/// Get or set the pin mapper function.
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_mapper(size_t n_args, const mp_obj_t *args) {
|
2014-04-18 22:38:09 +01:00
|
|
|
if (n_args > 1) {
|
2015-01-07 23:38:50 +00:00
|
|
|
MP_STATE_PORT(pin_class_mapper) = args[1];
|
2014-04-18 22:38:09 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-01-07 23:38:50 +00:00
|
|
|
return MP_STATE_PORT(pin_class_mapper);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_mapper_fun_obj, 1, 2, pin_mapper);
|
2018-07-08 14:25:11 +01:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_mapper_obj, MP_ROM_PTR(&pin_mapper_fun_obj));
|
2014-04-18 22:38:09 +01:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod dict([dict])
|
|
|
|
/// Get or set the pin mapper dictionary.
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_map_dict(size_t n_args, const mp_obj_t *args) {
|
2014-04-18 22:38:09 +01:00
|
|
|
if (n_args > 1) {
|
2015-01-07 23:38:50 +00:00
|
|
|
MP_STATE_PORT(pin_class_map_dict) = args[1];
|
2014-04-18 22:38:09 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-01-07 23:38:50 +00:00
|
|
|
return MP_STATE_PORT(pin_class_map_dict);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_map_dict_fun_obj, 1, 2, pin_map_dict);
|
2018-07-08 14:25:11 +01:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_map_dict_obj, MP_ROM_PTR(&pin_map_dict_fun_obj));
|
2014-04-18 22:38:09 +01:00
|
|
|
|
2014-08-08 22:26:53 +01:00
|
|
|
/// \classmethod af_list()
|
2014-07-22 15:57:36 +01:00
|
|
|
/// Returns an array of alternate functions available for this pin.
|
|
|
|
STATIC mp_obj_t pin_af_list(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_obj_t result = mp_obj_new_list(0, NULL);
|
|
|
|
|
|
|
|
const pin_af_obj_t *af = self->af;
|
|
|
|
for (mp_uint_t i = 0; i < self->num_af; i++, af++) {
|
2018-07-08 14:25:11 +01:00
|
|
|
mp_obj_list_append(result, MP_OBJ_FROM_PTR(af));
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_list_obj, pin_af_list);
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod debug([state])
|
|
|
|
/// Get or set the debugging state (`True` or `False` for on or off).
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_debug(size_t n_args, const mp_obj_t *args) {
|
2014-04-18 22:38:09 +01:00
|
|
|
if (n_args > 1) {
|
|
|
|
pin_class_debug = mp_obj_is_true(args[1]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(pin_class_debug);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_debug_fun_obj, 1, 2, pin_debug);
|
2018-07-08 14:25:11 +01:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, MP_ROM_PTR(&pin_debug_fun_obj));
|
2014-04-18 22:38:09 +01:00
|
|
|
|
2015-10-19 22:48:21 +01:00
|
|
|
// init(mode, pull=None, af=-1, *, value, alt)
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-10-19 22:48:21 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
|
2019-12-16 04:40:05 +00:00
|
|
|
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
|
2015-10-19 22:48:21 +01:00
|
|
|
{ MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy
|
|
|
|
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
|
|
|
|
{ MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
|
|
|
|
};
|
2014-07-22 15:57:36 +01:00
|
|
|
|
|
|
|
// parse args
|
2015-10-19 22:48:21 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2014-04-19 02:17:57 +01:00
|
|
|
|
|
|
|
// get io mode
|
2015-10-19 22:48:21 +01:00
|
|
|
uint mode = args[0].u_int;
|
2014-04-19 02:17:57 +01:00
|
|
|
if (!IS_GPIO_MODE(mode)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode);
|
2014-04-19 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get pull mode
|
2015-10-19 22:48:21 +01:00
|
|
|
uint pull = GPIO_NOPULL;
|
|
|
|
if (args[1].u_obj != mp_const_none) {
|
|
|
|
pull = mp_obj_get_int(args[1].u_obj);
|
|
|
|
}
|
2014-07-22 15:57:36 +01:00
|
|
|
if (!IS_GPIO_PULL(pull)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin pull: %d"), pull);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:48:21 +01:00
|
|
|
// get af (alternate function); alt-arg overrides af-arg
|
|
|
|
mp_int_t af = args[4].u_int;
|
|
|
|
if (af == -1) {
|
|
|
|
af = args[2].u_int;
|
|
|
|
}
|
2014-08-09 08:51:12 +01:00
|
|
|
if ((mode == GPIO_MODE_AF_PP || mode == GPIO_MODE_AF_OD) && !IS_GPIO_AF(af)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin af: %d"), af);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
2014-04-19 02:17:57 +01:00
|
|
|
|
2014-08-06 16:04:57 +01:00
|
|
|
// enable the peripheral clock for the port of this pin
|
2015-08-03 00:05:16 +01:00
|
|
|
mp_hal_gpio_clock_enable(self->gpio);
|
2014-08-06 16:04:57 +01:00
|
|
|
|
2015-10-19 22:48:21 +01:00
|
|
|
// if given, set the pin value before initialising to prevent glitches
|
|
|
|
if (args[3].u_obj != MP_OBJ_NULL) {
|
2016-10-18 04:32:42 +01:00
|
|
|
mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj));
|
2015-10-19 22:48:21 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 02:17:57 +01:00
|
|
|
// configure the GPIO as requested
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.Pin = self->pin_mask;
|
|
|
|
GPIO_InitStructure.Mode = mode;
|
|
|
|
GPIO_InitStructure.Pull = pull;
|
2018-02-13 04:37:35 +00:00
|
|
|
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
|
2014-08-09 08:51:12 +01:00
|
|
|
GPIO_InitStructure.Alternate = af;
|
2014-04-19 02:17:57 +01:00
|
|
|
HAL_GPIO_Init(self->gpio, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-08-08 22:26:53 +01:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2018-07-08 14:25:11 +01:00
|
|
|
return pin_obj_init_helper(MP_OBJ_TO_PTR(args[0]), n_args - 1, args + 1, kw_args);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
2014-08-20 21:21:11 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(pin_init_obj, 1, pin_obj_init);
|
2014-04-19 02:17:57 +01:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method value([value])
|
|
|
|
/// Get or set the digital logic level of the pin:
|
|
|
|
///
|
|
|
|
/// - With no argument, return 0 or 1 depending on the logic level of the pin.
|
|
|
|
/// - With `value` given, set the logic level of the pin. `value` can be
|
|
|
|
/// anything that converts to a boolean. If it converts to `True`, the pin
|
|
|
|
/// is set high, otherwise it is set low.
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pin_value(size_t n_args, const mp_obj_t *args) {
|
2015-10-19 22:48:21 +01:00
|
|
|
return pin_call(args[0], n_args - 1, 0, args + 1);
|
2014-04-18 22:38:09 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value);
|
|
|
|
|
2017-04-16 09:14:48 +01:00
|
|
|
STATIC mp_obj_t pin_off(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-10-18 04:32:42 +01:00
|
|
|
mp_hal_pin_low(self);
|
2014-04-18 22:38:09 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-16 09:14:48 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_off_obj, pin_off);
|
2014-04-18 22:38:09 +01:00
|
|
|
|
2017-04-16 09:14:48 +01:00
|
|
|
STATIC mp_obj_t pin_on(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-10-18 04:32:42 +01:00
|
|
|
mp_hal_pin_high(self);
|
2014-04-18 22:38:09 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-16 09:14:48 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_on_obj, pin_on);
|
2014-04-18 22:38:09 +01:00
|
|
|
|
2017-06-15 08:17:36 +01:00
|
|
|
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False)
|
|
|
|
STATIC mp_obj_t pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_handler, ARG_trigger, ARG_hard };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-12-16 04:40:05 +00:00
|
|
|
{ MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2017-06-15 08:17:36 +01:00
|
|
|
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_MODE_IT_RISING | GPIO_MODE_IT_FALLING} },
|
|
|
|
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
if (n_args > 1 || kw_args->used != 0) {
|
|
|
|
// configure irq
|
|
|
|
extint_register_pin(self, args[ARG_trigger].u_int,
|
|
|
|
args[ARG_hard].u_bool, args[ARG_handler].u_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO should return an IRQ object
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pin_irq_obj, 1, pin_irq);
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method name()
|
|
|
|
/// Get the pin name.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t pin_name(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
return MP_OBJ_NEW_QSTR(self->name);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_name_obj, pin_name);
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
/// \method names()
|
|
|
|
/// Returns the cpu and board names for this pin.
|
|
|
|
STATIC mp_obj_t pin_names(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_obj_t result = mp_obj_new_list(0, NULL);
|
|
|
|
mp_obj_list_append(result, MP_OBJ_NEW_QSTR(self->name));
|
|
|
|
|
2018-07-08 13:11:28 +01:00
|
|
|
const mp_map_t *map = &pin_board_pins_locals_dict.map;
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_map_elem_t *elem = map->table;
|
|
|
|
|
|
|
|
for (mp_uint_t i = 0; i < map->used; i++, elem++) {
|
2018-07-08 14:25:11 +01:00
|
|
|
if (elem->value == self_in) {
|
2014-07-22 15:57:36 +01:00
|
|
|
mp_obj_list_append(result, elem->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_names_obj, pin_names);
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method port()
|
|
|
|
/// Get the pin port.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t pin_port(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-31 10:49:14 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->port);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_port_obj, pin_port);
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method pin()
|
|
|
|
/// Get the pin number.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t pin_pin(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-31 10:49:14 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->pin);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pin_obj, pin_pin);
|
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
/// \method gpio()
|
|
|
|
/// Returns the base address of the GPIO block associated with this pin.
|
|
|
|
STATIC mp_obj_t pin_gpio(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((intptr_t)self->gpio);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_gpio_obj, pin_gpio);
|
|
|
|
|
2014-08-11 07:18:10 +01:00
|
|
|
/// \method mode()
|
|
|
|
/// Returns the currently configured mode of the pin. The integer returned
|
|
|
|
/// will match one of the allowed constants for the mode argument to the init
|
|
|
|
/// function.
|
|
|
|
STATIC mp_obj_t pin_mode(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(pin_get_mode(MP_OBJ_TO_PTR(self_in)));
|
2014-08-11 07:18:10 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_mode_obj, pin_mode);
|
|
|
|
|
|
|
|
/// \method pull()
|
|
|
|
/// Returns the currently configured pull of the pin. The integer returned
|
|
|
|
/// will match one of the allowed constants for the pull argument to the init
|
|
|
|
/// function.
|
|
|
|
STATIC mp_obj_t pin_pull(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(pin_get_pull(MP_OBJ_TO_PTR(self_in)));
|
2014-08-11 07:18:10 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pull_obj, pin_pull);
|
|
|
|
|
|
|
|
/// \method af()
|
2014-08-24 18:30:22 +01:00
|
|
|
/// Returns the currently configured alternate-function of the pin. The
|
|
|
|
/// integer returned will match one of the allowed constants for the af
|
|
|
|
/// argument to the init function.
|
2014-08-11 07:18:10 +01:00
|
|
|
STATIC mp_obj_t pin_af(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(pin_get_af(MP_OBJ_TO_PTR(self_in)));
|
2014-08-11 07:18:10 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = {
|
2014-04-18 22:38:09 +01:00
|
|
|
// instance methods
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pin_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pin_value_obj) },
|
2017-04-16 09:14:48 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&pin_off_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&pin_on_obj) },
|
2017-06-15 08:17:36 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pin_irq_obj) },
|
|
|
|
|
2017-04-16 09:14:48 +01:00
|
|
|
// Legacy names as used by pyb.Pin
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&pin_off_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&pin_on_obj) },
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pin_name_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_names), MP_ROM_PTR(&pin_names_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_af_list), MP_ROM_PTR(&pin_af_list_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_port), MP_ROM_PTR(&pin_port_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&pin_pin_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_gpio), MP_ROM_PTR(&pin_gpio_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&pin_mode_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&pin_pull_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_af), MP_ROM_PTR(&pin_af_obj) },
|
2014-04-18 22:38:09 +01:00
|
|
|
|
|
|
|
// class methods
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mapper), MP_ROM_PTR(&pin_mapper_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dict), MP_ROM_PTR(&pin_map_dict_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_debug), MP_ROM_PTR(&pin_debug_obj) },
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
// class attributes
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&pin_board_pins_obj_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&pin_cpu_pins_obj_type) },
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
// class constants
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_INPUT) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_AF_PP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_AF_OD) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(GPIO_MODE_ANALOG) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULLDOWN) },
|
2017-06-15 08:17:36 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) },
|
2015-10-19 22:48:21 +01:00
|
|
|
|
|
|
|
// legacy class constants
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OUT_PP), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OUT_OD), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AF_PP), MP_ROM_INT(GPIO_MODE_AF_PP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AF_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
|
2014-08-08 22:26:53 +01:00
|
|
|
|
2014-08-08 22:34:06 +01:00
|
|
|
#include "genhdr/pins_af_const.h"
|
2014-03-13 01:06:26 +00:00
|
|
|
};
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table);
|
|
|
|
|
2017-01-31 01:36:04 +00:00
|
|
|
STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
(void)errcode;
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2017-01-31 01:36:04 +00:00
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case MP_PIN_READ: {
|
|
|
|
return mp_hal_pin_read(self);
|
|
|
|
}
|
|
|
|
case MP_PIN_WRITE: {
|
|
|
|
mp_hal_pin_write(self, arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_pin_p_t pin_pin_p = {
|
|
|
|
.ioctl = pin_ioctl,
|
|
|
|
};
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
const mp_obj_type_t pin_type = {
|
2014-03-13 01:06:26 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Pin,
|
2014-04-18 22:38:09 +01:00
|
|
|
.print = pin_print,
|
2017-04-11 04:12:54 +01:00
|
|
|
.make_new = mp_pin_make_new,
|
2015-10-19 22:48:21 +01:00
|
|
|
.call = pin_call,
|
2017-01-31 01:36:04 +00:00
|
|
|
.protocol = &pin_pin_p,
|
2017-05-06 08:03:40 +01:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&pin_locals_dict,
|
2014-03-13 01:06:26 +00:00
|
|
|
};
|
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class PinAF - Pin Alternate Functions
|
|
|
|
///
|
|
|
|
/// A Pin represents a physical pin on the microcprocessor. Each pin
|
|
|
|
/// can have a variety of functions (GPIO, I2C SDA, etc). Each PinAF
|
|
|
|
/// object represents a particular function for a pin.
|
|
|
|
///
|
|
|
|
/// Usage Model:
|
|
|
|
///
|
|
|
|
/// x3 = pyb.Pin.board.X3
|
|
|
|
/// x3_af = x3.af_list()
|
|
|
|
///
|
|
|
|
/// x3_af will now contain an array of PinAF objects which are availble on
|
|
|
|
/// pin X3.
|
|
|
|
///
|
|
|
|
/// For the pyboard, x3_af would contain:
|
|
|
|
/// [Pin.AF1_TIM2, Pin.AF2_TIM5, Pin.AF3_TIM9, Pin.AF7_USART2]
|
|
|
|
///
|
|
|
|
/// Normally, each peripheral would configure the af automatically, but sometimes
|
|
|
|
/// the same function is available on multiple pins, and having more control
|
|
|
|
/// is desired.
|
|
|
|
///
|
|
|
|
/// To configure X3 to expose TIM2_CH3, you could use:
|
|
|
|
/// pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=pyb.Pin.AF1_TIM2)
|
|
|
|
/// or:
|
|
|
|
/// pin = pyb.Pin(pyb.Pin.board.X3, mode=pyb.Pin.AF_PP, af=1)
|
|
|
|
|
|
|
|
/// \method __str__()
|
|
|
|
/// Return a string describing the alternate function.
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void pin_af_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_af_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-16 18:40:06 +01:00
|
|
|
mp_printf(print, "Pin.%q", self->name);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
/// \method index()
|
|
|
|
/// Return the alternate function index.
|
|
|
|
STATIC mp_obj_t pin_af_index(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_af_obj_t *af = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(af->idx);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_index_obj, pin_af_index);
|
|
|
|
|
2014-08-08 23:30:01 +01:00
|
|
|
/// \method name()
|
2014-07-22 15:57:36 +01:00
|
|
|
/// Return the name of the alternate function.
|
|
|
|
STATIC mp_obj_t pin_af_name(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_af_obj_t *af = MP_OBJ_TO_PTR(self_in);
|
2014-07-22 15:57:36 +01:00
|
|
|
return MP_OBJ_NEW_QSTR(af->name);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_name_obj, pin_af_name);
|
|
|
|
|
2014-08-08 23:30:01 +01:00
|
|
|
/// \method reg()
|
2014-07-22 15:57:36 +01:00
|
|
|
/// Return the base register associated with the peripheral assigned to this
|
|
|
|
/// alternate function. For example, if the alternate function were TIM2_CH3
|
|
|
|
/// this would return stm.TIM2
|
|
|
|
STATIC mp_obj_t pin_af_reg(mp_obj_t self_in) {
|
2018-07-08 14:25:11 +01:00
|
|
|
pin_af_obj_t *af = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((uintptr_t)af->reg);
|
2014-07-22 15:57:36 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_reg_obj, pin_af_reg);
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pin_af_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&pin_af_index_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pin_af_name_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reg), MP_ROM_PTR(&pin_af_reg_obj) },
|
2014-07-22 15:57:36 +01:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pin_af_locals_dict, pin_af_locals_dict_table);
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
const mp_obj_type_t pin_af_type = {
|
2014-03-13 01:06:26 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_PinAF,
|
|
|
|
.print = pin_af_obj_print,
|
2017-05-06 08:03:40 +01:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&pin_af_locals_dict,
|
2014-03-13 01:06:26 +00:00
|
|
|
};
|