diff --git a/boards/arm/stm32/common/include/stm32_drv8266.h b/boards/arm/stm32/common/include/stm32_drv8266.h new file mode 100644 index 00000000000..93622d19cb3 --- /dev/null +++ b/boards/arm/stm32/common/include/stm32_drv8266.h @@ -0,0 +1,73 @@ +/**************************************************************************** + * boards/arm/stm32/common/include/stm32_drv8266.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DRV8825_H +#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DRV8825_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_drv8825_initialize + * + * Description: + * Initialize drv8825 and register the stepper motor driver. + * + ****************************************************************************/ + +int board_drv8825_initialize(int devno); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_DRV8825_H */ \ No newline at end of file diff --git a/boards/arm/stm32/common/src/CMakeLists.txt b/boards/arm/stm32/common/src/CMakeLists.txt index 8557119f0ea..815241a3b5d 100644 --- a/boards/arm/stm32/common/src/CMakeLists.txt +++ b/boards/arm/stm32/common/src/CMakeLists.txt @@ -132,4 +132,8 @@ if(CONFIG_BOARD_STM32_IHM16M1) list(APPEND SRCS stm32_ihm16m1.c) endif() +if(CONFIG_STEPPER_DRV8825) + list(APPEND SRCS stm32_drv8825.c) +endif() + target_sources(board PRIVATE ${SRCS}) diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs index d99d07f0620..271e8a2bdb2 100644 --- a/boards/arm/stm32/common/src/Make.defs +++ b/boards/arm/stm32/common/src/Make.defs @@ -140,6 +140,10 @@ ifeq ($(CONFIG_BOARD_STM32_IHM16M1),y) CSRCS += stm32_ihm16m1.c endif +ifeq ($(CONFIG_STEPPER_DRV8825),y) + CSRCS += stm32_drv8825.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src diff --git a/boards/arm/stm32/common/src/stm32_drv8825.c b/boards/arm/stm32/common/src/stm32_drv8825.c new file mode 100644 index 00000000000..28fe8f402c2 --- /dev/null +++ b/boards/arm/stm32/common/src/stm32_drv8825.c @@ -0,0 +1,198 @@ +/**************************************************************************** + * boards/arm/stm32/common/src/stm32_drv8825.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "stm32.h" +#include "chip.h" +#include "arm_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void drv8825_initialize(void); +static void drv8825_step(int level); +static void drv8825_direction(int level); +static void drv8825_microstepping(int ms1, int ms2, int ms3); +static void drv8825_enable(int level); +static void drv8825_idle(int level); +static int drv8825_fault(void); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct drv8825_ops_s g_drv8825_ops = +{ + drv8825_initialize, /* initialize */ + drv8825_step, /* step */ + drv8825_direction, /* direction */ + drv8825_microstepping, /* microstepping */ + drv8825_enable, /* enable */ + drv8825_idle, /* idle */ + drv8825_fault /* fault */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: drv8825_initialize + * + * Description: + * Initialize and configure the GPIOs. + * + ****************************************************************************/ + +static void drv8825_initialize(void) +{ + stm32_configgpio(GPIO_DIR); + stm32_configgpio(GPIO_STEP); + stm32_configgpio(GPIO_SLEEP); + stm32_configgpio(GPIO_M1); + stm32_configgpio(GPIO_M2); + stm32_configgpio(GPIO_M3); + stm32_configgpio(GPIO_RESET); +} + +/**************************************************************************** + * Name: drv8825_step + * + * Description: + * Control the step output. + * + ****************************************************************************/ + +static void drv8825_step(int level) +{ + stm32_gpiowrite(GPIO_STEP, (bool)level); +} + +/**************************************************************************** + * Name: drv8825_direction + * + * Description: + * Control the direction (clockwise or counterclockwise). + * + ****************************************************************************/ + +static void drv8825_direction(int level) +{ + stm32_gpiowrite(GPIO_DIR, (bool)level); +} + +/**************************************************************************** + * Name: drv8825_microstepping + * + * Description: + * Configure the step resolution. + * + ****************************************************************************/ + +static void drv8825_microstepping(int ms1, int ms2, int ms3) +{ + stm32_gpiowrite(GPIO_M1, (bool)ms1); + stm32_gpiowrite(GPIO_M2, (bool)ms2); + stm32_gpiowrite(GPIO_M3, (bool)ms3); +} + +/**************************************************************************** + * Name: drv8825_enable + * + * Description: + * Enable control. + * + ****************************************************************************/ + +static void drv8825_enable(int level) +{ + stm32_gpiowrite(GPIO_RESET, (bool)level); +} + +/**************************************************************************** + * Name: drv8825_idle + * + * Description: + * Idle control. + * + ****************************************************************************/ + +static void drv8825_idle(int level) +{ + stm32_gpiowrite(GPIO_SLEEP, !level); +} + +/**************************************************************************** + * Name: drv8825_fault + * + * Description: + * Fault fetch. + * + ****************************************************************************/ + +static int drv8825_fault(void) +{ + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_drv8825_initialize + * + * Description: + * Initialize drv8825 and register the stepper motor driver. + * + ****************************************************************************/ + +int board_drv8825_initialize(int devno) +{ + int ret; + char devname[15]; + + drv8825_initialize(); + + snprintf(devname, 15, "/dev/stepper%d", devno); + + ret = drv8825_register(devname, &g_drv8825_ops); + + return ret; +} diff --git a/boards/arm/stm32/stm32f401rc-rs485/include/board.h b/boards/arm/stm32/stm32f401rc-rs485/include/board.h index 6ade75f5cb2..d1f0404ca89 100644 --- a/boards/arm/stm32/stm32f401rc-rs485/include/board.h +++ b/boards/arm/stm32/stm32f401rc-rs485/include/board.h @@ -406,4 +406,23 @@ extern "C" #define GPIO_TIM2_CH1IN (GPIO_TIM2_CH1IN_1 | GPIO_PULLUP) #define GPIO_TIM2_CH2IN (GPIO_TIM2_CH2IN_1 | GPIO_PULLUP) +/* Stepper Motor - DRV8266 */ + +#define GPIO_DIR (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTA|GPIO_PIN7) +#define GPIO_STEP (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTC|GPIO_PIN4) +#define GPIO_SLEEP (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTC|GPIO_PIN5) + +#define GPIO_M1 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTB|GPIO_PIN0) +#define GPIO_M2 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTB|GPIO_PIN1) +#define GPIO_M3 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTB|GPIO_PIN2) + +#define GPIO_RESET (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\ + GPIO_OUTPUT_CLEAR|GPIO_PORTB|GPIO_PIN10) + #endif /* __BOARDS_ARM_STM32F401RC_RS485_INCLUDE_BOARD_H */ diff --git a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c index 5695084a3c1..17d51c65b32 100644 --- a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c @@ -63,6 +63,10 @@ #include "stm32_hcsr04.h" #endif +#ifdef CONFIG_STEPPER_DRV8825 +#include "stm32_drv8266.h" +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -257,5 +261,15 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_STEPPER_DRV8825 + /* Configure and initialize the drv8825 driver */ + + ret = board_drv8825_initialize(0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_drv8825_initialize failed: %d\n", ret); + } +#endif + return ret; }