pca9685.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Gunar Schorcht
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
84 #ifndef PCA9685_H
85 #define PCA9685_H
86 
87 #ifdef __cplusplus
88 extern "C"
89 {
90 #endif
91 
92 #include <stdbool.h>
93 #include <stdint.h>
94 
95 #include "periph/gpio.h"
96 #include "periph/i2c.h"
97 #include "periph/pwm.h"
98 
115 #define PCA9685_I2C_BASE_ADDR (0x40)
116 #define PCA9685_I2C_ALLCALLADDR (0x70)
117 #define PCA9685_I2C_SUBADR1 (0x71)
118 #define PCA9685_I2C_SUBADR2 (0x72)
119 #define PCA9685_I2C_SUBADR3 (0x73)
125 #define PCA9685_CHANNEL_NUM (16U)
126 
130 #define PCA9685_RESOLUTION (1 << 12)
131 
135 #define PCA9685_OSC_FREQ (25000000)
136 
140 #define PCA9685_EXT_FERQ_MAX (50000000)
141 
145 typedef enum {
149 
155 typedef enum {
159 
166 typedef enum {
174 
178 typedef struct {
179 
181  uint8_t i2c_addr;
185  uint32_t freq;
186  uint16_t res;
188  bool inv;
190  uint32_t ext_freq;
191  gpio_t oe_pin;
197 
201 typedef struct {
202 
204  bool powered_on;
206 } pca9685_t;
207 
208 #if MODULE_SAUL || DOXYGEN
209 
212 typedef struct {
213  const char *name;
214  unsigned int dev;
215  uint8_t channel;
216  uint16_t initial;
218 #endif
219 
234 int pca9685_init(pca9685_t *dev, const pca9685_params_t *params);
235 
259 uint32_t pca9685_pwm_init(pca9685_t *dev, pwm_mode_t mode, uint32_t freq,
260  uint16_t res);
261 
277 void pca9685_pwm_set(pca9685_t *dev, uint8_t channel, uint16_t value);
278 
290 void pca9685_pwm_poweron(pca9685_t *dev);
291 
305 
311 static inline uint8_t pca9685_pwm_channels(pca9685_t *dev)
312 {
313  (void)dev;
314  return PCA9685_CHANNEL_NUM;
315 }
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
321 #endif /* PCA9685_H */
322 
pca9685_out_drv_t
pca9685_out_drv_t
PCA9685 output driver mode.
Definition: pca9685.h:155
PCA9685_OUT_DRV
@ PCA9685_OUT_DRV
Outputs depend on the output driver mode pca9685_params_t::out_drv.
Definition: pca9685.h:168
pca9685_t
PCA9685 PWM device data structure type.
Definition: pca9685.h:201
pca9685_saul_pwm_params_t::channel
uint8_t channel
channel of the PCA9685 device
Definition: pca9685.h:215
pca9685_params_t::i2c_dev
i2c_t i2c_dev
I2C device, default I2C_DEV(0)
Definition: pca9685.h:180
pca9685_pwm_poweroff
void pca9685_pwm_poweroff(pca9685_t *dev)
Stop the PWM generation on the given PCA9685 device.
pca9685_t::powered_on
bool powered_on
Devices is powered on if true.
Definition: pca9685.h:204
pca9685_saul_pwm_params_t::dev
unsigned int dev
index of the PCA9685 device
Definition: pca9685.h:214
pca9685_saul_pwm_params_t
PCA9685 configuration structure for mapping PWM channels to SAUL.
Definition: pca9685.h:212
pca9685_init
int pca9685_init(pca9685_t *dev, const pca9685_params_t *params)
Initialize the PCA9685 PWM device driver.
PCA9685_TOTEM_POLE
@ PCA9685_TOTEM_POLE
Totem pole structure output.
Definition: pca9685.h:157
pca9685_params_t::inv
bool inv
Invert outputs, e.g., for LEDs (default yes)
Definition: pca9685.h:188
i2c_t
unsigned int i2c_t
Default i2c_t type definition.
Definition: i2c.h:151
pca9685_params_t::oe_pin
gpio_t oe_pin
Active LOW output enable pin /OE.
Definition: pca9685.h:191
PCA9685_OPEN_DRAIN
@ PCA9685_OPEN_DRAIN
Open-drain structure output.
Definition: pca9685.h:156
pca9685_error_t
pca9685_error_t
PCA9685 driver error codes.
Definition: pca9685.h:145
pca9685_params_t::res
uint16_t res
PWM resolution (default 4096)
Definition: pca9685.h:186
pca9685_saul_pwm_params_t::initial
uint16_t initial
initial duty-cycle value
Definition: pca9685.h:216
pca9685_params_t::ext_freq
uint32_t ext_freq
If not 0, EXTCLK pin is used with this frequency.
Definition: pca9685.h:190
PCA9685_OFF
@ PCA9685_OFF
If /OE pin is HIGH, outputs are LOW.
Definition: pca9685.h:167
pca9685_params_t::i2c_addr
uint8_t i2c_addr
I2C slave address.
Definition: pca9685.h:181
pca9685_params_t
PCA9685 device initialization parameters.
Definition: pca9685.h:178
pca9685_params_t::mode
pwm_mode_t mode
PWM mode for all channels: PWM_LEFT, PWM_CENTER, PWM_RIGHT supported, (default PWM_CENTER)
Definition: pca9685.h:183
PCA9685_ERROR_I2C
@ PCA9685_ERROR_I2C
I2C communication error.
Definition: pca9685.h:147
pca9685_out_ne_t
pca9685_out_ne_t
PCA9685 output-not-enabled mode.
Definition: pca9685.h:166
PCA9685_OK
@ PCA9685_OK
Success.
Definition: pca9685.h:146
pca9685_saul_pwm_params_t::name
const char * name
name of the PCA9685 device
Definition: pca9685.h:213
PCA9685_CHANNEL_NUM
#define PCA9685_CHANNEL_NUM
Number of PWM channels provided by PCA9685.
Definition: pca9685.h:125
pca9685_pwm_set
void pca9685_pwm_set(pca9685_t *dev, uint8_t channel, uint16_t value)
Set the duty-cycle for a given channel or all channels of the given PCA9685 PWM device.
PCA9685_HIHGH_Z
@ PCA9685_HIHGH_Z
If /OE pin is HIGH, outputs are high-impedance.
Definition: pca9685.h:172
gpio.h
Low-level GPIO peripheral driver interface definitions.
pca9685_params_t::out_drv
pca9685_out_drv_t out_drv
Output driver mode.
Definition: pca9685.h:193
pca9685_params_t::out_ne
pca9685_out_ne_t out_ne
Output-not-enabled mode.
Definition: pca9685.h:194
pwm.h
Low-level PWM peripheral driver interface definitions.
pca9685_pwm_init
uint32_t pca9685_pwm_init(pca9685_t *dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
Initialize the PCA9685 PWM device.
pca9685_t::params
pca9685_params_t params
Device initialization parameters.
Definition: pca9685.h:203
pca9685_pwm_channels
static uint8_t pca9685_pwm_channels(pca9685_t *dev)
Get the number of available channels of the given PCA9685 device.
Definition: pca9685.h:311
pwm_mode_t
pwm_mode_t
Definition: periph_cpu.h:159
i2c.h
Low-level I2C peripheral driver interface definition.
pca9685_params_t::freq
uint32_t freq
PWM frequency in Hz (default 100)
Definition: pca9685.h:185
pca9685_pwm_poweron
void pca9685_pwm_poweron(pca9685_t *dev)
Resume PWM generation on the given PCA9685 device.