Skip to content

Instantly share code, notes, and snippets.

@Novakov
Created July 5, 2022 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Novakov/2d921fc26b6743a96a092147b5fad408 to your computer and use it in GitHub Desktop.
Save Novakov/2d921fc26b6743a96a092147b5fad408 to your computer and use it in GitHub Desktop.
void mtb_disable(void) {
MTB_REGS->MTB_MASTER &= ~MTB_MASTER_EN_Msk;
}
void mtb_enable(size_t mtb_size) {
if ((mtb_size < 16) || (__builtin_popcount(mtb_size) != 1)) {
// MTB must be at least 16 bytes and be a power of 2
while (1);
}
// scrub MTB SRAM so it's easy to see what has gotten written to
memset((void *) MTB_REGS->MTB_BASE, 0x00, mtb_size);
const int mask = __builtin_ctz(mtb_size) - 4;
mtb_disable();
MTB_REGS->MTB_POSITION = 0;
MTB_REGS->MTB_MASTER = MTB_MASTER_EN(1) | MTB_MASTER_MASK(mask);
}
int main(int argc, char** argv) {
mtb_enable(4096);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment