SPISettings Class

SPISettings Class

Description
A class to set SPI parameters.

Syntax
class SPISettings

Members

Public Constructors 
SPISettings::SPISettingsCreate a SPISettings object and set SPI clock speed, bit order and data mode.

SPISettings::SPISettings

Description
Construct an object and configure SPI parameters — clock speed, bit order and data model to the preferred default value.

Syntax
SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode);

Parameters
clock: SPI clock speed, default is 4000000.
bitOrder: order of bit stream, MSB first or LSB first, default is MSBFIRST.
dataMode: There are a total of 4 modes. SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3. default is SPI_MODE0.

Returns
NA

Example Code
NA

Notes and Warnings
This class seldom used alone, it is always used with beginTransaction() as a parameter in SPIClass. “SPI.h” must be included to use the class function.

SPIClass Class

Description
A class of SPI implementation for Ameba.

Syntax
class SPIClass

Members

Public Constructors 
SPIClass::SPIClassConstructs an SPI object.
Public Methods 
SPIClass::transferTransfer data through SPI.
SPIClass::transfer16Transfer data of 16-bits through SPI.
SPIClass::beginTransactionSet slave select pin and SPI initial settings.
SPIClass::endTransactionStop SPI transaction.
SPIClass::beginAssociate each SPI pin to Ameba pin using ameba HAL APIs.
SPIClass::endStop SPI master mode.
SPIClass::setBitOrderSet bit order to either MSB first or LSB first.
SPIClass::setDataModeSet data mode.
SPIClass::setClockDividerSet to correct clock speed (no effect on Ameba).
SPIClass::setDefaultFrequencySet default SPI frequency.

SPIClass::SPIClass

Description
Construct an SPI object, create a pointer to the object, and assign “MOSI, MISO, CLK, and SS” to each pin on Ameba boards.

Syntax
SPIClass(void *pSpiObj, int mosi, int miso, int clk, int ss);

Parameters
pSpiObj: SPI pointer to the object
mosi: master out slave in
miso: master in slave out
clk: clock
ss: slave select

Returns
NA

Example Code
Example: PM25_on_ILI9341_TFT_LCD
(https://github.com/ambiot/amb1_arduino/blob/dev/Arduino_package/hardware/libraries/SPI/examples/PM25_on_ILI9341_TFT_LCD/PM25_on_ILI9341_TFT_LCD.ino)

Notes and Warnings
2 SPI objects are created in the library for 2 different hardware SPI on Ameba (if applicable), use “SPI” for first hardware SPI and “SPI1” for the second. “SPI.h” must be included to use the class function.

SPIClass::transfer

Description
Transfer data through SPI to the slave.

Syntax
byte transfer(byte _pin, uint8_t _data, SPITransferMode _mode = SPI_LAST);
byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST);
void transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST);
void transfer(void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST);

Parameters
_pin: Slave select pin
_data: Actual data being sent over
_mode: SPI transfer mode
_count: number of bytes of data
_buf: data buffer

Returns
This function returns void or “0” in the case of error and returns “d” if it successfully transfers data through SPI to slave.

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::transfer16

Description
Transfer data of 16-bits through SPI to the slave.

Syntax
uint16_t transfer16(byte _pin, uint16_t _data, SPITransferMode _mode = SPI_LAST);
uint16_t transfer16(uint16_t _data, SPITransferMode _mode = SPI_LAST);

Parameters
_pin: Slave select pin
_data: Actual data being sent over
_mode: SPI transfer mode

Returns
This function returns the data being transferred.

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::beginTransaction

Description
Set slave select pin and initialize SPI with default settings using SPISettings class.

Syntax
void beginTransaction(uint8_t pin, SPISettings settings);
void beginTransaction(SPISettings settings);

Parameters
pin: slave select pin
settings: an object of SPISettings class

Returns
NA

Example Code
NA

Notes and Warnings
Refer to SPISettings class for details of the initial settings.
“SPI.h” must be included to use the class function.

SPIClass::endTransaction

Description
Set slave select pin to 1 and stop SPI transaction.

Syntax
void endTransaction(void);

Parameters
NA

Returns
NA

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::begin

Description
Initialize SPI pins to pins on Ameba board, also set SPI format and frequency.

Syntax
void begin(void);
void begin(int ss);

Parameters
ss: slave select

Returns
NA

Example Code
NA

Notes and Warnings
This is a required method to use SPI on Ameba. “SPI.h” must be included to use the class function.

SPIClass::end

Description
Deinitialize/stop SPI master mode.

Syntax
void end(void);

Parameters
NA

Returns
NA

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::setBitOrder

Description
Set bit order to either MSB first or LSB first and set slave select pin.

Syntax
void setBitOrder(uint8_t _pin, BitOrder _bitOrder);
void setBitOrder(BitOrder _order);

Parameters
_pin: slave select
_bitOrder: bit order. Valid Value: MSB first or LSB first
_order: order. Valid Value: MSB first or LSB first

Returns
NA

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::setDataMode

Description
Set data mode. A total of 4 modes and set slave select pin.

Syntax
void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode);
void SPIClass::setDataMode(uint8_t _mode);

Parameters
_pin: slave select
_mode: SPI data modes. A total of 4 modes: SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3. (Default: SPI_MODE0)

Returns
NA

Example Code
NA

Notes and Warnings
“SPI.h” must be included to use the class function.

SPIClass::setClockDivider

Description
Set clock divider in order to get correct clock speed.

Syntax
void setClockDivider(uint8_t _pin, uint8_t _divider);
void setClockDivider(uint8_t _div);

Parameters
_pin: slave select pin
_divider: clock divider
_div: clock divider

Returns
NA

Example Code
NA

Notes and Warnings
This function does not affect the Ameba board. “SPI.h” must be included to use the class function.

SPIClass::setDefaultFrequency

Description
Set default SPI frequency.

Syntax
void setDefaultFrequency(int _frequency);

Parameters
_frequency: the default SPI frequency

Returns
NA

Example Code
Example: PM25_on_ILI9341_TFT_LCD
(https://github.com/ambiot/amb1_arduino/blob/dev/Arduino_package/hardware/libraries/SPI/examples/PM25_on_ILI9341_TFT_LCD/PM25_on_ILI9341_TFT_LCD.ino)

Notes and Warnings
Take note that defaultFrequency = _frequency. “SPI.h” must be included to use the class function.

Please confirm that QQ communication software is installed