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.h@186:707f6e361f3e, 2018-06-22 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Jun 22 16:45:37 2018 +0100
- Revision:
- 186:707f6e361f3e
mbed-dev library. Release version 162
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 I2CS (Inter-Integrated Circuit Slave) function prototypes and data types. |
Anna Bridge |
186:707f6e361f3e | 4 | */ |
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: 2017-02-16 12:07:30 -0600 (Thu, 16 Feb 2017) $ |
Anna Bridge |
186:707f6e361f3e | 38 | * $Revision: 26468 $ |
Anna Bridge |
186:707f6e361f3e | 39 | * |
Anna Bridge |
186:707f6e361f3e | 40 | *************************************************************************** */ |
Anna Bridge |
186:707f6e361f3e | 41 | |
Anna Bridge |
186:707f6e361f3e | 42 | /* Define to prevent redundant inclusion */ |
Anna Bridge |
186:707f6e361f3e | 43 | #ifndef _I2CS_H_ |
Anna Bridge |
186:707f6e361f3e | 44 | #define _I2CS_H_ |
Anna Bridge |
186:707f6e361f3e | 45 | |
Anna Bridge |
186:707f6e361f3e | 46 | /* **** Includes **** */ |
Anna Bridge |
186:707f6e361f3e | 47 | #include "mxc_config.h" |
Anna Bridge |
186:707f6e361f3e | 48 | #include "mxc_sys.h" |
Anna Bridge |
186:707f6e361f3e | 49 | #include "mxc_assert.h" |
Anna Bridge |
186:707f6e361f3e | 50 | #include "i2cs_regs.h" |
Anna Bridge |
186:707f6e361f3e | 51 | |
Anna Bridge |
186:707f6e361f3e | 52 | #ifdef __cplusplus |
Anna Bridge |
186:707f6e361f3e | 53 | extern "C" { |
Anna Bridge |
186:707f6e361f3e | 54 | #endif |
Anna Bridge |
186:707f6e361f3e | 55 | |
Anna Bridge |
186:707f6e361f3e | 56 | /** |
Anna Bridge |
186:707f6e361f3e | 57 | * @ingroup periphlibs |
Anna Bridge |
186:707f6e361f3e | 58 | * @defgroup i2cs I2C Slave |
Anna Bridge |
186:707f6e361f3e | 59 | * @brief I2C Slave Peripheral API |
Anna Bridge |
186:707f6e361f3e | 60 | * @{ |
Anna Bridge |
186:707f6e361f3e | 61 | */ |
Anna Bridge |
186:707f6e361f3e | 62 | |
Anna Bridge |
186:707f6e361f3e | 63 | /* **** Definitions **** */ |
Anna Bridge |
186:707f6e361f3e | 64 | /** |
Anna Bridge |
186:707f6e361f3e | 65 | * Internal buffer size for storing I2C Slave Messages |
Anna Bridge |
186:707f6e361f3e | 66 | */ |
Anna Bridge |
186:707f6e361f3e | 67 | #define I2CS_BUFFER_SIZE 32 |
Anna Bridge |
186:707f6e361f3e | 68 | |
Anna Bridge |
186:707f6e361f3e | 69 | /** |
Anna Bridge |
186:707f6e361f3e | 70 | * Enumeration type to select supported I2CS frequencies. |
Anna Bridge |
186:707f6e361f3e | 71 | */ |
Anna Bridge |
186:707f6e361f3e | 72 | typedef enum { |
Anna Bridge |
186:707f6e361f3e | 73 | I2CS_SPEED_100KHZ = 0, /**< Use to select a bus communication speed of 100 kHz. */ |
Anna Bridge |
186:707f6e361f3e | 74 | I2CS_SPEED_400KHZ = 1 /**< Use to select a bus communication speed of 400 kHz. */ |
Anna Bridge |
186:707f6e361f3e | 75 | } i2cs_speed_t; |
Anna Bridge |
186:707f6e361f3e | 76 | |
Anna Bridge |
186:707f6e361f3e | 77 | /** |
Anna Bridge |
186:707f6e361f3e | 78 | * Enumeration type to select the I2CS addressing mode. |
Anna Bridge |
186:707f6e361f3e | 79 | */ |
Anna Bridge |
186:707f6e361f3e | 80 | typedef enum { |
Anna Bridge |
186:707f6e361f3e | 81 | I2CS_ADDR_8 = 0, /**< Sets the slave address mode to 8-bits (7-bits address plus read/write bit). */ |
Anna Bridge |
186:707f6e361f3e | 82 | I2CS_ADDR_10 = MXC_F_I2CS_DEV_ID_TEN_BIT_ID_MODE /**< Sets the slave address mode to 10-bits. */ |
Anna Bridge |
186:707f6e361f3e | 83 | } i2cs_addr_t; |
Anna Bridge |
186:707f6e361f3e | 84 | |
Anna Bridge |
186:707f6e361f3e | 85 | /** |
Anna Bridge |
186:707f6e361f3e | 86 | * Type alias for an I2CS callback function that will be called when a given byte is updated by the Master, see I2CS_RegisterCallback(mxc_i2cs_regs_t *i2cs, uint8_t addr, i2cs_callback_fn callback). |
Anna Bridge |
186:707f6e361f3e | 87 | * @details The function prototype for implementing callback_fn is: |
Anna Bridge |
186:707f6e361f3e | 88 | * @code |
Anna Bridge |
186:707f6e361f3e | 89 | * void func(uint8_t addr); |
Anna Bridge |
186:707f6e361f3e | 90 | * @endcode |
Anna Bridge |
186:707f6e361f3e | 91 | */ |
Anna Bridge |
186:707f6e361f3e | 92 | typedef void (*i2cs_callback_fn)(uint8_t error_code); |
Anna Bridge |
186:707f6e361f3e | 93 | /* **** Globals **** */ |
Anna Bridge |
186:707f6e361f3e | 94 | |
Anna Bridge |
186:707f6e361f3e | 95 | /* **** Function Prototypes **** */ |
Anna Bridge |
186:707f6e361f3e | 96 | |
Anna Bridge |
186:707f6e361f3e | 97 | /** |
Anna Bridge |
186:707f6e361f3e | 98 | * @brief Initialize I2CS module. |
Anna Bridge |
186:707f6e361f3e | 99 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 100 | * @param sys_cfg Pointer to I2CS system configuration, see |
Anna Bridge |
186:707f6e361f3e | 101 | * #sys_cfg_i2cs_t. |
Anna Bridge |
186:707f6e361f3e | 102 | * @param speed I2CS frequency. |
Anna Bridge |
186:707f6e361f3e | 103 | * @param address I2CS address. |
Anna Bridge |
186:707f6e361f3e | 104 | * @param addr_len I2CS address length. |
Anna Bridge |
186:707f6e361f3e | 105 | * @return #E_NO_ERROR if everything is successful or an |
Anna Bridge |
186:707f6e361f3e | 106 | * @ref MXC_Error_Codes "error code" if unsuccessful. |
Anna Bridge |
186:707f6e361f3e | 107 | * |
Anna Bridge |
186:707f6e361f3e | 108 | */ |
Anna Bridge |
186:707f6e361f3e | 109 | int I2CS_Init(mxc_i2cs_regs_t *i2cs, const sys_cfg_i2cs_t *sys_cfg, i2cs_speed_t speed, uint16_t address, i2cs_addr_t addr_len); |
Anna Bridge |
186:707f6e361f3e | 110 | |
Anna Bridge |
186:707f6e361f3e | 111 | /** |
Anna Bridge |
186:707f6e361f3e | 112 | * @brief Shutdown I2CS module. |
Anna Bridge |
186:707f6e361f3e | 113 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 114 | * @return #E_NO_ERROR if everything is successful or an |
Anna Bridge |
186:707f6e361f3e | 115 | * @ref MXC_Error_Codes "error code" if unsuccessful. |
Anna Bridge |
186:707f6e361f3e | 116 | */ |
Anna Bridge |
186:707f6e361f3e | 117 | int I2CS_Shutdown(mxc_i2cs_regs_t *i2cs); |
Anna Bridge |
186:707f6e361f3e | 118 | |
Anna Bridge |
186:707f6e361f3e | 119 | /** |
Anna Bridge |
186:707f6e361f3e | 120 | * @brief I2CS interrupt handler. |
Anna Bridge |
186:707f6e361f3e | 121 | * @details This function should be called by the application from the |
Anna Bridge |
186:707f6e361f3e | 122 | * interrupt handler if I2CS interrupts are enabled. Alternately, |
Anna Bridge |
186:707f6e361f3e | 123 | * this function can be periodically called by the application if |
Anna Bridge |
186:707f6e361f3e | 124 | * I2CS interrupts are disabled. |
Anna Bridge |
186:707f6e361f3e | 125 | * |
Anna Bridge |
186:707f6e361f3e | 126 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 127 | */ |
Anna Bridge |
186:707f6e361f3e | 128 | void I2CS_Handler(mxc_i2cs_regs_t *i2cs); |
Anna Bridge |
186:707f6e361f3e | 129 | |
Anna Bridge |
186:707f6e361f3e | 130 | /** |
Anna Bridge |
186:707f6e361f3e | 131 | * @brief Register a callback that is triggered by an update of a specified |
Anna Bridge |
186:707f6e361f3e | 132 | * byte. |
Anna Bridge |
186:707f6e361f3e | 133 | * @details Registering a callback causes the slave to interrupt when the |
Anna Bridge |
186:707f6e361f3e | 134 | * master has updated a specified byte. |
Anna Bridge |
186:707f6e361f3e | 135 | * |
Anna Bridge |
186:707f6e361f3e | 136 | * @param i2cs Pointer to the I2CS register structure, see |
Anna Bridge |
186:707f6e361f3e | 137 | * #mxc_i2cs_regs_t. |
Anna Bridge |
186:707f6e361f3e | 138 | * @param addr Index to trigger a call to the #i2cs_callback_fn. |
Anna Bridge |
186:707f6e361f3e | 139 | * @param callback callback function of type #i2cs_callback_fn to be called |
Anna Bridge |
186:707f6e361f3e | 140 | * when the addr being written by the master matches \c addr. |
Anna Bridge |
186:707f6e361f3e | 141 | */ |
Anna Bridge |
186:707f6e361f3e | 142 | void I2CS_RegisterCallback(mxc_i2cs_regs_t *i2cs, uint8_t addr, i2cs_callback_fn callback); |
Anna Bridge |
186:707f6e361f3e | 143 | |
Anna Bridge |
186:707f6e361f3e | 144 | /** |
Anna Bridge |
186:707f6e361f3e | 145 | * @brief Write I2CS data to a given byte. |
Anna Bridge |
186:707f6e361f3e | 146 | * @details The slave has a buffer of registers that the external master can |
Anna Bridge |
186:707f6e361f3e | 147 | * read. Use this function to write data into a specified |
Anna Bridge |
186:707f6e361f3e | 148 | * address/index. |
Anna Bridge |
186:707f6e361f3e | 149 | * |
Anna Bridge |
186:707f6e361f3e | 150 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 151 | * @param addr Address/Index to write. |
Anna Bridge |
186:707f6e361f3e | 152 | * @param data Data to be written. |
Anna Bridge |
186:707f6e361f3e | 153 | */ |
Anna Bridge |
186:707f6e361f3e | 154 | __STATIC_INLINE void I2CS_Write(mxc_i2cs_regs_t *i2cs, uint8_t addr, uint8_t data) |
Anna Bridge |
186:707f6e361f3e | 155 | { |
Anna Bridge |
186:707f6e361f3e | 156 | // Make sure we don't overflow |
Anna Bridge |
186:707f6e361f3e | 157 | MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE); |
Anna Bridge |
186:707f6e361f3e | 158 | i2cs->data_byte[addr] = ((i2cs->data_byte[addr] & ~MXC_F_I2CS_DATA_BYTE_DATA_FIELD) | |
Anna Bridge |
186:707f6e361f3e | 159 | (data << MXC_F_I2CS_DATA_BYTE_DATA_FIELD_POS)); |
Anna Bridge |
186:707f6e361f3e | 160 | } |
Anna Bridge |
186:707f6e361f3e | 161 | |
Anna Bridge |
186:707f6e361f3e | 162 | /** |
Anna Bridge |
186:707f6e361f3e | 163 | * @brief Read I2CS data from a given address . |
Anna Bridge |
186:707f6e361f3e | 164 | * @details The slave has a buffer of registers that the external master can |
Anna Bridge |
186:707f6e361f3e | 165 | * read. Use this function to read the data from the registers. |
Anna Bridge |
186:707f6e361f3e | 166 | * |
Anna Bridge |
186:707f6e361f3e | 167 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 168 | * @param addr Address/Index to read from. |
Anna Bridge |
186:707f6e361f3e | 169 | * |
Anna Bridge |
186:707f6e361f3e | 170 | * @return Data contained in requested @c addr register. |
Anna Bridge |
186:707f6e361f3e | 171 | */ |
Anna Bridge |
186:707f6e361f3e | 172 | __STATIC_INLINE uint8_t I2CS_Read(mxc_i2cs_regs_t *i2cs, uint8_t addr) |
Anna Bridge |
186:707f6e361f3e | 173 | { |
Anna Bridge |
186:707f6e361f3e | 174 | // Make sure we don't overflow |
Anna Bridge |
186:707f6e361f3e | 175 | MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE); |
Anna Bridge |
186:707f6e361f3e | 176 | return ((i2cs->data_byte[addr] & MXC_F_I2CS_DATA_BYTE_DATA_FIELD) >> |
Anna Bridge |
186:707f6e361f3e | 177 | MXC_F_I2CS_DATA_BYTE_DATA_FIELD_POS); |
Anna Bridge |
186:707f6e361f3e | 178 | } |
Anna Bridge |
186:707f6e361f3e | 179 | |
Anna Bridge |
186:707f6e361f3e | 180 | /** |
Anna Bridge |
186:707f6e361f3e | 181 | * @brief Set the given index to read only (RO). |
Anna Bridge |
186:707f6e361f3e | 182 | * @details This index will be flagged as read only. The slave will NACK the |
Anna Bridge |
186:707f6e361f3e | 183 | * master if it attempts to write this location. Multiple calls with |
Anna Bridge |
186:707f6e361f3e | 184 | * different index/address values will yield multiple read-only |
Anna Bridge |
186:707f6e361f3e | 185 | * locations within the slave register set. |
Anna Bridge |
186:707f6e361f3e | 186 | * |
Anna Bridge |
186:707f6e361f3e | 187 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 188 | * @param addr Address/Index of the byte to set to RO. |
Anna Bridge |
186:707f6e361f3e | 189 | */ |
Anna Bridge |
186:707f6e361f3e | 190 | __STATIC_INLINE void I2CS_SetRO(mxc_i2cs_regs_t *i2cs, uint8_t addr) |
Anna Bridge |
186:707f6e361f3e | 191 | { |
Anna Bridge |
186:707f6e361f3e | 192 | // Make sure we don't overflow |
Anna Bridge |
186:707f6e361f3e | 193 | MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE); |
Anna Bridge |
186:707f6e361f3e | 194 | i2cs->data_byte[addr] |= MXC_F_I2CS_DATA_BYTE_READ_ONLY_FL; |
Anna Bridge |
186:707f6e361f3e | 195 | } |
Anna Bridge |
186:707f6e361f3e | 196 | |
Anna Bridge |
186:707f6e361f3e | 197 | /** |
Anna Bridge |
186:707f6e361f3e | 198 | * @brief Sets the given address to R/W. |
Anna Bridge |
186:707f6e361f3e | 199 | * @param i2cs Pointer to I2CS regs. |
Anna Bridge |
186:707f6e361f3e | 200 | * @param addr Index to start clearing RO flag. |
Anna Bridge |
186:707f6e361f3e | 201 | */ |
Anna Bridge |
186:707f6e361f3e | 202 | __STATIC_INLINE void I2CS_ClearRO(mxc_i2cs_regs_t *i2cs, uint8_t addr) |
Anna Bridge |
186:707f6e361f3e | 203 | { |
Anna Bridge |
186:707f6e361f3e | 204 | // Make sure we don't overflow |
Anna Bridge |
186:707f6e361f3e | 205 | MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE); |
Anna Bridge |
186:707f6e361f3e | 206 | i2cs->data_byte[addr] &= ~MXC_F_I2CS_DATA_BYTE_READ_ONLY_FL; |
Anna Bridge |
186:707f6e361f3e | 207 | } |
Anna Bridge |
186:707f6e361f3e | 208 | |
Anna Bridge |
186:707f6e361f3e | 209 | /**@} end of group i2cs */ |
Anna Bridge |
186:707f6e361f3e | 210 | |
Anna Bridge |
186:707f6e361f3e | 211 | #ifdef __cplusplus |
Anna Bridge |
186:707f6e361f3e | 212 | } |
Anna Bridge |
186:707f6e361f3e | 213 | #endif |
Anna Bridge |
186:707f6e361f3e | 214 | |
Anna Bridge |
186:707f6e361f3e | 215 | #endif /* _I2CS_H_ */ |