Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu: RIOT port for Freescale Kinetis CPUs #2188

Closed
33 of 45 tasks
jnohlgard opened this issue Dec 12, 2014 · 86 comments
Closed
33 of 45 tasks

cpu: RIOT port for Freescale Kinetis CPUs #2188

jnohlgard opened this issue Dec 12, 2014 · 86 comments
Assignees
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT

Comments

@jnohlgard
Copy link
Member

jnohlgard commented Dec 12, 2014

Tracker to coordinate porting efforts for Freescale Kinetis CPUs #2265.

It was discussed on the riot-devel mailing list that it would be useful to have a tracking issue for coordinating efforts on Kinetis platforms.

Pull requests related to Kinetis platforms:

Porting progress:

@jnohlgard jnohlgard self-assigned this Dec 12, 2014
@jnohlgard jnohlgard added State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT Platform: ARM Platform: This PR/issue effects ARM-based platforms labels Dec 12, 2014
@haukepetersen
Copy link
Contributor

nice, I can enable the c++ support once you initial PRs are merged.

@jfischer-no
Copy link
Contributor

Startup code depends on MCU (resp. SiP).

@jnohlgard
Copy link
Member Author

Maybe not the most urgent question, but still:

Should the cpu directory for the Kinetis CPUs be named mkw2x, mk60 etc. or kw2x, k60 etc.?

The marking on the package begins with an M, but the CPU series according to Freescale's product catalog is Kinetis Kxx.

Update: M is actually the Qualification status according to the data sheet: P for prerelease, M for general market

@jfischer-no
Copy link
Contributor

@gebart
Our current status (unfortunately, something went in the background):

  • I've looked at your hwtimer implementation, but I do not quite understand it. I have completed our hwtimer implementation. It can actually be realized only with lptmr, the PIT will not run in low power modes. As your implementation, it simulates a 32-bit timer. You can set one of four different clock sources. We have tested it with RTC clock (32768Hz) and LPO clock (1kHz). It works quite well, except that vtimer is not working properly after some time. I suspect the vtimer has a bug that shows up when HWTIMER_SPEED is less 1MHz.
  • We have already moved all peripheral drivers to kinetis_common. RIOT port for the MKW22D512 SiP and Phytec PBA-D-01 PhyWave Evaluations-Board #2059

We have completed the porting on kw01z128, but not tested everything. Both ports (kw0x and kw2x) use kinetis_common and it seems to work with some minor difficulties. First cognitions of it are:

  • Startup code and linker script depends on MCU
  • Interrupt tables are different
  • WDT is implemented in different kind
  • There are two RNG implementations (RNGA and RNGB)
  • There are several different implementations of UART (kw01z128 has two slightly different, UART0 and uart)

How far are you with k60? How can we merge it all? I would prefer if it were so far in mid-January, the end of February is the Embedded World ...

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot
I only recently had any time to spend on this. I have updated my branch to make use of your kinetis_common peripherals, I discarded most of my older work. I also started using your hwtimer implementation, which seem to do mostly the same thing as mine was intended to do, but it seemed more mature and easier to follow.

Regarding the RNG implementations, we are in the same situation, the MK60DZ10 uses one implementation, the MK60D10 (no Z) uses the other implementation. There are other differences as well. In our Contiki port we decided to introduce a #define constant for the board serial number, which can then be used to pick between the two different versions. There are quite a lot of differences between the two in clocking set up and power control (LPM).

I can make an attempt to fix up my K60 port later during this week and rebase it on your tree to have a combined tree.

I also have some code in the Contiki port for computing SPI timing parameters and UART timing parameters automatically based on core clock which makes the configuration more robust and also more flexible. The API of the SPI and UART drivers in our Contiki port is designed for ease of use, you only supply the desired baud rate and timings and the driver computes the optimal settings. This makes it easier to work with dynamic core clocking (use a higher speed when doing some computations, then scale down)

The Contiki stuff is BSD licensed, but I am the author and Eistec is the copyright owner and will re-release whatever we decide to reuse in RIOT under LGPL to integrate with the rest of the tree.

I do not think it is necessary to unify the initial startup and interrupt vector code for the Kinetis ports at this point in time, it is better to do a refactoring of this later in my opinion. The interrupt handler names can be unified in order to make it easier to move between the platforms.

Once the core functionality is there and the SPI driver is running on the K60 I want to focus on getting the AT86RF212 on my board working, hopefully it will "just work", but I know that @thomaseichinger is doing some major refactoring of the rf23x driver.

@thomaseichinger
Copy link
Member

@gebart Looking at the registers and operating modes, the current at86rf231 should work out of the box with your radio. Because I'm curious, do you have an at86rf212 or at86rf212b.

@jfischer-no
Copy link
Contributor

@gebart I want to change the todo list. I would remove:

  • CMSIS PAL headers
  • linker scripts
  • Startup code

Is it ok for you?

@jnohlgard
Copy link
Member Author

@thomaseichinger I have the AT86RF212B

@jfischer-phytec-iot CMSIS PAL headers are done, I guess. The MKW22D5.h header looks like a mashup between the older non-CMSIS headers from Freescale and the newer CMSIS-compatible ones. Using CMSIS headers makes many things alot easier when writing code, and the code will be more readable, than with the Freescale non-CMSIS headers.

Startup code is working fine for MK60DxxxxVLL10, have not yet ported to MK60DxxxxZVLL10.

Linker scripts seem OK, though only works with BFD linker, broken with gold (required for LTO)

@jfischer-no
Copy link
Contributor

@gebart Since kinetis_common shold house only the peripheral driver (for the beginning), I thought, we can remove these sub-items completely from the todo list.
MKW22D5.h is the newest CMSIS compliant header file from Freescale.

@jfischer-no
Copy link
Contributor

@gebart There is a own PR #2265 for kinetis_common now.

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot I think I found out why you had to specify everything related to RNGA manually, the header MKW22D5.h is misnamed, it is actually the MK22xxxx (no W), which means that it is a completely different set of modules. I have opened a PR against your WIP branch for the kw2x, but still missing is some system functions found in system_MKW22D5.{c,h}

@jfischer-no
Copy link
Contributor

@gebart The name of the header is correct, the SiP MKW22D512 for example, consists of Cortex M4 and the Modem. The headers are from our FAE and already have the appropriate license.

system_MKW22D5.{c,h}: We do not want to use it, the code is horrible, I'm on the driver for MCG, I will publish it on Monday.

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot I understand what you mean with the code being horrible in system_xxxxx.c. I actually adapted the functions in the file to my own use in the K60 port to fit the use case. I adapted the SystemCoreClockUpdate function to work with my CPUs and use the global variables SystemXxxClock (Sys, Bus, Core, Flash) to keep track of the current clock frequency, instead of a compiler macro (F_BUS etc.). This means that the module clock passed to e.g. uart_init is correct for the current running clock configuration as long as SystemCoreClockUpdate was called after setting the clocks.

@felinira
Copy link

Regarding 'Watchdog disable during boot': Is RIOT supposed to work with watchdog? I just disable it right now but it could be desirable for some applications to switch it back on after booting. Would it be a good idea to add watchdog APIs to kinetis_common (or even to riot itself) that only have to be implemented or would that be overkill right now?

@OlegHahm
Copy link
Member

@haukepetersen, didn't you have an opinion concerninc watchdogs in RIOT?

RIOT now (scnr) we don't make use of a watchdog for any architecture afaik.

@jfischer-no
Copy link
Contributor

The KL (Cortex-M0) devices have a different WDT than K Devices (Cortex-M4). I think for now we have enough to do. I would like to see kinetis_common mainline until the end of January.

@haukepetersen
Copy link
Contributor

@OlegHahm Yes, I have some concerns, tickless scheduling and watchdogs are not best friends, as watchdogs need to be refreshed periodically... But if RIOT is used in a non-low-energy context (or if there are periodic threads running), a watchdog could well be used. But these are just not our default IoT-scenarios - so I did not want to spend time on it. I wouldn't object a watchdog driver API in RIOT - all I would object to is to enable watchdogs per default.

@jfischer-no
Copy link
Contributor

@gebart @Farthen I finisched my initial support for MCG.
https://github.com/jfischer-phytec-iot/RIOT/commit/2e4a450f338e05bb2ac6353ee10c522d36a7c86c

I have tested all MCG modes (FEI, FEE, PBE, PEE ...) with crystal or external clock on two MCUs (Cortex-M0 and Cortex-M4). Please look for examples in mcg.h

@felinira
Copy link

Looks great, will test the FEI soon. I currently can't test the external clock as my module does not have any installed right now and I would need to start soldering… I may be able to do this in a few weeks or so.

Just one question: My board's 'original' (open source) firmware enables the DMX32 bit on FEI even though it is not recommended. It works great for me as the internal oscillator is actually somewhat calibrated. I don't really know if this should be an option as it could potentially damage hardware but at least people using my board have probably already been exposed to this risk? I'm not really sure if the disclaimer in the reference manual about enabling DMX32 is a real concern or just a safety measurement from Freescale to not be sued.

For the clock mode switching: You could maybe replace the while loops with loops that automatically end after some time and just fail or panic for easier debugging if some hardware configuration is wrong. Right now they would be infinite loops which may be suboptimal. But that is optional I guess. Maybe only enable when DEVELHELP is defined or sth.

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot I will test the MCG later.

I will focus on implementing lpm_arch.c next (using LLWU).

@jnohlgard
Copy link
Member Author

@Farthen I wish to use the DMX32 bit for FEE mode as well. I have only an RTC 32kHz crystal to use on most of my boards and I wish to be able to reach 96 MHz core clock.

@jfischer-no
Copy link
Contributor

@gebart great, that I wanted to make next, MCG may need to be extended with respect to LPM. How can I help you?

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot You can comment on my idea for the lpm design:

My general idea is to always strive to reach LLS mode whenever the CPU is idle. I will implement inhibitors for each power mode which will be used to signal when a peripheral driver needs to keep the CPU at a higher power mode than LLS. The inhibitors will be simple atomic counters (counting semaphores) which will be checked before entering a power save mode. We have a similar implementation in our Contiki port, which I am the author of and I have permission to use so I might copy bits from there.

I don't think we will need to map all of the power modes in RIOT to separate modes but rather always try to go as low as the currently active drivers allow. The LPTMR0 is fully functional even in LLS, so hwtimer should always be able to wake the CPU.

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot Did you test the i2c driver on your device?

I need to interface with some i2c devices but I have not had time to read up on the Kinetis I2C module yet, so I was hoping it will "just work".

@jnohlgard
Copy link
Member Author

Regarding syscalls I think this may even be moved to cortex-mX_common

@jfischer-no
Copy link
Contributor

@gebart

It would be very convenient if we moved/merged our common parts of the ldscript, rename and refactor my K60-base.ld and adapt the kw2x ldscript to reduce it to only defining the physical memory regions and include kinetis-base.ld for the rest.

Yes, it even fits everything so far.

git mv ...

cpu/k60/ldscripts/data.ld -> cpu/kinetis_common/ldscripts/data.ld
cpu/k60/ldscripts/debugging-sections.ld -> cpu/kinetis_common/ldscripts/debugging-sections.ld
cpu/k60/ldscripts/flash-security.ld -> cpu/kinetis_common/ldscripts/flash-security.ld
cpu/k60/ldscripts/stack.ld -> cpu/kinetis_common/ldscripts/stack.ld
cpu/k60/ldscripts/text.ld -> cpu/kinetis_common/ldscripts/text.ld
cpu/k60/ldscripts/vector-table.ld -> cpu/kinetis_common/ldscripts/vector-table.ld

..add kinetis-base_m4.ld

I am not familiar enough with the Kinetis M0 variants to know if it is adaptable for the kw0x as well.

I think that almost everything can be reused.

Can you make a PR? Do you have time for it?

@jnohlgard
Copy link
Member Author

Actually, only the kinetis-base.ld is used, I used the other split ldscripts before but it turned out that it was not really helping and it was just hard to get an overview in the text editor so I copied them back into one big section file (K60-base.ld)

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot I can make a PR for getting the ldscripts into kinetis_common, can you update your cpu ports to use the scripts and I'll update the K60 port?

@jfischer-no
Copy link
Contributor

@gebart

I can make a PR for getting the ldscripts into kinetis_common, can you update your cpu ports to use the scripts and I'll update the K60 port?

Yes.

@jfischer-no
Copy link
Contributor

@gebart I have looked at your syscalls.c. Not so bad, but depends on devio. Would it be easier to move it with devio to kinetis_common for now?

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot do you have the desire/time to work on migrating the devio and syscalls part from k60+kw2x to kinetis_common?
It would be beneficial in the long run to share as much as possible between the Kinetis CPUs.

@jfischer-no
Copy link
Contributor

@gebart Yes, I think this is the better solution, even if it takes more time now. Later, it will make the review for K60 and KW2X easier.

@jnohlgard
Copy link
Member Author

We should add default handlers for the standard exceptions (hard fault, usage fault, memmanage etc.) to kinetis_common, similar to what is done in #2402 for the nRF cpu

@jfischer-no
Copy link
Contributor

We should add default handlers for the standard exceptions (hard fault, usage fault, memmanage etc.)

WIP -> 81cb7fa

This consumes approximately +400 bytes flash. crash.c could also be slimmer and the index is not necessary, because error is uniquely identified by the string. It makes more sense to print some ARM registers instead of index.

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot Regarding registers in hardfault handler, see https://github.com/gebart/RIOT/blob/2d1d87b98aee1008057dbecc83f51f72216db4c8/cpu/k60/fault-handlers.c

You'll also need an ASM trampoline something like the following (declared with __attribute__((naked))):

https://github.com/gebart/RIOT/blob/2d1d87b98aee1008057dbecc83f51f72216db4c8/cpu/k60/interrupt-vector-k60.c#L310

@jfischer-no
Copy link
Contributor

@jfischer-phytec-iot Regarding registers in hardfault handler, see https://github.com/gebart/RIOT/blob/2d1d87b98aee1008057dbecc83f51f72216db4c8/cpu/k60/fault-handlers.c

@gebart Sorry, I had only now time for it.
I was unsure if it belongs to kinetis_common. My opinion is that it is one for cortex-m4_common/crach.c. So I left it and made as in #2402. I think it's good to start with.
Please review 14faa83

@jnohlgard
Copy link
Member Author

@jfischer-phytec-iot 14faa83 looks good except for the missing prefix on the enum values. I will try it out on my WIP k60 branch.

@jfischer-no
Copy link
Contributor

🔦 I am working on RIOT port for frdm-k64f. I will open a PR after #3023 is merged.

@haukepetersen
Copy link
Contributor

nice. probably also makes sense to wait for #3100. Will try to get them both merged today.

@DipSwitch
Copy link
Member

I just bought a Teensy 3.2, the k20 CPU implementation seems abandon. And there where issues with the license. If I base it off the K60 I guess I can still make the implementation?

@felinira
Copy link

Yeah sorry, I really didn't find the time to actually do the work. As the code base has evolved since starting the whole thing it would probably be best to start over. Can't promise I will pick it up again.

You can ship the freescale k20 headers though, freescale explicitely allowed this last year sometime.

@DipSwitch
Copy link
Member

No problem! Than I have something to do in my free time ;)

@DipSwitch
Copy link
Member

Thx for the response :D (and sorry for the spam...)

@miri64 miri64 added this to the Release 2016.10 milestone Oct 17, 2016
@miri64
Copy link
Member

miri64 commented Oct 17, 2016

To my knowledge Kinetis integration is complete or am I mistaken?

@miri64 miri64 modified the milestones: Release 2017.01, Release 2016.10 Oct 31, 2016
@OlegHahm
Copy link
Member

I think so, too.

@OlegHahm
Copy link
Member

Ah, overlooked the check list.

@OlegHahm OlegHahm reopened this Jan 15, 2017
@felinira
Copy link

you can probably throw out the k20 for now, not sure if/when I would actually find the time to take care of it and nobody else seems to need it anyways.

@jfischer-no
Copy link
Contributor

I have removed k20 and kw01 from the list, I think it can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

No branches or pull requests

9 participants