6-11章 傳感,驅動,控制-final-複習筆記(UTS-41081)

6.2.1: Latency.

【試題總結】:
Latency:The time between the input event and the triggered output event 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 Computing a task’s worst-case execution time

  • 3 assembly instructions for a simple C assignment statement, like cnt = 0 or B1 = cnt + A0.
  • 2 assembly instructions for a C comparison, like cnt >= 3.

習題總結

  • The state has actions B0 = 1, B1 = 0, B2 = 1, B3 = 0. How many microsec does one tick require? (3 + 3 + 3 + 3) * 2 = 24
  • The state has the action B = X + Y + Z. How many microsec does one tick require? (3+3) * 2=12
  • If the synchSM has period 20 microsec and actions tmp = X + Y, B = tmp + Z, what is the utilization? (3+3) * 2/20=60%
  • If a microcontroller has a 100 MHz clock and executes 1 instr per clock cycle, how long does 1 instr take to execute (in nanosec)? 1/(10^8 * 10 ^(-9)) = 10 nanosecond
    -在這裏插入圖片描述
  • As should be clear from above, the worst-case execution time (or WCET) of a synchSM task is determined as the time to execute the worst-case number of instructions for any possible tick of the synchSM.
    在這裏插入圖片描述
  • WCET for S0 is 3(tmp1…) + 2(if…) + 3(tmp2…) +3(tmp3…) = 11
  • The WCET for S1 involves approximately 3 + 100 * (2+3+3)+ 3 = 806
  • the worst-case execution time is the maximum of the WCETs of S0 and S1, not the minimum.

11.2 Jitter

A task’s jitter is the delay between the time that a task was ready to start executing and the time that it actually starts executing on the microcontroller.

習題總結:

Given a microcontroller that executes Task1 with WCET 50 ms and period 500 ms, and Task2 with WCET 100 ms and period 500 ms. Assume the scheduler schedules Task1 first. Draw the microcontroller usage diagram before answering the following.

  • What is Task1’s worst-case jitter? 0 ms(T1不需要等待T2)
  • What is Task2’s worst-case jitter? 50 ms(T2需要等待T1執行完,需要50ms)

11.3 Triggered synchSMs

  • Sometimes a synchSM tick just samples an input (such as input A0) to detect a change, a process known as polling.
  • The event triggers the synchSM to become active, and thus such a synchSM is called a triggered synchSM.
    在這裏插入圖片描述
  • In scheduling terminology, a task triggered by an event is known as an aperiodic task, in contrast to a periodic task that ticks at a known rate.
    在這裏插入圖片描述
  • Using existing special hardware that detects events on pins can yield the additional benefit of sampling the pin faster than sampling achieved by polling done by a task on a microprocessor.

習題總結

  • triggered synchSM:Tick only when specific events occur
  • aperiod task: A task triggered by an event
  • peridoic task: A task that ticks at a specific rate
  • Polling: Sampling an input while waiting for an event
  • event: A change on an input or data variable

11.4 Reducing power consumption using a sleep function

  • microcontrollers typically have a special low-power mode, known as sleep mode, in which the microcontroller stops the microcontroller’s CPU and thus stops executing the program, but continues to operate other hardware like the timer and the UART.
printf("Going to sleep..."); 
Sleep(); // Put processor in low-power mode waiting for wakeup event

// Below instructions won't execute until hw event wakes processor
// and calls appropriate ISR
printf("Waking up.\r\n"); 
while(!TimerFlag && !RxFlag);

【習題總結】

  • Most microcontrollers can be put into a low-power mode.
  • Microcontroller hardware wakes up the processor when certain events occur, like receiving a UART byte.
  • RIM can be put into a low-power mode via the Sleep() function. : RIM will wake up if the timer interrupt or UART ISR handlers are executed.
  • An interesting power technology for embedded systems is that of supercapacitors, which store energy using capacitance rather than typical battery technology.
  • Suppose a battery stores 5,000 J. For how many seconds can that battery power a desktop processor running at 100 W? 50
    (5,000 J * (1 sec / 100 J) = 50 sec)
  • Suppose a battery stores 5,000 J. For how many seconds can that battery power a high-end embedded processor running at 1 W (typical for a 32-bit embedded processor like an ARM)? 5,000 J * (1 sec / 1 J) = 5000 sec.
  • Suppose a battery stores 5,000 J. For how many seconds can that battery power a high-end embedded processor running at 1 W, if that processor is asleep 80% of the time and consumes 1 mW while asleep?5,000 J * ( (20% * 1s/1W) + (80% * 1s/0.001W) ) = 4001000.

11.5 Disciplined programming

【習題總結】

  • Desired behavior can be captured directly using C code but still using the synchSM computation model. 答案:While a graphical front-end tool might be convenient sometimes, such a tool is not required. A programmer can create tick functions for each synchSM directly using C code.
  • Given synchSMs implemented in C with the scheduler code in main() , a clever trick is to add counting variables inside the ISR to reduce the counting done by a synchSM using transitions and actions. FALSEBreaking from the disciplined process of capturing behavior in the synchSM, and then using a standard method to implement that synchSM in C, may save a few lines of code but can result in problems in the future including code that is harder to maintain.

11.6 Framework introduction

  • Pattern recognition is the job of monitoring sensors to detect a specific situation known as a pattern.
  • An embedded system pattern recognition framework is a structured approach to monitoring sensors to detect particular patterns and for controlling actuators in response.

Figure 11.6.1: Busy conference room example in embedded systems pattern recognition framework.

在這裏插入圖片描述

Figure 11.6.2: Box carrying example for the embedded systems pattern recognition framework.

在這裏插入圖片描述

  • The feature extraction part outputs a feature of a sensor that captures the characteristics of each label.
  • The pattern recognition part applies a label, e.g., low-light, based on a given input, e.g., the amount of light outside.
  • The actuation part controls one or more actuators based on the label (or labels) output by the pattern recognition part.

Figure 11.6.3: Embedded pattern recognition framework.

在這裏插入圖片描述

  • A feature is a property of a sensor output. For example, one feature of a motion sensor is the amount of output voltage, which is based on the amount of motion.
  • A label is a name given to a specific pattern.
  • A label category is a collection of related and mutually-exclusive labels.
  • Pattern recognition : part that gives a name to a characteristic of a feature
  • Actuation: Part that controls the actuators based on labels.
  • Feature extraction: Part of getting a feature from a sensor output.
  • Feature : A sensor output property
  • Label category: A collection of related labels.

【習題總結】

  • After adding the microphone in the busy conference room example, the actuation part changed. False The actuation part remained exactly the same.
  • Some parts in the pattern recognition framework cannot consist of synchSMs. False Consider the busy conference room example, in which each part consists of synchSMs.
  • Each part in the pattern recognition framework can have a different period. Consider the synchSMs from the busy conference room example.
  • The pattern recognition framework should be used for monitoring an Alzheimer’s patient and blinking a light at a nurse’s station in case the patient tries to leave at night.The sensors could be a motion sensor and a clock, with features person-walking and night-time, respectively. If the person-walking is at night-time, then a blinking light could be actuated.
  • The pattern recognition framework should be used for button debouncing.False Although button debouncing is a type of pattern recognition, button debouncing does not need feature extraction or actuation.

11.7 Part 1: Feature extraction

  • The feature extraction part outputs a feature of a sensor that captures the characteristics of each label, such as a photoresistor’s output voltage to capture low-light or good-light.

【習題總結】

  • The designer should collect then categorize boxes that are representative of the types of boxes that the system will be used to categorize. True
  • A synchSM to capture the weight value extraction will require more than 2 states. False==The synchSM may require only two states: The first state to initialize the analog-to-digital converter, and a second state to perform analog-to-digital conversion. The second state would always loop onto itself.
    ==
  • Refer to the graph of categorized boxes. The weight value feature can be used to distinguish between boxes that should be carried by two persons or a forklift. True
  • Refer to the graph of categorized boxes. The weight value feature can be used to distinguish between boxes that should be carried by one person or two persons. False==For boxes under 100 pounds, the weight value does not distinguish between one or two persons.The weight value does not distinguish between one or two persons for boxes under 100 pounds.
    ==
  • An accelerometer outputs a g-force acceleration, e.g., an accelerometer parallel to Earth’s gravity would output either 1.0 (if facing upward) or -1.0 (if facing downward).
  • The magnitude of a tri-axial accelerometer is better for fall detection than a single accelerometer because three sensors have more data than one sensor. FalseThe magnitude of a tri-axial accelerometer is better because the single accelerometer has a less consistent pattern during falls.
  • Graphing a feature over time makes identifying patterns easier, especially for time-oriented patterns. True
  • A synchSM could capture the feature extraction for the magnitude of a tri-axial accelerometer with two states. True

11.8 Part 2: Pattern recognition

  • The pattern recognition part applies a label, such as low-light, based on a given input, such as the amount of light outside.
  • A threshold is the value at which a feature changes from being recognized as one label to being recognized as another label.

Amount of light outside over a 24-hour cycle. The threshold value of 240 distinguishes between good-light and low-light.

在這裏插入圖片描述
A decision tree is the logical combination of multiple thresholds.

【習題總結】

在這裏插入圖片描述

  • Code for /* Question 1 */:hgtCM > 17
  • Code for /* Question 2 */:isFigNrm = 0;
  • Code for /* Question 3 */:wdhCM == 10
  • Code for /* Question 4 */:isFigNrm = 0;

SynchSM capturing the defective apple pattern recognition based on the apple’s size and green color.

**加粗樣式**

A synchSM captures the pattern recognition part of a busy conference room system.

在這裏插入圖片描述

Decision tree for box carrying (used in Try above).

在這裏插入圖片描述

11.10 C functions versus lookup tables

Array for converting Celsius to Fahrenheit.

const unsigned char C2F_uc_array[256] = {32, 34, 36, 37, 39, 41,
  ..., 255, 255, 255};    // middle values are omitted (...)

Function using array for converting Celsius to Fahrenheit.

inline unsigned char C2F_uc(unsigned char C) {
  return C2F_uc_array[C];
}

【習題總結】

const unsigned char lookup_table[10] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34};

unsigned char fibonacci_num(unsigned char x) {

   unsigned char num;

   if ( x >= 10 ) {
      num = 255;
   }
   else {
      num = lookup_table[x];
   }
   return num;
}
  • After the below code runs, what will be the value of num?`unsigned char num; num = fibonacci_num(6); 8
  • After the below code runs, what will be the value of num?unsigned char num; num = fibonacci_num(21);** 255 **
  • How many bytes of memory does lookup_table require?10
  • A new lookup table contains 100 separate 32-bit unsigned integers. How many bytes of memory does this new lookup table require?400

11.11 Fixed-point programming

  • C has built-in support for floating-point arithmetic, wherein variables are declared as real numbers and arithmetic/comparison operators (+, -, *, /, <, <=, >, >=, ==, !=) operate on real numbers.
float x;
x = 25.61;
  • The float type uses 4 bytes. C also provides a double type that uses 8 bytes (hence the name “double”).

C2F function using floating point.

float C2F_fl(float C) {
  float F;
  F = (9.0/5.0)*C + 32;
  return F;
}

Function computing the distance between two points using floating point.

#include <math.h>

typedef struct {
  double x, y;
} point_t;

double compute_distance(point_t p1, point_t p2) {
  double t1 = p2.x - p1.x;
  double t2 = p2.y - p1.y;
  t1 = t1 * t1;
  t2 = t2 * t2;
  return sqrt(t1 + t2);
}
  • In a fixed-point implementation, integer variables are used to represent real numbers (with limited range and precision).

Converting floating point to fixed point, and vice versa.

#include <assert.h>

const double MIN_REAL = (short)0x8000 / 16.0;
const double MAX_REAL = (short)0x7fff / 16.0;

short conv_to_fixed_point(double x) {
   if( x < MIN_REAL ) {
     assert( 0 /* Underflow */ );
   }
   else if( x > MAX_REAL ) {
     assert( 0 /* Overflow */ );
   }
   else {
     return (short)(x * 16.0);
   }
}

double conv_to_floating_point(short x) {
   return x / 16.0;
}

【習題總結】

  • A fixed point representation must specify the number of bits available to store the whole and fractional parts of a number.True
  • The fixed point representation “10.6” indicates that there are 10 bits for f (fractions) and 6 bits for w (whole numbers).False “10.6” indicates w=10 and f=6.
  • Given the number 20 in “5.3” fixed-point format, what’s the value of the whole part?2
  • Given the number 20 in “5.3” fixed-point format, what’s the value of the fractional part?0.5
  • Convert the number 4.75 to “5.3” fixed-point format.38

Fixed-point addition and subtraction.

inline short fixed_point_add(short x, short y) {
   return x + y;
}

inline short fixed_point_sub(short x, short y) {
   return x - y;
}

Add/subtract with over-/underflow checking.

#include <assert.h>

const short MIN_FP = 0x8000;
const short MAX_FP = 0x7fff;

inline short fixed_point_check(long x) {
   if( x < MIN_FP ) {
     assert( 0 /* underflow */ );
   }
   else if( x > MAX_FP ) {
     assert( 0 /* overflow */ );
   }
   else {
     return (short)x;
   }
}

inline short fixed_point_add(short x, short y) {
   return fixed_point_check( (long)x + (long)y );
}

inline short fixed_point_sub(short x, short y) {
   return fixed_point_check( (long)x - (long)y );
}
  • What’s the result of fixed_point_add(20, 38) using “5.3” fixed-point format? Enter OVERFLOW or UNDERFLOW if such a condition occurs. 58
  • What’s the result of fixed_point_sub(20, 38) using “5.3” fixed-point format? Enter OVERFLOW or UNDERFLOW if such a condition occurs.UNDERFLOW
  • What’s the result of fixed_point_mul(20, 38) using “5.3” fixed-point format? Enter OVERFLOW or UNDERFLOW if such a condition occurs.95
  • What’s the result of fixed_point_div(20, 38) using “5.3” fixed-point format? Enter OVERFLOW or UNDERFLOW if such a condition occurs.4

11.12 Lookup tables again

Computing distance between points using floating point.

#include <math.h>
typedef struct {
  double x, y;
} point_t;

double compute_distance(point_t p1, point_t p2) {
   double t1 = p2.x - p1.x;
   double t2 = p2.y - p1.y;
   t1 = t1 * t1;
   t2 = t2 * t2;
   return sqrt(t1 + t2);
}

Computing distance between points using (12.4) fixed point.

#include <math.h>
typedef struct {
   short x, y;  // fixed point format (12.4)
} point_t;

short compute_distance(point_t p1, point_t p2) {
   short t1 = fixed_point_sub(p2.x, p1.x);
   short t2 = fixed_point_sub(p2.y, p1.y);
   short t3;
   double t4;
   t1 = fixed_point_mul(t1, t1);
   t2 = fixed_point_mul(t2, t2);
   t3 = fixed_point_add(t1, t2);
   t4 = conv_to_floating_point(t3);
   t4 = sqrt(t4);
   return conv_to_fixed_point(t4);
}

short fixed_point_sqrt(short x) { // an algorithm for computing square root goes here ... }

Pre-computing square roots.

#include <stdio.h>
#include <math.h>
#include <assert.h>

const double MIN_REAL = (short)0x8000 / 16.0;
const double MAX_REAL = (short)0x7fff / 16.0;

short conv_to_fixed_point(double x) {
   if( x < MIN_REAL ) {
     assert( 0 /* underflow */ );
   }
   else if( x > MAX_REAL ) {
     assert( 0 /* overflow */ );
   }
   else {
     return (short)(x * 16.0);
   }
}

double conv_to_floating_point(short x) {
   return x / 16.0;
}

void main() {
   short i;
   printf("const short sqrt_loopkup_table[] = {\r\n");
   for(i=0; i<32678; i++) {
     printf("   %i,\n",conv_to_fixed_point(sqrt(conv_to_floating_point(i))));
   }
   printf("}\r\n");
   return 0;
}

Fixed-point square root function using a lookup table.

short fixed_point_sqrt(short x) {
   if( x < 0 ) {
     assert( 0 /* Can't take sqrt of a negative number */ );
   }
   else {
     return sqrt_loopkup_table[x];
   }
}
typedef struct {
   short x, y;  // fixed point format (12.4)
} point_t;

short compute_distance(point_t p1, point_t p2) {
   short t1 = fixed_point_sub(p2.x, p1.x);
   short t2 = fixed_point_sub(p2.y, p1.y);
   short t3;
   t1 = fixed_point_mul(t1, t1);
   t2 = fixed_point_mul(t2, t2);
   t3 = fixed_point_add(t1, t2);
   return fixed_point_sqrt(t3);
}
  • A lookup table’s values can be pre-computed by a separate program.A programmer might write a program that calculates 64KB worth of square root results, and then place those results in another program’s lookup table.
  • A lookup table can not contain fixed-point representations of numbers.False****A lookup table can not contain fixed-point representations of numbers.
  • A lookup table that contains 32KB worth of square root values probably requires less memory than the amount of code to compute a square root.FalseMost likely the code to compute a square root requires at most a few hundred bytes, thus the lookup table requires much more memory.

11.13 Introduction to digital signal processing

  • A signal is a value that is continuously changing over time.
  • It is often necessary to process signals in a number of ways to obtain usable results for a variety of applications.
  • Some of these applications are very simple in nature, for example, a thermostat continuously monitoring the temperature in a room and enabling/disabling the heating/cooling system when certain high/low thresholds are exceeded.
  • a radio continuously receiving electromagnetic waves and extracting an audio broadcast that is played back through the radio’s speaker.
  • With the increased use of digital components in system design (in particular the microprocessor) signal processing is frequently referred to as digital signal processing (DSP).

11.14 Sensors

  • Conceptually, a sensor is a component that measures some physical phenomena and provides the measurement result as an output, as shown below.
    A sensor outputs a signal that varies over time.
  • Most sensors output an analog signal. An analog signal not only has continuous values, but also has a value continuously over time.
  • In contrast, some sensors output a digital signal.
  • Viewed from the outside, such a sensor would be generating a stream of samples (say Temp0, Temp1, Temp2, …) that represent the temperature at time 0 seconds, 0.1 seconds, 0.2 seconds and so on.
  • The above analog temperature sensor is able to sense temperatures in the range of 0-100 degree Celsius.
  • The temperature sensor is said to have a +/- 1 degree resolution.
  • Sensors output values that are raw and usually require further processing to be of much use.

11.15 Actuators

  • We now consider an actuator.
  • For that to happen, we introduce a very important building block, namely a transform component, as shown below.
    -

11.16 ADC and amplification

  • An important transform component is the analog to digital converter (ADC).
  • An ADC is a mixed-signal transform.
  • Input range, measured in Volts, is the range of voltages that can be applied at the input of the ADC.
  • Output range, or quantization, measured in bits, is the number of bits in each output sample (typical values are 8, 12, 16, 24, and 32).
  • Sampling rate, measured in Hz, is the rate at which the ADC generates its output samples.
  • An electrical engineer might use the term signal under-loading to describe this design flaw.
  • We solve this problem by adding an amplifier trasform.
  • An amplifier is an analog device that takes its input signal and multiplies it by a number, called gain, to obtain its output.
  • Such a device is often referred to as an attenuator. An amplifier with a gain of 1 is referred to as a buffer. Continuing with our design, we obtain the following system.
  • Our attempt at fixing the problem of under-loading may introduce a new problem, commonly known as signal overloading.
  • The result of such a condition is clipping, namely the loss of signal values that are beyond the input range, as shown below.

11.17 Bias correction

  • Another problem that needs attention is that of bias correction.

Biased signal.-

Mic with bias correction, amplifier, and adc.在這裏插入圖片描述

11.18 Sample rate and quantization

  • So far, we have made an effort to fully utilize our ADC input range by performing signal conditioning activities along the path from the sensor to the ADC.
  • What governs the amount of loss of signal in the space axis has to do with the quantization or bit-width of the ADC (sometimes referred to as the resolution of the ADC)
  • We begin by introducing the concept of dynamic range.
  • The ADC we’ve used in our example can convert an input voltage in the range of -1V to +1V, thus we might reason that the conversion range of the ADC is 2 Volts.
  • The conversion range ignores the voltage gap between two consecutive samples.
  • We now turn our attention to sampling rate, or the rate at which our input signal is converted to a stream of samples.
  • According to the sampling theorem, an analog signal can be reconstructed from its samples if the sampling rate is at least twice the highest frequency present in the signal.
  • This sampling rate is also known as the Nyquist rate.

11.19 Aliasing

  • All systems are subject to noise.
  • it is desirable to have a very small noise relative to the signal, thus the signal-to-noise ratio (SNR). Higher SNR is better.
    在這裏插入圖片描述

11.20 Low pass filter

  • This can be accomplished using a low pass filter.
  • a low pass filter is sometimes referred to as an anti-aliasing filter.
  • Low pass filters are design to let all frequencies below a specific value, called the center frequency, through while eliminating all frequencies above the center frequency.

11.21 Playback path

在這裏插入圖片描述

  • The amplifier must have a gain that is sufficiently high to drive, or operate, the speaker.

11.22 Digital-to-analog converter

  • Another important transform component is the digital to analog converter (DAC).
  • Input range, or quantization, measured in bits, is the number of bits in each input sample (typical values are 8, 12, 16, 24, and 32).
  • Sampling rate, measured in Hz, is the rate at which the DAC consumes its input samples.

11.23 A DSP example

在這裏插入圖片描述
在這裏插入圖片描述

  • The microprocessor must perform all necessary computations in real time. Often times, engineers select specially designed microprocessors, called digital signal processors (DSPs) to ensure that all processing can be done quickly and with little power consumption.

11.24 Digital processing

Simple transform synchSM.

在這裏插入圖片描述

  • Let us make things more interesting by introducing a transform called scaling.
  • Another transform might be one that mixes the two signals Si1 and Si2

Mixing transform synchSM.

在這裏插入圖片描述

  • Another transform might be one that adds an echo to the stream.
    -
  • Depending on the application, these arrays may be implemented as bounded buffers.
  • Our last transformation is a digital low pass filter.

Digital low pass filter transform synchSM.

在這裏插入圖片描述

Digital low pass filter transform with low center frequency synchSM.

在這裏插入圖片描述

  • In sound, this is the function performed by an audio equalizer.
  • To see how such a device might be implemented we’ll need to make a distinction between time-domain transformations vs. frequency-domain transformations.
  • These transformations, known as the Fourier transform (FT) and inverse Fourier transform (IFT), are the cornerstone of most signal processing activities.
  • A second issue that might come up is that, if the input signal is slightly shifted (for instance, it has a phase shift of 45 degrees), our test fails to work.
  • Many digital signal processors have special multiply-accumulate instructions to perform these computations a little more efficiently.
  • Computer scientists have studied the FT procedure from an algorithmic point of view and have developed a version that is highly optimized, the so called fast Fourier transform (FFT).

11.25 Introduction to synchSMs on FPGAs

  • An FPGA is a type of programmable chip that is growing in popularity in embedded systems, commonly found alongside or even instead of a microcontroller.

11.26 Translating a synchSM to VHDL

MainSystem VHDL entity.

LIBRARY ieee; 
USE ieee.std_logic_1164.ALL;

ENTITY MainSystem IS
   PORT( 
      A_i: IN std_logic_vector(7 DOWNTO 0);
      B_o: OUT std_logic_vector(7 DOWNTO 0);
      clk_i: IN std_logic;
      rst_i: IN std_logic
   );
END MainSystem;

BlinkLed in VHDL.

在這裏插入圖片描述

ARCHITECTURE beh OF MainSystem IS
BEGIN
  
BlinkLed: PROCESS(clk_i)
   TYPE states IS (LedOff, LedOn);
   VARIABLE state : states := LedOff;
   VARIABLE B0_v : std_logic := '0';
BEGIN
   IF (clk_i='1' AND clk_i'EVENT) THEN
      IF (rst_i='1') THEN
         state := LedOff;
         B0_v := '0';
      ELSE  -- SM case stmts go here
         CASE state IS  -- Transitions
            WHEN LedOff => 
               state := LedOn;
            WHEN LedOn =>
               state := LedOff;
            WHEN OTHERS => 
               state := LedOff;
         END CASE;
         CASE state IS -- Actions
            WHEN LedOff =>
               B0_v := '0';
            WHEN LedOn =>
               B0_v := '1';
         END CASE;
      END IF;
      B_o(0) <= B0_v; -- update port signal
   END IF; 
END PROCESS;

END beh;

11.27 Achieving the proper synchSM tick rate

Adding a clock converter process to tick synchSM with a particular period.

ARCHITECTURE beh OF MainSystem IS
   SIGNAL BLclk_s: std_logic;
BEGIN
  
BlinkLed: PROCESS(BLclk_s)
   ...
   IF (BLclk_s='1' AND BLclk_s'EVENT) THEN
   ...
END PROCESS;

BlinkLedClk: PROCESS(clk_i) -- Suppose 1 MHz

   VARIABLE cnt: INTEGER := 0; 
BEGIN
   -- 1 MHz means 1 microsecond(us) per tick
   -- 500 ms * 1000us/ms = 500,000us
   IF (clk_i='1' AND clk_i'EVENT) THEN
      IF (rst_i='1') THEN
         cnt := 0;
      ELSE  
         cnt := cnt + 1; 
         IF (cnt = 500000) THEN
            BLclk_s <= '1';
            cnt := 0; 
         ELSE
            BLclk_s <= '0'; 
         END IF;
      END IF;  
   END IF;
END PROCESS;
-- Note: above process is shorthand for 1-state synchSM  

END beh;

BlinkingLed2 code.

ARCHITECTURE beh OF MainSystem IS
   SIGNAL BL2clk_s: std_logic;
BEGIN
  
BlinkingLed2: PROCESS(BL2clk_s)
   TYPE states IS (Init, LedOn, LedOff);
   VARIABLE state : states := Init;

   VARIABLE X_v : std_logic_vector(7 DOWNTO 0);
   VARIABLE B0_v : std_logic; 

BEGIN
   IF (BL2clk_s='1' AND BL2clk_s'EVENT) THEN
      IF (rst_i='1') THEN
         state := Init;
      ELSE 
         CASE state IS  -- Transitions
            WHEN Init=> 
               state := LedOn;
            WHEN LedOn =>
               IF (X_v < "00000011") THEN
                  state := LedOn;
               ELSIF (NOT(X_v < "00000011")) THEN
                  state := LedOff;
               END IF;
            WHEN LedOff => ...
            WHEN OTHERS => 
               state := Init;
         END CASE;
         CASE state IS -- Actions
            WHEN Init => 
               X_v := "00000000";
               B0_v := '0';
            WHEN LedOn =>
               X_v := X_v + "00000001";
               B0_v := '1';
            ...
         END CASE;
      END IF;
      B_o(0) <= B0_v; 
   END IF;  
END PROCESS;

11.28 Multiple synchSMs

在這裏插入圖片描述

Multiple synchSM VHDL.

ARCHITECTURE beh OF MainSystem IS
   SIGNAL BLclk_s, TLclk_s: std_logic;
BEGIN
  
-----
 
BlinkLed: PROCESS(BLclk_s) 
   ...
END PROCESS;
 
BlinkLedClk: PROCESS(clk_i) -- gen 300 ms BLclk_s
   ...
END PROCESS;
 
------
 
ThreeLeds: PROCESS(TLclk_s)
   ...
END PROCESS;

ThreeLedsClk: PROCESS(clk_i) -- gen 200 ms TLclk_s
   ...
END PROCESS;

END beh;

MainSystem with shared variable.

ARCHITECTURE beh OF MainSystem IS
   SIGNAL DMclk_s, ILclk_s: std_logic;
   SHARED VARIABLE mtn_sv: std_logic;
BEGIN
  
-----
 
DetectMotion: PROCESS(DMclk_s) 
   ...
         mtn_sv := '1';
   ...
END PROCESS;
 
DetectMotionClk: PROCESS(clk_i) --gen 200 ms DMclk_s
   ...
END PROCESS;
 
------
 
IlluminateLamp: PROCESS(TLclk_s)
   ...
         IF (mtn_sv = '1') THEN
   ...
END PROCESS;

IlluminateLampClk: PROCESS(clk_i) --gen 200 ms ILclk_s
   ...
END PROCESS;

END beh;

11.1 計算任務最壞執行時間
可以通過用匯編指令數乘執行器執行每個彙編任務的時間來估算。

  • 簡單的複製語句需要3個彙編指令
  • 比較語句需要2個彙編指令
  • 只考慮簡單賦值的操作,複雜的語句替換成簡單的再執行
  • 現代的微控制器每個時鐘週期執行大約1條指令,因此時鐘頻率爲1 MHz的微控制器執行1百萬條指令/秒,即每條指令1微秒,而1 GHz時鐘意味着每條指令1納秒。
    在這裏插入圖片描述

【錯題總結】
假設每條指令需要2微秒才能執行。

  1. 該狀態有動作B0 = 1,B1 = 0,B2 = 1,B3 =0。一個刻度需要多少微秒? 4x3x2 = 24
  2. 該狀態的作用爲B = X + Y +Z。一個刻度需要多少微秒?tmp = X+Y; B = tmp+Z => 3x2x2=12
  3. 如果synchSM的週期爲20微秒,並且動作tmp = X + Y,B = tmp + Z,那麼利用率是多少? 12/20 = 60%
  4. 如果微控制器具有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的最長執行時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章