アナログ・コンパレータ(AC)ライブラリを作ってみた。
これで、Seeeduino XIAO のArduino-Core或いはlibrariesで非対応な周辺機能の対応はほぼ完了したのかな。
さて、あとは、どうしょうかな...
【概要】
1 |
void begin(bool runstdby = false, bool lpmux = false) |
ライブラリを初期化する。
1 |
void end(void) |
ライブラリを終了する。
1 2 3 |
void config(ANACOMP_COMP comp, ANACOMP_MUXPOS pos, ANACOMP_MUXNEG neg, bool singleShot = false, bool hyst = false, bool swap = false, ANACOMP_SPEED speed = ANACOMP_SPEED_HIGH, ANACOMP_OUT out = ANACOMP_OUT_OFF, ANACOMP_FLEN flen = ANACOMP_FLEN_OFF) |
コンパレータを設定する。
1 |
void scaler(ANACOMP_COMP comp, uint8_t value) |
VDDANA voltage scaler を設定する。
1 |
void start(ANACOMP_COMP comp = ANACOMP_COMP0) |
コンパレータをスタートする。シングル・ショット・モード或いはウインドウ・モードを起動する場合は、コンパレータ(comp)の指定が必要。 それ以外はコンパレータ(comp)省略可能。
1 |
void setCallback(ANACOMP_CALLBACK cb) |
割り込みコールバック関数を登録する。
1 |
void interrupt(ANACOMP_COMP comp, ANACOMP_INTSEL sel, bool enable) |
コンパレータの割り込みを設定する。
1 |
void winterrupt(ANACOMP_WINTSEL sel, bool enable) |
ウインドウ・モードの割り込みを設定する。
1 |
EVENTSYS_USER eventInput(ANACOMP_COMP comp, bool enable) |
イベント入力を設定する。
1 |
EVENTSYS_GEN eventOutput(ANACOMP_COMP comp, bool enable) |
イベント出力を設定する。
1 |
bool state(ANACOMP_COMP comp) |
コンパレータの状態を返す。
1 |
ANACOMP_WSTATE wstate(void) |
ウインドウ・モードの状態を返す。
【サンプル・スケッチ】
ウインドウ・モードを使ってA9ピンの電圧が1.0V – 2.0Vの範囲を外れたときに割り込みを起こすサンプル・スケッチ。
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 |
#include "anacomp.h" void AnaComp_Callback(ANACOMP_COMP comp) { // Interrupt on signal outside window switch (AnaComp::wstate()) { case ANACOMP_WSTATE_ABOVE: // 2.0V <= A9 break; case ANACOMP_WSTATE_INSIDE: // 1.0V < A9 < 2.0V break; case ANACOMP_WSTATE_BELOW: // A9 <= 1.0V break; } } void setup() { AnaComp::begin(); AnaComp::config(ANACOMP_COMP0, ANACOMP_MUXPOS_PIN_A9, ANACOMP_MUXNEG_VSCALE); AnaComp::scaler(ANACOMP_COMP0, ANACOMP_SCALER_MAX * 2.0 / 3.3 - 1); AnaComp::config(ANACOMP_COMP1, ANACOMP_MUXPOS_PIN_A9, ANACOMP_MUXNEG_VSCALE); AnaComp::scaler(ANACOMP_COMP1, ANACOMP_SCALER_MAX * 1.0 / 3.3 - 1); AnaComp::setCallback(AnaComp_Callback); AnaComp::winterrupt(ANACOMP_WINTSEL_OUTSIDE, true); AnaComp::start(ANACOMP_WIN0); } void loop() { } |
【修正】
2020-06-11
XIAO用のポート定義をデジタルピン名からアナログピン名へ変更。
2020-05-24
eventsys.hのインクルード追加と、eventInput()及びeventOutput()を仕様変更し、EVENTSYS_USER型/EVENTSYS_GEN型を返すように修正。
【ライブラリ】
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 |
/* anacomp.h - Analog Comparator Library for Microchip ATSAMD21 (Cortex®-M0+) Copyright (c) 2020 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef __ANACOMP_H #define __ANACOMP_H #include <stdint.h> #include <stdbool.h> #include "gclock.h" #include "eventsys.h" #include "IOBUS.h" // // Port Define for Seeeduino XIAO // #ifdef SEEED_XIAO_M0 #define ANACOMP_MUXPOS_PIN_A1 ANACOMP_MUXPOS_PIN0 #define ANACOMP_MUXPOS_PIN_A8 ANACOMP_MUXPOS_PIN3 #define ANACOMP_MUXPOS_PIN_A9 ANACOMP_MUXPOS_PIN1 #define ANACOMP_MUXPOS_PIN_A10 ANACOMP_MUXPOS_PIN2 #define ANACOMP_MUXNEG_PIN_A1 ANACOMP_MUXNEG_PIN0 #define ANACOMP_MUXNEG_PIN_A8 ANACOMP_MUXNEG_PIN3 #define ANACOMP_MUXNEG_PIN_A9 ANACOMP_MUXNEG_PIN1 #define ANACOMP_MUXNEG_PIN_A10 ANACOMP_MUXNEG_PIN2 #endif #define ANACOMP_PIN_AIN0 IOBUS_PIN('A', 4) // AIN[0] #define ANACOMP_PIN_AIN1 IOBUS_PIN('A', 5) // AIN[1] #define ANACOMP_PIN_AIN2 IOBUS_PIN('A', 6) // AIN[2] #define ANACOMP_PIN_AIN3 IOBUS_PIN('A', 7) // AIN[3] #if 1 #define ANACOMP_PIN_OUT0 IOBUS_PIN('A', 18) // AC/CMP[0] // SAMD21J/G/E #define ANACOMP_PIN_OUT1 IOBUS_PIN('A', 19) // AC/CMP[1] // SAMD21J/G/E #else #define ANACOMP_PIN_OUT0 IOBUS_PIN('A', 12) // AC/CMP[0] // SAMD21J/G #define ANACOMP_PIN_OUT1 IOBUS_PIN('A', 13) // AC/CMP[1] // SAMD21J/G #endif typedef enum { ANACOMP_COMP0 = 0, ANACOMP_COMP1 = 1, ANACOMP_WIN0 = 2, } ANACOMP_COMP; typedef enum { ANACOMP_SPEED_LOW = AC_COMPCTRL_SPEED_LOW_Val, // Low speed ANACOMP_SPEED_HIGH = AC_COMPCTRL_SPEED_HIGH_Val, // High speed } ANACOMP_SPEED; typedef enum { ANACOMP_MUXNEG_PIN0 = AC_COMPCTRL_MUXNEG_PIN0_Val, // I/O pin 0 ANACOMP_MUXNEG_PIN1 = AC_COMPCTRL_MUXNEG_PIN1_Val, // I/O pin 1 ANACOMP_MUXNEG_PIN2 = AC_COMPCTRL_MUXNEG_PIN2_Val, // I/O pin 2 ANACOMP_MUXNEG_PIN3 = AC_COMPCTRL_MUXNEG_PIN3_Val, // I/O pin 3 ANACOMP_MUXNEG_GND = AC_COMPCTRL_MUXNEG_GND_Val, // Ground ANACOMP_MUXNEG_VSCALE = AC_COMPCTRL_MUXNEG_VSCALE_Val, // VDD scaler ANACOMP_MUXNEG_BANDGAP = AC_COMPCTRL_MUXNEG_BANDGAP_Val, // Internal bandgap voltage ANACOMP_MUXNEG_DAC = AC_COMPCTRL_MUXNEG_DAC_Val, // DAC output } ANACOMP_MUXNEG; typedef enum { ANACOMP_MUXPOS_PIN0 = AC_COMPCTRL_MUXPOS_PIN0_Val, // I/O pin 0 ANACOMP_MUXPOS_PIN1 = AC_COMPCTRL_MUXPOS_PIN1_Val, // I/O pin 1 ANACOMP_MUXPOS_PIN2 = AC_COMPCTRL_MUXPOS_PIN2_Val, // I/O pin 2 ANACOMP_MUXPOS_PIN3 = AC_COMPCTRL_MUXPOS_PIN3_Val, // I/O pin 3 } ANACOMP_MUXPOS; #define ANACOMP_SCALER_MAX 64 // scaler(0 - 63) typedef enum { ANACOMP_FLEN_OFF = AC_COMPCTRL_FLEN_OFF_Val, // No filtering ANACOMP_FLEN_MAJ3 = AC_COMPCTRL_FLEN_MAJ3_Val, // 3-bit majority function (2 of 3) ANACOMP_FLEN_MAJ5 = AC_COMPCTRL_FLEN_MAJ5_Val, // 5-bit majority function (3 of 5) } ANACOMP_FLEN; typedef enum { ANACOMP_OUT_OFF = AC_COMPCTRL_OUT_OFF_Val, // The output of COMPn is not routed to the COMPn I/O port ANACOMP_OUT_ASYNC = AC_COMPCTRL_OUT_ASYNC_Val, // The asynchronous output of COMPn is routed to the COMPn I/O port ANACOMP_OUT_SYNC = AC_COMPCTRL_OUT_SYNC_Val, // The synchronous output (including filtering) of COMPn is routed to the COMPn I/O port } ANACOMP_OUT; typedef enum { ANACOMP_INTSEL_TOGGLE = AC_COMPCTRL_INTSEL_TOGGLE_Val, // Interrupt on comparator output toggle ANACOMP_INTSEL_RISING = AC_COMPCTRL_INTSEL_RISING_Val, // Interrupt on comparator output rising ANACOMP_INTSEL_FALLING = AC_COMPCTRL_INTSEL_FALLING_Val, // Interrupt on comparator output falling ANACOMP_INTSEL_EOC = AC_COMPCTRL_INTSEL_EOC_Val, // Interrupt on end of comparison (single-shot mode only) } ANACOMP_INTSEL; typedef enum { ANACOMP_WINTSEL_ABOVE = AC_WINCTRL_WINTSEL0_ABOVE_Val, // Interrupt on signal above window ANACOMP_WINTSEL_INSIDE = AC_WINCTRL_WINTSEL0_INSIDE_Val, // Interrupt on signal inside window ANACOMP_WINTSEL_BELOW = AC_WINCTRL_WINTSEL0_BELOW_Val, // Interrupt on signal below window ANACOMP_WINTSEL_OUTSIDE = AC_WINCTRL_WINTSEL0_OUTSIDE_Val, // Interrupt on signal outside window } ANACOMP_WINTSEL; typedef enum { ANACOMP_WSTATE_ABOVE = AC_STATUSA_WSTATE0_ABOVE_Val, // Signal is above window ANACOMP_WSTATE_INSIDE = AC_STATUSA_WSTATE0_INSIDE_Val, // Signal is inside window ANACOMP_WSTATE_BELOW = AC_STATUSA_WSTATE0_BELOW_Val, // Signal is below window } ANACOMP_WSTATE; typedef void (*ANACOMP_CALLBACK)(ANACOMP_COMP); #if defined(__GNUC__) #pragma GCC diagnostic ignored "-Wswitch" #endif class AnaComp { private: static void sync(void) { while (AC->CTRLA.bit.SWRST || AC->STATUSB.bit.SYNCBUSY) continue; } static void controlGCLK(bool lpmux) { GClock::select (GCLOCK_ID_AC_ANA, GCLOCK_GEN_OSCULP32K); GClock::control(GCLOCK_ID_AC_ANA, !lpmux); // 32.768KHz GClock::control(GCLOCK_ID_AC_DIG, true); // 48MHz } static void stop(ANACOMP_COMP comp) { switch (comp) { case ANACOMP_COMP0: case ANACOMP_COMP1: if (AC->COMPCTRL[comp].bit.ENABLE) { multiplexing((ANACOMP_MUXPOS)AC->COMPCTRL[comp].bit.MUXNEG, false); multiplexing((ANACOMP_MUXPOS)AC->COMPCTRL[comp].bit.MUXPOS, false); if (AC->COMPCTRL[comp].bit.OUT != ANACOMP_OUT_OFF) multiplexing(comp, false); AC->COMPCTRL[comp].reg = AC_COMPCTRL_RESETVALUE; sync(); } break; } } static void multiplexing(ANACOMP_MUXPOS mux, bool enable) { switch (mux) { case ANACOMP_MUXPOS_PIN0: IOBUS::pinMode(ANACOMP_PIN_AIN0, DISABLE); break; case ANACOMP_MUXPOS_PIN1: IOBUS::pinMode(ANACOMP_PIN_AIN1, DISABLE); break; case ANACOMP_MUXPOS_PIN2: IOBUS::pinMode(ANACOMP_PIN_AIN2, DISABLE); break; case ANACOMP_MUXPOS_PIN3: IOBUS::pinMode(ANACOMP_PIN_AIN3, DISABLE); break; } if (enable) { switch (mux) { case ANACOMP_MUXPOS_PIN0: IOBUS::multiplexing(ANACOMP_PIN_AIN0, IOBUS_PMUX_B); break; case ANACOMP_MUXPOS_PIN1: IOBUS::multiplexing(ANACOMP_PIN_AIN1, IOBUS_PMUX_B); break; case ANACOMP_MUXPOS_PIN2: IOBUS::multiplexing(ANACOMP_PIN_AIN2, IOBUS_PMUX_B); break; case ANACOMP_MUXPOS_PIN3: IOBUS::multiplexing(ANACOMP_PIN_AIN3, IOBUS_PMUX_B); break; } } } static void multiplexing(ANACOMP_COMP comp, bool enable) { if (enable) { switch (comp) { case ANACOMP_COMP0: IOBUS::pinMode(ANACOMP_PIN_OUT0, OUTPUT); IOBUS::multiplexing(ANACOMP_PIN_OUT0, IOBUS_PMUX_H); break; case ANACOMP_COMP1: IOBUS::pinMode(ANACOMP_PIN_OUT1, OUTPUT); IOBUS::multiplexing(ANACOMP_PIN_OUT1, IOBUS_PMUX_H); break; } } #ifndef SEEED_XIAO_M0 else { switch (comp) { case ANACOMP_COMP0: IOBUS::pinMode(ANACOMP_PIN_OUT0, DISABLE); break; case ANACOMP_COMP1: IOBUS::pinMode(ANACOMP_PIN_OUT1, DISABLE); break; } } #endif } public: static void begin(bool runstdby = false, bool lpmux = false) { if (AC->CTRLA.bit.ENABLE) return; PM->APBCMASK.bit.AC_ = 1; controlGCLK(lpmux); AC->CTRLA.reg = AC_CTRLA_SWRST; sync(); AC->CTRLA.reg = (lpmux ? AC_CTRLA_LPMUX : 0) | (runstdby ? AC_CTRLA_RUNSTDBY(1) : 0); NVIC_EnableIRQ(AC_IRQn); NVIC_SetPriority(AC_IRQn, 0); } static void end(void) { if (PM->APBCMASK.bit.AC_) { NVIC_DisableIRQ(AC_IRQn); stop(ANACOMP_COMP0); stop(ANACOMP_COMP1); AC->CTRLA.reg = AC_CTRLA_SWRST; sync(); GClock::control(GCLOCK_ID_AC_DIG, false); GClock::control(GCLOCK_ID_AC_ANA, false); PM->APBCMASK.bit.AC_ = 0; } } static void config(ANACOMP_COMP comp, ANACOMP_MUXPOS pos, ANACOMP_MUXNEG neg, bool singleShot = false, bool hyst = false, bool swap = false, ANACOMP_SPEED speed = ANACOMP_SPEED_HIGH, ANACOMP_OUT out = ANACOMP_OUT_OFF, ANACOMP_FLEN flen = ANACOMP_FLEN_OFF) { switch (comp) { case ANACOMP_COMP0: case ANACOMP_COMP1: AC->COMPCTRL[comp].reg = AC_COMPCTRL_ENABLE | (singleShot ? AC_COMPCTRL_SINGLE : 0) | AC_COMPCTRL_SPEED(speed) | AC_COMPCTRL_MUXNEG(neg) | AC_COMPCTRL_MUXPOS(pos) | (swap ? AC_COMPCTRL_SWAP : 0) | AC_COMPCTRL_OUT(out) | (hyst ? AC_COMPCTRL_HYST : 0) | AC_COMPCTRL_FLEN(flen); sync(); multiplexing((ANACOMP_MUXPOS)pos, true); multiplexing((ANACOMP_MUXPOS)neg, true); if (out != ANACOMP_OUT_OFF) multiplexing(comp, true); break; } } static void scaler(ANACOMP_COMP comp, uint8_t value) { switch (comp) { case ANACOMP_COMP0: case ANACOMP_COMP1: AC->SCALER[comp].reg = AC_SCALER_VALUE(value); break; } } static void start(ANACOMP_COMP comp = ANACOMP_COMP0) { // enable AC if (!AC->CTRLA.bit.ENABLE) { AC->WINCTRL.bit.WEN0 = (comp == ANACOMP_WIN0); sync(); AC->CTRLA.bit.ENABLE = 1; sync(); } // start single-shot mode. continuous mode has no effect. switch (comp) { case ANACOMP_WIN0 : case ANACOMP_COMP0: AC->CTRLB.bit.START0 = 1; break; case ANACOMP_COMP1: AC->CTRLB.bit.START1 = 1; break; } } static void setCallback(ANACOMP_CALLBACK cb); static void interrupt(ANACOMP_COMP comp, ANACOMP_INTSEL sel, bool enable) { switch (comp) { case ANACOMP_COMP0: case ANACOMP_COMP1: AC->COMPCTRL[comp].bit.INTSEL = sel; if (enable) AC->INTENSET.reg = AC_INTENSET_COMP(1 << comp); else AC->INTENCLR.reg = AC_INTENCLR_COMP(1 << comp); break; } } static void winterrupt(ANACOMP_WINTSEL sel, bool enable) { AC->WINCTRL.bit.WINTSEL0 = sel; if (enable) AC->INTENSET.reg = AC_INTENSET_WIN0; else AC->INTENCLR.reg = AC_INTENCLR_WIN0; } static EVENTSYS_USER eventInput(ANACOMP_COMP comp, bool enable) { switch (comp) { case ANACOMP_COMP0: AC->EVCTRL.bit.COMPEI0 = enable; return EVENTSYS_USER_AC_SOC_0; case ANACOMP_COMP1: AC->EVCTRL.bit.COMPEI1 = enable; return EVENTSYS_USER_AC_SOC_1; } return EVENTSYS_USER_NONE; } static EVENTSYS_GEN eventOutput(ANACOMP_COMP comp, bool enable) { switch (comp) { case ANACOMP_COMP0: AC->EVCTRL.bit.COMPEO0 = enable; return EVENTSYS_GEN_AC_COMP_0; case ANACOMP_COMP1: AC->EVCTRL.bit.COMPEO1 = enable; return EVENTSYS_GEN_AC_COMP_1; case ANACOMP_WIN0 : AC->EVCTRL.bit.WINEO0 = enable; return EVENTSYS_GEN_AC_WIN_0; } return EVENTSYS_GEN_NONE; } static bool state(ANACOMP_COMP comp) { switch (comp) { case ANACOMP_COMP0: return AC->STATUSA.bit.STATE0 & AC->STATUSB.bit.READY0; case ANACOMP_COMP1: return AC->STATUSA.bit.STATE1 & AC->STATUSB.bit.READY1; } return false; } static ANACOMP_WSTATE wstate(void) { return (ANACOMP_WSTATE)AC->STATUSA.bit.WSTATE0; } }; #endif |
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 |
/* anacomp.cpp - Analog Comparator Library for Microchip ATSAMD21 (Cortex®-M0+) Copyright (c) 2020 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "anacomp.h" static ANACOMP_CALLBACK _callback; void AC_Handler(void) { if (AC->INTFLAG.bit.WIN0) { AC->INTFLAG.reg = AC_INTFLAG_WIN0; if (AC->INTENSET.bit.WIN0 && _callback) _callback(ANACOMP_WIN0); } if (AC->INTFLAG.bit.COMP1) { AC->INTFLAG.reg = AC_INTFLAG_COMP1; if (AC->INTENSET.bit.COMP1 && _callback) _callback(ANACOMP_COMP1); } if (AC->INTFLAG.bit.COMP0) { AC->INTFLAG.reg = AC_INTFLAG_COMP0; if (AC->INTENSET.bit.COMP0 && _callback) _callback(ANACOMP_COMP0); } } void AnaComp::setCallback(ANACOMP_CALLBACK cb) { _callback = cb; } |
【参照ライブラリ】
Seeeduino XIAO (ATSAMD21G18) のGPIO速度 (IOBUS.h)
Seeeduino XIAO (ATSAMD21G18) のGCLKライブラリ (gclock.h/gclock.cpp)
Seeeduino XIAO (ATSAMD21G18) のEVSYSライブラリ (eventsys.h/eventsys.cpp)