switch of interrupts while writing and reading to the SPI. avoids

desynchronization problems.
This commit is contained in:
nvt-se 2009-05-25 13:19:04 +00:00
parent 1fdd19079f
commit da91d87ae6
2 changed files with 38 additions and 8 deletions

View file

@ -47,9 +47,9 @@ Berlin, 2007
* @brief MMC-/SD-Card library * @brief MMC-/SD-Card library
* *
* @author Michael Baar <baar@inf.fu-berlin.de> * @author Michael Baar <baar@inf.fu-berlin.de>
* @version $Revision: 1.6 $ * @version $Revision: 1.7 $
* *
* $Id: sd.c,v 1.6 2008/05/27 14:22:55 nvt-se Exp $ * $Id: sd.c,v 1.7 2009/05/25 13:19:04 nvt-se Exp $
* *
* Initialisation and basic functions for read and write access * Initialisation and basic functions for read and write access
*/ */
@ -58,6 +58,8 @@ Berlin, 2007
#include "sd.h" #include "sd.h"
#include "sdspi.h" #include "sdspi.h"
#include "dev/leds.h"
volatile sd_state_t sd_state; volatile sd_state_t sd_state;
@ -400,6 +402,7 @@ _sd_write_finish(void)
uint8_t ret; uint8_t ret;
enum sd_write_ret result = SD_WRITE_STORE_ERR; enum sd_write_ret result = SD_WRITE_STORE_ERR;
uint16_t i; uint16_t i;
int s;
#if SPI_DMA_WRITE #if SPI_DMA_WRITE
sdspi_dma_wait(); sdspi_dma_wait();
@ -409,6 +412,8 @@ _sd_write_finish(void)
// dummy crc // dummy crc
sdspi_idle(2); sdspi_idle(2);
s = splhigh();
// receive data response (ZZS___ 3 bits crc response) // receive data response (ZZS___ 3 bits crc response)
for (i = 0; i < SD_TIMEOUT_NCR; i++) { for (i = 0; i < SD_TIMEOUT_NCR; i++) {
ret = sdspi_rx(); ret = sdspi_rx();
@ -421,6 +426,8 @@ _sd_write_finish(void)
} }
} }
splx(s);
// wait for data to be written // wait for data to be written
_sd_wait_standby(NULL); _sd_wait_standby(NULL);
sdspi_unselect(); sdspi_unselect();
@ -462,6 +469,7 @@ enum sd_write_ret
_sd_write_block(const uint32_t * pAddress, const void *pBuffer, int increment) _sd_write_block(const uint32_t * pAddress, const void *pBuffer, int increment)
{ {
uint8_t r1, ret; uint8_t r1, ret;
int s;
// block write-access on write protection // block write-access on write protection
if (sd_protected()) { if (sd_protected()) {
@ -478,16 +486,20 @@ _sd_write_block(const uint32_t * pAddress, const void *pBuffer, int increment)
r1 = 0; r1 = 0;
ret = _sd_send_cmd(SD_CMD_WRITE_SINGLE_BLOCK, SD_RESPONSE_SIZE_R1, ret = _sd_send_cmd(SD_CMD_WRITE_SINGLE_BLOCK, SD_RESPONSE_SIZE_R1,
pAddress, &r1); pAddress, &r1);
if (!ret | r1) { if (!ret || r1) {
leds_on(LEDS_ALL);
_sd_reset(NULL);
uart_unlock(UART_MODE_SPI); uart_unlock(UART_MODE_SPI);
SD_LED_WRITE_OFF; SD_LED_WRITE_OFF;
return SD_WRITE_COMMAND_ERR; return SD_WRITE_COMMAND_ERR;
} }
// write data // write data
sdspi_select(); sdspi_select();
s = splhigh();
sdspi_tx(0xFF); sdspi_tx(0xFF);
sdspi_tx(SD_TOKEN_WRITE); sdspi_tx(SD_TOKEN_WRITE);
sdspi_write(pBuffer, sd_state.BlockLen, increment); sdspi_write(pBuffer, sd_state.BlockLen, increment);
splx(s);
SD_LED_WRITE_OFF; SD_LED_WRITE_OFF;
@ -620,8 +632,9 @@ _sd_send_cmd(const uint8_t command,
uint8_t cmd[6] = { uint8_t cmd[6] = {
0x40, 0, 0, 0, 0, 0x95 0x40, 0, 0, 0, 0, 0x95
}; };
uint8_t data; // rx buffer uint8_t data; /* reception buffer */
int i; // loop counter int i; /* loop counter */
int s; /* interrupt state */
#if SD_WRITE && SPI_DMA_WRITE #if SD_WRITE && SPI_DMA_WRITE
sd_write_flush(); sd_write_flush();
@ -636,6 +649,7 @@ _sd_send_cmd(const uint8_t command,
cmd[4] = ((uint8_t *) pArg)[0]; cmd[4] = ((uint8_t *) pArg)[0];
} }
s = splhigh();
sdspi_write(cmd, 6, 1); sdspi_write(cmd, 6, 1);
// wait for start bit // wait for start bit
@ -647,9 +661,12 @@ _sd_send_cmd(const uint8_t command,
} }
} while (i--); } while (i--);
splx(s);
goto sd_send_cmd_fail; goto sd_send_cmd_fail;
_sd_send_cmd_response: _sd_send_cmd_response:
s = splhigh();
// start bit received, read response with size i // start bit received, read response with size i
i = response_size - 1; i = response_size - 1;
if (pResponse != NULL) { if (pResponse != NULL) {
@ -671,11 +688,13 @@ _sd_send_cmd_response:
// done successfully // done successfully
sdspi_unselect(); sdspi_unselect();
splx(s);
return TRUE; return TRUE;
sd_send_cmd_fail: sd_send_cmd_fail:
// failed // failed
//sdspi_unselect(); sdspi_unselect();
return FALSE; return FALSE;
} }

View file

@ -47,9 +47,9 @@ Berlin, 2007
* @brief Serial Peripheral Interface for SD library * @brief Serial Peripheral Interface for SD library
* *
* @author Michael Baar <baar@inf.fu-berlin.de> * @author Michael Baar <baar@inf.fu-berlin.de>
* @version $Revision: 1.2 $ * @version $Revision: 1.3 $
* *
* $Id: sdspi.c,v 1.2 2008/03/28 23:03:05 nvt-se Exp $ * $Id: sdspi.c,v 1.3 2009/05/25 13:19:04 nvt-se Exp $
*/ */
#include <msp430x16x.h> #include <msp430x16x.h>
@ -111,6 +111,10 @@ sdspi_dma_wait(void)
void void
sdspi_read(void *pDestination, const uint16_t size, const bool incDest) sdspi_read(void *pDestination, const uint16_t size, const bool incDest)
{ {
int s;
s = splhigh();
#if SPI_DMA_READ #if SPI_DMA_READ
sdspi_dma_wait(); sdspi_dma_wait();
@ -153,12 +157,17 @@ sdspi_read(void *pDestination, const uint16_t size, const bool incDest)
i--; i--;
} while (i); } while (i);
#endif #endif
splx(s);
} }
#if SPI_WRITE #if SPI_WRITE
void void
sdspi_write(const void *pSource, const uint16_t size, const int increment) sdspi_write(const void *pSource, const uint16_t size, const int increment)
{ {
int s;
s = splhigh();
#if SPI_DMA_WRITE #if SPI_DMA_WRITE
sdspi_dma_wait(); sdspi_dma_wait();
@ -191,6 +200,8 @@ sdspi_write(const void *pSource, const uint16_t size, const int increment)
i--; i--;
} while (i); } while (i);
#endif #endif
splx(s);
} }
#endif #endif