Kinetis UART DMA

cancel
Showing results for 
Search instead for 
Did you mean: 

Kinetis UART DMA

Jump to solution
2,092 Views
carlnormansuret
Contributor V

Hi All,

I am using a serial download tool that has no flow control and no speed control (so all those options for a solution are no good as I know this will be the first solutions offered up). I need to stream data in while writing to flash, its not very fast, but fast enough that data arrives while I am writing to flash and causes a hardware collision.

My first thought was to use the UART DMA examples supplied with the SDK (fsl_uart_dma.c) as the data could arrive while i write to flash without issue. However, the example (uart_dma_transfer.c) doesn't appear very useful as you appear to have to know the length of data you're getting? And, there appears to be no "idle timeout" signalling of any kind? And no information on how to check how much data is in a buffer? and no ring buffer?

I imagine almost all applications cannot know the length of data until it arrives, and, people will need to check if nothing has happened in some time, and, dont want to be firing interrupts off constantly as DMA UART would be completely useless.

I would have hoped/thought the general most common implementation for DMA UART is to have some way to "check" the DMA location in a ring buffer and access that data in its buffer so I can go off and do my processing on that section of the buffer while the ring keeps going around in the DMA, then, when I am done doing my processing, see where the DMA is up to, or see if nothing else has happened in some period of time.

Is what I am thinking possible? Am I not understanding the normal implementation of this type of feature? 

I am using FreeRToS, and KL27z, but I dont think that really matters... Other than running interrupts and context switching make for extended delays.

1 Solution
700 Views
mjbcswitzerland
Specialist V

Hi Carl

I just did some optimisation for the KL27 in order to ensure that all of its UARTs could be used in this mode; both LPUARTs and the UART.

I recommend that you chose an option at http://www.utasker.com/kinetis.html so that you can directly use the developer's version. If you encounter problems with licensing (I know that some companies will prefer to invest 4 weeks of overtime rather than part with a couple of hundred bucks for an immediate solution and professional support ;-) ) there is also a free commercial license option which is still supported on this forum. This means that there is no problem either way, but it will save me needing to port it to the open source version (which is not generally intended to be maintained to the same performance capability as the professional version) and may need half a day of work.
In any case, once you have access to the developer's repository you can directly use the development branch "Dev_24-04-2017" which I just checked-in to be fully sure. It allows all 3 UARTs to be operated like this at the same time (or mixed with Tx DMA or interrupt operation up to the 4 channel limit of the DMA hardware).

The following two videos go through the configuration, explain the interface routines as well as the technical details (which you are already well aware of) and verify on the FRDM-KL27Z HW, including proving continued operation of the receiver when the processor is not operating:
FRDM-KL27Z UART reception based on free-running DMA - Part 1: https://youtu.be/dNZvvouiqis
FRDM-KL27Z UART reception based on free-running DMA - part 2: https://youtu.be/GaoWE-tMRq4

From the videos you will also see that there are only really two interfaces that do the specific work:
- fnEnableRxAndDMA() to configire the DMA and UART for reception
- fnCheckFreerunningDMA_reception() for checking the state of the input buffer

These could theoretically by used in any environment if needed as long as the overall management can be performed suitably. The uTasker interfaces uses fnOpen() to manage the complete operation (UART configuration, interrupt and DMA modes as well as providing suitably aligned buffers) and fnRead() [or fnMsgs()] to check the input buffer state and read out data which encapsulate all further compications. It is then also compatible on any part with DMA (whether K or KL DMA controller types). Also the uTasker KL27 simulator emulates accurately the interrupt and DMA operation with the UART to simplify maintenance (and porting if ever required).

Regards

Mark

P.S. There is also a binary available at http://www.utasker.com/kinetis/FRDM-KL27Z.html#UART_RX_DMA which can be tested on a FRDM-KL27Z

View solution in original post

10 Replies
700 Views
mjbcswitzerland
Specialist V

Hi Carl

See chapter 10 of http://www.utasker.com/docs/uTasker/uTaskerUART.PDF which outlines various DMA reception strategies.

The free-running method is also discussed here: https://community.nxp.com/thread/450290

The complete code (for KL and K implementation) is available as open source at
Web:      https://github.com/uTasker/uTasker-Kinetis
HTTPS:    https://github.com/uTasker/uTasker-Kinetis.git
SSH:      git@github.com:uTasker/uTasker-Kinetis.git
in case you need a reference to develop from.

Specific to the KL27, and in order to avoid any interrupts, it needs its Rx circular buffer to be on a modulo boundary and have a modulo size - eg. 1024 bytes, 2048 bytes etc. (aligned to 1024 or 2048 byte boundary in RAM). This allows a zero CPU overhead, free-running operation including wrap-around in HW. A little annoying is the fact that the KL DMA can't actually be put into "forever" mode and so needs to be programmed to do the largest possible transfer (0xffff0 resp. 1048560 bytes) and the application then needs to re-write this register to its maximum value again periodically (before it expires).

Regards

Mark


Professional support for Kinetis: http://www.utasker.com/index.html
Remote desktop one-on-one coaching: http://www.utasker.com/services.html
Getting started to expert videos: https://www.youtube.com/results?search_query=utasker+shorts

700 Views
carlnormansuret
Contributor V

I tried to give it a go, but I cant get uTasker working, I downloaded a zip file from 

https://github.com/uTasker/uTasker-Kinetis

I copied over the entire folder .settings for KDS from:

H:\Buffer\uTasker-Kinetis-master\Applications\uTaskerV1.4\KinetisDesignStudio\Project_Settings\.settings

to 

H:\Buffer\uTasker-Kinetis-master\.settings

Then in my KDS try to import like I usually do:

File > import > general > import existing into work space > select root directory > browse... > H:\Buffer\uTasker-Kinetis-master

"No projects to import" is displayed. There is something wrong with me, or, the project settings for KDS that you supply. 

0 Kudos
700 Views
mjbcswitzerland
Specialist V

Carl

Make sure you use the "Development" branch and not the "master" branch because the KDS setup is not in the master (I moved to GITHUB recently and the KDS settings got lost).

The following video shows using a GIT client and importing to KDS:
https://www.youtube.com/watch?v=K8ScSgpgQ6M&feature=youtu.be

Regards

Mark

P.S. I would first open in Visual Studio since it simulates the K27 and UART operation, including interrupts and DMA so making it easier to evaluate the operation and avoiding possibly confusing HW debugging requirements.

0 Kudos
700 Views
carlnormansuret
Contributor V

Thank again, you're very helpful.

You were right, the master branch is missing the project information.

I then followed the link details exactly... twice.. and I get errors

Building file: ../Hardware/Kinetis/kinetis.c
Invoking: Cross ARM C Compiler
arm-none-eabi-gcc -mcpu=cortex-m0plus -mthumb -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -g -D_KINETIS -D_KDS -D_GNU -I"C:/Users/carln/Downloads/uTasker-Kinetis-Development/Applications/uTaskerSerialBoot" -std=c99 -MMD -MP -MF"Hardware/Kinetis/kinetis.d" -MT"Hardware/Kinetis/kinetis.o" -c -o "Hardware/Kinetis/kinetis.o" "../Hardware/Kinetis/kinetis.c"
In file included from C:/Users/carln/Downloads/uTasker-Kinetis-Development/Applications/uTaskerSerialBoot/types.h:141:0,
from C:/Users/carln/Downloads/uTasker-Kinetis-Development/Applications/uTaskerSerialBoot/config.h:1019,
from ../Hardware/Kinetis/kinetis.c:157:
C:/Users/carln/Downloads/uTasker-Kinetis-Development/Applications/uTaskerSerialBoot/app_hw_kinetis.h:921:158: error: 'FTFL_FOPT_BOOTPIN_OPT' undeclared here (not in a function)
#define KINETIS_FLASH_CONFIGURATION_NONVOL_OPTION (FTFL_FOPT_LPBOOT_CLK_DIV_0 | FTFL_FOPT_RESET_PIN_ENABLED | FTFL_FOPT_BOOTSRC_SEL_FLASH | FTFL_FOPT_BOOTPIN_OPT | FTFL_FOPT_NMI_DISABLED)
^
../Hardware/Kinetis/kinetis.c:2368:5: note: in expansion of macro 'KINETIS_FLASH_CONFIGURATION_NONVOL_OPTION'
KINETIS_FLASH_CONFIGURATION_NONVOL_OPTION,
^
Hardware/Kinetis/subdir.mk:18: recipe for target 'Hardware/Kinetis/kinetis.o' failed
make: *** [Hardware/Kinetis/kinetis.o] Error 1

13:23:01 Build Finished (took 3s.720ms)

And if i try to build the 

uTasker1.4 application I get (I am using KL27Z with the linker for KL_256_32.ld as this is the processor I am actually using.

make --no-print-directory post-build
"C:/Users/carln/Downloads/uTasker-Kinetis-Development/Applications/uTaskerV1.4/KinetisDesignStudio/uTaskerV1.4_FLASH/generate.bat
c:/freescale/kds_v3/eclipse/../bin/sh: syntax error: unterminated quoted string
makefile:97: recipe for target 'post-build' failed
make[1]: [post-build] Error 2 (ignored)

So far my introduction to uTasker is not going so well.

0 Kudos
700 Views
mjbcswitzerland
Specialist V

Carl

Sorry, the open source version was recently moved and KDS lost some things. Also it is a year behind the developer's version so may miss some KDS workarounds required for the latest version.


I just checked:
- the error with "c:/freescale/kds_v3/eclipse/../bin/sh: syntax error: unterminated quoted string" is not a big problem. It  is not the project that doesn't build but a post-build step that is used to generate multiple output sources. The project can still be loaded and debugged etc.
However to solve it you can open the C/C++ Build property settings - select the Build Steps tab and add a termination to the bat file.
"${ProjDirPath}/Applications/uTaskerSerialBoot/KinetisDesignStudio/uTaskerSerialLoader_FLASH/generate.bat
becomes
"${ProjDirPath}/Applications/uTaskerSerialBoot/KinetisDesignStudio/uTaskerSerialLoader_FLASH/generate.bat"

This was previously not an issue but from KDS 3.2.0 it started failing without the termination character at the end of the line.

To solve the flash parameter issue, change FTFL_FOPT_BOOTPIN_OPT to either FTFL_FOPT_BOOTPIN_OPT_ENABLE or FTFL_FOPT_BOOTPIN_OPT_DISABLE.
I think I updated some headers to allow some people to use some newer parts (KL82 for example) in the open source version and this definition had changed.

I just checked in the two mods if you prefer to pull them from the repository.

Regards

Mark

0 Kudos
700 Views
mjbcswitzerland
Specialist V

Carl

While checking I took a look at the Rx DMA part and have seen that the open source version has full support only for K parts (with the more powerful DMA controller). I can try to migrate KL DMA parts from the developer's project but it is a bit fiddly.

Which UART or LPUART are you in fact in need of? The KL27 has 2 x LPUARTs and 1 x UART. These are not compatible and so slightly different methods are required.

Regards

Mark

0 Kudos
700 Views
carlnormansuret
Contributor V

Wow, you really are helpful! 

We are using LPUART1 in this case connected to a HE910 Telit cellular modem. 

I would have it configured in a circular buffer 1024 aligned and no interrupts running so I can write to flash while the file stream in the serial port. I would then just read the buffer when flash write is done and see where its up to. I will be interested to see how this goes, if it works, I will get the boss to purchase the full uTasker and port away from FreeRToS.

0 Kudos
701 Views
mjbcswitzerland
Specialist V

Hi Carl

I just did some optimisation for the KL27 in order to ensure that all of its UARTs could be used in this mode; both LPUARTs and the UART.

I recommend that you chose an option at http://www.utasker.com/kinetis.html so that you can directly use the developer's version. If you encounter problems with licensing (I know that some companies will prefer to invest 4 weeks of overtime rather than part with a couple of hundred bucks for an immediate solution and professional support ;-) ) there is also a free commercial license option which is still supported on this forum. This means that there is no problem either way, but it will save me needing to port it to the open source version (which is not generally intended to be maintained to the same performance capability as the professional version) and may need half a day of work.
In any case, once you have access to the developer's repository you can directly use the development branch "Dev_24-04-2017" which I just checked-in to be fully sure. It allows all 3 UARTs to be operated like this at the same time (or mixed with Tx DMA or interrupt operation up to the 4 channel limit of the DMA hardware).

The following two videos go through the configuration, explain the interface routines as well as the technical details (which you are already well aware of) and verify on the FRDM-KL27Z HW, including proving continued operation of the receiver when the processor is not operating:
FRDM-KL27Z UART reception based on free-running DMA - Part 1: https://youtu.be/dNZvvouiqis
FRDM-KL27Z UART reception based on free-running DMA - part 2: https://youtu.be/GaoWE-tMRq4

From the videos you will also see that there are only really two interfaces that do the specific work:
- fnEnableRxAndDMA() to configire the DMA and UART for reception
- fnCheckFreerunningDMA_reception() for checking the state of the input buffer

These could theoretically by used in any environment if needed as long as the overall management can be performed suitably. The uTasker interfaces uses fnOpen() to manage the complete operation (UART configuration, interrupt and DMA modes as well as providing suitably aligned buffers) and fnRead() [or fnMsgs()] to check the input buffer state and read out data which encapsulate all further compications. It is then also compatible on any part with DMA (whether K or KL DMA controller types). Also the uTasker KL27 simulator emulates accurately the interrupt and DMA operation with the UART to simplify maintenance (and porting if ever required).

Regards

Mark

P.S. There is also a binary available at http://www.utasker.com/kinetis/FRDM-KL27Z.html#UART_RX_DMA which can be tested on a FRDM-KL27Z

700 Views
carlnormansuret
Contributor V

Mark, the level of support you've shown already is amazing. I will be arranging uTasker as the platform for all these projects over the next week!

0 Kudos
700 Views
carlnormansuret
Contributor V

You my friend are possibly the most helpful person on these forums. I should probably look into using uTasker instead of FreeRToS.

0 Kudos