PWM Channels to STM32F405 pins

Can anyone tell me how to translate I/O pins on the STM32F405 to PWM channels. I am using pins 41 and 42 which seem to be TIM1_CH1 and TIM1_CH2 respectively.


PWM pwmLEDb;
PWM pwmLEDc;

pwmLEDb = new PWM(Cpu.PWMChannel.PWM_1, 500, 100, false);
pwmLEDc = new PWM(Cpu.PWMChannel.PWM_2, 500, 100, false);

pwmLEDb.Start();
pwmLEDc.Start();

Using the code above nothing happens to the LEDs on these two pins.

What am I doing wrong?

C6 = 0
A7 = 1
C7 = 2
A8 = 3
B0 = 4
B1 = 5
B5 = 6
B4 = 7
B3 = 8
B11 = 9
B10 = 10
A10 = 11
A9 = 12
A15 = 13
B8 = 14
B9 = 15

So…

Pin 41 = PA8 == PWM3
Pin 42 = PA9 == PWM12

Obvious init :whistle:

1 Like

It couldn’t be less obvious! How do you know this and I don’t? I don’t even know where to look in the netmf/STM documentation. Where could I have found your answer? Teach a man to fish…

Also, does the enum go up that that far?

It hasn’t felt like it this week. This stuff is addictive.

Only goes to 8…

1 Like

The information for those pins is only available on the schematic for the Cerbuino devices.

[url]http://www.ghielectronics.com/downloads/schematic/FEZ_Cerb40_II_SCH.pdf[/url]

@ Mr. John Smith - So how do you tell from that schematic which pin is which PWM channel?

@ Justin, yea, you’d also need the device datasheet :smiley:

I have both but the answer is too well hidden to be straight forward to determine. If you have any insight I’d be grateful to receive it.

@ Jason - https://ghiopensource.codeplex.com/SourceControl/latest#Solutions/FEZCerberus/platform_selector.h

:wink:

2 Likes

Re: “Can anyone tell me how to translate I/O pins on the STM32F405 to PWM channels”

The Datasheet is wrong. Its posted in the specs section about the error in the Datasheet and Pin Maps but I cant find it now. Site Changes…

EDIT: Found it:
[b]
Known Issues

Hardware
On boards version 1.1, sockets 5 and 6 are mislabeled. These sockets have PWM feature so they should also include P socket type.
[/b]

Page removed but I had a saved copy…

Where PWM is shown to be X5 and X6 on the Datasheet, its incorrect and its actually on X3 and X4, Pins 7, 8 and 9 See below.

I have started some code on the register access to configure PMW, see: https://www.ghielectronics.com/community/codeshare/entry/931

Also I wonder if this may help some, I wrote a helper class:


public sealed class PWMChannel : PWM
    {
        #region PWM Pin Outs...
        // X3...
        // Pin 7
        // Pin 8
        // Pin 9

        // X4...
        // Pin 7
        // Pin 8
        // Pin 9

        #endregion

        #region Below does not work...
        // X5...
        // Pin 7
        // Pin 8
        // Pin 9

        // X6...
        // Pin 7
        // Pin 8
        // Pin 9

        // X8...
        // Pin 7
        #endregion

        /////////////////////////////////////////////////////////////////////////
        // See for more information:                                           //
        // http://msdn.microsoft.com/en-us/library/jj633523(v=vs.102).aspx     //
        // http://msdn.microsoft.com/en-us/library/jj646655(v=vs.102).aspx     //
        /////////////////////////////////////////////////////////////////////////

        /////////////////////////////////////////////////////////////// 
        // References the Library: "Microsoft.SPOT.Hardware.PWM"     //
        // Add using statement: using Microsoft.SPOT.Hardware;       //
        ///////////////////////////////////////////////////////////////

        /// <summary>
        /// Get or Set PWMChannel's Frequency using the inhetited member 'Frequency'...
        /// </summary>
        private uint ChannelFrequency
        {
            get { return (uint)Frequency; }
            set { Frequency = value; }
        }

        /// <summary>
        /// Get or Set the PWMChannel's Duty Cycle using the inhetited member 'DutyCycle'...
        /// </summary>
        private double ChannelDutyCycle
        {
            get { return DutyCycle; }
            set { DutyCycle = value; }
        }

        /// <summary>
        /// Channel 1 - Set for output on Pins 8 on X3.
        /// </summary>
        public static PWMChannel Channel_1 = new PWMChannel(Cpu.PWMChannel.PWM_1, 10, 0, GlobalVariable.InvertChannel_1);

        /// <summary>
        /// Channel 2 - Set for output on Pins 9 on X3.
        /// </summary>
        public static PWMChannel Channel_2 = new PWMChannel(Cpu.PWMChannel.PWM_2, 10, 0, GlobalVariable.InvertChannel_2);

        /// <summary>
        /// Channel 3 - Set for output on Pins 8 on X4.
        /// </summary>
        public static PWMChannel Channel_3 = new PWMChannel(Cpu.PWMChannel.PWM_3, 10, 0, GlobalVariable.InvertChannel_3);

        /// <summary>
        /// Channel 4 - Set for output on Pins 9 on X4.
        /// </summary>
        public static PWMChannel Channel_4 = new PWMChannel(Cpu.PWMChannel.PWM_5, 10, 0, GlobalVariable.InvertChannel_4);

        /// <summary>
        /// Stop Set Parameters and Start PWM on the desired Configuration...
        /// </summary>
        /// <param name="frequency_Hz">The Frequency in Hertz</param>
        /// <param name="dutyCycle">Duty Cycle expressed in Percentage EG: 0.5 gives 50% Duty Cycle of the Frequency</param>
        /// <param name="invert">Inverts the Output Pin</param>
        public void Configure(uint frequency_Hz, double dutyCycle)
        {
            this.Stop();

            ChannelFrequency = frequency_Hz;
            ChannelDutyCycle = dutyCycle;

            this.Start();
        }

        /// <summary>
        /// All Stop! Stops the Channel's PWM...
        /// </summary>
        public void ChannelStop()
        {
            this.Stop();
        }

        /// <summary>
        /// Standard Constructor, explicit PWM Inheritance.
        /// </summary>
        /// <param name="channel">The Cpu.PWMChannel Channel</param>
        /// <param name="frequency_Hz">The Frequency in Hertz</param>
        /// <param name="dutyCycle">Duty Cycle expressed in Percentage EG: 0.5 gives 50% Duty Cycle of the Frequency</param>
        /// <param name="invert">Inverts the Output Pin</param>
        private PWMChannel(Cpu.PWMChannel channel, uint frequency_Hz, double dutyCycle, bool invert) : base(channel, frequency_Hz, dutyCycle, invert)
        {
            Debug.Print("PWMChannel Class Instantiated - First Constructor called...");
        }

        /// <summary>
        /// Standard Constructor, explicit PWM Inheritance.
        /// </summary>
        /// <param name="channel">The Cpu.PWMChannel Channel</param>
        /// <param name="period">The period of the pulse in microseconds.</param>
        /// <param name="duration">The duration of the pulse in microseconds. This value should be a fraction of the period parameter.</param>
        /// <param name="scale">PWM.ScaleFactor Enumeration - Provides scale factor values for duration and period.</param>
        /// <param name="invert">Inverts the Output Pin</param>
        private PWMChannel(Cpu.PWMChannel channel, uint period, uint duration, PWM.ScaleFactor scale, bool invert) : base(channel, period, duration, scale, invert)
        {
            Debug.Print("PWMChannel Class Instantiated - First Constructor called...");
        }

    } // END of Class...

This code does actually make some features come to life, EG: PWM Changes are much better…

To Use this Code:


PWMChannel.Channel_1.Configure(184000, 0.5);

All the best

Chris

3 Likes

Just to help out some more, I wanted to share some information, some I just gained by experimenting.

NOTE: PAx, or PCx relates to the GPIO Port, EG: GPIOC, or GPIOA where x is the Number of the Pin 0 - 15 for each GPIO Port.

Also NOTE: Each Timer can have multiple Channels, Channel 1 through 4 for most Timers. The CCMR1 and CCMR2 Register control the functionality of the Channels. These Channels have nothing to do with the Cpu.PWMChannel.PWM_x Channels in the Microsoft Class.

Pin to Channel to Timer Mapping is as follows. See attached Picture: