# Phil's Lab #65 - KiCad 6 STM32 PCB Design Full Tutorial https://youtu.be/aVUqaB0IMh4 --- Good for moving away from Arduinos as modules and make something more professional. Design is based loosely on the STM32 Blue Pill This will be the "Black Pill" STM32 F103C8T6 [[STM32 Naming Conventions]] ![[model_A.avif]] https://www.alldatasheet.com/view.jsp?Searchword=STM32F #### STM32 Schematic Symbol Datablock ``` STM32F103C8Tx Derived from STM32F103C_8-B_Tx (STMicroelectronics Arm Cortex-M3 MCU, 64-128KB flash, 20KB RAM, 72 MHz, 2.0-3.6V, 37 GPIO, LQFP48) STMicroelectronics Arm Cortex-M3 MCU, 64KB flash, 20KB RAM, 72 MHz, 2.0-3.6V, 37 GPIO, LQFP48 Keywords: Arm Cortex-M3 STM32F1 STM32F103 Reference U? Footprint Package_QFP:LQFP-48_7x7mm_P0.5mm Datasheet https://www.st.com/resource/en/datasheet/stm32f103c8.pdf Description STMicroelectronics Arm Cortex-M3 MCU, 64KB flash, 20KB RAM, 72 MHz, 2.0-3.6V, 37 GPIO, LQFP48 ``` ### Notes #### uC Power Wiring - Can hide the `GND` designator, since it's redundant - Generally, you want to tie VSS and VSSA to the same ground - 2.7V - 3.6V inputs for VDD - Just use 3.3V unless you have a good reason otherwise - VBAT for RTC or external batteries - If not, just tie to VDD - Decoupling Capacitors are there for peak, transient loads - Energy takes a long time to get to the board from the supply, by comparison - 1x100nF per VBAT or VDD pin. and a single 1uF bulk decoupling cap. - Put the decoupling capacitors connected to the VDD pin network on the schematic so it's easy to understand that they should be placed near the capacitor - Provide additional filtering before connecting the VDDA - 10nF and 1uF in parallel is a general configuration - This is an ST Microelectronics official recommendation - 3.3VA is a distinct power net label - Add a R, L, or Ferrite Bead - Ferrite Bead Spec is 120R at 100MHz, designate value accordingly - Add Ferrite Bead in series, and another 1uF on the other side. ![[Pasted image 20241217181243.png]] #### NRST and BOOT0 - All we really need to get this thing to power up and boot properly is: - Power Supply - Voltage Regulator - NRST and BOOT0 configs - NRST - Has an internal pullup resistor - safe to leave floating - Inverted Reset - If pulled low, the uC goes into Reset Mode - Can attach to push-button to pull it down - It's wise to attach a 100nF to ground on it to prevent spurious shorts to ground and surprise resets - Good to name this net so you know what it does later - BOOT0 - Enables or disables the internal bootloader - JTAG or Serial Wire Debug is typical - ST-Link/V2 is a typical SWD device. Connects to PC via USB. - He recommends you get it - Lets you read live variables and such - Enable the Bootloader by pulling BOOT0 HIGH - This allows UART, USB, I2C to program the uC - We want to be able to flash programs via USB to the device - Steps - 1. pull boot0 high - 2. plug in the device to your computer via USB - 3. device appears as a programmable device if you have the drivers - If BOOT0 is low, it'll just run whatever program it has preloaded - We add a SPDT Switch to BOOT0 so we can toggle the boot mode - X and Y keys to flip/mirror the symbol - In our case HIGH = 3.3V, LOW = GND - Take the time to add net labels for critical debug stuff like this ![[Pasted image 20241217182446.png]] #### STM32CubeIDE - Project Setup - Has an internal crystal oscillator, that is not very accurate - STM32CubeIDE is both Programming Environment and the Pin Assigner - After File -> New Project -> New STM32 Project, - You have to target a specific MCU or Development Board ![[Pasted image 20241217182911.png]] ![[STM32F103xx Technical Description]] --- Name it and save it with default settings ![[Pasted image 20241217183129.png]] Had some database updates in order to target the board and generate code properly - not that we're at that point in the tutorial yet. Fixed it though. ![[Pasted image 20241217185019.png]] #### STM32CubeIDE - Custom Pin Selections - You can see all that the device is capable of in the tabs on the left. - You can left-click a pin to reassign individual pins where applicable. ![[Pasted image 20241217193235.png]] We want USB and Crystal enabled, but the rest are going to be GPIO headers basically. ##### Enable Serial Wire Debug `SYSTEM CORE -> SYS -> Debug Method -> Serial Wire` This will highlight PA13 and PA14 for the Serial Wire IO and CLK pins If you want live tracking of variables, there's also `Trace Asynchronous SW`, an expanded version of Serial Wire ![[Pasted image 20241217193452.png]] ##### Enable Crystal Oscillator Clock Next for the Crystal, `System Core -> RCC -> High Speed Clock (HSE) -> Crystal/Ceramic Resonator` Two more pins will light up for PD0 and PD1 to act as the clock input pins. ##### Enable USB `Connectivity -> USB -> Checkbox for "Device (FS)"` FS is for full speed. This enable PA11 and PA12 as USB + and - respectively There are Middleware options that allow you to subclass your USB Device as audio, debug, serial communication, etc. ##### STM32CubeIDE - Clock Configuration There's another tab along the top for setting up your clock. It's actually pretty complex. But at minimum, you can let it know your crystal frequency and your intended processor frequency, and it can figure out the rest. ![[Pasted image 20241217193930.png]] Note the `Resolve Clock Issues` and Reset to default buttons at the top center. #### Setting up Custom Pinout in KiCAD Now that the pins and clock are configured on STM32CubeIDE, we can implement this desired pinout accordingly back in KiCAD. We start by adding net labels for each of these pins so there's no confusion going forward. | Pin Name | Net Label | | -------- | --------- | | PD0 | HSE_IN | | PD1 | HSE_OUT | | PA11 | USB_D- | | PA12 | USB_D+ | | PA13 | SWDIO | | PA14 | SWCLK | And then just punch in each net label and attach it with a wire accordingly. ![[Pasted image 20241218130010.png]] ![[Pasted image 20241218125953.png]] #### Crystal Circuitry Open the library and search `crystal` Crystals typically come in 4-pin packages. The crystal itself has two pins, the other two pins are grounded. In this case `Crystal_GND24` means pins `2` and `4` are the grounded ones. Like in the footprint. ![[Pasted image 20241218124713.png]] `Oscillator Design guide for STM8AF/AL/S, STM32 MCUs and MPUs` is a useful document, also known as Application Note or `AN2867` and covers how to design crystal oscillators for STM32 applications. Inside the Microcontroller, there is an inverter and a feedback resistor, which forms part of the oscillator circuit. Externally, we have the crystal, a stray capacitance, two load capacitances, and sometimes a series resistor on `OSC_OUT`. ![[Pasted image 20241218123922.png]] The Load Capacitors depend on the crystal you choose and must be equal in value. 16MHz is a typical crystal frequency. 4-16MHz is the allowable external crystal speed for the STM32 Clock Configurator in the IDE. His example crystal is `X322516MMB4SI`, and calls for a load capacitance of 10pF. The load capacitance on the crystal spec is not necessarily the value of the two identical load capacitors you place next to it on the board. 1. Determine the crystal spec's load capacitance - `10pF` 2. Subtract from that the stray capacitance of the board's traces to ground at that point (typically 3-5pF) - `10pF - 5pF = 5pF` 3. Multiply that by two, and use the resulting value for each of the Load Capacitors - `5pF x 2 = 10pF` There are more advanced ways of determining these values. Here is the completed Crystal Oscillator Subcircuit: ![[Pasted image 20241218130031.png]] #### USB Connector Subcircuit Open the library and search `USB` Lots of options. For this design, `USB_B_Micro` USB has the following pins: - VBUS - D+ - D- - ID - GND - Shield For this design, we will not be connecting the `SHIELD` pin. Press `Q` to attach a *Do Not Connect* flag onto that pin so KiCAD doesn't fuss about it in ERC. The `ID` pin is used for USB-OTG (on-the-go) and allows it to switch between device mode and host mode. This will also be a DNC pin. Connecting the D+ and D- pins to the `USB_D+` and `USB_D-` pins defined earlier is straightforward. For `VBUS`, we create another power symbol/net label. VBUS is an option in the library already. Another Application Note `AN4879 - USB Hardware and PCB guidelines using STM32 MCUs` We are using an STM32F1 Series MCU. This document lets us know if we need any Series Termination or Pullup Resistors, etc. ![[Pasted image 20241218130425.png]] We can see that our STM32F103 Series MCU does NOT have an embedded Pullup resistor on `USB_D+`, which means we have to add our own. The footnote specifies further that we need to pull it up to 3.3V using 1.5kΩ. Without this pullup, the USB would not work. Note the convention of tagging 1.5kΩ as `1k5` in the Schematic. Here is the completed USB Connector subcircuit: ![[Pasted image 20241218130706.png]] #### Power Supply and Connectors Open the library and search `conn` and you'll find the GPIO header blocks. In this video he's using `Conn_01x04_Male`, but my library in KiCAD 8 has `pin` and `socket` instead of `male` and `female`. ![[Pasted image 20241218131940.png]] For any connector like this, you want to include a distinct power and ground as well. A decent rule of thumb is at least one ground for every signal line, regardless of the protocol. This overall STM32 implementation is fairly crude. It's accessible, but there are lots of more advanced modules and formats that make things easier to use for the professional. Suboptimal ESD and EMI, etc. Most STM32 MCU's do have some internal ESD. Next is the Regulator. This tutorial is using a `AMS1117-3.3` It can provide up to a 1A output at 3.3V. This symbol exists in KiCAD already. We build the Regulator subsystem off in the corner of the schematic sheet in its own area. Voltage regulators always require input and output capacitors. It's fairly straightforward. USB Power rails are typically noisy, and additional power supply filtering is best practice, but we can cut that corner in this tutorial. It's good to add a power supply status LED. We can keep our BOM simple by reusing values already in the design, such as the 1k5 resistor. All that's left is to break out all of the remaining pins into headers. In the STM32CubeIDE - Pinout & Configuration Tool: We can set up a USART1 line. If we don't like where those pins are, just set up USART 2 or 3 and see where the pins land for those ones. ![[Pasted image 20241218135003.png]] New pin mappings: | Pin Number | Net Label | | ---------- | --------- | | PB6 | I2C1_SCL | | PB7 | I2C1_SDA | | PB10 | USART3_TX | | PB11 | USART3_RX | After mapping those in STM32CubeIDE, create the corresponding net labels in KiCAD. Then for all the remaining unused pins on the MCU, you can quickly block them all off using `Q` to place a no connect flag, and then press `INSERT` to repeat the last action, which automatically targets the next pin in line. Saves some time. Finally, duplicate the 1x4 header for the UART and I2C buses. I2C requires pullup resistors around 2.2kΩ on both SDA and SCL. But again, keep the BOM simple, so just reuse the 1k5. Altogether, this collection of subcircuits forms a solid basis for STM32 microcontrollers. Here is the full Schematic: ![[Pasted image 20241218140218.png]] #### ERC and Annotation Schematics should be visually sectioned. There should be notes to let you know what things are and why they are there. It's good to add these little snippets of text as hints to help the person reading your schematic actually understand what they're looking at. ![[Pasted image 20241218145506.png]] After building out the schematic, it may be wise to clear the annotations, if it was set to auto, and then automatically re-annotate. In this case, it makes the most sense to reannotate by Y position: ![[Pasted image 20241218145709.png]] And of course, you can annotate by selection or sheet in order to make things make sense. Finalizing the Schematic: - Clean up positioning, wiring - Add cosmetic zones, zone names - Add helpful text notes - Annotate components/re-number reference designators - Run ERC Only ERC at this point is a power flag error on 3.3VA. Adding a `PWR_FLAG` to `3.3VA` marks it as a power output node and now the ERC is good to go. Before moving on to layout, **THOROUGHLY DOUBLE CHECK EVERYTHING:** - Check pinouts - Check parts are available to buy, and you have a source for them - Verify connections are correct - Verify component values are correct Finally, assigning Footprints. Here's a convenient layout for footprint assignment: ![[Pasted image 20241218150818.png]] Top Left: Current Footprint Viewer Bottom Left: Footprint Assignment Wizard Right: Schematic Editor **The Schematic Editor** automatically centers, highlights the component selected in the footprint assignment tool. **The Footprint Viewer** automatically updates with whatever footprint is currently selected. **Footprint Selection Criteria**: - Phil uses 0204 or 0603 for typical stuff. - For larger values or high power handling, 0805 - You get used to sizing this as you develop experience intuition. - I'll do 0805's for for everything for this one. - Footprint selection for Switches and Connectors can be controversial. **Here are all the Footprint Selections for this Board:** ![[Pasted image 20241218152157.png]] **Footprint Selection Notes:** - I chose 0805 for everything standard: Resistors, Capacitors, LEDs, etc. - Ferrite Beads do come in 0805 as well, so a standard inductor footprint there. - The voltage regulator and the microcontroller come with pre-assigned footprints - The Crystal is a standard size - `3225` - USB Connector, I'm just using the one he used. - 2.54mm Pitch Pinheaders aka GPIO pins for all the ... GPIO pins. Now it's time for PCB Layout and Routing The first step in PCB Layout is always **Board Setup** For two layer boards, we typically want the front copper layer to be a Signal layer, and the bottom copper layer to be a power plane. Specifically a ground plane. ![[Pasted image 20241218155058.png]] After handling Board Editor Layers and Physical Stackup, The most important thing you can do before you get started is ensure your design rules match the manufacturing capabilities of your fab house. Let's take a detour and fill this stuff out according to JLCPCB's Capabilities. [[JLCPCB - Manufacturing & Assembly Capabilities]] #### KiCAD Constraints ![[Pasted image 20241218173405.png]] #### Pre-Defined Sizes Phil recommends some general purpose traces: - 0.3mm for signal - 0.5mm for power - 0.7mm ring x 0.3mm hole for vias - 0.3mm track x 0.3mm spacing for differential pairs For more complicated designs, it's in your interest to get into extreme detail for these settings. The predefined tracks can be found at the top-left Track: selector.