Skip to content
Snippets Groups Projects
Commit 4897099e authored by Jonas Jacobsson's avatar Jonas Jacobsson
Browse files

Did my best

parent 7e456dfe
No related branches found
No related tags found
No related merge requests found
//! bare5.rs
//! rtic_bare5.rs
//!
//! C Like Peripheral API
//!
......@@ -11,6 +11,7 @@
extern crate cortex_m;
extern crate panic_semihosting;
//use bitvec::prelude::*; <-- Might wont to look in to this.
// C like API...
mod stm32f40x {
......@@ -58,7 +59,9 @@ mod stm32f40x {
impl VolatileCell<u32> {
#[inline(always)]
pub fn modify(&self, offset: u8, width: u8, value: u32) {
// your code here
let mut value_mut = value << 32-width;
value_mut = value_mut >> 32-width;
self.write(value_mut << offset);
}
}
......@@ -177,32 +180,31 @@ const APP: () = {
let r = gpioa.MODER.read() & !(0b11 << (5 * 2)); // read and mask
gpioa.MODER.write(r | 0b01 << (5 * 2)); // set output mode
// test_modify();
test_modify();
loop {
// set PA5 high
gpioa.BSRRH.write(1 << 5); // set bit, output hight (turn on led)
//gpioa.BSRRH.write(1 << 5); // set bit, output hight (turn on led)
// alternatively to set the bit high we can
// read the value, or with PA5 (bit 5) and write back
// gpioa.ODR.write(gpioa.ODR.read() | (1 << 5));
gpioa.ODR.write(gpioa.ODR.read() | (1 << 5));
wait(10_000);
// set PA5 low
gpioa.BSRRL.write(1 << 5); // clear bit, output low (turn off led)
//gpioa.BSRRL.write(1 << 5); // clear bit, output low (turn off led)
// alternatively to clear the bit we can
// read the value, mask out PA5 (bit 5) and write back
// gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5));
gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5));
wait(10_000);
}
}
};
// 0. Build and run the application.
//
// > cargo build --example bare5
// (or use the vscode)
//
// 1. C like API.
// Using C the .h files are used for defining interfaces, like function signatures (prototypes),
// structs and macros (but usually not the functions themselves).
......@@ -211,9 +213,8 @@ const APP: () = {
// provided by ST (and other companies). Actually, the file presented here is mostly a
// cut/paste/replace of the stm32f40x.h, just Rustified.
//
//
// In the loop we access PA5 through bit set/clear operations.
// Comment out those operations and uncomment the the ODR accesses.
// Comment out those operations and uncomment the ODR based accesses.
// (They should have the same behavior, but is a bit less efficient.)
//
// Run and see that the program behaves the same.
......@@ -228,12 +229,10 @@ const APP: () = {
//
// Implement and check that running `test` gives you expected behavior.
//
// Change the code into using your new API.
// Change the code into using your new `modify` API.
//
// Run and see that the program behaves the same.
//
// Commit your answers (bare5_2)
//
// Discussion:
// As with arithmetic operations, default semantics differ in between
// debug/dev and release builds.
......@@ -247,4 +246,6 @@ const APP: () = {
// What if we could automatically generate that from Vendors specifications (SVD files)?
// Wouldn't that be great?
//
// ** your answer here **
// I got the first assert to work. But I don't understand the second. What are we supposed to do?
//
// Commit your answers (bare5_2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment