Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
e7020e_2019
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ridge
e7020e_2019
Commits
cda22ea3
Commit
cda22ea3
authored
6 years ago
by
Ridge
Browse files
Options
Downloads
Patches
Plain Diff
bare5_2
parent
42377ea6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/bare5.rs
+45
-35
45 additions, 35 deletions
examples/bare5.rs
with
45 additions
and
35 deletions
examples/bare5.rs
+
45
−
35
View file @
cda22ea3
...
@@ -57,12 +57,17 @@ mod stm32f40x {
...
@@ -57,12 +57,17 @@ mod stm32f40x {
// width (field width)
// width (field width)
// value (new value that the field should take)
// value (new value that the field should take)
//
//
// impl VolatileCell<u32> {
impl
VolatileCell
<
u32
>
{
// #[inline(always)]
#[inline(always)]
// pub fn modify(&self, offset: u8, width: u8, value: u32) {
pub
fn
modify
(
&
self
,
offset
:
u8
,
width
:
u8
,
value
:
u32
)
{
// // your code here
let
start_value
=
self
.read
();
// }
let
b
=
0b11111111
;
// }
let
m
=
!
((
b
<<
offset
+
width
)
|
!
(
b
<<
offset
));
let
m_v
=
m
&
(
value
<<
offset
);
let
modi
=
(
start_value
&
!
m
)
|
m_v
;
self
.write
(
modi
);
}
}
#[repr(C)]
#[repr(C)]
#[allow(non_snake_case)]
#[allow(non_snake_case)]
...
@@ -141,27 +146,27 @@ fn wait(i: u32) {
...
@@ -141,27 +146,27 @@ fn wait(i: u32) {
}
}
// simple test of Your `modify`
// simple test of Your `modify`
//
fn test() {
fn
test
()
{
//
let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() };
let
t
:
VolatileCell
<
u32
>
=
unsafe
{
core
::
mem
::
uninitialized
()
};
//
t.write(0);
t
.write
(
0
);
//
assert!(t.read() == 0);
assert!
(
t
.read
()
==
0
);
//
t.modify(3, 3, 0b10101);
t
.modify
(
3
,
3
,
0b10101
);
//offset, width, value
//
//
//
//
// 10101
// 10101
//
// ..0111000
// ..0111000
//
// ---------
// ---------
//
// 000101000
// 000101000
//
assert!(t.read() == 0b101 << 3);
assert!
(
t
.read
()
==
0b101
<<
3
);
//
t.modify(4, 3, 0b10001);
t
.modify
(
4
,
3
,
0b10001
);
//
// 000101000
// 000101000
//
// 111
// 111
//
// 001
// 001
//
// 000011000
// 000011000
//
assert!(t.read() == 0b011 << 3);
assert!
(
t
.read
()
==
0b011
<<
3
);
// if << is used, your code will panic in dev (debug), but not in release mode
// if << is used, your code will panic in dev (debug), but not in release mode
//
t.modify(32, 3, 1);
t
.modify
(
32
,
3
,
1
);
//
}
}
// system startup, can be hidden from the user
// system startup, can be hidden from the user
#[entry]
#[entry]
...
@@ -169,7 +174,7 @@ fn main() -> ! {
...
@@ -169,7 +174,7 @@ fn main() -> ! {
let
rcc
=
unsafe
{
&
mut
*
RCC
::
get
()
};
// get the reference to RCC in memory
let
rcc
=
unsafe
{
&
mut
*
RCC
::
get
()
};
// get the reference to RCC in memory
let
gpioa
=
unsafe
{
&
mut
*
GPIOA
::
get
()
};
// get the reference to GPIOA in memory
let
gpioa
=
unsafe
{
&
mut
*
GPIOA
::
get
()
};
// get the reference to GPIOA in memory
//
test(); // uncomment to run test
test
();
// uncomment to run test
idle
(
rcc
,
gpioa
);
idle
(
rcc
,
gpioa
);
loop
{}
loop
{}
}
}
...
@@ -177,24 +182,29 @@ fn main() -> ! {
...
@@ -177,24 +182,29 @@ fn main() -> ! {
// user application
// user application
fn
idle
(
rcc
:
&
mut
RCC
,
gpioa
:
&
mut
GPIOA
)
{
fn
idle
(
rcc
:
&
mut
RCC
,
gpioa
:
&
mut
GPIOA
)
{
// power on GPIOA
// power on GPIOA
let
r
=
rcc
.AHB1ENR
.read
();
// read
// let r = rcc.AHB1ENR.read(); // read
rcc
.AHB1ENR
.write
(
r
|
1
<<
(
0
));
// set enable
// rcc.AHB1ENR.write(r | 1 << (0)); // set enable
rcc
.AHB1ENR
.modify
(
0
,
1
,
0b1
);
// configure PA5 as output
// configure PA5 as output
let
r
=
gpioa
.MODER
.read
()
&
!
(
0b11
<<
(
5
*
2
));
// read and mask
gpioa
.MODER
.modify
(
5
*
2
,
2
,
0b01
);
gpioa
.MODER
.write
(
r
|
0b01
<<
(
5
*
2
));
// set output mode
// let r = gpioa.MODER.read() & !(0b11 << (5 * 2)); // read and mask
// gpioa.MODER.write(r | 0b01 << (5 * 2)); // set output mode
loop
{
loop
{
// set PA5 high
// 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)
gpioa
.ODR
.write
(
gpioa
.ODR
.read
()
|
(
1
<<
5
));
// gpioa.ODR.write(gpioa.ODR.read() | (1 << 5));
gpioa
.ODR
.modify
(
5
,
1
,
0b1
);
wait
(
10_000
);
wait
(
1_119_982
);
// wait(10_000);
// set PA5 low
// 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)
gpioa
.ODR
.write
(
gpioa
.ODR
.read
()
&
!
(
1
<<
5
));
// gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5));
wait
(
10_000
);
gpioa
.ODR
.modify
(
5
,
1
,
0b0
);
// wait(10_000);
wait
(
1_119_982
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment