DACライブラリを作ってみた。が、DAC出力は正常であるものの割り込みとイベント連携が正常に動作しない。ユースケース的にはそれらが動作しなくても困ることはなさそうではあるものの...プログラムの問題なのかハードウェアの問題なのかDACの仕様が不明確でわからないことが多すぎるし簡単なプログラムなだけに何が問題なのか検討も付かない。
何が問題なのかわかる人がいれば教えて。お願いします。<(_ _)>
【概要】
1 |
void begin(DACONVERTER_REFSEL refsel, bool external = false, bool internal = false, bool runstdby = false) |
ライブラリを初期化する。
1 |
void end(void) |
ライブラリを終了する。
1 |
void config(bool leftadj = false, bool bdwp = false, bool vpd = false) |
DACの設定を行う。
1 |
void setCallback(DACONVERTER_CALLBACK cb) |
割り込みコールバック関数を設定する。
1 |
void interrupt(DACONVERTER_INT flag, bool enable) |
割り込み要因の設定を行う。
1 |
EVENTSYS_USER eventInput(bool enable) |
イベント入力を設定する。
1 |
EVENTSYS_GEN eventOutput(bool enable) |
イベント出力を設定する。
1 |
void start(void) |
DACを開始する。
1 |
void stop(void) |
DACを停止する。
1 |
void data(uint16_t value) |
DACデータを設定する。
1 |
void databuf(uint16_t value) |
DACデータバッファを設定する。
1 |
void voltage(uint32_t value, uint32_t vref = 3300000) |
電圧(uV)を指定してDACデータを設定する。
【サンプル・スケッチ】
1 2 3 4 5 6 7 8 9 10 11 12 |
#include "daconverter.h" void setup() { DAConverter::begin(DACONVERTER_REFSEL_INT1V, true); DAConverter::voltage(500000); // 0.5V DAConverter::start(); } void loop() { } |
【ライブラリ】
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 |
/* daconverter.h - DAC 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 __DACONVERTER_H #define __DACONVERTER_H #include <stdint.h> #include <stdbool.h> #include <sam.h> #include "gclock.h" #include "eventsys.h" #include "IOBUS.h" // // Port Define for Seeeduino XIAO // #ifdef SEEED_XIAO_M0 #define DACONVERTER_VOUT_PIN_A0 DAC_VOUT_PIN_PA02 #endif #define DACONVERTER_VOUT_PIN_PA02 IOBUS_PIN('A', 2) #define DACONVERTER_VREF_PIN_PA03 IOBUS_PIN('A', 3) #define DACONVERTER_DATA_BIT 10 #define DACONVERTER_DATA_MAX ((1 << DACONVERTER_DATA_BIT) - 1) typedef enum { DACONVERTER_REFSEL_INT1V = DAC_CTRLB_REFSEL_INT1V_Val, // Internal 1.0V reference DACONVERTER_REFSEL_AVCC = DAC_CTRLB_REFSEL_AVCC_Val, // AVCC DACONVERTER_REFSEL_VREFP = DAC_CTRLB_REFSEL_VREFP_Val, // External reference } DACONVERTER_REFSEL; typedef enum { DACONVERTER_EVOSEL_START = DAC_EVCTRL_STARTEI, // Start Conversion Event Input DACONVERTER_EVOSEL_EMPTY = DAC_EVCTRL_EMPTYEO, // Data Buffer Empty Event Output } DACONVERTER_EVOSEL; typedef enum { DACONVERTER_INT_UNDERRUN = DAC_INTFLAG_UNDERRUN, // Underrun DACONVERTER_INT_EMPTY = DAC_INTFLAG_EMPTY, // Data Buffer Empty } DACONVERTER_INT; typedef void (*DACONVERTER_CALLBACK)(DACONVERTER_INT); class DAConverter { private: static void sync(void) { while (DAC->CTRLA.bit.SWRST || DAC->STATUS.bit.SYNCBUSY) continue; } static void controlGCLK(bool enable) { // GCLK = 8MHz / 23 = 347.8KHz // GClock::select (GCLOCK_ID_DAC, GClock::config(GCLOCK_SRC_OSC8M, 23)); GClock::control(GCLOCK_ID_DAC, enable); } static void multiplexing(bool enable) { if (DAC->CTRLB.bit.REFSEL == DACONVERTER_REFSEL_VREFP) IOBUS::multiplexing(DACONVERTER_VREF_PIN_PA03, enable ? IOBUS_PMUX_B : IOBUS_PMUX_DISABLE); if (DAC->CTRLB.bit.EOEN) { if (enable) { IOBUS::pinMode(DACONVERTER_VOUT_PIN_PA02, OUTPUT); IOBUS::multiplexing(DACONVERTER_VOUT_PIN_PA02, IOBUS_PMUX_B); } else IOBUS::pinMode(DACONVERTER_VOUT_PIN_PA02, DISABLE); } } public: static void begin(DACONVERTER_REFSEL refsel, bool external = false, bool internal = false, bool runstdby = false) { PM->APBCMASK.bit.DAC_ = 1; controlGCLK(true); DAC->CTRLA.reg = DAC_CTRLA_SWRST; sync(); DAC->CTRLA.bit.RUNSTDBY = runstdby; multiplexing(false); DAC->CTRLB.bit.REFSEL = refsel; DAC->CTRLB.bit.EOEN = external; // VOUT or ADC DAC->CTRLB.bit.IOEN = internal; // AC multiplexing(true); config(); NVIC_EnableIRQ(DAC_IRQn); NVIC_SetPriority(DAC_IRQn, 0); } static void end(void) { if (PM->APBCMASK.bit.DAC_) { NVIC_DisableIRQ(DAC_IRQn); multiplexing(false); DAC->CTRLA.reg = DAC_CTRLA_SWRST; sync(); controlGCLK(false); PM->APBCMASK.bit.DAC_ = 0; } } static void config(bool leftadj = false, bool bdwp = false, bool vpd = false) { DAC->CTRLB.bit.LEFTADJ = leftadj; DAC->CTRLB.bit.BDWP = bdwp; DAC->CTRLB.bit.VPD = vpd; } static void setCallback(DACONVERTER_CALLBACK cb); static void interrupt(DACONVERTER_INT flag, bool enable) { switch (flag) { case DACONVERTER_INT_UNDERRUN: if (enable) DAC->INTENSET.reg = DAC_INTENSET_UNDERRUN; else DAC->INTENCLR.reg = DAC_INTENCLR_UNDERRUN; break; case DACONVERTER_INT_EMPTY: if (enable) DAC->INTENSET.reg = DAC_INTENSET_EMPTY; else DAC->INTENCLR.reg = DAC_INTENCLR_EMPTY; break; } } static EVENTSYS_USER eventInput(bool enable) { DAC->EVCTRL.bit.STARTEI = enable; return EVENTSYS_USER_DAC_START; } static EVENTSYS_GEN eventOutput(bool enable) { DAC->EVCTRL.bit.EMPTYEO = enable; return EVENTSYS_GEN_DAC_EMPTY; } static void start(void) { DAC->CTRLA.bit.ENABLE = 1; sync(); } static void stop(void) { DAC->CTRLA.bit.ENABLE = 0; sync(); } static void data(uint16_t value) { DAC->DATA.reg = value; sync(); } static void databuf(uint16_t value) { DAC->DATABUF.reg = value; sync(); } static void voltage(uint32_t value, uint32_t vref = 3300000) { if (DAC->CTRLB.bit.REFSEL == DACONVERTER_REFSEL_INT1V) vref = 1000000; value = (value > vref ? vref : value) * DACONVERTER_DATA_MAX / vref; data(DAC->CTRLB.bit.LEFTADJ ? value << (16 - DACONVERTER_DATA_BIT) : value); } }; #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 |
/* daconverter.cpp - DAC 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 "daconverter.h" static DACONVERTER_CALLBACK _callback; void DAC_Handler(void) { if (DAC->INTFLAG.bit.UNDERRUN) { DAC->INTFLAG.bit.UNDERRUN = 1; if (DAC->INTENSET.bit.UNDERRUN && _callback) _callback(DACONVERTER_INT_UNDERRUN); } if (DAC->INTFLAG.bit.EMPTY) { DAC->INTFLAG.bit.EMPTY = 1; if (DAC->INTENSET.bit.EMPTY && _callback) _callback(DACONVERTER_INT_EMPTY); } } void DAConverter::setCallback(DACONVERTER_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)