Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 22fb4a7

Browse files
committed
Fix issues found by code review, add more documentation.
1 parent 02cb500 commit 22fb4a7

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

src/debug.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
//! Interacting with debugging agent
2+
//!
3+
//! # Example
4+
//!
5+
//! This example will show how to terminate the QEMU session. The program
6+
//! should be running under QEMU with semihosting enabled
7+
//! (use `-semihosting` flag).
8+
//!
9+
//! Target program:
10+
//!
11+
//! ```
12+
//! #[macro_use]
13+
//! extern crate cortex_m_semihosting;
14+
//! use cortex_m_semihosting::debug;
15+
//!
16+
//! fn main() {
17+
//! if 2 == 2 {
18+
//! // report success
19+
//! debug::exit(0);
20+
//! } else {
21+
//! // report failure
22+
//! debug::exit(1);
23+
//! }
24+
//! }
25+
//!
226
27+
/// This values are taken from section 5.5.2 of
28+
/// "ADS Debug Target Guide" (DUI0058)
329
pub enum Exception {
430
// Hardware reason codes
531
BranchThroughZero = 0x20000,
@@ -26,7 +52,13 @@ pub enum Exception {
2652
/// Reports to the debugger that the execution has completed.
2753
///
2854
/// If `status` is not 0 then an error is reported.
29-
/// This call may not return.
55+
///
56+
/// This call can be used to terminate QEMU session, and report back success
57+
/// or failure.
58+
///
59+
/// This call should not return. However, it is possible for the debugger
60+
/// to request that the application continue. In that case this call
61+
/// returns normally.
3062
///
3163
pub fn exit(status: i8) {
3264
if status == 0 {
@@ -38,7 +70,12 @@ pub fn exit(status: i8) {
3870

3971
/// Report an exception to the debugger directly.
4072
///
41-
/// This call may not return.
73+
/// Exception handlers can use this SWI at the end of handler chains
74+
/// as the default action, to indicate that the exception has not been handled.
75+
///
76+
/// This call should not return. However, it is possible for the debugger
77+
/// to request that the application continue. In that case this call
78+
/// returns normally.
4279
///
4380
/// # Arguments
4481
///
@@ -47,6 +84,6 @@ pub fn exit(status: i8) {
4784
pub fn report_exception(reason: Exception) {
4885
let code = reason as usize;
4986
unsafe {
50-
syscall!(REPORT_EXCEPTION, code);
87+
syscall1!(REPORT_EXCEPTION, code);
5188
}
5289
}

src/lib.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
//!
2929
//! Target program:
3030
//!
31-
//! ```rust,ignore
31+
//! ```
32+
//! #[macro_use]
33+
//! extern crate cortex_m_semihosting;
34+
//!
3235
//! fn main() {
3336
//! // File descriptor (on the host)
3437
//! const STDOUT: usize = 1;
@@ -112,10 +115,28 @@ pub mod io;
112115
pub mod nr;
113116
pub mod debug;
114117

115-
/// Performs a semihosting operation
118+
/// Performs a semihosting operation, takes a pointer to an argument block
119+
#[inline(always)]
120+
#[cfg(target_arch = "arm")]
121+
pub unsafe fn syscall<T>(mut nr: usize, arg: &T) -> usize {
122+
asm!("bkpt 0xAB"
123+
: "+{r0}"(nr)
124+
: "{r1}"(arg)
125+
: "memory"
126+
: "volatile");
127+
nr
128+
}
129+
130+
/// Performs a semihosting operation, takes a pointer to an argument block
131+
#[cfg(not(target_arch = "arm"))]
132+
pub unsafe fn syscall<T>(_nr: usize, _arg: &T) -> usize {
133+
0
134+
}
135+
136+
/// Performs a semihosting operation, takes one integer as an argument
116137
#[inline(always)]
117138
#[cfg(target_arch = "arm")]
118-
pub unsafe fn syscall<T: Sized>(mut nr: usize, arg: T) -> usize {
139+
pub unsafe fn syscall1(mut nr: usize, arg: usize) -> usize {
119140
asm!("bkpt 0xAB"
120141
: "+{r0}"(nr)
121142
: "{r1}"(arg)
@@ -124,7 +145,8 @@ pub unsafe fn syscall<T: Sized>(mut nr: usize, arg: T) -> usize {
124145
nr
125146
}
126147

148+
/// Performs a semihosting operation, takes one integer as an argument
127149
#[cfg(not(target_arch = "arm"))]
128-
pub unsafe fn syscall<T: Sized>(_nr: usize, _arg: T) -> usize {
150+
pub unsafe fn syscall1(_nr: usize, _arg: usize) -> usize {
129151
0
130152
}

src/macros.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#[macro_export]
33
macro_rules! syscall {
44
($nr:ident) => {
5-
$crate::syscall($crate::nr::$nr, 0usize)
5+
$crate::syscall1($crate::nr::$nr, 0)
66
};
77
($nr:ident, $a1:expr) => {
8-
$crate::syscall($crate::nr::$nr, $a1 as usize)
8+
$crate::syscall($crate::nr::$nr, &[$a1 as usize])
99
};
1010
($nr:ident, $a1:expr, $a2:expr) => {
1111
$crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize])
@@ -20,6 +20,14 @@ macro_rules! syscall {
2020
};
2121
}
2222

23+
/// Macro version of `syscall1`
24+
#[macro_export]
25+
macro_rules! syscall1 {
26+
($nr:ident, $a1:expr) => {
27+
$crate::syscall1($crate::nr::$nr, $a1 as usize)
28+
};
29+
}
30+
2331
/// Macro for printing to the **host's** standard stderr
2432
#[macro_export]
2533
macro_rules! ehprint {

0 commit comments

Comments
 (0)