Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Interrupts, semaphores, priority management and PWM

Posted by ndsmith34 on June 25, 2015

I am a little out of my depth and could use some guidance.

My goal is to drive a stepper motor using a PWM signal. Each motor movement will be a specific number of pulses. I am also implementing an acceleration profile, which means that the period between pulses will change at the beginning and end.

The main players are: MotorStart() – a function that initializes and starts the PWM and sets the initial case for the switch statement inside MotorProfile. MotorProfile – a task that calculates the period for the next PWM pulse. MotorUpdate – a task that updates the PWM parameters at the proper time.

In MotorTask I call MotorStart() which kicks off the PWM. The rest of the program is driven by interrupts.

Each rising edge on the PWM trips an interrupt that in turn calls a notification that gives the MotorProfile_sem semaphore that unblocks MotorProfile. This calculates the period for the next PWM pulse. I do it in this way because I am using TI’s Halcogen code generator and cannot modify the ISR.

Then, an endofperiod interrupt triggers a different notification that gives the MotorUpdate_sem semaphore that unblocks MotorUpdate, which updates the PWM signal.

However, my notification functions are not being called and the PWM is never being updated. I can see that the PWM signal is being output on a scope. Sample snippets of code are below.


xSemaphoreTake(MotorProfile_sem, 10);
xSemaphoreTake(MotorUpdate_sem, 10);

xTaskCreate(MotorTask, (signed char*) "MotorTask", 512, NULL, 1, NULL);	
    xTaskCreate(MotorProfile, (signed char*) "MotorProfile", 512, NULL, 3, NULL);
xTaskCreate(MotorUpdate, (signed char*) "MotorUpdate", 512, NULL, 2, NULL);

void MotorUpdate (void *p) { while(xSemaphoreTake(MotorUpdatesem, portMAXDELAY) != pdTRUE); pwmSetSignal(hetRAM2, pwm0, motor_drive); } …

void MotorStart (void) // Called to start motor movement { motordrive.duty = 30; motordrive.period = 68374; motorstate = 'a'; pwmSetSignal(hetRAM2, pwm0, motordrive); pwmStart(hetRAM2, pwm0); }

My main questions are: Am I handling the PWM correctly? Are pwmSetSignal and pwmStart redundant? What are my task, semaphore, and interrupt/notification mistakes?

Interrupts, semaphores, priority management and PWM

Posted by ndsmith34 on June 26, 2015

The main issue was that when I transformed my tasks from functions I forgot to make them infinite loops.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner