move I2C communication to arbiter
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#include "drvI2C.h"
|
||||
#include "cmsis_os2.h"
|
||||
#include "main.h"
|
||||
#include "mutex.h"
|
||||
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
extern I2C_HandleTypeDef hi2c2;
|
||||
|
||||
static bool initialized;
|
||||
|
||||
static osMutexId_t mutex1, mutex2;
|
||||
|
||||
bool drvI2C_Init(void)
|
||||
{
|
||||
if (initialized)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(mutex1 = osMutexNew(NULL)))
|
||||
{
|
||||
return initialized = false;
|
||||
}
|
||||
|
||||
if (!(mutex2 = osMutexNew(NULL)))
|
||||
{
|
||||
osMutexDelete(mutex1);
|
||||
return initialized = false;
|
||||
}
|
||||
|
||||
return initialized = true;
|
||||
}
|
||||
|
||||
static bool drvI2C_write(I2C_HandleTypeDef *hi2c, uint16_t dev_addr, uint16_t mem_addr, uint8_t data)
|
||||
{
|
||||
return HAL_I2C_Mem_Write(hi2c,
|
||||
dev_addr,
|
||||
mem_addr,
|
||||
I2C_MEMADD_SIZE_8BIT,
|
||||
&data,
|
||||
1,
|
||||
50) == HAL_OK;
|
||||
}
|
||||
|
||||
bool drvI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
OS_ACQUIRE(mutex1, osWaitForever);
|
||||
|
||||
bool res = drvI2C_write(&hi2c1, dev, reg, data);
|
||||
|
||||
OS_RELEASE(mutex1);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool drvI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
OS_ACQUIRE(mutex2, osWaitForever);
|
||||
|
||||
bool res = drvI2C_write(&hi2c2, dev, reg, data);
|
||||
|
||||
OS_RELEASE(mutex2);
|
||||
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user