Timers & interrupts PWM

Hi

I am looking at using your boards instead of an Arduino. Where would I find information about how many timers or PWM Pins are available to me. For my project I am sending pulses to stepper drivers. So I would need to adjust the frequency of any PWM.

I saw this


It states there are 17 timers which would be available to me

Have you taken a look at the documentation already? You can find it at docs.spark.io

@russell, the Core only has three main timers available, each with 4 channels which can be configured for various functions. Each main timer/prescaler sets the clock for its 4 channels. At this time, because of extensive pin remapping on the Core, the PWM frequency used by analogWrite is set to 500Hz. However, if you carefully map your PWM pins to their associated timers, you can program the PWM frequency to whatever you want. The datasheet you pointed to is for the STM32F2xx series which will be used by the Photon. The Core uses the STM32F103.

2 Likes

I used this example to get “10Mhz” (unmeasured yet) on the PWM pin :wink:

1 Like

Thanks
Sorry I should have been more clear

Yes I am looking at the possibility of using the Photon. The data sheet says it has 12 timers so I was thinking it would be good to use this board for control of 8 stepper motors 4 with 2 joy sticks & 6 with code & acceleration ramps.

Of course at the price I could use 2 or 3 boards which might suit me at this time I am using an arduino Mega & ethernet shield.

As you mention sometimes not all the functions of a microprocessor chip are available on a board so I was interested in finding out how practical it would be for me to consider the photon as I love the WIFi option

Best Russell

@russell, it is important to note that the Photon will be running FreeRTOS, a real-time operating “kernel”. The number of timers available is linked to how the Photon’s GPIO pins will be mapped. I have discussed with @mdma about creating a HAL layer library for managing timers in both the Core and the Photon. It is easy to configure a timer when all its channels are committed to the same function (eg. PWM). It is not so easy when you want one channel doing PWM at 5KHz and another channel producing interval interrupts at 1ms intervals. This is the challenge.

1 Like

But is it already known how many PWM capable pins will be exposed on the Photon ?

@kefir135, the final pin assignment for the Photon is still in the works but you can take a look here for now :smiley:

Guys, now that this is a few months old, I was wondering if we’re any closer to being able to use custom PWM and interval interrupts on the Photon?

I’ve used custom PWM on the Core along with @peekay123’s most excellent SparkIntervalTimer to make a custom PWM fan controller (timer being used to track tach output) and would love to be able to do this on the Photon.

Looks like it’s still possible to change the PWM frequency by copying the logic in the HAL_PWM_Write routine and using a different TIM_PWM_FREQ. But the SparkIntervalTimer library is rather outside of my skill level and I am not sure if this approach even makes sense anymore, given the new HAL.

@peekay123, how hard would it be to port SparkIntervalTimer to the Photon? I am willing to sacrifice other channels on the same timer in order to be able to get this, as long as I don’t lose SPI, I2C, and Serial connectivity.

Thanks!
Alexander

2 Likes

@akiesels, I’ll be posting my updated (and Photon compatible) library over the next couple of days. :smile:

4 Likes

Yay @peekay123!! :grinning: Thanks for the wonderful support of this community!

1 Like

updated library updated yet?

I’m just working on Readme updates ant then off it goes… later this eve. :wink:

1 Like

hi team,
What is the go with the photon and setting a custom PWM frequency? On the Core I’d been using Bdub/BKO’s (from memory) code, though now flashing a Photon it errors as soon as it sees:

#define PWM_FREQ 25000

The error:

../platform/MCU/STM32F2xx/CMSIS/Device/ST/Include/stm32f2xx.h:1156:50: error: expected ')' before '*' token
 #define ADC                 ((ADC_Common_TypeDef *) ADC_BASE)
                                                  ^
1 Like

@peekay123 I tried to build the SparkIntervalTimer for my photon project and hit the following linker errors - haven’t explored too much at the minute but any ideas?

In function `__static_initialization_and_destruction_0':
..particle/firmware/user/../../../src/Pump.cpp:49: undefined reference to `Wiring_TIM2_Interrupt_Handler'
..particle/firmware/user/../../../src/Pump.cpp:49: undefined reference to `Wiring_TIM3_Interrupt_Handler'
..particle/firmware/user/../../../src/Pump.cpp:49: undefined reference to `Wiring_TIM4_Interrupt_Handler'

@peekay123 sorry - I just read your reply on the main Interval Timer thread…

peekay123, August 31 2015: “I have not had a chance to post the Photon compatible version the library yet. I am working on that!”

Just to link multiple similar threads, I posted a PWM gist here: Using PWM in Particle Photon

Since the tim_period is not being honoured, I don’t think a custom frequency is possible. Have asked for bright ideas!

@mterrill, on the Photon, PIN_MAP[] is now “hidden” by the HAL so you need to create a local copy using STM32_Pin_Info* PIN_MAP = HAL_Pin_Map(); at the top of AnalogWrite2(). Without this, the code will not be able to access the correct timer registers. DO remember that changing a timer’s value will affect all of its channels:

PHOTON
PIN    TMR3    TMR4    TMR5
-----------------------------
D0               x
D1               x
D2       x
D3       x
A4       x
A5       x
WKP                      x

Let me know how it goes! :smile:

4 Likes

I must admit, I’ve given it a good go but still it seems to be evading me.

I believe I’ve been running basic PWM on D0. IE able to scale from 0 - 65k (default period) and get the fan to act accordingly, though it refuses to budge at less than 30%. The humming and the lack of fan movement suggests 25khz is not being written, which is backed up by setting the max value of the period and it still doesn’t kick life into it. Just using 65k as the value seems to show the period is being ignored.

So, the code. I understand D0 is TIM4 and CH2 so have been pursuing your suggested angle. I can’t get the following to work, despite correlating what seem to be multiple sources in agreement

I presume that the GPIO wiring is done for me, ie the GPIO code at the top of: http://amarkham.com/?p=37

…I’m guessing that where I may be wrong… ?

@mterrill, I have to admit to being thoroughly confused as to what you are trying to do. The code you are using from @BDub uses the PWM mode of a timer where the base frequency and the period are configured through timer hardware registers. The amarkham link you provided uses the Output Compare mode of a timer, which is how the Tone() library works. You can’t mix the two. I will need to spend more time on your code to figure out what you are doing. Do you have an oscilloscope?

1 Like