spi_sd_hal.c 3.4 KB

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