libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_i2c.hpp
1#pragma once
2
3#include "main.h"
4
5#ifdef HAL_I2C_MODULE_ENABLED
6
7#ifdef I2C
8#undef I2C
9#endif
10
11#include "i2c.hpp"
12#include "libxr.hpp"
13
14typedef enum
15{
16#ifdef I2C1
17 STM32_I2C1,
18#endif
19#ifdef I2C2
20 STM32_I2C2,
21#endif
22#ifdef I2C3
23 STM32_I2C3,
24#endif
25#ifdef I2C4
26 STM32_I2C4,
27#endif
28#ifdef I2C5
29 STM32_I2C5,
30#endif
31#ifdef I2C6
32 STM32_I2C6,
33#endif
34#ifdef I2C7
35 STM32_I2C7,
36#endif
37#ifdef I2C8
38 STM32_I2C8,
39#endif
40 STM32_I2C_NUMBER,
41 STM32_I2C_ID_ERROR
42} stm32_i2c_id_t;
43
44stm32_i2c_id_t STM32_I2C_GetID(I2C_TypeDef* hi2c); // NOLINT
45
46namespace LibXR
47{
51class STM32I2C : public I2C
52{
53 public:
57 STM32I2C(I2C_HandleTypeDef* hi2c, RawData dma_buff, uint32_t dma_enable_min_size = 3);
58
59 ErrorCode Read(uint16_t slave_addr, RawData read_data, ReadOperation& op,
60 bool in_isr) override;
61
62 ErrorCode Write(uint16_t slave_addr, ConstRawData write_data, WriteOperation& op,
63 bool in_isr) override;
64
65 ErrorCode MemRead(uint16_t slave_addr, uint16_t mem_addr, RawData read_data,
66 ReadOperation& op, MemAddrLength mem_addr_size, bool in_isr) override;
67
68 ErrorCode MemWrite(uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data,
69 WriteOperation& op, MemAddrLength mem_addr_size,
70 bool in_isr) override;
71
72 template <typename, typename = void>
73 struct HasClockSpeed : std::false_type
74 {
75 };
76
77 template <typename T>
78 struct HasClockSpeed<T, std::void_t<decltype(std::declval<T>()->Init.ClockSpeed)>>
79 : std::true_type
80 {
81 };
82
83 template <typename T>
84 typename std::enable_if<!HasClockSpeed<T>::value>::type SetClockSpeed(
85 T&, const Configuration&)
86 {
87 }
88
89 template <typename T>
90 typename std::enable_if<HasClockSpeed<T>::value>::type SetClockSpeed(
91 T& i2c_handle, const Configuration& config)
92 {
93 i2c_handle->Init.ClockSpeed = config.clock_speed;
94 }
95
96 ErrorCode SetConfig(Configuration config) override;
97
98 stm32_i2c_id_t id_;
99 I2C_HandleTypeDef* i2c_handle_;
100 uint32_t dma_enable_min_size_;
101
102 RawData dma_buff_;
103
104 ReadOperation read_op_;
105 WriteOperation write_op_;
106 RawData read_buff_;
107
108 bool read_ = false;
109
110 static STM32I2C* map[STM32_I2C_NUMBER]; // NOLINT
111};
112} // namespace LibXR
113
114#endif
常量原始数据封装类。 A class for encapsulating constant raw data.
I2C(Inter-Integrated Circuit)接口类。 I2C (Inter-Integrated Circuit) interface class.
Definition i2c.hpp:17
原始数据封装类。 A class for encapsulating raw data.
STM32 I2C 驱动实现 / STM32 I2C driver implementation.
Definition stm32_i2c.hpp:52
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
Definition stm32_i2c.cpp:92
LibXR 命名空间
Definition ch32_can.hpp:14
Operation< ErrorCode > ReadOperation
Read operation type.
Definition libxr_rw.hpp:237
Operation< ErrorCode > WriteOperation
Write operation type.
Definition libxr_rw.hpp:241
I2C 设备的配置信息结构体。 Configuration structure for an I2C device.
Definition i2c.hpp:24