]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Pearl_Gecko_Simplicity_Studio/SiLabs_Source/emlib/em_rtcc.c
Replace Gecko Simplicity Studio project that had multiple build configurations with...
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Pearl_Gecko_Simplicity_Studio / SiLabs_Source / emlib / em_rtcc.c
1 /***************************************************************************//**\r
2  * @file\r
3  * @brief Real Time Counter with Calendar (RTCC) Peripheral API\r
4  * @version 4.2.1\r
5  *******************************************************************************\r
6  * @section License\r
7  * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>\r
8  *******************************************************************************\r
9  *\r
10  * Permission is granted to anyone to use this software for any purpose,\r
11  * including commercial applications, and to alter it and redistribute it\r
12  * freely, subject to the following restrictions:\r
13  *\r
14  * 1. The origin of this software must not be misrepresented; you must not\r
15  *    claim that you wrote the original software.\r
16  * 2. Altered source versions must be plainly marked as such, and must not be\r
17  *    misrepresented as being the original software.\r
18  * 3. This notice may not be removed or altered from any source distribution.\r
19  *\r
20  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no\r
21  * obligation to support this Software. Silicon Labs is providing the\r
22  * Software "AS IS", with no express or implied warranties of any kind,\r
23  * including, but not limited to, any implied warranties of merchantability\r
24  * or fitness for any particular purpose or warranties against infringement\r
25  * of any proprietary rights of a third party.\r
26  *\r
27  * Silicon Labs will not be liable for any consequential, incidental, or\r
28  * special damages, or any other relief, or for any claim by any third party,\r
29  * arising from your use of this Software.\r
30  *\r
31  ******************************************************************************/\r
32 \r
33 #include "em_rtcc.h"\r
34 #if defined( RTCC_COUNT ) && ( RTCC_COUNT == 1 )\r
35 #include "em_bus.h"\r
36 \r
37 /***************************************************************************//**\r
38  * @addtogroup EM_Library\r
39  * @{\r
40  ******************************************************************************/\r
41 \r
42 /***************************************************************************//**\r
43  * @addtogroup RTCC\r
44  * @brief Real Time Counter (RTCC) Peripheral API\r
45  * @{\r
46  ******************************************************************************/\r
47 \r
48 /*******************************************************************************\r
49  *******************************   DEFINES   ***********************************\r
50  ******************************************************************************/\r
51 \r
52 /*******************************************************************************\r
53  **************************   LOCAL FUNCTIONS   ********************************\r
54  ******************************************************************************/\r
55 \r
56 /*******************************************************************************\r
57  **************************   GLOBAL FUNCTIONS   *******************************\r
58  ******************************************************************************/\r
59 \r
60 /***************************************************************************//**\r
61  * @brief\r
62  *   Configure the selected capture/compare channel of the RTCC.\r
63  *\r
64  * @details\r
65  *   Use this function to configure a RTCC channel.\r
66  *   Select capture/compare mode, match output action, overflow output action\r
67  *   and PRS input configuration.\r
68  *   Refer to the configuration structure @ref RTCC_CCChConf_TypeDef for more\r
69  *   details.\r
70  *\r
71  * @param[in] ch\r
72  *   Channel selector.\r
73  *\r
74  * @param[in] confPtr\r
75  *   Pointer to configuration structure.\r
76  ******************************************************************************/\r
77 void RTCC_ChannelInit( int ch, RTCC_CCChConf_TypeDef const *confPtr )\r
78 {\r
79   EFM_ASSERT( RTCC_CH_VALID( ch ) );\r
80   EFM_ASSERT( (uint32_t)confPtr->compMask\r
81               < ( _RTCC_CC_CTRL_COMPMASK_MASK >> _RTCC_CC_CTRL_COMPMASK_SHIFT )\r
82               + 1 );\r
83 \r
84   /** Configure the selected capture/compare channel. */\r
85   RTCC->CC[ch].CTRL = ( (uint32_t)confPtr->chMode << _RTCC_CC_CTRL_MODE_SHIFT )\r
86                       | ( (uint32_t)confPtr->compMatchOutAction << _RTCC_CC_CTRL_CMOA_SHIFT )\r
87                       | ( (uint32_t)confPtr->prsSel << _RTCC_CC_CTRL_PRSSEL_SHIFT )\r
88                       | ( (uint32_t)confPtr->inputEdgeSel << _RTCC_CC_CTRL_ICEDGE_SHIFT )\r
89                       | ( (uint32_t)confPtr->compBase << _RTCC_CC_CTRL_COMPBASE_SHIFT )\r
90                       | ( (uint32_t)confPtr->compMask << _RTCC_CC_CTRL_COMPMASK_SHIFT )\r
91                       | ( (uint32_t)confPtr->dayCompMode << _RTCC_CC_CTRL_DAYCC_SHIFT );\r
92 }\r
93 \r
94 /***************************************************************************//**\r
95  * @brief\r
96  *   Enable/disable RTCC.\r
97  *\r
98  * @param[in] enable\r
99  *   True to enable RTCC, false to disable.\r
100  ******************************************************************************/\r
101 void RTCC_Enable( bool enable )\r
102 {\r
103   /* Bitbanding the enable bit in the CTRL register (atomic). */\r
104   BUS_RegBitWrite((&RTCC->CTRL), _RTCC_CTRL_ENABLE_SHIFT, enable);\r
105 }\r
106 \r
107 /***************************************************************************//**\r
108  * @brief\r
109  *   Initialize RTCC.\r
110  *\r
111  * @details\r
112  *   Note that the compare values must be set separately with RTCC_CompareSet().\r
113  *   That should probably be done prior to the use of this function if\r
114  *   configuring the RTCC to start when initialization is completed.\r
115  *\r
116  * @param[in] init\r
117  *   Pointer to RTCC initialization structure.\r
118  ******************************************************************************/\r
119 void RTCC_Init( const RTCC_Init_TypeDef *init )\r
120 {\r
121   RTCC->CTRL = ( (uint32_t)init->enable << _RTCC_CTRL_ENABLE_SHIFT )\r
122                | ( (uint32_t)init->debugRun << _RTCC_CTRL_DEBUGRUN_SHIFT )\r
123                | ( (uint32_t)init->precntWrapOnCCV0 << _RTCC_CTRL_PRECCV0TOP_SHIFT )\r
124                | ( (uint32_t)init->cntWrapOnCCV1 << _RTCC_CTRL_CCV1TOP_SHIFT )\r
125                | ( (uint32_t)init->presc << _RTCC_CTRL_CNTPRESC_SHIFT )\r
126                | ( (uint32_t)init->prescMode << _RTCC_CTRL_CNTTICK_SHIFT )\r
127 #if defined(_RTCC_CTRL_BUMODETSEN_MASK)\r
128                | ( (uint32_t)init->enaBackupModeSet << _RTCC_CTRL_BUMODETSEN_SHIFT )\r
129 #endif\r
130                | ( (uint32_t)init->enaOSCFailDetect << _RTCC_CTRL_OSCFDETEN_SHIFT )\r
131                | ( (uint32_t)init->cntMode << _RTCC_CTRL_CNTMODE_SHIFT )\r
132                | ( (uint32_t)init->disLeapYearCorr << _RTCC_CTRL_LYEARCORRDIS_SHIFT );\r
133 }\r
134 \r
135 /***************************************************************************//**\r
136  * @brief\r
137  *   Restore RTCC to its reset state.\r
138  ******************************************************************************/\r
139 void RTCC_Reset( void )\r
140 {\r
141   int i;\r
142 \r
143   /* Restore all RTCC registers to their default values. */\r
144   RTCC_Unlock();\r
145   RTCC->CTRL    = _RTCC_CTRL_RESETVALUE;\r
146   RTCC->PRECNT  = _RTCC_PRECNT_RESETVALUE;\r
147   RTCC->CNT     = _RTCC_CNT_RESETVALUE;\r
148   RTCC->TIME    = _RTCC_TIME_RESETVALUE;\r
149   RTCC->DATE    = _RTCC_DATE_RESETVALUE;\r
150   RTCC->IEN     = _RTCC_IEN_RESETVALUE;\r
151   RTCC->IFC     = _RTCC_IFC_MASK;\r
152   RTCC_StatusClear();\r
153   RTCC->EM4WUEN = _RTCC_EM4WUEN_RESETVALUE;\r
154 \r
155   for (i = 0; i < 3; i++)\r
156   {\r
157     RTCC->CC[i].CTRL = _RTCC_CC_CTRL_RESETVALUE;\r
158     RTCC->CC[i].CCV  = _RTCC_CC_CCV_RESETVALUE;\r
159     RTCC->CC[i].TIME = _RTCC_CC_TIME_RESETVALUE;\r
160     RTCC->CC[i].DATE = _RTCC_CC_DATE_RESETVALUE;\r
161   }\r
162 }\r
163 \r
164 /***************************************************************************//**\r
165  * @brief\r
166  *   Clear STATUS register.\r
167  ******************************************************************************/\r
168 void RTCC_StatusClear( void )\r
169 {\r
170   while ( RTCC->SYNCBUSY & RTCC_SYNCBUSY_CMD )\r
171   {\r
172     // Wait for syncronization.\r
173   }\r
174   RTCC->CMD = RTCC_CMD_CLRSTATUS;\r
175 }\r
176 \r
177 /** @} (end addtogroup RTCC) */\r
178 /** @} (end addtogroup EM_Library) */\r
179 \r
180 #endif /* defined( RTCC_COUNT ) && ( RTCC_COUNT == 1 ) */\r