30 #include "stm32f3xx_hal.h"
31 #include "user_diskio_spi.h"
40 extern SPI_HandleTypeDef hspi1;
48 DSTATUS disk_initialize(BYTE drv
50 BYTE n, cmd, ty, ocr[4];
52 if (drv != 0)
return STA_NOINIT;
55 if (Stat & STA_NODISK)
return Stat;
58 for (n = 10; n; n--) xchg_spi(0xFF);
61 if (send_cmd(CMD0, 0) == 1) {
63 if (send_cmd(CMD8, 0x1AA) == 1) {
64 for (n = 0; n < 4; n++)
65 ocr[n] = xchg_spi(0xFF);
68 while (SPI_Timer_Status() &&
69 send_cmd(ACMD41, 1UL << 30));
71 if (SPI_Timer_Status() &&
72 send_cmd(CMD58, 0) == 0) {
73 for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
75 (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
79 if (send_cmd(ACMD41, 0) <= 1) {
86 while (SPI_Timer_Status() &&
88 if (!SPI_Timer_Status() ||
89 send_cmd(CMD16, 512) != 0)
110 DSTATUS disk_status(BYTE drv
112 if (drv)
return STA_NOINIT;
126 if (drv || !count)
return RES_PARERR;
127 if (Stat & STA_NOINIT)
return RES_NOTRDY;
129 if (!(CardType & CT_BLOCK))
133 if ((send_cmd(CMD17, sector) == 0)
134 && rcvr_datablock(buff, 512)) {
138 if (send_cmd(CMD18, sector) == 0) {
140 if (!rcvr_datablock(buff, 512))
break;
148 return count ? RES_ERROR : RES_OK;
156 DRESULT disk_write(BYTE drv,
161 if (drv || !count)
return RES_PARERR;
162 if (Stat & STA_NOINIT)
return RES_NOTRDY;
163 if (Stat & STA_PROTECT)
return RES_WRPRT;
165 if (!(CardType & CT_BLOCK))
169 if ((send_cmd(CMD24, sector) == 0)
170 && xmit_datablock(buff, 0xFE)) {
174 if (CardType & CT_SDC)
175 send_cmd(ACMD23, count);
176 if (send_cmd(CMD25, sector) == 0) {
178 if (!xmit_datablock(buff, 0xFC))
break;
181 if (!xmit_datablock(0, 0xFD)) count = 1;
186 return count ? RES_ERROR : RES_OK;
195 DRESULT disk_ioctl(BYTE drv,
201 DWORD *dp, st, ed, csize;
203 if (drv)
return RES_PARERR;
204 if (Stat & STA_NOINIT)
return RES_NOTRDY;
210 if (spiselect()) res = RES_OK;
213 case GET_SECTOR_COUNT:
214 if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
215 if ((csd[0] >> 6) == 1) {
217 csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
218 *(DWORD *)buff = csize << 10;
220 n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) +
222 csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) +
223 ((WORD)(csd[6] & 3) << 10) + 1;
224 *(DWORD *)buff = csize << (n - 9);
231 if (CardType & CT_SD2) {
232 if (send_cmd(ACMD13, 0) == 0) {
234 if (rcvr_datablock(csd, 16)) {
235 for (n = 64 - 16; n; n--)
237 *(DWORD *)buff = 16UL << (csd[10] >> 4);
242 if ((send_cmd(CMD9, 0) == 0) &&
243 rcvr_datablock(csd, 16)) {
244 if (CardType & CT_SD1) {
246 (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1)
247 << ((csd[13] >> 6) - 1);
250 ((WORD)((csd[10] & 124) >> 2) + 1) *
251 (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
259 if (!(CardType & CT_SDC))
break;
260 if (USER_SPI_ioctl(drv, MMC_GET_CSD, csd))
break;
261 if (!(csd[0] >> 6) && !(csd[10] & 0x40))
266 if (!(CardType & CT_BLOCK)) {
270 if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 &&
271 send_cmd(CMD38, 0) == 0 &&
289 volatile DSTATUS Stat = STA_NOINIT;
291 uint32_t spiTimerTickStart;
292 uint32_t spiTimerTickDelay;
294 inline void CS_HIGH() {
295 HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_SET);
298 HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_RESET);
305 inline void FCLK_SLOW() {
306 MODIFY_REG(hspi1.Instance->CR1, SPI_BAUDRATEPRESCALER_256,
307 SPI_BAUDRATEPRESCALER_128);
309 inline void FCLK_FAST() {
310 MODIFY_REG(hspi1.Instance->CR1, SPI_BAUDRATEPRESCALER_256,
311 SPI_BAUDRATEPRESCALER_8);
314 void SPI_Timer_On(uint32_t waitTicks) {
315 spiTimerTickStart = HAL_GetTick();
316 spiTimerTickDelay = waitTicks;
319 uint8_t SPI_Timer_Status() {
320 return ((HAL_GetTick() - spiTimerTickStart) < spiTimerTickDelay);
328 BYTE xchg_spi(BYTE dat
331 HAL_SPI_TransmitReceive(&hspi1, &dat, &rxDat, 1, 50);
336 void rcvr_spi_multi(BYTE *buff,
339 for (UINT i = 0; i < btr; i++) {
340 *(buff + i) = xchg_spi(0xFF);
346 void xmit_spi_multi(
const BYTE *buff,
349 for (UINT i = 0; i < btx; i++) {
350 xchg_spi(*(buff + i));
365 uint32_t waitSpiTimerTickStart;
366 uint32_t waitSpiTimerTickDelay;
368 waitSpiTimerTickStart = HAL_GetTick();
369 waitSpiTimerTickDelay = (uint32_t)wt;
374 }
while (d != 0xFF &&
375 ((HAL_GetTick() - waitSpiTimerTickStart) <
376 waitSpiTimerTickDelay));
378 return (d == 0xFF) ? 1 : 0;
385 void despiselect(
void) {
398 if (wait_ready(500))
return 1;
416 token = xchg_spi(0xFF);
419 }
while ((token == 0xFF) && SPI_Timer_Status());
423 rcvr_spi_multi(buff, btr);
441 if (!wait_ready(500))
return 0;
445 xmit_spi_multi(buff, 512);
449 resp = xchg_spi(0xFF);
450 if ((resp & 0x1F) != 0x05)
469 res = send_cmd(CMD55, 0);
470 if (res > 1)
return res;
476 if (!spiselect())
return 0xFF;
480 xchg_spi(0x40 | cmd);
481 xchg_spi((BYTE)(arg >> 24));
482 xchg_spi((BYTE)(arg >> 16));
483 xchg_spi((BYTE)(arg >> 8));
486 if (cmd == CMD0) n = 0x95;
487 if (cmd == CMD8) n = 0x87;
495 res = xchg_spi(0xFF);
496 }
while ((res & 0x80) && --n);
Empty IO implementation that we can use to test the compilation.
Definition: BaseIO.h:10
SPI interface for STM32 microcontrollers.
Definition: SDSPISTM32IO.h:46