ADCは簡単だと思っていたがかなり苦戦してしまった。SDKに問題があるらしく機能の組み合わせ方で誤動作してしまうのだ。
最初に試したのは、ADC with DMA というDMA転送機能であるがマルチソース機能が便利かなと思って試して見たら見事にドツボにハマる。とにかく不安定というかソースを複数組み合わると変換結果がおかしくなる。暫く悪戦苦闘してみたがラチがあかないのでこの機能は問題外として綺麗さっぱり忘れることに。DMA転送を使ったシンプルな連続サンプリングも便利かなとは思ったけど肝心のADCが遅いしなと考えたら必要ないかもと思ってしまった。
さらに平均化処理のためのマルチサンプリング機能や連続変換モードも変換結果がおかしかったり永久に変換完了にならなかったりする。マルチソースやマルチサンプリング、連続変換機能はソフトウェア実装していてそのロジックがバグバグしてるんだろうと思う。
ということで普通にシンプルなシングルショット変換しか使えないということがわかったがマルチソースやマルチサンプリング機能はないよりもあったほうが良いと思うので独自実装してみた。特にマルチソースに関してはソース毎に全ての変換パラメタを変更できるようにしたので凄く便利に使えるはず。一つだけ気になるのは内部温度センサーだ。安定はしているがデータシートがウソっぽく見えるほどの誤差というか個体差によるバラツキが激しいようなので内部温度センサーは使えないと思った方がいいだろう。
ちなみに、変換完了にならなかった経験から変換完了を判断するためのbAHI_AdcPoll()は怪しすぎて使う気になれなかったのでデータシートから変換に必要なクロック数を計算しその時間経過したら変換完了としている。変換完了の判定処理まで自作することになるなんて初めての経験だ。(-_-;)
変換時間は最速で9.5usなので割り込みを使って連続変換すると他のコードが動かなくなる可能性があることから割り込みは使っていない。
あと、マルチソース機能はバックグラウンド処理(yield)を利用して連続変換していることに注意しよう。
【マルチソースのサンプル】
| 
					 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  | 
						#include "device.h" AdcConfig adc[6]; void setup(void) {   Debug::begin();   adc[0].source(AdcConfig::SRC_ADC_1);   adc[1].source(AdcConfig::SRC_ADC_2);   adc[2].source(AdcConfig::SRC_ADC_3);   adc[3].source(AdcConfig::SRC_ADC_4);   adc[4].source(AdcConfig::SRC_ADC_TEMP);   adc[5].source(AdcConfig::SRC_ADC_VOLT);   MAdc::begin(adc, sizeof(adc) / sizeof(adc[0])); } void loop(void) {   static uint32 t = 0;   if (micros() - t >= 1000000)   {     t += 1000000;     uint16 adc1 = MAdc::voltage(AdcConfig::SRC_ADC_1);     uint16 adc2 = MAdc::voltage(AdcConfig::SRC_ADC_2);     uint16 adc3 = MAdc::voltage(AdcConfig::SRC_ADC_3);     uint16 adc4 = MAdc::voltage(AdcConfig::SRC_ADC_4);     int16  temp = MAdc::temperature();     uint16 volt = MAdc::voltage(AdcConfig::SRC_ADC_VOLT);     Debug::printf("ADC(%u, %u, %u, %u), TEMP(%d), VOLT(%u)\n", adc1, adc2, adc3, adc4, temp, volt);   } }  | 
					
【ライブラリ】
| 
					 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381  | 
						/*   adc.h - ADC Library for NXP-JN516x   Copyright (c) 2022 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., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #pragma once #include <jendefs.h> #include <AppHardwareApi.h> #include "ticktimer.h" #include "analog.h" #include "gpio.h" #define ADC_BIT_RESOLUTION 10 // bits #define ADC_MAX_VALUE      ((1 << ADC_BIT_RESOLUTION) - 1) #ifndef ADC_TEMP_CORRECTION_VALUE #define ADC_TEMP_CORRECTION_VALUE 14 #endif /*    I/O Ports      ADC1: ADC1      ADC2: ADC2_VREF      ADC3: DIO0      ADC4: DIO1    The following SDK API seems to have a bug.      vAHI_AdcStartAccumulateSamples()      bAHI_AdcPoll()      bAHI_AdcEnableSampleBuffer() */ class AdcConfig {   public:     typedef enum     {       SRC_ADC_1    = E_AHI_ADC_SRC_ADC_1, // ADC1 input       SRC_ADC_2    = E_AHI_ADC_SRC_ADC_2, // ADC2 input       SRC_ADC_3    = E_AHI_ADC_SRC_ADC_3, // ADC3 input       SRC_ADC_4    = E_AHI_ADC_SRC_ADC_4, // ADC4 input #ifdef E_AHI_ADC_SRC_ADC_5       SRC_ADC_5    = E_AHI_ADC_SRC_ADC_5, // ADC5 input on JN5169 #endif #ifdef E_AHI_ADC_SRC_ADC_6       SRC_ADC_6    = E_AHI_ADC_SRC_ADC_6, // ADC6 input on JN5169 #endif       SRC_ADC_TEMP = E_AHI_ADC_SRC_TEMP,  // on-chip temperature sensor       SRC_ADC_VOLT = E_AHI_ADC_SRC_VOLT,  // internal voltage monitor     } SRC;     typedef enum     {       RANGE_1 = E_AHI_AP_INPUT_RANGE_1, // 0 to Vref       RANGE_2 = E_AHI_AP_INPUT_RANGE_2, // 0 to 2Vref     } RANGE;     typedef enum     {       SAMPLES_2  = E_AHI_ADC_ACC_SAMPLE_2,  //  2 samples       SAMPLES_4  = E_AHI_ADC_ACC_SAMPLE_4,  //  4 samples       SAMPLES_8  = E_AHI_ADC_ACC_SAMPLE_8,  //  8 samples       SAMPLES_16 = E_AHI_ADC_ACC_SAMPLE_16, // 16 samples       SAMPLES_1,     } SAMPLES;     /* must be first call */     AdcConfig& source(SRC source)     {       _source  = source;       _range   = RANGE_1;       _samples = SAMPLES_1;       _adcref  = ANALOG_BANDGAP;       _clock   = Analog::CLOCK_500KHZ;       _capture = Analog::CAPTURE_2;       return *this;     }     AdcConfig& range(RANGE value)     {       _range = value;       return *this;     }     AdcConfig& samples(SAMPLES value)     {       _samples = value;       return *this;     }     AdcConfig& reference(uint16 value)     {       _adcref = value;       return *this;     }     AdcConfig& clock(Analog::CLOCK value)     {       _clock = value;       return *this;     }     AdcConfig& capture(Analog::CAPTURE value)     {       _capture = value;       return *this;     }   private:     void configure(void)     {       Analog::clock(_clock);       Analog::capture(_capture);       Analog::reference(_adcref != ANALOG_BANDGAP);       Analog::interrupt(false);       Analog::configure();     }     uint16 periods(void)     {       // return 16MHz clock periods.       return (((_capture + 1) << 1) * 3 + 13) << (_clock + 3);     }     friend class Adc;     friend class MAdc;     SRC             _source;     RANGE           _range;     SAMPLES         _samples;     uint16          _adcref;     Analog::CLOCK   _clock;     Analog::CAPTURE _capture; }; class Adc {   public:     typedef enum     {       MODE_SINGLE_SHOT = E_AHI_ADC_SINGLE_SHOT, // single-shot mode       MODE_CONTINUOUS  = E_AHI_ADC_CONTINUOUS,  // continous mode     } MODE;     static     void begin(MODE mode, AdcConfig& conf)     {       AdcConfig::RANGE range = conf._range;       _adcref = conf._adcref;       if (conf._adcref == ANALOG_BANDGAP)       {         if (conf._source == AdcConfig::SRC_ADC_TEMP)           range = AdcConfig::RANGE_1;         else if (conf._source == AdcConfig::SRC_ADC_VOLT)         {           range = AdcConfig::RANGE_2;           _adcref = ANALOG_BANDGAP * 2 * 1000 / 666;         }       }       else if (conf._range)         _adcref <<= 1;       switch ((uint8)conf._source)       {         case AdcConfig::SRC_ADC_3: Gpio::pinMode(Gpio::DIO0, Gpio::INPUT); break;         case AdcConfig::SRC_ADC_4: Gpio::pinMode(Gpio::DIO1, Gpio::INPUT); break;       }       _samples = conf._samples;       _periods = conf.periods();       _mode    = mode;       _status  = 0;       _adcsum  = 0;       _count   = 0;       _result  = 0;       conf.configure();       vAHI_AdcEnable(E_AHI_ADC_SINGLE_SHOT, range, conf._source);     }     static     void end(void)     {       if (_status)       {         _status = 0;         vAHI_AdcDisable();       }     }     static     void start(void)     {       if (!(_status & ~COMVERSION_END))       {         _status = 1 << _mode;         _adcsum = 0;         _count  = 0;         vAHI_AdcStartSample();         _start  = TickTimer::read();       }     }     static     bool conversion(void)     {       if (_status & COMVERSION_END)         return true;       if (!_status || (TickTimer::read() - _start < _periods))         return false;       uint16 value = u16AHI_AdcRead();       if (_samples >= AdcConfig::SAMPLES_1)         _result = value;       else       {         _adcsum += value;         if (++_count < (1 << (_samples + 1)))         {           vAHI_AdcStartSample();           _start = TickTimer::read();           return false;         }         _result = (_adcsum + (1 << _samples)) >> (_samples + 1);         _adcsum = 0;         _count  = 0;       }       if (_mode == MODE_CONTINUOUS)       {         vAHI_AdcStartSample();         _start  = TickTimer::read();         _status = COMVERSION_END | (1 << MODE_CONTINUOUS);       }       else         _status = COMVERSION_END;       return true;     }     static     uint16 read(void)     {       _status &= ~COMVERSION_END;       return _result;     }     static     uint16 voltage(void)     {       return read() * _adcref / ADC_MAX_VALUE;     }     static     int16 temperature(void)     {       return temperature(voltage());     }     static     int16 temperature(int16 volt)     {       if (volt <= 610)         return -40;       if (volt >= 840)         return 125;       volt -= 730;       return ADC_TEMP_CORRECTION_VALUE + 25 + (volt < 0 ? volt * 65 / 120 : volt * 100 / 110);     }     static     uint16 reference(void)     {       return _adcref;     }   private:     static const uint8 COMVERSION_END = 0x80;     static AdcConfig::SAMPLES _samples;     static MODE   _mode;     static uint32 _start;     static uint16 _periods;     static uint16 _adcref;     static uint16 _adcsum;     static uint16 _result;     static uint8  _count;     static uint8  _status; }; class MAdc {   public:     static     void begin(const AdcConfig *conf, uint8 size)     {       _index = 0;       _count = 0;       while (size-- && (_count < ADC_SRC_MAX))         _config[_count++] = *conf++;       if (_count)         next(false);     }     static     void end(void)     {       _count = 0;       Adc::end();     }     static     uint16 read(AdcConfig::SRC source)     {       return _result[source];     }     static     uint16 voltage(AdcConfig::SRC source)     {       return read(source) * _adcref[source] / ADC_MAX_VALUE;     }     static     int16 temperature(void)     {       return Adc::temperature(voltage(AdcConfig::SRC_ADC_TEMP));     }     static     void handle(void)     {       if (_count)       {         if (Adc::conversion())           next(true);       }     }   private:     static     void next(bool conversion)     {       if (conversion)       {         AdcConfig::SRC source = _config[_index]._source;         _result[source] = Adc::read();         _adcref[source] = Adc::reference();         if (++_index >= _count)           _index = 0;       }       Adc::begin(Adc::MODE_SINGLE_SHOT, _config[_index]);       Adc::start();     }     static const uint8 ADC_SRC_MAX = AdcConfig::SRC_ADC_VOLT + 1;     static uint8     _index;     static uint8     _count;     static AdcConfig _config[ADC_SRC_MAX];     static uint16    _result[ADC_SRC_MAX];     static uint16    _adcref[ADC_SRC_MAX]; };  | 
					
| 
					 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  | 
						/*   adc.cpp - ADC Library for NXP-JN516x   Copyright (c) 2022 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., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #include "adc.h" AdcConfig::SAMPLES Adc::_samples; Adc::MODE Adc::_mode; uint32    Adc::_start; uint16    Adc::_periods; uint16    Adc::_adcref; uint16    Adc::_adcsum; uint16    Adc::_result; uint8     Adc::_count; uint8     Adc::_status; uint8     MAdc::_count; uint8     MAdc::_index; AdcConfig MAdc::_config[]; uint16    MAdc::_result[]; uint16    MAdc::_adcref[];  | 
					
| 
					 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  | 
						/*   analog.h - Analog Configure Library for NXP-JN516x   Copyright (c) 2022 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., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #pragma once #include <jendefs.h> #include <AppHardwareApi.h> /*    I/O Ports      External VREF: ADC2-VREF */ #define ANALOG_BANDGAP  1235 // mV class Analog {   public:     typedef enum     {       CAPTURE_2 = E_AHI_AP_SAMPLE_2, // 2 clock periods       CAPTURE_4 = E_AHI_AP_SAMPLE_4, // 4 clock periods       CAPTURE_6 = E_AHI_AP_SAMPLE_6, // 6 clock periods       CAPTURE_8 = E_AHI_AP_SAMPLE_8, // 8 clock periods     } CAPTURE;     typedef enum     {       CLOCK_2MHZ   = E_AHI_AP_CLOCKDIV_2MHZ,   // divisor of 8       CLOCK_1MHZ   = E_AHI_AP_CLOCKDIV_1MHZ,   // divisor of 16       CLOCK_500KHZ = E_AHI_AP_CLOCKDIV_500KHZ, // divisor of 32 (500kHz is recommended for ADC)       CLOCK_250KHZ = E_AHI_AP_CLOCKDIV_250KHZ, // divisor of 64     } CLOCK;     static     void begin(void)     {       _interrupt = false;       _clock     = CLOCK_500KHZ;       _capture   = CAPTURE_2;       _reference = false;     }     static     void configure(void)     {       vAHI_ApConfigure(true, _interrupt, _capture, _clock, _reference);       while (!bAHI_APRegulatorEnabled())         continue;     }     static     void stop(void)     {       vAHI_ApConfigure(false, 0, 0, 0, 0);     }     static     void decoupling(bool enable)     {       // Decoupling VREF Pin.       vAHI_ProtocolPower(true);       vAHI_ApSetBandGap(enable);     }     static     void interrupt(bool enable)     {       _interrupt = enable;     }     static     void clock(CLOCK clock)     {       _clock = clock;     }     static     void capture(CAPTURE capture)     {       _capture = capture;     }     static     void reference(bool external)     {       _reference = external;     }   private:     static bool    _interrupt;     static CLOCK   _clock;     static CAPTURE _capture;     static bool    _reference; };  | 
					
| 
					 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  | 
						/*   analog.cpp - Analog Configure Library for NXP-JN516x   Copyright (c) 2022 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., 59 Temple Place, Suite 330,   Boston, MA  02111-1307  USA */ #include "analog.h" bool            Analog::_interrupt; Analog::CLOCK   Analog::_clock; Analog::CAPTURE Analog::_capture; bool            Analog::_reference;  | 
					
次回は、Comparatorの予定。
【関連投稿】
NXP JN516X (TWELITE) をプログラミングする(開発環境の構築)
NXP JN516X (TWELITE) をプログラミングする(メイン・ルーチン)
NXP JN516X (TWELITE) をプログラミングする(TICKTIMER)
NXP JN516X (TWELITE) をプログラミングする(UART)
NXP JN516X (TWELITE) をプログラミングする(SYSTEM)
NXP JN516X (TWELITE) をプログラミングする(GPIO)
NXP JN516X (TWELITE) をプログラミングする(TIMER)
NXP JN516X (TWELITE) をプログラミングする(ALARM)
NXP JN516X (TWELITE) をプログラミングする(WAKETIMER)
NXP JN516X (TWELITE) をプログラミングする(WATCHDOG)
NXP JN516X (TWELITE) をプログラミングする(I2C)
NXP JN516X (TWELITE) をプログラミングする(SPI)
NXP JN516X (TWELITE) をプログラミングする(ADC)
NXP JN516X (TWELITE) をプログラミングする(COMPARATOR)
NXP JN516X (TWELITE) をプログラミングする(CLOCK)
NXP JN516X (TWELITE) をプログラミングする(BROWNOUT)
NXP JN516X (TWELITE) をプログラミングする(PULSCOUNTER)
NXP JN516X (TWELITE) をプログラミングする(INFRARED)
NXP JN516X (TWELITE) をプログラミングする(RANDOM-GENERATOR)
NXP JN516X (TWELITE) をプログラミングする(FLASH)
NXP JN516X (TWELITE) をプログラミングする(EEPROM)
NXP JN516X (TWELITE) をプログラミングする(WPAN)
NXP JN516X (TWELITE) をプログラミングする(Eclipse-CDT+MWSTAGE)
NXP JN516X (TWELITE) をプログラミングする(乗算と除算)
NXP JN516X (TWELITE) をプログラミングする(マルチタスク)
NXP JN516X (TWELITE) をプログラミングする(フラッシュ・プログラマー)
NXP JN516X (TWELITE) をプログラミングする(OTA UPDATE)
NXP JN516X (TWELITE) をプログラミングする(TWELITE CUE/MC3630)
NXP JN516X (TWELITE) をプログラミングする(LED)
NXP JN516X (TWELITE) をプログラミングする(AES)
NXP JN516X (TWELITE) をプログラミングする(Downloads)
