mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 *******************************************************************************
elessair 0:f269e3021894 3 * Copyright (c) 2014, STMicroelectronics
elessair 0:f269e3021894 4 * All rights reserved.
elessair 0:f269e3021894 5 *
elessair 0:f269e3021894 6 * Redistribution and use in source and binary forms, with or without
elessair 0:f269e3021894 7 * modification, are permitted provided that the following conditions are met:
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * 1. Redistributions of source code must retain the above copyright notice,
elessair 0:f269e3021894 10 * this list of conditions and the following disclaimer.
elessair 0:f269e3021894 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
elessair 0:f269e3021894 12 * this list of conditions and the following disclaimer in the documentation
elessair 0:f269e3021894 13 * and/or other materials provided with the distribution.
elessair 0:f269e3021894 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
elessair 0:f269e3021894 15 * may be used to endorse or promote products derived from this software
elessair 0:f269e3021894 16 * without specific prior written permission.
elessair 0:f269e3021894 17 *
elessair 0:f269e3021894 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elessair 0:f269e3021894 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elessair 0:f269e3021894 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elessair 0:f269e3021894 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
elessair 0:f269e3021894 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
elessair 0:f269e3021894 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
elessair 0:f269e3021894 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
elessair 0:f269e3021894 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
elessair 0:f269e3021894 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
elessair 0:f269e3021894 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elessair 0:f269e3021894 28 *******************************************************************************
elessair 0:f269e3021894 29 */
elessair 0:f269e3021894 30 #include "rtc_api.h"
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 #if DEVICE_RTC
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 #include "mbed_error.h"
elessair 0:f269e3021894 35
elessair 0:f269e3021894 36 static int rtc_inited = 0;
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 static RTC_HandleTypeDef RtcHandle;
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 void rtc_init(void)
elessair 0:f269e3021894 41 {
elessair 0:f269e3021894 42 RCC_OscInitTypeDef RCC_OscInitStruct;
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 if (rtc_inited) return;
elessair 0:f269e3021894 45 rtc_inited = 1;
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 RtcHandle.Instance = RTC;
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 #if !RTC_LSI
elessair 0:f269e3021894 50 // Enable LSE Oscillator
elessair 0:f269e3021894 51 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
elessair 0:f269e3021894 52 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
elessair 0:f269e3021894 53 RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT
elessair 0:f269e3021894 54 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
elessair 0:f269e3021894 55 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
elessair 0:f269e3021894 56 // Connect LSE to RTC
elessair 0:f269e3021894 57 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
elessair 0:f269e3021894 58 } else {
elessair 0:f269e3021894 59 error("Cannot initialize RTC with LSE\n");
elessair 0:f269e3021894 60 }
elessair 0:f269e3021894 61 #else
elessair 0:f269e3021894 62 // Enable Power clock
elessair 0:f269e3021894 63 __HAL_RCC_PWR_CLK_ENABLE();
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 // Enable access to Backup domain
elessair 0:f269e3021894 66 HAL_PWR_EnableBkUpAccess();
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 // Reset Backup domain
elessair 0:f269e3021894 69 __HAL_RCC_BACKUPRESET_FORCE();
elessair 0:f269e3021894 70 __HAL_RCC_BACKUPRESET_RELEASE();
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 // Enable LSI clock
elessair 0:f269e3021894 73 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
elessair 0:f269e3021894 74 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
elessair 0:f269e3021894 75 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
elessair 0:f269e3021894 76 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
elessair 0:f269e3021894 77 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
elessair 0:f269e3021894 78 error("Cannot initialize RTC with LSI\n");
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80 // Connect LSI to RTC
elessair 0:f269e3021894 81 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
elessair 0:f269e3021894 82 // This value is LSI typical value. To be measured precisely using a timer input capture for example.
elessair 0:f269e3021894 83 #endif
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 // Enable RTC
elessair 0:f269e3021894 86 __HAL_RCC_RTC_ENABLE();
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
elessair 0:f269e3021894 91 error("RTC error: RTC initialization failed.");
elessair 0:f269e3021894 92 }
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 void rtc_free(void)
elessair 0:f269e3021894 96 {
elessair 0:f269e3021894 97 #if RTC_LSI
elessair 0:f269e3021894 98 // Enable Power clock
elessair 0:f269e3021894 99 __PWR_CLK_ENABLE();
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 // Enable access to Backup domain
elessair 0:f269e3021894 102 HAL_PWR_EnableBkUpAccess();
elessair 0:f269e3021894 103
elessair 0:f269e3021894 104 // Reset Backup domain
elessair 0:f269e3021894 105 __HAL_RCC_BACKUPRESET_FORCE();
elessair 0:f269e3021894 106 __HAL_RCC_BACKUPRESET_RELEASE();
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 // Disable access to Backup domain
elessair 0:f269e3021894 109 HAL_PWR_DisableBkUpAccess();
elessair 0:f269e3021894 110 #endif
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 // Disable LSI and LSE clocks
elessair 0:f269e3021894 113 RCC_OscInitTypeDef RCC_OscInitStruct;
elessair 0:f269e3021894 114 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
elessair 0:f269e3021894 115 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
elessair 0:f269e3021894 116 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
elessair 0:f269e3021894 117 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
elessair 0:f269e3021894 118 HAL_RCC_OscConfig(&RCC_OscInitStruct);
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 rtc_inited = 0;
elessair 0:f269e3021894 121 }
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 int rtc_isenabled(void)
elessair 0:f269e3021894 124 {
elessair 0:f269e3021894 125 return rtc_inited;
elessair 0:f269e3021894 126 }
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 /*
elessair 0:f269e3021894 129 RTC Registers
elessair 0:f269e3021894 130 RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
elessair 0:f269e3021894 131 RTC_Month 1=january, 2=february, ..., 12=december
elessair 0:f269e3021894 132 RTC_Date day of the month 1-31
elessair 0:f269e3021894 133 RTC_Year year 0-99
elessair 0:f269e3021894 134 struct tm
elessair 0:f269e3021894 135 tm_sec seconds after the minute 0-61
elessair 0:f269e3021894 136 tm_min minutes after the hour 0-59
elessair 0:f269e3021894 137 tm_hour hours since midnight 0-23
elessair 0:f269e3021894 138 tm_mday day of the month 1-31
elessair 0:f269e3021894 139 tm_mon months since January 0-11
elessair 0:f269e3021894 140 tm_year years since 1900
elessair 0:f269e3021894 141 tm_wday days since Sunday 0-6
elessair 0:f269e3021894 142 tm_yday days since January 1 0-365
elessair 0:f269e3021894 143 tm_isdst Daylight Saving Time flag
elessair 0:f269e3021894 144 */
elessair 0:f269e3021894 145 time_t rtc_read(void)
elessair 0:f269e3021894 146 {
elessair 0:f269e3021894 147 RTC_DateTypeDef dateStruct;
elessair 0:f269e3021894 148 RTC_TimeTypeDef timeStruct;
elessair 0:f269e3021894 149 struct tm timeinfo;
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 RtcHandle.Instance = RTC;
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 // Read actual date and time
elessair 0:f269e3021894 154 // Warning: the time must be read first!
elessair 0:f269e3021894 155 HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
elessair 0:f269e3021894 156 HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
elessair 0:f269e3021894 157
elessair 0:f269e3021894 158 // Setup a tm structure based on the RTC
elessair 0:f269e3021894 159 timeinfo.tm_wday = dateStruct.WeekDay;
elessair 0:f269e3021894 160 timeinfo.tm_mon = dateStruct.Month - 1;
elessair 0:f269e3021894 161 timeinfo.tm_mday = dateStruct.Date;
elessair 0:f269e3021894 162 timeinfo.tm_year = dateStruct.Year;
elessair 0:f269e3021894 163 timeinfo.tm_hour = timeStruct.Hours;
elessair 0:f269e3021894 164 timeinfo.tm_min = timeStruct.Minutes;
elessair 0:f269e3021894 165 timeinfo.tm_sec = timeStruct.Seconds;
elessair 0:f269e3021894 166 // Daylight Saving Time information is not available
elessair 0:f269e3021894 167 timeinfo.tm_isdst = -1;
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 // Convert to timestamp
elessair 0:f269e3021894 170 time_t t = mktime(&timeinfo);
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 return t;
elessair 0:f269e3021894 173 }
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 void rtc_write(time_t t)
elessair 0:f269e3021894 176 {
elessair 0:f269e3021894 177 RTC_DateTypeDef dateStruct;
elessair 0:f269e3021894 178 RTC_TimeTypeDef timeStruct;
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 RtcHandle.Instance = RTC;
elessair 0:f269e3021894 181
elessair 0:f269e3021894 182 // Convert the time into a tm
elessair 0:f269e3021894 183 struct tm *timeinfo = localtime(&t);
elessair 0:f269e3021894 184
elessair 0:f269e3021894 185 // Fill RTC structures
elessair 0:f269e3021894 186 dateStruct.WeekDay = timeinfo->tm_wday;
elessair 0:f269e3021894 187 dateStruct.Month = timeinfo->tm_mon + 1;
elessair 0:f269e3021894 188 dateStruct.Date = timeinfo->tm_mday;
elessair 0:f269e3021894 189 dateStruct.Year = timeinfo->tm_year;
elessair 0:f269e3021894 190 timeStruct.Hours = timeinfo->tm_hour;
elessair 0:f269e3021894 191 timeStruct.Minutes = timeinfo->tm_min;
elessair 0:f269e3021894 192 timeStruct.Seconds = timeinfo->tm_sec;
elessair 0:f269e3021894 193
elessair 0:f269e3021894 194 // Change the RTC current date/time
elessair 0:f269e3021894 195 HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
elessair 0:f269e3021894 196 HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
elessair 0:f269e3021894 197 }
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 #endif