Timerには5つの機能があり機能毎に利用するメソッドが微妙に異なっている。機能毎にクラス分けすべきかもと思ったりもしたがなんとなくSDKをそのままラップしただけのライブラリに落ち着いてしまった。
但し、vAHI_TimerFineGrainDIOControl()については各タイマーを一括設定する仕様となっていて非常に使いずらいため個別設定ができるような仕様に変更している。
【機能毎の主メソッド】
[Timer]
begin()
startSingleShot()
startRepeat()
[PWM]
begin()
startSingleShot()
startRepeat()
[Counter]
clockSelect()
begin()
startSingleShot()
startRepeat()
readCount()
[Capture]
begin()
startSingleShot()
startRepeat()
readCapture()
peekCapture()
[Delta-Sigma]
startDeltaSigma()
【ライブラリ】
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 |
/* timer.h - Timer Library for XNP-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 Input Capture Output ------------------------------------------- TIMER_0: DIO8/DIO2 DIO9/DIO3 DIO10/DIO4 TIMER_1: - - DIO11/DIO5 TIMER_2: - - DIO12/DIO6/DO0 TIMER_3: - - DIO13/DIO7/DO1 TIMER_4: - - DIO17/DIO8 */ class Timer { public: typedef enum { TIMER_0 = E_AHI_TIMER_0, // Timer 0 TIMER_1 = E_AHI_TIMER_1, // Timer 1 TIMER_2 = E_AHI_TIMER_2, // Timer 2 (Infra-Red) TIMER_3 = E_AHI_TIMER_3, // Timer 3 TIMER_4 = E_AHI_TIMER_4, // Timer 4 } TIMER; typedef enum { CLOCK_16MHZ = 0, CLOCK_8MHZ = 1, CLOCK_4MHZ = 2, CLOCK_2MHZ = 3, CLOCK_1MHZ = 4, CLOCK_500KHZ = 5, CLOCK_250KHZ = 6, CLOCK_125KHZ = 7, CLOCK_62500HZ = 8, CLOCK_31250HZ = 9, CLOCK_15625HZ = 10, } CLOCK; static void begin(TIMER timer, CLOCK clock = CLOCK_16MHZ, bool rise = false, bool period = false, bool output = false, PR_HWINT_APPCALLBACK prTimerCallback = 0) { if (prTimerCallback && (rise || period)) { switch (timer) { case TIMER_0: vAHI_Timer0RegisterCallback(prTimerCallback); break; case TIMER_1: vAHI_Timer1RegisterCallback(prTimerCallback); break; case TIMER_2: vAHI_Timer2RegisterCallback(prTimerCallback); break; case TIMER_3: vAHI_Timer3RegisterCallback(prTimerCallback); break; case TIMER_4: vAHI_Timer4RegisterCallback(prTimerCallback); break; } vAHI_TimerEnable(timer, clock, rise, period, output); } else { vAHI_TimerEnable(timer, clock, false, false, output); } if (timer == TIMER_0) vAHI_TimerClockSelect(timer, external, invert); } static void end(TIMER timer) { vAHI_TimerDisable(timer); } static void clockSelect(TIMER timer, bool external, bool invert) { if (timer == TIMER_0) vAHI_TimerClockSelect(timer, external, invert); } static void outputs(TIMER timer, bool invert, bool input = true) { vAHI_TimerConfigureOutputs(timer, invert, input); } static void inputs(TIMER timer, bool invert, bool falling) { if (timer == TIMER_0) vAHI_TimerConfigureInputs(timer, invert, falling); } static void location(TIMER timer, bool location, bool override) { vAHI_TimerSetLocation(timer, location, override); } static void startSingleShot(TIMER timer, uint16 hi, uint16 lo) { vAHI_TimerStartSingleShot(timer, hi, lo); } static void startRepeat(TIMER timer, uint16 hi, uint16 lo) { vAHI_TimerStartRepeat(timer, hi, lo); } static void startCapture(TIMER timer) { if (timer == TIMER_0) vAHI_TimerStartCapture(timer); } static void startDeltaSigma(TIMER timer, uint16 hi, bool rtz) { vAHI_TimerStartDeltaSigma(timer, hi, 0, rtz); } static uint16 readCount(TIMER timer) { return u16AHI_TimerReadCount(timer); } static void readCapture(TIMER timer, uint16 *hi, uint16 *lo) { if (timer == TIMER_0) vAHI_TimerReadCapture(timer, hi, lo); } static void peekCapture(TIMER timer, uint16 *hi, uint16 *lo) { if (timer == TIMER_0) vAHI_TimerReadCaptureFreeRunning(timer, hi, lo); } static void stop(TIMER timer) { vAHI_TimerStop(timer); } typedef enum { DIOMASK_TIMER0_INPUT = 0x01, // Timer 0 external gate/event input DIOMASK_TIMER0_CAPTURE = 0x02, // Timer 0 capture input DIOMASK_TIMER0_OUTPUT = 0x04, // Timer 0 PWM output DIOMASK_TIMER1_OUTPUT = 0x08, // Timer 1 PWM output DIOMASK_TIMER2_OUTPUT = 0x10, // Timer 2 PWM output DIOMASK_TIMER3_OUTPUT = 0x20, // Timer 3 PWM output DIOMASK_TIMER4_OUTPUT = 0x40, // Timer 4 PWM output } DIOMASK; static void DIOControl(TIMER timer, bool enable) { uint8 mask = 0; switch (timer) { case TIMER_0: mask = DIOMASK_TIMER0_OUTPUT | DIOMASK_TIMER0_INPUT | DIOMASK_TIMER0_CAPTURE; break; case TIMER_1: mask = DIOMASK_TIMER1_OUTPUT; break; case TIMER_2: mask = DIOMASK_TIMER2_OUTPUT; break; case TIMER_3: mask = DIOMASK_TIMER3_OUTPUT; break; case TIMER_4: mask = DIOMASK_TIMER4_OUTPUT; break; } DIOControl(mask, enable); } static void DIOControl(/*DIOMASK*/uint8 mask, bool enable) { if (enable) _diomask &= ~mask; else _diomask |= mask; vAHI_TimerFineGrainDIOControl(_diomask); } typedef enum { FIREMASK_RISE = E_AHI_TIMER_RISE_MASK, FIREMASK_PERIOD = E_AHI_TIMER_PERIOD_MASK, } FIREMASK; static FIREMASK fired(TIMER timer) { // bit combination return (FIREMASK)u8AHI_TimerFired(timer); } static TIMER id(uint32 device) { return TIMERS[device]; } private: static const TIMER TIMERS[]; static uint8 _diomask; }; |
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 |
/* timer.cpp - Timer Library for XNP-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 "timer.h" const Timer::TIMER Timer::TIMERS[] = { Timer::TIMER_1, /* Timer 1 */ Timer::TIMER_2, /* Timer 2 */ (Timer::TIMER)-1, /* System controller */ (Timer::TIMER)-1, /* Baseband controller */ (Timer::TIMER)-1, /* Encryption engine */ (Timer::TIMER)-1, /* Phy controller */ (Timer::TIMER)-1, /* UART 0 */ (Timer::TIMER)-1, /* UART 1 */ Timer::TIMER_0, /* Timer 0 */ (Timer::TIMER)-1, /* (undefined) */ (Timer::TIMER)-1, /* Serial Interface (2 wire) */ (Timer::TIMER)-1, /* SPI master */ (Timer::TIMER)-1, /* SPI slave */ (Timer::TIMER)-1, /* Analogue peripherals */ Timer::TIMER_3, /* Timer 3 */ (Timer::TIMER)-1, /* Tick timer */ Timer::TIMER_4, /* Timer 4 */ (Timer::TIMER)-1, /* Flash and EEPROM Controller */ (Timer::TIMER)-1, /* (undefined) */ (Timer::TIMER)-1, /* Infrared */ }; uint8 Timer::_diomask; |
次回は、アラームライブラリの予定。
【関連投稿】
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)