Route a UART to its pins from the alternate-function table
Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins. A medium embedded C problem…
medium · Reading datasheets and schematics · datasheets · driver · gpio
Write usart2_pins_init(), which connects USART2 to its pins on port A, working from the Datasheet excerpt below.
Find which pins carry USART2's TX and RX, and which alternate function selects it. Then put both pins in alternate-function mode with that function, make TX push-pull at high speed, and enable the pull-up on RX, as the application note asks.
Change only the bits that belong to those two pins. Everything else on port A, the debug port above all, must keep its value.
Datasheet excerpt
Alternate function mapping (datasheet, port A, excerpt)
| Pin | AF1 | AF2 | AF5 | AF7 | AF8 |
|---|---|---|---|---|---|
| PA2 | TIM2_CH3 | TIM5_CH3 | — | USART2_TX | — |
| PA3 | TIM2_CH4 | TIM5_CH4 | — | USART2_RX | — |
| PA9 | TIM1_CH2 | — | — | USART1_TX | — |
| PA10 | TIM1_CH3 | — | — | USART1_RX | — |
GPIO registers (reference manual)
| Offset | Register | Pin y occupies | Values |
|---|---|---|---|
| 0x00 | MODER | bits 2y+1:2y | 00 input, 01 output, 10 alternate function, 11 analog |
| 0x04 | OTYPER | bit y | 0 push-pull, 1 open-drain |
| 0x08 | OSPEEDR | bits 2y+1:2y | 00 low, 01 medium, 10 high, 11 very high |
| 0x0C | PUPDR | bits 2y+1:2y | 00 none, 01 pull-up, 10 pull-down |
| 0x20 | AFRL | bits 4y+3:4y, pins 0 to 7 | the AF number, 0 to 15 |
| 0x24 | AFRH | bits 4(y−8)+3:4(y−8), pins 8 to 15 | the AF number, 0 to 15 |
Reset values of port A: MODER 0xA8000000, OSPEEDR 0x0C000000, PUPDR 0x64000000 (PA13 and PA14 are the debug port, SWDIO and SWCLK).
Application note: UART pins
Use push-pull at high speed for TX. Enable the pull-up on RX, so that the line idles high (a UART's idle level) while the cable is unplugged, rather than floating and producing framing errors.
Starter code
void usart2_pins_init(void) {
}Example tests
From reset
usart2_pins_init();
show();Expected output:
MODER A80000A0 OTYPER 00000000 OSPEEDR 0C000020 PUPDR 64000040 AFRL 00007700 AFRH 00000000Learn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.