Wiblocks --- SPI

SPI/SPI.cpp

00001 #include "SPI.h"
00002 
00003 extern "C" {
00004   #include <inttypes.h>
00005   #include "WConstants.h"
00006 }
00007 
00008 
00032 
00033 SPI::SPI(unsigned char timeout, 
00034          unsigned char mosi_pin, 
00035          unsigned char miso_pin, 
00036          unsigned char sck_pin) {
00037   _timeout = timeout;
00038   _mosi_pin = mosi_pin;
00039   _miso_pin = miso_pin;
00040   _sck_pin = sck_pin;
00041 };
00042 
00050 
00051 void SPI::init(unsigned char spcr, unsigned char spsr) {
00052   digitalWrite(_mosi_pin, LOW);
00053   digitalWrite(_sck_pin, LOW);
00054   pinMode(_mosi_pin, OUTPUT);
00055   pinMode(_sck_pin,  OUTPUT);
00056   pinMode(_miso_pin, INPUT);
00057   SPCR = spcr;
00058   SPSR = spsr;
00059 };
00060 
00061 
00068 
00069 char SPI::write(unsigned char *data, unsigned char num_chars) {
00070   unsigned char *p;
00071   unsigned char i = 0;
00072   unsigned char count = 0;
00073   p = data;
00074   for ( i = 0; i < num_chars; i++) {
00075     SPDR = *p++;   
00076     count = 0;
00077     while (1) {
00078       if (SPSR & (1<<SPIF)) break; // transmission complete
00079       delay(1);
00080       //      if (++count == _timeout) return(-1);
00081       if (++count == 10) return(-1);
00082     }
00083   }
00084   return(0);
00085 };
00086 
00096 char SPI::read(unsigned char *data, unsigned char max_num_chars) {
00097   return(-1);
00098 };
00099