반응형

EmbeddedC 5

[MCU 통신] 구조체 패딩(Struct Padding) 바이트 밀림 현상과 attribute((packed)) 사용법

[Quick Summary - For Global Developers]Symptom: MCU 간 UART/SPI 통신 또는 통신 모듈 데이터 파싱 시, 데이터 필드가 특정 바이트만큼 뒤로 밀려 수신되거나 Alignment Fault로 인해 시스템이 다운됨.Cause: 컴파일러가 32비트 MCU 메모리 접근 효율을 위해 구조체 부전공 데이터 멤버 사이에 무지성으로 구조체 패딩(Struct Padding) 바이트를 자동 삽입하여 물리적 크기가 변경됨.Solution: 구조체 선언부에 attribute((packed)) 속성을 명시하여 컴파일러의 최적화 정렬을 강제로 억제하고 데이터 스트림 바이트를 1:1로 정렬함.구조체 패딩(Struct Padding) 데이터 정렬 불일치로 인한 통신 패킷 바이트 밀림 현상..

Troubleshooting 2026.06.11

[임베디드 C] volatile 키워드 누락으로 인한 컴파일러 최적화 오류 및 무한 루프 해결

[Quick Summary (TL;DR) - For Global Developers]Symptom: 인터럽트 서비스 루틴(ISR) 또는 하드웨어 레지스터 상태 변경 시, 메인 루프(Main Loop)의 조건문이 갱신되지 않고 시스템이 무한 루프(Infinite Loop) 또는 행(Hang) 상태에 빠짐.Cause: volatile 키워드 누락으로 인해 컴파일러 최적화(Compiler Optimization, -O2/-O3) 과정에서 하드웨어 레지스터 및 전역 변수의 RAM 재참조 로직이 제거되고 CPU 레지스터(Register) 값만 반복 조회하도록 잘못 최적화됨.Solution: 메모리 매핑 I/O(MMIO) 레지스터 주소 포인터 및 인터럽트 공유 전역 변수 선언문에 volatile 한정자를 명시하여 ..

Troubleshooting 2026.06.10

[C언어 임베디드] 버퍼 오버플로우(Buffer Overflow) 에러 예방과 안전한 메모리 복사

[Quick Summary (TL;DR) - For Global Developers]Symptom: Unexpected modification of adjacent variables, sudden system resets, or immediate jumps to HardFault_Handler during data parsing.Cause: Writing data beyond the allocated array boundary, which overwrites the Stack Frame (return address) or adjacent global variables in RAM.Solution: Implement strict input validation (Bounds Checking) before a..

Troubleshooting 2026.06.07

[MCU 디버깅] Wild Pointer 및 Dangling Pointer로 인한 시스템 다운 방지 대책

[Quick Summary (TL;DR) - For Global Developers]Symptom: Random system resets, infinite loops in HardFault_Handler, unexpected peripheral behaviors, or silent data corruption that alters operation based on Compiler Optimization levels.Cause: Dereferencing uninitialized pointer variables (Wild Pointer) or accessing memory addresses that have already been deallocated from the heap or stack frame (D..

Troubleshooting 2026.06.06

[임베디드 C] 스택 오버플로우(Stack Overflow) 원인 분석과 MAP 파일 활용한 해결 방법

[Quick Summary (TL;DR) - For Global Developers]Symptom: MCU enters HardFault_Handler or suddenly resets, causing memory corruption and unpredictable runtime behavior.Cause: A thread or function exceeds its allocated stack space due to large local variables, deep recursion, or high interrupt nesting.Solution: Optimize local memory allocation using static or dynamic memory, increase stack size in ..

Troubleshooting 2026.06.05
반응형