made more conform with contiki indentation.

This commit is contained in:
nvt-se 2008-03-28 23:03:05 +00:00
parent e6cc0105b0
commit 2822ff7ddb
8 changed files with 135 additions and 258 deletions

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,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.2 $ * @version $Revision: 1.3 $
* *
* $Id: sd.c,v 1.2 2008/03/28 15:58:43 nvt-se Exp $ * $Id: sd.c,v 1.3 2008/03/28 23:03:05 nvt-se Exp $
* *
* Initialisation and basic functions for read and write access * Initialisation and basic functions for read and write access
*/ */

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -47,9 +46,9 @@ Berlin, 2007
* @brief MMC-/SD-Card library, Public interface * @brief MMC-/SD-Card library, Public interface
* *
* @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: sd.h,v 1.2 2008/03/28 15:58:43 nvt-se Exp $ * $Id: sd.h,v 1.3 2008/03/28 23:03:05 nvt-se Exp $
*/ */
/** /**
@ -130,12 +129,14 @@ __attribute__ ((packed))
/// Card access library state /// Card access library state
#define SD_CACHE_LOCKED 0x01 #define SD_CACHE_LOCKED 0x01
#define SD_CACHE_DIRTY 0x02 #define SD_CACHE_DIRTY 0x02
typedef struct {
typedef struct {
char buffer[SD_WRITE_BLOCKLENGTH]; char buffer[SD_WRITE_BLOCKLENGTH];
uint32_t address; uint32_t address;
uint8_t state; uint8_t state;
} sd_cache_t; } sd_cache_t;
typedef struct {
typedef struct {
uint16_t MinBlockLen_bit:4; ///< minimum supported blocklength uint16_t MinBlockLen_bit:4; ///< minimum supported blocklength
uint16_t MaxBlockLen_bit:4; ///< maximum supported blocklength uint16_t MaxBlockLen_bit:4; ///< maximum supported blocklength
uint16_t Flags:8; ///< feature flags uint16_t Flags:8; ///< feature flags
@ -144,14 +145,14 @@ __attribute__ ((packed))
#if SD_CACHE #if SD_CACHE
sd_cache_t *Cache; sd_cache_t *Cache;
#endif #endif
} sd_state_t; } sd_state_t;
extern volatile sd_state_t sd_state; ///< Card access library state extern volatile sd_state_t sd_state; ///< Card access library state
/** /**
* @brief Library initialisation * @brief Library initialisation
*/ */
void sd_Init(void); void sd_init(void);
/** /**
* @brief Setup ports for sd card communication * @brief Setup ports for sd card communication
@ -167,25 +168,25 @@ __attribute__ ((packed))
/** /**
* @brief Return value of ::sd_init function * @brief Return value of ::sd_init function
*/ */
enum sd_init_ret { enum sd_init_ret {
SD_INIT_SUCCESS = 0, SD_INIT_SUCCESS = 0,
SD_INIT_NOCARD = 1, SD_INIT_NOCARD = 1,
SD_INIT_FAILED = 2, SD_INIT_FAILED = 2,
SD_INIT_NOTSUPP = 3 SD_INIT_NOTSUPP = 3
}; };
/** /**
* @brief Return value of write functions * @brief Return value of write functions
* @see ::sd_write, ::sd_write_block * @see ::sd_write, ::sd_write_block
*/ */
enum sd_write_ret { enum sd_write_ret {
SD_WRITE_SUCCESS = 0, ///< writing successfull SD_WRITE_SUCCESS = 0, ///< writing successfull
SD_WRITE_PROTECTED_ERR = 1, ///< card write protected SD_WRITE_PROTECTED_ERR = 1, ///< card write protected
SD_WRITE_INTERFACE_ERR = 2, ///< error in UART SPI interface SD_WRITE_INTERFACE_ERR = 2, ///< error in UART SPI interface
SD_WRITE_COMMAND_ERR = 3, ///< error in write command or command arguments (e.g. target address) SD_WRITE_COMMAND_ERR = 3, ///< error in write command or command arguments (e.g. target address)
SD_WRITE_STORE_ERR = 4, ///< storing written data to persistant memory on card failed SD_WRITE_STORE_ERR = 4, ///< storing written data to persistant memory on card failed
SD_WRITE_DMA_ERR = 5 SD_WRITE_DMA_ERR = 5
}; };
/** /**
* @brief Initialize card and state * @brief Initialize card and state
@ -195,12 +196,12 @@ __attribute__ ((packed))
* functionality. Initializes the global state struct sd_state. * functionality. Initializes the global state struct sd_state.
* Should be invoked once immediately after ::sd_setup. * Should be invoked once immediately after ::sd_setup.
*/ */
enum sd_init_ret sd_init_card(sd_cache_t * pCache); enum sd_init_ret sd_init_card(sd_cache_t * pCache);
/** /**
* @brief Last operation to call when finished with using the card. * @brief Last operation to call when finished with using the card.
*/ */
void sd_close(void); void sd_close(void);
/** /**
* @brief SD Card physically present? * @brief SD Card physically present?
@ -248,7 +249,7 @@ __attribute__ ((packed))
* @param[in,out] pAddress address to align, will be modified to be block aligned * @param[in,out] pAddress address to align, will be modified to be block aligned
* @return Offset from aligned address to original address * @return Offset from aligned address to original address
*/ */
uint16_t sd_AlignAddress(uint32_t * pAddress); uint16_t sd_AlignAddress(uint32_t * pAddress);
/** /**
* @brief Read one complete block from a block aligned address into buffer * @brief Read one complete block from a block aligned address into buffer
@ -265,11 +266,10 @@ __attribute__ ((packed))
* *
* @return Number of bytes read (should always be = sd_state.BlockLen) * @return Number of bytes read (should always be = sd_state.BlockLen)
*/ */
uint16_t sd_read_block(void (*const pBuffer), const uint32_t address); uint16_t sd_read_block(void (*const pBuffer), const uint32_t address);
#if SD_READ_BYTE #if SD_READ_BYTE
/**
/**
* @brief Read one byte from any address * @brief Read one byte from any address
* This function reads a single byte from any address. It is optimized for best speed * This function reads a single byte from any address. It is optimized for best speed
* at any blocklength. * at any blocklength.
@ -281,11 +281,10 @@ __attribute__ ((packed))
* *
* @return Number of bytes read (usually 1) * @return Number of bytes read (usually 1)
*/ */
bool sd_read_byte(void *pBuffer, const uint32_t address); bool sd_read_byte(void *pBuffer, const uint32_t address);
#endif #endif
#if SD_WRITE #if SD_WRITE
/** /**
* @brief Write one complete block at a block aligned address from buffer to card * @brief Write one complete block at a block aligned address from buffer to card
* *
@ -296,7 +295,7 @@ __attribute__ ((packed))
* \Note * \Note
* Only supported block size for writing is usually 512 bytes. * Only supported block size for writing is usually 512 bytes.
*/ */
enum sd_write_ret sd_write_block(const uint32_t address, enum sd_write_ret sd_write_block(const uint32_t address,
void const (*const pBuffer)); void const (*const pBuffer));
/** /**
@ -310,7 +309,7 @@ __attribute__ ((packed))
* @note Use this for settings blocks to 0x00. * @note Use this for settings blocks to 0x00.
* Only supported block size for writing is usually 512 bytes. * Only supported block size for writing is usually 512 bytes.
*/ */
enum sd_write_ret sd_set_block(const uint32_t address, enum sd_write_ret sd_set_block(const uint32_t address,
const char (*const pChar)); const char (*const pChar));
/** /**
@ -318,7 +317,7 @@ __attribute__ ((packed))
* *
* Wait for a running DMA write operation to finish * Wait for a running DMA write operation to finish
*/ */
enum sd_write_ret sd_write_flush(void); enum sd_write_ret sd_write_flush(void);
#endif #endif
#if SD_CACHE #if SD_CACHE
@ -333,7 +332,7 @@ __attribute__ ((packed))
* a high level operation has finished and you want to store all data * a high level operation has finished and you want to store all data
* persistantly. The write back operation does not use timers. * persistantly. The write back operation does not use timers.
*/ */
void sd_cache_flush(void); void sd_cache_flush(void);
/** /**
* @brief Read a block into the cache buffer * @brief Read a block into the cache buffer
@ -341,7 +340,7 @@ __attribute__ ((packed))
* *
* You won't usually need this operation. * You won't usually need this operation.
*/ */
sd_cache_t *sd_cache_read_block(const uint32_t * blAdr); sd_cache_t *sd_cache_read_block(const uint32_t * blAdr);
#endif #endif
/** /**
@ -372,15 +371,13 @@ __attribute__ ((packed))
uint32_t sd_get_size(void); uint32_t sd_get_size(void);
#if SD_READ_ANY #if SD_READ_ANY
/** /**
* @brief Read any number of bytes from any address into buffer * @brief Read any number of bytes from any address into buffer
* *
* @param[out] pBuffer Pointer to a buffer to which data is read. It should be least * @param[out] pBuffer Pointer to a buffer to which data is read. It should be least
* size bytes large * size bytes large
* @param[in] address The address of the first byte that shall be read to pBuffer * @param[in] address The address of the first byte that shall be read to pBuffer
* @param[in] size Number of bytes which shall be read starting at address * @param[in] size Number of bytes which shall be read starting at address */
*/
uint16_t sd_read(void *pBuffer, uint32_t address, uint16_t size); uint16_t sd_read(void *pBuffer, uint32_t address, uint16_t size);
#endif #endif

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,9 +47,9 @@ Berlin, 2007
* @brief MMC-/SD-Card library, cached read and write * @brief MMC-/SD-Card library, cached read and write
* *
* @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: sd_cache.c,v 1.2 2008/03/28 15:58:43 nvt-se Exp $ * $Id: sd_cache.c,v 1.3 2008/03/28 23:03:05 nvt-se Exp $
*/ */
@ -63,235 +62,142 @@ Berlin, 2007
#if SD_CACHE #if SD_CACHE
void void
_sd_cache_init() _sd_cache_init(void)
{ {
uint32_t addr = 0;
uint32_t adr = 0;
sd_state.Cache->address = 1; sd_state.Cache->address = 1;
sd_state.Cache->state = 0; sd_state.Cache->state = 0;
// pre-read first block // pre-read first block
sd_cache_read_block(&adr); sd_cache_read_block(&addr);
SD_FREE_LOCK(sd_state.Cache); SD_FREE_LOCK(sd_state.Cache);
} }
void void
_sd_cache_flush() _sd_cache_flush(void)
{ {
#if SD_WRITE #if SD_WRITE
SD_GET_LOCK(sd_state.Cache); SD_GET_LOCK(sd_state.Cache);
if (sd_state.Cache->state & SD_CACHE_DIRTY) { if (sd_state.Cache->state & SD_CACHE_DIRTY) {
sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT);
sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer); sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer);
sd_state.Cache->state &= ~SD_CACHE_DIRTY; sd_state.Cache->state &= ~SD_CACHE_DIRTY;
} }
SD_FREE_LOCK(sd_state.Cache); SD_FREE_LOCK(sd_state.Cache);
#endif
#endif /*
*/
} }
sd_cache_t * sd_cache_t *
sd_cache_read_block(const uint32_t * pblAdr) sd_cache_read_block(const uint32_t * pblAdr)
{ {
SD_GET_LOCK(sd_state.Cache); SD_GET_LOCK(sd_state.Cache);
if (sd_state.Cache->address != *pblAdr) { if (sd_state.Cache->address != *pblAdr) {
sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT);
if (sd_state.Cache->state & SD_CACHE_DIRTY) { if (sd_state.Cache->state & SD_CACHE_DIRTY) {
sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer); sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer);
sd_state.Cache->state &= ~SD_CACHE_DIRTY; sd_state.Cache->state &= ~SD_CACHE_DIRTY;
} }
sd_state.Cache->address = *pblAdr; sd_state.Cache->address = *pblAdr;
if (!sd_read_block(sd_state.Cache->buffer, *pblAdr)) { if (!sd_read_block(sd_state.Cache->buffer, *pblAdr)) {
SD_FREE_LOCK(sd_state.Cache); SD_FREE_LOCK(sd_state.Cache);
return false; return false;
} }
} }
return sd_state.Cache; return sd_state.Cache;
} }
#if SD_READ_ANY #if SD_READ_ANY
uint16_t uint16_t
sd_read(void *pBuffer, uint32_t address, uint16_t size) sd_read(void *pBuffer, uint32_t address, uint16_t size)
{ {
uint16_t offset; // bytes from aligned address to start of first byte to keep uint16_t offset; // bytes from aligned address to start of first byte to keep
char *p; // pointer to current pos in receive buffer char *p; // pointer to current pos in receive buffer
uint16_t bytes_left; // num bytes to read uint16_t bytes_left; // num bytes to read
uint16_t read_count; // num bytes to read from current block uint16_t read_count; // num bytes to read from current block
//
// parameter processing // parameter processing
//
p = (char *)pBuffer; p = (char *)pBuffer;
bytes_left = size; bytes_left = size;
// align to block // align to block
offset = sd_AlignAddress(&address); offset = sd_AlignAddress(&address);
//
// Data transfer // Data transfer
//
do { do {
// calculate block // calculate block
if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) { if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) {
read_count = sd_state.BlockLen; read_count = sd_state.BlockLen;
sd_read_block(p, address); sd_read_block(p, address);
} else { } else {
sd_cache_read_block(&address); sd_cache_read_block(&address);
read_count = bytes_left + offset; read_count = bytes_left + offset;
if (read_count > sd_state.BlockLen) {
if (read_count > sd_state.BlockLen)
read_count = sd_state.BlockLen - offset; read_count = sd_state.BlockLen - offset;
} else {
else
read_count = bytes_left; read_count = bytes_left;
memcpy(p, sd_state.Cache->buffer + offset, read_count);
SD_FREE_LOCK(sd_state.Cache);
} }
memcpy(p, sd_state.Cache->buffer + offset, read_count);
SD_FREE_LOCK(sd_state.Cache);
}
bytes_left -= read_count; bytes_left -= read_count;
if (bytes_left == 0) {
if (bytes_left == 0)
return size; return size;
}
p += read_count; p += read_count;
address += sd_state.BlockLen; address += sd_state.BlockLen;
} while (1); } while (1);
} }
#endif // SD_READ_ANY #endif // SD_READ_ANY
#if SD_WRITE #if SD_WRITE
uint16_t uint16_t
sd_write(uint32_t address, void *pBuffer, uint16_t size) sd_write(uint32_t address, void *pBuffer, uint16_t size)
{ {
uint16_t offset; // bytes from aligned address to start of first byte to keep uint16_t offset; // bytes from aligned address to start of first byte to keep
char *p; // pointer to current pos in receive buffer char *p; // pointer to current pos in receive buffer
uint16_t bytes_left; // num bytes to read uint16_t bytes_left; // num bytes to read
uint16_t read_count; // num bytes to read from current block uint16_t read_count; // num bytes to read from current block
//
// parameter processing // parameter processing
//
p = (char *)pBuffer; p = (char *)pBuffer;
bytes_left = size; bytes_left = size;
// align to block // align to block
offset = sd_AlignAddress(&address); offset = sd_AlignAddress(&address);
sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT);
//
// Data transfer // Data transfer
//
do { do {
// calculate block // calculate block
if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) { if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) {
read_count = sd_state.BlockLen; read_count = sd_state.BlockLen;
sd_write_block(address, p); sd_write_block(address, p);
} else { } else {
sd_cache_read_block(&address); sd_cache_read_block(&address);
read_count = bytes_left + offset; read_count = bytes_left + offset;
if (read_count > sd_state.BlockLen) {
if (read_count > sd_state.BlockLen)
read_count = sd_state.BlockLen - offset; read_count = sd_state.BlockLen - offset;
} else {
else
read_count = bytes_left; read_count = bytes_left;
}
memcpy(sd_state.Cache->buffer + offset, p, read_count); memcpy(sd_state.Cache->buffer + offset, p, read_count);
sd_state.Cache->state |= SD_CACHE_DIRTY; sd_state.Cache->state |= SD_CACHE_DIRTY;
SD_FREE_LOCK(sd_state.Cache); SD_FREE_LOCK(sd_state.Cache);
} }
if (bytes_left == 0) {
if (bytes_left == 0)
return size; return size;
}
p += read_count; p += read_count;
bytes_left -= read_count; bytes_left -= read_count;
address += sd_state.BlockLen; address += sd_state.BlockLen;
} while (1); } while (1);
} }
#endif // SD_WRITE #endif // SD_WRITE
#endif // SD_CACHE #endif // SD_CACHE
/** @} */ /** @} */

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,12 +47,11 @@ Berlin, 2007
* @brief MMC-/SD-Card library, Block erase * @brief MMC-/SD-Card library, Block erase
* *
* @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: sd_erase.c,v 1.2 2008/03/28 15:58:44 nvt-se Exp $ * $Id: sd_erase.c,v 1.3 2008/03/28 23:03:05 nvt-se Exp $
*/ */
/** /**
* @addtogroup libsd * @addtogroup libsd
* @{ * @{

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,12 +47,11 @@ Berlin, 2007
* @brief MMC-/SD-Card library, Additional Information * @brief MMC-/SD-Card library, Additional Information
* *
* @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: sd_info.c,v 1.2 2008/03/28 15:58:44 nvt-se Exp $ * $Id: sd_info.c,v 1.3 2008/03/28 23:03:05 nvt-se Exp $
*/ */
/** /**
* @addtogroup libsd * @addtogroup libsd
* @{ * @{
@ -64,45 +62,27 @@ Berlin, 2007
unsigned int unsigned int
sd_read_cid(struct sd_cid *pCID) sd_read_cid(struct sd_cid *pCID)
{ {
return _sd_read_register(pCID, SD_CMD_SEND_CID, sizeof (struct sd_cid)); return _sd_read_register(pCID, SD_CMD_SEND_CID, sizeof (struct sd_cid));
} }
unsigned long unsigned long
sd_get_size(void)
sd_get_size()
{ {
uint32_t size = 0; uint32_t size = 0;
if (uart_lock(UART_MODE_SPI)) { if (uart_lock(UART_MODE_SPI)) {
struct sd_csd csd; struct sd_csd csd;
if (_sd_read_register(&csd, SD_CMD_SEND_CSD, sizeof (struct sd_csd))) { if (_sd_read_register(&csd, SD_CMD_SEND_CSD, sizeof (struct sd_csd))) {
size = SD_CSD_C_SIZE(csd) + 1; size = SD_CSD_C_SIZE(csd) + 1;
size <<= SD_CSD_C_MULT(csd); size <<= SD_CSD_C_MULT(csd);
size <<= 2; size <<= 2;
size <<= sd_state.MaxBlockLen_bit; size <<= sd_state.MaxBlockLen_bit;
} }
uart_unlock(UART_MODE_SPI); uart_unlock(UART_MODE_SPI);
} }
return size; return size;
} }
/** @} */ /** @} */

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -49,11 +48,11 @@ Berlin, 2007
* *
* @author Michael Baar <baar@inf.fu-berlin.de> * @author Michael Baar <baar@inf.fu-berlin.de>
* @date Jan 2007 * @date Jan 2007
* @version $Revision: 1.1 $ * @version $Revision: 1.2 $
* *
* Replace this file for use on another platform. * Replace this file for use on another platform.
* *
* $Id: sd_msb430.c,v 1.1 2008/03/28 15:58:44 nvt-se Exp $ * $Id: sd_msb430.c,v 1.2 2008/03/28 23:03:05 nvt-se Exp $
*/ */
#include "sd_internals.h" #include "sd_internals.h"

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,11 +47,11 @@ Berlin, 2007
* *
* @author Michael Baar <baar@inf.fu-berlin.de> * @author Michael Baar <baar@inf.fu-berlin.de>
* @date Jan 2007 * @date Jan 2007
* @version $Revision: 1.1 $ * @version $Revision: 1.2 $
* *
* Replace this file for use on another platform. * Replace this file for use on another platform.
* *
* $Id: sd_platform.h,v 1.1 2008/03/28 15:58:44 nvt-se Exp $ * $Id: sd_platform.h,v 1.2 2008/03/28 23:03:05 nvt-se Exp $
*/ */
#ifndef __SCATTERWEB_SD_PLATFORM__ #ifndef __SCATTERWEB_SD_PLATFORM__

View file

@ -1,4 +1,3 @@
/* /*
Copyright 2007, Freie Universitaet Berlin. All rights reserved. Copyright 2007, Freie Universitaet Berlin. All rights reserved.
@ -48,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.1 $ * @version $Revision: 1.2 $
* *
* $Id: sdspi.c,v 1.1 2008/03/28 15:58:44 nvt-se Exp $ * $Id: sdspi.c,v 1.2 2008/03/28 23:03:05 nvt-se Exp $
*/ */
#include <msp430x16x.h> #include <msp430x16x.h>