From c2c2cc859290886e36ebed70c7ce4d5056f980fe Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 22 Oct 2024 21:40:30 +0200 Subject: [PATCH 1/3] drivers/periph_gpio: let gpio_read() return bool Since https://github.com/RIOT-OS/RIOT/pull/20935 gpio_write() uses a `bool` instead of an `int`. This does the same treatment for `gpio_read()`. This does indeed add an instruction to `gpio_read()` implementations. However, users caring about an instruction more are better served with `gpio_ll_read()` anyway. And `gpio_read() == 1` is often seen in newcomer's code, which would now work as expected. --- cpu/atmega_common/periph/gpio.c | 2 +- cpu/atxmega/periph/gpio.c | 2 +- cpu/cc2538/periph/gpio.c | 2 +- cpu/cc26xx_cc13xx/periph/gpio.c | 2 +- cpu/efm32/periph/gpio.c | 2 +- cpu/esp32/periph/gpio.c | 4 ++-- cpu/esp8266/periph/gpio.c | 2 +- cpu/fe310/periph/gpio.c | 2 +- cpu/gd32v/periph/gpio.c | 2 +- cpu/kinetis/periph/gpio.c | 2 +- cpu/lm4f120/periph/gpio.c | 2 +- cpu/lpc1768/periph/gpio.c | 2 +- cpu/lpc23xx/periph/gpio.c | 2 +- cpu/msp430/periph/gpio.c | 2 +- cpu/native/periph/gpio_linux.c | 2 +- cpu/native/periph/gpio_mock.c | 2 +- cpu/nrf5x_common/periph/gpio.c | 2 +- cpu/qn908x/periph/gpio.c | 2 +- cpu/rpx0xx/periph/gpio.c | 2 +- cpu/sam0_common/periph/gpio.c | 2 +- cpu/sam3/periph/gpio.c | 2 +- cpu/stm32/periph/gpio_all.c | 2 +- cpu/stm32/periph/gpio_f1.c | 2 +- drivers/include/periph/gpio.h | 6 +++--- 24 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cpu/atmega_common/periph/gpio.c b/cpu/atmega_common/periph/gpio.c index b59a1f703e..28e050d69f 100644 --- a/cpu/atmega_common/periph/gpio.c +++ b/cpu/atmega_common/periph/gpio.c @@ -154,7 +154,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return (_SFR_MEM8(atmega_pin_addr(pin)) & (1 << atmega_pin_num(pin))); } diff --git a/cpu/atxmega/periph/gpio.c b/cpu/atxmega/periph/gpio.c index 8f67990e05..dd6914ec07 100644 --- a/cpu/atxmega/periph/gpio.c +++ b/cpu/atxmega/periph/gpio.c @@ -252,7 +252,7 @@ void gpio_irq_disable(gpio_t pin) } } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { PORT_t *port = _port_addr(pin); uint8_t pin_mask = _pin_mask(pin); diff --git a/cpu/cc2538/periph/gpio.c b/cpu/cc2538/periph/gpio.c index 861810d4f9..82bf84e416 100644 --- a/cpu/cc2538/periph/gpio.c +++ b/cpu/cc2538/periph/gpio.c @@ -122,7 +122,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return (int)(gpio(pin)->DATA & _pin_mask(pin)); } diff --git a/cpu/cc26xx_cc13xx/periph/gpio.c b/cpu/cc26xx_cc13xx/periph/gpio.c index 82847bb194..17ad877d20 100644 --- a/cpu/cc26xx_cc13xx/periph/gpio.c +++ b/cpu/cc26xx_cc13xx/periph/gpio.c @@ -56,7 +56,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { if (GPIO->DOE & (1 << pin)) { return (GPIO->DOUT >> pin) & 1; diff --git a/cpu/efm32/periph/gpio.c b/cpu/efm32/periph/gpio.c index 589c7adf3e..0d37f1c8ae 100644 --- a/cpu/efm32/periph/gpio.c +++ b/cpu/efm32/periph/gpio.c @@ -73,7 +73,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return GPIO_PinInGet(_port_num(pin), _pin_num(pin)); } diff --git a/cpu/esp32/periph/gpio.c b/cpu/esp32/periph/gpio.c index 9050f011f1..137a7402a1 100644 --- a/cpu/esp32/periph/gpio.c +++ b/cpu/esp32/periph/gpio.c @@ -338,7 +338,7 @@ static gpio_hal_context_t _gpio_hal_ctx = { */ static BITFIELD(_output, GPIO_PIN_NUMOF); -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { assert(pin < GPIO_PIN_NUMOF); @@ -394,7 +394,7 @@ void gpio_toggle(gpio_t pin) #else /* IS_USED(MODULE_ESP_IDF_GPIO_HAL) */ -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { assert(pin < GPIO_PIN_NUMOF); diff --git a/cpu/esp8266/periph/gpio.c b/cpu/esp8266/periph/gpio.c index d94f3ab8d7..414516e693 100644 --- a/cpu/esp8266/periph/gpio.c +++ b/cpu/esp8266/periph/gpio.c @@ -237,7 +237,7 @@ void gpio_irq_disable (gpio_t pin) } #endif /* MODULE_PERIPH_GPIO_IRQ */ -int gpio_read (gpio_t pin) +bool gpio_read (gpio_t pin) { CHECK_PARAM_RET(pin < GPIO_PIN_NUMOF, -1); diff --git a/cpu/fe310/periph/gpio.c b/cpu/fe310/periph/gpio.c index c923b8e3e1..c7db92d517 100644 --- a/cpu/fe310/periph/gpio.c +++ b/cpu/fe310/periph/gpio.c @@ -90,7 +90,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return (GPIO_REG(GPIO_INPUT_VAL) & (1 << pin)) ? 1 : 0; } diff --git a/cpu/gd32v/periph/gpio.c b/cpu/gd32v/periph/gpio.c index f15ed83669..7fbfcbde4d 100644 --- a/cpu/gd32v/periph/gpio.c +++ b/cpu/gd32v/periph/gpio.c @@ -160,7 +160,7 @@ void gpio_init_analog(gpio_t pin) *pin_reg &= ~(0xfl << (4 * (pin_num - ((pin_num >= 8) * 8)))); } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { GPIO_Type *port = _port(pin); unsigned pin_num = _pin_num(pin); diff --git a/cpu/kinetis/periph/gpio.c b/cpu/kinetis/periph/gpio.c index df62aa6f89..ff918b6250 100644 --- a/cpu/kinetis/periph/gpio.c +++ b/cpu/kinetis/periph/gpio.c @@ -247,7 +247,7 @@ void gpio_init_port(gpio_t pin, uint32_t pcr) #endif /* KINETIS_HAVE_PCR */ } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { if (gpio(pin)->PDDR & (1 << pin_num(pin))) { return (gpio(pin)->PDOR & (1 << pin_num(pin))) ? 1 : 0; diff --git a/cpu/lm4f120/periph/gpio.c b/cpu/lm4f120/periph/gpio.c index 7d158616a7..a03a97eb0a 100644 --- a/cpu/lm4f120/periph/gpio.c +++ b/cpu/lm4f120/periph/gpio.c @@ -119,7 +119,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { const uint8_t port_num = _port_num(pin); const uint32_t port_addr = _port_base[port_num]; diff --git a/cpu/lpc1768/periph/gpio.c b/cpu/lpc1768/periph/gpio.c index 75b3217c50..668673f222 100644 --- a/cpu/lpc1768/periph/gpio.c +++ b/cpu/lpc1768/periph/gpio.c @@ -92,7 +92,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { LPC_GPIO_TypeDef *base = _base(pin); diff --git a/cpu/lpc23xx/periph/gpio.c b/cpu/lpc23xx/periph/gpio.c index 88033646c9..131ecd46ca 100644 --- a/cpu/lpc23xx/periph/gpio.c +++ b/cpu/lpc23xx/periph/gpio.c @@ -99,7 +99,7 @@ int gpio_init_mux(unsigned pin, unsigned mux) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { unsigned _pin = pin & 31; unsigned port = pin >> 5; diff --git a/cpu/msp430/periph/gpio.c b/cpu/msp430/periph/gpio.c index af1ea5ec8c..402493d7c9 100644 --- a/cpu/msp430/periph/gpio.c +++ b/cpu/msp430/periph/gpio.c @@ -112,7 +112,7 @@ void gpio_periph_mode(gpio_t pin, bool enable) } } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { msp430_port_t *port = _port(pin); if (port->DIR & _pin_mask(pin)) { diff --git a/cpu/native/periph/gpio_linux.c b/cpu/native/periph/gpio_linux.c index a554628d97..2b32790e99 100644 --- a/cpu/native/periph/gpio_linux.c +++ b/cpu/native/periph/gpio_linux.c @@ -167,7 +167,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { struct gpiohandle_data data; diff --git a/cpu/native/periph/gpio_mock.c b/cpu/native/periph/gpio_mock.c index a335f2ddec..3094be3144 100644 --- a/cpu/native/periph/gpio_mock.c +++ b/cpu/native/periph/gpio_mock.c @@ -66,7 +66,7 @@ __attribute__((weak)) void gpio_irq_disable(gpio_t pin) (void) pin; } -__attribute__((weak)) int gpio_read(gpio_t pin) { +__attribute__((weak)) bool gpio_read(gpio_t pin) { if (pin) { return pin->value; } diff --git a/cpu/nrf5x_common/periph/gpio.c b/cpu/nrf5x_common/periph/gpio.c index 2ede21d588..7f58963f3c 100644 --- a/cpu/nrf5x_common/periph/gpio.c +++ b/cpu/nrf5x_common/periph/gpio.c @@ -126,7 +126,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { if (port(pin)->DIR & (1 << pin_num(pin))) { return (port(pin)->OUT & (1 << pin_num(pin))) ? 1 : 0; diff --git a/cpu/qn908x/periph/gpio.c b/cpu/qn908x/periph/gpio.c index 761eefbf37..6258f325ce 100644 --- a/cpu/qn908x/periph/gpio.c +++ b/cpu/qn908x/periph/gpio.c @@ -182,7 +182,7 @@ void gpio_irq_disable(gpio_t pin) #endif /* defined(MODULE_PERIPH_GPIO_IRQ) */ -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return ((GPIO_T_ADDR(pin)->DATA) >> GPIO_T_PIN(pin)) & 1u; } diff --git a/cpu/rpx0xx/periph/gpio.c b/cpu/rpx0xx/periph/gpio.c index f8ec327404..859947e120 100644 --- a/cpu/rpx0xx/periph/gpio.c +++ b/cpu/rpx0xx/periph/gpio.c @@ -106,7 +106,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { if (SIO->GPIO_OE & (1LU << pin)) { /* pin is output: */ diff --git a/cpu/sam0_common/periph/gpio.c b/cpu/sam0_common/periph/gpio.c index 5fd7ed2057..50129c130d 100644 --- a/cpu/sam0_common/periph/gpio.c +++ b/cpu/sam0_common/periph/gpio.c @@ -195,7 +195,7 @@ int gpio_init(gpio_t pin, gpio_mode_t mode) return 0; } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { PortGroup *port; int mask = _pin_mask(pin); diff --git a/cpu/sam3/periph/gpio.c b/cpu/sam3/periph/gpio.c index 4edee67e3d..6253814094 100644 --- a/cpu/sam3/periph/gpio.c +++ b/cpu/sam3/periph/gpio.c @@ -244,7 +244,7 @@ void gpio_irq_disable(gpio_t pin) NVIC_DisableIRQ((1 << (_port_num(pin) + PIOA_IRQn))); } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { Pio *port = _port(pin); int pin_num = _pin_num(pin); diff --git a/cpu/stm32/periph/gpio_all.c b/cpu/stm32/periph/gpio_all.c index f7403b463f..526deccbb6 100644 --- a/cpu/stm32/periph/gpio_all.c +++ b/cpu/stm32/periph/gpio_all.c @@ -203,7 +203,7 @@ void gpio_irq_disable(gpio_t pin) EXTI_REG_IMR &= ~(1 << _pin_num(pin)); } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { return (_port(pin)->IDR & (1 << _pin_num(pin))); } diff --git a/cpu/stm32/periph/gpio_f1.c b/cpu/stm32/periph/gpio_f1.c index 18359fa137..0896765057 100644 --- a/cpu/stm32/periph/gpio_f1.c +++ b/cpu/stm32/periph/gpio_f1.c @@ -145,7 +145,7 @@ void gpio_init_analog(gpio_t pin) *(uint32_t *)(&_port(pin)->CRL + (pin_num >= 8)) &= ~(0xfl << (4 * (pin_num - ((pin_num >= 8) * 8)))); } -int gpio_read(gpio_t pin) +bool gpio_read(gpio_t pin) { GPIO_TypeDef *port = _port(pin); int pin_num = _pin_num(pin); diff --git a/drivers/include/periph/gpio.h b/drivers/include/periph/gpio.h index 8a07104984..57861880d4 100644 --- a/drivers/include/periph/gpio.h +++ b/drivers/include/periph/gpio.h @@ -227,10 +227,10 @@ void gpio_irq_disable(gpio_t pin); * * @param[in] pin the pin to read * - * @return 0 when pin is LOW - * @return >0 for HIGH + * @retval false pin is LOW + * @retval true pin is HIGH */ -int gpio_read(gpio_t pin); +bool gpio_read(gpio_t pin); /** * @brief Set the given pin to HIGH From ceeb787a6fd1174b6e7764b4fd4bbc8b4105410c Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 22 Oct 2024 21:50:00 +0200 Subject: [PATCH 2/3] drivers: no need to convert gpio_read() to bool We just changed the API so that it returns bool anyway. --- drivers/dht/dht.c | 2 +- drivers/epd_bw_spi/epd_bw_spi.c | 4 ++-- drivers/lm75/lm75.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 437e7ae4c0..cd57be636b 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -74,7 +74,7 @@ static void _wait_for_level(gpio_t pin, bool expected, uint32_t start) /* Calls to ztimer_now() can be relatively slow on low end platforms. * Mixing in a busy down-counting loop solves issues e.g. on AVR boards. */ uint8_t pre_timeout = 0; - while (((bool)gpio_read(pin) != expected) && (++pre_timeout + while ((gpio_read(pin) != expected) && (++pre_timeout || ztimer_now(ZTIMER_USEC) < start + SPIN_TIMEOUT)) {} } diff --git a/drivers/epd_bw_spi/epd_bw_spi.c b/drivers/epd_bw_spi/epd_bw_spi.c index d5ff4bb371..c343a2d421 100644 --- a/drivers/epd_bw_spi/epd_bw_spi.c +++ b/drivers/epd_bw_spi/epd_bw_spi.c @@ -30,7 +30,7 @@ static void epd_bw_spi_cmd_start(epd_bw_spi_params_t *p, uint8_t cmd, bool cont) { DEBUG("[epd_bw_spi] cmd_start: command 0x%02x\n", cmd); if (gpio_is_valid(p->busy_pin)) { - while ((bool)gpio_read(p->busy_pin) == p->busy_value) {} + while (gpio_read(p->busy_pin) == p->busy_value) {} } gpio_clear(p->dc_pin); spi_transfer_byte(p->spi, p->cs_pin, cont, (uint8_t)cmd); @@ -52,7 +52,7 @@ static void epd_bw_spi_wait(epd_bw_spi_params_t *p, uint32_t msec) { if (gpio_is_valid(p->busy_pin)) { DEBUG("[epd_bw_spi] wait: for busy bin\n"); - while ((bool)gpio_read(p->busy_pin) == p->busy_value) {} + while (gpio_read(p->busy_pin) == p->busy_value) {} } else { DEBUG("[epd_bw_spi] wait: for %" PRIu32 " milliseconds\n", msec); diff --git a/drivers/lm75/lm75.c b/drivers/lm75/lm75.c index 8a78251fe7..85e774cf3b 100644 --- a/drivers/lm75/lm75.c +++ b/drivers/lm75/lm75.c @@ -244,7 +244,7 @@ int lm75_get_os_pin(lm75_t *dev, bool *os_pin_state) { return LM75_ERROR; } - *os_pin_state = !!gpio_read(dev->lm75_params.gpio_alarm) == dev->lm75_params.polarity; + *os_pin_state = gpio_read(dev->lm75_params.gpio_alarm) == dev->lm75_params.polarity; return LM75_SUCCESS; } From 7d1313b3bfcc18886e8f001c11b2031d1473e5e3 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 24 Oct 2024 09:57:36 +0200 Subject: [PATCH 3/3] treewide: update rust-riot-wrappers --- examples/rust-async/Cargo.lock | Bin 21575 -> 21575 bytes examples/rust-gcoap/Cargo.lock | Bin 29022 -> 29022 bytes examples/rust-hello-world/Cargo.lock | Bin 17988 -> 17988 bytes sys/rust_riotmodules_standalone/Cargo.lock | Bin 20145 -> 20145 bytes tests/rust_minimal/Cargo.lock | Bin 17984 -> 17984 bytes 5 files changed, 0 insertions(+), 0 deletions(-) diff --git a/examples/rust-async/Cargo.lock b/examples/rust-async/Cargo.lock index d02ed8566ffa985b5affaa368e14684d316fa847..6d5d515cd9d871afe3bbf9c56c7b5d7e1f449334 100644 GIT binary patch delta 59 zcmX@Ug7NqY#tql4G?Gk>4U>~ClarGylZ}&8laehhQ_PIalFUt#43aEN5=~5v(^5?> Pk|!S&6x}RhZ7c}@ck~lU delta 59 zcmX@Ug7NqY#tql4G|UZCElf>PlTA`hQ;ky$EzFZl(~JyFlTwV5Ez*n)QWFi#lMItm P3@0BH6x}RhZ7c}@VnPzy diff --git a/examples/rust-gcoap/Cargo.lock b/examples/rust-gcoap/Cargo.lock index d4101e38b37a70a434c3592f603829b321950998..47760dd547128b30d9579230d8c0a9264368353c 100644 GIT binary patch delta 59 zcmccji1FSd#tpsx8cC+chRMm6$;nBU$;Qd4Ny(O$DP~4yN#-U=21ynsi6*ATX{n|b P$&(qmMK_=FpP~o=j=mHl delta 57 zcmccji1FSd#tpsx>gI;27N#bt$tJ0$sm3XW7UoH&X-0;oNhwCj7HP%?sfh;WNrp)& Nleon;pYorg2mn>N6M+B# diff --git a/examples/rust-hello-world/Cargo.lock b/examples/rust-hello-world/Cargo.lock index ffb68250e8eb80458c0e911d70aad1be9af112b3..9c487cacaee1b0c0352e4500593a6accf6ab3275 100644 GIT binary patch delta 57 zcmX@o!+4~Jal>al^(0ec!{lVkal4Rga(3saNSWRq0WRO1vw3-ct?G$TXPq!goMi!@_{)I O!^w<#Vw(;1g~b6AgPDK)y delta 59 zcmdlumvQ4<#tkv18s>(n7N#bt$tJ0$sm3XW7UoH&X-0;oNhwCj7HP%?sfh;WNrp)& PhLZzzL^sbgm5>AgIFk}5 diff --git a/tests/rust_minimal/Cargo.lock b/tests/rust_minimal/Cargo.lock index 98436a2e97f201d1de63edcc6c661206633650e5..fcf2547a5de116bf10900cb0305bfd338d572a0a 100644 GIT binary patch delta 57 zcmX@m!+4;Fal>al^(0ec!{lVkal4Rga(3saNSWRq0WRO1vw3-ct?G$TXPq!goMi!@_{)I O!^w<#Vw(;1`NaWH