MacroRat / MouseCode

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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