chipKIT® Development Platform

Inspired by Arduino™

Fubarino SD from MCP + RTCC library

Created Tue, 07 Jan 2014 09:00:07 +0000 by ChristopheDupriez


ChristopheDupriez

Tue, 07 Jan 2014 09:00:07 +0000

Ave Happy Chipkiters and a very successful 2014 to you all!

I am trying to take advantage of Fubarino SD RTC after soldering a 32KHz crystal on SOSCI/SOSCO + the 2 little capacitors.

I changed the bootloader: in BoardConfig.h I have now the line: #pragma config FSOSCEN = ON under the paragraph: #elif defined(BOARDFUBARINO_SD_512K_USB_)

I uploaded the new bootloader with an ICD3 and reuploaded the ChipKit sketch. This sketch makes an GPRS transaction and gets the date/time from the GSM network. This works and records correctly the current time in the RTC. But afterward the time does not change within the RTC (stopped time, kind of apocalypse!). Using an oscilloscope, I do not see the Crystal waves with an oscilloscope. I suppose the Secundary oscillator is not configured correctly. The class I use is "vanilla" RTCC from Majenko.

Any idea would be very very welcome!

Christophe @ChristopheDupri


ChristopheDupriez

Wed, 08 Jan 2014 09:37:32 +0000

Hi! Update after some progress... After reading: http://www.microchip.com/forums/m574919-print.aspx we changed the capacitors: no effect. we removed the capacitors: no effect. we changed the crystal for another brand: it started oscillating but wrong frequency. we added 18pf capacitors: it started oscillating but RTCC time does not increase. Now it is a soft(ware) problem, not an hard(ware) one anymore!!!

Any suggestion about RTCC.cpp and PIC32MX512F128H ?

Wishing you a very nice day!

Christophe


majenko

Wed, 08 Jan 2014 10:57:17 +0000

Those 32KHz crystals are really sensitive to the right capacitance - I have had untold troubles trying to get "unknown" rescued crystals to oscillate. Some you need an extra series resistance, some want some parallel resistance, etc. Nasty things :P

But now it's oscillating we're past that bit. One thing I always forget for some reason is the RTCC.begin() statement. That configures the clock to run - without it it's not going to do anything. You need to check that that's in there somewhere, and if it is then delve into it and see what's up.

That code is rather old and one of my first attempts at programming a library...


ChristopheDupriez

Wed, 08 Jan 2014 12:08:28 +0000

Thanks Majenko!

I have RTCC.begin() in setup(). I will have to double check if the board initialization is not configuring the SOSCI/SOSCO in an incompatible way. What helps me is the fact that you used it and manage to make it work: the problem is probably in the "environment" (different MCU, different board) rather than within RTCC code.

Would you be so kind to indicate with which MCU and/or board you used RTCC.cpp with success ?

Have a nice afternoon! Christophe


majenko

Wed, 08 Jan 2014 12:29:26 +0000

I wrote the code originally on the Uno32, which had the caps and everything (except the crystal) already installed.


ChristopheDupriez

Wed, 22 Jan 2014 13:30:19 +0000

I would like to share with the community the conditions of my final success with Fubarino SD RTCC (I suppose similar conditions would apply for other boards):

  1. Hardware: we used a crystal ABRACON - AB38T-32.768KHZ, Farnell reference #1611828 and two capacitors 18pf Farnell reference #1759194

  2. You need to change the Booloader:

#pragma config FSOSCEN  = ON                                   // Secondary oscillator enable
  • program your MCU using PicKit3 and ICSP connector
  1. I modified the RTCC library for two reasons: a) to have it work (bit of trial and error but I do not know for sure why it worked at the end because hardware and software problems were combined) b) to separate date coding/encoding from specific RTC device management: this makes the library even more useful. I attach the result in the ZIP hereby (hoping that Majenko merges it with his work!)

"brutal" Usage example (initializing the RTC with the compilation date/time):

RTCC.begin();
	RTCCValue rv = RTCC.value();
	char aaStr[9] = MIN_TIME;
	unsigned char hours = (10*(aaStr[0]-'0'))+aaStr[1]-'0';
	unsigned char minutes = (10*(aaStr[3]-'0'))+aaStr[4]-'0';
	unsigned char seconds = (10*(aaStr[6]-'0'))+aaStr[7]-'0';
	rv.time(hours, minutes, seconds);

	/* Jan 19 2014 */
	char bStr[12]= MIN_DATE;
	unsigned char year = (10*(bStr[9]-'0'))+bStr[10]-'0';
	unsigned char month = months[bStr[0]-'A'];
	if (month == 4 && bStr[1] == 'u') {
		month = 8;
	}
	if (month == 3 && bStr[2] != 'r') {
		month = 5;
	}
	if (month == 1 && bStr[1] == 'u') {
		if (bStr[2] == 'n') month = 6;
		else month = 7;
	}
	
	if (bStr[4] == ' ') bStr[4] = '0';
	unsigned char day = (10*(bStr[4]-'0'))+bStr[5]-'0';
	rv.date(year, month, day);

	RTCC.set(rv);

Printing of current date/time:

unsigned char aStr[9];
	RTCCValue rv = RTCC.value();
	if (rv.valid()) {
		rv.date(aStr);
		Serial.print( (const char*) aStr);
		Serial.print(",");
		rv.time(aStr);
		Serial.print( (const char*) aStr);
		Serial.print(": ");
	}

Thanks all for your help!

Christophe


majenko

Wed, 22 Jan 2014 14:09:04 +0000

I have split my RTCC library off into a new repo of its own on github

[url]https://github.com/majenkotech/RTCC[/url]

I have added your changes. In future feel free to fork the repo, push changes to your own fork, then generate a pull request for me ;)

I am gradually sorting out and repackaging all my libs into their own repos.


ChristopheDupriez

Wed, 22 Jan 2014 14:40:10 +0000

Super! Thanks a lot Majenko!

By the way, another example which takes the output of CCLK command from a SIM900 GSM chip (in variable comm_buff) and stores the date/time in the RTC:

char* timeString = strstr((char *)comm_buf,"+CCLK");
timeString = strstr(timeString,"\"");
if (timeString) timeString = &timeString[1];
if (timeString && strlen(timeString) > 0) {
	char str[21];
	int elem[6];
	strncpy (str, timeString, sizeof(str));
	Serial.println(str);
	char * pch;
	pch = strtok (str," ,.-:/+");
	int pElem = 0;
	while (pch != NULL)
	{
		if (pElem >= sizeof(elem)) break; 
		elem[pElem] = atoi(pch);
		pElem++;
		pch = strtok (NULL, " ,.-:/+");
	}
	if (elem[0] > 13 && elem[0] < 55) { // 2014-2054 !
		RTCCValue rv = RTCCValue(0,0);
		rv.date(elem[0],elem[1],elem[2]);
		rv.time(elem[3],elem[4],elem[5]);
		unsigned char aStr[9];
		if (rv.valid()) {
			RTCC.set(rv);
			rv = RTCC.value();
			rv.date(aStr);
			Serial.print( (const char*) aStr);
			Serial.print(",");
			rv.time(aStr);
			Serial.print( (const char*) aStr);
			break;
		} else {
			rv.date(aStr);
			Serial.print( (const char*) aStr);
			Serial.print(",");
			rv.time(aStr);
			Serial.print( (const char*) aStr);
			Serial.println(": invalid, retry!");
		}
	} else {
		Serial.print(elem[0],DEC);
		Serial.println(": invalid year, retry!");
	}
}

Often, some retries are needed before receiving a nice date/time from the GSM network...

Christophe


ricklon

Wed, 22 Jan 2014 17:20:57 +0000

I think Keith V was able to upload a bootloader via a sketch. I think that might be useful here. I'll look back through my email and see if I can find some instructions on how to do it.


laputa

Sun, 23 Feb 2014 18:11:07 +0000

I have been holding off for a while, but I really need to get the RTC function up on my Fubarino.

I have someone who can solder the caps as per your spec (ugh), but I still have not been able to track down if I really need to change my bootloater to enable this.

Is there a way to turn on the needed oscillator from a sketch as is?

If so how/ is there an example?


EmbeddedMan

Wed, 26 Feb 2014 13:54:34 +0000

I've just confirmed that Majenko's RTCC library works great on the Microchip Fubarino SD with no bootloader modifications. I pulled a crystal from another development board (unknown part number) along with the caps. I downloaded the RTCC library from here : [url]http://hacking.majenko.co.uk/chipkit-uno32-real-time-clock-library#attachments[/url] and installed it in MPIDE, then ran the example code provided with the library. It appeared to do exactly what it's supposed to do.

*Brian


ChristopheDupriez

Wed, 26 Feb 2014 15:15:01 +0000

Good news Bryan!

I suppose this means that the bootloader installed at factory already contains in BoardConfig.h:

#pragma config FSOSCEN  = ON   // Secondary oscillator enable

which is not what I found for BOARD_FUBARINO_SD_512K_USB in: https://raw.github.com/chipKIT32/PIC32-avrdude-bootloader/master/bootloaders/BoardConfig.h In that case, where one can find the real source code for the actual bootloader installed at factory for Fubarinos SD ?

Thanks!

Christophe


EmbeddedMan

Thu, 27 Feb 2014 01:31:09 +0000

Christophe,

So your question led me to do some looking into exactly what's going on here.

Turns out that the config bit (FSOSCEN) can be used to turn on the secondary oscillator at reset, before any code has time to run. But setting FSOSCEN to OFF in the config bits does NOT prevent you from using the secondary oscillator. (This is contrary to how I thought it worked.)

At any time, no matter the state of FSOSCEN, you can set the SOSCEN bit in the OSCCON register to turn on the secondary oscillator. This is what Majenko's RTCC library does.

So no, the bootloader code currently in Github, as well as what's shipping on Fubarino boards, has FSOSCEN set to OFF, which is what we want. (We don't want this turned on at reset, as it would prevent us from using those bits as I/O bits until we turned it off.)

But, you can turn it on or off any time you want in your own code, or using the RTCC library.

Hope that clears it up for you! I sure learned a lot in this little adventure.

And super-kudos to Majenko for his RTCC library that just works. Dude, you're awesome.

*Brian


ChristopheDupriez

Thu, 27 Feb 2014 02:17:28 +0000

Thanks a lot Bryan for digging up to resolution!

3 weeks ago, I contributed to Majenko an evolution of RTCC library here: https://github.com/majenkotech/RTCC It has a separated class for timestamp storage (RTCCValue) and for clock+alarm access (RTCC). The PDF Manual has not been updated: RTCC.h and the example project are the doc ! I hope it may be useful tu you also.

I just discovered the CANopen standard for timestamp (6 bytes, precision up to the millisecond): I should add it in the coming weeks.

Have a nice day!

Christophe