sifive/devices/i2c.h
static inline uint16_t i2c_min_clk_prescaler(unsigned int input_khz, unsigned int max_target_khz)
{
  // f_sck = f_in / (2 * (div + 1)) => div = (f_in / (2*f_sck)) - 1
  // prescale = (f_in / (5*f_scl)) - 1
  //
  // The nearest integer solution for div requires rounding up as to not exceed
  // max_target_khz.
  //
  // div = ceil(f_in / (5*f_scl)) - 1
  //     = floor((f_in - 1 + 5*f_scl) / (5*f_scl)) - 1
  //
  // This should not overflow as long as (f_in - 1 + 5*f_scl) does not exceed
  // 2^32 - 1, which is unlikely since we represent frequencies in kHz.
  unsigned int quotient = (input_khz + 5 * max_target_khz - 1) / (5 * max_target_khz);
  // Avoid underflow
  if (quotient == 0) {
    return 0;
  } else {
    return quotient - 1;
  }
}
/**
 * Get smallest clock prescaler that divides input_khz to a quotient less than or
 * equal to max_target_khz;
 */
Get smallest clock prescaler that divides input_khz to a quotient less than or
equal to max_target_khz.
Get smallest clock prescaler that divides input_khz to a quotient less than or equal to max_target_khz .
static inline uint16_t i2c_min_clk_prescaler(unsigned int input_khz, unsigned int max_target_khz)
{
   The nearest integer solution for div requires rounding up as to not exceed
   max_target_khz.
   div = ceil(f_in / (5*f_scl)) - 1
       = floor((f_in - 1 + 5*f_scl) / (5*f_scl)) - 1
   This should not overflow as long as (f_in - 1 + 5*f_scl) does not exceed
   2^32 - 1, which is unlikely since we represent frequencies in kHz.
  unsigned int quotient = (input_khz + 5 * max_target_khz - 1) / (5 * max_target_khz);
   Avoid underflow
  if (quotient == 0) {
    return 0;
  } else {
    return quotient - 1;
  }
}
static inline uint16_t i2c_min_clk_prescaler ( unsigned int input_khz , unsigned int max_target_khz ) { unsigned int quotient = ( input_khz + 5 * max_target_khz - 1 ) / ( 5 * max_target_khz ) ; if ( quotient == 0 ) { return 0 ; } else { return quotient - 1 ; } }
Get smallest clock prescaler that divides input_khz to a quotient less than or
 equal to max_target_khz;
Get smallest clock prescaler that divides input_khz to a quotient less than or equal to max_target_khz ;
// f_sck = f_in / (2 * (div + 1)) => div = (f_in / (2*f_sck)) - 1 // prescale = (f_in / (5*f_scl)) - 1 // // The nearest integer solution for div requires rounding up as to not exceed // max_target_khz. // // div = ceil(f_in / (5*f_scl)) - 1 //     = floor((f_in - 1 + 5*f_scl) / (5*f_scl)) - 1 // // This should not overflow as long as (f_in - 1 + 5*f_scl) does not exceed // 2^32 - 1, which is unlikely since we represent frequencies in kHz. // Avoid underflow