mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/mxc/i2cs.c@188:3f10722804f9, 2018-07-30 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /** |
Anna Bridge |
186:707f6e361f3e | 2 | * @file |
Anna Bridge |
186:707f6e361f3e | 3 | * @brief This file contains the function implementations for the I2CS |
Anna Bridge |
186:707f6e361f3e | 4 | * (Inter-Integrated Circuit Slave) peripheral module. |
Anna Bridge |
186:707f6e361f3e | 5 | */ |
Anna Bridge |
186:707f6e361f3e | 6 | /* **************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 7 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 8 | * |
Anna Bridge |
186:707f6e361f3e | 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 10 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 11 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 13 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 14 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 15 | * |
Anna Bridge |
186:707f6e361f3e | 16 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 17 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 18 | * |
Anna Bridge |
186:707f6e361f3e | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 22 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 23 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 24 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 25 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 26 | * |
Anna Bridge |
186:707f6e361f3e | 27 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 28 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 29 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 30 | * |
Anna Bridge |
186:707f6e361f3e | 31 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 32 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 33 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 34 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 35 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 36 | * |
Anna Bridge |
186:707f6e361f3e | 37 | * $Date: 2016-09-08 18:05:59 -0500 (Thu, 08 Sep 2016) $ |
Anna Bridge |
186:707f6e361f3e | 38 | * $Revision: 24332 $ |
Anna Bridge |
186:707f6e361f3e | 39 | * |
Anna Bridge |
186:707f6e361f3e | 40 | *************************************************************************** */ |
Anna Bridge |
186:707f6e361f3e | 41 | |
Anna Bridge |
186:707f6e361f3e | 42 | /* **** Includes **** */ |
Anna Bridge |
186:707f6e361f3e | 43 | #include <string.h> |
Anna Bridge |
186:707f6e361f3e | 44 | #include "mxc_assert.h" |
Anna Bridge |
186:707f6e361f3e | 45 | #include "mxc_errors.h" |
Anna Bridge |
186:707f6e361f3e | 46 | #include "mxc_sys.h" |
Anna Bridge |
186:707f6e361f3e | 47 | #include "i2cs.h" |
Anna Bridge |
186:707f6e361f3e | 48 | |
Anna Bridge |
186:707f6e361f3e | 49 | /** |
Anna Bridge |
186:707f6e361f3e | 50 | * @ingroup i2cs |
Anna Bridge |
186:707f6e361f3e | 51 | * @{ |
Anna Bridge |
186:707f6e361f3e | 52 | */ |
Anna Bridge |
186:707f6e361f3e | 53 | /* **** Definitions **** */ |
Anna Bridge |
186:707f6e361f3e | 54 | |
Anna Bridge |
186:707f6e361f3e | 55 | /* **** Globals ***** */ |
Anna Bridge |
186:707f6e361f3e | 56 | |
Anna Bridge |
186:707f6e361f3e | 57 | |
Anna Bridge |
186:707f6e361f3e | 58 | // No Doxygen documentation for the items between here and endcond. |
Anna Bridge |
186:707f6e361f3e | 59 | /* Clock divider lookup table */ |
Anna Bridge |
186:707f6e361f3e | 60 | static const uint32_t clk_div_table[2][8] = { |
Anna Bridge |
186:707f6e361f3e | 61 | /* I2CS_SPEED_100KHZ */ |
Anna Bridge |
186:707f6e361f3e | 62 | { |
Anna Bridge |
186:707f6e361f3e | 63 | // 12000000 |
Anna Bridge |
186:707f6e361f3e | 64 | (6 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 65 | // 24000000 |
Anna Bridge |
186:707f6e361f3e | 66 | (12 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 67 | // 36000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 68 | 0, |
Anna Bridge |
186:707f6e361f3e | 69 | // 48000000 |
Anna Bridge |
186:707f6e361f3e | 70 | (24 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 71 | // 60000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 72 | 0, |
Anna Bridge |
186:707f6e361f3e | 73 | // 72000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 74 | 0, |
Anna Bridge |
186:707f6e361f3e | 75 | // 84000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 76 | 0, |
Anna Bridge |
186:707f6e361f3e | 77 | // 96000000 |
Anna Bridge |
186:707f6e361f3e | 78 | (48 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS) |
Anna Bridge |
186:707f6e361f3e | 79 | }, |
Anna Bridge |
186:707f6e361f3e | 80 | /* I2CS_SPEED_400KHZ */ |
Anna Bridge |
186:707f6e361f3e | 81 | { |
Anna Bridge |
186:707f6e361f3e | 82 | // 12000000 |
Anna Bridge |
186:707f6e361f3e | 83 | (2 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 84 | // 24000000 |
Anna Bridge |
186:707f6e361f3e | 85 | (3 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 86 | // 36000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 87 | 0, |
Anna Bridge |
186:707f6e361f3e | 88 | // 48000000 |
Anna Bridge |
186:707f6e361f3e | 89 | (6 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS), |
Anna Bridge |
186:707f6e361f3e | 90 | // 60000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 91 | 0, |
Anna Bridge |
186:707f6e361f3e | 92 | // 72000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 93 | 0, |
Anna Bridge |
186:707f6e361f3e | 94 | // 84000000 NOT SUPPORTED |
Anna Bridge |
186:707f6e361f3e | 95 | 0, |
Anna Bridge |
186:707f6e361f3e | 96 | // 96000000 |
Anna Bridge |
186:707f6e361f3e | 97 | (12 << MXC_F_I2CS_CLK_DIV_FS_FILTER_CLOCK_DIV_POS) |
Anna Bridge |
186:707f6e361f3e | 98 | }, |
Anna Bridge |
186:707f6e361f3e | 99 | }; |
Anna Bridge |
186:707f6e361f3e | 100 | |
Anna Bridge |
186:707f6e361f3e | 101 | |
Anna Bridge |
186:707f6e361f3e | 102 | static void (*callbacks[MXC_CFG_I2CS_INSTANCES][MXC_CFG_I2CS_BUFFER_SIZE])(uint8_t); |
Anna Bridge |
186:707f6e361f3e | 103 | |
Anna Bridge |
186:707f6e361f3e | 104 | /* **** Functions **** */ |
Anna Bridge |
186:707f6e361f3e | 105 | |
Anna Bridge |
186:707f6e361f3e | 106 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 107 | int I2CS_Init(mxc_i2cs_regs_t *i2cs, const sys_cfg_i2cs_t *sys_cfg, i2cs_speed_t speed, |
Anna Bridge |
186:707f6e361f3e | 108 | uint16_t address, i2cs_addr_t addr_len) |
Anna Bridge |
186:707f6e361f3e | 109 | { |
Anna Bridge |
186:707f6e361f3e | 110 | int err, i, i2cs_index; |
Anna Bridge |
186:707f6e361f3e | 111 | |
Anna Bridge |
186:707f6e361f3e | 112 | i2cs_index = MXC_I2CS_GET_IDX(i2cs); |
Anna Bridge |
186:707f6e361f3e | 113 | MXC_ASSERT(i2cs_index >= 0); |
Anna Bridge |
186:707f6e361f3e | 114 | |
Anna Bridge |
186:707f6e361f3e | 115 | // Set system level configurations |
Anna Bridge |
186:707f6e361f3e | 116 | if ((err = SYS_I2CS_Init(i2cs, sys_cfg)) != E_NO_ERROR) { |
Anna Bridge |
186:707f6e361f3e | 117 | return err; |
Anna Bridge |
186:707f6e361f3e | 118 | } |
Anna Bridge |
186:707f6e361f3e | 119 | |
Anna Bridge |
186:707f6e361f3e | 120 | // Compute clock array index |
Anna Bridge |
186:707f6e361f3e | 121 | int clki = ((SYS_I2CS_GetFreq(i2cs) / 12000000) - 1); |
Anna Bridge |
186:707f6e361f3e | 122 | |
Anna Bridge |
186:707f6e361f3e | 123 | // Get clock divider settings from lookup table |
Anna Bridge |
186:707f6e361f3e | 124 | if ((speed == I2CS_SPEED_100KHZ) && (clk_div_table[I2CS_SPEED_100KHZ][clki] > 0)) { |
Anna Bridge |
186:707f6e361f3e | 125 | i2cs->clk_div = clk_div_table[I2CS_SPEED_100KHZ][clki]; |
Anna Bridge |
186:707f6e361f3e | 126 | } else if ((speed == I2CS_SPEED_400KHZ) && (clk_div_table[I2CS_SPEED_400KHZ][clki] > 0)) { |
Anna Bridge |
186:707f6e361f3e | 127 | i2cs->clk_div = clk_div_table[I2CS_SPEED_400KHZ][clki]; |
Anna Bridge |
186:707f6e361f3e | 128 | } else { |
Anna Bridge |
186:707f6e361f3e | 129 | MXC_ASSERT_FAIL(); |
Anna Bridge |
186:707f6e361f3e | 130 | } |
Anna Bridge |
186:707f6e361f3e | 131 | |
Anna Bridge |
186:707f6e361f3e | 132 | // Clear the interrupt callbacks |
Anna Bridge |
186:707f6e361f3e | 133 | for(i = 0; i < MXC_CFG_I2CS_BUFFER_SIZE; i++) { |
Anna Bridge |
186:707f6e361f3e | 134 | callbacks[i2cs_index][i] = NULL; |
Anna Bridge |
186:707f6e361f3e | 135 | } |
Anna Bridge |
186:707f6e361f3e | 136 | |
Anna Bridge |
186:707f6e361f3e | 137 | // Reset module |
Anna Bridge |
186:707f6e361f3e | 138 | i2cs->dev_id = MXC_F_I2CS_DEV_ID_SLAVE_RESET; |
Anna Bridge |
186:707f6e361f3e | 139 | i2cs->dev_id = ((((address >> 0) << MXC_F_I2CS_DEV_ID_SLAVE_DEV_ID_POS) |
Anna Bridge |
186:707f6e361f3e | 140 | & MXC_F_I2CS_DEV_ID_SLAVE_DEV_ID) | addr_len); |
Anna Bridge |
186:707f6e361f3e | 141 | |
Anna Bridge |
186:707f6e361f3e | 142 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 143 | } |
Anna Bridge |
186:707f6e361f3e | 144 | |
Anna Bridge |
186:707f6e361f3e | 145 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 146 | int I2CS_Shutdown(mxc_i2cs_regs_t *i2cs) |
Anna Bridge |
186:707f6e361f3e | 147 | { |
Anna Bridge |
186:707f6e361f3e | 148 | int err; |
Anna Bridge |
186:707f6e361f3e | 149 | |
Anna Bridge |
186:707f6e361f3e | 150 | // Disable and clear interrupts |
Anna Bridge |
186:707f6e361f3e | 151 | i2cs->inten = 0; |
Anna Bridge |
186:707f6e361f3e | 152 | i2cs->intfl = i2cs->intfl; |
Anna Bridge |
186:707f6e361f3e | 153 | |
Anna Bridge |
186:707f6e361f3e | 154 | // clears system level configurations |
Anna Bridge |
186:707f6e361f3e | 155 | if ((err = SYS_I2CS_Shutdown(i2cs)) != E_NO_ERROR) { |
Anna Bridge |
186:707f6e361f3e | 156 | return err; |
Anna Bridge |
186:707f6e361f3e | 157 | } |
Anna Bridge |
186:707f6e361f3e | 158 | |
Anna Bridge |
186:707f6e361f3e | 159 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 160 | } |
Anna Bridge |
186:707f6e361f3e | 161 | |
Anna Bridge |
186:707f6e361f3e | 162 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 163 | void I2CS_Handler(mxc_i2cs_regs_t *i2cs) |
Anna Bridge |
186:707f6e361f3e | 164 | { |
Anna Bridge |
186:707f6e361f3e | 165 | uint32_t intfl; |
Anna Bridge |
186:707f6e361f3e | 166 | uint8_t i; |
Anna Bridge |
186:707f6e361f3e | 167 | int i2cs_index = MXC_I2CS_GET_IDX(i2cs); |
Anna Bridge |
186:707f6e361f3e | 168 | |
Anna Bridge |
186:707f6e361f3e | 169 | // Save and clear the interrupt flags |
Anna Bridge |
186:707f6e361f3e | 170 | intfl = i2cs->intfl; |
Anna Bridge |
186:707f6e361f3e | 171 | i2cs->intfl = intfl; |
Anna Bridge |
186:707f6e361f3e | 172 | |
Anna Bridge |
186:707f6e361f3e | 173 | // Process each interrupt |
Anna Bridge |
186:707f6e361f3e | 174 | for(i = 0; i < 32; i++) { |
Anna Bridge |
186:707f6e361f3e | 175 | if(intfl & (0x1 << i)) { |
Anna Bridge |
186:707f6e361f3e | 176 | if(callbacks[i2cs_index][i] != NULL) { |
Anna Bridge |
186:707f6e361f3e | 177 | callbacks[i2cs_index][i](i); |
Anna Bridge |
186:707f6e361f3e | 178 | } |
Anna Bridge |
186:707f6e361f3e | 179 | } |
Anna Bridge |
186:707f6e361f3e | 180 | } |
Anna Bridge |
186:707f6e361f3e | 181 | |
Anna Bridge |
186:707f6e361f3e | 182 | } |
Anna Bridge |
186:707f6e361f3e | 183 | |
Anna Bridge |
186:707f6e361f3e | 184 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 185 | void I2CS_RegisterCallback(mxc_i2cs_regs_t *i2cs, uint8_t addr, i2cs_callback_fn callback) |
Anna Bridge |
186:707f6e361f3e | 186 | { |
Anna Bridge |
186:707f6e361f3e | 187 | int i2cs_index = MXC_I2CS_GET_IDX(i2cs); |
Anna Bridge |
186:707f6e361f3e | 188 | |
Anna Bridge |
186:707f6e361f3e | 189 | // Make sure we don't overflow |
Anna Bridge |
186:707f6e361f3e | 190 | MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE); |
Anna Bridge |
186:707f6e361f3e | 191 | |
Anna Bridge |
186:707f6e361f3e | 192 | if(callback != NULL) { |
Anna Bridge |
186:707f6e361f3e | 193 | // Save the callback address |
Anna Bridge |
186:707f6e361f3e | 194 | callbacks[i2cs_index][addr] = callback; |
Anna Bridge |
186:707f6e361f3e | 195 | |
Anna Bridge |
186:707f6e361f3e | 196 | // Clear and Enable the interrupt for the given byte |
Anna Bridge |
186:707f6e361f3e | 197 | i2cs->intfl = (0x1 << addr); |
Anna Bridge |
186:707f6e361f3e | 198 | i2cs->inten |= (0x1 << addr); |
Anna Bridge |
186:707f6e361f3e | 199 | } else { |
Anna Bridge |
186:707f6e361f3e | 200 | // Disable and clear the interrupt |
Anna Bridge |
186:707f6e361f3e | 201 | i2cs->inten &= ~(0x1 << addr); |
Anna Bridge |
186:707f6e361f3e | 202 | i2cs->intfl = (0x1 << addr); |
Anna Bridge |
186:707f6e361f3e | 203 | |
Anna Bridge |
186:707f6e361f3e | 204 | // Clear the callback address |
Anna Bridge |
186:707f6e361f3e | 205 | callbacks[i2cs_index][addr] = NULL; |
Anna Bridge |
186:707f6e361f3e | 206 | } |
Anna Bridge |
186:707f6e361f3e | 207 | } |
Anna Bridge |
186:707f6e361f3e | 208 | |
Anna Bridge |
186:707f6e361f3e | 209 | /**@} end of group i2cs*/ |