tm4c_hal/bb.rs
1//! Code to handle bit-banding.
2//!
3//! Bit-banding is where the SoC maps each 8-bit byte to 8 consecutive 32-bit
4//! words. Writing a 1 to that word sets the matching bit. Writing a 0 clears
5//! the matching bit. It means you can perform atomic bit set/clear; i.e.
6//! without a read-modify-write.
7
8use core::ptr::{read_volatile, write_volatile};
9use cortex_m::asm::nop;
10
11/// Sets/Clears a bit at the given address atomically, using the bit-banding
12/// feature.
13///
14/// # Safety
15///
16/// We take a const pointer and mutate it, but that's because the
17/// svd2rust crate will only give us const pointers.
18pub unsafe fn change_bit<T>(address: *const T, bit: u8, value: bool) {
19 let address = address as u32;
20 let bit_word = ref_to_bitband(address, bit);
21 write_volatile(bit_word, if value { 0x01 } else { 0x00 });
22}
23
24/// Sets and then Clears a bit at the given address atomically, using the bit-
25/// banding feature.
26///
27/// # Safety
28///
29/// We take a const pointer and mutate it, but that's because
30/// the svd2rust crate will only give us const pointers.
31pub unsafe fn toggle_bit<T>(address: *const T, bit: u8) {
32 let address = address as u32;
33 let bit_word = ref_to_bitband(address, bit);
34 write_volatile(bit_word, 0x01);
35 write_volatile(bit_word, 0x00);
36}
37
38/// Spins while reading a bit at the given address atomically, using the bit-
39/// banding feature. We take a const pointer and mutate it, but that's because
40/// the svd2rust crate will only give us const pointers.
41pub fn spin_bit<T>(address: *const T, bit: u8) {
42 while !read_bit(address, bit) {
43 nop();
44 }
45}
46
47/// Reads a bit at the given address atomically, using the bit-banding
48/// feature.
49pub fn read_bit<T>(address: *const T, bit: u8) -> bool {
50 let address = address as u32;
51 let bit_word = ref_to_bitband(address, bit);
52 unsafe { read_volatile(bit_word) != 0 }
53}
54
55/// Address must be >= 0x2000_0000 and <= 0x2007_FFFC. Bit must be < 32.
56fn ref_to_bitband(address: u32, bit: u8) -> *mut u32 {
57 let prefix = address & 0xF000_0000;
58 let byte_offset = address & 0x0FFF_FFFF;
59 let bit_word_offset = (byte_offset * 32) + (u32::from(bit) * 4);
60 let bit_word_addr = bit_word_offset + prefix + 0x0200_0000;
61 bit_word_addr as *mut u32
62}