-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathad.hpp
320 lines (270 loc) · 9.4 KB
/
ad.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
#pragma once
//=============================================================================//
/*! @file
@brief RX621/RX62N 10 ビット A/D コンバータ(ADa)
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2022, 2024 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=============================================================================//
#include "common/device.hpp"
#include "RX600/ad_utils.hpp"
namespace device {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief A/D コンバータ・ベース・クラス
@param[in] base ベースアドレス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template<uint32_t base>
struct ad_base_t {
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ A(ADDRA)
*/
//-----------------------------------------------------------------//
typedef ro16_t<base + 0x00> ADDRA_;
static inline ADDRA_ ADDRA;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ B(ADDRB)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x02> ADDRB;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ C(ADDRC)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x04> ADDRC;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ D(ADDRD)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x06> ADDRD;
//-----------------------------------------------------------------//
/*!
@brief A/D コントロール/ステータスレジスタ(ADCSR)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adcsr_t : public rw8_t<ofs> {
typedef rw8_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 4> CH;
bit_rw_t <io_, bitpos::B5> ADST;
bit_rw_t <io_, bitpos::B6> ADIE;
};
static inline adcsr_t<base + 0x10> ADCSR;
//-----------------------------------------------------------------//
/*!
@brief A/D コントロールレジスタ(ADCR)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adcr_t : public rw8_t<ofs> {
typedef rw8_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 2> MODE;
bits_rw_t<io_, bitpos::B2, 2> CKS;
bits_rw_t<io_, bitpos::B5, 3> TRGS;
};
static inline adcr_t<base + 0x11> ADCR;
//-----------------------------------------------------------------//
/*!
@brief ADDRn フォーマット選択レジスタ(ADDPR)(n = A ~ D)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct addpr_t : public rw8_t<ofs> {
typedef rw8_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B7> DPSEL;
};
static inline addpr_t<base + 0x12> ADDPR;
//-----------------------------------------------------------------//
/*!
@brief A/D サンプリングステートレジスタ(ADSSTR)
*/
//-----------------------------------------------------------------//
static inline rw8_t<base + 0x13> ADSSTR;
//-----------------------------------------------------------------//
/*!
@brief A/D 自己診断レジスタ(ADDIAGR)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct addiagr_t : public rw8_t<ofs> {
typedef rw8_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 2> DIAG;
};
static inline addiagr_t<base + 0x1F> ADDIAGR;
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief A/D コンバータ・ベース・クラス
@param[in] base ベースアドレス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t base>
struct ad0_t : public ad_base_t<base> {
typedef ad_base_t<base> BASE;
static constexpr auto PERIPHERAL = peripheral::AD0; ///< ペリフェラル型
static constexpr auto ADI = ICU::VECTOR::ADI0; ///< A/D 変換終了割り込みベクター
static constexpr auto GBADI = ICU::VECTOR::NONE; ///< グループBスキャン終了割り込みベクター
static constexpr auto GCADI = ICU::VECTOR::NONE; ///< グループCスキャン終了割り込みベクター
static constexpr auto CMPAI = ICU::VECTOR::NONE; ///< コンペアA割り込みベクター
static constexpr auto CMPBI = ICU::VECTOR::NONE; ///< コンペアB割り込みベクター
static constexpr auto PCLK = clock_profile::PCLK; ///< A/D 変換クロック元
static constexpr uint32_t CONV_TIME_NS = 1000; ///< 変換時間(ns)
static constexpr uint32_t ANALOG_NUM = 4; ///< アナログ入力数
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief アナログ入力型
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class ANALOG : uint8_t {
AN0,
AN1,
AN2,
AN3,
};
//-----------------------------------------------------------------//
/*!
@brief ポート設定と解除
@param[in] an アナログ入力型
@param[in] ena ポート無効の場合「false」
*/
//-----------------------------------------------------------------//
static void enable(ANALOG an, bool ena = true) noexcept
{
switch(an) {
case ANALOG::AN0:
if(ena) {
PORT4::DDR.B0 = 0;
}
PORT4::ICR.B0 = ena;
break;
case ANALOG::AN1:
if(ena) {
PORT4::DDR.B1 = 0;
}
PORT4::ICR.B1 = ena;
break;
case ANALOG::AN2:
if(ena) {
PORT4::DDR.B2 = 0;
}
PORT4::ICR.B2 = ena;
break;
case ANALOG::AN3:
if(ena) {
PORT4::DDR.B3 = 0;
}
PORT4::ICR.B3 = ena;
break;
default:
break;
}
}
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ(ADDR)
*/
//-----------------------------------------------------------------//
static inline ad_utils::addr_t<ANALOG, BASE::ADDRA_::address> ADDR;
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief A/D コンバータ・ベース・クラス
@param[in] base ベースアドレス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t base>
struct ad1_t : public ad_base_t<base> {
typedef ad_base_t<base> BASE;
static constexpr auto PERIPHERAL = peripheral::AD1; ///< ペリフェラル型
static constexpr auto ADI = ICU::VECTOR::ADI1; ///< A/D 変換終了割り込みベクター
static constexpr auto GBADI = ICU::VECTOR::NONE; ///< グループBスキャン終了割り込みベクター
static constexpr auto GCADI = ICU::VECTOR::NONE; ///< グループCスキャン終了割り込みベクター
static constexpr auto CMPAI = ICU::VECTOR::NONE; ///< コンペアA割り込みベクター
static constexpr auto CMPBI = ICU::VECTOR::NONE; ///< コンペアB割り込みベクター
static constexpr auto PCLK = clock_profile::PCLK; ///< A/D 変換クロック元
static constexpr uint32_t CONV_TIME_NS = 1000; ///< 変換時間(ns)
static constexpr uint32_t ANALOG_NUM = 4; ///< アナログ入力数
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief アナログ入力型
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class ANALOG : uint8_t {
AN4,
AN5,
AN6,
AN7,
};
//-----------------------------------------------------------------//
/*!
@brief ポート設定と解除
@param[in] an アナログ入力型
@param[in] ena ポート無効の場合「false」
*/
//-----------------------------------------------------------------//
static void enable(ANALOG an, bool ena = true) noexcept
{
switch(an) {
case ANALOG::AN4:
if(ena) {
PORT4::DDR.B4 = 0;
}
PORT4::ICR.B4 = ena;
break;
case ANALOG::AN5:
if(ena) {
PORT4::DDR.B5 = 0;
}
PORT4::ICR.B5 = ena;
break;
case ANALOG::AN6:
if(ena) {
PORT4::DDR.B6 = 0;
}
PORT4::ICR.B6 = ena;
break;
case ANALOG::AN7:
if(ena) {
PORT4::DDR.B7 = 0;
}
PORT4::ICR.B7 = ena;
break;
}
}
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ(ADDR)
*/
//-----------------------------------------------------------------//
static inline ad_utils::addr_t<ANALOG, BASE::ADDRA_::address> ADDR;
};
typedef ad0_t<0x0008'8040> AD0;
typedef ad1_t<0x0008'8060> AD1;
}