From 2822ff7ddb51b61faab8e7ce94685677b34fc05d Mon Sep 17 00:00:00 2001 From: nvt-se Date: Fri, 28 Mar 2008 23:03:05 +0000 Subject: [PATCH] made more conform with contiki indentation. --- platform/msb430/dev/sd/sd.c | 5 +- platform/msb430/dev/sd/sd.h | 213 +++++++++++++-------------- platform/msb430/dev/sd/sd_cache.c | 128 +++------------- platform/msb430/dev/sd/sd_erase.c | 6 +- platform/msb430/dev/sd/sd_info.c | 26 +--- platform/msb430/dev/sd/sd_msb430.c | 5 +- platform/msb430/dev/sd/sd_platform.h | 5 +- platform/msb430/dev/sd/sdspi.c | 5 +- 8 files changed, 135 insertions(+), 258 deletions(-) diff --git a/platform/msb430/dev/sd/sd.c b/platform/msb430/dev/sd/sd.c index 9e6fdb9a7..ccdf080c9 100644 --- a/platform/msb430/dev/sd/sd.c +++ b/platform/msb430/dev/sd/sd.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,9 +47,9 @@ Berlin, 2007 * @brief MMC-/SD-Card library * * @author Michael Baar - * @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 */ diff --git a/platform/msb430/dev/sd/sd.h b/platform/msb430/dev/sd/sd.h index 8f57621a4..f4f2f05f7 100644 --- a/platform/msb430/dev/sd/sd.h +++ b/platform/msb430/dev/sd/sd.h @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -47,9 +46,9 @@ Berlin, 2007 * @brief MMC-/SD-Card library, Public interface * * @author Michael Baar - * @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,28 +129,30 @@ __attribute__ ((packed)) /// Card access library state #define SD_CACHE_LOCKED 0x01 #define SD_CACHE_DIRTY 0x02 - typedef struct { - char buffer[SD_WRITE_BLOCKLENGTH]; - uint32_t address; - uint8_t state; - } sd_cache_t; - typedef struct { - uint16_t MinBlockLen_bit:4; ///< minimum supported blocklength - uint16_t MaxBlockLen_bit:4; ///< maximum supported blocklength - uint16_t Flags:8; ///< feature flags - uint8_t BlockLen_bit; ///< currently selected blocklength as bit value (n where BlockLen is 2^n) - uint16_t BlockLen; ///< currently selected blocklength for reading and writing -#if SD_CACHE - sd_cache_t *Cache; -#endif - } sd_state_t; - extern volatile sd_state_t sd_state; ///< Card access library state +typedef struct { + char buffer[SD_WRITE_BLOCKLENGTH]; + uint32_t address; + uint8_t state; +} sd_cache_t; + +typedef struct { + uint16_t MinBlockLen_bit:4; ///< minimum supported blocklength + uint16_t MaxBlockLen_bit:4; ///< maximum supported blocklength + uint16_t Flags:8; ///< feature flags + uint8_t BlockLen_bit; ///< currently selected blocklength as bit value (n where BlockLen is 2^n) + uint16_t BlockLen; ///< currently selected blocklength for reading and writing +#if SD_CACHE + sd_cache_t *Cache; +#endif +} sd_state_t; + +extern volatile sd_state_t sd_state; ///< Card access library state /** * @brief Library initialisation */ - void sd_Init(void); +void sd_init(void); /** * @brief Setup ports for sd card communication @@ -167,25 +168,25 @@ __attribute__ ((packed)) /** * @brief Return value of ::sd_init function */ - enum sd_init_ret { - SD_INIT_SUCCESS = 0, - SD_INIT_NOCARD = 1, - SD_INIT_FAILED = 2, - SD_INIT_NOTSUPP = 3 - }; +enum sd_init_ret { + SD_INIT_SUCCESS = 0, + SD_INIT_NOCARD = 1, + SD_INIT_FAILED = 2, + SD_INIT_NOTSUPP = 3 +}; /** * @brief Return value of write functions * @see ::sd_write, ::sd_write_block */ - enum sd_write_ret { - SD_WRITE_SUCCESS = 0, ///< writing successfull - SD_WRITE_PROTECTED_ERR = 1, ///< card write protected - 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_STORE_ERR = 4, ///< storing written data to persistant memory on card failed - SD_WRITE_DMA_ERR = 5 - }; +enum sd_write_ret { + SD_WRITE_SUCCESS = 0, ///< writing successfull + SD_WRITE_PROTECTED_ERR = 1, ///< card write protected + 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_STORE_ERR = 4, ///< storing written data to persistant memory on card failed + SD_WRITE_DMA_ERR = 5 +}; /** * @brief Initialize card and state @@ -195,12 +196,12 @@ __attribute__ ((packed)) * functionality. Initializes the global state struct sd_state. * 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. */ - void sd_close(void); +void sd_close(void); /** * @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 * @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 @@ -265,60 +266,58 @@ __attribute__ ((packed)) * * @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 - - /** - * @brief Read one byte from any address - * This function reads a single byte from any address. It is optimized for best speed - * at any blocklength. - * \Note: blocklength is modified - * - * @param[out] pBuffer Pointer to a buffer to which data is read. It should be least - * 1 byte large - * @param[in] address The address of the byte that shall be read to pBuffer - * - * @return Number of bytes read (usually 1) - */ - bool sd_read_byte(void *pBuffer, const uint32_t address); +/** + * @brief Read one byte from any address + * This function reads a single byte from any address. It is optimized for best speed + * at any blocklength. + * \Note: blocklength is modified + * + * @param[out] pBuffer Pointer to a buffer to which data is read. It should be least + * 1 byte large + * @param[in] address The address of the byte that shall be read to pBuffer + * + * @return Number of bytes read (usually 1) + */ +bool sd_read_byte(void *pBuffer, const uint32_t address); #endif #if SD_WRITE + /** + * @brief Write one complete block at a block aligned address from buffer to card + * + * @param[in] address block aligned address to write to + * @param[in] pBuffer pointer to buffer with a block of data to write + * @return result code (see enum #sd_write_ret) + * + * \Note + * Only supported block size for writing is usually 512 bytes. + */ +enum sd_write_ret sd_write_block(const uint32_t address, + void const (*const pBuffer)); - /** - * @brief Write one complete block at a block aligned address from buffer to card - * - * @param[in] address block aligned address to write to - * @param[in] pBuffer pointer to buffer with a block of data to write - * @return result code (see enum #sd_write_ret) - * - * \Note - * Only supported block size for writing is usually 512 bytes. - */ - enum sd_write_ret sd_write_block(const uint32_t address, - void const (*const pBuffer)); + /** + * @brief Fill one complete block at a block aligned address with + * a single character. + * + * @param[in] address block aligned address to write to + * @param[in] pChar pointer to buffer with a character to write + * @return result code (see enum #sd_write_ret) + * + * @note Use this for settings blocks to 0x00. + * Only supported block size for writing is usually 512 bytes. + */ +enum sd_write_ret sd_set_block(const uint32_t address, + const char (*const pChar)); - /** - * @brief Fill one complete block at a block aligned address with - * a single character. - * - * @param[in] address block aligned address to write to - * @param[in] pChar pointer to buffer with a character to write - * @return result code (see enum #sd_write_ret) - * - * @note Use this for settings blocks to 0x00. - * Only supported block size for writing is usually 512 bytes. - */ - enum sd_write_ret sd_set_block(const uint32_t address, - const char (*const pChar)); - - /** - * @brief Flush the DMA write buffer - * - * Wait for a running DMA write operation to finish - */ - enum sd_write_ret sd_write_flush(void); + /** + * @brief Flush the DMA write buffer + * + * Wait for a running DMA write operation to finish + */ +enum sd_write_ret sd_write_flush(void); #endif #if SD_CACHE @@ -326,22 +325,22 @@ __attribute__ ((packed)) #define SD_GET_LOCK(x) do { while( x ->state & SD_CACHE_LOCKED ) { _NOP(); }; x ->state |= SD_CACHE_LOCKED; } while(0) #define SD_FREE_LOCK(x) do { x ->state &= ~SD_CACHE_LOCKED; } while(0) - /** - * @brief Flush the sd cache - * - * Writes back the cache buffer, if it has been modified. Call this if - * a high level operation has finished and you want to store all data - * persistantly. The write back operation does not use timers. - */ - void sd_cache_flush(void); + /** + * @brief Flush the sd cache + * + * Writes back the cache buffer, if it has been modified. Call this if + * a high level operation has finished and you want to store all data + * persistantly. The write back operation does not use timers. + */ +void sd_cache_flush(void); - /** - * @brief Read a block into the cache buffer - * @internal - * - * You won't usually need this operation. - */ - sd_cache_t *sd_cache_read_block(const uint32_t * blAdr); + /** + * @brief Read a block into the cache buffer + * @internal + * + * You won't usually need this operation. + */ +sd_cache_t *sd_cache_read_block(const uint32_t * blAdr); #endif /** @@ -372,15 +371,13 @@ __attribute__ ((packed)) uint32_t sd_get_size(void); #if SD_READ_ANY - - /** - * @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 - * size bytes large - * @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 - */ + /** + * @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 + * size bytes large + * @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 */ uint16_t sd_read(void *pBuffer, uint32_t address, uint16_t size); #endif diff --git a/platform/msb430/dev/sd/sd_cache.c b/platform/msb430/dev/sd/sd_cache.c index f5008a6e2..374465459 100644 --- a/platform/msb430/dev/sd/sd_cache.c +++ b/platform/msb430/dev/sd/sd_cache.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,9 +47,9 @@ Berlin, 2007 * @brief MMC-/SD-Card library, cached read and write * * @author Michael Baar - * @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 void -_sd_cache_init() +_sd_cache_init(void) { - - uint32_t adr = 0; - + uint32_t addr = 0; sd_state.Cache->address = 1; - sd_state.Cache->state = 0; - // pre-read first block - sd_cache_read_block(&adr); - + sd_cache_read_block(&addr); SD_FREE_LOCK(sd_state.Cache); - } void -_sd_cache_flush() +_sd_cache_flush(void) { - #if SD_WRITE SD_GET_LOCK(sd_state.Cache); - if (sd_state.Cache->state & SD_CACHE_DIRTY) { - sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); - sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer); - sd_state.Cache->state &= ~SD_CACHE_DIRTY; - } - SD_FREE_LOCK(sd_state.Cache); - -#endif /* - */ +#endif } - sd_cache_t * sd_cache_read_block(const uint32_t * pblAdr) { - SD_GET_LOCK(sd_state.Cache); - if (sd_state.Cache->address != *pblAdr) { - sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); - if (sd_state.Cache->state & SD_CACHE_DIRTY) { - sd_write_block(sd_state.Cache->address, sd_state.Cache->buffer); - sd_state.Cache->state &= ~SD_CACHE_DIRTY; - } - sd_state.Cache->address = *pblAdr; - if (!sd_read_block(sd_state.Cache->buffer, *pblAdr)) { - SD_FREE_LOCK(sd_state.Cache); - return false; - } - } - return sd_state.Cache; - } - #if SD_READ_ANY uint16_t sd_read(void *pBuffer, uint32_t address, uint16_t size) { - uint16_t offset; // bytes from aligned address to start of first byte to keep char *p; // pointer to current pos in receive buffer uint16_t bytes_left; // num bytes to read uint16_t read_count; // num bytes to read from current block - - // // parameter processing - // p = (char *)pBuffer; - bytes_left = size; - // align to block offset = sd_AlignAddress(&address); - // // Data transfer - // do { - // calculate block if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) { - read_count = sd_state.BlockLen; - sd_read_block(p, address); - } else { - sd_cache_read_block(&address); - read_count = bytes_left + offset; - - if (read_count > sd_state.BlockLen) - + if (read_count > sd_state.BlockLen) { read_count = sd_state.BlockLen - offset; - - else - + } else { read_count = bytes_left; - + } memcpy(p, sd_state.Cache->buffer + offset, read_count); - SD_FREE_LOCK(sd_state.Cache); - } - bytes_left -= read_count; - - if (bytes_left == 0) - + if (bytes_left == 0) { return size; - + } p += read_count; - address += sd_state.BlockLen; - } while (1); - } - - #endif // SD_READ_ANY #if SD_WRITE uint16_t sd_write(uint32_t address, void *pBuffer, uint16_t size) { - uint16_t offset; // bytes from aligned address to start of first byte to keep char *p; // pointer to current pos in receive buffer uint16_t bytes_left; // num bytes to read uint16_t read_count; // num bytes to read from current block - - // // parameter processing - // p = (char *)pBuffer; - bytes_left = size; - // align to block offset = sd_AlignAddress(&address); - sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT); - // // Data transfer - // do { - // calculate block if ((offset == 0) && (bytes_left >= sd_state.BlockLen)) { - read_count = sd_state.BlockLen; - sd_write_block(address, p); - } else { - sd_cache_read_block(&address); - read_count = bytes_left + offset; - - if (read_count > sd_state.BlockLen) - + if (read_count > sd_state.BlockLen) { read_count = sd_state.BlockLen - offset; - - else - + } else { read_count = bytes_left; - - + } memcpy(sd_state.Cache->buffer + offset, p, read_count); - sd_state.Cache->state |= SD_CACHE_DIRTY; - - SD_FREE_LOCK(sd_state.Cache); - } - - if (bytes_left == 0) - + if (bytes_left == 0) { return size; - + } p += read_count; - bytes_left -= read_count; - address += sd_state.BlockLen; - } while (1); - } - #endif // SD_WRITE - #endif // SD_CACHE - /** @} */ diff --git a/platform/msb430/dev/sd/sd_erase.c b/platform/msb430/dev/sd/sd_erase.c index 732f38fc6..5bab58fac 100644 --- a/platform/msb430/dev/sd/sd_erase.c +++ b/platform/msb430/dev/sd/sd_erase.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,12 +47,11 @@ Berlin, 2007 * @brief MMC-/SD-Card library, Block erase * * @author Michael Baar - * @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 * @{ diff --git a/platform/msb430/dev/sd/sd_info.c b/platform/msb430/dev/sd/sd_info.c index 327b43297..56c4849f9 100644 --- a/platform/msb430/dev/sd/sd_info.c +++ b/platform/msb430/dev/sd/sd_info.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,12 +47,11 @@ Berlin, 2007 * @brief MMC-/SD-Card library, Additional Information * * @author Michael Baar - * @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 * @{ @@ -64,45 +62,27 @@ Berlin, 2007 unsigned int sd_read_cid(struct sd_cid *pCID) { - return _sd_read_register(pCID, SD_CMD_SEND_CID, sizeof (struct sd_cid)); - } unsigned long - -sd_get_size() +sd_get_size(void) { - uint32_t size = 0; - if (uart_lock(UART_MODE_SPI)) { - struct sd_csd 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_MULT(csd); - size <<= 2; - size <<= sd_state.MaxBlockLen_bit; - } - uart_unlock(UART_MODE_SPI); - } - return size; - } - - /** @} */ diff --git a/platform/msb430/dev/sd/sd_msb430.c b/platform/msb430/dev/sd/sd_msb430.c index a78473a50..b282da71c 100644 --- a/platform/msb430/dev/sd/sd_msb430.c +++ b/platform/msb430/dev/sd/sd_msb430.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -49,11 +48,11 @@ Berlin, 2007 * * @author Michael Baar * @date Jan 2007 - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * * 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" diff --git a/platform/msb430/dev/sd/sd_platform.h b/platform/msb430/dev/sd/sd_platform.h index 4c898df13..59be86d17 100644 --- a/platform/msb430/dev/sd/sd_platform.h +++ b/platform/msb430/dev/sd/sd_platform.h @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,11 +47,11 @@ Berlin, 2007 * * @author Michael Baar * @date Jan 2007 - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * * 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__ diff --git a/platform/msb430/dev/sd/sdspi.c b/platform/msb430/dev/sd/sdspi.c index e3dfaf03e..b28186f01 100644 --- a/platform/msb430/dev/sd/sdspi.c +++ b/platform/msb430/dev/sd/sdspi.c @@ -1,4 +1,3 @@ - /* Copyright 2007, Freie Universitaet Berlin. All rights reserved. @@ -48,9 +47,9 @@ Berlin, 2007 * @brief Serial Peripheral Interface for SD library * * @author Michael Baar - * @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