2026-10-11 19:29:03 +03:00
|
|
|
#include "PCA9685.h"
|
|
|
|
|
#include "stm32f4xx_hal.h"
|
2026-10-13 13:13:33 +03:00
|
|
|
#include "hwI2C.h"
|
2026-11-10 19:44:34 +02:00
|
|
|
#include "arbI2C.h"
|
2026-10-11 19:29:03 +03:00
|
|
|
|
|
|
|
|
/* Register Addresses */
|
2026-11-10 17:19:00 +02:00
|
|
|
#define MODE1 0x00
|
|
|
|
|
#define PRESCALE 0xFE
|
|
|
|
|
#define LED0_ON_L 0x06
|
2026-10-11 19:29:03 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper: Write a single byte to a register.
|
|
|
|
|
*/
|
2026-11-10 19:44:34 +02:00
|
|
|
#define pca9685_write(dev_addr, reg, data) do \
|
2026-10-11 19:29:03 +03:00
|
|
|
{ \
|
2026-11-10 19:44:34 +02:00
|
|
|
if (pca9685_write_i2c(dev_addr, reg, data) == false) \
|
2026-11-10 17:19:00 +02:00
|
|
|
{ \
|
|
|
|
|
return false; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 19:44:34 +02:00
|
|
|
static bool pca_initialized[2];
|
|
|
|
|
|
|
|
|
|
static bool pca9685_write_i2c(uint8_t dev_addr, uint8_t reg, uint8_t data)
|
2026-10-11 19:29:03 +03:00
|
|
|
{
|
2026-11-10 19:44:34 +02:00
|
|
|
return arbI2C_I2C2_Write(dev_addr, reg, data);
|
2026-10-11 19:29:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper: Write 4 bytes consecutively (Burst Write).
|
|
|
|
|
* Used to update all PWM registers (ON_L, ON_H, OFF_L, OFF_H) in one go.
|
|
|
|
|
*/
|
2026-11-10 19:44:34 +02:00
|
|
|
static void pca9685_write4(uint8_t dev_addr, uint8_t reg, uint16_t on, uint16_t off)
|
2026-10-11 19:29:03 +03:00
|
|
|
{
|
2026-11-10 17:19:00 +02:00
|
|
|
uint8_t buf[4];
|
|
|
|
|
/* Split 12-bit values into Low and High bytes */
|
2026-11-10 19:44:34 +02:00
|
|
|
buf[0] = on & 0xFF; // ON_L
|
|
|
|
|
buf[1] = on >> 8; // ON_H
|
|
|
|
|
buf[2] = off & 0xFF; // OFF_L
|
|
|
|
|
buf[3] = off >> 8; // OFF_H
|
|
|
|
|
|
2026-11-14 18:21:34 +02:00
|
|
|
arbI2C_I2C2_Write_Array(dev_addr, reg, buf, 4);
|
2026-10-11 19:29:03 +03:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 14:49:09 +02:00
|
|
|
static void bdelay(int delay)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < delay * 10000; i++)
|
|
|
|
|
{
|
|
|
|
|
asm volatile ("nop");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 19:29:03 +03:00
|
|
|
/**
|
|
|
|
|
* Initialize the PCA9685 chip.
|
|
|
|
|
* Setup frequency and enable auto-increment mode.
|
|
|
|
|
*/
|
2026-11-10 19:44:34 +02:00
|
|
|
bool PCA9685_Init(uint8_t dev_addr)
|
2026-10-11 19:29:03 +03:00
|
|
|
{
|
2026-11-10 19:44:34 +02:00
|
|
|
if (pca_initialized[(dev_addr - PCA9685_ADDR_1) >> 1])
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* 1. Reset device (Wake up) */
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write(dev_addr, MODE1, 0x00);
|
2026-11-18 14:49:09 +02:00
|
|
|
//HAL_Delay(10);
|
|
|
|
|
bdelay(10);
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* 2. Setup PWM Frequency ~1000Hz */
|
|
|
|
|
/* Formula: prescale = 25MHz / (4096 * update_rate) - 1 */
|
|
|
|
|
/* For 1000Hz: 25000000 / (4096 * 1000) - 1 ~= 5 */
|
2026-11-18 14:49:09 +02:00
|
|
|
uint8_t prescale = 3;
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* To change pre-scaler, we must be in SLEEP mode */
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write(dev_addr, MODE1, 0x10); // Sleep mode
|
2026-11-18 14:49:09 +02:00
|
|
|
//HAL_Delay(1);
|
|
|
|
|
bdelay(1);
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write(dev_addr, PRESCALE, prescale); // Write pre-scaler
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* 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
|
|
|
|
|
*/
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write(dev_addr, MODE1, 0xA1);
|
2026-11-18 14:49:09 +02:00
|
|
|
//HAL_Delay(5);
|
|
|
|
|
bdelay(5);
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 19:44:34 +02:00
|
|
|
pca_initialized[(dev_addr - PCA9685_ADDR_1) >> 1] = true;
|
|
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
return true;
|
2026-10-11 19:29:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set PWM duty cycle for a specific channel.
|
|
|
|
|
* @param channel: 0 to 15
|
|
|
|
|
* @param on: Step number to turn ON (usually 0)
|
|
|
|
|
* @param off: Step number to turn OFF (0 to 4095) - This acts as Duty Cycle
|
|
|
|
|
*/
|
2026-11-10 19:44:34 +02:00
|
|
|
void PCA9685_SetPWM(uint8_t dev_addr, uint8_t channel, uint16_t on, uint16_t off)
|
2026-10-11 19:29:03 +03:00
|
|
|
{
|
2026-11-10 17:19:00 +02:00
|
|
|
/* Safety check */
|
|
|
|
|
if (channel > 15)
|
|
|
|
|
return;
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* 12-bit limit check (4095 is max) */
|
|
|
|
|
if (off > 4095)
|
|
|
|
|
off = 4095;
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
/* Calculate register address for this channel */
|
|
|
|
|
/* Each channel has 4 registers, starting at LED0_ON_L (0x06) */
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, on, off);
|
2026-10-11 19:29:03 +03:00
|
|
|
}
|
|
|
|
|
|
2026-11-10 19:44:34 +02:00
|
|
|
void PCA9685_SetPin(uint8_t dev_addr, uint8_t channel, uint8_t val)
|
2026-10-11 19:29:03 +03:00
|
|
|
{
|
2026-11-10 17:19:00 +02:00
|
|
|
if (channel > 15) return;
|
2026-10-11 19:29:03 +03:00
|
|
|
|
2026-11-10 17:19:00 +02:00
|
|
|
if (val) {
|
|
|
|
|
// Full ON: Bit 4 в ON_H 1 - 4096, OFF в 0
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, 0x1000, 0x0000);
|
2026-11-10 17:19:00 +02:00
|
|
|
} else {
|
|
|
|
|
// Full OFF: ON в 0, Bit 4 в OFF_H 1 - 4096
|
2026-11-10 19:44:34 +02:00
|
|
|
pca9685_write4(dev_addr, LED0_ON_L + 4 * channel, 0x0000, 0x1000);
|
2026-11-10 17:19:00 +02:00
|
|
|
}
|
2026-10-11 19:29:03 +03:00
|
|
|
}
|