/* Computes a cycle count for a given time in nanoseconds.  */
static uint32_t LPI2C_GetCyclesForWidth(uint32_t sourceClock_Hz, uint32_t width_ns, uint32_t minCycles, uint32_t maxCycles, uint32_t prescaler)
/* Computes a cycle count for a given time in nanoseconds.  */
static uint32_t LPI2C_GetCyclesForWidth(uint32_t sourceClock_Hz, uint32_t width_ns, uint32_t minCycles, uint32_t maxCycles, uint32_t prescaler)
{
    assert(sourceClock_Hz > 0U);
    uint32_t divider = 1U;
    while (prescaler != 0U)
    {
        divider *= 2U;
        prescaler--;
    }
    uint32_t busCycle_ns = 1000000U / (sourceClock_Hz / divider / 1000U);
    uint32_t cycles = (width_ns * 10U / busCycle_ns + 5U) / 10U;
    if (cycles < minCycles)
    {
        cycles = minCycles;
    }
    if (cycles > maxCycles)
    {
        cycles = maxCycles;
    }
    return cycles;
}