10 #include <sys/types.h> 18 #define PCA9685_RA_MODE1 0x00 19 #define PCA9685_RA_MODE2 0x01 20 #define PCA9685_RA_LED0_ON_L 0x06 21 #define PCA9685_RA_LED0_ON_H 0x07 22 #define PCA9685_RA_LED0_OFF_L 0x08 23 #define PCA9685_RA_LED0_OFF_H 0x09 24 #define PCA9685_RA_ALL_LED_ON_L 0xFA 25 #define PCA9685_RA_ALL_LED_ON_H 0xFB 26 #define PCA9685_RA_ALL_LED_OFF_L 0xFC 27 #define PCA9685_RA_ALL_LED_OFF_H 0xFD 28 #define PCA9685_RA_PRE_SCALE 0xFE 30 #define PCA9685_MODE1_RESTART_BIT (1 << 7) 31 #define PCA9685_MODE1_EXTCLK_BIT (1 << 6) 32 #define PCA9685_MODE1_AI_BIT (1 << 5) 33 #define PCA9685_MODE1_SLEEP_BIT (1 << 4) 34 #define PCA9685_MODE1_SUB1_BIT (1 << 3) 35 #define PCA9685_MODE1_SUB2_BIT (1 << 2) 36 #define PCA9685_MODE1_SUB3_BIT (1 << 1) 37 #define PCA9685_MODE1_ALLCALL_BIT (1 << 0) 38 #define PCA9685_ALL_LED_OFF_H_SHUT (1 << 4) 39 #define PCA9685_MODE2_INVRT_BIT (1 << 4) 40 #define PCA9685_MODE2_OCH_BIT (1 << 3) 41 #define PCA9685_MODE2_OUTDRV_BIT (1 << 2) 42 #define PCA9685_MODE2_OUTNE1_BIT (1 << 1) 43 #define PCA9685_MODE2_OUTNE0_BIT (1 << 0) 50 #define PCA9685_INTERNAL_CLOCK (1.04f * 25000000.f) 51 #define PCA9685_EXTERNAL_CLOCK 24576000.f 53 using namespace Linux;
55 #define PWM_CHAN_COUNT 16 61 uint8_t channel_offset,
62 int16_t oe_pin_number) :
67 _external_clock(external_clock),
68 _channel_offset(channel_offset),
69 _oe_pin_number(oe_pin_number)
136 uint8_t prescale = ceilf(
_osc_clock / (4096 * freq_hz)) - 1;
208 struct PACKED pwm_values {
213 for (
unsigned ch = min_ch; ch < max_ch; ch++) {
218 length = round((period_us * 4096) / (1000000.
f /
_frequency)) - 1;
221 uint8_t *d = &pwm_values.data[(ch - min_ch) * 4];
224 *d++ = length & 0xFF;
234 size_t payload_size = 1 + (max_ch - min_ch) * 4;
236 _dev->
transfer((uint8_t *)&pwm_values, payload_size,
nullptr, 0);
248 for (
int i = 0; i < len; i++) {
249 period_us[i] =
read(0 + i);
void set_freq(uint32_t chmask, uint16_t freq_hz)
#define PCA9685_ALL_LED_OFF_H_SHUT
void disable_ch(uint8_t ch)
virtual Semaphore * get_semaphore() override=0
#define PCA9685_RA_ALL_LED_OFF_H
virtual bool take(uint32_t timeout_ms) WARN_IF_UNUSED=0
#define PCA9685_MODE1_RESTART_BIT
#define PCA9685_RA_PRE_SCALE
#define PCA9685_MODE1_EXTCLK_BIT
uint16_t _pending_write_mask
AP_HAL::DigitalSource * _enable_pin
virtual void mode(uint8_t output)=0
#define PCA9685_MODE1_SLEEP_BIT
virtual void delay(uint16_t ms)=0
AP_HAL::OwnPtr< AP_HAL::I2CDevice > _dev
virtual bool take_nonblocking() WARN_IF_UNUSED=0
static AP_HAL::OwnPtr< AP_HAL::Device > dev
#define PCA9685_INTERNAL_CLOCK
#define PCA9685_EXTERNAL_CLOCK
static const AP_HAL::HAL & hal
uint16_t get_freq(uint8_t ch)
virtual bool transfer(const uint8_t *send, uint32_t send_len, uint8_t *recv, uint32_t recv_len) override=0
virtual AP_HAL::DigitalSource * channel(uint16_t n)=0
void enable_ch(uint8_t ch)
#define PCA9685_RA_LED0_ON_L
RCOutput_PCA9685(AP_HAL::OwnPtr< AP_HAL::I2CDevice > dev, bool external_clock, uint8_t channel_offset, int16_t oe_pin_number)
uint16_t read(uint8_t ch)
#define PCA9685_MODE1_AI_BIT
uint16_t * _pulses_buffer
void write(uint8_t ch, uint16_t period_us)
void reset_all_channels()
bool write_register(uint8_t reg, uint8_t val, bool checked=false)
#define PCA9685_RA_ALL_LED_ON_L
AP_HAL::Scheduler * scheduler
virtual void write(uint8_t value)=0