AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Loading...
Searching...
No Matches
GpioPinMacros.h
Go to the documentation of this file.
1/*
2 GpioPinMacros.h - Macros for naming and manipulating Arduino pins.
3 For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4 This is part of the AVRTools library.
5 Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
35#ifndef GpioPinMacros_h
36#define GpioPinMacros_h
37
38
39#include <avr/io.h>
40
41
42
47enum
48{
50 kDigitalHigh = 1
51};
52
53
54
55
56
57/*
58
59 These macros are implementation details for the port naming macros and are not intended
60 for end-users. These are required to make the macros work due to the
61 reparsing and resubstitution rules of the C/C++ preprocessor.
62
63*/
64
65
66#define _GpioPin( ddr, port, pin, nbr ) \
67 ddr, port, pin, nbr, -1, 0, -1, 0
68
69#define _GpioPinAnalog( ddr, port, pin, nbr, adc ) \
70 ddr, port, pin, nbr, adc, 0, -1, 0
71
72#define _GpioPinPwm( ddr, port, pin, nbr, ocr, com, tccr ) \
73 ddr, port, pin, nbr, -1, ocr, com, tccr
74
75#define _isGpioPinModeOutput( ddr, port, pin, nbr, adc, ocr, com, tccr ) ( ddr & (1<<nbr) )
76
77#define _isGpioPinModeInput( ddr, port, pin, nbr, adc, ocr, com, tccr ) (!( ddr & (1<<nbr) ))
78
79#define _setGpioPinModeOutput( ddr, port, pin, nbr, adc, ocr, com, tccr ) ddr |= (1<<nbr)
80
81#define _setGpioPinModeInput( ddr, port, pin, nbr, adc, ocr, com, tccr ) ddr &= ~(1<<nbr), port &= ~(1<<nbr)
82
83#define _setGpioPinModeInputPullup( ddr, port, pin, nbr, adc, ocr, com, tccr ) ddr &= ~(1<<nbr), port |= (1<<nbr)
84
85#define _readGpioPinDigital( ddr, port, pin, nbr, adc, ocr, com, tccr ) ( pin & (1<<nbr) )
86
87#define _writeGpioPinDigital( ddr, port, pin, nbr, adc, ocr, com, tccr, value ) \
88 do { if (value) port |= (1<<nbr); else port &= ~(1<<nbr); } while ( 0 )
89
90#define _setGpioPinHigh( ddr, port, pin, nbr, adc, ocr, com, tccr ) port |= (1<<nbr)
91
92#define _setGpioPinLow( ddr, port, pin, nbr, adc, ocr, com, tccr ) port &= ~(1<<nbr)
93
94#define _getGpioDDR( ddr, port, pin, nbr, adc, ocr, com, tccr ) ddr
95
96#define _getGpioPORT( ddr, port, pin, nbr, adc, ocr, com, tccr ) port
97
98#define _getGpioPIN( ddr, port, pin, nbr, adc, ocr, com, tccr ) pin
99
100#define _getGpioMASK( ddr, port, pin, nbr, adc, ocr, com, tccr ) (1<<nbr)
101
102#define _getGpioADC( ddr, port, pin, nbr, adc, ocr, com, tccr ) adc
103
104#define _getGpioOCR( ddr, port, pin, nbr, adc, ocr, com, tccr ) ocr
105
106#define _getGpioCOM( ddr, port, pin, nbr, adc, ocr, com, tccr ) com
107
108#define _getGpioTCCR( ddr, port, pin, nbr, adc, ocr, com, tccr ) tccr
109
110
111
112
113
114/*
115
116 These macros are for end-users to name GPIO pins and manipulate GPIO pin name macros.
117
118*/
119
120
121
131#define GpioPin( portLtr, pinNbr ) \
132 _GpioPin( DDR##portLtr, PORT##portLtr, PIN##portLtr, pinNbr )
133
134
135
146#define GpioPinAnalog( portLtr, pinNbr, adcNbr ) \
147 _GpioPinAnalog( DDR##portLtr, PORT##portLtr, PIN##portLtr, pinNbr, adcNbr )
148
149
161#define GpioPinPwm( portLtr, pinNbr, timer, chan ) \
162 _GpioPinPwm( DDR##portLtr, PORT##portLtr, PIN##portLtr, pinNbr, OCR##timer##chan, COM##timer##chan##1, TCCR##timer##A )
163
164
165
174#define isGpioPinModeOutput( pinName ) _isGpioPinModeOutput( pinName )
175
176
177
186#define isGpioPinModeInput( pinName ) _isGpioPinModeInput( pinName )
187
188
189
198#define setGpioPinModeOutput( pinName ) _setGpioPinModeOutput( pinName )
199
200
201
210#define setGpioPinModeInput( pinName ) _setGpioPinModeInput( pinName )
211
212
213
222#define setGpioPinModeInputPullup( pinName ) _setGpioPinModeInputPullup( pinName )
223
224
225
236#define readGpioPinDigital( pinName ) _readGpioPinDigital( pinName )
237
238
248#define writeGpioPinDigital( pinName, val ) _writeGpioPinDigital( pinName, val )
249
250
251
260#define setGpioPinHigh( pinName ) _setGpioPinHigh( pinName )
261
262
263
272#define setGpioPinLow( pinName ) _setGpioPinLow( pinName )
273
274
275
286#define getGpioDDR( pinName ) _getGpioDDR( pinName )
287
288
289
300#define getGpioPORT( pinName ) _getGpioPORT( pinName )
301
302
303
314#define getGpioPIN( pinName ) _getGpioPIN( pinName )
315
316
317
328#define getGpioMASK( pinName ) _getGpioMASK( pinName )
329
330
331
342#define getGpioADC( pinName ) _getGpioADC( pinName )
343
344
345
356#define getGpioOCR( pinName ) _getGpioOCR( pinName )
357
358
359
370#define getGpioCOM( pinName ) _getGpioCOM( pinName )
371
372
373
384#define getGpioTCCR( pinName ) _getGpioTCCR( pinName )
385
386
387
388
389/******************************************/
390
391/*
392 * Support for GPIO pin variables
393 *
394 */
395
396
397typedef volatile uint8_t* Gpio8Ptr;
398typedef volatile uint16_t* Gpio16Ptr;
399
425{
426public:
427
429 : mDdr( 0 ), mPort( 0 ), mPin( 0 ), mOcr( 0 ), mTccr( 0 ), mCom( 0xFF ),
430 mNbr( 0xFF ), mAdc( 0xFF )
431 {}
432
433 GpioPinVariable( Gpio8Ptr ddr, Gpio8Ptr port, Gpio8Ptr pin, int8_t nbr )
434 : mDdr( ddr ), mPort( port ), mPin( pin ), mOcr( 0 ), mTccr( 0 ), mCom( 0xFF ),
435 mNbr( static_cast<uint8_t>(nbr) ), mAdc( 0xFF )
436 {}
437
438 GpioPinVariable( Gpio8Ptr ddr, Gpio8Ptr port, Gpio8Ptr pin, int8_t nbr, int8_t adc )
439 : mDdr( ddr ), mPort( port ), mPin( pin ), mOcr( 0 ), mTccr( 0 ), mCom( 0xFF ),
440 mNbr( static_cast<uint8_t>(nbr) ), mAdc( static_cast<uint8_t>(adc) )
441 {}
442
443 GpioPinVariable( Gpio8Ptr ddr, Gpio8Ptr port, Gpio8Ptr pin, int8_t nbr, Gpio16Ptr ocr, Gpio8Ptr tccr, int8_t com )
444 : mDdr( ddr ), mPort( port ), mPin( pin ), mOcr( ocr ), mTccr( tccr ), mCom( com ),
445 mNbr( static_cast<uint8_t>(nbr) ), mAdc( 0xFF )
446 {}
447
449 Gpio8Ptr ddr() const
450 { return mDdr; }
451
453 Gpio8Ptr port() const
454 { return mPort; }
455
457 Gpio8Ptr pin() const
458 { return mPin; }
459
461 Gpio16Ptr ocr() const
462 { return mOcr; }
463
465 Gpio8Ptr tccr() const
466 { return mTccr; }
467
469 uint8_t bitNbr() const
470 { return mNbr; }
471
473 uint8_t com() const
474 { return mCom; }
475
477 uint8_t adcNbr() const
478 { return mAdc; }
479
480
481private:
482
483 Gpio8Ptr mDdr;
484 Gpio8Ptr mPort;
485 Gpio8Ptr mPin;
486 Gpio16Ptr mOcr;
487 Gpio8Ptr mTccr;
488 uint8_t mCom;
489 uint8_t mNbr;
490 uint8_t mAdc;
491};
492
493
494
495
496
497#define _makeGpioVarFromGpioPin( ddr, port, pin, nbr, adc, ocr, com, tccr ) \
498 GpioPinVariable( &(ddr), &(port), &(pin), nbr )
499
500#define _makeGpioVarFromGpioPinAnalog( ddr, port, pin, nbr, adc, ocr, com, tccr ) \
501 GpioPinVariable( &(ddr), &(port), &(pin), nbr, adc )
502
503#define _makeGpioVarFromGpioPinPwm( ddr, port, pin, nbr, adc, ocr, com, tccr ) \
504 GpioPinVariable( &(ddr), &(port), &(pin), nbr, &(ocr), &(tccr), com )
505
506
507
518#define makeGpioVarFromGpioPin( pinName ) _makeGpioVarFromGpioPin( pinName )
519
520
531#define makeGpioVarFromGpioPinAnalog( pinName ) _makeGpioVarFromGpioPinAnalog( pinName )
532
533
544#define makeGpioVarFromGpioPinPwm( pinName ) _makeGpioVarFromGpioPinPwm( pinName )
545
546
547
548
549
556inline bool isGpioPinModeOutputV( const GpioPinVariable& pinVar )
557{
558 return *(pinVar.ddr()) & ( 1 << pinVar.bitNbr() );
559}
560
561
568inline bool isGpioPinModeInputV( const GpioPinVariable& pinVar )
569{
570 return !( *(pinVar.ddr()) & ( 1 << pinVar.bitNbr() ) );
571}
572
573
574
575
576
585inline void setGpioPinModeOutputV( const GpioPinVariable& pinVar )
586{
587 *(pinVar.ddr()) |= (1 << pinVar.bitNbr() );
588}
589
590
591
592
601inline void setGpioPinModeInputV( const GpioPinVariable& pinVar )
602{
603 *(pinVar.ddr()) &= ~( 1 << pinVar.bitNbr() );
604 *(pinVar.port()) &= ~( 1 << pinVar.bitNbr() );
605}
606
607
608
617inline void setGpioPinModeInputPullupV( const GpioPinVariable& pinVar )
618{
619 *(pinVar.ddr()) &= ~( 1 << pinVar.bitNbr() );
620 *(pinVar.port()) |= ( 1 << pinVar.bitNbr() );
621}
622
623
624
635inline bool readGpioPinDigitalV( const GpioPinVariable& pinVar )
636{
637 return *(pinVar.pin()) & ( 1 << pinVar.bitNbr() );
638}
639
640
641
642
652inline void writeGpioPinDigitalV( const GpioPinVariable& pinVar, bool value )
653{
654 if ( value )
655 {
656 *(pinVar.port()) |= ( 1 << pinVar.bitNbr() );
657 }
658 else
659 {
660 *(pinVar.port()) &= ~( 1 << pinVar.bitNbr() );
661 }
662}
663
664
665
674inline void setGpioPinHighV( const GpioPinVariable& pinVar )
675{
676 *(pinVar.port()) |= ( 1 << pinVar.bitNbr() );
677}
678
679
680
681
690inline void setGpioPinLowV( const GpioPinVariable& pinVar )
691{
692 *(pinVar.port()) &= ~( 1 << pinVar.bitNbr() );
693}
694
695
696
697#endif
@ kDigitalHigh
Value representing digital HIGH.
Definition GpioPinMacros.h:50
@ kDigitalLow
Value representing digital LOW.
Definition GpioPinMacros.h:49
void setGpioPinModeOutputV(const GpioPinVariable &pinVar)
Set the mode of the GPIO pin to output (i.e., set the corresponding DDRn bit).
Definition GpioPinMacros.h:585
void setGpioPinLowV(const GpioPinVariable &pinVar)
Write a 0 to the GPIO pin (i.e., clear the correspoinding the PORTn bit).
Definition GpioPinMacros.h:690
void setGpioPinHighV(const GpioPinVariable &pinVar)
Write a 1 to the GPIO pin (i.e., set the correspoinding the PORTn bit).
Definition GpioPinMacros.h:674
bool isGpioPinModeInputV(const GpioPinVariable &pinVar)
Test if the mode of the GPIO pin is input (i.e., the corresponding DDRn is clear).
Definition GpioPinMacros.h:568
bool readGpioPinDigitalV(const GpioPinVariable &pinVar)
Read the value of the GPIO pin (i.e., return the value of correspoinding the PINn bit).
Definition GpioPinMacros.h:635
void setGpioPinModeInputPullupV(const GpioPinVariable &pinVar)
Set the mode of the GPIO pin to input with pullup (i.e., clear the corresponding DDRn bit and set the...
Definition GpioPinMacros.h:617
void writeGpioPinDigitalV(const GpioPinVariable &pinVar, bool value)
Write a value the GPIO pin (i.e., set or clear the correspoinding the PORTn bit).
Definition GpioPinMacros.h:652
bool isGpioPinModeOutputV(const GpioPinVariable &pinVar)
Test if the mode of the GPIO pin is output (i.e., the corresponding DDRn bit is set).
Definition GpioPinMacros.h:556
void setGpioPinModeInputV(const GpioPinVariable &pinVar)
Set the mode of the GPIO pin to input (i.e., clear the corresponding DDRn and PORTn bits).
Definition GpioPinMacros.h:601
This class defines a type that can encode a GPIO pin as a variable. Read the section on [GPIO Pin Var...
Definition GpioPinMacros.h:425
uint8_t bitNbr() const
Return the bit number of this GPIO pin within the DDR, PORT, and PIN registers.
Definition GpioPinMacros.h:469
Gpio8Ptr port() const
Return a pointer to the PORT register.
Definition GpioPinMacros.h:453
Gpio16Ptr ocr() const
Return a pointer to the OCR register (PWM related).
Definition GpioPinMacros.h:461
Gpio8Ptr ddr() const
Return a pointer to the DDR register.
Definition GpioPinMacros.h:449
uint8_t com() const
Return the bit number needed for manipulating TCCR register (PWM related).
Definition GpioPinMacros.h:473
uint8_t adcNbr() const
Return the ADC channel number (analog-to-digital related).
Definition GpioPinMacros.h:477
Gpio8Ptr pin() const
Return a pointer to the PIN register.
Definition GpioPinMacros.h:457
Gpio8Ptr tccr() const
Return a pointer to the TCCR register (PWM related).
Definition GpioPinMacros.h:465