KL25Z PWM Driver and Demo

ChibiOS public support forum for topics related to the Freescale Kinetis family of micro-controllers.

Moderator: utzig

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

KL25Z PWM Driver and Demo

Postby aport » Fri Nov 07, 2014 2:09 am

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!
Attachments
kl25z-pwm.patch.zip
(11.96 KiB) Downloaded 99 times

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: KL25Z PWM Driver and Demo

Postby utzig » Fri Nov 07, 2014 2:19 am

I have both equipment and will test this tomorrow or in the weekend.

Cheers,
Fabio Utzig

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

Re: KL25Z PWM Driver and Demo

Postby aport » Fri Nov 07, 2014 4:49 am

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!

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

Re: KL25Z PWM Driver and Demo

Postby aport » Fri Nov 07, 2014 9:34 pm

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!

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

Re: KL25Z PWM Driver and Demo

Postby aport » Sun Nov 09, 2014 10:53 am

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.

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: KL25Z PWM Driver and Demo

Postby utzig » Sun Nov 09, 2014 1:02 pm

I fixed the TMP* typos.

Cheers,
Fabio Utzig

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

Re: KL25Z PWM Driver and Demo

Postby aport » Tue Nov 11, 2014 8:18 am

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
Attachments
kinetis-pwm.patch.zip
PWM Driver version 2
(12.1 KiB) Downloaded 100 times

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: KL25Z PWM Driver and Demo

Postby utzig » Tue Nov 11, 2014 12:40 pm

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

aport
Posts: 27
Joined: Sat May 10, 2014 7:35 pm

Re: KL25Z PWM Driver and Demo

Postby aport » Tue Nov 11, 2014 8:31 pm

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
Attachments
kinetis-pwm-interrupt.patch.zip
(1.52 KiB) Downloaded 105 times

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: KL25Z PWM Driver and Demo

Postby utzig » Tue Nov 11, 2014 10:06 pm

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


Return to “Kinetis Support”

Who is online

Users browsing this forum: No registered users and 2 guests