|
38 | 38 | //!
|
39 | 39 | //! # Reference implementation
|
40 | 40 | //!
|
41 |
| -//! The [`stm32f30x-hal`] crate contains a reference implementation of this HAL. |
| 41 | +//! The [`stm32f1xx-hal`] crate contains a reference implementation of this HAL. |
42 | 42 | //!
|
43 |
| -//! [`stm32f30x-hal`]: https://crates.io/crates/stm32f30x-hal/0.1.0 |
| 43 | +//! [`stm32f1xx-hal`]: https://crates.io/crates/stm32f1xx-hal |
44 | 44 | //!
|
45 | 45 | //! # Platform agnostic drivers
|
46 | 46 | //!
|
|
64 | 64 | //! Here's how a HAL trait may look like:
|
65 | 65 | //!
|
66 | 66 | //! ```
|
67 |
| -//! extern crate nb; |
| 67 | +//! use nb; |
68 | 68 | //!
|
69 | 69 | //! /// A serial interface
|
70 | 70 | //! pub trait Serial {
|
|
90 | 90 | //! those cases `nb::Result<_, Infallible>` is used.
|
91 | 91 | //!
|
92 | 92 | //! ```
|
93 |
| -//! extern crate nb; |
| 93 | +//! use nb; |
94 | 94 | //!
|
95 | 95 | //! # use std as core;
|
96 | 96 | //! use ::core::convert::Infallible;
|
|
113 | 113 | //!
|
114 | 114 | //! [`svd2rust`]: https://crates.io/crates/svd2rust
|
115 | 115 | //!
|
116 |
| -//! Shown below is an implementation of some of the HAL traits for the [`stm32f30x`] crate. This |
117 |
| -//! single implementation will work for *any* microcontroller in the STM32F30x family. |
| 116 | +//! Shown below is an implementation of some of the HAL traits for the [`stm32f1xx-hal`] crate. This |
| 117 | +//! single implementation will work for *any* microcontroller in the STM32F1xx family. |
118 | 118 | //!
|
119 |
| -//! [`stm32f3`]: https://crates.io/crates/stm32f3 |
| 119 | +//! [`stm32f1`]: https://crates.io/crates/stm32f1 |
120 | 120 | //!
|
121 | 121 | //! ```not_run
|
122 |
| -//! // crate: stm32f3xx-hal |
123 |
| -//! // An implementation of the `embedded-hal` traits for STM32F3xx microcontrollers |
| 122 | +//! // crate: stm32f1xx-hal |
| 123 | +//! // An implementation of the `embedded-hal` traits for STM32F1xx microcontrollers |
124 | 124 | //!
|
125 |
| -//! extern crate embedded_hal as hal; |
126 |
| -//! extern crate nb; |
| 125 | +//! use embedded_hal as hal; |
| 126 | +//! use nb; |
127 | 127 | //!
|
128 | 128 | //! // device crate
|
129 |
| -//! extern crate stm32f3; |
130 |
| -//! |
131 |
| -//! use stm32f3::stm32f303::USART1; |
| 129 | +//! use stm32f1::stm32f103::USART1; |
132 | 130 | //!
|
133 | 131 | //! /// A serial interface
|
134 | 132 | //! // NOTE generic over the USART peripheral
|
|
185 | 183 | //!
|
186 | 184 | //! ## Intended usage
|
187 | 185 | //!
|
188 |
| -//! Thanks to the [`nb`] crate the HAL API can be used in a blocking manner, |
189 |
| -//! with `futures` or with the `await` operator using the [`block!`], |
190 |
| -//! [`try_nb!`] and [`await!`] macros respectively. |
| 186 | +//! Thanks to the [`nb`] crate the HAL API can be used in a blocking manner |
| 187 | +//! with the [`block!`] macro or with `futures`. |
191 | 188 | //!
|
192 |
| -//! [`block!`]: https://docs.rs/nb/0.1.0/nb/macro.block.html |
193 |
| -//! [`try_nb!`]: https://docs.rs/nb/0.1.0/nb/index.html#how-to-use-this-crate |
194 |
| -//! [`await!`]: https://docs.rs/nb/0.1.0/nb/index.html#how-to-use-this-crate |
| 189 | +//! [`block!`]: https://docs.rs/nb/1.0.0/nb/macro.block.html |
195 | 190 | //!
|
196 | 191 | //! ### Blocking mode
|
197 | 192 | //!
|
198 | 193 | //! An example of sending a string over the serial interface in a blocking
|
199 | 194 | //! fashion:
|
200 | 195 | //!
|
201 | 196 | //! ```
|
202 |
| -//! extern crate embedded_hal; |
203 |
| -//! #[macro_use(block)] |
204 |
| -//! extern crate nb; |
205 |
| -//! |
206 |
| -//! use stm32f30x_hal::Serial1; |
| 197 | +//! use crate::stm32f1xx_hal::Serial1; |
207 | 198 | //! use embedded_hal::serial::Write;
|
| 199 | +//! use nb::block; |
208 | 200 | //!
|
209 | 201 | //! # fn main() {
|
210 | 202 | //! let mut serial: Serial1 = {
|
|
219 | 211 | //! }
|
220 | 212 | //! # }
|
221 | 213 | //!
|
222 |
| -//! # mod stm32f30x_hal { |
| 214 | +//! # mod stm32f1xx_hal { |
| 215 | +//! # use nb; |
223 | 216 | //! # use core::convert::Infallible;
|
224 | 217 | //! # pub struct Serial1;
|
225 | 218 | //! # impl Serial1 {
|
226 |
| -//! # pub fn try_write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { |
| 219 | +//! # pub fn try_write(&mut self, _: u8) -> nb::Result<(), Infallible> { |
227 | 220 | //! # Ok(())
|
228 | 221 | //! # }
|
229 | 222 | //! # }
|
|
238 | 231 | //! the case for many embedded targets.
|
239 | 232 | //!
|
240 | 233 | //! ```no_run
|
241 |
| -//! extern crate embedded_hal as hal; |
242 |
| -//! extern crate futures; |
243 |
| -//! |
244 |
| -//! #[macro_use(try_nb)] |
245 |
| -//! extern crate nb; |
246 |
| -//! |
247 |
| -//! use hal::prelude::*; |
| 234 | +//! use embedded_hal as hal; |
| 235 | +//! use crate::hal::prelude::*; |
248 | 236 | //! use futures::{
|
249 |
| -//! future, |
| 237 | +//! future::{self, Loop}, |
250 | 238 | //! Async,
|
251 | 239 | //! Future,
|
252 | 240 | //! };
|
253 |
| -//! use futures::future::Loop; |
254 |
| -//! use stm32f30x_hal::{Led, Serial1, Timer6}; |
| 241 | +//! use nb; |
| 242 | +//! use stm32f1xx_hal::{Led, Serial1, Timer6}; |
255 | 243 | //! use core::convert::Infallible;
|
256 | 244 | //!
|
257 | 245 | //! /// `futures` version of `CountDown.try_wait`
|
|
263 | 251 | //! {
|
264 | 252 | //! let mut timer = Some(timer);
|
265 | 253 | //! future::poll_fn(move || {
|
266 |
| -//! try_nb!(timer.as_mut().unwrap().try_wait()); |
| 254 | +//! match timer.as_mut().unwrap().try_wait() { |
| 255 | +//! Err(nb::Error::Other(e)) => return Err(e), |
| 256 | +//! Err(nb::Error::WouldBlock) => return Ok(Async::NotReady), |
| 257 | +//! Ok(_) => (), |
| 258 | +//! }; |
267 | 259 | //!
|
268 | 260 | //! Ok(Async::Ready(timer.take().unwrap()))
|
269 | 261 | //! })
|
|
278 | 270 | //! {
|
279 | 271 | //! let mut serial = Some(serial);
|
280 | 272 | //! future::poll_fn(move || {
|
281 |
| -//! let byte = try_nb!(serial.as_mut().unwrap().try_read()); |
| 273 | +//! let byte = match serial.as_mut().unwrap().try_read() { |
| 274 | +//! Err(nb::Error::Other(e)) => return Err(e), |
| 275 | +//! Err(nb::Error::WouldBlock) => return Ok(Async::NotReady), |
| 276 | +//! Ok(x) => x, |
| 277 | +//! }; |
282 | 278 | //!
|
283 | 279 | //! Ok(Async::Ready((serial.take().unwrap(), byte)))
|
284 | 280 | //! })
|
|
293 | 289 | //! {
|
294 | 290 | //! let mut serial = Some(serial);
|
295 | 291 | //! future::poll_fn(move || {
|
296 |
| -//! try_nb!(serial.as_mut().unwrap().try_write(byte)); |
| 292 | +//! match serial.as_mut().unwrap().try_write(byte) { |
| 293 | +//! Err(nb::Error::Other(e)) => return Err(e), |
| 294 | +//! Err(nb::Error::WouldBlock) => return Ok(Async::NotReady), |
| 295 | +//! Ok(_) => (), |
| 296 | +//! }; |
297 | 297 | //!
|
298 | 298 | //! Ok(Async::Ready(serial.take().unwrap()))
|
299 | 299 | //! })
|
|
345 | 345 | //! }
|
346 | 346 | //! }
|
347 | 347 | //!
|
348 |
| -//! # mod stm32f30x_hal { |
| 348 | +//! # mod stm32f1xx_hal { |
| 349 | +//! # use crate::hal; |
349 | 350 | //! # use core::convert::Infallible;
|
350 | 351 | //! # pub struct Timer6;
|
351 |
| -//! # impl ::hal::timer::CountDown for Timer6 { |
| 352 | +//! # impl hal::timer::CountDown for Timer6 { |
352 | 353 | //! # type Error = Infallible;
|
353 | 354 | //! # type Time = ();
|
354 | 355 | //! #
|
|
357 | 358 | //! # }
|
358 | 359 | //! #
|
359 | 360 | //! # pub struct Serial1;
|
360 |
| -//! # impl ::hal::serial::Read<u8> for Serial1 { |
| 361 | +//! # impl hal::serial::Read<u8> for Serial1 { |
361 | 362 | //! # type Error = Infallible;
|
362 | 363 | //! # fn try_read(&mut self) -> ::nb::Result<u8, Infallible> { Err(::nb::Error::WouldBlock) }
|
363 | 364 | //! # }
|
364 |
| -//! # impl ::hal::serial::Write<u8> for Serial1 { |
| 365 | +//! # impl hal::serial::Write<u8> for Serial1 { |
365 | 366 | //! # type Error = Infallible;
|
366 | 367 | //! # fn try_flush(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
|
367 | 368 | //! # fn try_write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
|
|
375 | 376 | //! # }
|
376 | 377 | //! ```
|
377 | 378 | //!
|
378 |
| -//! ### `await` |
379 |
| -//! |
380 |
| -//! Same example as above but using `await!` instead of `futures` |
381 |
| -//! (same remark concerning the availability of `libstd` on the |
382 |
| -//! target). |
383 |
| -//! |
384 |
| -//! ```no_run |
385 |
| -//! #![feature(generator_trait)] |
386 |
| -//! #![feature(generators)] |
387 |
| -//! |
388 |
| -//! extern crate embedded_hal as hal; |
389 |
| -//! |
390 |
| -//! #[macro_use(r#await)] |
391 |
| -//! extern crate nb; |
392 |
| -//! |
393 |
| -//! use core::ops::Generator; |
394 |
| -//! use core::pin::Pin; |
395 |
| -//! |
396 |
| -//! use hal::prelude::*; |
397 |
| -//! use stm32f30x_hal::{Led, Serial1, Timer6}; |
398 |
| -//! |
399 |
| -//! fn main() { |
400 |
| -//! // HAL implementers |
401 |
| -//! let mut timer: Timer6 = { |
402 |
| -//! // .. |
403 |
| -//! # Timer6 |
404 |
| -//! }; |
405 |
| -//! let mut serial: Serial1 = { |
406 |
| -//! // .. |
407 |
| -//! # Serial1 |
408 |
| -//! }; |
409 |
| -//! let mut led: Led = { |
410 |
| -//! // .. |
411 |
| -//! # Led |
412 |
| -//! }; |
413 |
| -//! |
414 |
| -//! // Tasks |
415 |
| -//! let mut blinky = (move || { |
416 |
| -//! let mut state = false; |
417 |
| -//! loop { |
418 |
| -//! // `await!` means "suspend / yield here" instead of "block until |
419 |
| -//! // completion" |
420 |
| -//! nb::r#await!(timer.try_wait()).unwrap(); // NOTE(unwrap) E = Infallible |
421 |
| -//! |
422 |
| -//! state = !state; |
423 |
| -//! |
424 |
| -//! if state { |
425 |
| -//! led.on(); |
426 |
| -//! } else { |
427 |
| -//! led.off(); |
428 |
| -//! } |
429 |
| -//! } |
430 |
| -//! }); |
431 |
| -//! |
432 |
| -//! let mut loopback = (move || { |
433 |
| -//! loop { |
434 |
| -//! let byte = nb::r#await!(serial.try_read()).unwrap(); |
435 |
| -//! nb::r#await!(serial.try_write(byte)).unwrap(); |
436 |
| -//! } |
437 |
| -//! }); |
438 |
| -//! |
439 |
| -//! // Event loop |
440 |
| -//! loop { |
441 |
| -//! Pin::new(&mut blinky).resume(()); |
442 |
| -//! Pin::new(&mut loopback).resume(()); |
443 |
| -//! # break; |
444 |
| -//! } |
445 |
| -//! } |
446 |
| -//! |
447 |
| -//! # mod stm32f30x_hal { |
448 |
| -//! # use core::convert::Infallible; |
449 |
| -//! # pub struct Serial1; |
450 |
| -//! # impl Serial1 { |
451 |
| -//! # pub fn try_read(&mut self) -> ::nb::Result<u8, Infallible> { Err(::nb::Error::WouldBlock) } |
452 |
| -//! # pub fn try_write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) } |
453 |
| -//! # } |
454 |
| -//! # pub struct Timer6; |
455 |
| -//! # impl Timer6 { |
456 |
| -//! # pub fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) } |
457 |
| -//! # } |
458 |
| -//! # pub struct Led; |
459 |
| -//! # impl Led { |
460 |
| -//! # pub fn off(&mut self) {} |
461 |
| -//! # pub fn on(&mut self) {} |
462 |
| -//! # } |
463 |
| -//! # } |
464 |
| -//! ``` |
465 |
| -//! |
466 | 379 | //! ## Generic programming and higher level abstractions
|
467 | 380 | //!
|
468 | 381 | //! The core of the HAL has been kept minimal on purpose to encourage building **generic** higher
|
|
479 | 392 | //! - Write a whole buffer to a serial device in blocking a fashion.
|
480 | 393 | //!
|
481 | 394 | //! ```
|
482 |
| -//! extern crate embedded_hal as hal; |
483 |
| -//! #[macro_use(block)] |
484 |
| -//! extern crate nb; |
485 |
| -//! |
| 395 | +//! use embedded_hal as hal; |
| 396 | +//! use nb::block; |
486 | 397 | //! use hal::prelude::*;
|
487 | 398 | //!
|
488 | 399 | //! fn write_all<S>(serial: &mut S, buffer: &[u8]) -> Result<(), S::Error>
|
|
502 | 413 | //! - Blocking serial read with timeout
|
503 | 414 | //!
|
504 | 415 | //! ```
|
505 |
| -//! extern crate embedded_hal as hal; |
506 |
| -//! extern crate nb; |
| 416 | +//! use embedded_hal as hal; |
| 417 | +//! use nb; |
507 | 418 | //!
|
508 | 419 | //! use hal::prelude::*;
|
509 | 420 | //!
|
|
552 | 463 | //! # fn main() {}
|
553 | 464 | //! ```
|
554 | 465 | //!
|
555 |
| -//! - Asynchronous SPI transfer |
556 |
| -//! |
557 |
| -//! ```no_run |
558 |
| -//! #![feature(conservative_impl_trait)] |
559 |
| -//! #![feature(generators)] |
560 |
| -//! #![feature(generator_trait)] |
561 |
| -//! |
562 |
| -//! extern crate embedded_hal as hal; |
563 |
| -//! #[macro_use(r#await)] |
564 |
| -//! extern crate nb; |
565 |
| -//! |
566 |
| -//! use core::ops::Generator; |
567 |
| -//! |
568 |
| -//! /// Transfers a byte buffer of size N |
569 |
| -//! /// |
570 |
| -//! /// Returns the same byte buffer but filled with the data received from the |
571 |
| -//! /// slave device |
572 |
| -//! fn transfer<S, B>( |
573 |
| -//! mut spi: S, |
574 |
| -//! mut buffer: [u8; 16], // NOTE this should be generic over the size of the array |
575 |
| -//! ) -> impl Generator<Return = Result<(S, [u8; 16]), S::Error>, Yield = ()> |
576 |
| -//! where |
577 |
| -//! S: hal::spi::FullDuplex<u8>, |
578 |
| -//! { |
579 |
| -//! move || { |
580 |
| -//! let n = buffer.len(); |
581 |
| -//! for i in 0..n { |
582 |
| -//! nb::r#await!(spi.try_send(buffer[i]))?; |
583 |
| -//! buffer[i] = nb::r#await!(spi.try_read())?; |
584 |
| -//! } |
585 |
| -//! |
586 |
| -//! Ok((spi, buffer)) |
587 |
| -//! } |
588 |
| -//! } |
589 |
| -//! |
590 |
| -//! # fn main() {} |
591 |
| -//! ``` |
592 |
| -//! |
593 | 466 | //! - Buffered serial interface with periodic flushing in interrupt handler
|
594 | 467 | //!
|
595 | 468 | //! ```
|
596 | 469 | //! # use std as core;
|
597 |
| -//! extern crate embedded_hal as hal; |
598 |
| -//! extern crate nb; |
| 470 | +//! use embedded_hal as hal; |
| 471 | +//! use nb; |
599 | 472 | //!
|
600 | 473 | //! use hal::prelude::*;
|
601 | 474 | //! use ::core::convert::Infallible;
|
|
666 | 539 | //! # fn deref_mut(&mut self) -> &mut T { self.0 }
|
667 | 540 | //! # }
|
668 | 541 | //! # struct Serial1;
|
669 |
| -//! # impl ::hal::serial::Write<u8> for Serial1 { |
| 542 | +//! # impl hal::serial::Write<u8> for Serial1 { |
670 | 543 | //! # type Error = Infallible;
|
671 | 544 | //! # fn try_write(&mut self, _: u8) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
|
672 | 545 | //! # fn try_flush(&mut self) -> nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) }
|
|
0 commit comments