Skip to content

Commit 6cfea8d

Browse files
committed
serial partially unmacro
1 parent ffba652 commit 6cfea8d

File tree

1 file changed

+91
-87
lines changed

1 file changed

+91
-87
lines changed

src/serial.rs

Lines changed: 91 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -362,118 +362,118 @@ where
362362
}
363363

364364
macro_rules! hal {
365-
($(
365+
(
366366
$(#[$meta:meta])*
367367
$USARTX:ident: (
368368
$usartX:ident,
369369
$usartX_remap:ident,
370370
$bit:ident,
371371
$closure:expr,
372372
),
373-
)+) => {
374-
$(
375-
$(#[$meta])*
376-
/// The behaviour of the functions is equal for all three USARTs.
377-
/// Except that they are using the corresponding USART hardware and pins.
378-
impl<PINS> Serial<$USARTX, PINS> {
379-
380-
/// Configures the serial interface and creates the interface
381-
/// struct.
382-
///
383-
/// `Bps` is the baud rate of the interface.
384-
///
385-
/// `Clocks` passes information about the current frequencies of
386-
/// the clocks. The existence of the struct ensures that the
387-
/// clock settings are fixed.
388-
///
389-
/// The `serial` struct takes ownership over the `USARTX` device
390-
/// registers and the specified `PINS`
391-
///
392-
/// `MAPR` and `APBX` are register handles which are passed for
393-
/// configuration. (`MAPR` is used to map the USART to the
394-
/// corresponding pins. `APBX` is used to reset the USART.)
395-
pub fn $usartX(
396-
usart: $USARTX,
397-
pins: PINS,
398-
mapr: &mut MAPR,
399-
config: Config,
400-
clocks: Clocks,
401-
) -> Self
402-
where
403-
PINS: Pins<$USARTX>,
404-
{
405-
#[allow(unused_unsafe)]
406-
Serial { usart, pins }.init(
407-
config,
408-
clocks,
409-
|| mapr.modify_mapr(|_, w| unsafe{
410-
#[allow(clippy::redundant_closure_call)]
411-
w.$usartX_remap().$bit(($closure)(PINS::REMAP))
412-
}))
413-
}
414-
373+
) => {
374+
$(#[$meta])*
375+
/// The behaviour of the functions is equal for all three USARTs.
376+
/// Except that they are using the corresponding USART hardware and pins.
377+
impl<PINS> Serial<$USARTX, PINS> {
378+
/// Configures the serial interface and creates the interface
379+
/// struct.
380+
///
381+
/// `Bps` is the baud rate of the interface.
382+
///
383+
/// `Clocks` passes information about the current frequencies of
384+
/// the clocks. The existence of the struct ensures that the
385+
/// clock settings are fixed.
386+
///
387+
/// The `serial` struct takes ownership over the `USARTX` device
388+
/// registers and the specified `PINS`
389+
///
390+
/// `MAPR` and `APBX` are register handles which are passed for
391+
/// configuration. (`MAPR` is used to map the USART to the
392+
/// corresponding pins. `APBX` is used to reset the USART.)
393+
pub fn $usartX(
394+
usart: $USARTX,
395+
pins: PINS,
396+
mapr: &mut MAPR,
397+
config: Config,
398+
clocks: Clocks,
399+
) -> Self
400+
where
401+
PINS: Pins<$USARTX>,
402+
{
403+
#[allow(unused_unsafe)]
404+
Serial { usart, pins }.init(config, clocks, || {
405+
mapr.modify_mapr(|_, w| unsafe {
406+
#[allow(clippy::redundant_closure_call)]
407+
w.$usartX_remap().$bit(($closure)(PINS::REMAP))
408+
})
409+
})
415410
}
411+
}
416412

417-
impl Tx<$USARTX> {
418-
pub fn listen(&mut self) {
419-
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().set_bit()) };
420-
}
421-
422-
pub fn unlisten(&mut self) {
423-
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().clear_bit()) };
424-
}
413+
impl Tx<$USARTX> {
414+
pub fn listen(&mut self) {
415+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().set_bit()) };
425416
}
426417

427-
impl Rx<$USARTX> {
428-
pub fn listen(&mut self) {
429-
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().set_bit()) };
430-
}
431-
432-
pub fn unlisten(&mut self) {
433-
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().clear_bit()) };
434-
}
418+
pub fn unlisten(&mut self) {
419+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().clear_bit()) };
435420
}
421+
}
436422

437-
impl crate::hal::serial::Read<u8> for Rx<$USARTX> {
438-
type Error = Error;
423+
impl Rx<$USARTX> {
424+
pub fn listen(&mut self) {
425+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().set_bit()) };
426+
}
439427

440-
fn read(&mut self) -> nb::Result<u8, Error> {
441-
unsafe { &*$USARTX::ptr() }.read()
442-
}
428+
pub fn unlisten(&mut self) {
429+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().clear_bit()) };
443430
}
431+
}
444432

445-
impl crate::hal::serial::Write<u8> for Tx<$USARTX> {
446-
type Error = Infallible;
433+
impl crate::hal::serial::Read<u8> for Rx<$USARTX> {
434+
type Error = Error;
447435

448-
fn flush(&mut self) -> nb::Result<(), Self::Error> {
449-
unsafe { &*$USARTX::ptr() }.flush()
450-
}
451-
fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
452-
unsafe { &*$USARTX::ptr() }.write(byte)
453-
}
436+
fn read(&mut self) -> nb::Result<u8, Error> {
437+
unsafe { &*$USARTX::ptr() }.read()
454438
}
439+
}
455440

456-
impl<PINS> crate::hal::serial::Read<u8> for Serial<$USARTX, PINS> {
457-
type Error = Error;
441+
impl crate::hal::serial::Write<u8> for Tx<$USARTX> {
442+
type Error = Infallible;
458443

459-
fn read(&mut self) -> nb::Result<u8, Error> {
460-
self.usart.deref().read()
461-
}
444+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
445+
unsafe { &*$USARTX::ptr() }.flush()
446+
}
447+
fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
448+
unsafe { &*$USARTX::ptr() }.write(byte)
462449
}
450+
}
451+
};
452+
}
463453

464-
impl<PINS> crate::hal::serial::Write<u8> for Serial<$USARTX, PINS> {
465-
type Error = Infallible;
454+
impl<USART, PINS> crate::hal::serial::Read<u8> for Serial<USART, PINS>
455+
where
456+
USART: Instance,
457+
{
458+
type Error = Error;
466459

467-
fn flush(&mut self) -> nb::Result<(), Self::Error> {
468-
self.usart.deref().flush()
469-
}
460+
fn read(&mut self) -> nb::Result<u8, Error> {
461+
self.usart.deref().read()
462+
}
463+
}
470464

471-
fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
472-
self.usart.deref().write(byte)
473-
}
474-
}
465+
impl<USART, PINS> crate::hal::serial::Write<u8> for Serial<USART, PINS>
466+
where
467+
USART: Instance,
468+
{
469+
type Error = Infallible;
475470

476-
)+
471+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
472+
self.usart.deref().flush()
473+
}
474+
475+
fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
476+
self.usart.deref().write(byte)
477477
}
478478
}
479479

@@ -497,13 +497,17 @@ hal! {
497497
bit,
498498
|remap| remap == 1,
499499
),
500+
}
501+
hal! {
500502
/// # USART2 functions
501503
USART2: (
502504
usart2,
503505
usart2_remap,
504506
bit,
505507
|remap| remap == 1,
506508
),
509+
}
510+
hal! {
507511
/// # USART3 functions
508512
USART3: (
509513
usart3,

0 commit comments

Comments
 (0)