Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPIFlash.hxx
Go to the documentation of this file.
1
36#ifndef _FREERTOS_DRIVERS_COMMON_SPIFLASH_HXX_
37#define _FREERTOS_DRIVERS_COMMON_SPIFLASH_HXX_
38
39#include <inttypes.h>
40#include <spi/spidev.h>
41#include <sys/types.h>
42
43#include "utils/logging.h"
44
45class OSMutex;
46class SPI;
47
59{
61 uint32_t speedHz_ {1000000};
62
64 uint32_t sectorSize_ {4 * 1024};
65
73 uint32_t pageSizeMask_ {~(256u - 1)};
74
76 uint8_t spiMode_ {SPI_MODE_0};
77
79 uint8_t idCommand_ {0x9F};
81 uint8_t readCommand_ {0x03};
83 uint8_t writeEnableCommand_ {0x06};
85 uint8_t writeCommand_ {0x02};
87 uint8_t eraseCommand_ {0x20};
89 uint8_t chipEraseCommand_ {0x60};
90
92 uint8_t statusReadCommand_ {0x05};
95 uint8_t statusWritePendingBit_ {0x01};
96
98 uint8_t readNeedsStuffing_ : 1;
99};
100
104{
105public:
111 : cfg_(cfg)
112 , lock_(lock)
113 {
115 HASSERT((cfg->sectorSize_ & (cfg->sectorSize_ - 1)) == 0);
116 }
117
120 {
121 return *cfg_;
122 }
123
126 void init(const char *dev_name);
127
133 void write(uint32_t addr, const void *buf, size_t len);
134
139 void read(uint32_t addr, void *buf, size_t len);
140
146 uint32_t next_sector_address(uint32_t addr)
147 {
148 return (addr + cfg_->sectorSize_ - 1) & ~(cfg_->sectorSize_ - 1);
149 }
150
154 void erase(uint32_t addr, size_t len);
155
157 void chip_erase();
158
162 void get_id(char id_out[3]);
163
164private:
167 unsigned wait_for_write();
168
171
174
176 int spiFd_ {-1};
180};
181
182#endif // _FREERTOS_DRIVERS_COMMON_SPIFLASH_HXX_
This class provides a mutex API.
Definition OS.hxx:427
Shared implementation for operating spiflash devices.
Definition SPIFlash.hxx:104
const SPIFlashConfig & cfg()
Definition SPIFlash.hxx:119
void write(uint32_t addr, const void *buf, size_t len)
Performs write to the device.
Definition SPIFlash.cxx:127
void init(const char *dev_name)
Opens the SPI bus.
Definition SPIFlash.cxx:78
void chip_erase()
Erases the entire device.
Definition SPIFlash.cxx:224
SPI * spi_
Direct access of the SPI device pointer.
Definition SPIFlash.hxx:179
uint32_t next_sector_address(uint32_t addr)
Aligns an address to the next possible sector start (i.e., rounds up to sector boundary).
Definition SPIFlash.hxx:146
void get_id(char id_out[3])
Fetches the identification bytes form the SPIFlash.
Definition SPIFlash.cxx:93
void read(uint32_t addr, void *buf, size_t len)
Reads data from the device.
Definition SPIFlash.cxx:105
void erase(uint32_t addr, size_t len)
Erases sector(s) of the device.
Definition SPIFlash.cxx:196
OSMutex * lock_
Lock that protects accesses to the flash chip.
Definition SPIFlash.hxx:173
SPIFlash(const SPIFlashConfig *cfg, OSMutex *lock)
Constructor.
Definition SPIFlash.hxx:110
int spiFd_
File descriptor for the opened SPI bus.
Definition SPIFlash.hxx:176
const SPIFlashConfig * cfg_
Configuration.
Definition SPIFlash.hxx:170
unsigned wait_for_write()
Waits until write is complete.
Definition SPIFlash.cxx:172
Private data for an SPI device.
Definition SPI.hxx:53
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
Create a const structure like this to tell the spiflash driver how to talk to your spiflash device.
Definition SPIFlash.hxx:59
uint8_t statusReadCommand_
Command to use for status register read.
Definition SPIFlash.hxx:92
uint8_t chipEraseCommand_
Command to use for chip erase.
Definition SPIFlash.hxx:89
uint8_t spiMode_
SPI mode to use.
Definition SPIFlash.hxx:76
uint32_t pageSizeMask_
A page program operation might wrap around a page.
Definition SPIFlash.hxx:73
uint8_t readNeedsStuffing_
Set this to 1 if the read command needs a dummy byte after the address.
Definition SPIFlash.hxx:98
uint8_t statusWritePendingBit_
Which bit to check in the status register for write complete.
Definition SPIFlash.hxx:95
uint32_t speedHz_
Use this frequency to talk to SPI.
Definition SPIFlash.hxx:61
uint8_t writeCommand_
Command to use for writes.
Definition SPIFlash.hxx:85
uint32_t sectorSize_
How many bytes is an erase sector.
Definition SPIFlash.hxx:64
uint8_t idCommand_
Command to use for get identification bytes.
Definition SPIFlash.hxx:79
uint8_t readCommand_
Command to use for reads.
Definition SPIFlash.hxx:81
uint8_t writeEnableCommand_
Command sent out before each write/erase command.
Definition SPIFlash.hxx:83
uint8_t eraseCommand_
Command to use for sector erases.
Definition SPIFlash.hxx:87