***Question***

*MCU STM32 with stepper motor driver Allegro A4988 are moving a 2 coil, 4 wire, bipolar stepper motor ridiculously slowly, with flickers and stutters.  How to fix?*

---

[![a4988 pic 1][1]][1]

---

[![a4988 pic 3][2]][2]

---

[![a4988 pic 2][3]][3]

---

*The OP's original code (See Appendix A, B, and C below)*

---

***Answer***

*Contents*

*1. The MCU + Driver _ Motor Schematic V0.1* 

*2. The OP's STM32 C++ Test Code Analysis*

---

*1. The MCU + Driver + Motor Schematic v0.1*

---

[![a4988 v0.1][4]][4]

---

*2. The OP's STM32 C++ Test Code Analysis*

I skimmed the OP's three short functions and found them more or less OK, though I did not go step by step in detail to detect any bug. 

I think I better test the A4899 driver and motor independently off line, without using any STM32 C++ code, but just use a NE555 timer to simulate the step pulses, and jumper wired by hand for inputting signals.

---

*3. Offline (by hand without STM32 code) testing A4899 and motor, using an NE555 timer to simulate step pulses*

---

*/ to continue, ...*

---

***References***

*/ to continue, ...*

---

***Appendices***

*Appendix A - The OP's Original Code - main.c*

---

    #include "./headers/stm32f767xx.h"
    #include <stdint.h>
    
    int main(void)
    {
        initMotor(0); // initialise the motor
        initLed(7); // initialise the led
        unsigned long a = 0;
        while (1)
        {
            if (a == 50000)
            {
                toggleLed(7); // this LED flashes a little quicker than twice per second 
                stepMotor(0); // output a pulse to the driver to step the motor, attached to PA2
                a = 0;
            }
            a++;
        }
    }

---

*Appenidx B - The OP's Original Code - ./drivers/led.c*

---

        #include "../headers/stm32f767xx.h"
        
        void initLed(int pin)
        {
            RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable the GPIOB clock
            GPIOB->MODER |= (0x1 << (pin * 2)); // set to output
            GPIOB->OTYPER = 0x00; // push-pull mode
            GPIOB->ODR = 0x00; // set output register to 0 across all pins
        }
        
        void toggleLed(int pin)
        {
            GPIOB->ODR ^= (0x1 << pin); // toggle the pin
        }
    
    ---
    
*Appenidx C - The OP's Original Code - ./drivers/motor.c*
    
    ---
    
    #include "../headers/stm32f767xx.h"
    
    void initMotor(int step_pin)
    {
        RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable the GPIOA clock
        GPIOA->MODER |= (0x1 << (step_pin * 2)); // set to output
        GPIOA->OTYPER = 0x00; // push-pull mode
        GPIOA->PUPDR |= (0x2 << (step_pin * 2)); // pull down the pin specified
        GPIOA->ODR = 0x00; // set output register to 0 across all pins
    }
    
    void stepMotor(int step_pin)
    {
        GPIOA->ODR |= (1 << step_pin); // output to the pin specified
        GPIOA->ODR &= ~(1 << step_pin); // reset the output back to 0
    }

----

Appendix D - Troubleshsooting the OP's A4899 Test Code v0.1

I skimmed the OP's three functions and found them in general more or less OK, although I did not go step by step to detect any bug.  I think I better test the A4899 driver and motor independently off line, without using any STM32 C++ code, but just use a NE555 timer to simulate the step pulses, and jumper wired by hand for inputting signals.   

    1. Main Funtion
    
    1.1 Initialize GPIO pins interfacing motor
    1.2 Initialize the status LED pin
    1.3 Repeatedly (a) Toggle LED pin, (b) Send one step pulse
    
    
    ##############################################################################
    
    # Appendix A - The OP's Original Code - main.c
    
    #include "./headers/stm32f767xx.h"
    #include <stdint.h>
    
    int main(void)
    {
        initMotor(0); // initialise the motor
        initLed(7); // initialise the led
        unsigned long a = 0;
        while (1)
        {
            if (a == 50000)
            {
                toggleLed(7); // this LED flashes a little quicker than twice per second 
                stepMotor(0); // output a pulse to the driver to step the motor, attached to PA2
                a = 0;
            }
            a++;
        }
    }
    
    ##############################################################################
    
    Appenidx B - The OP's Original Code - ./drivers/led.c
    
        #include "../headers/stm32f767xx.h"
        
        void initLed(int pin)
        {
            RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable the GPIOB clock
            GPIOB->MODER |= (0x1 << (pin * 2)); // set to output
            GPIOB->OTYPER = 0x00; // push-pull mode
            GPIOB->ODR = 0x00; // set output register to 0 across all pins
        }
        
        void toggleLed(int pin)
        {
            GPIOB->ODR ^= (0x1 << pin); // toggle the pin
        }
    
    ###############################################################################
    
    # Appenidx C - The OP's Original Code - ./drivers/motor.c
    
    
    #include "../headers/stm32f767xx.h"
    
    void initMotor(int step_pin)
    {
        RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable the GPIOA clock
        GPIOA->MODER |= (0x1 << (step_pin * 2)); // set to output
        GPIOA->OTYPER = 0x00; // push-pull mode
        GPIOA->PUPDR |= (0x2 << (step_pin * 2)); // pull down the pin specified
        GPIOA->ODR = 0x00; // set output register to 0 across all pins
    }
    
    void stepMotor(int step_pin)
    {
        GPIOA->ODR |= (1 << step_pin); // output to the pin specified
        GPIOA->ODR &= ~(1 << step_pin); // reset the output back to 0
    }

    ##############################################################################

*/ to continue, ...*


  [1]: https://i.sstatic.net/FN7SL.jpg
  [2]: https://i.sstatic.net/Kf6Mj.jpg
  [3]: https://i.sstatic.net/QeS1S.jpg
  [4]: https://i.sstatic.net/V71gH.jpg