Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
stm32f423-rtfm-aes
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
stm32f423-rtfm-aes
Commits
c8bdb732
Commit
c8bdb732
authored
7 years ago
by
Jonas Schievink
Browse files
Options
Downloads
Patches
Plain Diff
Add late resources example
parent
7ebba496
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/late-resources.rs
+64
-0
64 additions, 0 deletions
examples/late-resources.rs
with
64 additions
and
0 deletions
examples/late-resources.rs
0 → 100644
+
64
−
0
View file @
c8bdb732
//! Demonstrates initialization of resources in `init`.
#![deny(unsafe_code)]
#![feature(proc_macro)]
#![no_std]
extern
crate
cortex_m_rtfm
as
rtfm
;
extern
crate
stm32f103xx
;
use
rtfm
::{
app
,
Threshold
};
app!
{
device
:
stm32f103xx
,
resources
:
{
// Usually, resources are initialized with a constant initializer:
static
ON
:
bool
=
false
;
// However, there are cases where this is not possible or not desired. For example, there
// may not be a sensible value to use, or the type may not be constructible in a constant
// (like `Vec`).
// While it is possible to use an `Option` in some cases, that requires you to properly
// initialize it and `.unwrap()` it at every use. It also consumes more memory.
//
// To solve this, it is possible to defer initialization of resources to `init` by omitting
// the initializer. Doing that will require `init` to return the values of all "late"
// resources.
static
IP_ADDRESS
:
u32
;
},
tasks
:
{
SYS_TICK
:
{
path
:
sys_tick
,
resources
:
[
IP_ADDRESS
,
ON
],
},
}
}
// The signature of `init` is now required to have a specific return type.
fn
init
(
_p
:
init
::
Peripherals
,
_r
:
init
::
Resources
)
->
init
::
LateResourceValues
{
// `init::Resources` does not contain `IP_ADDRESS`, since it is not yet initialized.
//_r.IP_ADDRESS; // doesn't compile
// ...obtain value for IP_ADDRESS from EEPROM/DHCP...
let
ip_address
=
0x7f000001
;
init
::
LateResourceValues
{
// This struct will contain fields for all resources with omitted initializers.
IP_ADDRESS
:
ip_address
,
}
}
fn
sys_tick
(
_t
:
&
mut
Threshold
,
r
:
SYS_TICK
::
Resources
)
{
// Other tasks can access late resources like any other, since they are guaranteed to be
// initialized when tasks are run.
r
.IP_ADDRESS
;
}
fn
idle
()
->
!
{
loop
{
rtfm
::
wfi
();
}
}
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