‘C’ for Microcontrollers, Just Being Efficient Lloyd Moore, President Seattle Robotics Society.

Slides:



Advertisements
Similar presentations
Computer Architecture
Advertisements

DSPs Vs General Purpose Microprocessors
Instruction Set Design
There are two types of addressing schemes:
1 Chapter 3 Jump, Loop, and Call Instructions. 2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
The University of Adelaide, School of Computer Science
MICROPROCESSORS TWO TYPES OF MODELS ARE USED :  PROGRAMMER’S MODEL :- THIS MODEL SHOWS FEATURES, SUCH AS INTERNAL REGISTERS, ADDRESS,DATA & CONTROL BUSES.
8051 Core Specification.
Chapter 8. Pipelining. Instruction Hazards Overview Whenever the stream of instructions supplied by the instruction fetch unit is interrupted, the pipeline.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Computer Organization. This module surveys the physical resources of a computer system. –Basic components CPUMemoryBus I/O devices –CPU structure Registers.
Software and Hardware Circular Buffer Operations First presented in ENCM There are 3 earlier lectures that are useful for midterm review. M. R.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) Lecturer: Hui Wu Session.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
State Machines Timing Computer Bus Computer Performance Instruction Set Architectures RISC / CISC Machines.
PhD/Master course, Uppsala  Understanding the interaction between your program and computer  Structuring the code  Optimizing the code  Debugging.
ARM Core Architecture. Common ARM Cortex Core In the case of ARM-based microcontrollers a company named ARM Holdings designs the core and licenses it.
MICROCONTROLLER INSTRUCTION SET
‘C’ for Microcontrollers, Just Being Efficient Lloyd Moore, President
System Calls 1.
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
IT253: Computer Organization
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Chapter 4 Memory Management Virtual Memory.
Moving Arrays -- 1 Completion of ideas needed for a general and complete program Final concepts needed for Final Review for Final – Loop efficiency.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
4-Oct Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept  direct mode: OK for static addresses  indirect register mode:
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
Computer Architecture Lecture 32 Fasih ur Rehman.
Lecture Set 4 Programming the 8051.
The 8051 Assembly Language. Overview Data transfer instructions Addressing modes Data processing (arithmetic and logic) Program flow instructions.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
8051 Micro Controller. Microcontroller versus general-purpose microprocessor.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Introduction 8051 Programming language options: Assembler or High Level Language(HLL). Among HLLs, ‘C’ is the choice. ‘C’ for 8051 is more than just ‘C’
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Week 6 Dr. Muhammad Ayaz Intro. to Assembly Language.
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
I NTEL 8086 M icroprocessor بسم الله الرحمن الرحيم 1.
CHAPTER ADDRESSING MODES.
Format of Assembly language
Embedded Systems Design
Advanced Topic: Alternative Architectures Chapter 9 Objectives
8051 Addressing Modes The way, using which the data source or destination addresses are specified in the instruction mnemonic for moving the data, is.
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
BIC 10503: COMPUTER ARCHITECTURE
Baremetal C Programming for Embedded Systems
Lecture 4: MIPS Instruction Set
Code Generation.
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Subroutines and the Stack
Explaining issues with DCremoval( )
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Understanding the TigerSHARC ALU pipeline
Process.
‘C’ for Microcontrollers, Just Being Efficient
Where is all the knowledge we lost with information? T. S. Eliot
Addressing Modes in 8051 MC S. Lourduraj Asst. Prof. of Physics
COMP755 Advanced Operating Systems
Presentation transcript:

‘C’ for Microcontrollers, Just Being Efficient Lloyd Moore, President Seattle Robotics Society 9/15/2012

Agenda Microcontroller Resources Knowing Your Environment Memory Usage Code Structure Optimization Summary

Disclaimer Some microcontroller techniques necessarily need to trade one benefit for another – typically lower resource usage for maintainability Point of this presentation is to point out various techniques that can be used as needed Use these suggestions when necessary Feel free to suggest better solutions as we go along

Microcontroller Resources EVERYTHING resides on one die inside one package: RAM, Flash, Processor, I/O Cost is a MAJOR design consideration Typical costs are $0.25 to $25 each (1000’s) RAM: 16 BYTES to 256K Bytes typical Flash/ROM: 384 BYTES to 1M Byte Clock Speed: 4MHz to 175MHz typical Much lower for battery saving modes (32KHz) Bus is 8, 16, or 32 bits wide Have dedicated peripherals (MAC, Phys, etc)

Power Consumption Microcontrollers typically used in battery operated devices Power requirements can be EXTREMELY tight Energy harvesting applications Long term battery installations (remote controls, hard to reach devices, etc.) EVERY instruction executed consumes power, even if you have the time and memory!

Know Your Environment Traditionally we ignore hardware details Need to tailor code to hardware available Specialized hardware MUCH more efficient Compilers typically have extensions Interrupt – specifies code as being ISR Memory model – may handle banked memory and/or simultaneous access banks Multiple data pointers / address generators Debugger may use some resources

Memory Usage Put constant data into program memory (Flash/ROM) Alignment / padding issues Typically NOT an issue, non-aligned access ok Avoid dynamic memory allocation, even if available Take extra space and processing time Memory fragmentation a big issue Use and reuse static buffers Reduces variable passing overhead Allows for smaller / faster code due to reduced indirections Does bring back over write bugs if not done carefully More reliable for mission critical systems Use the appropriate variable type Don’t use int and double for everything!! Affects processing time as well as storage

C99 Datatypes – inttypes.h int8_t, int16_t, int32_t, int64_t uint8_t, uint16_t, uint32_t, uint_64_t Avoids the ambiguity of int and uint when moving code between processors of different native size Makes code more portable and upgradable over time

Char vs. Int Increment on 8051 char cX; cX++; 000A MOV DPTR,#cX 000D E0 MOVX 000E 04 INC A 000F F0 6 Bytes of Flash 4 Instruction cycles int iX; iX++; MOV DPTR,#iX 0003 E4 CLR A F001 MOV B,#01H LCALL ?C?IILDX 10 Bytes of Flash + subroutine overhead Many more than 4 instruction cycles with a LCALL

Code Structure Count down instead of up Saves a subtraction on all processors Decrement-jump-not-zero style instruction on some processors Pointers vs. array notation Generally better using pointers Bit Shifting May not always generate what you think May or may not have barrel shifter hardware May or may not have logical vs. arithmetic shifts

Shifting Example on 8051 cX = cX << 3; RLC A RLC A RLC A F8 ANL A,#0F8H Constants turn into seperate statements Variables turn into loops Both of these can be one instruction with a barrel shifter cA = 3; cX = cX << cA; 000B MOV DPTR,#cA 000E E0 MOVX 000F FE MOV R6,A 0010 EF MOV A,R A806 MOV R0,AR INC R SJMP ?C ?C0004: 0016 C3 CLR C RLC A 0018 ?C D8FC DJNZ R0,?C0004

Indexed Array vs Pointer on M8C ucMode = g_Channels[uc_Channel].ucMode; 01DC 52FC mov A,[X-4] 01DE 5300 mov [__r1],A 01E mov A,0 01E2 08 push A 01E mov A,[__r1] 01E5 08 push A 01E mov A,0 01E8 08 push A 01E mov A,7 01EB 08 push A 01EC 7C0000 xcall __mul16 01EF 38FC add SP,-4 01F1 5F0000 mov [__r1],[__rX] 01F4 5F0000 mov [__r0],[__rY] 01F add[__r1],<_g_Channels 01FA 0E0000 adc[__r0],>_g_Channels 01FD 3E00 mvi A,[__r1] 01FF 5403 mov [X+3],A ucMode = pChannel->ucMode; 01ED 5201 mov A,[X+1] 01EF 5300 mov [__r1],A 01F1 3E00 mvi A,[__r1] 01F mov [X+5],A Does the same thing Saves 29 bytes of memory AND a call to a 16 bit multiplication routine! Pointer version will be at least 4x faster to execute as well, maybe 10x Most compilers not this bad – but you do find some!

More Code Structure Actual parameters typically passed in registers if available Keep function parameters to less than 3 May also be passed on stack or special parameter area May be more efficient to pass pointer to struct Global variables While generally frowned upon for most code can be very helpful here Typically ends up being a direct access Read assembly code for critical areas Know which optimizations are present Small compilers do not always have common optimizations Inline, loop unrolling, loop invariant, pointer conversion

Switch Statement Implementation Switch statements can be implemented in various ways Sequential compares In line table look up for case block Special function with look up table Specific implementation can also vary based case clauses Clean sequence (1, 2, 3, 4, 5) Gaps in sequence (1, 10, 30, 255) Ordering of sequence (5, 4, 1, 2, 3) Knowing which method gets implemented is critical to optimizing!

Switch Statement Example switch(cA) { case 0: cX = 4; break; case 1: cX = 10; break; case 2: cX = 30; break; default: cX = 0; break; } MOV DPTR,#cA 0009 E0 MOVX 000A FF MOV R7,A 000B EF MOV A,R7 000C LCALL ?C?CCASE 000F 0000 DW ?C DB 00H DW ?C DB 01H DW ?C DB 02H DW 00H 001A 0000 DW ?C C ?C0002: 001C MOV DPTR,#cX 001F 7404 MOV A,#04H 0021 F SJMP ?C More blocks follow for each case

Optimization Process Step 0 – Before coding anything, think about risk points and prototype unknowns!!! Use available dedicated hardware Step 1 – Get it working!! Fast but wrong is of no use to anyone Optimization will typically reduce readability Step 2 – Profile to know where to optimize Usually only one or two routines are critical You need to have specific performance metrics to target

Optimization Process Step 3 – Let the tools do as much as they can Turn off debugging! Select the correct memory model Select the correct optimization level Step 4 – Do it manually Read the generated code! Might be able to make a simple code or structure change. Last – think about assembly coding

Summary Microcontrollers are a resource constrained environment Be familiar with the hardware in your microcontroller Be familiar with your compiler options and how it translates your code For time or space critical code look at the assembly listing from time to time

Questions?