\documentclass[aspectratio=169]{beamer}

\usepackage{listings}

% \usepackage{qrcode} doesn't work easily with white-on-black

\usepackage{url}
\usepackage[utf8]{inputenc}

\usecolortheme{albatross} % easiest base for white-on-black
\setbeamercolor{titlelike}{bg=black,fg=white}
\setbeamercolor{normal text}{bg=black,fg=white}

\title{Embedded Rust, by example of RIOT-OS applications}
\author{Christian M. Amsüss \url{<ca@etonomy.org>}}
\date{2018-11-27}

\titlegraphic{
  \centering
  \raisebox{-0.2cm}{\includegraphics[width=2cm]{rust-embedded.pdf}\hspace*{1cm}}~%
  \includegraphics[width=4cm]{etonomy.pdf}\hspace*{1cm}~%
  \raisebox{0.5cm}{\includegraphics[width=2cm]{riot.png}}
}

\newcommand{\crate}[1]{\texttt{\href{https://crates.io/crates/#1}{#1}}}

\begin{document}

\frame{\titlepage}

\begin{frame}{Embedded Devices}
  \begin{itemize}
    \item 10kB -- 1MB ROM
    \item 1kB -- 100kB RAM
    \item Typical hardware: ARM Cortex-M3, eg. STM32
  \end{itemize}
\end{frame}

\begin{frame}{Embedded Devices}
  \begin{itemize}
    \item allocation: static or on stack
    \item Someone needs to initialize the RAM
    \item CPU specific linker scripts
    \item Software shipped via hardware debugger or using a bootloader
  \end{itemize}
\end{frame}

\begin{frame}{Embedded Devices: On-board peripherals}
  \begin{itemize}
    \item Clock(s)
    \item UARTs (eg. console)
    \item GPIO pins (eg. LEDs, buttons)
    \item SPI (eg. to access SD cards)
  \end{itemize}

  need to be set up, need drivers / file systems
\end{frame}


\begin{frame}{}
  \center\Huge{Why Rust?}
\end{frame}

\begin{frame}{}
  \center\Huge{Why not?}
\end{frame}

\begin{frame}{Why Rust: My personal selection}
  \begin{itemize}
    \item Fearless development
    \item Reusing code throughout the infrastructure
  \end{itemize}
\end{frame}


\begin{frame}
  \center\Huge{\texttt{no\_std}}
\end{frame}

\begin{frame}{Options 1a: Bare metal}
  \begin{itemize}
    \item \crate{cortex-m-rt}
    \item peripherals wrapped from \crate{svd2rust} (eg. \crate{stm32f30x})
    \item device drivers (eg. \crate{stm32f30x-hal})
    \item board support crate (eg. \crate{f3})
  \end{itemize}

  See \crate{f3} crate for examples
\end{frame}

\begin{frame}{Options 1b: RTFM}
  ``Real-Time For The Masses''

  \begin{itemize}
    \item \crate{cortex-m-rtfm}
    \item peripherals
    \item device drivers
    \item board support crate
  \end{itemize}

  More descriptive knowledge, fewer mutexes
\end{frame}

\begin{frame}{Options 2: Full Rust operating system}
    Tock

    \begin{itemize}
      \item Operating system written in Rust
      \item Trusted (cooperative) and untrusted (preemtive) processes
      \item Network stack is WIP
      \item Limited hardware support
    \end{itemize}
\end{frame}

\begin{frame}{Options 3: RIOT-OS}
    \begin{itemize}
      \item Operating system written in C
      \item Trusted processes (cooperative or preemtive)
      \item Mature network stack
      \item Large community
      \item Good hardware support
    \end{itemize}
\end{frame}

\begin{frame}
  \center\Huge{Which to pick?}
\end{frame}

\begin{frame}
  \center\Huge{Does it matter?}
\end{frame}

\begin{frame}
  \center\Huge{Abstractions}
\end{frame}

\begin{frame}
  \center\Huge{\crate{embedded-hal}}
\end{frame}

\begin{frame}{embeded-hal traits}
    \begin{itemize}
      \item GPIO
      \item SPI
      \item ADC
      \item I2C
      \item UART
      \item delays
      \item \ldots
    \end{itemize}

    covers usage, not initialization
\end{frame}

\begin{frame}[fragile]{embedded-hal driver example: ENC28J60}
  \begin{lstlisting}
impl<E, SPI, NCS, INT, RESET> Enc28j60<...>
where
    SPI: spi::Transfer<u8, Error=E> + ...,
    NCS: OutputPin,
    INT: IntPin + InputPin,
    RESET: ResetPin,
{
  ...
}
  \end{lstlisting}
\end{frame}

\begin{frame}
  \center\Huge{Traits in general}
\end{frame}

\begin{frame}[fragile]{Emulating a different network stack}
  \begin{lstlisting}
impl<'a> jnet::Resize for
    &'a mut riot_sys::Pktsnip<Writable>
{
    fn truncate(&mut self, len: u16) {
        self.realloc_data(len as usize).unwrap();
    }
}
  \end{lstlisting}

  \bigskip

  Translation at build time; no runtime overhead if concepts align
\end{frame}

\begin{frame}{}
    \huge \center RIOT Operating System

    \center \includegraphics[width=8cm]{riot.png}

    \Large \center \url{https://riot-os.org}
\end{frame}

\begin{frame}{Recap: RIOT-OS}
    \begin{itemize}
      \item Operating system written in C
      \item Trusted processes (cooperative or preemtive)
      \item Mature network stack
      \item Large community
      \item Good hardware support
    \end{itemize}
\end{frame}

\begin{frame}
  \center\Large \crate{riot-sys}

  \bigskip

  bindgen

  \bigskip

  many unsafe functions and raw pointers
\end{frame}

\begin{frame}
  \center\Large \crate{riot-wrappers}

  \bigskip

  safe wrappers

  \bigskip

  Mutext, RwLock: like in \texttt{std::sync}

  \bigskip

  embedded-hal implementations
\end{frame}

\begin{frame}{}
  \center\Huge{Examples}
\end{frame}

\begin{frame}{Recap}
  \begin{itemize}
    \item What are embedded devices?
    \item Bare metal development is possible
    \item Choice of operating systems
    \item embedded-hal \& co (Embedded Rust WG)
    \item RIOT Operating System
    \item riot-sys and riot-wrappers
    \item Go try it!
  \end{itemize}
\end{frame}

\begin{frame}{}
  \center\Huge{Questions?}
\end{frame}

\begin{frame}{}
    Thanks for your attention

    \vspace{1cm}

    Slides and more links on \url{http://christian.amsuess.com/presentations/2018/embedded-rust-riot/}

    \bigskip
    \center \includegraphics[width=3.2cm]{qr.eps}

    \bigskip
  % FIXME: why is there no \thetitlegraphic?
  \centering
  \raisebox{-0.2cm}{\includegraphics[width=2cm]{rust-embedded.pdf}\hspace*{1cm}}~%
  \includegraphics[width=4cm]{etonomy.pdf}\hspace*{1cm}~%
  \raisebox{0.5cm}{\includegraphics[width=2cm]{riot.png}}
\end{frame}

\end{document}
