[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2014 Arduino. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 12 | See the GNU Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | #include "wiring_digital.h"
|
---|
| 21 | #include "WVariant.h"
|
---|
| 22 |
|
---|
| 23 | #ifdef __cplusplus
|
---|
| 24 | extern "C" {
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral )
|
---|
| 28 | {
|
---|
| 29 | if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
|
---|
| 30 | {
|
---|
| 31 | return -1 ;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | switch ( ulPeripheral )
|
---|
| 35 | {
|
---|
| 36 | case PIO_DIGITAL:
|
---|
| 37 | case PIO_INPUT:
|
---|
| 38 | case PIO_INPUT_PULLUP:
|
---|
| 39 | case PIO_OUTPUT:
|
---|
| 40 | // Disable peripheral muxing, done in pinMode
|
---|
| 41 | // Configure pin mode, if requested
|
---|
| 42 | if ( ulPeripheral == PIO_INPUT )
|
---|
| 43 | {
|
---|
| 44 | pinMode( ulPin, INPUT ) ;
|
---|
| 45 | }
|
---|
| 46 | else
|
---|
| 47 | {
|
---|
| 48 | if ( ulPeripheral == PIO_INPUT_PULLUP )
|
---|
| 49 | {
|
---|
| 50 | pinMode( ulPin, INPUT_PULLUP ) ;
|
---|
| 51 | }
|
---|
| 52 | else
|
---|
| 53 | {
|
---|
| 54 | if ( ulPeripheral == PIO_OUTPUT )
|
---|
| 55 | {
|
---|
| 56 | pinMode( ulPin, OUTPUT ) ;
|
---|
| 57 | }
|
---|
| 58 | else
|
---|
| 59 | {
|
---|
| 60 | // do notting
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | break ;
|
---|
| 65 |
|
---|
| 66 | case PIO_ANALOG:
|
---|
| 67 | case PIO_SERCOM:
|
---|
| 68 | case PIO_SERCOM_ALT:
|
---|
| 69 | case PIO_TIMER:
|
---|
| 70 | case PIO_TIMER_ALT:
|
---|
| 71 | case PIO_EXTINT:
|
---|
| 72 | case PIO_COM:
|
---|
| 73 | case PIO_AC_CLK:
|
---|
| 74 | #if 0
|
---|
| 75 | // Is the pio pin in the lower 16 ones?
|
---|
| 76 | // The WRCONFIG register allows update of only 16 pin max out of 32
|
---|
| 77 | if ( g_APinDescription[ulPin].ulPin < 16 )
|
---|
| 78 | {
|
---|
| 79 | PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) |
|
---|
| 80 | PORT_WRCONFIG_WRPINCFG |
|
---|
| 81 | PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin ) ;
|
---|
| 82 | }
|
---|
| 83 | else
|
---|
| 84 | {
|
---|
| 85 | PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_HWSEL |
|
---|
| 86 | PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) |
|
---|
| 87 | PORT_WRCONFIG_WRPINCFG |
|
---|
| 88 | PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ;
|
---|
| 89 | }
|
---|
| 90 | #else
|
---|
| 91 | if ( g_APinDescription[ulPin].ulPin & 1 )
|
---|
| 92 | {
|
---|
| 93 | uint32_t temp ;
|
---|
| 94 |
|
---|
| 95 | // Get whole current setup for both odd and even pins and remove odd one
|
---|
| 96 | temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ;
|
---|
| 97 | // Set new muxing
|
---|
| 98 | PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXO( ulPeripheral ) ;
|
---|
| 99 | // Enable port mux
|
---|
| 100 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ;
|
---|
| 101 | }
|
---|
| 102 | else
|
---|
| 103 | {
|
---|
| 104 | uint32_t temp ;
|
---|
| 105 |
|
---|
| 106 | temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXO( 0xF ) ;
|
---|
| 107 | PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ;
|
---|
| 108 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux
|
---|
| 109 | }
|
---|
| 110 | #endif
|
---|
| 111 | break ;
|
---|
| 112 |
|
---|
| 113 | case PIO_NOT_A_PIN:
|
---|
| 114 | return -1l ;
|
---|
| 115 | break ;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | return 0l ;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void pinMode( uint32_t ulPin, uint32_t ulMode )
|
---|
| 122 | {
|
---|
| 123 | if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
|
---|
| 124 | {
|
---|
| 125 | return ;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | switch ( ulMode )
|
---|
| 129 | {
|
---|
| 130 | case INPUT:
|
---|
| 131 | // Set pin to input mode
|
---|
| 132 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg=(uint8_t)(PORT_PINCFG_INEN) ;
|
---|
| 133 | PORT->Group[g_APinDescription[ulPin].ulPort].DIRCLR.reg = (uint32_t)(1<<g_APinDescription[ulPin].ulPin) ;
|
---|
| 134 | break ;
|
---|
| 135 |
|
---|
| 136 | case INPUT_PULLUP:
|
---|
| 137 | // Set pin to input mode with pull-up resistor enabled
|
---|
| 138 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg=(uint8_t)(PORT_PINCFG_INEN|PORT_PINCFG_PULLEN) ;
|
---|
| 139 | PORT->Group[g_APinDescription[ulPin].ulPort].DIRCLR.reg = (uint32_t)(1<<g_APinDescription[ulPin].ulPin) ;
|
---|
| 140 |
|
---|
| 141 | //pull-upconfig
|
---|
| 142 | PORT->Group[g_APinDescription[ulPin].ulPort].OUTSET.reg = (uint32_t)(1<<g_APinDescription[ulPin].ulPin) ;
|
---|
| 143 | break ;
|
---|
| 144 |
|
---|
| 145 | case OUTPUT:
|
---|
| 146 | // Set pin to output mode
|
---|
| 147 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg&=~(uint8_t)(PORT_PINCFG_INEN) ;
|
---|
| 148 | PORT->Group[g_APinDescription[ulPin].ulPort].DIRSET.reg = (uint32_t)(1<<g_APinDescription[ulPin].ulPin) ;
|
---|
| 149 |
|
---|
| 150 | break ;
|
---|
| 151 |
|
---|
| 152 | default:
|
---|
| 153 | // do nothing
|
---|
| 154 | break ;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void digitalWrite( uint32_t ulPin, uint32_t ulVal )
|
---|
| 159 | {
|
---|
| 160 | /* Handle the case the pin isn't usable as PIO */
|
---|
| 161 | if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
|
---|
| 162 | {
|
---|
| 163 | return ;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // Enable pull-up resistor
|
---|
| 167 | PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg=(uint8_t)(PORT_PINCFG_PULLEN) ;
|
---|
| 168 |
|
---|
| 169 | switch ( ulVal )
|
---|
| 170 | {
|
---|
| 171 | case LOW:
|
---|
| 172 | PORT->Group[g_APinDescription[ulPin].ulPort].OUTCLR.reg = (1ul << g_APinDescription[ulPin].ulPin) ;
|
---|
| 173 | break ;
|
---|
| 174 |
|
---|
| 175 | case HIGH:
|
---|
| 176 | PORT->Group[g_APinDescription[ulPin].ulPort].OUTSET.reg = (1ul << g_APinDescription[ulPin].ulPin) ;
|
---|
| 177 | break ;
|
---|
| 178 |
|
---|
| 179 | default:
|
---|
| 180 | break ;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return ;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | int digitalRead( uint32_t ulPin )
|
---|
| 187 | {
|
---|
| 188 | /* Handle the case the pin isn't usable as PIO */
|
---|
| 189 | if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
|
---|
| 190 | {
|
---|
| 191 | return LOW ;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if ( (PORT->Group[g_APinDescription[ulPin].ulPort].IN.reg & (1ul << g_APinDescription[ulPin].ulPin)) != 0 )
|
---|
| 195 | {
|
---|
| 196 | return HIGH ;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return LOW ;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | #ifdef __cplusplus
|
---|
| 203 | }
|
---|
| 204 | #endif
|
---|
| 205 |
|
---|