Download presentation
Presentation is loading. Please wait.
Published byMay Lee Pierce Modified over 9 years ago
1
ENTC-489 Embedded Real Time Software Development Embedded Real Time Software Development Week 5 Mail Boxes / Binary Semaphores Fixed Point Arithmetic
2
ENTC-489 Embedded Real Time Software Development Today’s Agenda Announcements Homework Mail Boxes Mail Boxes as Binary Semaphores Fixed Point Arithmetic
3
ENTC-489 Embedded Real Time Software Development 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 19
4
ENTC-489 Embedded Real Time Software Development 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]
5
ENTC-489 Embedded Real Time Software Development Modifications to uartstdio.h Use the buffered (interrupt driven) version of the library code Declare the semaphore we will be using later
6
ENTC-489 Embedded Real Time Software Development Modifications to uartstdio.c 1 Change include from “util/…” to “…” Declare the semaphore we will be using
7
ENTC-489 Embedded Real Time Software Development Modifications to uartstdio.c 2 Create the Semaphore when we initialize the UART
8
ENTC-489 Embedded Real Time Software Development Modifications to uartstdio.c 3 Interrupt function needs to tell the kernel that we are servicing an interrupt.
9
ENTC-489 Embedded Real Time Software Development 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.
10
ENTC-489 Embedded Real Time Software Development Modifications to uartstdio.c 5 Tell the kernel we are through processing the interrupt.
11
ENTC-489 Embedded Real Time Software Development Modification to startup.c Call the task initialization function.
12
ENTC-489 Embedded Real Time Software Development 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.
13
ENTC-489 Embedded Real Time Software Development howdy.c 2
14
ENTC-489 Embedded Real Time Software Development 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 normally
15
ENTC-489 Embedded Real Time Software Development 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.
16
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
17
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
18
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 Blocks OS_EVENT *my_mbox_ptr; … my_mbox_ptr = OSMboxCreate((void *)0); …
19
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]); … }
20
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; … } …
21
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; … } …
22
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.
23
ENTC-489 Embedded Real Time Software Development Break
24
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
25
ENTC-489 Embedded Real Time Software Development What is a Fixed Point Number?
26
ENTC-489 Embedded Real Time Software Development Whoa! What?
27
ENTC-489 Embedded Real Time Software Development Huh?
28
ENTC-489 Embedded Real Time Software Development Why is this better, I understand floating point?
29
ENTC-489 Embedded Real Time Software Development Facts to Ponder
30
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”
31
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);
32
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))
33
ENTC-489 Embedded Real Time Software Development You have to design before writing code Working with fixed point numbers you have to think about – Range – Precision – Order of evaluation
34
ENTC-489 Embedded Real Time Software Development Range and Precision Example Variable to hold distance in meters. Assume Q10.21 (_iq10). – Range: >1300 miles – Resolution: ~0.1 inches
35
ENTC-489 Embedded Real Time Software Development 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…
36
ENTC-489 Embedded Real Time Software Development 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.9375
37
ENTC-489 Embedded Real Time Software Development 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.
38
ENTC-489 Embedded Real Time Software Development Homework Structure
39
ENTC-489 Embedded Real Time Software Development 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);
40
ENTC-489 Embedded Real Time Software Development 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.