Beginner questions

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

aimatt
Posts: 18
Joined: Tue Jun 05, 2012 7:11 pm

Beginner questions

Post by aimatt »

Hi there,

I'm coming from an Arduino background and I am impressed by the ability to use threads and all the extra nice things an RTOS, and ChibiOS in particular, provides. I got the demo to run on my STM32F0 Discovery board and played around with the code successfully. Now I've got an STM32F4 discovery which I see is better supported at the moment. I checked out the PWM HAL test and see that it is generating PWM and reading that PWM signal.

I want to use a PWM signal to control a servo, but I'm having a really hard time finding high level documentation to help me get there.

I know I need to somehow attach the PWM to a pin, but how do I do that without bit-banging in the callbacks? And once I see how to do that, I'm really confused about how to figure out which pad maps to which pin. How do I figure that out? Then I see these PAL_MODE_ALTERNATE macros and I read that these set the pin to an alternate function as described in the MCU's datasheet. I was able to locate these functions, but how do I know which number is which function?



Thanks for your patience.

Matt
aimatt
Posts: 18
Joined: Tue Jun 05, 2012 7:11 pm

Re: Beginner questions

Post by aimatt »

And what does assigning different channels in the PWM CONFIG do?
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Beginner questions

Post by Tectu »

You can find the documentation here:

You should also check out the PWM demo codes in chibios/testhal/STM32F4xx/PWM
This should give you a better understand of how the PWM modules works and how to use it.

Hope that helps for the first.


~ Tectu
aimatt
Posts: 18
Joined: Tue Jun 05, 2012 7:11 pm

Re: Beginner questions

Post by aimatt »

Hi Tectu, I've seen all three of those. But like in the PWM HAL example, which pin is it going out on, and how do I figure that out?
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Beginner questions

Post by Tectu »

I am sorry, I never used PWM with ChibiOS before. I cannot help you.
Wait until someone else reads this and feels to answer it :)


~ Tectu
aimatt
Posts: 18
Joined: Tue Jun 05, 2012 7:11 pm

Re: Beginner questions

Post by aimatt »

I think I am starting to get the hang of it. In the stm32f4 datasheet, they have the following listed as alternate functions for PA15

JTDI/
SPI3_NSS/
I2S3_WS/
TIM2_CH1_ETR/
SPI1_NSS


So palSetPadMode(GPIOA, 15, PAL_MODE_ALTERNATE(1)); means PA15 (A form GPIOA, 15 from the 15 in the args) is set to alternate function 1 (PWM, I guess?)

And since alternate function is set to TIM2_CH1_ETR, the Channel 1 of timer2 will automatically output on that pin.

Phew, a little tough for someone used to analogWrite(pin, value); but let me know if I'm getting this right.

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Beginner questions

Post by Giovanni »

Hi,

Correct, you need to read that table in the Reference Manual and then use the PAL driver in order to map the PWM output to one of the possible output pins. The F4 discovery demo drives the LEDs exactly this way without using callbacks.

This applies to any peripheral not just the PWM, see also USARTs, SPIs, I2Cs etc.

Make sure to not map a function to multiple pins, the result is unpredictable.

Giovanni
aimatt
Posts: 18
Joined: Tue Jun 05, 2012 7:11 pm

Re: Beginner questions

Post by aimatt »

Awesome, I've got 4 servos running on PORTB!

Now, is the code going to be portable in terms of the PAL_MODE_ALTERNATE macro? Is PAL_MODE_ALTERNATE(2) PWM on all boards or specific to STM32F4? Is there an enumeration of all the possible values somewhere? I don't get how PAL_MODE_ALTERNATE(2) maps to PWM. It is not even in order in the datasheet.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Beginner questions

Post by Giovanni »

Hi,

It is portable across similar STM32 devices, F2 and F4 are practically the same, L1/F0 are similar but mapping can change, F1 are different.

That "2" is the number of the alternate function for that pin, there is a table in the datasheet with all possible mappings: "Table 6. Alternate function mapping".

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Beginner questions

Post by Tectu »

Feel free to post the servo code under the forum "Code Samples" would like to see it :)



~ Tectu
Post Reply