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