Last Updated on 2026-03-07 by researcher
WEXはTCEからの出力にデッドタイムを挿入したモータードライブ回路などの信号を生成できる。ただ、CLK_PER同期処理なので分解能が最低50ns単位なのがちょっと惜しいかも...
【サンプル】
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include "avr8_tce.h" #include "avr8_wex.h" void sample(void) { Wex::begin(); Wex::deadtime(Wex::DTI0 | Wex::DTI1, 2, 1); Wex::output(Wex::WO0 | Wex::WO1); Tce::begin(100000, Tce::WGMODE_DSTOP); Tce::pwmduty(Tce::CMP0, Tce::PWM_MAX_COUNT >> 1); /* duty = 50% */ Tce::run(); } |
【ライブラリ】
|
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 |
/* avr8_wex.h - WEX Driver for Microchip AVR8 Series Copyright (c) 2026 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include "avr8_config.h" #if defined(WEX0) class Wex { public: /* Dead time insertion */ typedef enum { DTI0 = WEX_DTI0EN_bm, DTI1 = WEX_DTI1EN_bm, DTI2 = WEX_DTI2EN_bm, DTI3 = WEX_DTI3EN_bm } DTI; /* Dead time swap */ typedef enum { SWAP0 = WEX_SWAP0_bm, SWAP1 = WEX_SWAP1_bm, SWAP2 = WEX_SWAP2_bm, SWAP3 = WEX_SWAP3_bm } SWAP; /* WEX Output WOn */ typedef enum { WO_OFF = 0, WO0 = 0x01, WO1 = 0x02, WO2 = 0x04, WO3 = 0x08, WO4 = 0x10, WO5 = 0x20, WO6 = 0x40, WO7 = 0x80, } WO; /* Event Control */ typedef enum { EVENTA, EVENTB, EVENTC, } EVENT; /* Input Matrix select */ typedef enum { INMX_DIRECT = WEX_INMX_DIRECT_gc, /* Direct from TCE0 */ INMX_CWCMA = WEX_INMX_CWCMA_gc, /* Common Waveform Channel Mode A. Single WO */ INMX_CWCMB = WEX_INMX_CWCMB_gc /* Common Waveform Channel Mode B. WO from two PWM channels */ } INMX; /* Update Source select */ typedef enum { UPDSRC_TCPWM0 = WEX_UPDSRC_TCPWM0_gc, /* Timer/Counter for PWM 0 update condition */ UPDSRC_SW = WEX_UPDSRC_SW_gc /* Software update only. No hardware update condition */ } UPDSRC; /* Blanking Prescaler select */ typedef enum { BLANKPRESC_DIV1 = WEX_BLANKPRESC_DIV1_gc, /* No prescaling */ BLANKPRESC_DIV4 = WEX_BLANKPRESC_DIV4_gc, /* Divide CLK_PER by 4 */ BLANKPRESC_DIV16 = WEX_BLANKPRESC_DIV16_gc, /* Divide CLK_PER by 16 */ BLANKPRESC_DIV64 = WEX_BLANKPRESC_DIV64_gc /* Divide CLK_PER by 64 */ } BLANKPRESC; /* Blanking State select */ typedef enum { BLANKSTATE_OFF = WEX_BLANKSTATE_OFF_gc, /* Blanking off */ BLANKSTATE_ON = WEX_BLANKSTATE_ON_gc /* Blanking active */ } BLANKSTATE; /* Blanking Trigger select */ typedef enum { BLANKTRIG_NONE = WEX_BLANKTRIG_NONE_gc, /* No HW trigger (Software only) */ BLANKTRIG_TCE0UPD = WEX_BLANKTRIG_TCE0UPD_gc, /* TCE0 Update Condition */ BLANKTRIG_TCE0CMP0 = WEX_BLANKTRIG_TCE0CMP0_gc, /* TCE0 Compare 0 */ BLANKTRIG_TCE0CMP1 = WEX_BLANKTRIG_TCE0CMP1_gc, /* TCE0 Compare 1 */ BLANKTRIG_TCE0CMP2 = WEX_BLANKTRIG_TCE0CMP2_gc, /* TCE0 Compare 2 */ BLANKTRIG_TCE0CMP3 = WEX_BLANKTRIG_TCE0CMP3_gc /* TCE0 Compare 3 */ } BLANKTRIG; /* Fault Detection Action select */ typedef enum { FDACT_NONE = WEX_FDACT_NONE_gc, /* None. Fault Protection Disabled */ FDACT_LOW = WEX_FDACT_LOW_gc, /* Drive all pins low */ FDACT_CUSTOM = WEX_FDACT_CUSTOM_gc /* Drive all pins to setting defined by FAULTDRV and FAULTVAL */ } FDACT; /* Fault Detection Restart Mode select */ typedef enum { FDMODE_LATCHED = WEX_FDMODE_LATCHED_gc, /* Latched Mode. Output will remain in fault state until fault condition is no longer active and FDF is cleared by SW. */ FDMODE_CBC = WEX_FDMODE_CBC_gc /* Cycle-by-cycle mode. Waveform output will remain in fault state until fault condition is no longer active. */ } FDMODE; /* Fault Detection on Debug Break Detection select */ typedef enum { FDDBD_FAULT = WEX_FDDBD_FAULT_gc, /* OCD Break request is treated as a fault if fault protection is enabled */ FDDBD_IGNORE = WEX_FDDBD_IGNORE_gc /* OCD Breask request will not trigger a fault */ } FDDBD; /* Fault Event Filter Enable select */ typedef enum { FILTER_ZERO = WEX_FILTER_ZERO_gc, /* No digital filter */ FILTER_SAMPLE1 = WEX_FILTER_SAMPLE1_gc, /* One Sample */ FILTER_SAMPLE2 = WEX_FILTER_SAMPLE2_gc, /* Two Samples */ FILTER_SAMPLE3 = WEX_FILTER_SAMPLE3_gc, /* Three Samples */ FILTER_SAMPLE4 = WEX_FILTER_SAMPLE4_gc, /* Four Samples */ FILTER_SAMPLE5 = WEX_FILTER_SAMPLE5_gc, /* Five Samples */ FILTER_SAMPLE6 = WEX_FILTER_SAMPLE6_gc, /* Six Samples */ FILTER_SAMPLE7 = WEX_FILTER_SAMPLE7_gc /* Seven Samples */ } FILTER; /* WEX.INTCTRL bit masks and bit positions */ typedef enum { INTCTRL_FAULTDET = WEX_FAULTDET_bm /* Fault Detection Interrupt Enable bit mask. */ } INTCTRL; /* WEX.INTFLAGS bit masks and bit positions */ typedef enum { INTFLAGS_FAULTDET = WEX_FAULTDET_bm, /* Fault Detection Interrupt Enable bit mask. */ INTFLAGS_FDFEVA = WEX_FDFEVA_bm, /* Fault Detection Flag Event Input A bit mask. */ INTFLAGS_FDFEVB = WEX_FDFEVB_bm, /* Fault Detection Flag Event Input B bit mask. */ INTFLAGS_FDFEVC = WEX_FDFEVC_bm /* Fault Detection Flag Event Input C bit mask. */ } INTFLAGS; /* WEX.STATUS bit masks and bit positions */ typedef enum { STATUS_FDSTATE = WEX_FDSTATE_bm, /* Fault Detection State bit mask. */ STATUS_FDSEVA = WEX_FDSEVA_bm, /* Fault Detection State Event A bit mask. */ STATUS_FDSEVB = WEX_FDSEVB_bm, /* Fault Detection State Event B bit mask. */ STATUS_FDSEVC = WEX_FDSEVC_bm, /* Fault Detection State Event C bit mask. */ STATUS_BLANKSTATE = WEX_BLANKSTATE_bm /* Blanking State bit mask. */ } STATUS; /* Type of Callback Function */ typedef void (*callback_t)(INTFLAGS flags); Wex(void) { } /* virtual */ ~Wex(void) { } static void begin(bool pgm = false, INMX inmx = INMX_DIRECT, UPDSRC updsrc = UPDSRC_TCPWM0) { end(); WEX0.CTRLA = inmx | (pgm ? WEX_PGM_bm : 0); WEX0.CTRLB = updsrc; WEX0.CTRLC = 0; WEX0.EVCTRLA = 0; WEX0.EVCTRLB = 0; WEX0.EVCTRLC = 0; WEX0.BUFCTRL = 0; WEX0.BLANKCTRL = 0; WEX0.BLANKTIME = 0; WEX0.FAULTCTRL = 0; WEX0.FAULTDRV = 0; WEX0.FAULTOUT = 0; WEX0.INTCTRL = 0; WEX0.INTFLAGS = 0xFF; WEX0.DTLS = 0; WEX0.DTHS = 0; WEX0.DTBOTH = 0; WEX0.SWAP = 0; WEX0.PGMOVR = 0; WEX0.PGMOUT = 0; WEX0.OUTOVEN = 0; _callback = 0; } static void end(void) { WEX0.CTRLA = 0; } static void deadtime(/* DTI */ uint8_t dti, uint8_t ls, uint8_t hs) { WEX0.CTRLA = (WEX0.CTRLA & ~(WEX_DTI3EN_bm | WEX_DTI2EN_bm | WEX_DTI1EN_bm | WEX_DTI0EN_bm)) | dti; WEX0.DTLS = ls; WEX0.DTHS = hs; } static void swap(/* SWAP */ uint8_t swap) { WEX0.SWAP = swap; } static void output(/* WO */ uint8_t ovr) { WEX0.OUTOVEN = ovr; } static void pgmovr(/* WO */ uint8_t ovr) { WEX0.PGMOVR = ovr; } static void pgmout(/* WO */ uint8_t out) { WEX0.PGMOUT = out; } static /* WO */ uint8_t pgmout(void) { return WEX0.PGMOUT; } static void fault(FDACT act = FDACT_LOW, FDMODE mode = FDMODE_LATCHED, FDDBD dbd = FDDBD_FAULT) { WEX0.FAULTCTRL = dbd | mode | act; } static void faultovr(/* WO */ uint8_t drv, /* WO */ uint8_t out) { WEX0.FAULTDRV = drv; WEX0.FAULTOUT = out; } static void evctrl(EVENT event, bool enable = true, bool blank = true, FILTER filter = FILTER_SAMPLE3) { (&WEX0.EVCTRLA)[event] = filter | (blank ? WEX_BLANK_bm : 0) | (enable ? WEX_FAULTEI_bm : 0); } static void blank(BLANKPRESC presc = BLANKPRESC_DIV1, BLANKTRIG trig = BLANKTRIG_TCE0UPD, uint8_t time = 3) { WEX0.BLANKCTRL = presc | trig; WEX0.BLANKTIME = time; } static STATUS status(void) { return (STATUS)WEX0.STATUS; } static void update(void) { command(CMD_UPDATE); } static void fault(bool set) { command(set ? CMD_FAULTSET : CMD_FAULTCLR); } static void blank(bool set) { command(set ? CMD_BLANKSET : CMD_BLANKCLR); } static void callback(callback_t func) { _callback = func; } static void interrupt(INTCTRL ctrl = INTCTRL_FAULTDET, bool enable = true) { if (enable) { WEX0.INTFLAGS = ctrl; #if CONFIG_WEX_ISR WEX0.INTCTRL |= ctrl; #endif } else { #if CONFIG_WEX_ISR WEX0.INTCTRL &= ~ctrl; #endif WEX0.INTFLAGS = ctrl; } } static void poll(void) { #if !CONFIG_WEX_ISR isr(); #endif } private: /* Command select */ typedef enum { CMD_NONE = WEX_CMD_NONE_gc, /* No Command */ CMD_UPDATE = WEX_CMD_UPDATE_gc, /* Force update of Dead-time, SWAP and PGM buffer registers. */ CMD_FAULTSET = WEX_CMD_FAULTSET_gc, /* Set Fault Detection */ CMD_FAULTCLR = WEX_CMD_FAULTCLR_gc, /* Clear Fault Detection */ CMD_BLANKSET = WEX_CMD_BLANKSET_gc, /* Set SW Blanking */ CMD_BLANKCLR = WEX_CMD_BLANKCLR_gc /* Clear SW Blanking */ } CMD; static callback_t _callback; #if CONFIG_WEX_ISR friend void wex_isr(void); #endif static inline void isr(void) { uint8_t flags = WEX0.INTFLAGS = WEX0.INTFLAGS; if (flags && _callback) _callback((INTFLAGS)flags); } static void command(CMD cmd = CMD_NONE) { WEX0.CTRLC = cmd; } }; #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 |
/* avr8_wex.cpp - WEX Driver for Microchip AVR8 Series Copyright (c) 2026 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include "avr8_wex.h" #if defined(WEX0) Wex::callback_t Wex::_callback; #if CONFIG_WEX_ISR inline void wex_isr(void) { Wex::isr(); } ISR(WEX0_FAULTDET_vect) { wex_isr(); } #endif #endif |
【関連投稿】
Microchip AVR8 用のライブラリを自作する。(GPIO)
Microchip AVR8 用のライブラリを自作する。(FUSE)
Microchip AVR8 用のライブラリを自作する。(CLOCK)
Microchip AVR8 用のライブラリを自作する。(RESET)
Microchip AVR8 用のライブラリを自作する。(PORTMUX)
Microchip AVR8 用のライブラリを自作する。(USART)
Microchip AVR8 用のライブラリを自作する。(RTC)
Microchip AVR8 用のライブラリを自作する。(TCA)
Microchip AVR8 用のライブラリを自作する。(TCB)
Microchip AVR8 用のライブラリを自作する。(VREF)
Microchip AVR8 用のライブラリを自作する。(DAC)
Microchip AVR8 用のライブラリを自作する。(AC)
Microchip AVR8 用のライブラリを自作する。(ADC)
Microchip AVR8 用のライブラリを自作する。(ZCD)
Microchip AVR8 用のライブラリを自作する。(SPI)
Microchip AVR8 用のライブラリを自作する。(TWI)
Microchip AVR8 用のライブラリを自作する。(MAIN)
Microchip AVR8 用のライブラリを自作する。(CONFIG)
Microchip AVR8 用のライブラリを自作する。(ALARM)
Microchip AVR8 用のライブラリを自作する。(TASK)
Microchip AVR8 用のライブラリを自作する。(CCL)
Microchip AVR8 用のライブラリを自作する。(TCD)
Microchip AVR8 用のライブラリを自作する。(TCE)
Microchip AVR8 用のライブラリを自作する。(WEX)
Microchip AVR8 用のライブラリを自作する。(TCF)
Microchip AVR8 用のライブラリを自作する。(DOWNLOAD)
