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.

71 satır
2.4KB

  1. /* Arduino SdSpi Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the Arduino SdSpi Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdSpi Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <SdSpi.h>
  21. #if USE_ARDUINO_SPI_LIBRARY
  22. #include <SPI.h>
  23. //------------------------------------------------------------------------------
  24. void SdSpi::begin() {
  25. SPI.begin();
  26. }
  27. //------------------------------------------------------------------------------
  28. void SdSpi::init(uint8_t sckDivisor) {
  29. SPI.setBitOrder(MSBFIRST);
  30. SPI.setDataMode(SPI_MODE0);
  31. #ifndef SPI_CLOCK_DIV128
  32. SPI.setClockDivider(sckDivisor);
  33. #else // SPI_CLOCK_DIV128
  34. int v;
  35. if (sckDivisor <= 2) v = SPI_CLOCK_DIV2;
  36. else if (sckDivisor <= 4) v = SPI_CLOCK_DIV4;
  37. else if (sckDivisor <= 8) v = SPI_CLOCK_DIV8;
  38. else if (sckDivisor <= 16) v = SPI_CLOCK_DIV16;
  39. else if (sckDivisor <= 32) v = SPI_CLOCK_DIV32;
  40. else if (sckDivisor <= 64) v = SPI_CLOCK_DIV64;
  41. else v = SPI_CLOCK_DIV128;
  42. SPI.setClockDivider(v);
  43. #endif // SPI_CLOCK_DIV128
  44. }
  45. //------------------------------------------------------------------------------
  46. /** SPI receive a byte */
  47. uint8_t SdSpi::receive() {
  48. return SPI.transfer(0XFF);
  49. }
  50. //------------------------------------------------------------------------------
  51. /** SPI receive multiple bytes */
  52. uint8_t SdSpi::receive(uint8_t* buf, size_t n) {
  53. for (size_t i = 0; i < n; i++) {
  54. buf[i] = SPI.transfer(0XFF);
  55. }
  56. return 0;
  57. }
  58. //------------------------------------------------------------------------------
  59. /** SPI send a byte */
  60. void SdSpi::send(uint8_t b) {
  61. SPI.transfer(b);
  62. }
  63. //------------------------------------------------------------------------------
  64. /** SPI send multiple bytes */
  65. void SdSpi::send(const uint8_t* buf , size_t n) {
  66. for (size_t i = 0; i < n; i++) {
  67. SPI.transfer(buf[i]);
  68. }
  69. }
  70. #endif // USE_ARDUINO_SPI_LIBRARY