PIDUINO
Loading...
Searching...
No Matches
iomap.h
1/* Copyright © 2015-2018 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <piduino/global.h>
20
28namespace Piduino {
29
34 class IoMap {
35 public:
40
44 virtual ~IoMap();
45
46 bool open (const char *device, size_t size, off_t base = 0);
47
55 bool open (off_t base, size_t size);
56
66 bool openGpioMem (int gpio_bank, size_t size);
67
71 void close();
72
78 volatile uint32_t *io (size_t offset = 0) const {
79
80 return static_cast<volatile uint32_t *> (_map) + offset;
81 }
82
88 volatile uint32_t *operator [] (size_t offset) const {
89
90 return io (offset);
91 }
92
101 inline uint32_t read (size_t offset) const {
102 return *io (offset);
103 }
104
110 inline void write (size_t offset, uint32_t value) {
111 *io (offset) = value;
112 }
113
122 inline uint32_t atomicRead (size_t offset) const {
123 volatile uint32_t *reg = io (offset);
124 return __atomic_load_n (reg, __ATOMIC_SEQ_CST);
125 }
126
137 inline void atomicWrite (size_t offset, uint32_t value) {
138 volatile uint32_t *reg = io (offset);
139 __atomic_store_n (reg, value, __ATOMIC_SEQ_CST);
140 }
141
145 inline bool isOpen() const {
146
147 return _fd >= 0;
148 }
149
153 inline off_t base() const {
154 return _base;
155 }
156
160 inline size_t size() const {
161 return _size;
162 }
163
167 static size_t pageSize();
168
169 private:
170 off_t _base; /*< base address of the area */
171 size_t _size; /*< size of the area */
172 int _fd; /*< file descriptor for memory area */
173 void *_map; /*< pointer to the area */
174 };
175}
180/* ========================================================================== */
Memory mapping.
Definition iomap.h:34
void close()
Close a memory mapping.
bool open(off_t base, size_t size)
Open a memory mapping on /dev/mem.
off_t _base
Definition iomap.h:170
void * _map
Definition iomap.h:173
volatile uint32_t * operator[](size_t offset) const
Pointer to access registers.
Definition iomap.h:88
volatile uint32_t * io(size_t offset=0) const
Pointer to access registers.
Definition iomap.h:78
size_t _size
Definition iomap.h:171
bool open(const char *device, size_t size, off_t base=0)
void atomicWrite(size_t offset, uint32_t value)
Atomic write to a register.
Definition iomap.h:137
virtual ~IoMap()
Destructor.
uint32_t atomicRead(size_t offset) const
Atomic read of a register.
Definition iomap.h:122
uint32_t read(size_t offset) const
Read a register.
Definition iomap.h:101
bool isOpen() const
Indicates if a memory mapping is open.
Definition iomap.h:145
void write(size_t offset, uint32_t value)
Write a value to a register.
Definition iomap.h:110
off_t base() const
Base address of the mapped area.
Definition iomap.h:153
static size_t pageSize()
Size of a mapping page.
IoMap()
Constructor.
bool openGpioMem(int gpio_bank, size_t size)
Open a GPIO memory mapping for RP1.
size_t size() const
Size of the mapped area.
Definition iomap.h:160
Global namespace for Piduino.
Definition board.h:28