This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
In “Optimizing the Kinetis gcc Startup” I stripped down the fat of my startup code. Now time to add some useful things. And what does a microcontroller like the KL25Z on the Freedom FRDM-KL25ZFRDM-KL25Z board have: Pins! And this means I have bits to set and read .
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
So this post is about how to use digital I/O pins on the KL25Z to either turn on or off, or to read in values. In the LED tutorial I used my LED component to control them, but of course it is possible to do that kind of thing on a lower level: BitIO_LDD.
Note: I assume that you are familiar with the basics like creating a Processor Expert project, generating code and debugging it. If not, please see one of my earlier tutorials: Enlightening the Freedom Board
BitIO_LDD for an Output Pin
The BitIO_LDD is included in CodeWarrior for MCU10.3. Adding that component to the project is easy from them Components Library View:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
This adds it to my project:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
BitIO in Project
Time to configure it! I map it to the Red LED on my FRDM-KL25Z board which is connected to pin PTB18. For this I use the ‘Expert’ mode, give it a name (“RED”), assign it to PTB18, name the (optional) signal name and set it to output. I enable ‘Auto Initialization’ so the Processor Expert startup code initializes the pin for me:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Time to try it out! For this I add two lines of code to the main() routine:
01 int main(void)
02 /*lint -restore Enable MISRA rule (6.3) checking. */
03 {
04 /* Write your local variable definition here */
05
06 /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
07 PE_low_level_init();
08 /*** End of Processor Expert internal initialization. ***/
09
10 RED_SetVal(NULL);
11 RED_ClrVal(NULL);
12
13 /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
14 /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
15 #ifdef PEX_RTOS_START
16 PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
17 #endif
18 /*** End of RTOS startup code. ***/
19 /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
20 for(;;){}
21 /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
22 } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
Wondering about the NULL parameter? The logical device drivers (LDD) components in Processor Expert need this. I will explore things how this can be optimized later on as the overhead caused by this is really not good even for such a powerful ARM Cortex-M0+ microcontroller.
Time to create code (selecting the ProcessorExpert.pe file and context menu t ‘Generate Processor Expert Code’, build it and download it. The code should turn on the RED led, and then turn it off. Simple as that .
The same way as of above, I add a BitIO_LDD for Green (pin PTB19) and Blue (pin PTD1):
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
BitIO_LDD for an Input Pin
Now I want to use a pin as input. As the FRDM-KL25Z has no dedicated button, I’m going to use the Reset button (SW1) instead which is connected to PTA20:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Again I add a BitIO_LDD to my project and configure it:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Now I have a little problem, as PTA20 is configured as reset pin for the CPU. So I need to tell the CPU component that I’m using the reset pin for my purpose. For this I select the Cpu in the Components view and use the Inspector menu:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Here it tells me again that I’m ‘double booking’ that pin:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
What I need to do is to disable it:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
But wait: there is one more setting:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Normally would need to configure an input pin with an internal pull-up resistor or similar. But SW1 already has a 10k Ohm pull-up resistor R4 attached:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Time to test it in my code.
I change the above code in main()
now to turn on the LED if I press the button, plus to prove that pressing the reset button does *not* cause a reset, I turn on the green LED for a second (using the Wait component):
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
And my code looks like this:
01 GREEN_ClrVal(NULL); /* turn on green LED */
02 WAIT1_Waitms(1000);
03 GREEN_SetVal(NULL); /* turn off green LED */
04 for(;;) {
05 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */
06 RED_ClrVal(NULL); /* LED cathode is connected to microcontroller pin: low level turns it on */
07 BLUE_SetVal(NULL); /* turn off blue led */
08 } else {
09 RED_SetVal(NULL); /* turn off red led */
10 BLUE_ClrVal(NULL); /* turn on blue led */
11 }
12 }
While reset/SW1 is not pressed, the blue LED is on:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Pressing the reset/SW1 button turns the red LED on:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Hardware Settings
I know now how to configure pins for input or output, and how to set or read them. But there is more you typically can configure. Things like pull-up or pull-down resistors, slew rates or driving strength. All these settings are missing in the above components. But they are available in the non-LDD (Logical Device Driver) land .
For other Processor Expert supported (non-LDD) cores (like the S08), such hardware settings can be configured in the BitIO component.
So it looks like these important settings are missing here. I was lucky that my reset pin in the example above had a pull-up resistor on the board. But I know that the hardware supports an internal pull-up (or pull-down) resistor. So it should be possible to establish a ‘resistor-less’ switch between GND and pin 14 on the J2 header on the FRDM-KL25Z board:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
D15 is connected to the PTE1 pin on the KL25Z. And I want it as an input with an internal pull-up. So how to configure it? The trick is to use Init_GPIO with pin sharing.
Pin with Pin Sharing
First, I add a normal BitIO_LDD for my new switch SW2, and configure it for PTE1:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
I’m no going to configure the hardware (internal pull-up) in an Init_GPIO.
Init_GPIO
Init_GPIO is a component which only performs initialization, nothing else. So I add it to my project:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Next, I configure it to use PTE1 with enabled pull-up resistor:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
But: it reports an error as PTE1 is already used by my BitIO_LDD component. How to solve this? The solution is to configure the pin for ‘pin sharing’: this means that several Processor Expert components can share that pin. To enable pin sharing I select the context menu on that pin:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Now notice the subtle color change of that icon which can easily be missed:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
But: it means that it is enabled for sharing, and the error from above is fixed .
Time to generate Processor Expert code and to update my source:
01 GREEN_ClrVal(NULL); /* turn on green LED */
02 WAIT1_Waitms(1000);
03 GREEN_SetVal(NULL); /* turn off green LED */
04 for(;;) {
05 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */
06 RED_ClrVal(NULL); /* LED cathode is connected to microcontroller pin: low level turns it on */
07 } else {
08 RED_SetVal(NULL); /* turn off red led */
09 }
10 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */
11 BLUE_SetVal(NULL); /* turn off blue led */
12 } else {
13 BLUE_ClrVal(NULL); /* turn on blue led */
14 }
15 }
Time again to try it out on the board: it will turn on the green LED for a second and then turn it off. Then the LED will be off. If I press SW1, the led will be red (as above).
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
If make put PTE1 to ground with a wire (emulating a switch), the LED will turn blue:
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>
Summary
Doing Bit I/O really is easy with the Eclipse based CodeWarrior for MCU10.3. The project and source files created and discussed is available here.
PS: As noticed earlier: The LDD approach comes with some avoidable overhead. I preparing a follow-up article on this how to get things optimized, so it fits my needs and expectations. Stay tuned …