spi_sd_hal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "main.h"
  2. #include "api-hal-spi.h"
  3. #define SD_DUMMY_BYTE 0xFF
  4. const uint32_t SpiTimeout = 1000;
  5. uint8_t SD_IO_WriteByte(uint8_t Data);
  6. static const ApiHalSpiDevice* sd_spi_dev = &api_hal_spi_devices[ApiHalSpiDeviceIdSdCardFast];
  7. /******************************************************************************
  8. BUS OPERATIONS
  9. *******************************************************************************/
  10. /**
  11. * @brief SPI error treatment function
  12. * @retval None
  13. */
  14. static void SPIx_Error(void) {
  15. /* Re-Initiaize the SPI communication BUS */
  16. api_hal_spi_bus_reset(sd_spi_dev->bus);
  17. }
  18. /**
  19. * @brief SPI Write byte(s) to device
  20. * @param DataIn: Pointer to data buffer to write
  21. * @param DataOut: Pointer to data buffer for read data
  22. * @param DataLength: number of bytes to write
  23. * @retval None
  24. */
  25. static void SPIx_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
  26. bool status = api_hal_spi_bus_trx(sd_spi_dev->bus, (uint8_t*)DataIn, DataOut, DataLength, SpiTimeout);
  27. /* Check the communication status */
  28. if(!status) {
  29. /* Execute user timeout callback */
  30. SPIx_Error();
  31. }
  32. }
  33. /**
  34. * @brief SPI Write a byte to device
  35. * @param Value: value to be written
  36. * @retval None
  37. */
  38. __attribute__((unused)) static void SPIx_Write(uint8_t Value) {
  39. uint8_t data;
  40. bool status = api_hal_spi_bus_trx(sd_spi_dev->bus, (uint8_t*)&Value, &data, 1, SpiTimeout);
  41. /* Check the communication status */
  42. if(!status) {
  43. /* Execute user timeout callback */
  44. SPIx_Error();
  45. }
  46. }
  47. /******************************************************************************
  48. LINK OPERATIONS
  49. *******************************************************************************/
  50. /********************************* LINK SD ************************************/
  51. /**
  52. * @brief Initialize the SD Card and put it into StandBy State (Ready for
  53. * data transfer).
  54. * @retval None
  55. */
  56. void SD_IO_Init(void) {
  57. uint8_t counter = 0;
  58. /* SD chip select high */
  59. hal_gpio_write(sd_spi_dev->chip_select, true);
  60. /* Send dummy byte 0xFF, 10 times with CS high */
  61. /* Rise CS and MOSI for 80 clocks cycles */
  62. for(counter = 0; counter <= 200; counter++) {
  63. /* Send dummy byte 0xFF */
  64. SD_IO_WriteByte(SD_DUMMY_BYTE);
  65. }
  66. }
  67. /**
  68. * @brief Set SD interface Chip Select state
  69. * @param val: 0 (low) or 1 (high) state
  70. * @retval None
  71. */
  72. void SD_IO_CSState(uint8_t val) {
  73. if(val == 1) {
  74. hal_gpio_write(sd_spi_dev->chip_select, true);
  75. } else {
  76. hal_gpio_write(sd_spi_dev->chip_select, false);
  77. }
  78. }
  79. /**
  80. * @brief Write byte(s) on the SD
  81. * @param DataIn: Pointer to data buffer to write
  82. * @param DataOut: Pointer to data buffer for read data
  83. * @param DataLength: number of bytes to write
  84. * @retval None
  85. */
  86. void SD_IO_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
  87. /* Send the byte */
  88. SPIx_WriteReadData(DataIn, DataOut, DataLength);
  89. }
  90. /**
  91. * @brief Write a byte on the SD.
  92. * @param Data: byte to send.
  93. * @retval Data written
  94. */
  95. uint8_t SD_IO_WriteByte(uint8_t Data) {
  96. uint8_t tmp;
  97. /* Send the byte */
  98. SPIx_WriteReadData(&Data, &tmp, 1);
  99. return tmp;
  100. }