=====================================================================
=     RTC Driver for CPI-RAS                                        =
=                                                          Ver1.05  =
=                                                  CONTEC Co.,Ltd.  =
=====================================================================

◆ Table of contents
=======
  Outline
  Restrictions
  How to build the RTC driver
  How to operation RTC
  RTC tool
  Version history


◆ Outline
===========
  A Linux driver is available for RTC function operations for CPI-RAS. Use this driver as necessary.
  The RTC driver creates a device file for the Linux hwclock command.


◆ Restrictions
===========
  The operation check environment is as follows. 
  Some of the contents below are described together with the confirmation information of past versions. 
  Please note that we have not confirmed the combination with all models and distributions(versions).
  In addition, we do not guarantee the operation in combination with all the listed models and each 
  distribution(version).
  * The contents below are as of the release. It may not match the support status of each distribution.
  * For operation confirmation information for individual models, see the contents of the operation 
  confirmation document for each model.

  Supporting Raspberry Pi models
  ==============
    Rasberry Pi 4 Model B  
    Rasberry Pi 3 Model B+

  Distribution                            Kernel version
  -----------------------------------     ------------------
   <32bit OS>
    Raspberry Pi OS 
     Release: March 15th 2024(bookworm)    6.6.20
     Release: May 3th 2023(bullseye)       6.1.21
     Release: April 4th 2022(bullseye)     5.15.32
     Release: March 4th 2021(buster)       5.10.17

    Raspberry Pi OS Legacy
     Release: March 12th 2024(bullseye)    6.1.21
     Release: May 3th 2023(buster)         5.10.103
     Release: April 4th 2022(buster)       5.10.103

    Ubuntu Server
     Ubuntu 22.04 LTS                      5.15.0-1005-raspi

   <64bit OS>
    Raspberry Pi OS 
     Release: March 12th 2024(bookworm)    6.6.20
     Release: May 3th 2023(bullseye)       6.1.21
     Release: April 4th 2022(bullseye)     5.15.32
     Release: March 4th 2021(buster)       5.10.17

    Ubuntu Server
     Ubuntu 22.04 LTS                      5.15.0-1005-raspi
     Ubuntu 20.04.2 LTS                    5.4.0-1028-raspi


◆ How to build the RTC driver
===========

  *You need to connect the Raspberry Pi to the internet to build the driver.

 1. Connect the product to the Raspberry Pi, and turn on the CPI-RAS.

 2. Updating package of OS, and reboot

      sudo apt update && sudo apt upgrade
      sudo reboot

 3. Install the Raspberry Pi kernel header package.

    <Raspberry Pi OS>
      sudo apt install raspberrypi-kernel-headers

    <Ubuntu>
      sudo apt install sudo apt install linux-headers-`uname -r`

 3. Using the downloaded file that is "Software for CPI-RAS" from the CONTEC website.
    If the downloaded file was not extracted, please extract it.

      tar xvfz CPI-RAS_XXX.tgz

 4. Execute the following commands in Terminal to build the driver, and install the built driver.

      cd ./CPI-RAS_XXX/rtcDrv
      sudo make drivers_install

 5. Load the RTC driver using the following command.

      sudo sh -c 'echo rx8900 0x32 > /sys/class/i2c-adapter/i2c-1/new_device'

 6. Confirm that the driver has been loaded and that the device file has been created using the following commands.

      lsmod | grep rtc_rv8803
      ls /dev/rtc


◆ How to operation RTC
===========

 - Use the hwclock command on Linux to check the RTC time.

      sudo hwclock -r

 - Use the following command to set the Linux system time to the RTC time.

      sudo hwclock -w 

 - Use the following command to set the RTC time to the Linux system time.

      sudo hwclock -s 


◆ RTC tool
===========

  RTC tool is control to the following.

    Get RTC time
    Set RTC time
    Get Alarm time
    Set Alarm time
    Enable / Disable Alarm function
    Get RTC IRQ state
    Clear RTC IRQ

  This tool is created by building from the source code.

  [The building Step]

    1. Execute the following commands in Terminal to build the tool, and install the built tool.

         cd ./CPI-RAS_XXX/rtcDrv
         sudo make tools_install

       RTC tool (rx8900tool) is installed to /usr/local/sbin .

  [Usage]

    RTC tool (rx8900tool) gives each function to command and executes it using the sudo command.

    I/F:
       rx8900tool [command] [args]

    Example： Get RTC time
       sudo rx8900tool get

    command:

        get              Get current the RTC time.

        set              Set the RTC time (UTC).
                         Please set UTC time.

                         Example： 2021-6-1 12:24:00
                           rx8900tool set '2021-06-01 12:24:00'

        get_alarm        Get the alarm time.
                         Those set for everyday, everyhour and every minute are displayed as **.
                         When the alarm function is enabled, [Alarm Enable] is displayed.
                         Also when the alarm function and the alarm flag are enabled, [Alarm Pending] is displayed.

        set_alarm        Set the alarm time (Day, hour and minute).

                         args: 
                           DD-hh:mm [--everyday] [--everyhour] [--everyminute] [--week SUN or MON ... SAT ]
                           WEEK-hh:mm [--everyday] [--everyhour] [--everyminute] [--week SUN or MON ... SAT ]

                         Example： If set at 12:24 on the 15th
                           rx8900tool set_alarm 15-12:24

                         Example： If set at 12:24 on the Wednesday
                           rx8900tool set_alarm WED-12:24

                         Example： If set at 12:24 on the Monday, Wednesday and Friday
                           rx8900tool set_alarm 01-12:24 --week MON --week WED --week FRI

                         Example： If set at 12:24 on the everyday
                           rx8900tool set_alarm 00-12:24 --everyday

                         Example： If set at 24min of everyhour on the 15th
                           rx8900tool set_alarm 15-00:24 --everyhour

                         Example： If set at every minute of 12 oclock on the 15th
                           rx8900tool set_alarm 15-12:00 --everyminute

        enable_alarm     Enable the alarm function.

        disable_alarm    Disable the alarm function.

        get_irq          Get the IRQ flag of RTC.
                         IRQ flag 
                           [UF] Update flag
                           [AF] Alarm flag

        clear_irq        Clear the IRQ flag.
                         Only the alarm flag is cleared.
                         The update flag is cleared, but it is set every second.

  [Alarm usage example]

    When the power is supplied to CPI-RAS and the power is not supplied to Raspberry Pi (LED of CPI-RAS is off),
    Raspberry Pi can be started automatically by the alarm function.

    Steps：

      1. Set the alarm time that you want it using by RTC tool.

        Example： If set at 09:00 on the everyday.

         sudo rx8900tool set_alarm 00-09:00 --everyday

      2. Clear the IRQ flag of RTC.

         sudo rx8900tool clear_irq

      3. Shutdown Raspberry Pi.

         sudo shutdown -h now

      4. Press and hold the CPI-RAS button to turn off the LED of CPI-RAS, and turn off the power supply to the Raspberry Pi.

      5. The Raspberry Pi will start at the alarm time.


◆ Version history
===========

Ver. 1.05(Ver.Apl.2024)
--------------------------------
  - RTC driver supports kernel version 6.6
  - Supported RTC wakealarm by Sysfs Interface  (kernel version 6.1 later)

Ver. 1.04(Ver.Aug.2023)
--------------------------------
  - Fixed the issue that RTC driver is not installed correctly when it is in .ko.xz format.

Ver. 1.03(Ver.May.2022)
--------------------------------
  - Supports kernel version 5.15
  - Added Raspberry Pi OS bullseye (April 4th 2022) and Ubuntu 22.04 LTS to the confirmed distribution.

Ver. 1.02(Ver.Dec.2021)
--------------------------------
  - Integrate to CORAS(LNX) for ARM
  - Change Makefile for easy build procedure

Ver. 1.01(Ver.Aug.2021)
--------------------------------
  - Added RTC tool
  - Added the alarm function.

Ver. 1.00(Ver.May.2021)
--------------------------------
  - First Release

