Go Down

Topic: STM32 "for the rest of us" (Read 21150 times) previous topic - next topic

Sunspot

If any developers with spare time drop in here please help with PWM.

I have an Arduino speaking wav speech files from a SD card and using PWM to create audio.

Now trying to do the same on STM32.

SD reading seems OK but any Arduino PWM code fails. I do see a built in Maple PWM option
http://leaflabs.com/docs/lang/api/analogwrite.html

I have yet to fully understand it.

Pulse Width Modulation -  next for the user community???  :smiley-confuse:


westfw

What Pwm support do you use on avrs?  I would not have thought that the default pwm would be high enough in frequency for speech.



rogerClark

Ah

This is not going to work for you.

Use PWMWrite

To make AnalogWrite compatible with the AVR, the pin mode is set to PWM inside AnalogWrite, so its not going to be fast enough  for speech

Set the pin mode as in the leaflabs docs and then call pwmwrite

I'm pretty sure its working, as I thought the servo library uses it

However, I'm not sure how you are going to output speech on 400Hz PWM

Your code must be doing something AVR specific to change the pwm rate, and you'll need to do something different to the AVR code to change the PWM rate on the STM

The chips are just too different for the code to just work.
Freelance developer and IT consultant
www.rogerclark.net

Sunspot

#18
Apr 04, 2015, 11:04 am Last Edit: Apr 04, 2015, 11:12 am by Sunspot
Some of the wav playing Arduino files do use PWM but (I was wrong) the one I pasted in for my system seems to just drive output pins in a "home build" PWM. I will do some work and try to ask a better informed question.

.....or just hope for ready made STM32 wav player code to appear spontaneously ........ :smiley-twist:

(I once wrote some real C code to make my router talk - :smiley-eek: - but I think I may have lost too many brain cells since then)


http://www.sunspot.co.uk/Projects/SWEEX/beeper.html
Bottom of page

rogerClark

Hi Sunspot,

I just wrote a nice long technical repsonse but I hit a wrong keystroke and it was all gone in a flash :-(

Anyway, here goes again, but expurgated.

Here is some code I use to generate a high frequency square wave

Code: [Select]
void setup() {
  // put your setup code here, to run once:
     setupCameraClock(PA8);
     Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void setupCameraClock(int pin)
{
  #define CLOCK_COUNT 3
  timer_dev *timerDevice  = PIN_MAP[pin].timer_device;
  uint8 timerChannel         = PIN_MAP[pin].timer_channel;
  pinMode(pin,PWM);
  timer_pause(timerDevice);
  timer_set_reload(timerDevice,CLOCK_COUNT);
  timer_set_prescaler(timerDevice,timerChannel);
  timer_set_compare(timerDevice, timerChannel, CLOCK_COUNT);
  timer_resume(timerDevice);
}


I forget the precise frequiency, but its at least 8Mhz

So if you adapt that code, you can get the PWM on your audio output in running at a nice value e.g. 100Khz

Then if you look at PWM write it does this

Code: [Select]

void pwmWrite(uint8 pin, uint16 duty_cycle) {
    if (pin >= BOARD_NR_GPIO_PINS) {
        return;
    }
    timer_dev *dev = PIN_MAP[pin].timer_device;
    uint8 cc_channel = PIN_MAP[pin].timer_channel;
    ASSERT(dev && cc_channel);
    timer_set_compare(dev, cc_channel, duty_cycle);
}


And looking at what set_timer_compare does


Code: [Select]


static inline void timer_set_compare(timer_dev *dev,
                                     uint8 channel,
                                     uint16 value) {
    __io uint32 *ccr = &(dev->regs).gen->CCR1 + (channel - 1);
    *ccr = value;
}



So if you cache *ccr in to a global variable in setup, and just write new values you can adjust the PWM very quickly


So to play from SD, once Victor gets the SD DMA working, you should be able to double buffer DMA into ram, then use another timer interrupt to take the data from the double buffer and play it.

You also need a low pass audio filter, perhaps with the 3dB point at around 5kHz would probably do

However, this isn't the simplest of projects.
Freelance developer and IT consultant
www.rogerclark.net

Sunspot

Roger
Thanks for the response.
Low pass with solder!

Sunspot

#21
Apr 04, 2015, 05:59 pm Last Edit: Apr 04, 2015, 06:06 pm by Sunspot
STM32 .WAV playing.

This is the AVR code that I was trying to convert
https://github.com/TMRh20/TMRpcm/wiki

PWM but perhaps "not as we know it"

rogerClark

Good luck with porting that code.

Unfortunately its full of direct register access stuff to AVR which isn't going to be the same on STM32 or any other hardware.

I hope it has enough comments, as you are going to need to go back to first principals to work out what the AVR code actually does, and then recode again in STM specific code.

It's probably doing some soft of adaptive pulse with modulation, either on a fixed or dynamic timer interrupt.

Personally, I'd just write a basic test program first using a sine wave table eg.  1k of ints. And try to get that to work, before I'd attempt all the complexity of SD card reading at the same time
Freelance developer and IT consultant
www.rogerclark.net

mrburnette

#23
Apr 08, 2015, 05:01 pm Last Edit: Apr 09, 2015, 01:29 am by mrburnette
Problems w/ OneWire and DS18B20

UPDATE:
independently confirm to be an issue with Maple & Maple Mini clones - but not an issue on generic boards.  This has been placed on the issues list and will be prioritized for research and correction.  I'll report here when corrected.


Response to @turkogluky: link

Summary: the current version of OneWire appears to freeze the Maple uC.


Test procedure:
I used a Nano to prove the DS18B20 was working; the 4.7K pull-up was required at 5V.

I used a new STM32 Maple Mini clone tested with my blink-count script to confirm the Maple Mini was fully functional.

When I went to the
\Documents\Arduino\hardware\STM32\STM32F1\libraries\OneWire\examples\DS18x20_Temperature
script, it would not compile on my Windows 8.1 box.  

The error was traced to the fact that for some crazy reason, the OneWire lib was being pulled from:
\Documents\Arduino\libraries\OneWire\
and not from
\Documents\Arduino\hardware\STM32\STM32F1\libraries\OneWire\

I fixed that by including the entire path:
#include "\Documents\Arduino\hardware\STM32\STM32F1\libraries\OneWire\OneWire.h"

The program compiled: (1.6.1 shipping)

Code: [Select]
Sketch uses 15,388 bytes (14%) of program storage space. Maximum is 108,000 bytes.
Global variables use 4,528 bytes of dynamic memory.



HOWEVER:
The instantiation of OneWire causes the uC to either freeze or appear to freeze.  Pressing RESET goes into DFU but when the user program starts, nothing.  Worst, the COMn port does not enumerate on Windows, so no debug output.

I changed Serial. to Serial1. and got a clean compile, but again, OneWire freezes the uC.

Ray

evildave_666

Just a note that the Beestore Maple Mini clone (whose PCB is a different layout than the original or Baite version) also works fine with the STM32 repo. Have had a couple for a while but never got around to trying them until this week, when I made a big push to get a fresh 1.6.1 install running all of the STM32/core13/TinyCore1/esp8266 cores.

rogerClark

Just a quick update

OneWire only seems to work on generic STM32 boards

I have posted a fix to the main thread, but it involves a slight difference in the way the OneWire class is initialised - I won't bore you with the technical details
Freelance developer and IT consultant
www.rogerclark.net

rogerClark

There were various issues with OneWire, so it has now been renamed to OneWireSTM

See the other thread for the technical details, or just download the relevant files and enjoy ;-)
Freelance developer and IT consultant
www.rogerclark.net

bigplik

#27
Apr 09, 2015, 12:42 pm Last Edit: Apr 09, 2015, 12:43 pm by bigplik
hi,
is there any tutorial how to use ST-Link with arduinoIDE to program STM32?
i can do it by usb, but if want to program single STM32 chip on custom board
don't know how to set it

rogerClark

Stm32 to flash.   Board type had STLink option on windows

There are no tutorials for this on the arduino but there may be general tutorials for stm32 in general e.g. On YouTube
Freelance developer and IT consultant
www.rogerclark.net

bigplik

#29
Apr 09, 2015, 01:00 pm Last Edit: Apr 09, 2015, 01:01 pm by bigplik
I've checked YT, still didn't manage, there were some programming stuff about discovery boards and different than arduinoIDE program uploaders, some ST-Link tools,
I have an option on my IDE for upload method as ST-Link, but it shown me a message that it can't connect into st-link device - however windows seen st-link in device manager

Go Up