move I2C communication to arbiter
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#ifndef __RTOSM_MUTEX
|
||||
#define __RTOSM_MUTEX
|
||||
|
||||
#define OS_ACQUIRE(mutex, time) osMutexAcquire(mutex, time)
|
||||
#define OS_RELEASE(mutex) osMutexRelease(mutex)
|
||||
|
||||
#endif /* __RTOSM_MUTEX */
|
||||
@@ -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 */
|
||||
+16
-5
@@ -1,6 +1,7 @@
|
||||
#include "KPIRover.h"
|
||||
|
||||
#include "ProtocolHandler.h"
|
||||
#include "drvI2C.h"
|
||||
#include "ulDatabase.h"
|
||||
#include "ulDip.h"
|
||||
#include "ulEncoder.h"
|
||||
@@ -68,23 +69,33 @@ static struct ulDatabase_ParamMetadata ulDatabase_params[] = {
|
||||
};
|
||||
|
||||
void KPIRover_Init(void) {
|
||||
static bool dip[4];
|
||||
// boot mode check
|
||||
ulDip_Init();
|
||||
if (ulDip_Read(dip))
|
||||
{
|
||||
if (dip[0] == true)
|
||||
bool dip[4];
|
||||
|
||||
if (ulDip_Read(dip))
|
||||
{
|
||||
ulLEDMode_Start();
|
||||
if (dip[0] == true)
|
||||
{
|
||||
ulLEDMode_Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// normal boot sequence
|
||||
|
||||
// logger
|
||||
ULOG_INIT();
|
||||
ULOG_SUBSCRIBE(ul_ulog_send, ULOG_DEBUG_LEVEL);
|
||||
|
||||
// database
|
||||
ulDatabase_init(ulDatabase_params, sizeof(ulDatabase_params) / sizeof(struct ulDatabase_ParamMetadata));
|
||||
ProtocolHandler_init();
|
||||
|
||||
// arbiters
|
||||
drvI2C_Init();
|
||||
|
||||
// components
|
||||
//ulAdc_init();
|
||||
ulMotorsController_init();
|
||||
ulEncoder_Init();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "PCA9685.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "hwI2C.h"
|
||||
#include "drvI2C.h"
|
||||
|
||||
/* PCA9685 I2C address */
|
||||
/* 0x40 is the 7-bit address. HAL requires it shifted left by 1 (0x80) */
|
||||
@@ -24,13 +25,7 @@
|
||||
|
||||
static bool pca9685_write_i2c(uint8_t reg, uint8_t data)
|
||||
{
|
||||
return HAL_I2C_Mem_Write(&hi2c2,
|
||||
PCA9685_ADDR,
|
||||
reg,
|
||||
I2C_MEMADD_SIZE_8BIT,
|
||||
&data,
|
||||
1,
|
||||
50) == HAL_OK;
|
||||
return drvI2C_I2C2_Write(PCA9685_ADDR, reg, data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ set(STM32_HAL_Include_Dirs
|
||||
)
|
||||
|
||||
set(FREERTOS_Include_Dirs
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../Include/RTOSMacros
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../Include/FreeRTOS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2
|
||||
|
||||
Reference in New Issue
Block a user