PIDUINO
Loading...
Searching...
No Matches
Piduino::IoMap

Memory mapping. More...

#include <iomap.h>

Collaboration diagram for Piduino::IoMap:

Public Member Functions

 IoMap ()
 Constructor.
 
virtual ~IoMap ()
 Destructor.
 
bool open (const char *device, size_t size, off_t base=0)
 
bool open (off_t base, size_t size)
 Open a memory mapping on /dev/mem.
 
bool openGpioMem (int gpio_bank, size_t size)
 Open a GPIO memory mapping for RP1.
 
void close ()
 Close a memory mapping.
 
volatile uint32_t * io (size_t offset=0) const
 Pointer to access registers.
 
volatile uint32_t * operator[] (size_t offset) const
 Pointer to access registers.
 
uint32_t read (size_t offset) const
 Read a register.
 
void write (size_t offset, uint32_t value)
 Write a value to a register.
 
uint32_t atomicRead (size_t offset) const
 Atomic read of a register.
 
void atomicWrite (size_t offset, uint32_t value)
 Atomic write to a register.
 
bool isOpen () const
 Indicates if a memory mapping is open.
 
off_t base () const
 Base address of the mapped area.
 
size_t size () const
 Size of the mapped area.
 

Static Public Member Functions

static size_t pageSize ()
 Size of a mapping page.
 

Private Attributes

off_t _base
 
size_t _size
 
int _fd
 
void * _map
 

Detailed Description

Memory mapping.

Definition at line 34 of file iomap.h.

Constructor & Destructor Documentation

◆ IoMap()

Piduino::IoMap::IoMap ( )

Constructor.

◆ ~IoMap()

virtual Piduino::IoMap::~IoMap ( )
virtual

Destructor.

Member Function Documentation

◆ atomicRead()

uint32_t Piduino::IoMap::atomicRead ( size_t  offset) const
inline

Atomic read of a register.

Parameters
offsetoffset inside the area in sizeof(int)
Returns
the value read

This function reads a register atomically, ensuring that the read operation is not interrupted by other threads or processes.

Definition at line 122 of file iomap.h.

References io().

Here is the call graph for this function:

◆ atomicWrite()

void Piduino::IoMap::atomicWrite ( size_t  offset,
uint32_t  value 
)
inline

Atomic write to a register.

Parameters
offsetoffset inside the area in sizeof(int)
valuevalue to write

This function writes a value to a register atomically, ensuring that the write operation is not interrupted by other threads or processes. It uses C11 atomic operations to ensure memory consistency.

Note
This function is useful for ensuring that hardware registers are updated safely in a multi-threaded environment.

Definition at line 137 of file iomap.h.

References io().

Here is the call graph for this function:

◆ base()

off_t Piduino::IoMap::base ( ) const
inline

Base address of the mapped area.

Definition at line 153 of file iomap.h.

References _base.

◆ close()

void Piduino::IoMap::close ( )

Close a memory mapping.

◆ io()

volatile uint32_t * Piduino::IoMap::io ( size_t  offset = 0) const
inline

Pointer to access registers.

Parameters
offsetoffset inside the area in sizeof(int)
Returns
pointer to the register, NULL if error

Definition at line 78 of file iomap.h.

References _map.

Referenced by atomicRead(), atomicWrite(), operator[](), read(), and write().

◆ isOpen()

bool Piduino::IoMap::isOpen ( ) const
inline

Indicates if a memory mapping is open.

Definition at line 145 of file iomap.h.

References _fd.

◆ open() [1/2]

bool Piduino::IoMap::open ( const char *  device,
size_t  size,
off_t  base = 0 
)

◆ open() [2/2]

bool Piduino::IoMap::open ( off_t  base,
size_t  size 
)

Open a memory mapping on /dev/mem.

Parameters
basebase address of the area to be mapped, must be a multiple of pageSize().
sizesize of the area to be mapped in bytes
Returns
true if opened, false if error

◆ openGpioMem()

bool Piduino::IoMap::openGpioMem ( int  gpio_bank,
size_t  size 
)

Open a GPIO memory mapping for RP1.

Parameters
gpio_bankGPIO bank (0-4 for /dev/gpiomem0 to /dev/gpiomem4)
sizesize of the area to be mapped in bytes
Returns
true if opened, false if error
Exceptions
std::system_errorif the open the device or memory mapping fails.

◆ operator[]()

volatile uint32_t * Piduino::IoMap::operator[] ( size_t  offset) const
inline

Pointer to access registers.

Parameters
offsetoffset inside the area in sizeof(int)
Returns
pointer to the register, NULL if error

Definition at line 88 of file iomap.h.

References io().

Here is the call graph for this function:

◆ pageSize()

static size_t Piduino::IoMap::pageSize ( )
static

Size of a mapping page.

◆ read()

uint32_t Piduino::IoMap::read ( size_t  offset) const
inline

Read a register.

Parameters
offsetoffset inside the area in sizeof(int)
Returns
the value read

This function reads a register at the specified offset and returns its value. It is a convenience method that allows direct access to the memory-mapped registers.

Definition at line 101 of file iomap.h.

References io().

Here is the call graph for this function:

◆ size()

size_t Piduino::IoMap::size ( ) const
inline

Size of the mapped area.

Definition at line 160 of file iomap.h.

References _size.

◆ write()

void Piduino::IoMap::write ( size_t  offset,
uint32_t  value 
)
inline

Write a value to a register.

Parameters
offsetoffset inside the area in sizeof(int)
valuevalue to write

Definition at line 110 of file iomap.h.

References io().

Here is the call graph for this function:

Member Data Documentation

◆ _base

off_t Piduino::IoMap::_base
private

Definition at line 170 of file iomap.h.

Referenced by base().

◆ _fd

int Piduino::IoMap::_fd
private

Definition at line 172 of file iomap.h.

Referenced by isOpen().

◆ _map

void* Piduino::IoMap::_map
private

Definition at line 173 of file iomap.h.

Referenced by io().

◆ _size

size_t Piduino::IoMap::_size
private

Definition at line 171 of file iomap.h.

Referenced by size().