diff --git a/boards/arduino-due/board.c b/boards/arduino-due/board.c index 9d116a371..b19ed2c58 100644 --- a/boards/arduino-due/board.c +++ b/boards/arduino-due/board.c @@ -27,5 +27,5 @@ void board_init(void) /* initialize the CPU */ cpu_init(); /* initialize the on-board Amber "L" LED @ pin PB27 */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/f4vi1/board.c b/boards/f4vi1/board.c index 2f45e73d1..a390ca792 100644 --- a/boards/f4vi1/board.c +++ b/boards/f4vi1/board.c @@ -25,9 +25,9 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/fox/board.c b/boards/fox/board.c index 2e5164dfc..8b68b7986 100644 --- a/boards/fox/board.c +++ b/boards/fox/board.c @@ -28,8 +28,8 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs and turn them off */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); gpio_set(LED0_PIN); gpio_set(LED1_PIN); } diff --git a/boards/frdm-k64f/board.c b/boards/frdm-k64f/board.c index 021f03399..c02590bff 100644 --- a/boards/frdm-k64f/board.c +++ b/boards/frdm-k64f/board.c @@ -28,9 +28,9 @@ void board_init(void) cpu_init(); /* initialize and turn off the on-board RGB-LED */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); gpio_set(LED0_PIN); gpio_set(LED1_PIN); gpio_set(LED2_PIN); diff --git a/boards/iotlab-m3/board.c b/boards/iotlab-m3/board.c index b2a112507..0c4bba1fe 100644 --- a/boards/iotlab-m3/board.c +++ b/boards/iotlab-m3/board.c @@ -27,9 +27,9 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs and turn them off */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); gpio_set(LED0_PIN); gpio_set(LED1_PIN); gpio_set(LED2_PIN); diff --git a/boards/iotlab-m3/include/gpio_params.h b/boards/iotlab-m3/include/gpio_params.h index 17a73f6e1..7a3f26e6c 100644 --- a/boards/iotlab-m3/include/gpio_params.h +++ b/boards/iotlab-m3/include/gpio_params.h @@ -34,20 +34,17 @@ static const saul_gpio_params_t saul_gpio_params[] = { .name = "LED(red)", .pin = LED0_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL, + .mode = GPIO_OUT }, { .name = "LED(green)", .pin = LED1_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL, + .mode = GPIO_OUT }, { .name = "LED(orange)", .pin = LED2_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL, + .mode = GPIO_OUT }, }; diff --git a/boards/limifrog-v1/board.c b/boards/limifrog-v1/board.c index 51840ae8e..ac52819c4 100644 --- a/boards/limifrog-v1/board.c +++ b/boards/limifrog-v1/board.c @@ -25,7 +25,7 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); } diff --git a/boards/limifrog-v1/include/periph_conf.h b/boards/limifrog-v1/include/periph_conf.h index d61b7fd0d..d4740507a 100644 --- a/boards/limifrog-v1/include/periph_conf.h +++ b/boards/limifrog-v1/include/periph_conf.h @@ -150,9 +150,9 @@ static const timer_conf_t timer_config[] = { static const i2c_conf_t i2c_config[] = { /* device, port, scl-, sda-pin-number, I2C-AF, ER-IRQn, EV-IRQn */ {I2C1, GPIO_PIN(PORT_B, 8), GPIO_PIN(PORT_B, 9), - GPIO_AF4, I2C1_ER_IRQn, I2C1_EV_IRQn}, + GPIO_OD_PU, GPIO_AF4, I2C1_ER_IRQn, I2C1_EV_IRQn}, {I2C2, GPIO_PIN(PORT_B, 10), GPIO_PIN(PORT_B, 11), - GPIO_AF4, I2C2_ER_IRQn, I2C2_EV_IRQn}, + GPIO_OD_PU, GPIO_AF4, I2C2_ER_IRQn, I2C2_EV_IRQn}, }; /** @} */ diff --git a/boards/mulle/board.c b/boards/mulle/board.c index 06ebacee6..77fa447bf 100644 --- a/boards/mulle/board.c +++ b/boards/mulle/board.c @@ -60,9 +60,9 @@ void board_init(void) int status; /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); /* Initialize power control pins */ power_pins_init(); @@ -124,9 +124,9 @@ void board_init(void) static inline void power_pins_init(void) { - gpio_init(MULLE_POWER_AVDD, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(MULLE_POWER_VPERIPH, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(MULLE_POWER_VSEC, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(MULLE_POWER_AVDD, GPIO_OUT); + gpio_init(MULLE_POWER_VPERIPH, GPIO_OUT); + gpio_init(MULLE_POWER_VSEC, GPIO_OUT); gpio_clear(MULLE_POWER_AVDD); gpio_clear(MULLE_POWER_VPERIPH); gpio_clear(MULLE_POWER_VSEC); diff --git a/boards/nrf51dongle/board.c b/boards/nrf51dongle/board.c index 3e20f2293..18ceac086 100644 --- a/boards/nrf51dongle/board.c +++ b/boards/nrf51dongle/board.c @@ -24,9 +24,9 @@ void board_init(void) { /* initialize the boards LEDs, set pins as output and turn LEDs off */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/nrf52dk/include/gpio_params.h b/boards/nrf52dk/include/gpio_params.h index 6045bd9f0..bb1846a06 100644 --- a/boards/nrf52dk/include/gpio_params.h +++ b/boards/nrf52dk/include/gpio_params.h @@ -35,50 +35,42 @@ static const saul_gpio_params_t saul_gpio_params[] = { .name = "LED 1", .pin = LED0_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL + .mode = GPIO_OUT }, { .name = "LED 2", .pin = LED1_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL + .mode = GPIO_OUT }, { .name = "LED 3", .pin = LED2_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL + .mode = GPIO_OUT }, { .name = "LED 4", .pin = LED3_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL + .mode = GPIO_OUT }, { .name = "Button 1", .pin = BUTTON1_PIN, - .dir = GPIO_DIR_IN, - .pull = GPIO_PULLUP + .mode = GPIO_IN_PU }, { .name = "Button 2", .pin = BUTTON2_PIN, - .dir = GPIO_DIR_IN, - .pull = GPIO_PULLUP + .mode = GPIO_IN_PU }, { .name = "Button 3", .pin = BUTTON3_PIN, - .dir = GPIO_DIR_IN, - .pull = GPIO_PULLUP + .mode = GPIO_IN_PU }, { .name = "Button 4", .pin = BUTTON4_PIN, - .dir = GPIO_DIR_IN, - .pull = GPIO_PULLUP + .mode = GPIO_IN_PU } }; diff --git a/boards/nucleo-f091/board.c b/boards/nucleo-f091/board.c index 78fb5f716..a8d59f892 100644 --- a/boards/nucleo-f091/board.c +++ b/boards/nucleo-f091/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-f103/board.c b/boards/nucleo-f103/board.c index c79800518..1e2820e37 100644 --- a/boards/nucleo-f103/board.c +++ b/boards/nucleo-f103/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-f303/board.c b/boards/nucleo-f303/board.c index 5c01d7852..d291b0e2a 100755 --- a/boards/nucleo-f303/board.c +++ b/boards/nucleo-f303/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-f334/board.c b/boards/nucleo-f334/board.c index 650a76250..bb320d3f9 100644 --- a/boards/nucleo-f334/board.c +++ b/boards/nucleo-f334/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-f401/board.c b/boards/nucleo-f401/board.c index d1ca7f4fb..5459df139 100644 --- a/boards/nucleo-f401/board.c +++ b/boards/nucleo-f401/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-l1/board.c b/boards/nucleo-l1/board.c index 89f78ade6..a26984467 100644 --- a/boards/nucleo-l1/board.c +++ b/boards/nucleo-l1/board.c @@ -27,5 +27,5 @@ void board_init(void) cpu_init(); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); } diff --git a/boards/nucleo-l1/include/periph_conf.h b/boards/nucleo-l1/include/periph_conf.h index 92db33d20..f546df389 100644 --- a/boards/nucleo-l1/include/periph_conf.h +++ b/boards/nucleo-l1/include/periph_conf.h @@ -254,9 +254,9 @@ static const timer_conf_t timer_config[] = { static const i2c_conf_t i2c_config[] = { /* device, port, scl-, sda-pin-number, I2C-AF, ER-IRQn, EV-IRQn */ - {I2C1, GPIO_PIN(PORT_B, 8), GPIO_PIN(PORT_B, 9), + {I2C1, GPIO_PIN(PORT_B, 8), GPIO_PIN(PORT_B, 9), GPIO_OD_PU, GPIO_AF4, I2C1_ER_IRQn, I2C1_EV_IRQn}, - {I2C2, GPIO_PIN(PORT_B, 10), GPIO_PIN(PORT_B, 11), + {I2C2, GPIO_PIN(PORT_B, 10), GPIO_PIN(PORT_B, 11), GPIO_OD_PU, GPIO_AF4, I2C2_ER_IRQn, I2C2_EV_IRQn}, }; diff --git a/boards/openmote-cc2538/board.c b/boards/openmote-cc2538/board.c index e036b4713..71b532be2 100644 --- a/boards/openmote-cc2538/board.c +++ b/boards/openmote-cc2538/board.c @@ -24,10 +24,10 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED3_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); + gpio_init(LED3_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/pba-d-01-kw2x/board.c b/boards/pba-d-01-kw2x/board.c index a31e8d3c8..0f025f032 100644 --- a/boards/pba-d-01-kw2x/board.c +++ b/boards/pba-d-01-kw2x/board.c @@ -26,9 +26,9 @@ void board_init(void) { /* initialize the on-board LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); /* initialize the CPU core */ cpu_init(); diff --git a/boards/remote/board.c b/boards/remote/board.c index 4627fcce7..010e8b0e6 100644 --- a/boards/remote/board.c +++ b/boards/remote/board.c @@ -28,9 +28,9 @@ static inline void rf_switch_init(void); void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); /* initialize the 2.4GHz RF switch */ diff --git a/boards/saml21-xpro/board.c b/boards/saml21-xpro/board.c index d01673d58..2b9184a95 100644 --- a/boards/saml21-xpro/board.c +++ b/boards/saml21-xpro/board.c @@ -44,5 +44,5 @@ void board_init(void) */ void led_init(void) { - gpio_init(GPIO_PIN(PB,10), GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(GPIO_PIN(PB,10), GPIO_OUT); } diff --git a/boards/samr21-xpro/board.c b/boards/samr21-xpro/board.c index 3a492c8d2..9c63ee27b 100644 --- a/boards/samr21-xpro/board.c +++ b/boards/samr21-xpro/board.c @@ -26,7 +26,7 @@ void board_init(void) { /* initialize the on-board LED */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/samr21-xpro/include/gpio_params.h b/boards/samr21-xpro/include/gpio_params.h index 8551e0d80..6adf9f974 100644 --- a/boards/samr21-xpro/include/gpio_params.h +++ b/boards/samr21-xpro/include/gpio_params.h @@ -36,14 +36,12 @@ static const saul_gpio_params_t saul_gpio_params[] = { .name = "LED(orange)", .pin = LED0_PIN, - .dir = GPIO_DIR_OUT, - .pull = GPIO_NOPULL, + .mode = GPIO_OUT }, { .name = "Button(SW0)", .pin = BUTTON_GPIO, - .dir = GPIO_DIR_IN, - .pull = GPIO_PULLUP, + .mode = GPIO_IN_PU }, }; diff --git a/boards/slwstk6220a/board.c b/boards/slwstk6220a/board.c index 4baec01e0..62560f11d 100644 --- a/boards/slwstk6220a/board.c +++ b/boards/slwstk6220a/board.c @@ -26,11 +26,11 @@ void board_init(void) { /* enable access to the evaluation board controller chip. Without this, the * board controller does not forward the UART output to the USB port */ - gpio_init(BC_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(BC_PIN, GPIO_OUT); gpio_set(BC_PIN); /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); } diff --git a/boards/spark-core/board.c b/boards/spark-core/board.c index b783065d4..111a53aa5 100644 --- a/boards/spark-core/board.c +++ b/boards/spark-core/board.c @@ -25,10 +25,10 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED3_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); + gpio_init(LED3_PIN, GPIO_OUT); gpio_set(LED0_PIN); gpio_set(LED1_PIN); gpio_set(LED2_PIN); diff --git a/boards/stm32f0discovery/board.c b/boards/stm32f0discovery/board.c index ae47f3053..64eea24f9 100644 --- a/boards/stm32f0discovery/board.c +++ b/boards/stm32f0discovery/board.c @@ -24,8 +24,8 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/stm32f3discovery/board.c b/boards/stm32f3discovery/board.c index eff388d18..4960cfaac 100644 --- a/boards/stm32f3discovery/board.c +++ b/boards/stm32f3discovery/board.c @@ -24,14 +24,14 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED3_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED4_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED5_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED6_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED7_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); + gpio_init(LED3_PIN, GPIO_OUT); + gpio_init(LED4_PIN, GPIO_OUT); + gpio_init(LED5_PIN, GPIO_OUT); + gpio_init(LED6_PIN, GPIO_OUT); + gpio_init(LED7_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/stm32f4discovery/board.c b/boards/stm32f4discovery/board.c index 5477cd112..b06836cfb 100644 --- a/boards/stm32f4discovery/board.c +++ b/boards/stm32f4discovery/board.c @@ -24,10 +24,10 @@ void board_init(void) { /* initialize the boards LEDs */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED1_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED2_PIN, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(LED3_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); + gpio_init(LED1_PIN, GPIO_OUT); + gpio_init(LED2_PIN, GPIO_OUT); + gpio_init(LED3_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); diff --git a/boards/udoo/board.c b/boards/udoo/board.c index 87eb99b9b..e5298c6a4 100644 --- a/boards/udoo/board.c +++ b/boards/udoo/board.c @@ -25,7 +25,7 @@ void board_init(void) { /* initialize the on-board Amber "L" LED @ pin PB27 */ - gpio_init(LED0_PIN, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(LED0_PIN, GPIO_OUT); /* initialize the CPU */ cpu_init(); } diff --git a/cpu/atmega2560/periph/gpio.c b/cpu/atmega2560/periph/gpio.c index 3cea0534a..7ce2ec766 100644 --- a/cpu/atmega2560/periph/gpio.c +++ b/cpu/atmega2560/periph/gpio.c @@ -86,53 +86,38 @@ static inline uint16_t _pin_addr(gpio_t pin) return (_port_addr(pin) - 0x02); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { - int res; - - if (dir == GPIO_DIR_OUT) { - _SFR_MEM8(_ddr_addr(pin)) |= (1 << _pin_num(pin)); - res = bit_is_set(_SFR_MEM8(_ddr_addr(pin)), _pin_num(pin)); - } - else { - _SFR_MEM8(_ddr_addr(pin)) &= ~(1 << _pin_num(pin)); - res = bit_is_clear(_SFR_MEM8(_ddr_addr(pin)), _pin_num(pin)); - - if (res == 0) { + switch (mode) { + case GPIO_OUT: + _SFR_MEM8(_ddr_addr(pin)) |= (1 << _pin_num(pin)); + break; + case GPIO_IN: + _SFR_MEM8(_ddr_addr(pin)) &= ~(1 << _pin_num(pin)); + _SFR_MEM8(_port_addr(pin)) &= ~(1 << _pin_num(pin)); + break; + case GPIO_IN_PU: + _SFR_MEM8(_port_addr(pin)) |= (1 << _pin_num(pin)); + break; + default: return -1; - } - - switch (pullup) { - case GPIO_NOPULL: - _SFR_MEM8(_port_addr(pin)) &= ~(1 << _pin_num(pin)); - res = bit_is_clear(_SFR_MEM8(_port_addr(pin)), _pin_num(pin)); - break; - case GPIO_PULLUP: - _SFR_MEM8(_port_addr(pin)) |= (1 << _pin_num(pin)); - res = bit_is_set(_SFR_MEM8(_port_addr(pin)), _pin_num(pin)); - break; - case GPIO_PULLDOWN: - /* Not supported by atmega2560 */ - return -1; - } } - return (res == 0) ? -1 : 0; + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { uint8_t pin_num = _pin_num(pin); if ((_port_num(pin) == PORT_D && pin_num > 3) - || (_port_num(pin) == PORT_E && pin_num < 4)) { + || (_port_num(pin) == PORT_E && pin_num < 4) + || ((mode != GPIO_IN) && (mode != GPIO_IN_PU))) { return -1; } - if (gpio_init(pin, GPIO_DIR_IN, pullup) < 0) { - return -1; - } + gpio_init(pin, mode); /* clear global interrupt flag */ cli(); diff --git a/cpu/cc2538/periph/gpio.c b/cpu/cc2538/periph/gpio.c index 38fa2f5a7..c9823954d 100644 --- a/cpu/cc2538/periph/gpio.c +++ b/cpu/cc2538/periph/gpio.c @@ -343,13 +343,7 @@ static const uint8_t reverse_pin_lut[] = { #endif }; -static const uint32_t ioc_mask_lut[] = { - [GPIO_NOPULL ] = IOC_OVERRIDE_DIS, - [GPIO_PULLUP ] = IOC_OVERRIDE_PUE, - [GPIO_PULLDOWN] = IOC_OVERRIDE_PDE, -}; - -int gpio_init(gpio_t dev, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t dev, gpio_mode_t mode) { int pin; @@ -360,28 +354,42 @@ int gpio_init(gpio_t dev, gpio_dir_t dir, gpio_pp_t pushpull) pin = pin_lut[dev]; gpio_software_control(pin); - if (dir == GPIO_DIR_OUT) { - gpio_dir_output(pin); - /* configure the pin's pull resistor state */ - IOC_PXX_OVER[pin] = IOC_OVERRIDE_OE | ioc_mask_lut[pushpull]; - } - else { - gpio_dir_input(pin); - /* configure the pin's pull resistor state */ - IOC_PXX_OVER[pin] = ioc_mask_lut[pushpull]; + switch (mode) { + case GPIO_IN: + gpio_dir_input(pin); + /* configure the pin's pull resistor state */ + IOC_PXX_OVER[pin] = (IOC_OVERRIDE_DIS); + break; + case GPIO_IN_PD: + gpio_dir_input(pin); + /* configure the pin's pull resistor state */ + IOC_PXX_OVER[pin] = (IOC_OVERRIDE_PDE); + break; + case GPIO_IN_PU: + gpio_dir_input(pin); + /* configure the pin's pull resistor state */ + IOC_PXX_OVER[pin] = (IOC_OVERRIDE_PUE); + case GPIO_OUT: + gpio_dir_output(pin); + /* configure the pin's pull resistor state */ + IOC_PXX_OVER[pin] = (IOC_OVERRIDE_OE | IOC_OVERRIDE_DIS); + break; + default: + return -1; } return 0; } -int gpio_init_int(gpio_t dev, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb, void *arg) +int gpio_init_int(gpio_t dev, gpio_mode_t mode, gpio_flank_t flank, + gpio_cb_t cb, void *arg) { int res, pin, irq_num; uint32_t mask; cc2538_gpio_t* instance; /* Note: gpio_init() also checks if the gpio is enabled. */ - res = gpio_init(dev, GPIO_DIR_IN, pullup); + res = gpio_init(dev, mode); if (res < 0) { return res; } diff --git a/cpu/ezr32wg/include/periph_cpu.h b/cpu/ezr32wg/include/periph_cpu.h index 51f4484bd..450a180ae 100644 --- a/cpu/ezr32wg/include/periph_cpu.h +++ b/cpu/ezr32wg/include/periph_cpu.h @@ -89,26 +89,18 @@ enum { }; /** - * @brief Override direction values + * @brief Override GPIO modes * @{ */ -#define HAVE_GPIO_DIR_T +#define HAVE_GPIO_MODE_T typedef enum { - GPIO_DIR_IN = 0, /**< configure pin as input */ - GPIO_DIR_OUT = 4, /**< configure pin as output */ -} gpio_dir_t; -/** @} */ - -/** - * @brief Override pull register configuration values - * @{ - */ -#define HAVE_GPIO_PP_T -typedef enum { - GPIO_NOPULL = 1, /**< do not use internal pull resistors */ - GPIO_PULLUP = 6, /**< enable internal pull-up resistor */ - GPIO_PULLDOWN = 2 /**< enable internal pull-down resistor */ -} gpio_pp_t; + GPIO_IN = _GPIO_P_MODEL_MODE0_INPUT, /**< IN */ + GPIO_IN_PD = _GPIO_P_MODEL_MODE0_INPUTPULL, /**< IN with pull-down */ + GPIO_IN_PU = _GPIO_P_MODEL_MODE0_INPUTPULL, /**< IN with pull-up */ + GPIO_OUT = _GPIO_P_MODEL_MODE0_PUSHPULL, /**< OUT (push-pull) */ + GPIO_OD = _GPIO_P_MODEL_MODE0_WIREDAND, /**< OD */ + GPIO_OD_PU = _GPIO_P_MODEL_MODE0_WIREDANDPULLUP /**< OD with pull-up */ +} gpio_mode_t; /** @} */ /** diff --git a/cpu/ezr32wg/periph/gpio.c b/cpu/ezr32wg/periph/gpio.c index 09b836174..14e6cd0d4 100644 --- a/cpu/ezr32wg/periph/gpio.c +++ b/cpu/ezr32wg/periph/gpio.c @@ -56,43 +56,38 @@ static inline int _pin_mask(gpio_t pin) return (1 << _pin_pos(pin)); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_P_TypeDef *port = _port(pin); uint32_t pin_pos = _pin_pos(pin); - uint32_t mode; /* enable power for the GPIO module */ CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; - /* if configured as output, no pull resistors are supported */ - if ((dir == GPIO_DIR_OUT) && (pushpull != GPIO_NOPULL)) { - return -1; - } - - /* configure the pin mode: - * case output: no pull resistors available, use default drive strength - * case input: use input without filter, set pull-up, pull-down or no-pull - * as given */ - mode = (dir | (pushpull & 0x3)); + /* configure the mode */ port->MODE[pin_pos >> 3] &= ~(0xf << ((pin_pos & 0x7) * 4)); port->MODE[pin_pos >> 3] |= (mode << ((pin_pos & 0x7) * 4)); - port->CTRL = GPIO_P_CTRL_DRIVEMODE_DEFAULT; - port->DOUT |= (((pushpull >> 2) & 0x1) << pin_pos); + /* reset output register */ + port->DOUTCLR = (1 << pin_pos); + /* if input with pull-up, set the data out register */ + if (mode == GPIO_IN_PU) { + port->DOUTSET = (1 << pin_pos); + } + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { uint32_t pin_pos = _pin_pos(pin); /* configure as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* just in case, disable interrupt for this channel */ GPIO->IEN &= ~(1 << pin_pos); - // /* save callback */ + /* save callback */ isr_ctx[pin_pos].cb = cb; isr_ctx[pin_pos].arg = arg; /* configure interrupt */ diff --git a/cpu/ezr32wg/periph/uart.c b/cpu/ezr32wg/periph/uart.c index 68ba4a062..1fea3e37a 100644 --- a/cpu/ezr32wg/periph/uart.c +++ b/cpu/ezr32wg/periph/uart.c @@ -67,8 +67,8 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) * the division afterwards... */ uart->CLKDIV = (((CLOCK_HFPERCLK << 5) / (16 * baudrate) - 32) << 3); /* configure the pins */ - gpio_init(uart_config[dev].rx_pin, GPIO_DIR_IN, GPIO_NOPULL); - gpio_init(uart_config[dev].tx_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(uart_config[dev].rx_pin, GPIO_IN); + gpio_init(uart_config[dev].tx_pin, GPIO_OUT); uart->ROUTE = ((uart_config[dev].loc << _USART_ROUTE_LOCATION_SHIFT) | USART_ROUTE_RXPEN | USART_ROUTE_TXPEN); /* enable RX interrupt */ diff --git a/cpu/kinetis_common/include/periph_cpu.h b/cpu/kinetis_common/include/periph_cpu.h index 794d95304..9a872219f 100644 --- a/cpu/kinetis_common/include/periph_cpu.h +++ b/cpu/kinetis_common/include/periph_cpu.h @@ -50,6 +50,32 @@ typedef uint16_t gpio_t; */ #define CPUID_LEN (16U) +/** + * @brief Generate GPIO mode bitfields + * + * We use the following bits to encode the pin mode: + * - bit 0: 0 for pull-down or 1 for pull-up + * - bit 1: pull register enable (as configured in bit 0) + * - bit 5: OD enable + * - bit 7: output or input mode + */ +#define GPIO_MODE(pu, pe, od, out) (pu | (pe << 1) | (od << 5) | (out << 7)) + +/** + * @brief Override GPIO modes + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 0, 0, 0), /**< IN */ + GPIO_IN_PD = GPIO_MODE(0, 1, 0, 0), /**< IN with pull-down */ + GPIO_IN_PU = GPIO_MODE(1, 1, 0, 0), /**< IN with pull-up */ + GPIO_OUT = GPIO_MODE(0, 0, 0, 1), /**< OUT (push-pull) */ + GPIO_OD = GPIO_MODE(1, 0, 1, 1), /**< OD */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1, 1), /**< OD with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Define a condensed set of PORT PCR values * @@ -69,18 +95,6 @@ enum { GPIO_PCR_PU = (PORT_PCR_PE_MASK | PORT_PCR_PS_MASK) /**< enable PU */ }; -/** - * @brief Override values for pull register configuration - * @{ - */ -#define HAVE_GPIO_PP_T -typedef enum { - GPIO_NOPULL = 0x0, /**< do not use internal pull resistors */ - GPIO_PULLUP = GPIO_PCR_PU, /**< enable internal pull-up resistor */ - GPIO_PULLDOWN = GPIO_PCR_PD /**< enable internal pull-down resistor */ -} gpio_pp_t; -/** @} */ - /** * @brief Override flank configuration values * @{ diff --git a/cpu/kinetis_common/periph/gpio.c b/cpu/kinetis_common/periph/gpio.c index a44d6255a..9eb5814ed 100644 --- a/cpu/kinetis_common/periph/gpio.c +++ b/cpu/kinetis_common/periph/gpio.c @@ -29,6 +29,16 @@ #include "cpu.h" #include "periph/gpio.h" +/** + * @brief Get the OCR reg value from the gpio_mode_t value + */ +#define MODE_PCR_MASK (PORT_PCR_ODE_MASK | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK) + +/** + * @brief This bit in the mode is set to 1 for output configuration + */ +#define MODE_OUT (0x80) + /** * @brief Shifting a gpio_t value by this number of bit we can extract the * port number from the GPIO base address @@ -161,24 +171,29 @@ static void ctx_clear(int port, int pin) write_map(port, pin, ctx); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { /* set pin to analog mode while configuring it */ gpio_init_port(pin, GPIO_AF_ANALOG); + /* set pin direction */ - gpio(pin)->PDDR &= ~(1 << pin_num(pin)); - gpio(pin)->PDDR |= (dir << pin_num(pin)); - if (dir == GPIO_DIR_OUT) { + if (mode & MODE_OUT) { + gpio(pin)->PDDR |= (1 << pin_num(pin)); gpio(pin)->PCOR = (1 << pin_num(pin)); } + else { + gpio(pin)->PDDR &= ~(1 << pin_num(pin)); + } + /* enable GPIO function */ - port(pin)->PCR[pin_num(pin)] = (GPIO_AF_GPIO | pullup); + port(pin)->PCR[pin_num(pin)] = (GPIO_AF_GPIO | (mode & MODE_PCR_MASK)); return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb, void *arg) +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, + gpio_cb_t cb, void *arg) { - if (gpio_init(pin, GPIO_DIR_IN, pullup) < 0) { + if (gpio_init(pin, mode) < 0) { return -1; } diff --git a/cpu/lm4f120/include/periph_cpu.h b/cpu/lm4f120/include/periph_cpu.h index 901f580e8..d029ff00f 100644 --- a/cpu/lm4f120/include/periph_cpu.h +++ b/cpu/lm4f120/include/periph_cpu.h @@ -40,6 +40,21 @@ typedef uint32_t gpio_t; #define GPIO_PIN(x,y) ((gpio_t)((x<<4) | y)) /** @} */ +/** + * @brief Override GPIO modes + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = (GPIO_DIR_MODE_IN | (GPIO_PIN_TYPE_STD << 4)), /**< IN */ + GPIO_IN_PD = (GPIO_DIR_MODE_IN | (GPIO_PIN_TYPE_STD_WPD << 4)), /**< IN with pull-down */ + GPIO_IN_PU = (GPIO_DIR_MODE_IN | (GPIO_PIN_TYPE_STD_WPU << 4)), /**< IN with pull-up */ + GPIO_OUT = (GPIO_DIR_MODE_OUT | (GPIO_PIN_TYPE_STD << 4)), /**< OUT (push-pull) */ + GPIO_OD = (GPIO_DIR_MODE_OUT | (GPIO_PIN_TYPE_OD << 4)), /**< OD */ + GPIO_OD_PU = (GPIO_DIR_MODE_OUT | (GPIO_PIN_TYPE_OD_WPU << 4)), /**< OD with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Override values for pull register configuration * @{ diff --git a/cpu/lm4f120/periph/gpio.c b/cpu/lm4f120/periph/gpio.c index c5df44432..528d0de32 100644 --- a/cpu/lm4f120/periph/gpio.c +++ b/cpu/lm4f120/periph/gpio.c @@ -29,6 +29,16 @@ #define ENABLE_DEBUG (0) #include "debug.h" +/** + * @brief Mask out the pin type from the gpio_mode_t value + */ +#define TYPE(mode) (mode >> 4) + +/** + * @brief Mask out the pin mode from the gpio_mode_t value + */ +#define MODE(mode) (mode & 0x0f) + /** * @brief Extract the pin number of the given pin */ @@ -90,7 +100,7 @@ static inline uint16_t _port_addr(gpio_t pin) return port_addr; } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { const uint8_t port_num = _port_num(pin); const uint32_t port_addr = _port_base[port_num]; @@ -109,8 +119,8 @@ int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) HWREG(port_addr+GPIO_LOCK_R_OFF) = 0; ROM_GPIOPadConfigSet(port_addr, pin_bit, - GPIO_STRENGTH_2MA, pullup); - ROM_GPIODirModeSet(port_addr, pin_bit, dir); + GPIO_STRENGTH_2MA, TYPE(mode)); + ROM_GPIODirModeSet(port_addr, pin_bit, MODE(mode)); return 0; } @@ -156,7 +166,7 @@ void isr_gpio_portf(void){ _isr_gpio(5); } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { const uint8_t port_num = _port_num(pin); @@ -176,7 +186,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, ROM_GPIODirModeSet(port_addr, 1<DIR[port] |= (1 << pin); /* set pin to output mode */ - } - else { - LPC_GPIO->DIR[port] &= ~(1 << pin); /* set pin to output mode */ + /* Set mode */ + switch (mode) { + case GPIO_IN: + LPC_GPIO->DIR[port] &= ~(1 << pin); + break; + case GPIO_IN_PD: + LPC_GPIO->DIR[port] &= ~(1 << pin); + *lpc_pin_registers[pin + (port * 24)] |= (1 << 3); + break; + case GPIO_IN_PU: + LPC_GPIO->DIR[port] &= ~(1 << pin); + *lpc_pin_registers[pin + (port * 24)] |= (2 << 3); + break; + case GPIO_OUT: + LPC_GPIO->DIR[port] |= (1 << pin); + break; + default: + return -1; } return 0; /* all OK */ } -int gpio_init_int(gpio_t dev, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb, void *arg) +int gpio_init_int(gpio_t dev, gpio_mode_t mode, gpio_flank_t flank, + gpio_cb_t cb, void *arg) { int res; uint8_t pin; @@ -318,7 +324,7 @@ int gpio_init_int(gpio_t dev, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb pin = gpio_pin_map[dev]; /* configure pin as input */ - res = gpio_init(dev, GPIO_DIR_IN, pullup); + res = gpio_init(dev, mode); if (res < 0) { return res; } diff --git a/cpu/lpc2387/include/periph_cpu.h b/cpu/lpc2387/include/periph_cpu.h index b80cf392e..0a84387c2 100644 --- a/cpu/lpc2387/include/periph_cpu.h +++ b/cpu/lpc2387/include/periph_cpu.h @@ -56,13 +56,6 @@ void gpio_init_states(void); #define GPIO_PIN(port, pin) (port*32 + pin) -#define HAVE_GPIO_PP_T -typedef enum { - GPIO_PULLUP = 0, /**< enable internal pull-up resistor */ - GPIO_NOPULL = 2, /**< do not use internal pull resistors */ - GPIO_PULLDOWN = 3 /**< enable internal pull-down resistor */ -} gpio_pp_t; - #define HAVE_GPIO_FLANK_T typedef enum { GPIO_FALLING = 1, /**< emit interrupt on falling flank */ diff --git a/cpu/lpc2387/periph/gpio.c b/cpu/lpc2387/periph/gpio.c index 6862115e7..987d99582 100644 --- a/cpu/lpc2387/periph/gpio.c +++ b/cpu/lpc2387/periph/gpio.c @@ -62,12 +62,15 @@ static int _isr_map_entry(gpio_t pin) { return _pin; } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { - (void) dir; unsigned _pin = pin & 31; unsigned port = pin >> 5; + if (mode != GPIO_OUT) { + return -1; + } + FIO_PORT_t *_port = &FIO_PORTS[port]; /* set mask */ @@ -76,9 +79,6 @@ int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) /* set direction */ _port->DIR = ~0; - /* set pullup/pulldown **/ - PINMODE[pin>>4] |= pullup << (_pin*2); - gpio_init_mux(pin, 0); return 0; @@ -92,9 +92,11 @@ int gpio_init_mux(unsigned pin, unsigned mux) return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { + (void)mode; + DEBUG("gpio_init_int(): pin %u\n", pin); int isr_map_entry; @@ -125,7 +127,6 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, _gpio_states[_state_index].arg = arg; extern void GPIO_IRQHandler(void); - gpio_init(pin, GPIO_DIR_IN, pullup); if (flank & GPIO_FALLING) { bf_set(_gpio_falling, _state_index); diff --git a/cpu/msp430fxyz/include/periph_cpu.h b/cpu/msp430fxyz/include/periph_cpu.h index c7050ca2d..7ac660732 100644 --- a/cpu/msp430fxyz/include/periph_cpu.h +++ b/cpu/msp430fxyz/include/periph_cpu.h @@ -45,17 +45,6 @@ typedef uint16_t gpio_t; */ #define GPIO_PIN(x, y) ((gpio_t)(((x & 0xff) << 8) | (1 << (y & 0xff)))) -/** - * @brief Override direction values - * @{ - */ -#define HAVE_GPIO_DIR_T -typedef enum { - GPIO_DIR_IN = 0x00, /**< configure pin as input */ - GPIO_DIR_OUT = 0xff, /**< configure pin as output */ -} gpio_dir_t; -/** @} */ - /** * @brief Override flank selection values * @{ diff --git a/cpu/msp430fxyz/periph/gpio.c b/cpu/msp430fxyz/periph/gpio.c index 4e820e174..1432e9e64 100644 --- a/cpu/msp430fxyz/periph/gpio.c +++ b/cpu/msp430fxyz/periph/gpio.c @@ -78,35 +78,41 @@ static int _ctx(gpio_t pin) return (_port(pin) == PORT_1) ? i : (i + 8); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { msp_port_t *port = _port(pin); - /* check if port is valid and pull resistor are valid */ - if ((port == NULL) || (pullup != GPIO_NOPULL)) { + + /* check if port is valid and mode applicable */ + if ((port == NULL) || ((mode != GPIO_IN) && (mode != GPIO_OUT))) { return -1; } - /* set pin direction */ + + /* reset pin and output value */ port->DIR &= ~(_pin(pin)); - port->DIR |= (dir & _pin(pin)); - /* reset output value */ port->OD &= ~(_pin(pin)); + if (mode == GPIO_OUT) { + port->DIR |= _pin(pin); + } + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { msp_port_isr_t *port = _isr_port(pin); /* check if port, pull resistor and flank configuration are valid */ - if ((port == NULL) || (pullup != GPIO_NOPULL) || (flank == GPIO_BOTH)) { + if ((port == NULL) || (flank == GPIO_BOTH)) { return -1; } /* disable any activated interrupt */ port->IE &= ~(_pin(pin)); /* configure as input */ - gpio_init(pin, GPIO_DIR_IN, GPIO_NOPULL); + if (gpio_init(pin, mode) < 0) { + return -1; + } /* save ISR context */ isr_ctx[_ctx(pin)].cb = cb; isr_ctx[_ctx(pin)].arg = arg; diff --git a/cpu/nrf5x_common/include/periph_cpu.h b/cpu/nrf5x_common/include/periph_cpu.h index d287e0feb..07ab0affb 100644 --- a/cpu/nrf5x_common/include/periph_cpu.h +++ b/cpu/nrf5x_common/include/periph_cpu.h @@ -54,15 +54,33 @@ extern "C" { #define GPIO_PIN(x,y) ((x & 0) | y) /** - * @brief Override GPIO pull register select values + * @brief Generate GPIO mode bitfields + * + * We use 4 bit to encode the pin mode: + * - bit 0: output enable + * - bit 1: input connect + * - bit 2+3: pull resistor configuration + */ +#define GPIO_MODE(oe, ic, pr) (oe | (ic << 1) | (pr << 2)) + +/** + * @brief Override GPIO modes + * + * We use 4 bit to encode the pin mode: + * - bit 0: output enable + * - bit 1: input connect + * - bit 2+3: pull resistor configuration * @{ */ -#define HAVE_GPIO_PP_T +#define HAVE_GPIO_MODE_T typedef enum { - GPIO_NOPULL = 0, /**< do not use internal pull resistors */ - GPIO_PULLUP = 3, /**< enable internal pull-up resistor */ - GPIO_PULLDOWN = 1 /**< enable internal pull-down resistor */ -} gpio_pp_t; + GPIO_IN = GPIO_MODE(0, 0, 0), /**< IN */ + GPIO_IN_PD = GPIO_MODE(0, 0, 1), /**< IN with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 0, 3), /**< IN with pull-up */ + GPIO_OUT = GPIO_MODE(1, 1, 0), /**< OUT (push-pull) */ + GPIO_OD = (0xff), /**< not supported by HW */ + GPIO_OD_PU = (0xfe) /**< not supported by HW */ +} gpio_mode_t; /** @} */ /** diff --git a/cpu/nrf5x_common/periph/gpio.c b/cpu/nrf5x_common/periph/gpio.c index e8f7a84d0..861bd1a7f 100644 --- a/cpu/nrf5x_common/periph/gpio.c +++ b/cpu/nrf5x_common/periph/gpio.c @@ -38,16 +38,24 @@ static gpio_isr_ctx_t exti_chan; -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { - /* configure pin direction, input buffer and pull resistor state */ - GPIO_BASE->PIN_CNF[pin] = ((dir << GPIO_PIN_CNF_DIR_Pos) | - (dir << GPIO_PIN_CNF_INPUT_Pos) | - (pullup << GPIO_PIN_CNF_PULL_Pos)); + switch (mode) { + case GPIO_IN: + case GPIO_IN_PD: + case GPIO_IN_PU: + case GPIO_OUT: + /* configure pin direction, input buffer and pull resistor state */ + GPIO_BASE->PIN_CNF[pin] = mode; + break; + default: + return -1; + } + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { /* disable external interrupt in case one is active */ @@ -56,7 +64,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, exti_chan.cb = cb; exti_chan.arg = arg; /* configure pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* set interrupt priority and enable global GPIOTE interrupt */ NVIC_EnableIRQ(GPIOTE_IRQn); /* configure the GPIOTE channel: set even mode, pin and active flank */ diff --git a/cpu/sam3/include/periph_cpu.h b/cpu/sam3/include/periph_cpu.h index 2208afe2b..efcc16326 100644 --- a/cpu/sam3/include/periph_cpu.h +++ b/cpu/sam3/include/periph_cpu.h @@ -71,15 +71,28 @@ typedef uint32_t gpio_t; #define TIMER_CHANNELS (3) /** - * @brief Override values for pull register configuration + * @brief Generate GPIO mode bitfields + * + * We use 3 bit to determine the pin functions: + * - bit 0: in/out + * - bit 1: PU enable + * - bit 2: OD enable + */ +#define GPIO_MODE(io, pu, od) (io | (pu << 1) | (od << 2)) + +/** + * @brief Override GPIO modes * @{ */ -#define HAVE_GPIO_PP_T +#define HAVE_GPIO_MODE_T typedef enum { - GPIO_NOPULL = 4, /**< do not use internal pull resistors */ - GPIO_PULLUP = 9, /**< enable internal pull-up resistor */ - GPIO_PULLDOWN = 8 /**< enable internal pull-down resistor */ -} gpio_pp_t; + GPIO_IN = GPIO_MODE(0, 0, 0), /**< IN */ + GPIO_IN_PD = 0xf, /**< not supported by HW */ + GPIO_IN_PU = GPIO_MODE(0, 1, 0), /**< IN with pull-up */ + GPIO_OUT = GPIO_MODE(1, 0, 0), /**< OUT (push-pull) */ + GPIO_OD = GPIO_MODE(1, 0, 1), /**< OD */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1), /**< OD with pull-up */ +} gpio_mode_t; /** @} */ /** diff --git a/cpu/sam3/periph/gpio.c b/cpu/sam3/periph/gpio.c index 1bc9cfa4c..7043054b4 100644 --- a/cpu/sam3/periph/gpio.c +++ b/cpu/sam3/periph/gpio.c @@ -42,6 +42,15 @@ */ #define CTX_NUMOF (7U) +/** + * @brief Bit positions in the GPIO mode value + * @{ + */ +#define MODE_BIT_IO (0x1) +#define MODE_BIT_PUE (0x2) +#define MODE_BIT_ODE (0x4) +/** @} */ + /** * @brief Allocation of memory for 7 independent interrupt slots */ @@ -137,14 +146,14 @@ static void _ctx_clear(int port, int pin) } } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t pin, gpio_mode_t mode) { Pio *port = _port(pin); int pin_num = _pin_num(pin); int port_num = _port_num(pin); - /* make sure port is valid */ - if (!_port_valid(port)) { + /* make sure port is valid and no pull-down is selected*/ + if (!_port_valid(port) || (mode == GPIO_IN_PD)) { return -1; } @@ -157,31 +166,33 @@ int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) /* give the PIO module the power over the corresponding pin */ port->PIO_PER = (1 << pin_num); - /* configure the pin's pull resistor state */ - switch (pushpull) { - case GPIO_PULLDOWN: - return -1; - case GPIO_PULLUP: - port->PIO_PUER = (1 << pin_num); - break; - case GPIO_NOPULL: - port->PIO_PUDR = (1 << pin_num); - break; - } - if (dir == GPIO_DIR_OUT) { - /* configure pin as output */ + + /* configure pin direction (in/out) */ + if (mode & MODE_BIT_IO) { port->PIO_OER = (1 << pin_num); - port->PIO_CODR = (1 << pin_num); } else { - /* configure pin as input */ port->PIO_ODR = (1 << pin_num); } + /* set pull-up */ + if (mode & MODE_BIT_PUE) { + port->PIO_PUER = (1 << pin_num); + } + else { + port->PIO_PUDR = (1 << pin_num); + } + /* set multi-driver (open-drain) mode */ + if (mode & MODE_BIT_ODE) { + port->PIO_MDER = (1 << pin_num); + } + else { + port->PIO_MDDR = (1 << pin_num); + } return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pushpull, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { Pio *port = _port(pin); @@ -194,7 +205,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pushpull, gpio_flank_t flank, } /* configure pin as input */ - gpio_init(pin, GPIO_DIR_IN, pushpull); + gpio_init(pin, mode); /* try go grab a free spot in the context array */ int ctx_num = _get_free_ctx(); diff --git a/cpu/samd21/include/periph_cpu.h b/cpu/samd21/include/periph_cpu.h index 714301023..1979078cc 100644 --- a/cpu/samd21/include/periph_cpu.h +++ b/cpu/samd21/include/periph_cpu.h @@ -34,6 +34,31 @@ enum { PC = 2, /**< port C */ }; +/** + * @brief Generate GPIO mode bitfields + * + * We use 3 bit to determine the pin functions: + * - bit 0: PU or PU + * - bit 1: input enable + * - bit 2: pull enable + */ +#define GPIO_MODE(pr, ie, pe) (pr | (ie << 1) | (pe << 2)) + +/** + * @brief Override GPIO modes + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 1, 0), /**< IN */ + GPIO_IN_PD = GPIO_MODE(0, 1, 1), /**< IN with pull-down */ + GPIO_IN_PU = GPIO_MODE(1, 1, 1), /**< IN with pull-up */ + GPIO_OUT = GPIO_MODE(0, 0, 0), /**< OUT (push-pull) */ + GPIO_OD = 0xfe, /**< not supported by HW */ + GPIO_OD_PU = 0xff /**< not supported by HW */ +} gpio_mode_t; +/** @} */ + /** * @brief PWM channel configuration data structure */ diff --git a/cpu/samd21/periph/gpio.c b/cpu/samd21/periph/gpio.c index 77fb87249..bc1ce3dac 100644 --- a/cpu/samd21/periph/gpio.c +++ b/cpu/samd21/periph/gpio.c @@ -34,6 +34,11 @@ */ #define NUMOF_IRQS (16U) +/** + * @brief Mask to get PINCFG reg value from mode value + */ +#define MODE_PINCFG_MASK (0x06) + /** * @brief Mapping of pins to EXTI lines, -1 means not EXTI possible */ @@ -84,42 +89,38 @@ void gpio_init_mux(gpio_t pin, gpio_mux_t mux) port->PMUX[pin_pos >> 1].reg |= (mux << (4 * (pin_pos & 0x1))); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t pin, gpio_mode_t mode) { PortGroup* port = _port(pin); int pin_pos = _pin_pos(pin); int pin_mask = _pin_mask(pin); - /* configure the pin's pull resistor and reset all other configuration */ - switch (pushpull) { - case GPIO_PULLDOWN: - port->OUTCLR.reg = pin_mask; - port->PINCFG[pin_pos].reg = PORT_PINCFG_PULLEN; - break; - case GPIO_PULLUP: - port->OUTSET.reg = pin_mask; - port->PINCFG[pin_pos].reg = PORT_PINCFG_PULLEN; - break; - case GPIO_NOPULL: - port->PINCFG[pin_pos].reg = 0; - break; + /* make sure pin mode is applicable */ + if (mode > 0x7) { + return -1; } - /* set pin_pos direction */ - if (dir == GPIO_DIR_OUT) { - if (pushpull == GPIO_PULLDOWN) { - return -1; - } - port->DIRSET.reg = pin_mask; /* configure as output */ - port->OUTCLR.reg = pin_mask; /* set pin LOW on init */ + + /* set pin direction */ + if (mode & 0x2) { + port->DIRCLR.reg = pin_mask; } else { - port->DIRCLR.reg = pin_mask; /* configure as input */ - port->PINCFG[pin_pos].reg |= PORT_PINCFG_INEN; + port->DIRSET.reg = pin_mask; } + + /* configure the pin cfg and clear output register */ + port->PINCFG[pin_pos].reg = (mode & MODE_PINCFG_MASK); + port->OUTCLR.reg = pin_mask; + + /* and set pull-up/pull-down if applicable */ + if (mode == 0x7) { + port->OUTSET.reg = pin_mask; + } + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int exti = _exti(pin); @@ -133,7 +134,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, gpio_config[exti].cb = cb; gpio_config[exti].arg = arg; /* configure pin as input and set MUX to peripheral function A */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); gpio_init_mux(pin, GPIO_MUX_A); /* enable clocks for the EIC module */ PM->APBAMASK.reg |= PM_APBAMASK_EIC; diff --git a/cpu/samd21/periph/pwm.c b/cpu/samd21/periph/pwm.c index 477bd2329..2c96f23df 100644 --- a/cpu/samd21/periph/pwm.c +++ b/cpu/samd21/periph/pwm.c @@ -107,7 +107,7 @@ uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res) /* configure the used pins */ for (int i = 0; i < PWM_MAX_CHANNELS; i++) { if (pwm_config[dev].chan[i].pin != GPIO_UNDEF) { - gpio_init(pwm_config[dev].chan[i].pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(pwm_config[dev].chan[i].pin, GPIO_OUT); gpio_init_mux(pwm_config[dev].chan[i].pin, pwm_config[dev].chan[i].mux); } } diff --git a/cpu/samd21/periph/uart.c b/cpu/samd21/periph/uart.c index e2047951d..44efebba4 100644 --- a/cpu/samd21/periph/uart.c +++ b/cpu/samd21/periph/uart.c @@ -79,9 +79,9 @@ static int init_base(uart_t uart, uint32_t baudrate) /* enable sync and async clocks */ uart_poweron(uart); /* configure pins */ - gpio_init(uart_config[uart].rx_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(uart_config[uart].rx_pin, GPIO_IN); gpio_init_mux(uart_config[uart].rx_pin, uart_config[uart].mux); - gpio_init(uart_config[uart].tx_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(uart_config[uart].tx_pin, GPIO_OUT); gpio_init_mux(uart_config[uart].tx_pin, uart_config[uart].mux); /* reset the UART device */ dev->CTRLA.reg = SERCOM_USART_CTRLA_SWRST; diff --git a/cpu/saml21/include/periph_cpu.h b/cpu/saml21/include/periph_cpu.h index 1067842fe..3da96d183 100644 --- a/cpu/saml21/include/periph_cpu.h +++ b/cpu/saml21/include/periph_cpu.h @@ -33,6 +33,31 @@ enum { PB = 1, /**< port B */ }; +/** + * @brief Generate GPIO mode bitfields + * + * We use 3 bit to determine the pin functions: + * - bit 0: PU or PU + * - bit 1: input enable + * - bit 2: pull enable + */ +#define GPIO_MODE(pr, ie, pe) (pr | (ie << 1) | (pe << 2)) + +/** + * @brief Override GPIO modes + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 1, 0), /**< IN */ + GPIO_IN_PD = GPIO_MODE(0, 1, 1), /**< IN with pull-down */ + GPIO_IN_PU = GPIO_MODE(1, 1, 1), /**< IN with pull-up */ + GPIO_OUT = GPIO_MODE(0, 0, 0), /**< OUT (push-pull) */ + GPIO_OD = 0xfe, /**< not supported by HW */ + GPIO_OD_PU = 0xff /**< not supported by HW */ +} gpio_mode_t; +/** @} */ + #ifdef __cplusplus } #endif diff --git a/cpu/saml21/periph/gpio.c b/cpu/saml21/periph/gpio.c index 74f6bce3e..29d10bf82 100644 --- a/cpu/saml21/periph/gpio.c +++ b/cpu/saml21/periph/gpio.c @@ -41,6 +41,11 @@ */ #define NUMOF_IRQS (16U) +/** + * @brief Mask to get PINCFG reg value from mode value + */ +#define MODE_PINCFG_MASK (0x06) + /** * @brief Mapping of pins to EXTI lines, -1 means not EXTI possible */ @@ -89,42 +94,38 @@ void gpio_init_mux(gpio_t pin, gpio_mux_t mux) port->PMUX[pin_pos >> 1].reg |= (mux << (4 * (pin_pos & 0x1))); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t pin, gpio_mode_t mode) { PortGroup* port = _port(pin); int pin_pos = _pin_pos(pin); int pin_mask = _pin_mask(pin); - /* configure the pin's pull resistor and reset all other configuration */ - switch (pushpull) { - case GPIO_PULLDOWN: - port->OUTCLR.reg = pin_mask; - port->PINCFG[pin_pos].reg = PORT_PINCFG_PULLEN; - break; - case GPIO_PULLUP: - port->OUTSET.reg = pin_mask; - port->PINCFG[pin_pos].reg = PORT_PINCFG_PULLEN; - break; - case GPIO_NOPULL: - port->PINCFG[pin_pos].reg = 0; - break; + /* make sure pin mode is applicable */ + if (mode > 0x7) { + return -1; } - /* set pin_pos direction */ - if (dir == GPIO_DIR_OUT) { - if (pushpull == GPIO_PULLDOWN) { - return -1; - } - port->DIRSET.reg = pin_mask; /* configure as output */ - port->OUTCLR.reg = pin_mask; /* set pin LOW on init */ + + /* set pin direction */ + if (mode & 0x2) { + port->DIRCLR.reg = pin_mask; } else { - port->DIRCLR.reg = pin_mask; /* configure as input */ - port->PINCFG[pin_pos].reg |= PORT_PINCFG_INEN; + port->DIRSET.reg = pin_mask; } + + /* configure the pin cfg and clear output register */ + port->PINCFG[pin_pos].reg = (mode & MODE_PINCFG_MASK); + port->OUTCLR.reg = pin_mask; + + /* and set pull-up/pull-down if applicable */ + if (mode == 0x7) { + port->OUTSET.reg = pin_mask; + } + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int exti = _exti(pin); @@ -138,7 +139,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, gpio_config[exti].cb = cb; gpio_config[exti].arg = arg; /* configure pin as input and set MUX to peripheral function A */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); gpio_init_mux(pin, GPIO_MUX_A); /* enable clocks for the EIC module */ MCLK->APBAMASK.reg |= MCLK_APBAMASK_EIC; diff --git a/cpu/saml21/periph/spi.c b/cpu/saml21/periph/spi.c index 38c12cebc..617024308 100644 --- a/cpu/saml21/periph/spi.c +++ b/cpu/saml21/periph/spi.c @@ -134,10 +134,10 @@ int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed) while (!(GCLK->PCHCTRL[spi[dev].gclk_id].reg & GCLK_PCHCTRL_CHEN)) {} /* SCLK+MOSI = output */ - gpio_init(spi[dev].sclk.pin, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(spi[dev].mosi.pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(spi[dev].sclk.pin, GPIO_OUT); + gpio_init(spi[dev].mosi.pin, GPIO_OUT); /* MISO = input */ - gpio_init(spi[dev].miso.pin, GPIO_DIR_IN, GPIO_PULLUP); + gpio_init(spi[dev].miso.pin, GPIO_IN); /* * Set alternate funcion (PMUX) for our ports. diff --git a/cpu/stm32f0/include/periph_cpu.h b/cpu/stm32f0/include/periph_cpu.h index 5674f1ecb..34d9e2c0b 100644 --- a/cpu/stm32f0/include/periph_cpu.h +++ b/cpu/stm32f0/include/periph_cpu.h @@ -44,6 +44,31 @@ typedef uint32_t gpio_t; */ #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y) +/** + * @brief Generate GPIO mode bitfields + * + * We use 5 bit to encode the mode: + * - bit 0+1: pin mode (input / output) + * - bit 2+3: pull resistor configuration + * - bit 4: output type (0: push-pull, 1: open-drain) + */ +#define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4)) + +/** + * @brief Override GPIO mode options + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 0, 0), /**< input w/o pull R */ + GPIO_IN_PD = GPIO_MODE(0, 2, 0), /**< input with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 1, 0), /**< input with pull-up */ + GPIO_OUT = GPIO_MODE(1, 0, 0), /**< push-pull output */ + GPIO_OD = GPIO_MODE(1, 0, 1), /**< open-drain w/o pull R */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1) /**< open-drain with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Override flank configuration values * @{ diff --git a/cpu/stm32f0/periph/gpio.c b/cpu/stm32f0/periph/gpio.c index 5f1e15f2e..1a40186f8 100644 --- a/cpu/stm32f0/periph/gpio.c +++ b/cpu/stm32f0/periph/gpio.c @@ -61,31 +61,32 @@ static inline int _pin_num(gpio_t pin) return (pin & 0x0f); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); /* enable clock */ RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin)); - /* configure pull register */ - port->PUPDR &= ~(3 << (2 * pin_num)); - port->PUPDR |= (pullup << (2 * pin_num)); - /* set direction */ - if (dir == GPIO_DIR_OUT) { - port->MODER &= ~(3 << (2 * pin_num)); /* set pin to output mode */ - port->MODER |= (1 << (2 * pin_num)); - port->OTYPER &= ~(1 << pin_num); /* set to push-pull */ - port->OSPEEDR |= (3 << (2 * pin_num)); /* set to high speed */ - port->ODR &= ~(1 << pin_num); /* set pin to low signal */ - } - else { - port->MODER &= ~(3 << (2 * pin_num)); /* configure pin as input */ - } + + /* set mode */ + port->MODER &= ~(0x3 << (2 * pin_num)); + port->MODER |= ((mode & 0x3) << (2 * pin_num)); + /* set pull resistor configuration */ + port->PUPDR &= ~(0x3 << (2 * pin_num)); + port->PUPDR |= (((mode >> 2) & 0x3) << (2 * pin_num)); + /* set output mode */ + port->OTYPER &= ~(1 << pin_num); + port->OTYPER |= (((mode >> 4) & 0x1) << pin_num); + /* finally set pin speed to maximum and reset output */ + port->OSPEEDR |= (3 << (2 * pin_num)); + port->BRR = (1 << pin_num); + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb, void *arg) +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, + gpio_cb_t cb, void *arg) { int pin_num = _pin_num(pin); int port_num = _port_num(pin); @@ -98,7 +99,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, gpio_cb_t cb RCC->APB2ENR |= RCC_APB2ENR_SYSCFGCOMPEN; /* initialize pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* enable global pin interrupt */ if (pin_num < 2) { diff --git a/cpu/stm32f1/include/periph_cpu.h b/cpu/stm32f1/include/periph_cpu.h index 4cbf4d79f..459faaff2 100644 --- a/cpu/stm32f1/include/periph_cpu.h +++ b/cpu/stm32f1/include/periph_cpu.h @@ -58,6 +58,33 @@ typedef uint32_t gpio_t; */ #define TIMER_MAXVAL (0xffff) +/** + * @brief Generate GPIO mode bitfields + * + * We use 4 bit to determine the pin functions: + * - bit 2+3: in/out + * - bit 1: PU enable + * - bit 2: OD enable + */ +#define GPIO_MODE(mode, cnf) (mode | (cnf << 2)) + +/** + * @brief Override GPIO mode options + * + * We use 4 bit to encode CNF and MODE. + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 1), /**< input w/o pull R */ + GPIO_IN_PD = GPIO_MODE(0, 2), /**< input with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 2), /**< input with pull-up */ + GPIO_OUT = GPIO_MODE(3, 0), /**< push-pull output */ + GPIO_OD = GPIO_MODE(3, 1), /**< open-drain w/o pull R */ + GPIO_OD_PU = (0xff) /**< not supported by HW */ +} gpio_mode_t; +/** @} */ + /** * @brief Override values for pull register configuration * @{ diff --git a/cpu/stm32f1/periph/gpio.c b/cpu/stm32f1/periph/gpio.c index 209402e73..ebd8020d3 100644 --- a/cpu/stm32f1/periph/gpio.c +++ b/cpu/stm32f1/periph/gpio.c @@ -68,38 +68,32 @@ static inline int _pin_num(gpio_t pin) } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); + /* open-drain output with pull-up is not supported */ + if (mode == GPIO_OD_PU) { + return -1; + } /* enable the clock for the selected port */ RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN << _port_num(pin)); - /* clear configuration */ + + /* set pin mode */ port->CR[pin_num >> 3] &= ~(0xf << ((pin_num & 0x7) * 4)); - /* set new configuration */ - if (dir == GPIO_DIR_OUT) { - if (pullup != GPIO_NOPULL) { - return -1; - } - /* set to output, push-pull, 50MHz */ - port->CR[pin_num >> 3] |= (0x3 << ((pin_num & 0x7) * 4)); - /* clear pin */ - port->BRR = (1 << pin_num); - } - else { - /* configure pin to input, pull register according to the value of - * the pullup parameter */ - port->CR[pin_num >> 3] |= ((pullup & 0xc) << ((pin_num & 0x7) * 4)); - port->ODR &= ~(1 << pin_num); - port->ODR |= ((pullup & 0x1) << pin_num); + port->CR[pin_num >> 3] |= (mode << ((pin_num & 0x7) * 4)); + /* set initial state of output register */ + port->BRR = (1 << pin_num); + if (mode == GPIO_IN_PU) { + port->BSRR = (1 << pin_num); } return 0; /* all OK */ } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int pin_num = _pin_num(pin); @@ -107,7 +101,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, /* disable interrupts on the channel we want to edit (just in case) */ EXTI->IMR &= ~(1 << pin_num); /* configure pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* set callback */ exti_ctx[pin_num].cb = cb; exti_ctx[pin_num].arg = arg; diff --git a/cpu/stm32f1/periph/i2c.c b/cpu/stm32f1/periph/i2c.c index 48702e9fe..7c1aef723 100644 --- a/cpu/stm32f1/periph/i2c.c +++ b/cpu/stm32f1/periph/i2c.c @@ -167,8 +167,8 @@ static void _pin_config(gpio_t scl, gpio_t sda) { /* toggle pins to reset analog filter -> see datasheet */ /* set as output */ - gpio_init(scl, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(sda, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(scl, GPIO_OUT); + gpio_init(sda, GPIO_OUT); /* run through toggling sequence */ gpio_set(scl); gpio_set(sda); diff --git a/cpu/stm32f1/periph/spi.c b/cpu/stm32f1/periph/spi.c index 9c8c09240..8075b9c91 100644 --- a/cpu/stm32f1/periph/spi.c +++ b/cpu/stm32f1/periph/spi.c @@ -161,7 +161,7 @@ int spi_conf_pins(spi_t dev) /* configure pins for alternate function input (MISO) or output (MOSI, CLK) */ gpio_init_af(clk, GPIO_AF_OUT_PP); gpio_init_af(mosi, GPIO_AF_OUT_PP); - gpio_init(miso, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(miso, GPIO_IN); return 0; } diff --git a/cpu/stm32f1/periph/uart.c b/cpu/stm32f1/periph/uart.c index bfe88789b..0e3cd765f 100644 --- a/cpu/stm32f1/periph/uart.c +++ b/cpu/stm32f1/periph/uart.c @@ -66,7 +66,7 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) isr_ctx[uart].arg = arg; /* configure RX and TX pin */ - gpio_init(uart_config[uart].rx_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(uart_config[uart].rx_pin, GPIO_IN); gpio_init_af(uart_config[uart].tx_pin, GPIO_AF_OUT_PP); /* enable the clock */ diff --git a/cpu/stm32f3/include/periph_cpu.h b/cpu/stm32f3/include/periph_cpu.h index bfb65319c..0c5a5e7bc 100644 --- a/cpu/stm32f3/include/periph_cpu.h +++ b/cpu/stm32f3/include/periph_cpu.h @@ -43,6 +43,31 @@ typedef uint32_t gpio_t; */ #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y) +/** + * @brief Generate GPIO mode bitfields + * + * We use 5 bit to encode the mode: + * - bit 0+1: pin mode (input / output) + * - bit 2+3: pull resistor configuration + * - bit 4: output type (0: push-pull, 1: open-drain) + */ +#define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4)) + +/** + * @brief Override GPIO mode options + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 0, 0), /**< input w/o pull R */ + GPIO_IN_PD = GPIO_MODE(0, 2, 0), /**< input with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 1, 0), /**< input with pull-up */ + GPIO_OUT = GPIO_MODE(1, 0, 0), /**< push-pull output */ + GPIO_OD = GPIO_MODE(1, 0, 1), /**< open-drain w/o pull R */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1) /**< open-drain with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Available ports on the STM32F3 family */ diff --git a/cpu/stm32f3/periph/gpio.c b/cpu/stm32f3/periph/gpio.c index 17cec769b..b13a7515c 100644 --- a/cpu/stm32f3/periph/gpio.c +++ b/cpu/stm32f3/periph/gpio.c @@ -24,9 +24,6 @@ #include "periph/gpio.h" #include "periph_conf.h" -#define ENABLE_DEBUG (0) -#include "debug.h" - /** * @brief The STM32F3 has 16 EXTI channels */ @@ -64,33 +61,31 @@ static inline int _pin_num(gpio_t pin) return (pin & 0x0f); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pushpull) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); - DEBUG("Init %i: port %i, pin %i\n", dir, pin_num, _port_num(pin)); - /* enable clock */ RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin)); - /* configure pull register */ - port->PUPDR &= ~(3 << (2 * pin_num)); - port->PUPDR |= (pushpull << (2 * pin_num)); - /* set direction */ - if (dir == GPIO_DIR_OUT) { - port->MODER &= ~(3 << (2 * pin_num)); /* set pin to output mode */ - port->MODER |= (1 << (2 * pin_num)); - port->OTYPER &= ~(1 << pin_num); /* set to push-pull */ - port->OSPEEDR |= (3 << (2 * pin_num)); /* set to high speed */ - port->ODR &= ~(1 << pin_num); /* set pin to low signal */ - } - else { - port->MODER &= ~(3 << (2 * pin_num)); /* configure pin as input */ - } + + /* set mode */ + port->MODER &= ~(0x3 << (2 * pin_num)); + port->MODER |= ((mode & 0x3) << (2 * pin_num)); + /* set pull resistor configuration */ + port->PUPDR &= ~(0x3 << (2 * pin_num)); + port->PUPDR |= (((mode >> 2) & 0x3) << (2 * pin_num)); + /* set output mode */ + port->OTYPER &= ~(1 << pin_num); + port->OTYPER |= (((mode >> 4) & 0x1) << pin_num); + /* reset speed value and clear pin */ + port->OSPEEDR |= (3 << (2 * pin_num)); + port->BSRRH = (1 << pin_num); + return 0; } -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int pin_num = _pin_num(pin); @@ -102,7 +97,7 @@ int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, /* enable the SYSCFG clock */ RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; /* configure pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* enable global pin interrupt */ if (pin_num < 5) { NVIC_EnableIRQ(EXTI0_IRQn + pin_num); diff --git a/cpu/stm32f4/include/periph_cpu.h b/cpu/stm32f4/include/periph_cpu.h index 575b7dc9f..2cb82d0d1 100644 --- a/cpu/stm32f4/include/periph_cpu.h +++ b/cpu/stm32f4/include/periph_cpu.h @@ -76,6 +76,31 @@ typedef enum { } adc_res_t; /** @} */ +/** + * @brief Generate GPIO mode bitfields + * + * We use 5 bit to encode the mode: + * - bit 0+1: pin mode (input / output) + * - bit 2+3: pull resistor configuration + * - bit 4: output type (0: push-pull, 1: open-drain) + */ +#define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4)) + +/** + * @brief Override GPIO mode options + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 0, 0), /**< input w/o pull R */ + GPIO_IN_PD = GPIO_MODE(0, 2, 0), /**< input with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 1, 0), /**< input with pull-up */ + GPIO_OUT = GPIO_MODE(1, 0, 0), /**< push-pull output */ + GPIO_OD = GPIO_MODE(1, 0, 1), /**< open-drain w/o pull R */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1) /**< open-drain with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Available ports on the STM32F4 family */ diff --git a/cpu/stm32f4/periph/gpio.c b/cpu/stm32f4/periph/gpio.c index 774a31acf..4ebb07d18 100644 --- a/cpu/stm32f4/periph/gpio.c +++ b/cpu/stm32f4/periph/gpio.c @@ -62,32 +62,31 @@ static inline int _pin_num(gpio_t pin) return (pin & 0x0f); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); /* enable clock */ RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN << _port_num(pin)); - /* configure pull register */ - port->PUPDR &= ~(3 << (2 * pin_num)); - port->PUPDR |= (pullup << (2 * pin_num)); - /* set direction */ - if (dir == GPIO_DIR_OUT) { - port->MODER &= ~(3 << (2 * pin_num)); /* set pin to output mode */ - port->MODER |= (1 << (2 * pin_num)); - port->OTYPER &= ~(1 << pin_num); /* set to push-pull */ - port->OSPEEDR |= (3 << (2 * pin_num)); /* set to high speed */ - port->ODR &= ~(1 << pin_num); /* set pin to low signal */ - } - else { - port->MODER &= ~(3 << (2 * pin_num)); /* configure pin as input */ - } + + /* set mode */ + port->MODER &= ~(0x3 << (2 * pin_num)); + port->MODER |= ((mode & 0x3) << (2 * pin_num)); + /* set pull resistor configuration */ + port->PUPDR &= ~(0x3 << (2 * pin_num)); + port->PUPDR |= (((mode >> 2) & 0x3) << (2 * pin_num)); + /* set output mode */ + port->OTYPER &= ~(1 << pin_num); + port->OTYPER |= (((mode >> 4) & 0x1) << pin_num); + /* reset speed value and clear pin */ + port->OSPEEDR |= (3 << (2 * pin_num)); + port->BSRRH = (1 << pin_num); + return 0; } -int gpio_init_int(gpio_t pin, - gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int pin_num = _pin_num(pin); @@ -99,7 +98,7 @@ int gpio_init_int(gpio_t pin, /* enable the SYSCFG clock */ RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; /* initialize pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* enable global pin interrupt */ if (pin_num < 5) { NVIC_EnableIRQ(EXTI0_IRQn + pin_num); diff --git a/cpu/stm32f4/periph/uart.c b/cpu/stm32f4/periph/uart.c index dda1f6011..51007f607 100644 --- a/cpu/stm32f4/periph/uart.c +++ b/cpu/stm32f4/periph/uart.c @@ -69,8 +69,8 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) mutex_lock(&_tx_dma_sync[uart]); /* configure pins */ - gpio_init(uart_config[uart].rx_pin, GPIO_DIR_IN, GPIO_NOPULL); - gpio_init(uart_config[uart].tx_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(uart_config[uart].rx_pin, GPIO_IN); + gpio_init(uart_config[uart].tx_pin, GPIO_OUT); gpio_init_af(uart_config[uart].rx_pin, uart_config[uart].af); gpio_init_af(uart_config[uart].tx_pin, uart_config[uart].af); /* enable UART clock */ diff --git a/cpu/stm32l1/include/periph_cpu.h b/cpu/stm32l1/include/periph_cpu.h index 35068642c..18399b038 100644 --- a/cpu/stm32l1/include/periph_cpu.h +++ b/cpu/stm32l1/include/periph_cpu.h @@ -45,6 +45,31 @@ typedef uint32_t gpio_t; */ #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y) +/** + * @brief Generate GPIO mode bitfields + * + * We use 5 bit to encode the mode: + * - bit 0+1: pin mode (input / output) + * - bit 2+3: pull resistor configuration + * - bit 4: output type (0: push-pull, 1: open-drain) + */ +#define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4)) + +/** + * @brief Override GPIO mode options + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN = GPIO_MODE(0, 0, 0), /**< input w/o pull R */ + GPIO_IN_PD = GPIO_MODE(0, 2, 0), /**< input with pull-down */ + GPIO_IN_PU = GPIO_MODE(0, 1, 0), /**< input with pull-up */ + GPIO_OUT = GPIO_MODE(1, 0, 0), /**< push-pull output */ + GPIO_OD = GPIO_MODE(1, 0, 1), /**< open-drain w/o pull R */ + GPIO_OD_PU = GPIO_MODE(1, 1, 1) /**< open-drain with pull-up */ +} gpio_mode_t; +/** @} */ + /** * @brief Available ports on the STM32L1 family */ @@ -106,6 +131,7 @@ typedef struct { I2C_TypeDef *dev; /**< i2c device */ gpio_t scl; /**< scl pin number */ gpio_t sda; /**< sda pin number */ + gpio_mode_t pin_mode; /**< with or without pull resistor */ gpio_af_t af; /**< I2C alternate function value */ uint8_t er_irqn; /**< error IRQ */ uint8_t ev_irqn; /**< event IRQ */ diff --git a/cpu/stm32l1/periph/gpio.c b/cpu/stm32l1/periph/gpio.c index bb80559f9..9de5baea7 100644 --- a/cpu/stm32l1/periph/gpio.c +++ b/cpu/stm32l1/periph/gpio.c @@ -63,32 +63,31 @@ static inline int _pin_num(gpio_t pin) return (pin & 0x0f); } -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup) +int gpio_init(gpio_t pin, gpio_mode_t mode) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); /* enable clock */ RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin)); - /* configure pull register */ - port->PUPDR &= ~(3 << (2 * pin_num)); - port->PUPDR |= (pullup << (2 * pin_num)); - /* set direction */ - if (dir == GPIO_DIR_OUT) { - port->MODER &= ~(3 << (2 * pin_num)); /* set pin to output mode */ - port->MODER |= (1 << (2 * pin_num)); - port->OTYPER &= ~(1 << pin_num); /* set to push-pull */ - port->OSPEEDR |= (3 << (2 * pin_num)); /* set to high speed */ - port->ODR &= ~(1 << pin_num); /* set pin to low signal */ - } - else { - port->MODER &= ~(3 << (2 * pin_num)); /* configure pin as input */ - } + + /* set mode */ + port->MODER &= ~(0x3 << (2 * pin_num)); + port->MODER |= ((mode & 0x3) << (2 * pin_num)); + /* set pull resistor configuration */ + port->PUPDR &= ~(0x3 << (2 * pin_num)); + port->PUPDR |= (((mode >> 2) & 0x3) << (2 * pin_num)); + /* set output mode */ + port->OTYPER &= ~(1 << pin_num); + port->OTYPER |= (((mode >> 4) & 0x1) << pin_num); + /* finally set pin speed to maximum and reset output */ + port->OSPEEDR |= (3 << (2 * pin_num)); + port->BRR = (1 << pin_num); + return 0; } -int gpio_init_int(gpio_t pin, - gpio_pp_t pullup, gpio_flank_t flank, +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg) { int pin_num = _pin_num(pin); @@ -100,7 +99,7 @@ int gpio_init_int(gpio_t pin, /* enable the SYSCFG clock */ RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; /* initialize pin as input */ - gpio_init(pin, GPIO_DIR_IN, pullup); + gpio_init(pin, mode); /* enable global pin interrupt */ if (pin_num < 5) { NVIC_EnableIRQ(EXTI0_IRQn + pin_num); diff --git a/cpu/stm32l1/periph/i2c.c b/cpu/stm32l1/periph/i2c.c index 3fad897d9..5d2d4f6d8 100644 --- a/cpu/stm32l1/periph/i2c.c +++ b/cpu/stm32l1/periph/i2c.c @@ -97,9 +97,9 @@ int i2c_init_master(i2c_t dev, i2c_speed_t speed) NVIC_EnableIRQ(i2c_config[dev].er_irqn); /* configure pins */ - gpio_init(i2c_config[dev].scl, GPIO_DIR_OUT, GPIO_PULLUP); + gpio_init(i2c_config[dev].scl, i2c_config[dev].pin_mode); gpio_init_af(i2c_config[dev].scl, i2c_config[dev].af); - gpio_init(i2c_config[dev].sda, GPIO_DIR_OUT, GPIO_PULLUP); + gpio_init(i2c_config[dev].sda, i2c_config[dev].pin_mode); gpio_init_af(i2c_config[dev].sda, i2c_config[dev].af); /* configure device */ diff --git a/cpu/stm32l1/periph/uart.c b/cpu/stm32l1/periph/uart.c index 22da881ef..056f261d8 100644 --- a/cpu/stm32l1/periph/uart.c +++ b/cpu/stm32l1/periph/uart.c @@ -114,9 +114,9 @@ static int init_base(uart_t uart, uint32_t baudrate) } /* uart_configure RX and TX pins, set pin to use alternative function mode */ - gpio_init(tx_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(tx_pin, GPIO_OUT); gpio_init_af(tx_pin, af); - gpio_init(rx_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(rx_pin, GPIO_IN); gpio_init_af(rx_pin, af); /* uart_configure UART to mode 8N1 with given baudrate */ diff --git a/drivers/adt7310/adt7310.c b/drivers/adt7310/adt7310.c index b5214313c..50c38da62 100644 --- a/drivers/adt7310/adt7310.c +++ b/drivers/adt7310/adt7310.c @@ -153,7 +153,7 @@ int adt7310_init(adt7310_t *dev, spi_t spi, gpio_t cs) dev->high_res = false; /* CS */ - gpio_init(dev->cs, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->cs, GPIO_OUT); gpio_set(dev->cs); #if ENABLE_DEBUG diff --git a/drivers/at86rf2xx/at86rf2xx.c b/drivers/at86rf2xx/at86rf2xx.c index 1327cbcd7..ec7ef6f13 100644 --- a/drivers/at86rf2xx/at86rf2xx.c +++ b/drivers/at86rf2xx/at86rf2xx.c @@ -63,13 +63,13 @@ int at86rf2xx_init(at86rf2xx_t *dev, spi_t spi, spi_speed_t spi_speed, /* initialise SPI */ spi_init_master(dev->spi, SPI_CONF_FIRST_RISING, spi_speed); /* initialise GPIOs */ - gpio_init(dev->cs_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->cs_pin, GPIO_OUT); gpio_set(dev->cs_pin); - gpio_init(dev->sleep_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->sleep_pin, GPIO_OUT); gpio_clear(dev->sleep_pin); - gpio_init(dev->reset_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->reset_pin, GPIO_OUT); gpio_set(dev->reset_pin); - gpio_init_int(dev->int_pin, GPIO_NOPULL, GPIO_RISING, _irq_handler, dev); + gpio_init_int(dev->int_pin, GPIO_IN, GPIO_RISING, _irq_handler, dev); /* make sure device is not sleeping, so we can query part number */ at86rf2xx_assert_awake(dev); diff --git a/drivers/cc110x/cc110x-netdev2.c b/drivers/cc110x/cc110x-netdev2.c index 2ced1624a..12531724a 100644 --- a/drivers/cc110x/cc110x-netdev2.c +++ b/drivers/cc110x/cc110x-netdev2.c @@ -199,7 +199,7 @@ static int _init(netdev2_t *dev) cc110x_t *cc110x = &((netdev2_cc110x_t*) dev)->cc110x; - gpio_init_int(cc110x->params.gdo2, GPIO_NOPULL, GPIO_BOTH, + gpio_init_int(cc110x->params.gdo2, GPIO_IN, GPIO_BOTH, &_netdev2_cc110x_isr, (void*)dev); gpio_set(cc110x->params.gdo2); diff --git a/drivers/cc110x/cc110x-spi.c b/drivers/cc110x/cc110x-spi.c index 6a11789a4..976fd4ab1 100644 --- a/drivers/cc110x/cc110x-spi.c +++ b/drivers/cc110x/cc110x-spi.c @@ -44,7 +44,7 @@ void cc110x_cs(cc110x_t *dev) volatile int retry_count = 0; /* Switch MISO/GDO1 to GPIO input mode */ #ifndef GPIO_READS_SPI_PINS - gpio_init(dev->params.gdo1, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(dev->params.gdo1, GPIO_IN); #endif /* CS to low */ gpio_clear(dev->params.cs); diff --git a/drivers/cc110x/cc110x.c b/drivers/cc110x/cc110x.c index 0f2c2aee5..557a09a04 100644 --- a/drivers/cc110x/cc110x.c +++ b/drivers/cc110x/cc110x.c @@ -55,11 +55,11 @@ int cc110x_setup(cc110x_t *dev, const cc110x_params_t *params) dev->params = *params; /* Configure chip-select */ - gpio_init(dev->params.cs, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->params.cs, GPIO_OUT); gpio_set(dev->params.cs); /* Configure GDO1 */ - gpio_init(dev->params.gdo1, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(dev->params.gdo1, GPIO_IN); /* Configure SPI */ spi_acquire(dev->params.spi); diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index f4d078f5e..002a7a9df 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -80,7 +80,7 @@ int dht_init(dht_t *dev, const dht_params_t *params) memcpy(dev, params, sizeof(dht_t)); - if (gpio_init(dev->pin, GPIO_DIR_OUT, dev->pull) == -1) { + if (gpio_init(dev->pin, GPIO_OUT) == -1) { return -1; } gpio_set(dev->pin); @@ -103,7 +103,7 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum) xtimer_usleep(40); /* sync on device */ - gpio_init(dev->pin, GPIO_DIR_IN, dev->pull); + gpio_init(dev->pin, dev->in_mode); while (!gpio_read(dev->pin)) ; while (gpio_read(dev->pin)) ; @@ -120,7 +120,7 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum) /* set pin high again - so we can trigger the next reading by pulling it low * again */ - gpio_init(dev->pin, GPIO_DIR_OUT, dev->pull); + gpio_init(dev->pin, GPIO_OUT); gpio_set(dev->pin); /* validate the checksum */ diff --git a/drivers/dht/include/dht_params.h b/drivers/dht/include/dht_params.h index bed1e094f..0d68ccc51 100644 --- a/drivers/dht/include/dht_params.h +++ b/drivers/dht/include/dht_params.h @@ -36,12 +36,12 @@ extern "C" { #define DHT_PARAM_TYPE (DHT11) #endif #ifndef DHT_PARAM_PULL -#define DHT_PARAM_PULL (GPIO_PULLUP) +#define DHT_PARAM_PULL (GPIO_IN_PU) #endif -#define DHT_PARAMS_DEFAULT {.pin = DHT_PARAM_PIN, \ - .type = DHT_PARAM_TYPE, \ - .pull = DHT_PARAM_PULL} +#define DHT_PARAMS_DEFAULT {.pin = DHT_PARAM_PIN, \ + .type = DHT_PARAM_TYPE, \ + .in_mode = DHT_PARAM_PULL} /**@}*/ /** diff --git a/drivers/enc28j60/enc28j60.c b/drivers/enc28j60/enc28j60.c index cc6d6634b..5208bdf14 100644 --- a/drivers/enc28j60/enc28j60.c +++ b/drivers/enc28j60/enc28j60.c @@ -288,11 +288,11 @@ static int nd_init(netdev2_t *netdev) mutex_lock(&dev->devlock); /* setup the low-level interfaces */ - gpio_init(dev->reset_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->reset_pin, GPIO_OUT); gpio_clear(dev->reset_pin); /* this puts the device into reset state */ - gpio_init(dev->cs_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->cs_pin, GPIO_OUT); gpio_set(dev->cs_pin); - gpio_init_int(dev->int_pin, GPIO_NOPULL, GPIO_FALLING, on_int, (void *)dev); + gpio_init_int(dev->int_pin, GPIO_IN, GPIO_FALLING, on_int, (void *)dev); res = spi_init_master(dev->spi, SPI_CONF_FIRST_RISING, SPI_SPEED); if (res < 0) { DEBUG("[enc28j60] init: error initializing SPI bus [%i]\n", res); diff --git a/drivers/encx24j600/encx24j600.c b/drivers/encx24j600/encx24j600.c index 8323137b9..6959129d2 100644 --- a/drivers/encx24j600/encx24j600.c +++ b/drivers/encx24j600/encx24j600.c @@ -250,9 +250,9 @@ static int _init(netdev2_t *encdev) DEBUG("encx24j600: starting initialization...\n"); /* setup IO */ - gpio_init(dev->cs, GPIO_DIR_OUT, GPIO_PULLUP); + gpio_init(dev->cs, GPIO_OUT); gpio_set(dev->cs); - gpio_init_int(dev->int_pin, GPIO_PULLUP, GPIO_FALLING, encx24j600_isr, (void*)dev); + gpio_init_int(dev->int_pin, GPIO_IN_PU, GPIO_FALLING, encx24j600_isr, (void*)dev); if (spi_init_master(dev->spi, SPI_CONF_FIRST_RISING, ENCX24J600_SPI_SPEED) < 0) { return -1; diff --git a/drivers/include/dht.h b/drivers/include/dht.h index e1eabf6de..054590b7e 100644 --- a/drivers/include/dht.h +++ b/drivers/include/dht.h @@ -59,8 +59,8 @@ typedef enum { typedef struct { gpio_t pin; /**< GPIO pin of the device's data pin */ dht_type_t type; /**< type of the DHT device */ - gpio_pp_t pull; /**< internal pull resistor configuration, set to - * GPIO_NOPULL when using an external pull-up */ + gpio_mode_t in_mode; /**< input pin configuration, with or without pull + * resistor */ } dht_t; /** diff --git a/drivers/include/periph/gpio.h b/drivers/include/periph/gpio.h index c91dda1f6..eb7f0efa8 100644 --- a/drivers/include/periph/gpio.h +++ b/drivers/include/periph/gpio.h @@ -86,24 +86,24 @@ typedef unsigned int gpio_t; #endif /** - * @brief Definition of available pin directions - */ -#ifndef HAVE_GPIO_DIR_T -typedef enum { - GPIO_DIR_IN = 0, /**< configure pin as input */ - GPIO_DIR_OUT = 1 /**< configure pin as output */ -} gpio_dir_t; -#endif - -/** - * @brief Definition of pull-up/pull-down modes + * @brief Available pin modes + * + * Generally, a pin can be configured to be input or output. In output mode, a + * pin can further be put into push-pull or open drain configuration. Though + * this is supported by most platforms, this is not always the case, so driver + * implementations may return an error code if a mode is not supported. */ -#ifndef HAVE_GPIO_PP_T +#ifndef HAVE_GPIO_MODE_T typedef enum { - GPIO_NOPULL = 0, /**< do not use internal pull resistors */ - GPIO_PULLUP = 1, /**< enable internal pull-up resistor */ - GPIO_PULLDOWN = 2 /**< enable internal pull-down resistor */ -} gpio_pp_t; + GPIO_IN , /**< configure as input without pull resistor */ + GPIO_IN_PD, /**< configure as input with pull-down resistor */ + GPIO_IN_PU, /**< configure as input with pull-up resistor */ + GPIO_OUT, /**< configure as output in push-pull mode */ + GPIO_OD, /**< configure as output in open-drain mode without + * pull resistor */ + GPIO_OD_PU /**< configure as output in open-drain mode with + * pull resistor enabled */ +} gpio_mode_t; #endif /** @@ -146,7 +146,7 @@ typedef struct { * @return 0 on success * @return -1 on error */ -int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup); +int gpio_init(gpio_t pin, gpio_mode_t mode); /** * @brief Initialize a GPIO pin for external interrupt usage @@ -165,8 +165,8 @@ int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup); * @return 0 on success * @return -1 on error */ -int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank, - gpio_cb_t cb, void *arg); +int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, + gpio_cb_t cb, void *arg); /** * @brief Enable pin's interrupt if configured as interrupt source diff --git a/drivers/include/saul/periph.h b/drivers/include/saul/periph.h index 98e47dd38..4f49cdf83 100644 --- a/drivers/include/saul/periph.h +++ b/drivers/include/saul/periph.h @@ -31,8 +31,7 @@ extern "C" { typedef struct { const char *name; /**< name of the device connected to this pin */ gpio_t pin; /**< GPIO pin to initialize and expose */ - gpio_dir_t dir; /**< use GPIO as input or output */ - gpio_pp_t pull; /**< define the pull-up/pull-down mode */ + gpio_mode_t mode; /**< pin mode to use */ } saul_gpio_params_t; #ifdef __cplusplus diff --git a/drivers/kw2xrf/kw2xrf.c b/drivers/kw2xrf/kw2xrf.c index 36cd9505d..4a3eb1dcb 100644 --- a/drivers/kw2xrf/kw2xrf.c +++ b/drivers/kw2xrf/kw2xrf.c @@ -304,7 +304,7 @@ void kw2xrf_init_interrupts(kw2xrf_t *dev, gpio_t int_pin) kw2xrf_write_dreg(MKW2XDM_PHY_CTRL3, reg); /* set up GPIO-pin used for IRQ */ - gpio_init_int(int_pin, GPIO_NOPULL, GPIO_FALLING, &kw2xrf_irq_handler, dev); + gpio_init_int(int_pin, GPIO_IN, GPIO_FALLING, &kw2xrf_irq_handler, dev); } int kw2xrf_set_pan(kw2xrf_t *dev, uint16_t pan) diff --git a/drivers/kw2xrf/kw2xrf_spi.c b/drivers/kw2xrf/kw2xrf_spi.c index 27250833c..8a9bc9e4f 100644 --- a/drivers/kw2xrf/kw2xrf_spi.c +++ b/drivers/kw2xrf/kw2xrf_spi.c @@ -64,7 +64,7 @@ int kw2xrf_spi_init(spi_t spi, spi_speed_t spi_speed, res = spi_init_master(kw2xrf_spi, SPI_CONF_FIRST_RISING, spi_speed); #if KW2XRF_SHARED_SPI spi_release(kw2xrf_spi); - gpio_init(kw2xrf_cs_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(kw2xrf_cs_pin, GPIO_OUT); gpio_set(kw2xrf_cs_pin); #endif diff --git a/drivers/lis3dh/lis3dh.c b/drivers/lis3dh/lis3dh.c index 674d5218e..fae0064e0 100644 --- a/drivers/lis3dh/lis3dh.c +++ b/drivers/lis3dh/lis3dh.c @@ -39,7 +39,7 @@ int lis3dh_init(lis3dh_t *dev, spi_t spi, gpio_t cs_pin, uint8_t scale) dev->scale = 0; /* CS */ - gpio_init(dev->cs, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->cs, GPIO_OUT); gpio_set(dev->cs); if (lis3dh_read_regs(dev, LIS3DH_REG_WHO_AM_I, 1, &in) < 0) { diff --git a/drivers/lsm303dlhc/lsm303dlhc.c b/drivers/lsm303dlhc/lsm303dlhc.c index e79a8a79d..f2084c861 100644 --- a/drivers/lsm303dlhc/lsm303dlhc.c +++ b/drivers/lsm303dlhc/lsm303dlhc.c @@ -72,7 +72,7 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_NONE); /* configure acc data ready pin */ - gpio_init(acc_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(acc_pin, GPIO_IN); /* configure magnetometer and temperature */ /* enable temperature output and set sample rate */ @@ -87,7 +87,7 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS); i2c_release(dev->i2c); /* configure mag data ready pin */ - gpio_init(mag_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(mag_pin, GPIO_IN); return (res < 7) ? -1 : 0; } @@ -217,7 +217,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev) tmp = (LSM303DLHC_CTRL4_A_BDU| LSM303DLHC_CTRL4_A_SCALE_2G | LSM303DLHC_CTRL4_A_HR); res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL4_A, tmp); res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_DRDY1); - gpio_init(dev->acc_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(dev->acc_pin, GPIO_IN); tmp = LSM303DLHC_TEMP_EN | LSM303DLHC_TEMP_SAMPLE_75HZ; res += i2c_write_reg(dev->i2c, dev->mag_address, LSM303DLHC_REG_CRA_M, tmp); @@ -229,7 +229,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev) LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS); i2c_release(dev->i2c); - gpio_init(dev->mag_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init(dev->mag_pin, GPIO_IN); return (res < 6) ? -1 : 0; } diff --git a/drivers/nrf24l01p/nrf24l01p.c b/drivers/nrf24l01p/nrf24l01p.c index c4d8ce8c8..3ca880bf8 100644 --- a/drivers/nrf24l01p/nrf24l01p.c +++ b/drivers/nrf24l01p/nrf24l01p.c @@ -81,14 +81,14 @@ int nrf24l01p_init(nrf24l01p_t *dev, spi_t spi, gpio_t ce, gpio_t cs, gpio_t irq dev->listener = KERNEL_PID_UNDEF; /* Init CE pin */ - gpio_init(dev->ce, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->ce, GPIO_OUT); /* Init CS pin */ - gpio_init(dev->cs, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(dev->cs, GPIO_OUT); gpio_set(dev->cs); /* Init IRQ pin */ - gpio_init_int(dev->irq, GPIO_PULLUP, GPIO_FALLING, nrf24l01p_rx_cb, dev); + gpio_init_int(dev->irq, GPIO_IN_PU, GPIO_FALLING, nrf24l01p_rx_cb, dev); /* Init SPI */ diff --git a/drivers/nvram_spi/nvram-spi.c b/drivers/nvram_spi/nvram-spi.c index 319f6e0bc..a544afdd7 100644 --- a/drivers/nvram_spi/nvram-spi.c +++ b/drivers/nvram_spi/nvram-spi.c @@ -114,7 +114,7 @@ int nvram_spi_init(nvram_t *dev, nvram_spi_params_t *spi_params, size_t size) } dev->extra = spi_params; - gpio_init(spi_params->cs, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(spi_params->cs, GPIO_OUT); gpio_set(spi_params->cs); return 0; diff --git a/drivers/pcd8544/pcd8544.c b/drivers/pcd8544/pcd8544.c index 65f7ef97f..208af5660 100644 --- a/drivers/pcd8544/pcd8544.c +++ b/drivers/pcd8544/pcd8544.c @@ -231,9 +231,9 @@ int pcd8544_init(pcd8544_t *dev, spi_t spi, gpio_t cs, gpio_t reset, gpio_t mode DEBUG("done setting dev members\n"); /* initialze pins */ - gpio_init(cs, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(reset, GPIO_DIR_OUT, GPIO_NOPULL); - gpio_init(mode, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init(cs, GPIO_OUT); + gpio_init(reset, GPIO_OUT); + gpio_init(mode, GPIO_OUT); DEBUG("done with gpios\n"); /* clear CS line */ gpio_set(cs); diff --git a/drivers/pir/pir.c b/drivers/pir/pir.c index c76f3a52d..635bec41d 100644 --- a/drivers/pir/pir.c +++ b/drivers/pir/pir.c @@ -41,7 +41,7 @@ int pir_init(pir_t *dev, gpio_t gpio) { dev->gpio_dev = gpio; dev->msg_thread_pid = KERNEL_PID_UNDEF; - return gpio_init(dev->gpio_dev, GPIO_DIR_IN, GPIO_NOPULL); + return gpio_init(dev->gpio_dev, GPIO_IN); } pir_event_t pir_get_status(pir_t *dev) @@ -106,5 +106,5 @@ static void pir_callback(void *arg) static int pir_activate_int(pir_t *dev) { - return gpio_init_int(dev->gpio_dev, GPIO_NOPULL, GPIO_BOTH, pir_callback, dev); + return gpio_init_int(dev->gpio_dev, GPIO_IN, GPIO_BOTH, pir_callback, dev); } diff --git a/drivers/xbee/xbee.c b/drivers/xbee/xbee.c index ef20a4d87..234413a13 100644 --- a/drivers/xbee/xbee.c +++ b/drivers/xbee/xbee.c @@ -428,14 +428,14 @@ int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate, return -ENXIO; } if (reset_pin != GPIO_UNDEF) { - if (gpio_init(reset_pin, GPIO_DIR_OUT, GPIO_NOPULL) < 0) { + if (gpio_init(reset_pin, GPIO_OUT) < 0) { DEBUG("xbee: Error initializing RESET pin\n"); return -ENXIO; } gpio_set(reset_pin); } if (sleep_pin != GPIO_UNDEF) { - if (gpio_init(sleep_pin, GPIO_DIR_OUT, GPIO_NOPULL) < 0) { + if (gpio_init(sleep_pin, GPIO_OUT) < 0) { DEBUG("xbee: Error initializing SLEEP pin\n"); return -ENXIO; } diff --git a/sys/arduino/base.cpp b/sys/arduino/base.cpp index d3c06be1a..6c9ea49f8 100644 --- a/sys/arduino/base.cpp +++ b/sys/arduino/base.cpp @@ -25,19 +25,18 @@ extern "C" { #include "arduino.hpp" -static inline gpio_dir_t _dir(int mode) +void pinMode(int pin, int mode) { - return (mode == OUTPUT) ? GPIO_DIR_OUT : GPIO_DIR_IN; -} + gpio_mode_t m = GPIO_OUT; -static inline gpio_pp_t _pr(int mode) -{ - return (mode == INPUT_PULLUP) ? GPIO_PULLUP : GPIO_NOPULL; -} + if (mode == INPUT) { + m = GPIO_IN; + } + else if (mode == INPUT_PULLUP) { + m = GPIO_IN_PU; + } -void pinMode(int pin, int mode) -{ - gpio_init(arduino_pinmap[pin], _dir(mode), _pr(mode)); + gpio_init(arduino_pinmap[pin], m); } void digitalWrite(int pin, int state) diff --git a/sys/auto_init/saul/auto_init_gpio.c b/sys/auto_init/saul/auto_init_gpio.c index f72723ec7..454c1d0b8 100644 --- a/sys/auto_init/saul/auto_init_gpio.c +++ b/sys/auto_init/saul/auto_init_gpio.c @@ -62,7 +62,7 @@ void auto_init_gpio(void) saul_reg_entries[i].name = p->name; saul_reg_entries[i].driver = &gpio_saul_driver; /* initialize the GPIO pin */ - gpio_init(p->pin, p->dir, p->pull); + gpio_init(p->pin, p->mode); /* add to registry */ saul_reg_add(&(saul_reg_entries[i])); } diff --git a/tests/driver_lis3dh/main.c b/tests/driver_lis3dh/main.c index 3c7ec592e..2fea3559c 100644 --- a/tests/driver_lis3dh/main.c +++ b/tests/driver_lis3dh/main.c @@ -152,7 +152,7 @@ int main(void) } puts("Set INT1 callback"); - if (gpio_init_int(TEST_LIS3DH_INT1, GPIO_NOPULL, GPIO_RISING, test_int1, (void*)&int1_count) == 0) { + if (gpio_init_int(TEST_LIS3DH_INT1, GPIO_IN, GPIO_RISING, test_int1, (void*)&int1_count) == 0) { puts("[OK]"); } else { diff --git a/tests/driver_si70xx/main.c b/tests/driver_si70xx/main.c index b954cd871..447d2cb01 100644 --- a/tests/driver_si70xx/main.c +++ b/tests/driver_si70xx/main.c @@ -48,7 +48,7 @@ int main(void) if (TEST_PIN_EN != GPIO_UNDEF) { printf("Toggling enable pin..."); - if (gpio_init(TEST_PIN_EN, GPIO_DIR_OUT, GPIO_NOPULL) == 0) { + if (gpio_init(TEST_PIN_EN, GPIO_OUT) == 0) { puts("[OK]\n"); } else { diff --git a/tests/periph_gpio/main.c b/tests/periph_gpio/main.c index 286605855..6f2672366 100644 --- a/tests/periph_gpio/main.c +++ b/tests/periph_gpio/main.c @@ -29,101 +29,81 @@ static void cb(void *arg) printf("INT: external interrupt from pin %i\n", (int)arg); } -static int parse_pull(char *val) +static int init_pin(int argc, char **argv, gpio_mode_t mode) { - switch (atoi(val)) { - case 0: - return GPIO_NOPULL; - case 1: - return GPIO_PULLUP; - case 2: - return GPIO_PULLDOWN; - default: - puts("Error: invalid pull configuration"); - return -1; - } -} - -static int init_out(int argc, char **argv) -{ - int port, pin, pull; + int po, pi; if (argc < 3) { - printf("usage: %s [pull_config]\n", argv[0]); - puts(" pull_config: 0: no pull resistor (default)\n" - " 1: pull up\n" - " 2: pull down"); + printf("usage: %s \n", argv[0]); return 1; } - port = atoi(argv[1]); - pin = atoi(argv[2]); - if (argc >= 4) { - pull = parse_pull(argv[3]); - if (pull < 0) { - return 1; - } - } - else { - pull = GPIO_NOPULL; - } - if (gpio_init(GPIO_PIN(port, pin), GPIO_DIR_OUT, pull) < 0) { - printf("Error while initializing PORT_%i.%i as output\n", port, pin); + + po = atoi(argv[1]); + pi = atoi(argv[2]); + + if (gpio_init(GPIO_PIN(po, pi), mode) < 0) { + printf("Error to initialize GPIO_PIN(%i, %02i)\n", po, pi); return 1; } - printf("PORT_%i.%i initialized successful as output\n", port, pin); return 0; } +static int init_out(int argc, char **argv) +{ + return init_pin(argc, argv, GPIO_OUT); +} + static int init_in(int argc, char **argv) { - int port, pin, pull; + return init_pin(argc, argv, GPIO_IN); +} - if (argc < 3) { - printf("usage: %s [pull_config]\n", argv[0]); - puts(" pull_config: 0: no pull resistor (default)\n" - " 1: pull up\n" - " 2: pull down"); - return 1; - } - port = atoi(argv[1]); - pin = atoi(argv[2]); - if (argc >= 4) { - pull = parse_pull(argv[3]); - if (pull < 0) { - return 1; - } - } - else { - pull = GPIO_NOPULL; - } - if (gpio_init(GPIO_PIN(port, pin), GPIO_DIR_IN, pull) < 0) { - printf("Error while initializing PORT_%i.%02i as input\n", port, pin); - return 1; - } - printf("PORT_%i.%02i initialized successful as input\n", port, pin); +static int init_in_pu(int argc, char **argv) +{ + return init_pin(argc, argv, GPIO_IN_PU); +} - return 0; +static int init_in_pd(int argc, char **argv) +{ + return init_pin(argc, argv, GPIO_IN_PD); +} + +static int init_od(int argc, char **argv) +{ + return init_pin(argc, argv, GPIO_OD); +} + +static int init_od_pu(int argc, char **argv) +{ + return init_pin(argc, argv, GPIO_OD_PU); } static int init_int(int argc, char **argv) { - int port, pin, flank, pull; + int po, pi; + gpio_mode_t mode = GPIO_IN; + gpio_flank_t flank; + int fl, pr; if (argc < 4) { printf("usage: %s [pull_config]\n", argv[0]); - puts(" flank: 0: falling\n" - " 1: rising\n" - " 2: both\n" - " pull_config: 0: no pull resistor (default)\n" - " 1: pull up\n" - " 2: pull down"); + puts("\tflank:\n" + "\t0: falling\n" + "\t1: rising\n" + "\t2: both\n" + "\tpull_config:\n" + "\t0: no pull resistor (default)\n" + "\t1: pull up\n" + "\t2: pull down"); return 1; } - port = atoi(argv[1]); - pin = atoi(argv[2]); - flank = atoi(argv[3]); - switch (flank) { + + po = atoi(argv[1]); + pi = atoi(argv[2]); + + fl = atoi(argv[3]); + switch (fl) { case 0: flank = GPIO_FALLING; break; @@ -134,25 +114,33 @@ static int init_int(int argc, char **argv) flank = GPIO_BOTH; break; default: - printf("wrong flank setting.\n"); - return 0; + puts("error: invalid value for active flank"); + return 1; } + if (argc >= 5) { - pull = parse_pull(argv[4]); - if (pull < 0) { - return 1; + pr = atoi(argv[4]); + switch (pr) { + case 0: + mode = GPIO_IN; + break; + case 1: + mode = GPIO_IN_PU; + break; + case 2: + mode = GPIO_IN_PD; + break; + default: + puts("error: invalid pull resistor option"); + return 1; } } - else { - pull = GPIO_NOPULL; - } - if (gpio_init_int(GPIO_PIN(port, pin), pull, flank, cb, (void *)pin) < 0) { - printf("Error while initializing PORT_%i.%02i as external interrupt\n", - port, pin); + + if (gpio_init_int(GPIO_PIN(po, pi), mode, flank, cb, (void *)pi) < 0) { + printf("error: init_int of GPIO_PIN(%i, %i) failed\n", po, pi); return 1; } - printf("PORT_%i.%02i initialized successful as external interrupt\n", - port, pin); + printf("GPIO_PIN(%i, %i) successfully initialized as ext int\n", po, pi); return 0; } @@ -165,64 +153,64 @@ static int read(int argc, char **argv) printf("usage: %s \n", argv[0]); return 1; } + port = atoi(argv[1]); pin = atoi(argv[2]); + if (gpio_read(GPIO_PIN(port, pin))) { - printf("PORT_%i.%02i is HIGH\n", port, pin); + printf("GPIO_PIN(%i.%02i) is HIGH\n", port, pin); } else { - printf("PORT_%i.%02i is LOW\n", port, pin); + printf("GPIO_PIN(%i.%02i) is LOW\n", port, pin); } + return 0; } static int set(int argc, char **argv) { - int port, pin; - if (argc < 3) { printf("usage: %s \n", argv[0]); return 1; } - port = atoi(argv[1]); - pin = atoi(argv[2]); - gpio_set(GPIO_PIN(port, pin)); + gpio_set(GPIO_PIN(atoi(argv[1]), atoi(argv[2]))); + return 0; } static int clear(int argc, char **argv) { - int port, pin; - if (argc < 3) { printf("usage: %s \n", argv[0]); return 1; } - port = atoi(argv[1]); - pin = atoi(argv[2]); - gpio_clear(GPIO_PIN(port, pin)); + + gpio_clear(GPIO_PIN(atoi(argv[1]), atoi(argv[2]))); + return 0; } static int toggle(int argc, char **argv) { - int port, pin; - if (argc < 3) { printf("usage: %s \n", argv[0]); return 1; } - port = atoi(argv[1]); - pin = atoi(argv[2]); - gpio_toggle(GPIO_PIN(port, pin)); + + gpio_toggle(GPIO_PIN(atoi(argv[1]), atoi(argv[2]))); + return 0; } static const shell_command_t shell_commands[] = { - { "init_in", "initialize pin as input", init_in }, - { "init_out", "initialize pin as output", init_out }, - { "init_int", "initialize pin as input with interrupt", init_int }, + { "init_out", "init as output (push-pull mode)", init_out }, + { "init_in", "init as input w/o pull resistor", init_in }, + { "init_in_pu", "init as input with pull-up", init_in_pu }, + { "init_in_pd", "init as input with pull-down", init_in_pd }, + { "init_od", "init as output (open-drain without pull resistor)", init_od }, + { "init_od_pu", "init as output (open-drain with pull-up)", init_od_pu }, + { "init_int", "init as external INT w/o pull resistor", init_int }, { "read", "read pin status", read }, { "set", "set pin to HIGH", set }, { "clear", "set pin to LOW", clear }, diff --git a/tests/periph_spi/main.c b/tests/periph_spi/main.c index e6e038b8d..93b76eae1 100644 --- a/tests/periph_spi/main.c +++ b/tests/periph_spi/main.c @@ -182,7 +182,7 @@ int cmd_init_master(int argc, char **argv) spi_dev, res); return 1; } - res = gpio_init(spi_cs, GPIO_DIR_OUT, GPIO_PULLUP); + res = gpio_init(spi_cs, GPIO_OUT); if (res < 0){ printf("gpio_init: error initializing GPIO_%ld as CS line (code %i)\n", (long)spi_cs, res); @@ -210,7 +210,7 @@ int cmd_init_slave(int argc, char **argv) spi_dev, res); return 1; } - res = gpio_init_int(spi_cs, GPIO_NOPULL, GPIO_FALLING, slave_on_cs, 0); + res = gpio_init_int(spi_cs, GPIO_IN, GPIO_FALLING, slave_on_cs, 0); if (res < 0){ printf("gpio_init_int: error initializing GPIO_%ld as CS line (code %i)\n", (long)spi_cs, res);