FRDM:KL03Z: When to use GPIO vs PORT Commands?

cancel
Showing results for 
Search instead for 
Did you mean: 

FRDM:KL03Z: When to use GPIO vs PORT Commands?

Jump to solution
738 Views
Contributor III

Hello,

I am confused by GPIO and PORT. It sounds from reading the documentation that to set up port pins as input or output that you need to do it through GPIO, but this would mean that you can't use the ALT functionality since GPIO is a separate multiplexing function. SO...is it safe to assume that you only use GPIO when you are not working with one of the specific ALT functionality (like TPM0-CH0)? If so, then how do you set the port pins as input/output using PORT commands only?

Thanks,

Sara

Tags (3)
1 Solution
106 Views
Specialist V

Sara

This is my representation of the GPIO and PORT interaction:

pastedImage_4.png

The interesting part is that if the GPIO function is not selected (eg. the UART is connected to the pin in the PCR MUX setting) the PDIR register still reflects the pin's state, which suggests that the PDIR is always connected to the pin, so reading the state is always possible.

When the PORT doesn't select GPIO mode it is not possible to control the pins state as an output (this is obvious).

The PORT module controls also pin interrupts and DMA (not your KL03 but most other devices), which can operate irrespective of the pin's multiplex function.


Always read the GPIO instantiation description for deviations and reset defaults:

pastedImage_6.png

In case of doubt is is best to experiment. I do this with a built in memory debugger but you can do it with SWD debugger if you don't have such capabilities build into your project. Eg.

md 40049000 l 32
Memory Display
0x40049000     00000706 00000203 00000203 00000703  ................
0x40049010     00000707 00000202 00000000 00000000  ................
0x40049020     00000000 00000000 00000000 00000000  ................
0x40049030     00000103 00000005 00000001 00000001  ................
0x40049040     00000004 00000001 00000005 00000005  ................
0x40049050     00000005 00000000 00000000 00000000  ................
0x40049060     00000000 00000000 00000000 00000000  ................
0x40049070     00000000 00000000 00000000 00000000  ................
This display the PORTA register settings, where PTA5 is set as an input peripheral function with pull-down enabled.

md 400ff000 l 7
Memory Display
0x400ff000     00000000 00000000 00000000 00000000  ................
0x400ff010     0010101a 00000000 00000000  ............

This is the GPIOA register set, where the PDIR is showing that a '0' is read at PTA5

mm 40049014 l 203
Write - OK

Here PORTA_PCR5 modified to have a pull-up resistor active.

md 400ff000 l 7
Memory Display
0x400ff000     00000000 00000000 00000000 00000000  ................
0x400ff010     0010103a 00000000 00000000  ...:........

It is seen that the PDIR sees that the input is not at '1'.

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

View solution in original post

0 Kudos
3 Replies
106 Views
Contributor III

Thanks Mark! The diagram was very helpful!

Sara

0 Kudos
106 Views
Specialist V

Sara

Also take a look at the port macros in the uTasker project, which are listed here:
http://www.utasker.com/forum/index.php?topic=1875.msg6798#msg6798

They allow full control without risk of forgetting anything (they control port module clocking, PORT characteristics and GPIO configuration in a portable fashion making work with ports simpler) and remove the need to set up maybe three different modules as is required in other environments.

Regards

Mark

107 Views
Specialist V

Sara

This is my representation of the GPIO and PORT interaction:

pastedImage_4.png

The interesting part is that if the GPIO function is not selected (eg. the UART is connected to the pin in the PCR MUX setting) the PDIR register still reflects the pin's state, which suggests that the PDIR is always connected to the pin, so reading the state is always possible.

When the PORT doesn't select GPIO mode it is not possible to control the pins state as an output (this is obvious).

The PORT module controls also pin interrupts and DMA (not your KL03 but most other devices), which can operate irrespective of the pin's multiplex function.


Always read the GPIO instantiation description for deviations and reset defaults:

pastedImage_6.png

In case of doubt is is best to experiment. I do this with a built in memory debugger but you can do it with SWD debugger if you don't have such capabilities build into your project. Eg.

md 40049000 l 32
Memory Display
0x40049000     00000706 00000203 00000203 00000703  ................
0x40049010     00000707 00000202 00000000 00000000  ................
0x40049020     00000000 00000000 00000000 00000000  ................
0x40049030     00000103 00000005 00000001 00000001  ................
0x40049040     00000004 00000001 00000005 00000005  ................
0x40049050     00000005 00000000 00000000 00000000  ................
0x40049060     00000000 00000000 00000000 00000000  ................
0x40049070     00000000 00000000 00000000 00000000  ................
This display the PORTA register settings, where PTA5 is set as an input peripheral function with pull-down enabled.

md 400ff000 l 7
Memory Display
0x400ff000     00000000 00000000 00000000 00000000  ................
0x400ff010     0010101a 00000000 00000000  ............

This is the GPIOA register set, where the PDIR is showing that a '0' is read at PTA5

mm 40049014 l 203
Write - OK

Here PORTA_PCR5 modified to have a pull-up resistor active.

md 400ff000 l 7
Memory Display
0x400ff000     00000000 00000000 00000000 00000000  ................
0x400ff010     0010103a 00000000 00000000  ...:........

It is seen that the PDIR sees that the input is not at '1'.

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

View solution in original post

0 Kudos