1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

cpu/samd21: make i2c thread-safe

This commit is contained in:
Thomas Eichinger 2015-01-19 12:15:09 +01:00
parent fe643f7077
commit 30040fa311

View File

@ -12,6 +12,7 @@
* @file
* @brief Low-level I2C driver implementation
* @author Baptiste Clenet <baptiste.clenet@xsoen.com>
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
* @}
*/
@ -19,7 +20,7 @@
#include "cpu.h"
#include "board.h"
#include "mutex.h"
#include "periph_conf.h"
#include "periph/i2c.h"
#include "instance/instance_sercom3.h"
@ -41,6 +42,23 @@ static inline void _write(SercomI2cm *dev, char *data, int length);
static inline void _read(SercomI2cm *dev, char *data, int length);
static inline void _stop(SercomI2cm *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)
{
@ -191,6 +209,24 @@ int i2c_init_slave(i2c_t dev, uint8_t address)
return 0;
}
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);