"Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post here first, or if you can't find a relevant section!
nicolas_soundforce
Posts: 22
Joined: Sat Jun 17, 2017 10:18 am

"Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by nicolas_soundforce » Thu Jul 13, 2017 7:42 am

Hallo,

I've been using the bluepill almost exclusively the last months with this core. It is has been amazing, thank you so much to all the members involved to improve this project.

My question is, is it possible to make the blue pill interrupts with a timer of 22.67us ? I am building a wave file player and I want to feed a DAC with samples every 22.67us to reach the sample rate of 44.1khz. At the moment it works at 22 and 23 us, but it's not playing back at the original speed.

I am using one of the 3 timers.

Thanks in advance,
Nicolas.

aster
Posts: 100
Joined: Thu Mar 30, 2017 2:41 pm
Location: bella italy
Contact:

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by aster » Thu Jul 13, 2017 9:08 am

Have a look here, it could be useful
https://libstock.mikroe.com/projects/vi ... calculator

User avatar
Pito
Posts: 1529
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by Pito » Thu Jul 13, 2017 9:51 am

The STM32F103 does not possess a fractional PLL or pre-scalers/dividers, only integer ones.
So the only way to get something better than 22-23us is to play with CPU clock settings such the integer division gives you a better result.
Pukao Hats Cleaning Services Ltd.

dannyf
Posts: 140
Joined: Wed May 11, 2016 4:29 pm

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by dannyf » Thu Jul 13, 2017 10:01 am

to reach the sample rate of 44.1khz. At the moment it works at 22 and 23 us
rather than thinking about it from a time perspective,t hink about it from a frequency perspective: what frequency do you need the mcu to run at for you to hit that 44.1khz number with an integer dividor?

then it becomes much easier.

User avatar
Pito
Posts: 1529
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by Pito » Thu Jul 13, 2017 10:18 am

There are special crystal frequencies to get 44.1kHz for audio with integer dividers:
https://en.wikipedia.org/wiki/Crystal_o ... requencies
So you may replace the crystal and you get a perfect audio :)
Pukao Hats Cleaning Services Ltd.

nicolas_soundforce
Posts: 22
Joined: Sat Jun 17, 2017 10:18 am

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by nicolas_soundforce » Thu Jul 13, 2017 10:41 am

Thank you for the first replies,
the calculator is very interesting. Unfortunately the code is for the ST library (I guess) and I doesn't compile with the STM32duino core. But it's great to get the prescaler and autoreload registers values. With a autoreload of 1632 I can get as close as 44,117647058823529 kHz, so a period of 22.666666 us. It's probably enough for my application.

But changing the CPU clock is indeed probably a much better idea. I could easily run a 48mhz and using 48khz instead (another audio sampling freq standard). Simply with a reload of 999.

I hope my program runs smoothly at 48Mhz too. I could also try to overlock the CPU to 96Mhz.

EDIT:silly me, for 48khz I can run at 72mhz and using a reload of 1500.

For those interested the math behind it :

The update event period is calculated as follows:
Update_event = TIM_CLK/((PSC + 1)*(ARR + 1)*(RCR + 1))
TIM_CLK = 72 MHz
Prescaler = 0
Auto reload = 1499
No repetition counter RCR = 0
Update_event = 72*(10^6)/((0 + 1)*(1499 + 1)*(1))
Update_event = 48000 Hz

nicolas_soundforce
Posts: 22
Joined: Sat Jun 17, 2017 10:18 am

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by nicolas_soundforce » Thu Jul 13, 2017 10:56 am

for those interested, I still have to test this (dont have the hardware with me right now), but according to HardwareTimer.cpp it would should be right

to set the prescaler:
Timer2.setPrescaleFactor(0);

to set the autoreload value:
Timer2.setOverflow(1499);

A overflow of 1633 would give me a freq of 44090,630740967544397 Hz which is an error of 0,021%. I have to test but I think it's probably fine for my application.

Thanks for the pointers!

User avatar
Pito
Posts: 1529
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by Pito » Thu Jul 13, 2017 11:03 am

I am building a wave file player and I want to feed a DAC with samples
Btw, there are none internal DACs in the BluePill.. With F103 you have to go with larger packages.. :(
Or use an external DAC, or try with PWM and a low-pass..
Pukao Hats Cleaning Services Ltd.

nicolas_soundforce
Posts: 22
Joined: Sat Jun 17, 2017 10:18 am

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by nicolas_soundforce » Thu Jul 13, 2017 11:30 am

Pito wrote:
Thu Jul 13, 2017 11:03 am
I am building a wave file player and I want to feed a DAC with samples
Btw, there are none internal DACs in the BluePill.. With F103 you have to go with larger packages.. :(
Or use an external DAC, or try with PWM and a low-pass..
Indeed I am building a board with a PT8211 and I am testing at the moment the timings with another board with a MCP4822.

ChrisMicro
Posts: 297
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: "Irregular" non-integer timer interrupt values (e.g. 22.67us/44.1khz)

Post by ChrisMicro » Thu Jul 13, 2017 1:18 pm

I also experimented a little bit with audio applications on a STM32F103.
E.g I tried this I2S amplifier from adafruit but I couldn't get it running.
It needs a quite accurate timing of the I2S signal and the STM32F103 bluepill has not I2S peripheral.

Here I made some experiments with a sigma delta DAC wav player.
It plays only one second of the wav sampled with 8kHz and than crashes ...

As I sad ... an experiment ...

line 28: filename
line 39: DACSAMPLINGRATE_HZ 8000

Post Reply