Course: Computer Architectures
Source: R. Ferrero and P. Bernardi, Politecnico di Torino (Dipartimento di Automatica e Informatica — DAUIN), NXP LPC1768: led, 32 PDF pages, licensed under CC BY-SA 3.0. Supplied directly as a PDF during the study session (unnumbered slide deck; references cite NXP UM10360 LPC176x/5x User Manual Rev. 4.1, 19 December 2016, and the LandTiger board schematic). The notes below distinguish supplied source material from added study explanations.
Why this lesson matters
Writing embedded C for a microcontroller means driving physical pins without an operating system layer in between. This lesson connects three separate artifacts—the chip User Manual, the sample Keil project (LPC17xx.h), and the board schematic—to trace the complete path from a C line (LPC_PINCON->PINSEL4 &= 0xFFFF0000;) down to turning LEDs LD4–LD11 on and off on the LandTiger board.
Learning route
- Review ARM Toolchain and ARM Toolchain and Memory Images for how startup code (
Reset_Handler) runs beforemain(). - Study LPC1768 Memory-Mapped Registers and LPC17xx.h for how
LPC17xx.hmaps chip addresses into C structs and how the LandTiger schematic wiresLD4–LD11toP2.7–P2.0. - Study LPC1768 GPIO Configuration and Bitwise Control for configuring
PINSEL4andFIODIR, togglingFIOSET/FIOCLR, writing<<bitmasks, and checking state in Keil uVision.
Source coverage by PDF page
| PDF pages | Material |
|---|---|
| 1–2 | Title, CC BY-SA 3.0 license, and the three resources (User Manual, Sample Project, Schematic) |
| 3–5 | LPC1768 features (ARM Cortex-M3, MPU, up to 100 MHz, NVIC, 512 kB Flash, 64 kB SRAM, 8-ch DMA, AHB/APB, serial interfaces and peripherals; User Manual p. 6) |
| 6–7 | LandTiger board photo/composition and LPC1768 block diagram showing High Speed GPIO on AHB and Pin Connect Block on APB0 (User Manual p. 10) |
| 8–10 | Sample project structure in Keil (startup_LPC17xx.s, sample.c, system_LPC17xx.c, led/ driver files) |
| 11–19 | 4-step LPC17xx.h memory-mapped register mechanism (BASE constants, peripheral offsets, LPC_PINCON_TypeDef, pointer cast, PINSEL4 example; User Manual pp. 14, 16, 117) |
| 20–23 | Why the board schematic is required and tracing LD11–LD4 to P2.0–P2.7 (DB00–DB07, U11 74LV244PW, 470R resistors, 1 = on, 0 = off) |
| 24–28 | Pin multiplexing and direction: PINSEL4 (P2[15:0], 00 = GPIO, User Manual pp. 114, 120) and Fast GPIO registers FIODIR, FIOMASK, FIOPIN, FIOSET, FIOCLR (pp. 132–133) |
| 29–31 | Source C code: LED_init(), led_mask[], LED_On(), LED_Off(), all_LED_on(), all_LED_off() |
| 32 | Software debugging in Keil uVision (Peripherals -> GPIO Fast Interface -> Port 2) |
Chip and board overview (familiarity and supporting context)
- Microcontroller (slides 3–5): NXP LPC1768, a 32-bit ARM Cortex-M3 SoC running up to 100 MHz with an MPU, Nested Vectored Interrupt Controller (NVIC), 512 kB Flash ROM, 64 kB SRAM, 8-channel general-purpose DMA, Ethernet MAC, USB 2.0, four UARTs, CAN 2.0B, I²C, SPI/SSP, I²S, 70 GPIO pins (100-pin package), 12-bit ADC, 10-bit DAC, four 32-bit timers, PWM, RTC, Watchdog, System Tick and Repetitive Interrupt Timer.
- Bus placement (slide 7): High Speed GPIO sits on the Multilayer AHB Matrix for fast access, whereas the Pin Connect Block (
PINCON) sits on APB0 (APB slave group 0). - LandTiger board (slide 6): Exposes the LPC1768 alongside power, JTAG, serial ports, USB, Ethernet, speaker, buttons/keys and the 8-LED bank (
LD4–LD11).
Sample project file roles (slides 9–10)
| File | Role |
|---|---|
startup_LPC17xx.s | Assembly startup: stack and heap allocation, Interrupt Vector Table (IVT), and Reset_Handler (which launches main) |
sample.c | Application entry point containing main() |
system_LPC17xx.c | SystemInit() clock setup; includes system headers such as LPC17xx.h |
led.h | Peripheral function prototypes for the LED driver |
lib_led.c | Initialization routines (LED_init()) |
funct_led.c | Operational routines (LED_On(), LED_Off(), all_LED_on(), all_LED_off()) |
Memory-mapped registers and board schematic (slides 11–23)
LPC17xx.h converts hardware addresses into C struct accesses in four steps (detailed in LPC1768 Memory-Mapped Registers and LPC17xx.h):
- Memory block bases:
LPC_GPIO_BASE(0x2009C000UL),LPC_APB0_BASE(0x40000000UL). - Peripheral start bases:
LPC_GPIO2_BASE(LPC_GPIO_BASE + 0x00040),LPC_PINCON_BASE(LPC_APB0_BASE + 0x2C000). - Register
struct(LPC_PINCON_TypeDef) where each__IO uint32_tadvances the byte offset by 4 anduint32_t RESERVED0[5]preserves the 20-byte hardware gap betweenPINSEL10(0x4002C028) andPINMODE0(0x4002C040). - Pointer macros (
LPC_PINCON,LPC_GPIO2) allowing expressions such asLPC_PINCON->PINSEL4.
Only the board schematic (not the User Manual) shows how the 8 LEDs connect to the CPU: nets DB00–DB07 link P2.0–P2.7 through buffer U11 (74LV244PW) and 470R resistors to LD11–LD4 (in reverse order: LD11 on P2.0, down to LD4 on P2.7). Because the LEDs return to GND, they are active-high: 1 turns an LED on, 0 turns it off.
Source LED driver code (slides 29–31)
Configuring and driving Port 2 LEDs follows three steps (detailed in LPC1768 GPIO Configuration and Bitwise Control):
/* lib_led.c (slide 29) */
void LED_init(void) {
// PIN mode GPIO: P2.0-P2.7 are set to zero (2 bits per pin -> bits 15:0)
LPC_PINCON->PINSEL4 &= 0xFFFF0000;
// P2.0-P2.7 on PORT2 defined as Output (1 bit per pin -> bits 7:0)
LPC_GPIO2->FIODIR |= 0x000000FF;
}/* funct_led.c (slides 30-31) */
const unsigned long led_mask[] = { 1UL<<0, 1UL<<1, 1UL<<2, 1UL<<3,
1UL<<4, 1UL<<5, 1UL<<6, 1UL<<7 };
void LED_On(unsigned int num) {
LPC_GPIO2->FIOSET |= led_mask[num];
led_value = LPC_GPIO2->FIOPIN;
}
void LED_Off(unsigned int num) {
LPC_GPIO2->FIOCLR &= ~led_mask[num]; /* see hardware qualification below */
led_value = LPC_GPIO2->FIOPIN;
}
void all_LED_on(void) { LPC_GPIO2->FIOSET = 0x000000FF; }
void all_LED_off(void) { LPC_GPIO2->FIOCLR = 0x000000FF; }Source qualification on
LED_Off(slide 30 vs. slide 27 / slide 31)Slide 30 writes
LPC_GPIO2->FIOCLR &= ~led_mask[num];. However, Table 102 fromUM10360(slide 27) specifies thatFIOCLRis Write-Only (WO) and that writing1produces a LOW output while writing0has no effect (just asall_LED_off()on slide 31 writes0x000000FFdirectly). Using=withled_mask[num](LPC_GPIO2->FIOCLR = led_mask[num];) matches the hardware behavior without reading a write-only register or setting1s on other bits.
Software debugging view (slide 32)
Without physical hardware, Keil uVision’s Peripherals -> GPIO Fast Interface -> Port 2 window displays live checkboxes for bits 31:0 of FIO2DIR, FIO2MASK, FIO2SET, FIO2CLR, FIO2PIN and Pins, allowing step-by-step verification of LED_init(), all_LED_on() (0x000000FF) and all_LED_off().
Review questions
- Why can you not find out which pin controls
LD4by reading only the LPC1768 User Manual? - How do the four steps in
LPC17xx.htranslateLPC_PINCON->PINSEL4into address0x4002C010? - Why does
LED_init()use&= 0xFFFF0000onPINSEL4(4 hex digits cleared) but|= 0x000000FFonFIODIR(2 hex digits set)? - How can you rewrite
LPC_PINCON->PINSEL4 &= 0xFFFF0000;andLPC_GPIO2->FIODIR |= 0x000000FF;using<<bit shifts? - What is wrong with
LPC_GPIO2->FIOCLR &= ~led_mask[num];compared with theFIOCLRspecification in Table 102?