00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _SPI_H_INCLUDED
00012 #define _SPI_H_INCLUDED
00013
00014 #include <stdio.h>
00015 #include <WProgram.h>
00016 #include <avr/pgmspace.h>
00017
00018 #define SPI_CLOCK_DIV4 0x00
00019 #define SPI_CLOCK_DIV16 0x01
00020 #define SPI_CLOCK_DIV64 0x02
00021 #define SPI_CLOCK_DIV128 0x03
00022 #define SPI_CLOCK_DIV2 0x04
00023 #define SPI_CLOCK_DIV8 0x05
00024 #define SPI_CLOCK_DIV32 0x06
00025 #define SPI_CLOCK_DIV64 0x07
00026
00027 #define SPI_MODE0 0x00
00028 #define SPI_MODE1 0x04
00029 #define SPI_MODE2 0x08
00030 #define SPI_MODE3 0x0C
00031
00032 #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
00033 #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
00034 #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
00035
00036 class SPIClass {
00037 public:
00038 inline static byte transfer(byte _data);
00039
00040
00041
00042 inline static void attachInterrupt();
00043 inline static void detachInterrupt();
00044
00045 static void begin();
00046 static void end();
00047
00048 static void setBitOrder(uint8_t);
00049 static void setDataMode(uint8_t);
00050 static void setClockDivider(uint8_t);
00051 };
00052
00053 extern SPIClass SPI;
00054
00055 byte SPIClass::transfer(byte _data) {
00056 SPDR = _data;
00057 while (!(SPSR & _BV(SPIF)))
00058 ;
00059 return SPDR;
00060 }
00061
00062 void SPIClass::attachInterrupt() {
00063 SPCR |= _BV(SPIE);
00064 }
00065
00066 void SPIClass::detachInterrupt() {
00067 SPCR &= ~_BV(SPIE);
00068 }
00069
00070 #endif