You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 line
2.2KB

  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. #include "SPI.h"
  11. #include "pins_arduino.h"
  12. SPIClass SPI;
  13. uint8_t SPIClass::interruptMode = 0;
  14. uint8_t SPIClass::interruptMask = 0;
  15. uint8_t SPIClass::interruptSave = 0;
  16. void SPIClass::begin()
  17. {
  18. // Set SS to high so a connected chip will be "deselected" by default
  19. digitalWrite(SS, HIGH);
  20. // When the SS pin is set as OUTPUT, it can be used as
  21. // a general purpose output port (it doesn't influence
  22. // SPI operations).
  23. pinMode(SS, OUTPUT);
  24. // Warning: if the SS pin ever becomes a LOW INPUT then SPI
  25. // automatically switches to Slave, so the data direction of
  26. // the SS pin MUST be kept as OUTPUT.
  27. SPCR |= _BV(MSTR);
  28. SPCR |= _BV(SPE);
  29. // Set direction register for SCK and MOSI pin.
  30. // MISO pin automatically overrides to INPUT.
  31. // By doing this AFTER enabling SPI, we avoid accidentally
  32. // clocking in a single bit since the lines go directly
  33. // from "input" to SPI control.
  34. // http://code.google.com/p/arduino/issues/detail?id=888
  35. #ifdef __AVR__
  36. pinMode(SCK, OUTPUT);
  37. pinMode(MOSI, OUTPUT);
  38. #endif
  39. }
  40. void SPIClass::end() {
  41. SPCR &= ~_BV(SPE);
  42. }
  43. void SPIClass::usingInterrupt(uint8_t interruptNumber)
  44. {
  45. uint8_t mask;
  46. if (interruptMode > 1) return;
  47. noInterrupts();
  48. switch (interruptNumber) {
  49. #ifdef SPI_INT0_MASK
  50. case 0: mask = SPI_INT0_MASK; break;
  51. #endif
  52. #ifdef SPI_INT1_MASK
  53. case 1: mask = SPI_INT1_MASK; break;
  54. #endif
  55. #ifdef SPI_INT2_MASK
  56. case 2: mask = SPI_INT2_MASK; break;
  57. #endif
  58. #ifdef SPI_INT3_MASK
  59. case 3: mask = SPI_INT3_MASK; break;
  60. #endif
  61. #ifdef SPI_INT4_MASK
  62. case 4: mask = SPI_INT4_MASK; break;
  63. #endif
  64. #ifdef SPI_INT5_MASK
  65. case 5: mask = SPI_INT5_MASK; break;
  66. #endif
  67. #ifdef SPI_INT6_MASK
  68. case 6: mask = SPI_INT6_MASK; break;
  69. #endif
  70. #ifdef SPI_INT7_MASK
  71. case 7: mask = SPI_INT7_MASK; break;
  72. #endif
  73. default:
  74. interruptMode = 2;
  75. interrupts();
  76. return;
  77. }
  78. interruptMode = 1;
  79. interruptMask |= mask;
  80. interrupts();
  81. }