chrtclib. Fixed overflows in mcrosecond functions. Fixed initializations of RTCTime structs in functions.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4217 35acf78f-673a-0410-8e92-d51de3d6d3f4
master
barthess 2012-05-19 15:39:24 +00:00
parent 581267b0db
commit ec65999d9b
1 changed files with 28 additions and 17 deletions

View File

@ -139,7 +139,11 @@ static void stm32_rtc_tm2bcd(struct tm *timp, RTCTime *timespec) {
* @api * @api
*/ */
void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) {
#if STM32_RTC_HAS_SUBSECONDS
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0,FALSE,0};
#else
RTCTime timespec = {0,0,FALSE};
#endif
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
stm32_rtc_bcd2tm(timp, &timespec); stm32_rtc_bcd2tm(timp, &timespec);
@ -154,7 +158,11 @@ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) {
* @api * @api
*/ */
void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) {
#if STM32_RTC_HAS_SUBSECONDS
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0,FALSE,0};
#else
RTCTime timespec = {0,0,FALSE};
#endif
stm32_rtc_tm2bcd(timp, &timespec); stm32_rtc_tm2bcd(timp, &timespec);
rtcSetTime(rtcp, &timespec); rtcSetTime(rtcp, &timespec);
@ -169,7 +177,11 @@ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) {
* @api * @api
*/ */
time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { time_t rtcGetTimeUnixSec(RTCDriver *rtcp) {
#if STM32_RTC_HAS_SUBSECONDS
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0,FALSE,0};
#else
RTCTime timespec = {0,0,FALSE};
#endif
struct tm timp; struct tm timp;
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
@ -187,11 +199,13 @@ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) {
* @api * @api
*/ */
void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) {
#if STM32_RTC_HAS_SUBSECONDS
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0,FALSE,0};
struct tm *timp; #else
RTCTime timespec = {0,0,FALSE};
#endif
timp = localtime(&tv_sec); stm32_rtc_tm2bcd(localtime(&tv_sec), &timespec);
stm32_rtc_tm2bcd(timp, &timespec);
rtcSetTime(rtcp, &timespec); rtcSetTime(rtcp, &timespec);
} }
@ -204,20 +218,18 @@ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) {
* @api * @api
*/ */
uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) { uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) {
#if STM32_RTC_HAS_SUBSECONDS
uint64_t result = 0; uint64_t result = 0;
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0,FALSE,0};
struct tm timp; struct tm timp;
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
stm32_rtc_bcd2tm(&timp, &timespec); stm32_rtc_bcd2tm(&timp, &timespec);
result = mktime(&timp) * 1000000; result = (uint64_t)mktime(&timp) * 1000000;
#if STM32_RTC_HAS_SUBSECONDS
return result + timespec.tv_msec * 1000; return result + timespec.tv_msec * 1000;
#else #else
return result; return (uint64_t)rtcGetTimeUnixSec(rtcp) * 1000000;
#endif #endif
} }
@ -231,7 +243,7 @@ uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) {
* @api * @api
*/ */
void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) {
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0};
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
if (timp != NULL) /* this comparison needed to avoid compiler warning */ if (timp != NULL) /* this comparison needed to avoid compiler warning */
@ -247,7 +259,7 @@ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) {
* @api * @api
*/ */
void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) {
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0};
timespec.tv_sec = mktime(timp); timespec.tv_sec = mktime(timp);
timespec.tv_msec = 0; timespec.tv_msec = 0;
@ -263,7 +275,7 @@ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) {
* @api * @api
*/ */
time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { time_t rtcGetTimeUnixSec(RTCDriver *rtcp) {
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0};
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
return timespec.tv_sec; return timespec.tv_sec;
@ -278,7 +290,7 @@ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) {
* @api * @api
*/ */
void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) {
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0};
timespec.tv_sec = tv_sec; timespec.tv_sec = tv_sec;
timespec.tv_msec = 0; timespec.tv_msec = 0;
@ -294,16 +306,15 @@ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) {
* @api * @api
*/ */
uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) { uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) {
#if STM32_RTC_HAS_SUBSECONDS
uint64_t result = 0; uint64_t result = 0;
RTCTime timespec = {0,0,FALSE,0}; RTCTime timespec = {0,0};
rtcGetTime(rtcp, &timespec); rtcGetTime(rtcp, &timespec);
result = timespec.tv_sec * 1000000; result = (uint64_t)timespec.tv_sec * 1000000;
#if STM32_RTC_HAS_SUBSECONDS
return result + timespec.tv_msec * 1000; return result + timespec.tv_msec * 1000;
#else #else
return result; return (uint64_t)rtcGetTimeUnixSec(rtcp) * 1000000;
#endif #endif
} }
#endif /* STM32_RTC_IS_CALENDAR */ #endif /* STM32_RTC_IS_CALENDAR */