libyggdrasil  v1.0.0
spi.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Common - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
28 #include <cpp/common/registers.hpp>
30 #include <cpp/common/utils.hpp>
31 
32 #include <array>
33 #include <type_traits>
34 
35 namespace bsp::drv {
36 
37  enum class SPIMode:u8 {
38  _0 = 0b00,
39  _1 = 0b01,
40  _2 = 0b10,
41  _3 = 0b11,
42  };
43 
50  template<auto Context, template<auto> typename SPIImpl>
51  struct SPI {
52  SPI() = delete;
53 
54  using Impl = SPIImpl<Context>;
55 
62  static auto init(auto ... args) {
63  return Impl::init(args...);
64  }
65 
72  static auto deinit(auto ... args) {
73  return Impl::deinit(args...);
74  }
75 
82  template<typename T>
83  static T read() {
84  std::array<u8, sizeof(T)> data;
85  Impl::read(data);
86 
87  return bit_cast<T>(data);
88  }
89 
96  template<typename T>
97  static void write(const T &value) {
98  if constexpr (std::is_pointer<T>::value) {
99  constexpr size_t Size = sizeof(std::remove_pointer<T>);
100 
101  std::array<u8, Size> data;
102  std::memcpy(data.data(), value, Size);
103  Impl::write(data);
104  } else {
105  constexpr size_t Size = sizeof(T);
106 
107  std::array<u8, Size> data;
108  std::memcpy(data.data(), &value, Size);
109  Impl::write(data);
110  }
111 
112  }
113 
120  static void setMode(SPIMode mode) {
121  Impl::setMode(mode);
122  }
123  };
124 }
bsp::drv::SPI
Base class for SPI abstraction.
Definition: spi.hpp:51
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::drv::SPIMode
SPIMode
Definition: spi.hpp:37
bsp::drv::SPI::SPI
SPI()=delete
bsp::drv::SPI::Impl
SPIImpl< Context > Impl
Definition: spi.hpp:54
bsp::drv::SPI::setMode
static void setMode(SPIMode mode)
Set the spi mode (CPOL and CPHA)
Definition: spi.hpp:120
bsp::drv::SPI::read
static T read()
SPI read function.
Definition: spi.hpp:83
registers.hpp
Zero-cost abstraction for accessing registers and bits/bitfields within them.
bsp::drv::SPI::init
static auto init(auto ... args)
SPI initialization.
Definition: spi.hpp:62
bsp::drv::SPI::deinit
static auto deinit(auto ... args)
SPI deinitialization.
Definition: spi.hpp:72
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::drv
Definition: display.hpp:37
bsp::drv::SPI::write
static void write(const T &value)
SPI write function.
Definition: spi.hpp:97
bsp::drv::SPIMode::_0
@ _0
CPOL = 0 | CPHA = 0.