SPI.h 1.6 KB

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