Basic DRV8231 driver in C++ compatible with ESP-IDF & PlatformIO

This example class uses the LEDC PWM driver of the ESP-IDF to control a DRV8231 motor driver (you could also use the MCPWM driver) to control a DRV8231 motor driver.

Header (DRV8231.hpp)

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

class DRV8231 {
public:
    DRV8231(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();
private:
    gpio_num_t pwmA;
    gpio_num_t pwmB;
    ledc_channel_t channelA;
    ledc_channel_t channelB;
};

Source (DRV8231.cpp)

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


DRV8231::DRV8231(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 100kHz
        .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 DRV8231::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 DRV8231::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 "DRV8231.hpp"

DRV8231 motor(GPIO_NUM_7, GPIO_NUM_8, LEDC_CHANNEL_0, LEDC_CHANNEL_1);

extern "C" void app_main() {
  while (true) {
    // Initialize the DRV8231 driver
    motor.Start(512);  // Start motor at half speed
    vTaskDelay(pdMS_TO_TICKS(2000));  // Run for 2 seconds
    motor.Stop();  // Stop the motor
  }
}