© Warren Gay 2018

Warren Gay, Beginning STM32, https://doi.org/10.1007/978-1-4842-3624-6_22

Answers to Exercises

Warren Gay

(1)St. Catharines, Ontario, Canada

Chapter 4

  1. What GPIO port does the built-in LED on the Blue Pill PCB use? Specify the libopencm3 macro name for the port.

    Answer: PORTC

  2. What GPIO pin does the built-in LED on the Blue Pill PCB use? Specify the libopencm3 macro name.

    Answer: GPIO13

  3. What level is required to turn the built-in LED on for the Blue Pill PCB?

    Answer: logic low (or zero volts)

  4. What are two factors affecting the chosen loop count in a programmed delay in non-multitasking environments?

    Answer:

    1. The CPU clock rate

    2. Instruction execution time

  5. Why are programmed delays not used in a multi-tasking environment?

    Answer: Because the timing of other tasks in your system will affect the elapsed time of your programmed delay.

  6. What three factors that affect instruction timing?

    Answer:

    1. the chosen platform

    2. CPU clock rate

    3. execution context (running code in flash or SRAM)

  7. What are the three modes of an input GPIO port?

    Answer:

    1. Analog

    2. Digital, floating

    3. Digital, pull up and pull down

  8. Do the weak pull-up and pull-down resistors participate in an analog input?

    Answer: No

  9. When is the Schmitt trigger enabled for input ports?

    Answer: GPIO or peripheral digital input

  10. Do the weak pull-up and pull-down resistors participate for output GPIO ports?

    Answer: No. They only apply to inputs.

  11. When configuring a USART TX (transmit) output for push/pull operation, which specialization macro should be used?

    Answer: GPIO_CNF_OUTPUT_ALTFN_PUSHPULL

  12. When configuring a pin for LED use, which GPIO mode macro is preferred for low EMI?

    Answer: GPIO_MODE_OUTPUT_2_MHZ (higher-rate choices like GPIO_MODE_OUTPUT_10_MHZ use more current and generate additional EMI).

Chapter 5

Answer the following:

  1. How many tasks are running in blinky2?

    Answer: 1

  2. How many threads of control are operating in blinky2?

    Answer: 2 threads: main thread and task 1

  3. What would happen to the blink rate of blinky2 if the value of configCPU_CLOCK_HZ were configured as 36000000?

    Answer: The blink rate would double because the FreeRTOS scheduler is expecting the CPU to be half as fast.

  4. Where does task 1’s stack come from?

    Answer: Task 1’s stack is allocated from the heap.

  5. Exactly when does task1() begin?

    Answer: when the function vTaskStartScheduler() is called

  6. Why is a message queue needed?

    Answer: to safely communicate between different threads of control

  7. Even though it uses an execution delay loop, why does it seem to work with a nearly 50 percent duty cycle?

    Answer: Because there is only one task executing, the timing remains fairly consistent.

  8. How difficult is it to estimate how long the LED on PC13 is on for? Why?

    Answer: Difficult due to instruction timing, flash prefetch, and so on

  9. Using a scope, measure the on and off times of PC13 (or count how many blinks per second and compute the inverse). How many milliseconds is the LED on for?

    Answer: 84 ms

  10. If another task were added to this project that consumed most of the CPU, how would the blink rate be affected?

    Answer: The blink rate would slow considerably.

  11. Add to the file main.c a task 2 that does nothing but execute __asm__("nop") in a loop. Create that task in main() prior to starting the scheduler. How did that impact the blink rate? Why?

    Answer: It slowed considerably because the second task was consuming CPU time away from the first task.

Chapter 6

  1. What is the idle state of the TTL level of a USART signal?

    Answer: High (near 5 volts)

  2. USART data is provided in a big or little endian sequence?

    Answer: little endian (least significant bit first)

  3. What clock(s) must be enabled for UART use?

    Answer: RCC_GPIOx and RCC_USARTn

  4. What does the abbreviation 8N1 stand for?

    Answer: 8 bits of data, no parity, and 1 stop bit.

  5. What happens if you provide UART data to be sent if the device is not yet empty?

    Answer: Data is lost.

  6. Can tasks be created before, after, or before and after vTaskStartScheduler()?

    Answer: Before and after

  7. What is the minimum buffer size determined by for xQueueReceive()?

    Answer: The receiving buffer size must meet or exceed the item size as was specified when the queue was created.

  8. How do you specify that xQueueSend() should return immediately if the queue is full?

    Answer: Supply argument xTicksToWait with the value 0.

  9. How do you specify that xQueueReceive() should block forever if the queue is empty?

    Answer: Supply argument xTicksToWait with the macro portMAX_DELAY.

  10. What happens to the task if xQueueReceive() finds the queue empty and it must wait?

    Answer: The task will yield to another task.

Chapter 7

  1. What GPIO preparation is necessary before enabling the USB peripheral?

    Answer: The GPIOA clock must be enabled, but otherwise the USB peripheral takes over PA11 and PA12 automatically.

  2. What are the alternate GPIO configurations available for USB?

    Answer: There are no alternate configurations for USB. Only PA11 and PA12 are used.

  3. What libopencm3 routine must be called regularly to handle USB events?

    Answer: The routine usbd_poll() must be called frequently to handle events that require action.

Chapter 8

  1. How many data lines are used by SPI in bidirectional links? What are their signal names?

    Answer: There are two data lines used by SPI bidirectional links: MOSI and MISO.

  2. Where does the clock originate from?

    Answer: The clock (SCK) is always provided by the master of the SPI bus.

  3. What voltage levels are used for SPI signaling?

    Answer: The voltage levels used are usually 5 volts or 3.3 volts, according to the system design requirements. The STM32 device will use 3.3 volts.

  4. Why must a pull-up resistor be used for the STM32 /NSS line?

    Answer: A pull-up resistor for /NSS must be used because the STM32 MCU configures the output as an open-drain output, regardless of how it was initially configured. Without the pull-up resistor, the select line will never go high.

  5. Why must a dummy value be sent in some SPI transactions?

    Answer: A dummy value is written to cause the master peripheral to emit the clock pulses necessary for the slave to send its data. The slave always depends upon the SPI master to provide the clock.

Chapter 9

  1. In the structure typedef'ed as s_overlay, why are members defined as character pointers rather than long int?

    Answer: When the byte size is calculated, you need character pointers. If the type were long int, then the calculated size would be in words instead of bytes.

  2. Why was the xflash memory region added to the linker script?

    Answer: The xflash region was created to hold all of the W25QXX flash code, which will not appear in the MCU’s flash. Additionally, this code is loaded into the xflash at starting address of zero, whereas the MCU flash started at 0x08000000 instead.

  3. What is the purpose of the overlay stub function?

    Answer: The stub function calls the overlay manager to make sure the required code is loaded into the overlay region in SRAM. Once the function pointer is known, it must then pass on the calling arguments and return values, if any.

  4. In the Gnu declaration __attribute__((noinline, section(".ov_fee"))), what is the purpose of noinline? Why is it needed?

    Answer: The attribute noinline prevents the compiler from treating the function as “inline” code. This is especially important for small functions that the compiler may optimize.

  5. Where does the declaration __attribute((section("…")) belong?

    Answer: The __attribute__((section("…")) declaration may only appear in the function prototype.

Chapter 10

  1. What are the three possible interrupt events from the RTC?

    Answer: The three interrupt sources are RTC (tick), Alarm, and Overflow.

  2. What is the purpose of the calls taskENTER_CRITICAL_FROM_ISR and taskEXIT_CRITICAL_FROM_ISR?

    Answer: The taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() calls block other interrupts from occurring while performing a critical operation.

  3. How many bits wide is the RTC counter?

    Answer: The RTC counter is 32 bits wide.

  4. Which clock source continues when the STM32 is powered down?

    Answer: The LSE clock (32.768 kHz crystal oscillator), which continues to work even when the supply voltage is off, provided that the battery voltage V BAT supply is maintained

  5. Which is the most accurate clock source?

    Answer: The most accurate clock source is the HSE clock because it is controlled by an 8 MHz crystal oscillator, but only while power is maintained.

Chapter 11

  1. What is the byte value sent when reading from slave address $21 (hexadecimal)?

    Answer: Hexadecimal address $21 is $42 when shifted left by one bit. A read operation requires a 1-bit in the least significant position, which results in a byte value of $43.

  2. When the master requests a response from a non-existing slave device on the bus, how does the NAK get received?

    Answer: To ACK a response, the slave must pull the SDA line low. If there is no slave acknowledging, the pull-up resistor keeps the line high, causing the NAK to be received by default.

  3. What is the advantage of the /INT line from the PCF8574?

    Answer: The /INT line allows the slave device to notify the controlling MCU directly if an input line changes state. Otherwise, the MCU would need to busy the I2C bus with read requests to see when the line changes.

  4. What does quasi-bidirectional mean in the context of the PCF8574?

    Answer: To receive an input signal, the GPIO port needs to be set weakly high so that an input driver can pull it low. This effectively makes it an input or an output port. However, if the GPIO port is set low, it cannot be used for input. For this reason, it is considered quasi-bidirectional.

  5. What is the difference between sourcing and sinking current?

    Answer: When current is sourced, it is controlled and flows from the positive side through a load connected to ground (negative). When sinking current, the load is attached to the positive rail and current is switched on and off at the ground end instead.

Chapter 12

  1. For AFIO output pins, what GPIO configuration macros must be used?

    Answer: GPIO outputs must use GPIO_CNF_OUTPUT_ALTFN_PUSHPULL or GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN macros or the pin will remain unconnected to the peripheral (it will act as a regular GPIO).

  2. What clock must be enabled for AFIO changes?

    Answer: The RCC_AFIO clock must be enabled by rcc_periph_clock_enable().

  3. What GPIO configuration macros be used for input pins?

    Answer: Inputs require no special treatment other than having the AFIO peripheral clock and the GPIO clock enabled, and the peripheral’s needing the input initialized.

  4. What is the purpose of the OLED D/C input serve?

    Answer: The D/C input line (to the OLED) allows it to distinguish between OLED command bytes (when low) and OLED graphics data (when high).

Chapter 13

  1. In the demo program, what DMA controller aspects had to be changed before starting the next transfer?

    Answer: The start address and length were changed after the DMA channel was disabled.

  2. Does each DMA channel have its own ISR routine?

    Answer: Yes, each DMA channel has its own ISR.

  3. In a memory-to-peripheral transfer, like the demo, where does the DMA request come from?

    Answer: The DMA request comes from the peripheral, except in a memory-to-memory transfer. In the demo program, the SPI transmit buffer empty flag signaled the need for a transfer.

  4. In the demo program where SPI was used, what were the three conditions necessary before a DMA transfer could begin?

    Answer: In the demo program where SPI was used, the following were necessary to cause the DMA to begin:

    1. The DMA channel must be enabled.

    2. The DMA TX enable for SPI must be enabled.

    3. SPI must be enabled.

Chapter 14

  1. How is the internal STM32 temperature represented?

    Answer: As a voltage

  2. How does GPIO_CNF_INPUT_ANALOG differ from the value GPIO_CNF_INPUT_PULL_UPDOWN or GPIO_CNF_INPUT_FLOAT?

    Answer: The configuration value GPIO_CNF_INPUT_ANALOG allows a varying voltage to reach the ADC input. Otherwise, only a low or high signal would be sensed.

  3. If PCLK has a frequency of 36 MHz, what would be the ADC clock rate be when configured with a prescale divisor of 4?

    Answer: The ADC clock would be 36 MHz ÷ 4, which is 9 MHz.

  4. Name three configuration options that affect the total power consumed by ADC.

    Answer: Three factors that affect power consumption for ADC are:

    1. adc_power_on(adc) (and off)

    2. adc_enable_temperature_sensor() (and disable)

    3. adc_start_conversion_direct(adc)

  5. Assuming that the ADC clock after the prescaler is 12 MHz, how long does the ADC_SMPR_SMP_41DOT5CYC configured sample take?

    Answer: (41.5 + 12.5) ÷ 12 MHz = 54 ÷ 12e6 = 4.5 μs.

Chapter 15

  1. What is the advantage of an RC clock?

    Answer: An RC clock requires no external crystal (crystals are too large to include on an IC).

  2. What is the disadvantage of an RC clock?

    Answer: An RC clock is prone to drift and jitter and is less stable. It is also less precise, leading to problems with generating baud rates and so forth.

  3. What is the advantage of a crystal-derived clock?

    Answer: A crystal-controlled clock is stable and can match external hardware. This makes it more ideal for generating baud rates and so forth.

  4. What is the PLL used for?

    Answer: The PLL is used to multiply a clock to a rate higher than its input clock.

  5. What does AHB stand for?

    Answer: AHB stands for AMBA High-performance Bus.

  6. Why must the GPIO PA8 be configured with GPIO_CNF_OUTPUT_ALTFN_PUSHPULL?

    Answer: Without the ALTFN in the macro name, the GPIO would remain disconnected and otherwise be a normal GPIO having nothing to do with MCO output.

Chapter 16

  1. In a RC Servo signal, what is the period of the signal?

    Answer: The period of a PWM signal is the time between the start of the pulse and the start of the next.

  2. Why is the timer input clock frequency 72 Mhz on the Blue Pill STM32F103C8T6? Why isn’t it 36 MHz?

    Answer: The input frequency to the timer is 72 MHz (for the Blue Pill STM32) because when the AHB1 prescaler is not equal to one, the timer frequency is the AHB1 bus frequency doubled.

  3. What is changed in the timer to effect a change in the pulse width?

    Answer: The value of the output compare register

Chapter 17

  1. Why does the timer have a digital filter available on its inputs?

    Answer: The digital filter eliminates false triggering from random noise pulses.

  2. When does the timer reset in PWM input mode?

    Answer: As configured in the demo of Chapter 17, the counter resets after the capture 1 event occurs.

  3. Where does the IC2 input signal come from in PWM input mode?

    Answer: In PWM input mode, the IC2 input comes from input channel 1.

Chapter 19

  1. How many FIFO’s are supported by the STM32F103 CAN peripheral?

    Answer: There are two FIFOs in the CAN peripheral (FIFO 0 and FIFO 1).

  2. How many filter banks are supported by the CAN peripheral?

    Answer: There are two filter banks in the CAN peripheral (banks 0 and 1).

  3. When a pair of filters must be supplied, but only one is needed, what are two ways to accomplish this?

    Answer: You can supply one filter when a pair are required in one of two ways:

    1. Declare two identical filters (only one will trigger)

    2. Declare one good filter and one impossible filter.

  4. What is the RTR flag and what is its purpose?

    Answer: The RTR (remote transmission request) flag is used to request a transmission when it is sent in the recessive state. The reply is always sent with the RTR flag in the dominant state.

Chapter 20

  1. What does the make macro BINARY define?

    Answer: The BINARY macro defines the name of the application executable with the .elf suffix attached.

  2. What is the purpose of the header file FreeRTOSConfig.h?

    Answer: The header file FreeRTOSConfig.h configures several aspects of the FreeRTOS system.

  3. How do you add compiler option -O3 only to the compile of module speedy.c?

    Answer: In the Makefile, add the following rule: speedy.o: CFLAGS += -O3

  4. What is the main disadvantage of using heap_1?

    Answer: The main disadvantage of using heap_1.c in a FreeRTOS project is that the function free() is not supported. No dynamically allocated memory can be released and reused.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.169