SAM4S-EK_FreeRTOS+FAT-SL  1.0
An example project to test the functionality of FreeRTOS+FAT-SL using SD card as data storage medium
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
util.c
Go to the documentation of this file.
1 /*
2  * FreeRTOS+FAT SL V1.0.1 (C) 2014 HCC Embedded
3  *
4  * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license
5  * terms.
6  *
7  * FreeRTOS+FAT SL uses a dual license model that allows the software to be used
8  * under a standard GPL open source license, or a commercial license. The
9  * standard GPL license (unlike the modified GPL license under which FreeRTOS
10  * itself is distributed) requires that all software statically linked with
11  * FreeRTOS+FAT SL is also distributed under the same GPL V2 license terms.
12  * Details of both license options follow:
13  *
14  * - Open source licensing -
15  * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and
16  * distributed without charge provided the user adheres to version two of the
17  * GNU General Public License (GPL) and does not remove the copyright notice or
18  * this text. The GPL V2 text is available on the gnu.org web site, and on the
19  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
20  *
21  * - Commercial licensing -
22  * Businesses and individuals who for commercial or other reasons cannot comply
23  * with the terms of the GPL V2 license must obtain a commercial license before
24  * incorporating FreeRTOS+FAT SL into proprietary software for distribution in
25  * any form. Commercial licenses can be purchased from
26  * http://shop.freertos.org/fat_sl and do not require any source files to be
27  * changed.
28  *
29  * FreeRTOS+FAT SL is distributed in the hope that it will be useful. You
30  * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as
31  * is'. FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the
32  * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A
33  * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all
34  * conditions and terms, be they implied, expressed, or statutory.
35  *
36  * http://www.FreeRTOS.org
37  * http://www.FreeRTOS.org/FreeRTOS-Plus
38  *
39  */
40 
41 #include "../../api/fat_sl.h"
42 #include "../../psp/include/psp_rtc.h"
43 #include "dir.h"
44 
45 #include "util.h"
46 
47 #include "../../version/ver_fat_sl.h"
48 #if VER_FAT_SL_MAJOR != 5 || VER_FAT_SL_MINOR != 2
49  #error Incompatible FAT_SL version number!
50 #endif
51 
52 
53 /****************************************************************************
54  *
55  * _f_getword
56  *
57  * get a word 16bit number from a memory (it uses LITTLE ENDIAN mode always)
58  *
59  * INPUTS
60  *
61  * ptr - pointer where data is
62  *
63  * RETURNS
64  *
65  * word number
66  *
67  ***************************************************************************/
68 unsigned short _f_getword ( void * ptr )
69 {
70  unsigned char * sptr = (unsigned char *)ptr;
71  unsigned short ret;
72 
73  ret = (unsigned short)( sptr[1] & 0xff );
74  ret <<= 8;
75  ret |= ( sptr[0] & 0xff );
76  return ret;
77 }
78 
79 
80 /****************************************************************************
81  *
82  * _f_setword
83  *
84  * set a word 16bit number into a memory (it uses LITTLE ENDIAN mode always)
85  *
86  * INPUTS
87  *
88  * ptr - where to store data
89  * num - 16 bit number to store
90  *
91  ***************************************************************************/
92 void _f_setword ( void * ptr, unsigned short num )
93 {
94  unsigned char * sptr = (unsigned char *)ptr;
95 
96  sptr[1] = (unsigned char)( num >> 8 );
97  sptr[0] = (unsigned char)( num );
98 }
99 
100 
101 /****************************************************************************
102  *
103  * _f_getlong
104  *
105  * get a long 32bit number from a memory (it uses LITTLE ENDIAN mode always)
106  *
107  * INPUTS
108  *
109  * ptr - pointer where data is
110  *
111  * RETURNS
112  *
113  * long number
114  *
115  ***************************************************************************/
116 unsigned long _f_getlong ( void * ptr )
117 {
118  unsigned char * sptr = (unsigned char *)ptr;
119  unsigned long ret;
120 
121  ret = (unsigned long)( sptr[3] & 0xff );
122  ret <<= 8;
123  ret |= ( sptr[2] & 0xff );
124  ret <<= 8;
125  ret |= ( sptr[1] & 0xff );
126  ret <<= 8;
127  ret |= ( sptr[0] & 0xff );
128  return ret;
129 }
130 
131 
132 /****************************************************************************
133  *
134  * _f_setlong
135  *
136  * set a long 32bit number into a memory (it uses LITTLE ENDIAN mode always)
137  *
138  * INPUTS
139  *
140  * ptr - where to store data
141  * num - 32 bit number to store
142  *
143  ***************************************************************************/
144 void _f_setlong ( void * ptr, unsigned long num )
145 {
146  unsigned char * sptr = (unsigned char *)ptr;
147 
148  sptr[3] = (unsigned char)( num >> 24 );
149  sptr[2] = (unsigned char)( num >> 16 );
150  sptr[1] = (unsigned char)( num >> 8 );
151  sptr[0] = (unsigned char)( num );
152 }
153 
154 
155 /****************************************************************************
156  *
157  * _setcharzero
158  *
159  * fills with zero charater to memory
160  *
161  * INPUTS
162  *
163  * num - number of characters
164  * ptr - where to store data
165  *
166  * RETURNS
167  *
168  * last write position
169  *
170  ***************************************************************************/
171 unsigned char * _setcharzero ( int num, unsigned char * ptr )
172 {
173  while ( num-- )
174  {
175  *ptr++ = 0;
176  }
177 
178  return ptr;
179 }
180 
181 
182 /****************************************************************************
183  *
184  * _setchar
185  *
186  * copy a charater string to memory
187  *
188  * INPUTS
189  *
190  * array - original code what to copy
191  * num - number of characters
192  * ptr - where to store data
193  *
194  * RETURNS
195  *
196  * last write position
197  *
198  ***************************************************************************/
199 unsigned char * _setchar ( const unsigned char * array, int num, unsigned char * ptr )
200 {
201  if ( !array )
202  {
203  return _setcharzero( num, ptr );
204  }
205 
206  while ( num-- )
207  {
208  *ptr++ = *array++;
209  }
210 
211  return ptr;
212 }
213 
214 
215 /****************************************************************************
216  *
217  * _setword
218  *
219  * store a 16bit word into memory
220  *
221  * INPUTS
222  *
223  * num - 16bit number to store
224  * ptr - where to store data
225  *
226  * RETURNS
227  *
228  * last write position
229  *
230  ***************************************************************************/
231 unsigned char * _setword ( unsigned short num, unsigned char * ptr )
232 {
233  _f_setword( ptr, num );
234  return ptr + 2;
235 }
236 
237 
238 /****************************************************************************
239  *
240  * _setlong
241  *
242  * store a 32bit long number into memory
243  *
244  * INPUTS
245  *
246  * num - 32bit number to store
247  * ptr - where to store data
248  *
249  * RETURNS
250  *
251  * last write position
252  *
253  ***************************************************************************/
254 unsigned char * _setlong ( unsigned long num, unsigned char * ptr )
255 {
256  _f_setlong( ptr, num );
257  return ptr + 4;
258 }
259 
260 
261 /****************************************************************************
262  *
263  * _f_toupper
264  *
265  * convert a string into lower case
266  *
267  * INPUTS
268  *
269  * s - input string to convert
270  *
271  ***************************************************************************/
272 char _f_toupper ( char ch )
273 {
274  if ( ( ch >= 'a' ) && ( ch <= 'z' ) )
275  {
276  return (char)( ch - 'a' + 'A' );
277  }
278 
279  return ch;
280 }
281 
282 
283 /****************************************************************************
284  *
285  * f_igettimedate
286  *
287  * INPUTS
288  * time - pointer to time variable
289  * date - pointer to date variable
290  * OUTPUTS
291  * time - current time
292  * date - current date
293  *
294  * RETURNS
295  * none
296  *
297  ***************************************************************************/
298 void f_igettimedate ( unsigned short * time, unsigned short * date )
299 {
300  t_psp_timedate s_timedate;
301 
302  psp_getcurrenttimedate( &s_timedate );
303 
304  *time = ( ( (uint16_t)s_timedate.hour << F_CTIME_HOUR_SHIFT ) & F_CTIME_HOUR_MASK )
305  | ( ( (uint16_t)s_timedate.min << F_CTIME_MIN_SHIFT ) & F_CTIME_MIN_MASK )
306  | ( ( ( (uint16_t)s_timedate.sec >> 1 ) << F_CTIME_SEC_SHIFT ) & F_CTIME_SEC_MASK );
307 
308  *date = ( ( ( s_timedate.year - 1980 ) << F_CDATE_YEAR_SHIFT ) & F_CDATE_YEAR_MASK )
309  | ( ( (uint16_t)s_timedate.month << F_CDATE_MONTH_SHIFT ) & F_CDATE_MONTH_MASK )
310  | ( ( (uint16_t)s_timedate.day << F_CDATE_DAY_SHIFT ) & F_CDATE_DAY_MASK );
311 
312  return;
313 }
314 
315 
316 
317 
318 
319 
320 
321 
322 
uint16_t year
Definition: psp_rtc.h:62
unsigned char * _setchar(const unsigned char *array, int num, unsigned char *ptr)
Definition: util.c:199
void _f_setword(void *ptr, unsigned short num)
Definition: util.c:92
void _f_setlong(void *ptr, unsigned long num)
Definition: util.c:144
char _f_toupper(char ch)
Definition: util.c:272
#define F_CDATE_YEAR_MASK
Definition: dir.h:69
#define F_CDATE_MONTH_MASK
Definition: dir.h:67
#define F_CDATE_YEAR_SHIFT
Definition: dir.h:68
#define F_CTIME_SEC_SHIFT
Definition: dir.h:55
void psp_getcurrenttimedate(t_psp_timedate *p_timedate)
Definition: psp_rtc.c:69
unsigned char * _setlong(unsigned long num, unsigned char *ptr)
Definition: util.c:254
uint8_t hour
Definition: psp_rtc.h:59
#define F_CDATE_DAY_MASK
Definition: dir.h:65
unsigned long _f_getlong(void *ptr)
Definition: util.c:116
uint8_t month
Definition: psp_rtc.h:61
unsigned char * _setcharzero(int num, unsigned char *ptr)
Definition: util.c:171
void f_igettimedate(unsigned short *time, unsigned short *date)
Definition: util.c:298
#define F_CTIME_HOUR_SHIFT
Definition: dir.h:59
uint8_t sec
Definition: psp_rtc.h:57
#define F_CDATE_DAY_SHIFT
Definition: dir.h:64
uint8_t min
Definition: psp_rtc.h:58
#define F_CTIME_MIN_SHIFT
Definition: dir.h:57
unsigned char * _setword(unsigned short num, unsigned char *ptr)
Definition: util.c:231
uint8_t day
Definition: psp_rtc.h:60
unsigned short _f_getword(void *ptr)
Definition: util.c:68
#define F_CDATE_MONTH_SHIFT
Definition: dir.h:66
#define F_CTIME_HOUR_MASK
Definition: dir.h:60
#define F_CTIME_SEC_MASK
Definition: dir.h:56
#define F_CTIME_MIN_MASK
Definition: dir.h:58