1 macro_rules! volatile_read { 2 ($data: expr) => { 3 unsafe { core::ptr::read_volatile(core::ptr::addr_of!($data)) } 4 }; 5 } 6 7 macro_rules! volatile_write { 8 ($data: expr, $value: expr) => { 9 unsafe { core::ptr::write_volatile(core::ptr::addr_of_mut!($data), $value) } 10 }; 11 } 12 13 /// @brief: 用于volatile设置某些bits 14 /// @param val: 设置这些位 15 /// @param flag: true表示设置这些位为1; false表示设置这些位为0; 16 macro_rules! volatile_set_bit { 17 ($data: expr, $val: expr, $flag: expr) => { 18 volatile_write!( 19 $data, 20 match $flag { 21 true => core::ptr::read_volatile(core::ptr::addr_of!($data)) | $val, 22 false => core::ptr::read_volatile(core::ptr::addr_of!($data)) & (!$val), 23 } 24 ) 25 }; 26 } 27 28 /// @param data: volatile变量 29 /// @param bits: 置1的位才有效,表示写这些位 30 /// @param val: 要写的值 31 /// 比如: 写 x 的 2至8bit, 为 10, 可以这么写 volatile_write_bit(x, (1<<8)-(1<<2), 10<<2); 32 macro_rules! volatile_write_bit { 33 ($data: expr, $bits: expr, $val: expr) => { 34 volatile_set_bit!($data, $bits, false); 35 volatile_set_bit!($data, ($val) & ($bits), true); 36 }; 37 } 38