Page 1 of 2

KL25Z PWM Driver and Demo

Posted: Fri Nov 07, 2014 2:09 am
by aport
Hello!

This is my first ChibiOS HAL driver, so I would really appreciate any and all feedback. It's not complete (no pwm/channel interrupt support) but PWM output does work.
I just wanted to share what I've done so far just in case anyone else finds it useful.

I don't have a logic analyzer or scope here at home so I can't verify that the timing is proper, but I went over the datasheet a million and a half times and the code looks correct.

The demo is for the Freedom KL25Z board. It activates the accelerometer and modulates the RGB LED depending on board orientation.

After completing the driver with channel interrupt support, I'll be looking at the ICU driver next since the same TPM block is used.

Have fun!

Re: KL25Z PWM Driver and Demo

Posted: Fri Nov 07, 2014 2:19 am
by utzig
I have both equipment and will test this tomorrow or in the weekend.

Cheers,
Fabio Utzig

Re: KL25Z PWM Driver and Demo

Posted: Fri Nov 07, 2014 4:49 am
by aport
utzig wrote:I have both equipment and will test this tomorrow or in the weekend.

Cheers,
Fabio Utzig


You rock! I'll try to grab my Saleae from work for this weekend... working blind is a pain.

I appreciate you testing this and I especially appreciate the work you did for the Kinetis port!

Re: KL25Z PWM Driver and Demo

Posted: Fri Nov 07, 2014 9:34 pm
by aport
Okay, so I hooked up my Saleae at work and here's what I got:

Frequency: 12000000
Period: 65535
Measured PWM frequency: 183.1 Hz

Frequency: 12000000
Period: 20000
Measured PWM frequency: 599.97 (sometimes 600 even) Hz

Frequency: 6000000
Period: 30000
Measured PWM frequency: 199.99 Hz

I set duty cycle to 20% using

Code: Select all

pwmEnableChannel(&PWMD3, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD3, 2000));


At 200Hz I measured 1ms low to 4ms high on for the 5ms period (my configuration is PWM_OUTPUT_ACTIVE_LOW).

Image

Everything looks right!

Re: KL25Z PWM Driver and Demo

Posted: Sun Nov 09, 2014 10:53 am
by aport
I finished the support for periodic and channel notifications via interrupts... it's way past my bedtime so I'll work up a new patch set tomorrow.

I've been fighting with the kl25z.h CMSIS header... a lot of TPM references are typed TMP. I also made a change to the TPM_TypeDef structure to ease access to channel SC and V registers.

Re: KL25Z PWM Driver and Demo

Posted: Sun Nov 09, 2014 1:02 pm
by utzig
I fixed the TMP* typos.

Cheers,
Fabio Utzig

Re: KL25Z PWM Driver and Demo

Posted: Tue Nov 11, 2014 8:18 am
by aport
Fabio,

Thank you! I've attached the finished PWM driver and demo.

The stuff in kinetis_tpm.h might belong in kl25z.h... I wasn't sure. If you would prefer the defines in the CMSIS header, let me know and I'll move them and resubmit.

Thank you for helping me with this, I really appreciate it!

Regards,
Adam

Re: KL25Z PWM Driver and Demo

Posted: Tue Nov 11, 2014 12:40 pm
by utzig
Adam,

I commited the HAL driver. I also changed the TPM typedef to enable channel access with C[channel_number].SC, etc. I have not commited the demo yet because I want to merge it with the current main demo.

Please update your SVN repo and review that everything is ok, I tested it here but always good having a second opinion.

Would also be cool if you could implement:

Code: Select all

  void pwm_lld_enable_periodic_notification(PWMDriver *pwmp);
  void pwm_lld_disable_periodic_notification(PWMDriver *pwmp);
  void pwm_lld_enable_channel_notification(PWMDriver *pwmp,
                                           pwmchannel_t channel);
  void pwm_lld_disable_channel_notification(PWMDriver *pwmp,
                                            pwmchannel_t channel);


Ah, the other Kinetis MCUs (not KL) use a slightly different subsystem called Flexible Timer Module, or something like that, which is a superset of the TPM.

Cheers,
Fabio Utzig

Re: KL25Z PWM Driver and Demo

Posted: Tue Nov 11, 2014 8:31 pm
by aport
Hi Fabio,

The second version of the patch, attached to my last post, has interrupt and periodic/channel notification support.

What I'll do is update my repository, and make a new incremental patch against what you've checked in.

Edit: Attached

The main HAL pwm.c does channel bounds checking, so I'm not sure if we need it in the lld as well. But I've followed your examples and added them to the channel notification functions.

Thank you,
Adam

Re: KL25Z PWM Driver and Demo

Posted: Tue Nov 11, 2014 10:06 pm
by utzig
The main HAL pwm.c does channel bounds checking, so I'm not sure if we need it in the lld as well. But I've followed your examples and added them to the channel notification functions.


Totally right. I removed the extra checking.

Sorry for the previous mess up and thanks for everything!

Cheers,
Fabio Utzig