test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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