Created
September 20, 2018 05:15
-
-
Save jeremyd2019/7c497af008544862774b1fc0c4042365 to your computer and use it in GitHub Desktop.
Atomic bitfield insert for Cortex-M3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SYNC_BFI_H_839745__ | |
#define SYNC_BFI_H_839745__ | |
#include <stdint.h> | |
__attribute__((always_inline)) static inline void sync_bitfield_insert(uint8_t * p, uint8_t v, int bitno) | |
{ | |
uint8_t retval; | |
uint8_t tmp; | |
__asm__ volatile ("dmb ish\n" | |
"sbfi_%=:\n\t" | |
"ldrexb %1, [%3]\n\t" | |
"bfi %1, %4, %5, #1\n\t" | |
"strexb %0, %1, [%3]\n\t" | |
"cmp %0, #0\n\t" | |
"bne sbfi_%=\n\t" | |
"dmb ish" | |
: "=&r" (retval), "=&r" (tmp), "+m" (*p) | |
: "r" (p), "r" (v), "i" (bitno) | |
: "cc"); | |
} | |
#endif /* SYNC_BFI_H_839745__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put this together in a project, and then decided I didn't need it after all. Stashed here in a gist so that it hopefully doesn't go to waste.