embedded-stm32
БесплатноБез исполняемых скриптовНе проверенBest practices for embedded C/C++ development on STM32 microcontrollers using the HAL, covering peripherals, DMA, interrupts, memory constraints, and…
от Mindrally★ 323 звезды на GitHubОб этом скилле
Embedded STM32 / HAL Development
This skill covers firmware development for STM32 microcontrollers using the STM32 HAL, including project structure, peripheral and interrupt handling, memory and timing constraints, and testing strategies for hardware-focused code.
Workflow for STM32 HAL Firmware Development
- Configure the hardware in CubeMX — Set up clocks, pins, and peripherals in the
.iocfile; generate the HAL initialization code. - Separate generated and hand-written code — Keep CubeMX-generated files untouched except in their designated
USER CODE BEGIN/ENDblocks; put application logic in separate files. - Initialize peripherals once — Centralize
HAL_*_Init()calls inmain()/MX_*_Init()and avoid ad hoc reconfiguration elsewhere in the code. - Write interrupt handlers — Keep ISRs (
HAL_*_Callbackfunctions,EXTI/DMA/timer IRQ handlers) short; set flags or push to a queue and defer real work to the main loop or an RTOS task. - Use DMA for high-throughput I/O — Configure DMA for UART/SPI/I2C/ADC transfers that would otherwise block or burn CPU cycles on polling.
- Add timeouts everywhere — Every blocking HAL call and every hardware wait loop needs a timeout and an explicit error path.
- Test in layers — Unit-test pure logic on a host build (no hardware dependency), then validate peripheral behavior with hardware-in-the-loop tests.
- Flash and debug — Use SWD/JTAG (ST-Link, OpenOCD, or J-Link) with a debugger, plus rate-limited serial logs, to verify behavior on real hardware.
Project Structure
- Keep board support (pin/clock configuration), drivers, middleware (e.g., FreeRTOS, USB stack), application logic, and tests in clearly separated directories.
- Isolate CubeMX-generated or vendor code (
Core/Src/main.c,Drivers/) from hand-written application code so regenerating with CubeMX doesn't clobber custom logic — only edit inside/* USER CODE BEGIN */ ... /* USER CODE END */markers in generated files. - Put hardware access behind narrow interfaces (e.g., a
motor_driver.hwithmotor_set_speed()) so application logic can be unit-tested on a host build without real peripherals. - Document the clock tree (
SYSCLK,HCLK,PCLK1/PCLK2and their max rates), pin mappings, peripheral ownership, and interrupt priority assignments in a single reference (README or header comments) — this is the first thing a debugging session needs.
STM32 HAL and Peripherals
- Initialize each peripheral in exactly one place; avoid scattering
HAL_*_Init()/HAL_*_MspInit()calls or ad hoc register writes across multiple files. - Always check the return value of HAL calls (
HAL_OK,HAL_ERROR,HAL_BUSY,HAL_TIMEOUT) and handle timeout/error cases explicitly — a silently ignoredHAL_TIMEOUTfromHAL_UART_Transmitis a classic source of "it works on my desk" bugs. - Keep blocking HAL calls (
HAL_UART_Transmit,HAL_I2C_Master_Receivewithout_IT/_DMAsuffix) out of time-critical paths like control loops or ISRs. - Use DMA for high-throughput UART, SPI, I2C, ADC, or timer-capture paths when the CPU shouldn't spend cycles byte-shuffling.
- Document buffer ownership and lifetime for every DMA operation — a buffer being read by DMA must not be modified or freed by the CPU until the transfer-complete callback fires.
- Use
volatileonly for memory shared with an ISR or memory-mapped hardware registers;volatileis not a substitute for a proper memory barrier or critical section when data is shared between contexts.
Example: Non-Blocking UART Receive with DMA and Idle-Line Detection
/* USER CODE BEGIN Includes */
#include "main.h"
#include <string.h>
#define RX_BUF_SIZE 128
static uint8_t rx_buf[RX_BUF_SIZE];
static volatile uint8_t rx_ready = 0;
static volatile uint16_t rx_len = 0;
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart2_rx;
/* USER CODE END Includes */
/* USER CODE BEGIN 2 */
void app_uart_start_receive(void)
{
/* Enable idle-line interrupt so a packet of unknown length completes
* the transfer without waiting for the buffer to fill. */
__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
if (HAL_UART_Receive_DMA(&huart2, rx_buf, RX_BUF_SIZE) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE END 2 */
/* USER CODE BEGIN 4 */
void USART2_IRQHandler(void)
{
if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE)) {
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
HAL_UART_DMAStop(&huart2);
rx_len = RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);
rx_ready = 1; /* Deferred: main loop processes the packet. */
/* Re-arm for the next packet. */
HAL_UART_Receive_DMA(&huart2, rx_buf, RX_BUF_SIZE);
return;
}
HAL_UART_IRQHandler(&huart2);
}
/* USER CODE END 4 */
/* Main loop excerpt: heavy work deferred out of the ISR. */
void app_main_loop(void)
{
if (rx_ready) {
rx_ready = 0;
uint16_t len = rx_len;
Установить embedded-stm32 в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл embedded-stm32?
Best practices for embedded C/C++ development on STM32 microcontrollers using the HAL, covering peripherals, DMA, interrupts, memory constraints, and hardware-focused testing. Use when writing STM32 HAL code, configuring peripherals generated by STM32CubeMX, working with interrupts or DMA, debugging with SWD/JTAG, managing memory-constrained firmware, or writing hardware-in-the-loop or host-build tests for embedded C.
Как установить скилл embedded-stm32?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл embedded-stm32 запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic