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
  • 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
Azure Sphere Starter Kit
  • Products
  • Dev Tools
  • Avnet & Tria Boards Community
  • Azure Sphere Starter Kit
  • More
  • Cancel
Azure Sphere Starter Kit
Blog Generating Software PWM using Azure Sphere Real-Time Core Application
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Azure Sphere Starter Kit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jgroman
  • Date Created: 15 Oct 2019 7:12 AM Date Created
  • Views 715 views
  • Likes 6 likes
  • Comments 2 comments
  • real-time
  • azure sphere
  • pwm
  • gpt
Related
Recommended

Generating Software PWM using Azure Sphere Real-Time Core Application

jgroman
jgroman
15 Oct 2019

As a part of my project I needed a PWM source and since hardware PWM was not accessible before OS 19.09, the only other option was to do it old school: by bit-banging GPIO pins. Turns out it wasn't that difficult and since this technique might sometimes be useful even with HW PWM present, I'd like to share my code for others to use.

 

PWM (or Pulse Width Modulation) is basically a clock signal with a programmable duty cycle. PWM generator could then be decribed by using two values: clock frequency (or period) and current duty cycle percentage. My implementation uses real-time M4 core GPT timers for generating clock signal where timer duration is set depending on duty cycle value. GPT timer after running out generates interrupt and the interrupt handling routine flips GPIO state.

Since the original GPT timer setup routine supports only 1 kHz timer clock, which is probably too slow for PWM generating purposes, I have tweaked this routine a bit (pun intended image ) and switched GPT timer clock to maximum supported 32 kHz. See mt3620-timer-user.c where I also added basic routines for previously unsupported GPT2 timer. GPT2 is a free-running timer and cannot be used for interrupts, but it can be very useful for time keeping as an Arduino's millis() and delay() function replacement. With this setup we can reach maximum software PWM frequencies around 5 kHz.

 

The repository contains complete real-time core application which demonstrates PWM function by dimming RGB LED after repeated button1 presses. You can find the complete code here: https://github.com/jgroman/azsphere_rtcore_softpwm_example

 

PWM generating function is basically this short interrupt handling routine, which is called when pwm timer runs out. Clock period is set by COUNTER_PWM_MAX and the current ducty cycle value is stored in counter_pwm_on array.

 

static void
handle_irq_pwm_timer(void)
{
    static bool b_is_pwm_gpio_high = true;


    // Calculate this pulse duration counter
    uint32_t counter = (b_is_pwm_gpio_high) ?
        counter_pwm_on[index_counter_pwm] : 
        COUNTER_PWM_MAX - counter_pwm_on[index_counter_pwm];


    if (counter > 0)
    {
        // Do not flip output GPIO if requested pulse duration is 0
        Mt3620_Gpio_Write(GPIO_RGBLED_RED, b_is_pwm_gpio_high);
        Mt3620_Gpio_Write(GPIO0, b_is_pwm_gpio_high);
    }


    b_is_pwm_gpio_high = !b_is_pwm_gpio_high;


    start_pwm_timer(counter);
}

  • Sign in to reply

Top Comments

  • Fred27
    Fred27 over 6 years ago +4
    Thanks for sharing. It's nice to see some useful code for the MT3620 - especially as it's for the under-utilised M4 core. I wonder if we'll get more people posting after the contests are over? Maybe people…
  • jgroman
    jgroman over 6 years ago in reply to Fred27 +2
    You are welcome! Those are just some bits and pieces I stumbled upon when trying to learn ins and outs of this devkit. I figured it could be useful so that somebody else doesn't have to invent wheel once…
  • jgroman
    jgroman over 6 years ago in reply to Fred27

    You are welcome! Those are just some bits and pieces I stumbled upon when trying to learn ins and outs of this devkit. I figured it could be useful so that somebody else doesn't have to invent wheel once again.

    As for me, after this contest I will be busy with something else, so I'm trying to write some interesting MT3620 findings now image

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Fred27
    Fred27 over 6 years ago

    Thanks for sharing. It's nice to see some useful code for the MT3620 - especially as it's for the under-utilised M4 core.

     

    I wonder if we'll get more people posting after the contests are over? Maybe people don't want to show their hand too soon.

    • Cancel
    • Vote Up +4 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