No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

87 líneas
2.6KB

  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_NATIVE_AVR_SPI
  22. //------------------------------------------------------------------------------
  23. void SdSpi::begin() {
  24. // set SS high - may be chip select for another SPI device
  25. digitalWrite(SS, HIGH);
  26. // SS must be in output mode even it is not chip select
  27. pinMode(SS, OUTPUT);
  28. pinMode(MISO, INPUT);
  29. pinMode(MOSI, OUTPUT);
  30. pinMode(SCK, OUTPUT);
  31. }
  32. //------------------------------------------------------------------------------
  33. void SdSpi::init(uint8_t sckDivisor) {
  34. uint8_t r = 0;
  35. for (uint8_t b = 2; sckDivisor > b && r < 6; b <<= 1, r++) {}
  36. // See avr processor documentation
  37. SPCR = (1 << SPE) | (1 << MSTR) | (r >> 1);
  38. SPSR = r & 1 || r == 6 ? 0 : 1 << SPI2X;
  39. }
  40. #if !USE_AVR_NATIVE_SPI_INLINE
  41. //------------------------------------------------------------------------------
  42. uint8_t SdSpi::receive() {
  43. SPDR = 0XFF;
  44. while (!(SPSR & (1 << SPIF))) {}
  45. return SPDR;
  46. }
  47. //------------------------------------------------------------------------------
  48. uint8_t SdSpi::receive(uint8_t* buf, size_t n) {
  49. if (n-- == 0) return 0;
  50. SPDR = 0XFF;
  51. for (size_t i = 0; i < n; i++) {
  52. while (!(SPSR & (1 << SPIF))) {}
  53. uint8_t b = SPDR;
  54. SPDR = 0XFF;
  55. buf[i] = b;
  56. }
  57. while (!(SPSR & (1 << SPIF))) {}
  58. buf[n] = SPDR;
  59. return 0;
  60. }
  61. //------------------------------------------------------------------------------
  62. void SdSpi::send(uint8_t data) {
  63. SPDR = data;
  64. while (!(SPSR & (1 << SPIF))) {}
  65. }
  66. //------------------------------------------------------------------------------
  67. void SdSpi::send(const uint8_t* buf , size_t n) {
  68. if (n == 0) return;
  69. SPDR = buf[0];
  70. if (n > 1) {
  71. uint8_t b = buf[1];
  72. size_t i = 2;
  73. while (1) {
  74. while (!(SPSR & (1 << SPIF))) {}
  75. SPDR = b;
  76. if (i == n) break;
  77. b = buf[i++];
  78. }
  79. }
  80. while (!(SPSR & (1 << SPIF))) {}
  81. }
  82. #endif // !USE_AVR_NATIVE_SPI_INLINE
  83. #endif // USE_NATIVE_AVR_SPI