Floor Plan

Memory Mapped I/O in C Exercise

Task

A control panel has an on/off switch and a status lamp that shows whether the switch is on, so the operator can tell the machine's state from across the room. The switch and the lamp are on two different ports of the chip.

Make the lamp follow the switch for as long as the board runs: switch on, lamp on; switch off, lamp off Write one struct type that describes a GPIO port's registers, and reach port A and port C only through it. The registers are on the Datasheet tab, under GPIOA and GPIOC.

Circuit

Block diagram: SW1 on/off panel switch from +3V3 into U1 STM32F4 pin PC13 with no resistor on the net, and pin PA5 driving R1 330R into D1, the status lamp, to ground, with a TP1 logic analyzer probe on the PA5 net

Pass condition

The lamp on PA5 matches the switch on PC13 at all times and never turns on or off by itself, and PC13's pull-down is on. Every register on port A and port C is reached through your struct type, and the struct's registers are volatile. A volatile value is read from or written to the hardware each time the code names it, never kept in a copy.

Expected output: the switch on PC13 turned on once and off again, and the lamp on PA5 following it with two edges, first rising and then falling

Notes

The switch is on PC13 (pin 13 of port C), and the lamp is on PA5 (pin 5 of port A). In each port, MODER sets what each pin does (input or output), PUPDR sets each pin's pull-up or pull-down, IDR shows each pin's level right now (bit 13 is pin 13), and BSRR drives single pins high or low. A pull-down is a weak link to 0 inside the chip. With the switch off, nothing on the board holds PC13 at 0, but a pull-down does once port C's PUPDR turns it on. The Datasheet tab lists each port's registers in order. Each offset counts bytes from the port's base address to that register.

Related challenges

Loading the interactive arena.