久しぶりにATtiny202を使ってみたらプログラム領域が少ないため公開されているライブラリが使えずHW直接アクセスするコードを書かなければいけなかったりして何かと面倒なので手始めにGPIOライブラリを作ってみた。
関数引数に定数を指定したときが高速かつ最小なコードになるが、ビルドモードや関数引数が定数かどうかを判断し適切かつ最小なコードが生成されるようにしてみたことやピン変化によるHW割込み処理にも対応してみた。ちなみにHW割込み処理を使うと意外にコードが大きくなってしまうためHW割込み処理を使わずに同じことができるポーリング処理にも対応。単純な処理のみならポーリングでOKかも。
自作にあたって特に悩んだのはシフト演算を使うと巨大かつ遅いコードになりがちなこと。そのためシフト演算なしですむようGPIOピン定義にそのビット位置とビットマスク値を含めている。
【生成されるコードの例(疑似コード)】
1 2 3 4 5 |
Port::digitalWrite(Port::PA0, Port::LOW); ------------------------------------------ cbi VPORTA.OUT, 0 or sts PORTA.OUTCLR, 1 |
1 2 3 4 5 |
Port::digitalWrite(Port::PA0, Port::HIGH); ------------------------------------------ sbi VPORTA.OUT, 0 or sts PORTA.OUTSET, 1 |
1 2 3 4 5 |
Port::digitalWrite(Port::PA0, Port::TOGGLE); ------------------------------------------ sbi VPORTA.IN, 0 or sts PORTA.OUTTGL, 1 |
【使い方】
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 |
#include "avr8_port.h" void callback(uint8_t value, uint8_t flags) { /* value ... port value */ /* flags ... port interrupt status */ } int main(void) { /* INPUT MODE */ Port::pinMode(Port::PA0, Port::INPUT); Port::digitalRead(Port::PA0); /* INPUT_PULLUP MODE */ Port::pinMode(Port::PA0, Port::INPUT_PULLUP); Port::digitalRead(Port::PA0); /* OUTPUT MODE */ Port::pinMode(Port::PA0, Port::OUTPUT); Port::digitalWrite(Port::PA0, Port::LOW); Port::digitalWrite(Port::PA0, Port::HIGH); Port::digitalWrite(Port::PA0, Port::TOGGLE); /* OPEN DRAIN MODE */ Port::pinMode(Port::PA0, Port::OPEN_DRAIN); Port::digitalWriteOD(Port::PA0, Port::LOW); Port::digitalWriteOD(Port::PA0, Port::HIGH); Port::digitalWriteOD(Port::PA0, Port::TOGGLE); /* OPEN SOURCE MODE */ Port::pinMode(Port::PA0, Port::OPEN_SOURCE); Port::digitalWriteOS(Port::PA0, Port::LOW); Port::digitalWriteOS(Port::PA0, Port::HIGH); Port::digitalWriteOS(Port::PA0, Port::TOGGLE); /* DISABLE MODE (ADC/AC MODE) */ Port::pinMode(Port::PA0, Port::DISABLE); /* Interrupt Callback */ Port::interrupt(Port::PA0, Port::ISC_BOTHEDGES, callback); Port::interrupt(Port::PA0, Port::ISC_RISING , callback); Port::interrupt(Port::PA0, Port::ISC_FALLING , callback); Port::interrupt(Port::PA0, Port::ISC_LEVEL , callback); /* GPIO Interrupt Polling */ Port::poll(); return 0; } |
【ライブラリ】
※余計な内容が入ってるが気にしないように!
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 |
/* avr8_config.h - Driver Configuration for Microchip AVR8 Series Copyright (c) 2025 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 <avr/io.h> /* Default VDD */ #define CONFIG_VDD 5000 /* mV */ /* PORT */ #define CONFIG_PORT_ISR 0 /* HW割込み処理を使うときは1にする */ /* TCA */ #define CONFIG_TCA_ISR 0 /* TCB */ #define CONFIG_TCB_ISR 0 /* RTC */ #define CONFIG_RTC_ISR 0 /* ADC */ #define CONFIG_AC_ISR 1 /* USART */ #define CONFIG_USART_ISR 1 #define CONFIG_USART_RXBUF_SIZE 8 /* Set a power of 2 value */ #define CONFIG_USART_TXBUF_SIZE 0 /* Set a power of 2 value */ |
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
/* avr8_port.h - Port Driver for Microchip AVR8 Series Copyright (c) 2025 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(PORTG) #define PORT_MAX_PORTS 7 #elif defined(PORTF) #define PORT_MAX_PORTS 6 #elif defined(PORTE) #define PORT_MAX_PORTS 5 #elif defined(PORTD) #define PORT_MAX_PORTS 4 #elif defined(PORTC) #define PORT_MAX_PORTS 3 #elif defined(PORTB) #define PORT_MAX_PORTS 2 #elif defined(PORTA) #define PORT_MAX_PORTS 1 #else #define PORT_MAX_PORTS 0 #endif class Port { public: /* Pin ID (pindef_t) */ typedef enum { #if defined(PORTA) PA0 = 0x0100, PA1 = 0x0210, PA2 = 0x0420, PA3 = 0x0830, PA4 = 0x1040, PA5 = 0x2050, PA6 = 0x4060, PA7 = 0x8070, #endif #if defined(PORTB) PB0 = 0x0101, PB1 = 0x0211, PB2 = 0x0421, PB3 = 0x0831, PB4 = 0x1041, PB5 = 0x2051, PB6 = 0x4061, PB7 = 0x8071, #endif #if defined(PORTC) PC0 = 0x0102, PC1 = 0x0212, PC2 = 0x0422, PC3 = 0x0832, PC4 = 0x1042, PC5 = 0x2052, PC6 = 0x4062, PC7 = 0x8772, #endif #if defined(PORTD) PD0 = 0x0103, PD1 = 0x0213, PD2 = 0x0423, PD3 = 0x0833, PD4 = 0x1043, PD5 = 0x2053, PD6 = 0x4063, PD7 = 0x8073, #endif #if defined(PORTE) PE0 = 0x0104, PE1 = 0x0214, PE2 = 0x0424, PE3 = 0x0834, PE4 = 0x1044, PE5 = 0x2054, PE6 = 0x4064, PE7 = 0x8074, #endif #if defined(PORTF) PF0 = 0x0105, PF1 = 0x0215, PF2 = 0x0425, PF3 = 0x0835, PF4 = 0x1045, PF5 = 0x2055, PF6 = 0x4065, PF7 = 0x8075, #endif #if defined(PORTG) PG0 = 0x0106, PG1 = 0x0216, PG2 = 0x0426, PG3 = 0x0836, PG4 = 0x1046, PG5 = 0x2056, PG6 = 0x4066, PG7 = 0x8076, #endif } PIN; /* Pin Mode */ typedef enum { DISABLE, INPUT, INPUT_PULLUP, OUTPUT, OPEN_DRAIN, OPEN_SOURCE, } MODE; /* Pin Value */ typedef enum { LOW, HIGH, TOGGLE = 0xFF, IGNORE = TOGGLE, } VAL; /* Input/Sense Configuration select */ typedef enum { ISC_INTDISABLE = PORT_ISC_INTDISABLE_gc, /* Interrupt disabled but input buffer enabled */ ISC_BOTHEDGES = PORT_ISC_BOTHEDGES_gc, /* Sense Both Edges */ ISC_RISING = PORT_ISC_RISING_gc, /* Sense Rising Edge */ ISC_FALLING = PORT_ISC_FALLING_gc, /* Sense Falling Edge */ ISC_INPUT_DISABLE = PORT_ISC_INPUT_DISABLE_gc, /* Digital Input Buffer disabled */ ISC_LEVEL = PORT_ISC_LEVEL_gc, /* Sense low Level */ } ISC; /* Type of Callback Function */ typedef void (*callback_t)(uint8_t value, uint8_t flags); static void disable(void) { for (uint8_t i = 0; i < PORT_MAX_PORTS; ++i) { PORT_t *port = getPort(i); if (port) { port->PIN0CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN5CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; port->PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; port->DIR = 0; port->OUT = 0; port->INTFLAGS = 0xFF; } } } static void pinMode(PIN pin, MODE mode, VAL value = IGNORE, bool invert = false) { pindef_t spin = { .port = pin }; PORT_t *port = getPort(spin.x.port); if (port) { uint8_t ctrl = 0; switch (mode) { case DISABLE: ctrl = PORT_ISC_INPUT_DISABLE_gc; break; case OUTPUT: if (value != IGNORE) digitalWrite(pin, value); port->DIRSET = spin.x.bmsk; break; case OPEN_DRAIN: digitalWrite(pin, LOW); if (value != IGNORE) digitalWriteOD(pin, value); ctrl = PORT_PULLUPEN_bm; break; case OPEN_SOURCE: digitalWrite(pin, HIGH); if (value == IGNORE) digitalWriteOS(pin, value); break; case INPUT_PULLUP: ctrl = PORT_PULLUPEN_bm; /* fall through */ case INPUT: port->DIRCLR = spin.x.bmsk; break; } ((register8_t *)&port->PIN0CTRL)[spin.x.bpos] = ctrl | (invert ? PORT_INVEN_bm : 0); } } /* for pinMode(OUTPUT) */ static void digitalWrite(PIN pin, VAL value) { pindef_t spin = { .port = pin }; #if !defined(DEBUG) if (__builtin_constant_p(pin)) { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) VPORTA.IN |= spin.x.bmsk; else if (value == LOW) VPORTA.OUT &= ~spin.x.bmsk; else VPORTA.OUT |= spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) VPORTB.IN |= spin.x.bmsk; else if (value == LOW) VPORTB.OUT &= ~spin.x.bmsk; else VPORTB.OUT |= spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) VPORTC.IN |= spin.x.bmsk; else if (value == LOW) VPORTC.OUT &= ~spin.x.bmsk; else VPORTC.OUT |= spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) VPORTD.IN |= spin.x.bmsk; else if (value == LOW) VPORTD.OUT &= ~spin.x.bmsk; else VPORTD.OUT |= spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) VPORTE.IN |= spin.x.bmsk; else if (value == LOW) VPORTE.OUT &= ~spin.x.bmsk; else VPORTE.OUT |= spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) VPORTF.IN |= spin.x.bmsk; else if (value == LOW) VPORTF.OUT &= ~spin.x.bmsk; else VPORTF.OUT |= spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) VPORTG.IN |= spin.x.bmsk; else if (value == LOW) VPORTG.OUT &= ~spin.x.bmsk; else VPORTG.OUT |= spin.x.bmsk; break; #endif } } else #endif { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) PORTA.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTA.OUTCLR = spin.x.bmsk; else PORTA.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) PORTB.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTB.OUTCLR = spin.x.bmsk; else PORTB.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) PORTC.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTC.OUTCLR = spin.x.bmsk; else PORTC.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) PORTD.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTD.OUTCLR = spin.x.bmsk; else PORTD.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) PORTE.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTE.OUTCLR = spin.x.bmsk; else PORTE.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) PORTF.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTF.OUTCLR = spin.x.bmsk; else PORTF.OUTSET = spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) PORTG.OUTTGL = spin.x.bmsk; else if (value == LOW) PORTG.OUTCLR = spin.x.bmsk; else PORTG.OUTSET = spin.x.bmsk; break; #endif } } } /* for pinMode(OPEN_DRAIN) */ static void digitalWriteOD(PIN pin, VAL value) { pindef_t spin = { .port = pin }; #if !defined(DEBUG) if (__builtin_constant_p(pin)) { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) PORTA.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTA.DIR |= spin.x.bmsk; else VPORTA.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) PORTB.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTB.DIR |= spin.x.bmsk; else VPORTB.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) PORTC.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTC.DIR |= spin.x.bmsk; else VPORTC.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) PORTD.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTD.DIR |= spin.x.bmsk; else VPORTD.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) PORTE.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTE.DIR |= spin.x.bmsk; else VPORTE.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) PORTF.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTF.DIR |= spin.x.bmsk; else VPORTF.DIR &= ~spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) PORTG.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTG.DIR |= spin.x.bmsk; else VPORTG.DIR &= ~spin.x.bmsk; break; #endif } } else #endif { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) PORTA.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTA.DIRSET = spin.x.bmsk; else PORTA.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) PORTB.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTB.DIRSET = spin.x.bmsk; else PORTB.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) PORTC.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTC.DIRSET = spin.x.bmsk; else PORTC.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) PORTD.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTD.DIRSET = spin.x.bmsk; else PORTD.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) PORTE.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTE.DIRSET = spin.x.bmsk; else PORTE.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) PORTF.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTF.DIRSET = spin.x.bmsk; else PORTF.DIRCLR = spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) PORTG.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTG.DIRSET = spin.x.bmsk; else PORTG.DIRCLR = spin.x.bmsk; break; #endif } } } /* for pinMode(OPEN_SOURCE) */ static void digitalWriteOS(PIN pin, VAL value) { pindef_t spin = { .port = pin }; #if !defined(DEBUG) if (__builtin_constant_p(pin)) { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) PORTA.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTA.DIR &= ~spin.x.bmsk; else VPORTA.DIR |= spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) PORTB.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTB.DIR &= ~spin.x.bmsk; else VPORTB.DIR |= spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) PORTC.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTC.DIR &= ~spin.x.bmsk; else VPORTC.DIR |= spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) PORTD.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTD.DIR &= ~spin.x.bmsk; else VPORTD.DIR |= spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) PORTE.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTE.DIR &= ~spin.x.bmsk; else VPORTE.DIR |= spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) PORTF.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTF.DIR &= ~spin.x.bmsk; else VPORTF.DIR |= spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) PORTG.DIRTGL = spin.x.bmsk; else if (value == LOW) VPORTG.DIR &= ~spin.x.bmsk; else VPORTG.DIR |= spin.x.bmsk; break; #endif } } else #endif { switch (spin.x.port) { #if defined(PORTA) case 0: if (value == TOGGLE) PORTA.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTA.DIRCLR = spin.x.bmsk; else PORTA.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTB) case 1: if (value == TOGGLE) PORTB.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTB.DIRCLR = spin.x.bmsk; else PORTB.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTC) case 2: if (value == TOGGLE) PORTC.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTC.DIRCLR = spin.x.bmsk; else PORTC.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTD) case 3: if (value == TOGGLE) PORTD.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTD.DIRCLR = spin.x.bmsk; else PORTD.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTE) case 4: if (value == TOGGLE) PORTE.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTE.DIRCLR = spin.x.bmsk; else PORTE.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTF) case 5: if (value == TOGGLE) PORTF.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTF.DIRCLR = spin.x.bmsk; else PORTF.DIRSET = spin.x.bmsk; break; #endif #if defined(PORTG) case 6: if (value == TOGGLE) PORTG.DIRTGL = spin.x.bmsk; else if (value == LOW) PORTG.DIRCLR = spin.x.bmsk; else PORTG.DIRSET = spin.x.bmsk; break; #endif } } } static VAL digitalRead(PIN pin) { pindef_t spin = { .port = pin }; switch (spin.x.port) { #if defined(PORTA) case 0: return VPORTA.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTB) case 1: return VPORTB.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTC) case 2: return VPORTC.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTD) case 3: return VPORTD.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTE) case 4: return VPORTE.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTF) case 5: return VPORTF.IN & spin.x.bmsk ? HIGH : LOW; #endif #if defined(PORTG) case 6: return VPORTG.IN & spin.x.bmsk ? HIGH : LOW; #endif } return LOW; } static void interrupt(PIN pin, ISC mode, callback_t func = 0) { pindef_t spin = { .port = pin }; PORT_t *port = getPort(spin.x.port); if (port) { register8_t &ctrl = ((register8_t *)&port->PIN0CTRL)[spin.x.bpos]; if ((mode != ISC_INTDISABLE) && func) { _callback[spin.x.port] = func; ctrl = (ctrl & ~PORT_ISC_gm) | mode; } else { ctrl = (ctrl & ~PORT_ISC_gm) | ISC_INTDISABLE; _callback[spin.x.port] = 0; } } } static void poll(void) { #if !CONFIG_TCA_ISR #if defined(PORTA) isr_a(); #endif #if defined(PORTB) isr_b(); #endif #if defined(PORTC) isr_c(); #endif #if defined(PORTD) isr_d(); #endif #if defined(PORTE) isr_e(); #endif #if defined(PORTF) isr_f(); #endif #if defined(PORTG) isr_g(); #endif #endif } private: typedef union { struct { uint8_t port: 4; uint8_t bpos: 3; uint8_t ____: 1; uint8_t bmsk: 8; } x; uint16_t port; } pindef_t; static callback_t _callback[PORT_MAX_PORTS]; static PORT_t *getPort(uint8_t port) { if (port < PORT_MAX_PORTS) { #if (1 < PORT_MAX_POTS) && !defined(PORTB) if (port != 1) #endif return &PORTA + port; } return 0; } #if defined(PORTA) inline static void isr_a(void) { uint8_t flags = VPORTA.INTFLAGS; if (flags) { VPORTA.INTFLAGS = flags; if (_callback[0]) _callback[0](VPORTA.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_a(void); #endif #endif #if defined(PORTB) inline static void isr_b(void) { uint8_t flags = VPORTB.INTFLAGS; if (flags) { VPORTB.INTFLAGS = flags; if (_callback[1]) _callback[1](VPORTB.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_b(void); #endif #endif #if defined(PORTC) inline static void isr_c(void) { uint8_t flags = VPORTC.INTFLAGS; if (flags) { VPORTC.INTFLAGS = flags; if (_callback[2]) _callback[2](VPORTC.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_c(void); #endif #endif #if defined(PORTD) inline static void isr_d(void) { uint8_t flags = VPORTD.INTFLAGS; if (flags) { VPORTD.INTFLAGS = flags; if (_callback[3]) _callback[3](VPORTD.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_d(void); #endif #endif #if defined(PORTE) inline static void isr_e(void) { uint8_t flags = VPORTE.INTFLAGS; if (flags) { VPORTE.INTFLAGS = flags; if (_callback[4]) _callback[4](VPORTE.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_e(void); #endif #endif #if defined(PORTF) inline static void isr_f(void) { uint8_t flags = VPORTF.INTFLAGS; if (flags) { VPORTF.INTFLAGS = flags; if (_callback[5]) _callback[5](VPORTF.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_f(void); #endif #endif #if defined(PORTG) inline static void isr_g(void) { uint8_t flags = VPORTG.INTFLAGS; if (flags) { VPORTG.INTFLAGS = flags; if (_callback[6]) _callback[6](VPORTG.IN, flags); } } #if CONFIG_PORT_ISR friend void port_isr_g(void); #endif #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 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 |
/* avr8_port.cpp - Port Driver for Microchip AVR8 Series Copyright (c) 2025 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 <avr/interrupt.h> #include "avr8_port.h" Port::callback_t Port::_callback[]; #if CONFIG_PORT_ISR #if defined(PORTA) inline void port_isr_a(void) { Port::isr_a(); } ISR(PORTA_PORT_vect) { port_isr_a(); } #endif #if defined(PORTB) inline void port_isr_b(void) { Port::isr_b(); } ISR(PORTB_PORT_vect) { port_isr_b(); } #endif #if defined(PORTC) inline void port_isr_c(void) { Port::isr_c(); } ISR(PORTC_PORT_vect) { port_isr_c(); } #endif #if defined(PORTD) inline void port_isr_d(void) { Port::isr_d(); } ISR(PORTD_PORT_vect) { port_isr_d(); } #endif #if defined(PORTE) inline void port_isr_e(void) { Port::isr_e(); } ISR(PORTE_PORT_vect) { port_isr_e(); } #endif #if defined(PORTF) inline void port_isr_f(void) { Port::isr_f(); } ISR(PORTF_PORT_vect) { port_isr_f(); } #endif #if defined(PORTG) inline void port_isr_g(void) { Port::isr_g(); } ISR(PORTG_PORT_vect) { port_isr_g(); } #endif #endif |