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

  1. Review ARM Toolchain and ARM Toolchain and Memory Images for how startup code (Reset_Handler) runs before main().
  2. Study LPC1768 Memory-Mapped Registers and LPC17xx.h for how LPC17xx.h maps chip addresses into C structs and how the LandTiger schematic wires LD4–LD11 to P2.7–P2.0.
  3. Study LPC1768 GPIO Configuration and Bitwise Control for configuring PINSEL4 and FIODIR, toggling FIOSET/FIOCLR, writing << bitmasks, and checking state in Keil uVision.

Source coverage by PDF page

PDF pagesMaterial
1–2Title, CC BY-SA 3.0 license, and the three resources (User Manual, Sample Project, Schematic)
3–5LPC1768 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–7LandTiger board photo/composition and LPC1768 block diagram showing High Speed GPIO on AHB and Pin Connect Block on APB0 (User Manual p. 10)
8–10Sample project structure in Keil (startup_LPC17xx.s, sample.c, system_LPC17xx.c, led/ driver files)
11–194-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–23Why 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–28Pin 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–31Source C code: LED_init(), led_mask[], LED_On(), LED_Off(), all_LED_on(), all_LED_off()
32Software 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)

FileRole
startup_LPC17xx.sAssembly startup: stack and heap allocation, Interrupt Vector Table (IVT), and Reset_Handler (which launches main)
sample.cApplication entry point containing main()
system_LPC17xx.cSystemInit() clock setup; includes system headers such as LPC17xx.h
led.hPeripheral function prototypes for the LED driver
lib_led.cInitialization routines (LED_init())
funct_led.cOperational 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):

  1. Memory block bases: LPC_GPIO_BASE (0x2009C000UL), LPC_APB0_BASE (0x40000000UL).
  2. Peripheral start bases: LPC_GPIO2_BASE (LPC_GPIO_BASE + 0x00040), LPC_PINCON_BASE (LPC_APB0_BASE + 0x2C000).
  3. Register struct (LPC_PINCON_TypeDef) where each __IO uint32_t advances the byte offset by 4 and uint32_t RESERVED0[5] preserves the 20-byte hardware gap between PINSEL10 (0x4002C028) and PINMODE0 (0x4002C040).
  4. Pointer macros (LPC_PINCON, LPC_GPIO2) allowing expressions such as LPC_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 from UM10360 (slide 27) specifies that FIOCLR is Write-Only (WO) and that writing 1 produces a LOW output while writing 0 has no effect (just as all_LED_off() on slide 31 writes 0x000000FF directly). Using = with led_mask[num] (LPC_GPIO2->FIOCLR = led_mask[num];) matches the hardware behavior without reading a write-only register or setting 1s 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 LD4 by reading only the LPC1768 User Manual?
  • How do the four steps in LPC17xx.h translate LPC_PINCON->PINSEL4 into address 0x4002C010?
  • Why does LED_init() use &= 0xFFFF0000 on PINSEL4 (4 hex digits cleared) but |= 0x000000FF on FIODIR (2 hex digits set)?
  • How can you rewrite LPC_PINCON->PINSEL4 &= 0xFFFF0000; and LPC_GPIO2->FIODIR |= 0x000000FF; using << bit shifts?
  • What is wrong with LPC_GPIO2->FIOCLR &= ~led_mask[num]; compared with the FIOCLR specification in Table 102?