Upload
mariah-sanders
View
216
Download
0
Embed Size (px)
Citation preview
Developing a bicycle speed-o-meter
Midterm Review
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada2
General Project concept
Magnetic Sensor Signal
High speed clock signal
BlackfinProgrammableFlag (PF) Input
MotorolaParallel
Interface Timer (PIT)
Input
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada3
Main function concept ulong DetermineSpeed(ulong wheelDiameter, ulong clockFrequency)
#define ulong unsigned long int;
extern “C” ulong CountClockASM(const int); // Assembly code interfaceextern “C” ulong CalculateSpeedASM(ulong, ulong, ulong);extern “C” void SetupInterface(void);
ulong DetermineSpeed(ulong wheelDiameter, ulong clockFrequency) { // Get to known position on magnetic sensor signal unsigned long discard_count; unsigned long count_high, count_low; SetupInterface( ); discard_count = CountClockASM(while_MagneticSensorHigh); // Not ready yet discard_count = CountClockASM(while_MagneticSensorLow); // Not ready yet count_high = CountClockASM(while_MagneticSensorHigh); count_low = CountClockASM(while_MagneticSensorLow); return CalculateSpeedASM(count_high + count_low, wheelDiameter, clockFrequency);}
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada4
How many clock ticks pass while magnetic sensor value stays at certain level?
extern “C” ulong CountClockASM(const int); // Assembly code interfaceextern “C” void SetupInterface(void);extern “C” ulong CalculateSpeedASM(ulong, ulong, ulong);
ulong CountClockASM(const int high_low) { // Either a high magnetic sensor value (1) // or low sensor value (0) needs measuring ulong clock_count = 0; while (magnetic_sensor = = high_low) { // if signal is unchanged from start // Must count just one clock signal low-to-high transition while (clock_signal = = high) /* wait */; while (clock_signal = = low) /* wait */; // Changes on low-to-high edge clock_count++; } return clock_count;} // Must assume that clock-signal is MUCH faster than sensor signal
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada5
What we need to know
How can you pass parameters between “C/C++” and assembly code functions?
How can you return a parameter from assembly code functions?
What registers are available on the processor? How do you set up the interface between the processor and the
real world Many signals are coming into processor, how do you separate
(mask off) the signals you want from those you don’t? What are the basic operations for accessing memory? What are the basic ALU operations for this processor?
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada6
Programming model
Blackfin
Data Registers R0, R1 …. R7
Address Registers
Pointer Registers P0, P1 … P5
Frame Buffer FP
Stack Pointer SP
Special DSP I0-I3, B0-B3, M0-M3, L0-L3
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada7
Syntax examples
BlackfinRegister to Register Move
reg1 reg 232 bit operations
R1 = R2;(1 cycle @ 500 MHz)
Register to Register Move reg1 reg 2
16 bit operations
R1.L = R2.L; and also
R1.H = R2.L;
Register to Register Move reg1 reg 2
8 bit operations
Here is some new syntaxthat convert 8-bit values into 32 bit values
R1 = R2.B (X);R1.L = R2.B (Z);
R1 = R2.B(X);
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada8
Syntax examples
BlackfinRegister to Register ADD
reg1 reg 2 + reg332 bit operations
R1 = R2 + R3; (1 cycle @ 500 MHz)
Register to Register Move reg1 reg 2 + reg3
16 bit operations
R1.L = R2.L + R3.L (NS);
(1 cycle @ 500 MHz)
Also R1 = R2 +|- R3;
Means R1.L = R2.L – R3.L; and R1.H = R2.H + R3.H;
Register to Register Move reg1 reg 2 + reg3
8 bit operations
R1.L = R2.B (X);R0.L = R3.B (X);R1.L = R1.L + R0.L (NS);
( > 3 cycles @ 500 MHz)
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada9
32-bit Memory Move operations
Blackfin
Value at memory location 1 placed at memory location 2
P0.H = hi(_MEM1); P0.L = lo(_MEM1);
P1.H = hi(_MEM2); P1.L = lo(_MEM2); R0 = [P0];
[P1] = R0; > 6 cycles
Multiple moves P0.H = hi(_MEM1); P0.L = lo(_MEM1);
P1.H = hi(_MEM2); P1.L = lo(_MEM2); R0 = [P0++];
[P1++] = R0;
R0 = [P0++];
[P1++] = R0;
R0 = [P0++];
[P1++] = R0;
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada10
16-bit Memory Move operations
Blackfin
Value at memory location 1 placed at memory location 2
P0.H = hi(_MEM1); P0.L = lo(_MEM1);
P1.H = hi(_MEM2); P1.L = lo(_MEM2); R0 =W [P0];
W[P1] = R0; > 6 cycles
Multiple moves P0.H = hi(_MEM1); P0.L = lo(_MEM1);
P1.H = hi(_MEM2); P1.L = lo(_MEM2); R0 = W[P0++] (X);
W [P1++] = R0;
R0 = W[P0++] (X);
W[P1++] = R0;
R0 =W [P0++] (X);
W [P1++] = R0;
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada11
Memory Move operations
Blackfin
Multiple moves of registers
[- - SP] = (R7:5, P5:3);
Combination of memory moves and ALU operations
Can also do parallel read and write operations together with math operations
Syntax looks like this
R1 = R2 | | R3 = [P0++] | | [I1++] = R4;
Not all operations can be made parallel
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada12
If – then – else constructsSigned tests
BlackfinSet the condition code register
CC = D1 == D0;CC = D1 < D0;CC = D1 <= D0;
Conditional jumpIF CC JUMP NEXT_INSTR
IF !CC JUMP NEXT_INSTR
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada13
If – then – else constructsUn-signed tests
BlackfinSet the condition code register
CC = D1 == D0 (IU);CC = D1 < D0 (IU);CC = D1 <= D0 (IU);
Conditional jumpIF CC JUMP NEXT_INSTR
IF !CC JUMP NEXT_INSTR
R0 = (signed int)1, R2= (unsigned int) 1 R1 = (signed int) -1, R3 = (unsigned int) 0xFFFFFFFFCC = R1 < R0 (true); CC = R3 < R2 (IU) (false)even though R1 == R3 and R0 == R2
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada14
Example if-then-else code
C++ example Blackfin
IF (A > B) C = D;
ELSE C = E;
Set A, B, C, D, Eto registers R0, R1, .. R4
CC = R1 < R0; IF !CC JUMP ELSE; R2 = R3; JUMP END_IF;ELSE: R2 = R4;END_IF:
CC = R1 < R0; IF CC R2 = R3; IF !CC R2 = R4;
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada15
Example loop code -- software loop
C++ example Blackfinsum = 0;
for (loop = 0; loop < 6; loop++) sum = sum + loop;
Set R0 = sum , R1 = loop
R0 = 0; R1 = 0;
R2 = 6;
LOOP: CC = R2 <= R1; IF !CC JUMP PAST_LOOP; R0 = R0 + R1; R1 += 1;
JUMP LOOP
PAST_LOOP:
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada16
Example loop code -- Hardware loop
C++ example Blackfinsum = 0;
for (loop = 0; loop < 6; loop++) sum = sum + loop;
Set R0 = sum , R1 = loop
R0 = 0; R1 = 0;
P1 = 6;
LSETUP(LSTART, LEND) LC1 = P1;
LSTART: R0 = R0 + R1;LEND: R1 += 1;
PAST_LOOP: OUTSIDE THE LOOP
Has a capability of 2 hardware (high-speed, zero overhead) loop
NO SUBROUTINE CALLS ALLOWED
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada17
Reminder of what we were trying to do General Project concept
Magnetic Sensor Signal
High speed clock signal
BlackfinProgrammableFlag (PF) Input
MotorolaParallel
Interface Timer (PIT)
Input
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada18
Required Assembly Language
extern “C” ulong CountClockASM(const int); // Assembly code interfaceextern “C” void SetupInterface(void);extern “C” ulong CalculateSpeedASM(ulong, ulong, ulong);
ulong CountClockASM(const int high_low) { ulong clock_count = 0; while (magnetic_sensor = = high_low) { // if signal is unchanged from start // Must count just one clock signal low-to-high transition while (clock_signal = = high) /* wait */; while (clock_signal = = low) /* wait */; // Changes on low-to-high edge clock_count++; } return clock_count;}
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada19
Hardware test – wait while magnetic signal is “high”
C++ BlackfinMagnetic Signal Bit 10 of FIO_FLAG_D
register of PF interface
while (mag_signal == HIGH)
/* wait */ ;
#define MASK 0x400
P0.H = hi(FIO_FLAG_D);
P0.L = lo(FIO_FLAG_D); R1 = MASK;
WHILE:
R0 = W[P0] (Z); R0 = R0 & R1; CC = R0 == R1; IF CC JUMP WHILE (BP);
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada20
Required BF533 Assembly Language
ulong CountClockASM(const int high_low) { ulong clock_count = 0; while (magnetic_sensor = = high_low) { // if signal is unchanged from start // Must count just one clock signal // low-to-high transition while (clock_signal = = high) /* wait */; while (clock_signal = = low) /* wait */; // Changes on low-to-high edge clock_count++; } return clock_count;}
_CountClockASM: R0 contains the INPAR R1 = 0; P0.H = hi(FIO_FLAG_D); P0.L = lo(FIO_FLAG_D);WHILE: R2 = W[P0] (Z); R3 = MASKMAG; // SLOW Magnetic Signal on bit 10 R2 = R2 & R3; CC = R2 == R0; IF !CC JUMP ENDWHILE;
R3 = MASKCLK; // FAST Clock Signal on bit 11
HIGH: R2 = W[P0] (Z); R2 = R2 & R3; CC = R2 == R3 IF CC JUMP HIGH (BP);
LOW : R2 = W[P0] (Z); R2 = R2 & R3; CC = R2 < R3 IF CC JUMP LOW (BP);
R1 += 1; JUMP WHILE
END_WHILE: R0 = R1; RTS
04/19/23 Timer Control -- Lab.3, Copyright M. Smith, ECE, University of Calgary, Canada
21
Jump predicted taken (bp) -- Example
IF CC JUMP WHILE [--SP] = (R7:0);
// Save to stackWHILE:
R0 = R1; R2 = R3;CC = R1 < R4;IF CC JUMP WHILE; R6 = R7; R5 = R4; R6 = R7; R5 = R4;
R6 = R7; R5 = R4;
IF CC JUMP WHILE [--SP] = (R7:0);
// Save to stackWHILE:
R0 = R1; R2 = R3;CC = R1 < R4;
IF CC JUMP WHILE (BP); R6 = R7; R5 = R4; R6 = R7; R5 = R4;
R6 = R7; R5 = R4;
04/19/23 Timer Control -- Lab.3, Copyright M. Smith, ECE, University of Calgary, Canada
22
Processor fetches
instructionS before execution
04/19/23 Timer Control -- Lab.3, Copyright M. Smith, ECE, University of Calgary, Canada
23
Processor fetches 9 new instructions before execution of 1st instruction is complete
UP to 10 instructionsbeing executed in parallel
04/19/23 Timer Control -- Lab.3, Copyright M. Smith, ECE, University of Calgary, Canada
24
Jump predicted taken (bp)
IF CC JUMP WHILE[--SP] = (R7:0);
// Save to stackWHILE:
R0 = R1; R2 = R3;CC = R1 < R4;IF CC JUMP WHILE; R6 = R7; THESE R5 = R4; ARE R6 = R7; FETCHED
R5 = R4; GOOD SPEED
R6 = R7; IF NO JUMP R5 = R4;
IF CC JUMP WHILE[--SP] = (R7:0);
// Save to stackWHILE:
R0 = R1; THESE R2 = R3; FETCHEDCC = R1 < R4;
IF CC JUMP WHILE (bp); R6 = R7; R5 = R4; R6 = R7; R5 = R4;
R6 = R7; R5 = R4;
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada25
Subroutine / Function calls
C++ Blackfin
extern “C” int FooASM(int, int, int)
C = FooASM(1,2,3)
.extern _FooASM
LINK 20;
[SP + 16] = R4;
R0 = 1;
R1 = 2;
R2 = 3;
CALL _FooASM;
R4 = R0;
.. Other code
R4 = [SP + 16];
UNLINK;
RTS;
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada26
Code conventions for subroutines / functions
Blackfin -- VisualDSP
Volatile registersDestroyed after CALL
R0, R1, R2, R3P0, P1, P2
Non-volatile registersMUST BE UNCHANGED after CALL to a subroutine
R4, R5, R6, R7
P3, P4, P5, FP, SP
Subroutine return value is passed in
R0
Subroutine OUTPARS OUTPAR1 R0
OUTPAR2 R1
OUTPAR3 R2
OUTPAR4 [SP + 12]
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada27
Main function concept ulong DetermineSpeed(ulong wheelDiameter, ulong clockFrequency)
#define ulong unsigned long int;
extern “C” ulong CountClockASM(const int); // Assembly code interfaceextern “C” ulong CalculateSpeedASM(ulong, ulong, ulong);extern “C” void SetupInterface(void);
ulong DetermineSpeed(ulong wheelDiameter, ulong clockFrequency) { // Get to known position on magnetic sensor signal unsigned long discard_count; unsigned long count_high, count_low; SetupInterface( ); discard_count = CountClockASM(while_MagneticSensorHigh); discard_count = CountClockASM(while_MagneticSensorLow); count_high = CountClockASM(while_MagneticSensorHigh); count_low = CountClockASM(while_MagneticSensorLow); return CalculateSpeedASM(count_high + count_low, wheelDiameter, clockFrequency);}
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada28
Main Blackfin function conceptulong DetermineSpeed(ulong wheelDiameter, ulong clockFrequency) { // Get to known position on magnetic sensor signal unsigned long discard_count; unsigned long count_high, count_low; SetupInterface( ); discard_count = CountClockASM(while_MagneticSensorHigh); discard_count = CountClockASM(while_MagneticSensorLow); count_high = CountClockASM(while_MagneticSensorHigh); count_low = CountClockASM(while_MagneticSensorLow); return CalculateSpeedASM(count_high + count_low, wheelDiameter, clockFrequency);}
_DetermineSpeed: LINK 20 [FP + 8] = R0; [FP + 12] = R1; // Save INPARS [SP + 16] = R4; CALL _SetupInterface;
R0 = MASKMAG; CALL _CountClockASM; R0 = 0; CALL _CountClockASM
R0 = MASKMAG; CALL _CountClockASM; R4 = R0; // count_high
R0 = 0; CALL _CountClockASM
R0 = R0 + R4; // high + low. R1 = [FP + 8]; // old INPAR1 R2 = [FP + 12]; // old INPAR2 CALL _CalculateSpeedASM // Return in R0 [SP + 16] = R4; UNLINK RTS
04/19/23
Motorola Blackfin Comparison Part 2 , Copyright M. Smith, ECE, University of Calgary,
Canada29
extern “C” void SetupInterface(void);
Blackfin
Direction register
0 = input, 1 = output
FIO_DIR 16-bits
Interrupt control FIO_MASKA_D on any bit
Enable input(Power save issue)
FIO_INEN on any bit
Polarity FIO_POLAR on any bit
Edge / Level sensitivity FIO_EDGE FIO_BOTH on any bit
Magnetic signalClock signal
Bit 10 MASKMAG 0x200Bit 11 MASKCLK 0x400
04/19/23
Motorola Blackfin Comparison Part 2 , Copyright M. Smith, ECE, University of Calgary,
Canada30
extern “C” void SetupInterface(void);SPECIFICS
Blackfin
Magnetic signalClock signal
Bit 10 MASKMAG 0x200Bit 11 MASKCLK 0x400
Direction register
0 = input, 1 = output
R0 FIO_DIR MASK OFF BITS 10, 11 FIO_DIR R0
Interrupt control R0 FIO_MASKA_D MASK OFF BITS 10, 11 FIO_MASKA_D R0
Enable input(Power save issue)
R0 FIO_INEN OR BITS 10, 11 FIO_INEN R0
Polarity R0 FIO_POLAR MASK OFF BITS 10, 11 FIO_POLAR R0
Edge / Level sensitivity FIO_EDGE FIO_BOTH MASK OFF BITS 10, 11
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada31
In class exercise – Write Blackfin code for extern “C” void SetupInterface(void);
04/19/23
Motorola Blackfin Comparison Part 1 , Copyright M. Smith, ECE, University of Calgary,
Canada32
Information taken from Analog Devices On-line Manuals with permission http://www.analog.com/processors/resources/technicalLibrary/manuals/
Information furnished by Analog Devices is believed to be accurate and reliable. However, Analog Devices assumes no responsibility for its use or for any infringement of any patent other rights of any third party which may result from its use. No license is granted by implication or otherwise under any patent or patent right of Analog Devices. Copyright Analog Devices, Inc. All rights reserved.