kstmApp/src/pi_2_mmio.h
changeset 0 bd6bb22c6533
equal deleted inserted replaced
-1:000000000000 0:bd6bb22c6533
       
     1 // Copyright (c) 2014 Adafruit Industries
       
     2 // Author: Tony DiCola
       
     3 // Based on code from Gert van Loo & Dom: http://elinux.org/RPi_Low-level_peripherals#GPIO_Code_examples
       
     4 
       
     5 // Permission is hereby granted, free of charge, to any person obtaining a copy
       
     6 // of this software and associated documentation files (the "Software"), to deal
       
     7 // in the Software without restriction, including without limitation the rights
       
     8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
     9 // copies of the Software, and to permit persons to whom the Software is
       
    10 // furnished to do so, subject to the following conditions:
       
    11 
       
    12 // The above copyright notice and this permission notice shall be included in all
       
    13 // copies or substantial portions of the Software.
       
    14 
       
    15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
       
    21 // SOFTWARE.
       
    22 
       
    23 // Simple fast memory-mapped GPIO library for the Raspberry Pi.
       
    24 #ifndef PI_2_MMIO_H
       
    25 #define PI_2_MMIO_H
       
    26 
       
    27 #include <stdint.h>
       
    28 
       
    29 #define MMIO_SUCCESS 0
       
    30 #define MMIO_ERROR_DEVMEM -1
       
    31 #define MMIO_ERROR_MMAP -2
       
    32 #define MMIO_ERROR_OFFSET -3
       
    33 
       
    34 volatile uint32_t* pi_2_mmio_gpio;
       
    35 
       
    36 int pi_2_mmio_init(void);
       
    37 
       
    38 static inline void pi_2_mmio_set_input(const int gpio_number) {
       
    39   // Set GPIO register to 000 for specified GPIO number.
       
    40   *(pi_2_mmio_gpio+((gpio_number)/10)) &= ~(7<<(((gpio_number)%10)*3));
       
    41 }
       
    42 
       
    43 static inline void pi_2_mmio_set_output(const int gpio_number) {
       
    44   // First set to 000 using input function.
       
    45   pi_2_mmio_set_input(gpio_number);
       
    46   // Next set bit 0 to 1 to set output.
       
    47   *(pi_2_mmio_gpio+((gpio_number)/10)) |=  (1<<(((gpio_number)%10)*3));
       
    48 }
       
    49 
       
    50 static inline void pi_2_mmio_set_high(const int gpio_number) {
       
    51   *(pi_2_mmio_gpio+7) = 1 << gpio_number;
       
    52 }
       
    53 
       
    54 static inline void pi_2_mmio_set_low(const int gpio_number) {
       
    55   *(pi_2_mmio_gpio+10) = 1 << gpio_number;
       
    56 }
       
    57 
       
    58 static inline uint32_t pi_2_mmio_input(const int gpio_number) {
       
    59   return *(pi_2_mmio_gpio+13) & (1 << gpio_number);
       
    60 }
       
    61 
       
    62 #endif