首页 > > 详细

CSYE 7374讲解、讲解Systems、辅导C/C++程序语言、讲解c++解析Haskell程序|讲解留学生Processing

CSYE 7374: IoT Embedded Systems
Assignment 5
Due on 4/15/20
1) Consider the C program and simplified memory map for a 16-bit microcontroller shown
below. Assume that the stack grows from the top (area D) and that the program and static
variables are stored in the bottom (area C) of the data and program memory region. Also,
assume that the entire address space has physical memory associated with it.
You may assume that in this system, an int is a 16-bit number, that there is no operating
system and no memory protection, and that the program has been compiled and loaded into
area C of the memory.
1. For each of the variables n, m, and a, indicate where in memory (region A, B, C, or
D) the variable will be stored.
2. Determine what the program will do if the contents at address 0x0010 is 0 upon entry.
3. Determine what the program will do if the contents of memory location 0x0010 is 1
upon entry.
2) Consider the following piece of code:
// C program to implement cond(), signal()
// and wait() functions
#include
#include
#include
// Declaration of thread condition variable
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
// declaring mutex
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int done = 1;
1
// Thread function
void* foo()
{
// acquire a lock
pthread_mutex_lock(&lock);
if (done == 1) {
// let’s wait on conition variable cond1
done = 2;
printf("Waiting on condition variable cond1\n");
pthread_cond_wait(&cond1, &lock);
}
else {
// Let’s signal condition variable cond1
printf("Signaling condition variable cond1\n");
pthread_cond_signal(&cond1);
}
// release lock
pthread_mutex_unlock(&lock);
printf("Returning thread\n");
return NULL;
}
What main function generates the following output ?
Waiting on condition variable cond1
Signaling condition variable cond1
Returning thread
Returning thread
3) Consider the following excerpt of code:
pthread_mutex_t X; // Resource X: Radio communication
pthread_mutex_t Y; // Resource Y: LCD Screen
pthread_mutex_t Z; // Resource Z: External Memory (slow)
void ISR_A() { // Safety sensor Interrupt Service Routine
pthread_mutex_lock(&Y);
pthread_mutex_lock(&X);
display_alert(); // Uses resource Y
send_radio_alert(); // Uses resource X
pthread_mutex_unlock(&X);
pthread_mutex_unlock(&Y);
}
void taskB() { // Status recorder task
while (1) {
static time_t starttime = time();
pthread_mutex_lock(&X);
pthread_mutex_lock(&Z);
stats_t stat = get_stats();
radio_report( stat ); // uses resource X
record_report( stat ); // uses resource Z
pthread_mutex_unlock(&Z);
pthread_mutex_unlock(&X);
sleep(100-(time()-starttime)); // schedule next execution
}
}
void taskC() { // UI Updater task
while(1) {
2
pthread_mutex_lock(&Z);
pthread_mutex_lock(&Y);
read_log_and_display(); // uses resources Y and Z
pthread_mutex_unlock(&Y);
pthread_mutex_unlock(&Z);
}
}
The scheduler running aboard the system is a priority-based preemptive scheduler, where
taskB is higher priority than taskC. In this problem, ISR A can be thought of as an
asynchronous task with the highest priority.
The intended behavior is for the system to send out a radio report every 100ms and for the
UI to update constantly. Additionally, if there is a safety interrupt, a radio report is sent
immediately and the UI alerts the user.
Occasionally, when there is a safety interrupt, the system completely stops working. What
scenario would that be?
4) Connect an LED to GPIO 5 on the Raspberry PI 3, using a current limiting resistor
in series (any resistor between 100 and 300 Ω should be fine, the smaller the resistor the
brighter the LED will be):
If you are using a different type of Raspberry PI (i.e 4) make sure to find out what pin
controls GPIO 5. And remember that LEDs have polarity:
Write a function setLed that can be used with the following program:
#include ... // MAKE SURE TO INCLUDE THE RIGHT HEADERS
void setLed(int dutyCycle)
{
3
...
// YOUR CODE GOES HERE
// SET LED BRIGHTNESS ACCORDING TO PWM DUTY CYCLE FOR 1 SECOND
// YOU CAN USE MEMORY MAPS OR LINUX DRIVER TO ACCESS GPIO PORT 5
...
}
int main()
{
for (;;) {
setLed(0); // turn off
setLed(50); // half brightness
setLed(100); // full brightness
}
return 0;
}
The program turns off the LED for a second, then sets its brightness to half way for another
second and then it shows full brightness for one more second. This cycle is repeated forever.
The setLed function must take the PWM duty cycle as parameter (percentage of brightness
between 0 and 100). PWM consists of multiple short periods where the LED is ON for a time
proportional to the duty cycle. For example, if duty cycle is 20% and the PWM period is 10
millesconds, the LED is ON for 2 milliseconds and OFF for 8 milliseconds. In one second,
you will have 100 PWM periods. In this case, it is up to you to decide what the PWM
period length should be (you can start trying 10 milliseconds and see whether it looks good
in your scenario). Remember you can use the usleep function to sleep for a small number
of milliseconds. You can use either memory maps or the linux driver to access GPIO port 5
(see class slides for code to set and clear GPIO ports in each case).

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!