Real-Time Clock (RTC) in PSoC® 4 – KBA96934
Translation - Japanese: PSoC 4 の リアルタイムクロック (RTC) - Community Translated (JA)
Question: What is a real-time clock (RTC)? How do I configure and use an RTC in PSoC 4?
RTC is a clock that keeps track of the current time and date. RTCs keep track of time accurately using external crystals, typically 32.768-kHz watch crystal oscillators (WCO) or quartz crystal oscillators (QCO).
RTC Component in PSoC 4
All PSoC 4 devices support the Real-time Clock Component from PSoC Creator 3.2. However, the watch crystal oscillator or the external 32.768-kHz crystal support required for an accurate RTC operation is available only in PSoC 4100M/4200M/4100BL/4200BL families. Other families like PSoC 4000/4100/4200 support the RTC Component through an external clock or the internal main oscillator (IMO). In the latter case, the RTC accuracy will depend on the accuracy of the clock source used. Depending on the desired accuracy and available resources, there are two ways to use the RTC in a PSoC 4 device:
Option #1: RTC Using WCO
You can implement an RTC with an accuracy of +/- 250 ppm using this option but an external WCO is required for the operation. Only the 4100M/4200M/4100BL/4200BL families with the WCO capability support this implementation. Follow the steps provided below to build an RTC with WCO:
- Open PSoC Creator 3.2 or higher and create a new PSoC 4100M/4200M or PSoC 4100 BLE/4200 BLE project.
- Place a Real-time Component (RTC) [v1.0] Component in the TopDesign from Component Catalog as shown in Figure 1.
Figure 1. Placing RTC Component from Catalog
- Double-click and configure the Component as per your requirements. Do not check the Implement RTC update manually checkbox. See the Component datasheet for details on the parameters.
Figure 2. RTC Component Customizer
- Open the <project_name>.cydwr file from Workspace Explorer.
Figure 3. Opening 'cydwr' File from Workspace Explorer
- Go to the Clocks tab at the bottom of the cydwr window and double-click on the WCO clock option as shown in Figure 4.
Figure 4. Opening Configure System Clocks Window
- Enable the WCO (32.768 kHz) checkbox to enable the WCO and select WCO(32.768 kHz) as the LFCLK output (Figure 5).
- Enable the Timer2(WDT2) checkbox. By default, WDT2 is configured to generate a 1-second interrupt required for RTC clock tracking (Figure 5).
- In the RTC_Sel Mux, select Timer2(WDT2) as the output to the RTC (Figure 5); other WDT timers can be used to source the RTC – you will need to configure the RTC clock period to 1 second (or the period you desire).
- Click OK and build the project.
Figure 5. Low Frequency Clocks Configuration for Using RTC with WCO
- Call the RTC_Start() API in main.c for starting the RTC and use the RTC_GetTime() API to get the time and RTC_GetDate() API to get the date in the format selected in the RTC Component Customizer. See the RTC Component datasheet for details on all the APIs supported by the RTC Component.
The PSoC Creator Example project RTC_P4_WDT_Example implements an RTC using a WCO and WDT. See the Example project documentation for details on using the example with PSoC 4100M/4200M/4100BL/4200BL devices. To access the RTC example projects, right-click on the RTC Component in Topdesign or the Component Catalog and then select the Find Example Projects option.
Figure 6. Finding RTC Example Projects in PSoC Creator
Note that you also need to connect the WCO to the device for the RTC to work properly. You can use any standard 32.768-kHz crystal. Sample circuits provided in Figure 7 and Figure 8 show the WCO connection for ECS-.327-12.5-34B
Figure 7. WCO Connection for PSoC 4100M/4200M
Figure 8. WCO Connection for PSoC 4100BL/4200BL
Option #2: RTC Without WCO
You can implement an RTC with an external clock or IMO using this option but the accuracy achieved will depend on the accuracy of the clock source. Using this option, you can add the RTC functionality to any PSoC 4 device. To implement an RTC without a WCO, follow the steps below:
- Open PSoC Creator 3.2 or higher and create a PSoC 4 project.
- Place a Real-time Component (RTC) [v1.0] Component in the TopDesign from Component Catalog.
- Double-click and configure the Component as per your requirements. Check the Implement RTC update manually checkbox. See the Component datasheet for details on the parameters.
Figure 9. RTC Customizer for Using Clock Sources Without WCO
- In RTC_Sel Mux (Step#8 in Option#1), select None as the output to RTC as shown in Figure 9.
Figure 10. Low Frequency Clocks Configuration for Using RTC Without WCO
- Set up the RTC interrupt source (with the desired period); the source can be anything – an external pin interrupt, a TCPWM interrupt, Systick interrupt, and so on.
- Use the RTC_SetPeriod() API to configure the RTC period and call the RTC_Update() API from the interrupt source to update the RTC time and date.
See RTC_P4_SysTick_Example in the Find Example Project window for an RTC example using the Systick timer. For further details on RTC, see the RTC Component datasheet.
This widget could not be displayed.