1 /**
2  * \file
3  *
4  * \brief Generic CALENDAR functionality declaration.
5  *
6  * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 #ifndef _HPL_CALENDER_H_INCLUDED
44 #define _HPL_CALENDER_H_INCLUDED
45 
46 #include <compiler.h>
47 #include <utils_list.h>
48 #include "hpl_irq.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * \brief Calendar structure
56  *
57  * The Calendar structure forward declaration.
58  */
59 struct calendar_dev;
60 
61 /**
62  * \brief Available mask options for alarms.
63  *
64  * Available mask options for alarms.
65  */
66 enum calendar_alarm_option {
67 	/** Alarm disabled. */
68 	CALENDAR_ALARM_MATCH_DISABLED = 0,
69 	/** Alarm match on second. */
70 	CALENDAR_ALARM_MATCH_SEC,
71 	/** Alarm match on second and minute. */
72 	CALENDAR_ALARM_MATCH_MIN,
73 	/** Alarm match on second, minute, and hour. */
74 	CALENDAR_ALARM_MATCH_HOUR,
75 	/** Alarm match on second, minute, hour, and day. */
76 	CALENDAR_ALARM_MATCH_DAY,
77 	/** Alarm match on second, minute, hour, day, and month. */
78 	CALENDAR_ALARM_MATCH_MONTH,
79 	/** Alarm match on second, minute, hour, day, month and year. */
80 	CALENDAR_ALARM_MATCH_YEAR
81 };
82 
83 /**
84  * \brief Available mode for alarms.
85  */
86 enum calendar_alarm_mode { ONESHOT = 1, REPEAT };
87 /**
88  * \brief Prototype of callback on alarm match
89  */
90 typedef void (*calendar_drv_cb_alarm_t)(struct calendar_dev *const dev);
91 
92 /**
93  * \brief Structure of Calendar instance
94  */
95 struct calendar_dev {
96 	/** Pointer to the hardware base */
97 	void *hw;
98 	/** Alarm match callback */
99 	calendar_drv_cb_alarm_t callback;
100 	/** IRQ struct */
101 	struct _irq_descriptor irq;
102 };
103 
104 /**
105  * \brief Time struct for calendar
106  */
107 struct calendar_time {
108 	/*range from 0 to 59*/
109 	uint8_t sec;
110 	/*range from 0 to 59*/
111 	uint8_t min;
112 	/*range from 0 to 23*/
113 	uint8_t hour;
114 };
115 
116 /**
117  * \brief Time struct for calendar
118  */
119 struct calendar_date {
120 	/*range from 1 to 28/29/30/31*/
121 	uint8_t day;
122 	/*range from 1 to 12*/
123 	uint8_t month;
124 	/*absolute year>= 1970(such as 2000)*/
125 	uint16_t year;
126 };
127 
128 /** \brief Calendar driver struct
129  *
130  */
131 struct calendar_descriptor {
132 	struct calendar_dev    device;
133 	struct list_descriptor alarms;
134 	/*base date/time = base_year/1/1/0/0/0(year/month/day/hour/min/sec)*/
135 	uint32_t base_year;
136 	uint8_t  flags;
137 };
138 
139 /** \brief Date&Time struct for calendar
140  */
141 struct calendar_date_time {
142 	struct calendar_time time;
143 	struct calendar_date date;
144 };
145 
146 /** \brief struct for alarm time
147  */
148 struct _calendar_alarm {
149 	struct calendar_date_time  datetime;
150 	uint32_t                   timestamp;
151 	enum calendar_alarm_option option;
152 	enum calendar_alarm_mode   mode;
153 };
154 
155 /**
156  * \brief Initialize Calendar instance
157  *
158  * \param[in] dev The pointer to calendar device struct
159  *
160  * \return ERR_NONE on success, or an error code on failure.
161  */
162 int32_t _calendar_init(struct calendar_dev *const dev);
163 
164 /**
165  * \brief Deinitialize Calendar instance
166  *
167  * \param[in] dev The pointer to calendar device struct
168  *
169  * \return ERR_NONE on success, or an error code on failure.
170  */
171 int32_t _calendar_deinit(struct calendar_dev *const dev);
172 
173 /**
174  * \brief Enable Calendar instance
175  *
176  * \param[in] dev The pointer to calendar device struct
177  *
178  * \return ERR_NONE on success, or an error code on failure.
179  */
180 int32_t _calendar_enable(struct calendar_dev *const dev);
181 
182 /**
183  * \brief Disable Calendar instance
184  *
185  * \param[in] dev The pointer to calendar device struct
186  *
187  * \return ERR_NONE on success, or an error code on failure.
188  */
189 int32_t _calendar_disable(struct calendar_dev *const dev);
190 /**
191  * \brief Set time for calendar
192  *
193  * \param[in] dev The pointer to calendar device struct
194  * \param[in] p_calendar_time Pointer to the time configuration
195  *
196  * \return ERR_NONE on success, or an error code on failure.
197  */
198 int32_t _calendar_set_time(struct calendar_dev *const dev, struct calendar_time *const p_calendar_time);
199 
200 /**
201  * \brief Set date for calendar
202  *
203  * \param[in] dev The pointer to calendar device struct
204  * \param[in] p_calendar_date Pointer to the date configuration
205  *
206  * \return ERR_NONE on success, or an error code on failure.
207  */
208 int32_t _calendar_set_date(struct calendar_dev *const dev, struct calendar_date *const p_calendar_date);
209 
210 /**
211  * \brief Get the time for calendar HAL instance and hardware
212  *  Retrieve the time from calendar instance.
213  *
214  * \param[in] dev The pointer to calendar device struct
215  * \param[in] date_time Pointer to value that will be filled with current time
216  *
217  * \return Return current counter value
218  */
219 uint32_t _calendar_get_date_time(struct calendar_dev *const dev, struct calendar_date_time *const date_time);
220 
221 /**
222  * \brief Set compare value for calendar
223  *
224  * \param[in] dev The pointer to calendar device struct
225  * \param[in] alarm Pointer to the configuration
226  *
227  * \return ERR_NONE on success, or an error code on failure.
228  */
229 int32_t _calendar_set_alarm(struct calendar_dev *const dev, struct _calendar_alarm *const alarm);
230 
231 /**
232  * \brief Register callback for calendar alarm
233  *
234  * \param[in] dev The pointer to calendar device struct
235  * \param[in] callback The pointer to callback function
236  *
237  * \return ERR_NONE on success, or an error code on failure.
238  */
239 int32_t _calendar_register_callback(struct calendar_dev *const dev, calendar_drv_cb_alarm_t callback);
240 
241 /**
242  * \brief Set calendar IRQ
243  *
244  * \param[in] dev The pointer to calendar device struct
245  */
246 void _calendar_set_irq(struct calendar_dev *const dev);
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif /* _HPL_RTC_H_INCLUDED */
253