Refactor to use new protocol module and sipo.
[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/module.h>
42 #include <cfg/debug.h>
43
44 #include <io/stm32.h>
45 #include <io/stm32_pwr.h>
46
47 #include <cpu/power.h> // cpu_relax()
48
49 #include <drv/rtc.h>
50
51 /* PWR registers base */
52 static struct PWR *PWR = (struct PWR *)PWR_BASE;
53
54 /* RTC clock source: LSE */
55 #define RTC_CLKSRC      0x00000100
56 /* RTC clock: 32768 Hz */
57 #define RTC_CLOCK       32768
58 /* RTC clock period (in ms) */
59 #define RTC_PERIOD      1000
60
61 /* RTC control register */
62 #define RTC_CRH         (*(reg16_t *)(RTC_BASE + 0x00))
63 #define RTC_CRL         (*(reg16_t *)(RTC_BASE + 0x04))
64
65 #define RTC_CRL_SECIE         BV(0)
66 #define RTC_CRL_ALRIE         BV(1)
67 #define RTC_CRL_OWIE          BV(2)
68
69 #define RTC_CRL_SECF          BV(0)
70 #define RTC_CRL_ALRF          BV(1)
71 #define RTC_CRL_OWF           BV(2)
72 #define RTC_CRL_RSF           BV(3)
73 #define RTC_CRL_CNF           BV(4)
74 #define RTC_CRL_RTOFF         BV(5)
75
76 /* RTC prescaler load register */
77 #define RTC_PRLH        (*(reg16_t *)(RTC_BASE + 0x08))
78 #define RTC_PRLL        (*(reg16_t *)(RTC_BASE + 0x0c))
79
80 /* RTC prescaler divider register */
81 #define RTC_DIVH        (*(reg16_t *)(RTC_BASE + 0x10))
82 #define RTC_DIVL        (*(reg16_t *)(RTC_BASE + 0x14))
83
84 /* RTC counter register */
85 #define RTC_CNTH        (*(reg16_t *)(RTC_BASE + 0x18))
86 #define RTC_CNTL        (*(reg16_t *)(RTC_BASE + 0x1c))
87
88 /* RTC alarm register */
89 #define RTC_ALRH        (*(reg16_t *)(RTC_BASE + 0x20))
90 #define RTC_ALRL        (*(reg16_t *)(RTC_BASE + 0x24))
91
92 static void rtc_enterConfig(void)
93 {
94         /* Enter configuration mode */
95         RTC_CRL |= RTC_CRL_CNF;
96 }
97
98 static void rtc_exitConfig(void)
99 {
100         /* Exit from configuration mode */
101         RTC_CRL &= ~RTC_CRL_CNF;
102         while (!(RTC_CRL & RTC_CRL_RTOFF))
103                 cpu_relax();
104 }
105
106 uint32_t rtc_time(void)
107 {
108         return (RTC_CNTH << 16) | RTC_CNTL;
109 }
110
111 void rtc_setTime(uint32_t val)
112 {
113         rtc_enterConfig();
114         RTC_CNTH = (val >> 16) & 0xffff;
115         RTC_CNTL = val & 0xffff;
116         rtc_exitConfig();
117 }
118
119 /* Initialize the RTC clock */
120 int rtc_init(void)
121 {
122 #if CONFIG_KERN
123         MOD_CHECK(proc);
124 #endif
125         /* Enable clock for Power interface */
126         RCC->APB1ENR |= RCC_APB1_PWR;
127
128         /* Enable access to RTC registers */
129         PWR->CR |= PWR_CR_DBP;
130
131         /* Enable LSE */
132         RCC->BDCR |= RCC_BDCR_LSEON;
133         /* Wait for LSE ready */
134         while (!(RCC->BDCR & RCC_BDCR_LSERDY))
135                 cpu_relax();
136
137         /* Set clock source and enable RTC peripheral */
138         RCC->BDCR |= RTC_CLKSRC | RCC_BDCR_RTCEN;
139
140         rtc_enterConfig();
141
142         /* Set prescaler */
143         RTC_PRLH = ((RTC_PERIOD * RTC_CLOCK / 1000 - 1) >> 16) & 0xff;
144         RTC_PRLL = ((RTC_PERIOD * RTC_CLOCK / 1000 - 1)) & 0xffff;
145
146         rtc_exitConfig();
147
148         /* Disable access to the RTC registers */
149         PWR->CR &= ~PWR_CR_DBP;
150
151         return 0;
152 }