pin_defs_teensy.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdint.h>
  2. #include <mk20dx128.h>
  3. #include "py/runtime.h"
  4. #include "py/mphal.h"
  5. #include "pin.h"
  6. // Returns the pin mode. This value returned by this macro should be one of:
  7. // GPIO_MODE_INPUT, GPIO_MODE_OUTPUT_PP, GPIO_MODE_OUTPUT_OD,
  8. // GPIO_MODE_AF_PP, GPIO_MODE_AF_OD, or GPIO_MODE_ANALOG.
  9. uint32_t pin_get_mode(const pin_obj_t *pin) {
  10. if (pin->gpio == NULL) {
  11. // Analog only pin
  12. return GPIO_MODE_ANALOG;
  13. }
  14. volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
  15. uint32_t pcr = *port_pcr;
  16. uint32_t af = (pcr & PORT_PCR_MUX_MASK) >> 8;
  17. if (af == 0) {
  18. return GPIO_MODE_ANALOG;
  19. }
  20. if (af == 1) {
  21. if (pin->gpio->PDDR & (1 << pin->pin)) {
  22. if (pcr & PORT_PCR_ODE) {
  23. return GPIO_MODE_OUTPUT_OD;
  24. }
  25. return GPIO_MODE_OUTPUT_PP;
  26. }
  27. return GPIO_MODE_INPUT;
  28. }
  29. if (pcr & PORT_PCR_ODE) {
  30. return GPIO_MODE_AF_OD;
  31. }
  32. return GPIO_MODE_AF_PP;
  33. }
  34. // Returns the pin pullup/pulldown. The value returned by this macro should
  35. // be one of GPIO_NOPULL, GPIO_PULLUP, or GPIO_PULLDOWN.
  36. uint32_t pin_get_pull(const pin_obj_t *pin) {
  37. if (pin->gpio == NULL) {
  38. // Analog only pin
  39. return GPIO_NOPULL;
  40. }
  41. volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
  42. uint32_t pcr = *port_pcr;
  43. uint32_t af = (pcr & PORT_PCR_MUX_MASK) >> 8;
  44. // pull is only valid for digital modes (hence the af > 0 test)
  45. if (af > 0 && (pcr & PORT_PCR_PE) != 0) {
  46. if (pcr & PORT_PCR_PS) {
  47. return GPIO_PULLUP;
  48. }
  49. return GPIO_PULLDOWN;
  50. }
  51. return GPIO_NOPULL;
  52. }
  53. // Returns the af (alternate function) index currently set for a pin.
  54. uint32_t pin_get_af(const pin_obj_t *pin) {
  55. if (pin->gpio == NULL) {
  56. // Analog only pin
  57. return 0;
  58. }
  59. volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(pin->gpio, pin->pin);
  60. return (*port_pcr & PORT_PCR_MUX_MASK) >> 8;
  61. }