libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
hpm_i2c_platform.hpp
Go to the documentation of this file.
1#pragma once
2
19#include <cstddef>
20#include <cstdint>
21
22#include "hpm_i2c.hpp"
23
24#if LIBXR_HPM_I2C_SUPPORTED
25namespace LibXR
26{
27namespace HPMI2CPlatform
28{
29
30constexpr uint8_t kInvalidDmaSource = 0xFFU;
31constexpr int32_t kInvalidIndex = -1;
32constexpr int32_t kInvalidIrq = -1;
33
34struct Resource
35{
36 I2C_Type* base;
37 int32_t index;
38 uint8_t dma_source;
39 int32_t irq;
40};
41
42#if defined(HPM_DMA_SRC_I2C0)
43#define LIBXR_HPM_I2C_DMA_SRC_0 HPM_DMA_SRC_I2C0
44#else
45#define LIBXR_HPM_I2C_DMA_SRC_0 kInvalidDmaSource
46#endif
47#if defined(HPM_DMA_SRC_I2C1)
48#define LIBXR_HPM_I2C_DMA_SRC_1 HPM_DMA_SRC_I2C1
49#else
50#define LIBXR_HPM_I2C_DMA_SRC_1 kInvalidDmaSource
51#endif
52#if defined(HPM_DMA_SRC_I2C2)
53#define LIBXR_HPM_I2C_DMA_SRC_2 HPM_DMA_SRC_I2C2
54#else
55#define LIBXR_HPM_I2C_DMA_SRC_2 kInvalidDmaSource
56#endif
57#if defined(HPM_DMA_SRC_I2C3)
58#define LIBXR_HPM_I2C_DMA_SRC_3 HPM_DMA_SRC_I2C3
59#else
60#define LIBXR_HPM_I2C_DMA_SRC_3 kInvalidDmaSource
61#endif
62#if defined(HPM_DMA_SRC_I2C4)
63#define LIBXR_HPM_I2C_DMA_SRC_4 HPM_DMA_SRC_I2C4
64#else
65#define LIBXR_HPM_I2C_DMA_SRC_4 kInvalidDmaSource
66#endif
67#if defined(HPM_DMA_SRC_I2C5)
68#define LIBXR_HPM_I2C_DMA_SRC_5 HPM_DMA_SRC_I2C5
69#else
70#define LIBXR_HPM_I2C_DMA_SRC_5 kInvalidDmaSource
71#endif
72#if defined(HPM_DMA_SRC_I2C6)
73#define LIBXR_HPM_I2C_DMA_SRC_6 HPM_DMA_SRC_I2C6
74#else
75#define LIBXR_HPM_I2C_DMA_SRC_6 kInvalidDmaSource
76#endif
77#if defined(HPM_DMA_SRC_I2C7)
78#define LIBXR_HPM_I2C_DMA_SRC_7 HPM_DMA_SRC_I2C7
79#else
80#define LIBXR_HPM_I2C_DMA_SRC_7 kInvalidDmaSource
81#endif
82
83#if defined(IRQn_I2C0)
84#define LIBXR_HPM_I2C_IRQ_0 IRQn_I2C0
85#else
86#define LIBXR_HPM_I2C_IRQ_0 kInvalidIrq
87#endif
88#if defined(IRQn_I2C1)
89#define LIBXR_HPM_I2C_IRQ_1 IRQn_I2C1
90#else
91#define LIBXR_HPM_I2C_IRQ_1 kInvalidIrq
92#endif
93#if defined(IRQn_I2C2)
94#define LIBXR_HPM_I2C_IRQ_2 IRQn_I2C2
95#else
96#define LIBXR_HPM_I2C_IRQ_2 kInvalidIrq
97#endif
98#if defined(IRQn_I2C3)
99#define LIBXR_HPM_I2C_IRQ_3 IRQn_I2C3
100#else
101#define LIBXR_HPM_I2C_IRQ_3 kInvalidIrq
102#endif
103#if defined(IRQn_I2C4)
104#define LIBXR_HPM_I2C_IRQ_4 IRQn_I2C4
105#else
106#define LIBXR_HPM_I2C_IRQ_4 kInvalidIrq
107#endif
108#if defined(IRQn_I2C5)
109#define LIBXR_HPM_I2C_IRQ_5 IRQn_I2C5
110#else
111#define LIBXR_HPM_I2C_IRQ_5 kInvalidIrq
112#endif
113#if defined(IRQn_I2C6)
114#define LIBXR_HPM_I2C_IRQ_6 IRQn_I2C6
115#else
116#define LIBXR_HPM_I2C_IRQ_6 kInvalidIrq
117#endif
118#if defined(IRQn_I2C7)
119#define LIBXR_HPM_I2C_IRQ_7 IRQn_I2C7
120#else
121#define LIBXR_HPM_I2C_IRQ_7 kInvalidIrq
122#endif
123
124#define LIBXR_HPM_I2C_RESOURCE_ENTRY(index_value) \
125 {HPM_I2C##index_value, static_cast<int32_t>(index_value), \
126 static_cast<uint8_t>(LIBXR_HPM_I2C_DMA_SRC_##index_value), \
127 static_cast<int32_t>(LIBXR_HPM_I2C_IRQ_##index_value)}
128
129static const Resource kResources[] = {
130#if defined(HPM_I2C0)
131 LIBXR_HPM_I2C_RESOURCE_ENTRY(0),
132#endif
133#if defined(HPM_I2C1)
134 LIBXR_HPM_I2C_RESOURCE_ENTRY(1),
135#endif
136#if defined(HPM_I2C2)
137 LIBXR_HPM_I2C_RESOURCE_ENTRY(2),
138#endif
139#if defined(HPM_I2C3)
140 LIBXR_HPM_I2C_RESOURCE_ENTRY(3),
141#endif
142#if defined(HPM_I2C4)
143 LIBXR_HPM_I2C_RESOURCE_ENTRY(4),
144#endif
145#if defined(HPM_I2C5)
146 LIBXR_HPM_I2C_RESOURCE_ENTRY(5),
147#endif
148#if defined(HPM_I2C6)
149 LIBXR_HPM_I2C_RESOURCE_ENTRY(6),
150#endif
151#if defined(HPM_I2C7)
152 LIBXR_HPM_I2C_RESOURCE_ENTRY(7),
153#endif
154 {nullptr, kInvalidIndex, kInvalidDmaSource, kInvalidIrq},
155};
156
157#undef LIBXR_HPM_I2C_RESOURCE_ENTRY
158#undef LIBXR_HPM_I2C_IRQ_7
159#undef LIBXR_HPM_I2C_IRQ_6
160#undef LIBXR_HPM_I2C_IRQ_5
161#undef LIBXR_HPM_I2C_IRQ_4
162#undef LIBXR_HPM_I2C_IRQ_3
163#undef LIBXR_HPM_I2C_IRQ_2
164#undef LIBXR_HPM_I2C_IRQ_1
165#undef LIBXR_HPM_I2C_IRQ_0
166#undef LIBXR_HPM_I2C_DMA_SRC_7
167#undef LIBXR_HPM_I2C_DMA_SRC_6
168#undef LIBXR_HPM_I2C_DMA_SRC_5
169#undef LIBXR_HPM_I2C_DMA_SRC_4
170#undef LIBXR_HPM_I2C_DMA_SRC_3
171#undef LIBXR_HPM_I2C_DMA_SRC_2
172#undef LIBXR_HPM_I2C_DMA_SRC_1
173#undef LIBXR_HPM_I2C_DMA_SRC_0
174
175static const Resource* Find(I2C_Type* i2c)
176{
177 if (i2c == nullptr)
178 {
179 return nullptr;
180 }
181
182 for (size_t i = 0U; i < (sizeof(kResources) / sizeof(kResources[0])); ++i)
183 {
184 if (i2c == kResources[i].base)
185 {
186 return &kResources[i];
187 }
188 }
189 return nullptr;
190}
191
192static uint8_t ResolveBoardI2cDmaSource(I2C_Type* i2c)
193{
194#if LIBXR_HPM_I2C_HAS_BOARD_HELPER
195#ifdef BOARD_APP_I2C_BASE
196 if (i2c == BOARD_APP_I2C_BASE)
197 {
198 return BOARD_APP_I2C_DMA_SRC;
199 }
200#endif
201#endif
202
203 const Resource* resource = Find(i2c);
204 return resource != nullptr ? resource->dma_source : kInvalidDmaSource;
205}
206
207static int32_t ResolveI2cIndex(I2C_Type* i2c)
208{
209 const Resource* resource = Find(i2c);
210 return resource != nullptr ? resource->index : kInvalidIndex;
211}
212
213static int32_t ResolveBoardI2cIrq(I2C_Type* i2c)
214{
215#if LIBXR_HPM_I2C_HAS_BOARD_HELPER
216#ifdef BOARD_APP_I2C_BASE
217 if (i2c == BOARD_APP_I2C_BASE)
218 {
219 return BOARD_APP_I2C_IRQ;
220 }
221#endif
222#endif
223
224 const Resource* resource = Find(i2c);
225 return resource != nullptr ? resource->irq : kInvalidIrq;
226}
227
228} // namespace HPMI2CPlatform
229} // namespace LibXR
230#endif
HPM I2C 主机驱动适配头文件 / Adapter header for the HPM I2C master driver.
LibXR 命名空间
Definition ch32_can.hpp:14