Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
bcm2835-inl.hpp
1 /*
2  Title --- driver/bcm2835-inl.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw 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 Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_IFACE_BCM2835_INL_HPP_
23 #define _RPI_HW_IFACE_BCM2835_INL_HPP_
24 
25 namespace rpihw { // Begin main namespace
26 
27 namespace driver { // Begin drivers namespace
28 
29 inline void
30 bcm2835::setBit( uint8_t offset, uint8_t index, bool value ) {
31 
32  // Set the bit value on the GPIO controller register
33  utils::set_bit( m_gpio, offset + index / 32, index % 32, value );
34 }
35 
36 inline bool
37 bcm2835::getBit( uint8_t offset, uint8_t index ) const {
38 
39  // Return the bit value from one of the GPIO controller registers
40  return utils::get_bit( m_gpio, offset + index / 32, index % 32 );
41 }
42 
43 inline void
44 bcm2835::waitCycles( size_t cycles ) const {
45 
46  // Wait some CPU cycles
47  while ( --cycles ) asm volatile( "nop" );
48 }
49 
50 inline void
51 bcm2835::write( uint8_t pin, bool value ) {
52 
53  // Set the value of the output pin
54  // 0 = Low
55  // 1 = High
56  *( m_gpio + ( value ? GPSET0 : GPCLR0 ) + pin / 32 ) = 1 << ( pin % 32 );
57 }
58 
59 inline bool
60 bcm2835::read( uint8_t pin ) const {
61 
62  // Return the value of the input pin
63  // 0 = Low
64  // 1 = High
65  return getBit( GPLEV0, pin );
66 }
67 
68 inline bool
69 bcm2835::checkEvent( uint8_t pin ) const {
70 
71  // Return event state of the GPIO pin
72  // 0 = Event not detected on the GPIO pin
73  // 1 = Event detected on the GPIO pin
74  return getBit( GPEDS0, pin );
75 }
76 
77 inline void
78 bcm2835::setRisingEvent( uint8_t pin, bool enabled ) {
79 
80  // Enable/disable rising edge event on the GPIO pin
81  // 0 = Disabled
82  // 1 = Enabled
83  setBit( GPREN0, pin, enabled );
84 }
85 
86 inline void
87 bcm2835::setFallingEvent( uint8_t pin, bool enabled ) {
88 
89  // Enable/disable falling edge event on the GPIO pin
90  // 0 = Disabled
91  // 1 = Enabled
92  setBit( GPFEN0, pin, enabled );
93 }
94 
95 inline void
96 bcm2835::setHighEvent( uint8_t pin, bool enabled ) {
97 
98  // Enable/disable high event on the GPIO pin
99  // 0 = Disabled
100  // 1 = Enabled
101  setBit( GPHEN0, pin, enabled );
102 }
103 
104 inline void
105 bcm2835::setLowEvent( uint8_t pin, bool enabled ) {
106 
107  // Enable/disable low event on the GPIO pin
108  // 0 = Disabled
109  // 1 = Enabled
110  setBit( GPLEN0, pin, enabled );
111 }
112 
113 } // End of drivers namespace
114 
115 } // End of main namespace
116 
117 #endif /* _RPI_HW_IFACE_BCM2835_INL_HPP_ */
bool checkEvent(uint8_t pin) const
Returns the event state of a GPIO pin.
Definition: bcm2835-inl.hpp:69
void setLowEvent(uint8_t pin, bool enabled)
Enables/disables the low event on a GPIO pin.
bool get_bit(T &buffer, size_t offset, uint8_t index)
Retuns a bit value in a variable.
Definition: utils-inl.hpp:58
void setFallingEvent(uint8_t pin, bool enabled)
Enables/disables the falling edge event on a GPIO pin.
Definition: bcm2835-inl.hpp:87
volatile uint32_t * m_gpio
GPIO controller virtual address.
Definition: bcm2835.hpp:640
void set_bit(T &buffer, size_t offset, uint8_t index, bool value)
Sets a bit in a variable.
Definition: utils-inl.hpp:31
void setBit(uint8_t offset, uint8_t index, bool value)
Sets a bit value on one of the GPIO controller registers.
Definition: bcm2835-inl.hpp:30
bool getBit(uint8_t offset, uint8_t index) const
Returns a bit value from one of the GPIO controller registers.
Definition: bcm2835-inl.hpp:37
void setHighEvent(uint8_t pin, bool enabled)
Enables/disables the high event on a GPIO pin.
Definition: bcm2835-inl.hpp:96
void write(uint8_t pin, bool value)
Sets the value of a output pin.
Definition: bcm2835-inl.hpp:51
void setRisingEvent(uint8_t pin, bool enabled)
Enables/disables the rising edge event on a GPIO pin.
Definition: bcm2835-inl.hpp:78
void waitCycles(size_t cycles) const
Wait some CPU cycles.
Definition: bcm2835-inl.hpp:44
bool read(uint8_t pin) const
Returns the value of a input pin.
Definition: bcm2835-inl.hpp:60