Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 6 Forum Discussions

TeMa_1467596
Level 5
Level 5
5 sign-ins 5 likes given First like received

I am using GPS parsing code from Tilen Majerle that can be found here tilen@majerle.eu

There's a structure defined to hold the GPS date and time that sets up hgps.date hgps.month hgps.year hpgs.hours hpgs.minutes and hgps.seconds which are all uint8_t and the parsing code works well so that I can print the GPS time using this routine

void gpsDtTimeReport(void) {

    xprintf("%s", "GPS Date Time\r\n");

    xprintf("Date %02d/%02d/20%02u\r\n", hgps.month, hgps.date, hgps.year);

    xprintf("Time %02d:%02d:%02u UTC\r\n", hgps.hours, hgps.minutes, hgps.seconds);

}

I've defined a dateTime variable as follows

cy_stc_rtc_config_t dateTime;

The RTCC in the PSoC 6 defines dateTime.date dateTime.month dateTime.year dateTime.hour dateTime.min and dateTime.sec as uint32_t and I've created code so that I can print the PSoC date time using this routine

void dateTimeReport(void) {

    xprintf("%s", "PSoC Date Time\r\n");

    Cy_RTC_GetDateAndTime(&dateTime);

    xprintf("Date %02d/%02d/20%02u\r\n", dateTime.month, dateTime.date, dateTime.year);

    xprintf("Time %02d:%02d:%02u UTC\r\n", dateTime.hour, dateTime.min, dateTime.sec);

}

In my code I wait until I know tha the GPS data is good and then run the following code

                if (hgps.fix) { // we have good GGA data

                    xprintf("%s", "GGA data received...\n");

                    // and we previously got good RMC data so we must have all the data we need

                    dateTime.sec = hgps.seconds;

                    dateTime.min = hgps.minutes;

                    dateTime.hour = hgps.hours;

                    dateTime.year = hgps.year;

                    dateTime.month = hgps.month;

                    dateTime.date = hgps.date;

                    dateTimeReport();

                    gpsDtTimeReport();

                    Cy_RTC_SetDateAndTime(&dateTime); // and now the PSoC should run the RTCC

                    gpsState = normal; // for now we jump to normal and forget but what about a better fix? More satellites? etc?

                    xprintf("%s", "We got here\r\n");

                    GPS_UART_PutString("$PRTHS,U1OP,ALL=0,GGA=60\r"); // GPS only GGA data every 60 seconds

                    gpsReport();

                    dateTimeReport();

                    dstrState = getSWIMSdata;

                }

but the output looks like this...

PSoC Date Time

Date 10/03/2018

Time 14:00:13 UTC

GPS Date Time

Date 10/04/2018

Time 15:31:32 UTC

Where the GPS info is correct and the PSoC info is wrong!

And if I move the Cy_RTS_SetDateAndTime(&dateTime); up 2 lines to before the 2 dateTime() reports, the whole program gets stuck and debug finds that I'm stuck in some exception loop.

At first I thought that the issue was in assigning a uint8_t to a uint32_t but from what I've read, that should not be a problem

Any ideas what my issue is?

0 Likes
14 Replies
ajays_86
Employee
Employee
First like received

ted_1467596,

when you are calling dateTimeReport() (at line 10), your code at line 4 to 9 will have no effect. Because "dateTime" (assuming its a global variable based on the code snippet) is modified by the dateTimeReport(). In that case line 10 and 11 may have different values. A simple fix is to declare a local variable inside the dateTimeReport() function.

Also use Cy_RTC_SetDateAndTimeDirect() API instead of Cy_RTC_SetDateAndTime(). The reason your program gets stuck is because, all the values  inside dateTime structure must be valid.

Please let me know if it solves your problem.

Thanks Ajya but It's still not working.

I got it to work occasionally but it's not actually getting the right time values.  The code is now as follows...

               if (hgps.fix) { // we have good GGA data

                    xprintf("%s", "GGA data received...\n");

                    // and we previously got good RMC data so we must have all the data we need

                    dateTime.hrFormat = CY_RTC_24_HOURS;

                    dateTime.amPm = CY_RTC_AM;

                    dateTime.sec = hgps.seconds;

                    xprintf("sec = %02d\r\n", dateTime.sec);

                    dateTime.min =  hgps.minutes;

                    xprintf("min = %02d\r\n", dateTime.min);

                    dateTime.hour = hgps.hours;

                    xprintf("hour = %02d\r\n", dateTime.hour);

                    dateTime.year = (2000 + hgps.year);

                    xprintf("year = %02d\r\n", dateTime.year);

                    dateTime.month = hgps.month;

                    xprintf("month = %02d\r\n", dateTime.month);

                    dateTime.date = hgps.date;

                    xprintf("month = %02d\r\n", dateTime.date);

                    dateTime.dayOfWeek = Cy_RTC_ConvertDayOfWeek(dateTime.date, dateTime.month, dateTime.year);  // calculate day of week

                    xprintf("DOW = %02d\r\n", dateTime.dayOfWeek);

                    dateTimeReport();

                    gpsDtTimeReport();

                    Cy_RTC_SetDateAndTimeDirect(dateTime.sec, dateTime.min, dateTime.hour,

                                               dateTime.date, dateTime.month, dateTime.year);

                    //Cy_RTC_SetDateAndTime(&dateTime); // and now the PSoC should run the RTCC                 

                    gpsState = normal; // for now we jump to normal and forget but what about a better fix? More satellites? etc?

                    xprintf("%s", "We got here\r\n");

                    GPS_UART_PutString("$PRTHS,U1OP,ALL=0,GGA=60\r"); // GPS only GGA data every 60 seconds

                    gpsDtTimeReport();

                    dateTimeReport();

                    dstrState = getSWIMSdata;

                }

And the output is...

Requesting GGA sentence...

GGA data received...

sec = 37

min = 24

hour = 20

year = 2018

month = 10

date = 04

DOW = 05

PSoC Date Time

Date 10/03/0018

Time 14:00:09 UTC  4

GPS Date Time

Date 10/04/2018

Time 20:24:37 UTC

We got here

GPS Date Time

Date 10/04/2018

Time 20:24:37 UTC

PSoC Date Time

Date 10/03/0018

Time 14:00:09 UTC  4

rd

PSoC Date Time

Date 10/03/0018

Time 14:04:03 UTC  4

rd

PSoC Date Time

Date 10/03/0018

Time 14:04:26 UTC  4

There's something odd going on, I think the year is supposed to be in the range 2000 2100 (from reading the cy_rtc.h file comments so I changed that and the DayofWeek function is supposed to come back with 5 for Thursday but it comes back with 4 for Wednesday.  The time is also messed up - my xprintf statements prove that the values set in dateTime.xxxxx are what I expect but the values the clock starts running from are the ones specified in the Cy_RTC_Init(&RTCC_config); config command.

[EDIT] the number after the UTC is the numerical DayofWeek

Sigh...

0 Likes

The issue seems to be that my settings of date, time, and DOW don't seem to replace of the original configuration start up values; the RTCC is running but not from my values.

0 Likes

Ted,

Please use the below method for setting the date and time -

/* Set the default date and time and wait till the operation is successful */

    while(Cy_RTC_SetDateAndTimeDirect(TIME_AT_RESET) != CY_RET_SUCCESS);

The reason is the API can return fail if RTC was synchronizing the data in the date registers. This could explain why it occasionally works.

Try that and let me know if it helps or not. In general, this rule applies to all "Cy_RTC_Set.." APIs. Check the API description for its return values and you can take a decision based on that.

Regards,

Meenakshi Sundaram R

0 Likes

Hmmm, I added that line but the program got stuck at it, like there was never success on setting the clock.

Let's check all the CY_RTC statements I have...

cy_stc_rtc_config_t dateTime; // declare a global variable to hold date and time

then in main...

    Cy_RTC_Init(&RTCC_config);

    Cy_RTC_SetHoursFormat(CY_RTC_24_HOURS);

    // while(Cy_RTC_SetDateAndTimeDirect(0,0,1,1,1,2000) != CY_RET_SUCCESS);  // I commented this out because it hung my code

and then the rest of my code above is all I have.   I also wanted a check of understanding.

1. When I add the RTC component and initialize it, there's actual hardware registers keeping a track of time and date.

2. I need to create my dateTime variable to hold a copy of these registers.

3. When I issue the command Cy_RTC_GetDateAndTime(&dateTime); I'm copying the hardware registers from the actual RTC into my variable elements and then I should be able to use them to print out or store in a file.

4. To get a new set of info into the actual RTC hardware registers, I have to either populate the elements of my dateTime variable and then run Cy_RTC_SetDateAndTime(&dateTime); or I can set the registers directly using

Cy_RTC_SetDateAndTimeDirect(dateTime.sec, dateTime.min, dateTime.hour,

                                               dateTime.date, dateTime.month, dateTime.year);

but that doesn't seem to work

Is this correct?

0 Likes

I've run through the PDL and datasheet documentation again line by line and can't see anything wrong.

When you look at my output listing above, the values in dateTime.min/hour etc are all good because, after I've assigned them from the data from the gps, I print them out and they are all good.

Then I issue

Cy_RTC_SetDateAndTimeDirect(dateTime.sec, dateTime.min, dateTime.hour, dateTime.date, dateTime.month, dateTime.year); but those values don't get into the RTC.

0 Likes

Okay - I see the issue.

The year value you are passing (2018) is wrong. The year parameter's valid range is 0-99 (it is assumed 0 ==> 2000 and 99 ==>2099). Please subtract 2000 from the year parameter and pass it to the function. That should solve the issue. As that is the reason the API never returns success

FYI, this info is present in the param description in the API header and PDL documentation

Let me know if that fixes the issue.

0 Likes

I just tried that, it hung on the Cy_RTC_ConvertDayOfWeek(... function because that DOES expect 2000 to 2100 year so I changed the call to 

dateTime.dayOfWeek = Cy_RTC_ConvertDayOfWeek(dateTime.date, dateTime.month, (2000 + dateTime.year));

and it got past that statement but the

Cy_RTC_SetDateAndTimeDirect(dateTime.sec, dateTime.min, dateTime.hour,dateTime.date, dateTime.month, dateTime.year);

runs and the month/date/year is OK now but the time is wrong!

sec = 52

min = 50

hour = 12

year = 18

month = 10

date = 05

DOW = 06

PSoC Date Time

Date 10/05/0018

Time 06:00:13 UTC  6

GPS Date Time

Date 10/05/2018

Time 12:50:52 UTC

We got here

GPS Date Time

Date 10/05/2018

Time 12:50:52 UTC

PSoC Date Time

Date 10/05/0018

Time 06:00:13 UTC  6

0 Likes

Is the above behavior with "while(Cy_RTC_SetDateAndTimeDirect(TIME_AT_RESET) != CY_RET_SUCCESS);" line or just the API call?

Looks like the date was same even before the call to set date/time - so I think the API returned failure.

0 Likes

Meenakshe,

It was with just the API, the code section that produces the output is now...

                if (hgps.fix) { // we have good GGA data

                    xprintf("%s", "GGA data received...\n");

                    // and we previously got good RMC data so we must have all the data we need

                    dateTime.hrFormat = CY_RTC_24_HOURS;

                    dateTime.amPm = CY_RTC_AM;

                    dateTime.sec = hgps.seconds;

                    xprintf("sec = %02d\r\n", dateTime.sec);

                    dateTime.min =  hgps.minutes;

                    xprintf("min = %02d\r\n", dateTime.min);

                    dateTime.hour = hgps.hours;

                    xprintf("hour = %02d\r\n", dateTime.hour);

                    dateTime.year = hgps.year;

                    xprintf("year = %02d\r\n", dateTime.year);

                    dateTime.month = hgps.month;

                    xprintf("month = %02d\r\n", dateTime.month);

                    dateTime.date = hgps.date;

                    xprintf("date = %02d\r\n", dateTime.date);

                    dateTime.dayOfWeek = Cy_RTC_ConvertDayOfWeek(dateTime.date, dateTime.month, (2000 + dateTime.year));  // calculate day of week

                    xprintf("DOW = %02d\r\n", dateTime.dayOfWeek);

                    dateTimeReport();

                    gpsDtTimeReport();

                    Cy_RTC_SetDateAndTimeDirect(dateTime.sec, dateTime.min, dateTime.hour,dateTime.date, dateTime.month, dateTime.year);

                    // Cy_RTC_SetDateAndTime(&dateTime); // and now the PSoC should run the RTCC                 

                    gpsState = normal; // for now we jump to normal and forget but what about a better fix? More satellites? etc?

                    xprintf("%s", "We got here\r\n");

                    GPS_UART_PutString("$PRTHS,U1OP,ALL=0,GGA=60\r"); // GPS only GGA data every 60 seconds

                    gpsDtTimeReport();

                    dateTimeReport();

                    dstrState = getSWIMSdata;

                }

0 Likes

Ted,

I think you will have to use the while() irrespective of whether you pass the parameters correct or not. At least, you need to handle the response from the API i.e. whether it succeeds or not.

Can you please check the CE222604 code? It does something similar with RTC but uses Current Time server running on an iPhone for getting the time instead of GPS.

If all these do not help, can you share your project? I will try to go over the settings and see what is the issue.

Regards,

Meenakshi Sundaram R

0 Likes

Meenakshi,

I looked at CE222604 code but it's for RTOS.  In looking at the PDL documentation for the RTC, it looks like there may be a write enable command required before I can put data into the RTCC registers, see Cy_RTC_WriteEnable();  any comments on that?

I am willing to share the project with you but I'd rather do that privately so that it can't be seen by the public; is that possible?

Thanks,

Ted

[EDIT] I was thinking I would build a stripped out project for this and then I looked at the initialization code and wondered if that's what the issue is.

I declare my variable like this...

cy_stc_rtc_config_t dateTime;

but my initialization is like this...

    Cy_RTC_Init(&RTCC_config);

    Cy_RTC_SetHoursFormat(CY_RTC_24_HOURS);

and I wondered if the initialization should have been

    Cy_RTC_Init(&dateTime);

    Cy_RTC_SetHoursFormat(CY_RTC_24_HOURS);

but when I tried that the program hung, presumably at that line. 

Can you please clarify what the initialization line(s) should be?

0 Likes

Hello Ted,

Sorry for missing out the thread, I was a bit away.

The RTCC_config is same format as dateTime structure, so it should be fine.

If it is stuck with you passing dateTime as parameter, then it needs to fail at one of the parameter validation asserts.

If you cannot share the project, then I would recommend you to try the below to see where it fails and rectify accordingly.

1. Debug the project

2. Step into the Cy_RTC_Init() function

3. Step till the point where it fails - this point/line of code should give you which parameter it fails

4. Read the parameter's valid range (should be part of the assert) and make sure your parameters meets the requirement.

In case you are using, SetDateAndTimeDirect() API, please use while(Cy_RTC_SetDateAndTimeDirect(...) != CY_RET_SUCCESS) - ideally this line should not hang if your parameters are proper. Though it may return fail one or two times, it should eventually succeed in setting the date and time. This is what we use in our examples and know that works.

If you want, you can send the project to my email ID (msur@cypress.com).

Regards,

Meenakshi Sundaram R

0 Likes

Meenakshi, (hope that is your first name)

I tweaked my code to allow running on a standard CY8CKIT-062-BLE board.  I'll send you my entire project along with comments on the things  that don't work properly.

Thanks,

Ted

0 Likes