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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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
Code Exchange
  • Technologies
  • More
Code Exchange
Blog embedded C++: add cheap bitwise conversion to a register structure
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 30 Mar 2025 6:20 PM Date Created
  • Views 1190 views
  • Likes 6 likes
  • Comments 12 comments
Related
Recommended

embedded C++: add cheap bitwise conversion to a register structure

Jan Cumps
Jan Cumps
30 Mar 2025

This blog is based on real world work. I'm controlling a TI DRV8711 stepper motor controller via SPI.

I need to modify 16-bit registers of that IC. TI provided a header file with register definitions, and code to convert that struct to a 16 bit unsigned integer. That integer can then be sent over SPI.

C code

register struct (TI example: the control register)

 // CTRL Register
 struct CTRL_Register
 {
     uint16_t Address;	// bits 14-12
     uint16_t DTIME;		// bits 11-10
     uint16_t ISGAIN;	// bits 9-8
     uint16_t EXSTALL;	// bit 7
     uint16_t MODE;		// bits 6-3
     uint16_t RSTEP;		// bit 2
     uint16_t RDIR;		// bit 1
     uint16_t ENBL;		// bit 0
 };

code to convert an instance of the struct (reg) to a 16 bit unsigned int and write to SPI:

    uint16_t data;
    // prepare CTRL Register
    data = (reg.Address << 12) | (reg.DTIME << 10) | (reg.ISGAIN << 8) |(reg.EXSTALL << 7) | (reg.MODE << 3) | (reg.RSTEP << 2) | (reg.RDIR << 1) | (reg.ENBL);
    spi_write(data);    

C++ code

in C++, we can teach the structure to convert itself to a 16 bit unsigned int (the operator uint16_t() below): 
I also encapsulated the address of the register. It's fixed, so your firmware should not have to deal with that.

 // CTRL Register
 struct CTRL_Register
 {
     uint16_t DTIME;		// bits 11-10
     uint16_t ISGAIN;	// bits 9-8
     uint16_t EXSTALL;	// bit 7
     uint16_t MODE;		// bits 6-3
     uint16_t RSTEP;		// bit 2
     uint16_t RDIR;		// bit 1
     uint16_t ENBL;		// bit 0
     inline operator uint16_t() const {
        return (0x0000 << 12) | (DTIME << 10) | (ISGAIN << 8) |(EXSTALL << 7) | (MODE << 3) | (RSTEP << 2) | (RDIR << 1) | (ENBL);
     }
 };

and then write the struct directly to SPI (conversion is done by the struct when being passed to this call - because we trained it how to do that).

    spi_write(reg);

Note that there is no conversion in your user code - the struct will do it. We can just pass the struct to the SPI write procedure.
All knowledge on what fields are available in the register, and how to club it into the 16 bits, is contained within the struct.

The code does not add anything to the runtime / firmware. But the register's bit structure knowledge is fully contained within the struct. Your client software doesn't need to shift bits.

Thank you for reading. Critique is welcome.

bonus pub quiz question: why did I put const in the conversion function's declaration?

  • Sign in to reply
  • michaelkellett
    michaelkellett 11 months ago

    What happens if ENBL = 2u  ?

    and

    what does the generated assembler code look like ?

    and

    why not use bit fields, although in C the assignment of an out of range value to a bit field is implementation dependent (don't know about C++).

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz 11 months ago

    Nice work! Very clever how all this can be encapsulated when using C++, and structs, not just classes. Sometimes easy to forget. I understand why the const (I won't put it here in case others want to have a go).

    • 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 © 2026 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