12
AGH University of Science and Technology Faculty of Computer Science, Electronics and Telecommunications Department of Electronics MICROPROCESSOR TECHNOLOGY II LAB 1 Introduction to Keil environment and Kinetis family © Sebastian Koryciak http://www.fpga.agh.edu.pl/upt2 01.10.2015

MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

  • Upload
    doannhi

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

AGH University of Science and Technology

Faculty of Computer Science, Electronics and Telecommunications Department of Electronics

MICROPROCESSOR TECHNOLOGY II

LAB 1

Introduction to Keil environment and Kinetis family

© Sebastian Koryciak

http://www.fpga.agh.edu.pl/upt2

01.10.2015

Page 2: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

1 INTRODUCTION

1.1 OBJECTIVE

The main goal of this exercise is to introduce laboratory participants to the way of creating their first

project in uVision environment. At the beginning there is briefly described characteristics of the

FRDM-KL46Z set. Next there are the steps of creating, programming, debugging (fundamentals) and

simulating the software written in C language.

1.2 PREREQUISITES

Hardware:

desktop PC that meets the hardware requirements of KEIL v5 software,

FRDM-KL46Z development kit

Software:

Windows XP or higher,

Keil / uVision 5 environment

Experience:

basic computer skills,

basic knowledge about Windows operating systems,

basic knowledge about Microprocessor Technology,

Digital Electronics principles

Page 3: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

2 HARDWARE PLATFORM – FRMD-KL46Z EVALUATION BOARD

The Freescale Freedom development platform is a set of software and hardware tools for evaluation

and development. It is ideal for rapid prototyping of microcontroller-based applications. The Freescale

Freedom KL46Z hardware, FRDM-KL46Z, is a simple, yet sophisticated design featuring a Kinetis L

series microcontroller, built on the ARM® Cortex™-M0+ core. The FRDM-KL46Z features the Freescale

open standard embedded serial and debug adapter known as OpenSDA. This circuit offers several

options for serial communications, flash programming and run-control debugging. The features of the

evaluation board include:

32 bit MKL46Z356VLLZ4 microcontroller from Kinetis L family containing:

o 256 kB Flash,

o 32 kB SRAM,

o Max operational frequency: 48 MHz,

o RTC,

o Watchdog,

o DMA,

o 12-bit DAC,

o 16-bit SAR ADC,

o USB Full Speed controller,

o Low-voltage UART module,

o 2 standard UART modules,

o 2 x I2C,

o I2S,

o 2 x SPI,

OpenSDA,

Capacitive touch slider,

4 digit segment LCD module,

MMA8451Q accelerometer,

MAG3110 magnetometer,

2 user LEDs,

2 user push buttons,

Ambient light sensor,

Reset button

Page 4: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

3 KEIL ENVIRONMENT – FIRST LAUNCH

The uVision 5 is environment made by Keil (ARM) and is designed for programming and debugging

microcontrollers based on ARM architecture.

3.1 EXERCISE Please make sure that software is installed. If appropriate icon of „Keil uVision 5” is not placed on your

desktop, or any other place like Menu Start, please open PRE LAB instruction and follow its second

chapter.

3.2 EXERCISE Run uVision 5 and create new C language project.

1. Click twice on icon placed both on desktop and in Menu Start.

2. Choose from menu Project > New uVision Project …

3. Point the destination path for new project which we would call: hello_world\project (ex.

C:\MDK-ARM\sr_1115_4\hello_world\project.uvproj)

WARNING! All of the projects created during this course must appear in folder C:\MDK-ARM\x_y_z\

where x is the day of classes (mo / tu / we / th / fr), y is classes start time (1100 / 1230 / …), and z is

number of two-persons team

4. The next step is to select a microcontroller on which we will work. According to description

placed in second section of this instruction we are looking for MKL46Z256xxx4

Page 5: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

5. Newly opened window serves to select basic libraries for our project. For this exercise will be

needed: CMSIS > CORE, and Device > Startup

6. Project created in this way should look according to the following screenshot.

Page 6: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

7. The first cosmetic changes will apply to directory names. Name „Target1” (A) change for

„CMSIS-DAP”, and „Source Group 1” (B) for „src”. Then right click on „src” folder and choose

option „Add New Item to Group ‘src’…”.

8. In newly opened window choose C language, and fill the Name gap with „main”.

A

B

Page 7: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

3.3 EXERCISE Create your first program by adding following content to main.c file:

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

#include "MKL46Z4.h" #define GreenLED 0 const uint32_t MaskLED[] = {1UL << 5}; void InitLED(void){ SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK; PORTD->PCR[5] = PORT_PCR_MUX(1UL); PTD->PDDR |= MaskLED[GreenLED]; } void BlinkLED(void){ uint32_t i = 0; PTD->PTOR = MaskLED[GreenLED]; for(i = 0; i < 3000000; i++){}; } int main(void){ InitLED(); while(1){ BlinkLED(); } }

Program always starts from attached header files. In this case this header file holds only register

“masks" and names which allow us for more intuitive use of those registers. If you wish to look into

the file, just right click on header name and select Open Document <MKL46Z4.H>. For the most part,

the masks are named based on the KL46 reference manual in the form 'CHAPTER NAME'_'REGISTER

NAME'_'BIT NAME'_MASK. Secondly there is a definition of green LED, as an index in mask, created in

the next line of code. Whole program consist of modules:

Page 8: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

InitLED,

BlinkLED,

main.

InitLED is a function to initialize GPIO port (General Purpose Input Output). For correct configuration

we will be modifying registers listed below:

System Integration Module (SIM) – this module gives control of which peripherals are clocked.

By leaving them without access to the clock we save power, but leave them inoperable.

o System Clock Gating Control Register 5 (SIM_SCGC5) – this register holds the clock

enable for the various ports. In order to use any pins on a port this must be enabled

for that port (in our case port D).

Port Control and Interrupts (PORT) – this register determines which pins are used for which

peripheral (page 172 in KL46_RM)

o Pin Control Register (PORTD_PCR5) – setting up a function on a multiplekser, which

pin will fulfill; we will be using 5 pin on port D to control green LED

General Purpose Input Output (GPIO) – once a pin is configured as GPIO this register

determines whether it is an input or an output

o Port Data Direction Register (GPIO_PDDR) – 1 = output, 0 = input

o Port Data Output Register (GPIO_PDOR) – change of value on pin

o Port Data Set Register (GPIO_PSOR) – setting value 1

o Port Data Clear Register (GPIO_PCOR) – setting value 0

o Port Data Toggle Register (GPIO_PTOR) – changing value for opposite form current

o Port Data Input Register (GPIO_PDIR) – for reading value when selected input option

BlinkLED is a function to implement one from many ways for blinking LED. There was chosen

GPIO_PTOR register.

Main is a master function, which perform initialization of GPIO ports, and then starts blinking of a LED

in the loop.

3.4 EXERCISE Based on above description of program functionality and Reference Manual, add in your own words

comment to every line of the code in main.c file. In what purpose in lines 7 and 9 there is |= sign?

Place answer as an equation in comment.

3.5 EXERCISE Run application on the FRDM-KL46Z set:

WARNING! On this stage please make sure that on your evaluation board are loaded correct CMSIS

drivers. Please go back to PRE LAB instruction and procced according to section 4.

1. After making file main.c complete please build project. We can do this by selecting from the

menu Project > Build Target, or by pressing F7 key.

2. Next once we should configure debugger. For this purpose click on Options for Target …

3. In newly opened window choose Debug tab, and from list in „Use” look for „CMSIS-DAP

Debugger”.

Page 9: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

4. Then click on Settings, and there from list for Port choose „SW”.

5. Additionally in Flash Download tab select „Reset and Run” option.

Page 10: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

6. After actualization of every settings, in main window in toolbar click on

Flash > Download (or icon).

3.6 EXERCISE When programming the essential feature is the ability to debug code. Therefore, now check how our

first program work.

WARNING! During the debugging or simulating program please do not close the windows with files

that are open in this perspective. Their closure may threatens suspension of the program.

1. In main.c file please choose the line of code in which there is the procedure for changing value

of pin 5 in port D (PTD->PTOR), and press F9 key (you can also click on red ball icon). In this

way we assign the Breakpoint. There could be only one during debugging.

2. Then to start debug session please press Ctrl+F5 combination or (In case of using

evaluation version there will appear message about code size limitations)

3. Open content of GPIO ports: Peripherals > System Viewer > GPIO > PTD

4. Several times press F5 key (Run) and check what kind of changes occurred in individual

registers.

5. Close debug perspective by clicking on „d” icon, or by Ctrl+F5 shortcut.

3.7 EXERCISE Often we do not have access to hardware during creating software. This is when we use simulator.

1. To run simulator go to debuger configuration, that is

2. Then in tab „Debug” choose Use Simulator (A).

Page 11: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

3. Next to window titled: „Initialization File” click on three dots icon (B), enter „init” name and

confirm it by Open button. Accept creating new file.

4. Then we must modify this initialization file. Therefore open it by Edit button (C) (it will open in

the background). Window „Options of Target …” close by OK button.

5. To init.ini file copy:

1. MAP 0x40000000, 0x400FFFFF read write

6. Save file, and next localize and open „system_MKL46Z.c” file (in Project sub-window, Device)

7. In opened file find and comment (by adding in the front of each of them //) following lines:

124. 125. 126. 127.

while((MCG->S & MCG_S_IREFST_MASK) == 0x00U) { /* Check that the source of the …. } while((MCG->S & 0x0CU) != 0x00U) { /* Wait until output of the FLL is selected */ }

8. Save and build whole project (shortcut F7)

9. Next repeat steps from exercise 3.6

A

B

C

Page 12: MICROPROCESSOR TECHNOLOGY II LAB1.pdf · project in uVision ... basic knowledge about Microprocessor Technology, ... Create your first program by adding following content to main.c

4 HOMEWORK

In order to practice setting ports please modify main program according to tasks:

1. Add support of red LED (schematics of FRDM-KL46Z) by using mask (modify existing one for

both LEDs)

2. Add function of blinking red LED with different than green one frequency, and using other

than GPIO_PTOR register.

3. Try to simulate program only on core registers.