Skip to content

Commit

Permalink
Merge pull request #190 from rpavlik/rebased-sd-improvements
Browse files Browse the repository at this point in the history
Rebased sd improvements
  • Loading branch information
dcnewman committed Dec 28, 2017
2 parents 1b9f7b7 + 5a3cdcd commit 4d8456e
Show file tree
Hide file tree
Showing 14 changed files with 506 additions and 320 deletions.
17 changes: 17 additions & 0 deletions firmware/src/MightyBoard/Motherboard/lib_sd/ChangeLog
Expand Up @@ -2,6 +2,23 @@
* Added CRC support
* Added sd_errno and fat_errno so that information about errors can percolate upstack

2012-06-12 sd-reader
* fix capacity readout from csd register depending on format version
* fix gcc strict-aliasing warnings (also somewhat enlarges code size)

2011-04-23 sd-reader
* fix FAT access for cluster numbers beyond 2^15 (for FAT16) and 2^30 (for FAT32) (thanks to Darwin Engwer for testing)
* correctly return disk-full condition from fat_write_file() on certain conditions
* use byteorder memory access functions for fat_fs_get_free()
* be more specific on the return value of fat_write_file()

2011-02-05 sd-reader
* implement renaming a file or directory
* rewrite byteorder handling to fix unaligned memory accesses on 32-bit and probably 16-bit architectures
* make fat_create_file() not return failure if the file already exists
* make the "cat" output respect the count of bytes actually read
* document how to use fat_seek_file() for retrieving the file position

2010-10-10 sd-reader
* Fix equal file names when reading two successive 8.3 directory entries.
* Fix calculation of cluster positions beyond 4GB (32 bit integer overflow).
Expand Down
7 changes: 6 additions & 1 deletion firmware/src/MightyBoard/Motherboard/lib_sd/README.txt
Expand Up @@ -3,7 +3,12 @@ Roland Riegel's excellent SD/MMC card library. You can find
the latest version of the code here:
http://www.roland-riegel.de/sd-reader/doc/

This particular version is from the 2010-01-10 release.
The Sailfish firmware initially started by forking the 2010-01-10 release.
Some changes (including the larger one below) were made over time.
In 2017, some cleanup and rebasing was performed by Ryan Pavlik to
merge newer upstream versions into a combined tree with Sailfish changes,
so the current code here corresponds to the 2012-06-12 release merged with
the Sailfish-originated changes.

Dan Newman <dan.newman@mtbaldy.us> added CRC support and error return
handling, February 2013.
Expand Down
84 changes: 66 additions & 18 deletions firmware/src/MightyBoard/Motherboard/lib_sd/byteordering.c
@@ -1,6 +1,6 @@

/*
* Copyright (c) 2006-2010 by Roland Riegel <feedback@roland-riegel.de>
* Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
Expand All @@ -25,37 +25,85 @@
* \author Roland Riegel
*/

#if DOXYGEN || !(LITTLE_ENDIAN || __AVR__)
#if DOXYGEN || SWAP_NEEDED

/**
* Converts a 16-bit integer to little-endian byte order.
* \internal
* Swaps the bytes of a 16-bit integer.
*
* Use this function on variable values instead of the
* macro HTOL16(). This saves code size.
* \param[in] i A 16-bit integer which to swap.
* \returns The swapped 16-bit integer.
*/
uint16_t swap16(uint16_t i)
{
return SWAP16(i);
}

/**
* \internal
* Swaps the bytes of a 32-bit integer.
*
* \param[in] h A 16-bit integer in host byte order.
* \returns The given 16-bit integer converted to little-endian byte order.
* \param[in] i A 32-bit integer which to swap.
* \returns The swapped 32-bit integer.
*/
uint16_t htol16(uint16_t h)
uint32_t swap32(uint32_t i)
{
return HTOL16(h);
return SWAP32(i);
}

#endif

#if DOXYGEN || !(LITTLE_ENDIAN || __AVR__)
/**
* Converts a 32-bit integer to little-endian byte order.
* Reads a 16-bit integer from memory in little-endian byte order.
*
* Use this function on variable values instead of the
* macro HTOL32(). This saves code size.
* \param[in] p Pointer from where to read the integer.
* \returns The 16-bit integer read from memory.
*/
uint16_t read16(const uint8_t* p)
{
return (((uint16_t) p[1]) << 8) |
(((uint16_t) p[0]) << 0);
}

/**
* Reads a 32-bit integer from memory in little-endian byte order.
*
* \param[in] h A 32-bit integer in host byte order.
* \returns The given 32-bit integer converted to little-endian byte order.
* \param[in] p Pointer from where to read the integer.
* \returns The 32-bit integer read from memory.
*/
uint32_t htol32(uint32_t h)
uint32_t read32(const uint8_t* p)
{
return HTOL32(h);
return (((uint32_t) p[3]) << 24) |
(((uint32_t) p[2]) << 16) |
(((uint32_t) p[1]) << 8) |
(((uint32_t) p[0]) << 0);
}

/**
* Writes a 16-bit integer into memory in little-endian byte order.
*
* \param[in] p Pointer where to write the integer to.
* \param[in] i The 16-bit integer to write.
*/
void write16(uint8_t* p, uint16_t i)
{
p[1] = (uint8_t) ((i & 0xff00) >> 8);
p[0] = (uint8_t) ((i & 0x00ff) >> 0);
}

/**
* Writes a 32-bit integer into memory in little-endian byte order.
*
* \param[in] p Pointer where to write the integer to.
* \param[in] i The 32-bit integer to write.
*/
void write32(uint8_t* p, uint32_t i)
{
p[3] = (uint8_t) ((i & 0xff000000) >> 24);
p[2] = (uint8_t) ((i & 0x00ff0000) >> 16);
p[1] = (uint8_t) ((i & 0x0000ff00) >> 8);
p[0] = (uint8_t) ((i & 0x000000ff) >> 0);
}
#endif

/**
* @}
Expand Down
128 changes: 86 additions & 42 deletions firmware/src/MightyBoard/Motherboard/lib_sd/byteordering.h
@@ -1,6 +1,6 @@

/*
* Copyright (c) 2006-2010 by Roland Riegel <feedback@roland-riegel.de>
* Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
Expand Down Expand Up @@ -30,10 +30,27 @@ extern "C"
* \author Roland Riegel
*/

#define SWAP16(val) ((((uint16_t) (val)) << 8) | \
(((uint16_t) (val)) >> 8) \
)
#define SWAP32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
((((uint32_t) (val)) & 0x0000ff00) << 8) | \
((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
((((uint32_t) (val)) & 0xff000000) >> 24) \
)

#if LITTLE_ENDIAN || __AVR__
#define SWAP_NEEDED 0
#elif BIG_ENDIAN
#define SWAP_NEEDED 1
#else
#error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1."
#endif

/**
* \def HTOL16(val)
*
* Converts a 16-bit integer to little-endian byte order.
* Converts a 16-bit integer from host byte order to little-endian byte order.
*
* Use this macro for compile time constants only. For variable values
* use the function htol16() instead. This saves code size.
Expand All @@ -44,96 +61,123 @@ extern "C"
/**
* \def HTOL32(val)
*
* Converts a 32-bit integer to little-endian byte order.
* Converts a 32-bit integer from host byte order to little-endian byte order.
*
* Use this macro for compile time constants only. For variable values
* use the function htol32() instead. This saves code size.
*
* \param[in] val A 32-bit integer in host byte order.
* \returns The given 32-bit integer converted to little-endian byte order.
*/

#if DOXYGEN || LITTLE_ENDIAN || __AVR__
#define HTOL16(val) (val)
#define HTOL32(val) (val)
#elif BIG_ENDIAN
#define HTOL16(val) ((((uint16_t) (val)) << 8) | \
(((uint16_t) (val)) >> 8) \
)
#define HTOL32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
((((uint32_t) (val)) & 0x0000ff00) << 8) | \
((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
((((uint32_t) (val)) & 0xff000000) >> 24) \
)
#else
#error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1."
#endif

uint16_t htol16(uint16_t h);
uint32_t htol32(uint32_t h);

/**
* Converts a 16-bit integer to host byte order.
* \def LTOH16(val)
*
* Converts a 16-bit integer from little-endian byte order to host byte order.
*
* Use this macro for compile time constants only. For variable values
* use the function ltoh16() instead. This saves code size.
*
* \param[in] val A 16-bit integer in little-endian byte order.
* \returns The given 16-bit integer converted to host byte order.
*/
#define LTOH16(val) HTOL16(val)

/**
* Converts a 32-bit integer to host byte order.
* \def LTOH32(val)
*
* Converts a 32-bit integer from little-endian byte order to host byte order.
*
* Use this macro for compile time constants only. For variable values
* use the function ltoh32() instead. This saves code size.
*
* \param[in] val A 32-bit integer in little-endian byte order.
* \returns The given 32-bit integer converted to host byte order.
*/
#define LTOH32(val) HTOL32(val)

#if SWAP_NEEDED
#define HTOL16(val) SWAP16(val)
#define HTOL32(val) SWAP32(val)
#define LTOH16(val) SWAP16(val)
#define LTOH32(val) SWAP32(val)
#else
#define HTOL16(val) (val)
#define HTOL32(val) (val)
#define LTOH16(val) (val)
#define LTOH32(val) (val)
#endif

#if DOXYGEN

/**
* Converts a 16-bit integer from host byte order to little-endian byte order.
*
* Use this function on variable values instead of the
* macro HTOL16(). This saves code size.
*
* \param[in] h A 16-bit integer in host byte order.
* \returns The given 16-bit integer converted to little-endian byte order.
*/
uint16_t htol16(uint16_t h);

/**
* Converts a 32-bit integer from host byte order to little-endian byte order.
*
* Use this function on variable values instead of the
* macro HTOL32(). This saves code size.
*
* \param[in] h A 32-bit integer in host byte order.
* \returns The given 32-bit integer converted to little-endian byte order.
*/
uint32_t htol32(uint32_t h);

/**
* Converts a 16-bit integer to host byte order.
* Converts a 16-bit integer from little-endian byte order to host byte order.
*
* Use this function on variable values instead of the
* macro LTOH16(). This saves code size.
*
* \param[in] l A 16-bit integer in little-endian byte order.
* \returns The given 16-bit integer converted to host byte order.
*/
#if DOXYGEN
uint16_t ltoh16(uint16_t l);
#else
#define ltoh16(l) htol16(l)
#endif

/**
* Converts a 32-bit integer to host byte order.
* Converts a 32-bit integer from little-endian byte order to host byte order.
*
* Use this function on variable values instead of the
* macro LTOH32(). This saves code size.
*
* \param[in] l A 32-bit integer in little-endian byte order.
* \returns The given 32-bit integer converted to host byte order.
*/
#if DOXYGEN
uint32_t ltoh32(uint32_t l);

#elif SWAP_NEEDED

#define htol16(h) swap16(h)
#define htol32(h) swap32(h)
#define ltoh16(l) swap16(l)
#define ltoh32(l) swap32(l)

#else
#define ltoh32(l) htol32(l)

#define htol16(h) (h)
#define htol32(h) (h)
#define ltoh16(l) (l)
#define ltoh32(l) (l)

#endif

uint16_t read16(const uint8_t* p);
uint32_t read32(const uint8_t* p);
void write16(uint8_t* p, uint16_t i);
void write32(uint8_t* p, uint32_t i);

/**
* @}
*/

#if LITTLE_ENDIAN || __AVR__
#define htol16(h) (h)
#define htol32(h) (h)
#else
uint16_t htol16(uint16_t h);
uint32_t htol32(uint32_t h);
#if SWAP_NEEDED
uint16_t swap16(uint16_t i);
uint32_t swap32(uint32_t i);
#endif

#ifdef __cplusplus
Expand Down

0 comments on commit 4d8456e

Please sign in to comment.