sd_card_spi.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Driver and interface for accessing SD card in SPI mode
  22. #include "BlueSCSI_platform.h"
  23. #include "BlueSCSI_log.h"
  24. #include <hardware/spi.h>
  25. #include <SdFat.h>
  26. #ifndef SD_USE_SDIO
  27. class RP2040SPIDriver : public SdSpiBaseClass
  28. {
  29. public:
  30. void begin(SdSpiConfig config) {
  31. }
  32. void activate() {
  33. _spi_init(SD_SPI, m_sckfreq);
  34. spi_set_format(SD_SPI, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
  35. }
  36. void deactivate() {
  37. }
  38. void wait_idle() {
  39. while (!(spi_get_hw(SD_SPI)->sr & SPI_SSPSR_TFE_BITS));
  40. while (spi_get_hw(SD_SPI)->sr & SPI_SSPSR_BSY_BITS);
  41. }
  42. // Single byte receive
  43. uint8_t receive() {
  44. uint8_t tx = 0xFF;
  45. uint8_t rx;
  46. spi_write_read_blocking(SD_SPI, &tx, &rx, 1);
  47. return rx;
  48. }
  49. // Single byte send
  50. void send(uint8_t data) {
  51. spi_write_blocking(SD_SPI, &data, 1);
  52. wait_idle();
  53. }
  54. // Multiple byte receive
  55. uint8_t receive(uint8_t* buf, size_t count)
  56. {
  57. spi_read_blocking(SD_SPI, 0xFF, buf, count);
  58. return 0;
  59. }
  60. // Multiple byte send
  61. void send(const uint8_t* buf, size_t count) {
  62. spi_write_blocking(SD_SPI, buf, count);
  63. }
  64. void setSckSpeed(uint32_t maxSck) {
  65. m_sckfreq = maxSck;
  66. }
  67. private:
  68. uint32_t m_sckfreq;
  69. };
  70. void sdCsInit(SdCsPin_t pin)
  71. {
  72. }
  73. void sdCsWrite(SdCsPin_t pin, bool level)
  74. {
  75. if (level)
  76. sio_hw->gpio_set = (1 << SD_SPI_CS);
  77. else
  78. sio_hw->gpio_clr = (1 << SD_SPI_CS);
  79. }
  80. RP2040SPIDriver g_sd_spi_port;
  81. SdSpiConfig g_sd_spi_config(0, DEDICATED_SPI, SD_SCK_MHZ(25), &g_sd_spi_port);
  82. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  83. {
  84. }
  85. #endif