This Project brings you a detailed steps involved in creating and demonstrating a RGB LED peripheral present on KE02 Freedom demo board (FRDM-KE02ZFRDM-KE02Z).
The Freescale KE02Z Freedom Board is a simple, yet sophisticated design featuring the Kinetis E Series microcontroller KE02Z64VQH2KE02Z64VQH2, the industry’s first 5 V microcontroller built on the ARM Cortex-M0+ core.
The Kinetis E series is the most scalable portfolio of low-power, high-robustness, mixed signal 32-bit ARM Cortex-M0+ MCUs running up to 20 MHz in the industry. It supports power supply voltage range from 2.7–5.5 V, ambient operating temperature range from –40 °C to 105 °C and includes up to 64 KB flash memory
The FRDM-KE02ZFRDM-KE02Z hardware is a Freescale Freedom Development Platform microcontroller board assembled with the following features
- Kinetis E Series KE02 family MCU in an 64 QFP package
- Onboard Serial and Debug Adapter (OpenSDA)
- I/O headers for easy access to MCU I/O pins
- Freescale inertial sensor, MMA8451QMMA8451Q
- Capacitive touch slider
- Reset pushbutton
- RGB LED
- Infrared communication
- One thermistor
- Motor control function interface for simple BLDC motor control
The block diagram of the board is as shown below:
Before we proceed with actual project creation let’s have a bit knowledge on Kinetis GPIO concept.
The most basic interface of a microcontroller is the General Purpose Input/Outputs (GPIOs). These modules allow the programmer to get input from devices such as switches, and outputting the values to display a status or result, for example, on LEDs.
Figure 1 shows the block diagram of the GPIO modules used in the Kinetis Family of microcontrollers. In general, most microcontrollers separate individual GPIO pins into groups, called ports.
In the Kinetis microcontrollers, each port has 32 pins [31:0] as shown in the next diagram. Each of these ports are connected to the microcontroller via the peripheral bridge, which is a bus that allows slower components to talk to the microcontroller on a shared bus. This design is very common and helps to save on space within the chip. Since GPIO pins are not a high speed interface, they are normally either multiplexed or connected in a bus. Later on, we will see how each of these ports is organized.
Figure 1: GPIO Port block diagram from Freescale Kinetis Microcontroller
In the Kinetis microcontrollers, the GPIO are connected into ports of 32 pins each. Each of these ports have a separate set of registers to control the pins. To control whether the GPIO pin is used as an input or an output, the user can set a bit in the Port Data Direction Register (GPIOx_PDDR) shown below in Figure 2.
Figure 2: GPIO Port Data Direction Register (GPIOx_PDDR)
If the pin is setup as an output there are four registers that can be modified to set the output value. The Port Data Output Register (GPIOx_PDOR) sets the value of a pin to logic level 0 when a 0 is written, and a logic level 1 when a 1 is written.
The Port Set Output Register (GPIOx_PSOR) allows the user to write a 1 to it in order to set the logic level at 1. The PortClear Output Register (GPIOx_PCOR) allows the user to write a 1 to it in order to clear the pin, thus setting it to logic level 0.
This may seem like overkill, to have two extra registers, when you can do the same with 1. However, it really makes it easier to always set a bit to change the value of a output pin. The Port Toggle Output Register (GPIOx_PTOR) allows the user to write a 1 to it in order to toggle the current logic state.
For example, if the current value is 1 then writing a 1 to the register will flip the output logic level to 0. Also, writing a 1 to the register when the current level is 0, flips the output logic level to 1. If the pin is setup as an input, then reading the value in the Port Data Input Register (GPIOx_PDIR) will give the current logic level on the pin. Through the use of these register, the user can read and write data onto the pin with ease.
The Kinetis microcontrollers were designed for very low power operation. One of the ways to reduce the power on a chip is to turn off the clocks to particular areas that are not in use. The default setting for the microcontroller is to have the clock disabled. In order to use a particular peripheral or resource the user needs to enable this clock to each item individually. This is a great feature, and builds upon the latest research in preventing wasted power by reducing the dynamic power required for a chip.
Figure 3: System Clock Gating Control Register 5 (SIM_SCGC5)
Functional Description:
Each GPIO pin has 6 registers that are used to modify and evaluate the state of the pin. The block diagram for the GPIO pin operations used in the Kinetis microcontrollers is shown below in Figure 4.
Figure 4: A GPIO Pin Block Diagram
The actual GPIO pin is buffered into and out of the microcontroller so that the voltage levels do not short out the delicate chips circuits. These buffers are actually tri-state buffers since they can be set to a low logic level, a high logic level, and a high impedance value. These buffers are controlled via the data direction register.
As is shown in the diagram, there is an inverter between this register and the input buffer, so in order to use the pin as an input a ‘0’ must be written to the register. This also would turn off the output buffer so that the user doesn’t accidentally drive the value on the pin and read it at the same time.
The four output registers are multiplexed to the output buffer and this is controlled by logic that enables the register that was written to last. This way the user can write to any register at any time and update the value. The input buffer connects directly to the input register that can then be read by the user to determine the logic level on the pin.
A Short note on OpenSDA:
OpenSDA is a low-cost debug/programming interface embedded in certain Freescale evaluation boards. It handles debug communications between the PC and target via USB. Evaluation boards that feature OpenSDA are great for the beginning phases of development.
OpenSDA is managed by a Kinetis K20 MCU built on the ARM Cortex-M4 core. The Kinetis K20 includes an integrated USB controller that can operate at clock rates up to 50MHz. The OpenSDA circuit includes, at a minimum, a status LED and a pushbutton. The pushbutton asserts the Reset signal to the target processor, which could be a microcontroller, digital signal controller, or a microprocessor. It can also be used to place the OpenSDA circuit into Bootloader mode. The OpenSDA circuit receives power when plugged into a USB Host.
OpenSDA software includes a flash-resident USB MSD Bootloader and a collection of OpenSDA Applications. The MSD Bootloader is consistent across all implementations of OpenSDA, but the available OpenSDA Applications may vary from one hardware system to another. The OpenSDA MSD Bootloader provides a simple interface for loading an OpenSDA Application into the OpenSDA processor’s non-volatile memory. Only one OpenSDA Application may be resident at a time, and this application will be automatically run at startup unless Bootloader mode is selected by holding down the Reset button while plugging in a USB cable.
The P&E Debug Application is an OpenSDA Application that provides debugging and a virtual serial port all in one application. It provides a run-control debug interface that controls the JTAG or SWD debug interface to the target processor. It also provides a USB communications device class (CDC) interface that bridges serial communications between the USB Host and a UART serial interface on the target processor. The P&E Debug Application is designed to debug the resident target processor in the OpenSDA system with limited support for off-board devices within the same processor family as the resident target processor.
Steps to be followed to load OpenSDA Debug file:
Before we start we need to download KE02 board quick start package from below link: ‘FRDM-KE02ZFRDM-KE02Z’ Quick Start Package containing Quick Start Guide, precompiled examples and OpenSDA Applications.
Plug in a USB cable from a USB host to the OpenSDA mini-B USB connector. The FRDM-KE02Z will be powered by this USB connection.
It comes with the mass-storage device (MSD) Flash Programmer OpenSDA Application preinstalled. It will appear as a removable storage drive with a volume label of 'FRDM-KE02Z'.
Now the board has to be in P&E Debug mode before we create a project in Processor Expert so that we can “Run” and “Debug” our example project,
P&E Debug Application
The P&E Debug Application is a run-control debug interface that controls the JTAG or SWD debug interface to the target controller/processor. It also provides a virtual serial port bridging a USB CDC device to a UART serial interface on the target device. Thus, it provides debugging and a serial port all in one application. Host drivers for this application are provided with several IDE toolchains such as CodeWarrior for Microcontrollers, IAR Embedded Workbench, and Keil MDK. Drivers are also available for download from P&E Microcomputer Systems at http://www.pemicro.com/opensda.
To do so the board should be in OpenSDA Bootloader mode so that the file “DEBUG-APP_Pemicro_v106.SDA” can be loaded.
To Enter into OpenSDA Bootloader Mode follow the below steps:
1. Unplug the USB cable if attached.
2. Press and hold the Reset button (SW1).
3. Plug in a USB cable between a USB host and the OpenSDA USB connector (labeled ―OPENSDA).
4. Release the Reset button.
A removable drive should now be visible in the host file system with a volume label of “BOOTLOADER” as shown below; you are now in OpenSDA Bootloader mode.
Now Locate the OpenSDA Applications folder in the FRDM-KE02Z Quick Start Package as shown here:
Inside the folder \FRDM-KE02Z_QSP\OpenSDA Applications you can find three files as shown below:
- CMSIS-DAP interface new ARM standard for embedded debug interface
- P&E Debug interface provides run-control debugging and compatibility with IDE tools
- Mass storage device flash programming interface (default) – no tool installation required to evaluate demo apps
Copy & paste or drag & drop the MSD Flash Programmer Application “DEBUG-APP_Pemicro_v106.SDA“to the BOOTLOADER drive.
Unplug the USB cable and plug it in again. The new OpenSDA Application (Debug Mode) should now be ready.
Now that our board is loaded in Debug Let’s start on creating a Processor expert project to demonstrate RGB LED interfaced to GPIO on KE02 board.
(Refer to my earlier blog for creating PE project:
Open Codewarrior and select the workspace as shown below:
Click on File -> New -> Bareboard Project as shown below:
Provide Project Name, here in my case i have given project name as “RGB Demo-KE02” as shown below:
Next select Kinetis-E series and KE02Z (20MHz) device MKE02Z64MKE02Z64
Select OpenSDA as connection to target board to be used for this project as shown below
Select Default Language and build tools option as shown
Next, Select ‘Processor Expert’ check box option for Rapid Application Development as shown below:
Now we can see the project wizard is gathering and creating project for given settings
After creating project we can see the above project window ready for next step as shown below:
Now, Select the component ‘BitIO’ from component library and add to project, we need three 'BitIO' for connecting three LED’s RGB.
We need three ‘BitIO’ for connecting three LED’s RGB, so add three components as shown here:
Rename the 3 components as ‘Red’, ‘Green’ and ‘Blue’ for our ease of recognize and readability convention as shown here:
Next refer to the schematic of the board as shown in order to identify the port pin connected to RGB LED (you will get this from “FRDM-KE02Z User’s Manual“)
As we can see the LED’s are having ‘common Anode’ connection which is connected to 3.3V and we need to send ‘0’ signal to port pin to glow the LED and send ‘1’ signal to port pin to switch off the LED.
Now we need to assign to the particular port pin number as shown here
LED Red is connected to PTH1
LED Green is connected to PTH2
LED Blue is connected to PTE7
Select direction as ‘output’ and initial value as ‘1’ so that the LED will be ‘switched off’ initially.
Perform the same settings to other two LED’s as shown below:
Next click on ‘Generate Processor Expert’ Code button as shown here to generate all its library contents to be used in our project
After clicking Generate code you can see progress of generating files, it can be seen even at right hand below side of CW which is running at background
After generating the code you can see ‘ProcessorExpert.c’ file is generated under ‘Source’ folder with the entire required header files linked and generated, we need to add our code in this file as shown here.
As shown here we have written piece of code to glow all the three LED sequentially one after the other by giving some delay
unsigned int i;
for(;;){
Blue_ClrVal(); // passing '0' to Blue LED port
for(i=0;i<=1000000;i++); //software delay
Blue_SetVal(); // passing '1' to Blue LED port
for(i=0;i<=1000000;i++); //software delay
Green_ClrVal(); // passing '0' to Green LED port
for(i=0;i<=1000000;i++); //software delay
Green_SetVal(); // passing '1' to Green LED port
for(i=0;i<=1000000;i++); //software delay
Red_ClrVal(); // passing '0' to Red LED port
for(i=0;i<=1000000;i++); //software delay
Red_SetVal(); // passing '1' to Red LED port
for(i=0;i<=1000000;i++); //software delay
}
Next we need to ‘Clean’ and ‘Build’ the project as shown here
You can see the build process progress, and the steps involved can be seen in ‘console’ sub-window as shown here and if any error exists it prompts here.
Now build process is completed.
It is ready for ‘debugging’ or ‘Run’ the project.
Now connect KE02 board to computer through USB cable to openSDA port as shown below below.
To Run the project right click on project folder ‘RGB Demo-KE02’ -> Run As -> and click Codewarrior option as shown here:
Click on ‘open Perspective’ then select ‘Debug’ then click on ‘ok’ button
Now click on “Run” menu then select ‘Run’
Here you can see the progress happening in the console window, after everything is finished it displays “Target has been RESET and is active”
Now the board starts working i.e you can see all the three LED blinks sequentially for some interval (software delay).
(Note: you can watch this execution in video attached)
Blue LED blinking
Green LED blinking
Red LED blinking
Here is a video output of RGB LED execution:
Now we will execute this program in “Debug” mode which is explained here:
Next proceed by clicking “Run”->”Debug”, you can see the debugging progress bar at below right side of the CW window.
Now Debug window is ready for debug process, you can see all associated resources available like Breakpoints, Memory menu, Register menu, Disassembly menu, and ProcessorExpert.c program menu for debugging. This is show below.
Now click on ‘step over’ icon as shown below, CW runs our program in single stepping mode and you can see corresponding registers value changes and while single stepping you can see output LED glowing accordingly.
Happy Glowing and Debugging RGB on KE-02 board