1 /*****************************************************************************
2 * FullFAT - High Performance, Thread-Safe Embedded FAT File-System *
3 * Copyright (C) 2009 James Walmsley (james@worm.me.uk) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 * Alternative Licensing is available directly from the Copyright holder, *
21 * (James Walmsley). For more information consult LICENSING.TXT to obtain *
22 * a Commercial license. *
24 * See RESTRICTIONS.TXT for extra restrictions on the use of FullFAT. *
26 * Removing the above notice is illegal and will invalidate this license. *
27 *****************************************************************************
28 * See http://worm.me.uk/fullfat for more information. *
29 * Or http://fullfat.googlecode.com/ for latest releases and the wiki. *
30 *****************************************************************************/
34 * @author James Walmsley
37 * @defgroup MEMORY FullFAT Memory Access Routines
38 * @brief Handles memory access in a portable way.
40 * Provides simple, fast, and portable access to memory routines.
41 * These are only used to read data from buffers. That are LITTLE ENDIAN
42 * due to the FAT specification.
44 * These routines may need to be modified to your platform.
48 #include "ff_memory.h"
49 #include "ff_config.h"
51 #ifdef FF_LITTLE_ENDIAN
55 * @brief 8 bit memory access routines.
58 These functions swap the byte-orders of shorts and longs. A getChar function is provided
59 incase there is a system that doesn't have byte-wise access to all memory.
61 These functions can be replaced with your own platform specific byte-order swapping routines
64 The provided functions should work on almost all platforms.
66 FF_T_UINT8
FF_getChar(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
67 return (FF_T_UINT8
) (pBuffer
[offset
]);
70 FF_T_UINT16
FF_getShort(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
71 return (FF_T_UINT16
) (pBuffer
[offset
] & 0x00FF) | ((FF_T_UINT16
) (pBuffer
[offset
+1] << 8) & 0xFF00);
74 FF_T_UINT32
FF_getLong(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
75 return (FF_T_UINT32
) (pBuffer
[offset
] & 0x000000FF) | ((FF_T_UINT32
) (pBuffer
[offset
+1] << 8) & 0x0000FF00) | ((FF_T_UINT32
) (pBuffer
[offset
+2] << 16) & 0x00FF0000) | ((FF_T_UINT32
) (pBuffer
[offset
+3] << 24) & 0xFF000000);
78 void FF_putChar(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT8 Value
) {
79 pBuffer
[offset
] = Value
;
82 void FF_putShort(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT16 Value
) {
83 FF_T_UINT8
*Val
= (FF_T_UINT8
*) &Value
;
84 pBuffer
[offset
] = Val
[0];
85 pBuffer
[offset
+ 1] = Val
[1];
88 void FF_putLong(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT32 Value
) {
89 FF_T_UINT8
*Val
= (FF_T_UINT8
*) &Value
;
90 pBuffer
[offset
] = Val
[0];
91 pBuffer
[offset
+ 1] = Val
[1];
92 pBuffer
[offset
+ 2] = Val
[2];
93 pBuffer
[offset
+ 3] = Val
[3];
100 These haven't been tested or checked. They should work in theory :)
101 Please contact james@worm.me.uk if they don't work, and also any fix.
103 FF_T_UINT8
FF_getChar(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
104 return (FF_T_UINT8
) (pBuffer
[offset
]);
107 FF_T_UINT16
FF_getShort(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
108 return (FF_T_UINT16
) ((pBuffer
[offset
] & 0xFF00) << 8) | ((FF_T_UINT16
) (pBuffer
[offset
+1]) & 0x00FF);
111 FF_T_UINT32
FF_getLong(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
) {
112 return (FF_T_UINT32
) ((pBuffer
[offset
] << 24) & 0xFF0000) | ((FF_T_UINT32
) (pBuffer
[offset
+1] << 16) & 0x00FF0000) | ((FF_T_UINT32
) (pBuffer
[offset
+2] << 8) & 0x0000FF00) | ((FF_T_UINT32
) (pBuffer
[offset
+3]) & 0x000000FF);
115 void FF_putChar(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT8 Value
) {
116 pBuffer
[offset
] = Value
;
119 void FF_putShort(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT16 Value
) {
120 FF_T_UINT8
*Val
= (FF_T_UINT8
*) &Value
;
121 pBuffer
[offset
] = Val
[1];
122 pBuffer
[offset
+ 1] = Val
[0];
125 void FF_putLong(FF_T_UINT8
*pBuffer
, FF_T_UINT16 offset
, FF_T_UINT32 Value
) {
126 FF_T_UINT8
*Val
= (FF_T_UINT8
*) &Value
;
127 pBuffer
[offset
] = Val
[3];
128 pBuffer
[offset
+ 1] = Val
[2];
129 pBuffer
[offset
+ 2] = Val
[1];
130 pBuffer
[offset
+ 3] = Val
[0];