rp2040_sdio.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SD card access using SDIO for RP2040 platform.
  2. // This module contains the low-level SDIO bus implementation using
  3. // the PIO peripheral. The high-level commands are in sd_card_sdio.cpp.
  4. #pragma once
  5. #include <stdint.h>
  6. enum sdio_status_t {
  7. SDIO_OK = 0,
  8. SDIO_BUSY = 1,
  9. SDIO_ERR_RESPONSE_TIMEOUT = 2, // Timed out waiting for response from card
  10. SDIO_ERR_RESPONSE_CRC = 3, // Response CRC is wrong
  11. SDIO_ERR_RESPONSE_CODE = 4, // Response command code does not match what was sent
  12. SDIO_ERR_DATA_TIMEOUT = 5, // Timed out waiting for data block
  13. SDIO_ERR_DATA_CRC = 6, // CRC for data packet is wrong
  14. SDIO_ERR_WRITE_CRC = 7, // Card reports bad CRC for write
  15. SDIO_ERR_WRITE_FAIL = 8, // Card reports write failure
  16. };
  17. #define SDIO_BLOCK_SIZE 512
  18. #define SDIO_WORDS_PER_BLOCK 128
  19. // Execute a command that has 48-bit reply (response types R1, R6, R7)
  20. // If response is NULL, does not wait for reply.
  21. sdio_status_t rp2040_sdio_command_R1(uint8_t command, uint32_t arg, uint32_t *response);
  22. // Execute a command that has 136-bit reply (response type R2)
  23. // Response buffer should have space for 16 bytes (the 128 bit payload)
  24. sdio_status_t rp2040_sdio_command_R2(uint8_t command, uint32_t arg, uint8_t *response);
  25. // Execute a command that has 48-bit reply but without CRC (response R3)
  26. sdio_status_t rp2040_sdio_command_R3(uint8_t command, uint32_t arg, uint32_t *response);
  27. // Start transferring data from SD card to memory buffer
  28. // Transfer block size is always 512 bytes.
  29. sdio_status_t rp2040_sdio_rx_start(uint8_t *buffer, uint32_t num_blocks);
  30. // Check if reception is complete
  31. // Returns SDIO_BUSY while transferring, SDIO_OK when done and error on failure.
  32. sdio_status_t rp2040_sdio_rx_poll(uint32_t *bytes_complete = nullptr);
  33. // Start transferring data from memory to SD card
  34. sdio_status_t rp2040_sdio_tx_start(const uint8_t *buffer, uint32_t num_blocks);
  35. // Check if transmission is complete
  36. sdio_status_t rp2040_sdio_tx_poll(uint32_t *bytes_complete = nullptr);
  37. // Force everything to idle state
  38. sdio_status_t rp2040_sdio_stop();
  39. // Performs one full CLK line cycle
  40. void cycleSdClock();
  41. // (Re)initialize the SDIO interface
  42. void rp2040_sdio_init(int clock_divider = 1);