spi_sd_hal.c 2.9 KB

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