PDI-2024: Classroom exercises
The following exercises are based on the topics covered in class.
Multibit field clear and set
The following figure describe the bits layout of a SPI ctrl register.
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
The LEN
field is made up of bits 23-20.
Assume you have pointer already initialized to the memory where register is mapped uint32_t *pREG = <register address>;
and the following code:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Does the LEN field really change to 3?, What is wrong with the previous code?
UART polling I/O time considerations
Lets have a UART serial line connected to another system with the following configuration:
- 1 bit start, 8 bits data, 1 bit stop, no parity
- 9600 bauds speed
- No flow control
- No receive FIFO
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
What would be the minimum polling period that the software have to carry out in order to avoid overrun errors?
- One bit last 1/9600 = 104,16 us
- The transmision of one data byte needs 10 bits: one bit start, 8 bits data and one bit stop. The time spent to send one data byte is 10 * 104,16 us = 1041,6 us.
- If several consecutive bytes could be received, the uart must be polled at least every 1041,6 us to avoid an overrun error.
UART frame
The figure shows the transmission sequence of a UART according to the following configuration:
- 8-bit data, LSB
- 1 start bit, 1 stop bit, no parity
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
What is the value of the transmitted data?
0x71
TIMER configuration
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Given a SysCLK clock at 100 Mhz and a prescaler value equal to 100:
- Select a timer reload value to obtain a periodic tick of 1 millisecond.
1000
Exercises with time measurements and sensors
Exercise 1
It is desired to obtain the processing time of the function uint16_t calculaCRC( uint8_t data[], uint32_t tam );
which performs the CRC 16 calculation of a sequence of data.
You are requested to:
Code a program that invokes the function 1000 times with a data sequence of size 256 octets and obtain the average, maximum and minimum processing time.
Another version:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
This is called Code Profiling
. Having the execution time of the code labeled is useful to do the system schedulability analysis.
Exercise 2
We wish to obtain the Round-Trip communication time, i.e. how long it takes from the beginning of the communication until the response is received.
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
You are requested to:
Code a program that sends a data via the UART and obtain the time it takes to get the response. Perform this action 1000 times and obtain the average, maximum and minimum Roud-trip time.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
What would be the Round Trip time for communications with a spacecraft located on the Moon?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
What about Mars?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Find information about the Voyager 1 mission and how long it takes to communicate with the spacecraft.
Exercise 3
It is desired to obtain the deferred processing time of an ISR. For example, how much time elapses between queuing a TC and starting its processing with the processTC function.
You are requested to:
Code a program that calculates such time. Perform this action 1000 times and obtain the average, maximum and minimum time.
Exercise 4
We want to estimate the distance to an object. For this purpose we have an ultrasonic sensor that is connected to the GPIO (General Purpose I/O) pins mapped in memory at address 0xFC083000. Specifically Bit 24 is connected to the sensor's terminal that generates the transmission of a pulse and Bit 28 is connected to the sensor's terminal that indicates the reception of an echo.
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Request:
Code a function for RISCV whose prototype is double getDistance()
that returns the distance in cm at which an object is located.
There is a problem with the above solution. The function is blocked if the echo is NOT received, either because the object is too far away (out of range of the sensor) or the receiver has simply broken down.
You are requested to:
Modify the function code so that it waits for the echo signal activation for a maximum of 10 milliseconds. In that case the function will return a value of -1 as error indication.
Exercise 5
It is intended to generate a square signal using Bit 24 of the GPIO (General Purpose I/O) port described in Practice 1.
The timing is as shown below with a fixed duty cycle of 50%:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
For this purpose, we already have the following program snippet:
You are requested to:
Complete the ISR timer_service ISR to generate a periodic signal with the timing shown in the statement. Declare additional variables and macros as appropriate.
Modify the code so that the generated signal is as follows with a fixed duty cycle of 70%:

Exercise 7
Modify exercise 6 so that the duty cycle of the signal can be modified according to the number of times the push buttons on the board are pressed.

The operation will be as follows:
- At startup the program will generate a square signal with a duty cycle of 50%.
- Pressing the first button (BTN0: the rightmost button) will increase the duty cycle by 10% (1 ms) up to a maximum of 90% (9 ms on high and 1 ms on low).
- Pressing the second button (BTN1) will decrease the duty cycle by 10% (1 ms) to a minimum of 10% (1 ms high and 9 ms low).
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
It is only necessary to modify the Super Loop
of the main program. It is about reading the state of the buttons and detecting a rising edge in any of the BTN0/BTN1 buttons. When this happen, the t_high, t_low
variables are incremented/decremented as appropriate, always keeping the 10ms period.