STM32 UNO Programmer Guide » Historie » Verze 1
Ján Hýbl, 2026-11-16 07:57
| 1 | 1 | Ján Hýbl | h1. STM32 UNO programmer guide |
|---|---|---|---|
| 2 | |||
| 3 | Documentation version 1.0, 13 November 2026. Applies to the @STM32-UNO_pcb@ files exported on 2026-11-13. This does not establish the manufacturing revision of an assembled board. |
||
| 4 | |||
| 5 | STM32-UNO is a development board based on the STM32F446RET6 microcontroller. It provides GPIO, analog inputs, headers in the UNO arrangement, mikroBUS, microSD, I2C, and direct USB. This guide explains how to prepare a first project, select the correct pins, and connect peripherals. The default programming environment is C with STM32 HAL. |
||
| 6 | |||
| 7 | [[STM32_UNO|Documentation home]] | [[STM32_UNO_Tutorial_Notes|Tutorial authoring notes]] |
||
| 8 | |||
| 9 | {{toc}} |
||
| 10 | |||
| 11 | h2. Basic rules |
||
| 12 | |||
| 13 | * Use GPIO and analog signals in the 0 to 3.3 V range in these tutorials. A 5 V supply pin does not specify the GPIO voltage. |
||
| 14 | * Every external device must share GND with the board. |
||
| 15 | * For initial programming and debugging, use an external ST-LINK and the SWD interface on U5. |
||
| 16 | * Select *STM32F446RET6* in the project rather than a ready-made profile for another board with a similar processor. |
||
| 17 | * Use names such as @PA0@, @PB0@, and @PC0@. Arduino pin numbers such as @D0@ and @A0@, or macros such as @LED_BUILTIN@, are not defined in these source files. |
||
| 18 | * No HSE crystal is connected in the schematic. Use HSI for initial projects; X1 is a 32.768 kHz crystal for the RTC only. |
||
| 19 | |||
| 20 | h2. Hardware overview |
||
| 21 | |||
| 22 | |_. Part |_. Programmer-relevant feature |_. Source | |
||
| 23 | | U2 | STM32F446RET6, Cortex-M4F, LQFP64 | S1, B1 | |
||
| 24 | | MCU memory | 512 KiB Flash, 128 KiB SRAM; maximum CPU clock 180 MHz | D1 | |
||
| 25 | | Logic | MCU supply and main logic levels: 3.3 V | S1, p. 1 | |
||
| 26 | | Clocks | Internal HSI; X1 at 32.768 kHz on PC14 and PC15; PH0 and PH1 unconnected | S1, p. 1 | |
||
| 27 | | U5 | SWD, reset, optional SWO, and USART2 | S1, p. 4 | |
||
| 28 | | USB-C | D- on PA11, D+ on PA12; direct MCU USB | S1, p. 1 | |
||
| 29 | | SW1 | Reset through NRST, active low | S1, p. 1 | |
||
| 30 | | SW2 | User button on PA0, pressed = 0 | S1, p. 1 | |
||
| 31 | | Digital indicators | Red and blue LEDs on 12 GPIOs; shared rails controlled by JP1 and JP2 | S1, p. 1 | |
||
| 32 | | CN1 | I2C1, powered from PWR_USB | S1, p. 2 | |
||
| 33 | | J2 | microSD wired to SPI2, card detection on PC8 | S1, p. 1 | |
||
| 34 | | mikroBUS | SPI1, USART6, I2C3, analog input, PWM, reset, and interrupt | S1, p. 5 | |
||
| 35 | | RTC | X1 crystal and B1 battery holder connected to VBAT | S1, p. 1 | |
||
| 36 | |||
| 37 | MCU capabilities and maximum ratings are not the board's default settings. The first project below runs from HSI at 16 MHz. For MCU specifications, see the "ST DS10693 datasheet":https://www.st.com/resource/en/datasheet/stm32f446re.pdf. |
||
| 38 | |||
| 39 | h2. Power supply and connecting modules |
||
| 40 | |||
| 41 | h3. Supply rails |
||
| 42 | |||
| 43 | In the schematic, USB VBUS feeds the @PWR_USB@ rail through diode D2. Converter U1 also supplies this rail from the external input. Regulator U3 generates @+3.3V@ from it. Pins labelled 5 V are connected to PWR_USB; when powered from USB, this rail includes the diode voltage drop, so an exact 5.00 V is not guaranteed. |
||
| 44 | |||
| 45 | For initial experiments, power the board through USB-C with no DC supply or shields connected. Also connect the USB side of the ST-LINK. If the probe provides a power output, do not automatically connect it to the board; U5/3 is the target voltage reference. Follow the wiring instructions for your particular probe. |
||
| 46 | |||
| 47 | DC1 feeds VIN through D1. The VIN pin on the power header is downstream of this diode, so powering through the header bypasses D1 protection. The schematic includes the note "Operating up to 28 V", but no validated operating range for the complete assembled board or available module current is specified. Do not treat 28 V as a recommended user supply voltage. Before writing a tutorial using a DC supply, verify DC1 polarity, the voltage range, regulator temperatures, and current headroom. |
||
| 48 | |||
| 49 | When connecting sensors, check their supply voltage and data signal voltage separately. The CN1 power pin is connected to PWR_USB, while the onboard I2C pull-ups connect to 3.3 V. The four-pin connector alone therefore does not guarantee compatibility with every 3.3 V module. External pull-ups to 5 V may change the voltage level of the entire bus. |
||
| 50 | |||
| 51 | h3. Analog signals and reference |
||
| 52 | |||
| 53 | The MCU VDDA/VREF+ supply is connected to 3.3 V. Do not exceed VDDA at ADC inputs, and use the actual reference voltage when converting readings to volts. The @ref@ net on the headers is not connected to MCU VREF+ in the schematic. It is not an input for changing the ADC reference. |
||
| 54 | |||
| 55 | H2 pin 1 is unconnected, pin 2 is @ref@, and pin 3 is 3.3 V. A jumper between pins 2 and 3 therefore connects @ref@ to 3.3 V. It does not switch the GPIO supply between 3.3 V and 5 V. |
||
| 56 | |||
| 57 | h2. First connection and programming |
||
| 58 | |||
| 59 | h3. What you need |
||
| 60 | |||
| 61 | * The board with no expansion modules connected, and a USB-C data cable. |
||
| 62 | * An external ST-LINK providing SWDIO, SWCLK, GND, and a target voltage reference; NRST is also recommended. |
||
| 63 | * A cable or adapter matching the U5 connector actually fitted. The BOM lists a 2 x 7 connector with 1.27 mm pitch; page 1 of the schematic includes a note about a future change to 2.54 mm. |
||
| 64 | * "STM32CubeIDE":https://www.st.com/en/development-tools/stm32cubeide.html, STM32CubeMX for project configuration, and the STM32CubeF4 package. You can also program the board separately using "STM32CubeProgrammer":https://www.st.com/en/development-tools/stm32cubeprog.html. |
||
| 65 | |||
| 66 | h3. Connecting the ST-LINK |
||
| 67 | |||
| 68 | The numbers below are U5 contact numbers in the schematic, not the numbers on an arbitrary probe connector. Identify pin 1 from the marking on the actual PCB or connector. Do not reverse the cable or offset it by one row. |
||
| 69 | |||
| 70 | |_. U5 pin |_. Board net |_. Probe connection | |
||
| 71 | | 1, 2 | Unconnected | Leave open | |
||
| 72 | | 3 | +3.3V | VTref / T_VCC, target reference | |
||
| 73 | | 4 | PA13 / T_SWDIO | SWDIO | |
||
| 74 | | 5 | GND | GND | |
||
| 75 | | 6 | PA14 / T_SWCLK | SWCLK | |
||
| 76 | | 7 | GND | GND | |
||
| 77 | | 8 | PB3 / T_SWO | SWO, optional | |
||
| 78 | | 9, 10 | Unconnected | Leave open | |
||
| 79 | | 11 | GND | GND / GNDDetect, depending on the probe | |
||
| 80 | | 12 | NRST | NRST | |
||
| 81 | | 13 | PA3 / USART2_RX | Probe serial adapter TX, optional | |
||
| 82 | | 14 | PA2 / USART2_TX | Probe serial adapter RX, optional | |
||
| 83 | |||
| 84 | U5 wiring is shown on page 4 of the schematic. Signals 13 and 14 are named from the MCU's perspective: PA2 transmits and PA3 receives. The ST-LINK must support a virtual COM port if you want to use this feature; the serial connection is not required for SWD. |
||
| 85 | |||
| 86 | # With power disconnected, connect GND, VTref, SWDIO, SWCLK, and NRST. |
||
| 87 | # Power the board through USB-C and connect the ST-LINK to the computer. |
||
| 88 | # In STM32CubeProgrammer, select ST-LINK and the SWD interface. Start at a lower frequency, such as 100 to 400 kHz. |
||
| 89 | # Connect and verify that the tool recognizes the target MCU and can read Flash. |
||
| 90 | # If the connection fails, check wiring and the reference voltage, then try connecting under reset with NRST connected. |
||
| 91 | |||
| 92 | h3. Creating your first project |
||
| 93 | |||
| 94 | # In STM32CubeMX, create a project by selecting the *STM32F446RET6* MCU. Depending on your tool version, the configurator may be standalone or accessible from the development environment. |
||
| 95 | # Keep *Serial Wire* enabled in the SYS configuration. Reserve PA13 and PA14 for debugging. |
||
| 96 | # In RCC, select HSI as the SYSCLK source without PLL, giving 16 MHz. Leave HSE disabled. The first example does not need LSE or RTC. |
||
| 97 | # Configure PB0 as GPIO Output, push-pull, no pull-up or pull-down, low speed, initial level 0. |
||
| 98 | # Configure PA0 as GPIO Input without internal pull-up or pull-down. External resistor R3 already provides a 10 kΩ pull-up. |
||
| 99 | # Generate the project for STM32CubeIDE. This guide assumes STM32F4 HAL and the usual generated functions @HAL_Init()@, @SystemClock_Config()@, and @MX_GPIO_Init()@. |
||
| 100 | # Insert the blinking example below, build the project, and start debugging through ST-LINK/SWD. |
||
| 101 | # After programming, let the application run without stopping at a breakpoint. Check the changing PB0 level and the corresponding LED indication. |
||
| 102 | |||
| 103 | For a standard application without a custom bootloader, Flash starts at @0x08000000@. ELF and HEX files contain addresses; for a raw BIN file, set the correct start address. Do not copy an offset from another board. For initial projects, leave memory protection and option bytes unchanged. |
||
| 104 | |||
| 105 | h3. USB and the bootloader |
||
| 106 | |||
| 107 | USB-C is not a USB-to-UART adapter or an onboard ST-LINK. Connecting it may not create a COM port. USB CDC or another USB class requires appropriate firmware and correctly configured USB clocks. |
||
| 108 | |||
| 109 | According to "ST AN2606, STM32F446xx chapter":https://www.st.com/resource/en/application_note/an2606-introduction-to-system-memory-boot-mode-on-stm32-mcus-stmicroelectronics.pdf, the factory USB DFU bootloader in STM32F446 requires an external HSE clock. PH0 and PH1 are unconnected in the supplied schematic, so USB DFU is not the default method for initial programming of this board. The 32.768 kHz X1 crystal does not meet this requirement. |
||
| 110 | |||
| 111 | BOOT0 is pulled to GND through R2, 10 kΩ. SW2 controls PA0, not BOOT0. Entering the system bootloader through BOOT0 and PB2/BOOT1 requires the correct levels at reset and access to BOOT0; the ordinary user button does not invoke it. UART bootloader pins may also differ from the application's UART pins. Prefer SWD for application recovery. |
||
| 112 | |||
| 113 | h2. Connector pin map |
||
| 114 | |||
| 115 | The tables use *schematic pin numbering*. They do not define a top view or cable orientation. Locate pin 1 on the board before making physical connections. The schematic labels @CP7@ and @CP9@ are net-name typos connected to MCU pins PC7 and PC9; use PC7 and PC9 in software. |
||
| 116 | |||
| 117 | h3. CONN_DIG1 |
||
| 118 | |||
| 119 | |_. Pin |_. MCU |_. Basic use |_. Indicators | |
||
| 120 | | 1 | PA1 | GPIO | D_B6, D_R6 | |
||
| 121 | | 2 | PB0 | GPIO, first blinking example | D_B1, D_R1 | |
||
| 122 | | 3 | PB1 | GPIO | D_B4, D_R4 | |
||
| 123 | | 4 | PB2 | GPIO; also BOOT1 during boot-mode selection | D_B5, D_R5 | |
||
| 124 | | 5 | PB4 | GPIO | D_B3, D_R3 | |
||
| 125 | | 6 | PB5 | GPIO | D_B2, D_R2 | |
||
| 126 | | 7 | PB6 | USART1_TX, AF7 | No digital indicator pair | |
||
| 127 | | 8 | PB7 | USART1_RX, AF7 | No digital indicator pair | |
||
| 128 | |||
| 129 | h3. CONN-DIG2 |
||
| 130 | |||
| 131 | |_. Pin |_. MCU or net |_. Basic use |_. Indicators | |
||
| 132 | | 1 | PB8 / I2C1_SCL | I2C1 SCL, AF4 | 3.3 kΩ pull-up to 3.3 V | |
||
| 133 | | 2 | PB9 / I2C1_SDA | I2C1 SDA, AF4 | 3.3 kΩ pull-up to 3.3 V | |
||
| 134 | | 3 | ref | Net optionally connected to 3.3 V through H2 | Not MCU VREF+ | |
||
| 135 | | 4 | GND | Ground | None | |
||
| 136 | | 5 | PC10 | SPI3 SCK, AF6 | D_B11, D_R11 | |
||
| 137 | | 6 | PC11 | SPI3 MISO, AF6 | D_B12, D_R12 | |
||
| 138 | | 7 | PC12 | SPI3 MOSI, AF6 | D_B10, D_R10 | |
||
| 139 | | 8 | PB3 | GPIO or SWO | D_B8, D_R8 | |
||
| 140 | | 9 | PD2 | GPIO for software-controlled SPI3 CS | D_B9, D_R9 | |
||
| 141 | | 10 | PC13 | GPIO | D_B7, D_R7 | |
||
| 142 | |||
| 143 | The @PD2/SPI3SSEL@ net name does not imply a hardware SPI3_NSS alternate function. In this circuit, drive PD2 as a normal GPIO output for CS. Use PC13 for slow signals and observe its specific current and speed limits in the datasheet. |
||
| 144 | |||
| 145 | h3. CONN-ANALOG |
||
| 146 | |||
| 147 | |_. Pin |_. MCU |_. Channel for the suggested ADC1 configuration |_. Other use | |
||
| 148 | | 1 | PA4 | IN4 | DAC_OUT1 | |
||
| 149 | | 2 | PC4 | IN14 | GPIO | |
||
| 150 | | 3 | PC3 | IN13 | GPIO | |
||
| 151 | | 4 | PC2 | IN12 | GPIO | |
||
| 152 | | 5 | PC1 | IN11 | GPIO | |
||
| 153 | | 6 | PC0 | IN10 | GPIO | |
||
| 154 | |||
| 155 | These signals do not automatically map to Arduino A0 through A5. A separate board variant would have to define numbering for the selected Arduino core. |
||
| 156 | |||
| 157 | h3. CONN-POWER and H2 |
||
| 158 | |||
| 159 | |_. CONN-POWER pin |_. Net |_. Meaning | |
||
| 160 | | 1 | VIN | Converter input, shared rail downstream of D1 | |
||
| 161 | | 2, 3 | GND | Ground | |
||
| 162 | | 4 | PWR_USB | Rail nominally labelled 5 V | |
||
| 163 | | 5 | +3.3V | 3.3 V regulator output | |
||
| 164 | | 6 | NRST | Reset, active low | |
||
| 165 | | 7 | ref | Optional connection to 3.3 V through H2 | |
||
| 166 | | 8 | Unconnected | No function | |
||
| 167 | |||
| 168 | |_. H2 pin |_. Net | |
||
| 169 | | 1 | Unconnected | |
||
| 170 | | 2 | ref | |
||
| 171 | | 3 | +3.3V | |
||
| 172 | |||
| 173 | h3. CN1 for I2C |
||
| 174 | |||
| 175 | |_. Pin |_. Net |_. Function | |
||
| 176 | | 1 | GND | Ground | |
||
| 177 | | 2 | PWR_USB | Module supply; not the 3.3 V rail | |
||
| 178 | | 3 | PB9 / I2C1_SDA | Data | |
||
| 179 | | 4 | PB8 / I2C1_SCL | Clock | |
||
| 180 | |||
| 181 | The contact labelled 0 in the schematic connects to GND; it is not a fifth cable signal. The BOM specifies JST BM04B-SRSS-TB. The same I2C signals are available on CONN-DIG2, so devices on both connectors share one address space. |
||
| 182 | |||
| 183 | h3. mikroBUS CONN-MB1 |
||
| 184 | |||
| 185 | Function names follow the mikroBUS symbol in the schematic. Determine physical orientation from the actual socket. CONN-MB1 is listed in the pick-and-place file but has no separate entry in the supplied BOM; verify that the sockets are fitted. |
||
| 186 | |||
| 187 | |_. mikroBUS signal |_. MCU or net |_. Suggested configuration | |
||
| 188 | | AN | PC5 | ADC1 IN15 | |
||
| 189 | | RST | PA9 | GPIO output for module reset; not board NRST | |
||
| 190 | | CS | PA15 | GPIO output for software-controlled CS | |
||
| 191 | | SCK | PA5 | SPI1 SCK, AF5 | |
||
| 192 | | MISO | PA6 | SPI1 MISO, AF5 | |
||
| 193 | | MOSI | PA7 | SPI1 MOSI, AF5 | |
||
| 194 | | 3V3 | +3.3V | Supply | |
||
| 195 | | GND, left | GND | Ground | |
||
| 196 | | PWM | PA10 | For example, TIM1 CH3, AF1 | |
||
| 197 | | INT | PB10 | GPIO input or EXTI10 | |
||
| 198 | | RX | PC7 | USART6 RX, AF8 | |
||
| 199 | | TX | PC6 | USART6 TX, AF8 | |
||
| 200 | | SCL | PA8 | I2C3 SCL, AF4 | |
||
| 201 | | SDA | PC9 | I2C3 SDA, AF4 | |
||
| 202 | | 5V | PWR_USB | Supply | |
||
| 203 | | GND, right | GND | Ground | |
||
| 204 | |||
| 205 | I2C3 has its own 3.3 kΩ pull-ups to 3.3 V. The mikroBUS I2C3 bus is separate from I2C1 on CN1. When using PA15 and PB4 as GPIOs, keep debugging in Serial Wire mode so that full JTAG does not reserve them. |
||
| 206 | |||
| 207 | h2. Buttons and LEDs |
||
| 208 | |||
| 209 | h3. User button |
||
| 210 | |||
| 211 | SW2 connects PA0 to GND. R3 pulls PA0 up to 3.3 V, so released = 1 and pressed = 0. LED3 and R22 also connect to this net; LED3 lights when the net is pulled low. Use PA0 as an input in basic tutorials. Configuring PA0 as an output driven high would create a conflict between the output and ground when the button is pressed. |
||
| 212 | |||
| 213 | For an interrupt on a press, select EXTI0 on the falling edge and debounce the button. For wake-up from a low-power mode, check the required polarity separately; wake-up functionality is not the same as ordinary EXTI configuration. |
||
| 214 | |||
| 215 | h3. Reset and power indicators |
||
| 216 | |||
| 217 | SW1 pulls NRST to ground and restarts the MCU. LED_RESET1 indicates that NRST is low. LED_PWR1 connects to the 3.3 V rail. LED_PWR2 is controlled by transistor Q2 according to VIN; it is not a user LED controlled by a GPIO. Do not use these indicators as substitutes for a software-controlled status LED. |
||
| 218 | |||
| 219 | h3. Digital level indicators |
||
| 220 | |||
| 221 | Twelve GPIOs have blue and red LED pairs. JP1 connects the shared @ledUp@ rail to 3.3 V, and JP2 connects @ledDwn@ to GND. With both jumpers fitted, the schematic indicates blue for logic 0 and red for logic 1. D_B13 and D_R13 belong to the shared rails; they are not additional independently controlled GPIOs. |
||
| 222 | |||
| 223 | Both rails can be enabled for ordinary GPIO observation. Remove both jumpers for analog measurements on these pins, weak signal sources, low-power operation, or fast buses. The LEDs and resistors load the signals and do not reliably indicate a high-impedance state. PWM may produce a mixture of colors; color alone does not demonstrate the correct frequency or duty cycle. |
||
| 224 | |||
| 225 | Removing the jumpers breaks the direct connections between the shared rails and power or ground, but does not disconnect the individual LEDs from the GPIOs. The rails remain interconnected through the indicator circuits. For accurate analog measurements, prefer the dedicated analog inputs PC0 through PC4 or PA4. |
||
| 226 | |||
| 227 | h2. Peripherals and shared functions |
||
| 228 | |||
| 229 | |_. Use |_. MCU instance |_. Wiring | |
||
| 230 | | I2C on CN1 and the digital header | I2C1 | PB8 SCL, PB9 SDA | |
||
| 231 | | mikroBUS I2C | I2C3 | PA8 SCL, PC9 SDA | |
||
| 232 | | mikroBUS SPI | SPI1 | PA5 SCK, PA6 MISO, PA7 MOSI, PA15 software-controlled CS | |
||
| 233 | | microSD | SPI2 | PB13 SCK, PB14 MISO, PB15 MOSI, PB12 software-controlled CS | |
||
| 234 | | SPI on the digital header | SPI3 | PC10 SCK, PC11 MISO, PC12 MOSI, PD2 software-controlled CS | |
||
| 235 | | UART on CONN_DIG1 | USART1 | PB6 TX, PB7 RX | |
||
| 236 | | UART on U5 | USART2 | PA2 TX, PA3 RX | |
||
| 237 | | mikroBUS UART | USART6 | PC6 TX, PC7 RX | |
||
| 238 | |||
| 239 | Alternate functions and analog channels follow "DS10693, Tables 10 and 11":https://www.st.com/resource/en/datasheet/stm32f446re.pdf. Always select the specific pins in CubeMX; automatically selecting a different pair does not change the PCB wiring. |
||
| 240 | |||
| 241 | PB3 is shared by U5/8, CONN-DIG2/8, and the LED indicators. Do not use another function on it while using SWO. PA9, used for mikroBUS RST, cannot simultaneously serve as the standard OTG_FS_VBUS input. PA10, used for mikroBUS PWM, cannot simultaneously serve as OTG_FS_ID. U5 is a connector for an external probe, not another onboard processor. |
||
| 242 | |||
| 243 | h3. microSD |
||
| 244 | |||
| 245 | Slot J2 is wired for *SPI2*, not four-bit SDIO. Contact numbers below refer to the J2 slot symbol. |
||
| 246 | |||
| 247 | |_. J2 contact |_. Function in SPI mode |_. Net | |
||
| 248 | | 1 | DAT2, unused in SPI | Pull-up | |
||
| 249 | | 2 | CS | PB12 | |
||
| 250 | | 3 | DI / MOSI | PB15 | |
||
| 251 | | 4 | VDD | +3.3V | |
||
| 252 | | 5 | CLK | PB13 | |
||
| 253 | | 6 | VSS | GND | |
||
| 254 | | 7 | DO / MISO | PB14 | |
||
| 255 | | 8 | DAT1, unused in SPI | Pull-up | |
||
| 256 | | 9 | CDN detection contact | PC8 | |
||
| 257 | |||
| 258 | In addition to FatFs, the file system needs a disk I/O layer that controls the SD card over SPI. Enabling FatFs alone does not provide an SPI SD card driver. During initialization, keep CS high and use SPI mode 0, eight-bit data, MSB first, and a clock no higher than 400 kHz. Increase the speed only after successful initialization. |
||
| 259 | |||
| 260 | PC8 is the detection net with a pull-up. Verify the actual switch polarity when inserting a card in the fitted slot before basing application behavior on it. Finish writes and close files before removing the card. Do not simultaneously use PB14 and PB15 for USB HS. |
||
| 261 | |||
| 262 | h3. RTC and backup power |
||
| 263 | |||
| 264 | The RTC can use the 32.768 kHz LSE clock from X1. Reserve PC14 and PC15 for the oscillator when doing so. B1 supplies backup voltage to VBAT; it does not keep the entire board running without main power. No battery charger is shown in the schematic. Check the cell type, polarity, and voltage against the actual holder and the VBAT range in the datasheet. Do not set the time on every reset without checking whether the backup domain has already been initialized. |
||
| 265 | |||
| 266 | h3. USB applications |
||
| 267 | |||
| 268 | For custom USB applications, PA11 and PA12 connect to USB-C. The schematic does not route VBUS to PA9, because PA9 serves mikroBUS RST, and does not connect ID to PA10. Start with fixed USB Device mode and VBUS sensing configured for this topology; do not automatically copy OTG settings from another board. |
||
| 269 | |||
| 270 | USB requires both a correct 48 MHz clock and sufficient clock accuracy. A 48 MHz value in the configurator alone does not guarantee the tolerance of clocks derived from HSI. Before publishing a USB CDC tutorial, verify the clock design, stable enumeration, and data transfer. A USB-C connector alone does not guarantee USB High Speed, USB Power Delivery, or host functionality. |
||
| 271 | |||
| 272 | h2. C examples for STM32 HAL |
||
| 273 | |||
| 274 | These examples are teaching fragments for a project generated for STM32F446RET6. They are not standalone applications or a complete BSP. They use blocking calls for clarity. They have not been built with a specific STM32CubeF4 version or tested on hardware. Place additions to generated files in @USER CODE@ sections so that project regeneration preserves them. |
||
| 275 | |||
| 276 | The file containing an example must have access to the HAL header and declarations of the handles it uses: @huart2@, @hi2c1@, or @hadc1@. If the configurator generates peripherals in separate files, add the appropriate @#include "usart.h"@, @#include "i2c.h"@, or @#include "adc.h"@. Check the names in your generated project. |
||
| 277 | |||
| 278 | h3. Blinking on PB0 |
||
| 279 | |||
| 280 | Prerequisite: PB0 is a push-pull GPIO Output without a pull-up or pull-down. On the board, it corresponds to CONN_DIG1/2 and the D_B1/D_R1 LED pair. Fit JP1 and JP2 for color indication. After GPIO initialization, use the following main loop: |
||
| 281 | |||
| 282 | <pre><code class="c"> |
||
| 283 | while (1) |
||
| 284 | { |
||
| 285 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0); |
||
| 286 | HAL_Delay(500); |
||
| 287 | } |
||
| 288 | </code></pre> |
||
| 289 | |||
| 290 | Expected result: PB0 changes level every 500 ms, giving a full period of approximately 1 s. With the indicator rails connected, blue and red alternate. |
||
| 291 | |||
| 292 | h3. Button on PA0 controls PB0 |
||
| 293 | |||
| 294 | Keep PB0 as an output. Configure PA0 as an input without internal pull resistors. The following code replaces the blinking example and accepts a change after at least 20 ms of stability. It runs in the main loop, not in an interrupt handler. Place the initial declarations and assignments before the loop. |
||
| 295 | |||
| 296 | <pre><code class="c"> |
||
| 297 | GPIO_PinState candidate = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0); |
||
| 298 | GPIO_PinState stable = candidate; |
||
| 299 | uint32_t changed_at = HAL_GetTick(); |
||
| 300 | |||
| 301 | while (1) |
||
| 302 | { |
||
| 303 | GPIO_PinState now = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0); |
||
| 304 | uint32_t tick = HAL_GetTick(); |
||
| 305 | |||
| 306 | if (now != candidate) { |
||
| 307 | candidate = now; |
||
| 308 | changed_at = tick; |
||
| 309 | } |
||
| 310 | if ((uint32_t)(tick - changed_at) >= 20U) { |
||
| 311 | stable = candidate; |
||
| 312 | } |
||
| 313 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, |
||
| 314 | stable == GPIO_PIN_RESET ? GPIO_PIN_SET : GPIO_PIN_RESET); |
||
| 315 | } |
||
| 316 | </code></pre> |
||
| 317 | |||
| 318 | Expected result: pressing the button sets PB0 high; releasing it sets PB0 low. With both jumpers fitted, the indication is red when pressed and blue when released. |
||
| 319 | |||
| 320 | h3. Serial output through USART2 |
||
| 321 | |||
| 322 | In CubeMX, configure USART2 on PA2/PA3 for asynchronous operation, 115200 baud, eight data bits, no parity, one stop bit, and no flow control. Generate initialization code and call @MX_USART2_UART_Init()@ before this example. Use the COM port of a VCP-capable probe connected through U5, or an external USB-to-UART adapter with 3.3 V logic. Cross TX to RX and connect GND; do not use RS-232 voltage levels. |
||
| 323 | |||
| 324 | <pre><code class="c"> |
||
| 325 | static const uint8_t message[] = "STM32-UNO ready\r\n"; |
||
| 326 | if (HAL_UART_Transmit(&huart2, message, sizeof(message) - 1U, 100U) |
||
| 327 | != HAL_OK) { |
||
| 328 | Error_Handler(); |
||
| 329 | } |
||
| 330 | </code></pre> |
||
| 331 | |||
| 332 | Expected result: one line appears in the terminal after reset. The board's USB-C connector does not automatically carry this UART output. |
||
| 333 | |||
| 334 | h3. Finding I2C devices |
||
| 335 | |||
| 336 | Configure I2C1 on PB8/PB9 for 100 kHz, seven-bit addressing, and open-drain operation. Initialize @hi2c1@ using the generated function. The connected module's supply must satisfy the CN1 power requirements described above. Place this fragment in a section where static variables and functions may be defined, and call the function after I2C initialization. |
||
| 337 | |||
| 338 | <pre><code class="c"> |
||
| 339 | static volatile uint8_t i2c_found[128]; |
||
| 340 | |||
| 341 | static void ScanI2C1(void) |
||
| 342 | { |
||
| 343 | for (uint16_t address = 0; address < 128U; ++address) { |
||
| 344 | i2c_found[address] = 0U; |
||
| 345 | } |
||
| 346 | for (uint16_t address = 0x08; address <= 0x77; ++address) { |
||
| 347 | if (HAL_I2C_IsDeviceReady(&hi2c1, |
||
| 348 | (uint16_t)(address << 1), 2U, 20U) == HAL_OK) { |
||
| 349 | i2c_found[address] = 1U; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | </code></pre> |
||
| 354 | |||
| 355 | Inspect @i2c_found@ in the debugger. An index containing 1 identifies a seven-bit address that acknowledged the query. HAL accepts the address shifted left by one bit, hence @address << 1@. Scan only devices for which this type of probing is appropriate; then implement reads for a specific sensor according to its protocol. For mikroBUS, use @hi2c3@ and PA8/PC9 in the same way. |
||
| 356 | |||
| 357 | h3. Analog measurement on PC0 |
||
| 358 | |||
| 359 | For the first measurement, select ADC1 IN10 on PC0, available at CONN-ANALOG/6. Connect the outer terminals of a potentiometer, for example 10 kΩ, between 3.3 V and GND, with the wiper connected to PC0. Configure the GPIO in analog mode and the ADC for 12-bit resolution, right alignment, one channel, software triggering, and a single conversion. Start with a longer sampling time and an ADC clock within the permitted range. Initialize @hadc1@. |
||
| 360 | |||
| 361 | <pre><code class="c"> |
||
| 362 | uint32_t raw; |
||
| 363 | float voltage; |
||
| 364 | const float vdda = 3.3f; /* Replace with measured VDDA for accuracy. */ |
||
| 365 | |||
| 366 | if (HAL_ADC_Start(&hadc1) != HAL_OK) { |
||
| 367 | Error_Handler(); |
||
| 368 | } |
||
| 369 | if (HAL_ADC_PollForConversion(&hadc1, 100U) != HAL_OK) { |
||
| 370 | Error_Handler(); |
||
| 371 | } |
||
| 372 | raw = HAL_ADC_GetValue(&hadc1); |
||
| 373 | if (HAL_ADC_Stop(&hadc1) != HAL_OK) { |
||
| 374 | Error_Handler(); |
||
| 375 | } |
||
| 376 | voltage = ((float)raw * vdda) / 4095.0f; |
||
| 377 | </code></pre> |
||
| 378 | |||
| 379 | Expected result: @raw@ varies approximately from 0 to 4095 as the wiper moves. The voltage conversion is approximate and does not include reference, ADC, or signal-source errors. |
||
| 380 | |||
| 381 | The APIs follow the public "STM32F4 HAL GPIO":https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/master/Inc/stm32f4xx_hal_gpio.h, "I2C":https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/master/Inc/stm32f4xx_hal_i2c.h, "UART":https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/master/Inc/stm32f4xx_hal_uart.h, and "ADC":https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/master/Inc/stm32f4xx_hal_adc.h headers. A published tutorial should state the exact package version used. |
||
| 382 | |||
| 383 | h2. Troubleshooting |
||
| 384 | |||
| 385 | |_. Symptom |_. What to check | |
||
| 386 | | ST-LINK cannot find the MCU | Target power, U5/3 as the reference, shared ground, U5/4 and U5/6, cable orientation; try a lower SWD frequency and connection under reset | |
||
| 387 | | No COM port appears after connecting USB | USB-C connects directly to the MCU. For UART, use the probe's VCP or a USB-to-UART adapter; direct USB needs USB firmware | |
||
| 388 | | USB DFU does not work | This circuit lacks the HSE required by the ROM bootloader; use SWD | |
||
| 389 | | Clock configuration ends in Error_Handler | Do not enable an unfitted HSE. Return the first project to HSI at 16 MHz | |
||
| 390 | | Blinking does not change the LEDs | Correct PB0 selection, GPIO initialization, running application, JP1 and JP2; check the signal at CONN_DIG1/2 | |
||
| 391 | | The LED color seems reversed | Logic 0 lights blue; logic 1 lights red. Check both indicator rails | |
||
| 392 | | The button reads backwards | PA0 is active low; SW2 is not BOOT0 | |
||
| 393 | | The button triggers an event several times | Add debouncing, for example accepting a state after 20 ms of stability | |
||
| 394 | | An I2C device does not respond | I2C1 versus I2C3, specific pins, module power, shared ground, seven-bit address, and the HAL address shift | |
||
| 395 | | Analog readings do not make sense | Correct ADC channel, analog mode, signal range, and VDDA; the ref net is not VREF+ | |
||
| 396 | | microSD cannot be mounted | SPI2 rather than SDIO, CS on PB12, slow initialization, disk I/O driver, and card format | |
||
| 397 | | Code disappears after regeneration | Put changes in USER CODE sections or your own files | |
||
| 398 | | A shield intended for UNO does not work | 3.3 V electrical levels, actual signal assignment, and available software support; the connector arrangement alone is insufficient | |