synced with newer version by Michael Baar.
This commit is contained in:
parent
d5c8b18fc2
commit
3fec8ee5e7
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,4 @@
|
|||
|
||||
/*
|
||||
Copyright 2007, Freie Universitaet Berlin. All rights reserved.
|
||||
|
||||
|
@ -41,13 +42,14 @@ Berlin, 2007
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.sd.h
|
||||
* @file ScatterWeb.Sd.h
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library, Public interface
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Jan 2007
|
||||
* @version 0.2
|
||||
* @version $Revision: 1.2 $
|
||||
*
|
||||
* $Id: sd.h,v 1.2 2008/03/28 15:58:43 nvt-se Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -55,25 +57,29 @@ Berlin, 2007
|
|||
* @{
|
||||
*/
|
||||
|
||||
#ifndef SD_H
|
||||
#define SD_H
|
||||
#ifndef __SD_H__
|
||||
#define __SD_H__
|
||||
|
||||
#define SD_BLOCKLENGTH_INVALID 0xFF
|
||||
#define SD_WRITE_BLOCKLENGTH_BIT 9
|
||||
#define SD_WRITE_BLOCKLENGTH 0x200
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* @name Setup, initialisation, configuration
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @name SD Flags
|
||||
* @brief SD Flags
|
||||
* @see sd_state_t
|
||||
* @{
|
||||
*/
|
||||
#define SD_READ_PARTIAL 0x80
|
||||
#define SD_WRITE_PARTIAL 0x40
|
||||
enum sd_state_flags {
|
||||
SD_READ_PARTIAL = 0x80,
|
||||
SD_WRITE_PARTIAL = 0x40,
|
||||
SD_INITIALIZED = 0x01
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
|
@ -87,61 +93,65 @@ Berlin, 2007
|
|||
#ifndef __TI_COMPILER_VERSION__
|
||||
__attribute__ ((packed))
|
||||
#endif
|
||||
typedef struct sd_cid {
|
||||
uint8_t mid;
|
||||
char oid[2];
|
||||
char pnm[5];
|
||||
uint8_t revision;
|
||||
uint32_t psn;
|
||||
uint16_t:4;
|
||||
uint16_t mdt:12;
|
||||
uint8_t crc7:7;
|
||||
uint8_t:1;
|
||||
} sd_cid_t;
|
||||
#if defined(__MSP430GCC__)
|
||||
__attribute__ ((packed))
|
||||
#endif
|
||||
struct sd_cid {
|
||||
uint8_t mid;
|
||||
char oid[2];
|
||||
char pnm[5];
|
||||
uint8_t revision;
|
||||
uint32_t psn;
|
||||
uint16_t:4;
|
||||
uint16_t mdt:12;
|
||||
uint8_t crc7:7;
|
||||
uint8_t:1;
|
||||
};
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#pragma pack()
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @name Card Specific Data register
|
||||
* @{
|
||||
*/
|
||||
typedef struct sd_csd {
|
||||
uint8_t raw[16];
|
||||
} sd_csd_t;
|
||||
|
||||
#define SD_CSD_READ_BL_LEN(csd) ((csd).raw[5] & 0x0F) // uchar : 4
|
||||
#define SD_CSD_READ_PARTIAL(csd) ((csd).raw[6] & 0x80) // bool
|
||||
#define SD_CSD_CCC(csd) (((csd).raw[4]<<4) | ((csd).raw[5]>>4)) // uchar
|
||||
#define SD_CSD_WRITE_PARTIAL(csd) ((csd).raw[13] & 0x04) // bool
|
||||
#define SD_CSD_C_SIZE(csd) ((((csd).raw[6] & 0x03)<<10) | ((csd).raw[7]<<2) | ((csd).raw[8]>>6)) // uint : 12
|
||||
#define SD_CSD_C_MULT(csd) ((((csd).raw[9] & 0x03)<<1) | ((csd).raw[10]>>7)) // uchar : 4
|
||||
struct sd_csd {
|
||||
uint8_t raw[16];
|
||||
};
|
||||
#define SD_CSD_READ_BL_LEN(csd) (( csd ).raw[5] & 0x0F) // uchar : 4
|
||||
#define SD_CSD_READ_PARTIAL(csd) (( csd ).raw[ 6] & 0x80) // bool
|
||||
#define SD_CSD_CCC(csd) ( (( csd ).raw[4]<<4) | (( csd ).raw[5]>>4) ) // uchar
|
||||
#define SD_CSD_WRITE_PARTIAL(csd) (( csd ).raw[13] & 0x04) // bool
|
||||
#define SD_CSD_C_SIZE(csd) ( ((( csd ).raw[6] & 0x03)<<10) | (( csd ).raw[7]<<2) | (( csd ).raw[8]>>6) ) // uint : 12
|
||||
#define SD_CSD_C_MULT(csd) ( ((( csd ).raw[9] & 0x03)<<1) | (( csd ).raw[10]>>7) ) // uchar : 4
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#pragma pack()
|
||||
#endif
|
||||
|
||||
/* Card access library state */
|
||||
/// 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
|
||||
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;
|
||||
sd_cache_t *Cache;
|
||||
#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
|
||||
*/
|
||||
void sd_Init(void);
|
||||
|
||||
/**
|
||||
* @brief Setup ports for sd card communication
|
||||
|
@ -152,13 +162,30 @@ extern volatile sd_state_t sd_state; // Card access library state
|
|||
* If you need to reconfigure the UART for other operations only call
|
||||
* ::Spi_enable to return to SPI mode. ::sd_setup needs to be run only once.
|
||||
*/
|
||||
void sd_init(void);
|
||||
void sd_init_platform(void);
|
||||
|
||||
/**
|
||||
* @brief Return value of ::sd_init function
|
||||
*/
|
||||
enum sd_init_ret { SD_INIT_SUCCESS = 0, SD_INIT_FAILED = 1,
|
||||
SD_INIT_NOTSUPP = 2 };
|
||||
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
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initialize card and state
|
||||
|
@ -168,12 +195,12 @@ enum sd_init_ret { SD_INIT_SUCCESS = 0, SD_INIT_FAILED = 1,
|
|||
* 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 *);
|
||||
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?
|
||||
|
@ -214,14 +241,14 @@ void sd_close(void);
|
|||
* SD_BLOCKLENGTH_INVALID otherwise.
|
||||
*
|
||||
*/
|
||||
uint8_t sd_set_blocklength(const uint8_t blocklength_bit);
|
||||
uint8_t sd_set_blocklength(const uint8_t blocklength_bit);
|
||||
|
||||
/**
|
||||
* @brief Align byte address to current blocklength
|
||||
* @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_align_address(uint32_t * pAddress);
|
||||
uint16_t sd_AlignAddress(uint32_t * pAddress);
|
||||
|
||||
/**
|
||||
* @brief Read one complete block from a block aligned address into buffer
|
||||
|
@ -238,92 +265,83 @@ uint16_t sd_align_address(uint32_t * pAddress);
|
|||
*
|
||||
* @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);
|
||||
|
||||
#endif /* */
|
||||
/**
|
||||
* @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 number of bytes successfully written, zero if write failed
|
||||
*
|
||||
* \Note
|
||||
* Only supported block size for writing is usually 512 bytes.
|
||||
*/
|
||||
uint16_t 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 number of bytes successfully written, zero if write failed
|
||||
*
|
||||
* @note Use this for settings blocks to 0x00.
|
||||
* Only supported block size for writing is usually 512 bytes.
|
||||
*/
|
||||
uint16_t sd_set_block(const uint32_t address, const char (*const pChar));
|
||||
/**
|
||||
* @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 Flush the DMA write buffer
|
||||
*
|
||||
* Wait for a running DMA write operation to finish
|
||||
*/
|
||||
uint16_t sd_write_flush(void);
|
||||
/**
|
||||
* @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);
|
||||
#endif
|
||||
|
||||
#if SD_CACHE
|
||||
#define SD_WAIT_LOCK(x) \
|
||||
while( x ->state & SD_CACHE_LOCKED ) { _NOP(); }
|
||||
#define SD_WAIT_LOCK(x) while( x ->state & SD_CACHE_LOCKED ) { _NOP(); }
|
||||
#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)
|
||||
|
||||
#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 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 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);
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -334,7 +352,7 @@ sd_cache_t *sd_cache_read_block(const uint32_t * blAdr);
|
|||
* @note Data can only be erased in whole blocks. All bytes will be set
|
||||
* predifined character (see CSD). Common cards use 0xFF.
|
||||
*/
|
||||
bool sd_erase_blocks(const uint32_t address, const uint16_t numBlocks);
|
||||
bool sd_erase_blocks(const uint32_t address, const uint16_t numBlocks);
|
||||
|
||||
/**
|
||||
* @brief Read card identification register (CID)
|
||||
|
@ -343,7 +361,7 @@ bool sd_erase_blocks(const uint32_t address, const uint16_t numBlocks);
|
|||
*
|
||||
* @see ::sd_cid_t
|
||||
*/
|
||||
uint16_t sd_read_cid(sd_cid_t * pCID);
|
||||
uint16_t sd_read_cid(struct sd_cid *pCID);
|
||||
|
||||
/**
|
||||
* @brief Read size of card
|
||||
|
@ -351,8 +369,21 @@ uint16_t sd_read_cid(sd_cid_t * pCID);
|
|||
*
|
||||
* Reads the number of bytes in the card memory from the CSD register.
|
||||
*/
|
||||
uint32_t sd_get_size(void);
|
||||
uint32_t sd_get_size(void);
|
||||
|
||||
#endif /* !SD_H */
|
||||
#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
|
||||
*/
|
||||
uint16_t sd_read(void *pBuffer, uint32_t address, uint16_t size);
|
||||
#endif
|
||||
|
||||
#endif /*__SD_H__*/
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -41,68 +41,101 @@ scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
|||
Berlin, 2007
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.sd.cache.c
|
||||
* @file ScatterWeb.Sd.cache.c
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library, cached read and write
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Jan 2007
|
||||
* @version 0.2
|
||||
* @version $Revision: 1.2 $
|
||||
*
|
||||
* $Id: sd_cache.c,v 1.2 2008/03/28 15:58:43 nvt-se Exp $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup libsd
|
||||
* @{
|
||||
*/
|
||||
#include "sd_internals.h"
|
||||
#include "sd.h"
|
||||
|
||||
#if SD_CACHE
|
||||
|
||||
void
|
||||
sd_cache_init(void)
|
||||
_sd_cache_init()
|
||||
{
|
||||
|
||||
uint32_t adr = 0;
|
||||
|
||||
|
||||
sd_state.Cache->address = 1;
|
||||
|
||||
sd_state.Cache->state = 0;
|
||||
|
||||
// pre-read first block
|
||||
sd_cache_read_block(&adr);
|
||||
|
||||
SD_FREE_LOCK(sd_state.Cache);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
sd_cache_flush(void)
|
||||
_sd_cache_flush()
|
||||
{
|
||||
|
||||
#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 false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sd_state.Cache;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -110,65 +143,97 @@ sd_cache_read_block(const uint32_t * pblAdr)
|
|||
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;
|
||||
p = (char *)pBuffer;
|
||||
|
||||
bytes_left = size;
|
||||
|
||||
// align to block
|
||||
offset = sd_align_address(&address);
|
||||
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)
|
||||
|
||||
read_count = sd_state.BlockLen - offset;
|
||||
|
||||
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)
|
||||
|
||||
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;
|
||||
p = (char *)pBuffer;
|
||||
|
||||
bytes_left = size;
|
||||
|
||||
// align to block
|
||||
offset = sd_align_address(&address);
|
||||
offset = sd_AlignAddress(&address);
|
||||
|
||||
|
||||
sd_set_blocklength(SD_WRITE_BLOCKLENGTH_BIT);
|
||||
|
||||
//
|
||||
|
@ -178,25 +243,49 @@ sd_write(uint32_t address, void *pBuffer, uint16_t size)
|
|||
|
||||
// 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)
|
||||
|
||||
read_count = sd_state.BlockLen - offset;
|
||||
|
||||
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)
|
||||
|
||||
return size;
|
||||
|
||||
|
||||
p += read_count;
|
||||
|
||||
bytes_left -= read_count;
|
||||
|
||||
address += sd_state.BlockLen;
|
||||
|
||||
} while (1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -204,4 +293,5 @@ sd_write(uint32_t address, void *pBuffer, uint16_t size)
|
|||
|
||||
#endif // SD_CACHE
|
||||
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -41,16 +41,19 @@ scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
|||
Berlin, 2007
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.sd.erase.c
|
||||
* @file ScatterWeb.Sd.erase.c
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library, Block erase
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Feb 2007
|
||||
* @version 0.2
|
||||
* @version $Revision: 1.2 $
|
||||
*
|
||||
* $Id: sd_erase.c,v 1.2 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup libsd
|
||||
* @{
|
||||
|
@ -64,21 +67,26 @@ sd_erase_blocks(uint32_t address, uint16_t numBlocks)
|
|||
uint8_t ret, r1;
|
||||
uint32_t endAdr;
|
||||
|
||||
if (sd_protected())
|
||||
if (sd_protected()) {
|
||||
return FALSE;
|
||||
ret =
|
||||
sd_send_cmd(SD_CMD_ERASE_WR_BLK_START_ADDR, SD_RESPONSE_TYPE_R1,
|
||||
&address, &r1);
|
||||
if (!ret | r1)
|
||||
}
|
||||
|
||||
ret = _sd_send_cmd(SD_CMD_ERASE_WR_BLK_START_ADDR, SD_RESPONSE_SIZE_R1,
|
||||
&address, &r1);
|
||||
if (!ret | r1) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
endAdr = (numBlocks - 1) * sd_state.BlockLen;
|
||||
endAdr += address;
|
||||
ret =
|
||||
sd_send_cmd(SD_CMD_ERASE_WR_BLK_END_ADDR, SD_RESPONSE_TYPE_R1, &endAdr,
|
||||
&r1);
|
||||
if (!ret | r1)
|
||||
|
||||
ret = _sd_send_cmd(SD_CMD_ERASE_WR_BLK_END_ADDR, SD_RESPONSE_SIZE_R1,
|
||||
&endAdr, &r1);
|
||||
if (!ret | r1) {
|
||||
return FALSE;
|
||||
ret = sd_send_cmd(SD_CMD_ERASE, SD_RESPONSE_TYPE_R1, NULL, &r1);
|
||||
}
|
||||
|
||||
ret = _sd_send_cmd(SD_CMD_ERASE, SD_RESPONSE_SIZE_R1, NULL, &r1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,16 +41,19 @@ scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
|||
Berlin, 2007
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.sd.info.c
|
||||
* @file ScatterWeb.Sd.info.c
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library, Additional Information
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Feb 2007
|
||||
* @version 0.2
|
||||
* @version $Revision: 1.2 $
|
||||
*
|
||||
* $Id: sd_info.c,v 1.2 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup libsd
|
||||
* @{
|
||||
|
@ -58,29 +61,48 @@ Berlin, 2007
|
|||
#include "sd_internals.h"
|
||||
#include "sd.h"
|
||||
|
||||
uint16_t
|
||||
sd_read_cid(sd_cid_t * pCID)
|
||||
unsigned int
|
||||
sd_read_cid(struct sd_cid *pCID)
|
||||
{
|
||||
return sd_read_register(pCID, SD_CMD_SEND_CID, sizeof (sd_cid_t));
|
||||
|
||||
return _sd_read_register(pCID, SD_CMD_SEND_CID, sizeof (struct sd_cid));
|
||||
|
||||
}
|
||||
|
||||
unsigned long
|
||||
sd_get_size(void)
|
||||
|
||||
sd_get_size()
|
||||
{
|
||||
|
||||
uint32_t size = 0;
|
||||
|
||||
if (uart_lock(UART_MODE_SPI)) {
|
||||
sd_csd_t csd;
|
||||
|
||||
if (sd_read_register(&csd, SD_CMD_SEND_CSD, sizeof (sd_csd_t))) {
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -42,31 +42,30 @@ Berlin, 2007
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.sd.internals.h
|
||||
* @file ScatterWeb.Sd.internals.h
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Jan 2007
|
||||
* @version 0.2
|
||||
* @version $Revision: 1.2 $
|
||||
*
|
||||
* Header file containing private declarations used MMC-/SD-Card library.
|
||||
*
|
||||
* $Id: sd_internals.h,v 1.2 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
#ifndef SD_INTERNALS_H
|
||||
#define SD_INTERNALS_H
|
||||
#ifndef SD_INT_H_
|
||||
#define SD_INT_H_
|
||||
|
||||
#include <string.h>
|
||||
#include "contiki-msb430.h"
|
||||
#include "sd_platform.h"
|
||||
|
||||
#define RETF(x) if( ! x ) return false;
|
||||
#define MIN(x, y) ( x < y ) ? x : y
|
||||
#define RETF(x) if( ! x ) return FALSE;
|
||||
|
||||
/**
|
||||
* @name SD Card SPI responses
|
||||
*/
|
||||
|
||||
struct sd_response_r1_bits {
|
||||
struct sd_response_r1 {
|
||||
uint8_t in_idle_state:1;
|
||||
uint8_t erase_reset:1;
|
||||
uint8_t illegal_cmd:1;
|
||||
|
@ -77,17 +76,41 @@ struct sd_response_r1_bits {
|
|||
uint8_t start_bit:1;
|
||||
};
|
||||
|
||||
struct sd_response_r2_bits {
|
||||
uint8_t card_locked:1;
|
||||
uint8_t write_failed:1;
|
||||
uint8_t unspecified_err:1;
|
||||
uint8_t controller_err:1;
|
||||
uint8_t ecc_failed:1;
|
||||
uint8_t protect_violation:1;
|
||||
uint8_t erase_param:1;
|
||||
uint8_t out_of_range:1;
|
||||
struct sd_response_r2 {
|
||||
uint16_t in_idle_state:1;
|
||||
uint16_t erase_reset:1;
|
||||
uint16_t illegal_cmd:1;
|
||||
uint16_t crc_err:1;
|
||||
uint16_t erase_seq_err:1;
|
||||
uint16_t address_err:1;
|
||||
uint16_t param_err:1;
|
||||
uint16_t start_bit:1;
|
||||
|
||||
uint16_t card_locked:1;
|
||||
uint16_t write_failed:1;
|
||||
uint16_t unspecified_err:1;
|
||||
uint16_t controller_err:1;
|
||||
uint16_t ecc_failed:1;
|
||||
uint16_t protect_violation:1;
|
||||
uint16_t erase_param:1;
|
||||
uint16_t out_of_range:1;
|
||||
};
|
||||
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#pragma pack(1)
|
||||
#endif
|
||||
struct sd_response_r3 {
|
||||
struct sd_response_r1 r1;
|
||||
#ifdef __GNUC__
|
||||
__attribute__ ((packed))
|
||||
#endif
|
||||
uint32_t ocr;
|
||||
};
|
||||
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#pragma pack()
|
||||
#endif
|
||||
|
||||
struct sd_read_error_token {
|
||||
uint8_t unspecified_err:1;
|
||||
uint8_t controller_err:1;
|
||||
|
@ -102,132 +125,112 @@ struct sd_data_error_token {
|
|||
uint8_t cc_error:1;
|
||||
uint8_t ecc_error:1;
|
||||
uint8_t out_of_range:1;
|
||||
uint8_t:4; // always 0 (-> SD_DATA_ERROR_TOKEN_MASK)
|
||||
uint8_t:4; // always 0 (-> SD_DATA_ERROR_TOKEN_MASK)
|
||||
};
|
||||
|
||||
typedef struct sd_response_r1 {
|
||||
struct sd_response_r1_bits r1;
|
||||
} sd_response_r1_t;
|
||||
|
||||
typedef struct sd_response_r2 {
|
||||
struct sd_response_r1_bits r1;
|
||||
struct sd_response_r2_bits r2;
|
||||
} sd_response_r2_t;
|
||||
|
||||
typedef struct sd_response_r3 {
|
||||
uint32_t ocr;
|
||||
struct sd_response_r1_bits r1;
|
||||
} sd_response_r3_t;
|
||||
|
||||
typedef struct sd_read_error_token sd_read_error_token_t;
|
||||
typedef struct sd_data_error_token sd_data_error_t;
|
||||
|
||||
#define SD_DATA_ERROR_TOKEN_MASK 0x0F // mask for error token
|
||||
#define SD_R1_ERROR_MASK 0x7C // mask for error bits in r1 response
|
||||
#define SD_DATA_ERROR_TOKEN_MASK 0x0F ///< mask for error token
|
||||
#define SD_R1_ERROR_MASK 0x7C ///< mask for error bits in r1 response
|
||||
///@}
|
||||
|
||||
/**
|
||||
* @name Private interface
|
||||
*/
|
||||
|
||||
// Read operating condition
|
||||
bool sd_get_op_cond(sd_response_r1_t * pResponse);
|
||||
/// Reset card
|
||||
int _sd_reset(struct sd_response_r3 *pOpCond);
|
||||
|
||||
// Reset card
|
||||
bool sd_reset(void);
|
||||
/// Send command and read response
|
||||
bool _sd_send_cmd(const uint8_t command,
|
||||
const int response_size,
|
||||
const void *pArg, void (*const pResponse)
|
||||
);
|
||||
|
||||
// Send command and read response
|
||||
bool sd_send_cmd(const uint8_t command, const uint8_t response_type,
|
||||
const void *pArg, void (*const pResponse));
|
||||
/// Wait for card to leave idle mode
|
||||
bool _sd_wait_standby(struct sd_response_r3 *pOpCond);
|
||||
|
||||
// Select card
|
||||
void sd_select(void);
|
||||
/// Read card register
|
||||
uint16_t _sd_read_register(void (*const pBuffer), const uint8_t cmd,
|
||||
const uint16_t size);
|
||||
|
||||
// Unselect card
|
||||
void sd_unselect(void);
|
||||
/// Begin block read operation
|
||||
bool _sd_read_start(const uint8_t cmd, const uint32_t address);
|
||||
|
||||
// Wait for card to leave idle mode
|
||||
bool sd_wait_standby(void);
|
||||
|
||||
// Read card register
|
||||
uint16_t sd_read_register(void (*const pBuffer), const uint8_t cmd,
|
||||
const uint16_t size);
|
||||
|
||||
// Begin block read operation
|
||||
bool sd_read_start(const uint8_t cmd, const uint32_t address);
|
||||
|
||||
// Wait for begin of data
|
||||
bool sd_read_wait(void);
|
||||
|
||||
// Cancel block read operation
|
||||
void sd_read_stop(const uint16_t count);
|
||||
/// Cancel block read operation
|
||||
void _sd_read_stop(const uint16_t count);
|
||||
|
||||
#if SD_WRITE
|
||||
uint16_t sd_write_block_x(const uint32_t * pAddress, const void * pBuffer,
|
||||
bool incPtr);
|
||||
|
||||
#endif /* */
|
||||
enum sd_write_ret _sd_write_block(const uint32_t * pAddress,
|
||||
const void *pBuffer, int increment);
|
||||
#endif
|
||||
|
||||
#if SD_CACHE
|
||||
void sd_cache_init(void);
|
||||
void sd_cache_flush(void);
|
||||
|
||||
#endif /* */
|
||||
void _sd_cache_init();
|
||||
void _sd_cache_flush();
|
||||
#endif
|
||||
///@}
|
||||
|
||||
#define SD_TOKEN_READ 0xFE
|
||||
#define SD_TOKEN_WRITE 0xFE
|
||||
#define SD_TOKEN_ZP 0xFF
|
||||
#define SD_TOKEN_READ 0xFE
|
||||
#define SD_TOKEN_WRITE 0xFE
|
||||
#define SD_TOKEN_ZP 0xFF
|
||||
|
||||
#define SD_TIMEOUT_IDLE 1000 // # of poll-cycles for reset procedure (takes some time)
|
||||
#define SD_TIMEOUT_READ 20000
|
||||
#define SD_TIMEOUT_NCR 8 // 8-64 cycles
|
||||
#define SD_TIMEOUT_IDLE 1000 // # of poll-cycles for reset procedure (takes some time)
|
||||
#define SD_TIMEOUT_READ 20000
|
||||
#define SD_TIMEOUT_NCR 8 // 8-64 cycles
|
||||
#define SD_RESET_RETRY_COUNT 4
|
||||
|
||||
#define SD_V_MASK 0x003E0000 // 3,4 - 2,9 V
|
||||
#define SD_OCR_BUSY(ocr) ((ocr & 0x80000000) == 0)
|
||||
#define SD_V_MASK 0x003E0000 // 3,4 - 2,9 V
|
||||
#define SD_HCR_BIT 0x40000000
|
||||
|
||||
#define SD_RESPONSE_TYPE_R1 1
|
||||
#define SD_RESPONSE_TYPE_R2 2
|
||||
#define SD_RESPONSE_TYPE_R3 5
|
||||
#define SD_RESPONSE_SIZE_R1 1
|
||||
#define SD_RESPONSE_SIZE_R2 2
|
||||
#define SD_RESPONSE_SIZE_R3 5
|
||||
|
||||
/**
|
||||
* @name Command classes
|
||||
* @{
|
||||
*/
|
||||
#define SD_CCC_BASIC BIT0
|
||||
#define SD_CCC_BLOCK_READ BIT2
|
||||
#define SD_CCC_BLOCK_WRITE BIT4
|
||||
#define SD_CCC_APP_SPECIFIC BIT8
|
||||
#define SD_CCC_BASIC BIT0
|
||||
#define SD_CCC_BLOCK_READ BIT2
|
||||
#define SD_CCC_BLOCK_WRITE BIT4
|
||||
#define SD_CCC_APP_SPECIFIC BIT8
|
||||
|
||||
#if SD_WRITE
|
||||
#define SD_DEFAULT_MINCCC (SD_CCC_BASIC | \
|
||||
SD_CCC_BLOCK_READ | SD_CCC_APP_SPECIFIC | SD_CCC_BLOCK_WRITE)
|
||||
#else /* */
|
||||
SD_CCC_BLOCK_READ | \
|
||||
SD_CCC_APP_SPECIFIC | \
|
||||
SD_CCC_BLOCK_WRITE)
|
||||
#else
|
||||
#define SD_DEFAULT_MINCCC (SD_CCC_BASIC | \
|
||||
SD_CCC_BLOCK_READ | SD_CCC_APP_SPECIFIC)
|
||||
#endif /* */
|
||||
SD_CCC_BLOCK_READ | \
|
||||
SD_CCC_APP_SPECIFIC)
|
||||
#endif
|
||||
//@}
|
||||
|
||||
/**
|
||||
* @name Commands
|
||||
* @{
|
||||
*/
|
||||
#define SD_CMD_GO_IDLE_STATE 0 // R1 "reset command"
|
||||
#define SD_CMD_SEND_OP_COND 1 // R1 (MMC only!)
|
||||
#define SD_CMD_SEND_CSD 9 // R1
|
||||
#define SD_CMD_SEND_CID 10 // R1
|
||||
#define SD_CMD_STOP_TRANSMISSION 12 // R1b
|
||||
#define SD_CMD_SEND_STATUS 13 // R2
|
||||
#define SD_CMD_SET_BLOCKLENGTH 16
|
||||
#define SD_CMD_READ_SINGLE_BLOCK 17 // R1
|
||||
#define SD_CMD_READ_MULTI_BLOCK 18 // R1
|
||||
#define SD_CMD_WRITE_SINGLE_BLOCK 24 // R1
|
||||
#define SD_CMD_ERASE_WR_BLK_START_ADDR 32 // R1
|
||||
#define SD_CMD_ERASE_WR_BLK_END_ADDR 33 // R1
|
||||
#define SD_CMD_ERASE 38 // R1b
|
||||
#define SD_CMD_APP_SECIFIC_CMD 55
|
||||
#define SD_CMD_READ_OCR 58 // R3 OCR = voltage table
|
||||
#define SD_CMD_GO_IDLE_STATE 0 // R1 "reset command"
|
||||
#define SD_CMD_SEND_OP_COND 1 // R1 (MMC only!)
|
||||
#define SD_CMD_SEND_CSD 9 // R1
|
||||
#define SD_CMD_SEND_CID 10 // R1
|
||||
#define SD_CMD_STOP_TRANSMISSION 12 // R1b
|
||||
#define SD_CMD_SEND_STATUS 13 // R2
|
||||
#define SD_CMD_SET_BLOCKLENGTH 16
|
||||
#define SD_CMD_READ_SINGLE_BLOCK 17 // R1
|
||||
#define SD_CMD_READ_MULTI_BLOCK 18 // R1
|
||||
#define SD_CMD_WRITE_SINGLE_BLOCK 24 // R1
|
||||
#define SD_CMD_ERASE_WR_BLK_START_ADDR 32 // R1
|
||||
#define SD_CMD_ERASE_WR_BLK_END_ADDR 33 // R1
|
||||
#define SD_CMD_ERASE 38 // R1b
|
||||
#define SD_CMD_APP_SECIFIC_CMD 55
|
||||
#define SD_CMD_READ_OCR 58 // R3
|
||||
|
||||
#define SD_ACMD_SEND_OP_COND 41 // R1
|
||||
#define SD_ACMD_SEND_OP_COND 41 // R1
|
||||
//@}
|
||||
|
||||
#endif /* !SD_INTERNALS_H */
|
||||
#endif /*SD_INT_H_ */
|
||||
|
|
92
platform/msb430/dev/sd/sd_msb430.c
Normal file
92
platform/msb430/dev/sd/sd_msb430.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
|
||||
/*
|
||||
Copyright 2007, Freie Universitaet Berlin. All rights reserved.
|
||||
|
||||
These sources were developed at the Freie Universität Berlin, Computer
|
||||
Systems and Telematics group.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
This software is provided by FUB and the contributors on an "as is"
|
||||
basis, without any representations or warranties of any kind, express
|
||||
or implied including, but not limited to, representations or
|
||||
warranties of non-infringement, merchantability or fitness for a
|
||||
particular purpose. In no event shall FUB or contributors be liable
|
||||
for any direct, indirect, incidental, special, exemplary, or
|
||||
consequential damages (including, but not limited to, procurement of
|
||||
substitute goods or services; loss of use, data, or profits; or
|
||||
business interruption) however caused and on any theory of liability,
|
||||
whether in contract, strict liability, or tort (including negligence
|
||||
or otherwise) arising in any way out of the use of this software, even
|
||||
if advised of the possibility of such damage.
|
||||
|
||||
This implementation was developed by the CST group at the FUB.
|
||||
|
||||
For documentation and questions please use the web site
|
||||
http://scatterweb.mi.fu-berlin.de and the mailinglist
|
||||
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
||||
Berlin, 2007
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.Sd.MSB430.c
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library initialisation for MSB430
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Jan 2007
|
||||
* @version $Revision: 1.1 $
|
||||
*
|
||||
* Replace this file for use on another platform.
|
||||
*
|
||||
* $Id: sd_msb430.c,v 1.1 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
#include "sd_internals.h"
|
||||
|
||||
void
|
||||
sd_init_platform(void)
|
||||
{
|
||||
sdspi_init();
|
||||
|
||||
P5SEL |= 0x0E; // 00 00 11 10 -> Dout, Din, Clk = peripheral (now done in UART module)
|
||||
P5SEL &= ~0x01; // 00 00 00 01 -> Cs = I/O
|
||||
P5OUT |= 0x01; // 00 00 00 01 -> Cs = High
|
||||
P5DIR |= 0x0D; // 00 00 11 01 -> Dout, Clk, Cs = output
|
||||
P5DIR &= ~0x02; // 00 00 00 10 -> Din = Input
|
||||
P2SEL &= ~0x40; // 11 00 00 00 -> protect, detect = I/O
|
||||
P2DIR &= ~0x40; // 11 00 00 00 -> protect, detect = input
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Activate SD Card on SPI Bus
|
||||
* \internal
|
||||
*/
|
||||
__inline void
|
||||
sdspi_select()
|
||||
{
|
||||
P5OUT &= ~0x01; // Card Select
|
||||
}
|
||||
|
||||
__inline void
|
||||
sdspi_unselect()
|
||||
{
|
||||
UART_WAIT_TXDONE();
|
||||
|
||||
P5OUT |= 0x01; // Card Deselect
|
||||
sdspi_tx(0xFF);
|
||||
}
|
65
platform/msb430/dev/sd/sd_platform.h
Normal file
65
platform/msb430/dev/sd/sd_platform.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
/*
|
||||
Copyright 2007, Freie Universitaet Berlin. All rights reserved.
|
||||
|
||||
These sources were developed at the Freie Universität Berlin, Computer
|
||||
Systems and Telematics group.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
This software is provided by FUB and the contributors on an "as is"
|
||||
basis, without any representations or warranties of any kind, express
|
||||
or implied including, but not limited to, representations or
|
||||
warranties of non-infringement, merchantability or fitness for a
|
||||
particular purpose. In no event shall FUB or contributors be liable
|
||||
for any direct, indirect, incidental, special, exemplary, or
|
||||
consequential damages (including, but not limited to, procurement of
|
||||
substitute goods or services; loss of use, data, or profits; or
|
||||
business interruption) however caused and on any theory of liability,
|
||||
whether in contract, strict liability, or tort (including negligence
|
||||
or otherwise) arising in any way out of the use of this software, even
|
||||
if advised of the possibility of such damage.
|
||||
|
||||
This implementation was developed by the CST group at the FUB.
|
||||
|
||||
For documentation and questions please use the web site
|
||||
http://scatterweb.mi.fu-berlin.de and the mailinglist
|
||||
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
||||
Berlin, 2007
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.Sd.Platform.h
|
||||
* @ingroup libsd
|
||||
* @brief MMC-/SD-Card library initialisation for MSB430
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @date Jan 2007
|
||||
* @version $Revision: 1.1 $
|
||||
*
|
||||
* Replace this file for use on another platform.
|
||||
*
|
||||
* $Id: sd_platform.h,v 1.1 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
#ifndef __SCATTERWEB_SD_PLATFORM__
|
||||
#define __SCATTERWEB_SD_PLATFORM__
|
||||
|
||||
#include <msp430x16x.h>
|
||||
#include "contiki.h"
|
||||
#include "dev/msb430-uart1.h"
|
||||
|
||||
#endif
|
226
platform/msb430/dev/sd/sdspi.c
Normal file
226
platform/msb430/dev/sd/sdspi.c
Normal file
|
@ -0,0 +1,226 @@
|
|||
|
||||
/*
|
||||
Copyright 2007, Freie Universitaet Berlin. All rights reserved.
|
||||
|
||||
These sources were developed at the Freie Universität Berlin, Computer
|
||||
Systems and Telematics group.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
This software is provided by FUB and the contributors on an "as is"
|
||||
basis, without any representations or warranties of any kind, express
|
||||
or implied including, but not limited to, representations or
|
||||
warranties of non-infringement, merchantability or fitness for a
|
||||
particular purpose. In no event shall FUB or contributors be liable
|
||||
for any direct, indirect, incidental, special, exemplary, or
|
||||
consequential damages (including, but not limited to, procurement of
|
||||
substitute goods or services; loss of use, data, or profits; or
|
||||
business interruption) however caused and on any theory of liability,
|
||||
whether in contract, strict liability, or tort (including negligence
|
||||
or otherwise) arising in any way out of the use of this software, even
|
||||
if advised of the possibility of such damage.
|
||||
|
||||
This implementation was developed by the CST group at the FUB.
|
||||
|
||||
For documentation and questions please use the web site
|
||||
http://scatterweb.mi.fu-berlin.de and the mailinglist
|
||||
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
||||
Berlin, 2007
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.Spi.c
|
||||
* @ingroup libsdspi
|
||||
* @brief Serial Peripheral Interface for SD library
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @version $Revision: 1.1 $
|
||||
*
|
||||
* $Id: sdspi.c,v 1.1 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
#include <msp430x16x.h>
|
||||
#include "contiki-msb430.h"
|
||||
|
||||
#if SPI_DMA_WRITE
|
||||
uint8_t sdspi_dma_lock;
|
||||
#endif
|
||||
|
||||
#ifndef U1IFG
|
||||
#define U1IFG IFG2
|
||||
#endif
|
||||
|
||||
#define SPI_IDLE_SYMBOL 0xFF
|
||||
|
||||
void
|
||||
sdspi_init(void)
|
||||
{
|
||||
#if SPI_DMA_WRITE
|
||||
sdspi_dma_lock = FALSE;
|
||||
#endif
|
||||
|
||||
/* The 16-bit value of UxBR0+UxBR1 is the division factor of the USART clock
|
||||
* source, BRCLK. The maximum baud rate that can be generated in master
|
||||
* mode is BRCLK/2. The maximum baud rate that can be generated in slave
|
||||
* mode is BRCLK. The modulator in the USART baud rate generator is not used
|
||||
* for SPI mode and is recommended to be set to 000h. The UCLK frequency is
|
||||
* given by:
|
||||
* Baud rate = BRCLK / UxBR with UxBR= [UxBR1, UxBR0]
|
||||
*/
|
||||
uart_set_speed(UART_MODE_SPI, 0x02, 0x00, 0x00);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdspi_rx(void)
|
||||
{
|
||||
UART_TX = SPI_IDLE_SYMBOL;
|
||||
UART_WAIT_RX();
|
||||
return (UART_RX);
|
||||
}
|
||||
|
||||
void
|
||||
sdspi_tx(register const uint8_t c)
|
||||
{
|
||||
UART_TX = c;
|
||||
UART_WAIT_TXDONE();
|
||||
}
|
||||
|
||||
#if SPI_DMA_READ || SPI_DMA_WRITE
|
||||
void
|
||||
sdspi_dma_wait(void)
|
||||
{
|
||||
while (DMA0CTL & DMAEN) {
|
||||
_NOP();
|
||||
} // Wait until a previous transfer is complete
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
sdspi_read(void *pDestination, const uint16_t size, const bool incDest)
|
||||
{
|
||||
#if SPI_DMA_READ
|
||||
sdspi_dma_wait();
|
||||
|
||||
UART_RESET_RXTX(); // clear interrupts
|
||||
|
||||
// Configure the DMA transfer
|
||||
DMA0SA = (uint16_t) & UART_RX; // source DMA address
|
||||
DMA0DA = (uint16_t) pDestination; // destination DMA address
|
||||
DMA0SZ = size; // number of bytes to be transferred
|
||||
DMA1SA = (uint16_t) & UART_TX; // source DMA address (constant 0xff)
|
||||
DMA1DA = DMA1SA; // destination DMA address
|
||||
DMA1SZ = size - 1; // number of bytes to be transferred
|
||||
DMACTL0 = DMA0TSEL_9 | DMA1TSEL_9; // trigger is UART1 receive for both DMA0 and DMA1
|
||||
DMA0CTL = DMADT_0 | // Single transfer mode
|
||||
DMASBDB | // Byte mode
|
||||
DMADSTINCR0 | DMADSTINCR1 | // Increment destination
|
||||
DMAEN; // Enable DMA
|
||||
if (!incDest) {
|
||||
DMA0CTL &= ~(DMADSTINCR0 | DMADSTINCR1);
|
||||
}
|
||||
|
||||
DMA1CTL = DMADT_0 | // Single transfer mode
|
||||
DMASBDB | // Byte mode
|
||||
DMAEN; // Enable DMA
|
||||
|
||||
UART_TX = SPI_IDLE_SYMBOL; // Initiate transfer by sending the first byte
|
||||
sdspi_dma_wait();
|
||||
|
||||
#else
|
||||
register uint8_t *p = (uint8_t *) pDestination;
|
||||
register uint16_t i = size;
|
||||
|
||||
do {
|
||||
UART_TX = SPI_IDLE_SYMBOL;
|
||||
UART_WAIT_RX();
|
||||
*p = UART_RX;
|
||||
if (incDest) {
|
||||
p++;
|
||||
}
|
||||
i--;
|
||||
} while (i);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SPI_WRITE
|
||||
void
|
||||
sdspi_write(const void *pSource, const uint16_t size, const int increment)
|
||||
{
|
||||
#if SPI_DMA_WRITE
|
||||
sdspi_dma_wait();
|
||||
|
||||
UART_RESET_RXTX(); // clear interrupts
|
||||
|
||||
// Configure the DMA transfer
|
||||
DMA0SA = ((uint16_t) pSource) + 1; // source DMA address
|
||||
DMA0DA = (uint16_t) & UART_TX; // destination DMA address
|
||||
DMA0SZ = size - 1; // number of bytes to be transferred
|
||||
DMACTL0 = DMA0TSEL_9; // trigger is UART1 receive
|
||||
DMA0CTL = DMADT_0 | // Single transfer mode
|
||||
DMASBDB | // Byte mode
|
||||
DMASRCINCR_3 | // Increment source
|
||||
DMAEN; // Enable DMA
|
||||
if (increment == 0) {
|
||||
DMA0CTL &= ~DMASRCINCR_3;
|
||||
}
|
||||
|
||||
sdspi_dma_lock = TRUE;
|
||||
SPI_TX = ((uint8_t *) pSource)[0];
|
||||
#else
|
||||
register uint8_t *p = (uint8_t *) pSource;
|
||||
register uint16_t i = size;
|
||||
|
||||
do {
|
||||
UART_TX = *p;
|
||||
UART_WAIT_TXDONE();
|
||||
UART_RX;
|
||||
p += increment;
|
||||
i--;
|
||||
} while (i);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
sdspi_idle(register const uint16_t clocks)
|
||||
{
|
||||
register uint16_t i = clocks;
|
||||
|
||||
do {
|
||||
UART_TX = SPI_IDLE_SYMBOL;
|
||||
UART_WAIT_RX();
|
||||
UART_RX;
|
||||
i--;
|
||||
} while (i);
|
||||
}
|
||||
|
||||
|
||||
uint16_t
|
||||
sdspi_wait_token(const uint8_t feed, const uint8_t mask,
|
||||
const uint8_t token, const uint16_t timeout)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
uint8_t rx;
|
||||
|
||||
do {
|
||||
UART_TX = feed;
|
||||
UART_WAIT_RX();
|
||||
rx = UART_RX;
|
||||
i++;
|
||||
} while (((rx & mask) != token) && (i < timeout));
|
||||
return i;
|
||||
}
|
143
platform/msb430/dev/sd/sdspi.h
Normal file
143
platform/msb430/dev/sd/sdspi.h
Normal file
|
@ -0,0 +1,143 @@
|
|||
|
||||
/*
|
||||
Copyright 2007, Freie Universitaet Berlin. All rights reserved.
|
||||
|
||||
These sources were developed at the Freie Universität Berlin, Computer
|
||||
Systems and Telematics group.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
This software is provided by FUB and the contributors on an "as is"
|
||||
basis, without any representations or warranties of any kind, express
|
||||
or implied including, but not limited to, representations or
|
||||
warranties of non-infringement, merchantability or fitness for a
|
||||
particular purpose. In no event shall FUB or contributors be liable
|
||||
for any direct, indirect, incidental, special, exemplary, or
|
||||
consequential damages (including, but not limited to, procurement of
|
||||
substitute goods or services; loss of use, data, or profits; or
|
||||
business interruption) however caused and on any theory of liability,
|
||||
whether in contract, strict liability, or tort (including negligence
|
||||
or otherwise) arising in any way out of the use of this software, even
|
||||
if advised of the possibility of such damage.
|
||||
|
||||
This implementation was developed by the CST group at the FUB.
|
||||
|
||||
For documentation and questions please use the web site
|
||||
http://scatterweb.mi.fu-berlin.de and the mailinglist
|
||||
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
|
||||
Berlin, 2007
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup uart1
|
||||
* @ingroup libsd_spi
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ScatterWeb.Spi.h
|
||||
* @ingroup libsdspi
|
||||
* @brief Serial Peripheral Interface
|
||||
*
|
||||
* @author Michael Baar <baar@inf.fu-berlin.de>
|
||||
* @version $Revision: 1.1 $
|
||||
*
|
||||
* $Id: sdspi.h,v 1.1 2008/03/28 15:58:44 nvt-se Exp $
|
||||
*/
|
||||
|
||||
#ifndef __SPI_H__
|
||||
#define __SPI_H__
|
||||
|
||||
#ifndef SPI_DMA_READ
|
||||
#define SPI_DMA_READ 0
|
||||
#endif
|
||||
#ifndef SPI_DMA_WRITE
|
||||
#define SPI_DMA_WRITE 0
|
||||
#endif
|
||||
#ifndef SPI_WRITE
|
||||
#define SPI_WRITE 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Init SPI driver
|
||||
*/
|
||||
void sdspi_init(void);
|
||||
|
||||
/// chipselect
|
||||
__inline void sdspi_select();
|
||||
__inline void sdspi_unselect();
|
||||
|
||||
/**
|
||||
* @brief Receive one byte from SPI
|
||||
*/
|
||||
uint8_t sdspi_rx(void);
|
||||
|
||||
/**
|
||||
* @brief Send one byte to SPI
|
||||
*
|
||||
* @param[in] c Byte to send
|
||||
*/
|
||||
void sdspi_tx(const uint8_t c);
|
||||
|
||||
/**
|
||||
* @brief Read a number of bytes from SPI
|
||||
*
|
||||
* @param[in] pDestination Pointer to buffer to store bytestream
|
||||
* @param[in] size Number of bytes to store in pDestination
|
||||
* @param[in] incDest Increment destination pointer after each byte by one (or not)
|
||||
*/
|
||||
void sdspi_read(void *pDestination, const uint16_t size, const bool incDest);
|
||||
|
||||
#if SPI_DMA_WRITE
|
||||
extern uint8_t sdspi_dma_lock;
|
||||
#endif
|
||||
#if SPI_DMA_READ || SPI_DMA_WRITE
|
||||
void sdspi_dma_wait(void);
|
||||
#endif
|
||||
|
||||
#if SPI_WRITE
|
||||
/**
|
||||
* @brief Write a number of bytes to SPI
|
||||
*
|
||||
* @param[in] pSource Pointer to buffer with data to send
|
||||
* @param[in] size Number of bytes to send
|
||||
* @param[in] startToken First byte to send before starting to send size bytes from pSource
|
||||
* @param[in] incSource Increment source pointer after each byte by one (or not)
|
||||
*/
|
||||
void sdspi_write(const void *pSource,
|
||||
const uint16_t size, const int increment);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Wait a number of clock cycles
|
||||
*
|
||||
* @param[in] clocks Wait clocks x 8 cycles
|
||||
*/
|
||||
void sdspi_idle(const uint16_t clocks);
|
||||
|
||||
/**
|
||||
* @brief Read bytes from SPI until token is received
|
||||
*
|
||||
* @param[in] token Token to wait for
|
||||
* @param[in] timeout Maximum number of bytes to read
|
||||
* @return true if token received, false otherwise
|
||||
*/
|
||||
uint16_t sdspi_wait_token(const uint8_t feed, const uint8_t mask,
|
||||
const uint8_t token, const uint16_t timeout);
|
||||
|
||||
#endif /*__SPI_H__*/
|
||||
|
||||
/** @} */
|
Loading…
Reference in a new issue