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