drivers: stepper: include enable and microstep pins in common config
- Include the enable, m0, and m1 microstep to the common init config. - Refactor drivers that manually init these pins to use the pins from the common config. Signed-off-by: Hong Nguyen <hong.nguyen.k54@gmail.com>
This commit is contained in:
committed by
Fabio Baltieri
parent
346e4d041e
commit
e93279e773
@@ -15,9 +15,6 @@ LOG_MODULE_REGISTER(tmc22xx, CONFIG_STEPPER_LOG_LEVEL);
|
||||
|
||||
struct tmc22xx_config {
|
||||
struct step_dir_stepper_common_config common;
|
||||
const struct gpio_dt_spec enable_pin;
|
||||
struct gpio_dt_spec m0_pin;
|
||||
struct gpio_dt_spec m1_pin;
|
||||
enum stepper_drv_micro_step_resolution *msx_resolutions;
|
||||
};
|
||||
|
||||
@@ -32,7 +29,7 @@ static int tmc22xx_enable(const struct device *dev)
|
||||
const struct tmc22xx_config *config = dev->config;
|
||||
|
||||
LOG_DBG("Enabling Stepper motor controller %s", dev->name);
|
||||
return gpio_pin_set_dt(&config->enable_pin, 1);
|
||||
return gpio_pin_set_dt(&config->common.en_pin, 1);
|
||||
}
|
||||
|
||||
static int tmc22xx_disable(const struct device *dev)
|
||||
@@ -40,7 +37,7 @@ static int tmc22xx_disable(const struct device *dev)
|
||||
const struct tmc22xx_config *config = dev->config;
|
||||
|
||||
LOG_DBG("Disabling Stepper motor controller %s", dev->name);
|
||||
return gpio_pin_set_dt(&config->enable_pin, 0);
|
||||
return gpio_pin_set_dt(&config->common.en_pin, 0);
|
||||
}
|
||||
|
||||
static int tmc22xx_set_micro_step_res(const struct device *dev,
|
||||
@@ -50,7 +47,7 @@ static int tmc22xx_set_micro_step_res(const struct device *dev,
|
||||
const struct tmc22xx_config *config = dev->config;
|
||||
int ret;
|
||||
|
||||
if ((config->m0_pin.port == NULL) || (config->m1_pin.port == NULL)) {
|
||||
if ((config->common.m0_pin.port == NULL) || (config->common.m1_pin.port == NULL)) {
|
||||
LOG_ERR("%s: Failed to set microstep resolution: microstep pins are not defined "
|
||||
"(error: %d)",
|
||||
dev->name, -ENOTSUP);
|
||||
@@ -62,13 +59,13 @@ static int tmc22xx_set_micro_step_res(const struct device *dev,
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = gpio_pin_set_dt(&config->m0_pin, i & 0x01);
|
||||
ret = gpio_pin_set_dt(&config->common.m0_pin, i & 0x01);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set MS1 pin: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = gpio_pin_set_dt(&config->m1_pin, (i & 0x02) >> 1);
|
||||
ret = gpio_pin_set_dt(&config->common.m1_pin, (i & 0x02) >> 1);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to set MS2 pin: %d", ret);
|
||||
return ret;
|
||||
@@ -97,23 +94,23 @@ static int tmc22xx_stepper_configure_msx_pins(const struct device *dev)
|
||||
const struct tmc22xx_config *config = dev->config;
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&config->m0_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.m0_pin)) {
|
||||
LOG_ERR("MS1 pin not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT);
|
||||
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure ms1 pin");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!gpio_is_ready_dt(&config->m1_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.m1_pin)) {
|
||||
LOG_ERR("MS2 pin not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT);
|
||||
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure ms2 pin");
|
||||
return ret;
|
||||
@@ -128,18 +125,18 @@ static int tmc22xx_stepper_init(const struct device *dev)
|
||||
struct tmc22xx_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&config->enable_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.en_pin)) {
|
||||
LOG_ERR("GPIO pins are not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->enable_pin, GPIO_OUTPUT);
|
||||
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure enable pin: %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((config->m0_pin.port != NULL) && (config->m1_pin.port != NULL)) {
|
||||
if ((config->common.m0_pin.port != NULL) && (config->common.m1_pin.port != NULL)) {
|
||||
ret = tmc22xx_stepper_configure_msx_pins(dev);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure MSX pins: %d", ret);
|
||||
@@ -166,10 +163,7 @@ static DEVICE_API(stepper_drv, tmc22xx_stepper_api) = {
|
||||
#define TMC22XX_STEPPER_DEFINE(inst, msx_table) \
|
||||
static const struct tmc22xx_config tmc22xx_config_##inst = { \
|
||||
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
|
||||
.enable_pin = GPIO_DT_SPEC_INST_GET(inst, en_gpios), \
|
||||
.msx_resolutions = msx_table, \
|
||||
.m0_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m0_gpios, {0}), \
|
||||
.m1_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m1_gpios, {0}), \
|
||||
}; \
|
||||
static struct tmc22xx_data tmc22xx_data_##inst = { \
|
||||
.resolution = DT_INST_PROP(inst, micro_step_res), \
|
||||
|
||||
@@ -15,10 +15,7 @@ LOG_MODULE_REGISTER(a4979, CONFIG_STEPPER_LOG_LEVEL);
|
||||
|
||||
struct a4979_config {
|
||||
const struct step_dir_stepper_common_config common;
|
||||
const struct gpio_dt_spec en_pin;
|
||||
const struct gpio_dt_spec reset_pin;
|
||||
const struct gpio_dt_spec m0_pin;
|
||||
const struct gpio_dt_spec m1_pin;
|
||||
};
|
||||
|
||||
struct a4979_data {
|
||||
@@ -51,7 +48,7 @@ static int a4979_set_microstep_pin(const struct device *dev, const struct gpio_d
|
||||
static int a4979_enable(const struct device *dev)
|
||||
{
|
||||
const struct a4979_config *config = dev->config;
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
int ret;
|
||||
|
||||
/* Check availability of enable pin, as it might be hardwired. */
|
||||
@@ -60,7 +57,7 @@ static int a4979_enable(const struct device *dev)
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = gpio_pin_set_dt(&config->en_pin, 1);
|
||||
ret = gpio_pin_set_dt(&config->common.en_pin, 1);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -72,7 +69,7 @@ static int a4979_enable(const struct device *dev)
|
||||
static int a4979_disable(const struct device *dev)
|
||||
{
|
||||
const struct a4979_config *config = dev->config;
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
int ret;
|
||||
|
||||
/* Check availability of enable pin, as it might be hardwired. */
|
||||
@@ -81,7 +78,7 @@ static int a4979_disable(const struct device *dev)
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = gpio_pin_set_dt(&config->en_pin, 0);
|
||||
ret = gpio_pin_set_dt(&config->common.en_pin, 0);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -122,11 +119,11 @@ static int a4979_set_micro_step_res(const struct device *dev,
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
ret = a4979_set_microstep_pin(dev, &config->m0_pin, m0_value);
|
||||
ret = a4979_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = a4979_set_microstep_pin(dev, &config->m1_pin, m1_value);
|
||||
ret = a4979_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
@@ -150,7 +147,7 @@ static int a4979_init(const struct device *dev)
|
||||
const struct a4979_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
bool has_reset_pin = config->reset_pin.port != NULL;
|
||||
|
||||
LOG_DBG("Initializing %s gpios", dev->name);
|
||||
@@ -171,12 +168,12 @@ static int a4979_init(const struct device *dev)
|
||||
|
||||
/* Configure enable pin if it is available */
|
||||
if (has_enable_pin) {
|
||||
if (!gpio_is_ready_dt(&config->en_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.en_pin)) {
|
||||
LOG_ERR("Enable Pin is not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->en_pin, GPIO_OUTPUT_INACTIVE);
|
||||
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure en_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -184,22 +181,22 @@ static int a4979_init(const struct device *dev)
|
||||
}
|
||||
|
||||
/* Configure microstep pin 0 */
|
||||
if (!gpio_is_ready_dt(&config->m0_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.m0_pin)) {
|
||||
LOG_ERR("m0 Pin is not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
|
||||
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Configure microstep pin 1 */
|
||||
if (!gpio_is_ready_dt(&config->m1_pin)) {
|
||||
if (!gpio_is_ready_dt(&config->common.m1_pin)) {
|
||||
LOG_ERR("m1 Pin is not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
|
||||
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -225,10 +222,7 @@ static DEVICE_API(stepper_drv, a4979_stepper_api) = {
|
||||
\
|
||||
static const struct a4979_config a4979_config_##inst = { \
|
||||
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
|
||||
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
|
||||
.reset_pin = GPIO_DT_SPEC_INST_GET_OR(inst, reset_gpios, {0}), \
|
||||
.m0_pin = GPIO_DT_SPEC_INST_GET(inst, m0_gpios), \
|
||||
.m1_pin = GPIO_DT_SPEC_INST_GET(inst, m1_gpios), \
|
||||
}; \
|
||||
\
|
||||
static struct a4979_data a4979_data_##inst = { \
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
* This structure **must** be placed first in the driver's config structure.
|
||||
*/
|
||||
struct step_dir_stepper_common_config {
|
||||
struct gpio_dt_spec en_pin;
|
||||
struct gpio_dt_spec m0_pin;
|
||||
struct gpio_dt_spec m1_pin;
|
||||
uint32_t step_width_ns;
|
||||
bool dual_edge;
|
||||
};
|
||||
@@ -34,6 +37,9 @@ struct step_dir_stepper_common_config {
|
||||
*/
|
||||
#define STEP_DIR_STEPPER_DT_COMMON_CONFIG_INIT(node_id) \
|
||||
{ \
|
||||
.en_pin = GPIO_DT_SPEC_GET_OR(node_id, en_gpios, {0}), \
|
||||
.m0_pin = GPIO_DT_SPEC_GET_OR(node_id, m0_gpios, {0}), \
|
||||
.m1_pin = GPIO_DT_SPEC_GET_OR(node_id, m1_gpios, {0}), \
|
||||
.dual_edge = DT_PROP_OR(node_id, dual_edge_step, false), \
|
||||
.step_width_ns = DT_PROP(node_id, step_width_ns), \
|
||||
}
|
||||
|
||||
@@ -28,9 +28,6 @@ LOG_MODULE_REGISTER(drv84xx, CONFIG_STEPPER_LOG_LEVEL);
|
||||
struct drv84xx_config {
|
||||
struct step_dir_stepper_common_config common;
|
||||
struct gpio_dt_spec sleep_pin;
|
||||
struct gpio_dt_spec en_pin;
|
||||
struct gpio_dt_spec m0_pin;
|
||||
struct gpio_dt_spec m1_pin;
|
||||
struct gpio_dt_spec fault_pin;
|
||||
};
|
||||
|
||||
@@ -105,14 +102,14 @@ int drv84xx_microstep_recovery(const struct device *dev)
|
||||
uint8_t m0_value = data->pin_states.m0;
|
||||
uint8_t m1_value = data->pin_states.m1;
|
||||
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
|
||||
ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to restore microstep configuration (error: %d)", dev->name,
|
||||
ret);
|
||||
@@ -125,7 +122,7 @@ int drv84xx_microstep_recovery(const struct device *dev)
|
||||
static int drv84xx_check_en_sleep_pin(const struct drv84xx_config *config)
|
||||
{
|
||||
bool has_sleep_pin = config->sleep_pin.port != NULL;
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
|
||||
if (!has_sleep_pin && !has_enable_pin) {
|
||||
LOG_ERR("Failed to enable/disable device, neither sleep pin nor enable pin are "
|
||||
@@ -140,11 +137,11 @@ static int drv84xx_set_en_pin_state(const struct device *dev, bool enable)
|
||||
{
|
||||
const struct drv84xx_config *config = dev->config;
|
||||
struct drv84xx_data *data = dev->data;
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
int ret;
|
||||
|
||||
if (has_enable_pin) {
|
||||
ret = gpio_pin_set_dt(&config->en_pin, enable);
|
||||
ret = gpio_pin_set_dt(&config->common.en_pin, enable);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to set en_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -178,7 +175,7 @@ static int drv84xx_enable(const struct device *dev)
|
||||
{
|
||||
const struct drv84xx_config *config = dev->config;
|
||||
struct drv84xx_data *data = dev->data;
|
||||
bool has_enable_pin = config->en_pin.port != NULL;
|
||||
bool has_enable_pin = config->common.en_pin.port != NULL;
|
||||
bool has_sleep_pin = config->sleep_pin.port != NULL;
|
||||
bool has_fault_pin = config->fault_pin.port != NULL;
|
||||
k_timeout_t enable_timeout;
|
||||
@@ -276,7 +273,7 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
|
||||
uint8_t m0_value = 0;
|
||||
uint8_t m1_value = 0;
|
||||
|
||||
if ((config->m0_pin.port == NULL) || (config->m1_pin.port == NULL)) {
|
||||
if ((config->common.m0_pin.port == NULL) || (config->common.m1_pin.port == NULL)) {
|
||||
|
||||
LOG_ERR("%s: Failed to set microstep resolution: microstep pins are not defined "
|
||||
"(error: %d)",
|
||||
@@ -330,12 +327,12 @@ static int drv84xx_set_micro_step_res(const struct device *dev,
|
||||
return -ENOTSUP;
|
||||
};
|
||||
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->m0_pin, m0_value);
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->common.m0_pin, m0_value);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->m1_pin, m1_value);
|
||||
ret = drv84xx_set_microstep_pin(dev, &config->common.m1_pin, m1_value);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
@@ -384,8 +381,8 @@ static int drv84xx_init(const struct device *dev)
|
||||
}
|
||||
|
||||
/* Configure enable pin if it is available */
|
||||
if (config->en_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->en_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (config->common.en_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->common.en_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure en_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -394,8 +391,8 @@ static int drv84xx_init(const struct device *dev)
|
||||
}
|
||||
|
||||
/* Configure microstep pin 0 if it is available */
|
||||
if (config->m0_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->m0_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (config->common.m0_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->common.m0_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure m0_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -404,8 +401,8 @@ static int drv84xx_init(const struct device *dev)
|
||||
}
|
||||
|
||||
/* Configure microstep pin 1 if it is available */
|
||||
if (config->m1_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->m1_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (config->common.m1_pin.port != NULL) {
|
||||
ret = gpio_pin_configure_dt(&config->common.m1_pin, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("%s: Failed to configure m1_pin (error: %d)", dev->name, ret);
|
||||
return ret;
|
||||
@@ -413,7 +410,7 @@ static int drv84xx_init(const struct device *dev)
|
||||
data->pin_states.m1 = 0U;
|
||||
}
|
||||
|
||||
if ((config->m0_pin.port != NULL) && (config->m1_pin.port != NULL)) {
|
||||
if ((config->common.m0_pin.port != NULL) && (config->common.m1_pin.port != NULL)) {
|
||||
ret = drv84xx_set_micro_step_res(dev, data->ustep_res);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
@@ -455,9 +452,6 @@ static DEVICE_API(stepper_drv, drv84xx_stepper_api) = {
|
||||
static const struct drv84xx_config drv84xx_config_##inst = { \
|
||||
.common = STEP_DIR_STEPPER_DT_INST_COMMON_CONFIG_INIT(inst), \
|
||||
.sleep_pin = GPIO_DT_SPEC_INST_GET_OR(inst, sleep_gpios, {0}), \
|
||||
.en_pin = GPIO_DT_SPEC_INST_GET_OR(inst, en_gpios, {0}), \
|
||||
.m0_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m0_gpios, {0}), \
|
||||
.m1_pin = GPIO_DT_SPEC_INST_GET_OR(inst, m1_gpios, {0}), \
|
||||
.fault_pin = GPIO_DT_SPEC_INST_GET_OR(inst, fault_gpios, {0}), \
|
||||
}; \
|
||||
\
|
||||
|
||||
Reference in New Issue
Block a user