rpimemmap

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 7 Imported by: 2

README

rpimemmap GoDoc

Package for allocating uncached memory and physical devices on Raspberry Pi's using plain GO.

About

This package provides access to uncached memory and physical devices on any Raspberry Pi. Uncached memory is done with the help of videocore. All access is directly written to memory.

Installation

go get github.com/DerLukas15/rpimemmap

Usage

Open

You need to supply the desired size during the creation of the actual map. However, the actual allocation is done during the map call.

The supplyed size is rounded up to the nearest pageSize.

Physical Devices

Map any physical device to virtual memory for access by supplying the physical address of the device:

deviceMap := rpimemmap.NewPeripheral(size)
err := deviceMap.Map(physicalAddress, memoryDevice, 0)
Uncached Memory

Map uncached memory to virtual memory for access:

uncachedMap := rpimemmap.NewUncached(size)
err := uncachedMap.Map(0, "", UncachedMemFlags)

Close

Close any MemMap with

err := curMap.Unmap()

License

MIT, see LICENSE

Documentation

Overview

Package rpimemmap is used for allocating uncached memory and physical devices on Raspberry Pi's using plain GO.

Index

Constants

View Source
const (
	MemDevDefault = "/dev/mem"
	MemDevGPIO    = "/dev/gpiomem"
)

Valid MemDev to use for mapping of peripherals.

View Source
const (
	UncachedMemFlagDiscardable            = 1 << 0                                          // can be resized to 0 at any time. Use for cached data
	UncachedMemFlagNormal                 = 0 << 2                                          // normal allocating alias. Don't use from ARM
	UncachedMemFlagDirect          uint32 = 1 << 2                                          // 0xC alias uncached
	UncachedMemFlagCoherent               = 2 << 2                                          // 0x8 alias. Non-allocating in L2 but coherent
	UncachedMemFlagZero                   = 1 << 4                                          // initialise buffer to all zeros
	UncachedMemFlagNoInit                 = 1 << 5                                          // don't initialise (default is initialise to all ones)
	UncachedMemFlagHintPermaLock          = 1 << 6                                          // Likely to be locked for long periods of time
	UncachedMemFlagL1Nonallocation        = UncachedMemFlagDirect | UncachedMemFlagCoherent // Allocating in L2
)

Memory flags for uncached memory

Variables

This section is empty.

Functions

func Dump

func Dump(m MemMap, size uint32) string

Dump will output the content of the memory. If size == 0, the size of the allocated memory will be used.

func MapSegment

func MapSegment(curMap MemMap, memDev string) ([]byte, error)

MapSegment maps a MemMap to virtual memory from physical memory address. See const MemDev for possible memDev.

func Reg32

func Reg32(m MemMap, memOffsetToBase uint32) *uint32

Reg32 returns a pointer to 4 bytes (32 bits as uint32) of memory from m which starts with an offset by memOffsetToBase bytes. You should be aware that the order of the bits does not have to be the actual order in memory as uint32 does not have to store the bits in the correct order (endian). Use multiple Reg8 calls to get the same memory region in the correct bit order.

Example:

The virtual memory address of the MemMap 'm' is 0xa6c1e000.

`pointerToMemory := Reg32(m, 0)` will be 32 bits which start at 0xa6c1e000

`pointerToMemory := Reg32(m, 1)` will be 32 bits which start at 0xa6c1e001 as 1 byte was added

`pointerToMemory := Reg32(m, 8)` will be 32 bits which start at 0xa6c1e008 as 8 bytes were added

func Reg8

func Reg8(m MemMap, memOffsetToBase uint32) *byte

Reg8 returns a pointer to 1 byte of memory from m which starts with an offset by memOffsetToBase bytes. The order of bits is unaltered. See Reg32 for detailed description of usage.

func UnmapSegment

func UnmapSegment(ref []byte) error

UnmapSegment unmaps a MemMap from virtual memory

Types

type MemMap

type MemMap interface {
	fmt.Stringer
	Map(physAddr uint32, memDev string, flags uint32) error
	Unmap() error
	PhysAddr() uint32
	BusAddr() uint32
	VirtAddr() unsafe.Pointer
	Size() uint32
}

MemMap defines a memory device.

type PeripheralMap

type PeripheralMap struct {
	// contains filtered or unexported fields
}

PeripheralMap is the representation of memory to a peripheral.

func NewPeripheral

func NewPeripheral(sizeInBytes uint32) *PeripheralMap

NewPeripheral creates a new PeripheralMap by supplying the desired size. The size is rounded up to the nearest pageSize.

func (*PeripheralMap) BusAddr

func (m *PeripheralMap) BusAddr() uint32

BusAddr returns the current bus address if available.

func (*PeripheralMap) Map

func (m *PeripheralMap) Map(physAddr uint32, memDev string, unused uint32) error

Map maps a peripheral to virtual memory by physical address. memDev can be MemDevDefault (requires root) or MemDevGPIO (only for GPIOs).

func (*PeripheralMap) PhysAddr

func (m *PeripheralMap) PhysAddr() uint32

PhysAddr returns the current physical address if available.

func (*PeripheralMap) Size

func (m *PeripheralMap) Size() uint32

Size returns the current size.

func (*PeripheralMap) String

func (m *PeripheralMap) String() string

String implements Stringer interface

func (*PeripheralMap) Unmap

func (m *PeripheralMap) Unmap() error

Unmap unmaps a virtual address.

func (*PeripheralMap) VirtAddr

func (m *PeripheralMap) VirtAddr() unsafe.Pointer

VirtAddr returns the current virtual address if available.

type UncachedMap

type UncachedMap struct {
	// contains filtered or unexported fields
}

UncachedMap is the representation of uncached memory via videocore.

func NewUncached

func NewUncached(sizeInBytes uint32) *UncachedMap

NewUncached creates a new UncachedMap by supplying the desired size.

func (*UncachedMap) BusAddr

func (m *UncachedMap) BusAddr() uint32

BusAddr returns the current bus address if available.

func (*UncachedMap) Map

func (m *UncachedMap) Map(unused uint32, unused2 string, uncachedMemFlags uint32) error

Map maps a uncached memory to virtual memory.

func (*UncachedMap) PhysAddr

func (m *UncachedMap) PhysAddr() uint32

PhysAddr returns the current physical address if available.

func (*UncachedMap) Size

func (m *UncachedMap) Size() uint32

Size returns the current size.

func (*UncachedMap) String

func (m *UncachedMap) String() string

String implements Stringer interface.

func (*UncachedMap) Unmap

func (m *UncachedMap) Unmap() error

Unmap unmaps a virtual address.

func (*UncachedMap) VirtAddr

func (m *UncachedMap) VirtAddr() unsafe.Pointer

VirtAddr returns the current virtual address if available.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL