arch/arm/src/sam34/sam_twi.c
/*******************************************************************************
 * Name: twi_hw_setfrequency
 *
 * Description:
 *   Set the frequency for the next transfer
 *
 *******************************************************************************/
twi_hw_setfrequency
Description:
Set the frequency for the next transfer
twi_hw_setfrequency Description : Set the frequency for the next transfer
static uint32_t twi_hw_setfrequency(struct twi_dev_s *priv, uint32_t frequency)
{
  unsigned int ckdiv;
  unsigned int cldiv;
  uint32_t actual;
  uint32_t regval;
  for (ckdiv = 0; ckdiv < 8; ckdiv++)
    {
      cldiv = ((priv->frequency / (frequency << 1)) - 4) / (1 << ckdiv);
      if (cldiv <= 255)
        {
          break;
        }
    }
  twi_putrel(priv, SAM_TWI_CWGR_OFFSET, 0);
  regval = ((uint32_t)ckdiv << TWI_CWGR_CKDIV_SHIFT) |
           ((uint32_t)cldiv << TWI_CWGR_CHDIV_SHIFT) |
           ((uint32_t)cldiv << TWI_CWGR_CLDIV_SHIFT);
  twi_putrel(priv, SAM_TWI_CWGR_OFFSET, regval);
  actual = (priv->frequency / 2) / (((1 << ckdiv) * cldiv) + 2);
  i2cvdbg("TWI%d frequency: %d ckdiv: %d cldiv: %d actual: %d\n",
          priv->twi, frequency, ckdiv, cldiv, actual);
  priv->deffreq = frequency;
  return actual;
}
static uint32_t twi_hw_setfrequency ( struct twi_dev_s * priv , uint32_t frequency ) { unsigned int ckdiv ; unsigned int cldiv ; uint32_t actual ; uint32_t regval ; for ( ckdiv = 0 ; ckdiv < 8 ; ckdiv ++ ) { cldiv = ( ( priv -> frequency / ( frequency << 1 ) ) - 4 ) / ( 1 << ckdiv ) ; if ( cldiv <= 255 ) { break ; } } twi_putrel ( priv , SAM_TWI_CWGR_OFFSET , 0 ) ; regval = ( ( uint32_t ) ckdiv << TWI_CWGR_CKDIV_SHIFT ) | ( ( uint32_t ) cldiv << TWI_CWGR_CHDIV_SHIFT ) | ( ( uint32_t ) cldiv << TWI_CWGR_CLDIV_SHIFT ) ; twi_putrel ( priv , SAM_TWI_CWGR_OFFSET , regval ) ; actual = ( priv -> frequency / 2 ) / ( ( ( 1 << ckdiv ) * cldiv ) + 2 ) ; i2cvdbg ( " \n " , priv -> twi , frequency , ckdiv , cldiv , actual ) ; priv -> deffreq = frequency ; return actual ; }
Name: twi_hw_setfrequency
 Description:
   Set the frequency for the next transfer
Name : twi_hw_setfrequency Description : Set the frequency for the next transfer
/* Configure TWI output clocking, trying each value of CKDIV {0..7} */ /* Calulate the CLDIV value using the current CKDIV guess */ /* Is CLDIV in range? */ /* Yes, break out and use it */ /* Then setup the TWI Clock Waveform Generator Register, using the same
   * value for CLDIV and CHDIV (for 1:1 duty).
   */ /* Return the actual frequency */ /* Remember the selected frequency for error recovery */