Instant SoC Class Library  3.1
FC_IO_SPI Class Reference

SPI Master. More...

Inheritance diagram for FC_IO_SPI:
FC_Base

Public Member Functions

 FC_IO_SPI (int bitrate, int mode, int fifo_depth=4)
 
int WaitReady ()
 
bool IsReady ()
 
bool StartWrite (const U8 *pD, int nD)
 
bool Read (U8 *pD, int nD)
 
bool WriteRead (const U8 *pWD, U8 *pRD, int nD)
 
U32 GetNumValid ()
 
U32 GetNumReady ()
 
void ResetReadFIFO ()
 

Detailed Description

SPI Master.

Adds to interface:
<name>_SCLK : out std_logic
<name>_MOSI : out std_logic
<name>_MISO : in std_logic
<name>_SSn : out std_logic
where <name> is c++ object name.
Any number of instances are allowed.

#include <stdlib.h>
#include <math.h>
#include "fc_io.h"
#include "fc_system.h"
// Function to stream an int
template<class T>
T& operator<<(T& os, int v)
{
char b[16];
itoa(v,b,10);
os << b;
return os;
}
short GetAccel(FC_IO_SPI& spi, U8 reg)
{
U8 buff[4] = {0x0B, reg, 0x00, 0x00 }; // 0x0B Read command
spi.WriteRead( buff, buff, 4 );
return buff[3]<<8|buff[2];
}
int main(void)
{
//% hw_begin
FC_IO_Clk Clk( 100 );
FC_IO_UART_TX uart_tx( 115200, 64 );
FC_IO_SPI accel( 1000000, 0, 8 );
//% hw_end
// Start ADXL362 measure
U8 spi_command[3] = { 0x0A, 0x2D, 0x02 };
// 0x0A Write
// 0x2D POWER_CTL reg
// 0x02 Measurement Mode
accel.StartWrite( spi_command, 3 );
accel.WaitReady();
for(;;) {
timer.Sleep( 100, TU_ms );
// Read accelorometer
float x = GetAccel(accel,0x0E);
float y = GetAccel(accel,0x10);
float z = GetAccel(accel,0x12);
// Calculate roll and pitch angles and coverts from radians to deg angles*10 (180/pi)
int roll = atanf( x/sqrtf(y*y+z*z) ) * 573.0f;
int pitch = atanf( y/sqrtf(x*x+z*z) ) * 573.0f;
// Print roll and pitch angles with one decimal
uart_tx << "Roll: " << roll/10 << "." << abs(roll%10);
uart_tx << " \tPitch: " << pitch/10 << "." << abs(pitch%10) << "\r\n";
}
}
FC_IO_Clk
System clock input.
Definition: fc_io.h:55
FC_IO_UART_TX
UART Transmitter.
Definition: fc_io.h:348
FC_IO_SPI
SPI Master.
Definition: fc_io.h:480
FC_System_Timer
Timer.
Definition: fc_system.h:78