spi_sd_hal.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <furi_hal.h>
  2. #include <furi.h>
  3. #define SD_DUMMY_BYTE 0xFF
  4. const uint32_t SpiTimeout = 1000;
  5. uint8_t SD_IO_WriteByte(uint8_t Data);
  6. /******************************************************************************
  7. BUS OPERATIONS
  8. *******************************************************************************/
  9. /**
  10. * @brief SPI Write byte(s) to device
  11. * @param DataIn: Pointer to data buffer to write
  12. * @param DataOut: Pointer to data buffer for read data
  13. * @param DataLength: number of bytes to write
  14. * @retval None
  15. */
  16. static void SPIx_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
  17. furi_check(furi_hal_spi_bus_trx(
  18. furi_hal_sd_spi_handle, (uint8_t*)DataIn, DataOut, DataLength, SpiTimeout));
  19. }
  20. /**
  21. * @brief SPI Write a byte to device
  22. * @param Value: value to be written
  23. * @retval None
  24. */
  25. __attribute__((unused)) static void SPIx_Write(uint8_t Value) {
  26. furi_check(furi_hal_spi_bus_tx(furi_hal_sd_spi_handle, (uint8_t*)&Value, 1, SpiTimeout));
  27. }
  28. /******************************************************************************
  29. LINK OPERATIONS
  30. *******************************************************************************/
  31. /********************************* LINK SD ************************************/
  32. /**
  33. * @brief Initialize the SD Card and put it into StandBy State (Ready for
  34. * data transfer).
  35. * @retval None
  36. */
  37. void SD_IO_Init(void) {
  38. uint8_t counter = 0;
  39. /* SD chip select high */
  40. furi_hal_gpio_write(furi_hal_sd_spi_handle->cs, true);
  41. furi_delay_us(10);
  42. /* Send dummy byte 0xFF, 10 times with CS high */
  43. /* Rise CS and MOSI for 80 clocks cycles */
  44. for(counter = 0; counter <= 200; counter++) {
  45. /* Send dummy byte 0xFF */
  46. SD_IO_WriteByte(SD_DUMMY_BYTE);
  47. }
  48. }
  49. /**
  50. * @brief Set SD interface Chip Select state
  51. * @param val: 0 (low) or 1 (high) state
  52. * @retval None
  53. */
  54. void SD_IO_CSState(uint8_t val) {
  55. /* Some SD Cards are prone to fail if CLK-ed too soon after CS transition. Worst case found: 8us */
  56. if(val == 1) {
  57. furi_delay_us(10); // Exit guard time for some SD cards
  58. furi_hal_gpio_write(furi_hal_sd_spi_handle->cs, true);
  59. } else {
  60. furi_hal_gpio_write(furi_hal_sd_spi_handle->cs, false);
  61. furi_delay_us(10); // Entry guard time for some SD cards
  62. }
  63. }
  64. /**
  65. * @brief Write byte(s) on the SD
  66. * @param DataIn: Pointer to data buffer to write
  67. * @param DataOut: Pointer to data buffer for read data
  68. * @param DataLength: number of bytes to write
  69. * @retval None
  70. */
  71. void SD_IO_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
  72. /* Send the byte */
  73. SPIx_WriteReadData(DataIn, DataOut, DataLength);
  74. }
  75. /**
  76. * @brief Write a byte on the SD.
  77. * @param Data: byte to send.
  78. * @retval Data written
  79. */
  80. uint8_t SD_IO_WriteByte(uint8_t Data) {
  81. uint8_t tmp;
  82. /* Send the byte */
  83. SPIx_WriteReadData(&Data, &tmp, 1);
  84. return tmp;
  85. }