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):
| Resource | What it describes | What it cannot tell you |
|---|---|---|
User Manual (UM10360) | Internal LPC1768 architecture, buses, peripherals, register addresses and bit fields | Which external components sit on the LandTiger board or which pins they connect to |
| Board Schematic | External components (LD4–LD11, buffers, resistors) and exact chip pins wired to each | Register 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 initialization | Physical 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,
APB0andAPB1): 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=0x00000000ULLPC_RAM_BASE=0x10000000ULLPC_GPIO_BASE=0x2009C000ULLPC_APB0_BASE=0x40000000ULLPC_APB1_BASE=0x40080000ULLPC_AHB_BASE=0x50000000ULLPC_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):
PINSEL0sits atbase + 0(0x4002C000)PINSEL1sits atbase + 4(0x4002C004)PINSEL2sits atbase + 8(0x4002C008)PINSEL3sits atbase + 12(0x4002C00C)PINSEL4sits atbase + 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 / Register | Block base | Offset | Resulting address | Source reference |
|---|---|---|---|---|
LPC_PINCON->PINSEL0 | 0x40000000 | + 0x2C000 + 0x00 | 0x4002C000 | Slides 14–17 |
LPC_PINCON->PINSEL4 | 0x40000000 | + 0x2C000 + 0x10 | 0x4002C010 | Slides 16–17, Table 79 |
LPC_PINCON->PINMODE0 | 0x40000000 | + 0x2C000 + 0x40 | 0x4002C040 | Slide 17, Table 79 |
LPC_GPIO2->FIODIR (FIO2DIR) | 0x2009C000 | + 0x00040 + 0x00 | 0x2009C040 | Slides 13, 27, Table 102 |
LPC_GPIO2->FIOMASK (FIO2MASK) | 0x2009C000 | + 0x00040 + 0x10 | 0x2009C050 | Slide 27, Table 102 |
LPC_GPIO2->FIOPIN (FIO2PIN) | 0x2009C000 | + 0x00040 + 0x14 | 0x2009C054 | Slide 27, Table 102 |
LPC_GPIO2->FIOSET (FIO2SET) | 0x2009C000 | + 0x00040 + 0x18 | 0x2009C058 | Slide 27, Table 102 |
LPC_GPIO2->FIOCLR (FIO2CLR) | 0x2009C000 | + 0x00040 + 0x1C | 0x2009C05C | Slide 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:
- Net names
DB00–DB07: CPU pinsP2.0–P2.7(chip pins 75–66) connect to the LED circuit through netsDB00–DB07. - Buffer and current-limiting resistors: The signals pass through buffer IC
U11(74LV244PW) and470Rresistor networks (RN2,RN3) to LEDsLD4–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.) - Reverse LED-to-pin numbering (slide 23):
LD11P2.0LD10P2.1LD9P2.2LD8P2.3LD7P2.4LD6P2.5LD5P2.6LD4P2.7
- Active-high wiring: The cathodes of
LD4–LD11connect toGND. 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.
- Driving a pin to
Contrast: active-high vs. active-low
If a different board wired an LED’s anode to
3.3Vand its cathode toP2.0, the logic would invert (active-low): writing1(3.3V) would leave zero voltage across the LED (OFF), whereas writing0(0V) would allow current to flow and turn it ON. Always check the schematic before assuming1means ON.
Common confusions
- Looking in the chip User Manual for board LEDs: The User Manual documents
P2.0–P2.7and their registers, notLD4–LD11; only the board schematic mapsLD4–LD11ontoP2.7–P2.0. - Treating
RESERVED0[5]as dead code you can omit: Deleting reserved padding shifts all subsequent fields in thestructto wrong physical memory addresses. - Confusing
LD4withP2.0: On LandTiger, the numbering runs in opposite order (LD11isP2.0andLD4isP2.7). - Assuming
1always turns an LED ON:1= ON holds only because LandTiger’s LEDs are wired active-high towardGND.
Retrieval questions with answers
-
Why must
LPC_PINCON_TypeDefincludeuint32_t RESERVED0[5];even though software never reads or writes it?
Eachuint32_tfield advances the byte offset by 4. Because the hardware has a 20-byte gap betweenPINSEL10(0x4002C028) andPINMODE0(0x4002C040), the 5-word (5 × 4 = 20bytes) reserved array keepsPINMODE0and every later field aligned with its real hardware address. -
Compute the physical address of
LPC_GPIO2->FIOCLRfrom the base constants inLPC17xx.h.
LPC_GPIO_BASE(0x2009C000) + Port 2 offset (0x00040) =0x2009C040(LPC_GPIO2_BASE). AddingFIOCLR’s struct offset (0x1C) yields0x2009C05C. -
Suppose another board wires an LED between
3.3VandP2.0. What happens when firmware drivesP2.0to1?
The LED stays OFF, because both sides sit near3.3Vwith no forward voltage drop. DrivingP2.0to0(0V) turns that active-low LED ON. -
Why does the LPC1768 place High-Speed GPIO on the AHB matrix while placing
PINCONonAPB0?
GPIO pins are toggled frequently during execution and benefit from fast AHB access, whereasPINCONselects static pin functions (typically during initialization) and fits on the lower-speed peripheral busAPB0.