STM32F1 RTC

Started by tha, December 05, 2020, 08:15:08 AM

Previous topic - Next topic

tha

18 Real-time clock (RTC)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
ส่วนนี้ใช้กับตระกูล STM32F10xxx ทั้งหมดเว้นแต่จะระบุไว้เป็นอย่างอื่น

18.1 RTC introduction
The real-time clock เป็น an independent timer. The RTC จัดให้มี a set ของ counters ที่รันต่อเนื่อง ซึ่งสามารถถูกใช้, ด้วย suitable software, เพื่อจัดให้มี a clock-calendar function.The counter values สามารถถูกเขียนเพื่อเซ็ต the current time/date of the system

The RTC core และ clock configuration (RCC_BDCR register) อยู่ใน the Backup domain, ซึ่งหมายความว่า RTC setting และเวลาถูกเก็บหลังจาก reset หรือ wakeup จาก Standby mode.

หลังจาก reset, การเข้าถึง the Backup registers และ RTC ถูกปิดการใช้งานและ the Backup domain (BKP) ถูกป้องกันต้าน possible parasitic write access. เพื่อเปิดการใช้งานการเข้าถึง the Backup registers และ the RTC, มีขบวนการดังต่อไปนี้:
•    เปิดการใช้งาน the power and backup interface clocks โดยการเซ็ต the PWREN and BKPEN bits ใน the
      RCC_APB1ENR register
•    เซ็ต the DBP bit the Power Control Register (PWR_CR) เพื่อเปิดการใช้งานการเข้าถึง the Backup registers and RTC.

tha

18.2 RTC main features
•    Programmable prescaler: แฟคเตอร์ตัวหารได้ถึง 2 ยกกำลัง 20
•    32-bit programmable counter สำหรับ long-term measurement
•    Two separate clocks: PCLK1 สำหรับ the APB1 interface และ RTC clock (ต้องอย่างน้อยช้ากว่า the PCLK1 clock สี่เท่า)
•    The RTC clock source อาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้:
      – HSE clock ถูกหารโดย 128
      – LSE oscillator clock
      – LSI oscillator clock (อ้างอิงถึง Section 7.2.8: RTC clock สำหรับรายละเอียด)
•    Two separate reset types:
      – The APB1 interface ถูก reset โดย system reset
      – The RTC Core (Prescaler, Alarm, Counter and Divider) ถูก reset ได้เพียงโดย a Backup domain reset (ดู Section
         7.1.3: Backup domain reset).
•    Three dedicated maskable interrupt lines:
      – Alarm interrupt, สำหรับสร้าง a software programmable alarm interrupt.
      – Seconds interrupt, สำหรับสร้าง a periodic interrupt signal ด้วย a programmable period length (ได้ถึง 1 second).
      – Overflow interrupt, ตรวจจับได้เมื่อ the internal programmable counter ม้วนกลับไปยัง 0.

tha

18.3 RTC functional description

18.3.1 Overview (ภาพรวม)
The RTC ประกอบด้วย 2 main units (ดู Figure 179). อันดับแรกหนึ่ง (APB1 Interface) ถูกใช้เพื่อ interface กับ the APB1 bus. หน่วยนี้ยังบรรจุด้วย a set ของ 16-bit registers เข้าถึงได้จาก the APB1 bus ใน read หรือ write mode (สำหรับข้อมูลที่มากขึ้นอ้างอิงถึง Section 18.4: RTC registers). The APB1 interface ถูก clocked โดย the APB1 bus clock เพื่อ interface กับ the APB1 bus.

หน่วยอื่นอีกหน่วย (RTC Core) ประกอบด้วย a chain ของ programmable counters ทำจาก 2 main blocks. The first block คือ the RTC prescaler block, ซึ่งสร้าง the RTC time base TR_CLK ที่สามารถถูกโปรแกรมให้มี a period ได้ถึง 1 second. มันประกอบด้วย a 20-bit programmable divider (RTC Prescaler). ทุกๆ TR_CLK period, the RTC สร้าง an interrupt (Second Interrupt) ถ้ามันถูกเปิดการใช้งานใน the RTC_CR register. The second block คือ a 32-bit programmable counter ที่สามารถถูกเริ่มต้นเป็น the current system time. The system time ถูกเพิ่มขึ้นที่ the TR_CLK rate และเปรียบเทียบกับ a programmable date (เก็บใน the RTC_ALR register) เพื่อสร้าง an alarm interrupt, ถ้าถูกเปิดการใช้งานใน the RTC_CR control register.




tha

18.3.2 Resetting RTC registers
system registers ทั้งหมดถูก reset พร้อมกันโดย a System Reset หรื Power Reset, ยกเว้นสำหรับ RTC_PRL, RTC_ALR, RTC_CNT, และ RTC_DIV.

The RTC_PRL, RTC_ALR, RTC_CNT, และ RTC_DIV registers ถูก reset ได้เพียงโดย a Backup Domain reset. อ้างอิงถึง Section 7.1.3..

18.3.3 Reading RTC registers
The RTC core เป็นอิสระอย่างสมบูรณ์จาก the RTC APB1 interface.

Software เข้าถึง the RTC prescaler, counter และ alarm values ผ่านทาง the APB1 interface แต่ the associated readable registers ถูกอัปเดตภายในทีแต่ละ rising edge ของ the RTC clock ที่ประสานกันใหม่โดย the RTC APB1 clock. นี้เป็นจริงด้วยสำหรับ the RTC flags.

ซึ่งหมายความว่าการอ่านครั้งแรกใน the RTC APB1 registers อาจเสียหาย (โดยทั่วไปอ่านเป็น 0) ถ้า the APB1 interface ถูกปิดการใช้งานก่อนหน้านี้แล้วและการอ่านจะเกิดขึ้นทันทีหลังจาก the APB1 interface ถูกเปิดการใช้งานแต่ก่อน the first internal update ของ the registers. นี้สามารถเกิดขึ้นได้ถ้า :
•    A system reset หรือ power reset เกิดขึ้นแล้ว
•    The MCU เพิ่ง woken up จาก Standby mode (ดู Section 5.3: Low-power modes)
•    The MCU เพิ่ง woken up จาก Stop mode (ดู Section 5.3: Low-power modes)

ในทุกกรณีข้างต้น, the RTC core ยังคงทำงานต่อไปในขณะที่ the APB1 interface ถูกปิดการใช้งานแล้ว (reset, ไม่ถูก clocked หรือ unpowered).

ดังนั้นเมื่ออ่าน the RTC registers, หลังจากมีการปิดการใช้งาน the RTC APB1 interface, the software ต้องรอเป็นอันดับแรกสำหรับ the RSF bit (Register Synchronized Flag) ใน the
RTC_CRL register ถูกเซ็ตโดย hardware.

บันทึกไว้ว่า the RTC APB1 interface ไม่ถูกผลกระทบโดย WFI และ WFE low-power modes.

tha

18.3.4 Configuring RTC registers
เพื่อเขียนใน the RTC_PRL, RTC_CNT, RTC_ALR registers, the peripheral ต้องเข้าสู่ Configuration Mode. นี้ถูกทำโดยการเซ็ต the CNF bit ใน the RTC_CRL register.

นอกจากนี้, การเขียนไปยัง RTC register ใดๆ ถูกเปิดการใช้งานได้เพียงถ้า the previous write operation ถูกทำสำเร็จลง. เพื่อเปิดการใช้งานให้ the software ตรวจจับสถานะการณ์นี้, the RTOFF status bit ถูกจัดให้มีใน the RTC_CR register เพื่อแสดงว่า an update ของ the registers กำลังดำเนินการอยู่. ค่าใหม่สามารถถูกเขียนไปยัง the RTC registers ได้เพียงเมื่อ the RTOFF status bit value เป็น '1'.

Configuration procedure
1.    สำรวจ RTOFF, รอจนกระทั่งค่าของมันไปเป็น '1'
2.    เซ็ต the CNF bit เพื่อเข้าสู่ configuration mode
3.   เขียนหนึ่งหรือมากกว่า RTC registers
4.    เคลียร์ the CNF bit เพื่อออกจาก configuration mode
5.    สำรวจ RTOFF, รอจนกระทั่งค่าของมันไปเป็น '1' เพื่อเช็คการสิ้นสุดของ the write operation.

The write operation จะดำเนินการเพียงเมื่อ the CNF bit ถูกเคลียร์ ; มันใช้เวลาอย่างน้อย three RTCCLK cycles เพื่อเสร็จสมบูรณ์.

tha

18.3.5 RTC flag assertion
The RTC Second flag (SECF) ถูกยืนยันบนแต่ละ RTC Core clock cycle ก่อน the update ของ the RTC Counter.

The RTC Overflow flag (OWF) ถูกยืนยันบน the last RTC Core clock cycle ก่อน the counter ถึง 0x0000.

The RTC_Alarm และ RTC Alarm flag (ALRF) (ดู Figure 180) ถูกยืนยันบน the last RTC Core clock cycle ก่อน the counter ถึง the RTC Alarm value ที่เก็บใน the Alarm register เพิ่มขึ้นหนึ่ง (RTC_ALR + 1). The write operation ใน the RTC Alarm และ RTC Second flag ต้องถูก synchronized โดยการใช้หนึ่งในลำดับต่อไปนี้:
•    ใช้ the RTC Alarm interrupt และภายใน the RTC interrupt routine, the RTC Alarm และ/หรือ RTC Counter registers
      ถูก updated.
•    คอยสำหรับ SECF bit ถูกเซ็ตใน the RTC Control register. อัปเดต the RTC Alarm และ/หรือ the RTC Counter register.


tha

18.4 RTC registers
อ้างอิงถึง Section 2.2 บนหน้า 45 สำหรับรายการของตัวย่อที่ใช้ใน register descriptions.

The peripheral registers สามารถถูกเข้าถึงโดย half-words (16-bit) หรือ words (32-bit).

18.4.1 RTC control register high (RTC_CRH)
Address offset: 0x00
Reset value: 0x0000



Bits 15:3 สงวนไว้, บังคับโดย hardware เป็น 0.

Bit 2 OWIE: Overflow interrupt enable
     0: Overflow interrupt ถูกบัง.
     1: Overflow interrupt ถูกเปิดการใช้งาน.

Bit 1 ALRIE: Alarm interrupt enable
     0: Alarm interrupt ถูกบัง.
     1: Alarm interrupt ถูกเปิดการใช้งาน.

Bit 0 SECIE: Second interrupt enable
     0: Second interrupt ถูกบัง.
     1: Second interrupt ถูกเปิดการใช้งาน.

บิตเหล่านี้ถูกใช้เพื่อบัง interrupt requests. บันทึกไว้ว่าที่ reset ทุก interrupts ถูก disabled, ดังนั้นมันเป็นไปได้ที่จะเขียนถึง the RTC registers เพื่อให้แน่ใจว่าไม่มี interrupt requests ค้างอยู่หลังจากการเริ่มต้น. มันเป็นไปไม่ได้ที่จะเขียนถึง the RTC_CRH register เมื่อ the peripheral กำลังเสร็จสมบูรณ์ a previous write operation (ให้ flagged โดย RTOFF=0, ดู Section 18.3.4).

The RTC functions ถูกควบคุมโดย control register นี้. บางบิตต้องถูกเขียนโดยใช้ a specific configuration procedure (ดู Configuration procedure).

tha



Bits 15:6 Reserved, forced by hardware to 0.

Bit 5 RTOFF: RTC operation OFF
ด้วยบิตนี้ the RTC จะรายงาน the status ของ the last write operation ที่กระทำบน registers ของมัน, แสดงว่าเสร็จสมบูรณ์แล้วหรือไม่. ถ้าค่าของมันเป็น '0' จะเป็นไปไม่ได้ที่จะเขียนถึง the RTC registers ใดๆ. บิตนี้อ่านได้เพียงอย่างเดียว.
     0: Last write operation บน RTC registers ยังคงดำเนินการอยู่.
     1: Last write operation บน RTC registers สิ้นสุดลง.

Bit 4 CNF: Configuration flag
บิตนี้ต้องถูกเซ็ตโดย software เพื่อเข้าสู่ใน configuration mode เพื่อยอมให้ new values ถูกเขียนใน the RTC_CNT, RTC_ALR หรือ RTC_PRL registers. The write operation ถูกปฏิบัติได้เพียงเมื่อ the CNF bit ถูก reset โดย software หลังจากที่ถูกเซ็ตแล้ว.
     0: ออกจาก configuration mode (เริ่มการอัปเดตของ RTC registers).
     1: เข้าสู่ configuration mode.

Bit 3 RSF: Registers synchronized flag
บิตนี้ถูกเซ็ตโดย hardware ที่แต่ละเวลา the RTC_CNT and RTC_DIV registers ถูกอัปเดตและถูกเคลียร์โดย software. ก่อน read operation ใดๆหลังจาก an APB1 reset หรือ an APB1 clock
stop, บิตนี้ถูกเคลียร์โดย software, และ the user application ต้องคอยจนกระทั่งมันถูกเซ็ตเพื่อให้แน่ใจว่า the RTC_CNT, RTC_ALR หรือ RTC_PRL registers ถูก synchronized.
     0: Registers ยังไม่ synchronized.
     1: Registers synchronized.

Bit 2 OWF: Overflow flag
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the 32-bit programmable counter ล้น. An interrupt ถูกสร้างถ้า OWIE=1 ใน the RTC_CRH register. มันสามารถถูกเคลียร์ได้เพียงโดย software. การเขียน '1' ไม่มีผลอะไร.
     0: Overflow ไม่ตรวจพบ
     1: 32-bit programmable counter overflow เกิดขึ้น.

Bit 1 ALRF: Alarm flag
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the 32-bit programmable counter ถึงขอบเขต(the threshold) ที่เซ็ตใน the RTC_ALR register. An interrupt ถูกสร้างถ้า ALRIE=1 ใน the RTC_CRH register. มันสามารถถูกเคลียร์ได้เพียงโดย software. การเขียน '1' ไม่มีผลอะไร.
     0: Alarm ไม่ถูกตรวจจับ
     1: Alarm ถูกตรวจจับได้

Bit 0 SECF: Second flag (วินาที)
บิตนี้ถูกเซ็ตโดย hardware เมื่อ the 32-bit programmable prescaler ล้น, ดังนั้นจึงเพิ่ม the RTC counter. ดังนั้น flag นี้จัดให้มี a periodic signal ด้วย a period ตรงกันกับ the resolution ที่โปรแกรมสำหรับ the RTC counter (ปกติ one second). An interrupt ถูกสร้างถ้า SECIE=1 ใน the RTC_CRH register. มันสามารถถูกเคลียร์ได้เพียงโดย software. การเขียน '1' ไม่มีผลอะไร.
     0: Second flag condition ไม่พบ.
     1: Second flag condition พบ.

The functions ของ the RTC ถูกควบคุมโดย control register นี้. มันเป็นไปไม่ได้ที่จะเขียนถึง the RTC_CR register ขณะที่e the peripheral กำลังเสร็จสมบูรณ์ a previous write operation (ให้ flagged โดย RTOFF=0, ดู Section 18.3.4: Configuring RTC registers).

Note: flag ใดๆยังคงค้างจนกระทั่ง until the appropriate RTC_CR request bit ถูก reset โดย software, แสดงว่า the
          interrupt request ถูกได้รับแล้ว

          ที่รีเซ็ต the interrupts ถูก disabled, ไม่มี interrupt requests ค้างอยู่และมันสามารถเป็นได้ที่จะเขียนถึง to the RTC
          registers.

          The OWF, ALRF, SECF และ RSF bits ไม่ถูกอัปเดเมื่อ the APB1 clock ไม่ได้รันอยู่.

          The OWF, ALRF, SECF และ RSF bits สามารถถูกเซ็ตได้เพียง hardware และถูกเคลียร์ได้เพียงโดย software.

          ถ้า ALRF = 1 และ ALRIE = 1, the RTC global interrupt ถูก enabled. ถ้า EXTI Line 17 ถูก enabled อีกด้วยผ่านทาง
          the EXTI Controller, ทั้ง the RTC global interrupt และ the RTC Alarm interrupt ถูก enabled.

          ถ้า ALRF = 1, the RTC Alarm interrupt ถูก enabled ถ้า EXTI Line 17 ถูก enabled ผ่านทาง the EXTI Controller ใน
          interrupt mode. เมื่อ the EXTI Line 17 ถูก enabled ใน event mode, a pulse หนึ่งถูกสร้างบน line นี้ (no RTC Alarm
          interrupt generation).