420 likes | 557 Views
ELN5622 Embedded Systems Class 3 Spring, 2003. Kent Orthner korthner@hotmail.com. Assembly Language Programming. Programming Conventions. Conventions: Rules that a designer follows to make his/her program easier to understand, communicate to other, debug, and less prone to mistakes.
E N D
ELN5622Embedded SystemsClass 3Spring, 2003 Kent Orthnerkorthner@hotmail.com
Programming Conventions • Conventions: • Rules that a designer follows to make his/her program easier to understand, communicate to other, debug, and less prone to mistakes. • Includes: • Comment conventions • Naming conventions • Drawing Conventions
Assembly Language Style • Program Header • What the program does • Author’s Name • File Name • Date • Version • History • Function Header • What the function does/How it works • Author’s Name • Date • History
Assembly Language Style • Equates vs. In-line constants • Use constants wherever possible • Easier to understand, • Easier to fix • Easier to maintain • Location of Equate Statements • All together • Point-of-use
Assembly Language Style • Types of Equates • System Equates • System Functions • Location of I/O Registers • Port Addresses • Constant Equates • CR/LF, End of String, NIL • Memory Map Equates • Program Location • Flash Memory Location • Stack Pointer Location
Assembly Language Style • Constant Data Definitions • Tables, Strings, Etc. • Located in ROM • Often best to have at the end of the program to lessen the chance of them executed • Variable Data Definitions • System-wide Variables • Located in RAM • Non-volatile ROM Data • Database for user settings • Maintained when power is shut off
Assembly Language Style • Indenting • Not commonly used for assembly programs • Can ease understanding by making program flow more obvious. • Up to the individual designer. • Use ‘Boilerplate’ files • All files use the same flow • Saves Typing • Less potential Mistakes
Assembly Language Style • Commenting Style • Headers for functional blocks • Headers per line • Goal: To make it so the person maintaining the code can understand the program.
Assembly Language Style • Naming Conventions • Very important to prevent errors and make code easy to understand. • Examples: LDX SP LDB X,p_operand Stack variable LDA g_timer Global variable JSR s_timercheck Subroutine LDB X,Operand Stack variable LDA G_TimerVar Global variable JSR TIMERCHECK Subroutine
Assembly Language Style • Pseudocode • Self-commenting, less prone to error • Makes it natural to • design first with pseudo code • Implement second with assembly language * Get Temp * If Temp > MaxAllowed * Turn valve off * Else * turn the valve on * End if
Assembly Language Style • Pseudocode * Get Temp LDAA TEMP_PORT * If Temp > MaxAllowed IF_TEMPMAX: CMPA MaxAllowed A-MaxAllowed BLE EL_TEMPMAX Branch if <= 0 * Turn valve off LDAA ValveOff STAA CONTROL_PORT BRA EI_TEMPMAX * Else turn the valve on EL_TEMPMAX: LDAA ValveOn STAA CONTROL_PORT * End if EI_TEMPMAX: <Next Inst>
Assembly Language Style • While-Do Loop * While Temperatue > MaxAllowed WH_MAXALL: CMPA MaxAllowed A-MaxAllowed BLS EW_MAXALL Branch if < 0 * Do * End While BRA WH_MAXALL EW_MAXALL: <Next Inst>
Assembly Language Style • Repeat-Until Loop * Repeat RP_SWSTATE: * Until SwitchState = END_STATE LDAA SwitchState CMPA END_STATE BNE RP_SWSTATE <Next Inst>
Assembly Language Style • For Loop * For (I=COUNT, I--, I==0) LDAA COUNT * Begin FOR_LOOP: PUSH A . . . * Next PULA DECA BNE FOR_LOOP <Next Inst>
Embedded System Characteristics • A computing system embedded within a device. • A system intended for a single purpose, which includes a general purpose processor. • Often used for • providing user control over a product • to observe or control something in the “real world” (i.e. analog)
Embedded System Startup • What is the first thing the user expects when an embedded system starts up? • Begin application execution. • When should the software in an embedded system finish? • It shouldn't. It should (normally) run until the power is turned off.
Embedded Program Flow • Environment setup • System Equates • Constant Equates • Memory Map Equates • Initialization • Set stack pointer • Special Register Setup • Initialize Tasks • Enable Interrupts • Main Loop • Execute Applications
Embedded Program Flow Initialize Stack Pointer Special Register Setup Initialize Task 1 Initialize Task 2 Initialize Task 3 Enable Interrupts. ( Go! ) Execute Task 1 Execute Task 2 Execute Task 3
Environment Setup • Types of Equates • System Equates • System Functions • Location of I/O Registers • Port Addresses • Constant Equates • CR/LF, End of String, NIL • Memory Map Equates • Program Location • Flash Memory Location • Stack Pointer Location
Execution Start • 68HC11 jumps to the 'Reset Vector' located at 0xFFFE, 0xFFFF PC <- M(0xFFFE, 0xFFFF)
Initialization • Initialize Stack Pointer • Interrupts • Functions • Temporary Storage • Special Register Setup • Memory Mapping registers • Determine the location of RAM & the register block within the memory map. • Can only be written at Startup
Initialization • Special Register Setup (Continued) • System Configuration Registers • A/D Powerup • Clock Select • IRQE Edge-Sensitive Select • Clock Monitor Enable • COP Timer Rate • I/O Control Registers • Timer Registers • Interrupt Mask Registers • SCI & SPI Registers • ADC/DAC Control Registers
Initialization • Task Initializatoin • Set initial state for state machines • Set initial values for task variables • Pre-compute tables where necessary • Clear / pre-set buffers
Main Loop Architecture • 3 buttons need to be checked at least 10 times a second: repeat for (i=2; i--; i==0) Checkbutton(i); end for until ( false )
Main Loop Architecture • 3 buttons need to be checked at least 10 times a second, and 3 pins must be set 5 times a second. repeat for (i=2; i--; i==0) CheckButton(i); end for if (FifthSecondIsUp) for (i=2; i<3; i--) SetPin(i); end for end if until ( false )
Main Loop Architecture • 3 buttons need to be checked at least 10 times a second, and 3 pins must be set 5 times a second. repeat for (i=2; i--; i==0) CheckButton(i); end for if (FifthSecondIsUp) for (i=2; i<3; i--) SetPin(i); end for end if until ( false )
Main Loop Architecture • And if there are a couple more things to be done, all at different times … repeat CheckButtons() SetPins() ControlMotors() SetDisplay() until ( false )
Cyclical Executive • Architecture Repeat Task1 () Task2 () Task3 () Until ( false )
Cyclical Executive • Architecture * Repeat MAINLOOP: * Task1 () JSR TASK1 * Task2 () JSR TASK2 * Task3 () JSR TASK3 * Until ( false ) JMP MAINLOOP
Cyclical Executive • Adequate for simple applications. • Other Names: • Round Robin Executive • Round Robin Kernel • Super Loop • Rules: • Tasks may not employ busy waiting • Tasks must do their work quickly and return to the main loop so that other tasks can run • Tasks must save their place by using a state variable
Cyclical Executive • Advantages: • Small • Compact • Easy to use • Easy to understand • Disadvantages • No priorities • Polls for events • Timing responsibility is put on the programmer
Parallel I/O • Three Functions per pins: • Output (1 or 0) • Wired-or output (1 or 0) • Input • Up to 40 I/O pins. • Each pin can be used as I/O or another function. • Some pins are fixed-direction, some are bidirectional.
Parallel I/O • Port A • Shared with Timer & Pulse Accumulator • PA7: Bidir • PA6-PA3: Output Only • PA2-PA0 : Input Only • Port B • Shared as Expanded bus Address pins • Output Only • Port C • Shared as Expanded bus Data / Multiplexed Address pin. • Bidirectional
Parallel I/O Registers • PORTn • 8-bit register for each port • Reads return the level of the pin itself for input/bidirectional pins, or the state of the logic inside the output buffer at the pin output. • Writes cause the data to be latched, so it can be used for output operation. (not input pins.) • PORTCL • Special Port C register for handshake writes.
Parallel I/O Registers • DDRn • Register for each bidirectional pin. • Controls the direction of data flow. • 0 = Input • 1 = Output • Subsystem function overrides this pin function • Ie. SCI Tx/Rx • Control Registers • Determine if the pin is going to be used as General Purpose I/O, or as a subsystem pin. • Determines if an output will be a wired-or (open collector) output.
Handshaking • Ports B&C, with STRA input & STRB output • 3 modes: • Simple Strobe (default) • Port B = Simple Strobe Output • Port C = Simple Strobe Input • Full-input Handshake • Full-output Handshake • Configured with PIOC register
Handshaking - Simple Mode • Port B: Output with STAB • Port C: Input with STAA
Handshaking - Full-Input Handshake • Port C: Input with both STAA & STAB
Handshaking - Full-Output Handshake • Port C: Output with both STAA & STAB