I2C fast mode plus support library. This library works on mbed-11U24's p28&p27 only.
A sample code for I2C Fast mode plus operation.
Warning!
This code works on mbed-LPC11U24 only.
Import programI2C_FmPlus_Hello
Sample code for "I2C_FmPlus" library. I2C fast mode plus support library. This library works on mbed-11U24's p28&p27 only.
Diff: I2C_FmPlus.cpp
- Revision:
- 1:6d19e8abca69
- Parent:
- 0:213903402306
- Child:
- 2:16a05acd675b
--- a/I2C_FmPlus.cpp Tue Dec 02 01:17:19 2014 +0000 +++ b/I2C_FmPlus.cpp Tue Dec 02 03:57:29 2014 +0000 @@ -2,17 +2,14 @@ * * @class I2C_FmPlus * @author tedd - * @version 0.9 - * @date 13-Jul-2012 - * - * Released under the MIT License: http://mbed.org/license/mit - * + * @version 1.0 + * @date 02-Dec-2014 */ #include "mbed.h" #include "I2C_FmPlus.h" -I2C_FmPlus::I2C_FmPlus( PinName sda, PinName scl, const char *name ) : I2C( sda, scl, name ) +I2C_FmPlus::I2C_FmPlus( PinName sda, PinName scl ) : I2C( sda, scl ), _frequency( 1000 * 1000 ), _ratio( 0.4 ) { LPC_IOCON->PIO0_4 &= ~0x300; LPC_IOCON->PIO0_4 |= 0x200; @@ -20,8 +17,27 @@ LPC_IOCON->PIO0_5 |= 0x200; } +void I2C_FmPlus::frequency( float hz ) { + _frequency = hz; + uint32_t pclk = SystemCoreClock; + float i2c_scl_period = (1.0 / hz) - 100e-9; // for SCLH offset and rise/fall time conpensation; + float scl_period_in_clock = (float)pclk * i2c_scl_period; + uint32_t low_period = scl_period_in_clock * (1.0 - _ratio); + uint32_t high_period = scl_period_in_clock * _ratio; + // I2C Rate + LPC_I2C->SCLL = low_period < 4 ? 4 : low_period; + LPC_I2C->SCLH = high_period < 4 ? 4 : high_period; +} + +void I2C_FmPlus::duty_ratio( float ratio ) { + _ratio = ratio; + frequency( _frequency ); +} + +#if 0 void I2C_FmPlus::scl_setting( char sclh, char scll ) { LPC_I2C->SCLH = sclh; LPC_I2C->SCLL = scll; } +#endif