|
|
|
@ -18,6 +18,7 @@
|
|
|
|
|
* For implementation details please refer to STM application note AN2824.
|
|
|
|
|
*
|
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
|
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
|
|
|
|
|
*
|
|
|
|
|
* @}
|
|
|
|
|
*/
|
|
|
|
@ -26,6 +27,7 @@
|
|
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
|
#include "irq.h"
|
|
|
|
|
#include "mutex.h"
|
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
|
|
|
|
|
@ -44,6 +46,24 @@ static inline void _clear_addr(I2C_TypeDef *dev);
|
|
|
|
|
static inline void _write(I2C_TypeDef *dev, char *data, int length);
|
|
|
|
|
static inline void _stop(I2C_TypeDef *dev);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Array holding one pre-initialized mutex for each I2C device
|
|
|
|
|
*/
|
|
|
|
|
static mutex_t locks[] = {
|
|
|
|
|
#if I2C_0_EN
|
|
|
|
|
[I2C_0] = MUTEX_INIT,
|
|
|
|
|
#endif
|
|
|
|
|
#if I2C_1_EN
|
|
|
|
|
[I2C_1] = MUTEX_INIT,
|
|
|
|
|
#endif
|
|
|
|
|
#if I2C_2_EN
|
|
|
|
|
[I2C_2] = MUTEX_INIT
|
|
|
|
|
#endif
|
|
|
|
|
#if I2C_3_EN
|
|
|
|
|
[I2C_3] = MUTEX_INIT
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int i2c_init_master(i2c_t dev, i2c_speed_t speed)
|
|
|
|
|
{
|
|
|
|
|
I2C_TypeDef *i2c;
|
|
|
|
@ -105,6 +125,7 @@ int i2c_init_master(i2c_t dev, i2c_speed_t speed)
|
|
|
|
|
/* enable device */
|
|
|
|
|
_i2c_init(i2c, ccr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -173,6 +194,24 @@ int i2c_init_slave(i2c_t dev, uint8_t address)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i2c_acquire(i2c_t dev)
|
|
|
|
|
{
|
|
|
|
|
if (dev >= I2C_NUMOF) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
mutex_lock(&locks[dev]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i2c_release(i2c_t dev)
|
|
|
|
|
{
|
|
|
|
|
if (dev >= I2C_NUMOF) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
mutex_unlock(&locks[dev]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i2c_read_byte(i2c_t dev, uint8_t address, char *data)
|
|
|
|
|
{
|
|
|
|
|
return i2c_read_bytes(dev, address, data, 1);
|
|
|
|
|