Skip to content

Commit ac4fe90

Browse files
authored
Merge pull request #29 from robyoung/rewrite-serial_port-as-macro
Rewrite serial_port as a macro
2 parents 362108a + 68c3767 commit ac4fe90

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

examples/i2c_hal_printmagserial.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ fn main() -> ! {
4848
let _ = i2c.write(0xE, &[0x11, 0x7f]);
4949

5050
/* Initialise serial port on the micro:bit */
51-
//let (mut tx, _) = microbit::serial_port(gpio, p.UART0, BAUD115200);
52-
53-
/* Configure RX and TX pins accordingly */
54-
let tx = gpio.pin24.into_push_pull_output().into();
55-
let rx = gpio.pin25.into_floating_input().into();
56-
57-
/* Set up serial port using the prepared pins */
58-
let (mut tx, _) = serial::Serial::uart0(p.UART0, tx, rx, BAUD115200).split();
51+
let (mut tx, _) = microbit::serial_port!(gpio, p.UART0, BAUD115200);
5952

6053
let _ = write!(&mut tx, "\n\rWelcome to the magnetometer reader!\n\r");
6154

src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@ pub use nb::*;
77

88
pub use crate::hal::nrf51::*;
99

10-
use crate::hal::gpio::gpio::Parts;
11-
use crate::hal::serial::*;
12-
1310
pub mod display;
1411
pub mod led;
1512

16-
// FIXME: Rewrite as macro to prevent problems consuming parts of gpio
17-
pub fn serial_port(
18-
gpio: Parts,
19-
uart: hal::nrf51::UART0,
20-
speed: BAUDRATE_A,
21-
) -> (Tx<hal::nrf51::UART0>, Rx<hal::nrf51::UART0>) {
22-
/* Configure RX and TX pins accordingly */
23-
let tx = gpio.pin24.into_push_pull_output().into();
24-
let rx = gpio.pin25.into_floating_input().into();
13+
#[macro_export]
14+
macro_rules! serial_port {
15+
( $gpio:expr, $uart:expr, $speed:expr ) => {{
16+
use nrf51_hal::serial::Serial;
17+
18+
/* Configure RX and TX pins accordingly */
19+
let tx = $gpio.pin24.into_push_pull_output().into();
20+
let rx = $gpio.pin25.into_floating_input().into();
2521

26-
/* Set up serial port using the prepared pins */
27-
let serial = Serial::uart0(uart, tx, rx, speed);
28-
serial.split()
22+
/* Set up serial port using the prepared pins */
23+
let serial = Serial::uart0($uart, tx, rx, $speed);
24+
serial.split()
25+
}};
2926
}

0 commit comments

Comments
 (0)