Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPIFlash Class Reference

Shared implementation for operating spiflash devices. More...

#include <SPIFlash.hxx>

Public Member Functions

 SPIFlash (const SPIFlashConfig *cfg, OSMutex *lock)
 Constructor.
 
const SPIFlashConfigcfg ()
 
void init (const char *dev_name)
 Opens the SPI bus.
 
void write (uint32_t addr, const void *buf, size_t len)
 Performs write to the device.
 
void read (uint32_t addr, void *buf, size_t len)
 Reads data from the device.
 
uint32_t next_sector_address (uint32_t addr)
 Aligns an address to the next possible sector start (i.e., rounds up to sector boundary).
 
void erase (uint32_t addr, size_t len)
 Erases sector(s) of the device.
 
void chip_erase ()
 Erases the entire device.
 
void get_id (char id_out[3])
 Fetches the identification bytes form the SPIFlash.
 

Private Member Functions

unsigned wait_for_write ()
 Waits until write is complete.
 

Private Attributes

const SPIFlashConfigcfg_
 Configuration.
 
OSMutexlock_
 Lock that protects accesses to the flash chip.
 
int spiFd_ {-1}
 File descriptor for the opened SPI bus.
 
SPIspi_
 Direct access of the SPI device pointer.
 

Detailed Description

Shared implementation for operating spiflash devices.

This class is intended to be used by other device drivers.

Definition at line 103 of file SPIFlash.hxx.

Constructor & Destructor Documentation

◆ SPIFlash()

SPIFlash::SPIFlash ( const SPIFlashConfig cfg,
OSMutex lock 
)
inline

Constructor.

Parameters
cfgstatic configuration for this SPI flash device.
lockthis lock will be taken before performing any operation on the chip. Can be null.

This ensures that the sector size is a power of two.

Definition at line 110 of file SPIFlash.hxx.

Member Function Documentation

◆ cfg()

const SPIFlashConfig & SPIFlash::cfg ( )
inline
Returns
the configuration.

Definition at line 119 of file SPIFlash.hxx.

◆ chip_erase()

void SPIFlash::chip_erase ( )

Erases the entire device.

Definition at line 224 of file SPIFlash.cxx.

◆ erase()

void SPIFlash::erase ( uint32_t  addr,
size_t  len 
)

Erases sector(s) of the device.

Parameters
addrbeginning of the sector to erase. Must be sector aligned.
lenhow many bytes to erase (must be multiple of sector size).

Definition at line 196 of file SPIFlash.cxx.

◆ get_id()

void SPIFlash::get_id ( char  id_out[3])

Fetches the identification bytes form the SPIFlash.

Parameters
id_outreturn parameter, will be filled with the received identification bytes.

Definition at line 93 of file SPIFlash.cxx.

◆ init()

void SPIFlash::init ( const char *  dev_name)

Opens the SPI bus.

This is typically called in hw_postinit or main.

Parameters
dev_namethe name of the SPI device.

Definition at line 78 of file SPIFlash.cxx.

◆ next_sector_address()

uint32_t SPIFlash::next_sector_address ( uint32_t  addr)
inline

Aligns an address to the next possible sector start (i.e., rounds up to sector boundary).

Parameters
addran address in the flash address space.
Returns
If addr is the first byte of a sector, then returns addr unmodified. Otherwise returns the starting address of the next sector.

Definition at line 146 of file SPIFlash.hxx.

◆ read()

void SPIFlash::read ( uint32_t  addr,
void *  buf,
size_t  len 
)

Reads data from the device.

Parameters
addrwhere to read from
bufpoints to where to put the data read
lenhow many bytes to read

Definition at line 105 of file SPIFlash.cxx.

◆ wait_for_write()

unsigned SPIFlash::wait_for_write ( )
private

Waits until write is complete.

Returns
how many iterations the wait took

Definition at line 172 of file SPIFlash.cxx.

◆ write()

void SPIFlash::write ( uint32_t  addr,
const void *  buf,
size_t  len 
)

Performs write to the device.

Thiscall is synchronous; does not return until the write is complete.

Parameters
addrwhere to write (0 = beginning of the device).
bufdata to write
lenhow many bytes to write

Definition at line 127 of file SPIFlash.cxx.

Member Data Documentation

◆ cfg_

const SPIFlashConfig* SPIFlash::cfg_
private

Configuration.

Definition at line 170 of file SPIFlash.hxx.

◆ lock_

OSMutex* SPIFlash::lock_
private

Lock that protects accesses to the flash chip.

Definition at line 173 of file SPIFlash.hxx.

◆ spi_

SPI* SPIFlash::spi_
private

Direct access of the SPI device pointer.

Todo:
maybe we are not actually using this.

Definition at line 179 of file SPIFlash.hxx.

◆ spiFd_

int SPIFlash::spiFd_ {-1}
private

File descriptor for the opened SPI bus.

Definition at line 176 of file SPIFlash.hxx.


The documentation for this class was generated from the following files: