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. A medium embedded C problem (Reading datasheets…
medium · Reading datasheets and schematics · datasheets · registers · interrupts
Write int exti_route(char port, int pin, int rising, int falling), which makes edges on pin pin of port port (a letter, such as 'C') set EXTI line pin's pending bit and raise its interrupt. rising and falling say which edges count; a call for a line already in use replaces its port and edges.
Return the interrupt's position in the vector table, which the caller then enables in the NVIC, or -1 without changing anything if the port does not exist on this chip or the pin is not 0 to 15. Work from the Datasheet excerpt below, and change only the bits of the line you route.
Datasheet excerpt
RCC_APB2ENR (reference manual)
| Bit | Field | Description |
|---|---|---|
| 14 | SYSCFGEN | 1: the SYSCFG's clock is on. Its registers ignore writes while it is off |
SYSCFG_EXTICR1 to SYSCFG_EXTICR4 (reference manual)
EXTI line y is connected to pin y of one port at a time. Line y's 4-bit field EXTIy[3:0] is in SYSCFG_EXTICR(y / 4 + 1), at bits 4 × (y mod 4) + 3 to 4 × (y mod 4).
| EXTIy[3:0] | Source |
|---|---|
| 0000 | PA[y] |
| 0001 | PB[y] |
| 0010 | PC[y] |
| 0011 | PD[y] |
| 0100 | PE[y] |
| 0111 | PH[y] |
| others | reserved (this package has no ports F and G) |
EXTI registers (reference manual)
| Offset | Register | Bit y |
|---|---|---|
| 0x00 | IMR | 1: line y's interrupt is not masked |
| 0x08 | RTSR | 1: a rising edge on line y sets its pending bit |
| 0x0C | FTSR | 1: a falling edge on line y sets its pending bit |
Vector table (reference manual, excerpt)
| Position | Acronym | Description |
|---|---|---|
| 6 | EXTI0 | EXTI line 0 interrupt |
| 7 | EXTI1 | EXTI line 1 interrupt |
| 8 | EXTI2 | EXTI line 2 interrupt |
| 9 | EXTI3 | EXTI line 3 interrupt |
| 10 | EXTI4 | EXTI line 4 interrupt |
| 23 | EXTI9_5 | EXTI lines 5 to 9 interrupts |
| 40 | EXTI15_10 | EXTI lines 10 to 15 interrupts |
Starter code
int exti_route(char port, int pin, int rising, int falling) {
SYSCFG->EXTICR[pin / 4] = (uint32_t)(port - 'A') << ((pin % 4) * 4);
if (rising) EXTI->RTSR |= 1u << pin;
if (falling) EXTI->FTSR |= 1u << pin;
EXTI->IMR |= 1u << pin;
return 6 + pin;
}Example tests
The user button on PC13, falling edge
printf("IRQ %d\n", exti_route('C', 13, 0, 1));
show();Expected output:
IRQ 40
EXTICR 0000 0000 0000 0020 IMR 2000 RTSR 0000 FTSR 2000 APB2ENR 4000Learn 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.
- 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.
- 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.
- 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.