diff --git a/boards/arm/qemu_cortex_m0/nrf_timer_timer.c b/boards/arm/qemu_cortex_m0/nrf_timer_timer.c index 414f34d9e82..25d642578bc 100644 --- a/boards/arm/qemu_cortex_m0/nrf_timer_timer.c +++ b/boards/arm/qemu_cortex_m0/nrf_timer_timer.c @@ -195,7 +195,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) } ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); uint32_t unannounced = counter_sub(counter(), last_count); diff --git a/drivers/i2c/i2c_sam0.c b/drivers/i2c/i2c_sam0.c index f0fea05b0bf..76cd4b342a3 100644 --- a/drivers/i2c/i2c_sam0.c +++ b/drivers/i2c/i2c_sam0.c @@ -574,7 +574,7 @@ static int i2c_sam0_set_apply_bitrate(const struct device *dev, /* 2:1 low:high ratio */ baud_high = baud; baud_high /= 3U; - baud_high = MAX(MIN(baud_high, 255U), 1U); + baud_high = CLAMP(baud_high, 1U, 255U); baud_low = baud - baud_high; if (baud_low < 1U && baud_high > 1U) { --baud_high; @@ -605,7 +605,7 @@ static int i2c_sam0_set_apply_bitrate(const struct device *dev, /* 2:1 low:high ratio */ baud_high = baud; baud_high /= 3U; - baud_high = MAX(MIN(baud_high, 255U), 1U); + baud_high = CLAMP(baud_high, 1U, 255U); baud_low = baud - baud_high; if (baud_low < 1U && baud_high > 1U) { --baud_high; diff --git a/drivers/modem/wncm14a2a.c b/drivers/modem/wncm14a2a.c index 8ac0e0b875c..07bbeb1fc62 100644 --- a/drivers/modem/wncm14a2a.c +++ b/drivers/modem/wncm14a2a.c @@ -1654,7 +1654,7 @@ static int offload_connect(struct net_context *context, * AT@SOCKCONN timeout param has minimum value of 30 seconds and * maximum value of 360 seconds, otherwise an error is generated */ - timeout_sec = MIN(360, MAX(timeout_sec, 30)); + timeout_sec = CLAMP(timeout_sec, 30, 360); snprintk(buf, sizeof(buf), "AT@SOCKCONN=%d,\"%s\",%d,%d", sock->socket_id, wncm14a2a_sprint_ip_addr(addr), diff --git a/drivers/spi/spi_sam.c b/drivers/spi/spi_sam.c index 5821a074974..77642eac0aa 100644 --- a/drivers/spi/spi_sam.c +++ b/drivers/spi/spi_sam.c @@ -94,7 +94,7 @@ static int spi_sam_configure(const struct device *dev, /* Use the requested or next highest possible frequency */ div = SOC_ATMEL_SAM_MCK_FREQ_HZ / config->frequency; - div = MAX(1, MIN(UINT8_MAX, div)); + div = CLAMP(div, 1, UINT8_MAX); spi_csr |= SPI_CSR_SCBR(div); regs->SPI_CR = SPI_CR_SPIDIS; /* Disable SPI */ diff --git a/drivers/spi/spi_sam0.c b/drivers/spi/spi_sam0.c index 97b14abf6c9..5a860bbc5bc 100644 --- a/drivers/spi/spi_sam0.c +++ b/drivers/spi/spi_sam0.c @@ -119,7 +119,7 @@ static int spi_sam0_configure(const struct device *dev, /* Use the requested or next highest possible frequency */ div = (SOC_ATMEL_SAM0_GCLK0_FREQ_HZ / config->frequency) / 2U - 1; - div = MAX(0, MIN(UINT8_MAX, div)); + div = CLAMP(div, 0, UINT8_MAX); /* Update the configuration only if it has changed */ if (regs->CTRLA.reg != ctrla.reg || regs->CTRLB.reg != ctrlb.reg || diff --git a/drivers/timer/cavs_timer.c b/drivers/timer/cavs_timer.c index b3ff58ff33b..6e87a69832d 100644 --- a/drivers/timer/cavs_timer.c +++ b/drivers/timer/cavs_timer.c @@ -124,7 +124,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) #ifdef CONFIG_TICKLESS_KERNEL ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); k_spinlock_key_t key = k_spin_lock(&lock); uint64_t curr = count(); diff --git a/drivers/timer/cc13x2_cc26x2_rtc_timer.c b/drivers/timer/cc13x2_cc26x2_rtc_timer.c index 6c0414a5e17..1509151b552 100644 --- a/drivers/timer/cc13x2_cc26x2_rtc_timer.c +++ b/drivers/timer/cc13x2_cc26x2_rtc_timer.c @@ -208,7 +208,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) #ifdef CONFIG_TICKLESS_KERNEL ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t) MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t) MAX_TICKS); k_spinlock_key_t key = k_spin_lock(&lock); diff --git a/drivers/timer/cortex_m_systick.c b/drivers/timer/cortex_m_systick.c index ec046a7ba24..a9da68cefda 100644 --- a/drivers/timer/cortex_m_systick.c +++ b/drivers/timer/cortex_m_systick.c @@ -183,7 +183,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) uint32_t delay; ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); k_spinlock_key_t key = k_spin_lock(&lock); diff --git a/drivers/timer/hpet.c b/drivers/timer/hpet.c index bf5fe9c5733..11279a4f1d6 100644 --- a/drivers/timer/hpet.c +++ b/drivers/timer/hpet.c @@ -165,7 +165,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) } ticks = ticks == K_TICKS_FOREVER ? max_ticks : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)max_ticks), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)max_ticks); k_spinlock_key_t key = k_spin_lock(&lock); uint32_t now = MAIN_COUNTER_REG, cyc, adj; diff --git a/drivers/timer/nrf_rtc_timer.c b/drivers/timer/nrf_rtc_timer.c index 950e8e0f713..c80328374f2 100644 --- a/drivers/timer/nrf_rtc_timer.c +++ b/drivers/timer/nrf_rtc_timer.c @@ -229,7 +229,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) } ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); uint32_t unannounced = counter_sub(counter(), last_count); diff --git a/drivers/timer/riscv_machine_timer.c b/drivers/timer/riscv_machine_timer.c index eb6c50a95a4..be8e7d21964 100644 --- a/drivers/timer/riscv_machine_timer.c +++ b/drivers/timer/riscv_machine_timer.c @@ -106,7 +106,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) } ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); k_spinlock_key_t key = k_spin_lock(&lock); uint64_t now = mtime(); diff --git a/drivers/timer/sam0_rtc_timer.c b/drivers/timer/sam0_rtc_timer.c index 0a76901b898..b484d0472a2 100644 --- a/drivers/timer/sam0_rtc_timer.c +++ b/drivers/timer/sam0_rtc_timer.c @@ -259,7 +259,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) #ifdef CONFIG_TICKLESS_KERNEL ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t) MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t) MAX_TICKS); /* Compute number of RTC cycles until the next timeout. */ uint32_t count = rtc_count(); diff --git a/drivers/timer/stm32_lptim_timer.c b/drivers/timer/stm32_lptim_timer.c index 09c727b8820..ebb0f919be2 100644 --- a/drivers/timer/stm32_lptim_timer.c +++ b/drivers/timer/stm32_lptim_timer.c @@ -210,7 +210,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) * treated identically: it simply indicates the kernel would like the * next tick announcement as soon as possible. */ - ticks = MAX(MIN(ticks - 1, (int32_t)LPTIM_TIMEBASE), 1); + ticks = CLAMP(ticks - 1, 1, (int32_t)LPTIM_TIMEBASE); k_spinlock_key_t key = k_spin_lock(&lock); diff --git a/drivers/timer/xtensa_sys_timer.c b/drivers/timer/xtensa_sys_timer.c index 3273fae1459..6d1c1875ca3 100644 --- a/drivers/timer/xtensa_sys_timer.c +++ b/drivers/timer/xtensa_sys_timer.c @@ -73,7 +73,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle) #if defined(CONFIG_TICKLESS_KERNEL) ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks; - ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0); + ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS); k_spinlock_key_t key = k_spin_lock(&lock); uint32_t curr = ccount(), cyc, adj; diff --git a/kernel/timeout.c b/kernel/timeout.c index fcd13855749..8e8da334a5e 100644 --- a/kernel/timeout.c +++ b/kernel/timeout.c @@ -74,7 +74,7 @@ static int32_t next_timeout(void) struct _timeout *to = first(); int32_t ticks_elapsed = elapsed(); int32_t ret = to == NULL ? MAX_WAIT - : MIN(MAX_WAIT, MAX(0, to->dticks - ticks_elapsed)); + : CLAMP(to->dticks - ticks_elapsed, 0, MAX_WAIT); #ifdef CONFIG_TIMESLICING if (_current_cpu->slice_ticks && _current_cpu->slice_ticks < ret) { diff --git a/subsys/bluetooth/controller/ll_sw/ull_conn.c b/subsys/bluetooth/controller/ll_sw/ull_conn.c index d9c9f04ac4b..24ffd79e7a0 100644 --- a/subsys/bluetooth/controller/ll_sw/ull_conn.c +++ b/subsys/bluetooth/controller/ll_sw/ull_conn.c @@ -3235,9 +3235,9 @@ static inline void dle_max_time_get(const struct ll_conn *conn, (!feature_coded_phy && !feature_phy_2m)) { rx_time = PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M); #if defined(CONFIG_BT_CTLR_PHY) - tx_time = MAX(MIN(PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M), - conn->default_tx_time), - PKT_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M)); + tx_time = CLAMP(conn->default_tx_time, + PKT_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M), + PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M)); #else /* !CONFIG_BT_CTLR_PHY */ tx_time = PKT_US(conn->default_tx_octets, PHY_1M); #endif /* !CONFIG_BT_CTLR_PHY */