PropWare
C++ objects and CMake build system for Parallax Propeller
|
A simple SD driver communicating over the SPI protocol. More...
#include <sd.h>
Public Types | |
enum | ErrorCode { NO_ERROR = 0, BEG_ERROR = SPI::END_ERROR + 1, INVALID_CMD = BEG_ERROR, READ_TIMEOUT, INVALID_NUM_BYTES, INVALID_RESPONSE, INVALID_INIT, INVALID_DAT_START_ID, CMD8_FAILURE, END_ERROR = CMD8_FAILURE } |
Public Member Functions | |
unpack_sd_pins ((uint32_t *) pins) | |
this m_spi | set_mosi (pins[0]) |
this m_spi | set_miso (pins[1]) |
this m_spi | set_sclk (pins[2]) |
this m_cs | set_mask (pins[3]) |
this m_cs | set () |
this m_cs | set_dir_out () |
SD (SPI *spi, const Port::Mask mosi, const Port::Mask miso, const Port::Mask sclk, const Port::Mask cs) | |
Construct an SD object with the given SPI parameters. More... | |
PropWare::ErrorCode | start () const |
Initialize SD card communication over SPI for 3.3V configuration. More... | |
uint16_t | get_sector_size () const |
Return the size of a sector (also known as a "block") for the given storage device. More... | |
uint8_t | get_sector_size_shift () const |
Determine the number of shifts required to multiply or divide a number by the sector size. More... | |
PropWare::ErrorCode | read_data_block (const uint32_t address, uint8_t buf[]) const |
Read a block of data from the device into RAM. More... | |
PropWare::ErrorCode | write_data_block (uint32_t address, const uint8_t dat[]) const |
Write data to a storage device. More... | |
uint16_t | get_short (const uint16_t offset, const uint8_t buf[]) const |
Read two bytes from a buffer. More... | |
uint32_t | get_long (const uint16_t offset, const uint8_t buf[]) const |
Read four bytes from a buffer. More... | |
void | write_short (const uint16_t offset, uint8_t buf[], const uint16_t value) const |
Write two bytes to a buffer. More... | |
void | write_long (const uint16_t offset, uint8_t buf[], const uint32_t value) const |
Write four bytes to a buffer. More... | |
void | print_error_str (const Printer &printer, const ErrorCode err) |
Create a human-readable error string. More... | |
ErrorCode | read_data_block (uint32_t address, const BlockStorage::Buffer *buffer) const |
ErrorCode | reload_buffer (const BlockStorage::Buffer *buffer) const |
Use a buffer's metadata to determine the address and read data from the storage device into memory. More... | |
ErrorCode | write_data_block (uint32_t address, const BlockStorage::Buffer *buffer) const |
ErrorCode | flush (Buffer *buffer) const |
Flush the contents of a buffer and mark as unmodified. More... | |
uint8_t | get_byte (const uint16_t offset, const uint8_t *buf) const |
Read a byte from a buffer. More... | |
void | write_byte (const uint16_t offset, uint8_t buf[], const uint8_t value) const |
Write a byte to a buffer. More... | |
Static Public Member Functions | |
static void | print_block (const Printer &printer, const Buffer &buffer, const size_t words=512, const uint8_t wordsPerLine=16) |
Print the formatted contents of a buffer. More... | |
static void | print_block (const Printer &printer, const uint8_t data[], const size_t words=512, const uint8_t wordsPerLine=16) |
Public Attributes | |
__pad0__: m_spi(spi) { Pin::Mask pins[4] | |
Use the default SPI instance and pins for connecting to the SD card. More... | |
A simple SD driver communicating over the SPI protocol.
When using PropWare's default SPI class, this allows the entire SPI/SD card/FAT functionality to run in a single cog.
Error codes - preceded by SPI
Enumerator | |
---|---|
NO_ERROR |
No error |
BEG_ERROR |
First SD error code |
INVALID_CMD |
SD Error 0 |
READ_TIMEOUT |
SD Error 1 |
INVALID_NUM_BYTES |
SD Error 2 |
INVALID_RESPONSE |
SD Error 3 |
INVALID_INIT |
SD Error 4 |
INVALID_DAT_START_ID |
SD Error 5 |
CMD8_FAILURE |
SD Error 6 |
END_ERROR |
Last SD error code |
PropWare::SD::SD | ( | SPI * | spi, |
const Port::Mask | mosi, | ||
const Port::Mask | miso, | ||
const Port::Mask | sclk, | ||
const Port::Mask | cs | ||
) |
Construct an SD object with the given SPI parameters.
[in] | *spi | Address of a SPI instance. Its clock frequency and mode will be modified to fit the SD card's needs |
[in] | mosi | Pin mask for data line leaving the Propeller |
[in] | miso | Pin mask for data line going in to the Propeller |
[in] | sclk | Pin mask for clock line |
[in] | cs | Pin mask for chip select |
Definition at line 110 of file sd.h.
Flush the contents of a buffer and mark as unmodified.
Definition at line 208 of file blockstorage.h.
|
inherited |
Read a byte from a buffer.
[in] | offset | Offset from the beginning of the buffer |
[in] | *buf | Address of the buffer to read |
Definition at line 226 of file blockstorage.h.
|
virtual |
Read four bytes from a buffer.
Devices such as SD cards use reverse byte order compared with the Propeller - this method should be implemented to ensure that the returned value is reversed if necessary. The user of this function should not need to worry about reversing bytes
[in] | offset | Where in the buffer should the value be retrieved |
[in] | buf[] | Address of the buffer to read |
Implements PropWare::BlockStorage.
|
virtual |
Return the size of a sector (also known as a "block") for the given storage device.
Implements PropWare::BlockStorage.
|
virtual |
Determine the number of shifts required to multiply or divide a number by the sector size.
Because the Propeller does not have a hardware multiply/divide instruction, having the log_2(SECTOR_SIZE
) can be helpful when you need to multiply or divide a number the sector size. Simply invoke this function and then shift left or right by the return value.
SECTOR_SIZE
) Implements PropWare::BlockStorage.
|
virtual |
Read two bytes from a buffer.
Devices such as SD cards use reverse byte order compared with the Propeller - this method should be implemented to ensure that the returned value is reversed if necessary. The user of this function should not need to worry about reversing bytes
[in] | offset | Where in the buffer should the value be retrieved |
[in] | buf[] | Address of the buffer to read |
Implements PropWare::BlockStorage.
|
staticinherited |
Print the formatted contents of a buffer.
[in] | printer | Output console |
[in] | buffer | Data in this container will be printed |
[in] | words | Bytes in the buffer that should be printed |
[in] | wordsPerLine | Bytes to be printed before a newline |
Definition at line 85 of file blockstorage.h.
|
staticinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
[in] | printer | Output console |
[in] | data[] | Array with data to be printed |
[in] | words | Bytes in the buffer that should be printed |
[in] | wordsPerLine | Bytes to be printed before a newline |
Definition at line 101 of file blockstorage.h.
|
virtual |
Read a block of data from the device into RAM.
[in] | address | Address of the block on the storage device |
[out] | buf[] | Location in memory to store the block |
Special error handling is needed to ensure that, if an error is thrown, chip select is set high again before returning the error
Implements PropWare::BlockStorage.
Definition at line 157 of file sd.h.
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
[in] | address | Address of the block on the storage device |
[out] | *buffer | Address of the buffer to store data in |
Definition at line 160 of file blockstorage.h.
|
inherited |
Use a buffer's metadata to determine the address and read data from the storage device into memory.
[in] | *buffer | Address of a buffer |
Definition at line 175 of file blockstorage.h.
|
virtual |
Initialize SD card communication over SPI for 3.3V configuration.
Implements PropWare::BlockStorage.
Definition at line 127 of file sd.h.
|
inherited |
Write a byte to a buffer.
[in] | offset | Address of the buffer to modify |
[in] | buf[] | Buffer to modify |
[in] | value | Value to be written in the buffer |
Definition at line 265 of file blockstorage.h.
|
virtual |
Write data to a storage device.
[in] | address | Block address on the storage device |
[in] | dat | Array of data to be written |
Implements PropWare::BlockStorage.
Definition at line 177 of file sd.h.
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
[in] | address | Block address on the storage device |
[in] | *buffer | Address of the buffer that has data to be written |
Definition at line 197 of file blockstorage.h.
|
virtual |
Write four bytes to a buffer.
Devices such as SD cards use reverse byte order compared with the Propeller - this method should be implemented to ensure that the parameter is reversed if necessary. The user of this function should not need to worry about reversing bytes
[in] | offset | Address of the buffer to modify |
[in] | buf[] | Buffer to modify |
[in] | value | Value to be written in the buffer |
Implements PropWare::BlockStorage.
|
virtual |
Write two bytes to a buffer.
Devices such as SD cards use reverse byte order compared with the Propeller - this method should be implemented to ensure that the parameter is reversed if necessary. The user of this function should not need to worry about reversing bytes
[in] | offset | Address of the buffer to modify |
[in] | buf[] | Buffer to modify |
[in] | value | Value to be written in the buffer |
Implements PropWare::BlockStorage.
PropWare::SD::__pad0__ |
Use the default SPI instance and pins for connecting to the SD card.
If a board configuration file has been predefined in PropGCC (such as dna.cfg
for the Propeller DNA board), then an instance of PropWare::SD
can be constructed without any arguments. This is very convenient for anyone using a common Propeller board that comes pre-equipped with an SD card adapter.