2018-09-21 22:43:58 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_15_pca9685.ino - Support for I2C PCA9685 12bit 16 pin hardware PWM driver on Tasmota
|
2018-09-21 22:43:58 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Andre Thomas and Theo Arends
|
2018-09-21 22:43:58 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_PCA9685
|
2019-11-06 16:48:38 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* PCA9685 - 16-channel 12-bit pwm driver
|
|
|
|
*
|
|
|
|
* I2C Address: 0x40 .. 0x47
|
|
|
|
\*********************************************************************************************/
|
2018-09-21 22:43:58 +01:00
|
|
|
|
|
|
|
#define XDRV_15 15
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_01 1 // See I2CDEVICES.md
|
2018-09-21 22:43:58 +01:00
|
|
|
|
|
|
|
#define PCA9685_REG_MODE1 0x00
|
|
|
|
#define PCA9685_REG_LED0_ON_L 0x06
|
|
|
|
#define PCA9685_REG_PRE_SCALE 0xFE
|
|
|
|
|
2019-11-06 16:48:38 +00:00
|
|
|
#ifndef USE_PCA9685_ADDR
|
|
|
|
#define USE_PCA9685_ADDR 0x40
|
|
|
|
#endif
|
2018-09-30 22:33:14 +01:00
|
|
|
#ifndef USE_PCA9685_FREQ
|
2019-11-06 16:48:38 +00:00
|
|
|
#define USE_PCA9685_FREQ 50
|
2018-09-30 22:33:14 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-23 17:38:17 +00:00
|
|
|
bool pca9685_inverted = false; // invert PWM for open-collector load
|
2019-11-11 13:22:48 +00:00
|
|
|
bool pca9685_detected = false;
|
2018-09-30 22:07:46 +01:00
|
|
|
uint16_t pca9685_freq = USE_PCA9685_FREQ;
|
2018-09-30 23:18:37 +01:00
|
|
|
uint16_t pca9685_pin_pwm_value[16];
|
2018-09-21 22:43:58 +01:00
|
|
|
|
|
|
|
void PCA9685_Detect(void)
|
|
|
|
{
|
2021-10-31 14:13:28 +00:00
|
|
|
if (!I2cSetDevice(USE_PCA9685_ADDR)) { return; }
|
2018-09-21 22:43:58 +01:00
|
|
|
|
|
|
|
uint8_t buffer;
|
|
|
|
if (I2cValidRead8(&buffer, USE_PCA9685_ADDR, PCA9685_REG_MODE1)) {
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, 0x20);
|
|
|
|
if (I2cValidRead8(&buffer, USE_PCA9685_ADDR, PCA9685_REG_MODE1)) {
|
|
|
|
if (0x20 == buffer) {
|
2019-11-11 13:22:48 +00:00
|
|
|
pca9685_detected = true;
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(USE_PCA9685_ADDR, "PCA9685");
|
2018-09-21 22:43:58 +01:00
|
|
|
PCA9685_Reset(); // Reset the controller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCA9685_Reset(void)
|
|
|
|
{
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, 0x80);
|
2018-09-30 22:07:46 +01:00
|
|
|
PCA9685_SetPWMfreq(USE_PCA9685_FREQ);
|
2022-01-23 17:38:17 +00:00
|
|
|
pca9685_inverted = false;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t pin=0;pin<16;pin++) {
|
2022-01-23 17:38:17 +00:00
|
|
|
PCA9685_SetPWM(pin,0,pca9685_inverted);
|
|
|
|
pca9685_pin_pwm_value[pin] = PCA9685_GetPWMvalue(0, pca9685_inverted);
|
2018-09-21 22:43:58 +01:00
|
|
|
}
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"RESET\":\"OK\"}}"));
|
2018-09-21 22:43:58 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 17:38:17 +00:00
|
|
|
uint16_t PCA9685_GetPWMvalue(uint16_t pwm, bool inverted) {
|
|
|
|
uint16_t pwm_val = pwm;
|
|
|
|
if (inverted) {
|
|
|
|
pwm_val = 4096-pwm;
|
|
|
|
}
|
|
|
|
return pwm_val;
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:43:58 +01:00
|
|
|
void PCA9685_SetPWMfreq(double freq) {
|
|
|
|
/*
|
|
|
|
7.3.5 from datasheet
|
|
|
|
prescale value = round(25000000/(4096*freq))-1;
|
|
|
|
*/
|
2018-09-30 22:07:46 +01:00
|
|
|
if (freq > 23 && freq < 1527) {
|
|
|
|
pca9685_freq=freq;
|
|
|
|
} else {
|
|
|
|
pca9685_freq=50;
|
|
|
|
}
|
|
|
|
uint8_t pre_scale_osc = round(25000000/(4096*pca9685_freq))-1;
|
|
|
|
if (1526 == pca9685_freq) pre_scale_osc=0xFF; // force setting for 24hz because rounding causes 1526 to be 254
|
2018-09-21 22:43:58 +01:00
|
|
|
uint8_t current_mode1 = I2cRead8(USE_PCA9685_ADDR, PCA9685_REG_MODE1); // read current value of MODE1 register
|
|
|
|
uint8_t sleep_mode1 = (current_mode1&0x7F) | 0x10; // Determine register value to put PCA to sleep
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, sleep_mode1); // Let's sleep a little
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_PRE_SCALE, pre_scale_osc); // Set the pre-scaler
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, current_mode1 | 0xA0); // Reset MODE1 register to original state and enable auto increment
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCA9685_SetPWM_Reg(uint8_t pin, uint16_t on, uint16_t off) {
|
|
|
|
uint8_t led_reg = PCA9685_REG_LED0_ON_L + 4 * pin;
|
|
|
|
uint32_t led_data = 0;
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, led_reg, on);
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, led_reg+1, (on >> 8));
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, led_reg+2, off);
|
|
|
|
I2cWrite8(USE_PCA9685_ADDR, led_reg+3, (off >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCA9685_SetPWM(uint8_t pin, uint16_t pwm, bool inverted) {
|
2022-01-23 17:38:17 +00:00
|
|
|
uint16_t pwm_val = PCA9685_GetPWMvalue(pwm, inverted);
|
|
|
|
if (4096 == pwm_val) {
|
2018-09-21 22:43:58 +01:00
|
|
|
PCA9685_SetPWM_Reg(pin, 4096, 0); // Special use additional bit causes channel to turn on completely without PWM
|
|
|
|
} else {
|
2022-01-23 17:38:17 +00:00
|
|
|
PCA9685_SetPWM_Reg(pin, 0, pwm_val);
|
2018-09-21 22:43:58 +01:00
|
|
|
}
|
2022-01-23 17:38:17 +00:00
|
|
|
pca9685_pin_pwm_value[pin] = pwm_val;
|
2018-09-21 22:43:58 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
bool PCA9685_Command(void)
|
2018-09-21 22:43:58 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool serviced = true;
|
|
|
|
bool validpin = false;
|
2018-09-21 22:43:58 +01:00
|
|
|
uint8_t paramcount = 0;
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
paramcount=1;
|
|
|
|
} else {
|
|
|
|
serviced = false;
|
|
|
|
return serviced;
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
char argument[XdrvMailbox.data_len];
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t ca=0;ca<XdrvMailbox.data_len;ca++) {
|
2018-09-21 22:43:58 +01:00
|
|
|
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
|
|
|
|
if (',' == XdrvMailbox.data[ca]) { paramcount++; }
|
|
|
|
}
|
|
|
|
UpperCase(XdrvMailbox.data,XdrvMailbox.data);
|
2018-09-30 23:18:37 +01:00
|
|
|
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 1),"RESET")) { PCA9685_Reset(); return serviced; }
|
2018-09-21 22:43:58 +01:00
|
|
|
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 1),"STATUS")) { PCA9685_OutputTelemetry(false); return serviced; }
|
2018-10-01 18:32:55 +01:00
|
|
|
|
2022-01-23 17:38:17 +00:00
|
|
|
if (!strcmp(ArgV(argument, 1),"INVERT")) {
|
|
|
|
if (paramcount > 1) {
|
|
|
|
pca9685_inverted = (1 == atoi(ArgV(argument, 2)));
|
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"INVERT\":%i, \"Result\":\"OK\"}}"), pca9685_inverted?1:0);
|
|
|
|
return serviced;
|
|
|
|
} else { // No parameter was given for invert, so we return current setting
|
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"INVERT\":%i}}"), pca9685_inverted?1:0);
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 1),"PWMF")) {
|
2018-09-21 22:43:58 +01:00
|
|
|
if (paramcount > 1) {
|
2021-01-30 11:48:44 +00:00
|
|
|
uint16_t new_freq = atoi(ArgV(argument, 2));
|
2018-09-21 22:43:58 +01:00
|
|
|
if ((new_freq >= 24) && (new_freq <= 1526)) {
|
|
|
|
PCA9685_SetPWMfreq(new_freq);
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"PWMF\":%i, \"Result\":\"OK\"}}"),new_freq);
|
2018-09-21 22:43:58 +01:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
} else { // No parameter was given for setfreq, so we return current setting
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"PWMF\":%i}}"),pca9685_freq);
|
2018-09-21 22:43:58 +01:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 1),"PWM")) {
|
2018-09-21 22:43:58 +01:00
|
|
|
if (paramcount > 1) {
|
2021-01-30 11:48:44 +00:00
|
|
|
uint8_t pin = atoi(ArgV(argument, 2));
|
2018-09-21 22:43:58 +01:00
|
|
|
if (paramcount > 2) {
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 3), "ON")) {
|
2022-01-23 17:38:17 +00:00
|
|
|
PCA9685_SetPWM(pin, 4096, pca9685_inverted);
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,4096);
|
2018-09-21 22:43:58 +01:00
|
|
|
serviced = true;
|
|
|
|
return serviced;
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
if (!strcmp(ArgV(argument, 3), "OFF")) {
|
2022-01-23 17:38:17 +00:00
|
|
|
PCA9685_SetPWM(pin, 0, pca9685_inverted);
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,0);
|
2018-09-21 22:43:58 +01:00
|
|
|
serviced = true;
|
|
|
|
return serviced;
|
|
|
|
}
|
2021-01-30 11:48:44 +00:00
|
|
|
uint16_t pwm = atoi(ArgV(argument, 3));
|
2022-01-23 18:14:19 +00:00
|
|
|
if ((pin >= 0 && pin <= 15 || pin==61) && (pwm >= 0 && pwm <= 4096)) {
|
2022-01-23 17:38:17 +00:00
|
|
|
PCA9685_SetPWM(pin, pwm, pca9685_inverted);
|
2019-03-23 16:00:59 +00:00
|
|
|
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,pwm);
|
2018-09-21 22:43:58 +01:00
|
|
|
serviced = true;
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2019-11-11 13:22:48 +00:00
|
|
|
void PCA9685_OutputTelemetry(bool telemetry)
|
|
|
|
{
|
2019-09-04 17:06:34 +01:00
|
|
|
ResponseTime_P(PSTR(",\"PCA9685\":{\"PWM_FREQ\":%i,"),pca9685_freq);
|
2022-01-23 17:38:17 +00:00
|
|
|
ResponseAppend_P(PSTR("\"INVERT\":%i,"), pca9685_inverted?1:0);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t pin=0;pin<16;pin++) {
|
2022-01-23 17:38:17 +00:00
|
|
|
uint16_t pwm_val = PCA9685_GetPWMvalue(pca9685_pin_pwm_value[pin], pca9685_inverted); // return logical (possibly inverted) pwm value
|
|
|
|
ResponseAppend_P(PSTR("\"PWM%i\":%i,"),pin,pwm_val);
|
2018-09-30 23:18:37 +01:00
|
|
|
}
|
2019-03-23 16:00:59 +00:00
|
|
|
ResponseAppend_P(PSTR("\"END\":1}}"));
|
2018-10-01 18:32:55 +01:00
|
|
|
if (telemetry) {
|
2019-11-10 16:40:37 +00:00
|
|
|
MqttPublishTeleSensor();
|
2018-10-01 18:32:55 +01:00
|
|
|
}
|
2018-09-30 23:18:37 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xdrv15(uint32_t function)
|
2018-09-21 22:43:58 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_01)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-09-21 22:43:58 +01:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
PCA9685_Detect();
|
|
|
|
}
|
|
|
|
else if (pca9685_detected) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
2020-10-29 12:37:09 +00:00
|
|
|
if (TasmotaGlobal.tele_period == 0) {
|
2019-11-11 16:32:44 +00:00
|
|
|
PCA9685_OutputTelemetry(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND_DRIVER:
|
|
|
|
if (XDRV_15 == XdrvMailbox.index) {
|
|
|
|
result = PCA9685_Command();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-21 22:43:58 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PCA9685
|
|
|
|
#endif // USE_IC2
|