mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
ua1arn
Date:
Mon Jul 30 12:31:10 2018 +0000
Revision:
188:3f10722804f9
Parent:
186:707f6e361f3e
before add multi-configuration USB descriptors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 186:707f6e361f3e 1 /**
Anna Bridge 186:707f6e361f3e 2 * @file
Anna Bridge 186:707f6e361f3e 3 * @brief Modular Arithmetic Accelerator (MAA) API Function Implementations.
Anna Bridge 186:707f6e361f3e 4 */
Anna Bridge 186:707f6e361f3e 5 /* *****************************************************************************
Anna Bridge 186:707f6e361f3e 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 186:707f6e361f3e 7 *
Anna Bridge 186:707f6e361f3e 8 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 186:707f6e361f3e 9 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 186:707f6e361f3e 10 * to deal in the Software without restriction, including without limitation
Anna Bridge 186:707f6e361f3e 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 186:707f6e361f3e 12 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 186:707f6e361f3e 13 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 186:707f6e361f3e 14 *
Anna Bridge 186:707f6e361f3e 15 * The above copyright notice and this permission notice shall be included
Anna Bridge 186:707f6e361f3e 16 * in all copies or substantial portions of the Software.
Anna Bridge 186:707f6e361f3e 17 *
Anna Bridge 186:707f6e361f3e 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 186:707f6e361f3e 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 186:707f6e361f3e 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 186:707f6e361f3e 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 186:707f6e361f3e 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 186:707f6e361f3e 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 186:707f6e361f3e 24 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 186:707f6e361f3e 25 *
Anna Bridge 186:707f6e361f3e 26 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 186:707f6e361f3e 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 186:707f6e361f3e 28 * Products, Inc. Branding Policy.
Anna Bridge 186:707f6e361f3e 29 *
Anna Bridge 186:707f6e361f3e 30 * The mere transfer of this software does not imply any licenses
Anna Bridge 186:707f6e361f3e 31 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 186:707f6e361f3e 32 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 186:707f6e361f3e 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 186:707f6e361f3e 34 * ownership rights.
Anna Bridge 186:707f6e361f3e 35 *
Anna Bridge 186:707f6e361f3e 36 **************************************************************************** */
Anna Bridge 186:707f6e361f3e 37
Anna Bridge 186:707f6e361f3e 38 /* **** Includes **** */
Anna Bridge 186:707f6e361f3e 39 #include <string.h>
Anna Bridge 186:707f6e361f3e 40 #include "mxc_assert.h"
Anna Bridge 186:707f6e361f3e 41 #include "mxc_lock.h"
Anna Bridge 186:707f6e361f3e 42 #include "mxc_sys.h"
Anna Bridge 186:707f6e361f3e 43 #include "maa.h"
Anna Bridge 186:707f6e361f3e 44
Anna Bridge 186:707f6e361f3e 45 /**
Anna Bridge 186:707f6e361f3e 46 * @ingroup maa
Anna Bridge 186:707f6e361f3e 47 * @{
Anna Bridge 186:707f6e361f3e 48 */
Anna Bridge 186:707f6e361f3e 49
Anna Bridge 186:707f6e361f3e 50 ///@cond
Anna Bridge 186:707f6e361f3e 51 #define maa_is_running() (MXC_MAA->ctrl & MXC_F_MAA_CTRL_START ? 1 : 0)
Anna Bridge 186:707f6e361f3e 52
Anna Bridge 186:707f6e361f3e 53 /**
Anna Bridge 186:707f6e361f3e 54 * Macro that translates from #mxc_maa_reg_select_t to a pointer for MAA memory operations.
Anna Bridge 186:707f6e361f3e 55 */
Anna Bridge 186:707f6e361f3e 56 #define UMAA_REGFILE_TO_ADDR(base, x) ((base + (MXC_MAA_HALF_SIZE * x)))
Anna Bridge 186:707f6e361f3e 57
Anna Bridge 186:707f6e361f3e 58 /**
Anna Bridge 186:707f6e361f3e 59 * Macro that adjusts the pointer so that it is pointing to the last 32-bit word in the maa (sub-)segment
Anna Bridge 186:707f6e361f3e 60 */
Anna Bridge 186:707f6e361f3e 61 #define UMAA_ADDR_INDEX_LAST_32BIT(x) ((uint8_t *)x + (MXC_MAA_HALF_SIZE-4))
Anna Bridge 186:707f6e361f3e 62 ///@endcond
Anna Bridge 186:707f6e361f3e 63
Anna Bridge 186:707f6e361f3e 64
Anna Bridge 186:707f6e361f3e 65 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 66 mxc_maa_ret_t MAA_Init(void)
Anna Bridge 186:707f6e361f3e 67 {
Anna Bridge 186:707f6e361f3e 68 SYS_MAA_Init();
Anna Bridge 186:707f6e361f3e 69
Anna Bridge 186:707f6e361f3e 70 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 71 }
Anna Bridge 186:707f6e361f3e 72
Anna Bridge 186:707f6e361f3e 73 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 74 mxc_maa_ret_t MAA_WipeRAM(void)
Anna Bridge 186:707f6e361f3e 75 {
Anna Bridge 186:707f6e361f3e 76 /* Check for running MAA */
Anna Bridge 186:707f6e361f3e 77 if (maa_is_running()) {
Anna Bridge 186:707f6e361f3e 78 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 79 }
Anna Bridge 186:707f6e361f3e 80
Anna Bridge 186:707f6e361f3e 81 /* Clear register files */
Anna Bridge 186:707f6e361f3e 82 memset((void *)MXC_MAA_MEM->seg0, 0, sizeof(MXC_MAA_MEM->seg0));
Anna Bridge 186:707f6e361f3e 83 memset((void *)MXC_MAA_MEM->seg1, 0, sizeof(MXC_MAA_MEM->seg1));
Anna Bridge 186:707f6e361f3e 84 memset((void *)MXC_MAA_MEM->seg2, 0, sizeof(MXC_MAA_MEM->seg2));
Anna Bridge 186:707f6e361f3e 85 memset((void *)MXC_MAA_MEM->seg3, 0, sizeof(MXC_MAA_MEM->seg3));
Anna Bridge 186:707f6e361f3e 86 memset((void *)MXC_MAA_MEM->seg4, 0, sizeof(MXC_MAA_MEM->seg4));
Anna Bridge 186:707f6e361f3e 87 memset((void *)MXC_MAA_MEM->seg5, 0, sizeof(MXC_MAA_MEM->seg5));
Anna Bridge 186:707f6e361f3e 88
Anna Bridge 186:707f6e361f3e 89 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 90 }
Anna Bridge 186:707f6e361f3e 91
Anna Bridge 186:707f6e361f3e 92 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 93 mxc_maa_ret_t MAA_Load(mxc_maa_reg_select_t regfile, const uint8_t *data, unsigned int size, mxc_maa_endian_select_t flag)
Anna Bridge 186:707f6e361f3e 94 {
Anna Bridge 186:707f6e361f3e 95 uint32_t *maaptr;
Anna Bridge 186:707f6e361f3e 96 uint32_t fill;
Anna Bridge 186:707f6e361f3e 97 unsigned int zerotmp;
Anna Bridge 186:707f6e361f3e 98
Anna Bridge 186:707f6e361f3e 99 if ((regfile > MXC_E_REG_51) || (size > MXC_MAA_REG_SIZE)) {
Anna Bridge 186:707f6e361f3e 100 /* Out of range */
Anna Bridge 186:707f6e361f3e 101 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 102 }
Anna Bridge 186:707f6e361f3e 103
Anna Bridge 186:707f6e361f3e 104 if (flag == MXC_MAA_F_MEM_REVERSE) {
Anna Bridge 186:707f6e361f3e 105 /* This is not currently implemented */
Anna Bridge 186:707f6e361f3e 106 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 107 }
Anna Bridge 186:707f6e361f3e 108
Anna Bridge 186:707f6e361f3e 109 maaptr = (uint32_t *)UMAA_REGFILE_TO_ADDR(MXC_BASE_MAA_MEM, regfile);
Anna Bridge 186:707f6e361f3e 110
Anna Bridge 186:707f6e361f3e 111 /*
Anna Bridge 186:707f6e361f3e 112 * MAA (sub-)segments must be loaded with zero pad to a 64-bit boundary, or the "garbage bits"
Anna Bridge 186:707f6e361f3e 113 * will case erroneous results.
Anna Bridge 186:707f6e361f3e 114 */
Anna Bridge 186:707f6e361f3e 115 /* Find the ceiling for the closest 64-bit boundary based on the selected MAWS */
Anna Bridge 186:707f6e361f3e 116 zerotmp = (((MXC_MAA->maws & MXC_F_MAA_MAWS_MODLEN) >> MXC_F_MAA_MAWS_MODLEN_POS) + 63) & 0xfc0;
Anna Bridge 186:707f6e361f3e 117 /* Convert to bytes */
Anna Bridge 186:707f6e361f3e 118 zerotmp /= 8;
Anna Bridge 186:707f6e361f3e 119
Anna Bridge 186:707f6e361f3e 120 /* Fill uMAA memory in long word sized chunks */
Anna Bridge 186:707f6e361f3e 121 while (size > 3) {
Anna Bridge 186:707f6e361f3e 122 *maaptr++ = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
Anna Bridge 186:707f6e361f3e 123 data += 4;
Anna Bridge 186:707f6e361f3e 124 size -= 4;
Anna Bridge 186:707f6e361f3e 125 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
Anna Bridge 186:707f6e361f3e 126 }
Anna Bridge 186:707f6e361f3e 127
Anna Bridge 186:707f6e361f3e 128 /* Remainder */
Anna Bridge 186:707f6e361f3e 129 if (size) {
Anna Bridge 186:707f6e361f3e 130 fill = data[0];
Anna Bridge 186:707f6e361f3e 131 fill |= ((size > 1) ? (data[1] << 8) : 0);
Anna Bridge 186:707f6e361f3e 132 fill |= ((size > 2) ? (data[2] << 16) : 0);
Anna Bridge 186:707f6e361f3e 133 *maaptr++ = fill;
Anna Bridge 186:707f6e361f3e 134
Anna Bridge 186:707f6e361f3e 135 /* We just filled 4 bytes in this section */
Anna Bridge 186:707f6e361f3e 136 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
Anna Bridge 186:707f6e361f3e 137 }
Anna Bridge 186:707f6e361f3e 138
Anna Bridge 186:707f6e361f3e 139 /* Wipe the remaining "garbage bits" */
Anna Bridge 186:707f6e361f3e 140 while (zerotmp) {
Anna Bridge 186:707f6e361f3e 141 *maaptr++ = 0;
Anna Bridge 186:707f6e361f3e 142 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
Anna Bridge 186:707f6e361f3e 143 }
Anna Bridge 186:707f6e361f3e 144
Anna Bridge 186:707f6e361f3e 145 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 146 }
Anna Bridge 186:707f6e361f3e 147
Anna Bridge 186:707f6e361f3e 148 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 149 mxc_maa_ret_t MAA_Unload(mxc_maa_reg_select_t regfile, uint8_t *data, unsigned int size, mxc_maa_endian_select_t flag)
Anna Bridge 186:707f6e361f3e 150 {
Anna Bridge 186:707f6e361f3e 151 uint32_t *maaptr;
Anna Bridge 186:707f6e361f3e 152 uint32_t fill;
Anna Bridge 186:707f6e361f3e 153
Anna Bridge 186:707f6e361f3e 154 if ((regfile > MXC_E_REG_51) || (size > MXC_MAA_REG_SIZE)) {
Anna Bridge 186:707f6e361f3e 155 /* Out of range */
Anna Bridge 186:707f6e361f3e 156 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 157 }
Anna Bridge 186:707f6e361f3e 158
Anna Bridge 186:707f6e361f3e 159 if (flag == MXC_MAA_F_MEM_REVERSE) {
Anna Bridge 186:707f6e361f3e 160 /* This is not currently implemented */
Anna Bridge 186:707f6e361f3e 161 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 162 }
Anna Bridge 186:707f6e361f3e 163
Anna Bridge 186:707f6e361f3e 164 maaptr = (uint32_t *)UMAA_REGFILE_TO_ADDR(MXC_BASE_MAA_MEM, regfile);
Anna Bridge 186:707f6e361f3e 165
Anna Bridge 186:707f6e361f3e 166 /* Unload uMAA memory in long word sized chunks */
Anna Bridge 186:707f6e361f3e 167 while (size > 3) {
Anna Bridge 186:707f6e361f3e 168 fill = *maaptr++;
Anna Bridge 186:707f6e361f3e 169 data[0] = fill & 0xff;
Anna Bridge 186:707f6e361f3e 170 data[1] = (fill >> 8) & 0xff;
Anna Bridge 186:707f6e361f3e 171 data[2] = (fill >> 16) & 0xff;
Anna Bridge 186:707f6e361f3e 172 data[3] = (fill >> 24) & 0xff;
Anna Bridge 186:707f6e361f3e 173 data += 4;
Anna Bridge 186:707f6e361f3e 174 size -= 4;
Anna Bridge 186:707f6e361f3e 175 }
Anna Bridge 186:707f6e361f3e 176
Anna Bridge 186:707f6e361f3e 177 /* Remainder */
Anna Bridge 186:707f6e361f3e 178 if (size) {
Anna Bridge 186:707f6e361f3e 179 fill = *maaptr;
Anna Bridge 186:707f6e361f3e 180 data[0] = fill & 0xff;
Anna Bridge 186:707f6e361f3e 181 if (size > 1) {
Anna Bridge 186:707f6e361f3e 182 data[1] = (fill >> 8) & 0xff;
Anna Bridge 186:707f6e361f3e 183 }
Anna Bridge 186:707f6e361f3e 184 if (size > 2) {
Anna Bridge 186:707f6e361f3e 185 data[2] = (fill >> 16) & 0xff;
Anna Bridge 186:707f6e361f3e 186 }
Anna Bridge 186:707f6e361f3e 187 }
Anna Bridge 186:707f6e361f3e 188
Anna Bridge 186:707f6e361f3e 189 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 190 }
Anna Bridge 186:707f6e361f3e 191
Anna Bridge 186:707f6e361f3e 192 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 193 mxc_maa_ret_t MAA_Run(mxc_maa_operation_t op,
Anna Bridge 186:707f6e361f3e 194 mxc_maa_reg_select_t al, mxc_maa_reg_select_t bl,
Anna Bridge 186:707f6e361f3e 195 mxc_maa_reg_select_t rl, mxc_maa_reg_select_t tl)
Anna Bridge 186:707f6e361f3e 196 {
Anna Bridge 186:707f6e361f3e 197 if (maa_is_running()) {
Anna Bridge 186:707f6e361f3e 198 /* Attempt to start the MAA while already running */
Anna Bridge 186:707f6e361f3e 199 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 200 }
Anna Bridge 186:707f6e361f3e 201
Anna Bridge 186:707f6e361f3e 202 /* Clear out any previous flags */
Anna Bridge 186:707f6e361f3e 203 MXC_MAA->ctrl = 0x00000020;
Anna Bridge 186:707f6e361f3e 204
Anna Bridge 186:707f6e361f3e 205 /* Construct memory segment selections, select operation, and start the uMAA */
Anna Bridge 186:707f6e361f3e 206 MXC_MAA->ctrl = (((al << MXC_F_MAA_CTRL_SEG_A_POS) & MXC_F_MAA_CTRL_SEG_A) |
Anna Bridge 186:707f6e361f3e 207 ((bl << MXC_F_MAA_CTRL_SEG_B_POS) & MXC_F_MAA_CTRL_SEG_B) |
Anna Bridge 186:707f6e361f3e 208 ((rl << MXC_F_MAA_CTRL_SEG_RES_POS) & MXC_F_MAA_CTRL_SEG_RES) |
Anna Bridge 186:707f6e361f3e 209 ((tl << MXC_F_MAA_CTRL_SEG_TMP_POS) & MXC_F_MAA_CTRL_SEG_TMP) |
Anna Bridge 186:707f6e361f3e 210 ((op << MXC_F_MAA_CTRL_OPSEL_POS) & MXC_F_MAA_CTRL_OPSEL) |
Anna Bridge 186:707f6e361f3e 211 MXC_F_MAA_CTRL_START);
Anna Bridge 186:707f6e361f3e 212
Anna Bridge 186:707f6e361f3e 213 /* Blocking wait for uMAA to complete. */
Anna Bridge 186:707f6e361f3e 214 while ((MXC_MAA->ctrl & MXC_F_MAA_CTRL_IF_DONE) == 0);
Anna Bridge 186:707f6e361f3e 215
Anna Bridge 186:707f6e361f3e 216 if (MXC_MAA->ctrl & MXC_F_MAA_CTRL_IF_ERROR) {
Anna Bridge 186:707f6e361f3e 217 /* MAA signaled error */
Anna Bridge 186:707f6e361f3e 218 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 219 }
Anna Bridge 186:707f6e361f3e 220
Anna Bridge 186:707f6e361f3e 221 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 222 }
Anna Bridge 186:707f6e361f3e 223
Anna Bridge 186:707f6e361f3e 224 /* ************************************************************************* */
Anna Bridge 186:707f6e361f3e 225 mxc_maa_ret_t MAA_SetWordSize(unsigned int len)
Anna Bridge 186:707f6e361f3e 226 {
Anna Bridge 186:707f6e361f3e 227 if ((len > MXC_MAA_REG_SIZE_BITS) || maa_is_running()) {
Anna Bridge 186:707f6e361f3e 228 return MXC_E_MAA_ERR;
Anna Bridge 186:707f6e361f3e 229 }
Anna Bridge 186:707f6e361f3e 230
Anna Bridge 186:707f6e361f3e 231 /* Set bit length for calculation, and disable endian swap */
Anna Bridge 186:707f6e361f3e 232 MXC_MAA->maws = ((len << MXC_F_MAA_MAWS_MODLEN_POS) & MXC_F_MAA_MAWS_MODLEN);
Anna Bridge 186:707f6e361f3e 233
Anna Bridge 186:707f6e361f3e 234 return MXC_E_MAA_OK;
Anna Bridge 186:707f6e361f3e 235 }
Anna Bridge 186:707f6e361f3e 236 /**@} end of ingroup maa */