Skip to content

Instantly share code, notes, and snippets.

@jeremyd2019
Created September 20, 2018 05:15
Show Gist options
  • Save jeremyd2019/7c497af008544862774b1fc0c4042365 to your computer and use it in GitHub Desktop.
Save jeremyd2019/7c497af008544862774b1fc0c4042365 to your computer and use it in GitHub Desktop.
Atomic bitfield insert for Cortex-M3
#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__ */
@jeremyd2019
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment