-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy paths12ad.hpp
359 lines (305 loc) · 10.1 KB
/
s12ad.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
#pragma once
//=========================================================================//
/*! @file
@brief 12-Bit A/D Converter / 12 ビット A/D コンバータ (RX621/RX62N)
@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 S12AD base class
@param[in] base ベース・アドレス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t base>
struct s12ad_base_t {
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 0(ADDR0)
*/
//-----------------------------------------------------------------//
typedef ro16_t<base + 0x20> ADDR0_;
static inline ADDR0_ ADDR0;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 1(ADDR1)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x22> ADDR1;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 2(ADDR2)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x24> ADDR2;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 3(ADDR3)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x26> ADDR3;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 4(ADDR4)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x28> ADDR4;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 5(ADDR5)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x2A> ADDR5;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 6(ADDR6)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x2C> ADDR6;
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ 7(ADDR7)
*/
//-----------------------------------------------------------------//
static inline ro16_t<base + 0x2E> ADDR7;
//-----------------------------------------------------------------//
/*!
@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 &=;
bit_rw_t <io_, bitpos::B0> EXTRG;
bit_rw_t <io_, bitpos::B1> TRGE;
bits_rw_t<io_, bitpos::B2, 2> CKS;
bit_rw_t <io_, bitpos::B4> ADIE;
bit_rw_t <io_, bitpos::B6> ADCS;
bit_rw_t <io_, bitpos::B7> ADST;
};
static inline adcsr_t<base + 0x00> ADCSR;
//-----------------------------------------------------------------//
/*!
@brief A/D 変換値加算モード選択レジスタ(ADADS)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adads_t : public rw16_t<ofs> {
typedef rw16_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 8> ADS;
};
static inline adads_t<base + 0x08> ADADS;
//-----------------------------------------------------------------//
/*!
@brief A/D 変換値加算 / 平均回数選択レジスタ(ADADC)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adadc_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> ADC;
};
static inline adadc_t<base + 0x0C> ADADC;
//-----------------------------------------------------------------//
/*!
@brief A/D コントロール拡張レジスタ(ADCER)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adcer_t : public rw16_t<ofs> {
typedef rw16_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bit_rw_t <io_, bitpos::B5> ACE;
bit_rw_t <io_, bitpos::B15> ADRFMT;
};
static inline adcer_t<base + 0x0E> ADCER;
//-----------------------------------------------------------------//
/*!
@brief A/D 変換開始トリガ選択レジスタ(ADSTRGR)
@param[in] ofs オフセット
*/
//-----------------------------------------------------------------//
template <uint32_t ofs>
struct adstrgr_t : public rw16_t<ofs> {
typedef rw16_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 4> ADSTRS;
};
static inline adstrgr_t<base + 0x10> ADSTRGR;
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief S12AD 定義
@param[in] base ベース・アドレス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t base>
struct s12ad_t : public s12ad_base_t<base> {
typedef s12ad_base_t<base> BASE;
static constexpr auto PERIPHERAL = peripheral::S12AD; ///< ペリフェラル型
static constexpr auto ADI = ICU::VECTOR::S12ADI0; ///< 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 = 8; ///< アナログ入力数
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief アナログ入力型
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
enum class ANALOG : uint8_t {
AN000,
AN001,
AN002,
AN003,
AN004,
AN005,
AN006,
AN007,
};
//-----------------------------------------------------------------//
/*!
@brief ポート設定と解除
@param[in] an アナログ入力型
@param[in] ena ポート無効の場合「false」
*/
//-----------------------------------------------------------------//
static void enable(ANALOG an, bool ena = true)
{
switch(an) {
case ANALOG::AN000:
if(ena) {
PORT4::DDR.B0 = 0;
}
PORT4::ICR.B0 = ena;
break;
case ANALOG::AN001:
if(ena) {
PORT4::DDR.B1 = 0;
}
PORT4::ICR.B1 = ena;
break;
case ANALOG::AN002:
if(ena) {
PORT4::DDR.B2 = 0;
}
PORT4::ICR.B2 = ena;
break;
case ANALOG::AN003:
if(ena) {
PORT4::DDR.B3 = 0;
}
PORT4::ICR.B3 = ena;
break;
case ANALOG::AN004:
if(ena) {
PORT4::DDR.B4 = 0;
}
PORT4::ICR.B4 = ena;
break;
case ANALOG::AN005:
if(ena) {
PORT4::DDR.B5 = 0;
}
PORT4::ICR.B5 = ena;
break;
case ANALOG::AN006:
if(ena) {
PORT4::DDR.B6 = 0;
}
PORT4::ICR.B6 = ena;
break;
case ANALOG::AN007:
if(ena) {
PORT4::DDR.B7 = 0;
}
PORT4::ICR.B7 = ena;
break;
default:
break;
}
}
//-----------------------------------------------------------------//
/*!
@brief A/D データレジスタ(ADDR)
*/
//-----------------------------------------------------------------//
static inline ad_utils::addr_t<ANALOG, BASE::ADDR0_::address> ADDR;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief A/D チャネル選択レジスタ定義 (ADANS)
@param[in] ofs オフセット
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t ofs>
struct adans_t : public rw16_t<ofs> {
typedef rw16_t<ofs> io_;
using io_::operator =;
using io_::operator ();
using io_::operator |=;
using io_::operator &=;
bits_rw_t<io_, bitpos::B0, 8> ANS;
//-------------------------------------------------------------//
/*!
@brief A/D チャネル選択
@param[in] an アナログ入力型
@param[in] ena チャネルを無効にする場合「false」
*/
//-------------------------------------------------------------//
void set(ANALOG an, bool ena = true) noexcept
{
auto n = static_cast<uint8_t>(an);
if(ena) {
wr16_(ofs, rd16_(ofs) | (static_cast<uint16_t>(1) << n));
} else {
wr16_(ofs, rd16_(ofs) & ~(static_cast<uint16_t>(1) << n));
}
}
//-------------------------------------------------------------//
/*!
@brief A/D チャネル選択オペレーター
@param[in] an アナログ入力型
@return A/D チャネル選択
*/
//-------------------------------------------------------------//
bool operator() (ANALOG an) const noexcept
{
auto n = static_cast<uint32_t>(an);
return (rd16_(ofs) >> n) & 1;
}
};
static inline adans_t<base + 0x04> ADANS;
};
typedef s12ad_t<0x0008'9000> S12AD;
}