Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
1 year, 4 months ago.
How to get interrupt from pwmout
Been playing with microcontrollers for a while but very new to mbed.
Just ordered one of these and would like to use mbed on it. In fact, in PlatformIO, mbed is the only option for programming this board.
In Arduino it is possible to set up a PWM output on a pin and have an overflow (i.e. every time one pulse is finished) trigger an ISR. Admittedly you have to play with registers to do that, but on an 8 bit AVR processor it's not that difficult. I was wondering, given that mbed has a lot more high level functionality than Arduino/AVR, if the same thing is doable in mbed in a more elegant and efficient manner.
I've looked at the pwmout function but that doesn't seem to have an option to attach a callback. There is an interrupt enable and ISR enable in the register definitions for the timer(s) that can do PWM output, so it would seem it is possible. I was hoping that there might be an easier way than register manipulation to get this happening. Or do I have to be anointed into the mysteries of C++ to be able to understand any of this.
As a bit of background. I'm trying to create a PPM output stream using PWM, so the ISR set's the pulse length (period) of the next PWM output pulse. All this requires (in the ISR) is incrementing an index to an array, and setting the count max (modulo) to the value pointed to by the index. Just a few instructions, so the ISR is very fast. The array values are updated asynchronously by the rest of the program by reading analog and digital inputs, doing some manipulations, and setting the output value in the array. But most of you probably already knew this.
1 year, 4 months ago.
The platform you are working with is an STM32F303K8T6 - right? Your current approach is based on the PWM functionality, but I think you can easily do what you want using the "one pulse mode" (OPM) of any of the MCU’s timers. This timer mode is described in Section 2.6 of AN4013, "STM32 cross-series timer overview". You can find example code in the STM32Cube F3 firmware examples:
The two basic timers are a good choice (TIM 6 or TIM 7) for this application, as they are not used by mbed and are not assigned to any specific pins. You can set the desired output pin when you fire the timer to start the pulse. Toggle the output pin, update the pulse length and re-start the timer in the IRQ handler.
The register definitions you will need are in RM0316, the Reference Manual for the MCU.
The IRQn’s are defined in "stm32f303x8.h":
TIM6_DAC1_IRQn for TIM6 and TIM7_DAC2_IRQn for TIM7.
The default IRQHandler function names are defined in "startup_stm32f303x8.s":
TIM6_DAC1_IRQHandler for TIM6 and TIM7_DAC2_IRQHandler for TIM7.