This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Linux/AM5728: How can I make the timer16 output as PWM

Part Number: AM5728

Tool/software: Linux

I want to write a driver for timer to make it output as PWM. Is there a example that I can refer to? Please help me .

  • Hello,

    We do not have any examples in our PSDK Linux that enables timer was PWM. However, on the following thread, a colleague of mine shares useful information that could be used as a reference.

    Regards,

    Krunal

  • Thanks for ypu reply.

    I have check the thread and it didnit help me.

    I have test the devmem2 to read the timer 16 TCLR register but the core dumped. And the log is following:

    root@am57xx-evm:~# devmem2 0x4882e038
    /dev/mem opened.[ 2969.314585] ------------[ cut here ]------------
    [ 2969.320515] WARNING: CPU: 0 PID: 1139 at drivers/bus/omap_l3_noc.c:147 l3_interrupt_handler+0x254/0x370
    [ 2969.329951] 44000000.ocp:L3 Custom Error: MASTER MPU TARGET L4_PER3_P3 (Read): Data Access in User mode during Functional access
    [ 2969.341563] Modules linked in: creat_dev(O) sha512_generic sha512_arm sha256_generic sha1_generic sha1_arm_neon sha1_arm md5 cbc aes_arm_bs crypto_simd cryptd xfrm)
    [ 2969.392913] CPU: 0 PID: 1139 Comm: devmem2 Tainted: G      D W  O    4.14.79-ge669d52447 #65
    [ 2969.401386] Hardware name: Generic DRA74X (Flattened Device Tree)
    [ 2969.407504] Backtrace: 
    [ 2969.409971] [<c020b4dc>] (dump_backtrace) from [<c020b7c0>] (show_stack+0x18/0x1c)
    [ 2969.417576]  r7:00000009 r6:600b0193 r5:00000000 r4:c10540d0
    [ 2969.423268] [<c020b7a8>] (show_stack) from [<c0934428>] (dump_stack+0x90/0xa4)
    [ 2969.430526] [<c0934398>] (dump_stack) from [<c022b9cc>] (__warn+0xec/0x104)
    [ 2969.437519]  r7:00000009 r6:c0bd51bc r5:00000000 r4:ed6b1dd0
    [ 2969.443204] [<c022b8e0>] (__warn) from [<c022ba24>] (warn_slowpath_fmt+0x40/0x48)
    [ 2969.450721]  r9:0000001b r8:ef20e8d0 r7:c0bd5028 r6:00000002 r5:c0bd50e8 r4:c0bd518c
    [ 2969.458500] [<c022b9e8>] (warn_slowpath_fmt) from [<c051bd20>] (l3_interrupt_handler+0x254/0x370)
    [ 2969.467409]  r3:ef20e740 r2:c0bd518c
    [ 2969.470997]  r4:80080003
    [ 2969.473545] [<c051bacc>] (l3_interrupt_handler) from [<c027f5a4>] (__handle_irq_event_percpu+0x6c/0x134)
    [ 2969.483067]  r10:c1055acd r9:ef20d500 r8:00000017 r7:ed6b1ef4 r6:00000000 r5:ef20d500
    [ 2969.490928]  r4:ef20ec40
    [ 2969.493476] [<c027f538>] (__handle_irq_event_percpu) from [<c027f690>] (handle_irq_event_percpu+0x24/0x60)
    [ 2969.503172]  r10:00000002 r9:00000001 r8:ef008000 r7:00000000 r6:c10093ac r5:ef20d500
    [ 2969.511034]  r4:ef20d500
    [ 2969.513581] [<c027f66c>] (handle_irq_event_percpu) from [<c027f70c>] (handle_irq_event+0x40/0x64)
    [ 2969.522489]  r5:ef20d564 r4:ef20d500
    [ 2969.526084] [<c027f6cc>] (handle_irq_event) from [<c0283020>] (handle_fasteoi_irq+0xac/0x160)
    [ 2969.534645]  r7:00000000 r6:c10093ac r5:ef20d564 r4:ef20d500
    [ 2969.540332] [<c0282f74>] (handle_fasteoi_irq) from [<c027e7f4>] (generic_handle_irq+0x2c/0x3c)
    [ 2969.548981]  r7:00000000 r6:00000000 r5:00000017 r4:c0e64b3c
    [ 2969.554666] [<c027e7c8>] (generic_handle_irq) from [<c027ed7c>] (__handle_domain_irq+0x64/0xbc)
    [ 2969.563404] [<c027ed18>] (__handle_domain_irq) from [<c0201474>] (gic_handle_irq+0x44/0x80)
    [ 2969.571793]  r9:00000001 r8:fa213000 r7:fa212000 r6:ed6b1fb0 r5:fa21200c r4:c1004000
    [ 2969.579571] [<c0201430>] (gic_handle_irq) from [<c020c6e8>] (__irq_usr+0x48/0x60)
    [ 2969.587084] Exception stack(0xed6b1fb0 to 0xed6b1ff8)
    [ 2969.592157] 1fa0:                                     000103e3 b6fc4960 bea8ba3c b6fc4b18
    [ 2969.600371] 1fc0: 00000000 00021048 b6fc1d40 bea8b9a8 000103e3 00000001 00000002 bea8ba90
    [ 2969.608584] 1fe0: 000103cc bea8b988 b6fa6efb b6fa3286 200b0030 ffffffff
    [ 2969.615227]  r9:00000001 r8:30c5387d r7:30c5387d r6:ffffffff r5:200b0030 r4:b6fa3286
    [ 2969.623003] ---[ end trace 95310129dc5056a9 ]---
    
    Memory mapped at address 0xb6fc0000.
    [ 2969.627775] Unhandled fault: asynchronous external abort (0x1211) at 0x00000000
    [ 2969.638504] pgd = ed69d480
    [ 2969.641222] [00000000] *pgd=ad499003, *pmd=be374003
    Bus error (core dumped)
    root@am57xx-evm:~# 
    CTRL-A Z for help | 115200 8N1 | NOR | Minicom 2.7.1 | VT102 | Offline | ttyUSB0            

    I have no idea  to solve it. Please help me.

    I had also test another way, and the code is like following:
    #define TCLR 0x4882E038
    #define TLDR 0x4882E040
    #define TMAR 0x4882E04C
    
    static unsigned int *tclr;
    
    static long timer_pwm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    {
    	unsigned long temp;
    	//unsigned long TCLR;
    	//unsigned long TLDR;
    	//unsigned long TMAR;
    	//unsigned long *tclr;
    	
    
    	printk("start the timer");
    	tclr=ioremap(TCLR,4);
    	printk("ioremap");
    	temp = __raw_readl(tclr);
    	printk("TCLR=%lx",temp);
    	
    	return 0;
    }

    And when I test it use the ioctl, the output is like this:

    [ 3168.014133] start the timer
    [ 3168.014142] ioremap
    [ 3168.016963] ------------[ cut here ]------------
    [ 3168.023711] WARNING: CPU: 0 PID: 1148 at drivers/bus/omap_l3_noc.c:147 l3_interrupt_handler+0x254/0x370
    [ 3168.033146] 44000000.ocp:L3 Custom Error: MASTER MPU TARGET L4_PER3_P3 (Read): Data Access in User mode during Functional access
    [ 3168.044756] Modules linked in: creat_dev(O) sha512_generic sha512_arm sha256_generic sha1_generic sha1_arm_neon sha1_arm md5 cbc aes_arm_bs crypto_simd cryptd xfrm)
    [ 3168.096103] CPU: 0 PID: 1148 Comm: timer_pwm.out Tainted: G      D W  O    4.14.79-ge669d52447 #65
    [ 3168.105098] Hardware name: Generic DRA74X (Flattened Device Tree)
    [ 3168.111217] Backtrace: 
    [ 3168.113683] [<c020b4dc>] (dump_backtrace) from [<c020b7c0>] (show_stack+0x18/0x1c)
    [ 3168.121288]  r7:00000009 r6:600a0193 r5:00000000 r4:c10540d0
    [ 3168.126978] [<c020b7a8>] (show_stack) from [<c0934428>] (dump_stack+0x90/0xa4)
    [ 3168.134235] [<c0934398>] (dump_stack) from [<c022b9cc>] (__warn+0xec/0x104)
    [ 3168.141227]  r7:00000009 r6:c0bd51bc r5:00000000 r4:ed6cfc78
    [ 3168.146912] [<c022b8e0>] (__warn) from [<c022ba24>] (warn_slowpath_fmt+0x40/0x48)
    [ 3168.154428]  r9:0000001b r8:ef20e8d0 r7:c0bd5028 r6:00000002 r5:c0bd50e8 r4:c0bd518c
    [ 3168.162207] [<c022b9e8>] (warn_slowpath_fmt) from [<c051bd20>] (l3_interrupt_handler+0x254/0x370)
    [ 3168.171117]  r3:ef20e740 r2:c0bd518c
    [ 3168.174704]  r4:80080003
    [ 3168.177252] [<c051bacc>] (l3_interrupt_handler) from [<c027f5a4>] (__handle_irq_event_percpu+0x6c/0x134)
    [ 3168.186775]  r10:c1055acd r9:ef20d500 r8:00000017 r7:ed6cfd9c r6:00000000 r5:ef20d500
    [ 3168.194636]  r4:ef20ec40
    [ 3168.197184] [<c027f538>] (__handle_irq_event_percpu) from [<c027f690>] (handle_irq_event_percpu+0x24/0x60)
    [ 3168.206880]  r10:ed449070 r9:ed6ce000 r8:ef008000 r7:00000000 r6:c10093ac r5:ef20d500
    [ 3168.214742]  r4:ef20d500
    [ 3168.217289] [<c027f66c>] (handle_irq_event_percpu) from [<c027f70c>] (handle_irq_event+0x40/0x64)
    [ 3168.226199]  r5:ef20d564 r4:ef20d500
    [ 3168.229792] [<c027f6cc>] (handle_irq_event) from [<c0283020>] (handle_fasteoi_irq+0xac/0x160)
    [ 3168.238353]  r7:00000000 r6:c10093ac r5:ef20d564 r4:ef20d500
    [ 3168.244039] [<c0282f74>] (handle_fasteoi_irq) from [<c027e7f4>] (generic_handle_irq+0x2c/0x3c)
    [ 3168.252688]  r7:00000000 r6:00000000 r5:00000017 r4:c0e64b3c
    [ 3168.258373] [<c027e7c8>] (generic_handle_irq) from [<c027ed7c>] (__handle_domain_irq+0x64/0xbc)
    [ 3168.267111] [<c027ed18>] (__handle_domain_irq) from [<c0201474>] (gic_handle_irq+0x44/0x80)
    [ 3168.275499]  r9:ed6ce000 r8:fa213000 r7:fa212000 r6:ed6cfe58 r5:fa21200c r4:c1004000
    [ 3168.283276] [<c0201430>] (gic_handle_irq) from [<c020c378>] (__irq_svc+0x58/0x8c)
    [ 3168.290789] Exception stack(0xed6cfe58 to 0xed6cfea0)
    [ 3168.295861] fe40:                                                       00000000 2e7d9000
    [ 3168.304073] fe60: 00000000 ed6ce000 c0e5e420 bf33d0b4 ed6cfeec 00000000 00000003 ed6ce000
    [ 3168.312285] fe80: ed449070 ed6cfecc ed6cfea8 ed6cfea8 c027df4c c027e640 600a0013 ffffffff
    [ 3168.320499]  r9:ed6ce000 r8:00000003 r7:ed6cfe8c r6:ffffffff r5:600a0013 r4:c027e640
    [ 3168.328277] [<c027e5a4>] (vprintk_func) from [<c027df4c>] (printk+0x28/0x30)
    [ 3168.335357]  r7:00000001 r6:00000003 r5:ee7029c0 r4:bf33e1c0
    [ 3168.341047] [<c027df28>] (printk) from [<bf33c068>] (timer_pwm_ioctl+0x54/0x64 [creat_dev])
    [ 3168.349434]  r3:fa82e038 r2:00000000 r1:00000000 r0:bf33d0b4
    [ 3168.355124] [<bf33c014>] (timer_pwm_ioctl [creat_dev]) from [<c034ed3c>] (do_vfs_ioctl+0xa8/0x790)
    [ 3168.364120]  r5:ee7029c0 r4:00000000
    [ 3168.367714] [<c034ec94>] (do_vfs_ioctl) from [<c034f460>] (SyS_ioctl+0x3c/0x60)
    [ 3168.375055]  r10:00000036 r9:ed6ce000 r8:00000000 r7:00000001 r6:00000003 r5:ee7029c0
    [ 3168.382916]  r4:ee7029c0
    [ 3168.385465] [<c034f424>] (SyS_ioctl) from [<c0207c40>] (ret_fast_syscall+0x0/0x4c)
    [ 3168.393068]  r9:ed6ce000 r8:c0207e44 r7:00000036 r6:00000000 r5:00000000 r4:beb64b48
    [ 3168.400843] ---[ end trace 95310129dc5056aa ]---
    [ 3168.405542] Unhandled fault: asynchronous external abort (0x1211) at 0x00000000
    [ 3168.412885] pgd = ed49ffc0
    [ 3168.415604] [00000000] *pgd=ad6ab003, *pmd=be38e003
    [ 3168.420517] Internal error: : 1211 [#3] PREEMPT SMP ARM
    [ 3168.425763] Modules linked in: creat_dev(O) sha512_generic sha512_arm sha256_generic sha1_generic sha1_arm_neon sha1_arm md5 cbc aes_arm_bs crypto_simd cryptd xfrm)
    [ 3168.477095] CPU: 0 PID: 1148 Comm: timer_pwm.out Tainted: G      D W  O    4.14.79-ge669d52447 #65
    [ 3168.486092] Hardware name: Generic DRA74X (Flattened Device Tree)
    [ 3168.492210] task: ed4f1440 task.stack: ed6ce000
    [ 3168.496762] PC is at vprintk_func+0x9c/0x1ac
    [ 3168.501049] LR is at printk+0x28/0x30
    [ 3168.504726] pc : [<c027e640>]    lr : [<c027df4c>]    psr: 600a0013
    [ 3168.511019] sp : ed6cfea8  ip : ed6cfea8  fp : ed6cfecc
    [ 3168.516265] r10: ed449070  r9 : ed6ce000  r8 : 00000003
    [ 3168.521512] r7 : 00000000  r6 : ed6cfeec  r5 : bf33d0b4  r4 : c0e5e420
    [ 3168.528065] r3 : ed6ce000  r2 : 00000000  r1 : 2e7d9000  r0 : 00000000
    [ 3168.534621] Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
    [ 3168.541787] Control: 30c5387d  Table: ad49ffc0  DAC: fffffffd
    [ 3168.547556] Process timer_pwm.out (pid: 1148, stack limit = 0xed6ce210)
    [ 3168.554197] Stack: (0xed6cfea8 to 0xed6d0000)
    [ 3168.558573] fea0:                   bf33c044 ed8b0770 bf33e1c0 ee7029c0 00000003 00000001
    [ 3168.566787] fec0: ed6cfee4 ed6cfed0 c027df4c c027e5b0 c0216970 ed6cfeec ed6cff0c ed6cfef8
    [ 3168.575000] fee0: bf33c068 c027df34 bf33d0b4 00000000 00000000 fa82e038 00000000 ee7029c0
    [ 3168.583213] ff00: ed6cff7c ed6cff10 c034ed3c bf33c020 00000002 ee30b000 ee7029c0 ee30b000
    [ 3168.591427] ff20: 00000020 ee7029c8 ed6cff4c ed6cff38 c034b4e4 c032a86c 00000003 ee7029c0
    [ 3168.599640] ff40: ed6cff94 ed6cff50 c033a1f8 c034b490 00000000 ee7029c0 ee7029c0 00000003
    [ 3168.607854] ff60: 00000001 00000000 ed6ce000 00000036 ed6cffa4 ed6cff80 c034f460 c034eca0
    [ 3168.616066] ff80: beb64b48 00000000 00000000 00000036 c0207e44 ed6ce000 00000000 ed6cffa8
    [ 3168.624279] ffa0: c0207c40 c034f430 beb64b48 00000000 00000003 00000001 00000000 00000001
    [ 3168.632492] ffc0: beb64b48 00000000 00000000 00000036 00000000 00000000 b6fcc000 00000000
    [ 3168.640705] ffe0: b6f34191 beb64b14 000104a7 b6f34198 800a0030 00000003 00000000 00000000
    [ 3168.648913] Backtrace: 
    [ 3168.651377] [<c027e5a4>] (vprintk_func) from [<c027df4c>] (printk+0x28/0x30)
    [ 3168.658456]  r7:00000001 r6:00000003 r5:ee7029c0 r4:bf33e1c0
    [ 3168.664144] [<c027df28>] (printk) from [<bf33c068>] (timer_pwm_ioctl+0x54/0x64 [creat_dev])
    [ 3168.672531]  r3:fa82e038 r2:00000000 r1:00000000 r0:bf33d0b4
    [ 3168.678221] [<bf33c014>] (timer_pwm_ioctl [creat_dev]) from [<c034ed3c>] (do_vfs_ioctl+0xa8/0x790)
    [ 3168.687218]  r5:ee7029c0 r4:00000000
    [ 3168.690812] [<c034ec94>] (do_vfs_ioctl) from [<c034f460>] (SyS_ioctl+0x3c/0x60)
    [ 3168.698154]  r10:00000036 r9:ed6ce000 r8:00000000 r7:00000001 r6:00000003 r5:ee7029c0
    [ 3168.706014]  r4:ee7029c0
    [ 3168.708563] [<c034f424>] (SyS_ioctl) from [<c0207c40>] (ret_fast_syscall+0x0/0x4c)
    [ 3168.716167]  r9:ed6ce000 r8:c0207e44 r7:00000036 r6:00000000 r5:00000000 r4:beb64b48
    [ 3168.723944] Code: e7927001 e5932004 e2422001 e3520000 (e5832004) 
    [ 3168.730068] ---[ end trace 95310129dc5056ab ]---
    [ 3168.734773] note: timer_pwm.out[1148] exited with preempt_count 1
    Segmentation fault
    [ 3168.770789] thermal thermal_zone5: failed to read out thermal zone (-121)
    

    My program is stuck here. And I have no idea to solve this, please help me.





  • Can someone help me ?
  • Do you have the TIMER16 clock enabled? To test if this is the issue, you can comment your changes in the timer16 driver and boot your device. From user space use devmem2 to enable TIMER16 from the PRCM (program CM_L4PER3_TIMER16_CLKCTRL accordingly) and then try to read TIMER16_TCLR with devmem2.

    Best Regards,
    Yordan
  • I have write the CM_L4PER3_TIMER16_CLKCTRL by devmem2 0x4a009830 w 0x01030002. Then I use devmem2 0x4882e038 and the core dumped again. How can I solve it?

  • Hi,

    Which clock do you have as a source for TIMER16_FCLK? Also have you enabled (status="okay") the timer in dts?

    Best Regards,
    Yordan
  • The source is FUNC_32K_CLK. And I have enabled the timer.
  • Can you give me some advices?
  • Is there some updating? My job is stuck here and please help me.
  • Does your new thread CPTS unable to obtain a time stamp mean that you have resolved your timer problem?

    Regards,

    Nick

  • Yes, I use another way use the timer. But, I still can't use the devmem2 function or __raw_readl to read the timer16 register .
  • Hello,

    Could you please share your DTS changes?

    Regards,
    Krunal
  • Yes. And, is there some advice about the question?

    6445.am57xx-evm-reva3.zip

  • Hello,

    Recently, we helped a customer enable PWM using timer15 and here is the reference thread. Also, the devmem2 behavior is expected because no one is enabling the timer clock.

    Regards,
    Krunal 

  • Thanks for you reply.

    As you said, if no one is enabling the timer clock, what should I do to enable the timer  clock?

  • Hello,

    Could you please explain why you are trying to use devmem2 to control Timer16? I believe you should let the PWM driver communicate with the PRCM to set the appropriate registers.

    Regards,
    Krunal

  • Yes, the PWM driver worked. I just want to konw the operating mechanis of the timer. 

  • Hello,

    As an experiment, I launched my AM572x board and after booting Linux, I ran the following script in CCS (README available here). The script will create a rd1 file and please load the file in the Clock Tree Tool. The tool will display all the clocks enabled/disabled based on your current setup. My showed up as the following:

    I noticed TIMER16_GFCLK was disabled and to enable the clock, I used the following command "devmem2 0x4a009830 w 0x7030002". Next, I noticed the first read to timer16 register gave me bunch of warnings. This may be due to Linux not liking the fact we are hacking to enable the timer16 clock . But, after the initial warnings, I have been able to read/write to TIMER16 registers without any problems.

    Please keep in mind that devmem2 is not the recommended way of writing to the timer16 register. The PWM driver ensures the appropriate clocks are written in the correct sequence. 

    Regards,
    Krunal 

  • Thanks very much. This perfectly answered my question.