sdio.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // SD card access using SDIO for RP2040 platform.
  22. // This module contains the low-level SDIO bus implementation using
  23. // the PIO peripheral. The high-level commands are in sd_card_sdio.cpp.
  24. #pragma once
  25. #include <BlueSCSI_platform.h>
  26. #if defined(SD_USE_SDIO) && !defined(SD_USE_RP2350_SDIO)
  27. #include <stdint.h>
  28. enum sdio_status_t {
  29. SDIO_OK = 0,
  30. SDIO_BUSY = 1,
  31. SDIO_ERR_RESPONSE_TIMEOUT = 2, // Timed out waiting for response from card
  32. SDIO_ERR_RESPONSE_CRC = 3, // Response CRC is wrong
  33. SDIO_ERR_RESPONSE_CODE = 4, // Response command code does not match what was sent
  34. SDIO_ERR_DATA_TIMEOUT = 5, // Timed out waiting for data block
  35. SDIO_ERR_DATA_CRC = 6, // CRC for data packet is wrong
  36. SDIO_ERR_WRITE_CRC = 7, // Card reports bad CRC for write
  37. SDIO_ERR_WRITE_FAIL = 8, // Card reports write failure
  38. };
  39. #define SDIO_BLOCK_SIZE 512
  40. #define SDIO_WORDS_PER_BLOCK 128
  41. // Execute a command that has 48-bit reply (response types R1, R6, R7)
  42. // If response is NULL, does not wait for reply.
  43. sdio_status_t rp2040_sdio_command_R1(uint8_t command, uint32_t arg, uint32_t *response);
  44. // Execute a command that has 136-bit reply (response type R2)
  45. // Response buffer should have space for 16 bytes (the 128 bit payload)
  46. sdio_status_t rp2040_sdio_command_R2(uint8_t command, uint32_t arg, uint8_t *response);
  47. // Execute a command that has 48-bit reply but without CRC (response R3)
  48. sdio_status_t rp2040_sdio_command_R3(uint8_t command, uint32_t arg, uint32_t *response);
  49. // Start transferring data from SD card to memory buffer
  50. // Transfer block size is always 512 bytes except for certain special commands
  51. sdio_status_t rp2040_sdio_rx_start(uint8_t *buffer, uint32_t num_blocks, uint32_t num_size = SDIO_BLOCK_SIZE);
  52. // Check if reception is complete
  53. // Returns SDIO_BUSY while transferring, SDIO_OK when done and error on failure.
  54. sdio_status_t rp2040_sdio_rx_poll(uint32_t *bytes_complete = nullptr);
  55. // Start transferring data from memory to SD card
  56. sdio_status_t rp2040_sdio_tx_start(const uint8_t *buffer, uint32_t num_blocks);
  57. // Check if transmission is complete
  58. sdio_status_t rp2040_sdio_tx_poll(uint32_t *bytes_complete = nullptr);
  59. // Force everything to idle state
  60. sdio_status_t rp2040_sdio_stop();
  61. // Receives the SD Status register. Does not return until the register has been received.
  62. sdio_status_t receive_status_register(uint8_t* sds);
  63. // (Re)initialize the SDIO interface
  64. void rp2040_sdio_init(int clock_divider = 1);
  65. #endif