The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:84c0a372a020 1 /**
AnnaBridge 167:84c0a372a020 2 * @file
AnnaBridge 167:84c0a372a020 3 * @brief I2CS (Inter-Integrated Circuit Slave) function prototypes and data types.
AnnaBridge 167:84c0a372a020 4 */
AnnaBridge 167:84c0a372a020 5
AnnaBridge 167:84c0a372a020 6 /* ****************************************************************************
AnnaBridge 167:84c0a372a020 7 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
AnnaBridge 167:84c0a372a020 8 *
AnnaBridge 167:84c0a372a020 9 * Permission is hereby granted, free of charge, to any person obtaining a
AnnaBridge 167:84c0a372a020 10 * copy of this software and associated documentation files (the "Software"),
AnnaBridge 167:84c0a372a020 11 * to deal in the Software without restriction, including without limitation
AnnaBridge 167:84c0a372a020 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
AnnaBridge 167:84c0a372a020 13 * and/or sell copies of the Software, and to permit persons to whom the
AnnaBridge 167:84c0a372a020 14 * Software is furnished to do so, subject to the following conditions:
AnnaBridge 167:84c0a372a020 15 *
AnnaBridge 167:84c0a372a020 16 * The above copyright notice and this permission notice shall be included
AnnaBridge 167:84c0a372a020 17 * in all copies or substantial portions of the Software.
AnnaBridge 167:84c0a372a020 18 *
AnnaBridge 167:84c0a372a020 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
AnnaBridge 167:84c0a372a020 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
AnnaBridge 167:84c0a372a020 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
AnnaBridge 167:84c0a372a020 22 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
AnnaBridge 167:84c0a372a020 23 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
AnnaBridge 167:84c0a372a020 24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
AnnaBridge 167:84c0a372a020 25 * OTHER DEALINGS IN THE SOFTWARE.
AnnaBridge 167:84c0a372a020 26 *
AnnaBridge 167:84c0a372a020 27 * Except as contained in this notice, the name of Maxim Integrated
AnnaBridge 167:84c0a372a020 28 * Products, Inc. shall not be used except as stated in the Maxim Integrated
AnnaBridge 167:84c0a372a020 29 * Products, Inc. Branding Policy.
AnnaBridge 167:84c0a372a020 30 *
AnnaBridge 167:84c0a372a020 31 * The mere transfer of this software does not imply any licenses
AnnaBridge 167:84c0a372a020 32 * of trade secrets, proprietary technology, copyrights, patents,
AnnaBridge 167:84c0a372a020 33 * trademarks, maskwork rights, or any other form of intellectual
AnnaBridge 167:84c0a372a020 34 * property whatsoever. Maxim Integrated Products, Inc. retains all
AnnaBridge 167:84c0a372a020 35 * ownership rights.
AnnaBridge 167:84c0a372a020 36 *
AnnaBridge 167:84c0a372a020 37 * $Date: 2017-02-16 12:07:30 -0600 (Thu, 16 Feb 2017) $
AnnaBridge 167:84c0a372a020 38 * $Revision: 26468 $
AnnaBridge 167:84c0a372a020 39 *
AnnaBridge 167:84c0a372a020 40 *************************************************************************** */
AnnaBridge 167:84c0a372a020 41
AnnaBridge 167:84c0a372a020 42 /* Define to prevent redundant inclusion */
AnnaBridge 167:84c0a372a020 43 #ifndef _I2CS_H_
AnnaBridge 167:84c0a372a020 44 #define _I2CS_H_
AnnaBridge 167:84c0a372a020 45
AnnaBridge 167:84c0a372a020 46 /* **** Includes **** */
AnnaBridge 167:84c0a372a020 47 #include "mxc_config.h"
AnnaBridge 167:84c0a372a020 48 #include "mxc_sys.h"
AnnaBridge 167:84c0a372a020 49 #include "mxc_assert.h"
AnnaBridge 167:84c0a372a020 50 #include "i2cs_regs.h"
AnnaBridge 167:84c0a372a020 51
AnnaBridge 167:84c0a372a020 52 #ifdef __cplusplus
AnnaBridge 167:84c0a372a020 53 extern "C" {
AnnaBridge 167:84c0a372a020 54 #endif
AnnaBridge 167:84c0a372a020 55
AnnaBridge 167:84c0a372a020 56 /**
AnnaBridge 167:84c0a372a020 57 * @ingroup periphlibs
AnnaBridge 167:84c0a372a020 58 * @defgroup i2cs I2C Slave
AnnaBridge 167:84c0a372a020 59 * @brief I2C Slave Peripheral API
AnnaBridge 167:84c0a372a020 60 * @{
AnnaBridge 167:84c0a372a020 61 */
AnnaBridge 167:84c0a372a020 62
AnnaBridge 167:84c0a372a020 63 /* **** Definitions **** */
AnnaBridge 167:84c0a372a020 64 /**
AnnaBridge 167:84c0a372a020 65 * Internal buffer size for storing I2C Slave Messages
AnnaBridge 167:84c0a372a020 66 */
AnnaBridge 167:84c0a372a020 67 #define I2CS_BUFFER_SIZE 32
AnnaBridge 167:84c0a372a020 68
AnnaBridge 167:84c0a372a020 69 /**
AnnaBridge 167:84c0a372a020 70 * Enumeration type to select supported I2CS frequencies.
AnnaBridge 167:84c0a372a020 71 */
AnnaBridge 167:84c0a372a020 72 typedef enum {
AnnaBridge 167:84c0a372a020 73 I2CS_SPEED_100KHZ = 0, /**< Use to select a bus communication speed of 100 kHz. */
AnnaBridge 167:84c0a372a020 74 I2CS_SPEED_400KHZ = 1 /**< Use to select a bus communication speed of 400 kHz. */
AnnaBridge 167:84c0a372a020 75 } i2cs_speed_t;
AnnaBridge 167:84c0a372a020 76
AnnaBridge 167:84c0a372a020 77 /**
AnnaBridge 167:84c0a372a020 78 * Enumeration type to select the I2CS addressing mode.
AnnaBridge 167:84c0a372a020 79 */
AnnaBridge 167:84c0a372a020 80 typedef enum {
AnnaBridge 167:84c0a372a020 81 I2CS_ADDR_8 = 0, /**< Sets the slave address mode to 8-bits (7-bits address plus read/write bit). */
AnnaBridge 167:84c0a372a020 82 I2CS_ADDR_10 = MXC_F_I2CS_DEV_ID_TEN_BIT_ID_MODE /**< Sets the slave address mode to 10-bits. */
AnnaBridge 167:84c0a372a020 83 } i2cs_addr_t;
AnnaBridge 167:84c0a372a020 84
AnnaBridge 167:84c0a372a020 85 /**
AnnaBridge 167:84c0a372a020 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).
AnnaBridge 167:84c0a372a020 87 * @details The function prototype for implementing callback_fn is:
AnnaBridge 167:84c0a372a020 88 * @code
AnnaBridge 167:84c0a372a020 89 * void func(uint8_t addr);
AnnaBridge 167:84c0a372a020 90 * @endcode
AnnaBridge 167:84c0a372a020 91 */
AnnaBridge 167:84c0a372a020 92 typedef void (*i2cs_callback_fn)(uint8_t error_code);
AnnaBridge 167:84c0a372a020 93 /* **** Globals **** */
AnnaBridge 167:84c0a372a020 94
AnnaBridge 167:84c0a372a020 95 /* **** Function Prototypes **** */
AnnaBridge 167:84c0a372a020 96
AnnaBridge 167:84c0a372a020 97 /**
AnnaBridge 167:84c0a372a020 98 * @brief Initialize I2CS module.
AnnaBridge 167:84c0a372a020 99 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 100 * @param sys_cfg Pointer to I2CS system configuration, see
AnnaBridge 167:84c0a372a020 101 * #sys_cfg_i2cs_t.
AnnaBridge 167:84c0a372a020 102 * @param speed I2CS frequency.
AnnaBridge 167:84c0a372a020 103 * @param address I2CS address.
AnnaBridge 167:84c0a372a020 104 * @param addr_len I2CS address length.
AnnaBridge 167:84c0a372a020 105 * @return #E_NO_ERROR if everything is successful or an
AnnaBridge 167:84c0a372a020 106 * @ref MXC_Error_Codes "error code" if unsuccessful.
AnnaBridge 167:84c0a372a020 107 *
AnnaBridge 167:84c0a372a020 108 */
AnnaBridge 167:84c0a372a020 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);
AnnaBridge 167:84c0a372a020 110
AnnaBridge 167:84c0a372a020 111 /**
AnnaBridge 167:84c0a372a020 112 * @brief Shutdown I2CS module.
AnnaBridge 167:84c0a372a020 113 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 114 * @return #E_NO_ERROR if everything is successful or an
AnnaBridge 167:84c0a372a020 115 * @ref MXC_Error_Codes "error code" if unsuccessful.
AnnaBridge 167:84c0a372a020 116 */
AnnaBridge 167:84c0a372a020 117 int I2CS_Shutdown(mxc_i2cs_regs_t *i2cs);
AnnaBridge 167:84c0a372a020 118
AnnaBridge 167:84c0a372a020 119 /**
AnnaBridge 167:84c0a372a020 120 * @brief I2CS interrupt handler.
AnnaBridge 167:84c0a372a020 121 * @details This function should be called by the application from the
AnnaBridge 167:84c0a372a020 122 * interrupt handler if I2CS interrupts are enabled. Alternately,
AnnaBridge 167:84c0a372a020 123 * this function can be periodically called by the application if
AnnaBridge 167:84c0a372a020 124 * I2CS interrupts are disabled.
AnnaBridge 167:84c0a372a020 125 *
AnnaBridge 167:84c0a372a020 126 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 127 */
AnnaBridge 167:84c0a372a020 128 void I2CS_Handler(mxc_i2cs_regs_t *i2cs);
AnnaBridge 167:84c0a372a020 129
AnnaBridge 167:84c0a372a020 130 /**
AnnaBridge 167:84c0a372a020 131 * @brief Register a callback that is triggered by an update of a specified
AnnaBridge 167:84c0a372a020 132 * byte.
AnnaBridge 167:84c0a372a020 133 * @details Registering a callback causes the slave to interrupt when the
AnnaBridge 167:84c0a372a020 134 * master has updated a specified byte.
AnnaBridge 167:84c0a372a020 135 *
AnnaBridge 167:84c0a372a020 136 * @param i2cs Pointer to the I2CS register structure, see
AnnaBridge 167:84c0a372a020 137 * #mxc_i2cs_regs_t.
AnnaBridge 167:84c0a372a020 138 * @param addr Index to trigger a call to the #i2cs_callback_fn.
AnnaBridge 167:84c0a372a020 139 * @param callback callback function of type #i2cs_callback_fn to be called
AnnaBridge 167:84c0a372a020 140 * when the addr being written by the master matches \c addr.
AnnaBridge 167:84c0a372a020 141 */
AnnaBridge 167:84c0a372a020 142 void I2CS_RegisterCallback(mxc_i2cs_regs_t *i2cs, uint8_t addr, i2cs_callback_fn callback);
AnnaBridge 167:84c0a372a020 143
AnnaBridge 167:84c0a372a020 144 /**
AnnaBridge 167:84c0a372a020 145 * @brief Write I2CS data to a given byte.
AnnaBridge 167:84c0a372a020 146 * @details The slave has a buffer of registers that the external master can
AnnaBridge 167:84c0a372a020 147 * read. Use this function to write data into a specified
AnnaBridge 167:84c0a372a020 148 * address/index.
AnnaBridge 167:84c0a372a020 149 *
AnnaBridge 167:84c0a372a020 150 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 151 * @param addr Address/Index to write.
AnnaBridge 167:84c0a372a020 152 * @param data Data to be written.
AnnaBridge 167:84c0a372a020 153 */
AnnaBridge 167:84c0a372a020 154 __STATIC_INLINE void I2CS_Write(mxc_i2cs_regs_t *i2cs, uint8_t addr, uint8_t data)
AnnaBridge 167:84c0a372a020 155 {
AnnaBridge 167:84c0a372a020 156 // Make sure we don't overflow
AnnaBridge 167:84c0a372a020 157 MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE);
AnnaBridge 167:84c0a372a020 158 i2cs->data_byte[addr] = ((i2cs->data_byte[addr] & ~MXC_F_I2CS_DATA_BYTE_DATA_FIELD) |
AnnaBridge 167:84c0a372a020 159 (data << MXC_F_I2CS_DATA_BYTE_DATA_FIELD_POS));
AnnaBridge 167:84c0a372a020 160 }
AnnaBridge 167:84c0a372a020 161
AnnaBridge 167:84c0a372a020 162 /**
AnnaBridge 167:84c0a372a020 163 * @brief Read I2CS data from a given address .
AnnaBridge 167:84c0a372a020 164 * @details The slave has a buffer of registers that the external master can
AnnaBridge 167:84c0a372a020 165 * read. Use this function to read the data from the registers.
AnnaBridge 167:84c0a372a020 166 *
AnnaBridge 167:84c0a372a020 167 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 168 * @param addr Address/Index to read from.
AnnaBridge 167:84c0a372a020 169 *
AnnaBridge 167:84c0a372a020 170 * @return Data contained in requested @c addr register.
AnnaBridge 167:84c0a372a020 171 */
AnnaBridge 167:84c0a372a020 172 __STATIC_INLINE uint8_t I2CS_Read(mxc_i2cs_regs_t *i2cs, uint8_t addr)
AnnaBridge 167:84c0a372a020 173 {
AnnaBridge 167:84c0a372a020 174 // Make sure we don't overflow
AnnaBridge 167:84c0a372a020 175 MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE);
AnnaBridge 167:84c0a372a020 176 return ((i2cs->data_byte[addr] & MXC_F_I2CS_DATA_BYTE_DATA_FIELD) >>
AnnaBridge 167:84c0a372a020 177 MXC_F_I2CS_DATA_BYTE_DATA_FIELD_POS);
AnnaBridge 167:84c0a372a020 178 }
AnnaBridge 167:84c0a372a020 179
AnnaBridge 167:84c0a372a020 180 /**
AnnaBridge 167:84c0a372a020 181 * @brief Set the given index to read only (RO).
AnnaBridge 167:84c0a372a020 182 * @details This index will be flagged as read only. The slave will NACK the
AnnaBridge 167:84c0a372a020 183 * master if it attempts to write this location. Multiple calls with
AnnaBridge 167:84c0a372a020 184 * different index/address values will yield multiple read-only
AnnaBridge 167:84c0a372a020 185 * locations within the slave register set.
AnnaBridge 167:84c0a372a020 186 *
AnnaBridge 167:84c0a372a020 187 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 188 * @param addr Address/Index of the byte to set to RO.
AnnaBridge 167:84c0a372a020 189 */
AnnaBridge 167:84c0a372a020 190 __STATIC_INLINE void I2CS_SetRO(mxc_i2cs_regs_t *i2cs, uint8_t addr)
AnnaBridge 167:84c0a372a020 191 {
AnnaBridge 167:84c0a372a020 192 // Make sure we don't overflow
AnnaBridge 167:84c0a372a020 193 MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE);
AnnaBridge 167:84c0a372a020 194 i2cs->data_byte[addr] |= MXC_F_I2CS_DATA_BYTE_READ_ONLY_FL;
AnnaBridge 167:84c0a372a020 195 }
AnnaBridge 167:84c0a372a020 196
AnnaBridge 167:84c0a372a020 197 /**
AnnaBridge 167:84c0a372a020 198 * @brief Sets the given address to R/W.
AnnaBridge 167:84c0a372a020 199 * @param i2cs Pointer to I2CS regs.
AnnaBridge 167:84c0a372a020 200 * @param addr Index to start clearing RO flag.
AnnaBridge 167:84c0a372a020 201 */
AnnaBridge 167:84c0a372a020 202 __STATIC_INLINE void I2CS_ClearRO(mxc_i2cs_regs_t *i2cs, uint8_t addr)
AnnaBridge 167:84c0a372a020 203 {
AnnaBridge 167:84c0a372a020 204 // Make sure we don't overflow
AnnaBridge 167:84c0a372a020 205 MXC_ASSERT(addr < MXC_CFG_I2CS_BUFFER_SIZE);
AnnaBridge 167:84c0a372a020 206 i2cs->data_byte[addr] &= ~MXC_F_I2CS_DATA_BYTE_READ_ONLY_FL;
AnnaBridge 167:84c0a372a020 207 }
AnnaBridge 167:84c0a372a020 208
AnnaBridge 167:84c0a372a020 209 /**@} end of group i2cs */
AnnaBridge 167:84c0a372a020 210
AnnaBridge 167:84c0a372a020 211 #ifdef __cplusplus
AnnaBridge 167:84c0a372a020 212 }
AnnaBridge 167:84c0a372a020 213 #endif
AnnaBridge 167:84c0a372a020 214
AnnaBridge 167:84c0a372a020 215 #endif /* _I2CS_H_ */