mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Parent:
579:53297373a894
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

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

More API implementation for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 #include <pinmux.h>
mbed_official 579:53297373a894 2
mbed_official 579:53297373a894 3 /**
mbed_official 579:53297373a894 4 * \internal
mbed_official 579:53297373a894 5 * Writes out a given configuration of a Port pin configuration to the
mbed_official 579:53297373a894 6 * hardware module.
mbed_official 579:53297373a894 7 *
mbed_official 579:53297373a894 8 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 9 * configuration setting is ignored.
mbed_official 579:53297373a894 10 *
mbed_official 579:53297373a894 11 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 12 * \param[in] pin_mask Mask of the port pin to configure
mbed_official 579:53297373a894 13 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 14 */
mbed_official 579:53297373a894 15 static void _system_pinmux_config(
mbed_official 579:53297373a894 16 PortGroup *const port,
mbed_official 579:53297373a894 17 const uint32_t pin_mask,
mbed_official 579:53297373a894 18 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 19 {
mbed_official 579:53297373a894 20 Assert(port);
mbed_official 579:53297373a894 21 Assert(config);
mbed_official 579:53297373a894 22
mbed_official 579:53297373a894 23 /* Track the configuration bits into a temporary variable before writing */
mbed_official 579:53297373a894 24 uint32_t pin_cfg = 0;
mbed_official 579:53297373a894 25
mbed_official 579:53297373a894 26 /* Enabled powersave mode, don't create configuration */
mbed_official 579:53297373a894 27 if (!config->powersave) {
mbed_official 579:53297373a894 28 /* Enable the pin peripheral MUX flag if non-GPIO selected (pinmux will
mbed_official 579:53297373a894 29 * be written later) and store the new MUX mask */
mbed_official 579:53297373a894 30 if (config->mux_position != SYSTEM_PINMUX_GPIO) {
mbed_official 579:53297373a894 31 pin_cfg |= PORT_WRCONFIG_PMUXEN;
mbed_official 579:53297373a894 32 pin_cfg |= (config->mux_position << PORT_WRCONFIG_PMUX_Pos);
mbed_official 579:53297373a894 33 }
mbed_official 579:53297373a894 34
mbed_official 579:53297373a894 35 /* Check if the user has requested that the input buffer be enabled */
mbed_official 579:53297373a894 36 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_INPUT) ||
mbed_official 579:53297373a894 37 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 38 /* Enable input buffer flag */
mbed_official 579:53297373a894 39 pin_cfg |= PORT_WRCONFIG_INEN;
mbed_official 579:53297373a894 40
mbed_official 579:53297373a894 41 /* Enable pull-up/pull-down control flag if requested */
mbed_official 579:53297373a894 42 if (config->input_pull != SYSTEM_PINMUX_PIN_PULL_NONE) {
mbed_official 579:53297373a894 43 pin_cfg |= PORT_WRCONFIG_PULLEN;
mbed_official 579:53297373a894 44 }
mbed_official 579:53297373a894 45
mbed_official 579:53297373a894 46 /* Clear the port DIR bits to disable the output buffer */
mbed_official 579:53297373a894 47 port->DIRCLR.reg = pin_mask;
mbed_official 579:53297373a894 48 }
mbed_official 579:53297373a894 49
mbed_official 579:53297373a894 50 /* Check if the user has requested that the output buffer be enabled */
mbed_official 579:53297373a894 51 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT) ||
mbed_official 579:53297373a894 52 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 53 /* Cannot use a pullup if the output driver is enabled,
mbed_official 579:53297373a894 54 * if requested the input buffer can only sample the current
mbed_official 579:53297373a894 55 * output state */
mbed_official 579:53297373a894 56 pin_cfg &= ~PORT_WRCONFIG_PULLEN;
mbed_official 579:53297373a894 57 }
mbed_official 579:53297373a894 58 } else {
mbed_official 579:53297373a894 59 port->DIRCLR.reg = pin_mask;
mbed_official 579:53297373a894 60 }
mbed_official 579:53297373a894 61
mbed_official 579:53297373a894 62 /* The Write Configuration register (WRCONFIG) requires the
mbed_official 579:53297373a894 63 * pins to to grouped into two 16-bit half-words - split them out here */
mbed_official 579:53297373a894 64 uint32_t lower_pin_mask = (pin_mask & 0xFFFF);
mbed_official 579:53297373a894 65 uint32_t upper_pin_mask = (pin_mask >> 16);
mbed_official 579:53297373a894 66
mbed_official 579:53297373a894 67 /* Configure the lower 16-bits of the port to the desired configuration,
mbed_official 579:53297373a894 68 * including the pin peripheral multiplexer just in case it is enabled */
mbed_official 579:53297373a894 69 port->WRCONFIG.reg
mbed_official 579:53297373a894 70 = (lower_pin_mask << PORT_WRCONFIG_PINMASK_Pos) |
mbed_official 579:53297373a894 71 pin_cfg | PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_WRPINCFG;
mbed_official 579:53297373a894 72
mbed_official 579:53297373a894 73 /* Configure the upper 16-bits of the port to the desired configuration,
mbed_official 579:53297373a894 74 * including the pin peripheral multiplexer just in case it is enabled */
mbed_official 579:53297373a894 75 port->WRCONFIG.reg
mbed_official 579:53297373a894 76 = (upper_pin_mask << PORT_WRCONFIG_PINMASK_Pos) |
mbed_official 579:53297373a894 77 pin_cfg | PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_WRPINCFG |
mbed_official 579:53297373a894 78 PORT_WRCONFIG_HWSEL;
mbed_official 579:53297373a894 79
mbed_official 579:53297373a894 80 if(!config->powersave) {
mbed_official 579:53297373a894 81 /* Set the pull-up state once the port pins are configured if one was
mbed_official 579:53297373a894 82 * requested and it does not violate the valid set of port
mbed_official 579:53297373a894 83 * configurations */
mbed_official 579:53297373a894 84 if (pin_cfg & PORT_WRCONFIG_PULLEN) {
mbed_official 579:53297373a894 85 /* Set the OUT register bits to enable the pullup if requested,
mbed_official 579:53297373a894 86 * clear to enable pull-down */
mbed_official 579:53297373a894 87 if (config->input_pull == SYSTEM_PINMUX_PIN_PULL_UP) {
mbed_official 579:53297373a894 88 port->OUTSET.reg = pin_mask;
mbed_official 579:53297373a894 89 } else {
mbed_official 579:53297373a894 90 port->OUTCLR.reg = pin_mask;
mbed_official 579:53297373a894 91 }
mbed_official 579:53297373a894 92 }
mbed_official 579:53297373a894 93
mbed_official 579:53297373a894 94 /* Check if the user has requested that the output buffer be enabled */
mbed_official 579:53297373a894 95 if ((config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT) ||
mbed_official 579:53297373a894 96 (config->direction == SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK)) {
mbed_official 579:53297373a894 97 /* Set the port DIR bits to enable the output buffer */
mbed_official 579:53297373a894 98 port->DIRSET.reg = pin_mask;
mbed_official 579:53297373a894 99 }
mbed_official 579:53297373a894 100 }
mbed_official 579:53297373a894 101 }
mbed_official 579:53297373a894 102
mbed_official 579:53297373a894 103 /**
mbed_official 579:53297373a894 104 * \brief Writes a Port pin configuration to the hardware module.
mbed_official 579:53297373a894 105 *
mbed_official 579:53297373a894 106 * Writes out a given configuration of a Port pin configuration to the hardware
mbed_official 579:53297373a894 107 * module.
mbed_official 579:53297373a894 108 *
mbed_official 579:53297373a894 109 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 110 * configuration setting is ignored.
mbed_official 579:53297373a894 111 *
mbed_official 579:53297373a894 112 * \param[in] gpio_pin Index of the GPIO pin to configure
mbed_official 579:53297373a894 113 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 114 */
mbed_official 579:53297373a894 115 void system_pinmux_pin_set_config(
mbed_official 579:53297373a894 116 const uint8_t gpio_pin,
mbed_official 579:53297373a894 117 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 118 {
mbed_official 579:53297373a894 119 PortGroup *const port = system_pinmux_get_group_from_gpio_pin(gpio_pin);
mbed_official 579:53297373a894 120 uint32_t pin_mask = (1UL << (gpio_pin % 32));
mbed_official 579:53297373a894 121
mbed_official 579:53297373a894 122 _system_pinmux_config(port, pin_mask, config);
mbed_official 579:53297373a894 123 }
mbed_official 579:53297373a894 124
mbed_official 579:53297373a894 125 /**
mbed_official 579:53297373a894 126 * \brief Writes a Port pin group configuration to the hardware module.
mbed_official 579:53297373a894 127 *
mbed_official 579:53297373a894 128 * Writes out a given configuration of a Port pin group configuration to the
mbed_official 579:53297373a894 129 * hardware module.
mbed_official 579:53297373a894 130 *
mbed_official 579:53297373a894 131 * \note If the pin direction is set as an output, the pull-up/pull-down input
mbed_official 579:53297373a894 132 * configuration setting is ignored.
mbed_official 579:53297373a894 133 *
mbed_official 579:53297373a894 134 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 135 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 136 * \param[in] config Configuration settings for the pin
mbed_official 579:53297373a894 137 */
mbed_official 579:53297373a894 138 void system_pinmux_group_set_config(
mbed_official 579:53297373a894 139 PortGroup *const port,
mbed_official 579:53297373a894 140 const uint32_t mask,
mbed_official 579:53297373a894 141 const struct system_pinmux_config *const config)
mbed_official 579:53297373a894 142 {
mbed_official 579:53297373a894 143 Assert(port);
mbed_official 579:53297373a894 144
mbed_official 579:53297373a894 145 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 146 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 147 _system_pinmux_config(port, (1UL << i), config);
mbed_official 579:53297373a894 148 }
mbed_official 579:53297373a894 149 }
mbed_official 579:53297373a894 150 }
mbed_official 579:53297373a894 151
mbed_official 579:53297373a894 152 /**
mbed_official 579:53297373a894 153 * \brief Configures the input sampling mode for a group of pins.
mbed_official 579:53297373a894 154 *
mbed_official 579:53297373a894 155 * Configures the input sampling mode for a group of pins, to
mbed_official 579:53297373a894 156 * control when the physical I/O pin value is sampled and
mbed_official 579:53297373a894 157 * stored inside the microcontroller.
mbed_official 579:53297373a894 158 *
mbed_official 579:53297373a894 159 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 160 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 161 * \param[in] mode New pin sampling mode to configure
mbed_official 579:53297373a894 162 */
mbed_official 579:53297373a894 163 void system_pinmux_group_set_input_sample_mode(
mbed_official 579:53297373a894 164 PortGroup *const port,
mbed_official 579:53297373a894 165 const uint32_t mask,
mbed_official 579:53297373a894 166 const enum system_pinmux_pin_sample mode)
mbed_official 579:53297373a894 167 {
mbed_official 579:53297373a894 168 Assert(port);
mbed_official 579:53297373a894 169
mbed_official 579:53297373a894 170 if (mode == SYSTEM_PINMUX_PIN_SAMPLE_ONDEMAND) {
mbed_official 579:53297373a894 171 port->CTRL.reg |= mask;
mbed_official 579:53297373a894 172 } else {
mbed_official 579:53297373a894 173 port->CTRL.reg &= ~mask;
mbed_official 579:53297373a894 174 }
mbed_official 579:53297373a894 175 }
mbed_official 579:53297373a894 176
mbed_official 579:53297373a894 177 #ifdef FEATURE_SYSTEM_PINMUX_SLEWRATE_LIMITER
mbed_official 579:53297373a894 178 /**
mbed_official 579:53297373a894 179 * \brief Configures the output slew rate mode for a group of pins.
mbed_official 579:53297373a894 180 *
mbed_official 579:53297373a894 181 * Configures the output slew rate mode for a group of pins, to
mbed_official 579:53297373a894 182 * control the speed at which the physical output pin can react to
mbed_official 579:53297373a894 183 * logical changes of the I/O pin value.
mbed_official 579:53297373a894 184 *
mbed_official 579:53297373a894 185 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 186 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 187 * \param[in] mode New pin slew rate mode to configure
mbed_official 579:53297373a894 188 */
mbed_official 579:53297373a894 189 void system_pinmux_group_set_output_slew_rate(
mbed_official 579:53297373a894 190 PortGroup *const port,
mbed_official 579:53297373a894 191 const uint32_t mask,
mbed_official 579:53297373a894 192 const enum system_pinmux_pin_slew_rate mode)
mbed_official 579:53297373a894 193 {
mbed_official 579:53297373a894 194 Assert(port);
mbed_official 579:53297373a894 195
mbed_official 579:53297373a894 196 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 197 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 198 if (mode == SYSTEM_PINMUX_PIN_SLEW_RATE_LIMITED) {
mbed_official 579:53297373a894 199 port->PINCFG[i].reg |= PORT_PINCFG_SLEWLIM;
mbed_official 579:53297373a894 200 } else {
mbed_official 579:53297373a894 201 port->PINCFG[i].reg &= ~PORT_PINCFG_SLEWLIM;
mbed_official 579:53297373a894 202 }
mbed_official 579:53297373a894 203 }
mbed_official 579:53297373a894 204 }
mbed_official 579:53297373a894 205 }
mbed_official 579:53297373a894 206 #endif
mbed_official 579:53297373a894 207
mbed_official 579:53297373a894 208 #ifdef FEATURE_SYSTEM_PINMUX_DRIVE_STRENGTH
mbed_official 579:53297373a894 209 /**
mbed_official 579:53297373a894 210 * \brief Configures the output driver strength mode for a group of pins.
mbed_official 579:53297373a894 211 *
mbed_official 579:53297373a894 212 * Configures the output drive strength for a group of pins, to
mbed_official 579:53297373a894 213 * control the amount of current the pad is able to sink/source.
mbed_official 579:53297373a894 214 *
mbed_official 579:53297373a894 215 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 216 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 217 * \param[in] mode New output driver strength mode to configure
mbed_official 579:53297373a894 218 */
mbed_official 579:53297373a894 219 void system_pinmux_group_set_output_strength(
mbed_official 579:53297373a894 220 PortGroup *const port,
mbed_official 579:53297373a894 221 const uint32_t mask,
mbed_official 579:53297373a894 222 const enum system_pinmux_pin_strength mode)
mbed_official 579:53297373a894 223 {
mbed_official 579:53297373a894 224 Assert(port);
mbed_official 579:53297373a894 225
mbed_official 579:53297373a894 226 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 227 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 228 if (mode == SYSTEM_PINMUX_PIN_STRENGTH_HIGH) {
mbed_official 579:53297373a894 229 port->PINCFG[i].reg |= PORT_PINCFG_DRVSTR;
mbed_official 579:53297373a894 230 } else {
mbed_official 579:53297373a894 231 port->PINCFG[i].reg &= ~PORT_PINCFG_DRVSTR;
mbed_official 579:53297373a894 232 }
mbed_official 579:53297373a894 233 }
mbed_official 579:53297373a894 234 }
mbed_official 579:53297373a894 235 }
mbed_official 579:53297373a894 236 #endif
mbed_official 579:53297373a894 237
mbed_official 579:53297373a894 238 #ifdef FEATURE_SYSTEM_PINMUX_OPEN_DRAIN
mbed_official 579:53297373a894 239 /**
mbed_official 579:53297373a894 240 * \brief Configures the output driver mode for a group of pins.
mbed_official 579:53297373a894 241 *
mbed_official 579:53297373a894 242 * Configures the output driver mode for a group of pins, to
mbed_official 579:53297373a894 243 * control the pad behavior.
mbed_official 579:53297373a894 244 *
mbed_official 579:53297373a894 245 * \param[in] port Base of the PORT module to configure
mbed_official 579:53297373a894 246 * \param[in] mask Mask of the port pin(s) to configure
mbed_official 579:53297373a894 247 * \param[in] mode New pad output driver mode to configure
mbed_official 579:53297373a894 248 */
mbed_official 579:53297373a894 249 void system_pinmux_group_set_output_drive(
mbed_official 579:53297373a894 250 PortGroup *const port,
mbed_official 579:53297373a894 251 const uint32_t mask,
mbed_official 579:53297373a894 252 const enum system_pinmux_pin_drive mode)
mbed_official 579:53297373a894 253 {
mbed_official 579:53297373a894 254 Assert(port);
mbed_official 579:53297373a894 255
mbed_official 579:53297373a894 256 for (int i = 0; i < 32; i++) {
mbed_official 579:53297373a894 257 if (mask & (1UL << i)) {
mbed_official 579:53297373a894 258 if (mode == SYSTEM_PINMUX_PIN_DRIVE_OPEN_DRAIN) {
mbed_official 579:53297373a894 259 port->PINCFG[i].reg |= PORT_PINCFG_ODRAIN;
mbed_official 579:53297373a894 260 } else {
mbed_official 579:53297373a894 261 port->PINCFG[i].reg &= ~PORT_PINCFG_ODRAIN;
mbed_official 579:53297373a894 262 }
mbed_official 579:53297373a894 263 }
mbed_official 579:53297373a894 264 }
mbed_official 579:53297373a894 265 }
mbed_official 579:53297373a894 266 #endif