element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Stepper Motor Control with Raspberry Pico PIO and DRV8711 driver- Part 4: PIO
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 4 Apr 2025 2:08 PM Date Created
  • Views 718 views
  • Likes 8 likes
  • Comments 6 comments
Related
Recommended
  • pico
  • pico_eurocard
  • PIO

Stepper Motor Control with Raspberry Pico PIO and DRV8711 driver- Part 4: PIO

Jan Cumps
Jan Cumps
4 Apr 2025

The Pico has a set of PIO co-processors. They are real-time controllers that can execute logic with deterministic timing. Ideal to run strict-timed sequences and state machines. And to implement extra peripherals.
In this series, I'm trying to use the PIO to control a TI DRV8711 stepper motor controller.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Follow up of   Stepper Motor Control with Raspberry Pico PIO and DRV8711 driver- Part 3: GPIO .

The First 3 posts dealt with anything except the PIO part. Pin assignments and initialisation, SPI setup, DRV8711 driver and communication. This post handles the PIO.
Nothing new here. This post reuses everything from  the hackster project PIO Stepper Motor Control - an example that uses the Allegro A4988 stepper driver.

I selected that one because the PIO part is about as simple as it gets: generate a train of pulses. You can tell it the frequency and how many pulses. Then the state machine generates these for you. The pulses are sent to the stepper motor driver, and that takes care that the motor steps.

Here is the very summere PIO script:

.program stepper_1
;.side_set 1

.define public STD 1

.wrap_target
bitloop:
    out x, 1       ; Side-set still takes place when instruction stalls
    jmp !x do_zero ; Branch on the bit we shifted out
do_one:
    set pins 1     [STD + 3]
    set pins 0     [STD]
    jmp  bitloop   
do_zero:
    set pins 0   ; Or drive low, for a short pulse
.wrap

There is also the usual PIO init code in that file. Check the author's GitHub for the full listing. It's a good example if you are a beginning PIO designer.

All intelligence is in the main C code.

  • Set up of your motor configuration (in my case: 200 steps are a full 360°). 
  • logic to calculate how many steps to take to move from current position to the requested angle (left turn, right turn or shortest path)
  • hand over info to the PIO state machine

Here is that C code from the original author. My Pico project, with sources adapted for DRV8711, is attached at the end of this post.

Main differences:

  • DRV8711 requires SPI communication to set it up
  • I  use its microstep capability. I configured it to use 8 microsteps for a full single step
  • I run the PIO at 5 times the speed of the example
  • my motor has 200 steps per rotation. With the microsteps configuration that becomes 1600 steps. The original uses a stepper with 800 steps.

A lot more of the logic can be implemented inside the PIO, so that it becomes an autonomous motor manager. You could even try to make it perform speed ramp-up and -down.
But then, this post wouldn't be a simple PIO example anymore.

pio_drv8711_stepper.zip

That's it for this series. Enjoy.

  • Sign in to reply
  • Jan Cumps
    Jan Cumps 6 months ago in reply to Jan Cumps

    Here's the documentation:  Stepper Motor Control with Raspberry Pico PIO and DRV8711 driver- Part 5: a more autonomous PIO 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 6 months ago

    I got the new script working including dir pin now.

    bit 31:1 are the steps

    bit 0 is direction

    .program stepper
    .side_set 1
    
    .wrap_target
    start:
        pull block      side 0 ; Pull from FIFO to OSR
        out pins, 1     side 0 ; shift direction out of OSR and output to dir
        mov x, osr      side 0 ; Copy rest of OSR to scratch X
        jmp !x start    side 0
        jmp x-- count   side 0
    count:
        nop             side 1
        jmp x-- count   side 0
    .wrap
    out pins, 1 shifts the lsb out of the data that we passed, and sets the DIR pin to that
    the step pin is managed by the side command.
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 6 months ago

    I've started writing my own stepper PIO program.

    The original code that I used can step up to 32 steps per call. The version I'm writing, you can step whatever fits in a uint32_t (4,294,967,295).

    I may steal one of the 32 bits for the motor spin direction later ...

    code:

    .program stepper
    .side_set 1
    
    .wrap_target
    start:
        pull block      side 0 ; Pull from FIFO to OSR
        mov x, osr      side 0 ; Copy most-recently-pulled value back to scratch X
        jmp !x start    side 0
        jmp x-- count   side 0
    count:
        nop             side 1
        jmp x-- count   side 0
    .wrap

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB 6 months ago

    Nice update Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 6 months ago

    There is an extension for VSCode, to syntax-highlight PIO scripts. It's called PIO ASM Syntax Highlighting:

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube