mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Child:
592:a274ee790e56
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 /**
mbed_official 579:53297373a894 2 * \file
mbed_official 579:53297373a894 3 *
mbed_official 579:53297373a894 4 * \brief SAM Pin Multiplexer Driver
mbed_official 579:53297373a894 5 *
mbed_official 579:53297373a894 6 * Copyright (C) 2012-2014 Atmel Corporation. All rights reserved.
mbed_official 579:53297373a894 7 *
mbed_official 579:53297373a894 8 * \asf_license_start
mbed_official 579:53297373a894 9 *
mbed_official 579:53297373a894 10 * \page License
mbed_official 579:53297373a894 11 *
mbed_official 579:53297373a894 12 * Redistribution and use in source and binary forms, with or without
mbed_official 579:53297373a894 13 * modification, are permitted provided that the following conditions are met:
mbed_official 579:53297373a894 14 *
mbed_official 579:53297373a894 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 579:53297373a894 16 * this list of conditions and the following disclaimer.
mbed_official 579:53297373a894 17 *
mbed_official 579:53297373a894 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 579:53297373a894 19 * this list of conditions and the following disclaimer in the documentation
mbed_official 579:53297373a894 20 * and/or other materials provided with the distribution.
mbed_official 579:53297373a894 21 *
mbed_official 579:53297373a894 22 * 3. The name of Atmel may not be used to endorse or promote products derived
mbed_official 579:53297373a894 23 * from this software without specific prior written permission.
mbed_official 579:53297373a894 24 *
mbed_official 579:53297373a894 25 * 4. This software may only be redistributed and used in connection with an
mbed_official 579:53297373a894 26 * Atmel microcontroller product.
mbed_official 579:53297373a894 27 *
mbed_official 579:53297373a894 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
mbed_official 579:53297373a894 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 579:53297373a894 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
mbed_official 579:53297373a894 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
mbed_official 579:53297373a894 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 579:53297373a894 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mbed_official 579:53297373a894 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mbed_official 579:53297373a894 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
mbed_official 579:53297373a894 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
mbed_official 579:53297373a894 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 579:53297373a894 38 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 579:53297373a894 39 *
mbed_official 579:53297373a894 40 * \asf_license_stop
mbed_official 579:53297373a894 41 *
mbed_official 579:53297373a894 42 */
mbed_official 579:53297373a894 43 /**
mbed_official 579:53297373a894 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
mbed_official 579:53297373a894 45 */
mbed_official 579:53297373a894 46 #include <pinmux.h>
mbed_official 579:53297373a894 47
mbed_official 579:53297373a894 48 /**
mbed_official 579:53297373a894 49 * \internal
mbed_official 579:53297373a894 50 * Writes out a given configuration of a Port pin configuration to the
mbed_official 579:53297373a894 51 * hardware module.
mbed_official 579:53297373a894 52 *
mbed_official 579:53297373a894 53 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 54 * configuration setting is ignored.
mbed_official 579:53297373a894 55 *
mbed_official 579:53297373a894 56 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 57 * \param[in] pin_mask Mask of the port pin to configure
mbed_official 579:53297373a894 58 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 59 */
mbed_official 579:53297373a894 60 static void _system_pinmux_config(
mbed_official 579:53297373a894 61 PortGroup *const port,
mbed_official 579:53297373a894 62 const uint32_t pin_mask,
mbed_official 579:53297373a894 63 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 64 {
mbed_official 579:53297373a894 65 Assert(port);
mbed_official 579:53297373a894 66 Assert(config);
mbed_official 579:53297373a894 67
mbed_official 579:53297373a894 68 /* Track the configuration bits into a temporary variable before writing */
mbed_official 579:53297373a894 69 uint32_t pin_cfg = 0;
mbed_official 579:53297373a894 70
mbed_official 579:53297373a894 71 /* Enabled powersave mode, don't create configuration */
mbed_official 579:53297373a894 72 if (!config->powersave) {
mbed_official 579:53297373a894 73 /* Enable the pin peripheral MUX flag if non-GPIO selected (pinmux will
mbed_official 579:53297373a894 74 * be written later) and store the new MUX mask */
mbed_official 579:53297373a894 75 if (config->mux_position != SYSTEM_PINMUX_GPIO) {
mbed_official 579:53297373a894 76 pin_cfg |= PORT_WRCONFIG_PMUXEN;
mbed_official 579:53297373a894 77 pin_cfg |= (config->mux_position << PORT_WRCONFIG_PMUX_Pos);
mbed_official 579:53297373a894 78 }
mbed_official 579:53297373a894 79
mbed_official 579:53297373a894 80 /* Check if the user has requested that the input buffer be enabled */
mbed_official 579:53297373a894 81 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_INPUT) ||
mbed_official 579:53297373a894 82 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 83 /* Enable input buffer flag */
mbed_official 579:53297373a894 84 pin_cfg |= PORT_WRCONFIG_INEN;
mbed_official 579:53297373a894 85
mbed_official 579:53297373a894 86 /* Enable pull-up/pull-down control flag if requested */
mbed_official 579:53297373a894 87 if (config->input_pull != SYSTEM_PINMUX_PIN_PULL_NONE) {
mbed_official 579:53297373a894 88 pin_cfg |= PORT_WRCONFIG_PULLEN;
mbed_official 579:53297373a894 89 }
mbed_official 579:53297373a894 90
mbed_official 579:53297373a894 91 /* Clear the port DIR bits to disable the output buffer */
mbed_official 579:53297373a894 92 port->DIRCLR.reg = pin_mask;
mbed_official 579:53297373a894 93 }
mbed_official 579:53297373a894 94
mbed_official 579:53297373a894 95 /* Check if the user has requested that the output buffer be enabled */
mbed_official 579:53297373a894 96 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT) ||
mbed_official 579:53297373a894 97 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 98 /* Cannot use a pullup if the output driver is enabled,
mbed_official 579:53297373a894 99 * if requested the input buffer can only sample the current
mbed_official 579:53297373a894 100 * output state */
mbed_official 579:53297373a894 101 pin_cfg &= ~PORT_WRCONFIG_PULLEN;
mbed_official 579:53297373a894 102 }
mbed_official 579:53297373a894 103 } else {
mbed_official 579:53297373a894 104 port->DIRCLR.reg = pin_mask;
mbed_official 579:53297373a894 105 }
mbed_official 579:53297373a894 106
mbed_official 579:53297373a894 107 /* The Write Configuration register (WRCONFIG) requires the
mbed_official 579:53297373a894 108 * pins to to grouped into two 16-bit half-words - split them out here */
mbed_official 579:53297373a894 109 uint32_t lower_pin_mask = (pin_mask & 0xFFFF);
mbed_official 579:53297373a894 110 uint32_t upper_pin_mask = (pin_mask >> 16);
mbed_official 579:53297373a894 111
mbed_official 579:53297373a894 112 /* Configure the lower 16-bits of the port to the desired configuration,
mbed_official 579:53297373a894 113 * including the pin peripheral multiplexer just in case it is enabled */
mbed_official 579:53297373a894 114 port->WRCONFIG.reg
mbed_official 579:53297373a894 115 = (lower_pin_mask << PORT_WRCONFIG_PINMASK_Pos) |
mbed_official 579:53297373a894 116 pin_cfg | PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_WRPINCFG;
mbed_official 579:53297373a894 117
mbed_official 579:53297373a894 118 /* Configure the upper 16-bits of the port to the desired configuration,
mbed_official 579:53297373a894 119 * including the pin peripheral multiplexer just in case it is enabled */
mbed_official 579:53297373a894 120 port->WRCONFIG.reg
mbed_official 579:53297373a894 121 = (upper_pin_mask << PORT_WRCONFIG_PINMASK_Pos) |
mbed_official 579:53297373a894 122 pin_cfg | PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_WRPINCFG |
mbed_official 579:53297373a894 123 PORT_WRCONFIG_HWSEL;
mbed_official 579:53297373a894 124
mbed_official 579:53297373a894 125 if(!config->powersave) {
mbed_official 579:53297373a894 126 /* Set the pull-up state once the port pins are configured if one was
mbed_official 579:53297373a894 127 * requested and it does not violate the valid set of port
mbed_official 579:53297373a894 128 * configurations */
mbed_official 579:53297373a894 129 if (pin_cfg & PORT_WRCONFIG_PULLEN) {
mbed_official 579:53297373a894 130 /* Set the OUT register bits to enable the pullup if requested,
mbed_official 579:53297373a894 131 * clear to enable pull-down */
mbed_official 579:53297373a894 132 if (config->input_pull == SYSTEM_PINMUX_PIN_PULL_UP) {
mbed_official 579:53297373a894 133 port->OUTSET.reg = pin_mask;
mbed_official 579:53297373a894 134 } else {
mbed_official 579:53297373a894 135 port->OUTCLR.reg = pin_mask;
mbed_official 579:53297373a894 136 }
mbed_official 579:53297373a894 137 }
mbed_official 579:53297373a894 138
mbed_official 579:53297373a894 139 /* Check if the user has requested that the output buffer be enabled */
mbed_official 579:53297373a894 140 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT) ||
mbed_official 579:53297373a894 141 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 142 /* Set the port DIR bits to enable the output buffer */
mbed_official 579:53297373a894 143 port->DIRSET.reg = pin_mask;
mbed_official 579:53297373a894 144 }
mbed_official 579:53297373a894 145 }
mbed_official 579:53297373a894 146 }
mbed_official 579:53297373a894 147
mbed_official 579:53297373a894 148 /**
mbed_official 579:53297373a894 149 * \brief Writes a Port pin configuration to the hardware module.
mbed_official 579:53297373a894 150 *
mbed_official 579:53297373a894 151 * Writes out a given configuration of a Port pin configuration to the hardware
mbed_official 579:53297373a894 152 * module.
mbed_official 579:53297373a894 153 *
mbed_official 579:53297373a894 154 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 155 * configuration setting is ignored.
mbed_official 579:53297373a894 156 *
mbed_official 579:53297373a894 157 * \param[in] gpio_pin Index of the GPIO pin to configure
mbed_official 579:53297373a894 158 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 159 */
mbed_official 579:53297373a894 160 void system_pinmux_pin_set_config(
mbed_official 579:53297373a894 161 const uint8_t gpio_pin,
mbed_official 579:53297373a894 162 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 163 {
mbed_official 579:53297373a894 164 PortGroup *const port = system_pinmux_get_group_from_gpio_pin(gpio_pin);
mbed_official 579:53297373a894 165 uint32_t pin_mask = (1UL << (gpio_pin % 32));
mbed_official 579:53297373a894 166
mbed_official 579:53297373a894 167 _system_pinmux_config(port, pin_mask, config);
mbed_official 579:53297373a894 168 }
mbed_official 579:53297373a894 169
mbed_official 579:53297373a894 170 /**
mbed_official 579:53297373a894 171 * \brief Writes a Port pin group configuration to the hardware module.
mbed_official 579:53297373a894 172 *
mbed_official 579:53297373a894 173 * Writes out a given configuration of a Port pin group configuration to the
mbed_official 579:53297373a894 174 * hardware module.
mbed_official 579:53297373a894 175 *
mbed_official 579:53297373a894 176 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 177 * configuration setting is ignored.
mbed_official 579:53297373a894 178 *
mbed_official 579:53297373a894 179 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 180 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 181 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 182 */
mbed_official 579:53297373a894 183 void system_pinmux_group_set_config(
mbed_official 579:53297373a894 184 PortGroup *const port,
mbed_official 579:53297373a894 185 const uint32_t mask,
mbed_official 579:53297373a894 186 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 187 {
mbed_official 579:53297373a894 188 Assert(port);
mbed_official 579:53297373a894 189
mbed_official 579:53297373a894 190 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 191 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 192 _system_pinmux_config(port, (1UL << i), config);
mbed_official 579:53297373a894 193 }
mbed_official 579:53297373a894 194 }
mbed_official 579:53297373a894 195 }
mbed_official 579:53297373a894 196
mbed_official 579:53297373a894 197 /**
mbed_official 579:53297373a894 198 * \brief Configures the input sampling mode for a group of pins.
mbed_official 579:53297373a894 199 *
mbed_official 579:53297373a894 200 * Configures the input sampling mode for a group of pins, to
mbed_official 579:53297373a894 201 * control when the physical I/O pin value is sampled and
mbed_official 579:53297373a894 202 * stored inside the microcontroller.
mbed_official 579:53297373a894 203 *
mbed_official 579:53297373a894 204 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 205 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 206 * \param[in] mode New pin sampling mode to configure
mbed_official 579:53297373a894 207 */
mbed_official 579:53297373a894 208 void system_pinmux_group_set_input_sample_mode(
mbed_official 579:53297373a894 209 PortGroup *const port,
mbed_official 579:53297373a894 210 const uint32_t mask,
mbed_official 579:53297373a894 211 const enum system_pinmux_pin_sample mode)
mbed_official 579:53297373a894 212 {
mbed_official 579:53297373a894 213 Assert(port);
mbed_official 579:53297373a894 214
mbed_official 579:53297373a894 215 if (mode == SYSTEM_PINMUX_PIN_SAMPLE_ONDEMAND) {
mbed_official 579:53297373a894 216 port->CTRL.reg |= mask;
mbed_official 579:53297373a894 217 } else {
mbed_official 579:53297373a894 218 port->CTRL.reg &= ~mask;
mbed_official 579:53297373a894 219 }
mbed_official 579:53297373a894 220 }
mbed_official 579:53297373a894 221
mbed_official 579:53297373a894 222 #ifdef FEATURE_SYSTEM_PINMUX_SLEWRATE_LIMITER
mbed_official 579:53297373a894 223 /**
mbed_official 579:53297373a894 224 * \brief Configures the output slew rate mode for a group of pins.
mbed_official 579:53297373a894 225 *
mbed_official 579:53297373a894 226 * Configures the output slew rate mode for a group of pins, to
mbed_official 579:53297373a894 227 * control the speed at which the physical output pin can react to
mbed_official 579:53297373a894 228 * logical changes of the I/O pin value.
mbed_official 579:53297373a894 229 *
mbed_official 579:53297373a894 230 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 231 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 232 * \param[in] mode New pin slew rate mode to configure
mbed_official 579:53297373a894 233 */
mbed_official 579:53297373a894 234 void system_pinmux_group_set_output_slew_rate(
mbed_official 579:53297373a894 235 PortGroup *const port,
mbed_official 579:53297373a894 236 const uint32_t mask,
mbed_official 579:53297373a894 237 const enum system_pinmux_pin_slew_rate mode)
mbed_official 579:53297373a894 238 {
mbed_official 579:53297373a894 239 Assert(port);
mbed_official 579:53297373a894 240
mbed_official 579:53297373a894 241 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 242 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 243 if (mode == SYSTEM_PINMUX_PIN_SLEW_RATE_LIMITED) {
mbed_official 579:53297373a894 244 port->PINCFG[i].reg |= PORT_PINCFG_SLEWLIM;
mbed_official 579:53297373a894 245 } else {
mbed_official 579:53297373a894 246 port->PINCFG[i].reg &= ~PORT_PINCFG_SLEWLIM;
mbed_official 579:53297373a894 247 }
mbed_official 579:53297373a894 248 }
mbed_official 579:53297373a894 249 }
mbed_official 579:53297373a894 250 }
mbed_official 579:53297373a894 251 #endif
mbed_official 579:53297373a894 252
mbed_official 579:53297373a894 253 #ifdef FEATURE_SYSTEM_PINMUX_DRIVE_STRENGTH
mbed_official 579:53297373a894 254 /**
mbed_official 579:53297373a894 255 * \brief Configures the output driver strength mode for a group of pins.
mbed_official 579:53297373a894 256 *
mbed_official 579:53297373a894 257 * Configures the output drive strength for a group of pins, to
mbed_official 579:53297373a894 258 * control the amount of current the pad is able to sink/source.
mbed_official 579:53297373a894 259 *
mbed_official 579:53297373a894 260 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 261 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 262 * \param[in] mode New output driver strength mode to configure
mbed_official 579:53297373a894 263 */
mbed_official 579:53297373a894 264 void system_pinmux_group_set_output_strength(
mbed_official 579:53297373a894 265 PortGroup *const port,
mbed_official 579:53297373a894 266 const uint32_t mask,
mbed_official 579:53297373a894 267 const enum system_pinmux_pin_strength mode)
mbed_official 579:53297373a894 268 {
mbed_official 579:53297373a894 269 Assert(port);
mbed_official 579:53297373a894 270
mbed_official 579:53297373a894 271 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 272 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 273 if (mode == SYSTEM_PINMUX_PIN_STRENGTH_HIGH) {
mbed_official 579:53297373a894 274 port->PINCFG[i].reg |= PORT_PINCFG_DRVSTR;
mbed_official 579:53297373a894 275 } else {
mbed_official 579:53297373a894 276 port->PINCFG[i].reg &= ~PORT_PINCFG_DRVSTR;
mbed_official 579:53297373a894 277 }
mbed_official 579:53297373a894 278 }
mbed_official 579:53297373a894 279 }
mbed_official 579:53297373a894 280 }
mbed_official 579:53297373a894 281 #endif
mbed_official 579:53297373a894 282
mbed_official 579:53297373a894 283 #ifdef FEATURE_SYSTEM_PINMUX_OPEN_DRAIN
mbed_official 579:53297373a894 284 /**
mbed_official 579:53297373a894 285 * \brief Configures the output driver mode for a group of pins.
mbed_official 579:53297373a894 286 *
mbed_official 579:53297373a894 287 * Configures the output driver mode for a group of pins, to
mbed_official 579:53297373a894 288 * control the pad behavior.
mbed_official 579:53297373a894 289 *
mbed_official 579:53297373a894 290 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 291 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 292 * \param[in] mode New pad output driver mode to configure
mbed_official 579:53297373a894 293 */
mbed_official 579:53297373a894 294 void system_pinmux_group_set_output_drive(
mbed_official 579:53297373a894 295 PortGroup *const port,
mbed_official 579:53297373a894 296 const uint32_t mask,
mbed_official 579:53297373a894 297 const enum system_pinmux_pin_drive mode)
mbed_official 579:53297373a894 298 {
mbed_official 579:53297373a894 299 Assert(port);
mbed_official 579:53297373a894 300
mbed_official 579:53297373a894 301 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 302 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 303 if (mode == SYSTEM_PINMUX_PIN_DRIVE_OPEN_DRAIN) {
mbed_official 579:53297373a894 304 port->PINCFG[i].reg |= PORT_PINCFG_ODRAIN;
mbed_official 579:53297373a894 305 } else {
mbed_official 579:53297373a894 306 port->PINCFG[i].reg &= ~PORT_PINCFG_ODRAIN;
mbed_official 579:53297373a894 307 }
mbed_official 579:53297373a894 308 }
mbed_official 579:53297373a894 309 }
mbed_official 579:53297373a894 310 }
mbed_official 579:53297373a894 311 #endif