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{
48class STM32I2C : public I2C
49{
50 public:
51 STM32I2C(I2C_HandleTypeDef *hi2c, RawData dma_buff, uint32_t dma_enable_min_size = 3);
52
53 ErrorCode Read(uint16_t slave_addr, RawData read_data, ReadOperation &op) override;
54
55 ErrorCode Write(uint16_t slave_addr, ConstRawData write_data,
56 WriteOperation &op) override;
57
58 ErrorCode MemRead(uint16_t slave_addr, uint16_t mem_addr, RawData read_data,
59 ReadOperation &op, MemAddrLength mem_addr_size) override;
60
61 ErrorCode MemWrite(uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data,
62 WriteOperation &op, MemAddrLength mem_addr_size) override;
63
64 template <typename, typename = void>
65 struct HasClockSpeed : std::false_type
66 {
67 };
68
69 template <typename T>
70 struct HasClockSpeed<T, std::void_t<decltype(std::declval<T>()->Init.ClockSpeed)>>
71 : std::true_type
72 {
73 };
74
75 template <typename T>
76 typename std::enable_if<!HasClockSpeed<T>::value>::type SetClockSpeed(
77 T &, const Configuration &)
78 {
79 }
80
81 template <typename T>
82 typename std::enable_if<HasClockSpeed<T>::value>::type SetClockSpeed(
83 T &i2c_handle, const Configuration &config)
84 {
85 i2c_handle->Init.ClockSpeed = config.clock_speed;
86 }
87
88 ErrorCode SetConfig(Configuration config) override;
89
90 stm32_i2c_id_t id_;
91 I2C_HandleTypeDef *i2c_handle_;
92 uint32_t dma_enable_min_size_;
93
94 RawData dma_buff_;
95
96 ReadOperation read_op_;
97 WriteOperation write_op_;
98 RawData read_buff_;
99
100 bool read_ = false;
101
102 static STM32I2C *map[STM32_I2C_NUMBER]; // NOLINT
103};
104} // namespace LibXR
105
106#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.
ErrorCode SetConfig(Configuration config) override
配置 I2C 设备参数。 Configures the I2C device settings.
ErrorCode Write(uint16_t slave_addr, ConstRawData write_data, WriteOperation &op) override
向 I2C 设备写入数据。 Writes data to an I2C device.
ErrorCode MemWrite(uint16_t slave_addr, uint16_t mem_addr, ConstRawData write_data, WriteOperation &op, MemAddrLength mem_addr_size) override
向 I2C 设备指定寄存器写入数据。 Writes data to a specific register of an I2C device.
ErrorCode MemRead(uint16_t slave_addr, uint16_t mem_addr, RawData read_data, ReadOperation &op, MemAddrLength mem_addr_size) override
从 I2C 设备指定寄存器读取数据。 Reads data from a specific register of an I2C device.
ErrorCode Read(uint16_t slave_addr, RawData read_data, ReadOperation &op) override
读取 I2C 设备的数据。 Reads data from an I2C device.
Definition stm32_i2c.cpp:77
LibXR 命名空间
Definition ch32_gpio.hpp:9
Operation< ErrorCode > ReadOperation
Read operation type.
Definition libxr_rw.hpp:233
Operation< ErrorCode > WriteOperation
Write operation type.
Definition libxr_rw.hpp:237
I2C 设备的配置信息结构体。 Configuration structure for an I2C device.
Definition i2c.hpp:24