microcontroller-firmware
Embedded firmware development for microcontrollers including memory-mapped I/O, GPIO, interrupts, timers, UART/SPI/I2C serial protocols, PWM generation, ADC sampling, interrupt-driven versus polled architectures, RTOS fundamentals, and the bring-up sequence from power-on reset through first printf. Covers AVR/Arduino, ARM Cortex-M, and ESP32 patterns. Use when writing, porting, debugging, or reviewing firmware that directly manipulates hardware peripherals.
git clone --depth 1 https://github.com/Tibsfox/gsd-skill-creator /tmp/microcontroller-firmware && cp -r /tmp/microcontroller-firmware/examples/skills/electronics/microcontroller-firmware ~/.claude/skills/microcontroller-firmwareSKILL.md
# Microcontroller Firmware Firmware is the software layer that sits between hardware peripherals and the application logic. It is simultaneously the lowest-level code most engineers ever write and the most consequential: a firmware bug can brick a board, corrupt sensor data, or cause a motor to spin the wrong direction at full power. This skill covers what every firmware author needs to know before writing their first interrupt handler — the register model, the bring-up sequence, the standard peripheral set, and the decision points where architectural choices become hard to undo. **Agent affinity:** shima (microprocessor architecture and instruction-level behavior), horowitz (practical bring-up and debugging) **Concept IDs:** elec-microcontroller-interfacing, elec-sensor-actuator-systems ## The Microcontroller Model A microcontroller is a single chip containing a CPU core, RAM, nonvolatile program memory (typically flash), and a collection of peripherals (GPIO, timers, UART, SPI, I2C, ADC, PWM, DMA, and others). The CPU, memory, and peripherals share a bus — usually via memory-mapped I/O, where reading and writing specific addresses communicates with specific peripheral registers. **The three regions of memory:** | Region | Typical size | Contents | |---|---|---| | Flash | 16 KB - 2 MB | Program code, constant data, interrupt vectors | | SRAM | 1 KB - 512 KB | Variables, stack, heap if used | | Peripheral | a few KB mapped | GPIO, UART, timer registers | **Memory-mapped I/O.** Writing to address 0x40020014 might set the output latch of GPIO port A. Reading from 0x4001200C might return the status register of a UART. Peripheral addresses are defined in the vendor's device header file and never overlap RAM or flash. ## Technique 1 — The Bring-Up Sequence On power-on reset, a microcontroller performs a fixed sequence before any user code runs: 1. **Hardware reset.** CPU registers initialize, program counter jumps to the reset vector (in flash at a vendor-specific address). 2. **Clock initialization.** The firmware typically switches the CPU from a slow internal oscillator to a faster internal or external oscillator, possibly through a PLL. 3. **Memory initialization.** The `.data` segment is copied from flash to RAM; the `.bss` segment is zeroed. These are usually the first lines of the reset handler, before `main()`. 4. **Peripheral clock enable.** Every peripheral has its clock gated at reset to save power; it must be explicitly enabled before its registers can be accessed. 5. **GPIO configuration.** Pins are configured as input, output, analog, or alternate function, with pull-ups/pull-downs as needed. 6. **Interrupt configuration.** Enable relevant interrupts in the peripheral and in the CPU's interrupt controller (NVIC on ARM Cortex-M). 7. **First printf or LED blink.** The first observable output, confirming the bring-up reached this point. The single most useful first sign of life is a blinking LED. If you can toggle a GPIO in a loop, the reset handler is working, the clock tree is functional, flash is being read, and the GPIO peripheral is clocked. Everything else is incremental. ## Technique 2 — GPIO Basics **Output.** Configure the pin as a push-pull or open-drain output. Push-pull drives both high and low. Open-drain only pulls low; an external pull-up (or another device) supplies the high level. Open-drain is required for wire-OR signaling and for I2C. **Input.** Configure the pin as input. Enable the internal pull-up or pull-down if the external circuit does not guarantee a defined level when nothing is driving the line. Floating inputs are the single largest source of mystery bugs in embedded firmware. **Reading an input.** Read the input data register. The value reflects the instantaneous pin state, not what the output latch last wrote. **Bouncing.** Mechanical switches bounce for several milliseconds when pressed. Debounce in software by either sampling in a timer interrupt and requiring the value to be stable for several consecutive samples, or by using an RC filter and a Schmitt-trigger input. ## Technique 3 — Timers and PWM A timer is a counter that increments (or decrements) on every cycle of its clock. When it reaches a configured limit, it rolls over and optionally fires an interrupt. Timers are the most versatile peripheral in a microcontroller. **Common uses:** - **Periodic tick.** Set the timer to roll over at a known frequency (e.g., 1 kHz) and fire an interrupt. This is the heartbeat for RTOS schedulers and for periodic sensor polling. - **Pulse-width modulation (PWM).** Compare the counter against a configurable threshold. Output is high while counter < threshold, low otherwise. Varying the threshold varies the duty cycle. PWM drives motors, LEDs, and audio at low cost. - **Input capture.** Record the counter value when an external event (edge on a GPIO) occurs. Used for measuring pulse widths, decoding quadrature encoders, and time-stamping events. - **Output compare.** Trigger an action (toggle pin, fire interrupt, DMA request) when the counter matches a configured value. **Worked example.** To generate a 50% duty cycle PWM at 1 kHz from a 16 MHz clock, configure the timer with a prescaler of 1, a period of 16000 (for 1 kHz = 16 MHz / 16000), and a compare threshold of 8000. The output pin is high for 8000 counts and low for 8000 counts, producing 50% at 1 kHz. ## Technique 4 — Serial Protocols: UART, SPI, I2C ### UART Asynchronous, point-to-point, full-duplex. Two wires (TX, RX) plus a common ground. Each byte is framed by a start bit and one or more stop bits; there is no shared clock, so both ends must agree on the baud rate (9600, 115200, 921600, etc.). Simple, widely supported, the default for debug console output. ### SPI Synchronous, multi-peripheral, full-duplex. Four wires (MOSI, MISO, SCK, CS) — chip select selects which peripheral responds. The master generates the clock, so no baud rate negotiation. Fast (10+ MHz common, 10
Major art movements and their historical context for art education. Covers 12 movements from the Renaissance to contemporary art, their defining characteristics, key artists, signature works, and the intellectual/social forces that produced them. Use when analyzing artworks in historical context, understanding stylistic lineages, identifying influences across periods, or connecting studio practice to art-historical precedent.
Color theory principles for art education. Covers the three color properties (hue, saturation, value), color mixing systems (subtractive and additive), color relationships (complementary, analogous, triadic, split-complementary), color temperature, simultaneous contrast and the relativity of color perception, and practical palette construction. Use when analyzing color in artworks, planning color schemes, understanding optical phenomena in painting, or investigating Albers's Interaction of Color experiments.
The creative process in art from idea to exhibition. Covers five phases of creative work (inspiration, incubation, exploration, execution, reflection), sketchbook practice, artist statements, critique methodology (formal and conceptual), portfolio development, and the studio as a working environment. Use when guiding students through project development, facilitating critique sessions, developing artist statements, curating portfolios, or understanding how professional artists structure their creative practice.
Digital art tools, techniques, and workflows for art education. Covers raster and vector workflows, digital painting, photo manipulation, generative and procedural art, 3D modeling and rendering, pixel art, the relationship between traditional skills and digital execution, and ethical considerations of AI-generated imagery. Use when working with digital tools, evaluating digital art, or bridging traditional art concepts into digital practice.
Observational drawing and visual perception techniques for art education. Covers contour drawing, gesture drawing, negative space, proportion and measurement, value mapping, spatial depth cues, and the cognitive shift from symbolic to perceptual seeing. Use when teaching drawing fundamentals, analyzing observational accuracy, or developing visual literacy in any medium.
Three-dimensional art and sculptural thinking for art education. Covers additive and subtractive sculptural processes, armature construction, modeling in clay, carving principles, casting and moldmaking, assemblage and found-object sculpture, installation art as expanded sculpture, and the conceptual transition from pictorial to spatial thinking. Use when working with three-dimensional media, analyzing sculptural form, understanding spatial composition, or investigating the relationship between sculpture and site.
Celestial coordinate systems and sky positioning. Covers horizon (altitude-azimuth), equatorial (right ascension-declination), ecliptic, and galactic systems; epoch and precession; coordinate transformations; planisphere use; and practical sky-locating from any latitude and date. Use when locating objects, planning observations, converting catalog coordinates, or teaching the geometry of the sky.
Observational cosmology from Hubble's law to the CMB. Covers redshift, Hubble expansion, the cosmological parameters, the cosmic microwave background, large-scale structure, galaxy rotation curves and dark matter, Type Ia SNe and dark energy, and the current state of Lambda-CDM. Use when reasoning about the large-scale universe, interpreting cosmological surveys, or teaching the Big Bang evidence chain.