Download presentation
Presentation is loading. Please wait.
1
CSCI1600: Embedded and Real Time Software
Lecture 18: Advanced Programming with I/O Steven Reiss, Fall 2017 Check the termination condition on pop for circular queue.
2
Tasks Need to Communicate
Simple communication is one-way Task A decides what lights should be on Task B just displays what it is told Global variable Written only by task A Read by task B This is safe as long as there is only one writer / variable Why care about multiple writers? Multiple writers: synchronization issues with multiple processes, synchronization issues with interrupts Caching behaviors. Might be different processors in the same system (variable might be communications link) Lecture 17: Advanced Programming 11/11/2018
3
Problem to Consider Keypad for entering alarm code
Task to read the keypad Determine when a button has been pushed Want to check a sequence of button presses Against known code(s) Task that does the checking How do these communicate? What are the problems? FROM LAST TIME Lecture 16: Input/Output II 11/11/2018
4
Making Communication Simple
Here we have a button press task that sets the currently pressed button And a second task that checks for a correct series of buttons. Problem: how to only commuicate one-way between these tasks Lecture 16: Input/Output II 11/11/2018
5
Handshake Communication
The previous depended on timing (or sequential tasks) What should we do when tasks need to synchronize Task A sends a request to Task B Task A needs to know when Task B is done In order to send another request Ensure no new button is pressed until current one is processed Can use 2 variables rather than one Task A owns variable REQ Task B owns variable ACK Handshakes are more common in communications Arduino is single threaded – can this be done with a single variable? Lecture 16: Input/Output II 11/11/2018
6
Handshake Communication
Code A: set Data, set REQ, wait for ACK, clear REQ, wait for ~ACK B: wait for REQ, read Data, set ACK, wait for ~REQ, clear ACK Sequence A: set Data, set REQ B: wait for REQ, read Data, set ACK A: wait for ACK, clear REQ, wait for ~ACK B: wait for ~REQ, clear ACK Lecture 16: Input/Output II 11/11/2018
7
Communicating Arduinos
Suppose you want to have two Arduinos talk to each other Just simple message back and forth (sequence of bits) How would you do this? Lecture 16: Input/Output II 11/11/2018
8
Communication: Queues
More complex communication involves multiple requests Model railroad: multiple switches can be triggered But only one can be activated at a time This is generally implemented as a queue Allows the two tasks to be asynchronous Lecture 17: Advanced Programming 11/11/2018
9
Queue-based Tasks Lecture 17: Advanced Programming 11/11/2018
10
Queue Implementation Queues in embedded programs
Are generally fixed in size Often sized so they never overflow How to program a queue? Lecture 17: Advanced Programming 11/11/2018
11
Simple Queue What’s wrong with this? byte queue[10]; int qp = 0;
void push(byte x) [ TASK A ] if qp < 10, then queue[qp++] = x else exception byte pop() [ TASK B ] if (qp == 0) exception rslt = queue[0]; for (i = 1; i < qp; ++i) queue[i-1] = queue[i] qp = qp-1 return rslt What’s wrong with this? What is wrong with this code? First: both tasks are writing qp – would need to be synchronized Second: lots of copying going on of elements in the queue Third: both are reading/writing queue arbitrarily Lecture 17: Advanced Programming 11/11/2018
12
Circular Buffer Queue Use a single array Maintain two pointers
Maintain a start and end pointer Treat the array as a circular buffer Element beyond the last is the first, etc. Maintain two pointers Head (read): pointer to the first element to extract Tail (write): pointer to the next place to insert Lecture 17: Advanced Programming 11/11/2018
13
Circular Buffer Pop: if read == write then exception
result = buf[read]; read = (read + 1) % size Lecture 17: Advanced Programming 11/11/2018
14
Circular Buffer Push(data): If read == write then exception
buf[write] = data write = (write + 1)%size Lecture 17: Advanced Programming 11/11/2018
15
Circular Buffer Advantages Extensions Little data movement
Pointers are only changed by one task Queue cells are safely read/written Code is simpler Extensions Can read/write multiple things at once Data stream or communications channel Lecture 17: Advanced Programming 11/11/2018
16
Circular Buffers and Handshakes
What happens with a circular buffer of size one? Lecture 17: Advanced Programming 11/11/2018
17
Multiple Inputs on a Port
How to multiplex a port with different inputs Generally unsafe to connect multiple sensors at once If sensor is at ground, another sensor at high might ground thru the first, not through the input port Then the input would always be low To get around this Switches: only provide power/ground to enabled ones Use diodes to prevent back-flow Other logic circuits/devices: Open collector Lecture 18: Advanced Programming w/ I/O 11/11/2018
18
Open Collector Circuit
Add a transistor to circuit Acts as a switch Effectively unconnected if off External pull up makes it high Grounded if on Result is low Can connect multiple Effectively an OR gate Many ICs have this built in Hence negated outputs Lecture 18: Advanced Programming w/ I/O 11/11/2018
19
Serial Communication Send data from one machine to another
Using only 2 wires Using clocked serial input 10 bits to send 8 Plus parity? Lecture 18: Advanced Programming w/ I/O 11/11/2018
20
UART: Hardware for Serial I/O
Accepts bytes in parallel from the CPU Sends these serially at fixed rate Reads bytes from the serial line Tells CPU when a byte is ready to read Includes lines to ensure synchronization Ready to transmit, … Lecture 18: Advanced Programming w/ I/O 11/11/2018
21
UART Internals Lecture 18: Advanced Programming w/ I/O 11/11/2018
22
Interrupts Interrupts allow occasional inputs that aren’t polled
Limited set of pins support interrupts Varies based on Arduino model, for example attachInterrupt(pin,routine,mode) routine – pointer to function (function name in C) mode: LOW, CHANGE, RISING, FALLING, HIGH High not always supported on Arduino detachInterrupt(pin) Lecture 18: Advanced Programming w/ I/O 11/11/2018
23
Other Interrupts Analog interrupts Timer interrupts
Configure based on threshold Timer interrupts Based on clock counter overflowing Direct interrupt lines into CPU Lecture 18: Advanced Programming w/ I/O 11/11/2018
24
Interrupt Synchronization
Interrupts are asynchronous What can go wrong? Need to synchronize for them noInterrupts() – disables interrupts interrupts() – enables interrupts Can have multiple levels of interrupts Lecture 18: Advanced Programming w/ I/O 11/11/2018
25
NIM How could you use interrupts for nim? Lights? Switches? Timers?
Sound? Lecture 18: Advanced Programming w/ I/O 11/11/2018
26
Interrupt Coding Interrupt routines should do a minimum amount of work
Time added to time of executing task Asynchronous -> race conditions can occur Typically these routines just set a flag Possibly read/write value and set flag Possibly read/write from/to circular buffer Lecture 18: Advanced Programming w/ I/O 11/11/2018
27
Interrupt Problems What can go wrong with this code?
static int iTemperatures[2]; void interrupt vReadTemperatures (void) { iTemperatures[0] = read Dev 1 iTemperatures[1] = read Dev 2 } void main(void) { int iTemp0, iTemp1; // Setup code iTemperatures[0] = 0; iTemperatures[1] = 0; while(TRUE) { iTemp0 = iTemperatures[0]; iTemp1 = iTemperatures[1]; if ( iTemp0 != iTemp1 ) { // Set off alarm! } What can go wrong with this code? Where can the interrupt occur? Need to disable and enable interrupts around the reads of temperatures This leads us to concurrent programming issues (next time). Lecture 18: Advanced Programming w/ I/O 11/11/2018
28
Homework Read Chapters 10, 11 Lecture 18: Advanced Programming w/ I/O
11/11/2018
29
NIM Redux What were your tasks? What did your main loop look like?
What is done in the various tasks? Did you model your tasks before coding them? How often are the tasks run? Do this for minutes unless there is material to cover from previous lecture. Lecture 18: Advanced Programming w/ I/O 11/11/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.