ARM 개발환경 소개 및 실험 1 Cortex-M4 특징 S32K144 MCU 특징 Keil MDK 개발환경 GPIO 사용하기 About the Cortex-M4 processor and core peripherals ⚫ The Cortex-M4 processor is a high performance 32-bit processor designed for the microcontroller market. It offers significant benefits to developers, including: ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ outstanding processing performance combined with fast interrupt handling enhanced system debug with extensive breakpoint and trace capabilities efficient processor core, system and memories ultra-low power consumption with integrated sleep mode and an optional deep sleep mode platform security robustness, with optional integrated Memory Protection Unit (MPU) <Cortex-M4 Devices Generic User Guide> 자료 참고 1 2 Cortex-M4 processor features and benefits summary ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ tight integration of system peripherals reduces area and development costs Thumb instruction set combines high code density with 32-bit performance optional IEEE754-compliant single-precision FPU code-patch ability for ROM system updates power control optimization of system components integrated sleep modes for low power consumption fast code execution permits slower processor clock or increases sleep mode time hardware division and fast digital-signal-processing orientated multiply accumulate saturating arithmetic for signal processing deterministic, high-performance interrupt handling for time-critical applications optional Memory Protection Unit (MPU) for safety-critical applications extensive implementation-defined debug and trace capabilities: ⚫ Serial Wire Debug and Serial Wire Trace reduce the number of pins required for debugging, tracing, and code profiling. 3 Cortex-M4 core peripherals ⚫ Nested Vectored Interrupt Controller ⚫ ⚫ System Control Block ⚫ ⚫ The system timer, SysTick, is a 24-bit count-down timer. Use this as a Real Time Operating System (RTOS) tick timer or as a simple counter. Memory Protection Unit ⚫ ⚫ The System Control Block (SCB) is the programmers model interface to the processor. It provides system implementation information and system control, including configuration, control, and reporting of system exceptions. System timer ⚫ ⚫ The NVIC is an embedded interrupt controller that supports low latency interrupt processing. The Memory Protection Unit (MPU) improves system reliability by defining the memory attributes for different memory regions. It provides up to eight different regions, and an optional predefined background region. Floating-point Unit ⚫ The Floating-Point Unit (FPU) provides IEEE754-compliant operations on single-precision, 32-bit, floating-point values. 4 Core registers 5 Memory model 6 Bit Banding ⚫ Fast single-bit manipulation: 1MB ➔ 32MB aliased regions in SRAM & Peripheral space 7 S32K144 MCU 특징 8 High-level architecture diagram for the S32K14x and S32K14xW family 9 Instruction Set 10 11 12 13 14 GPIO 사용하기 15 16 17 18 19 20 Keil MDK 개발환경 ⚫ ⚫ <Getting Started with MDK Version 5> 자료 참조 <Cortex-M4 Tutorial Using the S32K144 EVB evaluation board> 자료 참조 21 프로젝트 생성방법 [1/9] ⚫ Bare Metal programming ⚫ a term for programming that operates without various layers of abstraction or, as some experts describe it, "without an operating system supporting it." Bare-metal programming interacts with a system at the hardware level, taking into account the specific build of the hardware. 1. Keil v5 실행 2. Project -> new uvision project 클릭 3. 프로젝트 생성 폴더에서 Asm_SW_LED2 폴더 선택 -> Asm_SW_LED2 저장 22 프로젝트 생성방법 [2/9] 4. Select Device for uvprojxTarget ‘Target 1’ 창에서 Core 선택 NXP -> S32K Series -> S32K14x -> S32K144 -> S32K144HAxxxLHx(최상단) OK (Project 생성 후 자동으로 실행되나, 이 단계를 놓쳤다면 메뉴에서 project -> Select Device for Target) *비활성화 시 Setp1부터 다시 시작 23 프로젝트 생성방법 [3/9] 5. Manage Run-time Environment -> CMSIS -> CORE , Device -> Startup 선택 후 OK 24 프로젝트 생성방법 [4/9] 6. Option for Target -> C/C++(AC6) -> include Paths -> Folder Setup -> [현재 프로젝터 경로] RTE 폴더선택 -> Device 폴더 선택 -> S32K144HAxxxLHx 폴더 선택 후 OK 25 프로젝트 생성방법 [5/9] 7. Linker -> Scatter File -> [현재 프로젝트 경로] RTE -> Device -> S32K144_64_Flash.sct 열기 26 프로젝트 생성방법 [6/9] 8. Debug Tab -> Use -> Pemicro Debugger 선택 후 Settings 클릭 * Pemicro Debugger가 인식이 되지 않을 경우, CMSIS-DAP Debugger 선택 27 프로젝트 생성방법 [7/9] 9. Setting 클릭 후 그림과 같이 “Connection port and Interface type”부분과 “Device selection” 부분 설정 후 ‘ok’ 28 프로젝트 생성방법 [8/9] 10. Output -> Name of Executable -> 현재 프로젝트 Name 입력 및 확인 29 프로젝트 생성방법 [9/9] 11. Project Windows에서 Source Group1 마우스 오른쪽 클릭 -> Add Existing File to group ‘source Group 1’ click -> [현재 프로젝트 경로] test_led.s File add -> Close 30 About Project Set-Up ⚫ Every Keil uVision project contains these two file: ⚫ ⚫ ⚫ Startup.s Startup.c assembly code c code ⚫ Sets the starting point of program: Reset_Handler ⚫ Outlines the stack size ⚫ Outlines the heap size ⚫ The reset vector and all the interrupt vectors Reset_Handler runs everytime the reset button is pressed. ⚫ _main ➔ Main() 31 PORT Module • The PORT Module contains Pin Configuration Registers (PCRs ) that are used to specify the purpose of a pin. • PCR n in Port x is referred to as PTxn and is located at an offset from the base address for that port. S32K144 EVB 관련\ S32K1xx Series Reference Manual.pdf Section 12.1.2 참조 • For port base addresses, see S32K1xx memory map found in the spreadsheet S32K1xx_Memory_Map.xlsx • Each port has additional registers we won’t be using. Memory Map • See the spreadsheet S32K1xx_Memory_Map.xlsx • Base address of all memory modules and peripheral devices • Subsequent references to register locations are as offsets from the base address (start address) given in the memory map. • Will need some of these in this class Finding the PCR Address PCR for GPIO • Pin Configuration register (PCR). • One set of PCRs for the pins in each port. • PCR register bitfields – S32K144 EVB 관련\ S32K1xx Series Reference Manual.pdf Section 12.6.1 참조 • MUX register determines purpose of pin: set MUX to 0b001 for GPIO. • Other bitfields set various electrical properties – leave at reset values. MUX Pin Mux Control Not all pins support all pin muxing slots. Unimplemented pin muxing slots are reserved and may result in configuring the pin for a different pin muxing slot. The corresponding pin is configured in the following pin muxing slot as follows: 000 Pin disabled (Alternative 0) (analog). 001 Alternative 1 (GPIO). 010 Alternative 2 (chip-specific). ⁞ GPIO Registers • Once a pin is configured for GPIO, additional properties are specified by GPIO registers S32K144 EVB 관련\ S32K1xx Series Reference Manual.pdf section 13.3.1.1 참조 • One set of GPIO registers for each port (Table 13.1) What are the base addresses for GPIO? (see the memory map) GPIOA: 0x400FF000h GPIOB: 0x400FF040h GPIOC: 0x400FF080h GPIOD: 0x400FF0C0h GPIOE: 0x400FF100h GPIO Registers • Each GPIO register has one bit for each pin associated with the port - some bits in the register not used • Port Data Direction Register (PDDR) - is the pin used for input or output? Port Data Output Register (PDOR) - if output, sets the value on the pin (0 or 1) Port Data Input Register (PDIR) - if input, reads the value of the pin (0 or 1) • • Example: Port Data Direction Register (PDDR) • • • • D17 is an output pin GPIOD base address is 0x400FF0C0 PDDR offset is 0x14 Write “1” to bit 17 GPIOD PDDR at 0x400FF0D4 Example: Port Data Output Register (PDOR) • • • • Light LED connected to D17 GPIOD base address is 0x400FF0C0 PDOR offset is 0 Write “1” to bit 17 GPIOD PDOR at 0x400FF0C0 Example: Port Data Input Register (PDIR) • • • • Switch connected to D10 GPIOD base address is 0x400FF0C0 PDIR offset is 0x10 Read bit 10 GPIOD PDIR at 0x400FF0D0 Hello LED for Assembly ⚫ Using on-board SW2 and Blue LED ⚫ SW2를 이용하여 Blue LED On/Off Hello LED for Assembly Clock Configuration GPIO Switch Configuration (input) ⚫ Flow-Chart GPIO LED Configuration (Output) Infinite loop Hello LED for Assembly Infinite loop Input State Check ⚫ Flow-Chart (Loop Flow) SW2 Input Value ==1 PD0 Blue LED ON Loop back PD0 Blue LED Off Arm assembly 소개 ⚫ ⚫ Instructions have four fields separated by space or tabs. Label opcode operand ; comment Init MOV R0, #150 ; R0 = 150 Directives ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ assist and control assembly process. are also called peudo-ops. are not part of the instruction set. Change the way code is assembled. EXPORT(or GLOBAL) – to make an object accessible from another file. IMPORT – to access an “exported” object. END – Placed at the end of each file. EQU – To give a symbolic name to a numeric constant. DCB, DCW, DCD… 44 Hello LED for Assembly #PCR(Pin Control Register) PORTC_PCR_BASE EQU 0x4004B000 PORTD_PCR_BASE EQU 0x4004C000 PCR_C12 EQU 0x4004B000+0x30 ;(12*4)hex PCR_D0 EQU 0x4004C000 ⚫ Assembly Hello LED for Assembly GPIOA EQU 0x400FF000 GPIOB EQU 0x400FF040 GPIOC EQU 0x400FF080 GPIOD EQU 0x400FF0C0 PDDR_C EQU GPIOC+0x14 PDDR_D EQU GPIOD+0x14 PDIR_C EQU GPIOC+0x10 PCOR_D EQU GPIOD+0x08 PSOR_D EQU GPIOD+0x04 ⚫ Assembly Hello LED for Assembly ⚫ Assembly 부록1 #Peripheral Clock Control PCC_BASE EQU 0x40065000 PCC_PORTC EQU PCC_BASE+0x12C PCC_PORTD EQU PCC_BASE+0x130 CLOCK_EN EQU 0x40000000 부록2 S32K116 S32K118 S32K148 S32K146 Peripheral instance S32K144 Peripheral description S32K142 “부록07_S32K-Reference Manual.pdf”참조 1. Base Address = S32K Reference Mannual -> PCC register descriptions -> PCC Memory Map PBRIDGE PBRIDGE onStart End off-platform platform Size (KB) address address slot slot (hex) (hex) (OPACR) (PACR) 2. Clock_EN = S32K1xx_Memory_Map.xlsx -> Peripheral Memory Map -> *Peripheral bridge (AIPS-Lit e) Peripheral bridge (AIPS-Lite) AIPS Lite 0 4 40000000 40000FFF x x x x x x Hello LED for Assembly ⚫ Assembly AREA mycode, CODE ENTRY EXPORT __main Clock Configuration __main GPIO Switch Configuration (input) GPIO LED Configuration (Output) Infinite loop LDR r1,=PCC_PORTC ; PCC_PORTC Register= 12C, ; PCC Base address Portc Clock = 4006_5000h LDR r2,=CLOCK_EN STR r2,[r1,#0x00] ; PCC Clock Setting C STR r2,[r1,#0x04] ; PCC Clock Setting D Hello LED for Assembly Clock Configuration GPIO Switch Configuration (input) GPIO LED Configuration (Output) Infinite loop ⚫ Assembly LDR r0,=PDDR_C LDR r1,[r0,#0x00] ; r1=PDDR_C BIC r1,r1,#0x1000 ;C12 is configured as input ; BIC : Logical AND NOT or bit clear (ex) BICS R1, R1, #0x10 => R1 = R1 AND ~0x10 STR LDR r1,[r0,#0x00] ;PDDR_C <-- 0x1000 C12 is input r1,=PCR_C12 ; MOV r2,#0x110 ; r2<--0x110 STR r2,[r1,#0x00] ; PCR_C12 is configured as GPIO Hello LED for Assembly Clock Configuration GPIO Switch Configuration (input) GPIO LED Configuration (Output) Infinite loop ⚫ Assembly LDR r2,[r0, #0x40] ; r2 = contents of [PDDR_D] ORR r2,r2,#0x01 ; r2= 0x01 STR r2,[r0,#0x40] ; PDDR_D0 is configured as output MOV r2,#0x100 ; r2=0x100 LDR r1,=PCR_D0 STR r2,[r1,#0x00] ; PCR_D0 is configured as GPIO B NOT_PUSH Hello LED for Assembly Clock Configuration ⚫ Assembly ON_PUSH LDR GPIO Switch Configuration (input) ; PCOR_D, 0x400FF094+0x34 = 0x400FF0C8 ORR GPIO LED Configuration (Output) Infinite loop r1,[r0,#0x34] STR r1,r1,#0x01 r1,[r0,#0x34] ; PORTD_PCOR Hello LED for Assembly Clock Configuration ⚫ Assembly NOT_PUSH LDR r1,[r0,#-0x04] ; PDIR_C, 0x400FF094-0x04 = 0x400FF090 GPIO Switch Configuration (input) LSLS ; LSL(s) – Logical shift left BMI GPIO LED Configuration (Output) r1,r1,#19 ON_PUSH ; BMI – Branch on Minus (Negative) LDR r1,[r0,#0x30] ; PSOR_D, 0x400FF094+0x30 = 0x400FF0C4 ORR Infinite loop r1,r1,#0x01 STR r1,[r0,#0x30] ;PORTD PSOR B NOT_PUSH END - Build 클릭 후 에러 시 ‘startup_S32k144.s’ 파일 내에 “IMPORT main” 부분 주석 처리 - Start/stop debug session 클릭 후 Connect ➔ Hotsync 실험 1-1 (S32K144 EVB – GPIO) ⚫ - 앞 실행 예제 실습 : SW2를 이용한 Blue LED 점멸 “test_led.s” 예제 파일 참조 실험 1-2 (S32K144 EVB – GPIO) - SW2를 이용한 Red LED 점멸 56 실험 1-3 (S32K144 EVB – GPIO) ⚫ SW2를 누르면 Red LED ON, SW2를 떼면 Blue LED ON 57