PWM example

Started by VladimirT, April 19, 2013, 09:57:25 AM

Previous topic - Next topic

VladimirT

Need help with PWM on OLinuXino-MAXI. I am trying to enable PWM0 and PWM1 on U_DEBUG to manage an external motor driver by doing following steps:

1.  // Write to HW_PINCTRL_MUXSELx
    // register bit to select pin as PWM.
    // set value to register 0x80018130
    set(0x80018130 0x200fffff);
2.  // Specify PWM parametrs
    // Active and Inactive for PWM0, PWM1
    set(0x80064010, 0x000f000f);
    set(0x80064030, 0x000f000f);
    // Period for PWM0, PWM1
    set(0x80064020, 0x00b0020);
    set(0x80064040, 0x00b0020);
3.  // Enable PWM0, PWM1
    set(0x80064000, 0x1f);
As result I have ~1.1 V on PWM0 and 0 V on PWM1, but expecting to have the same value. Any advice is appriciated.

TVC

Hej

I am doing the same for the IMX233 micro.

gpio_wr_value(HW_PINCTRL_MUXSEL3_CLR, 0x00f00000);

pwm_wr(HW_PWM_ACTIVE1_SET, 0b00001100100000000000011001000000);
pwm_wr(HW_PWM_ACTIVE1_CLR, 0b11110011011111111111100110111111);

pwm_wr(HW_PWM_PERIOD1_SET, 0b00000000000010110000110010000000);
pwm_wr(HW_PWM_PERIOD1_CLR, 0b00000001111100001111001101111111);

pwm_wr(HW_PWM_CTRL_SET, 0x06000002);
pwm_wr(HW_PWM_CTRL_CLR, 0b11111000000000000000000000011100);

I use the 32kHz cristal and want to make a 10Hz signal by setting the period on 0xC80.
I use only the SET en CLR registers because in some registers are reserved bits. By using the SET en CLR registers you ensure that you don't write in those reserved bits.
I also get a constant signal...

VladimirT

Sorry, did not help. Values on PWM0 and PWM1 are still different

jlumme

Hi VladimirT & TVC,

Did you guys manage to get PWM working for you ?
Can you give me an example how to use it ?
I want to generate a steady 37kHz clock, and I guess with external 24kHz oscillator and divisors (8&16) I could get 1kHz signal from where it would be simple to create a signal I want..

Using this PWM is a new concept for me, and I am having difficulty wrapping my head around it.. Do you use interrupts, or how do you get the pulse from PWM ? Is the code executed in user space ?

jlumme

Does anyone have any ideas what might be the problem why I can't get PWM2 to work on Olinuxino maxi?
I'm not changing from the default 24MHz source.

I'm trying to get about 37kHz signal to PWM 2.


imx233_wr(HW_PWM_CTRL_CLR,0x80000000); //Clear the SFTRST
usleep(500000);
imx233_wr(HW_PWM_CTRL_CLR,0x40000000); //Clear the CLKGATE
usleep(500000);
imx233_wr(HW_PWM_CTRL_SET,0x80000000); //Set the SFTRST
usleep(500000);
imx233_wr(HW_PWM_CTRL_CLR,0x80000000); //Clear the SFTRST
usleep(500000);
imx233_wr(HW_PWM_CTRL_CLR,0x40000000); //Clear the CLKGATE
usleep(500000);

//Set the desired state
imx233_wr(HW_PINCTRL_MUXSEL3_CLR, 0x03000000); //Set b1p28 to pwm2
imx233_wr(HW_PWM_ACTIVE2, 0x01410000); //0x141 = 321 (50% of 1/24MHz)
imx233_wr(HW_PWM_PERIOD2, 0x000b0282); //set active high, inact low. and period
imx233_wr(HW_PWM_CTRL_SET, 0x00000004); //PWM2 only(4)


There is some output after each step, to confirm that settings were set properly:

HW_PINCTRL_MUXSEL3 [0x3cafffff] after set
read HW_PWM_ACTIVE2 [0x01410000] after set
read HW_PWM_PERIOD2 [0x000b0282] after set
read HW_PWM_CTRL_SET [0x3e000004] after set


I'm measuring from the pin 91 (that goes to the GPIO_CON PIN22 on maxi), but all I see is zero voltage always :|

jlumme

Ok I found the problem. It turns out that (I'm assuming) u-boot sets the HW_CLKCTRL_XTAL PWM bit to 1, and thus PWM was never receiving any ticks from XTAL.. (ref.manual p96)
After clearing it, all is well, and now I can start working out how I can actually use this new and sweetly accurate signal :)
Hope it helps someone as well!

edurdag

hi, i read your pwm workshop, i am new and i learn ansi c, can you send your pwm programming code because i want to drive some devices via pwm, it is only education purposes, thanks for interesting...Edip

Evgeny Boger

By the way, PWM is supported by kernel driver on newer kernels (https://www.olimex.com/forum/index.php?topic=2103.0 ).

szakun

Its been a long time, but got a similar problem with running any of PMW0,1,2 on imx233 Maxi board.

The code for PWM2 is as follows:
        imx233_wr(HW_PWM_CTRL, 0x0);  //reset register
        usleep(500000);
        imx233_wr(HW_PWM_CTRL_SET, 0x80000000);  //Set the SFTRST
        usleep(500000);
        imx233_wr(HW_PWM_CTRL_CLR, 0x80000000);  //Clear the SFTRST
        usleep(500000);
        imx233_wr(HW_PWM_CTRL_CLR, 0x40000000);  //Clear the CLKGATE
        usleep(500000);

        imx233_wr(HW_CLKCTRL_XTAL_SET, 1 << 29); // enable XTAL for PWM
        usleep(500000);



        imx233_wr(HW_PINCTRL_MUXSEL3_CLR, 0x03000005); //Set pin as PWM2 output
        usleep(100000);

        imx233_wr(HW_PWM_ACTIVE2, 0x00000100);
        usleep(100000);
        imx233_wr(HW_PWM_PERIOD2, 0x000b0200); //set active high, inact low. and period
        usleep(100000);

        imx233_wr(HW_PWM_CTRL_SET, 1 << 2); //PWM2 only
        usleep(100000);
        imx233_wr(HW_PWM_CTRL_SET, 1 << 5); //PWM2 only(PWM2_ENABLE)
        usleep(100000);



Olinuxino is running Linux OlinuXino 3.16.2-1-ARCH #1 PREEMPT.


What else can be the cause of not working module? Is looks that those PWM0-2 pins are not changing status after setting MUXSEL register. Any ideas?