summaryrefslogtreecommitdiff
path: root/FreeRTOS-Labs/Source/FreeRTOS-Plus-FAT/ff_time.c
blob: a5e5856a913582e7cc78bba567b70b76a20a50ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * FreeRTOS+FAT build 191128 - Note:  FreeRTOS+FAT is still in the lab!
 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 * Authors include James Walmsley, Hein Tibosch and Richard Barry
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 *
 */

#include <stdio.h>
#include <time.h>
#include <string.h>

#include "FreeRTOS.h"
#include "task.h"

#include "ff_time.h"

/**
 *	@file		ff_time.c
 *	@ingroup	TIME
 *
 *	@defgroup	TIME Real-Time Clock Interface
 *	@brief		Allows FreeRTOS+FAT to time-stamp files.
 *
 *	Provides a means for receiving the time on any platform.
 **/

#if( ffconfigTIME_SUPPORT != 0 )	/* This if-block spans the rest of the source file. */
/**
 *	@public
 *	@brief	Populates an FF_SystemTime_t object with the current time from the system.
 *
 *	The developer must modify this function so that it is suitable for their platform.
 *	The function must return with 0, and if the time is not available all elements of the
 *	FF_SystemTime_t object must be zero'd, as in the examples provided.
 *
 *	@param	pxTime	Pointer to an FF_TIME object.
 *
 *	@return	Always returns 0.
 **/

int32_t	FF_GetSystemTime( FF_SystemTime_t *pxTime )
{
	FF_TimeStruct_t xTimeStruct;

	/* Fetch the current time. */
	time_t secs = FreeRTOS_time( NULL );

	/* Fill the fields in 'xTimeStruct'. */
	FreeRTOS_gmtime_r( &secs, &xTimeStruct );

	pxTime->Hour = xTimeStruct.tm_hour;
	pxTime->Minute = xTimeStruct.tm_min;
	pxTime->Second = xTimeStruct.tm_sec;
	pxTime->Day = xTimeStruct.tm_mday;
	pxTime->Month = xTimeStruct.tm_mon + 1;
	pxTime->Year = xTimeStruct.tm_year + 1900;

	return 0;
}	/* FF_GetSystemTime() */
/*-----------------------------------------------------------*/

/*
 * FreeRTOS+FAT
 * Time conversion functions:
 *
 * FF_TimeStruct_t *FreeRTOS_gmtime_r( const time_t *pxTime, FF_TimeStruct_t *pxTimeBuf )
 * time_t FreeRTOS_mktime(FF_TimeStruct_t *pxTimeBuf)
*/

#define GMTIME_FIRST_YEAR		( 1970 )
#define TM_STRUCT_FIRST_YEAR	( 1900 )
#define SECONDS_PER_MINUTE		( 60 )
#define MINUTES_PER_HOUR		( 60 )
#define HOURS_PER_DAY			( 24 )
#define SECONDS_PER_HOUR		( MINUTES_PER_HOUR * SECONDS_PER_MINUTE )
#define SECONDS_PER_DAY			( HOURS_PER_DAY * SECONDS_PER_HOUR )

/* The first weekday in 'FF_TimeStruct_t' is Sunday. */
#define WEEK_DAY_SUNDAY			0
#define WEEK_DAY_MONNDAY 		1
#define WEEK_DAY_TUESDAY 		2
#define WEEK_DAY_WEDNESDAY		3
#define WEEK_DAY_THURSDAY		4
#define WEEK_DAY_FRIDAY			5
#define WEEK_DAY_SATURDAY		6

/* Make a bitmask with a '1' for each 31-day month. */
#define _MM(month)			( 1u << ( month - 1 ) )
#define	MASK_LONG_MONTHS	( _MM(1) | _MM(3) | _MM(5) | _MM(7) | _MM(8) | _MM(10) | _MM(12) )

#define DAYS_UNTIL_1970		( ( 1970 * 365 ) + ( 1970 / 4 ) - ( 1970 / 100 ) + ( 1970 / 400 ) )
#define DAYS_BEFORE_MARCH	( 59 )

static portINLINE int iIsLeapyear( int iYear )
{
int iReturn;

	if( ( iYear % 4 ) != 0 )
	{
		/* Not a multiple of 4 years. */
		iReturn = pdFALSE;
	}
	else if( ( iYear % 400 ) == 0 )
	{
		/* Every 4 centuries there is a leap year */
		iReturn = pdTRUE;
	}
	else if( ( iYear % 100 ) == 0 )
	{
		/* Other centuries are not a leap year */
		iReturn = pdFALSE;
	}
	else
	{
		/* Otherwise every fourth year. */
		iReturn = pdTRUE;
	}

	return iReturn;
}

static portINLINE unsigned long ulDaysPerYear( int iYear )
{
int iDays;

	if( iIsLeapyear( iYear ) )
	{
		iDays = 366;
	}
	else
	{
		iDays = 365;
	}

	return iDays;
}

static int iDaysPerMonth( int iYear, int iMonth )
{
int iDays;

	/* Month is zero-based, 1 is February. */
	if (iMonth != 1 )
	{
		/* 30 or 31 days? */
		if(  ( MASK_LONG_MONTHS & ( 1u << iMonth ) ) != 0 )
		{
			iDays = 31;
		}
		else
		{
			iDays = 30;
		}
	}
	else if( iIsLeapyear( iYear ) == pdFALSE )
	{
		/* February, non leap year. */
		iDays = 28;
	}
	else
	{
		/* February, leap year. */
		iDays = 29;
	}
	return iDays;
}

FF_TimeStruct_t *FreeRTOS_gmtime_r( const time_t *pxTime, FF_TimeStruct_t *pxTimeBuf )
{
time_t xTime = *pxTime;
unsigned long ulDaySeconds, ulDayNumber;
int iYear = GMTIME_FIRST_YEAR;
int iMonth;

	/* Clear all fields, some might not get set here. */
	memset( ( void * )pxTimeBuf, '\0', sizeof( *pxTimeBuf ) );

	/* Seconds since last midnight. */
	ulDaySeconds = ( unsigned long ) ( xTime % SECONDS_PER_DAY ) ;

	/* Days since 1 Jan 1970. */
	ulDayNumber = ( unsigned long ) ( xTime / SECONDS_PER_DAY ) ;

	/* Today's HH:MM:SS */
	pxTimeBuf->tm_hour = ulDaySeconds / SECONDS_PER_HOUR;
	pxTimeBuf->tm_min = ( ulDaySeconds % SECONDS_PER_HOUR ) / 60;
	pxTimeBuf->tm_sec = ulDaySeconds % 60;

	/* Today's week day, knowing that 1-1-1970 was a THursday. */
	pxTimeBuf->tm_wday = ( ulDayNumber + WEEK_DAY_THURSDAY ) % 7;

	for( ; ; )
	{
		/* Keep subtracting 365 (or 366) days while possible. */
		unsigned long ulDays = ulDaysPerYear( iYear );
		if( ulDayNumber < ulDays )
		{
			break;
		}
		ulDayNumber -= ulDays;
		iYear++;
	}
	/* Subtract 1900. */
	pxTimeBuf->tm_year = iYear - TM_STRUCT_FIRST_YEAR;

	/* The day within this year. */
	pxTimeBuf->tm_yday = ulDayNumber;

	/* Month are counted as 0..11 */
	iMonth = 0;
	for( ; ; )
	{
		unsigned long ulDays = iDaysPerMonth( iYear, iMonth );
		/* Keep subtracting 30 (or 28, 29, or 31) days while possible. */
		if( ulDayNumber < ulDays )
		{
			break;
		}
		ulDayNumber -= ulDays;
		iMonth++;
	}
	pxTimeBuf->tm_mon = iMonth;

	/* Month days are counted as 1..31 */
	pxTimeBuf->tm_mday = ulDayNumber + 1;

	return pxTimeBuf;
}

time_t FreeRTOS_mktime( const FF_TimeStruct_t *pxTimeBuf )
{
/* Get year AD. */
int iYear = 1900 + pxTimeBuf->tm_year;	/* 20xx */
/* Get month zero-based. */
int iMonth = pxTimeBuf->tm_mon;			/* 0..11 */
uint32_t ulDays;
uint32_t ulResult;

	ulDays = pxTimeBuf->tm_mday - 1;	/* 1..31 */

	/* Make March the first month. */
	iMonth -= 2;
	if( iMonth < 0 )
	{
		/* January or February: leap day has yet to come for this year. */
		iYear--;
		iMonth += 12;
	}

	/* Add the number of days past until this month. */
	ulDays += ( ( 306 * iMonth ) + 5 ) / 10;

	/* Add days past before this year: */
	ulDays +=
		+ ( iYear * 365 )		/* Every normal year. */
		+ ( iYear / 4 )			/* Plus a day for every leap year. */
		- ( iYear / 100 )		/* Minus the centuries. */
		+ ( iYear / 400 )		/* Except every fourth century. */
		- ( DAYS_UNTIL_1970 )	/* Minus the days before 1-1-1970 */
		+ ( DAYS_BEFORE_MARCH );/* Because 2 months were subtracted. */

	ulResult =
		( ulDays * SECONDS_PER_DAY ) +
		( pxTimeBuf->tm_hour * SECONDS_PER_HOUR ) +
		( pxTimeBuf->tm_min * SECONDS_PER_MINUTE ) +
		pxTimeBuf->tm_sec;

	return ulResult;
}

#endif	/* ffconfigTIME_SUPPORT */