Change to new timer definitions
[inav.git] / src / main / io / pwmdriver_i2c.c
blob2fda679d41a9d05bfcc7a9c92ab7521f14a40998
1 #include <stdbool.h>
2 #include <stdint.h>
4 #include "drivers/io_pca9685.h"
6 #include "fc/config.h"
7 #include "fc/runtime_config.h"
9 #include "config/feature.h"
11 #define PWM_DRIVER_IMPLEMENTATION_COUNT 1
12 #define PWM_DRIVER_MAX_CYCLE 4
14 static bool driverEnabled = false;
15 static uint8_t driverImplementationIndex = 0;
17 typedef struct {
18 bool (*initFunction)(void);
19 void (*writeFunction)(uint8_t servoIndex, uint16_t off);
20 void (*setFrequencyFunction)(uint16_t freq);
21 void (*syncFunction)(uint8_t cycleIndex);
22 } pwmDriverDriver_t;
24 pwmDriverDriver_t pwmDrivers[PWM_DRIVER_IMPLEMENTATION_COUNT] = {
25 [0] = {
26 .initFunction = pca9685Initialize,
27 .writeFunction = pca9685setServoPulse,
28 .setFrequencyFunction = pca9685setPWMFreq,
29 .syncFunction = pca9685sync
33 bool isPwmDriverEnabled() {
34 return driverEnabled;
37 void pwmDriverSetPulse(uint8_t servoIndex, uint16_t length) {
38 (pwmDrivers[driverImplementationIndex].writeFunction)(servoIndex, length);
41 void pwmDriverInitialize(void) {
42 driverEnabled = (pwmDrivers[driverImplementationIndex].initFunction)();
44 if (driverEnabled) {
45 ENABLE_STATE(PWM_DRIVER_AVAILABLE);
46 } else {
47 DISABLE_STATE(PWM_DRIVER_AVAILABLE);
52 void pwmDriverSync(void) {
53 static uint8_t cycle = 0;
55 (pwmDrivers[driverImplementationIndex].syncFunction)(cycle);
57 cycle++;
58 if (cycle == PWM_DRIVER_MAX_CYCLE) {
59 cycle = 0;