Public Member Functions | List of all members
SPIClass Class Reference

SPI library (call with global object SPI) More...

#include <SPI.h>

Detailed Description

SPI library (call with global object SPI)


  1. (Optionally) call setPins() (see its documentation for default pins)
  2. Call begin()
  3. (Optionally) call setClockDivider() (default SPI_CLOCK_DIV2)
  4. (Optionally) call setBitOrder() (default MSBFIRST)
  5. (Optionally) call setDataMode() (default SPI_MODE0)
  6. Before starting any transfer the chip select pin must be driven low by the master
  7. To write to the slave, call transfer() with the byte to write
  8. To read from the slave, call transfer() with 0 and record the returned value
  9. Drive chip select high to end the transmission

Example: MPU-6000

Please also see the MPU6000 library.

#include <SPI.h>
// Connect to the chip select pin of the device
const int csPin = PC15;
// See setPins() for SCK, MISO, MOSI defaults
void setup() {
SPI.setPins(PA12, 6, PA13, 6, PF6, 5);
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
void loop() {
uint32_t tic = micros();
digitalWrite(csPin, LOW);
SPI.transfer(0x75 | 0x80);
uint8_t magic = SPI.transfer(0) & 0x7E;
digitalWrite(csPin, HIGH);
tic = micros() - tic;
Serial1 << "WHO_AM_I = 0x" << _HEX(magic) << ", took " << tic << "us.\n";

Public Member Functions

 SPIClass (SPI_TypeDef *SPIx)
void setPins (uint8_t SCK, uint8_t afSCK, uint8_t MISO, uint8_t afMISO, uint8_t MOSI, uint8_t afMOSI)
 Change default SCK, MISO and MOSI pins. More...
void begin ()
 Start SPI peripheral and configure pins.
void end ()
 Close SPI.
void setDataSize (uint16_t sz)
 Sets data size. More...
void setBitOrder (uint8_t ord)
 Sets the bit-order for data. More...
void setDataMode (uint8_t mode)
 Sets the SPI mode. More...
void setClockDivider (uint8_t div)
 Sets the clock divider (with respect to the system clock) More...
uint8_t transfer (uint8_t _data)
 Transfers one byte over the SPI bus, both sending and receiving. More...

Constructor & Destructor Documentation

SPIClass::SPIClass ( SPI_TypeDef *  SPIx)
Avik De avikd.nosp@m.e@gm.nosp@m.ail.c.nosp@m.om

This file is part of koduino https://github.com/avikde/koduino

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org/licenses/.

Member Function Documentation

void SPIClass::setBitOrder ( uint8_t  ord)

Sets the bit-order for data.

void SPIClass::setClockDivider ( uint8_t  div)

Sets the clock divider (with respect to the system clock)

divOne of SPI_CLOCK_DIV<x> where <x> can be 2, 4, 8, 16, 32, 64, 128, 256
void SPIClass::setDataMode ( uint8_t  mode)

Sets the SPI mode.

See the Wikipedia SPI page for details on these modes

modeOne of SPI_MODE<x>, where <x> can be 0, 1, 2, 3
void SPIClass::setDataSize ( uint16_t  sz)

Sets data size.

default is SPI_DataSize_8b

oneof SPI_DataSize_Xb where X is from 4..16
void SPIClass::setPins ( uint8_t  SCK,
uint8_t  afSCK,
uint8_t  MISO,
uint8_t  afMISO,
uint8_t  MOSI,
uint8_t  afMOSI 

Change default SCK, MISO and MOSI pins.

You will need to look at the datasheet to fill in the alternate function (AF) numbers

SCKDefault is PA5
MISODefault is PB4
MOSIDefault is PB5
uint8_t SPIClass::transfer ( uint8_t  _data)

Transfers one byte over the SPI bus, both sending and receiving.

Make sure you drive chip select low before calling this

_dataByte to send
Returns the byte received from the slave

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