Prerequisite: Analog Electronics 模擬電路, Digital Electronics 數字電路
One thing to keep in mind is that unlike software learning where you can just watch Youtube videos online and code along it, the learning method is different for hardware. You will find yourself reading text documentations and product specifications, protocol standards, measuring voltage, and asking people in the community a lot. To learn this skill, watch Hardware Wallet Research Series: #1, #2, #3, #4, #5, #6, #7
A: Caps are located close to each digital IC, or small set of such ICs, to act as local reservoirs to smooth out the rapidly fluctuating current demands of such ICs. This prevents those rapidly fluctuating currents from causing fluctuating voltages on longer supply wires (PCB traces) and possibly disrupting other chips connected to those supply wires.
In some instances you will also see a large cap parallel with a small cap right next to it. The large cap provides a large reservoir, but has a significant internal resistance, so doesn't respond as quickly as a small cap can. So together the two caps can respond quickly and provide a large reservoir.
Real capacitors have both some internal resistance and inductance in series with their "ideal" capacitance. The effects are larger with larger-value capacitors, and vary with capacitor material and construction. For the current discussion, both these non-ideal characteristics act to slow the speed with which the capacitor can respond.
A: These caps are used as "decoupling" capacitors. Even though they appear like they're all next to each other, they will be located (often in pairs) on the circuit board next to power pins of digital IC's.
Unlike analog circuitry, a digital circuit uses power in short, fast bursts. All traces or wires have some inductance, which prevents the current from changing as quickly as the IC needs it. This causes two problems: The voltage fluctuates at the input pin, and the rapidly-changing current causes the traces to radiate electrical noise.
A decoupling capacitor provides two main functions:
1. The first function is to prevent these two problems. It acts as a small power buffer right at the IC, and can provide the necessary rapidly-fluctuating currents. Since they are located right next to the IC's, there are no long traces to act as noise generators.
2. The second function is to act as a filter, dampening noise seen from the outside of the chip. This is where the multiple values of capacitors come into play. The capacitors have some small parasitic inductance, also. Each capacitor you add creates an LC filter. Each different capacitor value, combined with the parasitic inductance, filters a different range of frequencies. It is common to see a 100pF next to a 0.1uF cap at each power pin. This combination has a favorable filtering bandwidth.
So, even though you could use one large capacitor to match the nominal bus capacitance, you would lose the decoupling benefits.
USB Connections
USB Versions
USB Versions (Graphical)
The names are confusing:
USB 3.2 Gen 1 is USB 3.0. It has a maximum throughput of 5Gbps. This is also known as SuperSpeed USB.
USB 3.2 Gen 2 is USB 3.1. It has a maximum throughput of 10Gbps. This is also known as SuperSpeed USB 10Gbps.
USB 3.2 Gen 2×2 is USB 3.2. It has a maximum throughput of 20Gbps. This is also known as SuperSpeed USB 20Gbps.
USB Port Colorcode
Before USB Type C, all connectors has a host end and a client end, which lead to the design of different type of connector on different end to avoid confusion. However, both side Type C can either be host or client (dual role), therefore Type C is commonly made with Type C on both side.
Don't confuse USB protocols (like 2.0 and 3.0) with USB connector (USB-C, USB-A). But USB-C has protocol: 3A, 60W, dual role. Knowing a physical wire has a USB-C connector doesn't tell you what protocol the physical wire supports. A full-featured USB-C should support USB 3.1 Gen 2 and compatible with USB 2.0.
Voltage of Type-C. For more, read specification: https://www.usb.org/sites/default/files/USB%20Type-C%20Spec%20R2.0%20-%20August%202019.pdf
Power Delivery (PD): support 5A, 100W when the device requires it. Note that not all USB-C support PD.
Alt Mode: Alternate Mode is a mechanism that uses a USB Type-C shape port and cable to send a signal of another specification. This allows people to make different flavors of USB-C cable and port to send non-USB signal as well as USB-signal through USB-signal. Only USB-C can potentially support an Alt Mode and one USB-C cable or port is limited to have one of the "flavor". You can think of a port or a cable having one of the "flavor". A "flavor" can also be thought as a bundle of supporting protocols. For example, a cable with the Thunderbolt "flavor", which bundles USB, DisplayPort, Thunderbolt protocol, can be plugged into ports with original USB flavor, or DisplayPort flavor, or Thunderbolt flavor. The cable also has type Passive or Active.
While each protocol have their native cable (for example: USB 3.2 protocol uses USB-C, display port protocol uses display port), Thunderbolt protocol's native cable uses USB-C exclusively. Thunderbolt 3-port cable is "one of many variations of USB Type-C port cables"
Alt Mode Flavor
Note that all Thunderbolt Alt Mode support DisplayPort Alt Mode (except for DisplayPort 2.0) as shown in chart above.
The chart shows 5 original flavored Type-C cable's connection with different Alt Mode port. For example, it is fine to use a DisplayPort cable to connect Thunderbolt Alt Mode Type-C port. https://en.wikipedia.org/wiki/USB-C
"VBUS" is the USB bus voltage. It's nominally 5V (give or take 5%). You can connect it to the VCC on a 5V tolerant device. Sprinkle some decoupling capacitors (to filter signal) around it and your project can be bus powered. If you're self-powered, there's no need to connect VBUS to anything. Does your device has a use case where it's powered but not plugged into USB? If not, I'd put a weak pull-down on reset# and have a stronger pull-up tied to VBUS. That way the device gets a reset when it's plugged in. https://www.reddit.com/r/AskElectronics/comments/5dn0nf/what_is_vbus_in_this_diagram/
There are various symbol in the library for USB-C. For example TYPE-C-31-M-12 refers to Type-C male port (maybe 31 means with USB 3.1 protocol) and 12 ports.
Host and Peripheral: by determine with of CC1 or CC2 is pulled low, the host can determine peripheral's orientation. (3A and 5V case, otherwisde watch https://www.youtube.com/watch?v=V1OiQoyjDOo)
If a cable only support UBS2, not beyond 5V and not beyond 3A, then it may not be electronically marked. Otherwise, all cable need to be electronically marked.
An Electronically Marked Cable Assembly (EMCA) is a USB Type-C cable that uses a marker chip to provide the cable’s characteristics to the DFP. The electronic marking is accomplished by embedding a USB PD controller chip into the plug at one or both ends of the cable. These marker chips operate from VCONN power (or VBUS power as per the cable design). VCONN is a low voltage rail operating between 2.7 Volts and 5.5 Volts but is limited to 1 Watt of power. VBUS can be a high voltage rail up to 20 Volts. While marker chips operating off of VBUS can take advantage of more power a high voltage marker chip can also be more expensive. For this reason, most marker chips are powered by VCONN.
USB-C with Cable
For legacy device using USB2.0, the following connection should be made.
Source to Legacy Device Port Functional Model. Note the value for Rd can be found in Table 4-25 at https://www.usb.org/sites/default/files/USB%20Type-C%20Spec%20R2.0%20-%20August%202019.pdf
UBS connection needs impedance matching and sometimes 1.5k Resistor for indicate full-speed device. However, these two are incorporated in most STM32 chips.
Surface Mounting Technology (SMT)
封装
尺寸
功率
电阻耐压值 V
0201
1/20W
25
P2P
0402
1.0mmx0.5mm
1/16W
50
0603
1.6mmx0.8mm
1/10W
50
0805
2.0mmx1.2mm
1/8W
150
1206
3.2mmx1.6mm
1/4W
200
1210
3.2mmx2.5mm
1/3W
200
1812
4.5mmx3.2mm
1/2W
200
2225
5.6mmx6.5mm
Soldering
Soldering is to make connections by melting the right amount of solder to make bound with copper.
Flux: Flux is to remove oxidized layer of copper wire so that solder can directly bound with copper instead of staying on the surface of oxidized layer. The remaining flux can be removed from PCB by washing with Isopropine alcohol.
Usually we use rosin-core solder (don't use plumbing-core flux or acid-core flus as they corrode parts), note that rosin is also poisonous
Flux is helpful for soldering tiny parts of PCB
There are flux pen, flux paste in tube or flux paste in case
Solder: thin solder is good as it gives you more control of amount of solder you are applying. Typically solder comes with flux inside the wire.
Lead Solder: lower melting point, but bad for health
Lead-less Solder: higher melting point, but good for health
Cleaning:
use isopropyl alcohol to clean PCB with brush
brass (or sponge with water) to clean soldering tip
If you have multiple PCB boards, you can order a solder paste stencil to help you mount the components on PCB quickly. You must order the stencil with the PCB as the height and holes must match.
There are also machines in CMU TechSpark (and courses) that can mount components for you
51 Chip
Preperation:
Keil5: code IDE. While Keil5 C51 is for STM32, Keil5 MDK is for ARM.
STC-ISP: code transmission
USB to UART Driver CH340_CH341: driver for connecting board to PC
MCU: Micro Controller Unit, including CPU, RAM, ROM, click, ...
8051: The Intel MCS-51 (commonly termed 8051, typically pronounced eight-oh-five-one) is a single chip microcontroller (MCU) series developed by Intel in 1980 for use in embedded systems. So any chip support MCS-51 instruction set is 51-MCU.
STC89 Nomenclature
Different IC Package
STC98C51RC/RD+ Block Diagram from http://www.stcmcudata.com/datasheet/stc/STC-AD-PDF/STC89C51RC-english.pdf
STC98C51RC/RD+ Minimum Application System. The white arrow means positive and the dashed arrow means negative. The two capacitors are for electronic filters and 18/19 pins are for CPU clock. Pin 9 is reset button
HC6800-ES
A bar on the top of a port means low voltage enabling.
VCC (Volt Current Condenser): 电路的供电电压
5V: high voltage, depletion of electrons with respect to infinit electrons, denoted as 1 in digital signal.
0V: low voltage, plenty of electrons, denoted as 0 in digital signal.
TTL: 5V~0V
GND (COM): Ground
CE(OE): Chip Enable, 使能. 芯片的開關.
LED and Buttons
When we press a button, a raw voltage is sent to a port. Usually, we make the port output 0 on some port and connect LED with the port and GND to light up the LED. The same goes for LED arrays.
按鍵消抖: we can do hardware or software based signal filter to clean up the in-coming signal.
There are two types of connection for LED arrays, we can either share common anode or common cathode (GND).
Common Cathode and Common Anode in LED arrays
For display matrix, the circuit is arranged in the following manner (similar to keyboards)
LED Numeric Display Circuit Diagram
LED Numeric Display Circuit Diagram on 51 MCU where 'COM' means common port, most likely negative.
When there is lack of pins to accurately identify the LED or input button, we can scan the port (only detecting or outputing a certain row or column)
STC89C52 has built-in internal clocks (T0, T1, T2) where T0 and T1 clocks are in all traditional 8052 and T2 is specific to STC89C52.
Clock: clock is counter, you can command the clock to interupt after some time.
There are 4 modes for STC89C523 T0 and T1 clocks:
Mode 0: 13 bit clock
Mode 1: 16 bit clock (common)
Mode 2: 8 bit automatic reload
Mode 3: two 8 bit clock
How clocks work in hardware: SySclik (晶振週期) provide clock pulse to clock and is received by two counters. One counter count the number of low voltage and the other count the number of high voltage. They share a number of bits (16 in Mode 1). When the counters overflow, interrupt is triggered. When the input is connected to port instead of SySclik, then the clock becomes a counter.
How clocks work in hardware. T0 Pin here is a port to use it as a counter (if C/T is 0, then it listen from system clock, otherwise from pin T0). 12 T mode takes the input frequency and divide by 12. If 12 T mode is used, a 12 Hz CPU clock will become 1 Hz.
If the CPU clock is 1 Hz, then the voltage go high and low for 1000000 times.
There are limited amount of interuption hardware in STC89C523 (OutsideInterrupt0, ClockInterrupt0, OutsideInterrupt1, ClockInterrupt1, SerialPortInterrupt, OutsideInterrupt2, OutsideInterrupt3) where the last two are not common in typical 8051. There are also limited number of priority int.
Clock Wires are Controlled by Setting Bits in Memory
Strong pull-Up is the action of increasing the voltage of a point by connecting the point with higher voltage through a low resistor. This way, the voltage of the point is nearly identical as the voltage of the added voltage. Weak pull-up is the action of increasing the voltage of a point by connecting the point with higher voltage through a high resistor. This way, the voltage of the point is only increased a bit. Pull-down shares the same idea as pull-up but for the purpose of decreasing the voltage of a point.
Serial Port
Serial Port is a port to transmit byte information between two device.
The serial port looks like VGA port but it only has 9 pins. Do not confuse it with VGA. Many old computers have this port. There are serial port to usb or even bluetooth adaptor.
The easiest connector with serial port only involves 4 pins. VCC, TXD, RXD, GND for device 1 and VCC, TXD, RXD, GND for device 2. Note that we cross connect TXD and RXD. (Be careful on PCB schemetic) The TXD stands for Transmit Exchange Data and RXD stands for Receive Exchange Data.
Serial Port DB9 male
We could ignore one of TXD, RXD connection if the data flow is one directional.
RS485 protocol (Differential signalling): The voltage difference is [+2V, +6V] represent 1 and [-2V, -6V] represent 0 (or inverted). Note that this measures voltage difference and must require 2 wires.
The reason we use Differential Signalling is that it can transmit beyond 10 meters or wire and possibly 1000 meters without much disturbance. This protocol is commonly used in USB.
Other pins like DTR and RTS are pins that control data flow (not supported by 8051)
Half Duplex: can send and receive, but not at the same time. Usually 1 pin.
Full Duplex: can send and receive at the same time. Usually 2 pins.
Single Duplex: only one way transmission by 1 pin (or bluetooth wireless etc...)
Async: both device obey their own transmission rate
Sync: both device obey the same clock cycle, usually needs a clock pin.
There are a few more protocols: and also CAN(in cars), USB
Name
Pins
Transmission
Comments
UART
TXD, RXD
Full Duplex, Async
Point to Point Transmission
PC
SCL, SDA
Half Duplex, Sync
Multiple Device attach on Main Line
SPI
SCLK, MOSI, MISO, CS
Full Duplex, Sync
Multiple Device attach on Main Line
1-Wire
DQ
Half Duplex, Async
Multiple Device attach on Main Line
UART is serial port
// QUESTION
STC89C52 has 4 modes for UART:
Mode 0: synchronous register
Mode 1: 8 bit UART, can change Baud (common)
Mode 2: 9 bit UART, cannot change Baud
Mode 3: 9 bit UART, can change Baud
Bit Orders of Transmission on 9 bit UART
Baud Bit: the rate of transmission sampling
Verify Bit: Hamming Code
Stop Bit: divide byte to byte
I/O Extension
We can use chips like 74HC695 to extend the number of IO ports. It is esstentally a register that take sequential input and converted to synchronous signal.
74HC595
The way it works is that we send bit information to one port and then move the all information by one slot (and reset). Once all data is there, we batch send them.
STM32
STM32 MCUs
STM32 Nomenclature
There are following peripherals on the chip:
NVIC: 嵌套向量中斷控制器 (in chip)
Sysick: 定時器 (in chip)
RCC: 復位和時鐘控制
GPIO: General Purpose IO
AFIO: 複用 IO
EXTI: 外部中斷
TIM: 定時器
ADC: 模數轉換器
DMA: 內存訪問
USART: 同步/異步串口通信
I2C: I2C 通信
SPI: SPI 通信
CAN: CAN 通信
USB: USB 通信
RTC: 實時時鐘
CRC CRC 校驗
PWR: 電源控制
BKP: 備份寄存器
IWDG: 獨立看門狗
WWDG: 窗口看門狗
DAC: 數模轉換器
SDIO: SD 卡
FSMC: 可變靜態存儲控制器
USB OTG: USB 主機接口
STM32F103C8T6 Pin Defintion. Blue is minimal circuit while red is related to power. S means power. FT means 5V, otherwise 3V. Re-definition allows us to re-define a pin for other usage.
Note the Capacitor between GND and 3.3V is for stablizing voltage. VBAT can be connected to a backup Button Battery (not necessary, can connect to 3.3V or nothing).
In 6.3.15 NRST pin characteristics, you can find inctrustion on how to configure NRST pin. Table 56. NRST pin characteristics tells you the value for pull-up resistor R_{PU} is 40 k\Omega. And Figure 25. Recommended NRST pin protection tells you how to add protections.
In Table 24. General operating conditions, you can find voltage and temperature requirement for each pin during operation.
In Table 14. STM32F072x8/xB pin definitions, you can find all pin definitions, very useful for identifying I/O pins.
In Figure 13. Power supply scheme, you can find how to power up the chip.
In Figure 17. Typical application with an 8 MHz crystal you can find typicall configuration for external oscillator.
There isn't detailed documentation for BOOT0, watch this video instead.
In Table 4-25 Sink CC Termination (Rd) Requirements of USB Standard you can find value for Rd resistor and in Figure 4-30 Source to Legacy Device Port Functional Model you will find USB 2.0 schemetic.
In Table 50. ESD absolute maximum ratings tells you about protestion from ESD. However, you don't want them to be used since they are the last layer of protection and you should protect the peripherals from ESD.
If you use high frequency signal wires (like USB), Table 70. USB electrical characteristics gives you output driver impedance and it says "No external termination series resistors are required on USB_DP (D+) and USB_DM (D-); the matching impedance is already included in the embedded driver."
When purchasing parts, go to LCSC-EN as the chinese version sucks.
There isn't a consensus on how to connect SWD pins. You can define how many pins as you want and they are connected by jump wire.
These protocols are used for additional drivers in addition to having one MCU. Usually MCU flash is done with USB DFU protocol used in the STM32 bootloader. DFU stands for Device Firmware Upgrade and it only allows you to flash a new device (Getting started with STM32 built-in USB DFU Bootloader) and cannot be used for debugging purposes. Flashing a device with USB DFU involving set BOOT0 to 1 and flash using USB connection and then set BOOT0 to 0 before RESET. You need to have one of the above protocol (and port) open for debugging purposes as you can't debug with USB DFU. Specific tools like JTAG, ST-Link, or USB-to-UART is needed.
Many keyboards in the QMK Firmware repo are split keyboards. They use two controllers: one plugging into USB, and the second connected by a serial or an I2C connection over a TRRS or similar cable. ARM split supports most QMK subsystems when using the ‘serial’ and ‘serial_usart’ drivers. I2C slave is currently unsupported by QMK. Both sides must use the same MCU family.
For flashing firmware into STM32, see Boot Mode section.
At startup, the boot pin and boot selector option bit are used to select one of the three boot
options:
- boot from User Flash memory
- boot from System Memory
- boot from embedded SRAM
The boot loader is located in System Memory. It is used to reprogram the Flash memory by using USART on pins PA14/PA15, or PA9/PA10 or I2C on pins PB6/PB7 or through the USB DFU interface.
Some MCU does not have BOOT1 pins. You might only be able to set BOOT1 using software.
Sometimes scottky diode (instead of silicon diode as it doesn't impact voltage as much) can be used to prevent backflow of power when user connected to the wrong end. Note that scottky diode might not be able to block all current, 20mA will leak through as compared to silicon diode, but it is sufficient for reverse voltage protection. Absolute maximum rating is normally set as 10V greater than peak reverse voltage, peaking higher voltage rating will sacrefice efficiency.
Power protection (ESD/TVS) and filtering can get very complicated and often involves:
Compare ESD and TVS, we know that ESD is protection for human electrical discharge, and TVS is protection for electrical surge. They appear on schemetic as a inverte-connected diode. Such protection is needed at connection to USB since it USB provides both power and signal.
Q: So a bit of an odd question, I want to replace all the various bits on a Mysterium with a black pill (sans diodes, switch, etc) If I run lines from PA11, PA12 (Data + and Data -) as well as GND and VCC, and use an external USB port, will I need to add a resistors on the data lines or would that all be handled by the Black pill itself?
A: STM32 chips don't need impedance matching resistors on USB data lines, so you should not need to add anything extra (although you may want to add things like a fuse and ESD protection).
And if you will be using a Type C port, you will need two 5.1k resistors on CC pins (but they won't be connected to the blackpill).
You should check the manual to see if your chip has integrated impedance matching for USB.
NRST and Boot
advance circuit for reset and boot
Here is an advanced circuit for reset and boot. The circuit make use of MOSFETs to act as a voltage-controlled switch. When the human-controlled botton is pressed, the BOOT pin gets powered, and then once powered, the NRST pin is powered by the internal MCU, therefore disconnect BOOT from 3.3V. Once botton is released, NRST is pulled down by GND and reset the MCU.
Follow this guide so that you know to light up LD2, you need to set A5 high. You can use STM32CUBEIDE and run debug configuration. In C/C++ application set your flashing .elf file and hit run to flash your board with ST-LINK S/N enabled and scan automatically.
Create a new project (with the correct processor)
Hit Run
Hit Run Configuration
Double Click STM32 C/C++ Application
Give it a name
At C/C++ Application, give path to your .elf file
In Debugger, Misc -> Log to file, enable and select a log file (otherwise will result in device not found error)
In Debugger, GDB Server Command Line Option -> Interface, check ST-LINK S/N and hit Scan.
Press Run.
Assume you have just purchased ST-Link and soldered your board, here is what you need to do to debug:
Power up your board
Connect ST-Link to your computer
Remove two jumpers to ST-Link if you have purchased NUCLEO-F446RE instead
Connect SWD from ST-Link to your board following this video
Create a project in STM32CUBEIDE, and configure the pins you want to test by clicking the pin and select GPIO_OUTPUT
Configure external crystal setting if you are using one
Save the file and generate the code
In the while loop, add the code that looks like this (names may be different)
Click the debug icon to start debug
The program will pause on default breakpoint, click green arrow to continue
Note that for some reason, you can only configure one debug profile (the second one would not work)
while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
HAL_Delay(500);
/* USER CODE BEGIN 3 */
}
Impedance Matching
Here are a list of video explaining the concept of impedance matching
For hand-calculation of Edge-coupled coplanar, see this stackoverflow
Coupled Differential Pairs
Final Check
Once you settle down your design, check out this good video as it will run you through the entire process of pcb design with small caution (or nice to have) details like impedance matching and power protection.
Copper Pour
To connect GND together, you fill out copper in all planes. For 4 layers, it is common to have Signal, GND, POW, Signal stackup. However, there isn't a consensus on whether you should pour GND for the top layer in 2-layer PCB.
This is for:
lower impedance to ground
better heat dissipation
some protection on EMC
better quality in manufacture
Note that for digital signal, copper pour is good. But it might be bad for analog signal due to electromagnetic coupling. Therefore not all pcb needs copper pour.
There are two ways to do copper pour
Copper All Over The Place: just normal copper pour. It has better EMC protection but might sensitive to expansion/contaction due to temperature change.
Grid pour: Poor EMC protection, but not sensitive to temperature during manufacture.
Other Stuff
Before Finish Design:
- vias in chip for heat dissipation
- rounding edge
- testing pad with exposed copper
- screw holes
- pretty drawing and logos
- pin labels