libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
stm32_i2c.hpp
1#pragma once
2
3#include <type_traits>
4#include <utility>
5
6#include "main.h"
7
8#ifdef HAL_I2C_MODULE_ENABLED
9
10#ifdef I2C
11#undef I2C
12#endif
13
14#include "i2c.hpp"
15#include "libxr_assert.hpp"
16
17typedef enum
18{
19#ifdef I2C1
20 STM32_I2C1,
21#endif
22#ifdef I2C2
23 STM32_I2C2,
24#endif
25#ifdef I2C3
26 STM32_I2C3,
27#endif
28#ifdef I2C4
29 STM32_I2C4,
30#endif
31#ifdef I2C5
32 STM32_I2C5,
33#endif
34#ifdef I2C6
35 STM32_I2C6,
36#endif
37#ifdef I2C7
38 STM32_I2C7,
39#endif
40#ifdef I2C8
41 STM32_I2C8,
42#endif
43 STM32_I2C_NUMBER,
44 STM32_I2C_ID_ERROR
45} stm32_i2c_id_t;
46
47stm32_i2c_id_t STM32_I2C_GetID(I2C_TypeDef* hi2c); // NOLINT
48
49namespace LibXR
50{
54class STM32I2C : public I2C
55{
56 public:
60 STM32I2C(I2C_HandleTypeDef* hi2c, RawData dma_buff, uint32_t dma_enable_min_size = 3);
61
62 ErrorCode Read(uint16_t slave_addr, RawData read_data, ReadOperation& op,
63 bool in_isr) override;
64
65 ErrorCode Write(uint16_t slave_addr, ConstRawData write_data, WriteOperation& op,
66 bool in_isr) override;
67
68 ErrorCode MemRead(uint16_t slave_addr, uint16_t mem_addr, RawData read_data,
69 ReadOperation& op, MemAddrLength mem_addr_size, bool in_isr) override;
70
71 ErrorCode MemWrite(uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data,
72 WriteOperation& op, MemAddrLength mem_addr_size,
73 bool in_isr) override;
74
75 template <typename, typename = void>
76 struct HasTiming : std::false_type
77 {
78 };
79
80 template <typename T>
81 struct HasTiming<T, std::void_t<decltype(std::declval<T>()->Init.Timing)>>
82 : std::true_type
83 {
84 };
85
86 template <typename, typename = void>
87 struct HasClockSpeed : std::false_type
88 {
89 };
90
91 template <typename T>
92 struct HasClockSpeed<T, std::void_t<decltype(std::declval<T>()->Init.ClockSpeed)>>
93 : std::true_type
94 {
95 };
96
97 template <typename T>
98 typename std::enable_if<!HasClockSpeed<T>::value>::type SetClockSpeed(
99 T&, const Configuration&)
100 {
101 }
102
103 template <typename T>
104 typename std::enable_if<HasClockSpeed<T>::value>::type SetClockSpeed(
105 T& i2c_handle, const Configuration& config)
106 {
107 i2c_handle->Init.ClockSpeed = config.clock_speed;
108 }
109
110 template <typename T>
111 typename std::enable_if<HasTiming<T>::value>::type SetTiming(T& i2c_handle,
112 uint32_t timing)
113 {
114 i2c_handle->Init.Timing = timing;
115 }
116
117 template <typename T>
118 typename std::enable_if<!HasTiming<T>::value>::type SetTiming(T&, uint32_t)
119 {
120 }
121
122 ErrorCode SetConfig(Configuration config) override;
123
124 stm32_i2c_id_t id_;
125 I2C_HandleTypeDef* i2c_handle_;
126 uint32_t dma_enable_min_size_;
127
128 RawData dma_buff_;
129
130 ReadOperation read_op_;
131 WriteOperation write_op_;
132 RawData read_buff_;
133
134 bool read_ = false;
135 bool recovering_ = false;
136 AsyncBlockWait block_wait_{};
137
138 static STM32I2C* map[STM32_I2C_NUMBER]; // NOLINT
139};
140} // namespace LibXR
141
142#endif
只读原始数据视图 / Immutable raw data view
I2C(Inter-Integrated Circuit)接口类。 I2C (Inter-Integrated Circuit) interface class.
Definition i2c.hpp:17
可写原始数据视图 / Mutable raw data view
STM32 I2C 驱动实现 / STM32 I2C driver implementation.
Definition stm32_i2c.hpp:55
ErrorCode SetConfig(Configuration config) override
配置 I2C 设备参数。 Configures the I2C device settings.
ErrorCode MemRead(uint16_t slave_addr, uint16_t mem_addr, RawData read_data, ReadOperation &op, MemAddrLength mem_addr_size, bool in_isr) override
从 I2C 设备指定寄存器读取数据。 Reads data from a specific register of an I2C device.
ErrorCode MemWrite(uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data, WriteOperation &op, MemAddrLength mem_addr_size, bool in_isr) override
向 I2C 设备指定寄存器写入数据。 Writes data to a specific register of an I2C device.
ErrorCode Write(uint16_t slave_addr, ConstRawData write_data, WriteOperation &op, bool in_isr) override
向 I2C 设备写入数据。 Writes data to an I2C device.
ErrorCode Read(uint16_t slave_addr, RawData read_data, ReadOperation &op, bool in_isr) override
读取 I2C 设备的数据。 Reads data from an I2C device.
STM32I2C(I2C_HandleTypeDef *hi2c, RawData dma_buff, uint32_t dma_enable_min_size=3)
构造 I2C 对象 / Construct I2C object
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
Operation< ErrorCode > ReadOperation
读操作的完成通知描述符 / Read completion descriptor.
Operation< ErrorCode > WriteOperation
写操作的完成通知描述符 / Write completion descriptor.
I2C 设备的配置信息结构体。 Configuration structure for an I2C device.
Definition i2c.hpp:24