esp-open-rtos/extras/pca9685/pca9685.h

139 lines
3.3 KiB
C
Raw Normal View History

2016-12-19 00:53:07 +00:00
/**
* Driver for 16-channel, 12-bit PWM PCA9685
*
* Part of esp-open-rtos
* Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#ifndef _EXTRAS_PCA9685_H_
#define _EXTRAS_PCA9685_H_
#include <stdint.h>
#include <stdbool.h>
2017-09-01 09:29:32 +00:00
#include <i2c/i2c.h>
2016-12-19 00:53:07 +00:00
#ifdef __cplusplus
extern "C"
{
#endif
#define PCA9685_ADDR_BASE 0x40
/**
* Init device
* @param addr Device address
*/
2017-09-01 09:29:32 +00:00
void pca9685_init(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Setup device subaddress (see section 7.3.6 if the datasheet)
* @param addr Device address
* @param num Subaddress number, 0..2
* @param subaddr Subaddress, 7 bit
* @param enable True to enable subaddress, false to disable
* @return False if error occured
*/
2017-09-01 09:29:32 +00:00
bool pca9685_set_subaddr(i2c_dev_t* dev, uint8_t num, uint8_t subaddr, bool enable);
2016-12-19 00:53:07 +00:00
/**
* Restart device (see section 7.3.1.1 of the datasheet)
* @param addr Device address
*/
2017-09-01 09:29:32 +00:00
void pca9685_restart(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Check if device is in sleep mode
* @param addr Device address
* @return True if device is sleeping
*/
2017-09-01 09:29:32 +00:00
bool pca9685_is_sleeping(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Switch device to low-power mode or wake it up.
* @param addr Device address
* @param sleep True for sleep mode, false for wake up
*/
2017-09-01 09:29:32 +00:00
void pca9685_sleep(i2c_dev_t* dev, bool sleep);
2016-12-19 00:53:07 +00:00
/**
* Get logic inversion of the outputs
* @param addr Device address
* @return True if outputs are inverted, false otherwise
*/
2017-09-01 09:29:32 +00:00
bool pca9685_is_output_inverted(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Logically invert outputs (see section 7.7 of the datasheet)
* @param addr Device address
* @param inverted True for inverted outputs
*/
2017-09-01 09:29:32 +00:00
void pca9685_set_output_inverted(i2c_dev_t* dev, bool inverted);
2016-12-19 00:53:07 +00:00
/**
* Get outputs mode
* @param addr Device address
* @return True if outputs are in open drain mode
*/
2017-09-01 09:29:32 +00:00
bool pca9685_get_output_open_drain(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Set outputs mode
* @param addr Device address
* @param open_drain True to set open drain mode, false to normal mode
*/
2017-09-01 09:29:32 +00:00
void pca9685_set_output_open_drain(i2c_dev_t* dev, bool open_drain);
2016-12-19 00:53:07 +00:00
/**
* Get current PWM frequency prescaler.
* @param addr Device address
* @return Frequency prescaler
*/
2017-09-01 09:29:32 +00:00
uint8_t pca9685_get_prescaler(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Set PWM frequency prescaler.
* @param addr Device address
* @param prescaler Prescaler value
* @return False if error occured
*/
2017-09-01 09:29:32 +00:00
bool pca9685_set_prescaler(i2c_dev_t* dev, uint8_t prescaler);
2016-12-19 00:53:07 +00:00
/**
* Get current PWM frequency
* @param addr Device address
* @return PWM frequency, Hz
*/
2017-09-01 09:29:32 +00:00
uint16_t pca9685_get_pwm_frequency(i2c_dev_t* dev);
2016-12-19 00:53:07 +00:00
/**
* Set PWM frequency
* @param addr Device address
* @param freq PWM frequency, Hz
* @return False if error occured
*/
2017-09-01 09:29:32 +00:00
bool pca9685_set_pwm_frequency(i2c_dev_t* dev, uint16_t freq);
2016-12-19 00:53:07 +00:00
/**
* Set PWM value on output channel
* @param addr Device address
* @param channel Channel number, 0..15 or >15 for all channels
* @param val PWM value, 0..4096
*/
2017-09-01 09:29:32 +00:00
void pca9685_set_pwm_value(i2c_dev_t* dev, uint8_t channel, uint16_t val);
2016-12-19 00:53:07 +00:00
/**
* Set PWM values on output channels
* @param addr Device address
* @param first_ch First channel, 0..15
* @param channels Number of updating channels
* @param values Array of the channel values, each 0..4096
* @return False if error occured
*/
2017-09-01 09:29:32 +00:00
bool pca9685_set_pwm_values(i2c_dev_t* dev, uint8_t first_ch, uint8_t channels, const uint16_t *values);
2016-12-19 00:53:07 +00:00
#ifdef __cplusplus
}
#endif
#endif /* _EXTRAS_PCA9685_H_ */