ff_memory.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * FreeRTOS+FAT V2.3.3
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. /**
  27. * @file ff_memory.h
  28. * @ingroup MEMORY
  29. **/
  30. #ifndef _FF_MEMORY_H_
  31. #define _FF_MEMORY_H_
  32. /*
  33. * When sector data is written or analysed, some values might be stored unaligned.
  34. * The routines below treat all values as little arrays of either 2 or 4 bytes.
  35. * Also on big endian platforms, the order of bytes will be swapped.
  36. */
  37. /*---------- PROTOTYPES */
  38. #if ( ffconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
  39. /*
  40. * FAT is little endian.
  41. * On a little endian CPU, bytes will be copied to the structures below 1-to-1 :
  42. */
  43. typedef struct
  44. {
  45. uint8_t u8_0; /* the first byte */
  46. uint8_t u8_1; /* the second byte */
  47. } FF_TShort_t;
  48. typedef struct
  49. {
  50. uint8_t u8_0;
  51. uint8_t u8_1;
  52. uint8_t u8_2;
  53. uint8_t u8_3;
  54. } FF_TLong_t;
  55. #elif ( ffconfigBYTE_ORDER == pdFREERTOS_BIG_ENDIAN )
  56. /*
  57. * On a big endian CPU, all bytes will be swapped, either 2 or 4 bytes:
  58. */
  59. typedef struct
  60. {
  61. uint8_t u8_1; /* the second byte */
  62. uint8_t u8_0; /* the first byte */
  63. } FF_TShort_t;
  64. typedef struct
  65. {
  66. uint8_t u8_3;
  67. uint8_t u8_2;
  68. uint8_t u8_1;
  69. uint8_t u8_0;
  70. } FF_TLong_t;
  71. #else /* if ( ffconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN ) */
  72. #error Little or Big Endian? - Please set ffconfigBYTE_ORDER to either pdFREERTOS_LITTLE_ENDIAN or pdFREERTOS_BIG_ENDIAN 1 in FreeRTOSFATConfig.h
  73. #endif /* if ( ffconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN ) */
  74. /*! 16-bit union. */
  75. typedef union
  76. {
  77. uint16_t u16;
  78. FF_TShort_t bytes;
  79. } FF_T_UN16;
  80. /*! 32-bit union. */
  81. typedef union
  82. {
  83. uint32_t u32;
  84. FF_TLong_t bytes;
  85. } FF_T_UN32;
  86. /* HT inlined these functions:
  87. */
  88. #if ( ffconfigINLINE_MEMORY_ACCESS != 0 )
  89. static portINLINE uint8_t FF_getChar( const uint8_t * pBuffer,
  90. uint32_t aOffset )
  91. {
  92. return ( uint8_t ) ( pBuffer[ aOffset ] );
  93. }
  94. static portINLINE uint16_t FF_getShort( const uint8_t * pBuffer,
  95. uint32_t aOffset )
  96. {
  97. FF_T_UN16 u16;
  98. pBuffer += aOffset;
  99. u16.bytes.u8_1 = pBuffer[ 1 ];
  100. u16.bytes.u8_0 = pBuffer[ 0 ];
  101. return u16.u16;
  102. }
  103. static portINLINE uint32_t FF_getLong( const uint8_t * pBuffer,
  104. uint32_t aOffset )
  105. {
  106. FF_T_UN32 u32;
  107. pBuffer += aOffset;
  108. u32.bytes.u8_3 = pBuffer[ 3 ];
  109. u32.bytes.u8_2 = pBuffer[ 2 ];
  110. u32.bytes.u8_1 = pBuffer[ 1 ];
  111. u32.bytes.u8_0 = pBuffer[ 0 ];
  112. return u32.u32;
  113. }
  114. static portINLINE void FF_putChar( uint8_t * pBuffer,
  115. uint32_t aOffset,
  116. uint32_t Value )
  117. {
  118. pBuffer[ aOffset ] = ( uint8_t ) Value;
  119. }
  120. static portINLINE void FF_putShort( uint8_t * pBuffer,
  121. uint32_t aOffset,
  122. uint32_t Value )
  123. {
  124. FF_T_UN16 u16;
  125. u16.u16 = ( uint16_t ) Value;
  126. pBuffer += aOffset;
  127. pBuffer[ 0 ] = u16.bytes.u8_0;
  128. pBuffer[ 1 ] = u16.bytes.u8_1;
  129. }
  130. static portINLINE void FF_putLong( uint8_t * pBuffer,
  131. uint32_t aOffset,
  132. uint32_t Value )
  133. {
  134. FF_T_UN32 u32;
  135. u32.u32 = Value;
  136. pBuffer += aOffset;
  137. pBuffer[ 0 ] = u32.bytes.u8_0;
  138. pBuffer[ 1 ] = u32.bytes.u8_1;
  139. pBuffer[ 2 ] = u32.bytes.u8_2;
  140. pBuffer[ 3 ] = u32.bytes.u8_3;
  141. }
  142. #else /* ffconfigINLINE_MEMORY_ACCESS */
  143. uint8_t FF_getChar( const uint8_t * pBuffer,
  144. uint32_t aOffset );
  145. uint16_t FF_getShort( const uint8_t * pBuffer,
  146. uint32_t aOffset );
  147. uint32_t FF_getLong( const uint8_t * pBuffer,
  148. uint32_t aOffset );
  149. void FF_putChar( uint8_t * pBuffer,
  150. uint32_t aOffset,
  151. uint32_t Value );
  152. void FF_putShort( uint8_t * pBuffer,
  153. uint32_t aOffset,
  154. uint32_t Value );
  155. void FF_putLong( uint8_t * pBuffer,
  156. uint32_t aOffset,
  157. uint32_t Value );
  158. #endif /* ffconfigINLINE_MEMORY_ACCESS */
  159. #endif /* _FF_MEMORY_H_ */