Teensy 3.1 / MK20DX256VLH7 support

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

Moderator: utzig

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Tue Jan 20, 2015 6:14 pm

Hi,

I am new to ChibiOS and trying to port the current (Github master) branch to Teensy3.1; my goals are to have: I2C master/slave (Tsy3.1 <=> Tsy3.1), SPI master, USB slave, ADC, PWM, and Flash (EEPROM-like operations) working.

Since there is already some support for the Teensy3.0, I thought it would be quite easy to run the demo... Well, not so easy after all: Teensy3.0 uses a K20_50 CPU (MK20DX128VLH5) while Teensy3.1 uses a K20_72 CPU (MK20DX256VLH7). There's a more complete list of the differences on the official website: https://www.pjrc.com/teensy/teensy31.html.

My main problem was the Vector table is not the same (46 IRQ for Tsy3.0, 94 for Tsy3.1). The exisiting code in the K20x folders specializes for the K20D5 family. Fixing that led me to create new subfolders (I now have K20x, K20D7, KL2x) into the KINETIS folders. The demo is running fine now (with the TestThread) but I don't think this is the right way to go... Most of the HAL code will be the same for both boards (3.0 and 3.1); the differences will be in clock initialization, Vector tables and available peripherals (more DMA, more I2C/SPI, CAN,DAC... on the Teensy3.1).

I guess there is a way to keep it simple but I don't really know how you want to do that.

Also, the TestThread fails on both Teensy3.0 and Teensy3.1, on one testcase:

Code: Select all

--- Test Case 1.4 (Threads, delays)
--- Result: FAILURE (#1 [])

I don't know why nor how to fix that, and delays looks like they are fine (The Onboard LED blinks at a correct rate, checked with an analog scope). l I had to disable LTO in the Makefiles for the demo to compile... I don't know if that's why it fails now.

I join the diff file to this post in case it helps; sorry for the mess :roll:

Any advice or help is welcome; until then, I will keep working on my folder so it doesn't invades the upstream code.

Thanks for ChibiOS, awesome project !

RedoX
Attachments
add_tsy3_1.diff.gz
Add support for Teensy3.1
(50.44 KiB) Downloaded 458 times
Last edited by RedoX on Tue Jan 20, 2015 6:38 pm, edited 1 time in total.

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

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby utzig » Tue Jan 20, 2015 6:37 pm

There's a lot of duplication in this patch! I'll review later and see how to generalize the changes like the clock initialization in a way that we can enable mostly shared code between all K20 cpus.

Cheers,
Fabio Utzig

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Tue Jan 20, 2015 6:42 pm

utzig wrote:There's a lot of duplication in this patch!


Yes, it's complete duplication, I just copied the K20x folder and adapted. It is not meant to be a patch for ChibiOS, but to give you my current setup (and also spare you the boring task of updating the vector table :P ) so you could re-create quickly my setup with a Teensy3.1 if you have one.

Thanks

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Thu Jan 22, 2015 1:30 am

Quick update,

I just finished porting the pwm_lld from the KL2x folder to the K20D7. KL2x uses the TPM timer, while the K20D5/7 series uses the FlexTimer (FTM); the FTM is supposed to be backward compatible with the TPM so most of the code is just adapted; however, using just this mode, I ran into some issues so I had to update the code a little bit to use the new synchronization mechanism. I only tested FTM0 but the others should work fine too.

I also updated a few things since my previous post (IRQ table, peripheral definitions, ...). It's still a little bit messy but I can send them if you want...
Attachments
tsy3_pwm_lld.tar.gz
Low Level PWM driver for K20D7 series using the FlexTimer module
(4.1 KiB) Downloaded 464 times

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

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby utzig » Thu Jan 22, 2015 12:09 pm

How hard would it be to merge the TPMx based PWM driver with this one by using the relevant KINETIS_PWM_USE_(FTMx|TPMx) flags?

I guess we could merge them both and move it to the LLD directory to be shared by both MCUs. What do you think?

Cheers,
Fabio Utzig

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Thu Jan 22, 2015 9:36 pm

Hmm, I'm not sure it's the best thing to do, with my code at least:
> Initialization is different
> Updates are different, a flag needs to be added after MOD or CnV (channel duty cycle) is updated
> the IRQ handling is also different, the flags have moved from one single register (KL20x) to multiple ones (K20x)

Sure, we could handle it with preprocessor macros, but I don't see the point in merging two drivers into one file. One thing we can do is split the PWM driver in two: a TPM-based one, and a FTM-based one. Both could be moved in LLD though, and then included according to the MCU capabilities. I think finding a way to re-work/re-organize the K20x folder so it includes K20D5 and K20D7 (and the others) would help morefor now (and not only because I brought it up in the first place :P ).

Another option would be to have the FTM work in TPM-compatibility mode. I ran into some strange synchronization issues (Updating CnSC after CnV caused CnV to be screwed up, same for MOD and SC), but the datasheed says the FTM is backward compatible with the TPM... Maybe I just missed something but honestly, I spent a lot of time trying this option before moving on to the current code to make sure the high-level interface would work fine with either the TPM or FTM.

BTW, I tried the ADC, the I2C master, the SPI master, and Serial. All appear to work fine :)
I still have to port I2C slave, which might be tricky (since the I2C slave implementation I currently have, outside of ChibiOS, relies on a tricky hack with PORTB_ISR), and USB, which might also be tricky (just because it's USB), but I like working with ChibiOS; thanks again for the work !

Cheers

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

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby utzig » Fri Jan 23, 2015 10:57 am

Ah ok, I thought it might be more straight-forward. Thanks for the explanation!

RedoX wrote:I still have to port I2C slave, which might be tricky (since the I2C slave implementation I currently have, outside of ChibiOS, relies on a tricky hack with PORTB_ISR), and USB, which might also be tricky (just because it's USB)


Just out of curiosity, "port" from where?

Cheers,
Fabio Utzig

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Fri Jan 23, 2015 11:12 am

Hi,

I have bare-metal drivers for both I2C and USB, derivated from the "official" software stack developped by Paul Stoffregen from PJRC.

I plan to use this repository: https://github.com/brentr/ChibiOS-RT for I2C slave (found in this thread). Looks like they have both multimaster and I2C slave working for STM32. It doesn't solve anything on the lld side, but it gives me some pointers about the high level API.

As for USB... it will be more complicated. There is some USB support in the STM32 family, so I think I will start with that and see if it works out :roll:

It's a long shot, but I need both drivers for a bigger project... I will keep this thread updated when I have something.

Cheers

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

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby utzig » Fri Jan 23, 2015 11:24 am

Cool,

Btw, I'm working on an USB driver for the last few days but I have to agree that it's "complicated"!

As for I2C slave, ChibiOS doesn't support it (yet). There are people who proposed an API on the forums. Anyway should be easy to make it work not using the standard API or maybe adding i2cSlaveTransmit, i2cSlaveReceive, etc.

Cheers,
Fabio Utzig

User avatar
RedoX
Posts: 16
Joined: Tue Jan 20, 2015 4:36 pm

Re: Teensy 3.1 / MK20DX256VLH7 support

Postby RedoX » Fri Jan 23, 2015 5:48 pm

Hi,

That's really cool. If you have a Git/Svn repository with your changes for the USB driver, I would be okay to have a look and see if I can help.

For the I2C slave, I thought I would use the same structure as the Github repository I linked. It's not that difficult I agree. The only point that bugs me a little bit is that any I2C slave implementation on a Freescale MCU will have to rely on the EXT driver to detect the STOP condition.
Oh, by the way, I don't really need I2C. I just need to have multiple Teensys on a bus, with one master. I2C was an easy solution but, if you have a better idea, I would be happy to try it.

Here's a new patch to add support for the Teensy3.1 to ChibiOS. This time, there is way less duplication. The only hack that I kept is for the PLL initialization to get 72MHz for the Teensy3.1. There must be a cleaner way to implement it, but for now, it works with my Teensy3.1... Please, have a look and tell me what you think about it.

Any reason why st_lld and pal_lld are not in LLD/ ? They look the same for K20x and KL2x...
Also, I think there is a clean way to move serial_lld to LLD/ even if KL2x uses this UARTLP_Typedef for UART0 instead of the UART_Typedef. I don't have any board to test so I won't do it for now... but the KL25 Freedom is cheap so I might order one next time I need some parts. I think the spi_lld could also be moved to LLD/ since the KL2x family also has support for DMA.

Cheers
Attachments
add_tsy3_1.patch.gz
Cleaner patch for preliminary Teensy3.1 support
(36.15 KiB) Downloaded 457 times


Return to “Kinetis Support”

Who is online

Users browsing this forum: No registered users and 11 guests