Week 5 Mail Boxes Binary Semaphores Fixed Point Arithmetic Todays Agenda Announcements Homework Mail Boxes Mail Boxes as Binary Semaphores Fixed Point Arithmetic Announcements Due date on Intensity Task homework assignment has been moved out one week to March 11 ID: 483574
Download Presentation The PPT/PDF document "Embedded Real Time Software Development" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Embedded Real Time Software Development
Week
5
Mail Boxes / Binary Semaphores
Fixed Point ArithmeticSlide2
Today’s Agenda
Announcements
Homework
Mail Boxes
Mail Boxes as Binary Semaphores
Fixed Point ArithmeticSlide3
Announcements
Due date on “Intensity Task” homework assignment has been moved out one week to March 11.
The “Watchdog Timer” homework assignment is now optional. If turned in, the student may substitute that grade for the lowest of any homework or quiz grade.
There will be a quiz on fixed point arithmetic on March 4.
The mid-term exam will be on March 25
.
Homework due today will be accepted until 11:59 PM March 19Slide4
Homework Assignment
Start with OS
Blinky
project
Adds task that monitors Serial input
‘!’ received, print “Howdy, Darn it!”
Any other character, print “Howdy”
Requires modification of
uartstdio
.[
c|h
]Slide5
Modifications to uartstdio.h
Use the buffered (interrupt driven) version of the library code
Declare the semaphore we will be using laterSlide6
Modifications to uartstdio.c
1
Change include from “
util
/…” to “…”
Declare the semaphore we will be usingSlide7
Modifications to uartstdio.c
2
Create the Semaphore when we initialize the UARTSlide8
Modifications to uartstdio.c
3
Interrupt function needs to tell the kernel that we are servicing an interrupt.Slide9
Modifications to uartstdio.c
4
Put it here, because if the interrupt function is going to throw away the character, our function doesn’t need to know it was here
Make sure that the Semaphore was created, just in case we get an interrupt before the initialization is complete.Slide10
Modifications to uartstdio.c
5
Tell the kernel we are through processing the interrupt.Slide11
Modification to startup.c
Call the task initialization function.Slide12
howdy.c 1
Define the priority and stack size
Declare the stack and the task function
the ANSI declaration is here because
init_howdy
() is the only function that will ever refer to
howdy_task
()
howdy.h
(right) just needs to declare the
init_howdy
() function.Slide13
howdy.c 2Slide14
howdy.c 3
Wait for the semaphore with a 30 second timeout
Decide what to do based on the error code
OS_ERR_NONE – We got the semaphore normallySlide15
howdy.c
4
Had a timeout or unknown result code
Comment on line 125 because a “break;” would cause an unreachable code warning and no “break;” would cause a discussion in the code review as a standards violation.Slide16
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 0Slide17
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 waitSlide18
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 Blocks
OS_EVENT *
my_mbox_ptr
;
…
my_mbox_ptr
=
OSMboxCreate
((void *)0);
…Slide19
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]);
…
}Slide20
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
;
…
}
…Slide21
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
;
…
}
…Slide22
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.Slide23
BreakSlide24
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 arithmeticSlide25
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/16
th
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:
Slide26
Whoa! What?
Assuming Q4.3 notation (scale = 16)….
Decimal 4.0 = 01000000 = 0x40… how?
Decimal 1.62
= 25.9, (25)=00011001
converting back
=1.56
Decimal -2.13
=-34.08, (-34)=11011110
=-2.125
Note the precision is
Slide27
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
Slide28
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!
Slide29
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.
Slide30
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
”Slide31
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
);Slide32
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))Slide33
You have to design before writing code
Working with fixed point numbers you have to think about
Range
Precision
Order of evaluationSlide34
Range and Precision Example
Variable to hold distance in meters.
Assume Q10.21 (_iq10).
Range: >1300 miles
Resolution: ~0.1 inchesSlide35
Order of Evaluation (Range)
Assume C28.3 (_iq28)
Compute 2 * 4 / 5 = 1.6
2 = 0x2000 0000
4 = 0x4000 0000
5 = 0x5000 0000
2 * 4 results in 0x8000 0000, which is -1, OVERFLOW
4/5 results in 0xCCC CCCC, which is 0.79999…
2 * 0xCCC CCCC results in, 0x1999 9998, which is 1.59999…Slide36
Order of Evaluation (Precision)
Assume C4.27 (_iq4)
Compute (0.0625 / 64) * 1000 = 0.9765625
0.0625 = 0x0000 0001
64 = 0x0000 0400
1000 = 0x0000 0010
0.0625 / 64 = 0x0000 0000, UNDERFLOW
1000 / 64 = 0x0000 00FA
0.0625 * 0x0000 00FA = 0x0000 000F = 0.9375Slide37
Homework Assignment
Start with last week’s assignment.
Change blink rate of yellow led in proportion to the temperature reading. 68F (or less) blink at 2 Hz, 90F (or more) blink at 20 Hz.
Change messages from “Howdy” and “Howdy darn it!” to “Temp=??” and “Humidity=??” respectively, where ?? is the actual temperature or humidity.
Measure temperature and humidity once per second.Slide38
Homework StructureSlide39
I2C Port Setup
//
The I2C3 peripheral must be enabled before use.
ROM_SysCtlPeripheralEnable
(SYSCTL_PERIPH_I2C3);
ROM_SysCtlPeripheralEnable
(SYSCTL_PERIPH_GPIOD);
// Configure the pin
muxing
for I2C3 functions on port D0 and D1.
ROM_GPIOPinConfigure
(GPIO_PD0_I2C3SCL);
ROM_GPIOPinConfigure
(GPIO_PD1_I2C3SDA);
// Select the I2C function for these pins. This function will also
// configure the GPIO pins
pins
for I2C operation, setting them to
// open-drain operation with weak pull-ups.
GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
// Select the clock speed for the I2C bus to be 400kHz
ROM_I2CMasterInitExpClk(I2C3_BASE,SysCtlClockGet(),true);Slide40
I2C Port Commands
See SW-TM4C-DRL-UG-1.1.pdf
TivaWare
Peripheral Driver Library,
Section 13
Create and I2C interrupt function
Interrupts are generated when I2CMasterControl completes
Use post/pend to suspend task while I2C transfer is
occuring