2016-08-27 18:04:34 -04:00
|
|
|
/*
|
2016-11-03 18:50:59 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-08-27 18:04:34 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2016-11-03 18:50:59 -04:00
|
|
|
* Copyright (c) 2016 Scott Shawcroft
|
2016-08-27 18:04:34 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-02-19 08:23:29 -05:00
|
|
|
#include "shared-bindings/board/__init__.h"
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
|
2016-11-29 18:49:50 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/obj.h"
|
2018-07-31 16:39:12 -04:00
|
|
|
#include "py/runtime.h"
|
2016-11-29 18:49:50 -05:00
|
|
|
|
2020-05-07 15:59:52 -04:00
|
|
|
//| class Pin:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Identifies an IO pin on the microcontroller."""
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self) -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Identifies an IO pin on the microcontroller. They are fixed by the
|
|
|
|
//| hardware so they cannot be constructed on demand. Instead, use
|
2020-07-25 04:58:37 -04:00
|
|
|
//| :mod:`board` or :mod:`microcontroller.pin` to reference the desired pin."""
|
2020-05-07 15:59:52 -04:00
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2022-05-03 16:50:48 -04:00
|
|
|
//| def __hash__(self) -> int:
|
|
|
|
//| """Returns a hash for the Pin."""
|
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2023-10-19 16:02:42 -04:00
|
|
|
// Provided inherently.
|
|
|
|
// See https://github.com/micropython/micropython/pull/10348.
|
|
|
|
|
2022-05-03 16:50:48 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static void get_pin_name(const mcu_pin_obj_t *self, qstr *package, qstr *module, qstr *name) {
|
|
|
|
const mp_map_t *board_map = &board_module_globals.map;
|
2017-02-19 08:23:29 -05:00
|
|
|
for (uint8_t i = 0; i < board_map->alloc; i++) {
|
2021-08-04 19:27:54 -04:00
|
|
|
if (board_map->table[i].value == MP_OBJ_FROM_PTR(self)) {
|
2018-07-31 17:01:01 -04:00
|
|
|
*package = 0;
|
|
|
|
*module = MP_QSTR_board;
|
|
|
|
*name = MP_OBJ_QSTR_VALUE(board_map->table[i].key);
|
2017-02-19 08:23:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
const mp_map_t *mcu_map = &mcu_pin_globals.map;
|
2017-02-19 08:23:29 -05:00
|
|
|
for (uint8_t i = 0; i < mcu_map->alloc; i++) {
|
2021-08-04 19:27:54 -04:00
|
|
|
if (mcu_map->table[i].value == MP_OBJ_FROM_PTR(self)) {
|
2018-07-31 17:01:01 -04:00
|
|
|
*package = MP_QSTR_microcontroller;
|
|
|
|
*module = MP_QSTR_pin;
|
|
|
|
*name = MP_OBJ_QSTR_VALUE(mcu_map->table[i].key);
|
2017-02-19 08:23:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
void shared_bindings_microcontroller_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-31 17:01:01 -04:00
|
|
|
mcu_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-01-20 20:10:28 -05:00
|
|
|
qstr package = MP_QSTR_Pin;
|
2018-07-31 17:01:01 -04:00
|
|
|
qstr module;
|
2021-01-20 20:10:28 -05:00
|
|
|
qstr name = MP_QSTR_Pin;
|
2018-07-31 17:01:01 -04:00
|
|
|
|
|
|
|
get_pin_name(self, &package, &module, &name);
|
2021-01-20 20:10:28 -05:00
|
|
|
if (package) {
|
2020-09-25 13:08:26 -04:00
|
|
|
mp_printf(print, "%q.%q.%q", package, module, name);
|
|
|
|
} else {
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_printf(print, "%q.%q", module, name);
|
2020-09-25 13:08:26 -04:00
|
|
|
}
|
2018-07-31 17:01:01 -04:00
|
|
|
}
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
mcu_pin_type,
|
|
|
|
MP_QSTR_Pin,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2023-10-19 16:02:42 -04:00
|
|
|
print, shared_bindings_microcontroller_pin_print
|
2023-09-19 21:09:29 -04:00
|
|
|
);
|
2016-11-29 18:49:50 -05:00
|
|
|
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj, qstr arg_name) {
|
|
|
|
return MP_OBJ_TO_PTR(mp_arg_validate_type(obj, &mcu_pin_type, arg_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
const mcu_pin_obj_t *validate_obj_is_pin_in(mp_obj_t obj, qstr arg_name) {
|
|
|
|
return MP_OBJ_TO_PTR(mp_arg_validate_type_in(obj, &mcu_pin_type, arg_name));
|
2020-02-28 23:32:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that the obj is a pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly.
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj, qstr arg_name) {
|
2023-01-12 19:03:45 -05:00
|
|
|
if (obj == mp_const_none) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return validate_obj_is_pin(obj, arg_name);
|
2020-02-28 23:32:24 -05:00
|
|
|
}
|
|
|
|
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj, qstr arg_name) {
|
|
|
|
const mcu_pin_obj_t *pin = validate_obj_is_pin(obj, arg_name);
|
2020-02-28 23:32:24 -05:00
|
|
|
assert_pin_free(pin);
|
|
|
|
return pin;
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:55:26 -04:00
|
|
|
// Validate every element in the list is a unique pin
|
|
|
|
void validate_no_duplicate_pins(mp_obj_t seq, qstr arg_name) {
|
2021-08-17 20:16:26 -04:00
|
|
|
const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
|
2021-08-17 18:55:26 -04:00
|
|
|
|
|
|
|
for (size_t pin_cnt = 0; pin_cnt < num_pins; pin_cnt++) {
|
|
|
|
mp_obj_t pin1_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt), MP_OBJ_SENTINEL);
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *pin1 = validate_obj_is_pin_in(pin1_obj, arg_name);
|
2021-08-17 18:55:26 -04:00
|
|
|
|
|
|
|
for (size_t pin_cnt_2 = pin_cnt + 1; pin_cnt_2 < num_pins; pin_cnt_2++) {
|
|
|
|
mp_obj_t pin2_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *pin2 = validate_obj_is_pin_in(pin2_obj, arg_name);
|
2021-08-17 18:55:26 -04:00
|
|
|
if (pin1 == pin2) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_TypeError_varg(MP_ERROR_TEXT("%q contains duplicate pins"), arg_name);
|
2021-08-17 18:55:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void validate_no_duplicate_pins_2(mp_obj_t seq1, mp_obj_t seq2, qstr arg_name1, qstr arg_name2) {
|
2021-08-17 20:16:26 -04:00
|
|
|
const size_t num_pins_1 = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq1));
|
|
|
|
const size_t num_pins_2 = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq2));
|
2021-08-17 18:55:26 -04:00
|
|
|
|
2021-08-19 21:03:49 -04:00
|
|
|
validate_no_duplicate_pins(seq1, arg_name1);
|
|
|
|
validate_no_duplicate_pins(seq2, arg_name2);
|
|
|
|
|
2021-08-17 18:55:26 -04:00
|
|
|
for (size_t pin_cnt_1 = 0; pin_cnt_1 < num_pins_1; pin_cnt_1++) {
|
|
|
|
mp_obj_t pin1_obj = mp_obj_subscr(seq1, MP_OBJ_NEW_SMALL_INT(pin_cnt_1), MP_OBJ_SENTINEL);
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *pin1 = validate_obj_is_pin_in(pin1_obj, arg_name1);
|
2021-08-17 18:55:26 -04:00
|
|
|
|
|
|
|
for (size_t pin_cnt_2 = 0; pin_cnt_2 < num_pins_2; pin_cnt_2++) {
|
|
|
|
mp_obj_t pin2_obj = mp_obj_subscr(seq2, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *pin2 = validate_obj_is_pin_in(pin2_obj, arg_name2);
|
2021-08-17 20:16:26 -04:00
|
|
|
if (pin1 == pin2) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_TypeError_varg(MP_ERROR_TEXT("%q and %q contain duplicate pins"), arg_name1, arg_name2);
|
2021-08-17 18:55:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:09:39 -04:00
|
|
|
// Validate every element in the list to be a free pin.
|
2021-08-04 19:27:54 -04:00
|
|
|
void validate_list_is_free_pins(qstr what, const mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
|
2020-05-27 10:09:39 -04:00
|
|
|
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
|
2022-11-08 15:55:45 -05:00
|
|
|
mp_arg_validate_length_max(len, max_pins, what);
|
2020-05-27 10:09:39 -04:00
|
|
|
*count_out = len;
|
2021-03-15 09:57:36 -04:00
|
|
|
for (mp_int_t i = 0; i < len; i++) {
|
2023-01-10 13:39:10 -05:00
|
|
|
pins_out[i] =
|
|
|
|
validate_obj_is_pin_in(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL), what);
|
|
|
|
assert_pin_free(pins_out[i]);
|
2020-05-27 10:09:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:32:24 -05:00
|
|
|
// Validate that the obj is a free pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly.
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj, qstr arg_name) {
|
2020-02-28 23:32:24 -05:00
|
|
|
if (obj == mp_const_none) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-10 13:39:10 -05:00
|
|
|
const mcu_pin_obj_t *pin = validate_obj_is_pin(obj, arg_name);
|
2020-02-28 23:32:24 -05:00
|
|
|
assert_pin_free(pin);
|
|
|
|
return pin;
|
2016-11-29 18:49:50 -05:00
|
|
|
}
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void assert_pin_free(const mcu_pin_obj_t *pin) {
|
2017-02-19 06:59:31 -05:00
|
|
|
if (pin != NULL && pin != MP_OBJ_TO_PTR(mp_const_none) && !common_hal_mcu_pin_is_free(pin)) {
|
2018-07-31 17:01:01 -04:00
|
|
|
qstr package;
|
|
|
|
qstr module;
|
2021-01-20 20:10:28 -05:00
|
|
|
qstr name = MP_QSTR_Pin;
|
2018-07-31 17:01:01 -04:00
|
|
|
|
|
|
|
get_pin_name(pin, &package, &module, &name);
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError_varg(MP_ERROR_TEXT("%q in use"), name);
|
2016-12-06 13:31:38 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 13:15:31 -04:00
|
|
|
|
|
|
|
void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
|
2021-08-04 19:27:54 -04:00
|
|
|
const mcu_pin_obj_t *pins[max_pins];
|
2021-06-09 13:15:31 -04:00
|
|
|
validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
|
|
|
|
for (mp_int_t i = 0; i < *count_out; i++) {
|
|
|
|
pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 15:33:43 -04:00
|
|
|
|
|
|
|
NORETURN void raise_ValueError_invalid_pin(void) {
|
|
|
|
mp_arg_error_invalid(MP_QSTR_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
NORETURN void raise_ValueError_invalid_pins(void) {
|
|
|
|
mp_arg_error_invalid(MP_QSTR_pins);
|
|
|
|
}
|
2022-05-20 10:10:55 -04:00
|
|
|
|
|
|
|
NORETURN void raise_ValueError_invalid_pin_name(qstr pin_name) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q pin"), pin_name);
|
2022-05-20 10:10:55 -04:00
|
|
|
}
|