Skip to content

Mark bit banding function unsafe (and some minor fixes) #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- MonoTimer now takes ownership of the DCB register
- SPI objects now have a `FrameSize` type field
- Bit banding functions (`bb::*`) are now correctly marked as unsafe

### Added

- Add 16 bit dataframe size for `SPI`
- Implement `timer::Cancel` trait for `CountDownTimer`
- Changing Output pin slew rates through the OutputSpeed trait

### Added

- Add support for ADC continuous conversion
- Add supoort for ADC discontinuous mode

Expand Down
8 changes: 4 additions & 4 deletions src/bb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

use core::ptr;

pub fn clear<T>(register: *const T, bit: u8) {
pub unsafe fn clear<T>(register: *const T, bit: u8) {
write(register, bit, false);
}

pub fn set<T>(register: *const T, bit: u8) {
pub unsafe fn set<T>(register: *const T, bit: u8) {
write(register, bit, true);
}

pub fn write<T>(register: *const T, bit: u8, set: bool) {
pub unsafe fn write<T>(register: *const T, bit: u8, set: bool) {
let addr = register as usize;

assert!(addr >= 0x4000_0000 && addr <= 0x4010_0000);
assert!(bit < 32);

let bit = bit as usize;
let bb_addr = (0x4200_0000 + (addr - 0x4000_0000) * 32) + 4 * bit;
unsafe { ptr::write_volatile(bb_addr as *mut u32, if set { 1 } else { 0 }) }
ptr::write_volatile(bb_addr as *mut u32, if set { 1 } else { 0 })
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
//! [README]: https://github.com/stm32-rs/stm32f1xx-hal/tree/v0.6.1

#![no_std]
#![deny(intra_doc_link_resolution_failure)]
#![deny(broken_intra_doc_links)]

// If no target specified, print error message.
#[cfg(not(any(
Expand Down