VietNam National University
University of Engineering and Technology
PROGRAMMING
EMBEDDED AND REAL-TIME SYSTEMS
(INT3108, LẬP TRÌNH NHÚNG VÀ THỜI GIAN THỰC)
Dr. Nguyễn Kiêm Hùng
Email: kiemhung@[Link]
Laboratory for Smart Integrated Systems
Objectives
In this lecture you will be introduced to:
– Developing embedded applications with FRDM-KL46Z
Platform
– Master Keil MDK-ARM IDE tool
2
Lab 4: Blinking LEDs
3
Exercise 1: Blinking green LED
• Create a application with the infinite loop design. The
project contains the user code files:
– main.c: This file contains the main() function
– LED.c: The file contains functions to initialize the GPIO port pin
and to set the port pin on or off. The Init LED() function
initializes the GPIO port pin. The functions BlinkLEDx() blink
green LED on the port pin.
– LED.h: The header file contains the function prototypes
created in LED.c and must be included into the file main.c.
• Reading Material
– Chapter 11 & 12 of [Link]
4
Exercise 1: Blinking green LED
• Red LED and Green LED are connected as shown
in the below Table. (See [1])
Red LED
Green LED
LED signal Connections
LED KL46Z Pin
Red PTE29
Green PTD5
[FRDM-KL46Z User’s Manual]
5
Review: Programming I/O
• Initialization process:
– Programming the clock control circuitry
– configuring the operation mode of the I/O pins
– Peripheral configuration
– Interrupt configuration
• I/O Control:
• User code for controlling I/O
6
Exercise 1: Input Output Basic
• Signal multiplexing integration: ([3], chapter 10 &11)
Pin Control Register n (PORTx_PCRn)
MUX bits
PORTx_PCRn D10 D9 D8
Analog Circuit I 0 S2
GPIO Pn.x I1 S1
S0
Alternative Func2 I2
Alternative Func3 I3
Alternative Func4 Pin of Chip
I4
Alternative Func5 I5
Alternative Func6 I6
Alternative Func7 I8
7
Exercise 1: GPIO Port Registers
• Each GPIO port has ([3], chapter 42)
• Port Data Output Register (GPIOx_PDOR)
• Port Set Output Register (GPIOx_PSOR)
• Port Clear Output Register (GPIOx_PCOR)
• Port Toggle Output Register (GPIOx_PTOR)
• Port Data Input Register (GPIOx_PDIR)
• Port Data Direction Register (GPIOx_PDDR)
8
Exercise 1: Blinking green LED
• Initialization process:
– Programming the clock control circuitry
– System Integration Module (SIM) - This gives control of which peripherals are
clocked. (see [3] chapter 12)
– System Clock Gating Control Register 5 (SIM_SCGC5) - This register holds the
clock enable for the various ports on the MCU. IN ORDER TO USE ANY PINS
ON A PORT THIS MUST BE ENABLED FOR THAT PORT. For example, to use the
red LED on pin PTD5 we must first enable the clock to Port D in SCGC5.
9
Exercise 1: Blinking green LED
• Initialization process:
– Programming the clock control circuitry
– System Integration Module (SIM) - This gives control of which peripherals are
clocked. (see [3] chapter 12)
– System Clock Gating Control Register 5 (SIM_SCGC5) - This register holds the
clock enable for the various ports on the MCU. IN ORDER TO USE ANY PINS
ON A PORT THIS MUST BE ENABLED FOR THAT PORT. For example, to use the
red LED on pin PTD5 we must first enable the clock to Port D in SCGC5.
SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK; //0x1000 = 1<<12
//This enables the clock to PORTD
typedef struct {
__IO uint32_t SCGC4; /**< System Clock Gating Control Register 4, offset: 0x1034 */
__IO uint32_t SCGC5; /**< System Clock Gating Control Register 5, offset: 0x1038 */
__IO uint32_t SCGC6; /**< System Clock Gating Control Register 6, offset: 0x103C */
__IO uint32_t SCGC7; /**< System Clock Gating Control Register 7, offset: 0x1040 */
__IO uint32_t CLKDIV1; /**< System Clock Divider Register 1, offset: 0x1044 */
(...)
} SIM_Type;
10
Exercise 1: Blinking green LED
• Initialization process:
– Configuring the operation mode of the I/O pins
– Port Control and Interrupts (PORTx->PCR[n]) - This register
determines which pins are used for which peripheral (see [3] chapter
11).
– Pin Control Register (PORTD_PCR5) - We will be using the green
LED on the FRDM-KL46, so we will need PORTD (the port we're
on), PCR5 (the pin we're on) and configure it to GPIO.
PORTD->PCR[5] = ???
//This sets the Mux control of PTD5 to 001, or GPIO
11
Exercise 1: Blinking green LED
• Initialization process:
– Configuring the operation mode of the I/O pins
– Once a pin is configured as GPIO this register determines whether it
is an input or an output. (see [3], 42)
– Port Data Direction Register (GPIO_PDDR) - This register
configures GPIO as inputs or outputs. Pin number is the same
as bit number, 0 is defined as input, 1 as output. So to define
PTD5 as an output we would set bit 5 of PDDR to 1.
PTD->PDDR =???
//This sets PTD5 as an output. There are no masks defined for each
pin so we have to do it by hand
12
Exercise 1: Blinking green LED
• Blinking LED:
– The following register is used to write to GPIO pin ([3], 42).
– Port Data Output Register (GPIO_PDOR) - This register will change the
output of a pin to the value given. So an input of 0 will change the bit to
a 0, an input of 1 will change the bit to a 1.
– Port Data Set Register (GPIO_PSOR) - This register will set the output
of a pin. An input of 0 will not change the current value of the pin, while
a 1 will set the bit to a 1.
– Port Data Clear Register (GPIO_PCOR) - This register will clear the
output of a pin. An input of 0 will not change the current value of the pin,
while a 1 will clear the bit to a 0.
– Port Data Toggle Register (GPIO_PTOR) - This register will toggle the
output of a pin. An input of 0 will not change the current value of the pin,
while a 1 will change the bit to the inverse of its current state.
13
Exercise 1: Blinking green LED
• Blinking LED:
– Example:
PTD->PDOR |= (1u<<5); //Change PTD5 to 1, turns LED OFF
for (i; i < 3000000; i++){}; //Burn some time
PTD->PDOR &= ~((uint32_t)(1u<<5)); //Change PTD5 to 0, turns LED ON. .
for (i; i < 3000000; i++){}; //Burn some time
14
Exercise 1: Blinking green LED
• Create new project on Keil
– Add CORE & Startup from CMSIS as the following figure.
15
Exercise 1: Blinking green LED
• Adding to the project the user code files as follows:
– main.c: This file contains the main() function
– LED.c: The file contains functions to initialize the GPIO port pin
and to set the port pin on or off. The Init LED() function
initializes the GPIO port pin. The functions BlinkLEDx() blink
green LED on the port pin.
– LED.h: The header file contains the function prototypes
created in LED.c and must be included into the file main.c.
16
Exercise 1: Blinking green LED
• Write code for main.c
#include "LED.h" //
int main(void)
{
/* Configure RED LED pin as GPIO output*/
while (1)
{
/* Delay */
/* Toggle LED */
}
}
17
Exercise 1: Blinking green LED
• Write code for LED.c
#include "MKL46Z4.h" // Device header
void InitLED(void)
{ //This function enables the red LED on the FRDM-KL46Z development board
; //This enables the clock to PORTD
; //This sets the Mux control of PTD5 to 001, or GPIO
; //This sets PTD5 as an output. There are no masks defined for each pin so we have to do
it by hand.
}
void BlinkLED1(void)
{ //This method turns the LED off, then back on using two separate commands.
uint32_t i = 0; //Create a loop variable
; //Set PTD5 = 1, turns LED OFF (Cathode connected to PTD5)
for(i=0; i < 5000000; i++){}; //Burn some time
; //Clear PTD5 = 0, turns LED ON
for(i=0; i < 5000000; i++){}; //Burn some time
} 18
Exercise 1: Blinking green LED
• Write code for LED.h
void InitLED(void); // Initialize GPIO
void BlinkLED1(void); // Use GPIO_PDOR
void BlinkLED2(void); // Use GPIO_PSOR and GPIO_PCOR
void BlinkLED3(void); // Use GPIO_PTOR
19
Exercise 1: Blinking green LED
• Debug the Blinky application
– From the toolbar, choose Project -> Options for Target …, click the Debug
tab, enable Use, and select the applicable debug driver.
– Select <Setting> to configure the debugger
20
Exercise 1: Blinking green LED
• Debug the Blinky application
– setting the debug connection
21
Exercise 1: Blinking green LED
• Debug the Blinky application
– Switch to the dialog Utilities and enable Use Debug Driver
– The device selection already configures the Flash programming algorithm
for on-chip memory. Verify the configuration using the Settings button
22
Exercise 1: Blinking green LED
• Debug the Blinky application
– To compile our program click on Rebuild in the tool bar.
• The Build Output window shows messages about the compiling
progress.
11
23
Exercise 1: Blinking green LED
• Debug the Blinky application
– Program the application into Flash memory.
• From the toolbar, choose Download. The Build Output window
shows messages about the download progress.
• The green status light on the FRDM-KL46Z should light up, then go
dark, and your LED should be blinking if every is OK.
1 2
24
Exercise 2: Blinking LEDs
• Create an application to blink RED and Green LEDs
oppositely:
– If the Red LED is on, then green LED is off.
– If the Red LED is off, then green LED is on.
• Reading Material
– Chapter 11, 12 & 42 of /Docs/[Link]
25
Summary
26
FRDM-KL46Z Overview
FRDM-KL46Z Platform
• Hardware Overview:
– MKL46Z256VLL4 MCU (48 MHz,
256KB Flash, 32 KB RAM, Low
power, 100LQFP package)
– Dual role USB interface with
mini-B USB connector
– 4 digit segment LCD module
– Capacitive touch slider
– Ambient light sensor
– MMA8451Q accelerometer
– MAG3110 Magnetometer
– 2 user LEDs
– 2 user push buttons
– Programmable OpenSDA debug
interface
27
FRDM-KL46Z Overview
References: (at : [Link]
① FRDM-KL46Z User’s Manual:
/Docs/FRDM-KL46Z_UM.pdf
② Quick Start Guide for FRDM-KL46Z:
– /Docs/Quick Start Guide for Freedom KL46Z [Link]
③ KL46 Sub-Family Reference Manual
– /Docs/[Link]
④ FRDM-KL46Z Schematic
– /Docs/FRDM-KL46Z_SCH.pdf
⑤ FRDM-KL46Z Example
– /FRDM-KL46Z Demo example/
⑥ FRDM-KL46 OpenSDA applications:
– /OpenSDA applications/
28
Development Tool chain Overview (KEIL)
How to create a binary (.bin) file
- For Keil uVision this can also be done automatically.
- Go to Projects->Options for target 'project_name'...
- Go to the User tab,
- In Run User Programs After Build/Rebuild...
- add 'C:\Keil_v5\ARM\ARMCC\bin\[Link] --bin --output
=.\Objects\[Link] .\Objects\[Link]' to one of the lines.
This will run the conversion from AXF to BIN automatically after each
build.
29