Skip to content

Instantly share code, notes, and snippets.

@karlp
Created January 8, 2020 10:28
Show Gist options
  • Save karlp/eda0f04e46e4846ae3f5bd07c3383c35 to your computer and use it in GitHub Desktop.
Save karlp/eda0f04e46e4846ae3f5bd07c3383c35 to your computer and use it in GitHub Desktop.
$ ./rmw
set: from initial: 0xffffffff, setting mode: 6 => 0xffff6fff
set2: from initial: 0xffffffff, setting mode: 6 => 0xffff6fff
set: from initial: 0, setting mode: 6 => 0 <<<< failed to set properly.
set2: from initial: 0, setting mode: 6 => 0x6000
#include <stdint.h>
#include <stdio.h>
void set(uint32_t initial, uint32_t mode) {
uint32_t reg = initial;
uint32_t mask = 0xf << 12;
reg &= ~mask | (mode << 12);
printf("set: from initial: %#x, setting mode: %x => %#x\n", initial, mode, reg);
}
void set2(uint32_t initial, uint32_t mode) {
uint32_t reg = initial;
uint32_t mask = 0xf;
uint32_t shift = 12;
reg = (reg & ~(mask << shift)) | (mode << shift);
printf("set2: from initial: %#x, setting mode: %x => %#x\n", initial, mode, reg);
}
void test_val(uint32_t initial, uint32_t mode) {
set(initial, mode);
set2(initial, mode);
}
int main() {
test_val(0xffffffff, 6);
test_val(0x0, 6);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment