Course: Computer Architectures
Source lesson: NXP LPC1768 LED and GPIO

1. Why three separate resources are required

On a desktop computer, an operating system hides hardware behind drivers. On a bare-metal microcontroller (SoC — System on a Chip, here the NXP LPC1768), C code reads and writes hardware circuits directly. Neither the chip manual nor the code alone tells you everything needed to control an external device such as an LED on the LandTiger board (slides 2, 8, 20–21):

ResourceWhat it describesWhat it cannot tell you
User Manual (UM10360)Internal LPC1768 architecture, buses, peripherals, register addresses and bit fieldsWhich external components sit on the LandTiger board or which pins they connect to
Board SchematicExternal components (LD4–LD11, buffers, resistors) and exact chip pins wired to eachRegister offsets, bit meanings or C struct names
Sample Project (LPC17xx.h, startup and driver files)C definitions mapping numeric register addresses to readable names and boot/clock initializationPhysical board wiring or undocumented hardware quirks

Common misconception

Searching the NXP LPC1768 User Manual for “LED 4” will never succeed. The User Manual describes only the silicon chip; only the board schematic reveals which physical pins the LandTiger designer wired to the LEDs (slide 21).

2. Bus placement inside the LPC1768

According to the LPC1768 block diagram (User Manual p. 10, slide 7), internal blocks connect over two main bus levels:

  • Multilayer AHB Matrix (Advanced High-performance Bus): the fast system interconnect linking the ARM Cortex-M3 core, Flash, SRAM, DMA and High Speed GPIO. Keeping GPIO on AHB allows fast pin reads and writes.
  • APB (Advanced Peripheral Bus, APB0 and APB1): bridged from AHB for lower-speed peripheral control and configuration. The Pin Connect Block (PINCON), which chooses the active function of each chip pin, sits on APB0.

3. The 4-step LPC17xx.h memory-mapped register pattern

Every hardware control block inside the LPC1768 exposes memory-mapped registers: fixed 32-bit addresses in the processor’s 4 GB address space where reads and writes manipulate hardware switches rather than ordinary RAM. Rather than scattering raw hexadecimal pointer casts across C code, LPC17xx.h builds readable access in four layers (slides 11–18):

1. Block base address      (LPC_APB0_BASE = 0x40000000UL)
        ↓ + peripheral offset
2. Peripheral base address (LPC_PINCON_BASE = LPC_APB0_BASE + 0x2C000)
        ↓ mapped onto
3. C register struct       (LPC_PINCON_TypeDef with 4-byte uint32_t fields)
        ↓ cast to pointer macro
4. Peripheral handle       (#define LPC_PINCON ((LPC_PINCON_TypeDef *) LPC_PINCON_BASE))

Step 1: Memory block base addresses (slides 11–12)

LPC17xx.h defines constants matching Table 3 of the User Manual (p. 14):

  • LPC_FLASH_BASE = 0x00000000UL
  • LPC_RAM_BASE = 0x10000000UL
  • LPC_GPIO_BASE = 0x2009C000UL
  • LPC_APB0_BASE = 0x40000000UL
  • LPC_APB1_BASE = 0x40080000UL
  • LPC_AHB_BASE = 0x50000000UL
  • LPC_CM3_BASE = 0xE0000000UL

Step 2: Peripheral start addresses (slides 13–15)

Each peripheral starts at a fixed byte offset from its block base:

  • GPIO ports (slide 13):
    • LPC_GPIO0_BASE = LPC_GPIO_BASE + 0x00000 (0x2009C000)
    • LPC_GPIO1_BASE = LPC_GPIO_BASE + 0x00020 (0x2009C020)
    • LPC_GPIO2_BASE = LPC_GPIO_BASE + 0x00040 (0x2009C040)
    • LPC_GPIO3_BASE = LPC_GPIO_BASE + 0x00060 (0x2009C060)
    • LPC_GPIO4_BASE = LPC_GPIO_BASE + 0x00080 (0x2009C080)
  • APB0 Pin Connect Block (slides 14–15, User Manual Table 4 p. 16):
    • LPC_PINCON_BASE = LPC_APB0_BASE + 0x2C000 (0x4002C000, APB0 peripheral 11)

Step 3: C struct layout and reserved padding (slides 16–17)

A C typedef struct lists the peripheral’s registers in exact ascending address order. In LPC_PINCON_TypeDef, each register is declared as __IO uint32_t (a volatile 32-bit unsigned integer occupying 4 bytes):

  • PINSEL0 sits at base + 0 (0x4002C000)
  • PINSEL1 sits at base + 4 (0x4002C004)
  • PINSEL2 sits at base + 8 (0x4002C008)
  • PINSEL3 sits at base + 12 (0x4002C00C)
  • PINSEL4 sits at base + 16 (+0x10 = 0x4002C010)

After PINSEL10 (at 0x4002C028), the next hardware register in Table 79 (User Manual p. 117, slide 17) is PINMODE0 at 0x4002C040. Between 0x4002C02C (the byte right after PINSEL10) and 0x4002C040 there is a 20-byte unimplemented gap (0x4002C040 - 0x4002C02C = 0x14 = 20 bytes). LPC_PINCON_TypeDef fills that gap with:

uint32_t RESERVED0[5];   /* 5 * 4 bytes = 20 bytes of spacer */

Without RESERVED0[5], the compiler would place PINMODE0 at 0x4002C02C instead of 0x4002C040, and every register below it in the struct would silently access the wrong hardware address.

Step 4: Pointer-cast macro (slides 18–19)

Finally, LPC17xx.h casts each numeric base address into a pointer to its register struct:

#define LPC_GPIO2   ((LPC_GPIO_TypeDef   *) LPC_GPIO2_BASE)
#define LPC_PINCON  ((LPC_PINCON_TypeDef *) LPC_PINCON_BASE)

In C code, writing LPC_PINCON->PINSEL4 &= 0xFFFF0000; dereferences the struct pointer at 0x4002C000, adds +0x10, and performs a read-modify-write on the 32-bit hardware register at 0x4002C010.

Analogy and limit: Think of the 4 steps as addressing a row of identically sized mailboxes in a building: street address (block base) floor number (peripheral offset) blueprint listing every 4-unit slot in order, including empty spacer slots (struct + RESERVED0) a nickname pointing to the first mailbox on that floor (LPC_PINCON). Limit of the analogy: unlike passive mailboxes or normal RAM, hardware registers are qualified with __IO (volatile) because reading or writing them immediately triggers electrical behavior and their values can change outside the CPU’s instruction stream, so the compiler must never optimize away or cache accesses.

4. Concrete address derivation table (Port 2 LEDs and PINCON)

Peripheral / RegisterBlock baseOffsetResulting addressSource reference
LPC_PINCON->PINSEL00x40000000+ 0x2C000 + 0x000x4002C000Slides 14–17
LPC_PINCON->PINSEL40x40000000+ 0x2C000 + 0x100x4002C010Slides 16–17, Table 79
LPC_PINCON->PINMODE00x40000000+ 0x2C000 + 0x400x4002C040Slide 17, Table 79
LPC_GPIO2->FIODIR (FIO2DIR)0x2009C000+ 0x00040 + 0x000x2009C040Slides 13, 27, Table 102
LPC_GPIO2->FIOMASK (FIO2MASK)0x2009C000+ 0x00040 + 0x100x2009C050Slide 27, Table 102
LPC_GPIO2->FIOPIN (FIO2PIN)0x2009C000+ 0x00040 + 0x140x2009C054Slide 27, Table 102
LPC_GPIO2->FIOSET (FIO2SET)0x2009C000+ 0x00040 + 0x180x2009C058Slide 27, Table 102
LPC_GPIO2->FIOCLR (FIO2CLR)0x2009C000+ 0x00040 + 0x1C0x2009C05CSlide 27, Table 102

(Note: Table 102 on slide 27 lists FIO2DIR through FIO2CLR using Port 2’s addresses 0x2009C040–0x2009C05C.)

5. Reading the LandTiger LED schematic (slides 21–23)

Inspecting the LandTiger schematic establishes both pin mapping and electrical polarity:

  1. Net names DB00–DB07: CPU pins P2.0–P2.7 (chip pins 75–66) connect to the LED circuit through nets DB00–DB07.
  2. Buffer and current-limiting resistors: The signals pass through buffer IC U11 (74LV244PW) and 470R resistor networks (RN2, RN3) to LEDs LD4–LD11. (Added electrical clarification: the buffer drives the LED current so the microcontroller pins do not carry the full load, and the 470 resistors limit current through the diodes.)
  3. Reverse LED-to-pin numbering (slide 23):
    • LD11 P2.0
    • LD10 P2.1
    • LD9 P2.2
    • LD8 P2.3
    • LD7 P2.4
    • LD6 P2.5
    • LD5 P2.6
    • LD4 P2.7
  4. Active-high wiring: The cathodes of LD4–LD11 connect to GND. Therefore:
    • Driving a pin to 1 (HIGH) creates a voltage drop toward ground LED ON.
    • Driving a pin to 0 (LOW) leaves both sides at ground potential LED OFF.

Contrast: active-high vs. active-low

If a different board wired an LED’s anode to 3.3V and its cathode to P2.0, the logic would invert (active-low): writing 1 (3.3V) would leave zero voltage across the LED (OFF), whereas writing 0 (0V) would allow current to flow and turn it ON. Always check the schematic before assuming 1 means ON.

Common confusions

  • Looking in the chip User Manual for board LEDs: The User Manual documents P2.0–P2.7 and their registers, not LD4–LD11; only the board schematic maps LD4–LD11 onto P2.7–P2.0.
  • Treating RESERVED0[5] as dead code you can omit: Deleting reserved padding shifts all subsequent fields in the struct to wrong physical memory addresses.
  • Confusing LD4 with P2.0: On LandTiger, the numbering runs in opposite order (LD11 is P2.0 and LD4 is P2.7).
  • Assuming 1 always turns an LED ON: 1 = ON holds only because LandTiger’s LEDs are wired active-high toward GND.

Retrieval questions with answers

  1. Why must LPC_PINCON_TypeDef include uint32_t RESERVED0[5]; even though software never reads or writes it?
    Each uint32_t field advances the byte offset by 4. Because the hardware has a 20-byte gap between PINSEL10 (0x4002C028) and PINMODE0 (0x4002C040), the 5-word (5 × 4 = 20 bytes) reserved array keeps PINMODE0 and every later field aligned with its real hardware address.

  2. Compute the physical address of LPC_GPIO2->FIOCLR from the base constants in LPC17xx.h.
    LPC_GPIO_BASE (0x2009C000) + Port 2 offset (0x00040) = 0x2009C040 (LPC_GPIO2_BASE). Adding FIOCLR’s struct offset (0x1C) yields 0x2009C05C.

  3. Suppose another board wires an LED between 3.3V and P2.0. What happens when firmware drives P2.0 to 1?
    The LED stays OFF, because both sides sit near 3.3V with no forward voltage drop. Driving P2.0 to 0 (0V) turns that active-low LED ON.

  4. Why does the LPC1768 place High-Speed GPIO on the AHB matrix while placing PINCON on APB0?
    GPIO pins are toggled frequently during execution and benefit from fast AHB access, whereas PINCON selects static pin functions (typically during initialization) and fits on the lower-speed peripheral bus APB0.