-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathport.hpp
367 lines (303 loc) · 10.7 KB
/
port.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once
//=========================================================================//
/*! @file
@brief I/O Ports / I/O ポート (RX621/RX62N)
@author 平松邦仁 (hira@rvf-rc45.net)
@copyright Copyright (C) 2022, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=========================================================================//
#include "common/io_utils.hpp"
namespace device {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ポート定義基底クラス(DDR, DR, PORT, ICR, ODR)
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t base>
struct portx_t {
static constexpr uint32_t base_address_ = base; ///< ベースアドレス
typedef rw8_t<base + 0x00> ddr_io;
typedef rw8_t<base + 0x20> dr_io;
typedef rw8_t<base + 0x40> port_io;
typedef rw8_t<base + 0x60> icr_io;
typedef rw8_t<base + 0x80> odr_io;
//-----------------------------------------------------------------//
/*!
@brief DDR レジスタ
*/
//-----------------------------------------------------------------//
struct ddr_t : public ddr_io {
using ddr_io::operator =;
using ddr_io::operator ();
using ddr_io::operator |=;
using ddr_io::operator &=;
bit_rw_t<ddr_io, bitpos::B0> B0;
bit_rw_t<ddr_io, bitpos::B1> B1;
bit_rw_t<ddr_io, bitpos::B2> B2;
bit_rw_t<ddr_io, bitpos::B3> B3;
bit_rw_t<ddr_io, bitpos::B4> B4;
bit_rw_t<ddr_io, bitpos::B5> B5;
bit_rw_t<ddr_io, bitpos::B6> B6;
bit_rw_t<ddr_io, bitpos::B7> B7;
};
static inline ddr_t DDR;
//-----------------------------------------------------------------//
/*!
@brief DR レジスタ
*/
//-----------------------------------------------------------------//
struct dr_t : public dr_io {
using dr_io::operator =;
using dr_io::operator ();
using dr_io::operator |=;
using dr_io::operator &=;
bit_rw_t<dr_io, bitpos::B0> B0;
bit_rw_t<dr_io, bitpos::B1> B1;
bit_rw_t<dr_io, bitpos::B2> B2;
bit_rw_t<dr_io, bitpos::B3> B3;
bit_rw_t<dr_io, bitpos::B4> B4;
bit_rw_t<dr_io, bitpos::B5> B5;
bit_rw_t<dr_io, bitpos::B6> B6;
bit_rw_t<dr_io, bitpos::B7> B7;
};
static inline dr_t DR;
//-----------------------------------------------------------------//
/*!
@brief PORT
*/
//-----------------------------------------------------------------//
struct port_t : public port_io {
using port_io::operator ();
bit_ro_t<port_io, bitpos::B0> B0;
bit_ro_t<port_io, bitpos::B1> B1;
bit_ro_t<port_io, bitpos::B2> B2;
bit_ro_t<port_io, bitpos::B3> B3;
bit_ro_t<port_io, bitpos::B4> B4;
bit_ro_t<port_io, bitpos::B5> B5;
bit_ro_t<port_io, bitpos::B6> B6;
bit_ro_t<port_io, bitpos::B7> B7;
};
static inline port_t PORT;
//-----------------------------------------------------------------//
/*!
@brief ICR レジスタ
*/
//-----------------------------------------------------------------//
struct icr_t : public icr_io {
using icr_io::operator =;
using icr_io::operator ();
using icr_io::operator |=;
using icr_io::operator &=;
bit_rw_t<icr_io, bitpos::B0> B0;
bit_rw_t<icr_io, bitpos::B1> B1;
bit_rw_t<icr_io, bitpos::B2> B2;
bit_rw_t<icr_io, bitpos::B3> B3;
bit_rw_t<icr_io, bitpos::B4> B4;
bit_rw_t<icr_io, bitpos::B5> B5;
bit_rw_t<icr_io, bitpos::B6> B6;
bit_rw_t<icr_io, bitpos::B7> B7;
};
static inline icr_t ICR;
//-----------------------------------------------------------------//
/*!
@brief ODR レジスタ
*/
//-----------------------------------------------------------------//
struct odr_t : public odr_io {
using odr_io::operator =;
using odr_io::operator ();
using odr_io::operator |=;
using odr_io::operator &=;
bit_rw_t<odr_io, bitpos::B0> B0;
bit_rw_t<odr_io, bitpos::B1> B1;
bit_rw_t<odr_io, bitpos::B2> B2;
bit_rw_t<odr_io, bitpos::B3> B3;
bit_rw_t<odr_io, bitpos::B4> B4;
bit_rw_t<odr_io, bitpos::B5> B5;
bit_rw_t<odr_io, bitpos::B6> B6;
bit_rw_t<odr_io, bitpos::B7> B7;
};
static inline odr_t ODR;
};
typedef portx_t<0x0008'C000> PORT0;
typedef portx_t<0x0008'C001> PORT1;
typedef portx_t<0x0008'C002> PORT2;
typedef portx_t<0x0008'C003> PORT3;
typedef portx_t<0x0008'C004> PORT4;
typedef portx_t<0x0008'C005> PORT5;
typedef portx_t<0x0008'C006> PORT6;
typedef portx_t<0x0008'C007> PORT7;
typedef portx_t<0x0008'C008> PORT8;
typedef portx_t<0x0008'C009> PORT9;
typedef portx_t<0x0008'C00A> PORTA;
typedef portx_t<0x0008'C00B> PORTB;
typedef portx_t<0x0008'C00C> PORTC;
typedef portx_t<0x0008'C00D> PORTD;
typedef portx_t<0x0008'C00E> PORTE;
typedef portx_t<0x0008'C00F> PORTF;
typedef portx_t<0x0008'C010> PORTG;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief ポート・オープンドレイン・タイプ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
struct PORT_BASE {
enum class OD_TYPE : uint8_t {
NONE, ///< 無し
N_CH, ///< N-Channel
// P_CH, ///< P-Channel
};
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 単ポート定義テンプレート
@param[in] PORT ポート・クラス
@param[in] BPOS ビット位置
@param[in] ASSERT アサート論理 @n
※反転入力、反転出力として扱う場合「0」
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class PORTX, bitpos BPOS, bool ASSERT = 1>
struct PORT : public PORT_BASE {
static constexpr uint8_t PNO = static_cast<uint8_t>(PORTX::base_address_ & 0x1f);
static constexpr uint8_t BIT_POS = static_cast<uint8_t>(BPOS);
//-----------------------------------------------------------------//
/*!
@brief ポート方向レジスタ
*/
//-----------------------------------------------------------------//
static inline bit_rw_t<rw8_t<PORTX::base_address_ + 0x00>, BPOS> DIR;
//-----------------------------------------------------------------//
/*!
@brief ポートを出力にする
*/
//-----------------------------------------------------------------//
static void OUTPUT() { DIR = 1; }
//-----------------------------------------------------------------//
/*!
@brief ポートを入力にする
*/
//-----------------------------------------------------------------//
static void INPUT() { DIR = 0; }
//-----------------------------------------------------------------//
/*!
@brief プルアップ制御・レジスタ
*/
//-----------------------------------------------------------------//
static inline bit_rw_t<rw8_t<PORTX::base_address_ + 0xC0>, BPOS> PU;
//-----------------------------------------------------------------//
/*!
@brief オープンドレイン制御・レジスタ
*/
//-----------------------------------------------------------------//
struct od_t {
static rw8_t<PORTX::base_address_ + 0x80> ODR;
void operator = (OD_TYPE val) {
uint8_t pos = static_cast<uint8_t>(BPOS);
ODR = (ODR() & ~(3 << pos)) | (static_cast<uint8_t>(val) << pos);
}
OD_TYPE operator () () {
uint8_t pos = static_cast<uint8_t>(BPOS);
return static_cast<OD_TYPE>(ODR() & (3 << pos));
}
};
static inline od_t OD;
//-----------------------------------------------------------------//
/*!
@brief ポート・レジスタ @n
※ポート出力と、ポート入力が異なる
*/
//-----------------------------------------------------------------//
struct port_t {
static inline bit_rw_t<rw8_t<PORTX::base_address_ + 0x20>, BPOS> PO; // ポート出力用
static inline bit_ro_t<ro8_t<PORTX::base_address_ + 0x40>, BPOS> PI; // ポート入力用
void operator = (bool val) {
if(ASSERT) { PO = val; } else { PO = !val; }
}
bool operator () () {
if(ASSERT) { return PI(); }
else { return !PI(); }
}
};
static inline port_t P;
//-----------------------------------------------------------------//
/*!
@brief 反転
*/
//-----------------------------------------------------------------//
static void FLIP() { P = !P(); }
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 8ビット・ポート定義
@param[in] PORTx ポート・クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class PORTx>
struct PORT_BYTE {
//-----------------------------------------------------------------//
/*!
@brief ポート方向レジスタ
*/
//-----------------------------------------------------------------//
static inline rw8_t<PORTx::base_address_ + 0x00> DIR;
//-----------------------------------------------------------------//
/*!
@brief プルアップ制御・レジスタ
*/
//-----------------------------------------------------------------//
static inline rw8_t<PORTx::base_address_ + 0xC0> PU;
//-----------------------------------------------------------------//
/*!
@brief ポート・レジスタ @n
※ポート出力と、ポート入力が異なる
*/
//-----------------------------------------------------------------//
struct port_t {
static inline rw8_t<PORTx::base_address_ + 0x20> PO; // ポート出力用
static inline ro8_t<PORTx::base_address_ + 0x40> PI; // ポート入力用
void operator = (uint8_t val) { PO = val; }
uint8_t operator () () { return PI(); }
};
static inline port_t P;
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 無効ポート定義
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
struct NULL_PORT {
static constexpr uint8_t PNO = 0xff;
static constexpr uint8_t BIT_POS = 0xff;
struct null_t {
void operator = (bool f) { }
bool operator () () const { return 0; }
};
//-----------------------------------------------------------------//
/*!
@brief ポート方向レジスタ
*/
//-----------------------------------------------------------------//
static inline null_t DIR;
//-----------------------------------------------------------------//
/*!
@brief プルアップ制御・レジスタ
*/
//-----------------------------------------------------------------//
static inline null_t PU;
//-----------------------------------------------------------------//
/*!
@brief オープンドレイン制御・レジスタ
*/
//-----------------------------------------------------------------//
static inline null_t OD;
//-----------------------------------------------------------------//
/*!
@brief ポート・レジスタ
*/
//-----------------------------------------------------------------//
static inline null_t P;
};
}