Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion boards/common/native/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_qdec

# Put other features for this board (in alphabetical order)
FEATURES_PROVIDED += motor_driver
FEATURES_PROVIDED += netif_ethernet
58 changes: 0 additions & 58 deletions boards/common/native/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,62 +142,6 @@ void _native_LED_RED_TOGGLE(void);
/** @} */
#endif

#if MODULE_PERIPH_QDEC
/**
* @brief Simulate QDEC on motor_set() calls
*
* @param[in] motor_driver motor driver to which motor is attached
* @param[in] motor_id motor ID on driver
* @param[in] pwm_duty_cycle Signed PWM duty_cycle to set motor speed and direction
*/
void native_motor_driver_qdec_simulation( \
const motor_driver_t motor_driver, uint8_t motor_id, \
int32_t pwm_duty_cycle);

/* C++ standard do not support designated initializers */
#if !(defined __cplusplus) && (defined MODULE_PERIPH_QDEC)

/**
* @name Describe DC motors with PWM channel and GPIOs
* @{
*/
static const motor_driver_config_t motor_driver_config[] = {
{
.pwm_dev = 0,
.mode = MOTOR_DRIVER_1_DIR_BRAKE,
.mode_brake = MOTOR_BRAKE_LOW,
.pwm_mode = PWM_LEFT,
.pwm_frequency = 20000U,
.pwm_resolution = 1000U,
.nb_motors = 2,
.motors = {
{
.pwm_channel = 0,
.gpio_enable = GPIO_PIN(0, 0),
.gpio_dir0 = GPIO_PIN(0, 0),
.gpio_dir1_or_brake = GPIO_PIN(0, 0),
.gpio_dir_reverse = 0,
.gpio_enable_invert = 0,
.gpio_brake_invert = 0,
},
{
.pwm_channel = 1,
.gpio_enable = GPIO_PIN(0, 0),
.gpio_dir0 = GPIO_PIN(0, 0),
.gpio_dir1_or_brake = GPIO_PIN(0, 0),
.gpio_dir_reverse = 1,
.gpio_enable_invert = 0,
.gpio_brake_invert = 0,
},
},
.cb = native_motor_driver_qdec_simulation,
},
};

#define MOTOR_DRIVER_NUMOF ARRAY_SIZE(motor_driver_config)
/** @} */
#endif

/**
* @name ztimer configuration
* @{
Expand All @@ -208,8 +152,6 @@ static const motor_driver_config_t motor_driver_config[] = {
#define CONFIG_ZTIMER_USEC_MIN (64)
/** @} */

#endif /* __cplusplus */

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 0 additions & 32 deletions boards/common/nucleo64/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "board_nucleo.h"
#include "arduino_pinmap.h"
#include "motor_driver.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -54,37 +53,6 @@ extern "C" {
#endif
/** @} */

/**
* @name Describe DC motors with PWM channel and GPIOs
* @{
*/
static const motor_driver_config_t motor_driver_config[] = {
{
.pwm_dev = 1,
.mode = MOTOR_DRIVER_1_DIR,
.mode_brake = MOTOR_BRAKE_HIGH,
.pwm_mode = PWM_LEFT,
.pwm_frequency = 20000U,
.pwm_resolution = 2250U,
.nb_motors = 1,
.motors = {
{
.pwm_channel = 0,
.gpio_enable = 0,
.gpio_dir0 = ARDUINO_PIN_15,
.gpio_dir1_or_brake = 0,
.gpio_dir_reverse = 0,
.gpio_enable_invert = 0,
.gpio_brake_invert = 0,
},
},
.cb = NULL,
},
};

#define MOTOR_DRIVER_NUMOF ARRAY_SIZE(motor_driver_config)
/** @} */

/**
* @name Describe MRF24J40 radio
* @{
Expand Down
39 changes: 25 additions & 14 deletions drivers/include/motor_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extern "C" {
* @brief Maximum number of motors by motor driver
*/
#ifndef CONFIG_MOTOR_DRIVER_MAX
#define CONFIG_MOTOR_DRIVER_MAX (2)
# define CONFIG_MOTOR_DRIVER_MAX (2)
#endif
/** @} */

Expand Down Expand Up @@ -154,17 +154,22 @@ typedef struct {
*/
typedef unsigned int motor_driver_t;

/**
* @brief Forward declaration of the motor_driver_config struct.
*/
typedef struct motor_driver_config motor_driver_config_t;

/**
* @brief Motor callback. It is called at end of motor_set()
*/
typedef void (*motor_driver_cb_t)(const motor_driver_t motor_driver,
typedef void (*motor_driver_cb_t)(const motor_driver_config_t *motor_driver_conf,
uint8_t motor_id,
int32_t pwm_duty_cycle);

/**
* @brief Describe DC motor driver with PWM device and motors array
*/
typedef struct {
struct motor_driver_config {
pwm_t pwm_dev; /**< PWM device driving motors */
motor_driver_mode_t mode; /**< driver mode */
motor_driver_mode_brake_t mode_brake; /**< driver brake mode */
Expand All @@ -174,57 +179,63 @@ typedef struct {
uint8_t nb_motors; /**< number of moros */
motor_t motors[CONFIG_MOTOR_DRIVER_MAX]; /**< motors array */
motor_driver_cb_t cb; /**< callback on motor_set */
} motor_driver_config_t;
};

/**
* @brief Initialize DC motor driver board
*
* @param[out] motor_driver motor driver to initialize
* @param[in] motor_driver_conf board-dependent hardware configuration of the
* motor driver to which the motor is attached
*
* @return 0 on success
* @return -1 on error with errno set
*/
int motor_driver_init(const motor_driver_t motor_driver);
int motor_driver_init(const motor_driver_config_t *motor_driver_conf);

/**
* @brief Set motor speed and direction
*
* @param[in] motor_driver motor driver to which motor is attached
* @param[in] motor_driver_conf board-dependent hardware configuration of the
* motor driver to which the motor is attached
* @param[in] motor_id motor ID on driver
* @param[in] pwm_duty_cycle Signed PWM duty_cycle to set motor speed and direction
*
* @return 0 on success
* @return -1 on error with errno set
*/
int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \
int motor_set(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id, \
int32_t pwm_duty_cycle);

/**
* @brief Brake the motor of a given motor driver
*
* @param[in] motor_driver motor driver to which motor is attached
* @param[in] motor_driver_conf board-dependent hardware configuration of the
* motor driver to which the motor is attached
* @param[in] motor_id motor ID on driver
*
* @return 0 on success
* @return -1 on error with errno set
*/
int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id);
int motor_brake(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id);

/**
* @brief Enable a motor of a given motor driver
*
* @param[in] motor_driver motor driver to which motor is attached
* @param[in] motor_driver_conf board-dependent hardware configuration of the motor driver to
* which the motor is attached
* @param[in] motor_id motor ID on driver
*/
void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id);
void motor_enable(const motor_driver_config_t *motor_driver_conf, \
uint8_t motor_id);

/**
* @brief Disable a motor of a given motor driver
*
* @param[in] motor_driver motor driver to which motor is attached
* @param[in] motor_driver_conf board-dependent hardware configuration of the
* motor driver to which the motor is attached
* @param[in] motor_id motor ID on driver
*/
void motor_disable(const motor_driver_t motor_driver, uint8_t motor_id);
void motor_disable(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id);

#ifdef __cplusplus
}
Expand Down
40 changes: 8 additions & 32 deletions drivers/motor_driver/motor_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@
#define ENABLE_DEBUG 0
#include <debug.h>

int motor_driver_init(motor_driver_t motor_driver)
int motor_driver_init(const motor_driver_config_t* motor_driver_conf)
{
int err = 0;

assert(motor_driver < MOTOR_DRIVER_NUMOF);

const motor_driver_config_t *motor_driver_conf = \
&motor_driver_config[motor_driver];

pwm_t pwm_dev = motor_driver_conf->pwm_dev;
pwm_mode_t mode = motor_driver_conf->pwm_mode;
uint32_t freq = motor_driver_conf->pwm_frequency;
Expand Down Expand Up @@ -72,7 +67,7 @@ int motor_driver_init(motor_driver_t motor_driver)
LOG_ERROR("gpio_enable init failed\n");
goto motor_init_err;
}
motor_enable(motor_driver, i);
motor_enable(motor_driver_conf, i);
}
}

Expand All @@ -82,16 +77,12 @@ int motor_driver_init(motor_driver_t motor_driver)
return -err;
}

int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \
int motor_set(const motor_driver_config_t *motor_driver_conf, \
uint8_t motor_id, \
int32_t pwm_duty_cycle)
{
int err = 0;

assert(motor_driver < MOTOR_DRIVER_NUMOF);

const motor_driver_config_t *motor_driver_conf =
&motor_driver_config[motor_driver];

assert(motor_id < motor_driver_conf->nb_motors);

const motor_t *dev = &motor_driver_conf->motors[motor_id];
Expand Down Expand Up @@ -177,7 +168,7 @@ int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \

motor_driver_cb_t cb = motor_driver_conf->cb;
if (cb) {
cb(motor_driver, motor_id, pwm_duty_cycle);
cb(motor_driver_conf, motor_id, pwm_duty_cycle);
}

return 0;
Expand All @@ -186,15 +177,10 @@ int motor_set(const motor_driver_t motor_driver, uint8_t motor_id, \
return -err;
}

int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id)
int motor_brake(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id)
{
int err = 0;

assert(motor_driver < MOTOR_DRIVER_NUMOF);

const motor_driver_config_t *motor_driver_conf =
&motor_driver_config[motor_driver];

assert(motor_id < motor_driver_conf->nb_motors);

const motor_t *dev = &motor_driver_conf->motors[motor_id];
Expand Down Expand Up @@ -245,13 +231,8 @@ int motor_brake(const motor_driver_t motor_driver, uint8_t motor_id)
return -err;
}

void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id)
void motor_enable(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id)
{
assert(motor_driver < MOTOR_DRIVER_NUMOF);

const motor_driver_config_t *motor_driver_conf =
&motor_driver_config[motor_driver];

assert(motor_id < motor_driver_conf->nb_motors);

const motor_t *dev = &motor_driver_conf->motors[motor_id];
Expand All @@ -261,13 +242,8 @@ void motor_enable(const motor_driver_t motor_driver, uint8_t motor_id)
gpio_write(dev->gpio_enable, 1 ^ dev->gpio_enable_invert);
}

void motor_disable(const motor_driver_t motor_driver, uint8_t motor_id)
void motor_disable(const motor_driver_config_t *motor_driver_conf, uint8_t motor_id)
{
assert(motor_driver < MOTOR_DRIVER_NUMOF);

const motor_driver_config_t *motor_driver_conf =
&motor_driver_config[motor_driver];

assert(motor_id < motor_driver_conf->nb_motors);

const motor_t *dev = &motor_driver_conf->motors[motor_id];
Expand Down
4 changes: 4 additions & 0 deletions tests/drivers/motor_driver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ USEMODULE += motor_driver
USEMODULE += shell_cmds_default
USEMODULE += xtimer

ifneq (,$(filter native, $(BOARD)))
FEATURES_PROVIDED += motor_driver
endif

FEATURES_REQUIRED += periph_qdec
FEATURES_REQUIRED += motor_driver

Expand Down
Loading
Loading