move I2C communication to arbiter
This commit is contained in:
@@ -4,6 +4,7 @@ set(KPI_Rover_Arbiter_I2C_Inc
|
||||
|
||||
set(KPI_Rover_Arbiter_I2C_Src
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/hwI2C.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/drvI2C.c
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Arbiter_I2C STATIC)
|
||||
@@ -18,6 +19,7 @@ target_sources(KPI_Rover_Arbiter_I2C PRIVATE
|
||||
|
||||
target_link_libraries(KPI_Rover_Arbiter_I2C PRIVATE
|
||||
HAL_H
|
||||
FREERTOS_H
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Arbiter_I2C_H INTERFACE)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
#ifndef __ARB_DRV_I2C
|
||||
#define __ARB_DRV_I2C
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool drvI2C_Init(void);
|
||||
bool drvI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data);
|
||||
bool drvI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data);
|
||||
|
||||
#endif /* __ARB_DRV_I2C */
|
||||
Reference in New Issue
Block a user