悉尼科技大学期末复习笔记
- 6.2.1: Latency.
- 6.2.2: Button/LED system timing.
- 6.3 Input conditioning
- 6.4 Avoiding output glitching
- 6.5 I/O electrical issues
- 6.6 Dealing with too few pins
- 7.1 Pulse width modulation
- 7.2 UARTs
- 7.3 Analog-digital conversion
- 8.1 Converting different-period tasks to C
- 8.2 Creating a task structure in C
- 8.3 Code for a simple cooperative task scheduler
- 9.1 Timer overrun
- 9.2 Utilization
- 9.3 Utilization for multiple tasks
- 9.4 Scheduling
- 9.5 Preemptive scheduler
- 10.1 Introduction to control systems
- 10.2 Proportional control
- 10.3 Proportional-derivative (PD) control
- 10.4 Proportional-integral-derivative (PID) control
【试题总结】:
Latency:The time between the input event and the triggered output event latency.
6.2.1: Latency.
Consider the above synchSM. Round time answers to the nearest 100 ms, as in 500 rather than 499. Omit “ms” from your answers.
- What is longest time the system might take to detect a rise on A0? 500ms
- After A0 rises, how much time is spent in state Compute? 500ms
- When a tick occurs that transitions to state Write, how much time passes before the output B is updated?0
- What is the maximum latency between A0 rising and B being updated?1000ms
- The Compute state could be eliminated, moving Compute’s actions to the top of Write’s actions, or moving them to Mealy actions on the transition from Wait to Write. What is the maximum latency between A0 rising and B being updated in that case?500ms
- For the original synchSM above, what would be the maximum latency if the period was set to 100?200ms
6.2.2: Button/LED system timing.
When a button is pressed, a door bell rings. Given each timing specification, complete the corresponding synchSM constraints. Valid answers for questions 1-3 are (<, <=, >, >=, ==).
- Minimum button press: 200 ms:Period should be < 200
- Minimum separation time between a button release and a button press: 150 ms: Period should be < 150
- Max latency between button press and bell ring: 75 ms:Period should be <= 75
- Bell rings for exactly 1500 ms :Period should evenly divide 1500
- Given a period must be < 200, < 150, and <= 75, and must evenly divide 1500, what is the period that will also minimize microcontroller utilization?75ms
6.3 Input conditioning
【定义】
- Sensors are not perfect. The values they provide to a microcontroller commonly must be adjusted to reduce the impact of such imperfections; such adjustment is known as input conditioning.
- Button debouncing is the task of ignoring the bouncing on the signal from a button so that a single press is interpreted by the system correctly as a single press and not as multiple presses.
- More generally, filtering involves ignoring certain input events. A system’s inputs may be subject to noise, such as electromagnetic interference (EMI)
【问题】 - Filtering:ignoring certain input events
- Input conditioning: adjusted to reduce the impact of such imperfections
- EMI:noise caused by nearby electrical products
- Glitches: known as spurious signals, spikes
- Button debouncing: a type of glitch filtering a single button press from being interpreted as multiple button presses.
- Filtering can greatly reduce the possibility of an incorrect reading, but cannot eliminate all possible incorrect readings.
- The technique described above for debouncing is simply to set the synchSM period to longer than a button’s bouncing time.
- The technique described above for filtering glitches is to require two consecutive samples.
6.4 Avoiding output glitching
Each state machine below waits for A7 to be pressed, then counts the number of 1s on the low nibble of input A and displays the result on B.
- If A is 0x8C, what is B’s integer value after 5 ticks of the InputCounter_A SM? 1
- If A is 0x8C, what is B’s integer value after 7 ticks of the InputCounter_A SM? 2
- If A is 0x8C, what is B after 5 ticks of the InputCounter_B SM? 0
- If A is 0x8C, what is B after 7 ticks of the InputCounter_B SM? 2
- Which SM avoids output glitching, InputCounter_A or InputCounter_B? InputCounter_B
6.5 I/O electrical issues
-
A buffer IC may be added to a microcontroller output to increase the voltage and/or current drive capability
-
a load resistor may be added to reduce a microcontroller’s output voltage to the desired voltage.
-
In contrast, in the pull-up configuration, if a pin is not driven with a 0 or a 1, reading the pin yields a 1.
【问题】 -
If a microcontroller pin has a max output current of 25 mA, and an LED requires 15 mA, what is the maximum number of LEDs can be connected to that pin and still work properly?:1
解析: Only 1 LED can be connected to the pin without exceeding the max output current of the pin
-
The pull-up pulls the voltage up to 1 by default. Pressing the button completes a connection between ground and the pin, resulting in a reading of 0.
6.6 Dealing with too few pins
- Dividing output data into pieces, and sending those pieces one at a time is called:Time_multiplexed output
- If a transistor is connected to an LED, Rapid-refresh can be used to maintain the desired output on the LED.
7.1 Pulse width modulation
- periodic signal because the pattern repeats, in this case with a period of 1 second.
- duty cycle = H / (H + L).
- A signal with a duty cycle of 50% is called a square wave.
- The duty cycle is the percentage of time the signal is high during the period
- pulse width modulator or PWM is a programmable component that generates pulses to achieve a specified period and duty cycle.
- (output signal period) = (synchSM period) * ( H + L )
- A PWM has a period of 50 ms and a pulse duration of 10 ms. What is the duty cycle? 20%
- A PWM has a period of 200 ms. How long must the pulse duration be to achieve a 75% duty cycle? 150
- What is the pulse duration for a square wave with a period of 200 ms ? 100ms
- A DC motor has a maximum revolutions-per-minute of 500 rpm. If the motor’s rpm is linearly related to the PWM’s duty cycle, and the PWM has a period of 1 second, what should the duty cycle be to run the motor at 300 rpm? 60
- A PWM generates a square wave signal that produces a musical note of F0 on a speaker. What is the period in ms ? 46
- PWM1 generates a signal with a 10% duty cycle. PWM2 generates a signal with the same period but a 50% duty cycle. If both PWMs are connected to speakers, PWM1’s output sound will be quieter than PWM2’s output sound. Valid answers: quieter, louder, and same.
7.2 UARTs
- However, to conserve limited output pins, microcontrollers typically include a hardware device, called a UART
- When dealing with UARTs, receive is typically written as rx, and transmit as tx.
- Serial communication is when data is sent: one bit at a time
- A UART uses what type of communication?Serial
- How many bits of data does the above described UART automatically communicate?8
- In RIMS, what variable must be checked before writing data to the global UART transmit variable T?TxReady
- When is RxISR called?When the UART receives a complete byte data
- What is a common error when using the above described UART?Forgetting to turn on the UART, Failing to read R when data is stored in R
7.3 Analog-digital conversion
- analog-to-digital converter or ADC (sometimes A2D) converts an analog signal into a digital signal
- The analog signal whose voltage value varies continuously over time
- The digital signal is a series of bits, such as 8 bits or 12 bits, whose binary values represent the voltage level at the given time.
- An ADC is a mixed-signal peripheral device
- successive approximation: A common approach for analog to digital conversion where a controller repeadly guesses at a digital value using a comparator
- quantization error:The loss of precision when converting an analog signal to a digital signal
- mixed-signal peripheral: A device that has a mix of both analog and digital I/Os.
- If a signal has a minimum voltage of -3v and a maximum voltage of +3v, the range of the signal is between -3v and +3v (or 6v).
- An ADC has a range of digital values between -512 and +511. What is the precision (the number of bits required to represent the range of possible digital values.)of the ADC?10
- If an ADC has a precision of 9, what is the range of possible digital values?-256 to 256 //Using 2’s complement, the range of possible digital values of 2^9 is -256 to +255.
- An ADC has a range of -3 V to +3 V and a precision of 10 bits. What is the size of a conversion interval when converting an analog signal to a digital signal?0.00586 6/1024
8.1 Converting different-period tasks to C
- Consider a system with two tasks, Task1 and Task2. Task1 has a period of 200 ms, and Task2 has a period of 300 ms. All tasks initially tick at time 0.
- How many times will Task1 have ticked after 1000 ms? 6
- How many times will Task2 have ticked after 1000 ms?4
- After time 0, when do both Task1 and Task2 next tick at the same time?600ms
- What is the largest value for timerPeriod that allows both tasks to tick at the desired rate?100ms
8.2 Creating a task structure in C
- A struct (short for structure) is a C construct that allows several variables to be grouped together under a single name.
- typedef is a C construct that defines a new data type, in contrast to the built-in C data types like int and char.
- unsigned long elapsedTime:stores how much time has passed since the task last ticked
- int (*TickFct) (int): The synchSM tick function of a task
- unsigned long period: Store how often a task ticks
- int state: stores the current state of a task
typedef struct task {
int state; // Task's current state
unsigned long period; // Task period
unsigned long elapsedTime; // Time elapsed since last task tick
int (*TickFct)(int); // Task tick function
} task;
task tasks[V];
const unsigned short tasksNum = V;
enum BL_States { BL_SMStart, BL_S1 };
int TickFct_BlinkLed(int state) {
// ...
}
enum TL_States { TL_SMStart, TL_S1, TL_S2, TL_S3 };
int TickFct_ThreeLeds(int state) {
// ...
}
int main() {
unsigned char i = 0;
tasks[i].state = W;
tasks[i].period = 500;
tasks[i].elapsedTime = Y;
tasks[i].TickFct = &TickFct_BlinkLed;
i++;
tasks[i].state = TL_SMStart;
tasks[i].period = X;
tasks[i].elapsedTime = Y;
tasks[i].TickFct = Z;
// ...
}
- V = 2
- W : BL_SMStart
- What is X if ThreeLeds executes twice as frequently as BlinkLed? 250
- What is Y?tasks[i].period
- What is Z? TickFct_ThreeLeds
8.3 Code for a simple cooperative task scheduler
for (i=0; i < tasksNum; i++) {
if (tasks[i].elapsedTime >= tasks[i].period){
// Task is ready to tick, so call its tick function
tasks[i].state = tasks[i].TickFct(tasks[i].state);
tasks[i].elapsedTime = 0; // Reset the elapsed time
}
tasks[i].elapsedTime += tasksPeriodGCD;
}
- A task scheduler determines when each task should be executed in a multiple-task system. A task scheduler is commonly found inside the code for an operating system (OS), but can instead be included directly in user code as above, which is especially useful in the absence of an OS.
Complete the task scheduler.
void TimerISR() {
unsigned char i;
for (i = 0; i < tasksNum ; i++) {
if (tasks[i].elapsedTime >= tasks[i].period ) {
tasks[i].state = tasks[i].TickFct(tasks[i].state );
tasks[i].elapsedTime = 0;
}
tasks[i].elapsedTime += tasksPeriodGCD;
}
}
9.1 Timer overrun
Scheduler in ISR with flag indicating that ready tasks are being processed.
unsigned char processingRdyTasks = 0;
void TimerISR() {
unsigned char i;
if (processingRdyTasks) {
printf("Period too short to complete tasks.\r\n");
return;
}
processingRdyTasks = 1;
for (i = 0; i < tasksNum; ++i) { // Heart of the scheduler code
if ( tasks[i].elapsedTime >= tasks[i].period ) { // Ready
tasks[i].state = tasks[i].TickFct(tasks[i].state);
tasks[i].elapsedTime = 0;
}
tasks[i].elapsedTime += tasksPeriodGCD;
}
processingRdyTasks = 0;
}
- Automatically-detected incorrect execution is known as an exception. We call the above a timer-overrun exception.
- Detecting timer overrun within scheduler code.
1. Detect if timer interrupt occurred while processing tasks; print error and return
2. Raise flag (means processing tasks)
3. Process tasks
4. Lower flag (means no longer processing tasks)
- The fact that a task’s actions take non-zero time to execute may contribute to timer overrun.
- The scheduler code itself (the for loop, if statements, etc.) also take some time to execute and thus may contribute to timer overrun.
- A larger period means there is more time for the task’s actions to execute before the next task tick, so timer overrun is less likely.
- If a task has many actions, the scheduler spends most of its time in the function call tasks[i].TickFct(tasks[i].state)
9.2 Utilization
- The microcontroller is said to be idle during that waiting time.
- utilization = time-per-task-tick / task-period
A microcontroller executes a single synchSM task. Each task execution requires T ms. The task’s period is P ms. Determine the utilization for the following T and P values.
- T is 50, P is 500. 10%
- T is 4, P is 200. 2%
- T is 240, P is 200. 120%
- One way to decrease utilization is to increase the period
- One way to decrease utilization is to replace the first two statements with the one statement tmp2 = 20736 * A * A, assuming a compiler wouldn’t already make that replacement
- Each tick executes fewer actions so utilization is reduced. If the time to execute S0’s actions is reduced from 150 to 75 microsec, and S1 also requires 75 microsec, then the utilization is reduced from 150/100 or 150%, to 75/100 or 75%. Of course, system latency and the sampling rate are worsened.
- Increasing the microcontroller’s clock frequency will decrease utilization
9.3 Utilization for multiple tasks
- The timing diagram below is a microcontroller usage diagram showing how the two tasks execute on microcontroller M using our earlier scheduler and assuming each task tick executes for the task’s WCET.
- When two or more tasks have different periods, the time window of interest for computing utilization or determining actual microcontroller usage is the hyperperiod of the tasks, which is the least-common-multiple (LCM) of the tasks’ periods.
- Determine M’s sec/instr rate – call this R
- Analyze each task Ti to determine its worst case number of instructions per tick, then multiply that number by R to determine Ti.WCET
- Determine the hyperperiod H as LCM(T1.period, T2.period, …, Tn.period)
- Utilization = ( (H/T1.period)*T1.WCET + (H/T2.period)*T2.WCET + … + (H/Tn.period)*Tn.WCET ) / H. Note that H/Ti.period is simply the number of times that Ti executes during hyperperiod H.
- Utilization > 100%: Timer overrun will occur (assuming WCET)
- Utilization < 100%
Single task: Timer overrun shouldn’t occur;Multiple tasks: Timer overrun may occur. - For a microcontroller running one task, utilization < 100% indicates timer overrun should not occur.
- A fast-ticking task may yield timer overrun if the other task has a long WCET, causing the scheduler to be busy executing that task when a timer tick occurs.
9.4 Scheduling
-
Ready means the period has already elapsed and the task is now ready to execute.
-
A waiting task is not ready to execute, but instead waiting for its period to elapse. Even if the microcontroller is available, a waiting task should not execute.
-
Ideally, the moment a task becomes ready, the task would immediately begin executing.
-
A task status can change from ready to waiting.
-
Scheduling is the job of choosing which of several ready tasks to execute.
-
A scheduler is the code responsible for scheduling tasks.
-
A task’s deadline is the time by which a task must complete after becoming ready.
-
Priority is an ordering of tasks indicating which ready task should execute first.
-
A static-priority scheduler assigns a priority to each task before the tasks begin executing, and those priorities don’t change during runtime.
-
1.shortest deadlines.
2.shortest-period tasks( rate-monotonic scheduling (RMS),)
3.shortest-WCET tasks
4.manually assign static-priorities to tasks -
dynamic-priority scheduler determines task priorities as the program runs, meaning those priorities may change.
-
The approach is known as earliest deadline first (EDF).
9.5 Preemptive scheduler
- A preemptive scheduler may temporarily stop (preempt) an executing task if a higher-priority task becomes ready.
- Using a preemptive scheduler introduces nested interrupts, which occur when an interrupt handler interrupts itself.
- A critical section indicates a section of code that must not be interrupted (or accessed by multiple tasks at once, in the context of multi-threaded systems).
- Efficient at executing tasks consisting of infinite loops.Preemptive
10.1 Introduction to control systems
- A control system is a common type of embedded system that regulates the behavior of a physical device by seeking to match a system value to a desired value.
- The device being controlled is called the plant
- A desired output value is an input to the system
- The difference between the desired output value and the actual output value, desired - actual, is known as the error.
- A controller strives to reduce the error to zero by changing the value of the actuator input in response to positive or negative error values.
10.2 Proportional control
- Actuator = Kp * Error
- proportional controller sets the actuator input value equal to the error times a constant:
- A proportional controller adjusts the actuator according to the difference between actual and desired system output.
- Kp is a carefully-chosen constant.
10.3 Proportional-derivative (PD) control
- Actuator = KpError - KdDeriv
- Kd is typically larger than Kp.
- A PD controller considers both the output error and the output rate of change.
- Subtracting the current value from the last value yields the slope, as in Deriv = Actual - ActualPrev.
- The derivative term should be subtracted from the proportional term, as in KpError - KdDeriv.
10.4 Proportional-integral-derivative (PID) control
- An undesirable feature in a control system is steady-state error
- Actuator = KpError + KiInteg - Kd*Deriv
- Ki is typically much smaller than Kp
- Steady-state error occurs if the actual output never reaches the desired output level.
- A programmer should be careful to not exceed the valid range of actuator input.
11.1 计算任务最坏执行时间
可以通过用汇编指令数乘执行器执行每个汇编任务的时间来估算。
- 简单的复制语句需要3个汇编指令
- 比较语句需要2个汇编指令
- 只考虑简单赋值的操作,复杂的语句替换成简单的再执行
- 现代的微控制器每个时钟周期执行大约1条指令,因此时钟频率为1 MHz的微控制器执行1百万条指令/秒,即每条指令1微秒,而1 GHz时钟意味着每条指令1纳秒。
【错题总结】
假设每条指令需要2微秒才能执行。
- 该状态有动作B0 = 1,B1 = 0,B2 = 1,B3 =0。一个刻度需要多少微秒? 4x3x2 = 24
- 该状态的作用为B = X + Y +Z。一个刻度需要多少微秒?tmp = X+Y; B = tmp+Z => 3x2x2=12
- 如果synchSM的周期为20微秒,并且动作tmp = X + Y,B = tmp + Z,那么利用率是多少? 12/20 = 60%
- 如果微控制器具有100 MHz时钟,并且每个时钟周期执行1条指令,则执行1条指令需要多长时间(以纳秒为单位)?
状态的动作可能包括循环,函数调用,分支语句等
对于for循环,分析应包括循环初始化(i = 0:3个指令),加上循环控制指令(i <4,而i ++,所以2 + 3),并且还应将每个循环的指令相乘-迭代次数。上面的循环迭代4次。如果循环迭代次数与数据有关,则应使用迭代次数的上限。
WCET:worst -case exectue time
【错题整理】
基于下图回答问题:
9. S0的WCET是多少?3+2+3+3 = 11
10. S1的WCET是多少? 3+(3+3+2)*100+3 = 806
11. WECT是S0和S1的最长执行时间。