This chip is the follow-up of the famous DS1307 realtime clock (RTC).
It enables you to use RETAIN declared variables in your code on a Raspberry Pi or Beaglebone.
This RTC chip is used in the Monarco Hat but it can also be installed separatly as a standalone RTC add-on solution on a break-out board.
By adding a battery backed SRAM MCP7941x realtime clock to your Raspberry setup you obtain a hardware realtime clock and SRAM capability.
The RTC time itself can be accessed via several "of the shelve" libraries, so I did not bother to implement it (sorry).
However, obtaining current date and time is demonstrated in the provided example code with help of such an "of the shelve" library.
Install the .package file, it provides the library, example and an eula.
For a quick start, open the example code from the package installation directory. You can declare any type of variable as RETAIN.
If you have several POU's in your program containing RETAIN data, the total accumulated maximum size may not exceed 64 Bytes.
Anytime you write the retained data in your program, the SRAM gets updated with the new values.
Whenever your runtime stops or reboots, the values are stored in the battery buffered SRAM.
If your runtime resumes, the data is restored from SRAM to the userdata auto-magically.
abyMyRetainedBytes : ARRAY [0..63] OF BYTES; // maximum size of retained data in SRAM of mcp7491x
RETAIN data size restriction of 64 Bytes
If your total RETAIN data exceeds 64 Bytes, anything over this maximum is simply ignored and will not be retained!
Beware, no error messages or feedback is given if the data exceeds this size
You can verify this behaviour by opening the test program, re-edit the code for array's of e.g. 96 bytes size, fill it and then cold start the controller.
When you enable the runtime, only 64 bytes get reloaded from SRAM.
abyMyRetainedBytes : ARRAY [0..95] OF BYTES; // Exceeds maximum size of retained data in SRAM of mcp7491x, indexes [64..95] are simply ignored as they will not physically fit in the SRAM.
Performance as performed with the example code
Tested on a Raspberry Pi 3B+ with singlecore runtime license and using the example code from the package, this so you can verify these results for yourself if you wish.
If you have any meaningful feedback, please let me know. I will add your testresults to this page.
1) Use a single task, configure it as in the table.
1) Go online, flush the SRAM, then verify the array contents, then Cold reset the controller and press run and check the array again. The array should be empty.
2) Write the Test sequence, then Cold reset the controller and press run and check the array again, the array should be filled with correct value's.
Tested using Raspberry Pi 3B+ hardware with a Single Core License
As seen in the table, tasktimes as quick as 1 milliseconds is working correct. Beware, as your mileage may vary because of various reasons e.g. bigger programcode, more tasks, multitasking, etc, etc!
Installing the MCP7941 for SRAM/RTC usage in CODESYS
You can use this driver in combination with the Monarco HAT or any other pcb which uses the MCP7941x Realtimeclock (RTC).
It will provide you with 64 bytes battery buffered SRAM Retain support in CODESYS on a Raspberry Pi based hardware, as the Pi has no native SRAM support.
Getting and setting the time is not implemented in this library but can easily be achieve with several other common flavoured libraries in CODESYS.
MCP7941x hardware setup
In order to set up a MCP7941x real time clock, start by connecting it to the RPi and a quartz crystal.
The following a rough sketch on how to connect the pins to point one in the right direction - in practice, one might need capacitors to smooth signals or resitors to pull-up the SCL- and SDA-interface, depending for example on the battery and quartz crystal used.
The MCP7941x has two i2c adresses: 0x57 and 0x6f, if the addresses show up they are ready for communication.
0x57 leads to the EEPROM, a status register and a unique ID register whereas 0x6f is the address that leads to the SRAM and RTC itself
The MCP7941x is now ready for use with CODESYS
installing the MCP7941 for permanent use under linux
NOTE: If you install this chip for linux use, you cannot get access to the SRAM from CODESYS as linux has permanently claimed this resource!
Continue at your own risk
In order to establish a permanent linux OS connection add, first follow the steps to enable the RTC for use in CODESYS (see above)
Once the above steps are completed, follow the below steps.
sudo nano /etc/modules
Edit the file and close it via alt-x and save the changes
sudo nano /boot/config.txt
Again, edit the file and close it via alt-x and save the changes.
After reboot the 'sudo i2cdetect' yields;
The RTC module has been loaded and activated and the RTC is used by linux.
sudo nano /lib/udev/hwclock-set
comment out the lines;
#if [ -e /run/systemd/system ] ; then# exit 0#fi
Now, synchronize the RTC using the command
sudo hwclock -w
From now on, the RTC will keep the time and resynchronize the RPi's system time automatically on startup or manually by entering
sudo hwclock -r
The RTC is now in permanent use of the Linux OS!
CODESYS cannot use the SRAM from the RTC if the resource is in permanent use of the OS.
On a Monarco HAT UU shows up but you did not install the RTC for use on the linux side..
As for loading the rtc_ds1307 module, this is caused by the plug&play mechanism of the Monarco HAT and its onboard EEPROM.
For details, see https://www.raspberrypi.org/blog/introducing-raspberry-pi-hats/, namely this:
"The EEPROM holds the board manufacturer information, GPIO setup and a thing called a ‘device tree‘ fragment – basically a description of the attached hardware that allows Linux to automatically load the required drivers."
To disable automatic loading of the driver, use the following: