mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_DISCO_F051R8/i2c_api.c
- Revision:
- 227:7bd0639b8911
- Parent:
- 142:7f2b6c44bbe3
- Child:
- 240:9a7c54113eaf
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,17 +27,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -51,19 +51,16 @@ {NC, NC, 0} }; -void i2c_init(i2c_t *obj, PinName sda, PinName scl) { +void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); } //if (obj->i2c == I2C_2) { @@ -80,10 +77,11 @@ i2c_reset(obj); // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000)); //"Only 100kHz, 200kHz and 400kHz I2C frequencies are supported." I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; @@ -101,7 +99,6 @@ tim = 0x0010020A; // Fast mode break; default: - error("Only 100kHz, 200kHz and 400kHz I2C frequencies are supported."); break; } @@ -212,7 +209,7 @@ int timeout; // Wait until the byte is received - timeout = FLAG_TIMEOUT; + timeout = FLAG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { timeout--; if (timeout == 0) { @@ -244,13 +241,13 @@ } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } //if (obj->i2c == I2C_2) { // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); - // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); + // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); //} }