mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c

Dependents:   Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay

Fork of mbed-dev by mbed official

Committer:
maxxir
Date:
Tue Nov 07 16:46:29 2017 +0000
Revision:
177:619788de047e
Parent:
157:ff67d9f36b67
To fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..;  Used direct RTC register manipulation for STM32F1xx;  rtc_read() && rtc_write()  (native rtc_init() - works good);  also added stub for non-working on STM32F1xx rtc_read_subseconds().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 157:ff67d9f36b67 1 /**
<> 157:ff67d9f36b67 2 * @file
<> 157:ff67d9f36b67 3 * @brief This file contains the function implementations for the Advanced
<> 157:ff67d9f36b67 4 * Encryption Standard (AES) peripheral module.
<> 157:ff67d9f36b67 5 */
<> 157:ff67d9f36b67 6
<> 157:ff67d9f36b67 7 /* ****************************************************************************
<> 157:ff67d9f36b67 8 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 157:ff67d9f36b67 9 *
<> 157:ff67d9f36b67 10 * Permission is hereby granted, free of charge, to any person obtaining a
<> 157:ff67d9f36b67 11 * copy of this software and associated documentation files (the "Software"),
<> 157:ff67d9f36b67 12 * to deal in the Software without restriction, including without limitation
<> 157:ff67d9f36b67 13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 157:ff67d9f36b67 14 * and/or sell copies of the Software, and to permit persons to whom the
<> 157:ff67d9f36b67 15 * Software is furnished to do so, subject to the following conditions:
<> 157:ff67d9f36b67 16 *
<> 157:ff67d9f36b67 17 * The above copyright notice and this permission notice shall be included
<> 157:ff67d9f36b67 18 * in all copies or substantial portions of the Software.
<> 157:ff67d9f36b67 19 *
<> 157:ff67d9f36b67 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 157:ff67d9f36b67 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 157:ff67d9f36b67 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 157:ff67d9f36b67 23 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 157:ff67d9f36b67 24 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 157:ff67d9f36b67 25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 157:ff67d9f36b67 26 * OTHER DEALINGS IN THE SOFTWARE.
<> 157:ff67d9f36b67 27 *
<> 157:ff67d9f36b67 28 * Except as contained in this notice, the name of Maxim Integrated
<> 157:ff67d9f36b67 29 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 157:ff67d9f36b67 30 * Products, Inc. Branding Policy.
<> 157:ff67d9f36b67 31 *
<> 157:ff67d9f36b67 32 * The mere transfer of this software does not imply any licenses
<> 157:ff67d9f36b67 33 * of trade secrets, proprietary technology, copyrights, patents,
<> 157:ff67d9f36b67 34 * trademarks, maskwork rights, or any other form of intellectual
<> 157:ff67d9f36b67 35 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 157:ff67d9f36b67 36 * ownership rights.
<> 157:ff67d9f36b67 37 *
<> 157:ff67d9f36b67 38 * $Date: 2016-09-09 12:50:17 -0500 (Fri, 09 Sep 2016) $
<> 157:ff67d9f36b67 39 * $Revision: 24348 $
<> 157:ff67d9f36b67 40 *
<> 157:ff67d9f36b67 41 *************************************************************************** */
<> 157:ff67d9f36b67 42
<> 157:ff67d9f36b67 43 /* **** Includes **** */
<> 157:ff67d9f36b67 44 #include <string.h> /* Included for memcpy() & #includes stddef for NULL */
<> 157:ff67d9f36b67 45
<> 157:ff67d9f36b67 46 #include "mxc_config.h"
<> 157:ff67d9f36b67 47 #include "aes.h"
<> 157:ff67d9f36b67 48 #include "nvic_table.h"
<> 157:ff67d9f36b67 49
<> 157:ff67d9f36b67 50 /**
<> 157:ff67d9f36b67 51 * @ingroup aes
<> 157:ff67d9f36b67 52 * @{
<> 157:ff67d9f36b67 53 */
<> 157:ff67d9f36b67 54
<> 157:ff67d9f36b67 55 /* **** Definitions **** */
<> 157:ff67d9f36b67 56
<> 157:ff67d9f36b67 57 /* **** Globals **** */
<> 157:ff67d9f36b67 58
<> 157:ff67d9f36b67 59 /* **** Local Function Prototypes **** */
<> 157:ff67d9f36b67 60 static int aes_memcpy32(uint32_t *out, uint32_t *in, unsigned int count);
<> 157:ff67d9f36b67 61
<> 157:ff67d9f36b67 62 /* **** Functions **** */
<> 157:ff67d9f36b67 63
<> 157:ff67d9f36b67 64 /* ************************************************************************* */
<> 157:ff67d9f36b67 65 int AES_SetKey(const uint8_t *key, mxc_aes_mode_t mode)
<> 157:ff67d9f36b67 66 {
<> 157:ff67d9f36b67 67 unsigned int len;
<> 157:ff67d9f36b67 68
<> 157:ff67d9f36b67 69 /* Erase any existing key */
<> 157:ff67d9f36b67 70 MXC_AES_MEM->key[7] = MXC_AES_MEM->key[6] = MXC_AES_MEM->key[5] = MXC_AES_MEM->key[4] \
<> 157:ff67d9f36b67 71 = MXC_AES_MEM->key[3] = MXC_AES_MEM->key[2] = MXC_AES_MEM->key[1] = MXC_AES_MEM->key[0] \
<> 157:ff67d9f36b67 72 = 0x00000000;
<> 157:ff67d9f36b67 73
<> 157:ff67d9f36b67 74 /* Determine length of key */
<> 157:ff67d9f36b67 75 if (mode == MXC_E_AES_MODE_256) {
<> 157:ff67d9f36b67 76 len = MXC_AES_KEY_256_LEN;
<> 157:ff67d9f36b67 77 } else if (mode == MXC_E_AES_MODE_192) {
<> 157:ff67d9f36b67 78 len = MXC_AES_KEY_192_LEN;
<> 157:ff67d9f36b67 79 } else if (mode == MXC_E_AES_MODE_128) {
<> 157:ff67d9f36b67 80 len = MXC_AES_KEY_128_LEN;
<> 157:ff67d9f36b67 81 } else {
<> 157:ff67d9f36b67 82 return E_BAD_PARAM;
<> 157:ff67d9f36b67 83 }
<> 157:ff67d9f36b67 84
<> 157:ff67d9f36b67 85 /* Load new key, based on key mode */
<> 157:ff67d9f36b67 86 if (aes_memcpy32((uint32_t *)MXC_AES_MEM->key, (uint32_t *)key, len / sizeof(uint32_t)) < 0) {
<> 157:ff67d9f36b67 87 return E_NULL_PTR;
<> 157:ff67d9f36b67 88 }
<> 157:ff67d9f36b67 89
<> 157:ff67d9f36b67 90 return E_SUCCESS;
<> 157:ff67d9f36b67 91 }
<> 157:ff67d9f36b67 92
<> 157:ff67d9f36b67 93 /* ************************************************************************* */
<> 157:ff67d9f36b67 94 int AES_ECBOp(const uint8_t *in, uint8_t *out, mxc_aes_mode_t mode, mxc_aes_dir_t dir)
<> 157:ff67d9f36b67 95 {
<> 157:ff67d9f36b67 96 /* Output array can't be a NULL, unless we are in _ASYNC mode */
<> 157:ff67d9f36b67 97 if ((out == NULL)
<> 157:ff67d9f36b67 98 && ((dir != MXC_E_AES_ENCRYPT_ASYNC) && (dir != MXC_E_AES_DECRYPT_ASYNC))) {
<> 157:ff67d9f36b67 99 return E_NULL_PTR;
<> 157:ff67d9f36b67 100 }
<> 157:ff67d9f36b67 101
<> 157:ff67d9f36b67 102 /* Another encryption is already in progress */
<> 157:ff67d9f36b67 103 if (MXC_AES->ctrl & MXC_F_AES_CTRL_START) {
<> 157:ff67d9f36b67 104 return E_BUSY;
<> 157:ff67d9f36b67 105 }
<> 157:ff67d9f36b67 106
<> 157:ff67d9f36b67 107 /* Clear interrupt flag and any existing configuration*/
<> 157:ff67d9f36b67 108 MXC_AES->ctrl = MXC_F_AES_CTRL_INTFL;
<> 157:ff67d9f36b67 109
<> 157:ff67d9f36b67 110 /* Select key size & direction
<> 157:ff67d9f36b67 111 *
<> 157:ff67d9f36b67 112 * Note: This is done first to detect argument errors, before sensitive data
<> 157:ff67d9f36b67 113 * is loaded into AES_MEM block
<> 157:ff67d9f36b67 114 *
<> 157:ff67d9f36b67 115 */
<> 157:ff67d9f36b67 116 switch (mode) {
<> 157:ff67d9f36b67 117 case MXC_E_AES_MODE_128:
<> 157:ff67d9f36b67 118 MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_128;
<> 157:ff67d9f36b67 119 break;
<> 157:ff67d9f36b67 120
<> 157:ff67d9f36b67 121 case MXC_E_AES_MODE_192:
<> 157:ff67d9f36b67 122 MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_192;
<> 157:ff67d9f36b67 123 break;
<> 157:ff67d9f36b67 124
<> 157:ff67d9f36b67 125 case MXC_E_AES_MODE_256:
<> 157:ff67d9f36b67 126 MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_256;
<> 157:ff67d9f36b67 127 break;
<> 157:ff67d9f36b67 128
<> 157:ff67d9f36b67 129 default:
<> 157:ff67d9f36b67 130 return E_BAD_PARAM;
<> 157:ff67d9f36b67 131 }
<> 157:ff67d9f36b67 132
<> 157:ff67d9f36b67 133 switch (dir) {
<> 157:ff67d9f36b67 134 case MXC_E_AES_ENCRYPT:
<> 157:ff67d9f36b67 135 case MXC_E_AES_ENCRYPT_ASYNC:
<> 157:ff67d9f36b67 136 MXC_AES->ctrl |= MXC_S_AES_CTRL_ENCRYPT_MODE;
<> 157:ff67d9f36b67 137 break;
<> 157:ff67d9f36b67 138
<> 157:ff67d9f36b67 139 case MXC_E_AES_DECRYPT:
<> 157:ff67d9f36b67 140 case MXC_E_AES_DECRYPT_ASYNC:
<> 157:ff67d9f36b67 141 MXC_AES->ctrl |= MXC_S_AES_CTRL_DECRYPT_MODE;
<> 157:ff67d9f36b67 142 break;
<> 157:ff67d9f36b67 143
<> 157:ff67d9f36b67 144 default:
<> 157:ff67d9f36b67 145 return E_BAD_PARAM;
<> 157:ff67d9f36b67 146 }
<> 157:ff67d9f36b67 147
<> 157:ff67d9f36b67 148 /* If non-blocking mode has been selected, interrupts are automatically enabled */
<> 157:ff67d9f36b67 149 if ((dir == MXC_E_AES_ENCRYPT_ASYNC) ||
<> 157:ff67d9f36b67 150 (dir == MXC_E_AES_DECRYPT_ASYNC)) {
<> 157:ff67d9f36b67 151 MXC_AES->ctrl |= MXC_F_AES_CTRL_INTEN;
<> 157:ff67d9f36b67 152 }
<> 157:ff67d9f36b67 153
<> 157:ff67d9f36b67 154 /* Load input into engine */
<> 157:ff67d9f36b67 155 if (aes_memcpy32((uint32_t *)MXC_AES_MEM->inp, (uint32_t *)in, MXC_AES_DATA_LEN / sizeof(uint32_t)) < 0) {
<> 157:ff67d9f36b67 156 return E_NULL_PTR;
<> 157:ff67d9f36b67 157 }
<> 157:ff67d9f36b67 158
<> 157:ff67d9f36b67 159 /* Start operation */
<> 157:ff67d9f36b67 160 MXC_AES->ctrl |= MXC_F_AES_CTRL_START;
<> 157:ff67d9f36b67 161
<> 157:ff67d9f36b67 162 /* Block, waiting on engine to complete, or fall through if non-blocking */
<> 157:ff67d9f36b67 163 if ((dir != MXC_E_AES_ENCRYPT_ASYNC) &&
<> 157:ff67d9f36b67 164 (dir != MXC_E_AES_DECRYPT_ASYNC)) {
<> 157:ff67d9f36b67 165 while (MXC_AES->ctrl & MXC_F_AES_CTRL_START) {
<> 157:ff67d9f36b67 166 /* Ensure that this wait loop is not optimized out */
<> 157:ff67d9f36b67 167 __NOP();
<> 157:ff67d9f36b67 168 }
<> 157:ff67d9f36b67 169
<> 157:ff67d9f36b67 170 /* Get output from engine */
<> 157:ff67d9f36b67 171 return AES_GetOutput(out);
<> 157:ff67d9f36b67 172 }
<> 157:ff67d9f36b67 173
<> 157:ff67d9f36b67 174 return E_SUCCESS;
<> 157:ff67d9f36b67 175 }
<> 157:ff67d9f36b67 176
<> 157:ff67d9f36b67 177 /* ************************************************************************* */
<> 157:ff67d9f36b67 178 int AES_GetOutput(uint8_t *out)
<> 157:ff67d9f36b67 179 {
<> 157:ff67d9f36b67 180 /* Don't read it out of the AES memory unless engine is idle */
<> 157:ff67d9f36b67 181 if (MXC_AES->ctrl & MXC_F_AES_CTRL_START) {
<> 157:ff67d9f36b67 182 return E_BUSY;
<> 157:ff67d9f36b67 183 }
<> 157:ff67d9f36b67 184
<> 157:ff67d9f36b67 185 /* Pull out result */
<> 157:ff67d9f36b67 186 if (aes_memcpy32((uint32_t *)out, (uint32_t *)MXC_AES_MEM->out, MXC_AES_DATA_LEN / sizeof(uint32_t)) < 0) {
<> 157:ff67d9f36b67 187 return E_NULL_PTR;
<> 157:ff67d9f36b67 188 }
<> 157:ff67d9f36b67 189
<> 157:ff67d9f36b67 190 /* Clear interrupt flag, write 1 to clear */
<> 157:ff67d9f36b67 191 MXC_AES->ctrl |= MXC_F_AES_CTRL_INTFL;
<> 157:ff67d9f36b67 192
<> 157:ff67d9f36b67 193 return E_SUCCESS;
<> 157:ff67d9f36b67 194 }
<> 157:ff67d9f36b67 195
<> 157:ff67d9f36b67 196 /**
<> 157:ff67d9f36b67 197 * @internal This memory copy is used only by the AES module to avoid data leakage by the standard C library.
<> 157:ff67d9f36b67 198 * Copy count number of 32-bit locations from in to out
<> 157:ff67d9f36b67 199 */
<> 157:ff67d9f36b67 200 static int aes_memcpy32(uint32_t *out, uint32_t *in, unsigned int count)
<> 157:ff67d9f36b67 201 {
<> 157:ff67d9f36b67 202 if ((out == NULL) || (in == NULL)) {
<> 157:ff67d9f36b67 203 /* Invalid arguments, but is internal-only so don't use error codes */
<> 157:ff67d9f36b67 204 return -1;
<> 157:ff67d9f36b67 205 }
<> 157:ff67d9f36b67 206
<> 157:ff67d9f36b67 207 while (count--) {
<> 157:ff67d9f36b67 208 *out++ = *in++;
<> 157:ff67d9f36b67 209 }
<> 157:ff67d9f36b67 210
<> 157:ff67d9f36b67 211 return 0;
<> 157:ff67d9f36b67 212 }
<> 157:ff67d9f36b67 213
<> 157:ff67d9f36b67 214 /**@} end of group aes */