gpio.h
Go to the documentation of this file.
1 /*
2  * gpio.h
3  *
4  * Copyright (c) 2014 Jeremy Garff <jer @ jers.net>
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification, are permitted
9  * provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this list of
12  * conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
14  * of conditions and the following disclaimer in the documentation and/or other materials
15  * provided with the distribution.
16  * 3. Neither the name of the owner nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __GPIO_H__
31 #define __GPIO_H__
32 
33 #include <stdint.h>
34 
35 typedef struct
36 {
37  uint32_t fsel[6]; // GPIO Function Select
38  uint32_t resvd_0x18;
39  uint32_t set[2]; // GPIO Pin Output Set
40  uint32_t resvd_0x24;
41  uint32_t clr[2]; // GPIO Pin Output Clear
42  uint32_t resvd_0x30;
43  uint32_t lev[2]; // GPIO Pin Level
44  uint32_t resvd_0x3c;
45  uint32_t eds[2]; // GPIO Pin Event Detect Status
46  uint32_t resvd_0x48;
47  uint32_t ren[2]; // GPIO Pin Rising Edge Detect Enable
48  uint32_t resvd_0x54;
49  uint32_t fen[2]; // GPIO Pin Falling Edge Detect Enable
50  uint32_t resvd_0x60;
51  uint32_t hen[2]; // GPIO Pin High Detect Enable
52  uint32_t resvd_0x6c;
53  uint32_t len[2]; // GPIO Pin Low Detect Enable
54  uint32_t resvd_0x78;
55  uint32_t aren[2]; // GPIO Pin Async Rising Edge Detect
56  uint32_t resvd_0x84;
57  uint32_t afen[2]; // GPIO Pin Async Falling Edge Detect
58  uint32_t resvd_0x90;
59  uint32_t pud; // GPIO Pin Pull up/down Enable
60  uint32_t pudclk[2]; // GPIO Pin Pull up/down Enable Clock
61  uint32_t resvd_0xa0[4];
62  uint32_t test;
63 } __attribute__((packed, aligned(4))) gpio_t;
64 
65 
66 #define GPIO_OFFSET (0x00200000)
67 
68 
69 static inline void gpio_function_set(volatile gpio_t *gpio, uint8_t pin, uint8_t function)
70 {
71  int regnum = pin / 10;
72  int offset = (pin % 10) * 3;
73  uint8_t funcmap[] = { 4, 5, 6, 7, 3, 2 }; // See datasheet for mapping
74 
75  if (function > 5)
76  {
77  return;
78  }
79 
80  gpio->fsel[regnum] &= ~(0x7 << offset);
81  gpio->fsel[regnum] |= ((funcmap[function]) << offset);
82 }
83 
84 static inline void gpio_level_set(volatile gpio_t *gpio, uint8_t pin, uint8_t level)
85 {
86  int regnum = pin >> 5;
87  int offset = (pin & 0x1f);
88 
89  if (level)
90  {
91  gpio->set[regnum] = (1 << offset);
92  }
93  else
94  {
95  gpio->clr[regnum] = (1 << offset);
96  }
97 }
98 
99 static inline void gpio_output_set(volatile gpio_t *gpio, uint8_t pin, uint8_t output)
100 {
101  int regnum = pin / 10;
102  int offset = (pin % 10) * 3;
103  uint8_t function = output ? 1 : 0; // See datasheet for mapping
104 
105  gpio->fsel[regnum] &= ~(0x7 << offset);
106  gpio->fsel[regnum] |= ((function & 0x7) << offset);
107 }
108 
109 #endif /* __GPIO_H__ */
uint32_t resvd_0x6c
Definition: gpio.h:52
static void gpio_output_set(volatile gpio_t *gpio, uint8_t pin, uint8_t output)
Definition: gpio.h:99
uint32_t resvd_0x84
Definition: gpio.h:56
uint32_t resvd_0x30
Definition: gpio.h:42
uint32_t resvd_0x78
Definition: gpio.h:54
uint32_t resvd_0x3c
Definition: gpio.h:44
aligned(4))) gpio_t
uint32_t resvd_0x24
Definition: gpio.h:40
uint32_t resvd_0x48
Definition: gpio.h:46
uint32_t pud
Definition: gpio.h:59
static void gpio_level_set(volatile gpio_t *gpio, uint8_t pin, uint8_t level)
Definition: gpio.h:84
uint32_t resvd_0x60
Definition: gpio.h:50
uint32_t resvd_0x54
Definition: gpio.h:48
static void gpio_function_set(volatile gpio_t *gpio, uint8_t pin, uint8_t function)
Definition: gpio.h:69
uint32_t resvd_0x90
Definition: gpio.h:58
uint32_t test
Definition: gpio.h:62


ws281x
Author(s): Alexey Rogachevskiy , Oleg Kalachev
autogenerated on Wed Jun 15 2022 02:46:00