serial.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. //! # Serial Communication (USART)
  2. //!
  3. //! This module contains the functions to utilize the USART (Universal
  4. //! synchronous asynchronous receiver transmitter)
  5. //!
  6. //! ## Example usage:
  7. //!
  8. //! ```rust
  9. //! // prelude: create handles to the peripherals and registers
  10. //! let p = crate::pac::Peripherals::take().unwrap();
  11. //! let cp = cortex_m::Peripherals::take().unwrap();
  12. //! let mut flash = p.FLASH.constrain();
  13. //! let mut rcc = p.RCC.constrain();
  14. //! let clocks = rcc.cfgr.freeze(&mut flash.acr);
  15. //! let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  16. //! let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  17. //!
  18. //! // USART1 on Pins A9 and A10
  19. //! let pin_tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  20. //! let pin_rx = gpioa.pa10;
  21. //! // Create an interface struct for USART1 with 9600 Baud
  22. //! let serial = Serial::usart1(
  23. //! p.USART1,
  24. //! (pin_tx, pin_rx),
  25. //! &mut afio.mapr,
  26. //! Config::default().baudrate(9_600.bps()),
  27. //! clocks,
  28. //! &mut rcc.apb2,
  29. //! );
  30. //!
  31. //! // separate into tx and rx channels
  32. //! let (mut tx, mut rx) = serial.split();
  33. //!
  34. //! // Write 'R' to the USART
  35. //! block!(tx.write(b'R')).ok();
  36. //! // Receive a byte from the USART and store it in "received"
  37. //! let received = block!(rx.read()).unwrap();
  38. //! ```
  39. use core::marker::PhantomData;
  40. use core::ops::Deref;
  41. use core::ptr;
  42. use core::sync::atomic::{self, Ordering};
  43. use crate::pac::{USART1, USART2, USART3};
  44. use core::convert::Infallible;
  45. use embedded_dma::{StaticReadBuffer, StaticWriteBuffer};
  46. use embedded_hal::serial::Write;
  47. use crate::afio::MAPR;
  48. use crate::dma::{dma1, CircBuffer, RxDma, Transfer, TxDma, R, W};
  49. use crate::gpio::gpioa::{PA10, PA2, PA3, PA9};
  50. use crate::gpio::gpiob::{PB10, PB11, PB6, PB7};
  51. use crate::gpio::gpioc::{PC10, PC11};
  52. use crate::gpio::gpiod::{PD5, PD6, PD8, PD9};
  53. use crate::gpio::{Alternate, Floating, Input, PushPull};
  54. use crate::rcc::{sealed::RccBus, Clocks, Enable, GetBusFreq, Reset, APB1, APB2};
  55. use crate::time::{Bps, U32Ext};
  56. /// Interrupt event
  57. pub enum Event {
  58. /// New data has been received
  59. Rxne,
  60. /// New data can be sent
  61. Txe,
  62. }
  63. /// Serial error
  64. #[derive(Debug)]
  65. #[non_exhaustive]
  66. pub enum Error {
  67. /// Framing error
  68. Framing,
  69. /// Noise error
  70. Noise,
  71. /// RX buffer overrun
  72. Overrun,
  73. /// Parity check error
  74. Parity,
  75. }
  76. // USART REMAPPING, see: https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf
  77. // Section 9.3.8
  78. pub trait Pins<USART> {
  79. const REMAP: u8;
  80. }
  81. impl Pins<USART1> for (PA9<Alternate<PushPull>>, PA10<Input<Floating>>) {
  82. const REMAP: u8 = 0;
  83. }
  84. impl Pins<USART1> for (PB6<Alternate<PushPull>>, PB7<Input<Floating>>) {
  85. const REMAP: u8 = 1;
  86. }
  87. impl Pins<USART2> for (PA2<Alternate<PushPull>>, PA3<Input<Floating>>) {
  88. const REMAP: u8 = 0;
  89. }
  90. impl Pins<USART2> for (PD5<Alternate<PushPull>>, PD6<Input<Floating>>) {
  91. const REMAP: u8 = 0;
  92. }
  93. impl Pins<USART3> for (PB10<Alternate<PushPull>>, PB11<Input<Floating>>) {
  94. const REMAP: u8 = 0;
  95. }
  96. impl Pins<USART3> for (PC10<Alternate<PushPull>>, PC11<Input<Floating>>) {
  97. const REMAP: u8 = 1;
  98. }
  99. impl Pins<USART3> for (PD8<Alternate<PushPull>>, PD9<Input<Floating>>) {
  100. const REMAP: u8 = 0b11;
  101. }
  102. pub enum Parity {
  103. ParityNone,
  104. ParityEven,
  105. ParityOdd,
  106. }
  107. pub enum StopBits {
  108. #[doc = "1 stop bit"]
  109. STOP1,
  110. #[doc = "0.5 stop bits"]
  111. STOP0P5,
  112. #[doc = "2 stop bits"]
  113. STOP2,
  114. #[doc = "1.5 stop bits"]
  115. STOP1P5,
  116. }
  117. pub struct Config {
  118. pub baudrate: Bps,
  119. pub parity: Parity,
  120. pub stopbits: StopBits,
  121. }
  122. impl Config {
  123. pub fn baudrate(mut self, baudrate: Bps) -> Self {
  124. self.baudrate = baudrate;
  125. self
  126. }
  127. pub fn parity_none(mut self) -> Self {
  128. self.parity = Parity::ParityNone;
  129. self
  130. }
  131. pub fn parity_even(mut self) -> Self {
  132. self.parity = Parity::ParityEven;
  133. self
  134. }
  135. pub fn parity_odd(mut self) -> Self {
  136. self.parity = Parity::ParityOdd;
  137. self
  138. }
  139. pub fn stopbits(mut self, stopbits: StopBits) -> Self {
  140. self.stopbits = stopbits;
  141. self
  142. }
  143. }
  144. impl Default for Config {
  145. fn default() -> Config {
  146. let baudrate = 115_200_u32.bps();
  147. Config {
  148. baudrate,
  149. parity: Parity::ParityNone,
  150. stopbits: StopBits::STOP1,
  151. }
  152. }
  153. }
  154. /// Serial abstraction
  155. pub struct Serial<USART, PINS> {
  156. usart: USART,
  157. pins: PINS,
  158. }
  159. /// Serial receiver
  160. pub struct Rx<USART> {
  161. _usart: PhantomData<USART>,
  162. }
  163. /// Serial transmitter
  164. pub struct Tx<USART> {
  165. _usart: PhantomData<USART>,
  166. }
  167. /// Internal trait for the serial read / write logic.
  168. trait UsartReadWrite: Deref<Target = crate::pac::usart1::RegisterBlock> {
  169. fn read(&self) -> nb::Result<u8, Error> {
  170. let sr = self.sr.read();
  171. // Check for any errors
  172. let err = if sr.pe().bit_is_set() {
  173. Some(Error::Parity)
  174. } else if sr.fe().bit_is_set() {
  175. Some(Error::Framing)
  176. } else if sr.ne().bit_is_set() {
  177. Some(Error::Noise)
  178. } else if sr.ore().bit_is_set() {
  179. Some(Error::Overrun)
  180. } else {
  181. None
  182. };
  183. if let Some(err) = err {
  184. // Some error occurred. In order to clear that error flag, you have to
  185. // do a read from the sr register followed by a read from the dr
  186. // register
  187. // NOTE(read_volatile) see `write_volatile` below
  188. unsafe {
  189. ptr::read_volatile(&self.sr as *const _ as *const _);
  190. ptr::read_volatile(&self.dr as *const _ as *const _);
  191. }
  192. Err(nb::Error::Other(err))
  193. } else {
  194. // Check if a byte is available
  195. if sr.rxne().bit_is_set() {
  196. // Read the received byte
  197. // NOTE(read_volatile) see `write_volatile` below
  198. Ok(unsafe { ptr::read_volatile(&self.dr as *const _ as *const _) })
  199. } else {
  200. Err(nb::Error::WouldBlock)
  201. }
  202. }
  203. }
  204. fn write(&self, byte: u8) -> nb::Result<(), Infallible> {
  205. let sr = self.sr.read();
  206. if sr.txe().bit_is_set() {
  207. // NOTE(unsafe) atomic write to stateless register
  208. // NOTE(write_volatile) 8-bit write that's not possible through the svd2rust API
  209. unsafe { ptr::write_volatile(&self.dr as *const _ as *mut _, byte) }
  210. Ok(())
  211. } else {
  212. Err(nb::Error::WouldBlock)
  213. }
  214. }
  215. fn flush(&self) -> nb::Result<(), Infallible> {
  216. let sr = self.sr.read();
  217. if sr.tc().bit_is_set() {
  218. Ok(())
  219. } else {
  220. Err(nb::Error::WouldBlock)
  221. }
  222. }
  223. }
  224. impl UsartReadWrite for &crate::pac::usart1::RegisterBlock {}
  225. macro_rules! hal {
  226. ($(
  227. $(#[$meta:meta])*
  228. $USARTX:ident: (
  229. $usartX:ident,
  230. $usartX_remap:ident,
  231. $bit:ident,
  232. $closure:expr,
  233. $APBx:ident,
  234. ),
  235. )+) => {
  236. $(
  237. $(#[$meta])*
  238. /// The behaviour of the functions is equal for all three USARTs.
  239. /// Except that they are using the corresponding USART hardware and pins.
  240. impl<PINS> Serial<$USARTX, PINS> {
  241. /// Configures the serial interface and creates the interface
  242. /// struct.
  243. ///
  244. /// `Bps` is the baud rate of the interface.
  245. ///
  246. /// `Clocks` passes information about the current frequencies of
  247. /// the clocks. The existence of the struct ensures that the
  248. /// clock settings are fixed.
  249. ///
  250. /// The `serial` struct takes ownership over the `USARTX` device
  251. /// registers and the specified `PINS`
  252. ///
  253. /// `MAPR` and `APBX` are register handles which are passed for
  254. /// configuration. (`MAPR` is used to map the USART to the
  255. /// corresponding pins. `APBX` is used to reset the USART.)
  256. pub fn $usartX(
  257. usart: $USARTX,
  258. pins: PINS,
  259. mapr: &mut MAPR,
  260. config: Config,
  261. clocks: Clocks,
  262. apb: &mut $APBx,
  263. ) -> Self
  264. where
  265. PINS: Pins<$USARTX>,
  266. {
  267. // enable and reset $USARTX
  268. $USARTX::enable(apb);
  269. $USARTX::reset(apb);
  270. #[allow(unused_unsafe)]
  271. mapr.modify_mapr(|_, w| unsafe{
  272. #[allow(clippy::redundant_closure_call)]
  273. w.$usartX_remap().$bit(($closure)(PINS::REMAP))
  274. });
  275. // enable DMA transfers
  276. usart.cr3.write(|w| w.dmat().set_bit().dmar().set_bit());
  277. // Configure baud rate
  278. let brr = <$USARTX as RccBus>::Bus::get_frequency(&clocks).0 / config.baudrate.0;
  279. assert!(brr >= 16, "impossible baud rate");
  280. usart.brr.write(|w| unsafe { w.bits(brr) });
  281. // Configure parity and word length
  282. // Unlike most uart devices, the "word length" of this usart device refers to
  283. // the size of the data plus the parity bit. I.e. "word length"=8, parity=even
  284. // results in 7 bits of data. Therefore, in order to get 8 bits and one parity
  285. // bit, we need to set the "word" length to 9 when using parity bits.
  286. let (word_length, parity_control_enable, parity) = match config.parity {
  287. Parity::ParityNone => (false, false, false),
  288. Parity::ParityEven => (true, true, false),
  289. Parity::ParityOdd => (true, true, true),
  290. };
  291. usart.cr1.modify(|_r, w| {
  292. w
  293. .m().bit(word_length)
  294. .ps().bit(parity)
  295. .pce().bit(parity_control_enable)
  296. });
  297. // Configure stop bits
  298. let stop_bits = match config.stopbits {
  299. StopBits::STOP1 => 0b00,
  300. StopBits::STOP0P5 => 0b01,
  301. StopBits::STOP2 => 0b10,
  302. StopBits::STOP1P5 => 0b11,
  303. };
  304. usart.cr2.modify(|_r, w| {
  305. w.stop().bits(stop_bits)
  306. });
  307. // UE: enable USART
  308. // RE: enable receiver
  309. // TE: enable transceiver
  310. usart
  311. .cr1
  312. .modify(|_r, w| w.ue().set_bit().re().set_bit().te().set_bit());
  313. Serial { usart, pins }
  314. }
  315. /// Starts listening to the USART by enabling the _Received data
  316. /// ready to be read (RXNE)_ interrupt and _Transmit data
  317. /// register empty (TXE)_ interrupt
  318. pub fn listen(&mut self, event: Event) {
  319. match event {
  320. Event::Rxne => self.usart.cr1.modify(|_, w| w.rxneie().set_bit()),
  321. Event::Txe => self.usart.cr1.modify(|_, w| w.txeie().set_bit()),
  322. }
  323. }
  324. /// Stops listening to the USART by disabling the _Received data
  325. /// ready to be read (RXNE)_ interrupt and _Transmit data
  326. /// register empty (TXE)_ interrupt
  327. pub fn unlisten(&mut self, event: Event) {
  328. match event {
  329. Event::Rxne => self.usart.cr1.modify(|_, w| w.rxneie().clear_bit()),
  330. Event::Txe => self.usart.cr1.modify(|_, w| w.txeie().clear_bit()),
  331. }
  332. }
  333. /// Returns ownership of the borrowed register handles
  334. pub fn release(self) -> ($USARTX, PINS) {
  335. (self.usart, self.pins)
  336. }
  337. /// Separates the serial struct into separate channel objects for sending (Tx) and
  338. /// receiving (Rx)
  339. pub fn split(self) -> (Tx<$USARTX>, Rx<$USARTX>) {
  340. (
  341. Tx {
  342. _usart: PhantomData,
  343. },
  344. Rx {
  345. _usart: PhantomData,
  346. },
  347. )
  348. }
  349. }
  350. impl Tx<$USARTX> {
  351. pub fn listen(&mut self) {
  352. unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().set_bit()) };
  353. }
  354. pub fn unlisten(&mut self) {
  355. unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().clear_bit()) };
  356. }
  357. }
  358. impl Rx<$USARTX> {
  359. pub fn listen(&mut self) {
  360. unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().set_bit()) };
  361. }
  362. pub fn unlisten(&mut self) {
  363. unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().clear_bit()) };
  364. }
  365. }
  366. impl crate::hal::serial::Read<u8> for Rx<$USARTX> {
  367. type Error = Error;
  368. fn read(&mut self) -> nb::Result<u8, Error> {
  369. unsafe { &*$USARTX::ptr() }.read()
  370. }
  371. }
  372. impl crate::hal::serial::Write<u8> for Tx<$USARTX> {
  373. type Error = Infallible;
  374. fn flush(&mut self) -> nb::Result<(), Self::Error> {
  375. unsafe { &*$USARTX::ptr() }.flush()
  376. }
  377. fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
  378. unsafe { &*$USARTX::ptr() }.write(byte)
  379. }
  380. }
  381. impl<PINS> crate::hal::serial::Read<u8> for Serial<$USARTX, PINS> {
  382. type Error = Error;
  383. fn read(&mut self) -> nb::Result<u8, Error> {
  384. self.usart.deref().read()
  385. }
  386. }
  387. impl<PINS> crate::hal::serial::Write<u8> for Serial<$USARTX, PINS> {
  388. type Error = Infallible;
  389. fn flush(&mut self) -> nb::Result<(), Self::Error> {
  390. self.usart.deref().flush()
  391. }
  392. fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
  393. self.usart.deref().write(byte)
  394. }
  395. }
  396. )+
  397. }
  398. }
  399. impl<USART> core::fmt::Write for Tx<USART>
  400. where
  401. Tx<USART>: embedded_hal::serial::Write<u8>,
  402. {
  403. fn write_str(&mut self, s: &str) -> core::fmt::Result {
  404. s.as_bytes()
  405. .iter()
  406. .try_for_each(|c| nb::block!(self.write(*c)))
  407. .map_err(|_| core::fmt::Error)
  408. }
  409. }
  410. hal! {
  411. /// # USART1 functions
  412. USART1: (
  413. usart1,
  414. usart1_remap,
  415. bit,
  416. |remap| remap == 1,
  417. APB2,
  418. ),
  419. /// # USART2 functions
  420. USART2: (
  421. usart2,
  422. usart2_remap,
  423. bit,
  424. |remap| remap == 1,
  425. APB1,
  426. ),
  427. /// # USART3 functions
  428. USART3: (
  429. usart3,
  430. usart3_remap,
  431. bits,
  432. |remap| remap,
  433. APB1,
  434. ),
  435. }
  436. pub type Rx1 = Rx<USART1>;
  437. pub type Tx1 = Tx<USART1>;
  438. pub type Rx2 = Rx<USART2>;
  439. pub type Tx2 = Tx<USART2>;
  440. pub type Rx3 = Rx<USART3>;
  441. pub type Tx3 = Tx<USART3>;
  442. use crate::dma::{Receive, TransferPayload, Transmit};
  443. macro_rules! serialdma {
  444. ($(
  445. $USARTX:ident: (
  446. $rxdma:ident,
  447. $txdma:ident,
  448. $dmarxch:ty,
  449. $dmatxch:ty,
  450. ),
  451. )+) => {
  452. $(
  453. pub type $rxdma = RxDma<Rx<$USARTX>, $dmarxch>;
  454. pub type $txdma = TxDma<Tx<$USARTX>, $dmatxch>;
  455. impl Receive for $rxdma {
  456. type RxChannel = $dmarxch;
  457. type TransmittedWord = u8;
  458. }
  459. impl Transmit for $txdma {
  460. type TxChannel = $dmatxch;
  461. type ReceivedWord = u8;
  462. }
  463. impl TransferPayload for $rxdma {
  464. fn start(&mut self) {
  465. self.channel.start();
  466. }
  467. fn stop(&mut self) {
  468. self.channel.stop();
  469. }
  470. }
  471. impl TransferPayload for $txdma {
  472. fn start(&mut self) {
  473. self.channel.start();
  474. }
  475. fn stop(&mut self) {
  476. self.channel.stop();
  477. }
  478. }
  479. impl Rx<$USARTX> {
  480. pub fn with_dma(self, channel: $dmarxch) -> $rxdma {
  481. RxDma {
  482. payload: self,
  483. channel,
  484. }
  485. }
  486. }
  487. impl Tx<$USARTX> {
  488. pub fn with_dma(self, channel: $dmatxch) -> $txdma {
  489. TxDma {
  490. payload: self,
  491. channel,
  492. }
  493. }
  494. }
  495. impl $rxdma {
  496. pub fn split(mut self) -> (Rx<$USARTX>, $dmarxch) {
  497. self.stop();
  498. let RxDma {payload, channel} = self;
  499. (
  500. payload,
  501. channel
  502. )
  503. }
  504. }
  505. impl $txdma {
  506. pub fn split(mut self) -> (Tx<$USARTX>, $dmatxch) {
  507. self.stop();
  508. let TxDma {payload, channel} = self;
  509. (
  510. payload,
  511. channel,
  512. )
  513. }
  514. }
  515. impl<B> crate::dma::CircReadDma<B, u8> for $rxdma
  516. where
  517. &'static mut [B; 2]: StaticWriteBuffer<Word = u8>,
  518. B: 'static,
  519. {
  520. fn circ_read(mut self, mut buffer: &'static mut [B; 2]) -> CircBuffer<B, Self> {
  521. // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
  522. // until the end of the transfer.
  523. let (ptr, len) = unsafe { buffer.static_write_buffer() };
  524. self.channel.set_peripheral_address(unsafe{ &(*$USARTX::ptr()).dr as *const _ as u32 }, false);
  525. self.channel.set_memory_address(ptr as u32, true);
  526. self.channel.set_transfer_length(len);
  527. atomic::compiler_fence(Ordering::Release);
  528. self.channel.ch().cr.modify(|_, w| { w
  529. .mem2mem() .clear_bit()
  530. .pl() .medium()
  531. .msize() .bits8()
  532. .psize() .bits8()
  533. .circ() .set_bit()
  534. .dir() .clear_bit()
  535. });
  536. self.start();
  537. CircBuffer::new(buffer, self)
  538. }
  539. }
  540. impl<B> crate::dma::ReadDma<B, u8> for $rxdma
  541. where
  542. B: StaticWriteBuffer<Word = u8>,
  543. {
  544. fn read(mut self, mut buffer: B) -> Transfer<W, B, Self> {
  545. // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
  546. // until the end of the transfer.
  547. let (ptr, len) = unsafe { buffer.static_write_buffer() };
  548. self.channel.set_peripheral_address(unsafe{ &(*$USARTX::ptr()).dr as *const _ as u32 }, false);
  549. self.channel.set_memory_address(ptr as u32, true);
  550. self.channel.set_transfer_length(len);
  551. atomic::compiler_fence(Ordering::Release);
  552. self.channel.ch().cr.modify(|_, w| { w
  553. .mem2mem() .clear_bit()
  554. .pl() .medium()
  555. .msize() .bits8()
  556. .psize() .bits8()
  557. .circ() .clear_bit()
  558. .dir() .clear_bit()
  559. });
  560. self.start();
  561. Transfer::w(buffer, self)
  562. }
  563. }
  564. impl<B> crate::dma::WriteDma<B, u8> for $txdma
  565. where
  566. B: StaticReadBuffer<Word = u8>,
  567. {
  568. fn write(mut self, buffer: B) -> Transfer<R, B, Self> {
  569. // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
  570. // until the end of the transfer.
  571. let (ptr, len) = unsafe { buffer.static_read_buffer() };
  572. self.channel.set_peripheral_address(unsafe{ &(*$USARTX::ptr()).dr as *const _ as u32 }, false);
  573. self.channel.set_memory_address(ptr as u32, true);
  574. self.channel.set_transfer_length(len);
  575. atomic::compiler_fence(Ordering::Release);
  576. self.channel.ch().cr.modify(|_, w| { w
  577. .mem2mem() .clear_bit()
  578. .pl() .medium()
  579. .msize() .bits8()
  580. .psize() .bits8()
  581. .circ() .clear_bit()
  582. .dir() .set_bit()
  583. });
  584. self.start();
  585. Transfer::r(buffer, self)
  586. }
  587. }
  588. )+
  589. }
  590. }
  591. serialdma! {
  592. USART1: (
  593. RxDma1,
  594. TxDma1,
  595. dma1::C5,
  596. dma1::C4,
  597. ),
  598. USART2: (
  599. RxDma2,
  600. TxDma2,
  601. dma1::C6,
  602. dma1::C7,
  603. ),
  604. USART3: (
  605. RxDma3,
  606. TxDma3,
  607. dma1::C3,
  608. dma1::C2,
  609. ),
  610. }