레지스터 값을 비트 단위로 확인하고 펌웨어용 마스크를 생성합니다.
High Word (Bits 31-16)
Low Word (Bits 15-00)
FIELD NOTE · REGISTER
레지스터 값은 숫자 하나지만, 각 비트의 의미와 쓰기 동작은 주변장치마다 다릅니다. 이 분석기는 선택한 비트를 마스크로 변환하고 C 매크로를 만드는 과정에서 사람이 자주 내는 자리수 오류를 줄이기 위한 도구입니다.
비트 n의 마스크는 1U << n입니다. 여러 비트를 선택하면 각 마스크를 OR 연산해 SET 마스크를 만들고, CLEAR 마스크는 비트 폭 안에서 이를 반전합니다. 32비트 값은 0xFFFFFFFF, 16비트 값은 0xFFFF 범위로 제한해야 상위 비트가 의도치 않게 포함되지 않습니다.
CONTROL_REG에서 bit 7과 bit 3을 켠다면 SET 마스크는 0x00000088입니다. 기존 값을 유지하며 설정할 때는 REG |= 0x88U, 두 비트만 끌 때는 REG &= ~0x88U를 사용합니다. 필드 전체를 새 값으로 바꿀 때는 기존 필드를 지운 뒤 이동한 값을 OR 해야 합니다.
모든 레지스터에 일반적인 read-modify-write를 사용할 수 있는 것은 아닙니다. W1C(1을 쓰면 클리어), write-only, reserved bit, self-clearing bit는 데이터시트의 쓰기 규칙을 따라야 합니다. 인터럽트와 동시에 변경되는 레지스터는 원자성 또는 임계 구역도 검토해야 합니다.
왜 CLEAR 마스크 앞에 ~가 붙나요? 선택하지 않은 비트는 1로 유지하고 선택한 비트만 0으로 만들어 AND 연산하기 위해서입니다.
마스크가 맞으면 바로 하드웨어에 써도 되나요? 아닙니다. 비트 위치 계산과 레지스터 접근 규칙은 별개이므로 반드시 해당 MCU의 레퍼런스 매뉴얼을 확인해야 합니다.
signed 정수를 써도 되나요? 최상위 비트 이동 시 구현 차이와 경고를 피하려면 1U, UINT32_C(1) 같은 unsigned 상수를 권장합니다.
FIELD NOTE · REGISTER
A register is one number, but every bit may follow a different hardware rule. This analyzer reduces position and transcription errors when converting selected bits into hexadecimal masks and C macros.
The mask for bit n is 1U << n. Masks are ORed to create a SET mask; the width-limited inverse becomes the CLEAR mask. Limit results to 0xFFFF for 16-bit and 0xFFFFFFFF for 32-bit registers.
Selecting bits 7 and 3 produces 0x00000088. Use REG |= 0x88U to set them without changing other bits and REG &= ~0x88U to clear them. Replacing a field requires clearing the old field before ORing the shifted value.
Read-modify-write is unsafe for some W1C, write-only, reserved, or self-clearing bits. Follow the peripheral reference manual. Registers modified by interrupts may also require an atomic operation or critical section.
Why invert the CLEAR mask? AND needs ones for preserved positions and zeros only for the bits being cleared.
Can a correct mask be written directly? Not automatically. Bit arithmetic and the peripheral's write semantics are separate concerns.
Should signed integers be used? Prefer 1U or UINT32_C(1) to avoid sign-extension and top-bit shift issues.