#ifndef _SPI_H
#define _SPI_H
#include <avr/io.h>
#define LSB = 0;
#define MSB = 1;
//Defining SPI pins configuration ATMEGA 644P
#define SPI_SS 0x10;
#define SPI_MOSI 0x20;
#define SPI_MISO 0x00;
#define SPI_SCK 0x80;
#define SPI_PORT PORTB
#define PIN_SPI PINB
#define BIT_SPI_MISO 3
//Defining SPI function configuration
#define SPI_CLCK_Div4 0x00;
#define SPI_CLCK_Div16 0x01;
#define SPI_ClCK_Div64 0x02;
#define SPI_CLCK_Div128 0x03;
#define SPI_CLCK_Div2 0x04;
#define SPI_CLCK_Div8 0x05;
#define SPI_CLCK_Div32 0x06;
#define SPI_Mode0 0x00;
#define SPI_Mode1 0x04;
#define SPI_Mode2 0x08;
#define SPI_Mode3 0x0C;
#define SPI_MODE_MASK 0x0C;
#define SPI_CLOCK_MASK 0x03;
#define SPI_2XCLOCK_MASK 0X01;
#define WAIT_SPI() while(!(SPSR & _BV(SPIF)))
class SPI
{
public:
void init();
uint8_t send(uint8_t value);
inline static uint8_t transfer(uint8_t _data, uint8_t mode);
// SPI Configuration methods
inline static void attachInterrupt();
inline static void detachInterrupt(); // Default
static void end();
static void setBitOrder(uint8_t);
static void setDataMode(uint8_t);
static uint8_t getDataMode();
static void setClockDivider(uint8_t);
};
extern SPI objSPI;
uint8_t SPI::transfer(uint8_t _data, uint8_t mode)
{
uint8_t ret,prevmode;
prevmode = getDataMode();
setDataMode(mode);
SPDR = _data;
while (!(SPSR & _BV(SPIF)))
;
ret = SPDR;
setDataMode(prevmode);
return ret;
}
#endif