1d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen/* 2d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> 3d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * SPI Master library for arduino. 4d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * 5d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * This file is free software; you can redistribute it and/or modify 6d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * it under the terms of either the GNU General Public License version 2 7d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * or the GNU Lesser General Public License version 2.1, both as 8d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen * published by the Free Software Foundation. 9d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen */ 10d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 11d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#ifndef _SPI_H_INCLUDED 12d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define _SPI_H_INCLUDED 13d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 14d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#include <stdio.h> 15d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#include <WProgram.h> 16d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#include <avr/pgmspace.h> 17d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 18d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV4 0x00 19d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV16 0x01 20d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV64 0x02 21d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV128 0x03 22d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV2 0x04 23d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV8 0x05 24d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV32 0x06 25d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_DIV64 0x07 26d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 27d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_MODE0 0x00 28d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_MODE1 0x04 29d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_MODE2 0x08 30d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_MODE3 0x0C 31d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 32d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR 33d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR 34d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR 35d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 36d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenclass SPIClass { 37d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenpublic: 38d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen inline static byte transfer(byte _data); 39d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 40d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // SPI Configuration methods 41d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 42d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen inline static void attachInterrupt(); 43d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen inline static void detachInterrupt(); // Default 44d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 45d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen static void begin(); // Default 46d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen static void end(); 47d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 48d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen static void setBitOrder(uint8_t); 49d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen static void setDataMode(uint8_t); 50d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen static void setClockDivider(uint8_t); 51d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen}; 52d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 53d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenextern SPIClass SPI; 54d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 55d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenbyte SPIClass::transfer(byte _data) { 56d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen SPDR = _data; 57d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen while (!(SPSR & _BV(SPIF))) 58d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen ; 59d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return SPDR; 60d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 61d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 62d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid SPIClass::attachInterrupt() { 63d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen SPCR |= _BV(SPIE); 64d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 65d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 66d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid SPIClass::detachInterrupt() { 67d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen SPCR &= ~_BV(SPIE); 68d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 69d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 70d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#endif 71