Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
C++ in Embedded Systems

You're reading from   C++ in Embedded Systems A practical transition from C to modern C++

Arrow left icon
Product type Paperback
Published in Jul 2025
Publisher Packt
ISBN-13 9781835881149
Length 402 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Amar Mahmutbegović Amar Mahmutbegović
Author Profile Icon Amar Mahmutbegović
Amar Mahmutbegović
Arrow right icon
View More author details
Toc

Table of Contents (25) Chapters Close

Preface
1. Part I: Introduction to C++ in Embedded Development
2. Debunking Common Myths about C++ FREE CHAPTER 3. Challenges in Embedded Systems with Limited Resources 4. Embedded C++ Ecosystem 5. Setting Up the Development Environment for a C++ Embedded Project 6. Part II: C++ Fundamentals
7. Classes – Building Blocks of C++ Applications 8. Beyond Classes – Fundamental C++ Concepts 9. Strengthening Firmware – Practical C++ Error Handling Methods 10. Part III: C++ Advanced Concepts
11. Building Generic and Reusable Code with Templates 12. Improving Type-Safety with Strong Types 13. Writing Expressive Code with Lambdas 14. Compile-Time Computation 15. Part IV: Applying C++ to Solving Embedded Domain Problems
16. Writing C++ HAL 17. Working with C Libraries 18. Enhancing Super-Loop with Sequencer 19. Practical Patterns – Building a Temperature Publisher 20. Designing Scalable Finite State Machines 21. Libraries and Frameworks 22. Cross-Platform Development 23. Other Books You May Enjoy
24. Index

Timers

STM32F072 has multiple timers, including TIM2 and TIM3. TIM2 is a 32-bit timer and TIM3 is a 16-bit timer.

We will create a template class timer that will depend on timer traits structures containing timer-specific details. Here is the code for timer traits structures:

struct timer2_traits {
    constexpr static std::uintptr_t base_address = 0x40000000;
    constexpr static IRQn_Type irqn = TIM2_IRQn;
    constexpr static std::uint32_t arr_bit_mask = 0xFFFFFFFF;
};
struct timer3_traits {
    constexpr static std::uintptr_t base_address = 0x40000400;
    constexpr static IRQn_Type irqn = TIM3_IRQn;
    constexpr static std::uint32_t arr_bit_mask = 0xFFFF;
};

In this code, timer2_traits and timer3_traits are traits structures that encapsulate the hardware-specific details of TIM2 and TIM3 timers, respectively. They have the following members:

  • base_address: The base memory address of the timer’s register map
  • irqn: The interrupt request number...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime