Upload
emma-farmer
View
233
Download
0
Embed Size (px)
Citation preview
ENTC-489 Embedded Real Time Software Development
Embedded Real Time Software Development
Week 5Mail Boxes / Binary Semaphores
Fixed Point Arithmetic
ENTC-489 Embedded Real Time Software Development
Today’s Agenda
• Homework• Mail Boxes• Mail Boxes as Binary Semaphores• Fixed Point Arithmetic
ENTC-489 Embedded Real Time Software Development
Mail Boxes
• Mechanism to send a single value (usually pointer sized) to a task
• Receiving task can pend on the posting of a value
• Can not post 0
ENTC-489 Embedded Real Time Software Development
Mailbox Functions
• OSMboxCreate()– Creates a new mailbox
• OSMboxPost()– Posts a value to a mailbox
• OSMboxPend()– Waits on a value to be placed in a mailbox, then
empties the mailbox• OSMboxAccept()– Same as Pend, but does not wait
ENTC-489 Embedded Real Time Software Development
OSMboxCreate()
• OS_EVENT *OSMboxCreate(void *msg)– msg is the initial value to go in the box, usually 0– Returns a pointer to the created mailbox or 0 if
there are no more available Event Control BlocksOS_EVENT *my_mbox_ptr;…my_mbox_ptr = OSMboxCreate((void *)0);…
ENTC-489 Embedded Real Time Software Development
OSMboxPost()• INT8U OSMboxPost(OS_EVENT *pevent, void *msg);
– *pevent – pointer to the mailbox TCB– *msg – value to be posted, 0 doesn’t post anything– returns the following error codes:
OS_ERR_NONE, OS_ERR_FULL, OS_MBOX_FULL, OS_ERR_EVENT_TYPE, OS_ERR_PEVENT_NULL, OS_ERR_POST_NULL_PTR
OS_EVENT *my_mbox_ptr;uint8_t recvd_data[100]…if (index == new_index){ … err = OSMboxPost(my_mbox_ptr,(void *)&recvd_data[index]); …}
ENTC-489 Embedded Real Time Software Development
OSMboxPend()• void *OSMboxPend(OS_EVENT *pevent, INT16U timeout, INT8U *err);
– *pevent – Pointer to the mailbox TCB– timeout – Max time, in ticks, to pend. 0 waits forever– *err – Pointer to location that will hold the return code from the function
OS_ERR_NONE, OS_ERR_TIMEOUT, OS_ERR_EVENT_TYPE, OS_ERR_PEND_ISR, OS_ERR_PEVENT_NULL
– Returns the value in the mailbox, (void *)0, if the mailbox is empty
OS_EVENT *my_mbox_ptr;UINT8 error_code;uint16_t *next_value;…next_value = OSMboxPend(my_mbox_ptr,0,&error_code);switch (error_code){case OS_ERR_NONE: return *next_value;…}…
ENTC-489 Embedded Real Time Software Development
OSMboxAccept()
• void *OSMboxAccept(OS_EVENT *pevent)– *pevent – Pointer to the mailbox TCB– Returns the value in the mailbox, (void *)0, if the mailbox is empty
…OS_EVENT *my_mbox_ptr;uint16_t *next_value;…next_value = OSMboxAccept(my_mbox_ptr);if (next_value != 0){ … return *next_value; …}…
ENTC-489 Embedded Real Time Software Development
Mail Boxes as Binary Semaphores
• You do not have to post a pointer, you can post any variable or constant that is the same size or smaller than a pointer.
• If you “Post” a constant, the behavior of a mailbox is exactly the same as a binary semaphore would be.
ENTC-489 Embedded Real Time Software Development
Fixed Point Arithmetic
• The Problem– Floating point math is slower than integer math
(even with a Floating Point Unit)– Use of floating point math slows multi-threaded /
multi-tasked systems even more (lots of BIG registers to push to save context)
– We still need to deal with fractional values• The Solution– Fixed point arithmetic
ENTC-489 Embedded Real Time Software Development
What is a Fixed Point Number?
• The meaning of an N-bit binary word depends entirely on its interpretation. For example, if I have the 8 bit binary value 01000011– Traditionally it could be interpreted as
Hex 43, Octal 103, Decimal 67, ASCII ‘C’– But what if I decided to interpret the value as 1/16th of the
signed, binary count. We the value could then be interpreted as:Binary 0100.0011, Hex 4.3, Octal 4.14, Decimal 4.1875
– The notation describing the interpretation of this number is Q4.3, meaning 3 integer bits and 4 fractional bits (the high order bit is the sign).
– We say that the “Scale” of our numbers is:
ENTC-489 Embedded Real Time Software Development
Whoa! What?
• Assuming Q4.3 notation (scale = 16)….– Decimal 4.0 = 01000000 = 0x40… how?
– Decimal 1.62= 25.9, (25)=00011001converting back =1.56
– Decimal -2.13=-34.08, (-34)=11011110=-2.125
• Note the precision is
ENTC-489 Embedded Real Time Software Development
Huh?
• Let’s think about this in decimal…• I tell you that every number I give you is 100 times
the value I “mean” (scale=) then if I say:– 25, you interpret 0.25– 314, you interpret 3.14– -7, you interpret – 0.07– 3,583,771, you interpret 35,837.71
ENTC-489 Embedded Real Time Software Development
Why is this better, I understand floating point?
• You only think you understand floating point• The math is faster than floating point because you
can use integer arithmetic for everything– Addition and subtraction just work… using a scale of :
7.16-3.05=4.11 and 716-305=411– Multiplication and Division require us to think about
the scale:3.71*0.4=1.484,
– If our Scale is an even power of two the division above can be accomplished with shifts, which are cheap!
ENTC-489 Embedded Real Time Software Development
Facts to Ponder• Word size: • Range of values: • Resolution of values: for • Accuracy of values: for • Addition and subtraction require both operands to be the same scale• The result for addition and subtraction will always be the same scale• Multiplication and division the scale of the operands may be different• Multiplication, the scale of the result is the scale of the first operand
multiplied by the scale of the second operand• Division, the scale of the result is the scale of the dividend divided by the
scale of the divisor• “Saturation” is when a Fixed point number reaches the highest or lowest
value in its range.
ENTC-489 Embedded Real Time Software Development
Tivaware IQMath Library• Based on 32 bit values• Supports Q30.1 to Q1.30
– Type for Q30.1 is _iq30– Type for Q29.2 is _iq29– …– Type for Q1.30 is _iq1
• Conversion– To String– To _iq?? From _iq??– Do not use floating point
conversions
• Multiplication / Division• Trig, Sqrt, Exponents, etc.• #include “IQmath/IQmathlib.h”
ENTC-489 Embedded Real Time Software Development
Tivaware IQ Functions• Division - _IQNdiv()
_iq16 numerator, denominator, result;…result = _IQ16div(numerator,denominator);
• Multiplication - _IQNmpy()_iq16 mpy1, mpy2, result;…result = _IQ16mpy(mpy1,mpy2);
• Addition and subtraction_iq16 op1, op2, op3, result;…result = (op1 + op2) – op3;
• Output Format_iq16 op1;char outstr[16];…_IQ16toa(op1,”%2.05f”,outstr);
• Convert from IQn to IQm#define IQtoIQ(Q1,Q2,A) ((_iq ##Q1)(A) >> (Q1 – Q2))_iq16 val1;_iq24 val2;…val1 = IQtoIQ(16,24,val2);
ENTC-489 Embedded Real Time Software Development
Creating a Constant
• π as Q24.7– Range is -8 to 7.999999996– Scale is 16,777,216– π = 3.141592653 * Scale =52707178 (calculator)– #define Q24_SCALE ((int64_t)16777216)– #define PI_9DIGITS ((int64_t)3141592653)– #define TEN_EXP9 ((int64_t)1000000000)– #define PI_Q24 ((_iq24)((PI_9DIGITS*Q24_SCALE)/TEN_EXP9))