ESP-IDF driver for the CP2119/CP3119 motor driver

This driver uses the LEDC peripheral of the ESP32 to control the CP2119/CP3119 motor driver.

CP3119M.hpp

#pragma once
#include <cstdio>
#include <cstdint>
#include <cmath>
#include <driver/gpio.h>
#include <driver/ledc.h>

class CP3119M {
public:
    CP3119M(gpio_num_t pwmA, gpio_num_t pwmB, ledc_channel_t channelA, ledc_channel_t channelB);

    void Start(uint16_t speed, bool reverse=false);
    void Stop();

    /**
     * @brief Sets the speed and direction of the motor.
     *
     * This function adjusts the motor speed based on the provided value. If the speed is zero or invalid (NaN),
     * the motor is stopped. Otherwise, the speed is converted to a duty cycle in the range of 0-1023 and the motor
     * is started with the specified direction.
     *
     * @param speed The desired speed of the motor as a floating-point value. Positive values indicate forward
     *              direction, and negative values indicate reverse direction. The value is scaled to a range
     *              of 0-1023 for PWM control.
     * @param reverse Optional parameter to explicitly set the reverse direction. Defaults to false. If the speed
     *                is negative, the reverse direction is automatically set.
     */
    inline void SetSpeed(float speed, bool reverse=false) {
        if(speed == 0.0f || isnanf(speed)) {
            Stop();
            return;
        }
        // Convert speed to 0-1023 range
        uint16_t duty = static_cast<uint16_t>(fabs(1.0-speed) * 1023.0f);
        Start(duty, speed < 0.0f ? true : false);
    }
private:
    gpio_num_t pwmA;
    gpio_num_t pwmB;
    ledc_channel_t channelA;
    ledc_channel_t channelB;
};

CP3119M.cpp

#include "CP3119M.hpp"
#include <algorithm>


CP3119M::CP3119M(gpio_num_t pwmA, gpio_num_t pwmB, ledc_channel_t channelA, ledc_channel_t channelB) {
    // Store pin numbers
    this->pwmA = pwmA;
    this->pwmB = pwmB;
    this->channelA = channelA;
    this->channelB = channelB;
    
    // Configure LEDC timer for PWM generation
    ledc_timer_config_t ledc_timer = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .duty_resolution = LEDC_TIMER_10_BIT,  // 8-bit resolution (0-255)
        .timer_num = LEDC_TIMER_0,
        .freq_hz = 20000,  // Set frequency to 20kHz
        .clk_cfg = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
    
    // Configure LEDC channel for pwmA
    ledc_channel_config_t ledc_channel_a = {
        .gpio_num = pwmA,
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = channelA,
        .intr_type = LEDC_INTR_DISABLE,
        .timer_sel = LEDC_TIMER_0,
        .duty = 0,  // Initialize with 0 duty cycle
        .hpoint = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_a));
    
    // Configure LEDC channel for pwmB
    ledc_channel_config_t ledc_channel_b = {
        .gpio_num = pwmB,
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = channelB,
        .intr_type = LEDC_INTR_DISABLE,
        .timer_sel = LEDC_TIMER_0,
        .duty = 0,  // Initialize with 0 duty cycle
        .hpoint = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_b));

}

void CP3119M::Start(uint16_t speed, bool reverse) {
    uint16_t duty = std::min(speed, (uint16_t)1023);  // Clamp speed to 0-4095 range
    // Set the direction and duty cycle
    if (reverse) {
        ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, duty);
        ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
        ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, 1023);
        ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
    } else {
        ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, duty);
        ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
        ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, 1023); // 100%
        ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
    }
}

void CP3119M::Stop() {
    // Set both channels to 100% duty cycle (ie break)
    ledc_set_duty(LEDC_LOW_SPEED_MODE, channelA, 1023);
    ledc_update_duty(LEDC_LOW_SPEED_MODE, channelA);
    ledc_set_duty(LEDC_LOW_SPEED_MODE, channelB, 1023);
    ledc_update_duty(LEDC_LOW_SPEED_MODE, channelB);
}

Usage Example

#include "CP3119M.hpp"
#include <driver/gpio.h>

extern "C" void app_main() {
    // Initialize CP3119 motor driver on GPIO 18 and 19 with LEDC channels 0 and 1
    CP3119M motor(gpio_num_t::GPIO_NUM_18, gpio_num_t::GPIO_NUM_19, LEDC_CHANNEL_0, LEDC_CHANNEL_1);
    
    // Start the motor at full speed in forward direction
    motor.SetSpeed(1.0f);
    
    // Run for a while
    vTaskDelay(pdMS_TO_TICKS(5000));
    
    // Stop the motor
    motor.Stop();
    
    return 0;
}