hal_mmc_spi: optimization backport
This commit is contained in:
parent
ad80e276ad
commit
1e252e5d9f
|
@ -312,14 +312,14 @@ static uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len) {
|
||||||
*
|
*
|
||||||
* @notapi
|
* @notapi
|
||||||
*/
|
*/
|
||||||
static void wait(MMCDriver *mmcp) {
|
static bool mmc_wait_idle(MMCDriver *mmcp) {
|
||||||
int i;
|
int i;
|
||||||
uint8_t buf[4];
|
uint8_t buf[4];
|
||||||
|
|
||||||
for (i = 0; i < 16; i++) {
|
for (i = 0; i < 16; i++) {
|
||||||
spiReceiveSmall(mmcp->config->spip, 1, buf);
|
spiReceiveSmall(mmcp->config->spip, 1, buf);
|
||||||
if (buf[0] == 0xFFU) {
|
if (buf[0] == 0xFFU) {
|
||||||
return;
|
return HAL_SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if MMC_NICE_WAITING == TRUE
|
#if MMC_NICE_WAITING == TRUE
|
||||||
|
@ -330,7 +330,7 @@ static void wait(MMCDriver *mmcp) {
|
||||||
while (true) {
|
while (true) {
|
||||||
spiReceiveSmall(mmcp->config->spip, 1, buf);
|
spiReceiveSmall(mmcp->config->spip, 1, buf);
|
||||||
if (buf[0] == 0xFFU) {
|
if (buf[0] == 0xFFU) {
|
||||||
break;
|
return HAL_SUCCESS;
|
||||||
}
|
}
|
||||||
#if MMC_NICE_WAITING == TRUE
|
#if MMC_NICE_WAITING == TRUE
|
||||||
/* Trying to be nice with the other threads.*/
|
/* Trying to be nice with the other threads.*/
|
||||||
|
@ -341,6 +341,8 @@ static void wait(MMCDriver *mmcp) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return HAL_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -356,7 +358,7 @@ static void send_hdr(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
|
||||||
uint8_t buf[6];
|
uint8_t buf[6];
|
||||||
|
|
||||||
/* Wait for the bus to become idle if a write operation was in progress.*/
|
/* Wait for the bus to become idle if a write operation was in progress.*/
|
||||||
wait(mmcp);
|
mmc_wait_idle(mmcp);
|
||||||
|
|
||||||
buf[0] = (uint8_t)0x40U | cmd;
|
buf[0] = (uint8_t)0x40U | cmd;
|
||||||
buf[1] = (uint8_t)(arg >> 24U);
|
buf[1] = (uint8_t)(arg >> 24U);
|
||||||
|
@ -503,37 +505,6 @@ static bool read_CxD(MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) {
|
||||||
return HAL_FAILED;
|
return HAL_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Waits that the card reaches an idle state.
|
|
||||||
*
|
|
||||||
* @param[in] mmcp pointer to the @p MMCDriver object
|
|
||||||
*
|
|
||||||
* @notapi
|
|
||||||
*/
|
|
||||||
static void sync(MMCDriver *mmcp) {
|
|
||||||
uint8_t buf[1];
|
|
||||||
|
|
||||||
spiSelect(mmcp->config->spip);
|
|
||||||
#if MMC_NICE_WAITING == TRUE
|
|
||||||
int waitCounter = 0;
|
|
||||||
#endif
|
|
||||||
while (true) {
|
|
||||||
spiReceiveSmall(mmcp->config->spip, 1, buf);
|
|
||||||
if (buf[0] == 0xFFU) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#if MMC_NICE_WAITING == TRUE
|
|
||||||
/* Trying to be nice with the other threads.*/
|
|
||||||
osalThreadSleepMilliseconds(1);
|
|
||||||
if (++waitCounter == MMC_WAIT_RETRY) {
|
|
||||||
// it's time to give up, this MMC card is not working property
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
spiUnselect(mmcp->config->spip);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
/* Driver exported functions. */
|
/* Driver exported functions. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
@ -740,10 +711,12 @@ failed:
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
bool mmcDisconnect(MMCDriver *mmcp) {
|
bool mmcDisconnect(MMCDriver *mmcp) {
|
||||||
|
bool result;
|
||||||
|
|
||||||
osalDbgCheck(mmcp != NULL);
|
osalDbgCheck(mmcp != NULL);
|
||||||
|
|
||||||
osalSysLock();
|
osalSysLock();
|
||||||
|
|
||||||
osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY),
|
osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY),
|
||||||
"invalid state");
|
"invalid state");
|
||||||
if (mmcp->state == BLK_ACTIVE) {
|
if (mmcp->state == BLK_ACTIVE) {
|
||||||
|
@ -755,11 +728,16 @@ bool mmcDisconnect(MMCDriver *mmcp) {
|
||||||
|
|
||||||
/* Wait for the pending write operations to complete.*/
|
/* Wait for the pending write operations to complete.*/
|
||||||
spiStart(mmcp->config->spip, mmcp->config->hscfg);
|
spiStart(mmcp->config->spip, mmcp->config->hscfg);
|
||||||
sync(mmcp);
|
spiSelect(mmcp->config->spip);
|
||||||
|
|
||||||
|
result = mmc_wait_idle(mmcp);
|
||||||
|
|
||||||
|
spiUnselect(mmcp->config->spip);
|
||||||
spiStop(mmcp->config->spip);
|
spiStop(mmcp->config->spip);
|
||||||
|
|
||||||
mmcp->state = BLK_ACTIVE;
|
mmcp->state = BLK_ACTIVE;
|
||||||
return HAL_SUCCESS;
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -992,7 +970,7 @@ bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer) {
|
||||||
spiIgnoreSmall(mmcp->config->spip, 2); /* CRC ignored. */
|
spiIgnoreSmall(mmcp->config->spip, 2); /* CRC ignored. */
|
||||||
spiReceiveSmall(mmcp->config->spip, 1, b);
|
spiReceiveSmall(mmcp->config->spip, 1, b);
|
||||||
if ((b[0] & 0x1FU) == 0x05U) {
|
if ((b[0] & 0x1FU) == 0x05U) {
|
||||||
wait(mmcp);
|
mmc_wait_idle(mmcp);
|
||||||
return HAL_SUCCESS;
|
return HAL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1043,6 +1021,7 @@ bool mmcStopSequentialWrite(MMCDriver *mmcp) {
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
bool mmcSync(MMCDriver *mmcp) {
|
bool mmcSync(MMCDriver *mmcp) {
|
||||||
|
bool result;
|
||||||
|
|
||||||
osalDbgCheck(mmcp != NULL);
|
osalDbgCheck(mmcp != NULL);
|
||||||
|
|
||||||
|
@ -1054,11 +1033,16 @@ bool mmcSync(MMCDriver *mmcp) {
|
||||||
mmcp->state = BLK_SYNCING;
|
mmcp->state = BLK_SYNCING;
|
||||||
|
|
||||||
spiStart(mmcp->config->spip, mmcp->config->hscfg);
|
spiStart(mmcp->config->spip, mmcp->config->hscfg);
|
||||||
sync(mmcp);
|
spiSelect(mmcp->config->spip);
|
||||||
|
|
||||||
|
result = mmc_wait_idle(mmcp);
|
||||||
|
|
||||||
|
spiUnselect(mmcp->config->spip);
|
||||||
|
|
||||||
/* Synchronization operation finished.*/
|
/* Synchronization operation finished.*/
|
||||||
mmcp->state = BLK_READY;
|
mmcp->state = BLK_READY;
|
||||||
return HAL_SUCCESS;
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue