STM32-P103: add real-time clock (RTC) driver
[bertos.git] / bertos / cpu / cortex-m3 / drv / rtc_stm32.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32 RTC driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "clock_stm32.h"
39
40 #include <cfg/compiler.h>
41 #include <cfg/debug.h>
42
43 #include <io/stm32.h>
44 #include <io/stm32_pwr.h>
45
46 #include <cpu/power.h> // cpu_relax()
47
48 #include <drv/rtc.h>
49
50 /* PWR registers base */
51 static struct PWR *PWR = (struct PWR *)PWR_BASE;
52
53 /* RTC clock source: LSE */
54 #define RTC_CLKSRC      0x00000100
55 /* RTC clock: 32768 Hz */
56 #define RTC_CLOCK       32768
57 /* RTC clock period (in ms) */
58 #define RTC_PERIOD      1000
59
60 /* RTC control register */
61 #define RTC_CRH         (*(reg16_t *)(RTC_BASE + 0x00))
62 #define RTC_CRL         (*(reg16_t *)(RTC_BASE + 0x04))
63
64 #define RTC_CRL_SECIE         BV(0)
65 #define RTC_CRL_ALRIE         BV(1)
66 #define RTC_CRL_OWIE          BV(2)
67
68 #define RTC_CRL_SECF          BV(0)
69 #define RTC_CRL_ALRF          BV(1)
70 #define RTC_CRL_OWF           BV(2)
71 #define RTC_CRL_RSF           BV(3)
72 #define RTC_CRL_CNF           BV(4)
73 #define RTC_CRL_RTOFF         BV(5)
74
75 /* RTC prescaler load register */
76 #define RTC_PRLH        (*(reg16_t *)(RTC_BASE + 0x08))
77 #define RTC_PRLL        (*(reg16_t *)(RTC_BASE + 0x0c))
78
79 /* RTC prescaler divider register */
80 #define RTC_DIVH        (*(reg16_t *)(RTC_BASE + 0x10))
81 #define RTC_DIVL        (*(reg16_t *)(RTC_BASE + 0x14))
82
83 /* RTC counter register */
84 #define RTC_CNTH        (*(reg16_t *)(RTC_BASE + 0x18))
85 #define RTC_CNTL        (*(reg16_t *)(RTC_BASE + 0x1c))
86
87 /* RTC alarm register */
88 #define RTC_ALRH        (*(reg16_t *)(RTC_BASE + 0x20))
89 #define RTC_ALRL        (*(reg16_t *)(RTC_BASE + 0x24))
90
91 static void rtc_enterConfig(void)
92 {
93         /* Enter configuration mode */
94         RTC_CRL |= RTC_CRL_CNF;
95 }
96
97 static void rtc_exitConfig(void)
98 {
99         /* Exit from configuration mode */
100         RTC_CRL &= ~RTC_CRL_CNF;
101         while (!(RTC_CRL & RTC_CRL_RTOFF))
102                 cpu_relax();
103 }
104
105 uint32_t rtc_time(void)
106 {
107         return (RTC_CNTH << 16) | RTC_CNTL;
108 }
109
110 void rtc_setTime(uint32_t val)
111 {
112         rtc_enterConfig();
113         RTC_CNTH = (val >> 16) & 0xffff;
114         RTC_CNTL = val & 0xffff;
115         rtc_exitConfig();
116 }
117
118 /* Initialize the RTC clock */
119 int rtc_init(void)
120 {
121         /* Enable clock for Power interface */
122         RCC->APB1ENR |= RCC_APB1_PWR;
123
124         /* Enable access to RTC registers */
125         PWR->CR |= PWR_CR_DBP;
126
127         /* Enable LSE */
128         RCC->BDCR |= RCC_BDCR_LSEON;
129         /* Wait for LSE ready */
130         while (!(RCC->BDCR & RCC_BDCR_LSERDY))
131                 cpu_relax();
132
133         /* Set clock source and enable RTC peripheral */
134         RCC->BDCR |= RTC_CLKSRC | RCC_BDCR_RTCEN;
135
136         rtc_enterConfig();
137
138         /* Set prescaler */
139         RTC_PRLH = ((RTC_PERIOD * RTC_CLOCK / 1000 - 1) >> 16) & 0xff;
140         RTC_PRLL = ((RTC_PERIOD * RTC_CLOCK / 1000 - 1)) & 0xffff;
141
142         rtc_exitConfig();
143
144         /* Disable access to the RTC registers */
145         PWR->CR &= ~PWR_CR_DBP;
146
147         return 0;
148 }