PropWare
C++ objects and CMake build system for Parallax Propeller
|
SPI serial communications library; Core functionality comes from a dedicated assembly cog. More...
#include <spi.h>
Public Types | |
enum | Mode { MODE_0, MODE_1, MODE_2, MODE_3 } |
Descriptor for SPI signal as defined by Motorola modes. More... | |
enum | BitMode { LSB_FIRST = MODE_3 + 1, MSB_FIRST } |
Determine if data is communicated with the LSB or MSB sent/received first. More... | |
enum | ErrorCode { NO_ERROR = 0, BEG_ERROR = 1, INVALID_FREQ = BEG_ERROR, END_ERROR = INVALID_FREQ } |
Public Member Functions | |
SPI (const Port::Mask mosi=PropWare::Port::NULL_PIN, const Port::Mask miso=PropWare::Port::NULL_PIN, const Port::Mask sclk=PropWare::Port::NULL_PIN, const int32_t frequency=DEFAULT_FREQUENCY, const Mode mode=MODE_0, const BitMode bitmode=MSB_FIRST) | |
Construct an SPI bus on the given pins with the given settings. More... | |
~SPI () | |
Release the pins to floating inputs. | |
void | set_mosi (const Port::Mask mask) |
Release the current MOSI pin as a floating input and set the new one as output. | |
void | set_miso (const Port::Mask mask) |
Set the new pin as input. | |
void | set_sclk (const Port::Mask mask) |
Release the current SCLK pin as a floating input and set the new one as output. | |
void | set_mode (const Mode mode) |
Set the mode of SPI communication. More... | |
void | set_bit_mode (const BitMode bitmode) |
Set the bitmode of SPI communication. More... | |
PropWare::ErrorCode | set_clock (const int32_t frequency) |
Change the SPI module's clock frequency. More... | |
int32_t | get_clock () const |
Retrieve the SPI module's clock frequency. More... | |
void | shift_out (uint8_t bits, uint32_t value) const |
Send a value out to a peripheral device. More... | |
uint32_t | shift_in (const unsigned int bits) const |
Read a value from the MISO line. More... | |
void | shift_out_block_msb_first_fast (const uint8_t buffer[], size_t numberOfBytes) |
Receive an array of data at max transmit speed. Mode is always MODE_0 and data is always MSB first. More... | |
void | shift_in_block_mode0_msb_first_fast (uint8_t *buffer, size_t numberOfBytes) |
Receive an array of data at max transmit speed. Mode is always MODE_0 and data is always MSB first. More... | |
void | put_char (const char c) |
Print a single character. More... | |
void | puts (const char string[]) |
Send a null-terminated character array. Though this method could be created using put_char, some objects (such as PropWare::UART), have optimized methods for sending a string and PrintCapable::puts can utilize them. More... | |
char | get_char () |
Read and return a single character. Whether the method is blocking or not depends entirely on the implementation. | |
void | print_error_str (const Printer *printer, const ErrorCode err) const |
Print through UART an error string followed by entering an infinite loop. More... | |
Static Public Member Functions | |
static SPI * | get_instance () |
Best way to use SPI is through here, where you can get a shared instance of the SPI module (not thread safe) More... | |
Static Public Attributes | |
static const int32_t | DEFAULT_FREQUENCY = 100000 |
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Generally, multiple instances of the SPI class are not desired. To avoid the programmer from accidentally creating multiple instances, this class is set up as a singleton. A static instance can be retrieved with get_instance(). If multiple instances of PropWare::SPI are desired, the PropWare library (and your project) should be built from source with PROPWARE_NO_SAFE_SPI defined
enum PropWare::SPI::Mode |
PropWare::SPI::SPI | ( | const Port::Mask | mosi = PropWare::Port::NULL_PIN , |
const Port::Mask | miso = PropWare::Port::NULL_PIN , |
||
const Port::Mask | sclk = PropWare::Port::NULL_PIN , |
||
const int32_t | frequency = DEFAULT_FREQUENCY , |
||
const Mode | mode = MODE_0 , |
||
const BitMode | bitmode = MSB_FIRST |
||
) |
Construct an SPI bus on the given pins with the given settings.
[in] | mosi | Pin mask for MOSI |
[in] | miso | Pin mask for MISO |
[in] | sclk | Pin mask for SCLK |
[in] | frequency | Frequency to run the bus, in hertz |
[in] | mode | Determines clock phase and polarity |
[in] | bitmode | Determine if the MSB or LSB should be clocked out first |
Definition at line 120 of file spi.h.
int32_t PropWare::SPI::get_clock | ( | ) | const |
|
static |
Best way to use SPI is through here, where you can get a shared instance of the SPI module (not thread safe)
Print through UART an error string followed by entering an infinite loop.
[in] | *printer | Object used for printing error string |
[in] | err | Error number used to determine error string |
Definition at line 391 of file spi.h.
|
virtual |
Print a single character.
[in] | c | Individual char to be printed |
Implements PropWare::PrintCapable.
Definition at line 370 of file spi.h.
|
virtual |
Send a null-terminated character array. Though this method could be created using put_char, some objects (such as PropWare::UART), have optimized methods for sending a string and PrintCapable::puts can utilize them.
string[]
must be terminated with a null terminator[in] | string[] | Array of data words with the final word being 0 - the null terminator |
Implements PropWare::PrintCapable.
Definition at line 374 of file spi.h.
void PropWare::SPI::set_bit_mode | ( | const BitMode | bitmode | ) |
Set the bitmode of SPI communication.
[in] | bitmode | Select one of LSB_FIRST or MSB_FIRST to choose which bit will be shifted out first |
Definition at line 185 of file spi.h.
PropWare::ErrorCode PropWare::SPI::set_clock | ( | const int32_t | frequency | ) |
Change the SPI module's clock frequency.
[in] | frequency | Frequency, in Hz, to run the SPI clock; Must be less than CLKFREQ/4 (for 80 MHz, 900 kHz is the fastest I've tested successfully) |
Definition at line 197 of file spi.h.
void PropWare::SPI::set_mode | ( | const Mode | mode | ) |
Set the mode of SPI communication.
[in] | mode | Sets the SPI mode to one MODE_0, MODE_1, MODE_2, or MODE_3 |
Definition at line 168 of file spi.h.
uint32_t PropWare::SPI::shift_in | ( | const unsigned int | bits | ) | const |
Read a value from the MISO line.
[in] | bits | Number of bits to read |
Definition at line 244 of file spi.h.
void PropWare::SPI::shift_in_block_mode0_msb_first_fast | ( | uint8_t * | buffer, |
size_t | numberOfBytes | ||
) |
void PropWare::SPI::shift_out | ( | uint8_t | bits, |
uint32_t | value | ||
) | const |
Send a value out to a peripheral device.
Pass a value and mode into the assembly cog to be sent to the peripheral; NOTE: this function is non-blocking and chip-select should not be set inactive immediately after the return (you should call spi_wait() before setting chip-select inactive)
[in] | bits | Number of bits to be shifted out |
[in] | value | The value to be shifted out |
Definition at line 226 of file spi.h.
void PropWare::SPI::shift_out_block_msb_first_fast | ( | const uint8_t | buffer[], |
size_t | numberOfBytes | ||
) |