libyggdrasil  v1.0.0
i2c.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 
34 namespace bsp::drv {
35 
42  template<auto Context, template<auto> typename I2CImpl>
43  struct I2C {
44  I2C() = delete;
45 
46  using Impl = I2CImpl<Context>;
47 
54  static auto init(auto ... args) {
55  return Impl::init(args...);
56  }
57 
64  static auto deinit(auto ... args) {
65  return Impl::deinit(args...);
66  }
67 
75  template<typename T>
76  static T read(u8 address) {
77  std::array<u8, sizeof(T)> data;
78  Impl::read(address, data);
79 
80  return bit_cast<T>(data);
81  }
82 
91  template<typename T>
92  static T read(u8 address, u8 reg) {
93  std::array<u8, sizeof(T)> data;
94  Impl::write(address, std::array<u8, 1>{ reg });
95  Impl::read(address, data);
96 
97  return bit_cast<T>(data);
98  }
99 
107  template<typename T>
108  static void write(u8 address, T value) {
109  std::array<u8, sizeof(T)> data;
110  std::memcpy(data.data(), &value, sizeof(T));
111 
112  Impl::write(address, data);
113  }
114 
123  template<typename T>
124  static void write(u8 address, u8 reg, T value) {
125  std::array<u8, sizeof(T) + 1> data;
126  data[0] = reg;
127  std::memcpy(data.data() + 1, &value, sizeof(T));
128 
129  Impl::write(address, data);
130  }
131  };
132 }
bsp::drv::I2C::write
static void write(u8 address, T value)
I2C write function.
Definition: i2c.hpp:108
bsp::drv::I2C::read
static T read(u8 address)
I2C read function.
Definition: i2c.hpp:76
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::drv::I2C
Base class for I2C abstraction.
Definition: i2c.hpp:43
bsp::drv::I2C::write
static void write(u8 address, u8 reg, T value)
I2C write function.
Definition: i2c.hpp:124
bsp::drv::I2C::I2C
I2C()=delete
bsp::drv::I2C::init
static auto init(auto ... args)
I2C initialization.
Definition: i2c.hpp:54
bsp::drv::I2C::Impl
I2CImpl< Context > Impl
Definition: i2c.hpp:46
registers.hpp
Zero-cost abstraction for accessing registers and bits/bitfields within them.
bsp::drv::I2C::read
static T read(u8 address, u8 reg)
I2C read function.
Definition: i2c.hpp:92
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::drv
Definition: display.hpp:37
bsp::drv::I2C::deinit
static auto deinit(auto ... args)
I2C deinitialization.
Definition: i2c.hpp:64