23
ASM and C / C++ programing to activate a “MOCKED” microcontrolled COFFEEPOT Quick Overview of INDIVIDUAL Assignment 1 All form previous class slides – Look for more detail

ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

ASM and C / C++ programing to activate a “MOCKED” micro‐controlled COFFEEPOT

Quick Overview of INDIVIDUAL Assignment 1

All form previous class slides – Look for more detail

Page 2: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Essentials of INDIVIDUAL Assignment 1

• Demonstrate that you have learnt to use CCES IDE• Demonstrate you understand the use of bit‐wise AND and

OR• Recap your C++ knowledge• Demonstrate new knowledge of Blackfin ASM• Avoid the following common mistakes in ASM

– Missing LINK and UNLINK– Missing JUMP back in while loops– Misuse of the volatile registers R0 – R3, P0 – P2 when calling a 

subroutine from inside ASM function– Misuse of the registers R4 – R8, P3 – P5 when calling a 

subroutine from inside ASM function– Misuse of [‐‐SP] and [SP++] operations when calling a 

subroutine from inside ASM function• Have some FUN!! 2

Page 3: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

You already have• Coffeepot main which shows how to set up a MOCK coffeepot, install it on the MOCK bus

• Call a function that fills the coffee pot with water to a certain level

• Ability to show coffeepot behaviour using TEXT GUI and Java plugin GRAPHICAL GUI

• Conditional excluded code to  call a function that fills the coffee pot with water to a certain level and heats it to a certain temperature – YOU TO COMPLETE

• Conditional excluded code to  call a function that SIMULTANEOUSLY fills two coffee pots with water to a certain level and heats then to a certain temperature and MAINTAINS that water level and heat (+‐15% or better) YOU TO COMPLETE 3

Page 4: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

You already have• Demonstration of the C++ code to fill the coffee pot with water to a 

certain level using a loop• Demonstration of the ASM code to fill the coffee pot with water to 

a certain level using a loop• Your responsibility to develop C++ code to heat coffeepot to a 

certain level in a loop• Your responsibility to develop ASM code to heat coffeepot to a 

certain level in a loop• When heating and filling TWO coffee pots – you must fill and heat 

one coffeepot using C++ routines and fill and heat the other coffeepot using ASM routines 

• HINT – If you decide to change the C++ and ASM for heating and filling 1 coffeepot when you decide to fill and heat two coffeepots –then that is your choice.– Essentially – get the simple versions working – leave them alone. Now 

write better code now you have a better idea of what is needed

4

Page 5: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Simulated Coffeepot Functionality

• Provide a controlled environment to practice interfacing to peripheral devices

• Ability to interface using C++ and assembly code– Will write control code in C++ – Write in assembly code for practice

• Start by using working C++ control code as comments,and translating the working C++ code into ASM line by line

– Example of industrial practice of “MOCKING” a device– Program project uses a simulator before going into cost of generating a “production component”

• Pilots use simulators to practice emergency procedures• Surgeons (are starting to) use medical dummy simulators to practice surgery procedure rather than gain skills on real patients 

5

Page 6: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Project: Providing enough coffee to allow students to finish ENCM511 Labs

What I did – built a mock interface with this capability

• Up to 4 coffeepots to be controlled by a Blackfin BF533 microcontroller.– There is also a ARM STM32F4 Discovery and MPG board versions now available running under 

Eclipse‐Mar2 with the IAR EWARM plugin – see JEADI Articles for details of installing the plug‐in– P.P.L1.Q.Q.– What is difference between microcontroller and microprocessor?

• Coffeepot functionality – Requirements analysis– Set up coffeepot (Pot originally designed with a capacity of 0.5L = 500 mL). – Prepare coffeepot for computer control

• Perform RESET, then show that the system power is on by turning on LED1– Start water flow (One water flow control register ‐‐ Show water flow activate on LED2). 

Add water at a rate of X mL / second until 90% full– Heat water,  show heat is on (LED3), to boiling (100 C).  Designed with a heat boost for days when you 

need the coffee in a hurry– Perk for Y seconds, and indicate that ready (LED4)– Simulate in real time (over 5 minutes), and fast‐time (as fast as needed when testing functions using 

EUNIT testing framework interface)

• Throw in some unexpected behaviour so that if miss‐programmed the system does some fun stuff to simulate real industrial problems.

• Recalled and re‐Designed with safety features, likeAvoiding fires by turning off the heat if unattended, Avoiding water damage by ‐‐‐‐‐‐‐ 6

Page 7: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Picture of “McCoffeePot”(Picture for McCoffeePot for 2020,                                                 

M. Diaz (SCADA GUI) and E. Ramariez (SPI GUI) versions look different)

LEDs

WATER INLETWater power  must be on for water flow

WATER IN POT. FILL TO 90%  FOR PERFECT BREW

ORIGINAL DESIGNPOT HAS 500 mLCAPACITY (WAIL)

Pot mL capacity is controlledby “Your name Variable”

FINAL DESIGN using 3 pots(Assignment 1)

my name (WDS), yourand your S.O’s name

Bonus (1%) – 4th pot sizeDr. Who’s companion’s name

TABLE

h7

CONTROL  LED USINGHUMAN SPEAKLED 4          LED 1

LED FIELD DESCRIPTIONIS A SMALL PART OF THE BITS IN THE CONTROL REGISTER 

CR BIT           CR BIT15                   12 

HEATERwith BOOST

Page 8: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

MOCKED Coffeepots must be manually connected to “real” microcontroller

MICROCONTROLLER

BF5XX

or ARM

or ????

ADDRESS BUS – 32 BITS

DATA BUS – 32 BITS

CONTROL BUS – ??? BITS

PERIPHERALINTERFACE

DEVICEREGISTERS

BASEADDRESS‐1

PERIPHERALINTERFACE

DEVICEREGISTERS

BASEADDRESS‐2

es8W.A.I.L. Connection of a coffeepot to generate a plug‐and‐play base address

is “MOCKED” using C++ HEAPS which causes similar crashes to real life

Page 9: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Design the coffeepot interfaceMost of the code discussed is given to you as functions in “CoffeePot_Library2016.dlb (a code library)” (See Lab. 0)

• Each coffee pot will be plug‐and‐play (MOCKED)– This means you plug in the coffeepot interface into the (Mocked) USB port 

and the system will recognizes it.• System recognizes the coffeepot insertion ‐‐means – the system returns to you the 

memory address (baseAddress) that you can use to control the coffeepot when accessing coffeepot registers in your C++ or assembly code. 

• In the simulation, recognize means “MANUALLY set the memory loader description file (LDF) to provide an unique  baseaddress”.  WAIL for tutorial lectureIn the simulation, return means “Call a function that reads “the memory loader description file (LDF) to determine that  unique  baseaddress”. WAIL for tutorial lecture

• Plug‐and‐Play means that somebody else wrote the USB COFFEEPOT driver to interface to computer 

• You need to know nothing about building USB driver at the moment, you just use it (function calls) to control the device.

• Each coffeepot is connected by its own USB port• Each has its own baseAddress that you use when controlling from C++ or assembly 

code.

9

Page 10: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Design the Coffeepot PeripheralAttached to the Processor Address and Data Busses

• Peripheral device register – These are OUTSIDE the core of the processor at a memory location controlled by the 

System on a Chip interface– NOT the same as MIPS processor registers ($V1, $A1) you used in ENCM369 or the 

Blackfin BF533 registers (R1, R2 etc.) you while use in ENCM511.  Those registers are INSIDE (part of) the processor core .

• CONTROL peripheral device register (CR) ‐‐ 16 bits • Peripheral memory location connected to switches (2nd LAB. 0 LED hint)

• Each bit in the register controls one switch for one device (power, water flow, etc.)• Must  control (change) one switch without changing others (Lab 0 and Assignment 1)• COURSE IMPLICATION – Must know how to do BITWISE AND and OR operations as 

Control Register values are “bit‐patterns”  and not “numbers” stored as a bit values• RESET value = 0x0000 for the control register (ALWAYS WRITE ALL 16 BITS EVEN IF 0)

– RESET value is the value present when you just turn on the device. – RESET 0x0000 means all switches are turned off – good safety feature– RESET value is set by the hardware itself automatically during the power‐up sequence

• User friendly feature note. Turn on one of the coffeepot LEDs to signify to you when the device has actually started to work (POWER ON). Don’t use print statements to show “ON” as “COFFEEPOTS” don’t have screens unless very expensive

10

Page 11: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Controlling the coffeepot – Part 2• WATER_INFLOW peripheral device register – 8 bits – RESET VALUE 0x00

– Controls amount of water following into coffeepot– Value = number of mL of water flowing into coffeepot / second– Real time – fills in about 2 minutes– Fast time simulation – allow to fill in 30 seconds– Final design – only operates if water power turned on and device is ready

• User friendly feature note. Turn on one of the coffeepot LEDs to signify that the WATER power has been turned on.

• HEATER peripheral device register – 8 bits – RESET 0x00– Value = number of J (Joules) of heat flowing into coffeepot / second (W)– SAFETY FEATURE (or BUG?) : You can burn out the coffeepot if you forget to turn off the 

heater.  The computer control automatically turns down the heater to zero over a period of  time to avoid this problem. 

• HEATER BOOST device register – 4 bits – RESET 0x0– Allows you to boost the amount of heat flowing into water so that you can heat the water 

faster for a quick coffee fix in the morning– NOTE: ‐‐ Heat applied = HEATER VALUE  * HEATER BOOST VALUE

• User friendly feature note. Turn on one of the coffeepot LEDs to signify that the HEAT power has been turned on.

11

Page 12: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Controlling the coffeepot – Part 3• LED peripheral device register – 4 bits – a bit pattern

– Controls 4 LEDs via switches– RESET 0x0 (binary b0000 – all bits 0 – all LEDS off)

• TIMER peripheral device register – 32 bits – RO (R OH)– RESET 0x00000000 – This is a NUMBER and not a bit pattern controlling switches

– RO – Read only means that the peripheral controls (writes) this value and you can’t.  NOT R0 (R zero)– a register

– We are simulating a cheap system. There is no guarantee that the different coffee pots run at the same clock rate

• WARNING  ‐‐ By tradition a hex number 0x00 is pronounced Oh X zero zero and not normally by the correct pronunciation zero X zero zero(Except on Friday afternoon between 3 and 4 with the wind blowing from the south and the sun over the yard‐arm – meaning sometimes)

** P.P.Q1 question – what is a yard arm and why should the sun be over it?Very relevant 3rd year engineering knowledge 

(by tradition and not by recommendation)

12

Page 13: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

“Initial” Design for this “Design exercise”LDF Memory Map for 2 Coffeepot devices

Peripheral device register name 

Byte Offset(From device base address)

Size Actual LDF addressesCOFFEEPOT1 registers

coffeePot1_BaseAddress  = Add_CoffeePotToSystem_PlugAndPlay(COFFEEPOT1);Actual address system returns  ‐‐ coffeePot1_BaseAddress = 0x1000 

(No idea what actual system will give as a value until plugged in (program is run))

controlRegister 0x00 16 bits – 2 byte 0x1000

TIMER  ‐‐ R0 (Oh or zero?) 0x02 32 bits – 4 bytes  0x1002

heaterRegister 0x06 8 bits – 1 byte 0x1006

waterInFlowRegister 0x07 8 bits – 1 byte 0x1007

heaterBoostRegister 0x08 4 bits – 1 nybble 0x1008

Byte Offset(From device base address)

Size Actual LDF addressesCOFFEEPOT2 registers

coffeePot2BaseAddress  = Add_CoffeePotToSystem_PlugAndPlay(COFFEEPOT2);Suppose system gives – coffeePot2_BaseAddress = 0x3000 (No idea what actual system will give as a value until plugged in)

controlRegister 0x00 8 bits – 1 byte 0x3000

TIMER  ‐‐ R0 0x02 32 bits – 4 bytes  0x3002

heaterRegister 0x06 8 bits – 1 byte 0x3006

waterInFlowRegister 0x07 8 bits – 1 byte 0x3007

heaterBoostRegister 0x08 4 bits – 1 nybble 0x300813

Page 14: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

The control register has many fields(Parts that control switches of different things)

NOTE:  The design defect (no LED control) is “accidentally fixed”  by designerBUT introduces a different design defect.  Note: COFFEEPOD ‐‐ change in “Customer Scope”

Details shown in documentation as followsSimilar format in Blackfin manual for the registers used to control Blackfin ‘on‐chip’ peripherals

COFFEEPOT PERIPHERAL CONTROL REGISTER (CPCR) ‐‐ OFFSET 0x00 – RESET 0x00 – W1S, W0C15   14    13    12    11     10     9     8        7      6        5       4     3       2      1        0

LED CONTROL  FIELD (Bits 15 to 12) 0x1 – LED1_BIT0x2 – LED2_BIT0x4 – LED3_BIT0x8 – LED4_BIT

Can combine  values in a field (Lab 0, Assign 1)0x5 causes LED1 and LED3 to be on

COFFEEPOD CONTROL FIELD (Bit 11)0x1 – Insert coffee pod

GENERAL CONTROL   (Bits 4 to 0)0x1 – b00001 ‐ Cause device to initialize (2 seconds) and stay powered up0x2 – b00010 – LED_DISPLAY_ENABLE_BIT (So LEDs work)0x4 – b00100 – WATER_ENABLE_BIT (So water will flow)0x8 – b01000 – HEATER_ENABLE_BIT (So heating will occur)0x10 –b10000 ‐ Device is ready after initialization    ‐‐ RO (read only)

Can combine values in a field0x6 (b00110) tries to turn on BOTH LED and water power

but deactivates the device (bit 0 = 0; so nothing works as a RESET has been made to happen)

NOTE: Design refactoring occurring – LED and Coffeepod? control registers “fields” of CPCRREADY BIT accounts for coffeepot is “hardware” and not “software” – so not immediately readyYou CAN’T set the READY bit (its READ ONLY), the device itself sets that bit when running properly14

Page 15: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Demonstrate main.cpp to control 1 POT

15

Page 16: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

When we can control 1 pot, we will try to control a second and both

16

Page 17: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

What do PSP header files look like, and why? 

// Do some safety first programming – Example of PSP  .h file format shown for Example_ CoffeePot_SimulatorFunctions.h

#ifndef CoffeePot_SimulatorFunctions_h // Note – underscore not dot – C++ requirement#define CoffeePot_SimulatorFunctions_h // Why are first two lines needed?   PSP – Possible question in Quiz 1

#ifndef __CPP__ // This is a common ENCM511 mistake – so stop it from happening#error (“You can’t use this include file inside ASM code – Build an ASM version of the file”);

#end

//  GENERAL CONTROL   (Bits 4 to 0) // Name the control bits to make self‐documenting#define INITandSTAYPOWERED (1 << 0)       // 0x1 – Cause device to initialize (2 seconds) and stay powered up#define LEDPOWER                        (1 << 1)      // 0x2 – LED Power on#define WATERPOWER                 (1 << 2)      // 0x4 – Water control power on#define HEATERPOWER                You finish // 0x8 – Heater power on#define DEVICE_READY_RO         You finish  //  0x10 – Device is ready after initialization    ‐‐ RO

// LED CONTROL   (Bits 15 to 12)  ‐‐W1S#define LED_CONTROL_OFFSET 12#define LED1                       (1 << (1 LED_CONTROL_OFFSET + 0))     // 0x1 – LED 1#define LED2                       (You finish)  // 0x2 – LED 2

…..  Etc.  ……..

#endif17

Page 18: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Add My_CoffeePotFunctions.cpp with  function STUBS

STUBS built cut‐and‐pasted from “.h” fileProject should now build and run 

18

Page 19: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Lets Build MyCoffeePotFunctions.cpp in detail

This assignment we will manually cause display to update.  Later assignment we will do it via interrupts.  Last year doing the code switch  ws time consuming. This CONDITIONAL COMPILE might make things easier 19

You must call this function each time you have made changes to any of the Coffeepot Registers.

Page 20: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

If you don’t know how to do something  ‐‐ call a subroutine and fix later 

Top‐down programming ‐‐. P.P.P

20

Build a function stub

Build a function stub

If you builda function stubvoid Name(….) { }Then you can check 

that the code you have so far built while build 

and link

Page 21: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Pseudo Code details

21

Page 22: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Here is my water control code

Assignment 1 ‐‐Make your own heat control codeAlso write your own code for

22

Page 23: ASM and C C++ programing to activate a “MOCKED” micro ...people.ucalgary.ca/~smithmr/2016webs/encm511_16... · ASM and C / C++ programing to activate a ... a certain level using

Next step

• I will let you find the ASM details in the lecture notes

23