Arduino Project Libraries  0.00
Stock and random Arduino Libraries
Porting: SPI

Data Structures

class  SPI
 

Macros

#define SPI_SS_bm   (1<<4) /* SPI SS pin mask 4 */
 
#define SPI_MOSI_bm   (1<<5) /* SPI MOSI pin mask 5 */
 
#define SPI_MISO_bm   (1<<6) /* SPI MISO pin mask 6 */
 
#define SPI_SCK_bm   (1<<7) /* SPI SCK pin mask 7 */
 

Functions

 SPI ()
 
void begin (int busNo)
 
uint8_t transfer (uint8_t tx_)
 
void transfernb (char *tbuf, char *rbuf, uint32_t len)
 
void transfern (char *buf, uint32_t len)
 
virtual ~SPI ()
 
void init ()
 

Variables

string device
 
uint8_t mode
 
uint8_t bits
 
uint32_t speed
 
int fd
 

Detailed Description

Example of spi.h class declaration for SPI portability


Data Structure Documentation

◆ SPI

class SPI

Definition at line 41 of file spi.h.

Collaboration diagram for SPI:
Collaboration graph

Public Member Functions

 SPI ()
 
void begin (uint8_t port)
 
uint8_t transfer (uint8_t tx_)
 
void transfernb (char *tbuf, char *rbuf, uint32_t len)
 
void transfern (char *buf, uint32_t len)
 
virtual ~SPI ()
 
 SPI ()
 
void begin (int)
 
uint8_t transfer (uint8_t)
 
void transfernb (char *, char *, uint32_t)
 
void transfern (char *, const uint32_t)
 
virtual ~SPI ()
 

Private Member Functions

void init ()
 

Private Attributes

SPI_t * device
 
PORT_t * port
 
int fd
 
uint8_t msg [32+1]
 
uint8_t msgByte
 

Constructor & Destructor Documentation

◆ SPI() [1/2]

SPI::SPI ( )

SPI constructor

Definition at line 51 of file spi.cpp.

52 {
53 
54 }

◆ ~SPI() [1/2]

SPI::~SPI ( )
virtual

Definition at line 56 of file spi.cpp.

57 {
58 
59 }

◆ SPI() [2/2]

SPI::SPI ( )

SPI default constructor

◆ ~SPI() [2/2]

virtual SPI::~SPI ( )
virtual

SPI destructor

Member Function Documentation

◆ begin() [1/2]

void SPI::begin ( int  busNo)

Start SPI communication

Parameters
pinused for SPI

Definition at line 11 of file spi.cpp.

11  {
12  mspi = new mraa::Spi(busNo); // init mraa spi bus, it will handle chip select internally. For CS pin wiring user must check SPI details in hardware manual
13 
14  mspi->mode(mraa::SPI_MODE0);
15  mspi->bitPerWord(8);
16  mspi->frequency(8000000); // Prophet: this will try to set 8MHz, however MRAA will reset to max platform speed and syslog a message of it
17 }
#define SPI_MODE0

◆ begin() [2/2]

void SPI::begin ( uint8_t  port)

Start SPI

Parameters
portis the SPI port (XMEGA_SPI_PORT_C for SPI on PORTC, XMEGA_SPI_PORT_D on PORTD etc).

Definition at line 14 of file spi.cpp.

References init().

15 {
16  if (_port==XMEGA_SPI_PORT_C) // Select SPI on PORTC
17  {
18  device = &SPIC;
19  port = &PORTC;
20  }else if (_port==XMEGA_SPI_PORT_D) // Select SPI on PORTD
21  {
22  device = &SPID;
23  port = &PORTD;
24  }
25 
26  init();
27 }
void init()
Definition: spi.cpp:39
SPI_t * device
Definition: spi.h:81
PORT_t * port
Definition: spi.h:83
#define XMEGA_SPI_PORT_C
Definition: gpio_helper.h:77
#define XMEGA_SPI_PORT_D
Definition: gpio_helper.h:78
Here is the call graph for this function:

◆ init()

void SPI::init ( void  )
private

Definition at line 39 of file spi.cpp.

Referenced by begin().

40 {
41  port->DIRCLR = SPI_MISO_bm;
42  port->DIRSET = SPI_MOSI_bm | SPI_SCK_bm | SPI_SS_bm;
43 
44  //device->CTRL = 0;
45  device->CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV4_gc;
46  device->INTCTRL =0; //Disable interrupts
47 
48 }
#define SPI_MISO_bm
Definition: spi.h:34
SPI_t * device
Definition: spi.h:81
#define SPI_MOSI_bm
Definition: spi.h:33
PORT_t * port
Definition: spi.h:83
#define SPI_SS_bm
Definition: spi.h:32
#define SPI_SCK_bm
Definition: spi.h:35
Here is the caller graph for this function:

◆ transfer() [1/2]

uint8_t SPI::transfer ( uint8_t  )

Transfer a single byte of data

Parameters
txByte to send
Returns
Data returned via spi

◆ transfer() [2/2]

uint8_t SPI::transfer ( uint8_t  tx_)

Transfer a single byte

Parameters
tx_Byte to send
Returns
Data returned via spi

Definition at line 29 of file spi.cpp.

30 {
31  register8_t data;
32  device->DATA = tx_;
33  while(!(device->STATUS & (1<<7)));
34  data = device->DATA;
35  //PORTF.OUT = data;
36  return data;
37 }
uint8_t data[32]
SPI_t * device
Definition: spi.h:81

◆ transfern() [1/2]

void SPI::transfern ( char *  ,
const uint32_t   
)

Transfer a buffer of data without using an rx buffer

Parameters
bufPointer to a buffer of data
lenLength of the data

◆ transfern() [2/2]

void SPI::transfern ( char *  buf,
uint32_t  len 
)

Transfer a buffer of data without an rx buffer

Parameters
bufPointer to a buffer of data
lenLength of the data

Definition at line 58 of file spi.cpp.

59 {
60  printf("transfern(tx: %s)\n", buf);
61 
62  if(wiringPiSPIDataRW(RF24_SPI_CHANNEL, (uint8_t *)buf, len) < 0)
63  {
64  printf("transfern(): Cannot send data %s\n", strerror(errno));
65  fflush(stdout);
66  abort();
67  }
68 }
static uint16_t buf
Definition: talkie.cpp:54
#define printf
#define RF24_SPI_CHANNEL
Definition: spi.cpp:23

◆ transfernb() [1/2]

void SPI::transfernb ( char *  ,
char *  ,
uint32_t   
)

Transfer a buffer of data using rx and tx buffer

Parameters
tbufTransmit buffer
rbufReceive buffer
lenLength of the data

◆ transfernb() [2/2]

void SPI::transfernb ( char *  tbuf,
char *  rbuf,
uint32_t  len 
)

Transfer a buffer of data

Parameters
tbufTransmit buffer
rbufReceive buffer
lenLength of the data

Definition at line 133 of file spi.cpp.

References fd.

134 {
135  struct spi_ioc_transfer tr;
136  memset(&tr, 0, sizeof(tr));
137  tr.tx_buf = (unsigned long)tbuf;
138  tr.rx_buf = (unsigned long)rbuf;
139  tr.len = len;
140  tr.speed_hz = RF24_SPIDEV_SPEED;
141  tr.delay_usecs = 0;
142  tr.bits_per_word = RF24_SPIDEV_BITS;
143  tr.cs_change = 0;
144 
145  int ret;
146  ret = ioctl(this->fd, SPI_IOC_MESSAGE(1), &tr);
147  if (ret < 1)
148  {
149  perror("can't send spi message");
150  abort();
151  }
152 }
#define RF24_SPIDEV_BITS
Definition: spi.cpp:21
int fd
Definition: spi.h:63

Field Documentation

◆ device

SPI_t* SPI::device
private

Default SPI device

Definition at line 81 of file spi.h.

◆ fd

int SPI::fd
private

Definition at line 63 of file spi.h.

Referenced by transfernb().

◆ msg

uint8_t SPI::msg[32+1]
private

Definition at line 64 of file spi.h.

◆ msgByte

uint8_t SPI::msgByte
private

Definition at line 65 of file spi.h.

◆ port

PORT_t* SPI::port
private

Definition at line 83 of file spi.h.

Macro Definition Documentation

◆ SPI_MISO_bm

#define SPI_MISO_bm   (1<<6) /* SPI MISO pin mask 6 */

#include <RF24/utility/ATXMegaD3/spi.h>

Definition at line 34 of file spi.h.

◆ SPI_MOSI_bm

#define SPI_MOSI_bm   (1<<5) /* SPI MOSI pin mask 5 */

#include <RF24/utility/ATXMegaD3/spi.h>

Definition at line 33 of file spi.h.

◆ SPI_SCK_bm

#define SPI_SCK_bm   (1<<7) /* SPI SCK pin mask 7 */

#include <RF24/utility/ATXMegaD3/spi.h>

Definition at line 35 of file spi.h.

◆ SPI_SS_bm

#define SPI_SS_bm   (1<<4) /* SPI SS pin mask 4 */

#include <RF24/utility/ATXMegaD3/spi.h>

Definition at line 32 of file spi.h.

Function Documentation

◆ begin()

void begin ( int  busNo)

#include <RF24/utility/Template/spi.h>

Start SPI

◆ init()

void init ( )
private

◆ SPI()

SPI ( )

#include <RF24/utility/Template/spi.h>

SPI constructor

◆ transfer()

uint8_t transfer ( uint8_t  tx_)

#include <RF24/utility/Template/spi.h>

Transfer a single byte

Parameters
tx_Byte to send
Returns
Data returned via spi

◆ transfern()

void transfern ( char *  buf,
uint32_t  len 
)

#include <RF24/utility/Template/spi.h>

Transfer a buffer of data without an rx buffer

Parameters
bufPointer to a buffer of data
lenLength of the data

◆ transfernb()

void transfernb ( char *  tbuf,
char *  rbuf,
uint32_t  len 
)

#include <RF24/utility/Template/spi.h>

Transfer a buffer of data

Parameters
tbufTransmit buffer
rbufReceive buffer
lenLength of the data

◆ ~SPI()

virtual ~SPI ( )
virtual

Variable Documentation

◆ bits

uint8_t bits
private

#include <RF24/utility/Template/spi.h>

word size

Definition at line 72 of file spi.h.

◆ device

string device
private

#include <RF24/utility/Template/spi.h>

Default SPI device

Definition at line 68 of file spi.h.

◆ fd

int fd
private

#include <RF24/utility/Template/spi.h>

Definition at line 75 of file spi.h.

◆ mode

uint8_t mode
private

#include <RF24/utility/Template/spi.h>

SPI Mode set

Definition at line 70 of file spi.h.

◆ speed

uint32_t speed
private

#include <RF24/utility/Template/spi.h>

Set SPI speed

Definition at line 74 of file spi.h.