How do I write to a memory-mapped address in Rust?

fevgenym

I'm trying to make "Blinky" for STM32F1xx in Rust. I know that there are libs for it, but I want to make my own "lib" for learning purposes.

I can access STM32's "registers" by their addresses like this in C:

*(uint32_t*)(0x40021000 + 0x018) |= 0x10;
*(uint32_t*)(0x40011000 + 0x004) |= 0x33;
*(uint32_t*)(0x40011000 + 0x004) &= ~0xCC;
*(uint32_t*)(0x40011000 + 0x10) |= 0x300;

while(1) {}

This writes some bits to the RCC_APB2ENR register to enable clocking of port C, configures pins and enables LEDs on my Discovery.

I need to re-write this it in Rust, to make consts, fns and start writing nice Rusty code. Is it possible in Rust without FFI calling C code? Can I achieve this with the asm! macro?

starblue

In C you should declare your pointers as volatile when accessing hardware registers, so that the compiler does the accesses exactly as you program them. Otherwise it could reorder them or eliminate duplicate accesses to the same register.

Since Rust 1.9 (thanks to this RFC) you can use core::ptr::read_volatile and core::ptr::write_volatile to read and write to such memory.

If you have an older version of Rust, these are available as volatile_read and volatile_store in core::intrinsics, which however are permanently unstable, and thus require a nightly version of Rust to access them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Write in the specific memory address with Rust

How do I write a pipe filter in Rust?

How do I write to a virtual file in Rust?

How to create and write to memory mapped files?

How do I send a UDP datagram with source address 0.0.0.0 in Rust?

How do I encode a Rust Piston image and get the result in memory?

How do I declare a static variable in a specific memory region in Rust?

What happens when I write to a memory-mapped register?

How do I write to a specific raw file descriptor from Rust?

How do I write to a file from different threads in Rust?

How do I write inline assembly for `"={ecx}"(features)` in stable Rust?

How do I modify a font and save it with Rust 'write-fonts'?

How do I initialize a char array with a memory address in C?

How do I write a macro to repeat something up to a specific address?

Python How do I write a user provided IP address to a file?

How do I communicate between a server and a client on the same computer using memory mapped files?

Passing a memory address in Rust

How do I write a rust function that can both read and write to a cache?

How can I obtain the address of a closure in Rust?

How do I bind a mapped function?

How to do memory mapped IO on custom data types?

How to do Binary Search over memory mapped compressed file in java?

how can I detect whether a specific page is mapped in memory?

What happens when I write to a random memory address?

How do you write to a pty master Rust

How do I use the Rust memory allocator for a C library that can be provided an allocator?

How can I understand if a memory address is used or not?

c++ boost write memory mapped file

When should I prefer write-combined CUDA-allocated mapped host memory?