|
| 1 | +//** just starting suggestion at this point --- jcw - 9/10/20 |
| 2 | +/* |
| 3 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + See the GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "Arduino.h" |
| 21 | +#include "wiring_private.h" |
| 22 | + |
| 23 | +int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) |
| 24 | +{ |
| 25 | + // Handle the case the pin isn't usable as PIO |
| 26 | + if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) |
| 27 | + { |
| 28 | + return -1 ; |
| 29 | + } |
| 30 | + |
| 31 | + switch ( ulPeripheral ) |
| 32 | + { |
| 33 | + case PIO_DIGITAL: |
| 34 | + case PIO_INPUT: |
| 35 | + case PIO_INPUT_PULLUP: |
| 36 | + case PIO_OUTPUT: |
| 37 | + // Disable peripheral muxing, done in pinMode |
| 38 | +// PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PMUXEN = 0 ; |
| 39 | + |
| 40 | + // Configure pin mode, if requested |
| 41 | + if ( ulPeripheral == PIO_INPUT ) |
| 42 | + { |
| 43 | + pinMode( ulPin, INPUT ) ; |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + if ( ulPeripheral == PIO_INPUT_PULLUP ) |
| 48 | + { |
| 49 | + pinMode( ulPin, INPUT_PULLUP ) ; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + if ( ulPeripheral == PIO_OUTPUT ) |
| 54 | + { |
| 55 | + pinMode( ulPin, OUTPUT ) ; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + // PIO_DIGITAL, do we have to do something as all cases are covered? |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + break ; |
| 64 | + |
| 65 | + case PIO_ANALOG: |
| 66 | + case PIO_SERCOM: |
| 67 | + case PIO_SERCOM_ALT: |
| 68 | + case PIO_TIMER: |
| 69 | + case PIO_TIMER_ALT: |
| 70 | + case PIO_EXTINT: |
| 71 | + case PIO_COM: |
| 72 | + case PIO_AC_CLK: |
| 73 | +#if 0 |
| 74 | + // Is the pio pin in the lower 16 ones? |
| 75 | + // The WRCONFIG register allows update of only 16 pin max out of 32 |
| 76 | + if ( g_APinDescription[ulPin].ulPin < 16 ) |
| 77 | + { |
| 78 | + PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) | |
| 79 | + PORT_WRCONFIG_WRPINCFG | |
| 80 | + PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin ) ; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | |
| 85 | + PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) | |
| 86 | + PORT_WRCONFIG_WRPINCFG | |
| 87 | + PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ; |
| 88 | + } |
| 89 | +#else |
| 90 | + if ( g_APinDescription[ulPin].ulPin & 1 ) // is pin odd? |
| 91 | + { |
| 92 | + uint32_t temp ; |
| 93 | + |
| 94 | + // Get whole current setup for both odd and even pins and remove odd one |
| 95 | + temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ; |
| 96 | + // Set new muxing |
| 97 | + PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXO( ulPeripheral ) ; |
| 98 | + // Enable port mux |
| 99 | + PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; |
| 100 | + } |
| 101 | + else // even pin |
| 102 | + { |
| 103 | + uint32_t temp ; |
| 104 | + |
| 105 | + temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXO( 0xF ) ; |
| 106 | + PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ; |
| 107 | + PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux |
| 108 | + } |
| 109 | +#endif |
| 110 | + break ; |
| 111 | + |
| 112 | + case PIO_NOT_A_PIN: |
| 113 | + return -1l ; |
| 114 | + break ; |
| 115 | + } |
| 116 | + |
| 117 | + return 0l ; |
| 118 | +} |
| 119 | + |
0 commit comments