summaryrefslogtreecommitdiffstatshomepage
path: root/teensy/pin_defs_teensy.c
diff options
context:
space:
mode:
authorDave Hylands <dhylands@gmail.com>2014-07-22 07:57:36 -0700
committerDave Hylands <dhylands@gmail.com>2014-08-07 23:15:41 -0700
commit6f418fc1b068c1a41113bb2b019a5803765e1deb (patch)
tree79477835459eb424012f58d77a150bdf9a80eed5 /teensy/pin_defs_teensy.c
parent3ef911345c94a6d612ab50c1e912e81cb2cc3f71 (diff)
downloadmicropython-6f418fc1b068c1a41113bb2b019a5803765e1deb.tar.gz
micropython-6f418fc1b068c1a41113bb2b019a5803765e1deb.zip
Add support for selecting pin alternate functions from python.
Converts generted pins to use qstrs instead of string pointers. This patch also adds the following functions: pyb.Pin.names() pyb.Pin.af_list() pyb.Pin.gpio() dir(pyb.Pin.board) and dir(pyb.Pin.cpu) also produce useful results. pyb.Pin now takes kw args. pyb.Pin.__str__ now prints more useful information about the pin configuration. I found the following functions in my boot.py to be useful: ```python def pins(): for pin_name in dir(pyb.Pin.board): pin = pyb.Pin(pin_name) print('{:10s} {:s}'.format(pin_name, str(pin))) def af(): for pin_name in dir(pyb.Pin.board): pin = pyb.Pin(pin_name) print('{:10s} {:s}'.format(pin_name, str(pin.af_list()))) ```
Diffstat (limited to 'teensy/pin_defs_teensy.c')
-rw-r--r--teensy/pin_defs_teensy.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/teensy/pin_defs_teensy.c b/teensy/pin_defs_teensy.c
new file mode 100644
index 0000000000..dd2f0dc21f
--- /dev/null
+++ b/teensy/pin_defs_teensy.c
@@ -0,0 +1,61 @@
+#include <stdint.h>
+#include <mk20dx128.h>
+#include "mpconfig.h"
+#include "nlr.h"
+#include "misc.h"
+#include "qstr.h"
+#include "obj.h"
+#include "runtime.h"
+#include MICROPY_HAL_H
+#include "pin.h"
+
+// Returns the pin mode. This value returned by this macro should be one of:
+// GPIO_MODE_INPUT, GPIO_MODE_OUTPUT_PP, GPIO_MODE_OUTPUT_OD,
+// GPIO_MODE_AF_PP, GPIO_MODE_AF_OD, or GPIO_MODE_ANALOG.
+
+uint32_t pin_get_mode(const pin_obj_t *pin) {
+ volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
+ uint32_t pcr = *port_pcr;
+ uint32_t af = (*port_pcr & PORT_PCR_MUX_MASK) >> 8;;
+
+ if (af == 0) {
+ return GPIO_MODE_ANALOG;
+ }
+ if (af == 1) {
+ if (pin->gpio->PDDR & (1 << pin->pin)) {
+ if (pcr & PORT_PCR_ODE) {
+ return GPIO_MODE_OUTPUT_OD;
+ }
+ return GPIO_MODE_OUTPUT_PP;
+ }
+ return GPIO_MODE_INPUT;
+ }
+
+ if (pcr & PORT_PCR_ODE) {
+ return GPIO_MODE_AF_OD;
+ }
+ return GPIO_MODE_AF_PP;
+}
+
+// Returns the pin pullup/pulldown. The value returned by this macro should
+// be one of GPIO_NOPULL, GPIO_PULLUP, or GPIO_PULLDOWN.
+
+uint32_t pin_get_pull(const pin_obj_t *pin) {
+ volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
+
+ uint32_t pcr = *port_pcr;
+ if (pcr & PORT_PCR_PE) {
+ if (pcr & PORT_PCR_PS) {
+ return GPIO_PULLUP;
+ }
+ return GPIO_PULLDOWN;
+ }
+ return GPIO_NOPULL;
+}
+
+// Returns the af (alternate function) index currently set for a pin.
+
+uint32_t pin_get_af(const pin_obj_t *pin) {
+ volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
+ return (*port_pcr & PORT_PCR_MUX_MASK) >> 8;
+}