separate PCA9685 driver into a shared directory

This commit is contained in:
2026-11-10 19:44:34 +02:00
parent 16a709ac24
commit 05f892c59f
13 changed files with 113 additions and 76 deletions
+1 -1
View File
@@ -4,7 +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
${CMAKE_CURRENT_SOURCE_DIR}/arbI2C.c
)
add_library(KPI_Rover_Arbiter_I2C STATIC)
@@ -1,4 +1,4 @@
#include "drvI2C.h"
#include "arbI2C.h"
#include "cmsis_os2.h"
#include "main.h"
#include "mutex.h"
@@ -10,7 +10,7 @@ static bool initialized;
static osMutexId_t mutex1, mutex2;
bool drvI2C_Init(void)
bool arbI2C_Init(void)
{
if (initialized)
{
@@ -31,7 +31,7 @@ bool drvI2C_Init(void)
return initialized = true;
}
static bool drvI2C_write(I2C_HandleTypeDef *hi2c, uint16_t dev_addr, uint16_t mem_addr, uint8_t data)
static bool arbI2C_write(I2C_HandleTypeDef *hi2c, uint16_t dev_addr, uint16_t mem_addr, uint8_t data)
{
return HAL_I2C_Mem_Write(hi2c,
dev_addr,
@@ -42,7 +42,7 @@ static bool drvI2C_write(I2C_HandleTypeDef *hi2c, uint16_t dev_addr, uint16_t me
50) == HAL_OK;
}
bool drvI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data)
bool arbI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data)
{
if (!initialized)
{
@@ -51,14 +51,14 @@ bool drvI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data)
OS_ACQUIRE(mutex1, osWaitForever);
bool res = drvI2C_write(&hi2c1, dev, reg, data);
bool res = arbI2C_write(&hi2c1, dev, reg, data);
OS_RELEASE(mutex1);
return res;
}
bool drvI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data)
bool arbI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data)
{
if (!initialized)
{
@@ -67,7 +67,7 @@ bool drvI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data)
OS_ACQUIRE(mutex2, osWaitForever);
bool res = drvI2C_write(&hi2c2, dev, reg, data);
bool res = arbI2C_write(&hi2c2, dev, reg, data);
OS_RELEASE(mutex2);
+12
View File
@@ -0,0 +1,12 @@
#ifndef __ARB_DRV_I2C
#define __ARB_DRV_I2C
#include <stdint.h>
#include <stdbool.h>
bool arbI2C_Init(void);
bool arbI2C_I2C1_Write(uint8_t dev, uint8_t reg, uint8_t data);
bool arbI2C_I2C2_Write(uint8_t dev, uint8_t reg, uint8_t data);
#endif /* __ARB_DRV_I2C */
-12
View File
@@ -1,12 +0,0 @@
#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 */
+2 -1
View File
@@ -11,6 +11,7 @@ set(KPIROVER_ARBITERS
)
set(KPIROVER_COMPONENTS
#Storage
ADC
Communication
DIP
@@ -20,7 +21,7 @@ set(KPIROVER_COMPONENTS
LEDMode
Logger
Motors
#Storage
PCA9685
)
foreach(arb ${KPIROVER_ARBITERS})
+2 -2
View File
@@ -1,7 +1,7 @@
#include "KPIRover.h"
#include "ProtocolHandler.h"
#include "drvI2C.h"
#include "arbI2C.h"
#include "ulDatabase.h"
#include "ulDip.h"
#include "ulEncoder.h"
@@ -93,7 +93,7 @@ void KPIRover_Init(void) {
ProtocolHandler_init();
// arbiters
drvI2C_Init();
arbI2C_Init();
// components
//ulAdc_init();
+2 -1
View File
@@ -4,7 +4,6 @@ set(KPI_Rover_Motors_Inc
set(KPI_Rover_Motors_Src
${CMAKE_CURRENT_SOURCE_DIR}/drvMotors.c
${CMAKE_CURRENT_SOURCE_DIR}/PCA9685.c
${CMAKE_CURRENT_SOURCE_DIR}/ulGD.c
${CMAKE_CURRENT_SOURCE_DIR}/ulMotorsController.c
${CMAKE_CURRENT_SOURCE_DIR}/ulPID.c
@@ -27,6 +26,8 @@ target_link_libraries(KPI_Rover_Motors PRIVATE
KPI_Rover_Logger_H
KPI_Rover_Arbiter_I2C_H
KPI_Rover_Arbiter_I2C
KPI_Rover_PCA9685_H
KPI_Rover_PCA9685
)
add_library(KPI_Rover_Motors_H INTERFACE)
-11
View File
@@ -1,11 +0,0 @@
#ifndef PCA_9685_H_
#define PCA_9685_H_
#include <stdint.h>
#include <stdbool.h>
bool PCA9685_Init(void);
void PCA9685_SetPWM(uint8_t channel, uint16_t on, uint16_t off);
void PCA9685_SetPin(uint8_t channel, uint8_t val);
#endif // PCA_9685_H_
+8 -8
View File
@@ -9,9 +9,9 @@ void DriverMotor_Init(drvMotor_t* motor)
__HAL_TIM_MOE_ENABLE(motor->pwm.tim.htim);
}
else if (motor->pwm_src == PWM_SRC_PCA9685) {
PCA9685_SetPWM(motor->pwm.pca.channel, 0, 0);
PCA9685_SetPin(motor->in1_pca_channel, 0);
PCA9685_SetPin(motor->in2_pca_channel, 0);
PCA9685_SetPWM(PCA9685_ADDR_1, motor->pwm.pca.channel, 0, 0);
PCA9685_SetPin(PCA9685_ADDR_1, motor->in1_pca_channel, 0);
PCA9685_SetPin(PCA9685_ADDR_1, motor->in2_pca_channel, 0);
}
motor->enabled = false;
@@ -42,11 +42,11 @@ void DriverMotor_setDirection(drvMotor_t* motor, bool forward){
}
else if (motor->pwm_src == PWM_SRC_PCA9685) {
if (forward) {
PCA9685_SetPin(motor->in1_pca_channel, 1); // IN1 = HIGH
PCA9685_SetPin(motor->in2_pca_channel, 0); // IN2 = LOW
PCA9685_SetPin(PCA9685_ADDR_1, motor->in1_pca_channel, 1); // IN1 = HIGH
PCA9685_SetPin(PCA9685_ADDR_1, motor->in2_pca_channel, 0); // IN2 = LOW
} else {
PCA9685_SetPin(motor->in1_pca_channel, 0); // IN1 = LOW
PCA9685_SetPin(motor->in2_pca_channel, 1); // IN2 = HIGH
PCA9685_SetPin(PCA9685_ADDR_1, motor->in1_pca_channel, 0); // IN1 = LOW
PCA9685_SetPin(PCA9685_ADDR_1, motor->in2_pca_channel, 1); // IN2 = HIGH
}
motor->last_forward = forward;
@@ -67,7 +67,7 @@ void DriverMotor_setPwm(drvMotor_t* motor, uint16_t pwm)
}
else if (motor->pwm_src == PWM_SRC_PCA9685) {
if (pwm > 4095) pwm = 4095;
PCA9685_SetPWM(motor->pwm.pca.channel, 0, pwm);
PCA9685_SetPWM(PCA9685_ADDR_1, motor->pwm.pca.channel, 0, pwm);
}
}
+1 -1
View File
@@ -333,7 +333,7 @@ void ulMotorsController_Task(void* argument)
ulMotorsController_Init(&g_motors_ctrl);
if (!PCA9685_Init()) {
if (!PCA9685_Init(PCA9685_ADDR_1)) {
ULOG_ERROR("Failed to initialize PCA9685");
osThreadExit();
}
+28
View File
@@ -0,0 +1,28 @@
set(KPI_Rover_PCA9685_Inc
${CMAKE_CURRENT_SOURCE_DIR}
)
set(KPI_Rover_PCA9685_Src
${CMAKE_CURRENT_SOURCE_DIR}/PCA9685.c
)
add_library(KPI_Rover_PCA9685 STATIC)
target_include_directories(KPI_Rover_PCA9685 PRIVATE
${KPI_Rover_PCA9685_Inc}
)
target_sources(KPI_Rover_PCA9685 PRIVATE
${KPI_Rover_PCA9685_Src}
)
target_link_libraries(KPI_Rover_PCA9685 PRIVATE
HAL_H
KPI_Rover_Arbiter_I2C_H
KPI_Rover_Arbiter_I2C
)
add_library(KPI_Rover_PCA9685_H INTERFACE)
target_include_directories(KPI_Rover_PCA9685_H INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
@@ -1,11 +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) */
#define PCA9685_ADDR (0x41 << 1)
#include "arbI2C.h"
/* Register Addresses */
#define MODE1 0x00
@@ -15,51 +11,55 @@
/**
* Helper: Write a single byte to a register.
*/
#define pca9685_write(reg, data) do \
#define pca9685_write(dev_addr, reg, data) do \
{ \
if (pca9685_write_i2c(reg, data) == false) \
if (pca9685_write_i2c(dev_addr, reg, data) == false) \
{ \
return false; \
} \
} while (0)
static bool pca9685_write_i2c(uint8_t reg, uint8_t data)
static bool pca_initialized[2];
static bool pca9685_write_i2c(uint8_t dev_addr, uint8_t reg, uint8_t data)
{
return drvI2C_I2C2_Write(PCA9685_ADDR, reg, data);
return arbI2C_I2C2_Write(dev_addr, reg, data);
}
/**
* Helper: Write 4 bytes consecutively (Burst Write).
* Used to update all PWM registers (ON_L, ON_H, OFF_L, OFF_H) in one go.
*/
static void pca9685_write4(uint8_t reg, uint16_t on, uint16_t off)
static void pca9685_write4(uint8_t dev_addr, uint8_t reg, uint16_t on, uint16_t off)
{
uint8_t buf[4];
/* Split 12-bit values into Low and High bytes */
buf[0] = on & 0xFF; // ON_L
buf[1] = on >> 8; // ON_H
buf[2] = off & 0xFF; // OFF_L
buf[3] = off >> 8; // OFF_H
HAL_I2C_Mem_Write(&hi2c2,
PCA9685_ADDR,
reg,
I2C_MEMADD_SIZE_8BIT,
buf,
4,
10);
buf[0] = on & 0xFF; // ON_L
buf[1] = on >> 8; // ON_H
buf[2] = off & 0xFF; // OFF_L
buf[3] = off >> 8; // OFF_H
arbI2C_I2C2_Write(dev_addr, reg, buf[0]);
arbI2C_I2C2_Write(dev_addr, reg + 1, buf[1]);
arbI2C_I2C2_Write(dev_addr, reg + 2, buf[2]);
arbI2C_I2C2_Write(dev_addr, reg + 3, buf[3]);
}
/**
* Initialize the PCA9685 chip.
* Setup frequency and enable auto-increment mode.
*/
bool PCA9685_Init(void)
bool PCA9685_Init(uint8_t dev_addr)
{
if (pca_initialized[(dev_addr - PCA9685_ADDR_1) >> 1])
{
return true;
}
HW_I2C2_Init();
/* 1. Reset device (Wake up) */
pca9685_write(MODE1, 0x00);
pca9685_write(dev_addr, MODE1, 0x00);
HAL_Delay(10);
/* 2. Setup PWM Frequency ~1000Hz */
@@ -68,19 +68,21 @@ bool PCA9685_Init(void)
uint8_t prescale = 5;
/* To change pre-scaler, we must be in SLEEP mode */
pca9685_write(MODE1, 0x10); // Sleep mode
pca9685_write(dev_addr, MODE1, 0x10); // Sleep mode
HAL_Delay(1);
pca9685_write(PRESCALE, prescale); // Write pre-scaler
pca9685_write(dev_addr, PRESCALE, prescale); // Write pre-scaler
/* 3. Wake up and set Auto-Increment */
/* 0xA1: 1010 0001
* Bit 5 (0x20): Auto-Increment enabled (allows writing 4 bytes at once)
* Bit 0 (0x01): ALLCALL enabled
*/
pca9685_write(MODE1, 0xA1);
pca9685_write(dev_addr, MODE1, 0xA1);
HAL_Delay(5);
pca_initialized[(dev_addr - PCA9685_ADDR_1) >> 1] = true;
return true;
}
@@ -90,7 +92,7 @@ bool PCA9685_Init(void)
* @param on: Step number to turn ON (usually 0)
* @param off: Step number to turn OFF (0 to 4095) - This acts as Duty Cycle
*/
void PCA9685_SetPWM(uint8_t channel, uint16_t on, uint16_t off)
void PCA9685_SetPWM(uint8_t dev_addr, uint8_t channel, uint16_t on, uint16_t off)
{
/* Safety check */
if (channel > 15)
@@ -102,18 +104,18 @@ void PCA9685_SetPWM(uint8_t channel, uint16_t on, uint16_t off)
/* Calculate register address for this channel */
/* Each channel has 4 registers, starting at LED0_ON_L (0x06) */
pca9685_write4(LED0_ON_L + 4 * channel, on, off);
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, on, off);
}
void PCA9685_SetPin(uint8_t channel, uint8_t val)
void PCA9685_SetPin(uint8_t dev_addr, uint8_t channel, uint8_t val)
{
if (channel > 15) return;
if (val) {
// Full ON: Bit 4 в ON_H 1 - 4096, OFF в 0
pca9685_write4(LED0_ON_L + 4 * channel, 0x1000, 0x0000);
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, 0x1000, 0x0000);
} else {
// Full OFF: ON в 0, Bit 4 в OFF_H 1 - 4096
pca9685_write4(LED0_ON_L + 4 * channel, 0x0000, 0x1000);
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, 0x0000, 0x1000);
}
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef PCA_9685_H_
#define PCA_9685_H_
#include <stdint.h>
#include <stdbool.h>
/* PCA9685 I2C address */
/* 0x40 is the 7-bit address. HAL requires it shifted left by 1 (0x80) */
#define PCA9685_ADDR_1 (0x41 << 1)
#define PCA9685_ADDR_2 (0x42 << 1)
bool PCA9685_Init(uint8_t dev_addr);
void PCA9685_SetPWM(uint8_t dev_addr, uint8_t channel, uint16_t on, uint16_t off);
void PCA9685_SetPin(uint8_t dev_addr, uint8_t channel, uint8_t val);
#endif /* PCA_9685_H_ */